@@ -5,26 +5,26 @@ const types = @import("../types.zig");
55 const anthropic_version = "2023-06-01";
66
77 const ImageSource = struct {
8- @"type": []const u8 = "base64",
9- @"media_type": []const u8,
8+ type: []const u8 = "base64",
9+ media_type: []const u8,
1010 data: []const u8,
1111 };
1212
1313 const ImageBlock = struct {
14- @"type": []const u8 = "image",
14+ type: []const u8 = "image",
1515 source: ImageSource,
1616 };
1717
18-const TextBlock = struct { @"type": []const u8 = "text", text: []const u8 };
18+const TextBlock = struct { type: []const u8 = "text", text: []const u8 };
1919
2020 const ThinkingBlock = struct {
21- @"type": []const u8 = "thinking",
21+ type: []const u8 = "thinking",
2222 thinking: []const u8,
2323 signature: []const u8,
2424 };
2525
2626 const ToolUseBlock = struct {
27- @"type": []const u8 = "tool_use",
27+ type: []const u8 = "tool_use",
2828 id: []const u8,
2929 name: []const u8,
3030 input: api.Raw,
@@ -42,9 +42,9 @@ const ResultContent = union(enum) {
4242 };
4343
4444 const ToolResultBlock = struct {
45- @"type": []const u8 = "tool_result",
46- @"tool_use_id": []const u8,
47- @"is_error": ?bool = null,
45+ type: []const u8 = "tool_result",
46+ tool_use_id: []const u8,
47+ is_error: ?bool = null,
4848 content: Content,
4949 const Content = union(enum) {
5050 text: []const u8,
@@ -72,11 +72,6 @@ const AssistantBlock = union(enum) {
7272 }
7373 };
7474
75-const UserMessage = struct {
76- role: []const u8 = "user",
77- content: []const ResultContent,
78-};
79-
8075 const AssistantMessage = struct {
8176 role: []const u8 = "assistant",
8277 content: []const AssistantBlock,
@@ -94,11 +89,11 @@ const Message = union(enum) {
9489 }
9590 };
9691
97-const Thinking = struct { @"type": []const u8 = "enabled", @"budget_tokens": u64 };
92+const Thinking = struct { type: []const u8 = "enabled", budget_tokens: u64 };
9893
9994 const Body = struct {
10095 model: []const u8,
101- @"max_tokens": u64,
96+ max_tokens: u64,
10297 stream: bool = true,
10398 system: ?[]const u8 = null,
10499 thinking: ?Thinking = null,
@@ -126,7 +121,7 @@ fn resultContent(a: std.mem.Allocator, t: types.ToolResultMessage) !ToolResultBl
126121 var parts: std.ArrayList(ResultContent) = .empty;
127122 try parts.append(a, .{ .text = .{ .text = t.text } });
128123 for (t.images) |img| {
129- try parts.append(a, .{ .image = .{ .source = .{ .@"media_type" = img.mime_type, .data = img.data } } });
124+ try parts.append(a, .{ .image = .{ .source = .{ .media_type = img.mime_type, .data = img.data } } });
130125 }
131126 return .{ .parts = parts.items };
132127 }
@@ -162,8 +157,8 @@ fn buildMessages(a: std.mem.Allocator, req: api.Request) ![]const Message {
162157 while (i < req.messages.len and req.messages[i] == .tool_result) : (i += 1) {
163158 const t = req.messages[i].tool_result;
164159 try blocks.append(a, .{
165- .@"tool_use_id" = t.tool_call_id,
166- .@"is_error" = if (t.is_error) true else null,
160+ .tool_use_id = t.tool_call_id,
161+ .is_error = if (t.is_error) true else null,
167162 .content = try resultContent(a, t),
168163 });
169164 }
@@ -193,9 +188,9 @@ fn buildBody(req: api.Request) ![]u8 {
193188 if (budget > 0 and max_tokens <= budget) max_tokens = budget + 4096;
194189 const body = Body{
195190 .model = req.model.id,
196- .@"max_tokens" = max_tokens,
191+ .max_tokens = max_tokens,
197192 .system = if (req.system_prompt.len > 0) req.system_prompt else null,
198- .thinking = if (budget > 0) .{ .@"budget_tokens" = budget } else null,
193+ .thinking = if (budget > 0) .{ .budget_tokens = budget } else null,
199194 .messages = try buildMessages(a, req),
200195 .tools = if (req.tools_json.len > 0) .{ .a = a, .json = req.tools_json, .form = .{
201196 .open = "[",
@@ -218,11 +213,11 @@ const Block = struct {
218213 };
219214
220215 const Chunk = struct {
221- @"type": ?[]const u8 = null,
216+ type: ?[]const u8 = null,
222217 @"error": ?api.ErrField = null,
223218 index: ?i64 = null,
224219 message: ?MessageStart = null,
225- @"content_block": ?BlockStart = null,
220+ content_block: ?BlockStart = null,
226221 delta: ?Delta = null,
227222 usage: ?Usage = null,
228223
@@ -232,27 +227,27 @@ const Chunk = struct {
232227 };
233228
234229 const BlockStart = struct {
235- @"type": ?[]const u8 = null,
230+ type: ?[]const u8 = null,
236231 id: ?[]const u8 = null,
237232 name: ?[]const u8 = null,
238233 data: ?[]const u8 = null,
239234 };
240235
241236 const Delta = struct {
242- @"type": ?[]const u8 = null,
237+ type: ?[]const u8 = null,
243238 text: ?[]const u8 = null,
244239 thinking: ?[]const u8 = null,
245240 signature: ?[]const u8 = null,
246- @"partial_json": ?[]const u8 = null,
247- @"stop_reason": ?[]const u8 = null,
241+ partial_json: ?[]const u8 = null,
242+ stop_reason: ?[]const u8 = null,
248243 };
249244
250245 const Usage = struct {
251- @"input_tokens": api.Count = .{},
252- @"output_tokens": api.Count = .{},
253- @"cache_read_input_tokens": api.Count = .{},
254- @"cache_creation_input_tokens": api.Count = .{},
255- @"output_tokens_details": ?struct { @"thinking_tokens": api.Count = .{} } = null,
246+ input_tokens: api.Count = .{},
247+ output_tokens: api.Count = .{},
248+ cache_read_input_tokens: api.Count = .{},
249+ cache_creation_input_tokens: api.Count = .{},
250+ output_tokens_details: ?struct { thinking_tokens: api.Count = .{} } = null,
256251 };
257252 };
258253
@@ -269,34 +264,30 @@ const State = struct {
269264 failed: bool = false,
270265
271266 pub fn handle(st: *State, chunk: Chunk) !void {
272- const event_type = chunk.@"type" orelse "";
267+ const event_type = chunk.type orelse "";
273268
274269 if (std.mem.eql(u8, event_type, "error")) {
275270 const message = if (chunk.@"error") |e| e.message orelse "stream: provider error" else "stream: provider error";
276- if (st.stream_error == null) st.stream_error = try st.req.pers.dupe(u8, message);
271+ try api.keepString(&st.stream_error, st.req.pers, message);
277272 return;
278273 }
279274
280275 if (std.mem.eql(u8, event_type, "message_start")) {
281276 const message = chunk.message orelse return;
282- if (st.msg.response_id == null) {
283- if (message.id) |id| {
284- if (id.len > 0) st.msg.response_id = try st.req.pers.dupe(u8, id);
285- }
286- }
277+ try api.keepString(&st.msg.response_id, st.req.pers, message.id);
287278 if (message.usage) |u| {
288- st.usage.input = u.@"input_tokens".value;
289- st.usage.output = u.@"output_tokens".value;
290- st.usage.cache_read = u.@"cache_read_input_tokens".value;
291- st.usage.cache_write = u.@"cache_creation_input_tokens".value;
279+ st.usage.input = u.input_tokens.value;
280+ st.usage.output = u.output_tokens.value;
281+ st.usage.cache_read = u.cache_read_input_tokens.value;
282+ st.usage.cache_write = u.cache_creation_input_tokens.value;
292283 }
293284 return;
294285 }
295286
296287 if (std.mem.eql(u8, event_type, "content_block_start")) {
297288 const block = try api.blockAt(Block, &st.blocks, st.req.pers, chunk.index orelse -1);
298- const cb = chunk.@"content_block" orelse return;
299- const bt = cb.@"type" orelse "";
289+ const cb = chunk.content_block orelse return;
290+ const bt = cb.type orelse "";
300291 if (std.mem.eql(u8, bt, "tool_use")) {
301292 block.kind = .tool;
302293 block.call = st.calls.items.len;
@@ -323,7 +314,7 @@ const State = struct {
323314 const delta = chunk.delta orelse return;
324315 const block = &st.blocks.items[@intCast(idx)];
325316 const call = if (block.kind == .tool) &st.calls.items[block.call] else null;
326- const dt = delta.@"type" orelse "";
317+ const dt = delta.type orelse "";
327318 if (std.mem.eql(u8, dt, "text_delta")) {
328319 if (delta.text) |s| {
329320 try block.text.appendSlice(st.req.pers, s);
@@ -338,7 +329,7 @@ const State = struct {
338329 if (delta.signature) |s| try block.signature.appendSlice(st.req.pers, s);
339330 } else if (std.mem.eql(u8, dt, "input_json_delta")) {
340331 if (call) |c| {
341- if (delta.@"partial_json") |s| try c.args.appendSlice(st.req.pers, s);
332+ if (delta.partial_json) |s| try c.args.appendSlice(st.req.pers, s);
342333 }
343334 }
344335 return;
@@ -346,16 +337,16 @@ const State = struct {
346337
347338 if (std.mem.eql(u8, event_type, "message_delta")) {
348339 if (chunk.delta) |delta| {
349- if (delta.@"stop_reason") |s| {
340+ if (delta.stop_reason) |s| {
350341 if (s.len > 0) st.stop_reason = try st.req.pers.dupe(u8, s);
351342 }
352343 }
353344 if (chunk.usage) |u| {
354- st.usage.input = u.@"input_tokens".value;
355- st.usage.output = u.@"output_tokens".value;
356- st.usage.cache_read = u.@"cache_read_input_tokens".value;
357- st.usage.cache_write = u.@"cache_creation_input_tokens".value;
358- if (u.@"output_tokens_details") |d| st.usage.reasoning = d.@"thinking_tokens".value;
345+ st.usage.input = u.input_tokens.value;
346+ st.usage.output = u.output_tokens.value;
347+ st.usage.cache_read = u.cache_read_input_tokens.value;
348+ st.usage.cache_write = u.cache_creation_input_tokens.value;
349+ if (u.output_tokens_details) |d| st.usage.reasoning = d.thinking_tokens.value;
359350 }
360351 return;
361352 }