@open-document/core 0.13.3 → 0.14.1

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 (68) hide show
  1. package/dist/{build-CvsOak5B.js → build-I0cgoERP.js} +1 -1
  2. package/dist/{check-CbUBwYmf.js → check-Dys96QSL.js} +3 -3
  3. package/dist/cli/bin.js +10 -21
  4. package/dist/{config-BWtOZ1jc.d.ts → config-cAV1OCuY.d.ts} +9 -1
  5. package/dist/{context-CWgfsHoE.js → context-C-osJW5f.js} +1 -1
  6. package/dist/{dev-B8H2qT6G.js → dev-CC4PrIOC.js} +1 -1
  7. package/dist/{diff-BvIjjgnc.js → diff-DhutD4TJ.js} +2 -2
  8. package/dist/{export-DX30_s2q.js → export-KrajVJO7.js} +3 -3
  9. package/dist/{import-DgsdJT5e.js → import-Yv34za3L.js} +3 -2
  10. package/dist/index.d.ts +2 -2
  11. package/dist/ops/index.d.ts +3 -1
  12. package/dist/ops/index.js +2 -2
  13. package/dist/ops-Dux5HlkH.js +401 -0
  14. package/dist/{preview-DYMUNjXL.js → preview-7eqn9Wzf.js} +1 -1
  15. package/dist/rolldown-runtime-D7D4PA-g.js +13 -0
  16. package/dist/{config-CltJJVsw.js → session-BSbrC5gK.js} +3268 -385
  17. package/dist/{templates-B5Tz0AKb.js → templates-s-VLTSRc.js} +3 -3
  18. package/dist/vite/index.d.ts +1 -1
  19. package/dist/vite/index.js +1 -1
  20. package/package.json +1 -1
  21. package/skills/create-doc/SKILL.md +1 -1
  22. package/skills/doc-authoring/SKILL.md +6 -0
  23. package/src/app/app.tsx +2 -0
  24. package/src/app/components/agent/agent-dock.tsx +75 -0
  25. package/src/app/components/agent/agent-markdown.tsx +45 -0
  26. package/src/app/components/agent/agent-panel.tsx +695 -0
  27. package/src/app/components/agent/agent-presence.tsx +74 -0
  28. package/src/app/components/agent/composer.tsx +120 -0
  29. package/src/app/components/agent/connect-view.tsx +183 -0
  30. package/src/app/components/agent/history-view.tsx +134 -0
  31. package/src/app/components/agent/provider-logo.tsx +23 -0
  32. package/src/app/components/agent/transcript.tsx +717 -0
  33. package/src/app/components/comments/comments-panel.tsx +26 -2
  34. package/src/app/components/inspector/inspector.tsx +17 -0
  35. package/src/app/components/note-markdown.tsx +85 -0
  36. package/src/app/components/page-error-boundary.tsx +84 -0
  37. package/src/app/components/page-frame.tsx +2 -1
  38. package/src/app/components/panel/resize-handle.tsx +129 -0
  39. package/src/app/components/ui/button.tsx +97 -0
  40. package/src/app/components/ui/popover.tsx +144 -0
  41. package/src/app/components/ui/tooltip.tsx +123 -0
  42. package/src/app/lib/agent/use-agent.ts +543 -0
  43. package/src/app/lib/diagnostics.ts +13 -0
  44. package/src/app/lib/export-dom.ts +2 -1
  45. package/src/app/lib/export-pdf.ts +2 -1
  46. package/src/app/lib/i18n-ja/agent-panel.ts +115 -0
  47. package/src/app/lib/i18n-ja/agent.ts +9 -0
  48. package/src/app/lib/i18n-ja/index.ts +9 -1
  49. package/src/app/lib/i18n-ko/agent-panel.ts +114 -0
  50. package/src/app/lib/i18n-ko/agent.ts +9 -0
  51. package/src/app/lib/i18n-ko/index.ts +9 -1
  52. package/src/app/lib/i18n-zh-cn/agent-panel.ts +113 -0
  53. package/src/app/lib/i18n-zh-cn/agent.ts +9 -0
  54. package/src/app/lib/i18n-zh-cn/index.ts +9 -1
  55. package/src/app/lib/i18n-zh-tw/agent-panel.ts +113 -0
  56. package/src/app/lib/i18n-zh-tw/agent.ts +9 -0
  57. package/src/app/lib/i18n-zh-tw/index.ts +9 -1
  58. package/src/app/lib/note-markdown.ts +116 -0
  59. package/src/app/lib/toast.ts +50 -0
  60. package/src/app/lib/use-auto-grow.ts +15 -0
  61. package/src/app/lib/use-doc-titles.ts +28 -0
  62. package/src/app/lib/use-locale.ts +151 -0
  63. package/src/app/lib/utils.ts +4 -0
  64. package/src/app/routes/doc-preview.tsx +103 -0
  65. package/src/app/routes/doc.tsx +118 -11
  66. package/src/app/routes/home.tsx +43 -8
  67. package/src/app/styles.css +193 -0
  68. package/dist/ops-CSR5RlrE.js +0 -1673
