@stigmer/client 0.1.3 → 0.3.0
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/README.md +10 -0
- package/app-handover.d.ts +186 -0
- package/app-handover.d.ts.map +1 -0
- package/app-handover.js +630 -0
- package/app-handover.js.map +1 -0
- package/gen/agent.d.ts +2 -1
- package/gen/agent.d.ts.map +1 -1
- package/gen/agent.js +2 -0
- package/gen/agent.js.map +1 -1
- package/gen/organization.d.ts +2 -0
- package/gen/organization.d.ts.map +1 -1
- package/gen/organization.js +8 -0
- package/gen/organization.js.map +1 -1
- package/gen/run.d.ts +7 -1
- package/gen/run.d.ts.map +1 -1
- package/gen/run.js +23 -1
- package/gen/run.js.map +1 -1
- package/iam-principal.d.ts +11 -0
- package/iam-principal.d.ts.map +1 -1
- package/iam-principal.js +16 -0
- package/iam-principal.js.map +1 -1
- package/index.d.ts +11 -5
- package/index.d.ts.map +1 -1
- package/index.js +10 -3
- package/index.js.map +1 -1
- package/node.d.ts.map +1 -1
- package/node.js +12 -0
- package/node.js.map +1 -1
- package/package.json +9 -4
- package/platform.d.ts +41 -3
- package/platform.d.ts.map +1 -1
- package/platform.js +56 -3
- package/platform.js.map +1 -1
- package/run/format-elapsed.d.ts +10 -0
- package/run/format-elapsed.d.ts.map +1 -0
- package/run/format-elapsed.js +25 -0
- package/run/format-elapsed.js.map +1 -0
- package/run/step-words.d.ts +78 -0
- package/run/step-words.d.ts.map +1 -0
- package/run/step-words.js +316 -0
- package/run/step-words.js.map +1 -0
- package/run/tool-view.d.ts +49 -2
- package/run/tool-view.d.ts.map +1 -1
- package/run/tool-view.js +113 -17
- package/run/tool-view.js.map +1 -1
- package/run/turn-activity.d.ts +9 -7
- package/run/turn-activity.d.ts.map +1 -1
- package/run/turn-activity.js +24 -144
- package/run/turn-activity.js.map +1 -1
- package/run/turn-ending.d.ts +59 -0
- package/run/turn-ending.d.ts.map +1 -0
- package/run/turn-ending.js +405 -0
- package/run/turn-ending.js.map +1 -0
- package/run/turn-steps.d.ts +16 -0
- package/run/turn-steps.d.ts.map +1 -0
- package/run/turn-steps.js +65 -0
- package/run/turn-steps.js.map +1 -0
- package/session-events.d.ts +3 -1
- package/session-events.d.ts.map +1 -1
- package/session-events.js +1 -0
- package/session-events.js.map +1 -1
- package/src/app-handover.ts +777 -0
- package/src/gen/agent.ts +4 -1
- package/src/gen/organization.ts +7 -0
- package/src/gen/run.ts +21 -1
- package/src/iam-principal.ts +21 -0
- package/src/index.ts +55 -1
- package/src/node.ts +12 -0
- package/src/platform.ts +80 -3
- package/src/run/format-elapsed.ts +22 -0
- package/src/run/step-words.ts +360 -0
- package/src/run/tool-view.ts +133 -19
- package/src/run/turn-activity.ts +27 -146
- package/src/run/turn-ending.ts +487 -0
- package/src/run/turn-steps.ts +81 -0
- package/src/session-events.ts +4 -1
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
// The words a tool call reads as: one table of every kind's verbs, in the
|
|
2
|
+
// present (while it runs), the past (once it returned) and the failed form,
|
|
3
|
+
// beside the noun a settled stretch counts it by. The working turn's phrase
|
|
4
|
+
// (`activityPhrase`), the settled stretch's roll-up (`settledStepsPhrase`)
|
|
5
|
+
// and each step's own line (`stepPhrase`) all read this table, so the three
|
|
6
|
+
// cannot describe one call three ways.
|
|
7
|
+
//
|
|
8
|
+
// A step's line reads only what the call carries: its kind
|
|
9
|
+
// (`resolveToolKind`), its input (a file's path, a search's pattern and
|
|
10
|
+
// folder, a page's address, the agent's own description of a command) and
|
|
11
|
+
// whether it returned, failed, or is still open. A command that exited
|
|
12
|
+
// non-zero failed even when the engine did not mark it an error.
|
|
13
|
+
//
|
|
14
|
+
// Trade-offs:
|
|
15
|
+
// - A connected app's tool (MCP) and a tool the kit does not know read as
|
|
16
|
+
// the tool's own name, humanized ("Create issue"), with no tense: a past
|
|
17
|
+
// tense guessed from an arbitrary name ("Get status", "List merge
|
|
18
|
+
// requests") would misread, and the name is exact.
|
|
19
|
+
// - A search for files by name (glob) reads "Found files matching", told
|
|
20
|
+
// apart from a search in files' text by the tool's name, as the kind
|
|
21
|
+
// table itself is.
|
|
22
|
+
// - Targets are cut short (`TARGET_MAX`) so a line stays one line.
|
|
23
|
+
|
|
24
|
+
import type { ToolUse } from "@stigmer/session-log";
|
|
25
|
+
import { normalizeToolResult, resolveToolKind, type FileLineRange, type ToolKind, type ToolResultView } from "./tool-view.js";
|
|
26
|
+
|
|
27
|
+
/** How a step stands: running, returned, failed, or stopped before it returned (its turn ended). */
|
|
28
|
+
export type StepTense = "present" | "past" | "failed" | "stopped";
|
|
29
|
+
|
|
30
|
+
/** A step's words: the line, how it stands, and what it names beside the line. */
|
|
31
|
+
export interface StepPhrase {
|
|
32
|
+
/** The line: "Edited invoice.ts", "Searched for round( in src/billing", "Couldn't read notes.md". */
|
|
33
|
+
readonly text: string;
|
|
34
|
+
readonly tense: StepTense;
|
|
35
|
+
/** The folder of the file the line names, or where a search at work looks ("src/billing"); "" when it names none or the file sits at the root. */
|
|
36
|
+
readonly folder: string;
|
|
37
|
+
/** The connected app an MCP call belongs to; "" for a built-in tool. */
|
|
38
|
+
readonly app: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** One form of a kind's words: with a target ("Reading" + " invoice.ts") and without one ("Reading a file"). */
|
|
42
|
+
type Form = readonly [withTarget: string, alone: string];
|
|
43
|
+
|
|
44
|
+
/** Every kind's words in each tense, and the past verb and noun a settled stretch counts it by. */
|
|
45
|
+
interface KindWords {
|
|
46
|
+
readonly present: Form;
|
|
47
|
+
readonly past: Form;
|
|
48
|
+
readonly failed: Form;
|
|
49
|
+
/** The roll-up's verb and counted noun; undefined for a kind the roll-up leaves out. */
|
|
50
|
+
readonly counted?: readonly [verb: string, noun: string];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The words of every kind (module header). */
|
|
54
|
+
export const KIND_WORDS: Readonly<Record<ToolKind, KindWords>> = {
|
|
55
|
+
file_read: { present: ["Reading", "Reading a file"], past: ["Read", "Read a file"], failed: ["Couldn't read", "Couldn't read a file"], counted: ["read", "file"] },
|
|
56
|
+
file_write: { present: ["Writing", "Writing a file"], past: ["Wrote", "Wrote a file"], failed: ["Couldn't write", "Couldn't write a file"], counted: ["wrote", "file"] },
|
|
57
|
+
file_edit: { present: ["Editing", "Editing a file"], past: ["Edited", "Edited a file"], failed: ["Couldn't edit", "Couldn't edit a file"], counted: ["edited", "file"] },
|
|
58
|
+
file_delete: {
|
|
59
|
+
present: ["Deleting", "Deleting a file"],
|
|
60
|
+
past: ["Deleted", "Deleted a file"],
|
|
61
|
+
failed: ["Couldn't delete", "Couldn't delete a file"],
|
|
62
|
+
counted: ["deleted", "file"],
|
|
63
|
+
},
|
|
64
|
+
shell: { present: ["Running a command", "Running a command"], past: ["Ran a command", "Ran a command"], failed: ["Command failed", "Command failed"], counted: ["ran", "command"] },
|
|
65
|
+
search: { present: ["Searching for", "Searching"], past: ["Searched for", "Searched"], failed: ["Couldn't search for", "Couldn't search"], counted: ["searched", "pattern"] },
|
|
66
|
+
list: {
|
|
67
|
+
present: ["Looking in", "Looking through files"],
|
|
68
|
+
past: ["Looked in", "Looked through files"],
|
|
69
|
+
failed: ["Couldn't look in", "Couldn't look through files"],
|
|
70
|
+
counted: ["looked through", "folder"],
|
|
71
|
+
},
|
|
72
|
+
fetch: { present: ["Reading", "Reading a page"], past: ["Read", "Read a page"], failed: ["Couldn't read", "Couldn't read a page"], counted: ["read", "page"] },
|
|
73
|
+
web_search: {
|
|
74
|
+
present: ["Searching the web for", "Searching the web"],
|
|
75
|
+
past: ["Searched the web for", "Searched the web"],
|
|
76
|
+
failed: ["Couldn't search the web for", "Couldn't search the web"],
|
|
77
|
+
counted: ["searched the web", "time"],
|
|
78
|
+
},
|
|
79
|
+
think: { present: ["Thinking", "Thinking"], past: ["Thought it through", "Thought it through"], failed: ["Thought it through", "Thought it through"] },
|
|
80
|
+
todo: { present: ["Planning", "Planning"], past: ["Updated the plan", "Updated the plan"], failed: ["Couldn't update the plan", "Couldn't update the plan"] },
|
|
81
|
+
subagent: {
|
|
82
|
+
present: ["A helper is working on", "A helper is working"],
|
|
83
|
+
past: ["A helper worked on", "A helper worked"],
|
|
84
|
+
failed: ["A helper couldn't finish", "A helper couldn't finish"],
|
|
85
|
+
},
|
|
86
|
+
mcp: { present: ["Using", "Using a tool"], past: ["Used", "Used a tool"], failed: ["Couldn't use", "Couldn't use a tool"], counted: ["used", "tool"] },
|
|
87
|
+
memory: { present: ["Remembering", "Remembering"], past: ["Remembered", "Remembered"], failed: ["Couldn't remember", "Couldn't remember"] },
|
|
88
|
+
background_stop: {
|
|
89
|
+
present: ["Stopping a command", "Stopping a command"],
|
|
90
|
+
past: ["Stopped a command", "Stopped a command"],
|
|
91
|
+
failed: ["Couldn't stop a command", "Couldn't stop a command"],
|
|
92
|
+
},
|
|
93
|
+
skill: {
|
|
94
|
+
present: ["Using the skill", "Using a skill"],
|
|
95
|
+
past: ["Used the skill", "Used a skill"],
|
|
96
|
+
failed: ["Couldn't use the skill", "Couldn't use a skill"],
|
|
97
|
+
counted: ["used", "skill"],
|
|
98
|
+
},
|
|
99
|
+
unknown: { present: ["Working", "Working"], past: ["Used a tool", "Used a tool"], failed: ["A tool failed", "A tool failed"], counted: ["used", "tool"] },
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/** `form` with `target` after its verb, or alone when there is none. */
|
|
103
|
+
export function phraseOf(form: Form, target: string): string {
|
|
104
|
+
return target === "" ? form[1] : `${form[0]} ${target}`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The longest target a phrase carries before it is cut. */
|
|
108
|
+
const TARGET_MAX = 48;
|
|
109
|
+
|
|
110
|
+
/** Names that search for files by name, not in their text. */
|
|
111
|
+
const FILE_NAME_SEARCHES: ReadonlySet<string> = new Set(["glob", "Glob", "find_files"]);
|
|
112
|
+
|
|
113
|
+
/** The input's text at `key`, or "". */
|
|
114
|
+
function textAt(tool: Pick<ToolUse, "input">, key: string): string {
|
|
115
|
+
const value = tool.input[key];
|
|
116
|
+
return typeof value === "string" ? value : "";
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The path a file call names, from the fields the engines use. */
|
|
120
|
+
function pathOf(tool: Pick<ToolUse, "input">): string {
|
|
121
|
+
return textAt(tool, "path") || textAt(tool, "file_path") || textAt(tool, "notebook_path");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** What a call acts on, in a few characters: a file's name, a search's pattern, a page's host, a subagent's task. */
|
|
125
|
+
export function targetOf(tool: Pick<ToolUse, "name" | "mcpServerName" | "input">): string {
|
|
126
|
+
const kind = resolveToolKind(tool);
|
|
127
|
+
switch (kind) {
|
|
128
|
+
case "file_read":
|
|
129
|
+
case "file_write":
|
|
130
|
+
case "file_edit":
|
|
131
|
+
case "file_delete":
|
|
132
|
+
return short(baseName(pathOf(tool)));
|
|
133
|
+
case "search":
|
|
134
|
+
return short(textAt(tool, "pattern") || textAt(tool, "query"));
|
|
135
|
+
case "fetch":
|
|
136
|
+
return short(hostOf(textAt(tool, "url")));
|
|
137
|
+
case "web_search":
|
|
138
|
+
return short(textAt(tool, "query"));
|
|
139
|
+
case "subagent":
|
|
140
|
+
return short(textAt(tool, "description"));
|
|
141
|
+
case "mcp":
|
|
142
|
+
return short(tool.name);
|
|
143
|
+
case "skill":
|
|
144
|
+
return short(textAt(tool, "skill") || textAt(tool, "name"));
|
|
145
|
+
case "list":
|
|
146
|
+
// The folder's last two segments ("src/billing"): its last alone
|
|
147
|
+
// ("billing") leaves a reader guessing which, and a whole absolute
|
|
148
|
+
// path crowds the line.
|
|
149
|
+
return short(folderWords(textAt(tool, "path")));
|
|
150
|
+
case "shell":
|
|
151
|
+
case "think":
|
|
152
|
+
case "todo":
|
|
153
|
+
case "memory":
|
|
154
|
+
case "background_stop":
|
|
155
|
+
case "unknown":
|
|
156
|
+
return "";
|
|
157
|
+
/* v8 ignore next -- @preserve: the exhaustiveness guard over ToolKind; resolveToolKind answers only the arms above */
|
|
158
|
+
default: {
|
|
159
|
+
const never: never = kind;
|
|
160
|
+
return never;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* How `tool` stands: open, returned, failed (an error result, a denial, or a
|
|
167
|
+
* command that exited non-zero), or stopped before it returned. A background
|
|
168
|
+
* command's own end is its status line's to say, never the step's.
|
|
169
|
+
*/
|
|
170
|
+
export function stepTense(tool: ToolUse): StepTense {
|
|
171
|
+
if (tool.state === "failed" || tool.state === "denied") return "failed";
|
|
172
|
+
// Waiting: worded as what it would do (stepPhrase), standing as open.
|
|
173
|
+
if (tool.state === "waiting") return "present";
|
|
174
|
+
if (tool.state === "stopped") return "stopped";
|
|
175
|
+
if (tool.result === undefined) return "present";
|
|
176
|
+
// An edit the Stigmer harness refused answers as a result, not an error
|
|
177
|
+
// ("Error: String not found in file"): nothing changed, so it failed.
|
|
178
|
+
if (resolveToolKind(tool) === "file_edit" && refusedEdit(tool.result.text)) return "failed";
|
|
179
|
+
if (resolveToolKind(tool) === "shell" && tool.background === undefined) {
|
|
180
|
+
const view = normalizeToolResult(tool);
|
|
181
|
+
if (view.type === "terminal" && view.exitCode !== undefined && view.exitCode !== 0) return "failed";
|
|
182
|
+
}
|
|
183
|
+
return "past";
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* `tool`'s line, in the tense it stands in (module header). A call that
|
|
188
|
+
* waits for a person's yes has done nothing yet: it reads as what the agent
|
|
189
|
+
* wants to do ("Wants to write rounding.ts").
|
|
190
|
+
*/
|
|
191
|
+
export function stepPhrase(tool: ToolUse, options: StepPhraseOptions = {}): StepPhrase {
|
|
192
|
+
const phrase = stepPhraseByTense(tool);
|
|
193
|
+
// A write that made the file reads as what it did ("Created rounding.test.ts").
|
|
194
|
+
if (options.created === true && phrase.tense === "past" && resolveToolKind(tool) === "file_write") {
|
|
195
|
+
const wrote = KIND_WORDS.file_write.past;
|
|
196
|
+
const created = phrase.text === wrote[1] ? CREATED[1] : phrase.text.startsWith(`${wrote[0]} `) ? `${CREATED[0]}${phrase.text.slice(wrote[0].length)}` : phrase.text;
|
|
197
|
+
return { ...phrase, text: created };
|
|
198
|
+
}
|
|
199
|
+
if (tool.state !== "waiting" || !phrase.text.startsWith("Couldn't ")) return phrase;
|
|
200
|
+
return { ...phrase, text: `Wants to ${phrase.text.slice("Couldn't ".length)}` };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** What a step's line may know beyond the call itself. */
|
|
204
|
+
export interface StepPhraseOptions {
|
|
205
|
+
/**
|
|
206
|
+
* The file a write wrote did not exist before its turn: the turn's file
|
|
207
|
+
* review says so (its change is `added`). Nothing in the call says it, so
|
|
208
|
+
* a session with no review reads "Wrote".
|
|
209
|
+
*/
|
|
210
|
+
readonly created?: boolean;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** A write that made its file, with a target and without one. */
|
|
214
|
+
const CREATED: Form = ["Created", "Created a file"];
|
|
215
|
+
|
|
216
|
+
// The first lines the engines answer a refused edit with: the Stigmer
|
|
217
|
+
// harness answers it as a result in both its backends' words, which the
|
|
218
|
+
// conformance suite's classifier keys the same way
|
|
219
|
+
// (test/conformance/src/benchmark/tool-call-facts.ts).
|
|
220
|
+
const EDIT_NOT_FOUND = /^(?:Error: )?String not found in file/;
|
|
221
|
+
const EDIT_NOT_UNIQUE = /has multiple occurrences|^(?:Error: )?Multiple occurrences found in/;
|
|
222
|
+
const NO_SUCH_FILE = /no such file|ENOENT|file not found|does not exist/i;
|
|
223
|
+
|
|
224
|
+
/** The first line of an answer that reads, trimmed; "" when it has none. */
|
|
225
|
+
function firstLine(text: string): string {
|
|
226
|
+
return (text.split("\n").find((line) => line.trim() !== "") ?? "").trim();
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Whether an edit's answer says it changed nothing. */
|
|
230
|
+
function refusedEdit(text: string): boolean {
|
|
231
|
+
const first = firstLine(text);
|
|
232
|
+
return EDIT_NOT_FOUND.test(first) || EDIT_NOT_UNIQUE.test(first);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Why a step failed, as a sentence a person reads, from its answer. Only the
|
|
237
|
+
* answer's first line is the engine's reason: the runner appends recovery
|
|
238
|
+
* hints for the model after it (`enrichErrorMessage`), which are never shown.
|
|
239
|
+
* A reason the engines are known to give reads in plain words ("The text to
|
|
240
|
+
* replace wasn't in the file, so nothing changed."); any other is the
|
|
241
|
+
* engine's own line, without an "Error:" label, as a sentence.
|
|
242
|
+
*/
|
|
243
|
+
export function failureWords(text: string): string {
|
|
244
|
+
const first = firstLine(text);
|
|
245
|
+
if (EDIT_NOT_FOUND.test(first)) return "The text to replace wasn't in the file, so nothing changed.";
|
|
246
|
+
if (EDIT_NOT_UNIQUE.test(first)) return "The text to replace was in the file more than once, so nothing changed.";
|
|
247
|
+
if (NO_SUCH_FILE.test(first)) return "The file wasn't there.";
|
|
248
|
+
const reason = first.replace(/^error:\s*/i, "");
|
|
249
|
+
return reason === "" ? "" : reason.charAt(0).toUpperCase() + reason.slice(1);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* How many a search found, as its step says it: "4 files" for a search by
|
|
254
|
+
* name, "2 matches", and "2 matches in 2 files" once its matches name more
|
|
255
|
+
* than one file. Other views count nothing ("").
|
|
256
|
+
*/
|
|
257
|
+
export function searchCountWords(view: ToolResultView): string {
|
|
258
|
+
if (view.type !== "search") return "";
|
|
259
|
+
if (view.kind === "files") return `${view.count} ${view.count === 1 ? "file" : "files"}`;
|
|
260
|
+
const matches = `${view.count} ${view.count === 1 ? "match" : "matches"}`;
|
|
261
|
+
const files = new Set(view.matches.flatMap((match) => (match.file === undefined ? [] : [match.file]))).size;
|
|
262
|
+
return files > 1 ? `${matches} in ${files} files` : matches;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** The lines a step shows of a file, as words: "Lines 14–17 of 88", or "Lines 14–17" when the length is not known. */
|
|
266
|
+
export function lineRangeWords(range: FileLineRange): string {
|
|
267
|
+
const span = range.start === range.end ? `Line ${range.start}` : `Lines ${range.start}–${range.end}`;
|
|
268
|
+
return range.total === undefined ? span : `${span} of ${range.total}`;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function stepPhraseByTense(tool: ToolUse): StepPhrase {
|
|
272
|
+
const tense = stepTense(tool);
|
|
273
|
+
// A step its turn stopped did not get done: it reads as one that couldn't.
|
|
274
|
+
// A waiting one takes the same form, which stepPhrase turns into a want.
|
|
275
|
+
const form: Exclude<StepTense, "stopped"> = tense === "stopped" || tool.state === "waiting" ? "failed" : tense;
|
|
276
|
+
const kind = resolveToolKind(tool);
|
|
277
|
+
const app = tool.mcpServerName;
|
|
278
|
+
if (kind === "mcp" || kind === "unknown") return { text: humanizeToolName(tool.name), tense, folder: "", app };
|
|
279
|
+
if (kind === "shell") {
|
|
280
|
+
const description = textAt(tool, "description").replace(/\s+/g, " ").trim();
|
|
281
|
+
const words = tool.state === "waiting" ? "Wants to run a command" : phraseOf(KIND_WORDS.shell[form], "");
|
|
282
|
+
return { text: description === "" ? words : short(description), tense, folder: "", app };
|
|
283
|
+
}
|
|
284
|
+
if (kind === "search" && FILE_NAME_SEARCHES.has(tool.name)) {
|
|
285
|
+
const pattern = short(textAt(tool, "pattern"));
|
|
286
|
+
const words: Readonly<Record<Exclude<StepTense, "stopped">, Form>> = {
|
|
287
|
+
present: ["Finding files matching", "Finding files"],
|
|
288
|
+
past: ["Found files matching", "Found files"],
|
|
289
|
+
failed: ["Couldn't find files matching", "Couldn't find files"],
|
|
290
|
+
};
|
|
291
|
+
return { text: phraseOf(words[form], pattern), tense, folder: "", app };
|
|
292
|
+
}
|
|
293
|
+
const target = targetOf(tool);
|
|
294
|
+
const text = phraseOf(KIND_WORDS[kind][form], target);
|
|
295
|
+
if (kind === "search") {
|
|
296
|
+
const where = target !== "" ? short(trimSeparators(textAt(tool, "path"))) : "";
|
|
297
|
+
// At work, a search has no counts yet: where it looks is its second line,
|
|
298
|
+
// as a file step's folder is ("Searching for round(", then "src/billing").
|
|
299
|
+
// Done, its counts take the second line and the folder joins its words.
|
|
300
|
+
if (form === "present") return { text, tense, folder: where, app };
|
|
301
|
+
return { text: where !== "" ? `${text} in ${where}` : text, tense, folder: "", app };
|
|
302
|
+
}
|
|
303
|
+
const isFile = kind === "file_read" || kind === "file_write" || kind === "file_edit" || kind === "file_delete";
|
|
304
|
+
return { text, tense, folder: isFile && target !== "" ? short(folderOf(pathOf(tool))) : "", app };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* A tool's name as words, in sentence case: `create_issue`, `createIssue`
|
|
309
|
+
* and `Create-Issue` all read "Create issue". A name with nothing to read
|
|
310
|
+
* reads "A tool".
|
|
311
|
+
*/
|
|
312
|
+
export function humanizeToolName(name: string): string {
|
|
313
|
+
const words = name
|
|
314
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
315
|
+
.split(/[\s_\-.:/]+/)
|
|
316
|
+
.filter((word) => word !== "")
|
|
317
|
+
.map((word) => word.toLowerCase());
|
|
318
|
+
if (words.length === 0) return "A tool";
|
|
319
|
+
const text = words.join(" ");
|
|
320
|
+
return short(text.charAt(0).toUpperCase() + text.slice(1));
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// The path is the model's, so the trailing separators are trimmed by a loop:
|
|
324
|
+
// a regular expression like /[\\/]+$/ backtracks quadratically on a long run
|
|
325
|
+
// of them, and the activity line reads this every second.
|
|
326
|
+
function trimSeparators(path: string): string {
|
|
327
|
+
let end = path.length;
|
|
328
|
+
while (end > 0 && (path[end - 1] === "/" || path[end - 1] === "\\")) end -= 1;
|
|
329
|
+
return path.slice(0, end);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export function baseName(path: string): string {
|
|
333
|
+
const trimmed = trimSeparators(path);
|
|
334
|
+
return trimmed.slice(Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\")) + 1);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** The folder a path sits in, without its trailing separator; "" at the root or for a bare name. */
|
|
338
|
+
function folderOf(path: string): string {
|
|
339
|
+
const trimmed = trimSeparators(path);
|
|
340
|
+
const at = Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\"));
|
|
341
|
+
return at <= 0 ? "" : trimmed.slice(0, at);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function hostOf(url: string): string {
|
|
345
|
+
try {
|
|
346
|
+
return new URL(url).host;
|
|
347
|
+
} catch {
|
|
348
|
+
return "";
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** A folder as a step names it: its last two segments, "/workspace/app/src/billing" read as "src/billing". */
|
|
353
|
+
function folderWords(path: string): string {
|
|
354
|
+
return trimSeparators(path).split(/[\\/]/).filter((segment) => segment !== "").slice(-2).join("/");
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export function short(value: string): string {
|
|
358
|
+
const flat = value.replace(/\s+/g, " ").trim();
|
|
359
|
+
return flat.length <= TARGET_MAX ? flat : `${flat.slice(0, TARGET_MAX - 1)}…`;
|
|
360
|
+
}
|
package/src/run/tool-view.ts
CHANGED
|
@@ -72,6 +72,13 @@ export interface ToolContentBlock {
|
|
|
72
72
|
* any unrecognized or truncated payload lands there, so this is never worse than
|
|
73
73
|
* showing the raw result.
|
|
74
74
|
*/
|
|
75
|
+
/** The lines a read returned: 1-based, inclusive, and the file's length when the engine says it. */
|
|
76
|
+
export interface FileLineRange {
|
|
77
|
+
readonly start: number;
|
|
78
|
+
readonly end: number;
|
|
79
|
+
readonly total?: number;
|
|
80
|
+
}
|
|
81
|
+
|
|
75
82
|
export type ToolResultView =
|
|
76
83
|
// File edit. oldText/newText are the two sides of the change: the
|
|
77
84
|
// old_string/new_string fragments from the tool's input. The
|
|
@@ -91,6 +98,12 @@ export type ToolResultView =
|
|
|
91
98
|
readonly content: string;
|
|
92
99
|
readonly language?: string;
|
|
93
100
|
readonly truncated: boolean;
|
|
101
|
+
/**
|
|
102
|
+
* The lines a read returned, when its answer says (the Stigmer
|
|
103
|
+
* harness's `@@ lines A-B of T @@` frame; the Claude engine's
|
|
104
|
+
* numbered lines, which give no total): 1-based and inclusive.
|
|
105
|
+
*/
|
|
106
|
+
readonly range?: FileLineRange;
|
|
94
107
|
}
|
|
95
108
|
// A rendered terminal session. `command` is the command that produced this
|
|
96
109
|
// output (the prompt line) — it belongs here, not as a stray arg, because a
|
|
@@ -254,6 +267,37 @@ export function resolveToolKindByName(name: string, mcpServerSlug?: string): Too
|
|
|
254
267
|
return "unknown";
|
|
255
268
|
}
|
|
256
269
|
|
|
270
|
+
// ---------------------------------------------------------------------------
|
|
271
|
+
// Public name: toolPublicName
|
|
272
|
+
// ---------------------------------------------------------------------------
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The one name a tool call's tool is known by outside the kit, or
|
|
276
|
+
* `undefined` for a call that has none. A connected server's tool is
|
|
277
|
+
* `mcp__<server>__<tool>`: the name the agent's tool list, hooks and the
|
|
278
|
+
* console show, and the key a host registers its own card under
|
|
279
|
+
* (`toolCards` in `@stigmer/react-sdk`). Built-in tools, the memory
|
|
280
|
+
* attachment's tools and a server tool named like a built-in resolve to
|
|
281
|
+
* their own kinds ({@link resolveToolKind}), are drawn by the kit, and have
|
|
282
|
+
* none.
|
|
283
|
+
*
|
|
284
|
+
* Composed from the server slug and bare tool every record carries, never
|
|
285
|
+
* parsed. Mirrors the runner's `mcpToolName`
|
|
286
|
+
* (`backend/services/runner/src/shared/mcp-tool-names.ts`); kept honest by
|
|
287
|
+
* `test/fixtures/tool-view/public-names.json`, which both sides assert.
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```ts
|
|
291
|
+
* toolPublicName({ name: "create_invoice", mcpServerName: "billing" });
|
|
292
|
+
* // "mcp__billing__create_invoice"
|
|
293
|
+
* toolPublicName({ name: "Edit", mcpServerName: "" }); // undefined
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
export function toolPublicName(tool: Pick<ToolUse, "name" | "mcpServerName">): string | undefined {
|
|
297
|
+
if (resolveToolKind(tool) !== "mcp") return undefined;
|
|
298
|
+
return `mcp__${tool.mcpServerName}__${tool.name}`;
|
|
299
|
+
}
|
|
300
|
+
|
|
257
301
|
// ---------------------------------------------------------------------------
|
|
258
302
|
// Intent: extractShellIntent
|
|
259
303
|
// ---------------------------------------------------------------------------
|
|
@@ -387,14 +431,30 @@ export function withoutOffloadTail(text: string): string {
|
|
|
387
431
|
/**
|
|
388
432
|
* Normalizes a tool use into a typed ToolResultView for rendering.
|
|
389
433
|
*
|
|
390
|
-
* A result that is an error
|
|
391
|
-
*
|
|
434
|
+
* A result that is an error yields an `error` view, except a foreground
|
|
435
|
+
* command's: a command that failed is still a command, so it reads as its
|
|
436
|
+
* terminal (the command, what it printed, and the exit code when the output
|
|
437
|
+
* names one). Otherwise the kind drives interpretation, and anything
|
|
438
|
+
* unrecognized degrades to `json`/`text`.
|
|
392
439
|
*/
|
|
440
|
+
/**
|
|
441
|
+
* What an edit tried to change, as the diff of its own input (the text it
|
|
442
|
+
* meant to replace and its replacement), whatever its answer: a failed edit
|
|
443
|
+
* opens to what it tried rather than to its error. `undefined` for a call
|
|
444
|
+
* that is not a file edit.
|
|
445
|
+
*/
|
|
446
|
+
export function editAttemptOf(tool: ToolViewInput): Extract<ToolResultView, { type: "diff" }> | undefined {
|
|
447
|
+
if (resolveToolKind(tool) !== "file_edit") return undefined;
|
|
448
|
+
const view = normalizeEdit(tool.input as Args);
|
|
449
|
+
return view.type === "diff" ? view : undefined;
|
|
450
|
+
}
|
|
451
|
+
|
|
393
452
|
export function normalizeToolResult(tool: ToolViewInput): ToolResultView {
|
|
394
453
|
const result = tool.result?.text ?? "";
|
|
395
454
|
const args = tool.input as Args;
|
|
396
455
|
|
|
397
456
|
if (tool.result?.isError === true && result !== "") {
|
|
457
|
+
if (tool.background === undefined && resolveToolKind(tool) === "shell") return failedShell(result, args);
|
|
398
458
|
return { type: "error", message: result };
|
|
399
459
|
}
|
|
400
460
|
|
|
@@ -454,7 +514,7 @@ export function normalizeToolResult(tool: ToolViewInput): ToolResultView {
|
|
|
454
514
|
function normalizeEdit(args: Args): ToolResultView {
|
|
455
515
|
// The inline transcript diff for a file edit is reconstructed from the tool
|
|
456
516
|
// args (there is no captured `file_changes` mirror — apply-then-review renders
|
|
457
|
-
// captured diffs via
|
|
517
|
+
// captured diffs via FileChangeDiff, not here). The Stigmer
|
|
458
518
|
// and Claude engines return prose with no diff, so the presentation layer
|
|
459
519
|
// computes the visual diff from the args fragments.
|
|
460
520
|
const path = firstString(args, PATH_FIELDS) ?? "";
|
|
@@ -465,7 +525,7 @@ function normalizeEdit(args: Args): ToolResultView {
|
|
|
465
525
|
|
|
466
526
|
function normalizeWrite(args: Args): ToolResultView {
|
|
467
527
|
// The inline transcript view for a file write is the proposed content from the
|
|
468
|
-
// tool args (captured diffs render via
|
|
528
|
+
// tool args (captured diffs render via FileChangeDiff, not here).
|
|
469
529
|
const path = firstString(args, PATH_FIELDS) ?? "";
|
|
470
530
|
const content = firstString(args, WRITE_CONTENT_FIELDS) ?? "";
|
|
471
531
|
return { type: "file", path, content, language: languageFromPath(path), truncated: false };
|
|
@@ -480,6 +540,7 @@ function normalizeRead(args: Args, result: string): ToolResultView {
|
|
|
480
540
|
content: framed?.body ?? result,
|
|
481
541
|
language: languageFromPath(path),
|
|
482
542
|
truncated: framed?.truncated || isTruncated(result),
|
|
543
|
+
...(framed?.range !== undefined ? { range: framed.range } : {}),
|
|
483
544
|
};
|
|
484
545
|
}
|
|
485
546
|
|
|
@@ -499,13 +560,30 @@ function stigmerReadHeaderFields(line: string): string | undefined {
|
|
|
499
560
|
return STIGMER_READ_FIELDS.test(fields) ? fields : undefined;
|
|
500
561
|
}
|
|
501
562
|
|
|
502
|
-
|
|
563
|
+
/** A read's answer without its frame, whether the engine cut it, and the lines it returned when the frame says. */
|
|
564
|
+
interface ReadFrame {
|
|
565
|
+
readonly body: string;
|
|
566
|
+
readonly truncated: boolean;
|
|
567
|
+
readonly range?: FileLineRange;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// The header's range, read by position ("lines 14-17 of 88", "lines 1-40").
|
|
571
|
+
const STIGMER_READ_RANGE = /^lines (\d+)-(\d+)(?: of (\d+))?/;
|
|
572
|
+
|
|
573
|
+
function splitStigmerReadFrame(result: string): ReadFrame | undefined {
|
|
503
574
|
const lines = result.split("\n");
|
|
504
575
|
let at = 0;
|
|
505
576
|
while (STIGMER_READ_NOTICE.test(lines[at] ?? "")) at += 1;
|
|
506
577
|
const fields = stigmerReadHeaderFields(lines[at] ?? "");
|
|
507
578
|
if (fields === undefined) return undefined;
|
|
508
|
-
|
|
579
|
+
const range = STIGMER_READ_RANGE.exec(fields);
|
|
580
|
+
return {
|
|
581
|
+
body: lines.slice(at + 1).join("\n"),
|
|
582
|
+
truncated: fields.includes("truncated"),
|
|
583
|
+
...(range !== null
|
|
584
|
+
? { range: { start: Number(range[1]), end: Number(range[2]), ...(range[3] !== undefined ? { total: Number(range[3]) } : {}) } }
|
|
585
|
+
: {}),
|
|
586
|
+
};
|
|
509
587
|
}
|
|
510
588
|
|
|
511
589
|
// The Claude engine answers a text read as the file's lines, each after its
|
|
@@ -515,7 +593,7 @@ function splitStigmerReadFrame(result: string): { body: string; truncated: boole
|
|
|
515
593
|
// number and a tab, read by another engine, is never stripped.
|
|
516
594
|
const NUMBERED_READ_LINE = /^ *(\d+)\t/;
|
|
517
595
|
|
|
518
|
-
function splitNumberedReadFrame(result: string):
|
|
596
|
+
function splitNumberedReadFrame(result: string): ReadFrame | undefined {
|
|
519
597
|
const lines = result.split("\n");
|
|
520
598
|
const first = NUMBERED_READ_LINE.exec(lines[0] ?? "");
|
|
521
599
|
if (first === null) return undefined;
|
|
@@ -526,7 +604,7 @@ function splitNumberedReadFrame(result: string): { body: string; truncated: bool
|
|
|
526
604
|
if (numbered === null || Number(numbered[1]) !== start + index) return undefined;
|
|
527
605
|
body.push(line.slice(numbered[0].length));
|
|
528
606
|
}
|
|
529
|
-
return { body: body.join("\n"), truncated: false };
|
|
607
|
+
return { body: body.join("\n"), truncated: false, range: { start, end: start + body.length - 1 } };
|
|
530
608
|
}
|
|
531
609
|
|
|
532
610
|
// Matches the deepagents shell marker, e.g. "[Command failed with exit code 2]"
|
|
@@ -554,6 +632,19 @@ function normalizeBackground(background: BackgroundCommandView, args: Args): Too
|
|
|
554
632
|
}
|
|
555
633
|
}
|
|
556
634
|
|
|
635
|
+
/**
|
|
636
|
+
* A foreground command whose result is an error, as its terminal: an exit
|
|
637
|
+
* code only where the output names one (an error result with no exit line
|
|
638
|
+
* did not exit 0).
|
|
639
|
+
*/
|
|
640
|
+
function failedShell(result: string, args: Args): ToolResultView {
|
|
641
|
+
const view = normalizeShell(result, args);
|
|
642
|
+
if (view.type !== "terminal" || view.exitCode !== 0) return view;
|
|
643
|
+
return view.command === undefined
|
|
644
|
+
? { type: "terminal", stdout: view.stdout, stderr: view.stderr }
|
|
645
|
+
: { type: "terminal", command: view.command, stdout: view.stdout, stderr: view.stderr };
|
|
646
|
+
}
|
|
647
|
+
|
|
557
648
|
function normalizeShell(result: string, args: Args): ToolResultView {
|
|
558
649
|
// The command is the session's prompt line. "command" matches the shell
|
|
559
650
|
// primaryField shared with the runner/Go classifier and both Stigmer and
|
|
@@ -562,9 +653,8 @@ function normalizeShell(result: string, args: Args): ToolResultView {
|
|
|
562
653
|
|
|
563
654
|
// Stigmer-harness subagent steps emit structured shell output, a
|
|
564
655
|
// {stdout,stderr,exitCode} record; read it so the real output surfaces
|
|
565
|
-
// instead of the raw JSON string. A failed shell
|
|
566
|
-
//
|
|
567
|
-
// only success is handled.
|
|
656
|
+
// instead of the raw JSON string. A failed shell reaches here through
|
|
657
|
+
// failedShell, which keeps a named exit code and drops an unnamed one.
|
|
568
658
|
const shell = tryParseJson(result);
|
|
569
659
|
if (hasShellFields(shell)) {
|
|
570
660
|
return {
|
|
@@ -590,8 +680,25 @@ function normalizeShell(result: string, args: Args): ToolResultView {
|
|
|
590
680
|
return { type: "terminal", command, stdout: result.replace(SHELL_EXIT_LINE, ""), stderr: "", exitCode: exitLine !== null ? Number(exitLine[1]) : 0 };
|
|
591
681
|
}
|
|
592
682
|
|
|
593
|
-
// A
|
|
594
|
-
|
|
683
|
+
// A match line in either form a search tool writes: the Stigmer harness's
|
|
684
|
+
// " 12: // TODO: fix" (the line, then the text), and the "path:12:text" of
|
|
685
|
+
// ripgrep and the search tools of other engines (the file, the line, then
|
|
686
|
+
// the text).
|
|
687
|
+
const GREP_MATCH_LINE = /^\s*(\d+)[:\t]\s?(.*)$/;
|
|
688
|
+
const PATH_MATCH_LINE = /^(\S[^:]*):(\d+):(.*)$/;
|
|
689
|
+
/** Whether `line` is the harness's file header above its matches, e.g. "/workspace/a.go:" (checked without a pattern, in linear time). */
|
|
690
|
+
function isFileHeader(line: string): boolean {
|
|
691
|
+
return line !== "" && line.trimStart() === line && line.trimEnd().endsWith(":");
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/** A match line as its parts, or undefined when the line is neither form. */
|
|
695
|
+
function matchOf(line: string): ToolSearchMatch | undefined {
|
|
696
|
+
const pathForm = PATH_MATCH_LINE.exec(line);
|
|
697
|
+
if (pathForm !== null) return { file: pathForm[1]!, line: Number(pathForm[2]), text: pathForm[3]!.trim() };
|
|
698
|
+
const lineForm = GREP_MATCH_LINE.exec(line);
|
|
699
|
+
if (lineForm !== null) return { line: Number(lineForm[1]), text: lineForm[2]!.trim() };
|
|
700
|
+
return undefined;
|
|
701
|
+
}
|
|
595
702
|
|
|
596
703
|
// A search answers in plain text (see searchFromPlainText). A JSON answer is
|
|
597
704
|
// not a shape any engine is known to give, so it degrades to the json view
|
|
@@ -605,15 +712,22 @@ function normalizeSearch(result: string): ToolResultView {
|
|
|
605
712
|
return searchFromPlainText(result);
|
|
606
713
|
}
|
|
607
714
|
|
|
608
|
-
// The plain-text path
|
|
609
|
-
// glob/semantic search emit one path per line.
|
|
715
|
+
// The plain-text path: a content search emits match lines (either form,
|
|
716
|
+
// see matchOf); glob/semantic search emit one path per line.
|
|
610
717
|
function searchFromPlainText(result: string): ToolResultView {
|
|
611
718
|
const lines = nonEmptyLines(result);
|
|
612
|
-
|
|
719
|
+
// The harness's form names each file once, on a "path:" line above its
|
|
720
|
+
// matches; each match carries the file it sits under.
|
|
721
|
+
const found: ToolSearchMatch[] = [];
|
|
722
|
+
let file: string | undefined;
|
|
723
|
+
for (const line of lines) {
|
|
724
|
+
const match = matchOf(line);
|
|
725
|
+
if (match !== undefined) found.push(match.file === undefined && file !== undefined ? { ...match, file } : match);
|
|
726
|
+
else if (isFileHeader(line)) file = line.trim().slice(0, -1);
|
|
727
|
+
}
|
|
613
728
|
|
|
614
|
-
if (
|
|
615
|
-
|
|
616
|
-
return { type: "search", matches, count: matches.length, kind: "content" };
|
|
729
|
+
if (found.length > 0) {
|
|
730
|
+
return { type: "search", matches: found, count: found.length, kind: "content" };
|
|
617
731
|
}
|
|
618
732
|
|
|
619
733
|
// Path/name results (glob, semantic search): each meaningful line is a match.
|