summaryrefslogtreecommitdiff
path: root/libpanto/src/openai_chat_json.zig
blob: 465d951d43e915fd3bd6f3cfb6acad258a0bb865 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//! OpenAI Chat Completions JSON serialization and parsing.
//!
//! Two responsibilities:
//!   1. Serialize a `Conversation` into the OpenAI Chat Completions request body.
//!   2. Parse one streaming SSE event's JSON payload into a strongly-typed
//!      `StreamDelta` that the provider can consume.

const std = @import("std");
const Allocator = std.mem.Allocator;
const Writer = std.Io.Writer;
const conversation = @import("conversation.zig");
const config_mod = @import("config.zig");

/// A single parsed streaming chunk. Fields are populated only when present
/// in the wire payload; null fields signal "not in this chunk".
///
/// `content` and `reasoning_content` slices are borrowed from the parsed
/// JSON value, which is owned by the caller's `std.json.Parsed`.
pub const StreamDelta = struct {
    role: ?[]const u8 = null,
    content: ?[]const u8 = null,
    reasoning_content: ?[]const u8 = null,
    finish_reason: ?[]const u8 = null,
};

/// Serialize a Conversation into a `chat/completions` request body.
///
/// The caller owns the returned slice (allocated with `allocator`).
pub fn serializeRequest(
    allocator: Allocator,
    cfg: *const config_mod.OpenAIChatConfig,
    conv: *const conversation.Conversation,
) ![]u8 {
    var aw: Writer.Allocating = .init(allocator);
    errdefer aw.deinit();

    var s: std.json.Stringify = .{ .writer = &aw.writer };

    try s.beginObject();

    try s.objectField("model");
    try s.write(cfg.model);

    try s.objectField("stream");
    try s.write(true);

    switch (cfg.reasoning) {
        .default => {},
        .off => {
            try s.objectField("reasoning_effort");
            try s.write("none");
        },
        .minimal, .low, .medium, .high => |eff| {
            try s.objectField("reasoning_effort");
            try s.write(@tagName(eff));
        },
    }

    try s.objectField("messages");
    try s.beginArray();
    for (conv.messages.items) |msg| {
        try writeMessage(&s, msg, allocator);
    }
    try s.endArray();

    try s.endObject();

    return try aw.toOwnedSlice();
}

fn writeMessage(s: *std.json.Stringify, msg: conversation.Message, allocator: Allocator) !void {
    try s.beginObject();

    try s.objectField("role");
    try s.write(@tagName(msg.role));

    // All roles flatten to a plain `content` string in outbound requests.
    // Thinking blocks are intentionally dropped from history: the openai_chat
    // dialect has no portable way to round-trip them (OpenAI/DeepSeek strip;
    // OpenRouter/NanoGPT want a `reasoning` field instead of an inline block;
    // none accept the `{"type":"thinking",...}` shape). Preserving reasoning
    // across turns will land with tool-use in a later phase.
    try s.objectField("content");
    var buf: std.ArrayList(u8) = .empty;
    defer buf.deinit(allocator);
    try concatTextBlocks(msg.content.items, &buf, allocator);
    try s.write(buf.items);

    try s.endObject();
}

fn concatTextBlocks(
    blocks: []const conversation.ContentBlock,
    out: *std.ArrayList(u8),
    allocator: Allocator,
) !void {
    for (blocks) |block| {
        switch (block) {
            .Text => |tb| try out.appendSlice(allocator, tb.items),
            // Thinking: dropped. ToolUse/ToolResult: phase 3+.
            else => {},
        }
    }
}

/// Parse a single SSE event payload (the JSON object that follows "data: ").
///
/// Returns a `StreamDelta` borrowed from `parsed`. The caller must keep
/// `parsed` alive for as long as the delta's slices are in use, then call
/// `parsed.deinit()`.
pub const ParsedDelta = struct {
    parsed: std.json.Parsed(std.json.Value),
    delta: StreamDelta,

    pub fn deinit(self: *ParsedDelta) void {
        self.parsed.deinit();
    }
};

pub fn parseStreamEvent(allocator: Allocator, payload: []const u8) !ParsedDelta {
    var parsed = try std.json.parseFromSlice(std.json.Value, allocator, payload, .{});
    errdefer parsed.deinit();

    var delta: StreamDelta = .{};

    const root = parsed.value;
    if (root != .object) return .{ .parsed = parsed, .delta = delta };

    const choices_v = root.object.get("choices") orelse return .{ .parsed = parsed, .delta = delta };
    if (choices_v != .array or choices_v.array.items.len == 0) {
        return .{ .parsed = parsed, .delta = delta };
    }
    const choice = choices_v.array.items[0];
    if (choice != .object) return .{ .parsed = parsed, .delta = delta };

    if (choice.object.get("finish_reason")) |fr| {
        if (fr == .string) delta.finish_reason = fr.string;
    }

    if (choice.object.get("delta")) |d| {
        if (d == .object) {
            if (d.object.get("role")) |r| {
                if (r == .string) delta.role = r.string;
            }
            if (d.object.get("content")) |c| {
                if (c == .string) delta.content = c.string;
            }
            // Reasoning content lives under one of these names depending on
            // the provider. We accept either.
            if (d.object.get("reasoning_content")) |rc| {
                if (rc == .string) delta.reasoning_content = rc.string;
            } else if (d.object.get("reasoning")) |rc| {
                if (rc == .string) delta.reasoning_content = rc.string;
            }
        }
    }

    return .{ .parsed = parsed, .delta = delta };
}

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

