@@ -4,16 +4,10 @@
44 const std = @import("std");
55 const api = @import("../api.zig");
66 const types = @import("../types.zig");
7-const http = @import("../http.zig");
87 const json = @import("../json.zig");
98
10-const Value = std.json.Value;
119 const anthropic_version = "2023-06-01";
1210
13-/// An untrusted stream index is a hint, never a size. Anything past this is
14-/// treated like a missing index so a hostile chunk cannot force an allocation.
15-const max_stream_index = 10_000;
16-
1711 // ---- request building -----------------------------------------------------
1812
1913 fn writeImage(w: *std.Io.Writer, img: types.ImageContent) !void {
@@ -77,11 +71,6 @@ fn writeAssistant(w: *std.Io.Writer, am: *const types.AssistantMessage) !void {
7771 try json.writeString(w, t.text);
7872 try w.writeByte('}');
7973 },
80- .image => |img| {
81- if (!first) try w.writeByte(',');
82- first = false;
83- try writeImage(w, img);
84- },
8574 .tool_call => |c| {
8675 if (!first) try w.writeByte(',');
8776 first = false;
@@ -90,7 +79,7 @@ fn writeAssistant(w: *std.Io.Writer, am: *const types.AssistantMessage) !void {
9079 try w.writeAll(",\"name\":");
9180 try json.writeString(w, c.name);
9281 try w.writeAll(",\"input\":");
93- try w.writeAll(if (c.arguments.len > 0) c.arguments else "{}");
82+ try w.writeAll(api.argumentsOrObject(c.arguments));
9483 try w.writeByte('}');
9584 },
9685 };
@@ -103,18 +92,6 @@ fn writeUser(w: *std.Io.Writer, u: types.Message) !void {
10392 try w.writeAll("}]}");
10493 }
10594
106-fn writeTools(w: *std.Io.Writer, a: std.mem.Allocator, tools_json: []const u8, params_key: []const u8) !void {
107- const tools = api.parseTools(a, tools_json) orelse return;
108- try w.writeAll(",\"tools\":[");
109- for (tools, 0..) |tool, i| {
110- if (i > 0) try w.writeByte(',');
111- try w.writeByte('{');
112- try api.writeToolBody(w, tool.object, params_key);
113- try w.writeByte('}');
114- }
115- try w.writeByte(']');
116-}
117-
11895 /// Anthropic thinking budget per effort. Anthropic requires max_tokens to
11996 /// exceed the budget, so the caller raises max_tokens when it does not.
12097 fn budgetFor(effort: []const u8) u64 {
@@ -143,7 +120,7 @@ fn buildBody(req: api.Request) ![]u8 {
143120 try w.writeAll("{\"model\":");
144121 try json.writeString(w, req.model.id);
145122 try w.writeAll(",\"max_tokens\":");
146- try json.writeUint(w, max_tokens);
123+ try json.writeNum(w, max_tokens);
147124 try w.writeAll(",\"stream\":true");
148125 if (req.system_prompt.len > 0) {
149126 try w.writeAll(",\"system\":");
@@ -151,7 +128,7 @@ fn buildBody(req: api.Request) ![]u8 {
151128 }
152129 if (budget > 0) {
153130 try w.writeAll(",\"thinking\":{\"type\":\"enabled\",\"budget_tokens\":");
154- try json.writeUint(w, budget);
131+ try json.writeNum(w, budget);
155132 try w.writeByte('}');
156133 }
157134
@@ -184,22 +161,17 @@ fn buildBody(req: api.Request) ![]u8 {
184161 }
185162 try w.writeByte(']');
186163
187- try writeTools(w, req.scratch, req.tools_json, "input_schema");
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+ });
188171 try w.writeByte('}');
189172 return out.written();
190173 }
191174
192-fn buildUrl(a: std.mem.Allocator, base: []const u8) ![]u8 {
193- return api.buildUrl(a, base, "/messages");
194-}
195-
196-fn headers(a: std.mem.Allocator, req: api.Request, key: []const u8) ![]http.Header {
197- return api.headers(a, req, &.{
198- .{ "anthropic-version", anthropic_version },
199- .{ "x-api-key", key },
200- });
201-}
202-
203175 // ---- streaming state ------------------------------------------------------
204176
205177 const BlockKind = enum { text, thinking, tool };
@@ -208,10 +180,8 @@ const Block = struct {
208180 kind: BlockKind = .text,
209181 text: std.ArrayList(u8) = .empty,
210182 signature: std.ArrayList(u8) = .empty,
211- id: []const u8 = "",
212- name: []const u8 = "",
213- args: std.ArrayList(u8) = .empty,
214- started: bool = false,
183+ /// The call this block's arguments belong to, when `kind` is `.tool`.
184+ call: usize = 0,
215185 };
216186
217187 const State = struct {
@@ -220,166 +190,109 @@ const State = struct {
220190 arena: *std.heap.ArenaAllocator,
221191 msg: *types.AssistantMessage,
222192 blocks: std.ArrayList(Block) = .empty,
223- tokens_in: u64 = 0,
224- tokens_out: u64 = 0,
225- cache_read: u64 = 0,
226- cache_write: u64 = 0,
227- reasoning_tokens: ?u64 = null,
193+ calls: std.ArrayList(api.Call) = .empty,
194+ usage: types.Usage = .{},
228195 stop_reason: ?[]const u8 = null,
229196 stream_error: ?[]const u8 = null,
230-};
197+ /// Set by `run` when the stream did not run to completion.
198+ failed: bool = false,
231199
232-fn blockAt(st: *State, index: i64) !*Block {
233- const idx: usize = if (index >= 0 and index <= max_stream_index)
234- @intCast(index)
235- else
236- st.blocks.items.len;
237- while (st.blocks.items.len <= idx) try st.blocks.append(st.req.pers, .{});
238- return &st.blocks.items[idx];
239-}
240-
241-fn handle(st: *State, obj: std.json.ObjectMap) !void {
242- const event_type = api.stringOr(obj, "type");
200+ pub fn handle(st: *State, obj: std.json.ObjectMap) !void {
201+ const event_type = api.stringField(obj, "type") orelse "";
243202
244- if (std.mem.eql(u8, event_type, "error")) {
245- if (st.stream_error == null) st.stream_error = try st.req.pers.dupe(u8, errorMessage(obj));
246- return;
247- }
203+ 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");
205+ return;
206+ }
248207
249- if (std.mem.eql(u8, event_type, "message_start")) {
250- const message = obj.get("message") orelse return;
251- if (message != .object) return;
252- const message_obj = message.object;
253- if (st.msg.response_id == null) {
254- if (message_obj.get("id")) |v| {
255- if (v == .string) st.msg.response_id = try st.req.pers.dupe(u8, v.string);
208+ if (std.mem.eql(u8, event_type, "message_start")) {
209+ const message = api.objField(obj, "message") orelse return;
210+ if (st.msg.response_id == null) {
211+ if (api.stringField(message, "id")) |id| st.msg.response_id = try st.req.pers.dupe(u8, id);
256212 }
257- }
258- if (message_obj.get("usage")) |usage| {
259- if (usage == .object) {
260- st.tokens_in = api.numField(usage.object, "input_tokens");
261- st.tokens_out = api.numField(usage.object, "output_tokens");
262- st.cache_read = api.numField(usage.object, "cache_read_input_tokens");
263- st.cache_write = api.numField(usage.object, "cache_creation_input_tokens");
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");
264218 }
219+ return;
265220 }
266- return;
267- }
268221
269- if (std.mem.eql(u8, event_type, "content_block_start")) {
270- const raw = api.intOr(obj, "index", -1);
271- if (raw < 0 or raw > max_stream_index) return;
272- const block = try blockAt(st, raw);
273- const cb = obj.get("content_block") orelse return;
274- if (cb != .object) return;
275- const bt = api.stringOr(cb.object, "type");
276- if (std.mem.eql(u8, bt, "tool_use")) {
277- block.kind = .tool;
278- if (cb.object.get("id")) |id| {
279- if (id == .string) block.id = try st.req.pers.dupe(u8, id.string);
280- }
281- if (cb.object.get("name")) |name| {
282- if (name == .string) block.name = try st.req.pers.dupe(u8, name.string);
283- }
284- if (!block.started and block.name.len > 0) {
285- block.started = true;
286- st.sink.emit(.{ .tool_start = block.name });
287- }
288- } else if (std.mem.eql(u8, bt, "thinking") or std.mem.eql(u8, bt, "redacted_thinking")) {
289- block.kind = .thinking;
290- if (std.mem.eql(u8, bt, "redacted_thinking")) {
291- if (cb.object.get("data")) |d| {
292- if (d == .string) try block.signature.appendSlice(st.req.pers, d.string);
222+ 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 "";
226+ if (std.mem.eql(u8, bt, "tool_use")) {
227+ block.kind = .tool;
228+ block.call = st.calls.items.len;
229+ const call = try st.calls.addOne(st.req.pers);
230+ 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 "");
233+ } else if (std.mem.eql(u8, bt, "thinking") or std.mem.eql(u8, bt, "redacted_thinking")) {
234+ block.kind = .thinking;
235+ if (std.mem.eql(u8, bt, "redacted_thinking")) {
236+ if (api.stringField(cb, "data")) |d| try block.signature.appendSlice(st.req.pers, d);
293237 }
238+ } else {
239+ block.kind = .text;
294240 }
295- } else {
296- block.kind = .text;
241+ return;
297242 }
298- return;
299- }
300243
301- if (std.mem.eql(u8, event_type, "content_block_delta")) {
302- const idx = api.intOr(obj, "index", -1);
303- if (idx < 0 or idx >= st.blocks.items.len) return;
304- const delta = obj.get("delta") orelse return;
305- if (delta != .object) return;
306- const block = &st.blocks.items[@intCast(idx)];
307- const dt = api.stringOr(delta.object, "type");
308- if (std.mem.eql(u8, dt, "text_delta")) {
309- if (api.stringField(delta.object, "text")) |s| {
310- try block.text.appendSlice(st.req.pers, s);
311- st.sink.emit(.{ .text = s });
312- }
313- } else if (std.mem.eql(u8, dt, "thinking_delta")) {
314- if (api.stringField(delta.object, "thinking")) |s| {
315- try block.text.appendSlice(st.req.pers, s);
316- st.sink.emit(.{ .reasoning = s });
317- }
318- } else if (std.mem.eql(u8, dt, "signature_delta")) {
319- if (api.stringField(delta.object, "signature")) |s| {
320- try block.signature.appendSlice(st.req.pers, s);
321- }
322- } else if (std.mem.eql(u8, dt, "input_json_delta")) {
323- if (api.stringField(delta.object, "partial_json")) |s| {
324- try block.args.appendSlice(st.req.pers, s);
244+ if (std.mem.eql(u8, event_type, "content_block_delta")) {
245+ const idx = api.intOr(obj, "index", -1);
246+ if (idx < 0 or idx >= st.blocks.items.len) return;
247+ const delta = api.objField(obj, "delta") orelse return;
248+ const block = &st.blocks.items[@intCast(idx)];
249+ const call = if (block.kind == .tool) &st.calls.items[block.call] else null;
250+ const dt = api.stringField(delta, "type") orelse "";
251+ if (std.mem.eql(u8, dt, "text_delta")) {
252+ if (api.stringField(delta, "text")) |s| {
253+ try block.text.appendSlice(st.req.pers, s);
254+ st.sink.emit(.{ .text = s });
255+ }
256+ } else if (std.mem.eql(u8, dt, "thinking_delta")) {
257+ if (api.stringField(delta, "thinking")) |s| {
258+ try block.text.appendSlice(st.req.pers, s);
259+ st.sink.emit(.{ .reasoning = s });
260+ }
261+ } 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+ }
265+ } else if (std.mem.eql(u8, dt, "input_json_delta")) {
266+ if (call) |c| {
267+ if (api.stringField(delta, "partial_json")) |s| try c.args.appendSlice(st.req.pers, s);
268+ }
325269 }
270+ return;
326271 }
327- return;
328- }
329272
330- if (std.mem.eql(u8, event_type, "message_delta")) {
331- if (obj.get("delta")) |delta| {
332- if (delta == .object) {
333- if (api.stringField(delta.object, "stop_reason")) |s| {
273+ if (std.mem.eql(u8, event_type, "message_delta")) {
274+ if (api.objField(obj, "delta")) |delta| {
275+ if (api.stringField(delta, "stop_reason")) |s| {
334276 st.stop_reason = try st.req.pers.dupe(u8, s);
335277 }
336278 }
337- }
338- if (obj.get("usage")) |usage| {
339- if (usage == .object) {
340- if (usage.object.get("input_tokens") != null) st.tokens_in = api.numField(usage.object, "input_tokens");
341- if (usage.object.get("output_tokens") != null) st.tokens_out = api.numField(usage.object, "output_tokens");
342- if (usage.object.get("cache_read_input_tokens") != null) st.cache_read = api.numField(usage.object, "cache_read_input_tokens");
343- if (usage.object.get("cache_creation_input_tokens") != null) st.cache_write = api.numField(usage.object, "cache_creation_input_tokens");
344- if (usage.object.get("output_tokens_details")) |details| {
345- if (details == .object and details.object.get("thinking_tokens") != null) {
346- st.reasoning_tokens = api.numField(details.object, "thinking_tokens");
347- }
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");
348286 }
349287 }
288+ return;
350289 }
351- return;
352290 }
353-}
354-
355-fn onData(ctx: *anyopaque, data: []const u8) void {
356- const st: *State = @ptrCast(@alignCast(ctx));
357- const a = st.arena.allocator();
358- defer _ = st.arena.reset(.retain_capacity);
359-
360- const root = std.json.parseFromSliceLeaky(Value, a, data, .{}) catch {
361- if (st.stream_error == null) st.stream_error = "stream: invalid JSON chunk";
362- return;
363- };
364- if (root != .object) return;
365- handle(st, root.object) catch {
366- if (st.stream_error == null) st.stream_error = "stream: out of memory";
367- };
368-}
369-
370-fn errorMessage(obj: std.json.ObjectMap) []const u8 {
371- const err = obj.get("error") orelse return "stream: provider error";
372- return switch (err) {
373- .object => |o| api.stringField(o, "message") orelse "stream: provider error",
374- else => "stream: provider error",
375- };
376-}
291+};
377292
378-fn mapStopReason(st: *State) void {
379- var has_calls = false;
380- for (st.blocks.items) |block| {
381- if (block.kind == .tool and block.name.len > 0) has_calls = true;
382- }
293+/// A stream that produced finished calls is a tool turn, not a stop. A broken
294+/// stream reports none, and `run` restores the transport's own reason.
295+fn mapStopReason(st: *State, has_calls: bool) void {
383296 if (has_calls) {
384297 st.msg.stop_reason = .tool_use;
385298 return;
@@ -418,56 +331,20 @@ fn finalize(st: *State) !void {
418331 if (block.text.items.len == 0) continue;
419332 try st.msg.content.append(a, .{ .text = block.text.items });
420333 }
421- for (st.blocks.items) |block| {
422- if (block.kind != .tool or block.name.len == 0) continue;
423- const arguments = if (block.args.items.len > 0) block.args.items else "{}";
424- try st.msg.content.append(a, .{ .tool_call = .{
425- .id = block.id,
426- .name = block.name,
427- .arguments = arguments,
428- } });
429- st.sink.emit(.{ .tool_call = .{
430- .id = block.id,
431- .name = block.name,
432- .arguments = arguments,
433- } });
434- }
435-
436- var usage = types.Usage{};
437- usage.input = st.tokens_in;
438- usage.output = st.tokens_out;
439- usage.cache_read = st.cache_read;
440- usage.cache_write = st.cache_write;
441- usage.reasoning = st.reasoning_tokens;
442- usage.total_tokens = st.tokens_in + st.tokens_out + st.cache_read + st.cache_write;
443- api.applyCost(st.req.model, &usage);
444- st.msg.usage = usage;
334+ const has_calls = try api.appendCalls(st.msg, st.sink, a, st.calls.items, st.failed);
335+ api.finishUsage(st.msg, st.req.model, st.usage);
445336
446337 st.msg.raw_stop_reason = st.stop_reason;
447- mapStopReason(st);
338+ mapStopReason(st, has_calls);
448339 }
449340
450-pub fn stream(req: api.Request, sink: api.Sink) std.mem.Allocator.Error!types.AssistantMessage {
451- var msg = api.newAssistant(req);
452-
453- var arena = std.heap.ArenaAllocator.init(req.scratch);
454- defer arena.deinit();
455-
456- const body = buildBody(req) catch return error.OutOfMemory;
457- const url = buildUrl(req.scratch, req.model.base_url) catch return error.OutOfMemory;
458- const key = req.model.api_key orelse "";
459- const hdrs = headers(req.scratch, req, key) catch return error.OutOfMemory;
460-
461- var st = State{ .req = req, .sink = sink, .arena = &arena, .msg = &msg };
462-
463- if (!try api.post(req, url, hdrs, body, .{ .ctx = &st, .onEvent = onData }, &msg)) return msg;
464-
465- if (st.stream_error) |message| {
466- msg.stop_reason = .err;
467- msg.error_message = try req.pers.dupe(u8, message);
468- return msg;
469- }
341+const wire = api.Wire{
342+ .key_header = "x-api-key",
343+ .version = .{ "anthropic-version", anthropic_version },
344+ .path = "/messages",
345+ .build_body = buildBody,
346+};
470347
471- try finalize(&st);
472- return msg;
348+pub fn stream(req: api.Request, sink: api.Sink) std.mem.Allocator.Error!types.AssistantMessage {
349+ return api.run(State, wire, req, sink, finalize);
473350 }