diff options
Diffstat (limited to 'libpanto/src/conversation.zig')
| -rw-r--r-- | libpanto/src/conversation.zig | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/libpanto/src/conversation.zig b/libpanto/src/conversation.zig index 760f3ee..4338563 100644 --- a/libpanto/src/conversation.zig +++ b/libpanto/src/conversation.zig @@ -370,6 +370,24 @@ pub const Conversation = struct { }); } + /// Append a message of any role from a slice of content blocks, with + /// optional usage. The general form behind `addUserMessage` / + /// `addAssistantMessage`; used to rebuild a persisted message (tool calls, + /// thinking, mixed blocks) losslessly. Ownership of the blocks transfers + /// to the conversation; the caller must not deinit them after this call. + pub fn addMessage(self: *Conversation, role: MessageRole, blocks: []const ContentBlock, usage: ?Usage) !void { + var content: std.ArrayList(ContentBlock) = .empty; + try content.ensureTotalCapacity(self.allocator, blocks.len); + for (blocks) |block| { + content.appendAssumeCapacity(block); + } + try self.messages.append(self.allocator, .{ + .role = role, + .content = content, + .usage = usage, + }); + } + pub fn deinit(self: *Conversation) void { for (self.messages.items) |*msg| { msg.deinit(self.allocator); |
