xonecas.io
79113
Ma

mini-coding-agent / commit 08a291b

chore: agent garnening, hardening TUI rendering

xonecas2026-10-04 08a291bac56a72ec534d1f2dc3c18480004907b6 parent d00400b

15 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

src/api.zig+121−0
@@ -3,6 +3,7 @@
33
44 const std = @import("std");
55 const types = @import("types.zig");
6+const http = @import("http.zig");
67
78 pub const Event = union(enum) {
89 /// Assistant text delta.
@@ -71,3 +72,123 @@ pub fn newAssistant(req: Request) types.AssistantMessage {
7172 .timestamp = @import("util.zig").nowMs(),
7273 };
7374 }
75+
76+/// The header list every provider shares: the content negotiation pair, the
77+/// provider's own extra pairs (skipped when their value is empty), and then
78+/// the caller's custom and session headers.
79+pub fn headers(a: std.mem.Allocator, req: Request, extra: []const [2][]const u8) ![]http.Header {
80+ var list: std.ArrayList(http.Header) = .empty;
81+ errdefer list.deinit(a);
82+ try list.append(a, .{ .name = "Content-Type", .value = "application/json" });
83+ try list.append(a, .{ .name = "Accept", .value = "text/event-stream" });
84+ for (extra) |kv| {
85+ if (kv[1].len > 0) try list.append(a, .{ .name = kv[0], .value = kv[1] });
86+ }
87+ for (req.model.headers) |kv| try list.append(a, .{ .name = kv[0], .value = kv[1] });
88+ if (req.session_id) |sid| {
89+ if (req.model.session_header) |name| {
90+ if (sid.len > 0) try list.append(a, .{ .name = name, .value = sid });
91+ }
92+ }
93+ return list.toOwnedSlice(a);
94+}
95+
96+/// Sends the request and maps a transport failure onto `msg`. Returns false
97+/// once the caller must return `msg` as-is; true when the stream completed and
98+/// the caller should proceed to finalize.
99+pub fn post(req: Request, url: []const u8, hdrs: []const http.Header, body: []const u8, handler: http.SseHandler, msg: *types.AssistantMessage) std.mem.Allocator.Error!bool {
100+ var err_body: ?[]const u8 = null;
101+ http.postSse(req.scratch, url, hdrs, body, handler, req.cancel, &err_body) catch |e| {
102+ if (e == error.OutOfMemory) return error.OutOfMemory;
103+ if (e == error.Aborted or req.cancel.load(.acquire)) {
104+ msg.stop_reason = .aborted;
105+ } else {
106+ msg.stop_reason = .err;
107+ const message = switch (e) {
108+ error.HttpStatus => err_body orelse "provider returned an error status",
109+ error.ReadFailed => "stream read failed",
110+ else => "request failed",
111+ };
112+ msg.error_message = try req.pers.dupe(u8, message);
113+ }
114+ return false;
115+ };
116+ if (req.cancel.load(.acquire)) {
117+ msg.stop_reason = .aborted;
118+ return false;
119+ }
120+ return true;
121+}
122+
123+/// The `{d}` cost fields, shared by every provider's usage accounting.
124+pub fn applyCost(model: *const types.Model, u: *types.Usage) void {
125+ const m = 1_000_000.0;
126+ u.cost_input = @as(f64, @floatFromInt(u.input)) * model.cost_input / m;
127+ u.cost_output = @as(f64, @floatFromInt(u.output)) * model.cost_output / m;
128+ u.cost_cache_read = @as(f64, @floatFromInt(u.cache_read)) * model.cost_cache_read / m;
129+ u.cost_cache_write = 0;
130+ u.cost_total = u.cost_input + u.cost_output + u.cost_cache_read + u.cost_cache_write;
131+}
132+
133+pub fn num(v: std.json.Value) u64 {
134+ return switch (v) {
135+ .integer => |n| @intCast(@max(n, 0)),
136+ .float => |f| @intFromFloat(@max(f, 0)),
137+ else => 0,
138+ };
139+}
140+
141+pub fn numField(obj: std.json.ObjectMap, key: []const u8) u64 {
142+ return num(obj.get(key) orelse return 0);
143+}
144+
145+/// A non-empty string field, or null.
146+pub fn stringField(obj: std.json.ObjectMap, key: []const u8) ?[]const u8 {
147+ const v = obj.get(key) orelse return null;
148+ return switch (v) {
149+ .string => |s| if (s.len > 0) s else null,
150+ else => null,
151+ };
152+}
153+
154+/// A string field, or `""` when the field is missing or not a string.
155+pub fn stringOr(obj: std.json.ObjectMap, key: []const u8) []const u8 {
156+ return switch (obj.get(key) orelse .null) {
157+ .string => |s| s,
158+ else => "",
159+ };
160+}
161+
162+/// An integer field, or `default` when the field is missing or not an integer.
163+pub fn intOr(obj: std.json.ObjectMap, key: []const u8, default: i64) i64 {
164+ return switch (obj.get(key) orelse .null) {
165+ .integer => |n| n,
166+ else => default,
167+ };
168+}
169+
170+/// `base_url` plus one path segment, with a trailing slash on the base ignored.
171+pub fn buildUrl(a: std.mem.Allocator, base: []const u8, path: []const u8) ![]u8 {
172+ return std.fmt.allocPrint(a, "{s}{s}", .{ std.mem.trimEnd(u8, base, "/"), path });
173+}
174+
175+/// The tool schemas parsed from the session's flat form, or null when there
176+/// are none. Borrowed from `a`.
177+pub fn parseTools(a: std.mem.Allocator, tools_json: []const u8) ?[]const std.json.Value {
178+ if (tools_json.len == 0) return null;
179+ const root = std.json.parseFromSliceLeaky(std.json.Value, a, tools_json, .{}) catch return null;
180+ return if (root == .array) root.array.items else null;
181+}
182+
183+/// `"name":"…","description":"…","<params_key>":<schema>` — the body every
184+/// provider's tool encoding shares. The caller supplies the wrapper.
185+pub fn writeToolBody(w: *std.Io.Writer, obj: std.json.ObjectMap, params_key: []const u8) !void {
186+ try w.writeAll("\"name\":");
187+ try @import("json.zig").writeString(w, obj.get("name").?.string);
188+ try w.writeAll(",\"description\":");
189+ try @import("json.zig").writeString(w, obj.get("description").?.string);
190+ try w.writeAll(",\"");
191+ try w.writeAll(params_key);
192+ try w.writeAll("\":");
193+ try std.json.Stringify.value(obj.get("parameters").?, .{}, w);
194+}
src/api/anthropic_messages.zig+32−113
@@ -104,21 +104,12 @@ fn writeUser(w: *std.Io.Writer, u: types.Message) !void {
104104 }
105105
106106 fn writeTools(w: *std.Io.Writer, a: std.mem.Allocator, tools_json: []const u8, params_key: []const u8) !void {
107- if (tools_json.len == 0) return;
108- const root = std.json.parseFromSliceLeaky(Value, a, tools_json, .{}) catch return;
109- if (root != .array) return;
107+ const tools = api.parseTools(a, tools_json) orelse return;
110108 try w.writeAll(",\"tools\":[");
111- for (root.array.items, 0..) |tool, i| {
109+ for (tools, 0..) |tool, i| {
112110 if (i > 0) try w.writeByte(',');
113- const obj = tool.object;
114- try w.writeAll("{\"name\":");
115- try json.writeString(w, obj.get("name").?.string);
116- try w.writeAll(",\"description\":");
117- try json.writeString(w, obj.get("description").?.string);
118- try w.writeAll(",\"");
119- try w.writeAll(params_key);
120- try w.writeAll("\":");
121- try std.json.Stringify.value(obj.get("parameters").?, .{}, w);
111+ try w.writeByte('{');
112+ try api.writeToolBody(w, tool.object, params_key);
122113 try w.writeByte('}');
123114 }
124115 try w.writeByte(']');
@@ -199,23 +190,14 @@ fn buildBody(req: api.Request) ![]u8 {
199190 }
200191
201192 fn buildUrl(a: std.mem.Allocator, base: []const u8) ![]u8 {
202- return std.fmt.allocPrint(a, "{s}/v1/messages", .{std.mem.trimEnd(u8, base, "/")});
193+ return api.buildUrl(a, base, "/v1/messages");
203194 }
204195
205196 fn headers(a: std.mem.Allocator, req: api.Request, key: []const u8) ![]http.Header {
206- var list: std.ArrayList(http.Header) = .empty;
207- errdefer list.deinit(a);
208- try list.append(a, .{ .name = "Content-Type", .value = "application/json" });
209- try list.append(a, .{ .name = "Accept", .value = "text/event-stream" });
210- try list.append(a, .{ .name = "anthropic-version", .value = anthropic_version });
211- if (key.len > 0) try list.append(a, .{ .name = "x-api-key", .value = key });
212- for (req.model.headers) |kv| try list.append(a, .{ .name = kv[0], .value = kv[1] });
213- if (req.session_id) |sid| {
214- if (req.model.session_header) |name| {
215- if (sid.len > 0) try list.append(a, .{ .name = name, .value = sid });
216- }
217- }
218- return list.toOwnedSlice(a);
197+ return api.headers(a, req, &.{
198+ .{ "anthropic-version", anthropic_version },
199+ .{ "x-api-key", key },
200+ });
219201 }
220202
221203 // ---- streaming state ------------------------------------------------------
@@ -256,32 +238,8 @@ fn blockAt(st: *State, index: i64) !*Block {
256238 return &st.blocks.items[idx];
257239 }
258240
259-fn num(v: Value) u64 {
260- return switch (v) {
261- .integer => |n| @intCast(@max(n, 0)),
262- .float => |f| @intFromFloat(@max(f, 0)),
263- else => 0,
264- };
265-}
266-
267-fn numField(obj: std.json.ObjectMap, key: []const u8) u64 {
268- return num(obj.get(key) orelse return 0);
269-}
270-
271-fn applyCost(model: *const types.Model, u: *types.Usage) void {
272- const m = 1_000_000.0;
273- u.cost_input = @as(f64, @floatFromInt(u.input)) * model.cost_input / m;
274- u.cost_output = @as(f64, @floatFromInt(u.output)) * model.cost_output / m;
275- u.cost_cache_read = @as(f64, @floatFromInt(u.cache_read)) * model.cost_cache_read / m;
276- u.cost_cache_write = 0;
277- u.cost_total = u.cost_input + u.cost_output + u.cost_cache_read + u.cost_cache_write;
278-}
279-
280241 fn handle(st: *State, obj: std.json.ObjectMap) !void {
281- const event_type = switch (obj.get("type") orelse Value{ .null = {} }) {
282- .string => |s| s,
283- else => "",
284- };
242+ const event_type = api.stringOr(obj, "type");
285243
286244 if (std.mem.eql(u8, event_type, "error")) {
287245 if (st.stream_error == null) st.stream_error = try st.req.pers.dupe(u8, errorMessage(obj));
@@ -299,28 +257,22 @@ fn handle(st: *State, obj: std.json.ObjectMap) !void {
299257 }
300258 if (message_obj.get("usage")) |usage| {
301259 if (usage == .object) {
302- st.tokens_in = numField(usage.object, "input_tokens");
303- st.tokens_out = numField(usage.object, "output_tokens");
304- st.cache_read = numField(usage.object, "cache_read_input_tokens");
305- st.cache_write = numField(usage.object, "cache_creation_input_tokens");
260+ st.tokens_in = api.numField(usage.object, "input_tokens");
261+ st.tokens_out = api.numField(usage.object, "output_tokens");
262+ st.cache_read = api.numField(usage.object, "cache_read_input_tokens");
263+ st.cache_write = api.numField(usage.object, "cache_creation_input_tokens");
306264 }
307265 }
308266 return;
309267 }
310268
311269 if (std.mem.eql(u8, event_type, "content_block_start")) {
312- const raw: i64 = switch (obj.get("index") orelse Value{ .null = {} }) {
313- .integer => |n| n,
314- else => -1,
315- };
270+ const raw = api.intOr(obj, "index", -1);
316271 if (raw < 0 or raw > max_stream_index) return;
317272 const block = try blockAt(st, raw);
318273 const cb = obj.get("content_block") orelse return;
319274 if (cb != .object) return;
320- const bt = switch (cb.object.get("type") orelse Value{ .null = {} }) {
321- .string => |s| s,
322- else => "",
323- };
275+ const bt = api.stringOr(cb.object, "type");
324276 if (std.mem.eql(u8, bt, "tool_use")) {
325277 block.kind = .tool;
326278 if (cb.object.get("id")) |id| {
@@ -347,34 +299,28 @@ fn handle(st: *State, obj: std.json.ObjectMap) !void {
347299 }
348300
349301 if (std.mem.eql(u8, event_type, "content_block_delta")) {
350- const idx: i64 = switch (obj.get("index") orelse Value{ .null = {} }) {
351- .integer => |n| n,
352- else => return,
353- };
302+ const idx = api.intOr(obj, "index", -1);
303+ if (idx < 0 or idx >= st.blocks.items.len) return;
354304 const delta = obj.get("delta") orelse return;
355305 if (delta != .object) return;
356- if (idx < 0 or idx >= st.blocks.items.len) return;
357306 const block = &st.blocks.items[@intCast(idx)];
358- const dt = switch (delta.object.get("type") orelse Value{ .null = {} }) {
359- .string => |s| s,
360- else => "",
361- };
307+ const dt = api.stringOr(delta.object, "type");
362308 if (std.mem.eql(u8, dt, "text_delta")) {
363- if (stringField(delta.object, "text")) |s| {
309+ if (api.stringField(delta.object, "text")) |s| {
364310 try block.text.appendSlice(st.req.pers, s);
365311 st.sink.emit(.{ .text = s });
366312 }
367313 } else if (std.mem.eql(u8, dt, "thinking_delta")) {
368- if (stringField(delta.object, "thinking")) |s| {
314+ if (api.stringField(delta.object, "thinking")) |s| {
369315 try block.text.appendSlice(st.req.pers, s);
370316 st.sink.emit(.{ .reasoning = s });
371317 }
372318 } else if (std.mem.eql(u8, dt, "signature_delta")) {
373- if (stringField(delta.object, "signature")) |s| {
319+ if (api.stringField(delta.object, "signature")) |s| {
374320 try block.signature.appendSlice(st.req.pers, s);
375321 }
376322 } else if (std.mem.eql(u8, dt, "input_json_delta")) {
377- if (stringField(delta.object, "partial_json")) |s| {
323+ if (api.stringField(delta.object, "partial_json")) |s| {
378324 try block.args.appendSlice(st.req.pers, s);
379325 }
380326 }
@@ -384,20 +330,20 @@ fn handle(st: *State, obj: std.json.ObjectMap) !void {
384330 if (std.mem.eql(u8, event_type, "message_delta")) {
385331 if (obj.get("delta")) |delta| {
386332 if (delta == .object) {
387- if (stringField(delta.object, "stop_reason")) |s| {
333+ if (api.stringField(delta.object, "stop_reason")) |s| {
388334 st.stop_reason = try st.req.pers.dupe(u8, s);
389335 }
390336 }
391337 }
392338 if (obj.get("usage")) |usage| {
393339 if (usage == .object) {
394- if (usage.object.get("input_tokens") != null) st.tokens_in = numField(usage.object, "input_tokens");
395- if (usage.object.get("output_tokens") != null) st.tokens_out = numField(usage.object, "output_tokens");
396- if (usage.object.get("cache_read_input_tokens") != null) st.cache_read = numField(usage.object, "cache_read_input_tokens");
397- if (usage.object.get("cache_creation_input_tokens") != null) st.cache_write = numField(usage.object, "cache_creation_input_tokens");
340+ if (usage.object.get("input_tokens") != null) st.tokens_in = api.numField(usage.object, "input_tokens");
341+ if (usage.object.get("output_tokens") != null) st.tokens_out = api.numField(usage.object, "output_tokens");
342+ if (usage.object.get("cache_read_input_tokens") != null) st.cache_read = api.numField(usage.object, "cache_read_input_tokens");
343+ if (usage.object.get("cache_creation_input_tokens") != null) st.cache_write = api.numField(usage.object, "cache_creation_input_tokens");
398344 if (usage.object.get("output_tokens_details")) |details| {
399345 if (details == .object and details.object.get("thinking_tokens") != null) {
400- st.reasoning_tokens = numField(details.object, "thinking_tokens");
346+ st.reasoning_tokens = api.numField(details.object, "thinking_tokens");
401347 }
402348 }
403349 }
@@ -421,18 +367,10 @@ fn onData(ctx: *anyopaque, data: []const u8) void {
421367 };
422368 }
423369
424-fn stringField(obj: std.json.ObjectMap, key: []const u8) ?[]const u8 {
425- const v = obj.get(key) orelse return null;
426- return switch (v) {
427- .string => |s| if (s.len > 0) s else null,
428- else => null,
429- };
430-}
431-
432370 fn errorMessage(obj: std.json.ObjectMap) []const u8 {
433371 const err = obj.get("error") orelse return "stream: provider error";
434372 return switch (err) {
435- .object => |o| stringField(o, "message") orelse "stream: provider error",
373+ .object => |o| api.stringField(o, "message") orelse "stream: provider error",
436374 else => "stream: provider error",
437375 };
438376 }
@@ -502,7 +440,7 @@ fn finalize(st: *State) !void {
502440 usage.cache_write = st.cache_write;
503441 usage.reasoning = st.reasoning_tokens;
504442 usage.total_tokens = st.tokens_in + st.tokens_out + st.cache_read + st.cache_write;
505- applyCost(st.req.model, &usage);
443+ api.applyCost(st.req.model, &usage);
506444 st.msg.usage = usage;
507445
508446 st.msg.raw_stop_reason = st.stop_reason;
@@ -521,28 +459,9 @@ pub fn stream(req: api.Request, sink: api.Sink) std.mem.Allocator.Error!types.As
521459 const hdrs = headers(req.scratch, req, key) catch return error.OutOfMemory;
522460
523461 var st = State{ .req = req, .sink = sink, .arena = &arena, .msg = &msg };
524- var err_body: ?[]const u8 = null;
525462
526- http.postSse(req.scratch, url, hdrs, body, .{ .ctx = &st, .onEvent = onData }, req.cancel, &err_body) catch |e| {
527- if (e == error.OutOfMemory) return error.OutOfMemory;
528- if (e == error.Aborted or req.cancel.load(.acquire)) {
529- msg.stop_reason = .aborted;
530- } else {
531- msg.stop_reason = .err;
532- const message = switch (e) {
533- error.HttpStatus => err_body orelse "provider returned an error status",
534- error.ReadFailed => "stream read failed",
535- else => "request failed",
536- };
537- msg.error_message = try req.pers.dupe(u8, message);
538- }
539- return msg;
540- };
463+ if (!try api.post(req, url, hdrs, body, .{ .ctx = &st, .onEvent = onData }, &msg)) return msg;
541464
542- if (req.cancel.load(.acquire)) {
543- msg.stop_reason = .aborted;
544- return msg;
545- }
546465 if (st.stream_error) |message| {
547466 msg.stop_reason = .err;
548467 msg.error_message = try req.pers.dupe(u8, message);
src/api/google_generative_ai.zig+16−82
@@ -87,19 +87,12 @@ fn writeToolResults(w: *std.Io.Writer, run: []const types.Message) !void {
8787 }
8888
8989 fn writeTools(w: *std.Io.Writer, a: std.mem.Allocator, tools_json: []const u8) !void {
90- if (tools_json.len == 0) return;
91- const root = std.json.parseFromSliceLeaky(Value, a, tools_json, .{}) catch return;
92- if (root != .array) return;
90+ const tools = api.parseTools(a, tools_json) orelse return;
9391 try w.writeAll(",\"tools\":[{\"functionDeclarations\":[");
94- for (root.array.items, 0..) |tool, i| {
92+ for (tools, 0..) |tool, i| {
9593 if (i > 0) try w.writeByte(',');
96- const obj = tool.object;
97- try w.writeAll("{\"name\":");
98- try json.writeString(w, obj.get("name").?.string);
99- try w.writeAll(",\"description\":");
100- try json.writeString(w, obj.get("description").?.string);
101- try w.writeAll(",\"parametersJsonSchema\":");
102- try std.json.Stringify.value(obj.get("parameters").?, .{}, w);
94+ try w.writeByte('{');
95+ try api.writeToolBody(w, tool.object, "parametersJsonSchema");
10396 try w.writeByte('}');
10497 }
10598 try w.writeAll("]}]");
@@ -184,18 +177,7 @@ fn buildUrl(a: std.mem.Allocator, base: []const u8, id: []const u8) ![]u8 {
184177 }
185178
186179 fn headers(a: std.mem.Allocator, req: api.Request, key: []const u8) ![]http.Header {
187- var list: std.ArrayList(http.Header) = .empty;
188- errdefer list.deinit(a);
189- try list.append(a, .{ .name = "Content-Type", .value = "application/json" });
190- try list.append(a, .{ .name = "Accept", .value = "text/event-stream" });
191- if (key.len > 0) try list.append(a, .{ .name = "x-goog-api-key", .value = key });
192- for (req.model.headers) |kv| try list.append(a, .{ .name = kv[0], .value = kv[1] });
193- if (req.session_id) |sid| {
194- if (req.model.session_header) |name| {
195- if (sid.len > 0) try list.append(a, .{ .name = name, .value = sid });
196- }
197- }
198- return list.toOwnedSlice(a);
180+ return api.headers(a, req, &.{.{ "x-goog-api-key", key }});
199181 }
200182
201183 // ---- streaming state ------------------------------------------------------
@@ -222,44 +204,15 @@ const State = struct {
222204 stream_error: ?[]const u8 = null,
223205 };
224206
225-fn num(v: Value) u64 {
226- return switch (v) {
227- .integer => |n| @intCast(@max(n, 0)),
228- .float => |f| @intFromFloat(@max(f, 0)),
229- else => 0,
230- };
231-}
232-
233-fn numField(obj: std.json.ObjectMap, key: []const u8) u64 {
234- return num(obj.get(key) orelse return 0);
235-}
236-
237-fn stringField(obj: std.json.ObjectMap, key: []const u8) ?[]const u8 {
238- const v = obj.get(key) orelse return null;
239- return switch (v) {
240- .string => |s| if (s.len > 0) s else null,
241- else => null,
242- };
243-}
244-
245-fn applyCost(model: *const types.Model, u: *types.Usage) void {
246- const m = 1_000_000.0;
247- u.cost_input = @as(f64, @floatFromInt(u.input)) * model.cost_input / m;
248- u.cost_output = @as(f64, @floatFromInt(u.output)) * model.cost_output / m;
249- u.cost_cache_read = @as(f64, @floatFromInt(u.cache_read)) * model.cost_cache_read / m;
250- u.cost_cache_write = 0;
251- u.cost_total = u.cost_input + u.cost_output + u.cost_cache_read + u.cost_cache_write;
252-}
253-
254207 fn addCall(st: *State, part: std.json.ObjectMap) !void {
255208 const fc = part.get("functionCall") orelse return;
256209 if (fc != .object) return;
257- const name = stringField(fc.object, "name") orelse "";
210+ const name = api.stringField(fc.object, "name") orelse "";
258211 const call = try st.calls.addOne(st.req.pers);
259212 call.* = .{ .name = try st.req.pers.dupe(u8, name) };
260213 // Google omits a call id, so derive a stable one from the name and order.
261214 call.id = try std.fmt.allocPrint(st.req.pers, "{s}_{d}", .{ call.name, st.calls.items.len });
262- if (stringField(part, "thoughtSignature")) |sig| {
215+ if (api.stringField(part, "thoughtSignature")) |sig| {
263216 call.thought_signature = try st.req.pers.dupe(u8, sig);
264217 }
265218 if (call.name.len > 0) st.sink.emit(.{ .tool_start = call.name });
@@ -275,7 +228,7 @@ fn addCall(st: *State, part: std.json.ObjectMap) !void {
275228 fn handle(st: *State, obj: std.json.ObjectMap) !void {
276229 if (obj.get("error")) |err| {
277230 if (err == .object) {
278- const message = stringField(err.object, "message") orelse "stream: provider error";
231+ const message = api.stringField(err.object, "message") orelse "stream: provider error";
279232 if (st.stream_error == null) st.stream_error = try st.req.pers.dupe(u8, message);
280233 }
281234 return;
@@ -295,8 +248,8 @@ fn handle(st: *State, obj: std.json.ObjectMap) !void {
295248 if (fc == .object) try addCall(st, part.object);
296249 continue;
297250 }
298- const text = stringField(part.object, "text") orelse continue;
299- const thought = part.object.get("thought") orelse Value{ .null = {} };
251+ const text = api.stringField(part.object, "text") orelse continue;
252+ const thought = part.object.get("thought") orelse .null;
300253 if (thought == .bool and thought.bool) {
301254 try st.reasoning.appendSlice(st.req.pers, text);
302255 st.sink.emit(.{ .reasoning = text });
@@ -309,7 +262,7 @@ fn handle(st: *State, obj: std.json.ObjectMap) !void {
309262 }
310263 }
311264 }
312- if (stringField(candidate.object, "finishReason")) |fr| {
265+ if (api.stringField(candidate.object, "finishReason")) |fr| {
313266 st.finish = try st.req.pers.dupe(u8, fr);
314267 }
315268 }
@@ -318,9 +271,9 @@ fn handle(st: *State, obj: std.json.ObjectMap) !void {
318271
319272 if (obj.get("usageMetadata")) |usage| {
320273 if (usage == .object) {
321- st.tokens_in = numField(usage.object, "promptTokenCount");
322- st.tokens_out = numField(usage.object, "candidatesTokenCount") + numField(usage.object, "thoughtsTokenCount");
323- st.tokens_total = numField(usage.object, "totalTokenCount");
274+ st.tokens_in = api.numField(usage.object, "promptTokenCount");
275+ st.tokens_out = api.numField(usage.object, "candidatesTokenCount") + api.numField(usage.object, "thoughtsTokenCount");
276+ st.tokens_total = api.numField(usage.object, "totalTokenCount");
324277 }
325278 }
326279 }
@@ -366,7 +319,7 @@ fn finalize(st: *State) !void {
366319 usage.input = st.tokens_in;
367320 usage.output = st.tokens_out;
368321 usage.total_tokens = if (st.tokens_total > 0) st.tokens_total else st.tokens_in + st.tokens_out;
369- applyCost(st.req.model, &usage);
322+ api.applyCost(st.req.model, &usage);
370323 st.msg.usage = usage;
371324
372325 st.msg.raw_stop_reason = st.finish;
@@ -391,28 +344,9 @@ pub fn stream(req: api.Request, sink: api.Sink) std.mem.Allocator.Error!types.As
391344 const hdrs = headers(req.scratch, req, key) catch return error.OutOfMemory;
392345
393346 var st = State{ .req = req, .sink = sink, .arena = &arena, .msg = &msg };
394- var err_body: ?[]const u8 = null;
395347
396- http.postSse(req.scratch, url, hdrs, body, .{ .ctx = &st, .onEvent = onData }, req.cancel, &err_body) catch |e| {
397- if (e == error.OutOfMemory) return error.OutOfMemory;
398- if (e == error.Aborted or req.cancel.load(.acquire)) {
399- msg.stop_reason = .aborted;
400- } else {
401- msg.stop_reason = .err;
402- const message = switch (e) {
403- error.HttpStatus => err_body orelse "provider returned an error status",
404- error.ReadFailed => "stream read failed",
405- else => "request failed",
406- };
407- msg.error_message = try req.pers.dupe(u8, message);
408- }
409- return msg;
410- };
348+ if (!try api.post(req, url, hdrs, body, .{ .ctx = &st, .onEvent = onData }, &msg)) return msg;
411349
412- if (req.cancel.load(.acquire)) {
413- msg.stop_reason = .aborted;
414- return msg;
415- }
416350 if (st.stream_error) |message| {
417351 msg.stop_reason = .err;
418352 msg.error_message = try req.pers.dupe(u8, message);
src/api/openai_completions.zig+19−92
@@ -17,12 +17,7 @@ const max_stream_index = 10_000;
1717
1818 fn writeAssistant(w: *std.Io.Writer, am: *const types.AssistantMessage, a: std.mem.Allocator) !void {
1919 try w.writeAll("{\"role\":\"assistant\",\"content\":");
20- var text: std.ArrayList(u8) = .empty;
21- for (am.content.items) |block| switch (block) {
22- .text => |t| text.appendSlice(a, t) catch return error.OutOfMemory,
23- else => {},
24- };
25- try json.writeString(w, text.items);
20+ try json.writeString(w, try types.assistantText(a, am));
2621
2722 var calls: usize = 0;
2823 for (am.content.items) |block| {
@@ -134,47 +129,28 @@ fn buildBody(req: api.Request) ![]u8 {
134129 /// `function` object. The flat form recorded in the session is not the wire
135130 /// form here.
136131 fn writeTools(w: *std.Io.Writer, a: std.mem.Allocator, tools_json: []const u8) !void {
137- if (tools_json.len == 0) return;
138- const root = std.json.parseFromSliceLeaky(Value, a, tools_json, .{}) catch return;
139- if (root != .array) return;
132+ const tools = api.parseTools(a, tools_json) orelse return;
140133 try w.writeAll(",\"tools\":[");
141- for (root.array.items, 0..) |tool, i| {
134+ for (tools, 0..) |tool, i| {
142135 if (i > 0) try w.writeByte(',');
143- const obj = tool.object;
144- try w.writeAll("{\"type\":\"function\",\"function\":{\"name\":");
145- try json.writeString(w, obj.get("name").?.string);
146- try w.writeAll(",\"description\":");
147- try json.writeString(w, obj.get("description").?.string);
148- try w.writeAll(",\"parameters\":");
149- try std.json.Stringify.value(obj.get("parameters").?, .{}, w);
136+ try w.writeAll("{\"type\":\"function\",\"function\":{");
137+ try api.writeToolBody(w, tool.object, "parameters");
150138 try w.writeAll("}}");
151139 }
152140 try w.writeByte(']');
153141 }
154142
155143 fn buildUrl(a: std.mem.Allocator, base: []const u8) ![]u8 {
156- return std.fmt.allocPrint(a, "{s}/chat/completions", .{std.mem.trimEnd(u8, base, "/")});
144+ return api.buildUrl(a, base, "/chat/completions");
157145 }
158146
159147 fn headers(a: std.mem.Allocator, req: api.Request, auth: []const u8) ![]http.Header {
160- var list: std.ArrayList(http.Header) = .empty;
161- errdefer list.deinit(a);
162- try list.append(a, .{ .name = "Content-Type", .value = "application/json" });
163- try list.append(a, .{ .name = "Accept", .value = "text/event-stream" });
164- if (auth.len > 0) try list.append(a, .{ .name = "Authorization", .value = auth });
165- for (req.model.headers) |kv| try list.append(a, .{ .name = kv[0], .value = kv[1] });
166- if (req.session_id) |sid| {
167- if (req.model.session_header) |name| {
168- if (sid.len > 0) try list.append(a, .{ .name = name, .value = sid });
169- }
170- }
171- return list.toOwnedSlice(a);
148+ return api.headers(a, req, &.{.{ "Authorization", auth }});
172149 }
173150
174151 // ---- streaming state ------------------------------------------------------
175152
176153 const PendingCall = struct {
177- index: i64,
178154 id: []const u8 = "",
179155 name: []const u8 = "",
180156 args: std.ArrayList(u8) = .empty,
@@ -200,28 +176,20 @@ fn ensureCall(st: *State, index: i64) !*PendingCall {
200176 else
201177 st.calls.items.len;
202178 while (st.calls.items.len <= idx) {
203- try st.calls.append(st.req.pers, .{ .index = @intCast(st.calls.items.len) });
179+ try st.calls.append(st.req.pers, .{});
204180 }
205181 return &st.calls.items[idx];
206182 }
207183
208-fn deltaString(delta: std.json.ObjectMap, key: []const u8) ?[]const u8 {
209- const v = delta.get(key) orelse return null;
210- return switch (v) {
211- .string => |s| if (s.len > 0) s else null,
212- else => null,
213- };
214-}
215-
216184 fn handleDelta(st: *State, delta: std.json.ObjectMap) !void {
217- if (deltaString(delta, "content")) |s| {
185+ if (api.stringField(delta, "content")) |s| {
218186 try st.text.appendSlice(st.req.pers, s);
219187 st.sink.emit(.{ .text = s });
220188 }
221189
222190 const reasoning_fields = [_][]const u8{ "reasoning_content", "reasoning", "reasoning_text" };
223191 for (reasoning_fields) |field| {
224- if (deltaString(delta, field)) |s| {
192+ if (api.stringField(delta, field)) |s| {
225193 if (st.signature == null) st.signature = field;
226194 try st.thinking.appendSlice(st.req.pers, s);
227195 st.sink.emit(.{ .reasoning = s });
@@ -258,36 +226,23 @@ fn handleDelta(st: *State, delta: std.json.ObjectMap) !void {
258226 }
259227 }
260228
261-fn num(v: Value) u64 {
262- return switch (v) {
263- .integer => |i| @intCast(@max(i, 0)),
264- .float => |f| @intFromFloat(@max(f, 0)),
265- else => 0,
266- };
267-}
268-
269-fn numField(obj: std.json.ObjectMap, key: []const u8) u64 {
270- const v = obj.get(key) orelse return 0;
271- return num(v);
272-}
273-
274229 fn parseUsage(st: *State, usage: std.json.ObjectMap) void {
275230 var u = types.Usage{};
276- const prompt = numField(usage, "prompt_tokens");
231+ const prompt = api.numField(usage, "prompt_tokens");
277232 var cache_read: u64 = 0;
278233 if (usage.get("prompt_tokens_details")) |details| {
279- if (details == .object) cache_read = numField(details.object, "cached_tokens");
234+ if (details == .object) cache_read = api.numField(details.object, "cached_tokens");
280235 }
281- if (cache_read == 0) cache_read = numField(usage, "cached_tokens");
236+ if (cache_read == 0) cache_read = api.numField(usage, "cached_tokens");
282237 var cache_write: u64 = 0;
283238 if (usage.get("prompt_tokens_details")) |details| {
284- if (details == .object) cache_write = numField(details.object, "cache_write_tokens");
239+ if (details == .object) cache_write = api.numField(details.object, "cache_write_tokens");
285240 }
286- const output = numField(usage, "completion_tokens");
241+ const output = api.numField(usage, "completion_tokens");
287242 if (usage.get("completion_tokens_details")) |details| {
288243 if (details == .object) {
289244 if (details.object.get("reasoning_tokens") != null) {
290- u.reasoning = numField(details.object, "reasoning_tokens");
245+ u.reasoning = api.numField(details.object, "reasoning_tokens");
291246 }
292247 }
293248 }
@@ -296,19 +251,10 @@ fn parseUsage(st: *State, usage: std.json.ObjectMap) void {
296251 u.cache_read = cache_read;
297252 u.cache_write = cache_write;
298253 u.total_tokens = u.input + output + cache_read + cache_write;
299- applyCost(st.req.model, &u);
254+ api.applyCost(st.req.model, &u);
300255 st.msg.usage = u;
301256 }
302257
303-fn applyCost(model: *const types.Model, u: *types.Usage) void {
304- const m = 1_000_000.0;
305- u.cost_input = @as(f64, @floatFromInt(u.input)) * model.cost_input / m;
306- u.cost_output = @as(f64, @floatFromInt(u.output)) * model.cost_output / m;
307- u.cost_cache_read = @as(f64, @floatFromInt(u.cache_read)) * model.cost_cache_read / m;
308- u.cost_cache_write = 0;
309- u.cost_total = u.cost_input + u.cost_output + u.cost_cache_read + u.cost_cache_write;
310-}
311-
312258 fn errorMessageFrom(root: std.json.ObjectMap) ?[]const u8 {
313259 const err = root.get("error") orelse return null;
314260 return switch (err) {
@@ -442,28 +388,9 @@ pub fn stream(req: api.Request, sink: api.Sink) std.mem.Allocator.Error!types.As
442388 const hdrs = headers(req.scratch, req, auth) catch return error.OutOfMemory;
443389
444390 var st = State{ .req = req, .sink = sink, .arena = &arena, .msg = &msg };
445- var err_body: ?[]const u8 = null;
446-
447- http.postSse(req.scratch, url, hdrs, body, .{ .ctx = &st, .onEvent = onData }, req.cancel, &err_body) catch |e| {
448- if (e == error.OutOfMemory) return error.OutOfMemory;
449- if (e == error.Aborted or req.cancel.load(.acquire)) {
450- msg.stop_reason = .aborted;
451- } else {
452- msg.stop_reason = .err;
453- const message = switch (e) {
454- error.HttpStatus => err_body orelse "provider returned an error status",
455- error.ReadFailed => "stream read failed",
456- else => "request failed",
457- };
458- msg.error_message = try req.pers.dupe(u8, message);
459- }
460- return msg;
461- };
462391
463- if (req.cancel.load(.acquire)) {
464- msg.stop_reason = .aborted;
465- return msg;
466- }
392+ if (!try api.post(req, url, hdrs, body, .{ .ctx = &st, .onEvent = onData }, &msg)) return msg;
393+
467394 if (st.stream_error) |message| {
468395 msg.stop_reason = .err;
469396 msg.error_message = try req.pers.dupe(u8, message);
src/api/openai_responses.zig+31−97
@@ -57,19 +57,12 @@ fn writeInput(w: *std.Io.Writer, msg: types.Message) !void {
5757 }
5858
5959 fn writeTools(w: *std.Io.Writer, a: std.mem.Allocator, tools_json: []const u8) !void {
60- if (tools_json.len == 0) return;
61- const root = std.json.parseFromSliceLeaky(Value, a, tools_json, .{}) catch return;
62- if (root != .array) return;
60+ const tools = api.parseTools(a, tools_json) orelse return;
6361 try w.writeAll(",\"tools\":[");
64- for (root.array.items, 0..) |tool, i| {
62+ for (tools, 0..) |tool, i| {
6563 if (i > 0) try w.writeByte(',');
66- const obj = tool.object;
67- try w.writeAll("{\"type\":\"function\",\"name\":");
68- try json.writeString(w, obj.get("name").?.string);
69- try w.writeAll(",\"description\":");
70- try json.writeString(w, obj.get("description").?.string);
71- try w.writeAll(",\"parameters\":");
72- try std.json.Stringify.value(obj.get("parameters").?, .{}, w);
64+ try w.writeAll("{\"type\":\"function\",");
65+ try api.writeToolBody(w, tool.object, "parameters");
7366 try w.writeByte('}');
7467 }
7568 try w.writeByte(']');
@@ -110,22 +103,11 @@ fn buildBody(req: api.Request) ![]u8 {
110103 }
111104
112105 fn buildUrl(a: std.mem.Allocator, base: []const u8) ![]u8 {
113- return std.fmt.allocPrint(a, "{s}/responses", .{std.mem.trimEnd(u8, base, "/")});
106+ return api.buildUrl(a, base, "/responses");
114107 }
115108
116109 fn headers(a: std.mem.Allocator, req: api.Request, auth: []const u8) ![]http.Header {
117- var list: std.ArrayList(http.Header) = .empty;
118- errdefer list.deinit(a);
119- try list.append(a, .{ .name = "Content-Type", .value = "application/json" });
120- try list.append(a, .{ .name = "Accept", .value = "text/event-stream" });
121- if (auth.len > 0) try list.append(a, .{ .name = "Authorization", .value = auth });
122- for (req.model.headers) |kv| try list.append(a, .{ .name = kv[0], .value = kv[1] });
123- if (req.session_id) |sid| {
124- if (req.model.session_header) |name| {
125- if (sid.len > 0) try list.append(a, .{ .name = name, .value = sid });
126- }
127- }
128- return list.toOwnedSlice(a);
110+ return api.headers(a, req, &.{.{ "Authorization", auth }});
129111 }
130112
131113 // ---- streaming state ------------------------------------------------------
@@ -166,43 +148,14 @@ fn streamIndex(obj: std.json.ObjectMap, fallback: usize) usize {
166148 };
167149 }
168150
169-fn num(v: Value) u64 {
170- return switch (v) {
171- .integer => |n| @intCast(@max(n, 0)),
172- .float => |f| @intFromFloat(@max(f, 0)),
173- else => 0,
174- };
175-}
176-
177-fn numField(obj: std.json.ObjectMap, key: []const u8) u64 {
178- return num(obj.get(key) orelse return 0);
179-}
180-
181-fn stringField(obj: std.json.ObjectMap, key: []const u8) ?[]const u8 {
182- const v = obj.get(key) orelse return null;
183- return switch (v) {
184- .string => |s| if (s.len > 0) s else null,
185- else => null,
186- };
187-}
188-
189-fn applyCost(model: *const types.Model, u: *types.Usage) void {
190- const m = 1_000_000.0;
191- u.cost_input = @as(f64, @floatFromInt(u.input)) * model.cost_input / m;
192- u.cost_output = @as(f64, @floatFromInt(u.output)) * model.cost_output / m;
193- u.cost_cache_read = @as(f64, @floatFromInt(u.cache_read)) * model.cost_cache_read / m;
194- u.cost_cache_write = 0;
195- u.cost_total = u.cost_input + u.cost_output + u.cost_cache_read + u.cost_cache_write;
196-}
197-
198151 fn handle(st: *State, obj: std.json.ObjectMap) !void {
199- const event_type = stringField(obj, "type") orelse "";
152+ const event_type = api.stringField(obj, "type") orelse "";
200153
201154 if (std.mem.eql(u8, event_type, "error")) {
202- const message = stringField(obj, "message") orelse blk: {
155+ const message = api.stringField(obj, "message") orelse blk: {
203156 const e = obj.get("error") orelse break :blk "stream: provider error";
204157 break :blk switch (e) {
205- .object => |o| stringField(o, "message") orelse "stream: provider error",
158+ .object => |o| api.stringField(o, "message") orelse "stream: provider error",
206159 else => "stream: provider error",
207160 };
208161 };
@@ -213,7 +166,7 @@ fn handle(st: *State, obj: std.json.ObjectMap) !void {
213166 if (std.mem.eql(u8, event_type, "response.created")) {
214167 if (obj.get("response")) |resp| {
215168 if (resp == .object and st.msg.response_id == null) {
216- if (stringField(resp.object, "id")) |id| {
169+ if (api.stringField(resp.object, "id")) |id| {
217170 st.msg.response_id = try st.req.pers.dupe(u8, id);
218171 }
219172 }
@@ -224,12 +177,12 @@ fn handle(st: *State, obj: std.json.ObjectMap) !void {
224177 if (std.mem.eql(u8, event_type, "response.output_item.added")) {
225178 const item = obj.get("item") orelse return;
226179 if (item != .object) return;
227- if (!std.mem.eql(u8, stringField(item.object, "type") orelse "", "function_call")) return;
180+ if (!std.mem.eql(u8, api.stringField(item.object, "type") orelse "", "function_call")) return;
228181 const index = streamIndex(obj, st.calls.items.len);
229182 try grow(st, index + 1);
230183 const call = &st.calls.items[index];
231- if (stringField(item.object, "call_id")) |id| call.id = try st.req.pers.dupe(u8, id);
232- if (stringField(item.object, "name")) |name| call.name = try st.req.pers.dupe(u8, name);
184+ if (api.stringField(item.object, "call_id")) |id| call.id = try st.req.pers.dupe(u8, id);
185+ if (api.stringField(item.object, "name")) |name| call.name = try st.req.pers.dupe(u8, name);
233186 if (!call.started and call.name.len > 0) {
234187 call.started = true;
235188 st.sink.emit(.{ .tool_start = call.name });
@@ -240,7 +193,7 @@ fn handle(st: *State, obj: std.json.ObjectMap) !void {
240193 if (std.mem.eql(u8, event_type, "response.output_text.delta") or
241194 std.mem.eql(u8, event_type, "response.refusal.delta"))
242195 {
243- if (stringField(obj, "delta")) |s| {
196+ if (api.stringField(obj, "delta")) |s| {
244197 try st.text.appendSlice(st.req.pers, s);
245198 st.sink.emit(.{ .text = s });
246199 }
@@ -250,7 +203,7 @@ fn handle(st: *State, obj: std.json.ObjectMap) !void {
250203 if (std.mem.eql(u8, event_type, "response.reasoning_summary_text.delta") or
251204 std.mem.eql(u8, event_type, "response.reasoning_text.delta"))
252205 {
253- if (stringField(obj, "delta")) |s| {
206+ if (api.stringField(obj, "delta")) |s| {
254207 try st.reasoning.appendSlice(st.req.pers, s);
255208 st.sink.emit(.{ .reasoning = s });
256209 }
@@ -260,7 +213,7 @@ fn handle(st: *State, obj: std.json.ObjectMap) !void {
260213 if (std.mem.eql(u8, event_type, "response.function_call_arguments.delta")) {
261214 const index = streamIndex(obj, st.calls.items.len);
262215 if (index < st.calls.items.len) {
263- if (stringField(obj, "delta")) |s| try st.calls.items[index].args.appendSlice(st.req.pers, s);
216+ if (api.stringField(obj, "delta")) |s| try st.calls.items[index].args.appendSlice(st.req.pers, s);
264217 }
265218 return;
266219 }
@@ -268,7 +221,7 @@ fn handle(st: *State, obj: std.json.ObjectMap) !void {
268221 if (std.mem.eql(u8, event_type, "response.function_call_arguments.done")) {
269222 const index = streamIndex(obj, st.calls.items.len);
270223 if (index < st.calls.items.len) {
271- if (stringField(obj, "arguments")) |s| {
224+ if (api.stringField(obj, "arguments")) |s| {
272225 st.calls.items[index].args.clearRetainingCapacity();
273226 try st.calls.items[index].args.appendSlice(st.req.pers, s);
274227 }
@@ -279,18 +232,18 @@ fn handle(st: *State, obj: std.json.ObjectMap) !void {
279232 if (std.mem.eql(u8, event_type, "response.output_item.done")) {
280233 const item = obj.get("item") orelse return;
281234 if (item != .object) return;
282- if (!std.mem.eql(u8, stringField(item.object, "type") orelse "", "function_call")) return;
235+ if (!std.mem.eql(u8, api.stringField(item.object, "type") orelse "", "function_call")) return;
283236 const index = streamIndex(obj, st.calls.items.len);
284237 if (index >= st.calls.items.len) return;
285238 const call = &st.calls.items[index];
286- if (stringField(item.object, "call_id")) |id| call.id = try st.req.pers.dupe(u8, id);
287- if (stringField(item.object, "name")) |name| call.name = try st.req.pers.dupe(u8, name);
239+ if (api.stringField(item.object, "call_id")) |id| call.id = try st.req.pers.dupe(u8, id);
240+ if (api.stringField(item.object, "name")) |name| call.name = try st.req.pers.dupe(u8, name);
288241 if (!call.started and call.name.len > 0) {
289242 call.started = true;
290243 st.sink.emit(.{ .tool_start = call.name });
291244 }
292245 if (call.args.items.len == 0) {
293- if (stringField(item.object, "arguments")) |s| try call.args.appendSlice(st.req.pers, s);
246+ if (api.stringField(item.object, "arguments")) |s| try call.args.appendSlice(st.req.pers, s);
294247 }
295248 return;
296249 }
@@ -300,19 +253,19 @@ fn handle(st: *State, obj: std.json.ObjectMap) !void {
300253 {
301254 const resp = obj.get("response") orelse return;
302255 if (resp != .object) return;
303- if (stringField(resp.object, "status")) |s| st.status = try st.req.pers.dupe(u8, s);
256+ if (api.stringField(resp.object, "status")) |s| st.status = try st.req.pers.dupe(u8, s);
304257 if (resp.object.get("incomplete_details")) |details| {
305258 if (details == .object) {
306- if (stringField(details.object, "reason")) |r| st.incomplete = try st.req.pers.dupe(u8, r);
259+ if (api.stringField(details.object, "reason")) |r| st.incomplete = try st.req.pers.dupe(u8, r);
307260 }
308261 }
309262 if (resp.object.get("usage")) |usage| {
310263 if (usage == .object) {
311- st.tokens_in = numField(usage.object, "input_tokens");
312- st.tokens_out = numField(usage.object, "output_tokens");
313- st.tokens_total = numField(usage.object, "total_tokens");
264+ st.tokens_in = api.numField(usage.object, "input_tokens");
265+ st.tokens_out = api.numField(usage.object, "output_tokens");
266+ st.tokens_total = api.numField(usage.object, "total_tokens");
314267 if (usage.object.get("input_tokens_details")) |d| {
315- if (d == .object) st.cache_read = numField(d.object, "cached_tokens");
268+ if (d == .object) st.cache_read = api.numField(d.object, "cached_tokens");
316269 }
317270 }
318271 }
@@ -324,7 +277,7 @@ fn handle(st: *State, obj: std.json.ObjectMap) !void {
324277 if (resp != .object) return;
325278 const err = resp.object.get("error") orelse return;
326279 const message = switch (err) {
327- .object => |o| stringField(o, "message") orelse "response failed",
280+ .object => |o| api.stringField(o, "message") orelse "response failed",
328281 else => "response failed",
329282 };
330283 if (st.stream_error == null) st.stream_error = try st.req.pers.dupe(u8, message);
@@ -374,7 +327,7 @@ fn finalize(st: *State) !void {
374327 usage.output = st.tokens_out;
375328 usage.cache_read = st.cache_read;
376329 usage.total_tokens = if (st.tokens_total > 0) st.tokens_total else st.tokens_in + st.tokens_out;
377- applyCost(st.req.model, &usage);
330+ api.applyCost(st.req.model, &usage);
378331 st.msg.usage = usage;
379332
380333 if (st.status) |status| {
@@ -416,28 +369,9 @@ pub fn stream(req: api.Request, sink: api.Sink) std.mem.Allocator.Error!types.As
416369 const hdrs = headers(req.scratch, req, auth) catch return error.OutOfMemory;
417370
418371 var st = State{ .req = req, .sink = sink, .arena = &arena, .msg = &msg };
419- var err_body: ?[]const u8 = null;
420-
421- http.postSse(req.scratch, url, hdrs, body, .{ .ctx = &st, .onEvent = onData }, req.cancel, &err_body) catch |e| {
422- if (e == error.OutOfMemory) return error.OutOfMemory;
423- if (e == error.Aborted or req.cancel.load(.acquire)) {
424- msg.stop_reason = .aborted;
425- } else {
426- msg.stop_reason = .err;
427- const message = switch (e) {
428- error.HttpStatus => err_body orelse "provider returned an error status",
429- error.ReadFailed => "stream read failed",
430- else => "request failed",
431- };
432- msg.error_message = try req.pers.dupe(u8, message);
433- }
434- return msg;
435- };
436372
437- if (req.cancel.load(.acquire)) {
438- msg.stop_reason = .aborted;
439- return msg;
Cut short: this commit's diff is over 1000 lines.
src/models.zig+6−13
Cut short: this commit's diff is over 1000 lines.
src/tui/editor.zig+0−13
Cut short: this commit's diff is over 1000 lines.
src/tui/highlight.zig+129−71
Cut short: this commit's diff is over 1000 lines.
src/tui/render.zig+11−0
Cut short: this commit's diff is over 1000 lines.
src/tui/stream.zig+9−3
Cut short: this commit's diff is over 1000 lines.
src/tui/styles.zig+2−13
Cut short: this commit's diff is over 1000 lines.
src/tui/term.zig+4−4
Cut short: this commit's diff is over 1000 lines.
src/tui/theme.zig+41−21
Cut short: this commit's diff is over 1000 lines.
src/tui/tui.zig+16−22
Cut short: this commit's diff is over 1000 lines.
src/types.zig+0−9
Cut short: this commit's diff is over 1000 lines.