Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions zig/src/server/tool_parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -427,21 +427,18 @@ fn glmCall(a: Allocator, written: []const u8, schemas: ?*const tool_params.Schem
var residue: std.ArrayList(u8) = .empty;
var pos: usize = 0;
var kept: usize = 0;
outer: while (find(rest, "<arg_key>", pos)) |s| {
var key_end = valueKey(rest, 9); // the first </arg_key> an <arg_value> follows, searched again only once passed
var value_end = find(rest, "</arg_value>", 0); // likewise the first </arg_value>
while (find(rest, "<arg_key>", pos)) |s| {
pos = s + 1;
var key_end_at = s + 9;
while (find(rest, "</arg_key>", key_end_at)) |ke| : (key_end_at = ke + 1) {
var j = ke + 10;
while (j < rest.len and space(rest[j])) j += 1;
if (!std.mem.startsWith(u8, rest[j..], "<arg_value>")) continue;
const v_start = j + 11;
const ve = find(rest, "</arg_value>", v_start) orelse continue :outer;
try pairs.append(a, .{ .key = rest[s + 9 .. ke], .value = rest[v_start..ve] });
try residue.appendSlice(a, rest[kept..s]);
kept = ve + 12;
pos = kept;
continue :outer;
}
if (key_end != null and key_end.?[0] < s + 9) key_end = valueKey(rest, s + 9);
const k = key_end orelse continue;
if (value_end != null and value_end.? < k[1]) value_end = find(rest, "</arg_value>", k[1]);
const ve = value_end orelse continue;
try pairs.append(a, .{ .key = rest[s + 9 .. k[0]], .value = rest[k[1]..ve] });
try residue.appendSlice(a, rest[kept..s]);
kept = ve + 12;
pos = kept;
}
try residue.appendSlice(a, rest[kept..]);
if (complete and !allSpace(residue.items)) return null;
Expand All @@ -454,6 +451,17 @@ fn glmCall(a: Allocator, written: []const u8, schemas: ?*const tool_params.Schem
return .{ .name = name, .arguments = .{ .object = arguments } };
}

/// The first ``</arg_key>`` at or after `from` that an ``<arg_value>`` follows, and where that value starts.
fn valueKey(rest: []const u8, from: usize) ?[2]usize {
var at = from;
while (find(rest, "</arg_key>", at)) |ke| : (at = ke + 1) {
var j = ke + 10;
while (j < rest.len and space(rest[j])) j += 1;
if (std.mem.startsWith(u8, rest[j..], "<arg_value>")) return .{ ke, j + 11 };
}
return null;
}

/// Gemma 4's ``call:NAME{key:value,...}`` (or bare ``:NAME{...}``): strings between ``<|"|>``, keys bare.
fn gemmaCall(a: Allocator, block: []const u8) Error!?Call {
var i: usize = 0;
Expand Down
25 changes: 25 additions & 0 deletions zig/src/server/tool_parse_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,31 @@ test "a call to a tool the request did not declare stays text" {
try std.testing.expect((try parse(a, "{\"name\":\"launch\",\"arguments\":{}}", tools, null)).calls == null);
}

test "a GLM call's keys without values read in linear time" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
const tools = (try json.parse(a, "[{\"type\":\"function\",\"function\":{\"name\":\"lookup\"}}]")).ok.array;
var ns: [2]u64 = undefined;
for ([_]usize{ 800, 6400 }, &ns) |n, *best| {
var reply: std.ArrayList(u8) = .empty;
try reply.appendSlice(a, "<tool_call>lookup");
for (0..n) |_| try reply.appendSlice(a, "<arg_key>n</arg_key>");
try reply.appendSlice(a, "</tool_call>");
best.* = std.math.maxInt(u64);
for (0..3) |_| {
var run: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer run.deinit();
const start = std.Io.Clock.awake.now(std.testing.io).nanoseconds;
const r = try parse(run.allocator(), reply.items, tools, null);
best.* = @min(best.*, @as(u64, @intCast(std.Io.Clock.awake.now(std.testing.io).nanoseconds - start)));
try std.testing.expectEqualStrings("{}", r.calls.?[0].get("function").?.get("arguments").?.string);
}
}
// 8x the keys: near 8x the time, not 64x
try std.testing.expect(ns[1] <= 20 * std.time.ns_per_ms or ns[1] <= 24 * ns[0]);
}

test "GLM calls keep a declared name with spaces, dots, colons or unicode; an undeclared one stays text" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
Expand Down