@steerable/agent-protocol 0.14.2 → 0.14.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +3 -0
- package/dist/index.js +4 -0
- package/dist/proposed-plan-parser.d.ts +49 -0
- package/dist/proposed-plan-parser.js +236 -0
- package/dist/shell-command-classifier.d.ts +41 -0
- package/dist/shell-command-classifier.js +616 -0
- package/dist/tool-transcript-model.d.ts +92 -0
- package/dist/tool-transcript-model.js +626 -0
- package/package.json +1 -1
|
@@ -0,0 +1,626 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-dependency transcript parsing helpers shared by `@steerable/agent-shell-web`
|
|
3
|
+
* and `@steerable/agent-cli` (TUI).
|
|
4
|
+
*
|
|
5
|
+
* Lives in Tier-1 `@steerable/agent-protocol` (alongside `turn-failure-kind.ts`)
|
|
6
|
+
* so browser bundles and Node TUI share one implementation for:
|
|
7
|
+
* 1. Unwrapping serialized / 300-char-truncated `ToolResult` JSON envelopes;
|
|
8
|
+
* 2. Extracting clean terminal `command`, `stdout`, `stderr`, `exitCode`;
|
|
9
|
+
* 3. Extracting or synthesizing Unified Diff + `+added / -removed` stats;
|
|
10
|
+
* 4. Detecting skill invocations (`skill` tool or reading `.../SKILL.md`);
|
|
11
|
+
* 5. Codex-style head + tail middle output truncation (`truncateOutputMiddle`).
|
|
12
|
+
*/
|
|
13
|
+
import { classifyShellCommand, extractSkillNameFromPath, } from './shell-command-classifier.js';
|
|
14
|
+
const ENVELOPE_KEYS = new Set([
|
|
15
|
+
'success',
|
|
16
|
+
'error',
|
|
17
|
+
'message',
|
|
18
|
+
'data',
|
|
19
|
+
'needsFollowup',
|
|
20
|
+
'terminal',
|
|
21
|
+
'nextAction',
|
|
22
|
+
]);
|
|
23
|
+
function asObject(value) {
|
|
24
|
+
return value && typeof value === 'object' && !Array.isArray(value)
|
|
25
|
+
? value
|
|
26
|
+
: null;
|
|
27
|
+
}
|
|
28
|
+
function stringField(obj, key) {
|
|
29
|
+
if (!obj)
|
|
30
|
+
return null;
|
|
31
|
+
const value = obj[key];
|
|
32
|
+
return typeof value === 'string' && value.trim() ? value.trim() : null;
|
|
33
|
+
}
|
|
34
|
+
/** Parse a JSON object/array string when well-formed; return original value otherwise. */
|
|
35
|
+
export function parseJsonValue(value) {
|
|
36
|
+
if (typeof value !== 'string')
|
|
37
|
+
return value;
|
|
38
|
+
const trimmed = value.trim();
|
|
39
|
+
if (!trimmed.startsWith('{') && !trimmed.startsWith('['))
|
|
40
|
+
return value;
|
|
41
|
+
try {
|
|
42
|
+
return JSON.parse(trimmed);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export function extractPartialJsonStringFieldWithState(raw, field) {
|
|
49
|
+
const pattern = new RegExp(`"${field}"\\s*:\\s*"`);
|
|
50
|
+
const match = pattern.exec(raw);
|
|
51
|
+
if (!match)
|
|
52
|
+
return null;
|
|
53
|
+
let idx = match.index + match[0].length;
|
|
54
|
+
let out = '';
|
|
55
|
+
while (idx < raw.length) {
|
|
56
|
+
const ch = raw[idx];
|
|
57
|
+
if (ch === '"') {
|
|
58
|
+
return { value: out, complete: true };
|
|
59
|
+
}
|
|
60
|
+
if (ch === '\\') {
|
|
61
|
+
if (idx + 1 >= raw.length)
|
|
62
|
+
break;
|
|
63
|
+
const next = raw[idx + 1];
|
|
64
|
+
switch (next) {
|
|
65
|
+
case '"':
|
|
66
|
+
case '\\':
|
|
67
|
+
case '/':
|
|
68
|
+
out += next;
|
|
69
|
+
idx += 2;
|
|
70
|
+
break;
|
|
71
|
+
case 'n':
|
|
72
|
+
out += '\n';
|
|
73
|
+
idx += 2;
|
|
74
|
+
break;
|
|
75
|
+
case 'r':
|
|
76
|
+
out += '\r';
|
|
77
|
+
idx += 2;
|
|
78
|
+
break;
|
|
79
|
+
case 't':
|
|
80
|
+
out += '\t';
|
|
81
|
+
idx += 2;
|
|
82
|
+
break;
|
|
83
|
+
case 'b':
|
|
84
|
+
out += '\b';
|
|
85
|
+
idx += 2;
|
|
86
|
+
break;
|
|
87
|
+
case 'f':
|
|
88
|
+
out += '\f';
|
|
89
|
+
idx += 2;
|
|
90
|
+
break;
|
|
91
|
+
case 'u': {
|
|
92
|
+
const hex = raw.slice(idx + 2, idx + 6);
|
|
93
|
+
if (/^[0-9a-fA-F]{4}$/.test(hex)) {
|
|
94
|
+
out += String.fromCharCode(Number.parseInt(hex, 16));
|
|
95
|
+
idx += 6;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
out += 'u';
|
|
99
|
+
idx += 2;
|
|
100
|
+
}
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
default:
|
|
104
|
+
out += next;
|
|
105
|
+
idx += 2;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
out += ch;
|
|
111
|
+
idx += 1;
|
|
112
|
+
}
|
|
113
|
+
return { value: out, complete: false };
|
|
114
|
+
}
|
|
115
|
+
export function extractPartialJsonStringField(raw, field) {
|
|
116
|
+
return extractPartialJsonStringFieldWithState(raw, field)?.value ?? null;
|
|
117
|
+
}
|
|
118
|
+
export function extractPartialJsonExitCode(raw) {
|
|
119
|
+
const match = /"(?:exitCode|exit_code|returncode)"\s*:\s*(-?\d+)/.exec(raw);
|
|
120
|
+
if (!match)
|
|
121
|
+
return null;
|
|
122
|
+
const num = Number(match[1]);
|
|
123
|
+
return Number.isFinite(num) ? num : null;
|
|
124
|
+
}
|
|
125
|
+
function numericExitCode(record) {
|
|
126
|
+
if (!record)
|
|
127
|
+
return null;
|
|
128
|
+
for (const key of ['exitCode', 'exit_code', 'returncode']) {
|
|
129
|
+
const val = record[key];
|
|
130
|
+
if (typeof val === 'number' && Number.isFinite(val))
|
|
131
|
+
return val;
|
|
132
|
+
}
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
/** Unwrap a sidecar `ToolResult` (object or JSON/truncated-JSON string) into normalized envelope fields. */
|
|
136
|
+
export function parseToolEnvelope(result) {
|
|
137
|
+
const parsed = parseJsonValue(result);
|
|
138
|
+
const obj = asObject(parsed);
|
|
139
|
+
if (!obj) {
|
|
140
|
+
if (typeof parsed === 'string' && parsed.trimStart().startsWith('{')) {
|
|
141
|
+
const successMatch = /"success"\s*:\s*(true|false)/.exec(parsed);
|
|
142
|
+
const partialErr = extractPartialJsonStringField(parsed, 'error');
|
|
143
|
+
const partialMsg = extractPartialJsonStringField(parsed, 'message');
|
|
144
|
+
if (successMatch || partialErr || partialMsg) {
|
|
145
|
+
return {
|
|
146
|
+
success: successMatch ? successMatch[1] === 'true' : null,
|
|
147
|
+
error: partialErr?.trim() || null,
|
|
148
|
+
message: partialMsg?.trim() || null,
|
|
149
|
+
fields: {},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
success: null,
|
|
155
|
+
error: null,
|
|
156
|
+
message: typeof parsed === 'string' && parsed.trim() ? parsed.trim() : null,
|
|
157
|
+
fields: {},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
const data = asObject(obj.data);
|
|
161
|
+
const fields = {};
|
|
162
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
163
|
+
if (!ENVELOPE_KEYS.has(key) && value !== undefined)
|
|
164
|
+
fields[key] = value;
|
|
165
|
+
}
|
|
166
|
+
if (data) {
|
|
167
|
+
for (const [key, value] of Object.entries(data)) {
|
|
168
|
+
if (key === 'durationMs' || value === undefined)
|
|
169
|
+
continue;
|
|
170
|
+
fields[key] = value;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const dataText = typeof obj.data === 'string' && obj.data.trim() ? obj.data.trim() : null;
|
|
174
|
+
return {
|
|
175
|
+
success: typeof obj.success === 'boolean' ? obj.success : null,
|
|
176
|
+
error: stringField(obj, 'error'),
|
|
177
|
+
message: stringField(obj, 'message') ?? stringField(data, 'message') ?? dataText,
|
|
178
|
+
fields,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
function collectPackageList(argsObj) {
|
|
182
|
+
if (!argsObj)
|
|
183
|
+
return undefined;
|
|
184
|
+
const pkgs = [];
|
|
185
|
+
for (const key of ['pipPackages', 'npmPackages']) {
|
|
186
|
+
const arr = argsObj[key];
|
|
187
|
+
if (!Array.isArray(arr))
|
|
188
|
+
continue;
|
|
189
|
+
for (const item of arr) {
|
|
190
|
+
if (typeof item === 'string' && item.trim())
|
|
191
|
+
pkgs.push(item.trim());
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return pkgs.length > 0 ? pkgs : undefined;
|
|
195
|
+
}
|
|
196
|
+
/** Unwrap a terminal/snippet tool call into clean command + stdout/stderr without internal `_sandbox` keys. */
|
|
197
|
+
export function parseTerminalExecution(action) {
|
|
198
|
+
const argsObj = asObject(action.arguments);
|
|
199
|
+
const rawCommand = (typeof argsObj?.command === 'string' && argsObj.command.trim() ? argsObj.command : null) ??
|
|
200
|
+
(typeof argsObj?.cmd === 'string' && argsObj.cmd.trim() ? argsObj.cmd : null) ??
|
|
201
|
+
(typeof argsObj?.code === 'string' && argsObj.code.trim() ? argsObj.code : null) ??
|
|
202
|
+
(typeof argsObj?.chars === 'string' && argsObj.chars.length > 0 ? argsObj.chars : null) ??
|
|
203
|
+
stringField(argsObj, 'scriptId');
|
|
204
|
+
const language = stringField(argsObj, 'language');
|
|
205
|
+
const cwd = stringField(argsObj, 'cwd');
|
|
206
|
+
const packages = collectPackageList(argsObj);
|
|
207
|
+
const baseMeta = packages ? { packages } : {};
|
|
208
|
+
if (action.result === undefined || action.result === null) {
|
|
209
|
+
return {
|
|
210
|
+
settled: false,
|
|
211
|
+
command: rawCommand,
|
|
212
|
+
language,
|
|
213
|
+
cwd,
|
|
214
|
+
...baseMeta,
|
|
215
|
+
stdout: '',
|
|
216
|
+
stderr: '',
|
|
217
|
+
error: null,
|
|
218
|
+
exitCode: null,
|
|
219
|
+
timedOut: false,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
const parsed = parseJsonValue(action.result);
|
|
223
|
+
if (typeof parsed === 'string') {
|
|
224
|
+
if (parsed.trimStart().startsWith('{')) {
|
|
225
|
+
const stdout = extractPartialJsonStringField(parsed, 'stdout') ??
|
|
226
|
+
extractPartialJsonStringField(parsed, 'output') ??
|
|
227
|
+
extractPartialJsonStringField(parsed, 'message') ??
|
|
228
|
+
'';
|
|
229
|
+
const stderr = extractPartialJsonStringField(parsed, 'stderr') ?? '';
|
|
230
|
+
const rawErr = extractPartialJsonStringField(parsed, 'error');
|
|
231
|
+
const exitCode = extractPartialJsonExitCode(parsed);
|
|
232
|
+
const timedOut = /"timedOut"\s*:\s*true/.test(parsed);
|
|
233
|
+
if (stdout || stderr || rawErr || exitCode !== null || /"_sandbox"|"success"/.test(parsed)) {
|
|
234
|
+
const dedupedError = rawErr && rawErr.trim() !== stderr.trim() && rawErr.trim() !== stdout.trim()
|
|
235
|
+
? rawErr.trim()
|
|
236
|
+
: null;
|
|
237
|
+
return {
|
|
238
|
+
settled: true,
|
|
239
|
+
command: rawCommand,
|
|
240
|
+
language,
|
|
241
|
+
cwd,
|
|
242
|
+
...baseMeta,
|
|
243
|
+
stdout,
|
|
244
|
+
stderr,
|
|
245
|
+
error: dedupedError,
|
|
246
|
+
exitCode,
|
|
247
|
+
timedOut,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
settled: true,
|
|
253
|
+
command: rawCommand,
|
|
254
|
+
language,
|
|
255
|
+
cwd,
|
|
256
|
+
...baseMeta,
|
|
257
|
+
stdout: parsed,
|
|
258
|
+
stderr: '',
|
|
259
|
+
error: null,
|
|
260
|
+
exitCode: null,
|
|
261
|
+
timedOut: false,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
const obj = asObject(parsed);
|
|
265
|
+
if (!obj) {
|
|
266
|
+
return {
|
|
267
|
+
settled: true,
|
|
268
|
+
command: rawCommand,
|
|
269
|
+
language,
|
|
270
|
+
cwd,
|
|
271
|
+
...baseMeta,
|
|
272
|
+
stdout: String(parsed),
|
|
273
|
+
stderr: '',
|
|
274
|
+
error: null,
|
|
275
|
+
exitCode: null,
|
|
276
|
+
timedOut: false,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
const dataObj = asObject(obj.data);
|
|
280
|
+
const exitCode = numericExitCode(dataObj) ?? numericExitCode(obj);
|
|
281
|
+
const stdout = (typeof dataObj?.stdout === 'string' ? dataObj.stdout : null) ??
|
|
282
|
+
(typeof dataObj?.output === 'string' ? dataObj.output : null) ??
|
|
283
|
+
(typeof obj.data === 'string' ? obj.data : null) ??
|
|
284
|
+
(typeof obj.stdout === 'string' ? obj.stdout : null) ??
|
|
285
|
+
(typeof obj.output === 'string' ? obj.output : null) ??
|
|
286
|
+
(obj.success !== false && typeof obj.message === 'string' ? obj.message : null) ??
|
|
287
|
+
'';
|
|
288
|
+
const stderr = (typeof dataObj?.stderr === 'string' ? dataObj.stderr : null) ??
|
|
289
|
+
(typeof obj.stderr === 'string' ? obj.stderr : null) ??
|
|
290
|
+
'';
|
|
291
|
+
const rawError = (obj.success === false && stringField(dataObj, 'message')
|
|
292
|
+
? stringField(dataObj, 'message')
|
|
293
|
+
: null) ??
|
|
294
|
+
stringField(obj, 'error') ??
|
|
295
|
+
stringField(dataObj, 'error');
|
|
296
|
+
const dedupedError = rawError && rawError !== stderr.trim() && rawError !== stdout.trim() ? rawError : null;
|
|
297
|
+
const timedOut = dataObj?.timedOut === true || obj.timedOut === true;
|
|
298
|
+
return {
|
|
299
|
+
settled: true,
|
|
300
|
+
command: rawCommand,
|
|
301
|
+
language,
|
|
302
|
+
cwd,
|
|
303
|
+
...baseMeta,
|
|
304
|
+
stdout,
|
|
305
|
+
stderr,
|
|
306
|
+
error: dedupedError,
|
|
307
|
+
exitCode,
|
|
308
|
+
timedOut,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
/** True when a classified search command (`rg`/`grep`) exited with code 1 and empty stderr (standard "no matches"). */
|
|
312
|
+
export function isBenignSearchNoMatch(classified, terminal) {
|
|
313
|
+
if (!classified || classified.kind !== 'search')
|
|
314
|
+
return false;
|
|
315
|
+
if (!terminal.settled || terminal.exitCode !== 1)
|
|
316
|
+
return false;
|
|
317
|
+
if (terminal.stderr.trim().length > 0)
|
|
318
|
+
return false;
|
|
319
|
+
if (terminal.error && !/exit(?:ed with)? code 1\b/i.test(terminal.error))
|
|
320
|
+
return false;
|
|
321
|
+
return true;
|
|
322
|
+
}
|
|
323
|
+
/** Detect `skill` tool calls or `SKILL.md` file/shell reads. */
|
|
324
|
+
export function extractSkillInvocation(action) {
|
|
325
|
+
const args = asObject(action.arguments);
|
|
326
|
+
if (action.tool === 'skill' || action.tool === 'load_skill' || action.tool === 'use_skill') {
|
|
327
|
+
const name = stringField(args, 'name') ??
|
|
328
|
+
stringField(args, 'skill') ??
|
|
329
|
+
stringField(args, 'skillName') ??
|
|
330
|
+
stringField(args, 'id');
|
|
331
|
+
if (name)
|
|
332
|
+
return { skillName: name, path: stringField(args, 'path') };
|
|
333
|
+
}
|
|
334
|
+
if (action.tool === 'local_read_file' || action.tool === 'read_file') {
|
|
335
|
+
const rawPath = stringField(args, 'path') ?? stringField(args, 'file') ?? stringField(args, 'target');
|
|
336
|
+
if (rawPath) {
|
|
337
|
+
const skillName = extractSkillNameFromPath(rawPath);
|
|
338
|
+
if (skillName)
|
|
339
|
+
return { skillName, path: rawPath };
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
if (action.tool === 'local_exec_shell' || action.tool === 'bash' || action.tool === 'pwsh') {
|
|
343
|
+
const rawCmd = stringField(args, 'command') ?? stringField(args, 'cmd');
|
|
344
|
+
if (rawCmd) {
|
|
345
|
+
const classified = classifyShellCommand(rawCmd);
|
|
346
|
+
if (classified.kind === 'skill') {
|
|
347
|
+
return { skillName: classified.skillName, path: classified.path };
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
function countDiffLines(diff) {
|
|
354
|
+
let addedLines = 0;
|
|
355
|
+
let removedLines = 0;
|
|
356
|
+
for (const line of diff.split(/\r?\n/)) {
|
|
357
|
+
if (line.startsWith('+') && !line.startsWith('+++'))
|
|
358
|
+
addedLines += 1;
|
|
359
|
+
else if (line.startsWith('-') && !line.startsWith('---'))
|
|
360
|
+
removedLines += 1;
|
|
361
|
+
}
|
|
362
|
+
return { addedLines, removedLines };
|
|
363
|
+
}
|
|
364
|
+
function pickEditString(record, keys) {
|
|
365
|
+
for (const key of keys) {
|
|
366
|
+
if (typeof record[key] === 'string')
|
|
367
|
+
return record[key];
|
|
368
|
+
}
|
|
369
|
+
return null;
|
|
370
|
+
}
|
|
371
|
+
const OLD_EDIT_KEYS = ['oldText', 'old_text', 'old_string'];
|
|
372
|
+
const NEW_EDIT_KEYS = ['newText', 'new_text', 'new_string'];
|
|
373
|
+
function collectRawEdits(args) {
|
|
374
|
+
if (!args)
|
|
375
|
+
return [];
|
|
376
|
+
const out = [];
|
|
377
|
+
if (Array.isArray(args.edits)) {
|
|
378
|
+
for (const item of args.edits) {
|
|
379
|
+
const row = asObject(item);
|
|
380
|
+
if (!row)
|
|
381
|
+
continue;
|
|
382
|
+
const oldText = pickEditString(row, OLD_EDIT_KEYS);
|
|
383
|
+
const newText = pickEditString(row, NEW_EDIT_KEYS);
|
|
384
|
+
if (oldText !== null && newText !== null) {
|
|
385
|
+
out.push({ oldText, newText });
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
if (out.length === 0) {
|
|
390
|
+
const oldText = pickEditString(args, OLD_EDIT_KEYS);
|
|
391
|
+
const newText = pickEditString(args, NEW_EDIT_KEYS);
|
|
392
|
+
if (oldText !== null && newText !== null) {
|
|
393
|
+
out.push({ oldText, newText });
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return out;
|
|
397
|
+
}
|
|
398
|
+
function splitLogicalLines(text) {
|
|
399
|
+
const normalized = text.replace(/\r\n/g, '\n');
|
|
400
|
+
if (normalized.length === 0)
|
|
401
|
+
return [];
|
|
402
|
+
const lines = normalized.split('\n');
|
|
403
|
+
if (lines[lines.length - 1] === '')
|
|
404
|
+
lines.pop();
|
|
405
|
+
return lines;
|
|
406
|
+
}
|
|
407
|
+
function synthesizeDiffFromEdits(path, edits) {
|
|
408
|
+
if (edits.length === 0)
|
|
409
|
+
return null;
|
|
410
|
+
const hunks = [`--- a/${path}`, `+++ b/${path}`];
|
|
411
|
+
let addedLines = 0;
|
|
412
|
+
let removedLines = 0;
|
|
413
|
+
for (const edit of edits) {
|
|
414
|
+
const oldLines = splitLogicalLines(edit.oldText);
|
|
415
|
+
const newLines = splitLogicalLines(edit.newText);
|
|
416
|
+
let prefix = 0;
|
|
417
|
+
while (prefix < oldLines.length &&
|
|
418
|
+
prefix < newLines.length &&
|
|
419
|
+
oldLines[prefix] === newLines[prefix]) {
|
|
420
|
+
prefix += 1;
|
|
421
|
+
}
|
|
422
|
+
let suffix = 0;
|
|
423
|
+
while (suffix < oldLines.length - prefix &&
|
|
424
|
+
suffix < newLines.length - prefix &&
|
|
425
|
+
oldLines[oldLines.length - 1 - suffix] === newLines[newLines.length - 1 - suffix]) {
|
|
426
|
+
suffix += 1;
|
|
427
|
+
}
|
|
428
|
+
const removedSlice = oldLines.slice(prefix, oldLines.length - suffix);
|
|
429
|
+
const addedSlice = newLines.slice(prefix, newLines.length - suffix);
|
|
430
|
+
const contextBefore = oldLines.slice(Math.max(0, prefix - 2), prefix);
|
|
431
|
+
const contextAfter = oldLines.slice(oldLines.length - suffix, Math.min(oldLines.length, oldLines.length - suffix + 2));
|
|
432
|
+
const oldCount = contextBefore.length + removedSlice.length + contextAfter.length;
|
|
433
|
+
const newCount = contextBefore.length + addedSlice.length + contextAfter.length;
|
|
434
|
+
hunks.push(`@@ -1,${oldCount} +1,${newCount} @@`);
|
|
435
|
+
for (const line of contextBefore)
|
|
436
|
+
hunks.push(` ${line}`);
|
|
437
|
+
for (const line of removedSlice) {
|
|
438
|
+
hunks.push(`-${line}`);
|
|
439
|
+
removedLines += 1;
|
|
440
|
+
}
|
|
441
|
+
for (const line of addedSlice) {
|
|
442
|
+
hunks.push(`+${line}`);
|
|
443
|
+
addedLines += 1;
|
|
444
|
+
}
|
|
445
|
+
for (const line of contextAfter)
|
|
446
|
+
hunks.push(` ${line}`);
|
|
447
|
+
}
|
|
448
|
+
return { diff: hunks.join('\n'), addedLines, removedLines };
|
|
449
|
+
}
|
|
450
|
+
function extractResultDiff(result) {
|
|
451
|
+
const parsed = parseJsonValue(result);
|
|
452
|
+
const obj = asObject(parsed);
|
|
453
|
+
if (obj) {
|
|
454
|
+
const data = asObject(obj.data);
|
|
455
|
+
const rawDiff = (typeof data?.diff === 'string' && data.diff.length > 0 ? data.diff : null) ??
|
|
456
|
+
(typeof obj.diff === 'string' && obj.diff.length > 0 ? obj.diff : null);
|
|
457
|
+
if (rawDiff) {
|
|
458
|
+
return { diff: rawDiff.replace(/\u2026$/, ''), complete: !rawDiff.endsWith('…') };
|
|
459
|
+
}
|
|
460
|
+
return null;
|
|
461
|
+
}
|
|
462
|
+
if (typeof parsed === 'string' && parsed.trimStart().startsWith('{')) {
|
|
463
|
+
const partial = extractPartialJsonStringFieldWithState(parsed, 'diff');
|
|
464
|
+
if (partial && partial.value.length > 0) {
|
|
465
|
+
const cleaned = partial.value.replace(/\u2026$/, '');
|
|
466
|
+
return {
|
|
467
|
+
diff: cleaned,
|
|
468
|
+
complete: partial.complete && !partial.value.endsWith('…'),
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return null;
|
|
473
|
+
}
|
|
474
|
+
const FILE_CHANGE_TOOLS = new Set([
|
|
475
|
+
'local_edit_file',
|
|
476
|
+
'edit_file',
|
|
477
|
+
'apply_patch',
|
|
478
|
+
'local_write_file',
|
|
479
|
+
'write_file',
|
|
480
|
+
]);
|
|
481
|
+
/**
|
|
482
|
+
* Extract unified diff + `+added / -removed` counts for file mutation tools.
|
|
483
|
+
* Recovers full unified diffs from `arguments.edits` when persisted `result`
|
|
484
|
+
* strings were truncated to 300 characters.
|
|
485
|
+
*/
|
|
486
|
+
export function extractFileChangePreview(action) {
|
|
487
|
+
const args = asObject(action.arguments);
|
|
488
|
+
const resultDiff = extractResultDiff(action.result);
|
|
489
|
+
if (!FILE_CHANGE_TOOLS.has(action.tool) && !resultDiff)
|
|
490
|
+
return null;
|
|
491
|
+
const isWrite = action.tool === 'local_write_file' || action.tool === 'write_file';
|
|
492
|
+
const invalidMeta = asObject(args?._steerable_invalid_arguments);
|
|
493
|
+
const rawPatch = stringField(args, 'patch') ??
|
|
494
|
+
stringField(args, 'diff') ??
|
|
495
|
+
(typeof action.arguments === 'string' ? action.arguments : null);
|
|
496
|
+
const patchHeaderPath = rawPatch
|
|
497
|
+
? /^\+\+\+\s+(?:b\/)?([^\r\n]+)/m.exec(rawPatch)?.[1]?.trim() ??
|
|
498
|
+
/^\*\*\* (?:Update|Add|Delete) File:\s*([^\r\n]+)/m.exec(rawPatch)?.[1]?.trim() ??
|
|
499
|
+
null
|
|
500
|
+
: null;
|
|
501
|
+
const path = stringField(args, 'path') ??
|
|
502
|
+
stringField(args, 'file') ??
|
|
503
|
+
stringField(args, 'target') ??
|
|
504
|
+
stringField(invalidMeta, 'targetHint') ??
|
|
505
|
+
patchHeaderPath ??
|
|
506
|
+
'file';
|
|
507
|
+
const envelope = action.result !== undefined && action.result !== null
|
|
508
|
+
? parseToolEnvelope(action.result)
|
|
509
|
+
: null;
|
|
510
|
+
const failureError = envelope && (envelope.success === false || Boolean(envelope.error))
|
|
511
|
+
? envelope.message && envelope.error && !envelope.message.startsWith(envelope.error)
|
|
512
|
+
? `${envelope.error}: ${envelope.message}`
|
|
513
|
+
: (envelope.message ?? envelope.error)
|
|
514
|
+
: null;
|
|
515
|
+
const errorMeta = failureError ? { error: failureError } : {};
|
|
516
|
+
if (resultDiff && resultDiff.complete) {
|
|
517
|
+
const { addedLines, removedLines } = countDiffLines(resultDiff.diff);
|
|
518
|
+
return {
|
|
519
|
+
path,
|
|
520
|
+
isWrite,
|
|
521
|
+
addedLines,
|
|
522
|
+
removedLines,
|
|
523
|
+
totalLines: null,
|
|
524
|
+
diff: resultDiff.diff,
|
|
525
|
+
contentPreview: null,
|
|
526
|
+
...errorMeta,
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
const edits = collectRawEdits(args);
|
|
530
|
+
const synthesized = synthesizeDiffFromEdits(path, edits);
|
|
531
|
+
if (synthesized) {
|
|
532
|
+
return {
|
|
533
|
+
path,
|
|
534
|
+
isWrite,
|
|
535
|
+
addedLines: synthesized.addedLines,
|
|
536
|
+
removedLines: synthesized.removedLines,
|
|
537
|
+
totalLines: null,
|
|
538
|
+
diff: synthesized.diff,
|
|
539
|
+
contentPreview: null,
|
|
540
|
+
...errorMeta,
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
if (rawPatch && (action.tool === 'apply_patch' || rawPatch.includes('@@') || rawPatch.includes('*** '))) {
|
|
544
|
+
const { addedLines, removedLines } = countDiffLines(rawPatch);
|
|
545
|
+
return {
|
|
546
|
+
path,
|
|
547
|
+
isWrite,
|
|
548
|
+
addedLines,
|
|
549
|
+
removedLines,
|
|
550
|
+
totalLines: null,
|
|
551
|
+
diff: rawPatch,
|
|
552
|
+
contentPreview: null,
|
|
553
|
+
...errorMeta,
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
if (isWrite) {
|
|
557
|
+
const content = typeof args?.content === 'string' ? args.content : null;
|
|
558
|
+
const lines = content !== null ? splitLogicalLines(content) : null;
|
|
559
|
+
return {
|
|
560
|
+
path,
|
|
561
|
+
isWrite: true,
|
|
562
|
+
addedLines: lines ? lines.length : 0,
|
|
563
|
+
removedLines: 0,
|
|
564
|
+
totalLines: lines ? lines.length : null,
|
|
565
|
+
diff: resultDiff?.diff ?? null,
|
|
566
|
+
contentPreview: content,
|
|
567
|
+
...errorMeta,
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
if (resultDiff) {
|
|
571
|
+
const { addedLines, removedLines } = countDiffLines(resultDiff.diff);
|
|
572
|
+
return {
|
|
573
|
+
path,
|
|
574
|
+
isWrite,
|
|
575
|
+
addedLines,
|
|
576
|
+
removedLines,
|
|
577
|
+
totalLines: null,
|
|
578
|
+
diff: resultDiff.diff,
|
|
579
|
+
contentPreview: null,
|
|
580
|
+
...errorMeta,
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
if (FILE_CHANGE_TOOLS.has(action.tool) && (path !== 'file' || failureError !== null)) {
|
|
584
|
+
return {
|
|
585
|
+
path,
|
|
586
|
+
isWrite,
|
|
587
|
+
addedLines: 0,
|
|
588
|
+
removedLines: 0,
|
|
589
|
+
totalLines: null,
|
|
590
|
+
diff: null,
|
|
591
|
+
contentPreview: null,
|
|
592
|
+
...errorMeta,
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
return null;
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Keep head + tail lines of multi-line command output so both the command header
|
|
599
|
+
* and trailing error/summary stay visible on screen without scrolling.
|
|
600
|
+
*/
|
|
601
|
+
export function truncateOutputMiddle(text, options) {
|
|
602
|
+
const headLines = options?.headLines ?? 5;
|
|
603
|
+
const tailLines = options?.tailLines ?? 5;
|
|
604
|
+
const minTotalLines = options?.minTotalLines ?? 12;
|
|
605
|
+
const normalized = text.replace(/\r\n/g, '\n').replace(/\n$/, '');
|
|
606
|
+
if (!normalized) {
|
|
607
|
+
return { truncated: false, totalLines: 0, head: '', tail: '', omittedLines: 0 };
|
|
608
|
+
}
|
|
609
|
+
const lines = normalized.split('\n');
|
|
610
|
+
if (lines.length < minTotalLines || lines.length <= headLines + tailLines) {
|
|
611
|
+
return {
|
|
612
|
+
truncated: false,
|
|
613
|
+
totalLines: lines.length,
|
|
614
|
+
head: normalized,
|
|
615
|
+
tail: '',
|
|
616
|
+
omittedLines: 0,
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
return {
|
|
620
|
+
truncated: true,
|
|
621
|
+
totalLines: lines.length,
|
|
622
|
+
head: lines.slice(0, headLines).join('\n'),
|
|
623
|
+
tail: lines.slice(-tailLines).join('\n'),
|
|
624
|
+
omittedLines: lines.length - headLines - tailLines,
|
|
625
|
+
};
|
|
626
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@steerable/agent-protocol",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.4",
|
|
4
4
|
"description": "Steerable framework Tier 1 — wire-format types (SSEEvent, ChatMessage, ToolCall, ToolResult, AgentSession, HarnessTrace, sidecar JSON-RPC) shared by every Steerable client and server.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://steerableframework.com/",
|