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

main.zig

258 lines8.8 KB

1const std = @import("std");
2const Io = std.Io;
3​
4const agui = @import("agui.zig");
5const messages = @import("messages.zig");
6const providers = @import("providers.zig");
7​
8const LISTEN_ADDR = "127.0.0.1";
9const LISTEN_PORT: u16 = 8004;
10​
11pub fn main(init: std.process.Init) !void {
12 const allocator = init.gpa;
13 const io = init.io;
14​
15 const address = try Io.net.IpAddress.parse(LISTEN_ADDR, LISTEN_PORT);
16 var listener = try address.listen(io, .{ .reuse_address = true });
17 defer listener.socket.close(io);
18​
19 std.debug.print("AG-UI Zig server listening on http://127.0.0.1:{d}\n", .{LISTEN_PORT});
20​
21 while (true) {
22 const stream = try listener.accept(io);
23 handleConnection(allocator, io, stream) catch |err| {
24 std.log.err("connection error: {}", .{err});
25 };
26 }
28​
29fn handleConnection(allocator: std.mem.Allocator, io: Io, stream: Io.net.Stream) !void {
30 defer stream.close(io);
31​
32 var recv_buffer: [8192]u8 = undefined;
33 var send_buffer: [8192]u8 = undefined;
34 var conn_reader = stream.reader(io, &recv_buffer);
35 var conn_writer = stream.writer(io, &send_buffer);
36 var server = std.http.Server.init(&conn_reader.interface, &conn_writer.interface);
37​
38 while (server.reader.state == .ready) {
39 var request = server.receiveHead() catch |err| switch (err) {
40 error.HttpConnectionClosing => return,
41 else => return err,
42 };
43​
44 try serveRequest(allocator, io, &request);
45 }
47​
48fn serveRequest(allocator: std.mem.Allocator, io: Io, request: *std.http.Server.Request) !void {
49 if (request.head.method == .OPTIONS) {
50 try sendOptions(request);
51 return;
52 }
53​
54 if (request.head.method == .GET and std.mem.eql(u8, request.head.target, "/health")) {
55 try request.respond("ok", .{
56 .extra_headers = &.{
57 .{ .name = "content-type", .value = "text/plain" },
58 .{ .name = "access-control-allow-origin", .value = "*" },
59 },
60 });
61 return;
62 }
63​
64 if (request.head.method == .POST and std.mem.eql(u8, request.head.target, "/")) {
65 try handleChat(allocator, io, request);
66 return;
67 }
68​
69 if (request.head.method == .POST) {
70 try sendPlainError(request, .not_found, "not found");
71 return;
72 }
73​
74 try sendPlainError(request, .method_not_allowed, "method not allowed");
76​
77fn sendOptions(request: *std.http.Server.Request) !void {
78 try request.respond("", .{
79 .status = .no_content,
80 .extra_headers = &.{
81 .{ .name = "access-control-allow-origin", .value = "*" },
82 .{ .name = "access-control-allow-methods", .value = "POST, OPTIONS" },
83 .{ .name = "access-control-allow-headers", .value = "Content-Type" },
84 },
85 });
87​
88fn sendPlainError(request: *std.http.Server.Request, status: std.http.Status, message: []const u8) !void {
89 try request.respond(message, .{
90 .status = status,
91 .extra_headers = &.{
92 .{ .name = "content-type", .value = "text/plain" },
93 },
94 });
96​
97const EmitCtx = struct {
98 allocator: std.mem.Allocator,
99 response: *std.http.BodyWriter,
100 message_id: []const u8,
101​
102 fn emit(ctx: *anyopaque, delta: []const u8) !void {
103 const self: *EmitCtx = @ptrCast(@alignCast(ctx));
104 if (delta.len == 0) return;
105 const frame = try agui.textMessageContent(self.allocator, self.message_id, delta);
106 defer self.allocator.free(frame);
107 try agui.writeSse(self.response, frame);
108 }
109};
110​
111fn handleChat(allocator: std.mem.Allocator, io: Io, request: *std.http.Server.Request) !void {
112 var body_reader_buffer: [4096]u8 = undefined;
113 const body_reader = request.readerExpectContinue(&body_reader_buffer) catch {
114 try sendPlainError(request, .bad_request, "failed to read body");
115 return;
116 };
117​
118 const body = body_reader.allocRemaining(allocator, .limited(1024 * 1024)) catch {
119 try sendPlainError(request, .bad_request, "failed to read body");
120 return;
121 };
122 defer allocator.free(body);
123​
124 const parsed = std.json.parseFromSlice(agui.RunAgentInput, allocator, body, .{
125 .ignore_unknown_fields = true,
126 }) catch {
127 try sendPlainError(request, .bad_request, "invalid JSON body");
128 return;
129 };
130 defer parsed.deinit();
131​
132 const input = parsed.value;
133 if (input.threadId.len == 0 or input.runId.len == 0) {
134 try sendPlainError(request, .bad_request, "threadId and runId are required");
135 return;
136 }
137​
138 const config = agui.providerFromInput(&input, allocator) catch {
139 try sendPlainError(request, .internal_server_error, "failed to parse provider config");
140 return;
141 };
142 defer allocator.free(config.model);
143​
144 const chat_messages = messages.toChatMessages(allocator, input.messages) catch {
145 try sendPlainError(request, .internal_server_error, "failed to parse messages");
146 return;
147 };
148 defer messages.freeChatMessages(allocator, chat_messages.system, chat_messages.chat);
149​
150 if (chat_messages.chat.len == 0) {
151 try streamErrorOnly(allocator, request, input.threadId, input.runId, "no user or assistant messages to send");
152 return;
153 }
154​
155 var send_buffer: [4096]u8 = undefined;
156 var response = request.respondStreaming(&send_buffer, .{
157 .respond_options = .{
158 .extra_headers = &.{
159 .{ .name = "content-type", .value = "text/event-stream" },
160 .{ .name = "cache-control", .value = "no-cache" },
161 .{ .name = "connection", .value = "keep-alive" },
162 .{ .name = "access-control-allow-origin", .value = "*" },
163 },
164 },
165 }) catch {
166 try sendPlainError(request, .internal_server_error, "streaming unsupported");
167 return;
168 };
169​
170 const message_id = try std.fmt.allocPrint(allocator, "msg-{s}", .{input.runId});
171 defer allocator.free(message_id);
172​
173 const started = try agui.runStarted(allocator, input.threadId, input.runId);
174 defer allocator.free(started);
175 try agui.writeSse(&response, started);
176​
177 const text_start = try agui.textMessageStart(allocator, message_id);
178 defer allocator.free(text_start);
179 try agui.writeSse(&response, text_start);
180​
181 var emit_ctx = EmitCtx{
182 .allocator = allocator,
183 .response = &response,
184 .message_id = message_id,
185 };
186​
187 providers.streamCompletion(
188 allocator,
189 io,
190 config,
191 chat_messages.system,
192 chat_messages.chat,
193 &emit_ctx,
194 EmitCtx.emit,
195 ) catch |err| {
196 const message = switch (err) {
197 error.MissingOpenAIKey => "OPENAI_API_KEY is not set",
198 error.MissingAnthropicKey => "ANTHROPIC_API_KEY is not set",
199 error.UnsupportedProvider => "unsupported provider (expected openai or anthropic)",
200 error.UnsupportedCompressionMethod => "unsupported provider compression",
201 else => "provider request failed",
202 };
203 const frame = try agui.runError(allocator, input.threadId, input.runId, message);
204 defer allocator.free(frame);
205 try agui.writeSse(&response, frame);
206 const done = try agui.sseDone(allocator);
207 defer allocator.free(done);
208 try agui.writeSse(&response, done);
209 try response.end();
210 return;
211 };
212​
213 const text_end = try agui.textMessageEnd(allocator, message_id);
214 defer allocator.free(text_end);
215 try agui.writeSse(&response, text_end);
216​
217 const finished = try agui.runFinished(allocator, input.threadId, input.runId);
218 defer allocator.free(finished);
219 try agui.writeSse(&response, finished);
220​
221 const done = try agui.sseDone(allocator);
222 defer allocator.free(done);
223 try agui.writeSse(&response, done);
224 try response.end();
226​
227fn streamErrorOnly(
228 allocator: std.mem.Allocator,
229 request: *std.http.Server.Request,
230 thread_id: []const u8,
231 run_id: []const u8,
232 message: []const u8,
233) !void {
234 var send_buffer: [4096]u8 = undefined;
235 var response = try request.respondStreaming(&send_buffer, .{
236 .respond_options = .{
237 .extra_headers = &.{
238 .{ .name = "content-type", .value = "text/event-stream" },
239 .{ .name = "cache-control", .value = "no-cache" },
240 .{ .name = "connection", .value = "keep-alive" },
241 .{ .name = "access-control-allow-origin", .value = "*" },
242 },
243 },
244 });
245​
246 const started = try agui.runStarted(allocator, thread_id, run_id);
247 defer allocator.free(started);
248 try agui.writeSse(&response, started);
249​
250 const frame = try agui.runError(allocator, thread_id, run_id, message);
251 defer allocator.free(frame);
252 try agui.writeSse(&response, frame);
253​
254 const done = try agui.sseDone(allocator);
255 defer allocator.free(done);
256 try agui.writeSse(&response, done);
257 try response.end();