This commit is contained in:
Timothy Jaeryang Baek committed 2026-06-15 11:41:18 +02:00
1 parent 0c412f0d70
commit ed11c3cd6c
8 files changed
+325 -28

No files matched your search

+9
View File
@@ -500,6 +500,9 @@ def _to_responses_input(messages: list[dict], instructions: str) -> list[dict]:
}
)
elif role == "assistant" and m.get("tool_calls"):
# Emit reasoning items before function calls (required by reasoning models)
for ri in m.get("reasoning_items", []):
items.append(ri)
for tc in m["tool_calls"]:
args = tc["function"].get("arguments", "{}")
call_id = tc.get("id", "")
@@ -604,6 +607,12 @@ async def stream_openai_responses(
"name": item["name"],
"arguments": json.loads(item["arguments"]),
}
elif item["type"] == "reasoning":
# Reasoning items must be round-tripped for reasoning models
yield {
"type": "reasoning",
"item": item,
}
elif etype == "response.failed":
error = event.get("response", {}).get("error", {})
+45 -21
View File
@@ -597,6 +597,7 @@ async def _load_message_history(chat_id: str, message_id: str) -> tuple[list[dic
# Reconstruct tool calls from output items for the provider
if m.output:
tool_calls = []
reasoning_items = []
for item in m.output:
if item.get("type") == "function_call" and item.get("status") == "completed":
tc = {
@@ -611,8 +612,12 @@ async def _load_message_history(chat_id: str, message_id: str) -> tuple[list[dic
if item.get("fc_id"):
tc["fc_id"] = item["fc_id"]
tool_calls.append(tc)
elif item.get("type") == "reasoning":
reasoning_items.append(item)
if tool_calls:
entry["tool_calls"] = tool_calls
if reasoning_items:
entry["reasoning_items"] = reasoning_items
# Add tool results as separate messages
for item in m.output:
@@ -644,7 +649,10 @@ def _parse_image_data_uri(result: str) -> tuple[str, str] | None:
return None
def _append_tool_to_messages(messages: list[dict], event: dict, result: str, provider: str):
def _append_tool_to_messages(
messages: list[dict], event: dict, result: str, provider: str,
reasoning_items: list[dict] | None = None,
):
"""Append a tool call + result to the message history for the next API call."""
# Check for image result before truncation (data URI is large but needed)
image = _parse_image_data_uri(result)
@@ -656,23 +664,25 @@ def _append_tool_to_messages(messages: list[dict], event: dict, result: str, pro
result = result[:half] + "\n\n...(truncated)...\n\n" + result[-half:]
# Add assistant message with tool_call
messages.append(
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": event["call_id"],
"fc_id": event.get("id", ""),
"type": "function",
"function": {
"name": event["name"],
"arguments": json.dumps(event["arguments"]),
},
}
],
}
)
assistant_msg = {
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": event["call_id"],
"fc_id": event.get("id", ""),
"type": "function",
"function": {
"name": event["name"],
"arguments": json.dumps(event["arguments"]),
},
}
],
}
# Attach reasoning items for round-tripping (Responses API reasoning models)
if reasoning_items:
assistant_msg["reasoning_items"] = reasoning_items
messages.append(assistant_msg)
if image:
# Structured multimodal content — provider converters handle the
@@ -1029,6 +1039,7 @@ async def run_chat_task(
restart = False
pending_calls: list[dict] = [] # Collect tool calls from this response
pending_reasoning: list[dict] = [] # Reasoning items for round-tripping
async for event in stream:
if event["type"] == "text_delta":
@@ -1041,6 +1052,10 @@ async def run_chat_task(
# Collect tool call — don't execute yet
pending_calls.append(event)
elif event["type"] == "reasoning":
# Collect reasoning items for round-tripping with tool calls
pending_reasoning.append(event["item"])
elif event["type"] == "usage":
usage = {k: v for k, v in event.items() if k != "type"}
if "total_tokens" not in usage:
@@ -1102,6 +1117,9 @@ async def run_chat_task(
if needs_approval:
# First non-auto tool stops the loop for approval
tc = needs_approval
# Store reasoning items before function_call for round-tripping
for ri in pending_reasoning:
output_items.append(ri)
item = {
"type": "function_call",
"id": str(uuid.uuid4()),
@@ -1125,7 +1143,9 @@ async def run_chat_task(
await emit(done=True)
return
# All calls are auto-approved — build UI items
# All calls are auto-approved — store reasoning + build UI items
for ri in pending_reasoning:
output_items.append(ri)
call_items: list[tuple[dict, dict]] = [] # (event, ui_item)
for tc in pending_calls:
item = {
@@ -1176,7 +1196,9 @@ async def run_chat_task(
await emit(output=artifact_item)
_sync_state()
_append_tool_to_messages(messages, tc, result, provider)
# Only attach reasoning to the first tool call message
ri = pending_reasoning if idx == other_indices[0] else None
_append_tool_to_messages(messages, tc, result, provider, reasoning_items=ri)
new_messages_since += 2
# Execute delegate_task calls concurrently, emit each as it completes
@@ -1212,7 +1234,9 @@ async def run_chat_task(
await emit(output=item)
await emit(output=result_item)
_sync_state()
_append_tool_to_messages(messages, tc, result, provider)
# Attach reasoning to first delegate call if no sequential calls consumed it
ri = pending_reasoning if not other_indices and idx == delegate_indices[0] else None
_append_tool_to_messages(messages, tc, result, provider, reasoning_items=ri)
new_messages_since += 2
# Persist after all tool calls
+28 -2
View File
@@ -420,15 +420,41 @@ async def pull(root: str) -> dict[str, Any]:
return {"ok": code == 0, "message": (out + err).strip()}
async def push(root: str, force: bool = False) -> dict[str, Any]:
"""Push to remote."""
async def push(
root: str,
force: bool = False,
set_upstream: bool = False,
branch: str | None = None,
remote: str = "origin",
) -> dict[str, Any]:
"""Push to remote. Use *set_upstream* for first-time branch publish."""
args = ["push"]
if set_upstream:
args.extend(["-u", remote, branch or "HEAD"])
if force:
args.append("--force-with-lease")
code, out, err = await _run(*args, cwd=root, check=False)
return {"ok": code == 0, "message": (out + err).strip()}
async def uncommit(root: str) -> dict[str, str]:
"""Undo the last commit, moving its changes back to the staging area.
Uses ``git reset --soft HEAD~1``.
"""
# Grab info about the commit we're about to undo
_, log_out, _ = await _run(
"log", "-1", "--format=%H%x00%h%x00%s", cwd=root, check=False
)
parts = log_out.strip().split("\x00")
undone_hash = parts[1] if len(parts) >= 2 else ""
undone_msg = parts[2] if len(parts) >= 3 else ""
await _run("reset", "--soft", "HEAD~1", cwd=root)
return {"hash": undone_hash, "message": undone_msg}
async def stash_list(root: str) -> list[dict[str, str]]:
"""List stashes."""
_, out, _ = await _run("stash", "list", "--format=%gd%x00%s", cwd=root, check=False)