Add compact agent context command

This commit is contained in:
slhx agent
2026-06-21 05:00:21 +02:00
parent d1141c89a2
commit ffbff6e4b8
4 changed files with 238 additions and 17 deletions
+24
View File
@@ -1,4 +1,5 @@
const std = @import("std");
const context_mod = @import("context.zig");
const session_mod = @import("session.zig");
// Tiny local session protocol for trusted same-user tools.
@@ -13,6 +14,8 @@ pub const max_request_bytes = 4096;
pub fn handleLine(allocator: std.mem.Allocator, session: *session_mod.Session, line: []const u8) ![]u8 {
const trimmed = std.mem.trimEnd(u8, line, "\r\n");
if (std.mem.eql(u8, trimmed, "state")) return stateResponse(allocator, session);
if (std.mem.eql(u8, trimmed, "context")) return context_mod.sessionContextAlloc(allocator, session, null, null, .{});
if (std.mem.startsWith(u8, trimmed, "context ")) return contextResponse(allocator, session, trimmed[8..]);
if (std.mem.startsWith(u8, trimmed, "open ")) {
const bytes = trimmed[5..];
if (!std.unicode.utf8ValidateSlice(bytes)) return allocator.dupe(u8, "err invalid utf8\n");
@@ -25,6 +28,12 @@ pub fn handleLine(allocator: std.mem.Allocator, session: *session_mod.Session, l
return allocator.dupe(u8, "err unknown request\n");
}
fn contextResponse(allocator: std.mem.Allocator, session: *session_mod.Session, payload: []const u8) ![]u8 {
const split = std.mem.indexOfScalar(u8, payload, ' ');
if (split) |index| return context_mod.sessionContextAlloc(allocator, session, payload[0..index], payload[index + 1 ..], .{});
return context_mod.sessionContextAlloc(allocator, session, payload, null, .{});
}
fn commandResponse(allocator: std.mem.Allocator, session: *session_mod.Session, command: []const u8) ![]u8 {
if (std.mem.eql(u8, command, "move_left")) return dispatchAndRespond(allocator, session, .move_left);
if (std.mem.eql(u8, command, "move_right")) return dispatchAndRespond(allocator, session, .move_right);
@@ -162,6 +171,21 @@ fn stateResponse(allocator: std.mem.Allocator, session: *session_mod.Session) ![
);
}
test "regular: protocol context request returns compact agent context" {
var session = session_mod.Session.init(std.testing.allocator);
defer session.deinit();
try session.openFixture("abc");
try session.openListPanel("diag", &.{ "one", "two" });
const response = try handleLine(std.testing.allocator, &session, "context fix_bug inspect\n");
defer std.testing.allocator.free(response);
try std.testing.expect(std.mem.indexOf(u8, response, "context:v1\n") != null);
try std.testing.expect(std.mem.indexOf(u8, response, "active_panel=diag") != null);
try std.testing.expect(std.mem.indexOf(u8, response, "panel_summary=") != null);
try std.testing.expect(std.mem.indexOf(u8, response, "task=fix_bug") != null);
try std.testing.expect(std.mem.indexOf(u8, response, "intent=inspect") != null);
}
test "regular: protocol opens bytes, reports state, and dispatches commands" {
var session = session_mod.Session.init(std.testing.allocator);
defer session.deinit();