YET ANOTEHR BOTCHED TRACK.
This commit is contained in:
@@ -679,28 +679,56 @@ def _send_gemini(md_content: str, user_message: str, base_dir: str,
|
||||
for r_idx in range(MAX_TOOL_ROUNDS + 2):
|
||||
events.emit("request_start", payload={"provider": "gemini", "model": _model, "round": r_idx})
|
||||
if stream_callback:
|
||||
resp = _gemini_chat.send_message_stream(payload)
|
||||
# In 1.0.0, we use send_message with stream=True
|
||||
config = types.GenerateContentConfig(
|
||||
tools=[types.Tool(function_declarations=[types.FunctionDeclaration(**s) for s in mcp_client.get_tool_schemas()])] if enable_tools else [],
|
||||
temperature=_temperature,
|
||||
max_output_tokens=_max_tokens,
|
||||
)
|
||||
resp = _gemini_chat.send_message(payload, config=config, stream=True)
|
||||
txt_chunks: list[str] = []
|
||||
calls = []
|
||||
usage = {}
|
||||
reason = "STOP"
|
||||
final_resp = None
|
||||
for chunk in resp:
|
||||
c_txt = chunk.text
|
||||
if c_txt:
|
||||
txt_chunks.append(c_txt)
|
||||
stream_callback(c_txt)
|
||||
if chunk.text:
|
||||
txt_chunks.append(chunk.text)
|
||||
stream_callback(chunk.text)
|
||||
if chunk.candidates:
|
||||
c = chunk.candidates[0]
|
||||
if c.content and c.content.parts:
|
||||
calls.extend([p.function_call for p in c.content.parts if p.function_call])
|
||||
if hasattr(c, "finish_reason") and c.finish_reason:
|
||||
reason = c.finish_reason.name
|
||||
if chunk.usage_metadata:
|
||||
usage = {
|
||||
"input_tokens": chunk.usage_metadata.prompt_token_count,
|
||||
"output_tokens": chunk.usage_metadata.candidates_token_count,
|
||||
"total_tokens": chunk.usage_metadata.total_token_count,
|
||||
"cache_read_input_tokens": getattr(chunk.usage_metadata, "cached_content_token_count", 0)
|
||||
}
|
||||
final_resp = chunk
|
||||
txt = "".join(txt_chunks)
|
||||
calls = [p.function_call for c in resp.candidates if getattr(c, "content", None) for p in c.content.parts if hasattr(p, "function_call") and p.function_call]
|
||||
usage = {"input_tokens": getattr(resp.usage_metadata, "prompt_token_count", 0), "output_tokens": getattr(resp.usage_metadata, "candidates_token_count", 0)}
|
||||
cached_tokens = getattr(resp.usage_metadata, "cached_content_token_count", None)
|
||||
if cached_tokens: usage["cache_read_input_tokens"] = cached_tokens
|
||||
# Final validation of response object for subsequent code
|
||||
resp = final_resp
|
||||
else:
|
||||
resp = _gemini_chat.send_message(payload)
|
||||
txt = "\n".join(p.text for c in resp.candidates if getattr(c, "content", None) for p in c.content.parts if hasattr(p, "text") and p.text)
|
||||
calls = [p.function_call for c in resp.candidates if getattr(c, "content", None) for p in c.content.parts if hasattr(p, "function_call") and p.function_call]
|
||||
usage = {"input_tokens": getattr(resp.usage_metadata, "prompt_token_count", 0), "output_tokens": getattr(resp.usage_metadata, "candidates_token_count", 0)}
|
||||
cached_tokens = getattr(resp.usage_metadata, "cached_content_token_count", None)
|
||||
if cached_tokens: usage["cache_read_input_tokens"] = cached_tokens
|
||||
if txt: all_text.append(txt)
|
||||
events.emit("response_received", payload={"provider": "gemini", "model": _model, "usage": usage, "round": r_idx})
|
||||
reason = resp.candidates[0].finish_reason.name if resp.candidates and hasattr(resp.candidates[0], "finish_reason") else "STOP"
|
||||
config = types.GenerateContentConfig(
|
||||
tools=[types.Tool(function_declarations=[types.FunctionDeclaration(**s) for s in mcp_client.get_tool_schemas()])] if enable_tools else [],
|
||||
temperature=_temperature,
|
||||
max_output_tokens=_max_tokens,
|
||||
)
|
||||
resp = _gemini_chat.send_message(payload, config=config)
|
||||
txt = resp.text or ""
|
||||
calls = [p.function_call for c in resp.candidates if getattr(c, "content", None) for p in c.content.parts if p.function_call]
|
||||
usage = {
|
||||
"input_tokens": getattr(resp.usage_metadata, "prompt_token_count", 0),
|
||||
"output_tokens": getattr(resp.usage_metadata, "candidates_token_count", 0),
|
||||
"total_tokens": getattr(resp.usage_metadata, "total_token_count", 0),
|
||||
"cache_read_input_tokens": getattr(resp.usage_metadata, "cached_content_token_count", 0)
|
||||
}
|
||||
reason = resp.candidates[0].finish_reason.name if (resp.candidates and hasattr(resp.candidates[0], "finish_reason")) else "STOP"
|
||||
|
||||
_append_comms("IN", "response", {"round": r_idx, "stop_reason": reason, "text": txt, "tool_calls": [{"name": c.name, "args": dict(c.args)} for c in calls], "usage": usage})
|
||||
total_in = usage.get("input_tokens", 0)
|
||||
if total_in > _GEMINI_MAX_INPUT_TOKENS * 0.4 and _gemini_chat and _get_gemini_history_list(_gemini_chat):
|
||||
|
||||
Reference in New Issue
Block a user