Fix agent tool support, fix many other bugs

This commit is contained in:
Shivering Ocelot committed 2026-08-23 00:36:37 -05:00
1 parent 2b44dcebf4
commit 6c5140fb92
5 files changed
+79 -56

No files matched your search

+51 -47
View File
@@ -57,7 +57,7 @@ def format_openai(messages):
formatted.append({
'role': 'tool',
'tool_call_id': msg.content.id,
'content': json.dumps(msg.content.output),
'content': json.dumps(msg.content.result),
})
else:
fmsg = {'role': msg.role, 'content': msg.content}
@@ -337,6 +337,9 @@ async def openrouter_chat_completion(app, messages, model, tools=[], opts={}, ag
'model': model,
'messages': format_openai(messages),
'stream': True,
# Options
'timeout_ms': 45_000,
'retries': 3,
}
# Pass tools in OpenAI-compatible format if provided
@@ -344,63 +347,64 @@ async def openrouter_chat_completion(app, messages, model, tools=[], opts={}, ag
if formatted_tools:
kwargs['tools'] = formatted_tools
try:
stream_response = await app.openrouter_client.chat.send_async(**kwargs)
stream_response = await app.openrouter_client.chat.send_async(**kwargs)
# OpenAI format streams tool calls in parts
# We could, but I won't assume any tool call is complete until the message is over
tool_calls = []
# OpenAI format streams tool calls in parts
# We could, but I won't assume any tool call is complete until the message is over
tool_calls = []
async for event in stream_response:
if agent_run and agent_run.cancelled:
print(f"Cancelling openrouter inference for run {agent_run.id}")
# TODO: Need to cancell?
break
if event.choices and len(event.choices) > 0:
delta = event.choices[0].delta
async for event in stream_response:
if agent_run and agent_run.cancelled:
print(f"Cancelling openrouter inference for run {agent_run.id}")
# TODO: Need to cancell?
break
if event.choices and len(event.choices) > 0:
delta = event.choices[0].delta
if delta.content:
emit('output', delta.content)
if delta.reasoning:
emit('reasoning', delta.reasoning)
if delta.tool_calls:
print("[openrouter] tool call:" + repr(delta.tool_calls))
for tc in delta.tool_calls:
if tc.index >= len(tool_calls):
tool_calls.append(ToolCall(tool_name="", arguments="", id=""))
tool_call = tool_calls[tc.index]
if delta.content:
emit('output', delta.content)
if delta.reasoning:
emit('reasoning', delta.reasoning)
if delta.tool_calls:
print("[openrouter] tool call:" + repr(delta.tool_calls))
for tc in delta.tool_calls:
if tc.index >= len(tool_calls):
tool_calls.append(ToolCall(tool_name="", arguments="", id=""))
tool_call = tool_calls[tc.index]
if tc.id:
tool_call.id += tc.id
if tc.id:
tool_call.id += tc.id
if tc.function:
if tc.function.name:
tool_call.tool_name += tc.function.name
if tc.function.arguments:
tool_call.arguments += tc.function.arguments
else:
print("[openrouter] no event choices: " + repr(event))
if tc.function:
if tc.function.name:
tool_call.tool_name += tc.function.name
if tc.function.arguments:
tool_call.arguments += tc.function.arguments
else:
print("[openrouter] no event choices: " + repr(event))
for tc in tool_calls:
try:
arguments = json.loads(tc.arguments)
tc.arguments = arguments
emit('tool-call', tc)
except Exception:
print("[openrouter] Bad tool call arguments for " + repr(tc))
print(traceback.format_exc())
except asyncio.CancelledError:
print(f"[openrouter] task cancelled for run {agent_run.id}")
raise
except Exception:
print(f"[openrouter] exception during inference")
print(traceback.format_exc())
for tc in tool_calls:
try:
arguments = json.loads(tc.arguments)
tc.arguments = arguments
emit('tool-call', tc)
except Exception:
print("[openrouter] Bad tool call arguments for " + repr(tc))
print(traceback.format_exc())
emit('close')
print("END OPENROUTER INFERENCE")
def handle_done(task):
try:
task.result()
except Exception as e:
print(f"[openrouter] Worker error: {e}")
print(traceback.format_exc())
task = asyncio.create_task(worker())
task.add_done_callback(handle_done)
agent_run.task = task