79113
Ma
mini-coding-agent / files
mini-coding-agent / src / message.zig
2.3 KB
1const std = @import("std"); 2 3pub const Usage = struct { 4 input: u64 = 0, 5 output: u64 = 0, 6 cache_read: u64 = 0, 7 cache_write: u64 = 0, 8 reasoning: ?u64 = null, 9 total_tokens: u64 = 0, 10 cost_input: f64 = 0, 11 cost_output: f64 = 0, 12 cost_cache_read: f64 = 0, 13 cost_total: f64 = 0, 14}; 15 16pub const StopReason = enum { 17 pending, 18 stop, 19 length, 20 tool_use, 21 err, 22 aborted, 23 24 pub fn wire(self: StopReason) []const u8 { 25 return switch (self) { 26 .pending => "pending", 27 .stop => "stop", 28 .length => "length", 29 .tool_use => "toolUse", 30 .err => "error", 31 .aborted => "aborted", 32 }; 33 } 34 35 pub fn jsonStringify(self: StopReason, jws: anytype) !void { 36 try jws.write(self.wire()); 37 } 38}; 39 40pub const ImageContent = struct { 41 data: []const u8, 42 mime_type: []const u8, 43}; 44 45pub const ToolCall = struct { 46 id: []const u8, 47 name: []const u8, 48 arguments: []const u8, 49 thought_signature: ?[]const u8 = null, 50}; 51 52pub const ContentBlock = union(enum) { 53 text: []const u8, 54 thinking: struct { 55 text: []const u8, 56 signature: ?[]const u8 = null, 57 }, 58 tool_call: ToolCall, 59}; 60 61pub const AssistantMessage = struct { 62 content: std.ArrayList(ContentBlock), 63 api: []const u8, 64 provider: []const u8, 65 model: []const u8, 66 response_id: ?[]const u8 = null, 67 response_model: ?[]const u8 = null, 68 raw_stop_reason: ?[]const u8 = null, 69 usage: Usage = .{}, 70 stop_reason: StopReason = .pending, 71 error_message: ?[]const u8 = null, 72 timestamp: i64 = 0, 73 74 pub fn text(self: *const AssistantMessage, a: std.mem.Allocator) ![]const u8 { 75 var out: std.ArrayList(u8) = .empty; 76 for (self.content.items) |block| switch (block) { 77 .text => |t| try out.appendSlice(a, t), 78 else => {}, 79 }; 80 return out.items; 81 } 82}; 83 84pub const ToolResultMessage = struct { 85 tool_call_id: []const u8, 86 tool_name: []const u8, 87 text: []const u8, 88 images: []const ImageContent = &.{}, 89 is_error: bool, 90 timestamp: i64, 91}; 92 93pub const Message = union(enum) { 94 user: struct { content: []const u8, timestamp: i64 }, 95 assistant: *AssistantMessage, 96 tool_result: ToolResultMessage, 97};