Compare commits

...

4 Commits

Author SHA1 Message Date
copilot-swe-agent[bot]
1e0275ee08 Improve code clarity and add documentation about in-place modification
Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com>
2026-01-30 15:53:40 +00:00
copilot-swe-agent[bot]
3fa7389ba9 Add defensive checks for thought_signature modification
Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com>
2026-01-30 15:49:25 +00:00
copilot-swe-agent[bot]
84a3b1f465 Add thought_signature to Gemini function calls for API compatibility
Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com>
2026-01-30 15:47:56 +00:00
copilot-swe-agent[bot]
377d011da2 Initial plan 2026-01-30 15:44:33 +00:00

View File

@@ -20,6 +20,24 @@ class GeminiChatCompletions(chatcmpl.OpenAIChatCompletions):
'timeout': 120, 'timeout': 120,
} }
def _add_thought_signature_to_messages(self, messages: list[dict]) -> list[dict]:
"""Add thought_signature to tool_calls in messages for Gemini API compatibility
Gemini API requires a thought_signature field in function call parts.
See: https://ai.google.dev/gemini-api/docs/thought-signatures
Note: This function modifies the dictionaries in the messages list in place.
"""
for msg in messages:
if 'tool_calls' in msg and msg['tool_calls']:
# Ensure we're working with a mutable copy of tool_calls
if not isinstance(msg['tool_calls'], list):
continue
for tool_call in msg['tool_calls']:
if isinstance(tool_call, dict) and 'thought_signature' not in tool_call:
tool_call['thought_signature'] = ''
return messages
async def _closure_stream( async def _closure_stream(
self, self,
query: pipeline_query.Query, query: pipeline_query.Query,
@@ -42,6 +60,9 @@ class GeminiChatCompletions(chatcmpl.OpenAIChatCompletions):
# 设置此次请求中的messages # 设置此次请求中的messages
messages = req_messages.copy() messages = req_messages.copy()
# Add thought_signature to tool_calls for Gemini compatibility
messages = self._add_thought_signature_to_messages(messages)
# 检查vision # 检查vision
for msg in messages: for msg in messages:
if 'content' in msg and isinstance(msg['content'], list): if 'content' in msg and isinstance(msg['content'], list):
@@ -140,3 +161,29 @@ class GeminiChatCompletions(chatcmpl.OpenAIChatCompletions):
yield provider_message.MessageChunk(**chunk_data) yield provider_message.MessageChunk(**chunk_data)
chunk_idx += 1 chunk_idx += 1
async def _closure(
self,
query: pipeline_query.Query,
req_messages: list[dict],
use_model: requester.RuntimeLLMModel,
use_funcs: list[resource_tool.LLMTool] = None,
extra_args: dict[str, typing.Any] = {},
remove_think: bool = False,
) -> tuple[provider_message.Message, dict]:
"""Override _closure to add thought_signature to messages"""
# Make a shallow copy to avoid mutating the caller's list
messages = req_messages.copy()
# Add thought_signature to tool_calls for Gemini compatibility
messages = self._add_thought_signature_to_messages(messages)
# Call parent implementation
return await super()._closure(
query=query,
req_messages=messages,
use_model=use_model,
use_funcs=use_funcs,
extra_args=extra_args,
remove_think=remove_think,
)