CDP/MCP: add highly compressed text format for semantic tree

This commit is contained in:
Adrià Arrufat
2026-03-08 22:41:51 +09:00
parent b8139a6e83
commit b674c2e448
3 changed files with 138 additions and 9 deletions

View File

@@ -135,8 +135,7 @@ const ToolStreamingText = struct {
jw.endWriteRaw();
},
.semantic_tree => {
// MCP expects a string for "text" content, but our SemanticTree is a complex object.
// We'll serialize it as a string to fit the MCP text protocol requirements.
// Return the highly compressed Stagehand-style text format for maximum token efficiency
try jw.beginWriteRaw();
try jw.writer.writeByte('"');
var escaped = protocol.JsonEscapingWriter.init(jw.writer);
@@ -147,7 +146,8 @@ const ToolStreamingText = struct {
.page = self.server.page,
.arena = self.arena,
};
std.json.Stringify.value(st, .{ .whitespace = .minified }, &escaped.writer) catch |err| {
st.textStringify(&escaped.writer) catch |err| {
log.err(.mcp, "semantic tree dump failed", .{ .err = err });
};