@adecore/agents-react 0.50.7 → 0.52.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.
Files changed (43) hide show
  1. package/dist/chat/index.d.ts +2 -1
  2. package/dist/chat/index.js +2 -1
  3. package/dist/chat/logic/command-looks.d.ts +17 -5
  4. package/dist/chat/logic/command-looks.js +35 -7
  5. package/dist/chat/logic/timeline.d.ts +6 -4
  6. package/dist/chat/logic/timeline.js +19 -21
  7. package/dist/chat/logic/tool-looks.d.ts +16 -0
  8. package/dist/chat/logic/tool-looks.js +188 -0
  9. package/dist/chat/ui/Markdown.js +97 -103
  10. package/dist/chat/ui/ShellCommand.d.ts +2 -1
  11. package/dist/chat/ui/ShellCommand.js +73 -22
  12. package/dist/chat/ui/SubagentTimeline.js +5 -2
  13. package/dist/chat/ui/Timeline.js +2 -1
  14. package/dist/chat/ui/rows/RowContainer.js +26 -23
  15. package/dist/chat/ui/rows/WorkRows.d.ts +3 -4
  16. package/dist/chat/ui/rows/WorkRows.js +592 -519
  17. package/dist/chat/ui/rows/row-rhythm.d.ts +2 -2
  18. package/dist/chat/ui/rows/row-rhythm.js +9 -6
  19. package/dist/chat/ui/thread-references.d.ts +1 -0
  20. package/dist/chat/ui/thread-references.js +4 -3
  21. package/dist/host.d.ts +28 -0
  22. package/dist/index.d.ts +1 -1
  23. package/dist/locales/en/agent-chat.d.ts +3 -0
  24. package/dist/locales/en/agent-chat.js +3 -0
  25. package/dist/locales/nl/agent-chat.d.ts +3 -0
  26. package/dist/locales/nl/agent-chat.js +3 -0
  27. package/package.json +8 -8
  28. package/src/chat/index.ts +2 -1
  29. package/src/chat/logic/command-looks.ts +51 -10
  30. package/src/chat/logic/timeline.ts +28 -24
  31. package/src/chat/logic/tool-looks.ts +228 -0
  32. package/src/chat/ui/Markdown.tsx +21 -7
  33. package/src/chat/ui/ShellCommand.tsx +33 -6
  34. package/src/chat/ui/SubagentTimeline.tsx +3 -2
  35. package/src/chat/ui/Timeline.tsx +2 -1
  36. package/src/chat/ui/rows/RowContainer.tsx +4 -2
  37. package/src/chat/ui/rows/WorkRows.tsx +46 -33
  38. package/src/chat/ui/rows/row-rhythm.ts +9 -6
  39. package/src/chat/ui/thread-references.ts +5 -1
  40. package/src/host.ts +51 -0
  41. package/src/index.ts +3 -0
  42. package/src/locales/en/agent-chat.ts +3 -0
  43. package/src/locales/nl/agent-chat.ts +3 -0
