diff --git a/backend/src/inference.py b/backend/src/inference.py index bb16f55..5860fff 100644 --- a/backend/src/inference.py +++ b/backend/src/inference.py @@ -72,6 +72,7 @@ model_cache = {} last_model_file = None last_model_instance = None +# Wrap an async function in a queue to send it safely from another thread def safe_queue(callback): queue = asyncio.Queue() 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 # We could, but I won't assume any tool call is complete until the message is over - # TODO: tool_call_by_index = {} tool_calls = [] - last_tool_call = None async for event in stream_response: 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: print("[openrouter] tool call:" + repr(delta.tool_calls)) for tc in delta.tool_calls: - if not last_tool_call: - last_tool_call = ToolCall(None, arguments="") - tool_calls.append(last_tool_call) + if tc.index >= len(tool_calls): + tool_calls.append(ToolCall(tool_name="", arguments="", id="")) + tool_call = tool_calls[tc.index] if tc.id: - if last_tool_call.id is None: - 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) + tool_call.id += tc.id if tc.function: if tc.function.name: - last_tool_call.tool_name = tc.function.name + tool_call.tool_name += tc.function.name if tc.function.arguments: - last_tool_call.arguments += tc.function.arguments + tool_call.arguments += tc.function.arguments else: print("[openrouter] no event choices: " + repr(event))