@@ -0,0 +1,717 @@
1
+ import { Check, ChevronRight, CircleAlert, Copy, LoaderCircle, Pencil } from 'lucide-react';
2
+ import { type ReactNode, useEffect, useLayoutEffect, useRef, useState } from 'react';
3
+ import { Button } from '@/components/ui/button';
4
+ import { useAutoGrow } from '@/lib/use-auto-grow';
5
+ import { format, plural, useLocale } from '@/lib/use-locale';
6
+ import { cn } from '@/lib/utils';
7
+ import { checkFixPrompt, stripDocSlug } from '../../../agent/prompt';
8
+ import type { AgentItem, AgentQuestion, AgentSession, RespondPayload } from '../../../agent/types';
9
+ import { AgentMarkdown } from './agent-markdown';
10
+
11
+ export function Transcript({
12
+ session,
13
+ onRespond,
14
+ onEdit,
15
+ onFix,
16
+ }: {
17
+ session: AgentSession;
18
+ onRespond: (itemId: string, payload: RespondPayload) => Promise<void>;
19
+ onEdit: (itemId: string, text: string) => Promise<void>;
20
+ /** Sends a message asking the agent to fix what a check found. */
21
+ onFix: (text: string) => Promise<void>;
22
+ }) {
23
+ const scrollRef = useRef<HTMLDivElement>(null);
24
+ const stickRef = useRef(true);
25
+ const lastItem = session.items.at(-1);
26
+ const lastText = lastItem?.kind === 'assistant' ? lastItem.text.length : 0;
27
+ const running = session.status === 'running';
28
+ const replies = replyTexts(session.items, running);
29
+ // Only the newest check offers a fix; an older one was already acted on or
30
+ // describes a file that has changed since.
31
+ const lastCheckId = [...session.items].reverse().find((i) => i.kind === 'check')?.id;
32
+
33
+ // Follow the stream while the user is parked at the bottom; stop once they
34
+ // scroll up to read, like a terminal.
35
+ // `lastItem` too: an item can grow in place — a check that finishes turns a
36
+ // one-line "checking" into a card — without the transcript getting longer.
37
+ // biome-ignore lint/correctness/useExhaustiveDependencies: re-run as the transcript grows or streams
38
+ useLayoutEffect(() => {
39
+ const node = scrollRef.current;
40
+ if (!node || !stickRef.current) return;
41
+ node.scrollTop = node.scrollHeight;
42
+ }, [session.items.length, lastText, session.status, lastItem]);
43
+
44
+ return (
45
+ <div
46
+ ref={scrollRef}
47
+ onScroll={(e) => {
48
+ const node = e.currentTarget;
49
+ stickRef.current = node.scrollHeight - node.scrollTop - node.clientHeight < 48;
50
+ }}
51
+ className="min-h-0 flex-1 overflow-y-auto px-4 py-4"
52
+ >
53
+ <ol className="flex flex-col gap-3">
54
+ {toBlocks(session.items).map((block) => (
55
+ <li key={block.key}>
56
+ {'tools' in block ? (
57
+ <ToolGroup tools={block.tools} />
58
+ ) : (
59
+ <TranscriptItem
60
+ item={block.item}
61
+ running={running}
62
+ replyText={replies.get(block.item.id)}
63
+ onRespond={(payload) => onRespond(block.item.id, payload)}
64
+ onEdit={(text) => onEdit(block.item.id, text)}
65
+ onFix={block.item.id === lastCheckId && !running ? onFix : undefined}
66
+ />
67
+ )}
68
+ </li>
69
+ ))}
70
+ {running && !(lastItem?.kind === 'assistant' && lastItem.streaming) && (
71
+ <li>
72
+ <WorkingIndicator />
73
+ </li>
74
+ )}
75
+ </ol>
76
+ </div>
77
+ );
78
+ }
79
+
80
+ function WorkingIndicator() {
81
+ const t = useLocale();
82
+ return (
83
+ <div className="text-[11.5px]">
84
+ <span className="agent-shimmer">{t.agent.working}…</span>
85
+ </div>
86
+ );
87
+ }
88
+
89
+ // One reply spans every assistant text between a user message and the turn's
90
+ // result. Its copy button sits on the last of them and copies them all.
91
+ function replyTexts(items: AgentItem[], running: boolean): Map<string, string> {
92
+ const replies = new Map<string, string>();
93
+ let parts: { id: string; text: string }[] = [];
94
+ const flush = () => {
95
+ const last = parts.at(-1);
96
+ const text = parts
97
+ .map((p) => p.text.trim())
98
+ .filter(Boolean)
99
+ .join('\n\n');
100
+ if (last && text) replies.set(last.id, text);
101
+ parts = [];
102
+ };
103
+ for (const item of items) {
104
+ if (item.kind === 'assistant') parts.push({ id: item.id, text: stripDocSlug(item.text) });
105
+ else if (item.kind === 'user' || item.kind === 'result') flush();
106
+ }
107
+ if (!running) flush();
108
+ return replies;
109
+ }
110
+
111
+ function TranscriptItem({
112
+ item,
113
+ running,
114
+ replyText,
115
+ onRespond,
116
+ onEdit,
117
+ onFix,
118
+ }: {
119
+ item: AgentItem;
120
+ running: boolean;
121
+ replyText?: string;
122
+ onRespond: (payload: RespondPayload) => Promise<void>;
123
+ onEdit: (text: string) => Promise<void>;
124
+ onFix?: (text: string) => Promise<void>;
125
+ }) {
126
+ switch (item.kind) {
127
+ case 'user':
128
+ return <UserItem text={item.text} running={running} onEdit={onEdit} />;
129
+ case 'assistant':
130
+ if (!item.text && item.streaming) return <WorkingIndicator />;
131
+ return <AssistantItem text={stripDocSlug(item.text)} replyText={replyText} />;
132
+ case 'question':
133
+ return <QuestionItem item={item} onRespond={onRespond} />;
134
+ case 'permission':
135
+ return <PermissionItem item={item} onRespond={onRespond} />;
136
+ case 'error':
137
+ return <ErrorItem text={item.text} />;
138
+ case 'result':
139
+ return <ResultItem item={item} />;
140
+ case 'check':
141
+ return <CheckItem item={item} onFix={onFix} />;
142
+ default:
143
+ return null;
144
+ }
145
+ }
146
+
147
+ function ActionButton({
148
+ label,
149
+ onClick,
150
+ children,
151
+ }: {
152
+ label: string;
153
+ onClick: () => void;
154
+ children: ReactNode;
155
+ }) {
156
+ return (
157
+ <button
158
+ type="button"
159
+ aria-label={label}
160
+ title={label}
161
+ onClick={onClick}
162
+ className="grid size-6 place-items-center rounded-[5px] text-muted-foreground outline-none transition-colors duration-100 hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/30 [&_svg]:size-3"
163
+ >
164
+ {children}
165
+ </button>
166
+ );
167
+ }
168
+
169
+ function CopyButton({ text }: { text: string }) {
170
+ const t = useLocale();
171
+ const [copied, setCopied] = useState(false);
172
+ useEffect(() => {
173
+ if (!copied) return;
174
+ const timer = setTimeout(() => setCopied(false), 1500);
175
+ return () => clearTimeout(timer);
176
+ }, [copied]);
177
+ return (
178
+ <ActionButton
179
+ label={copied ? t.agent.copied : t.agent.copyMessage}
180
+ onClick={() => {
181
+ navigator.clipboard.writeText(text).then(
182
+ () => setCopied(true),
183
+ () => {},
184
+ );
185
+ }}
186
+ >
187
+ {copied ? <Check /> : <Copy />}
188
+ </ActionButton>
189
+ );
190
+ }
191
+
192
+ const ACTIONS_REVEAL =
193
+ 'flex opacity-0 transition-opacity duration-100 focus-within:opacity-100 group-hover/message:opacity-100 motion-reduce:transition-none';
194
+
195
+ function AssistantItem({ text, replyText }: { text: string; replyText?: string }) {
196
+ return (
197
+ <div className="group/message">
198
+ <AgentMarkdown text={text} />
199
+ {replyText && (
200
+ <div className={cn(ACTIONS_REVEAL, '-mb-2 -ml-1.5')}>
201
+ <CopyButton text={replyText} />
202
+ </div>
203
+ )}
204
+ </div>
205
+ );
206
+ }
207
+
208
+ function UserItem({
209
+ text,
210
+ running,
211
+ onEdit,
212
+ }: {
213
+ text: string;
214
+ running: boolean;
215
+ onEdit: (text: string) => Promise<void>;
216
+ }) {
217
+ const t = useLocale();
218
+ const [editing, setEditing] = useState(false);
219
+
220
+ if (editing) {
221
+ return (
222
+ <UserEditor
223
+ text={text}
224
+ running={running}
225
+ onCancel={() => setEditing(false)}
226
+ onSend={onEdit}
227
+ />
228
+ );
229
+ }
230
+
231
+ return (
232
+ <div className="group/message flex flex-col items-end">
233
+ <div className="max-w-[88%] whitespace-pre-wrap rounded-[8px] rounded-br-[3px] bg-muted px-3 py-2 text-[12.5px] leading-[1.5]">
234
+ {text}
235
+ </div>
236
+ <div className={cn(ACTIONS_REVEAL, '-mr-1.5 -mb-2 mt-0.5')}>
237
+ <CopyButton text={text} />
238
+ {!running && (
239
+ <ActionButton label={t.agent.editMessage} onClick={() => setEditing(true)}>
240
+ <Pencil />
241
+ </ActionButton>
242
+ )}
243
+ </div>
244
+ </div>
245
+ );
246
+ }
247
+
248
+ function UserEditor({
249
+ text,
250
+ running,
251
+ onCancel,
252
+ onSend,
253
+ }: {
254
+ text: string;
255
+ running: boolean;
256
+ onCancel: () => void;
257
+ onSend: (text: string) => Promise<void>;
258
+ }) {
259
+ const t = useLocale();
260
+ const [value, setValue] = useState(text);
261
+ const [busy, runBusy] = useBusy();
262
+ const ref = useRef<HTMLTextAreaElement>(null);
263
+ const canSend = !running && !busy && value.trim().length > 0 && value.trim() !== text.trim();
264
+
265
+ useAutoGrow(ref, value, 200);
266
+
267
+ useEffect(() => {
268
+ const node = ref.current;
269
+ if (!node) return;
270
+ node.focus();
271
+ node.setSelectionRange(node.value.length, node.value.length);
272
+ }, []);
273
+
274
+ const submit = async () => {
275
+ if (!canSend) return;
276
+ await runBusy(() => onSend(value.trim()));
277
+ };
278
+
279
+ return (
280
+ <div
281
+ data-agent-message-editor
282
+ className="rounded-[8px] border border-border bg-background shadow-edge transition-colors duration-100 focus-within:border-foreground/40 focus-within:ring-2 focus-within:ring-ring/30"
283
+ >
284
+ <textarea
285
+ ref={ref}
286
+ value={value}
287
+ rows={1}
288
+ aria-label={t.agent.editMessage}
289
+ onChange={(e) => setValue(e.target.value)}
290
+ onKeyDown={(e) => {
291
+ if (e.nativeEvent.isComposing) return;
292
+ if (e.key === 'Escape') {
293
+ e.preventDefault();
294
+ e.stopPropagation();
295
+ onCancel();
296
+ } else if (e.key === 'Enter' && !e.shiftKey) {
297
+ e.preventDefault();
298
+ void submit();
299
+ }
300
+ }}
301
+ className="w-full resize-none bg-transparent px-3 pt-2.5 text-[12.5px] leading-[1.5] outline-none"
302
+ />
303
+ <p className="px-3 pt-0.5 text-[11px] leading-snug text-muted-foreground">
304
+ {t.agent.editHint}
305
+ </p>
306
+ <div className="flex justify-end gap-1 px-2 pt-2 pb-2">
307
+ <Button size="xs" variant="ghost" disabled={busy} onClick={onCancel}>
308
+ {t.agent.cancelEdit}
309
+ </Button>
310
+ <Button size="xs" disabled={!canSend} onClick={() => void submit()}>
311
+ {t.agent.sendEdit}
312
+ </Button>
313
+ </div>
314
+ </div>
315
+ );
316
+ }
317
+
318
+ function useBusy() {
319
+ const [busy, setBusy] = useState(false);
320
+ const run = async (task: () => Promise<void>) => {
321
+ setBusy(true);
322
+ try {
323
+ await task();
324
+ } finally {
325
+ setBusy(false);
326
+ }
327
+ };
328
+ return [busy, run] as const;
329
+ }
330
+
331
+ type ToolEntry = Extract<AgentItem, { kind: 'tool' }>;
332
+
333
+ type Block = { key: string; item: AgentItem } | { key: string; tools: ToolEntry[] };
334
+
335
+ function toBlocks(items: AgentItem[]): Block[] {
336
+ const blocks: Block[] = [];
337
+ for (const item of items) {
338
+ const last = blocks.at(-1);
339
+ if (item.kind === 'tool' && last && 'tools' in last) last.tools.push(item);
340
+ else if (item.kind === 'tool') blocks.push({ key: item.id, tools: [item] });
341
+ else blocks.push({ key: item.id, item });
342
+ }
343
+ return blocks;
344
+ }
345
+
346
+ function ToolRow({ item }: { item: ToolEntry }) {
347
+ return (
348
+ <div className="flex min-w-0 items-center gap-2 text-[11.5px] leading-5 text-muted-foreground">
349
+ <span
350
+ aria-hidden
351
+ className={cn(
352
+ 'size-1 shrink-0 rounded-full',
353
+ item.status === 'running'
354
+ ? 'animate-pulse bg-foreground/70 motion-reduce:animate-none'
355
+ : item.status === 'error'
356
+ ? 'bg-destructive'
357
+ : 'bg-muted-foreground/40',
358
+ )}
359
+ />
360
+ <span
361
+ className={cn(
362
+ 'shrink-0',
363
+ item.status === 'running' && 'agent-shimmer',
364
+ item.status === 'error' && 'text-destructive',
365
+ )}
366
+ >
367
+ {item.title}
368
+ </span>
369
+ {item.detail && (
370
+ <span
371
+ className="min-w-0 truncate font-mono text-[10.5px] text-muted-foreground/70"
372
+ title={item.detail}
373
+ >
374
+ {item.detail}
375
+ </span>
376
+ )}
377
+ </div>
378
+ );
379
+ }
380
+
381
+ function ToolGroup({ tools }: { tools: ToolEntry[] }) {
382
+ const t = useLocale();
383
+ const [open, setOpen] = useState(false);
384
+ if (tools.length === 1) return <ToolRow item={tools[0]} />;
385
+ const live = [...tools].reverse().find((tool) => tool.status === 'running');
386
+ const failed = tools.some((tool) => tool.status === 'error');
387
+ return (
388
+ <div>
389
+ <button
390
+ type="button"
391
+ aria-expanded={open}
392
+ onClick={() => setOpen((v) => !v)}
393
+ className="flex items-center gap-1 rounded-[4px] text-[11.5px] leading-5 text-muted-foreground outline-none transition-colors duration-100 hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/30"
394
+ >
395
+ <ChevronRight
396
+ aria-hidden
397
+ className={cn(
398
+ 'size-3 transition-transform duration-150 motion-reduce:transition-none',
399
+ open && 'rotate-90',
400
+ )}
401
+ />
402
+ {format(plural(tools.length, t.agent.steps), { count: tools.length })}
403
+ {failed && <span aria-hidden className="ml-0.5 size-1 rounded-full bg-destructive" />}
404
+ </button>
405
+ {(open || live) && (
406
+ <div className="mt-0.5 ml-1.5 flex flex-col border-l border-hairline pl-3">
407
+ {open ? (
408
+ tools.map((tool) => <ToolRow key={tool.id} item={tool} />)
409
+ ) : live ? (
410
+ <ToolRow item={live} />
411
+ ) : null}
412
+ </div>
413
+ )}
414
+ </div>
415
+ );
416
+ }
417
+
418
+ function ErrorItem({ text }: { text: string }) {
419
+ const t = useLocale();
420
+ return (
421
+ <div className="rounded-[6px] border border-destructive/30 bg-destructive/5 px-3 py-2">
422
+ <span className="eyebrow flex items-center gap-1 text-destructive/80">
423
+ <CircleAlert className="size-3" aria-hidden />
424
+ {t.agent.errorEyebrow}
425
+ </span>
426
+ <pre className="mt-1 whitespace-pre-wrap font-mono text-[11px] leading-relaxed text-foreground/85">
427
+ {text}
428
+ </pre>
429
+ </div>
430
+ );
431
+ }
432
+
433
+ const CHECK_SHOWN = 5;
434
+
435
+ function CheckItem({
436
+ item,
437
+ onFix,
438
+ }: {
439
+ item: Extract<AgentItem, { kind: 'check' }>;
440
+ onFix?: (text: string) => Promise<void>;
441
+ }) {
442
+ const t = useLocale();
443
+ const [busy, runBusy] = useBusy();
444
+
445
+ if (item.status === 'running') {
446
+ return (
447
+ <p className="flex items-center gap-1.5 text-[11.5px] text-muted-foreground" data-agent-check>
448
+ <LoaderCircle aria-hidden className="size-3 animate-spin motion-reduce:animate-none" />
449
+ <span className="agent-shimmer">{t.agent.checkRunning}</span>
450
+ </p>
451
+ );
452
+ }
453
+ if (item.status === 'failed') {
454
+ return (
455
+ <p className="text-[11.5px] leading-relaxed text-muted-foreground" data-agent-check>
456
+ <span className="text-foreground/80">{t.agent.checkFailed}</span>
457
+ {item.error && item.error !== 'interrupted' && (
458
+ <span className="mt-0.5 block break-words font-mono text-[10.5px]">{item.error}</span>
459
+ )}
460
+ </p>
461
+ );
462
+ }
463
+ if (item.total === 0) {
464
+ return (
465
+ <p className="flex items-center gap-1.5 text-[11.5px] text-muted-foreground" data-agent-check>
466
+ <Check aria-hidden className="size-3" />
467
+ {item.pageCount !== null
468
+ ? format(t.agent.checkCleanPages, { count: item.pageCount })
469
+ : t.agent.checkClean}
470
+ </p>
471
+ );
472
+ }
473
+
474
+ const shown = item.findings.slice(0, CHECK_SHOWN);
475
+ const hidden = item.total - shown.length;
476
+ return (
477
+ <div
478
+ className="rounded-[8px] border border-hairline bg-background px-3 py-2.5"
479
+ data-agent-check
480
+ >
481
+ <p className="text-[11.5px] text-muted-foreground">
482
+ {format(plural(item.total, t.agent.checkFound), { count: item.total })}
483
+ </p>
484
+ <ul className="mt-1.5 flex flex-col gap-1">
485
+ {shown.map((f, n) => (
486
+ // biome-ignore lint/suspicious/noArrayIndexKey: a fixed report, never reordered
487
+ <li key={n} className="flex gap-1.5 text-[12px] leading-snug">
488
+ <span
489
+ aria-hidden
490
+ className={cn(
491
+ 'mt-[0.45em] size-1.5 shrink-0 rounded-full',
492
+ f.severity === 'error' ? 'bg-destructive' : 'bg-muted-foreground/50',
493
+ )}
494
+ />
495
+ <span className="min-w-0">
496
+ {f.page > 0 && (
497
+ <span className="mr-1 font-mono text-[10.5px] text-muted-foreground">
498
+ {format(t.agent.contextPage, { n: f.page })}
499
+ </span>
500
+ )}
501
+ {f.message}
502
+ </span>
503
+ </li>
504
+ ))}
505
+ </ul>
506
+ {hidden > 0 && (
507
+ <p className="mt-1 text-[11px] text-muted-foreground">
508
+ {format(t.agent.checkMore, { count: hidden })}
509
+ </p>
510
+ )}
511
+ {onFix && (
512
+ <div className="mt-2.5 flex justify-end">
513
+ <Button
514
+ size="xs"
515
+ disabled={busy}
516
+ onClick={() =>
517
+ runBusy(() => onFix(checkFixPrompt(item.docId, item.findings, item.total)))
518
+ }
519
+ >
520
+ {t.agent.checkFix}
521
+ </Button>
522
+ </div>
523
+ )}
524
+ </div>
525
+ );
526
+ }
527
+
528
+ function ResultItem({ item }: { item: Extract<AgentItem, { kind: 'result' }> }) {
529
+ const t = useLocale();
530
+ const parts = [item.aborted ? t.agent.resultStopped : t.agent.resultDone];
531
+ if (item.durationMs !== null) {
532
+ parts.push(
533
+ format(t.agent.resultSeconds, { seconds: Math.max(1, Math.round(item.durationMs / 1000)) }),
534
+ );
535
+ }
536
+ if (item.costUsd !== null && item.costUsd > 0) parts.push(`$${item.costUsd.toFixed(2)}`);
537
+ return <p className="folio text-muted-foreground/70">{parts.join(' · ')}</p>;
538
+ }
539
+
540
+ function PermissionItem({
541
+ item,
542
+ onRespond,
543
+ }: {
544
+ item: Extract<AgentItem, { kind: 'permission' }>;
545
+ onRespond: (payload: RespondPayload) => Promise<void>;
546
+ }) {
547
+ const t = useLocale();
548
+ const [busy, runBusy] = useBusy();
549
+ const decide = (allow: boolean) => runBusy(() => onRespond({ allow }));
550
+
551
+ if (item.status !== 'pending') {
552
+ return (
553
+ <p className="flex min-w-0 items-center gap-2 text-[11.5px] leading-5 text-muted-foreground">
554
+ <span
555
+ aria-hidden
556
+ className={cn(
557
+ 'size-1 shrink-0 rounded-full',
558
+ item.status === 'allowed' ? 'bg-muted-foreground/40' : 'bg-destructive',
559
+ )}
560
+ />
561
+ <span className="shrink-0">
562
+ {item.status === 'allowed' ? t.agent.allowed : t.agent.denied}
563
+ </span>
564
+ <span className="min-w-0 truncate text-foreground/70">{item.title}</span>
565
+ </p>
566
+ );
567
+ }
568
+
569
+ return (
570
+ <div className="rounded-[8px] border border-hairline bg-background px-3 py-2.5">
571
+ <p className="text-[11.5px] text-muted-foreground">{t.agent.permissionTitle}</p>
572
+ <p className="mt-0.5 text-[12.5px] font-medium leading-snug">{item.title}</p>
573
+ {item.detail && (
574
+ <pre className="mt-1.5 line-clamp-4 whitespace-pre-wrap break-all font-mono text-[10.5px] leading-relaxed text-muted-foreground">
575
+ {item.detail}
576
+ </pre>
577
+ )}
578
+ <div className="mt-2.5 flex justify-end gap-1">
579
+ <Button size="xs" variant="ghost" disabled={busy} onClick={() => decide(false)}>
580
+ {t.agent.deny}
581
+ </Button>
582
+ <Button size="xs" disabled={busy} onClick={() => decide(true)}>
583
+ {t.agent.allow}
584
+ </Button>
585
+ </div>
586
+ </div>
587
+ );
588
+ }
589
+
590
+ function QuestionItem({
591
+ item,
592
+ onRespond,
593
+ }: {
594
+ item: Extract<AgentItem, { kind: 'question' }>;
595
+ onRespond: (payload: RespondPayload) => Promise<void>;
596
+ }) {
597
+ const t = useLocale();
598
+ const [picked, setPicked] = useState<Record<string, string[]>>({});
599
+ const [other, setOther] = useState<Record<string, string>>({});
600
+ const [busy, runBusy] = useBusy();
601
+
602
+ const answerFor = (q: AgentQuestion): string | null => {
603
+ const custom = other[q.question]?.trim();
604
+ const chosen = picked[q.question] ?? [];
605
+ const parts = [...chosen, ...(custom ? [custom] : [])];
606
+ return parts.length ? parts.join(', ') : null;
607
+ };
608
+ const complete = item.questions.every((q) => answerFor(q) !== null);
609
+
610
+ const reply = (payload: RespondPayload) => runBusy(() => onRespond(payload));
611
+ const submit = () => {
612
+ const answers: Record<string, string> = {};
613
+ for (const q of item.questions) answers[q.question] = answerFor(q) ?? '';
614
+ void reply({ answers });
615
+ };
616
+ const toggle = (q: AgentQuestion, label: string) =>
617
+ setPicked((prev) => {
618
+ const current = prev[q.question] ?? [];
619
+ const on = current.includes(label);
620
+ const next = q.multiSelect
621
+ ? on
622
+ ? current.filter((l) => l !== label)
623
+ : [...current, label]
624
+ : on
625
+ ? []
626
+ : [label];
627
+ return { ...prev, [q.question]: next };
628
+ });
629
+
630
+ if (item.status !== 'pending') {
631
+ return (
632
+ <div className="flex flex-col gap-0.5">
633
+ {item.questions.map((q) => (
634
+ <p key={q.question} className="text-[11.5px] leading-5 text-muted-foreground">
635
+ <span>{q.header || q.question}</span>
636
+ <span aria-hidden className="mx-1.5">
637
+ ·
638
+ </span>
639
+ <span className="text-foreground/80">
640
+ {item.status === 'answered'
641
+ ? item.answers?.[q.question] || '—'
642
+ : t.agent.questionCancelled}
643
+ </span>
644
+ </p>
645
+ ))}
646
+ </div>
647
+ );
648
+ }
649
+
650
+ return (
651
+ <div className="rounded-[8px] border border-hairline bg-background px-1.5 pt-2.5 pb-2">
652
+ <div className="flex flex-col gap-3">
653
+ {item.questions.map((q) => (
654
+ <fieldset key={q.question} className="min-w-0">
655
+ <legend className="px-1.5 text-[12.5px] font-medium leading-snug">{q.question}</legend>
656
+ <div className="mt-1 flex flex-col">
657
+ {q.options.map((option) => {
658
+ const selected = (picked[q.question] ?? []).includes(option.label);
659
+ return (
660
+ <button
661
+ key={option.label}
662
+ type="button"
663
+ aria-pressed={selected}
664
+ onClick={() => toggle(q, option.label)}
665
+ className="flex items-start gap-2 rounded-[5px] px-1.5 py-1 text-left outline-none transition-colors duration-100 hover:bg-muted focus-visible:ring-2 focus-visible:ring-ring/30"
666
+ >
667
+ <span
668
+ aria-hidden
669
+ className={cn(
670
+ 'mt-[3px] grid size-3 shrink-0 place-items-center border transition-colors duration-100',
671
+ q.multiSelect ? 'rounded-[3px]' : 'rounded-full',
672
+ selected
673
+ ? 'border-foreground bg-foreground text-background'
674
+ : 'border-muted-foreground/40',
675
+ )}
676
+ >
677
+ {selected && <Check className="size-2" strokeWidth={3} />}
678
+ </span>
679
+ <span className="min-w-0">
680
+ <span className="block text-[12px] leading-snug">{option.label}</span>
681
+ {option.description && (
682
+ <span className="block text-[11px] leading-snug text-muted-foreground">
683
+ {option.description}
684
+ </span>
685
+ )}
686
+ </span>
687
+ </button>
688
+ );
689
+ })}
690
+ <input
691
+ value={other[q.question] ?? ''}
692
+ onChange={(e) => setOther((prev) => ({ ...prev, [q.question]: e.target.value }))}
693
+ onKeyDown={(e) => {
694
+ if (e.key === 'Enter' && !e.nativeEvent.isComposing && complete && !busy) {
695
+ e.preventDefault();
696
+ submit();
697
+ }
698
+ }}
699
+ placeholder={`${t.agent.questionOther}…`}
700
+ aria-label={t.agent.questionOtherPlaceholder}
701
+ className="mx-1.5 mt-1 h-7 border-b border-hairline bg-transparent text-[12px] outline-none placeholder:text-muted-foreground/60 focus-visible:border-foreground/40"
702
+ />
703
+ </div>
704
+ </fieldset>
705
+ ))}
706
+ </div>
707
+ <div className="mt-2.5 flex justify-end gap-1 px-1">
708
+ <Button size="xs" variant="ghost" disabled={busy} onClick={() => reply({ cancel: true })}>
709
+ {t.agent.questionSkip}
710
+ </Button>
711
+ <Button size="xs" disabled={busy || !complete} onClick={submit}>
712
+ {t.agent.questionSubmit}
713
+ </Button>
714
+ </div>
715
+ </div>
716
+ );
717
+ }