Skip to content
Open
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
32 changes: 32 additions & 0 deletions src/api/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ pub const ParsedConfigPath = struct {
name: []const u8,
};

pub const ParsedConfigPathOwned = struct {
component: []u8,
name: []u8,

pub fn deinit(self: ParsedConfigPathOwned, allocator: std.mem.Allocator) void {
allocator.free(self.component);
allocator.free(self.name);
}

pub fn borrowed(self: ParsedConfigPathOwned) ParsedConfigPath {
return .{ .component = self.component, .name = self.name };
}
};

pub fn parseConfigPath(target: []const u8) ?ParsedConfigPath {
const prefix = "/api/instances/";
const suffix = "/config";
Expand All @@ -205,6 +219,16 @@ pub fn parseConfigPath(target: []const u8) ?ParsedConfigPath {
return .{ .component = component, .name = name };
}

pub fn parseConfigPathAlloc(allocator: std.mem.Allocator, target: []const u8) !?ParsedConfigPathOwned {
const parsed = try query.parseInstancePathPrefixAlloc(allocator, target) orelse return null;
errdefer parsed.deinit(allocator);
if (!std.mem.eql(u8, parsed.suffix, "config")) {
parsed.deinit(allocator);
return null;
}
return .{ .component = parsed.component, .name = parsed.name };
}

fn lookupJsonPath(root: std.json.Value, dot_path: []const u8) ?std.json.Value {
if (dot_path.len == 0) return null;

Expand Down Expand Up @@ -251,6 +275,14 @@ test "parseConfigPath: keeps working with query string" {
try std.testing.expectEqualStrings("my-agent", p.name);
}

test "parseConfigPathAlloc decodes percent-encoded names" {
const allocator = std.testing.allocator;
const p = (try parseConfigPathAlloc(allocator, "/api/instances/nullclaw/Opencode%20Go/config?path=gateway.port")).?;
defer p.deinit(allocator);
try std.testing.expectEqualStrings("nullclaw", p.component);
try std.testing.expectEqualStrings("Opencode Go", p.name);
}

test "parseConfigPath: rejects path without /config suffix" {
try std.testing.expect(parseConfigPath("/api/instances/nullclaw/my-agent") == null);
}
Expand Down
Loading