Open History to see the last commit
1const std = @import("std");
2const agui = @import("agui.zig");
3
4pub fn textFromMessageAlloc(allocator: std.mem.Allocator, message: *const agui.IncomingMessage) ![]const u8 {
5 if (message.parts) |parts| {
6 var out: std.ArrayList(u8) = .empty;
7 errdefer out.deinit(allocator);
8 for (parts) |part| {
9 if (std.mem.eql(u8, part.@"type", "text")) {
10 if (part.content) |content| {
11 if (content.len > 0) {
12 try out.appendSlice(allocator, content);
13 }
14 }
15 }
16 }
17 if (out.items.len > 0) {
18 return out.toOwnedSlice(allocator);
19 }
20 }
21
22 if (message.content) |content| {
23 switch (content) {
24 .string => |text| return try allocator.dupe(u8, text),
25 .array => |items| {
26 var out: std.ArrayList(u8) = .empty;
27 errdefer out.deinit(allocator);
28 for (items.items) |item| {
29 if (item != .object) continue;
30 if (item.object.get("type")) |type_value| {
31 if (type_value != .string or !std.mem.eql(u8, type_value.string, "text")) continue;
32 } else continue;
33 if (item.object.get("text")) |text_value| {
34 if (text_value == .string) {
35 try out.appendSlice(allocator, text_value.string);
36 }
37 }
38 }
39 if (out.items.len > 0) {
40 return out.toOwnedSlice(allocator);
41 }
42 },
43 else => {},
44 }
45 }
46
47 return try allocator.dupe(u8, "");
48}
49
50pub fn toChatMessages(
51 allocator: std.mem.Allocator,
52 messages: []const agui.IncomingMessage,
53) !struct { system: []const u8, chat: []agui.ChatMessage } {
54 var system_parts: std.ArrayList([]const u8) = .empty;
55 defer {
56 for (system_parts.items) |part| allocator.free(part);
57 system_parts.deinit(allocator);
58 }
59
60 var chat: std.ArrayList(agui.ChatMessage) = .empty;
61 errdefer chat.deinit(allocator);
62
63 for (messages) |message| {
64 var role = message.role;
65 if (std.mem.eql(u8, role, "developer")) {
66 role = "system";
67 } else if (std.mem.eql(u8, role, "tool") or std.mem.eql(u8, role, "reasoning")) {
68 continue;
69 }
70
71 const text = try textFromMessageAlloc(allocator, &message);
72 defer allocator.free(text);
73 if (text.len == 0) continue;
74
75 if (std.mem.eql(u8, role, "system")) {
76 try system_parts.append(allocator, try allocator.dupe(u8, text));
77 } else if (std.mem.eql(u8, role, "user") or std.mem.eql(u8, role, "assistant")) {
78 try chat.append(allocator, .{
79 .role = try allocator.dupe(u8, role),
80 .content = try allocator.dupe(u8, text),
81 });
82 }
83 }
84
85 var system: std.ArrayList(u8) = .empty;
86 errdefer system.deinit(allocator);
87 for (system_parts.items, 0..) |part, index| {
88 if (index > 0) try system.appendSlice(allocator, "\n\n");
89 try system.appendSlice(allocator, part);
90 }
91
92 return .{
93 .system = try system.toOwnedSlice(allocator),
94 .chat = try chat.toOwnedSlice(allocator),
95 };
96}
97
98pub fn freeChatMessages(allocator: std.mem.Allocator, system: []const u8, chat: []agui.ChatMessage) void {
99 allocator.free(system);
100 for (chat) |message| {
101 allocator.free(message.role);
102 allocator.free(message.content);
103 }
104 allocator.free(chat);
105}