xonecas.io
79113
Ma

mini-coding-agent / files

mini-coding-agent / src / api / anthropic_messages.zig

13.5 KB
1const std = @import("std");
2const api = @import("../api.zig");
3const message_mod = @import("../message.zig");
4const json = @import("../json.zig");
5
6const anthropic_version = "2023-06-01";
7
8const ImageSource = struct {
9    type: []const u8 = "base64",
10    media_type: []const u8,
11    data: []const u8,
12};
13
14const ImageBlock = struct {
15    type: []const u8 = "image",
16    source: ImageSource,
17};
18
19const TextBlock = struct { type: []const u8 = "text", text: []const u8 };
20
21const ThinkingBlock = struct {
22    type: []const u8 = "thinking",
23    thinking: []const u8,
24    signature: []const u8,
25};
26
27const ToolUseBlock = struct {
28    type: []const u8 = "tool_use",
29    id: []const u8,
30    name: []const u8,
31    input: json.Raw,
32};
33
34const ResultContent = union(enum) {
35    text: TextBlock,
36    image: ImageBlock,
37
38    pub fn jsonStringify(self: ResultContent, jws: anytype) !void {
39        switch (self) {
40            inline else => |v| try jws.write(v),
41        }
42    }
43};
44
45const ToolResultBlock = struct {
46    type: []const u8 = "tool_result",
47    tool_use_id: []const u8,
48    is_error: ?bool = null,
49    content: Content,
50    const Content = union(enum) {
51        text: []const u8,
52        parts: []const ResultContent,
53
54        pub fn jsonStringify(self: Content, jws: anytype) !void {
55            switch (self) {
56                .text => |v| try jws.write(v),
57                .parts => |v| try jws.write(v),
58            }
59        }
60    };
61};
62
63const AssistantBlock = union(enum) {
64    text: TextBlock,
65    thinking: ThinkingBlock,
66    plain: TextBlock,
67    tool_use: ToolUseBlock,
68
69    pub fn jsonStringify(self: AssistantBlock, jws: anytype) !void {
70        switch (self) {
71            inline else => |v| try jws.write(v),
72        }
73    }
74};
75
76const AssistantMessage = struct {
77    role: []const u8 = "assistant",
78    content: []const AssistantBlock,
79};
80
81const Message = union(enum) {
82    user_text: struct { role: []const u8 = "user", content: []const TextBlock },
83    user_results: struct { role: []const u8 = "user", content: []const ToolResultBlock },
84    assistant: AssistantMessage,
85
86    pub fn jsonStringify(self: Message, jws: anytype) !void {
87        switch (self) {
88            inline else => |v| try jws.write(v),
89        }
90    }
91};
92
93const Thinking = struct { type: []const u8, budget_tokens: ?u64 = null };
94const OutputConfig = struct { effort: []const u8 };
95
96const Body = struct {
97    model: []const u8,
98    max_tokens: u64,
99    stream: bool = true,
100    system: ?[]const u8 = null,
101    thinking: ?Thinking = null,
102    output_config: ?OutputConfig = null,
103    messages: []const Message,
104    tools: ?api.Tools = null,
105};
106
107fn budgetFor(effort: []const u8) u64 {
108    const map = .{
109        .{ "minimal", 1024 },
110        .{ "low", 2048 },
111        .{ "medium", 4096 },
112        .{ "high", 8192 },
113        .{ "xhigh", 16384 },
114        .{ "max", 32768 },
115    };
116    inline for (map) |entry| {
117        if (std.mem.eql(u8, effort, entry[0])) return entry[1];
118    }
119    return 0;
120}
121
122fn resultContent(a: std.mem.Allocator, t: message_mod.ToolResultMessage) !ToolResultBlock.Content {
123    if (t.images.len == 0) return .{ .text = t.text };
124    var parts: std.ArrayList(ResultContent) = .empty;
125    try parts.append(a, .{ .text = .{ .text = t.text } });
126    for (t.images) |img| {
127        try parts.append(a, .{ .image = .{ .source = .{ .media_type = img.mime_type, .data = img.data } } });
128    }
129    return .{ .parts = parts.items };
130}
131
132fn assistantBlocks(a: std.mem.Allocator, am: *const message_mod.AssistantMessage) ![]const AssistantBlock {
133    var blocks: std.ArrayList(AssistantBlock) = .empty;
134    for (am.content.items) |block| switch (block) {
135        .text => |t| try blocks.append(a, .{ .text = .{ .text = t } }),
136        .thinking => |t| {
137            if (t.signature) |sig| {
138                if (sig.len > 0) {
139                    try blocks.append(a, .{ .thinking = .{ .thinking = t.text, .signature = sig } });
140                    continue;
141                }
142            }
143            try blocks.append(a, .{ .plain = .{ .text = t.text } });
144        },
145        .tool_call => |c| try blocks.append(a, .{ .tool_use = .{
146            .id = c.id,
147            .name = c.name,
148            .input = .{ .bytes = api.argumentsOrObject(c.arguments) },
149        } }),
150    };
151    return blocks.items;
152}
153
154fn buildMessages(a: std.mem.Allocator, req: api.Request) ![]const Message {
155    var out: std.ArrayList(Message) = .empty;
156    var i: usize = 0;
157    while (i < req.messages.len) {
158        if (req.messages[i] == .tool_result) {
159            var blocks: std.ArrayList(ToolResultBlock) = .empty;
160            while (i < req.messages.len and req.messages[i] == .tool_result) : (i += 1) {
161                const t = req.messages[i].tool_result;
162                try blocks.append(a, .{
163                    .tool_use_id = t.tool_call_id,
164                    .is_error = if (t.is_error) true else null,
165                    .content = try resultContent(a, t),
166                });
167            }
168            try out.append(a, .{ .user_results = .{ .content = blocks.items } });
169            continue;
170        }
171        switch (req.messages[i]) {
172            .user => |u| {
173                const blocks = try a.alloc(TextBlock, 1);
174                blocks[0] = .{ .text = u.content };
175                try out.append(a, .{ .user_text = .{ .content = blocks } });
176            },
177            .assistant => |am| try out.append(a, .{ .assistant = .{
178                .content = try assistantBlocks(a, am),
179            } }),
180            .tool_result => unreachable,
181        }
182        i += 1;
183    }
184    return out.items;
185}
186
187fn buildBody(req: api.Request) ![]u8 {
188    const a = req.scratch;
189    const budget = budgetFor(req.effort);
190    var max_tokens: u64 = if (req.model.max_tokens > 0) req.model.max_tokens else 8192;
191    if (budget > 0 and max_tokens <= budget) max_tokens = budget + 4096;
192    const adaptive = req.model.thinking == .effort and req.effort.len > 0 and budget > 0;
193    const body = Body{
194        .model = req.model.id,
195        .max_tokens = max_tokens,
196        .system = if (req.system_prompt.len > 0) req.system_prompt else null,
197        .thinking = if (adaptive)
198            Thinking{ .type = "adaptive" }
199        else if (budget > 0)
200            Thinking{ .type = "enabled", .budget_tokens = budget }
201        else
202            null,
203        .output_config = if (adaptive) OutputConfig{ .effort = req.effort } else null,
204        .messages = try buildMessages(a, req),
205        .tools = if (req.tools_json.len > 0) .{ .a = a, .json = req.tools_json, .form = .{
206            .open = "[",
207            .entry = "{",
208            .params_key = "input_schema",
209            .entry_close = "}",
210            .close = "]",
211        } } else null,
212    };
213    return std.json.Stringify.valueAlloc(a, body, .{ .emit_null_optional_fields = false });
214}
215
216const BlockKind = enum { text, thinking, tool };
217
218const Block = struct {
219    kind: BlockKind = .text,
220    text: std.ArrayList(u8) = .empty,
221    signature: std.ArrayList(u8) = .empty,
222    call: usize = 0,
223};
224
225const Chunk = struct {
226    type: ?[]const u8 = null,
227    @"error": ?api.ErrField = null,
228    index: ?i64 = null,
229    message: ?MessageStart = null,
230    content_block: ?BlockStart = null,
231    delta: ?Delta = null,
232    usage: ?Usage = null,
233
234    const MessageStart = struct {
235        id: ?[]const u8 = null,
236        usage: ?Usage = null,
237    };
238
239    const BlockStart = struct {
240        type: ?[]const u8 = null,
241        id: ?[]const u8 = null,
242        name: ?[]const u8 = null,
243        data: ?[]const u8 = null,
244    };
245
246    const Delta = struct {
247        type: ?[]const u8 = null,
248        text: ?[]const u8 = null,
249        thinking: ?[]const u8 = null,
250        signature: ?[]const u8 = null,
251        partial_json: ?[]const u8 = null,
252        stop_reason: ?[]const u8 = null,
253    };
254
255    const Usage = struct {
256        input_tokens: api.Count = .{},
257        output_tokens: api.Count = .{},
258        cache_read_input_tokens: api.Count = .{},
259        cache_creation_input_tokens: api.Count = .{},
260        output_tokens_details: ?struct { thinking_tokens: api.Count = .{} } = null,
261    };
262};
263
264const State = struct {
265    req: api.Request,
266    sink: api.Sink,
267    arena: *std.heap.ArenaAllocator,
268    msg: *message_mod.AssistantMessage,
269    blocks: std.ArrayList(Block) = .empty,
270    calls: std.ArrayList(api.Call) = .empty,
271    usage: message_mod.Usage = .{},
272    stop_reason: ?[]const u8 = null,
273    stream_error: ?[]const u8 = null,
274    failed: bool = false,
275
276    pub fn handle(st: *State, chunk: Chunk) !void {
277        const event_type = chunk.type orelse "";
278
279        if (std.mem.eql(u8, event_type, "error")) {
280            const message = if (chunk.@"error") |e| e.message orelse "stream: provider error" else "stream: provider error";
281            try api.keepString(&st.stream_error, st.req.pers, message);
282            return;
283        }
284
285        if (std.mem.eql(u8, event_type, "message_start")) {
286            const message = chunk.message orelse return;
287            try api.keepString(&st.msg.response_id, st.req.pers, message.id);
288            if (message.usage) |u| {
289                st.usage.input = u.input_tokens.value;
290                st.usage.output = u.output_tokens.value;
291                st.usage.cache_read = u.cache_read_input_tokens.value;
292                st.usage.cache_write = u.cache_creation_input_tokens.value;
293            }
294            return;
295        }
296
297        if (std.mem.eql(u8, event_type, "content_block_start")) {
298            const block = try api.blockAt(Block, &st.blocks, st.req.pers, chunk.index orelse -1);
299            const cb = chunk.content_block orelse return;
300            const bt = cb.type orelse "";
301            if (std.mem.eql(u8, bt, "tool_use")) {
302                block.kind = .tool;
303                block.call = st.calls.items.len;
304                const call = try st.calls.addOne(st.req.pers);
305                call.* = .{};
306                if (cb.id) |id| {
307                    if (id.len > 0) call.id = try st.req.pers.dupe(u8, id);
308                }
309                try call.announce(st.req.pers, st.sink, cb.name orelse "");
310            } else if (std.mem.eql(u8, bt, "thinking") or std.mem.eql(u8, bt, "redacted_thinking")) {
311                block.kind = .thinking;
312                if (std.mem.eql(u8, bt, "redacted_thinking")) {
313                    if (cb.data) |d| try block.signature.appendSlice(st.req.pers, d);
314                }
315            } else {
316                block.kind = .text;
317            }
318            return;
319        }
320
321        if (std.mem.eql(u8, event_type, "content_block_delta")) {
322            const idx = chunk.index orelse -1;
323            if (idx < 0 or idx >= st.blocks.items.len) return;
324            const delta = chunk.delta orelse return;
325            const block = &st.blocks.items[@intCast(idx)];
326            const call = if (block.kind == .tool) &st.calls.items[block.call] else null;
327            const dt = delta.type orelse "";
328            if (std.mem.eql(u8, dt, "text_delta")) {
329                if (delta.text) |s| {
330                    try block.text.appendSlice(st.req.pers, s);
331                    st.sink.emit(.{ .text = s });
332                }
333            } else if (std.mem.eql(u8, dt, "thinking_delta")) {
334                if (delta.thinking) |s| {
335                    try block.text.appendSlice(st.req.pers, s);
336                    st.sink.emit(.{ .reasoning = s });
337                }
338            } else if (std.mem.eql(u8, dt, "signature_delta")) {
339                if (delta.signature) |s| try block.signature.appendSlice(st.req.pers, s);
340            } else if (std.mem.eql(u8, dt, "input_json_delta")) {
341                if (call) |c| {
342                    if (delta.partial_json) |s| try c.grow(st.req.pers, st.sink, s);
343                }
344            }
345            return;
346        }
347
348        if (std.mem.eql(u8, event_type, "message_delta")) {
349            if (chunk.delta) |delta| {
350                if (delta.stop_reason) |s| {
351                    if (s.len > 0) st.stop_reason = try st.req.pers.dupe(u8, s);
352                }
353            }
354            if (chunk.usage) |u| {
355                st.usage.input = u.input_tokens.value;
356                st.usage.output = u.output_tokens.value;
357                st.usage.cache_read = u.cache_read_input_tokens.value;
358                st.usage.cache_write = u.cache_creation_input_tokens.value;
359                if (u.output_tokens_details) |d| st.usage.reasoning = d.thinking_tokens.value;
360            }
361            return;
362        }
363    }
364};
365
366fn finalize(st: *State) !void {
367    const a = st.req.pers;
368    for (st.blocks.items) |block| {
369        if (block.kind != .thinking) continue;
370        if (block.text.items.len == 0 and block.signature.items.len == 0) continue;
371        try st.msg.content.append(a, .{ .thinking = .{
372            .text = block.text.items,
373            .signature = if (block.signature.items.len > 0) block.signature.items else null,
374        } });
375    }
376    for (st.blocks.items) |block| {
377        if (block.kind != .text) continue;
378        if (block.text.items.len == 0) continue;
379        try st.msg.content.append(a, .{ .text = block.text.items });
380    }
381    const has_calls = try api.appendCalls(st.msg, st.sink, a, st.calls.items, st.failed);
382    api.finishUsage(st.msg, st.req.model, st.usage);
383
384    st.msg.raw_stop_reason = st.stop_reason;
385    if (has_calls) {
386        st.msg.stop_reason = .tool_use;
387    } else if (!api.stopReason(st.msg, st.stop_reason)) {
388        st.msg.stop_reason = .stop;
389    }
390}
391
392const wire = api.Wire{
393    .key_header = "x-api-key",
394    .version = .{ "anthropic-version", anthropic_version },
395    .path = "/messages",
396    .build_body = buildBody,
397};
398
399pub fn stream(req: api.Request, sink: api.Sink) std.mem.Allocator.Error!message_mod.AssistantMessage {
400    return api.run(State, Chunk, wire, req, sink, finalize);
401}