finish openrouter tool parsing

This commit is contained in:
Shivering Ocelot committed 2026-06-30 10:58:56 -05:00
1 parent c71b0a9642
commit 878a09f9e9
1 file changed
+7 -14
+7 -14
View File
@@ -72,6 +72,7 @@ model_cache = {}
last_model_file = None last_model_file = None
last_model_instance = None last_model_instance = None
# Wrap an async function in a queue to send it safely from another thread
def safe_queue(callback): def safe_queue(callback):
queue = asyncio.Queue() queue = asyncio.Queue()
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
@@ -348,9 +349,7 @@ async def openrouter_chat_completion(app, messages, model, tools=[], opts={}, ag
# OpenAI format streams tool calls in parts # OpenAI format streams tool calls in parts
# We could, but I won't assume any tool call is complete until the message is over # We could, but I won't assume any tool call is complete until the message is over
# TODO: tool_call_by_index = {}
tool_calls = [] tool_calls = []
last_tool_call = None
async for event in stream_response: async for event in stream_response:
if agent_run and agent_run.cancelled: if agent_run and agent_run.cancelled:
@@ -368,24 +367,18 @@ async def openrouter_chat_completion(app, messages, model, tools=[], opts={}, ag
if delta.tool_calls: if delta.tool_calls:
print("[openrouter] tool call:" + repr(delta.tool_calls)) print("[openrouter] tool call:" + repr(delta.tool_calls))
for tc in delta.tool_calls: for tc in delta.tool_calls:
if not last_tool_call: if tc.index >= len(tool_calls):
last_tool_call = ToolCall(None, arguments="") tool_calls.append(ToolCall(tool_name="", arguments="", id=""))
tool_calls.append(last_tool_call) tool_call = tool_calls[tc.index]
if tc.id: if tc.id:
if last_tool_call.id is None: tool_call.id += tc.id
last_tool_call.id = tc.id
elif last_tool_call.id != tc.id:
# New id = new tool
# TODO: WRONG! new id must append value to accumulating id. new index signals new tool
last_tool_call = ToolCall(None, arguments="", id=tc.id)
tool_calls.append(last_tool_call)
if tc.function: if tc.function:
if tc.function.name: if tc.function.name:
last_tool_call.tool_name = tc.function.name tool_call.tool_name += tc.function.name
if tc.function.arguments: if tc.function.arguments:
last_tool_call.arguments += tc.function.arguments tool_call.arguments += tc.function.arguments
else: else:
print("[openrouter] no event choices: " + repr(event)) print("[openrouter] no event choices: " + repr(event))