const testing = std.testing;

fn testConfig(model: []const u8) config_mod.OpenAIChatConfig {
    return .{
        .api_key = "k",
        .base_url = "u",
        .model = model,
    };
}

test "serializeRequest - system + user" {
    const allocator = testing.allocator;

    var conv = conversation.Conversation.init(allocator);
    defer conv.deinit();

    try conv.addSystemMessage("You are helpful.");
    try conv.addUserMessage("Hello!");

    const cfg = testConfig("gpt-4o");
    const body = try serializeRequest(allocator, &cfg, &conv);
    defer allocator.free(body);

    var parsed = try std.json.parseFromSlice(std.json.Value, allocator, body, .{});
    defer parsed.deinit();

    const root = parsed.value.object;
    try testing.expectEqualStrings("gpt-4o", root.get("model").?.string);
    try testing.expect(root.get("stream").?.bool);
    // reasoning_effort is omitted when set to .default.
    try testing.expect(root.get("reasoning_effort") == null);

    const msgs = root.get("messages").?.array.items;
    try testing.expectEqual(@as(usize, 2), msgs.len);
    try testing.expectEqualStrings("system", msgs[0].object.get("role").?.string);
    try testing.expectEqualStrings("You are helpful.", msgs[0].object.get("content").?.string);
    try testing.expectEqualStrings("user", msgs[1].object.get("role").?.string);
    try testing.expectEqualStrings("Hello!", msgs[1].object.get("content").?.string);
}

test "serializeRequest - assistant Thinking blocks are stripped from outbound history" {
    const allocator = testing.allocator;

    var conv = conversation.Conversation.init(allocator);
    defer conv.deinit();

    try conv.addAssistantMessage(&.{
        .{ .Thinking = .{ .text = try conversation.textualBlockFromSlice(allocator, "thinking step") } },
        .{ .Text = try conversation.textualBlockFromSlice(allocator, "answer here") },
    });

    const cfg = testConfig("gpt-4o");
    const body = try serializeRequest(allocator, &cfg, &conv);
    defer allocator.free(body);

    var parsed = try std.json.parseFromSlice(std.json.Value, allocator, body, .{});
    defer parsed.deinit();

    const msg = parsed.value.object.get("messages").?.array.items[0];
    try testing.expectEqualStrings("assistant", msg.object.get("role").?.string);
    // Content is a flat string, only the Text block survives.
    const content = msg.object.get("content").?.string;
    try testing.expectEqualStrings("answer here", content);
}

test "serializeRequest - reasoning effort level included when set" {
    const allocator = testing.allocator;

    var conv = conversation.Conversation.init(allocator);
    defer conv.deinit();
    try conv.addUserMessage("Hi");

    var cfg = testConfig("gpt-4o");
    cfg.reasoning = .high;

    const body = try serializeRequest(allocator, &cfg, &conv);
    defer allocator.free(body);

    var parsed = try std.json.parseFromSlice(std.json.Value, allocator, body, .{});
    defer parsed.deinit();

    try testing.expectEqualStrings(
        "high",
        parsed.value.object.get("reasoning_effort").?.string,
    );
}

test "serializeRequest - reasoning .off sends \"none\"" {
    const allocator = testing.allocator;

    var conv = conversation.Conversation.init(allocator);
    defer conv.deinit();
    try conv.addUserMessage("Hi");

    var cfg = testConfig("gpt-4o");
    cfg.reasoning = .off;

    const body = try serializeRequest(allocator, &cfg, &conv);
    defer allocator.free(body);

    var parsed = try std.json.parseFromSlice(std.json.Value, allocator, body, .{});
    defer parsed.deinit();

    try testing.expectEqualStrings(
        "none",
        parsed.value.object.get("reasoning_effort").?.string,
    );
}

test "parseStreamEvent - role only" {
    const allocator = testing.allocator;
    const payload =
        \\{"id":"chatcmpl-1","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}
    ;

    var pd = try parseStreamEvent(allocator, payload);
    defer pd.deinit();

    try testing.expectEqualStrings("assistant", pd.delta.role.?);
    try testing.expect(pd.delta.content == null);
    try testing.expect(pd.delta.finish_reason == null);
}

test "parseStreamEvent - content delta" {
    const allocator = testing.allocator;
    const payload =
        \\{"choices":[{"delta":{"content":"Hello"},"finish_reason":null}]}
    ;

    var pd = try parseStreamEvent(allocator, payload);
    defer pd.deinit();

    try testing.expectEqualStrings("Hello", pd.delta.content.?);
}

test "parseStreamEvent - finish_reason stop" {
    const allocator = testing.allocator;
    const payload =
        \\{"choices":[{"delta":{},"finish_reason":"stop"}]}
    ;

    var pd = try parseStreamEvent(allocator, payload);
    defer pd.deinit();

    try testing.expectEqualStrings("stop", pd.delta.finish_reason.?);
    try testing.expect(pd.delta.content == null);
}

test "parseStreamEvent - reasoning_content" {
    const allocator = testing.allocator;
    const payload =
        \\{"choices":[{"delta":{"reasoning_content":"hmm"}}]}
    ;

    var pd = try parseStreamEvent(allocator, payload);
    defer pd.deinit();

    try testing.expectEqualStrings("hmm", pd.delta.reasoning_content.?);
}