diff options
| author | t <t@tjp.lol> | 2026-06-13 23:45:59 -0600 |
|---|---|---|
| committer | t <t@tjp.lol> | 2026-06-15 15:08:32 -0600 |
| commit | 02b4c7a35ac0b714bc045d54fb4bb45d1ce4e490 (patch) | |
| tree | 134196a82dd6fdd55b735be4c0cf38428c85038d /libpanto | |
| parent | 71643a5d69ffc40882c9fcde3cc8a3bcf02d7396 (diff) | |
Add Codex Responses support and session debugging
Teach provider config and auth resolution about the Codex Responses
dialect, including user-facing `style = "openai_responses"` with
`dialect = "codex"`. Serialize and parse Responses traffic with
provider-specific reasoning replay, assistant phase metadata, and robust
function-call assembly keyed by `output_index` so streamed tool inputs
survive proxy quirks and empty terminal payloads.
Also persist thinking origins and message metadata across sessions, add
the Anthropic interleaved-thinking header switch, write per-session
debug logs, and improve the TUI and scripts for inspecting tool output
and session costs.
Diffstat (limited to 'libpanto')
| -rw-r--r-- | libpanto/src/agent.zig | 11 | ||||
| -rw-r--r-- | libpanto/src/anthropic_messages_json.zig | 54 | ||||
| -rw-r--r-- | libpanto/src/config.zig | 10 | ||||
| -rw-r--r-- | libpanto/src/conversation.zig | 60 | ||||
| -rw-r--r-- | libpanto/src/file_system_jsonl_store.zig | 59 | ||||
| -rw-r--r-- | libpanto/src/openai_responses_json.zig | 319 | ||||
| -rw-r--r-- | libpanto/src/provider.zig | 12 | ||||
| -rw-r--r-- | libpanto/src/provider_anthropic_messages.zig | 37 | ||||
| -rw-r--r-- | libpanto/src/provider_openai_chat.zig | 12 | ||||
| -rw-r--r-- | libpanto/src/provider_openai_responses.zig | 626 | ||||
| -rw-r--r-- | libpanto/src/public.zig | 1 |
11 files changed, 1123 insertions, 78 deletions
diff --git a/libpanto/src/agent.zig b/libpanto/src/agent.zig index 1da0261..0ff231a 100644 --- a/libpanto/src/agent.zig +++ b/libpanto/src/agent.zig @@ -78,7 +78,12 @@ fn cloneMessage(alloc: Allocator, msg: conversation.Message) !conversation.Messa for (msg.content.items) |block| { content.appendAssumeCapacity(try cloneBlock(alloc, block)); } - return .{ .role = msg.role, .content = content, .usage = msg.usage }; + return .{ + .role = msg.role, + .content = content, + .usage = msg.usage, + .metadata = if (msg.metadata) |m| try alloc.dupe(u8, m) else null, + }; } fn cloneBlock(alloc: Allocator, block: conversation.ContentBlock) !conversation.ContentBlock { @@ -91,7 +96,9 @@ fn cloneBlock(alloc: Allocator, block: conversation.ContentBlock) !conversation. mut.deinit(alloc); } const sig: ?[]const u8 = if (b.signature) |s| try alloc.dupe(u8, s) else null; - break :blk .{ .Thinking = .{ .text = tb, .signature = sig } }; + errdefer if (sig) |s| alloc.free(s); + const origin: ?conversation.SignatureOrigin = if (b.signature_origin) |o| try o.dupe(alloc) else null; + break :blk .{ .Thinking = .{ .text = tb, .signature = sig, .signature_origin = origin } }; }, .ToolUse => |b| blk: { const id = try alloc.dupe(u8, b.id); diff --git a/libpanto/src/anthropic_messages_json.zig b/libpanto/src/anthropic_messages_json.zig index f7df80b..1b1cf6e 100644 --- a/libpanto/src/anthropic_messages_json.zig +++ b/libpanto/src/anthropic_messages_json.zig @@ -138,7 +138,7 @@ pub fn serializeRequest( for (window, 0..) |msg, mi| { if (msg.role == .system) continue; const mark: ?usize = if (cache_at) |c| (if (c.message == mi) c.block else null) else null; - try writeMessage(&s, msg, mark); + try writeMessage(&s, msg, mark, cfg); } try s.endArray(); @@ -251,6 +251,7 @@ fn writeMessage( s: *std.json.Stringify, msg: conversation.Message, cache_block: ?usize, + cfg: *const config_mod.AnthropicMessagesConfig, ) !void { try s.beginObject(); @@ -261,7 +262,7 @@ fn writeMessage( try s.beginArray(); for (msg.content.items, 0..) |block, bi| { const mark = if (cache_block) |cb| cb == bi else false; - try writeBlock(s, block, mark); + try writeBlock(s, block, mark, cfg); } try s.endArray(); @@ -278,7 +279,12 @@ fn writeCacheControl(s: *std.json.Stringify) !void { try s.endObject(); } -fn writeBlock(s: *std.json.Stringify, block: conversation.ContentBlock, mark_cache: bool) !void { +fn writeBlock( + s: *std.json.Stringify, + block: conversation.ContentBlock, + mark_cache: bool, + cfg: *const config_mod.AnthropicMessagesConfig, +) !void { switch (block) { .Text => |tb| { try s.beginObject(); @@ -292,9 +298,14 @@ fn writeBlock(s: *std.json.Stringify, block: conversation.ContentBlock, mark_cac .Thinking => |tb| { // Anthropic requires the signature field to round-trip a thinking // block. If we don't have one (e.g. block was synthesized from a - // non-Anthropic provider), skip the block: Anthropic will reject - // unsigned thinking on incoming messages. + // non-Anthropic provider), or if the signature came from a + // different provider/model than the current request, skip the + // block: Anthropic will reject unsigned thinking on incoming + // messages, and opaque signatures are not portable across + // provider/model boundaries. const sig = tb.signature orelse return; + const origin = tb.signature_origin orelse return; + if (!origin.matches(.anthropic_messages, cfg.base_url, cfg.model)) return; try s.beginObject(); try s.objectField("type"); try s.write("thinking"); @@ -789,6 +800,7 @@ test "serializeRequest - cache breakpoint skips a trailing thinking block" { .signature = sig, } }, }, null); + try conversation.setThinkingOrigins(allocator, conv.messages.items[0].content.items, .anthropic_messages, "u", "claude-x"); var cfg = testConfig("claude-x"); var empty_tools = tool_registry_mod.ToolRegistry.init(allocator); @@ -935,6 +947,7 @@ test "serializeRequest - signed assistant Thinking blocks round-trip" { } }, .{ .Text = try conversation.textualBlockFromSlice(allocator, "the answer is 42") }, }, null); + try conversation.setThinkingOrigins(allocator, conv.messages.items[0].content.items, .anthropic_messages, "u", "claude-x"); const cfg = testConfig("claude-x"); var empty_tools = tool_registry_mod.ToolRegistry.init(allocator); @@ -962,6 +975,37 @@ test "serializeRequest - signed assistant Thinking blocks round-trip" { try testing.expectEqualStrings("the answer is 42", content[1].object.get("text").?.string); } +test "serializeRequest - mismatched thinking origin drops the block" { + const allocator = testing.allocator; + + var conv = conversation.Conversation.init(allocator); + defer conv.deinit(); + + const sig = try allocator.dupe(u8, "EqQBCgIYAhIM1gbcDa9GJwZA"); + try conv.addAssistantMessage(&.{ + .{ .Thinking = .{ + .text = try conversation.textualBlockFromSlice(allocator, "other provider thinking"), + .signature = sig, + } }, + .{ .Text = try conversation.textualBlockFromSlice(allocator, "answer") }, + }, null); + try conversation.setThinkingOrigins(allocator, conv.messages.items[0].content.items, .openai_responses, "https://api.individual.githubcopilot.com", "gpt-5.4-mini"); + + const cfg = testConfig("claude-x"); + var empty_tools = tool_registry_mod.ToolRegistry.init(allocator); + defer empty_tools.deinit(); + const body = try serializeRequest(allocator, &cfg, &conv, &empty_tools); + defer allocator.free(body); + + var parsed = try std.json.parseFromSlice(std.json.Value, allocator, body, .{}); + defer parsed.deinit(); + + const content = parsed.value.object.get("messages").?.array.items[0] + .object.get("content").?.array.items; + try testing.expectEqual(@as(usize, 1), content.len); + try testing.expectEqualStrings("text", content[0].object.get("type").?.string); +} + test "serializeRequest - unsigned Thinking blocks are dropped" { // Anthropic rejects thinking blocks without a valid signature on inbound // messages. If a block lacks one (e.g. from a different provider or an diff --git a/libpanto/src/config.zig b/libpanto/src/config.zig index c48cefc..7c8a495 100644 --- a/libpanto/src/config.zig +++ b/libpanto/src/config.zig @@ -27,6 +27,9 @@ pub const APIStyle = enum { /// OpenAI Responses API (`/responses`). Used by the ChatGPT-subscription /// Codex backend, whose request/stream shape differs from Chat Completions. openai_responses, + /// ChatGPT-subscription Codex Responses dialect. User config selects this + /// with `style = "openai_responses"` plus `dialect = "codex"`. + openai_codex_responses, }; /// A single HTTP request header (name/value). Used for provider @@ -155,6 +158,7 @@ pub const ProviderConfig = union(APIStyle) { openai_chat: OpenAIChatConfig, anthropic_messages: AnthropicMessagesConfig, openai_responses: OpenAIResponsesConfig, + openai_codex_responses: OpenAIResponsesConfig, pub fn style(self: ProviderConfig) APIStyle { return @as(APIStyle, self); @@ -187,6 +191,12 @@ pub const ProviderConfig = union(APIStyle) { .model = c.model, .reasoning = c.reasoning, }, + .openai_codex_responses => |c| .{ + .api_style = .openai_codex_responses, + .base_url = c.base_url, + .model = c.model, + .reasoning = c.reasoning, + }, }; } }; diff --git a/libpanto/src/conversation.zig b/libpanto/src/conversation.zig index 1942a0c..760f3ee 100644 --- a/libpanto/src/conversation.zig +++ b/libpanto/src/conversation.zig @@ -1,5 +1,6 @@ const std = @import("std"); const Allocator = std.mem.Allocator; +const config = @import("config.zig"); /// A streaming text buffer used by content blocks. /// Thin alias over ArrayList(u8) — amortized O(1) appends, @@ -13,19 +14,60 @@ pub fn textualBlockFromSlice(alloc: Allocator, slice: []const u8) !TextualBlock return buf; } +/// Provenance for a replayable thinking signature. Scoped conservatively to +/// the exact provider endpoint + wire model that produced the enclosing +/// assistant message. +pub const SignatureOrigin = struct { + api_style: config.APIStyle, + base_url: []const u8, + model: []const u8, + + pub fn init( + alloc: Allocator, + api_style: config.APIStyle, + base_url: []const u8, + model: []const u8, + ) !SignatureOrigin { + const burl = try alloc.dupe(u8, base_url); + errdefer alloc.free(burl); + const mdl = try alloc.dupe(u8, model); + return .{ .api_style = api_style, .base_url = burl, .model = mdl }; + } + + pub fn deinit(self: *SignatureOrigin, alloc: Allocator) void { + alloc.free(self.base_url); + alloc.free(self.model); + } + + pub fn dupe(self: SignatureOrigin, alloc: Allocator) !SignatureOrigin { + return init(alloc, self.api_style, self.base_url, self.model); + } + + pub fn matches(self: SignatureOrigin, api_style: config.APIStyle, base_url: []const u8, model: []const u8) bool { + return self.api_style == api_style and + std.mem.eql(u8, self.base_url, base_url) and + std.mem.eql(u8, self.model, model); + } +}; + /// A reasoning/thinking block from the assistant. /// /// `text` is the streamed reasoning content. `signature` is the opaque /// integrity token Anthropic emits with extended-thinking responses and /// requires echoed back verbatim on follow-up turns. It is optional because -/// other providers (OpenAI-compatible APIs) do not produce it. +/// other providers (OpenAI-compatible APIs) do not produce it. When present, +/// `signature_origin` records the exact provider/style/model that emitted the +/// enclosing assistant message so serializers can decide whether that opaque +/// signature is safe to replay on a later turn. pub const ThinkingBlock = struct { text: TextualBlock = .empty, signature: ?[]const u8 = null, + signature_origin: ?SignatureOrigin = null, pub fn deinit(self: *ThinkingBlock, alloc: Allocator) void { self.text.deinit(alloc); if (self.signature) |sig| alloc.free(sig); + if (self.signature_origin) |*origin| origin.deinit(alloc); } }; @@ -222,6 +264,7 @@ pub const Message = struct { block.deinit(alloc); } self.content.deinit(alloc); + if (self.metadata) |m| alloc.free(m); } }; @@ -335,6 +378,21 @@ pub const Conversation = struct { } }; +pub fn setThinkingOrigins( + allocator: Allocator, + blocks: []ContentBlock, + api_style: config.APIStyle, + base_url: []const u8, + model: []const u8, +) !void { + for (blocks) |*block| { + if (block.* != .Thinking) continue; + const origin = try SignatureOrigin.init(allocator, api_style, base_url, model); + if (block.Thinking.signature_origin) |*old| old.deinit(allocator); + block.Thinking.signature_origin = origin; + } +} + /// Derive the effective ordered list of system-text blocks from a slice of /// messages. This is the single shared rule that governs both provider /// serialization and session rebuild. diff --git a/libpanto/src/file_system_jsonl_store.zig b/libpanto/src/file_system_jsonl_store.zig index 1d56ae9..e47698c 100644 --- a/libpanto/src/file_system_jsonl_store.zig +++ b/libpanto/src/file_system_jsonl_store.zig @@ -709,7 +709,7 @@ pub const SessionFile = struct { for (self.entries.items) |entry| { switch (entry) { - .message => |me| try appendMessageToConv(&conv, self.allocator, me.message), + .message => |me| try appendMessageToConv(&conv, self.allocator, me.message, me.stamp), } } return conv; @@ -733,6 +733,7 @@ fn appendMessageToConv( conv: *conversation_mod.Conversation, allocator: Allocator, disk_msg: StoredMessage, + stamp: ?session_mod.WireStamp, ) !void { var content: std.ArrayList(conversation_mod.ContentBlock) = .empty; errdefer { @@ -756,6 +757,14 @@ fn appendMessageToConv( const tb = block.Text; block = .{ .System = .{ .text = tb, .mode = sys_mode } }; } + if (block == .Thinking and stamp != null) { + block.Thinking.signature_origin = try conversation_mod.SignatureOrigin.init( + allocator, + stamp.?.api_style, + stamp.?.base_url, + stamp.?.model, + ); + } content.appendAssumeCapacity(block); } const role: conversation_mod.MessageRole = switch (disk_msg.role) { @@ -2090,3 +2099,51 @@ test "FileSystemJSONLStore catalog: create → append → load round-trips" { try testing.expectEqual(@as(usize, 2), loaded.messages.items.len); try testing.expectEqual(conversation_mod.MessageRole.user, loaded.messages.items[0].role); } + +test "FileSystemJSONLStore load restores thinking signature origin from message stamp" { + const io = testing.io; + + var td = try TmpSessionDir.init(testing.allocator); + defer td.deinit(testing.allocator); + const sessions = try std.fs.path.join(testing.allocator, &.{ td.abs_path, "sessions" }); + defer testing.allocator.free(sessions); + + var catalog = try FileSystemJSONLStore.init(testing.allocator, io, sessions); + defer catalog.deinit(); + const store = catalog.store(); + + var sess = store.create(); + defer sess.info.deinit(testing.allocator); + + var conv = conversation_mod.Conversation.init(testing.allocator); + defer conv.deinit(); + try addUserText(&conv, "ping"); + try conv.addAssistantMessage(&.{ + .{ .Thinking = .{ + .text = try conversation_mod.textualBlockFromSlice(testing.allocator, "thinking..."), + .signature = try testing.allocator.dupe(u8, "sig123"), + } }, + .{ .Text = try conversation_mod.textualBlockFromSlice(testing.allocator, "pong") }, + }, null); + + const id: session_store_mod.WireIdentity = .{ + .api_style = .anthropic_messages, + .base_url = "https://api.anthropic.com", + .model = "claude-sonnet-4-20250514", + }; + var batch = [_]session_store_mod.PersistentMessage{ + .{ .message = conv.messages.items[0], .identity = id }, + .{ .message = conv.messages.items[1], .identity = id }, + }; + try sess.append(&batch); + + var loaded = (try store.load(sess.info.id)).?; + defer loaded.deinit(); + const thinking = loaded.messages.items[1].content.items[0].Thinking; + try testing.expect(thinking.signature_origin != null); + try testing.expect(thinking.signature_origin.?.matches( + .anthropic_messages, + "https://api.anthropic.com", + "claude-sonnet-4-20250514", + )); +} diff --git a/libpanto/src/openai_responses_json.zig b/libpanto/src/openai_responses_json.zig index 8ff7712..295e79c 100644 --- a/libpanto/src/openai_responses_json.zig +++ b/libpanto/src/openai_responses_json.zig @@ -30,6 +30,11 @@ const tool_registry_mod = @import("tool_registry.zig"); // Request serialization // =========================================================================== +pub const RequestDialect = enum { + public, + codex, +}; + /// Serialize a Conversation into a `/responses` request body. Caller owns the /// returned slice. pub fn serializeRequest( @@ -37,6 +42,7 @@ pub fn serializeRequest( cfg: *const config_mod.OpenAIResponsesConfig, conv: *const conversation.Conversation, tools: *const tool_registry_mod.ToolRegistry, + dialect: RequestDialect, ) ![]u8 { var aw: Writer.Allocating = .init(allocator); errdefer aw.deinit(); @@ -54,8 +60,24 @@ pub fn serializeRequest( try s.objectField("store"); try s.write(false); - try s.objectField("max_output_tokens"); - try s.write(cfg.max_tokens); + if (dialect == .public) { + try s.objectField("max_output_tokens"); + try s.write(cfg.max_tokens); + } + + if (dialect == .codex) { + try s.objectField("text"); + try s.beginObject(); + try s.objectField("verbosity"); + try s.write("low"); + try s.endObject(); + + try s.objectField("tool_choice"); + try s.write("auto"); + + try s.objectField("parallel_tool_calls"); + try s.write(true); + } // Carry encrypted reasoning so multi-turn reasoning can continue without // server-side storage. @@ -110,7 +132,7 @@ pub fn serializeRequest( try s.beginArray(); for (conversation.activeMessageWindow(conv.messages.items)) |msg| { if (msg.role == .system) continue; - try writeInputForMessage(&s, msg, allocator); + try writeInputForMessage(&s, msg, allocator, cfg, dialect); } try s.endArray(); @@ -129,7 +151,13 @@ fn writeReasoning(s: *std.json.Stringify, effort: []const u8) !void { } /// Emit the `input` item(s) for one conversation message. -fn writeInputForMessage(s: *std.json.Stringify, msg: conversation.Message, allocator: Allocator) !void { +fn writeInputForMessage( + s: *std.json.Stringify, + msg: conversation.Message, + allocator: Allocator, + cfg: *const config_mod.OpenAIResponsesConfig, + dialect: RequestDialect, +) !void { switch (msg.role) { .system => {}, .user => { @@ -161,17 +189,33 @@ fn writeInputForMessage(s: *std.json.Stringify, msg: conversation.Message, alloc defer text_buf.deinit(allocator); try concatTextBlocks(msg.content.items, &text_buf, allocator); if (text_buf.items.len > 0) { - try writeRoleMessage(s, "user", "input_text", text_buf.items); + try writeRoleMessage(s, "user", "input_text", text_buf.items, null); } }, .assistant => { + // Replay opaque reasoning items first so stateless follow-up turns + // preserve encrypted reasoning continuity. + for (msg.content.items) |block| { + if (block != .Thinking) continue; + const tb = block.Thinking; + const sig = tb.signature orelse continue; + const origin = tb.signature_origin orelse continue; + if (!origin.matches( + if (dialect == .codex) .openai_codex_responses else .openai_responses, + cfg.base_url, + cfg.model, + )) continue; + if (sig.len == 0 or sig[0] != '{') continue; + try writeRawJson(s, sig); + } + // Assistant text first (as an output_text message), then each // tool call as a `function_call` item. var text_buf: std.ArrayList(u8) = .empty; defer text_buf.deinit(allocator); try concatTextBlocks(msg.content.items, &text_buf, allocator); if (text_buf.items.len > 0) { - try writeRoleMessage(s, "assistant", "output_text", text_buf.items); + try writeRoleMessage(s, "assistant", "output_text", text_buf.items, openAIPhaseFromMetadata(msg.metadata)); } for (msg.content.items) |block| { if (block != .ToolUse) continue; @@ -192,10 +236,20 @@ fn writeInputForMessage(s: *std.json.Stringify, msg: conversation.Message, alloc } } -fn writeRoleMessage(s: *std.json.Stringify, role: []const u8, content_type: []const u8, text: []const u8) !void { +fn writeRoleMessage( + s: *std.json.Stringify, + role: []const u8, + content_type: []const u8, + text: []const u8, + phase: ?[]const u8, +) !void { try s.beginObject(); try s.objectField("role"); try s.write(role); + if (phase) |p| { + try s.objectField("phase"); + try s.write(p); + } try s.objectField("content"); try s.beginArray(); try s.beginObject(); @@ -208,6 +262,17 @@ fn writeRoleMessage(s: *std.json.Stringify, role: []const u8, content_type: []co try s.endObject(); } +fn openAIPhaseFromMetadata(metadata: ?[]const u8) ?[]const u8 { + const md = metadata orelse return null; + var parsed = std.json.parseFromSlice(std.json.Value, std.heap.page_allocator, md, .{}) catch return null; + defer parsed.deinit(); + if (parsed.value != .object) return null; + const phase = strField(parsed.value.object, "openai_responses_phase") orelse return null; + if (std.mem.eql(u8, phase, "commentary")) return "commentary"; + if (std.mem.eql(u8, phase, "final_answer")) return "final_answer"; + return null; +} + fn concatTextBlocks( blocks: []const conversation.ContentBlock, out: *std.ArrayList(u8), @@ -244,6 +309,7 @@ pub const EventKind = enum { output_text_delta, reasoning_summary_delta, function_call_arguments_delta, + function_call_arguments_done, output_item_done, completed, failed, @@ -261,8 +327,16 @@ pub const StreamEvent = struct { /// Item id (`response.output_item.added/done`, function-call argument /// deltas reference `item_id`). item_id: ?[]const u8 = null, + /// Output array index. Some function-call argument events identify the + /// item by this instead of repeating `item_id`. + output_index: ?usize = null, /// Item type on add/done: "message" | "function_call" | "reasoning". item_type: ?[]const u8 = null, + /// Assistant message phase on message output items. + item_phase: ?[]const u8 = null, + /// Raw reasoning output item JSON, used for stateless encrypted reasoning + /// replay on follow-up turns. + reasoning_item_json: ?[]const u8 = null, /// Function-call identity (on `output_item.added`/`done`). call_id: ?[]const u8 = null, name: ?[]const u8 = null, @@ -272,6 +346,8 @@ pub const StreamEvent = struct { error_message: ?[]const u8 = null, /// Usage on `response.completed`. usage: ?Usage = null, + /// Function-call output items included in a terminal `response.completed`. + completed_items: []const OutputItem = &.{}, pub const Usage = struct { input_tokens: u64 = 0, @@ -280,6 +356,15 @@ pub const StreamEvent = struct { reasoning_tokens: u64 = 0, }; + pub const OutputItem = struct { + output_index: usize, + item_id: ?[]const u8 = null, + item_type: ?[]const u8 = null, + call_id: ?[]const u8 = null, + name: ?[]const u8 = null, + arguments: ?[]const u8 = null, + }; + pub fn deinit(self: *StreamEvent) void { self.parsed.deinit(); } @@ -305,23 +390,36 @@ pub fn parseStreamEvent(allocator: Allocator, payload: []const u8) !StreamEvent ev.kind = .output_text_delta; ev.delta = strField(obj, "delta"); ev.item_id = strField(obj, "item_id"); + ev.output_index = usizeField(obj, "output_index"); } else if (std.mem.eql(u8, type_str, "response.reasoning_summary_text.delta")) { ev.kind = .reasoning_summary_delta; ev.delta = strField(obj, "delta"); ev.item_id = strField(obj, "item_id"); + ev.output_index = usizeField(obj, "output_index"); } else if (std.mem.eql(u8, type_str, "response.function_call_arguments.delta")) { ev.kind = .function_call_arguments_delta; - ev.delta = strField(obj, "delta"); + ev.delta = strField(obj, "delta") orelse + strField(obj, "arguments_delta") orelse + strField(obj, "arguments"); ev.item_id = strField(obj, "item_id"); + ev.output_index = usizeField(obj, "output_index"); + } else if (std.mem.eql(u8, type_str, "response.function_call_arguments.done")) { + ev.kind = .function_call_arguments_done; + ev.arguments = strField(obj, "arguments"); + ev.item_id = strField(obj, "item_id"); + ev.output_index = usizeField(obj, "output_index"); } else if (std.mem.eql(u8, type_str, "response.output_item.added")) { ev.kind = .output_item_added; - readItem(obj, &ev); + ev.output_index = usizeField(obj, "output_index"); + try readItem(parsed.arena.allocator(), obj, &ev); } else if (std.mem.eql(u8, type_str, "response.output_item.done")) { ev.kind = .output_item_done; - readItem(obj, &ev); - } else if (std.mem.eql(u8, type_str, "response.completed")) { + ev.output_index = usizeField(obj, "output_index"); + try readItem(parsed.arena.allocator(), obj, &ev); + } else if (std.mem.eql(u8, type_str, "response.completed") or std.mem.eql(u8, type_str, "response.done")) { ev.kind = .completed; readUsage(obj, &ev); + try readCompletedOutput(parsed.arena.allocator(), obj, &ev); } else if (std.mem.eql(u8, type_str, "response.failed") or std.mem.eql(u8, type_str, "response.incomplete")) { ev.kind = .failed; ev.error_message = readResponseError(obj); @@ -334,15 +432,31 @@ pub fn parseStreamEvent(allocator: Allocator, payload: []const u8) !StreamEvent return ev; } -fn readItem(obj: std.json.ObjectMap, ev: *StreamEvent) void { +fn readItem(allocator: Allocator, obj: std.json.ObjectMap, ev: *StreamEvent) !void { const item = obj.get("item") orelse return; if (item != .object) return; const io = item.object; ev.item_id = strField(io, "id"); ev.item_type = strField(io, "type"); + ev.item_phase = strField(io, "phase"); ev.call_id = strField(io, "call_id"); ev.name = strField(io, "name"); ev.arguments = strField(io, "arguments"); + if (ev.item_type) |it| { + if (std.mem.eql(u8, it, "reasoning")) { + if (io.get("encrypted_content") != null) { + ev.reasoning_item_json = try stringifyValue(allocator, item); + } + } + } +} + +fn stringifyValue(allocator: Allocator, value: std.json.Value) ![]const u8 { + var aw: Writer.Allocating = .init(allocator); + errdefer aw.deinit(); + var s: std.json.Stringify = .{ .writer = &aw.writer }; + try s.write(value); + return try aw.toOwnedSlice(); } fn readUsage(obj: std.json.ObjectMap, ev: *StreamEvent) void { @@ -366,6 +480,32 @@ fn readUsage(obj: std.json.ObjectMap, ev: *StreamEvent) void { ev.usage = usage; } +fn readCompletedOutput(allocator: Allocator, obj: std.json.ObjectMap, ev: *StreamEvent) !void { + const resp = obj.get("response") orelse return; + if (resp != .object) return; + const output = resp.object.get("output") orelse return; + if (output != .array) return; + + var items: std.ArrayList(StreamEvent.OutputItem) = .empty; + errdefer items.deinit(allocator); + for (output.array.items, 0..) |v, i| { + if (v != .object) continue; + const item_type = strField(v.object, "type") orelse continue; + if (!std.mem.eql(u8, item_type, "function_call")) continue; + try items.append(allocator, .{ + .output_index = i, + .item_id = strField(v.object, "id"), + .item_type = item_type, + .call_id = strField(v.object, "call_id"), + .name = strField(v.object, "name"), + .arguments = strField(v.object, "arguments"), + }); + } + if (items.items.len == 0) return; + const buf = try items.toOwnedSlice(allocator); + ev.completed_items = buf; +} + fn readResponseError(obj: std.json.ObjectMap) ?[]const u8 { const resp = obj.get("response") orelse return null; if (resp != .object) return null; @@ -389,6 +529,12 @@ fn u64Field(obj: std.json.ObjectMap, name: []const u8) u64 { return @intCast(v.integer); } +fn usizeField(obj: std.json.ObjectMap, name: []const u8) ?usize { + const v = obj.get(name) orelse return null; + if (v != .integer or v.integer < 0) return null; + return @intCast(v.integer); +} + // =========================================================================== // Tests // =========================================================================== @@ -421,7 +567,7 @@ test "responses serializeRequest - instructions, input, store/include" { cfg.reasoning = .high; var tools = emptyTools(); defer tools.deinit(); - const body = try serializeRequest(allocator, &cfg, &conv, &tools); + const body = try serializeRequest(allocator, &cfg, &conv, &tools, .public); defer allocator.free(body); var parsed = try std.json.parseFromSlice(std.json.Value, allocator, body, .{}); @@ -458,7 +604,7 @@ test "responses serializeRequest - tools are flat function items" { }); const cfg = testConfig("gpt-5.1-codex"); - const body = try serializeRequest(allocator, &cfg, &conv, &tools); + const body = try serializeRequest(allocator, &cfg, &conv, &tools, .public); defer allocator.free(body); var parsed = try std.json.parseFromSlice(std.json.Value, allocator, body, .{}); defer parsed.deinit(); @@ -471,6 +617,87 @@ test "responses serializeRequest - tools are flat function items" { try testing.expect(tool0.get("parameters").? == .object); } +test "responses serializeRequest - codex dialect omits max tokens and sends codex defaults" { + const allocator = testing.allocator; + var conv = conversation.Conversation.init(allocator); + defer conv.deinit(); + try addUserText(&conv, "Hello!"); + + var tools = emptyTools(); + defer tools.deinit(); + const cfg = testConfig("gpt-5.5"); + const body = try serializeRequest(allocator, &cfg, &conv, &tools, .codex); + defer allocator.free(body); + var parsed = try std.json.parseFromSlice(std.json.Value, allocator, body, .{}); + defer parsed.deinit(); + const root = parsed.value.object; + + try testing.expect(root.get("max_output_tokens") == null); + try testing.expectEqualStrings("low", root.get("text").?.object.get("verbosity").?.string); + try testing.expectEqualStrings("auto", root.get("tool_choice").?.string); + try testing.expect(root.get("parallel_tool_calls").?.bool); +} + +test "responses serializeRequest - assistant phase metadata and reasoning signature replay" { + const allocator = testing.allocator; + var conv = conversation.Conversation.init(allocator); + defer conv.deinit(); + + const sig = try allocator.dupe(u8, + "{\"type\":\"reasoning\",\"id\":\"rs_1\",\"encrypted_content\":\"sealed\"}" + ); + const thinking = try conversation.textualBlockFromSlice(allocator, "thinking"); + const text = try conversation.textualBlockFromSlice(allocator, "answer"); + try conv.addAssistantMessage(&.{ + .{ .Thinking = .{ .text = thinking, .signature = sig } }, + .{ .Text = text }, + }, null); + try conversation.setThinkingOrigins(allocator, conv.messages.items[0].content.items, .openai_codex_responses, "u", "gpt-5.5"); + conv.messages.items[0].metadata = try allocator.dupe(u8, "{\"openai_responses_phase\":\"final_answer\"}"); + + var tools = emptyTools(); + defer tools.deinit(); + const cfg = testConfig("gpt-5.5"); + const body = try serializeRequest(allocator, &cfg, &conv, &tools, .codex); + defer allocator.free(body); + var parsed = try std.json.parseFromSlice(std.json.Value, allocator, body, .{}); + defer parsed.deinit(); + + const input = parsed.value.object.get("input").?.array.items; + try testing.expectEqual(@as(usize, 2), input.len); + try testing.expectEqualStrings("reasoning", input[0].object.get("type").?.string); + try testing.expectEqualStrings("sealed", input[0].object.get("encrypted_content").?.string); + try testing.expectEqualStrings("assistant", input[1].object.get("role").?.string); + try testing.expectEqualStrings("final_answer", input[1].object.get("phase").?.string); +} + +test "responses serializeRequest - mismatched signature origin skips reasoning replay" { + const allocator = testing.allocator; + var conv = conversation.Conversation.init(allocator); + defer conv.deinit(); + + const sig = try allocator.dupe(u8, + "{\"type\":\"reasoning\",\"id\":\"rs_1\",\"encrypted_content\":\"sealed\"}" + ); + try conv.addAssistantMessage(&.{ + .{ .Thinking = .{ .text = try conversation.textualBlockFromSlice(allocator, "thinking"), .signature = sig } }, + .{ .Text = try conversation.textualBlockFromSlice(allocator, "answer") }, + }, null); + try conversation.setThinkingOrigins(allocator, conv.messages.items[0].content.items, .openai_responses, "https://api.individual.githubcopilot.com", "gpt-5.4-mini"); + + var tools = emptyTools(); + defer tools.deinit(); + const cfg = testConfig("gpt-5.5"); + const body = try serializeRequest(allocator, &cfg, &conv, &tools, .codex); + defer allocator.free(body); + var parsed = try std.json.parseFromSlice(std.json.Value, allocator, body, .{}); + defer parsed.deinit(); + + const input = parsed.value.object.get("input").?.array.items; + try testing.expectEqual(@as(usize, 1), input.len); + try testing.expectEqualStrings("assistant", input[0].object.get("role").?.string); +} + test "responses serializeRequest - assistant tool_use + tool result round-trip" { const allocator = testing.allocator; var conv = conversation.Conversation.init(allocator); @@ -495,7 +722,7 @@ test "responses serializeRequest - assistant tool_use + tool result round-trip" var tools = emptyTools(); defer tools.deinit(); const cfg = testConfig("gpt-5.1-codex"); - const body = try serializeRequest(allocator, &cfg, &conv, &tools); + const body = try serializeRequest(allocator, &cfg, &conv, &tools, .public); defer allocator.free(body); var parsed = try std.json.parseFromSlice(std.json.Value, allocator, body, .{}); defer parsed.deinit(); @@ -540,6 +767,29 @@ test "responses parseStreamEvent - function_call item added/done + args delta" { try testing.expectEqual(EventKind.function_call_arguments_delta, d.kind); try testing.expectEqualStrings("fc_1", d.item_id.?); try testing.expectEqualStrings("{\"x\":1}", d.delta.?); + + var alt = try parseStreamEvent(allocator, + \\{"type":"response.function_call_arguments.delta","item_id":"fc_1","arguments_delta":"{\"x\":1}"} + ); + defer alt.deinit(); + try testing.expectEqual(EventKind.function_call_arguments_delta, alt.kind); + try testing.expectEqualStrings("{\"x\":1}", alt.delta.?); + + var by_index = try parseStreamEvent(allocator, + \\{"type":"response.function_call_arguments.delta","output_index":0,"delta":"{\"x\":1}"} + ); + defer by_index.deinit(); + try testing.expectEqual(EventKind.function_call_arguments_delta, by_index.kind); + try testing.expectEqual(@as(usize, 0), by_index.output_index.?); + try testing.expectEqualStrings("{\"x\":1}", by_index.delta.?); + + var done = try parseStreamEvent(allocator, + \\{"type":"response.function_call_arguments.done","output_index":0,"arguments":"{\"x\":1}"} + ); + defer done.deinit(); + try testing.expectEqual(EventKind.function_call_arguments_done, done.kind); + try testing.expectEqual(@as(usize, 0), done.output_index.?); + try testing.expectEqualStrings("{\"x\":1}", done.arguments.?); } test "responses parseStreamEvent - completed usage" { @@ -555,6 +805,45 @@ test "responses parseStreamEvent - completed usage" { try testing.expectEqual(@as(u64, 8), ev.usage.?.reasoning_tokens); } +test "responses parseStreamEvent - done alias, phase, encrypted reasoning item" { + const allocator = testing.allocator; + var done = try parseStreamEvent(allocator, + \\{"type":"response.done","response":{"usage":{"input_tokens":1,"output_tokens":2}}} + ); + defer done.deinit(); + try testing.expectEqual(EventKind.completed, done.kind); + try testing.expectEqual(@as(u64, 1), done.usage.?.input_tokens); + + var msg = try parseStreamEvent(allocator, + \\{"type":"response.output_item.done","item":{"type":"message","id":"msg_1","phase":"commentary"}} + ); + defer msg.deinit(); + try testing.expectEqual(EventKind.output_item_done, msg.kind); + try testing.expectEqualStrings("commentary", msg.item_phase.?); + + var reasoning = try parseStreamEvent(allocator, + \\{"type":"response.output_item.done","item":{"type":"reasoning","id":"rs_1","encrypted_content":"sealed"}} + ); + defer reasoning.deinit(); + try testing.expectEqual(EventKind.output_item_done, reasoning.kind); + try testing.expect(reasoning.reasoning_item_json != null); +} + +test "responses parseStreamEvent - completed output function calls" { + const allocator = testing.allocator; + var ev = try parseStreamEvent(allocator, + \\{"type":"response.completed","response":{"output":[{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__read","arguments":"{\"path\":\"a\"}","status":"completed"}],"usage":{"input_tokens":100,"output_tokens":20}}} + ); + defer ev.deinit(); + try testing.expectEqual(EventKind.completed, ev.kind); + try testing.expectEqual(@as(usize, 1), ev.completed_items.len); + try testing.expectEqual(@as(usize, 0), ev.completed_items[0].output_index); + try testing.expectEqualStrings("fc_1", ev.completed_items[0].item_id.?); + try testing.expectEqualStrings("call_9", ev.completed_items[0].call_id.?); + try testing.expectEqualStrings("std__read", ev.completed_items[0].name.?); + try testing.expectEqualStrings("{\"path\":\"a\"}", ev.completed_items[0].arguments.?); +} + test "responses parseStreamEvent - error + failed" { const allocator = testing.allocator; var e = try parseStreamEvent(allocator, diff --git a/libpanto/src/provider.zig b/libpanto/src/provider.zig index 9c715b7..3c3b52e 100644 --- a/libpanto/src/provider.zig +++ b/libpanto/src/provider.zig @@ -326,6 +326,18 @@ pub fn openStream( const rr = try req.open(conv, registry); return rr.providerStream(); }, + .openai_codex_responses => |*c| { + var req: provider_openai_responses.OpenAIResponsesRequest = .{ + .allocator = allocator, + .io = io, + .config = c, + .dialect = .codex, + .http_client = client, + .diag = diag, + }; + const rr = try req.open(conv, registry); + return rr.providerStream(); + }, } } diff --git a/libpanto/src/provider_anthropic_messages.zig b/libpanto/src/provider_anthropic_messages.zig index 8f5d200..95013d1 100644 --- a/libpanto/src/provider_anthropic_messages.zig +++ b/libpanto/src/provider_anthropic_messages.zig @@ -5,7 +5,10 @@ //! //! Responsibilities: //! - Convert `Conversation` → request JSON (delegated to anthropic_messages_json.zig) -//! - POST to `{base_url}/v1/messages` with `stream: true` +//! - POST to `{base_url}/messages` with `stream: true` +//! (caller is responsible for the `/v1` version segment; panto will +//! introduce a new `anthropic_messages_v2` API style for v2 rather than +//! pulling the version into the provider) //! - Read the chunked body, feed bytes through SSEParser //! - Parse each event payload, drive a thin assembly loop, and emit Receiver //! callbacks. Anthropic gives us explicit block boundaries, so no @@ -58,6 +61,12 @@ pub const AnthropicMessagesRequest = struct { .parser = sse_mod.SSEParser.init(self.allocator), .state = .init(self.allocator), }; + rr.state.signature_origin = try conversation.SignatureOrigin.init( + self.allocator, + .anthropic_messages, + self.config.base_url, + self.config.model, + ); errdefer { rr.parser.deinit(); rr.state.deinit(); @@ -65,7 +74,7 @@ pub const AnthropicMessagesRequest = struct { const url = try std.fmt.allocPrint( self.allocator, - "{s}/v1/messages", + "{s}/messages", .{self.config.base_url}, ); defer self.allocator.free(url); @@ -74,6 +83,7 @@ pub const AnthropicMessagesRequest = struct { const body = try json_mod.serializeRequest(self.allocator, self.config, conv, tools); defer self.allocator.free(body); + std.log.debug("anthropic_messages => {s}", .{body}); // Build headers. The four base headers are always present; the // interleaved-thinking beta header is added only when the config @@ -96,7 +106,7 @@ pub const AnthropicMessagesRequest = struct { .value = "interleaved-thinking-2025-05-14", }; } - const base_headers = headers_buf[0 .. if (send_interleaved) @as(usize, 5) else @as(usize, 4)]; + const base_headers = headers_buf[0..if (send_interleaved) @as(usize, 5) else @as(usize, 4)]; // Merge any provider `extra_headers` onto the base set. Freed at the // end of `open` — after the request body has been flushed. const extra_headers = try provider_mod.mergeHeaders( @@ -147,12 +157,20 @@ pub const AnthropicMessagesRequest = struct { if (err_buf.items.len > 16 * 1024) break; } const status: u16 = @intFromEnum(rr.response.head.status); - std.log.err("anthropic_messages HTTP {d}: {s}", .{ status, err_buf.items }); // Anthropic rejects oversized requests with HTTP 400 and a // "prompt is too long" message; `classifyHttpStatus` maps that to // ContextOverflow so the caller can compact and retry. Other // statuses map to retryable/terminal provider errors. const classified = provider_mod.classifyHttpStatus(status, err_buf.items); + // 401/403 is routinely recovered by the turn-runner's forced token + // refresh + reopen; demote it to `.debug` (still in the debug log) + // so a transparent refresh doesn't surface a scary error line. The + // retry layer raises a hard error only if recovery ultimately fails. + if (classified == error.ProviderAuthFailed) { + std.log.debug("anthropic_messages HTTP {d} (recoverable auth): {s}", .{ status, err_buf.items }); + } else { + std.log.err("anthropic_messages HTTP {d}: {s}", .{ status, err_buf.items }); + } if (self.diag) |d| { d.status_code = status; d.retry_after_ms = retry_after_ms; @@ -287,6 +305,7 @@ const StreamState = struct { /// `onMessageComplete`, the latter stamps `null`. usage: provider_mod.Usage = .{}, usage_seen: bool = false, + signature_origin: ?conversation.SignatureOrigin = null, stop_reason: ?[]u8 = null, /// Owned, human-readable description of a mid-stream `error` event /// (e.g. `"overloaded_error: Overloaded"`), surfaced to the agent via @@ -321,6 +340,7 @@ const StreamState = struct { } for (self.blocks.items) |*b| b.deinit(self.allocator); self.blocks.deinit(self.allocator); + if (self.signature_origin) |*o| o.deinit(self.allocator); if (self.stop_reason) |s| self.allocator.free(s); if (self.stream_error_message) |s| self.allocator.free(s); } @@ -557,6 +577,15 @@ const StreamState = struct { defer self.allocator.free(moved_blocks); const usage: ?provider_mod.Usage = if (self.usage_seen) self.usage else null; + if (self.signature_origin) |origin| { + try conversation.setThinkingOrigins( + self.allocator, + moved_blocks, + origin.api_style, + origin.base_url, + origin.model, + ); + } try conv.addAssistantMessage(moved_blocks, usage); const msg = conv.messages.items[conv.messages.items.len - 1]; diff --git a/libpanto/src/provider_openai_chat.zig b/libpanto/src/provider_openai_chat.zig index b236a60..3f01e7f 100644 --- a/libpanto/src/provider_openai_chat.zig +++ b/libpanto/src/provider_openai_chat.zig @@ -94,6 +94,7 @@ pub const OpenAIChatRequest = struct { // Build the request body. const body = try json_mod.serializeRequest(self.allocator, self.config, conv, tools); defer self.allocator.free(body); + std.log.debug("openai_chat => {s}", .{body}); // Auth header const auth_value = try std.fmt.allocPrint( @@ -165,11 +166,19 @@ pub const OpenAIChatRequest = struct { if (err_buf.items.len > 16 * 1024) break; } const status: u16 = @intFromEnum(rr.response.head.status); - std.log.err("openai_chat HTTP {d}: {s}", .{ status, err_buf.items }); // Classify the status into a retryable/terminal provider error. // HTTP 400 with a context marker becomes `ContextOverflow` so the // caller can compact and retry rather than hard-fail. const classified = provider_mod.classifyHttpStatus(status, err_buf.items); + // 401/403 is routinely recovered by the turn-runner's forced token + // refresh + reopen; demote it to `.debug` (still in the debug log) + // so a transparent refresh doesn't surface a scary error line. The + // retry layer raises a hard error only if recovery ultimately fails. + if (classified == error.ProviderAuthFailed) { + std.log.debug("openai_chat HTTP {d} (recoverable auth): {s}", .{ status, err_buf.items }); + } else { + std.log.err("openai_chat HTTP {d}: {s}", .{ status, err_buf.items }); + } if (self.diag) |d| { d.status_code = status; d.retry_after_ms = retry_after_ms; @@ -1032,7 +1041,6 @@ test "inbound wire tool name is decoded to dotted form (even split across __)" { try testing.expectEqualStrings("std.read", tu.name); } - test "parallel tool_calls emit one complete start/delta/complete cycle per block" { // Regression test: previously, the OpenAI provider deferred ALL // tool_use onBlockComplete callbacks to finalize, so a four-tool diff --git a/libpanto/src/provider_openai_responses.zig b/libpanto/src/provider_openai_responses.zig index 92a309b..c172f6d 100644 --- a/libpanto/src/provider_openai_responses.zig +++ b/libpanto/src/provider_openai_responses.zig @@ -36,6 +36,11 @@ const tool_registry_mod = @import("tool_registry.zig"); const Event = stream_mod.Event; const EventQueue = stream_mod.EventQueue; +pub const OpenAIResponsesDialect = enum { + public, + codex, +}; + fn decodeNameInPlace(name_buf: *conversation.TextualBlock) void { const decoded = tool_registry_mod.decodeName(name_buf.items, name_buf.items); name_buf.items.len = decoded.len; @@ -45,6 +50,7 @@ pub const OpenAIResponsesRequest = struct { allocator: Allocator, io: Io, config: *const config_mod.OpenAIResponsesConfig, + dialect: OpenAIResponsesDialect = .public, http_client: *http.Client, diag: ?*provider_mod.ProviderDiagnostic = null, @@ -61,17 +67,27 @@ pub const OpenAIResponsesRequest = struct { .parser = sse_mod.SSEParser.init(self.allocator), .state = .init(self.allocator), }; + rr.state.signature_origin = try conversation.SignatureOrigin.init( + self.allocator, + if (self.dialect == .codex) .openai_codex_responses else .openai_responses, + self.config.base_url, + self.config.model, + ); errdefer { rr.parser.deinit(); rr.state.deinit(); } - const url = try std.fmt.allocPrint(self.allocator, "{s}/responses", .{self.config.base_url}); + const url = try responsesURL(self.allocator, self.config.base_url); defer self.allocator.free(url); const uri = try Uri.parse(url); - const body = try json_mod.serializeRequest(self.allocator, self.config, conv, tools); + const body = try json_mod.serializeRequest(self.allocator, self.config, conv, tools, switch (self.dialect) { + .public => .public, + .codex => .codex, + }); defer self.allocator.free(body); + std.log.debug("openai_responses => {s}", .{body}); const auth_value = try std.fmt.allocPrint(self.allocator, "Bearer {s}", .{self.config.api_key}); defer self.allocator.free(auth_value); @@ -123,8 +139,18 @@ pub const OpenAIResponsesRequest = struct { if (err_buf.items.len > 16 * 1024) break; } const status: u16 = @intFromEnum(rr.response.head.status); - std.log.err("openai_responses HTTP {d}: {s}", .{ status, err_buf.items }); const classified = provider_mod.classifyHttpStatus(status, err_buf.items); + // 401/403 is routinely recovered by the turn-runner's forced token + // refresh + reopen (see `driveTurn` in tui_app.zig). Logging it at + // `.err` surfaces a scary "token expired" line for what is a + // transparent, recoverable refresh. Demote it to `.debug` (still + // captured in the debug log); the retry layer raises a hard error + // only if recovery ultimately fails. + if (classified == error.ProviderAuthFailed) { + std.log.debug("openai_responses HTTP {d} (recoverable auth): {s}", .{ status, err_buf.items }); + } else { + std.log.err("openai_responses HTTP {d}: {s}", .{ status, err_buf.items }); + } if (self.diag) |d| { d.status_code = status; d.retry_after_ms = retry_after_ms; @@ -137,6 +163,16 @@ pub const OpenAIResponsesRequest = struct { } }; +// Appends `/responses` to `base_url` (with trailing slashes trimmed). The +// caller is responsible for any path segment preceding `/responses` — for +// the Codex ChatGPT-subscription endpoint, that means putting `/codex` +// (or any other prefix) in `base_url` directly. No `endsWith` guessing: the +// contract is the same regardless of `dialect`. +fn responsesURL(allocator: Allocator, base_url: []const u8) ![]u8 { + const trimmed = std.mem.trim(u8, base_url, "/"); + return std.fmt.allocPrint(allocator, "{s}/responses", .{trimmed}); +} + pub const ResumableResponse = struct { allocator: Allocator, conv: *conversation.Conversation, @@ -226,6 +262,29 @@ pub const ResumableResponse = struct { const ActiveBlock = enum { none, text, thinking }; +/// Streaming-response assembly state. +/// +/// Modeled on `provider_openai_chat.zig`'s `StreamState`: one active +/// text/thinking block at a time, plus a set of in-progress tool calls. The +/// Responses protocol is friendlier than Chat Completions here — every +/// function-call event carries an explicit `item_id` (and `output_index`), +/// and the lifecycle is spelled out by `output_item.added` → +/// `function_call_arguments.delta`* → `function_call_arguments.done`/ +/// `output_item.done` → `response.completed`. So tool calls are keyed by +/// `item_id` (the stable identity) and need none of Chat Completions' +/// contiguity inference. +/// +/// Argument-accumulation rule (the crux of the tool-input correctness): the +/// concatenation of `function_call_arguments.delta` fragments is the reliable +/// source of the tool input. A terminal `function_call_arguments.done` / +/// `output_item.done` / `response.completed` event also restates the full +/// `arguments`, but we apply it only as a *non-empty* override: a restated +/// value can improve the accumulation (e.g. if it is more complete) but never +/// wipe it. This matters because these events are observed to restate +/// `arguments` as `""` once the value has already streamed via deltas, and an +/// unconditional overwrite there destroys the real input — the original +/// empty-tool-input bug. Empty arguments normalize to `"{}"` so a tool never +/// receives an unparseable empty string. const StreamState = struct { allocator: Allocator, started: bool = false, @@ -233,14 +292,32 @@ const StreamState = struct { active: ActiveBlock = .none, block_index: usize = 0, current_buf: conversation.TextualBlock = .empty, + current_thinking_signature: ?[]const u8 = null, + signature_origin: ?conversation.SignatureOrigin = null, + assistant_phase: ?AssistantPhase = null, blocks: std.ArrayList(conversation.ContentBlock) = .empty, - /// In-progress tool calls keyed by their streaming `item_id`. - tools: std.StringArrayHashMapUnmanaged(ToolUseInProgress) = .empty, + /// In-progress and completed tool calls, keyed by `output_index`, in + /// first-seen order. Completed calls are retained (flagged `closed`) so + /// the terminal `response.completed`, which restates every output item, + /// does not re-emit a call that `output_item.done` already closed. + /// + /// We key by `output_index`, NOT the per-item `item_id`. The OpenAI spec + /// promises a stable `item.id` per output item, but the GitHub Copilot + /// Responses proxy emits a *fresh, opaque `item.id` on every event* for + /// the same call (`output_item.added`, each `…arguments.delta`, + /// `output_item.done`, and the `response.completed` restatement all + /// differ). Keying by `item_id` there made dedup miss, so one call + /// fanned out into three identical ToolUse blocks sharing one `call_id`. + /// `output_index` is stable across all of a call's events on both + /// backends — and is what `provider_openai_chat` keys on, too. + tools: std.AutoArrayHashMapUnmanaged(usize, ToolUseInProgress) = .empty, usage: ?provider_mod.Usage = null, stream_error_message: ?[]u8 = null, const ToolUseInProgress = struct { block_index: usize, + /// Set once `block_complete` has been emitted for this call. + closed: bool = false, id_buf: conversation.TextualBlock = .empty, // call_id name_buf: conversation.TextualBlock = .empty, arguments: conversation.TextualBlock = .empty, @@ -258,11 +335,12 @@ const StreamState = struct { fn deinit(self: *StreamState) void { self.current_buf.deinit(self.allocator); + if (self.current_thinking_signature) |s| self.allocator.free(s); + if (self.signature_origin) |*o| o.deinit(self.allocator); for (self.blocks.items) |*b| b.deinit(self.allocator); self.blocks.deinit(self.allocator); var it = self.tools.iterator(); while (it.next()) |e| { - self.allocator.free(e.key_ptr.*); e.value_ptr.deinit(self.allocator); } self.tools.deinit(self.allocator); @@ -284,10 +362,11 @@ const StreamState = struct { if (self.active == .none) return; const block: conversation.ContentBlock = switch (self.active) { .text => .{ .Text = self.current_buf }, - .thinking => .{ .Thinking = .{ .text = self.current_buf } }, + .thinking => .{ .Thinking = .{ .text = self.current_buf, .signature = self.current_thinking_signature } }, .none => unreachable, }; self.current_buf = .empty; + self.current_thinking_signature = null; try self.blocks.append(self.allocator, block); try out.push(.{ .block_complete = .{ .index = self.block_index, @@ -296,6 +375,19 @@ const StreamState = struct { self.active = .none; } + fn setThinkingSignature(self: *StreamState, signature: []const u8) !void { + if (self.current_thinking_signature) |old| self.allocator.free(old); + self.current_thinking_signature = try self.allocator.dupe(u8, signature); + } + + fn setAssistantPhase(self: *StreamState, phase: []const u8) void { + if (std.mem.eql(u8, phase, "commentary")) { + self.assistant_phase = .commentary; + } else if (std.mem.eql(u8, phase, "final_answer")) { + self.assistant_phase = .final_answer; + } + } + fn openBlock(self: *StreamState, new_active: ActiveBlock, out: *EventQueue) !void { std.debug.assert(new_active == .text or new_active == .thinking); if (self.active == new_active) return; @@ -320,24 +412,46 @@ const StreamState = struct { } }); } - /// Open a new ToolUse block for a `function_call` output item. + /// Normalize a final/seed arguments string: an empty value becomes `"{}"` + /// so a tool never sees an unparseable empty input. + fn normalizedArguments(arguments: []const u8) []const u8 { + return if (arguments.len == 0) "{}" else arguments; + } + + /// Resolve an in-progress (not-yet-closed) tool call by `output_index`. + /// Returns null for an unknown index or an already-closed call. + fn lookupTool(self: *StreamState, output_index: ?usize) ?*ToolUseInProgress { + const idx = output_index orelse return null; + const tu = self.tools.getPtr(idx) orelse return null; + if (tu.closed) return null; + return tu; + } + + /// Open a ToolUse block for a `function_call` output item. No-op if a + /// call at this `output_index` already exists (open or closed): the call + /// is announced once by `output_item.added`, then restated by + /// `output_item.done` and again by `response.completed` — and on the + /// Copilot proxy each restatement carries a different `item_id`, so + /// `output_index` is the only reliable dedup key. fn openToolUse( self: *StreamState, out: *EventQueue, - item_id: []const u8, + output_index: ?usize, call_id: ?[]const u8, name: ?[]const u8, ) !void { + const idx = output_index orelse return; + if (self.tools.contains(idx)) return; // Close any active text/thinking block so the tool gets its own index. if (self.active != .none) { try self.closeActive(out); self.block_index += 1; } var tu: ToolUseInProgress = .{ .block_index = self.block_index }; + errdefer tu.deinit(self.allocator); if (call_id) |c| try tu.id_buf.appendSlice(self.allocator, c); if (name) |nm| try tu.name_buf.appendSlice(self.allocator, nm); - const key = try self.allocator.dupe(u8, item_id); - try self.tools.put(self.allocator, key, tu); + try self.tools.put(self.allocator, idx, tu); try out.push(.{ .block_start = .{ .block_type = .ToolUse, .index = self.block_index } }); if (call_id != null and name != null) { @@ -350,8 +464,11 @@ const StreamState = struct { self.block_index += 1; } - fn appendToolArgs(self: *StreamState, out: *EventQueue, item_id: []const u8, delta: []const u8) !void { - const tu = self.tools.getPtr(item_id) orelse return; + /// Append a streaming argument fragment to the matching call. Fragments + /// for an unknown or already-closed call are dropped (the protocol always + /// opens a call before streaming its arguments). + fn appendToolArgs(self: *StreamState, out: *EventQueue, output_index: ?usize, delta: []const u8) !void { + const tu = self.lookupTool(output_index) orelse return; try tu.arguments.appendSlice(self.allocator, delta); try out.push(.{ .content_delta = .{ .index = tu.block_index, @@ -359,36 +476,51 @@ const StreamState = struct { } }); } - /// Close a ToolUse block on `output_item.done`. `final_args` (when the - /// done event carries the full arguments) overrides the accumulated ones. + /// Override the matching call's arguments with a restated full value, but + /// only when non-empty — terminal events frequently restate already- + /// streamed arguments as `""`, which must not wipe the accumulated input. + fn setToolArgs(self: *StreamState, output_index: ?usize, arguments: []const u8) !void { + if (arguments.len == 0) return; + const tu = self.lookupTool(output_index) orelse return; + tu.arguments.clearRetainingCapacity(); + try tu.arguments.appendSlice(self.allocator, arguments); + } + + /// Close a ToolUse block. `final_args` (when a `done`/`completed` event + /// restates the full arguments) overrides the accumulated value only when + /// non-empty. A call whose identity never resolved (missing id or name) + /// is dropped. Idempotent: closing an already-closed call is a no-op. fn closeToolUse( self: *StreamState, out: *EventQueue, - item_id: []const u8, + output_index: ?usize, final_args: ?[]const u8, ) !void { - const entry = self.tools.fetchSwapRemove(item_id) orelse return; - self.allocator.free(entry.key); - var tu = entry.value; + const tu = self.lookupTool(output_index) orelse return; + if (final_args) |fa| { + if (fa.len > 0) { + tu.arguments.clearRetainingCapacity(); + try tu.arguments.appendSlice(self.allocator, fa); + } + } + tu.closed = true; if (tu.id_buf.items.len == 0 or tu.name_buf.items.len == 0) { - tu.deinit(self.allocator); + // Identity never resolved — nothing dispatchable. Free the buffers + // now; the (empty) entry stays in the map for dedup + final free. + tu.id_buf.clearAndFree(self.allocator); + tu.name_buf.clearAndFree(self.allocator); + tu.arguments.clearAndFree(self.allocator); return; } - if (final_args) |fa| { - tu.arguments.clearRetainingCapacity(); - try tu.arguments.appendSlice(self.allocator, fa); - } - decodeNameInPlace(&tu.name_buf); - const id_owned = try tu.id_buf.toOwnedSlice(self.allocator); - const name_owned = try tu.name_buf.toOwnedSlice(self.allocator); + const input = try conversation.textualBlockFromSlice(self.allocator, normalizedArguments(tu.arguments.items)); + decodeNameInPlace(&tu.name_buf); const block: conversation.ContentBlock = .{ .ToolUse = .{ - .id = id_owned, - .name = name_owned, - .input = tu.arguments, + .id = try self.allocator.dupe(u8, tu.id_buf.items), + .name = try self.allocator.dupe(u8, tu.name_buf.items), + .input = input, } }; - tu.arguments = .empty; try self.blocks.append(self.allocator, block); try out.push(.{ .block_complete = .{ .index = tu.block_index, @@ -402,19 +534,38 @@ const StreamState = struct { try self.closeActive(out); // Close any tool calls that never received an explicit done event. var it = self.tools.iterator(); - var leftover: std.ArrayList([]const u8) = .empty; - defer leftover.deinit(self.allocator); - while (it.next()) |e| leftover.append(self.allocator, e.key_ptr.*) catch {}; - for (leftover.items) |k| try self.closeToolUse(out, k, null); + while (it.next()) |e| { + if (!e.value_ptr.closed) try self.closeToolUse(out, e.key_ptr.*, null); + } const moved_blocks = try self.blocks.toOwnedSlice(self.allocator); defer self.allocator.free(moved_blocks); + if (self.signature_origin) |origin| { + try conversation.setThinkingOrigins( + self.allocator, + moved_blocks, + origin.api_style, + origin.base_url, + origin.model, + ); + } try conv.addAssistantMessage(moved_blocks, self.usage); + if (self.assistant_phase) |phase| { + const md = switch (phase) { + .commentary => openai_phase_commentary_metadata, + .final_answer => openai_phase_final_answer_metadata, + }; + conv.messages.items[conv.messages.items.len - 1].metadata = try conv.allocator.dupe(u8, md); + } const msg = conv.messages.items[conv.messages.items.len - 1]; try out.push(.{ .message_complete = .{ .message = msg, .usage = self.usage } }); } }; +const AssistantPhase = enum { commentary, final_answer }; +const openai_phase_commentary_metadata = "{\"openai_responses_phase\":\"commentary\"}"; +const openai_phase_final_answer_metadata = "{\"openai_responses_phase\":\"final_answer\"}"; + /// Handle one parsed event. Returns true when the stream is terminal /// (`response.completed`) so the caller can finalize. fn handleEvent( @@ -445,23 +596,45 @@ fn handleEvent( if (ev.item_type) |it| { if (std.mem.eql(u8, it, "function_call")) { try state.ensureStarted(out); - if (ev.item_id) |id| try state.openToolUse(out, id, ev.call_id, ev.name); + try state.openToolUse(out, ev.output_index, ev.call_id, ev.name); + // `output_item.added` sometimes seeds the full args. + if (ev.arguments) |args| try state.setToolArgs(ev.output_index, args); } } }, .function_call_arguments_delta => { - if (ev.item_id) |id| { - if (ev.delta) |d| try state.appendToolArgs(out, id, d); - } + if (ev.delta) |d| try state.appendToolArgs(out, ev.output_index, d); + }, + .function_call_arguments_done => { + if (ev.arguments) |args| try state.setToolArgs(ev.output_index, args); }, .output_item_done => { if (ev.item_type) |it| { if (std.mem.eql(u8, it, "function_call")) { - if (ev.item_id) |id| try state.closeToolUse(out, id, ev.arguments); + try state.ensureStarted(out); + try state.openToolUse(out, ev.output_index, ev.call_id, ev.name); + try state.closeToolUse(out, ev.output_index, ev.arguments); + } else if (std.mem.eql(u8, it, "reasoning")) { + if (ev.reasoning_item_json) |sig| { + try state.ensureStarted(out); + try state.openBlock(.thinking, out); + try state.setThinkingSignature(sig); + try state.closeActive(out); + state.block_index += 1; + } + } else if (std.mem.eql(u8, it, "message")) { + if (ev.item_phase) |phase| state.setAssistantPhase(phase); } } }, .completed => { + // `response.completed` restates every output item; open+close any + // function call not already emitted via `output_item.done`. + for (ev.completed_items) |item| { + try state.ensureStarted(out); + try state.openToolUse(out, item.output_index, item.call_id, item.name); + try state.closeToolUse(out, item.output_index, item.arguments); + } if (ev.usage) |u| { const cached = u.cached_tokens; const total_in = u.input_tokens; @@ -593,13 +766,13 @@ test "responses stream: function call assembles a ToolUse" { defer rec.deinit(); const events = [_][]const u8{ - \\{"type":"response.output_item.added","item":{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__read"}} + \\{"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__read"}} , - \\{"type":"response.function_call_arguments.delta","item_id":"fc_1","delta":"{\"path\":"} + \\{"type":"response.function_call_arguments.delta","output_index":0,"item_id":"fc_1","delta":"{\"path\":"} , - \\{"type":"response.function_call_arguments.delta","item_id":"fc_1","delta":"\"a\"}"} + \\{"type":"response.function_call_arguments.delta","output_index":0,"item_id":"fc_1","delta":"\"a\"}"} , - \\{"type":"response.output_item.done","item":{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__read","arguments":"{\"path\":\"a\"}"}} + \\{"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__read","arguments":"{\"path\":\"a\"}"}} , \\{"type":"response.completed","response":{"usage":{"input_tokens":3,"output_tokens":1}}} , @@ -628,6 +801,195 @@ test "responses stream: function call assembles a ToolUse" { for (expect, rec.events.items) |w, g| try testing.expectEqualStrings(w, g); } +test "responses stream: function call arguments can be keyed by output_index" { + const allocator = testing.allocator; + var conv = conversation.Conversation.init(allocator); + defer conv.deinit(); + try addUserText(&conv, "call it"); + + const events = [_][]const u8{ + \\{"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__read"}} + , + \\{"type":"response.function_call_arguments.delta","output_index":0,"delta":"{\"path\":"} + , + \\{"type":"response.function_call_arguments.delta","output_index":0,"delta":"\"a\"}"} + , + \\{"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__read","arguments":""}} + , + \\{"type":"response.completed","response":{"usage":{"input_tokens":3,"output_tokens":1}}} + , + }; + try runStreamedTurn(allocator, &conv, null, &events); + + const tu = conv.messages.items[1].content.items[0].ToolUse; + try testing.expectEqualStrings("call_9", tu.id); + try testing.expectEqualStrings("std.read", tu.name); + try testing.expectEqualStrings("{\"path\":\"a\"}", tu.input.items); +} + +test "responses stream: function call arguments on item added are retained" { + const allocator = testing.allocator; + var conv = conversation.Conversation.init(allocator); + defer conv.deinit(); + try addUserText(&conv, "call it"); + + const events = [_][]const u8{ + \\{"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__bash","arguments":"{\"command\":\"ls\"}"}} + , + \\{"type":"response.completed","response":{"usage":{"input_tokens":3,"output_tokens":1}}} + , + }; + try runStreamedTurn(allocator, &conv, null, &events); + + const tu = conv.messages.items[1].content.items[0].ToolUse; + try testing.expectEqualStrings("call_9", tu.id); + try testing.expectEqualStrings("std.bash", tu.name); + try testing.expectEqualStrings("{\"command\":\"ls\"}", tu.input.items); +} + +test "responses stream: function call arguments done supplies final input" { + const allocator = testing.allocator; + var conv = conversation.Conversation.init(allocator); + defer conv.deinit(); + try addUserText(&conv, "call it"); + + const events = [_][]const u8{ + \\{"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__read"}} + , + \\{"type":"response.function_call_arguments.done","output_index":0,"arguments":"{\"path\":\"a\"}"} + , + \\{"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__read","arguments":""}} + , + \\{"type":"response.completed","response":{"usage":{"input_tokens":3,"output_tokens":1}}} + , + }; + try runStreamedTurn(allocator, &conv, null, &events); + + const tu = conv.messages.items[1].content.items[0].ToolUse; + try testing.expectEqualStrings("call_9", tu.id); + try testing.expectEqualStrings("std.read", tu.name); + try testing.expectEqualStrings("{\"path\":\"a\"}", tu.input.items); +} + +test "responses stream: completed output supplies final function call input" { + const allocator = testing.allocator; + var conv = conversation.Conversation.init(allocator); + defer conv.deinit(); + try addUserText(&conv, "call it"); + + const events = [_][]const u8{ + \\{"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__read","arguments":""}} + , + \\{"type":"response.completed","response":{"output":[{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__read","arguments":"{\"path\":\"a\"}","status":"completed"}],"usage":{"input_tokens":3,"output_tokens":1}}} + , + }; + try runStreamedTurn(allocator, &conv, null, &events); + + const tu = conv.messages.items[1].content.items[0].ToolUse; + try testing.expectEqualStrings("call_9", tu.id); + try testing.expectEqualStrings("std.read", tu.name); + try testing.expectEqualStrings("{\"path\":\"a\"}", tu.input.items); +} + +test "responses stream: completed output does not duplicate closed function call" { + const allocator = testing.allocator; + var conv = conversation.Conversation.init(allocator); + defer conv.deinit(); + try addUserText(&conv, "call it"); + + const events = [_][]const u8{ + \\{"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__read","arguments":""}} + , + \\{"type":"response.function_call_arguments.delta","output_index":0,"delta":"{\"path\":\"a\"}"} + , + \\{"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__read","arguments":""}} + , + \\{"type":"response.completed","response":{"output":[{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__read","arguments":"{\"path\":\"a\"}","status":"completed"}],"usage":{"input_tokens":3,"output_tokens":1}}} + , + }; + try runStreamedTurn(allocator, &conv, null, &events); + + const asst = conv.messages.items[1]; + try testing.expectEqual(@as(usize, 1), asst.content.items.len); + try testing.expectEqualStrings("{\"path\":\"a\"}", asst.content.items[0].ToolUse.input.items); +} + +test "responses stream: empty function call arguments normalize to object" { + const allocator = testing.allocator; + var conv = conversation.Conversation.init(allocator); + defer conv.deinit(); + try addUserText(&conv, "call it"); + + const events = [_][]const u8{ + \\{"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_9","name":"ping","arguments":""}} + , + \\{"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_9","name":"ping","arguments":""}} + , + \\{"type":"response.completed","response":{"usage":{"input_tokens":3,"output_tokens":1}}} + , + }; + try runStreamedTurn(allocator, &conv, null, &events); + + const tu = conv.messages.items[1].content.items[0].ToolUse; + try testing.expectEqualStrings("call_9", tu.id); + try testing.expectEqualStrings("ping", tu.name); + try testing.expectEqualStrings("{}", tu.input.items); +} + +test "responses stream: finalization closes an open function call" { + const allocator = testing.allocator; + var conv = conversation.Conversation.init(allocator); + defer conv.deinit(); + try addUserText(&conv, "call it"); + + const events = [_][]const u8{ + \\{"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_9","name":"std__bash"}} + , + \\{"type":"response.function_call_arguments.delta","output_index":0,"item_id":"fc_1","delta":"{\"command\":\"ls\"}"} + , + \\{"type":"response.completed","response":{"usage":{"input_tokens":3,"output_tokens":1}}} + , + }; + try runStreamedTurn(allocator, &conv, null, &events); + + const tu = conv.messages.items[1].content.items[0].ToolUse; + try testing.expectEqualStrings("call_9", tu.id); + try testing.expectEqualStrings("std.bash", tu.name); + try testing.expectEqualStrings("{\"command\":\"ls\"}", tu.input.items); +} + +test "responses stream: streamed args survive an empty arguments.done" { + // Codex streams the arguments as `function_call_arguments.delta` + // fragments and then emits a terminal `function_call_arguments.done` + // whose `arguments` field is empty (the value already arrived via the + // deltas). The accumulated input must NOT be wiped by that empty done. + const allocator = testing.allocator; + var conv = conversation.Conversation.init(allocator); + defer conv.deinit(); + try addUserText(&conv, "call it"); + + const events = [_][]const u8{ + \\{"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","status":"in_progress","arguments":"","call_id":"call_9","name":"std__read"}} + , + \\{"type":"response.function_call_arguments.delta","item_id":"fc_1","output_index":0,"delta":"{\"path\":"} + , + \\{"type":"response.function_call_arguments.delta","item_id":"fc_1","output_index":0,"delta":"\"a\"}"} + , + \\{"type":"response.function_call_arguments.done","item_id":"fc_1","output_index":0,"arguments":""} + , + \\{"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","id":"fc_1","status":"completed","arguments":"","call_id":"call_9","name":"std__read"}} + , + \\{"type":"response.completed","response":{"usage":{"input_tokens":3,"output_tokens":1}}} + , + }; + try runStreamedTurn(allocator, &conv, null, &events); + + const tu = conv.messages.items[1].content.items[0].ToolUse; + try testing.expectEqualStrings("call_9", tu.id); + try testing.expectEqualStrings("std.read", tu.name); + try testing.expectEqualStrings("{\"path\":\"a\"}", tu.input.items); +} + test "responses stream: text then tool call keeps block order" { const allocator = testing.allocator; var conv = conversation.Conversation.init(allocator); @@ -635,13 +997,13 @@ test "responses stream: text then tool call keeps block order" { try addUserText(&conv, "go"); const events = [_][]const u8{ - \\{"type":"response.output_text.delta","item_id":"msg_1","delta":"working"} + \\{"type":"response.output_text.delta","item_id":"msg_1","output_index":0,"delta":"working"} , - \\{"type":"response.output_item.added","item":{"type":"function_call","id":"fc_1","call_id":"c1","name":"ping"}} + \\{"type":"response.output_item.added","output_index":1,"item":{"type":"function_call","id":"fc_1","call_id":"c1","name":"ping"}} , - \\{"type":"response.function_call_arguments.delta","item_id":"fc_1","delta":"{}"} + \\{"type":"response.function_call_arguments.delta","output_index":1,"item_id":"fc_1","delta":"{}"} , - \\{"type":"response.output_item.done","item":{"type":"function_call","id":"fc_1","call_id":"c1","name":"ping","arguments":"{}"}} + \\{"type":"response.output_item.done","output_index":1,"item":{"type":"function_call","id":"fc_1","call_id":"c1","name":"ping","arguments":"{}"}} , \\{"type":"response.completed","response":{"usage":{"input_tokens":1,"output_tokens":1}}} , @@ -654,3 +1016,171 @@ test "responses stream: text then tool call keeps block order" { try testing.expectEqualStrings("ping", asst.content.items[1].ToolUse.name); try testing.expectEqualStrings("{}", asst.content.items[1].ToolUse.input.items); } + +/// Drive raw SSE bytes through the same path `produce` uses live: feed +/// arbitrary byte chunks to the `SSEParser`, decode each event, and drain the +/// `EventQueue` after every chunk (which resets its arena). This exercises +/// payload lifetimes the all-at-once `runStreamedTurn` helper does not. +fn runRawStream( + allocator: Allocator, + conv: *conversation.Conversation, + raw: []const u8, + chunk_len: usize, +) !void { + var parser = sse_mod.SSEParser.init(allocator); + defer parser.deinit(); + var state: StreamState = .init(allocator); + defer state.deinit(); + var queue = EventQueue.init(allocator); + defer queue.deinit(); + + var off: usize = 0; + var done = false; + while (off < raw.len and !done) { + const end = @min(off + chunk_len, raw.len); + const events = try parser.feed(raw[off..end]); + defer parser.freeEvents(events); + off = end; + for (events) |payload| { + if (try handleEvent(allocator, payload, &state, &queue)) { + done = true; + break; + } + } + // Drain (and reset the arena) between chunks, as the agent loop does. + while (queue.pop()) |_| {} + } + try state.finalize(&queue, conv); + while (queue.pop()) |_| {} +} + +test "responses stream: realistic codex SSE assembles tool input across chunks" { + // A representative ChatGPT-Codex function-call stream: explicit `event:` + // lines, args streamed as `function_call_arguments.delta` fragments, and + // terminal `done`/`completed` events that restate `arguments` as "". + // Sliced into small byte chunks so events straddle reads and the queue + // arena resets mid-stream. + const allocator = testing.allocator; + + const raw = + "event: response.created\n" ++ + "data: {\"type\":\"response.created\",\"response\":{\"id\":\"resp_1\"}}\n\n" ++ + "event: response.output_item.added\n" ++ + "data: {\"type\":\"response.output_item.added\",\"output_index\":0,\"item\":{\"id\":\"fc_1\",\"type\":\"function_call\",\"status\":\"in_progress\",\"arguments\":\"\",\"call_id\":\"call_9\",\"name\":\"std__read\"}}\n\n" ++ + "event: response.function_call_arguments.delta\n" ++ + "data: {\"type\":\"response.function_call_arguments.delta\",\"item_id\":\"fc_1\",\"output_index\":0,\"delta\":\"{\\\"path\\\":\"}\n\n" ++ + "event: response.function_call_arguments.delta\n" ++ + "data: {\"type\":\"response.function_call_arguments.delta\",\"item_id\":\"fc_1\",\"output_index\":0,\"delta\":\"\\\"/tmp/x\\\"}\"}\n\n" ++ + "event: response.function_call_arguments.done\n" ++ + "data: {\"type\":\"response.function_call_arguments.done\",\"item_id\":\"fc_1\",\"output_index\":0,\"arguments\":\"\"}\n\n" ++ + "event: response.output_item.done\n" ++ + "data: {\"type\":\"response.output_item.done\",\"output_index\":0,\"item\":{\"id\":\"fc_1\",\"type\":\"function_call\",\"status\":\"completed\",\"arguments\":\"\",\"call_id\":\"call_9\",\"name\":\"std__read\"}}\n\n" ++ + "event: response.completed\n" ++ + "data: {\"type\":\"response.completed\",\"response\":{\"output\":[{\"id\":\"fc_1\",\"type\":\"function_call\",\"status\":\"completed\",\"arguments\":\"{\\\"path\\\":\\\"/tmp/x\\\"}\",\"call_id\":\"call_9\",\"name\":\"std__read\"}],\"usage\":{\"input_tokens\":10,\"output_tokens\":5}}}\n\n"; + + // Try several chunk sizes so events land on different read boundaries. + for ([_]usize{ 1, 7, 64, raw.len }) |chunk_len| { + var c = conversation.Conversation.init(allocator); + defer c.deinit(); + try addUserText(&c, "read the file"); + try runRawStream(allocator, &c, raw, chunk_len); + + const asst = c.messages.items[1]; + try testing.expectEqual(@as(usize, 1), asst.content.items.len); + const tu = asst.content.items[0].ToolUse; + try testing.expectEqualStrings("call_9", tu.id); + try testing.expectEqualStrings("std.read", tu.name); + try testing.expectEqualStrings("{\"path\":\"/tmp/x\"}", tu.input.items); + } +} + +test "responses stream: parallel function calls assemble distinct ToolUse blocks" { + // The model emits several function calls in one turn, each with its own + // item_id / output_index / call_id, with their argument deltas + // interleaved. Each must become its own ToolUse block with the right + // input — no cross-talk between calls. + const allocator = testing.allocator; + var conv = conversation.Conversation.init(allocator); + defer conv.deinit(); + try addUserText(&conv, "ls three dirs"); + + const events = [_][]const u8{ + \\{"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_a","call_id":"call_a","name":"std__shell","arguments":""}} + , + \\{"type":"response.output_item.added","output_index":1,"item":{"type":"function_call","id":"fc_b","call_id":"call_b","name":"std__shell","arguments":""}} + , + \\{"type":"response.function_call_arguments.delta","item_id":"fc_a","output_index":0,"delta":"{\"command\":\"ls a\"}"} + , + \\{"type":"response.function_call_arguments.delta","item_id":"fc_b","output_index":1,"delta":"{\"command\":"} + , + \\{"type":"response.output_item.added","output_index":2,"item":{"type":"function_call","id":"fc_c","call_id":"call_c","name":"std__shell","arguments":""}} + , + \\{"type":"response.function_call_arguments.delta","item_id":"fc_b","output_index":1,"delta":"\"ls b\"}"} + , + \\{"type":"response.function_call_arguments.delta","item_id":"fc_c","output_index":2,"delta":"{\"command\":\"ls c\"}"} + , + \\{"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","id":"fc_a","call_id":"call_a","name":"std__shell","arguments":""}} + , + \\{"type":"response.output_item.done","output_index":1,"item":{"type":"function_call","id":"fc_b","call_id":"call_b","name":"std__shell","arguments":""}} + , + \\{"type":"response.output_item.done","output_index":2,"item":{"type":"function_call","id":"fc_c","call_id":"call_c","name":"std__shell","arguments":""}} + , + \\{"type":"response.completed","response":{"output":[{"type":"function_call","id":"fc_a","call_id":"call_a","name":"std__shell","arguments":"{\"command\":\"ls a\"}"},{"type":"function_call","id":"fc_b","call_id":"call_b","name":"std__shell","arguments":"{\"command\":\"ls b\"}"},{"type":"function_call","id":"fc_c","call_id":"call_c","name":"std__shell","arguments":"{\"command\":\"ls c\"}"}],"usage":{"input_tokens":5,"output_tokens":9}}} + , + }; + try runStreamedTurn(allocator, &conv, null, &events); + + const asst = conv.messages.items[1]; + try testing.expectEqual(@as(usize, 3), asst.content.items.len); + try testing.expectEqualStrings("call_a", asst.content.items[0].ToolUse.id); + try testing.expectEqualStrings("{\"command\":\"ls a\"}", asst.content.items[0].ToolUse.input.items); + try testing.expectEqualStrings("call_b", asst.content.items[1].ToolUse.id); + try testing.expectEqualStrings("{\"command\":\"ls b\"}", asst.content.items[1].ToolUse.input.items); + try testing.expectEqualStrings("call_c", asst.content.items[2].ToolUse.id); + try testing.expectEqualStrings("{\"command\":\"ls c\"}", asst.content.items[2].ToolUse.input.items); + for (asst.content.items) |b| try testing.expectEqualStrings("std.shell", b.ToolUse.name); +} + +test "responses stream: one call with a mutating item_id stays a single block" { + // Regression for the GitHub Copilot Responses proxy: it emits a *fresh*, + // opaque `item_id` on every event for the SAME call — the + // `output_item.added`, each `…arguments.delta`, the `…arguments.done`, the + // `output_item.done`, and the `response.completed` restatement all carry + // different `item_id`s. Only `output_index` (and `call_id`) are stable. + // Keying tool calls by `item_id` made dedup miss, fanning this single call + // out into three identical ToolUse blocks sharing one `call_id` (which in + // turn stranded two UI result boxes at the `(…)` placeholder). Keying by + // `output_index` must collapse it back to exactly one block. A leading + // reasoning item (also with mutating ids) must not spawn a phantom tool. + const allocator = testing.allocator; + var conv = conversation.Conversation.init(allocator); + defer conv.deinit(); + try addUserText(&conv, "what's in the current directory?"); + + const events = [_][]const u8{ + \\{"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"rsn_AAAA"}} + , + \\{"type":"response.output_item.done","output_index":0,"item":{"type":"reasoning","id":"rsn_BBBB"}} + , + \\{"type":"response.output_item.added","output_index":1,"item":{"type":"function_call","id":"itm_AAAA","call_id":"call_KOEB","name":"std__shell","arguments":""}} + , + \\{"type":"response.function_call_arguments.delta","output_index":1,"item_id":"itm_BBBB","delta":"{\"command\":"} + , + \\{"type":"response.function_call_arguments.delta","output_index":1,"item_id":"itm_CCCC","delta":"\"pwd && ls -la\"}"} + , + \\{"type":"response.function_call_arguments.done","output_index":1,"item_id":"itm_DDDD","arguments":""} + , + \\{"type":"response.output_item.done","output_index":1,"item":{"type":"function_call","id":"itm_EEEE","call_id":"call_KOEB","name":"std__shell","arguments":"{\"command\":\"pwd && ls -la\"}"}} + , + \\{"type":"response.completed","response":{"output":[{"type":"reasoning","id":"rsn_CCCC"},{"type":"function_call","id":"itm_FFFF","call_id":"call_KOEB","name":"std__shell","arguments":"{\"command\":\"pwd && ls -la\"}"}],"usage":{"input_tokens":5,"output_tokens":9}}} + , + }; + try runStreamedTurn(allocator, &conv, null, &events); + + const asst = conv.messages.items[1]; + try testing.expectEqual(@as(usize, 1), asst.content.items.len); + const tu = asst.content.items[0].ToolUse; + try testing.expectEqualStrings("call_KOEB", tu.id); + try testing.expectEqualStrings("std.shell", tu.name); + try testing.expectEqualStrings("{\"command\":\"pwd && ls -la\"}", tu.input.items); +} diff --git a/libpanto/src/public.zig b/libpanto/src/public.zig index 339c6f1..8a55e47 100644 --- a/libpanto/src/public.zig +++ b/libpanto/src/public.zig @@ -131,6 +131,7 @@ pub const TextualBlock = conversation_mod.TextualBlock; /// for binding code to construct `.Text`/`.Thinking`/tool-result text when /// assembling `ContentBlock`s by hand (e.g. for conversation resumption). pub const textualBlockFromSlice = conversation_mod.textualBlockFromSlice; +pub const SignatureOrigin = conversation_mod.SignatureOrigin; pub const ThinkingBlock = conversation_mod.ThinkingBlock; pub const ToolUseBlock = conversation_mod.ToolUseBlock; pub const ToolResultBlock = conversation_mod.ToolResultBlock; |