@@ -0,0 +1,228 @@
1
+ import type { ChatToolItem } from '@adecore/agent-contracts';
2
+ import type { ToolLook, ToolLookMatch } from '../../host';
3
+ import type { CommandLookWord, LookRun } from './command-looks';
4
+ import { isRecord } from './json';
5
+
6
+ // A value keeps this many characters of what it says.
7
+ const FOLD_LENGTH = 40;
8
+ // An ACP agent titles a call of an MCP tool with the server and the tool, each agent with a separator of its own.
9
+ const TITLE_SEPARATORS = ['__', '_', '-'];
10
+ const TOOL_NAME = /^[A-Za-z0-9][A-Za-z0-9_-]*$/;
11
+ // The name an ACP call of kind `other` gets, which is all a call of an MCP tool is where its agent marks none.
12
+ const ACP_TOOL_ROW = 'Tool';
13
+
14
+ /* A call of a tool on an MCP server, the same whichever CLI made it. */
15
+ export interface McpCall {
16
+ readonly server: string;
17
+ /* The tool's name on its server, without the server. */
18
+ readonly tool: string;
19
+ /* The arguments the tool got. */
20
+ readonly input: Readonly<Record<string, unknown>>;
21
+ /* What the tool answered, out of any envelope the CLI wraps it in. */
22
+ readonly output: string | null;
23
+ }
24
+
25
+ /* A call of a look's MCP server read as a tool of its own. */
26
+ export interface ToolLookRun extends LookRun {
27
+ readonly look: ToolLook;
28
+ readonly match: ToolLookMatch;
29
+ readonly call: McpCall;
30
+ }
31
+
32
+ const caches = new WeakMap<readonly ToolLook[], WeakMap<ChatToolItem, ToolLookRun | null>>();
33
+
34
+ /* The server and the tool `text` names as `<server><separator><tool>`, on the first of `servers` it starts with. */
35
+ function toolOn(text: string, servers: readonly string[], separators: readonly string[]): Pick<McpCall, 'server' | 'tool'> | null {
36
+ for (const server of servers) {
37
+ for (const separator of separators) {
38
+ const prefix = `${server}${separator}`;
39
+ const tool = text.slice(prefix.length);
40
+
41
+ if (text.startsWith(prefix) && TOOL_NAME.test(tool)) {
42
+ return { server, tool };
43
+ }
44
+ }
45
+ }
46
+
47
+ return null;
48
+ }
49
+
50
+ /*
51
+ * The call as its name says it, whatever its input holds: `mcp__<server>__<tool>`, `<server>/<tool>`, or the tool
52
+ * Grok's `use_tool` names in its input. Undefined where the name says nothing of a server, and null where it names
53
+ * one that is not in `servers`, which no title overrules.
54
+ */
55
+ function namedCall(name: string, input: Readonly<Record<string, unknown>>, servers: readonly string[]): Omit<McpCall, 'output'> | null | undefined {
56
+ if (name.startsWith('mcp__')) {
57
+ const named = toolOn(name.slice(5), servers, ['__']);
58
+
59
+ return named === null ? null : { ...named, input };
60
+ }
61
+
62
+ if (name.includes('/')) {
63
+ const named = toolOn(name, servers, ['/']);
64
+
65
+ return named === null ? null : { ...named, input };
66
+ }
67
+
68
+ if (name !== ACP_TOOL_ROW || typeof input.tool_name !== 'string') {
69
+ return undefined;
70
+ }
71
+
72
+ const used = toolOn(input.tool_name, servers, ['__']);
73
+
74
+ return used === null ? null : { ...used, input: isRecord(input.tool_input) ? input.tool_input : {} };
75
+ }
76
+
77
+ /*
78
+ * The call as an ACP agent that marks no server titles it, in the rows `toolRow` of `@adecore/agents` draws: a call
79
+ * of kind `other`, or a `Read` Copilot makes of a tool that only reads. A `Read` of a file is no MCP call.
80
+ */
81
+ function titledCall(name: string, input: Readonly<Record<string, unknown>>, servers: readonly string[]): Omit<McpCall, 'output'> | null {
82
+ const titledRow = name === ACP_TOOL_ROW || (name === 'Read' && input.file_path === undefined);
83
+ const title = typeof input.description === 'string' ? input.description : null;
84
+ const titled = !titledRow || title === null ? null : toolOn(title, servers, TITLE_SEPARATORS);
85
+
86
+ if (titled === null) {
87
+ return null;
88
+ }
89
+
90
+ // Antigravity hands the arguments over as `arguments`, Copilot beside the title.
91
+ return {
92
+ ...titled,
93
+ input: isRecord(input.arguments) ? input.arguments : Object.fromEntries(Object.entries(input).filter(([key]) => key !== 'description'))
94
+ };
95
+ }
96
+
97
+ /* A tool's answer out of the envelope Grok puts around an MCP answer, `{"type":"MCP",...,"output":{"OkayOutput":"..."}}`. */
98
+ function unwrapped(output: string | null): string | null {
99
+ if (output === null || !output.trimStart().startsWith('{')) {
100
+ return output;
101
+ }
102
+
103
+ try {
104
+ const parsed: unknown = JSON.parse(output);
105
+
106
+ if (!isRecord(parsed) || parsed.type !== 'MCP' || !isRecord(parsed.output)) {
107
+ return output;
108
+ }
109
+
110
+ const inner = Object.values(parsed.output);
111
+
112
+ return inner.length === 1 && typeof inner[0] === 'string' ? inner[0] : output;
113
+ } catch {
114
+ return output;
115
+ }
116
+ }
117
+
118
+ /*
119
+ * The call of an MCP tool on one of `servers` that `item` is: `mcp__<server>__<tool>` (Claude Code, the API and
120
+ * an ACP agent that marks its server), `<server>/<tool>` (Codex), or an ACP call titled with the server and the
121
+ * tool (Grok's `use_tool`, Copilot, and calls of agents from before they were marked). A name that says which
122
+ * server is read before any title, over every server, the longest name first. Null for any other call.
123
+ */
124
+ export function mcpCallOf(item: Pick<ChatToolItem, 'name' | 'input' | 'output'>, servers: readonly string[]): McpCall | null {
125
+ const input = isRecord(item.input) ? item.input : {};
126
+ const longestFirst = [...servers].sort((left, right) => right.length - left.length);
127
+ const named = namedCall(item.name, input, longestFirst);
128
+ const call = named === undefined ? titledCall(item.name, input, longestFirst) : named;
129
+
130
+ return call === null || call === undefined ? null : { ...call, output: unwrapped(item.output) };
131
+ }
132
+
133
+ function folded(text: string): string {
134
+ const line = text.replace(/\s+/g, ' ');
135
+
136
+ return line.length > FOLD_LENGTH ? `${line.slice(0, FOLD_LENGTH)}…` : line;
137
+ }
138
+
139
+ function scalarText(value: unknown): string | null {
140
+ return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' ? String(value) : null;
141
+ }
142
+
143
+ /* The lead and the fields of a call as `field=value`, each value a word a host may name; a value that is no text reads as `{…}` or `[…]`. */
144
+ function detailOf(match: ToolLookMatch, input: Readonly<Record<string, unknown>>): Pick<LookRun, 'detail' | 'words'> {
145
+ let detail = match.lead ?? '';
146
+ const words: CommandLookWord[] = [];
147
+
148
+ for (const field of match.fields ?? Object.keys(input)) {
149
+ const value = input[field];
150
+
151
+ if (value === undefined || value === null) {
152
+ continue;
153
+ }
154
+
155
+ detail += `${detail === '' ? '' : ' '}${field}=`;
156
+ const values: unknown[] = Array.isArray(value) ? value : [value];
157
+ const texts = values.map(scalarText).filter((text) => text !== null);
158
+
159
+ if (texts.length < values.length) {
160
+ detail += Array.isArray(value) ? '[…]' : '{…}';
161
+
162
+ continue;
163
+ }
164
+
165
+ texts.forEach((text, index) => {
166
+ const quoted = text === '' || /[\s",]/.test(text);
167
+ detail += `${index === 0 ? '' : ','}${quoted ? '"' : ''}`;
168
+ const start = detail.length;
169
+ detail += folded(text);
170
+ words.push({ start, end: detail.length, text, option: field });
171
+ detail += quoted ? '"' : '';
172
+ });
173
+ }
174
+
175
+ return { detail, words };
176
+ }
177
+
178
+ function read(item: ChatToolItem, looks: readonly ToolLook[]): ToolLookRun | null {
179
+ const call = mcpCallOf(
180
+ item,
181
+ looks.map(({ server }) => server)
182
+ );
183
+ const look = call === null ? undefined : looks.find(({ server }) => server === call.server);
184
+ const match = call === null || look === undefined ? null : look.describe(call.tool, call.input);
185
+
186
+ if (call === null || look === undefined || match === null) {
187
+ return null;
188
+ }
189
+
190
+ return {
191
+ look,
192
+ match,
193
+ call,
194
+ label: match.label,
195
+ group: match.group,
196
+ ...detailOf(match, call.input),
197
+ code: [],
198
+ others: [],
199
+ more: 0,
200
+ heredoc: false,
201
+ shell: false
202
+ };
203
+ }
204
+
205
+ /* The call of a look's MCP server that `item` is, read as that look's tool; null for any other call and one the look does not describe. */
206
+ export function matchToolLook(item: ChatToolItem, looks: readonly ToolLook[] | undefined): ToolLookRun | null {
207
+ if (looks === undefined || looks.length === 0) {
208
+ return null;
209
+ }
210
+
211
+ let cache = caches.get(looks);
212
+
213
+ if (cache === undefined) {
214
+ cache = new WeakMap();
215
+ caches.set(looks, cache);
216
+ }
217
+
218
+ const known = cache.get(item);
219
+
220
+ if (known !== undefined) {
221
+ return known;
222
+ }
223
+
224
+ const run = read(item, looks);
225
+ cache.set(item, run);
226
+
227
+ return run;
228
+ }
@@ -321,7 +321,7 @@ const replyComponents = {
321
321
  function ReplyCode({ className, children, node }: { className?: string; children?: ReactNode } & ExtraProps) {
322
322
  const reply = useContext(ShellCodeBlockReplyContext);
323
323
  const block = useContext(BlockCodeContext);
324
- const { renderShellCodeBlock } = useChatHost();
324
+ const { renderCodeFence, renderShellCodeBlock } = useChatHost();
325
325
  const properties = node?.properties;
326
326
  const language = properties?.['data-shell-language'];
327
327
  const code = properties?.['data-shell-code'];
@@ -333,12 +333,20 @@ function ReplyCode({ className, children, node }: { className?: string; children
333
333
 
334
334
  const text = String(children ?? '');
335
335
 
336
- if (block && languageOf(className) === 'mermaid') {
337
- return <Diagram code={text.replace(/\n$/, '')} />;
338
- }
339
-
340
336
  if (block) {
341
- return <CodeBlock code={text.replace(/\n$/, '')} lang={languageOf(className)} actions={shellCodeBlockActions(reply, shell, renderShellCodeBlock)} />;
337
+ const lang = languageOf(className);
338
+ const body = text.replace(/\n$/, '');
339
+ const drawn = renderCodeFence?.(lang, body);
340
+
341
+ if (drawn !== undefined && drawn !== null) {
342
+ return drawn;
343
+ }
344
+
345
+ if (lang === 'mermaid') {
346
+ return <Diagram code={body} />;
347
+ }
348
+
349
+ return <CodeBlock code={body} lang={lang} actions={shellCodeBlockActions(reply, shell, renderShellCodeBlock)} />;
342
350
  }
343
351
 
344
352
  return <InlineCode text={text} />;
@@ -437,10 +445,16 @@ const ReplyFence = memo(function ReplyFence({
437
445
  open: boolean;
438
446
  reply?: ShellCodeBlockReply;
439
447
  }) {
440
- const { renderShellCodeBlock } = useChatHost();
448
+ const { renderCodeFence, renderShellCodeBlock } = useChatHost();
441
449
  // The class markdown gives the code, read back the way a parsed fence's is.
442
450
  const lang = languageOf(`language-${info.split(/[ \t]/, 1)[0]}`);
443
451
  const shell = reply === undefined ? null : shellFence(source);
452
+ // The host gets a fence only once it closed, so it never draws a body that is still being written.
453
+ const drawn = open ? null : renderCodeFence?.(lang, body);
454
+
455
+ if (drawn !== undefined && drawn !== null) {
456
+ return drawn;
457
+ }
444
458
 
445
459
  return (
446
460
  <CodeStreamingContext.Provider value={open}>
@@ -6,6 +6,7 @@ import type { CodeToken } from './code-lines';
6
6
  import { useChatHost, type CommandName } from '../../host';
7
7
  import { useCodeTheme } from './code-theme';
8
8
  import { tokenStyle, useCodeHighlighter, type CodeHighlighter, type Highlighter } from './highlighter';
9
+ import { ReferenceChip } from './ReferenceChip';
9
10
 
10
11
  /* One colored run of a command, as offsets into it. */
11
12
  interface Run extends CodeToken {
@@ -156,8 +157,23 @@ function draw(command: string, runs: readonly Run[] | null, from: number, to: nu
156
157
  return parts;
157
158
  }
158
159
 
159
- /* A word as its name, in a frame so it reads as a reference; the word itself stays in the text, for a find. */
160
+ /*
161
+ * A word as its name: the `#` chip of the reference it is, else in a frame so it reads as one. The word itself
162
+ * stays in the text, for a find.
163
+ */
160
164
  function Name({ named }: { named: NamedWord }) {
165
+ const { reference } = named.name;
166
+
167
+ if (reference !== undefined) {
168
+ // A click opens what the chip points at and leaves the line it stands on as it was.
169
+ return (
170
+ <span className="font-sans" onClick={(e) => e.stopPropagation()}>
171
+ <ReferenceChip id={reference} lastName={named.name.name} />
172
+ <span className="sr-only">{named.word}</span>
173
+ </span>
174
+ );
175
+ }
176
+
161
177
  return (
162
178
  <Tooltip label={named.word}>
163
179
  <span className="inline-flex items-center gap-1 rounded bg-surface-hover px-1 font-sans text-text ring-1 ring-border ring-inset">
@@ -192,8 +208,19 @@ function drawNamed(command: string, runs: readonly Run[] | null, names: readonly
192
208
  * `python3 -c` argument, a heredoc) in its own language. Plain text until the grammars are in, which
193
209
  * changes no size; a command past the limit stays plain. `code` adds stretches whose language the
194
210
  * caller knows, and `names` draws words as the names a host gave them, both as offsets into `command`.
211
+ * `plain` draws text that is no shell source uncolored, with its names.
195
212
  */
196
- export function ShellCommand({ command, code = NO_CODE, names = NO_NAMES }: { command: string; code?: readonly EmbeddedCode[]; names?: readonly NamedWord[] }) {
213
+ export function ShellCommand({
214
+ command,
215
+ code = NO_CODE,
216
+ names = NO_NAMES,
217
+ plain = false
218
+ }: {
219
+ command: string;
220
+ code?: readonly EmbeddedCode[];
221
+ names?: readonly NamedWord[];
222
+ plain?: boolean;
223
+ }) {
197
224
  // The highlighter's cache fills in place; compiled, the runs it gave before its grammars loaded would stay.
198
225
  'use no memo';
199
226
  const theme = useCodeTheme();
@@ -201,11 +228,11 @@ export function ShellCommand({ command, code = NO_CODE, names = NO_NAMES }: { co
201
228
  const highlighter = useCodeHighlighter();
202
229
  // Bumped when a load settles, so the cache is asked again.
203
230
  const [, setLoads] = useState(0);
204
- const tooLong = command.length > EMBEDDED_CODE_LIMIT;
205
- const runs = tooLong ? null : highlight(command, code, theme, highlighter);
231
+ const uncolored = plain || command.length > EMBEDDED_CODE_LIMIT;
232
+ const runs = uncolored ? null : highlight(command, code, theme, highlighter);
206
233
 
207
234
  useEffect(() => {
208
- if (runs !== null || tooLong) {
235
+ if (runs !== null || uncolored) {
209
236
  return;
210
237
  }
211
238
 
@@ -218,7 +245,7 @@ export function ShellCommand({ command, code = NO_CODE, names = NO_NAMES }: { co
218
245
  return () => {
219
246
  cancelled = true;
220
247
  };
221
- }, [highlighter, command, code, theme, custom, runs, tooLong]);
248
+ }, [highlighter, command, code, theme, custom, runs, uncolored]);
222
249
 
223
250
  if (names.length > 0) {
224
251
  return <>{drawNamed(command, runs, names)}</>;
@@ -44,7 +44,7 @@ export function SubagentTimeline({
44
44
  }) {
45
45
  const { t } = useTranslation('agent-chat');
46
46
  const scope = useChatScope();
47
- const { commandLooks } = useChatHost();
47
+ const { commandLooks, toolLooks, saidBy } = useChatHost();
48
48
  const { trail, show } = useSubagentTrail(chatId);
49
49
  const [state, setState] = useState<SubagentConversationState>(INITIAL_CONVERSATION);
50
50
  const controller = useRef<SubagentConversation | null>(null);
@@ -89,6 +89,7 @@ export function SubagentTimeline({
89
89
  expandedSubagents: subagents.ids,
90
90
  activeTurnId: null,
91
91
  commandLooks,
92
+ toolLooks,
92
93
  hideTools
93
94
  });
94
95
 
@@ -185,7 +186,7 @@ export function SubagentTimeline({
185
186
  const previous = index > 0 ? rows[index - 1]! : null;
186
187
 
187
188
  return (
188
- <div key={row.id} data-item-id={row.id} className={rowRhythm(row, previous)}>
189
+ <div key={row.id} data-item-id={row.id} className={rowRhythm(row, previous, saidBy !== undefined)}>
189
190
  <Row
190
191
  row={row}
191
192
  chatId={chatId}
@@ -291,10 +291,11 @@ export function Timeline({
291
291
  activeTurnId,
292
292
  forkedTurns,
293
293
  commandLooks: host.commandLooks,
294
+ toolLooks: host.toolLooks,
294
295
  hideTools
295
296
  }
296
297
  );
297
- }, [order, items, groups.ids, turns.ids, subagents.ids, activeTurnId, forkedTurns, host.commandLooks, hideTools]);
298
+ }, [order, items, groups.ids, turns.ids, subagents.ids, activeTurnId, forkedTurns, host.commandLooks, host.toolLooks, hideTools]);
298
299
 
299
300
  const cards = useHostThreadCards(scopeId, chatId);
300
301
  const drawsVisuals = host.visuals !== null;
@@ -1,5 +1,6 @@
1
1
  import type { ReactNode, Ref } from 'react';
2
2
  import clsx from 'clsx';
3
+ import { useChatHost } from '../../../host';
3
4
  import type { TimelineRow } from '../../logic/timeline';
4
5
  import { rowPosition, rowRhythm, saidToChat } from './row-rhythm';
5
6
 
@@ -14,6 +15,7 @@ export interface RowContainerProps {
14
15
 
15
16
  export function RowContainer({ row, previous, index, top, ref, children }: RowContainerProps) {
16
17
  const wide = row.kind === 'visual' && row.visual.layout === 'wide';
18
+ const hostSpeaks = useChatHost().saidBy !== undefined;
17
19
 
18
20
  return (
19
21
  <div
@@ -23,9 +25,9 @@ export function RowContainer({ row, previous, index, top, ref, children }: RowCo
23
25
  className={clsx(
24
26
  'absolute inset-x-0 top-0 mx-auto w-full',
25
27
  wide ? 'chat-wide-content' : 'chat-column-content',
26
- rowRhythm(row, previous),
28
+ rowRhythm(row, previous, hostSpeaks),
27
29
  // Turn gaps belong inside the measurement used by the virtualizer.
28
- saidToChat(row) && index > 0 && 'pt-(--chat-turn-gap)'
30
+ saidToChat(row, hostSpeaks) && index > 0 && 'pt-(--chat-turn-gap)'
29
31
  )}
30
32
  style={rowPosition(row.kind, top)}
31
33
  >
@@ -5,7 +5,7 @@ import { useTranslation } from 'react-i18next';
5
5
  import { ChevronRight, FileDiff, X } from 'lucide-react';
6
6
  import type { ChatCheckpointDiff, ChatCheckpointFile, ChatFileChange, ChatToolItem, ChatTurnItem } from '@adecore/agent-contracts';
7
7
  import type { BaseMoved } from '../../logic/timeline';
8
- import { matchCommandLook, type CommandLookRun } from '../../logic/command-looks';
8
+ import { lookKey, lookRunOf, type Look, type LookRun } from '../../logic/command-looks';
9
9
  import {
10
10
  exitCodeOf,
11
11
  fileChanges,
@@ -22,7 +22,7 @@ import {
22
22
  type FileChange
23
23
  } from '../../logic/tools';
24
24
  import { useOpenForFind } from '../find-reveal';
25
- import { useChatHost, type CommandLook } from '../../../host';
25
+ import { useChatHost, type CommandLook, type ToolLook } from '../../../host';
26
26
  import { useCommandNames } from '../../../host-hooks';
27
27
  import { lazyNamed } from '../../../lazy';
28
28
  import { useChatActions } from '../../actions';
@@ -34,8 +34,10 @@ import { formatClockDuration, formatElapsedShort } from '@adecore/ui/format';
34
34
  import { Button, Icon, LazyBoundary, MiddleTruncate, Spinner, useTickingText } from '@adecore/ui';
35
35
  import { ROW_GUTTER, toolIcon } from '../icons';
36
36
  import { AnsiOutput } from '../AnsiOutput';
37
+ import { ReferenceLookupContext } from '../reference-lookup';
37
38
  import { RowTime } from '../RowTime';
38
39
  import { ShellCommand, type NamedWord } from '../ShellCommand';
40
+ import { useReferenceLookupIn } from '../thread-references';
39
41
  import { DiffStack, ReviewStackView } from '../diff/DiffStack';
40
42
  import { reviewEntries, type ReviewEntry } from '../diff/review-plan';
41
43
  import { useReviewStack } from '../diff/use-review-stack';
@@ -75,14 +77,15 @@ function withBrand(text: string, brands: readonly string[]): ReactNode {
75
77
  return text;
76
78
  }
77
79
 
78
- function hasNames(look: CommandLook): look is CommandLook & Required<Pick<CommandLook, 'useNames'>> {
80
+ function hasNames(look: Look): look is Look & Required<Pick<Look, 'useNames'>> {
79
81
  return look.useNames !== undefined;
80
82
  }
81
83
 
82
84
  /* The detail of a look's run, with the words its host names drawn as those names. */
83
- function NamedLookDetail({ run, chatId, look }: { run: CommandLookRun; chatId: string; look: Required<Pick<CommandLook, 'useNames'>> }) {
85
+ function NamedLookDetail({ run, chatId, look }: { run: LookRun; chatId: string; look: Required<Pick<Look, 'useNames'>> }) {
84
86
  const scope = useContext(ChatScopeContext);
85
87
  const nameOf = useCommandNames(look, scope?.id ?? '', chatId);
88
+ const lookup = useReferenceLookupIn(scope?.id ?? '', chatId);
86
89
 
87
90
  const names = run.words.flatMap((word): NamedWord[] => {
88
91
  const name = nameOf(word.text, word.option);
@@ -90,24 +93,28 @@ function NamedLookDetail({ run, chatId, look }: { run: CommandLookRun; chatId: s
90
93
  return name === null ? [] : [{ start: word.start, end: word.end, word: word.text, name }];
91
94
  });
92
95
 
93
- return <ShellCommand command={run.detail} code={run.code} names={names} />;
96
+ return (
97
+ <ReferenceLookupContext value={lookup}>
98
+ <ShellCommand command={run.detail} code={run.code} names={names} plain={!run.shell} />
99
+ </ReferenceLookupContext>
100
+ );
94
101
  }
95
102
 
96
- function LookDetail({ run, chatId }: { run: CommandLookRun; chatId: string }) {
103
+ function LookDetail({ run, chatId }: { run: LookRun; chatId: string }) {
97
104
  const { look } = run;
98
105
 
99
106
  if (!hasNames(look)) {
100
- return <ShellCommand command={run.detail} code={run.code} />;
107
+ return <ShellCommand command={run.detail} code={run.code} plain={!run.shell} />;
101
108
  }
102
109
 
103
110
  // Keyed by the look, so a row whose command turns into another look's starts its hooks again.
104
- return <NamedLookDetail key={look.program} run={run} chatId={chatId} look={look} />;
111
+ return <NamedLookDetail key={lookKey(look)} run={run} chatId={chatId} look={look} />;
105
112
  }
106
113
 
107
114
  const TAG = 'shrink-0 rounded bg-surface-hover px-1.5 text-text-faint';
108
115
 
109
116
  /* What else the line ran, in quiet tags: the other programs and a heredoc, and how many more runs of a look. */
110
- function LookTags({ run }: { run: CommandLookRun }) {
117
+ function LookTags({ run }: { run: LookRun }) {
111
118
  const { t } = useTranslation('agent-chat');
112
119
  const also = run.heredoc ? [...run.others, t('work.heredoc')] : run.others;
113
120
 
@@ -146,23 +153,24 @@ export function ToggleLine({
146
153
  detail?: string;
147
154
  /* Draws `detail` as a shell command, in the colors of the code theme. */
148
155
  command?: boolean;
149
- /* A shell call read as a look's tool, whose detail and tags take the place of `detail`. */
150
- look?: { run: CommandLookRun; chatId: string };
156
+ /* A call read as a look's tool, whose detail and tags take the place of `detail`. */
157
+ look?: { run: LookRun; chatId: string };
151
158
  /* `detail` is the path of a file, cut in the middle so its name stays in sight. */
152
159
  path?: boolean;
153
160
  open?: boolean;
154
161
  onToggle?(): void;
162
+ /* Marks the line with a cross in the error tone; the label and the detail keep their own. */
155
163
  failed?: boolean;
156
164
  live?: boolean;
157
165
  trailing?: ReactNode;
158
166
  time?: number;
159
167
  className?: string;
160
168
  }) {
169
+ const { t } = useTranslation('agent-chat');
161
170
  const classes = clsx(
162
171
  // Fix the width so long commands reach the truncation inside the negative margins.
163
- 'group/line -mx-1 mb-0.5 flex h-7 w-[calc(100%+8px)] items-center gap-2 rounded-md px-1 text-left text-xs',
172
+ 'group/line -mx-1 mb-0.5 flex h-7 w-[calc(100%+8px)] items-center gap-2 rounded-md px-1 text-left text-xs text-text-muted',
164
173
  onToggle !== undefined && 'hover:bg-surface-hover',
165
- failed ? 'text-status-error' : 'text-text-muted',
166
174
  className
167
175
  );
168
176
 
@@ -196,7 +204,12 @@ export function ToggleLine({
196
204
  <span className="grow" />
197
205
  {trailing}
198
206
  {time !== undefined && <RowTime at={time} />}
199
- {failed && <Icon icon={X} size={12} className="shrink-0" />}
207
+ {failed && (
208
+ <span className="shrink-0 text-status-error">
209
+ <Icon icon={X} size={12} />
210
+ <span className="sr-only">{t('work.failed')}</span>
211
+ </span>
212
+ )}
200
213
  {onToggle !== undefined && (
201
214
  <Icon icon={ChevronRight} size={12} className={clsx('shrink-0 text-text-faint transition-transform', open && 'rotate-90')} />
202
215
  )}
@@ -458,13 +471,13 @@ export function WorkRow({
458
471
  const summary = detail ?? toolSummary(tool.name, tool.input);
459
472
  const opens = hasToolBody(tool);
460
473
  const shell = shellCommand(tool.name, tool.input);
461
- const run = detail === undefined && icon === undefined && label === undefined && shell !== null ? matchCommandLook(shell, host.commandLooks) : null;
474
+ const run = detail === undefined && icon === undefined && label === undefined ? lookRunOf(tool, host.commandLooks, host.toolLooks) : null;
462
475
 
463
476
  return (
464
477
  <div data-find-item={tool.id} className={nested ? 'ml-6' : undefined}>
465
478
  <ToggleLine
466
479
  icon={run === null ? (icon ?? toolIcon(tool.name)) : <run.look.Mark size={12} />}
467
- label={run === null ? (label ?? tool.name) : run.match.label}
480
+ label={run === null ? (label ?? tool.name) : run.label}
468
481
  brand={run?.look.brand}
469
482
  detail={summary}
470
483
  look={run === null ? undefined : { run, chatId: chatId ?? '' }}
@@ -489,10 +502,9 @@ export function RunningFor({ startedAt }: { startedAt: number }) {
489
502
  return <span ref={ref} className="shrink-0 text-xs text-text-faint tabular-nums" />;
490
503
  }
491
504
 
492
- /* The mark of one call: the look its shell command runs under, else what the tool is about. */
493
- function callIcon(tool: ChatToolItem, commandLooks: readonly CommandLook[] | undefined): ReactNode {
494
- const shell = shellCommand(tool.name, tool.input);
495
- const run = shell === null ? null : matchCommandLook(shell, commandLooks);
505
+ /* The mark of one call: the look it runs under, else what the tool is about. */
506
+ function callIcon(tool: ChatToolItem, commandLooks: readonly CommandLook[] | undefined, toolLooks: readonly ToolLook[] | undefined): ReactNode {
507
+ const run = lookRunOf(tool, commandLooks, toolLooks);
496
508
 
497
509
  return run === null ? toolIcon(tool.name) : <run.look.Mark size={12} />;
498
510
  }
@@ -504,9 +516,8 @@ export function WorkLiveRow({ chatId, tool: derived }: { chatId: string; tool: C
504
516
  const tail = liveOutput(tool);
505
517
  const summary = toolSummary(tool.name, tool.input);
506
518
  const opens = hasToolBody(tool);
507
- const shell = shellCommand(tool.name, tool.input);
508
- const { commandLooks } = useChatHost();
509
- const run = shell === null ? null : matchCommandLook(shell, commandLooks);
519
+ const { commandLooks, toolLooks } = useChatHost();
520
+ const run = lookRunOf(tool, commandLooks, toolLooks);
510
521
  const awaiting = useAwaitsApproval(chatId, tool.toolUseId);
511
522
  const { t } = useTranslation('agent-chat');
512
523
 
@@ -514,7 +525,7 @@ export function WorkLiveRow({ chatId, tool: derived }: { chatId: string; tool: C
514
525
  <div>
515
526
  <ToggleLine
516
527
  icon={run === null ? toolIcon(tool.name) : <run.look.Mark size={12} />}
517
- label={run === null ? tool.name : run.match.label}
528
+ label={run === null ? tool.name : run.label}
518
529
  brand={run?.look.brand}
519
530
  look={run === null ? undefined : { run, chatId }}
520
531
  detail={summary || tool.progress?.description || ''}
@@ -551,28 +562,30 @@ export function WorkGroupRow({
551
562
  }: {
552
563
  tools: ChatToolItem[];
553
564
  summary: string;
554
- look?: CommandLook;
565
+ look?: Look;
555
566
  expanded: boolean;
556
567
  onToggle(): void;
557
568
  }) {
558
569
  const { t } = useTranslation('agent-chat');
559
- const { commandLooks } = useChatHost();
570
+ const { commandLooks, toolLooks } = useChatHost();
560
571
  const running = tools.filter((tool) => tool.state === 'running').length;
561
- const failed = tools.some((tool) => tool.state === 'error');
572
+ const failed = tools.filter((tool) => tool.state === 'error').length;
562
573
  const latest = tools.findLast((tool) => tool.state === 'running');
563
574
 
564
575
  return (
565
576
  <div>
566
577
  <ToggleLine
567
- icon={latest !== undefined ? callIcon(latest, commandLooks) : look === undefined ? toolIcon(tools[0]!.name) : <look.Mark size={12} />}
578
+ icon={
579
+ latest !== undefined ? callIcon(latest, commandLooks, toolLooks) : look === undefined ? toolIcon(tools[0]!.name) : <look.Mark size={12} />
580
+ }
568
581
  label={
569
582
  <span className="tabular-nums">
570
583
  {running > 0 ? t('group.toolCalls', { count: tools.length }) : look?.brand === undefined ? summary : withBrand(summary, [look.brand])}
584
+ {failed > 0 && <span className="text-status-error">{` · ${t('group.failed', { count: failed })}`}</span>}
571
585
  </span>
572
586
  }
573
587
  open={expanded}
574
588
  onToggle={onToggle}
575
- failed={failed}
576
589
  live={latest !== undefined}
577
590
  trailing={running > 0 && <span className="shrink-0 text-text-faint tabular-nums">{t('group.running', { count: running })}</span>}
578
591
  time={tools[0]!.createdAt}
@@ -601,8 +614,8 @@ export function TurnFoldRow({
601
614
  expandable: boolean;
602
615
  onToggle(): void;
603
616
  }) {
604
- const { commandLooks } = useChatHost();
605
- const brands = commandLooks?.flatMap(({ brand }) => (brand === undefined ? [] : [brand])) ?? [];
617
+ const { commandLooks = [], toolLooks = [] } = useChatHost();
618
+ const brands = [...commandLooks, ...toolLooks].flatMap(({ brand }) => (brand === undefined ? [] : [brand]));
606
619
  const className = clsx(
607
620
  'group group/line -mx-1 mb-2 flex h-7 w-[calc(100%+8px)] min-w-0 items-center gap-2 rounded-md px-1 text-left text-xs text-text-muted',
608
621
  expandable && 'hover:bg-surface-hover',
@@ -906,13 +919,13 @@ function ProviderChangedFiles({
906
919
  /* "Working for 00:14", under the mark of the call running last; between calls, a spinner. */
907
920
  export function WorkingRow({ startedAt, tool }: { startedAt: number; tool?: ChatToolItem }) {
908
921
  const { t } = useTranslation('agent-chat');
909
- const { commandLooks } = useChatHost();
922
+ const { commandLooks, toolLooks } = useChatHost();
910
923
  const ref = useTickingText(() => formatClockDuration(Date.now() - startedAt));
911
924
 
912
925
  return (
913
926
  <div className="-mx-1 mb-0.5 flex h-7 items-center gap-2 px-1 text-xs text-text-muted">
914
927
  <span className={clsx(ROW_GUTTER, tool === undefined ? 'text-status-running' : 'text-accent')}>
915
- {tool === undefined ? <Spinner size={12} /> : callIcon(tool, commandLooks)}
928
+ {tool === undefined ? <Spinner size={12} /> : callIcon(tool, commandLooks, toolLooks)}
916
929
  </span>
917
930
  <span className="chat-live-text">{t('work.workingFor')}</span>
918
931
  <span ref={ref} className="tabular-nums" />