@@ -4,93 +4,121 @@
44 const std = @import("std");
55 const api = @import("../api.zig");
66 const types = @import("../types.zig");
7-const json = @import("../json.zig");
87
98 const anthropic_version = "2023-06-01";
109
1110 // ---- request building -----------------------------------------------------
1211
13-fn writeImage(w: *std.Io.Writer, img: types.ImageContent) !void {
14- try w.writeAll("{\"type\":\"image\",\"source\":{\"type\":\"base64\",\"media_type\":");
15- try json.writeString(w, img.mime_type);
16- try w.writeAll(",\"data\":");
17- try json.writeString(w, img.data);
18- try w.writeAll("}}");
19-}
2012
21-/// A tool result is one block of a user message. Its content is a string when
22-/// there is only text, and an array when images must ride along.
23-fn writeToolResult(w: *std.Io.Writer, t: types.ToolResultMessage) !void {
24- try w.writeAll("{\"type\":\"tool_result\",\"tool_use_id\":");
25- try json.writeString(w, t.tool_call_id);
26- if (t.is_error) try w.writeAll(",\"is_error\":true");
27- if (t.images.len == 0) {
28- try w.writeAll(",\"content\":");
29- try json.writeString(w, t.text);
30- } else {
31- try w.writeAll(",\"content\":[{\"type\":\"text\",\"text\":");
32- try json.writeString(w, t.text);
33- try w.writeByte('}');
34- for (t.images) |img| {
35- try w.writeByte(',');
36- try writeImage(w, img);
13+const ImageSource = struct {
14+ @"type": []const u8 = "base64",
15+ @"media_type": []const u8,
16+ data: []const u8,
17+};
18+
19+const ImageBlock = struct {
20+ @"type": []const u8 = "image",
21+ source: ImageSource,
22+};
23+
24+const TextBlock = struct { @"type": []const u8 = "text", text: []const u8 };
25+
26+const ThinkingBlock = struct {
27+ @"type": []const u8 = "thinking",
28+ thinking: []const u8,
29+ signature: []const u8,
30+};
31+
32+const ToolUseBlock = struct {
33+ @"type": []const u8 = "tool_use",
34+ id: []const u8,
35+ name: []const u8,
36+ input: api.Raw,
37+};
38+
39+/// One block of a tool_result's content: text or an image.
40+const ResultContent = union(enum) {
41+ text: TextBlock,
42+ image: ImageBlock,
43+
44+ pub fn jsonStringify(self: ResultContent, jws: anytype) !void {
45+ switch (self) {
46+ inline else => |v| try jws.write(v),
3747 }
38- try w.writeByte(']');
3948 }
40- try w.writeByte('}');
41-}
49+};
4250
43-/// An assistant turn as content blocks. Thinking blocks carry their signature
44-/// back so the provider's continuation data survives the round trip; a
45-/// thinking block without a signature becomes plain text.
46-fn writeAssistant(w: *std.Io.Writer, am: *const types.AssistantMessage) !void {
47- try w.writeAll("{\"role\":\"assistant\",\"content\":[");
48- var first = true;
49- for (am.content.items) |block| switch (block) {
50- .text => |t| {
51- if (!first) try w.writeByte(',');
52- first = false;
53- try w.writeAll("{\"type\":\"text\",\"text\":");
54- try json.writeString(w, t);
55- try w.writeByte('}');
56- },
57- .thinking => |t| {
58- if (!first) try w.writeByte(',');
59- first = false;
60- if (t.signature) |sig| {
61- if (sig.len > 0) {
62- try w.writeAll("{\"type\":\"thinking\",\"thinking\":");
63- try json.writeString(w, t.text);
64- try w.writeAll(",\"signature\":");
65- try json.writeString(w, sig);
66- try w.writeByte('}');
67- continue;
68- }
51+/// A tool result as one block of a user message. Its content is a string when
52+/// there is only text, and an array when images must ride along.
53+const ToolResultBlock = struct {
54+ @"type": []const u8 = "tool_result",
55+ @"tool_use_id": []const u8,
56+ @"is_error": ?bool = null,
57+ content: Content,
58+ const Content = union(enum) {
59+ text: []const u8,
60+ parts: []const ResultContent,
61+
62+ pub fn jsonStringify(self: Content, jws: anytype) !void {
63+ switch (self) {
64+ .text => |v| try jws.write(v),
65+ .parts => |v| try jws.write(v),
6966 }
70- try w.writeAll("{\"type\":\"text\",\"text\":");
71- try json.writeString(w, t.text);
72- try w.writeByte('}');
73- },
74- .tool_call => |c| {
75- if (!first) try w.writeByte(',');
76- first = false;
77- try w.writeAll("{\"type\":\"tool_use\",\"id\":");
78- try json.writeString(w, c.id);
79- try w.writeAll(",\"name\":");
80- try json.writeString(w, c.name);
81- try w.writeAll(",\"input\":");
82- try w.writeAll(api.argumentsOrObject(c.arguments));
83- try w.writeByte('}');
84- },
67+ }
8568 };
86- try w.writeAll("]}");
87-}
69+};
8870
89-fn writeUser(w: *std.Io.Writer, u: types.Message) !void {
90- try w.writeAll("{\"role\":\"user\",\"content\":[{\"type\":\"text\",\"text\":");
91- try json.writeString(w, u.user.content);
92- try w.writeAll("}]}");
93-}
71+/// A content block of an assistant turn. A thinking block carries its
72+/// signature back so the provider's continuation data survives the round trip;
73+/// a thinking block without one becomes plain text.
74+const AssistantBlock = union(enum) {
75+ text: TextBlock,
76+ thinking: ThinkingBlock,
77+ plain: TextBlock,
78+ tool_use: ToolUseBlock,
79+
80+ pub fn jsonStringify(self: AssistantBlock, jws: anytype) !void {
81+ switch (self) {
82+ inline else => |v| try jws.write(v),
83+ }
84+ }
85+};
86+
87+const UserMessage = struct {
88+ role: []const u8 = "user",
89+ content: []const ResultContent,
90+};
91+
92+const AssistantMessage = struct {
93+ role: []const u8 = "assistant",
94+ content: []const AssistantBlock,
95+};
96+
97+/// One entry of the `messages` array: a user turn (with tool results) or an
98+/// assistant turn.
99+const Message = union(enum) {
100+ user_text: struct { role: []const u8 = "user", content: []const TextBlock },
101+ user_results: struct { role: []const u8 = "user", content: []const ToolResultBlock },
102+ assistant: AssistantMessage,
103+
104+ pub fn jsonStringify(self: Message, jws: anytype) !void {
105+ switch (self) {
106+ inline else => |v| try jws.write(v),
107+ }
108+ }
109+};
110+
111+const Thinking = struct { @"type": []const u8 = "enabled", @"budget_tokens": u64 };
112+
113+const Body = struct {
114+ model: []const u8,
115+ @"max_tokens": u64,
116+ stream: bool = true,
117+ system: ?[]const u8 = null,
118+ thinking: ?Thinking = null,
119+ messages: []const Message,
120+ tools: ?api.Tools = null,
121+};
94122
95123 /// Anthropic thinking budget per effort. Anthropic requires max_tokens to
96124 /// exceed the budget, so the caller raises max_tokens when it does not.
@@ -109,67 +137,92 @@ fn budgetFor(effort: []const u8) u64 {
109137 return 0;
110138 }
111139
112-fn buildBody(req: api.Request) ![]u8 {
113- var out: std.Io.Writer.Allocating = .init(req.scratch);
114- const w = &out.writer;
115-
116- const budget = budgetFor(req.effort);
117- var max_tokens: u64 = if (req.model.max_tokens > 0) req.model.max_tokens else 8192;
118- if (budget > 0 and max_tokens <= budget) max_tokens = budget + 4096;
119-
120- try w.writeAll("{\"model\":");
121- try json.writeString(w, req.model.id);
122- try w.writeAll(",\"max_tokens\":");
123- try json.writeNum(w, max_tokens);
124- try w.writeAll(",\"stream\":true");
125- if (req.system_prompt.len > 0) {
126- try w.writeAll(",\"system\":");
127- try json.writeString(w, req.system_prompt);
128- }
129- if (budget > 0) {
130- try w.writeAll(",\"thinking\":{\"type\":\"enabled\",\"budget_tokens\":");
131- try json.writeNum(w, budget);
132- try w.writeByte('}');
140+fn resultContent(a: std.mem.Allocator, t: types.ToolResultMessage) !ToolResultBlock.Content {
141+ if (t.images.len == 0) return .{ .text = t.text };
142+ var parts: std.ArrayList(ResultContent) = .empty;
143+ try parts.append(a, .{ .text = .{ .text = t.text } });
144+ for (t.images) |img| {
145+ try parts.append(a, .{ .image = .{ .source = .{ .@"media_type" = img.mime_type, .data = img.data } } });
133146 }
147+ return .{ .parts = parts.items };
148+}
149+
150+fn assistantBlocks(a: std.mem.Allocator, am: *const types.AssistantMessage) ![]const AssistantBlock {
151+ var blocks: std.ArrayList(AssistantBlock) = .empty;
152+ for (am.content.items) |block| switch (block) {
153+ .text => |t| try blocks.append(a, .{ .text = .{ .text = t } }),
154+ .thinking => |t| {
155+ if (t.signature) |sig| {
156+ if (sig.len > 0) {
157+ try blocks.append(a, .{ .thinking = .{ .thinking = t.text, .signature = sig } });
158+ continue;
159+ }
160+ }
161+ try blocks.append(a, .{ .plain = .{ .text = t.text } });
162+ },
163+ .tool_call => |c| try blocks.append(a, .{ .tool_use = .{
164+ .id = c.id,
165+ .name = c.name,
166+ .input = .{ .bytes = api.argumentsOrObject(c.arguments) },
167+ } }),
168+ };
169+ return blocks.items;
170+}
134171
135- try w.writeAll(",\"messages\":[");
136- var first = true;
172+fn buildMessages(a: std.mem.Allocator, req: api.Request) ![]const Message {
173+ var out: std.ArrayList(Message) = .empty;
137174 var i: usize = 0;
138175 while (i < req.messages.len) {
139176 if (req.messages[i] == .tool_result) {
140177 // Every result of one assistant turn shares a single user message.
141- if (!first) try w.writeByte(',');
142- first = false;
143- try w.writeAll("{\"role\":\"user\",\"content\":[");
144- var first_block = true;
178+ var blocks: std.ArrayList(ToolResultBlock) = .empty;
145179 while (i < req.messages.len and req.messages[i] == .tool_result) : (i += 1) {
146- if (!first_block) try w.writeByte(',');
147- first_block = false;
148- try writeToolResult(w, req.messages[i].tool_result);
180+ const t = req.messages[i].tool_result;
181+ try blocks.append(a, .{
182+ .@"tool_use_id" = t.tool_call_id,
183+ .@"is_error" = if (t.is_error) true else null,
184+ .content = try resultContent(a, t),
185+ });
149186 }
150- try w.writeAll("]}");
187+ try out.append(a, .{ .user_results = .{ .content = blocks.items } });
151188 continue;
152189 }
153- if (!first) try w.writeByte(',');
154- first = false;
155190 switch (req.messages[i]) {
156- .user => try writeUser(w, req.messages[i]),
157- .assistant => |am| try writeAssistant(w, am),
191+ .user => |u| {
192+ const blocks = try a.alloc(TextBlock, 1);
193+ blocks[0] = .{ .text = u.content };
194+ try out.append(a, .{ .user_text = .{ .content = blocks } });
195+ },
196+ .assistant => |am| try out.append(a, .{ .assistant = .{
197+ .content = try assistantBlocks(a, am),
198+ } }),
158199 .tool_result => unreachable,
159200 }
160201 i += 1;
161202 }
162- try w.writeByte(']');
163-
164- try api.writeTools(w, req.scratch, req.tools_json, .{
165- .open = ",\"tools\":[",
166- .entry = "{",
167- .params_key = "input_schema",
168- .entry_close = "}",
169- .close = "]",
170- });
171- try w.writeByte('}');
172- return out.written();
203+ return out.items;
204+}
205+
206+fn buildBody(req: api.Request) ![]u8 {
207+ const a = req.scratch;
208+ const budget = budgetFor(req.effort);
209+ var max_tokens: u64 = if (req.model.max_tokens > 0) req.model.max_tokens else 8192;
210+ if (budget > 0 and max_tokens <= budget) max_tokens = budget + 4096;
211+ const body = Body{
212+ .model = req.model.id,
213+ .@"max_tokens" = max_tokens,
214+ .system = if (req.system_prompt.len > 0) req.system_prompt else null,
215+ .thinking = if (budget > 0) .{ .@"budget_tokens" = budget } else null,
216+ .messages = try buildMessages(a, req),
217+ .tools = if (req.tools_json.len > 0) .{ .a = a, .json = req.tools_json, .form = .{
218+ .open = "[",
219+ .entry = "{",
220+ .params_key = "input_schema",
221+ .entry_close = "}",
222+ .close = "]",
223+ } } else null,
224+ };
225+ return std.json.Stringify.valueAlloc(a, body, .{ .emit_null_optional_fields = false });
173226 }
174227
175228 // ---- streaming state ------------------------------------------------------
@@ -184,6 +237,45 @@ const Block = struct {
184237 call: usize = 0,
185238 };
186239
240+const Chunk = struct {
241+ @"type": ?[]const u8 = null,
242+ @"error": ?api.ErrField = null,
243+ index: ?i64 = null,
244+ message: ?MessageStart = null,
245+ @"content_block": ?BlockStart = null,
246+ delta: ?Delta = null,
247+ usage: ?Usage = null,
248+
249+ const MessageStart = struct {
250+ id: ?[]const u8 = null,
251+ usage: ?Usage = null,
252+ };
253+
254+ const BlockStart = struct {
255+ @"type": ?[]const u8 = null,
256+ id: ?[]const u8 = null,
257+ name: ?[]const u8 = null,
258+ data: ?[]const u8 = null,
259+ };
260+
261+ const Delta = struct {
262+ @"type": ?[]const u8 = null,
263+ text: ?[]const u8 = null,
264+ thinking: ?[]const u8 = null,
265+ signature: ?[]const u8 = null,
266+ @"partial_json": ?[]const u8 = null,
267+ @"stop_reason": ?[]const u8 = null,
268+ };
269+
270+ const Usage = struct {
271+ @"input_tokens": api.Count = .{},
272+ @"output_tokens": api.Count = .{},
273+ @"cache_read_input_tokens": api.Count = .{},
274+ @"cache_creation_input_tokens": api.Count = .{},
275+ @"output_tokens_details": ?struct { @"thinking_tokens": api.Count = .{} } = null,
276+ };
277+};
278+
187279 const State = struct {
188280 req: api.Request,
189281 sink: api.Sink,
@@ -197,43 +289,48 @@ const State = struct {
197289 /// Set by `run` when the stream did not run to completion.
198290 failed: bool = false,
199291
200- pub fn handle(st: *State, obj: std.json.ObjectMap) !void {
201- const event_type = api.stringField(obj, "type") orelse "";
292+ pub fn handle(st: *State, chunk: Chunk) !void {
293+ const event_type = chunk.@"type" orelse "";
202294
203295 if (std.mem.eql(u8, event_type, "error")) {
204- if (st.stream_error == null) st.stream_error = try st.req.pers.dupe(u8, api.errorMessage(obj, "error") orelse "stream: provider error");
296+ const message = if (chunk.@"error") |e| e.message orelse "stream: provider error" else "stream: provider error";
297+ if (st.stream_error == null) st.stream_error = try st.req.pers.dupe(u8, message);
205298 return;
206299 }
207300
208301 if (std.mem.eql(u8, event_type, "message_start")) {
209- const message = api.objField(obj, "message") orelse return;
302+ const message = chunk.message orelse return;
210303 if (st.msg.response_id == null) {
211- if (api.stringField(message, "id")) |id| st.msg.response_id = try st.req.pers.dupe(u8, id);
304+ if (message.id) |id| {
305+ if (id.len > 0) st.msg.response_id = try st.req.pers.dupe(u8, id);
306+ }
212307 }
213- if (api.objField(message, "usage")) |u| {
214- st.usage.input = api.numField(u, "input_tokens");
215- st.usage.output = api.numField(u, "output_tokens");
216- st.usage.cache_read = api.numField(u, "cache_read_input_tokens");
217- st.usage.cache_write = api.numField(u, "cache_creation_input_tokens");
308+ if (message.usage) |u| {
309+ st.usage.input = u.@"input_tokens".value;
310+ st.usage.output = u.@"output_tokens".value;
311+ st.usage.cache_read = u.@"cache_read_input_tokens".value;
312+ st.usage.cache_write = u.@"cache_creation_input_tokens".value;
218313 }
219314 return;
220315 }
221316
222317 if (std.mem.eql(u8, event_type, "content_block_start")) {
223- const block = try api.blockAt(Block, &st.blocks, st.req.pers, api.intOr(obj, "index", -1));
224- const cb = api.objField(obj, "content_block") orelse return;
225- const bt = api.stringField(cb, "type") orelse "";
318+ const block = try api.blockAt(Block, &st.blocks, st.req.pers, chunk.index orelse -1);
319+ const cb = chunk.@"content_block" orelse return;
320+ const bt = cb.@"type" orelse "";
226321 if (std.mem.eql(u8, bt, "tool_use")) {
227322 block.kind = .tool;
228323 block.call = st.calls.items.len;
229324 const call = try st.calls.addOne(st.req.pers);
230325 call.* = .{};
231- if (api.stringField(cb, "id")) |id| call.id = try st.req.pers.dupe(u8, id);
232- try call.announce(st.req.pers, st.sink, api.stringField(cb, "name") orelse "");
326+ if (cb.id) |id| {
327+ if (id.len > 0) call.id = try st.req.pers.dupe(u8, id);
328+ }
329+ try call.announce(st.req.pers, st.sink, cb.name orelse "");
233330 } else if (std.mem.eql(u8, bt, "thinking") or std.mem.eql(u8, bt, "redacted_thinking")) {
234331 block.kind = .thinking;
235332 if (std.mem.eql(u8, bt, "redacted_thinking")) {
236- if (api.stringField(cb, "data")) |d| try block.signature.appendSlice(st.req.pers, d);
333+ if (cb.data) |d| try block.signature.appendSlice(st.req.pers, d);
237334 }
238335 } else {
239336 block.kind = .text;
@@ -242,48 +339,44 @@ const State = struct {
242339 }
243340
244341 if (std.mem.eql(u8, event_type, "content_block_delta")) {
245- const idx = api.intOr(obj, "index", -1);
342+ const idx = chunk.index orelse -1;
246343 if (idx < 0 or idx >= st.blocks.items.len) return;
247- const delta = api.objField(obj, "delta") orelse return;
344+ const delta = chunk.delta orelse return;
248345 const block = &st.blocks.items[@intCast(idx)];
249346 const call = if (block.kind == .tool) &st.calls.items[block.call] else null;
250- const dt = api.stringField(delta, "type") orelse "";
347+ const dt = delta.@"type" orelse "";
251348 if (std.mem.eql(u8, dt, "text_delta")) {
252- if (api.stringField(delta, "text")) |s| {
349+ if (delta.text) |s| {
253350 try block.text.appendSlice(st.req.pers, s);
254351 st.sink.emit(.{ .text = s });
255352 }
256353 } else if (std.mem.eql(u8, dt, "thinking_delta")) {
257- if (api.stringField(delta, "thinking")) |s| {
354+ if (delta.thinking) |s| {
258355 try block.text.appendSlice(st.req.pers, s);
259356 st.sink.emit(.{ .reasoning = s });
260357 }
261358 } else if (std.mem.eql(u8, dt, "signature_delta")) {
262- if (api.stringField(delta, "signature")) |s| {
263- try block.signature.appendSlice(st.req.pers, s);
264- }
359+ if (delta.signature) |s| try block.signature.appendSlice(st.req.pers, s);
265360 } else if (std.mem.eql(u8, dt, "input_json_delta")) {
266361 if (call) |c| {
267- if (api.stringField(delta, "partial_json")) |s| try c.args.appendSlice(st.req.pers, s);
362+ if (delta.@"partial_json") |s| try c.args.appendSlice(st.req.pers, s);
268363 }
269364 }
270365 return;
271366 }
272367
273368 if (std.mem.eql(u8, event_type, "message_delta")) {
274- if (api.objField(obj, "delta")) |delta| {
275- if (api.stringField(delta, "stop_reason")) |s| {
276- st.stop_reason = try st.req.pers.dupe(u8, s);
369+ if (chunk.delta) |delta| {
370+ if (delta.@"stop_reason") |s| {
371+ if (s.len > 0) st.stop_reason = try st.req.pers.dupe(u8, s);
277372 }
278373 }
279- if (api.objField(obj, "usage")) |u| {
280- if (api.optNumField(u, "input_tokens")) |v| st.usage.input = v;
281- if (api.optNumField(u, "output_tokens")) |v| st.usage.output = v;
282- if (api.optNumField(u, "cache_read_input_tokens")) |v| st.usage.cache_read = v;
283- if (api.optNumField(u, "cache_creation_input_tokens")) |v| st.usage.cache_write = v;
284- if (api.objField(u, "output_tokens_details")) |d| {
285- st.usage.reasoning = api.optNumField(d, "thinking_tokens");
286- }
374+ if (chunk.usage) |u| {
375+ st.usage.input = u.@"input_tokens".value;
376+ st.usage.output = u.@"output_tokens".value;
377+ st.usage.cache_read = u.@"cache_read_input_tokens".value;
378+ st.usage.cache_write = u.@"cache_creation_input_tokens".value;
379+ if (u.@"output_tokens_details") |d| st.usage.reasoning = d.@"thinking_tokens".value;
287380 }
288381 return;
289382 }
@@ -346,5 +439,5 @@ const wire = api.Wire{
346439 };
347440
348441 pub fn stream(req: api.Request, sink: api.Sink) std.mem.Allocator.Error!types.AssistantMessage {
349- return api.run(State, wire, req, sink, finalize);
442+ return api.run(State, Chunk, wire, req, sink, finalize);
350443 }