Repository navigation
Expand file tree
/
Copy pathprogress.zig
More file actions
41 lines (38 loc) · 2.06 KB
/
Copy pathprogress.zig
File metadata and controls
41 lines (38 loc) · 2.06 KB
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
const std = @import("std");
const dispatch = @import("dispatch.zig");
/// A label for the animation shown while `name` runs, or null for a tool that
/// must not have one.
///
/// A host tool's label is the one its own spec declares, so a tool is named
/// once whether the terminal is showing it, the model is being told about it,
/// or a call is being dispatched to it. Session tools use that same list.
///
/// The labels are written down rather than taken from the call: a tool name is
/// model output, and a name carrying escape sequences would be written straight
/// into the terminal (see `text.zig`). `ask_user` is deliberately absent — it
/// asks a question on the terminal and reads the answer from it, so it needs
/// the line to itself.
pub fn progressLabel(name: []const u8) ?[]const u8 {
inline for (dispatch.all) |spec| {
if (std.mem.eql(u8, name, spec.name)) return spec.label;
}
return null;
}
test "every tool that waits has a progress label, ask_user does not" {
try std.testing.expect(progressLabel("shell") == null);
try std.testing.expect(progressLabel("http_request") == null);
const labelled = [_][]const u8{ "bash", "read", "write", "edit", "ls", "rg", "fd", "apply_patch", "grep", "find", "web_fetch", "web_search", "ccr_retrieve" };
for (labelled) |name| {
const label = progressLabel(name) orelse return error.TestUnexpectedResult;
// The label names the tool, so a user watching the line knows what is
// taking the time.
try std.testing.expect(std.mem.startsWith(u8, label, name));
}
// ask_user puts a question on the terminal and reads the answer from it:
// an animation over that line would fight the prompt.
try std.testing.expectEqual(@as(?[]const u8, null), progressLabel("ask_user"));
// A name the model made up runs nothing and waits for nothing; it is not
// written to the terminal either.
try std.testing.expectEqual(@as(?[]const u8, null), progressLabel("rm -rf /"));
try std.testing.expectEqual(@as(?[]const u8, null), progressLabel("shell\x1b[31m"));
}