Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

agui.zig

152 lines4.4 KB

1const std = @import("std");
2​
3pub const DEFAULT_OPENAI_MODEL = "gpt-4o";
4pub const DEFAULT_ANTHROPIC_MODEL = "claude-sonnet-4-6";
5​
6pub const RunAgentInput = struct {
7 threadId: []const u8,
8 runId: []const u8,
9 messages: []IncomingMessage,
10 forwardedProps: std.json.Value = .null,
11 data: std.json.Value = .null,
12};
13​
14pub const IncomingMessage = struct {
15 role: []const u8,
16 content: ?std.json.Value = null,
17 parts: ?[]MessagePart = null,
18};
19​
20pub const MessagePart = struct {
21 @"type": []const u8,
22 content: ?[]const u8 = null,
23};
24​
25pub const ChatMessage = struct {
26 role: []const u8,
27 content: []const u8,
28};
29​
30pub const ProviderConfig = struct {
31 provider: []const u8,
32 model: []const u8,
33};
34​
35pub fn providerFromInput(input: *const RunAgentInput, allocator: std.mem.Allocator) !ProviderConfig {
36 const props = if (input.forwardedProps == .object) input.forwardedProps else input.data;
37​
38 var provider: []const u8 = "openai";
39 var model: ?[]const u8 = null;
40​
41 if (props == .object) {
42 if (props.object.get("provider")) |value| {
43 if (value == .string and value.string.len > 0) {
44 provider = value.string;
45 }
46 }
47 if (props.object.get("model")) |value| {
48 if (value == .string) {
49 model = value.string;
50 }
51 }
52 }
53​
54 if (std.mem.eql(u8, provider, "anthropic")) {
55 return ProviderConfig{
56 .provider = "anthropic",
57 .model = if (model) |m| try allocator.dupe(u8, m) else try allocator.dupe(u8, DEFAULT_ANTHROPIC_MODEL),
58 };
59 }
60​
61 return ProviderConfig{
62 .provider = "openai",
63 .model = if (model) |m| try allocator.dupe(u8, m) else try allocator.dupe(u8, DEFAULT_OPENAI_MODEL),
64 };
66​
67fn sseEvent(allocator: std.mem.Allocator, payload: anytype) ![]u8 {
68 const json = try std.json.Stringify.valueAlloc(allocator, payload, .{});
69 defer allocator.free(json);
70 return std.fmt.allocPrint(allocator, "data: {s}\n\n", .{json});
72​
73pub fn sseDone(allocator: std.mem.Allocator) ![]u8 {
74 return try allocator.dupe(u8, "data: [DONE]\n\n");
76​
77pub fn runStarted(allocator: std.mem.Allocator, thread_id: []const u8, run_id: []const u8) ![]u8 {
78 return sseEvent(allocator, .{
79 .type = "RUN_STARTED",
80 .threadId = thread_id,
81 .runId = run_id,
82 });
84​
85pub fn textMessageStart(allocator: std.mem.Allocator, message_id: []const u8) ![]u8 {
86 return sseEvent(allocator, .{
87 .type = "TEXT_MESSAGE_START",
88 .messageId = message_id,
89 .role = "assistant",
90 });
92​
93pub fn textMessageContent(allocator: std.mem.Allocator, message_id: []const u8, delta: []const u8) ![]u8 {
94 return sseEvent(allocator, .{
95 .type = "TEXT_MESSAGE_CONTENT",
96 .messageId = message_id,
97 .delta = delta,
98 });
100​
101pub fn textMessageEnd(allocator: std.mem.Allocator, message_id: []const u8) ![]u8 {
102 return sseEvent(allocator, .{
103 .type = "TEXT_MESSAGE_END",
104 .messageId = message_id,
105 });
107​
108pub fn runFinished(allocator: std.mem.Allocator, thread_id: []const u8, run_id: []const u8) ![]u8 {
109 return sseEvent(allocator, .{
110 .type = "RUN_FINISHED",
111 .threadId = thread_id,
112 .runId = run_id,
113 .finishReason = "stop",
114 });
116​
117pub fn runError(allocator: std.mem.Allocator, thread_id: []const u8, run_id: []const u8, message: []const u8) ![]u8 {
118 const RunErrorPayload = struct {
119 type: []const u8,
120 threadId: []const u8,
121 runId: []const u8,
122 err: struct { message: []const u8 },
123​
124 pub fn jsonStringify(self: @This(), jw: anytype) !void {
125 try jw.beginObject();
126 try jw.objectField("type");
127 try jw.write(self.type);
128 try jw.objectField("threadId");
129 try jw.write(self.threadId);
130 try jw.objectField("runId");
131 try jw.write(self.runId);
132 try jw.objectField("error");
133 try jw.beginObject();
134 try jw.objectField("message");
135 try jw.write(self.err.message);
136 try jw.endObject();
137 try jw.endObject();
138 }
139 };
140​
141 return sseEvent(allocator, RunErrorPayload{
142 .type = "RUN_ERROR",
143 .threadId = thread_id,
144 .runId = run_id,
145 .err = .{ .message = message },
146 });
148​
149pub fn writeSse(response: *std.http.BodyWriter, frame: []const u8) !void {
150 try response.writer.writeAll(frame);
151 try response.flush();