agent-canvas 0.1.44 → 0.2.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 (115) hide show
  1. package/README.md +14 -0
  2. package/bin/lib/commands/daemon.ts +3 -6
  3. package/bin/lib/commands/push.ts +1 -1
  4. package/bin/lib/daemon-lifecycle.ts +65 -72
  5. package/bin/lib/helpers.test.ts +1 -0
  6. package/bin/lib/helpers.ts +30 -74
  7. package/daemon/build.ts +37 -16
  8. package/daemon/client/App.tsx +27 -61
  9. package/daemon/client/CompareView.tsx +4 -4
  10. package/daemon/client/FileBrowser.tsx +4 -4
  11. package/daemon/client/FileViewer.tsx +4 -4
  12. package/daemon/client/OverviewView.tsx +2 -2
  13. package/daemon/client/RevisionSelect.tsx +1 -1
  14. package/daemon/client/canvas-runtime.ts +1 -0
  15. package/daemon/client/clientApi.ts +18 -0
  16. package/daemon/client/components/index.ts +3 -16
  17. package/daemon/client/styles.css +4 -457
  18. package/daemon/dist/annotations-runtime.js +1 -0
  19. package/daemon/dist/client.css +1 -1
  20. package/daemon/dist/client.js +76 -76
  21. package/daemon/dist/components.js +59 -59
  22. package/daemon/dist/index.html +1 -0
  23. package/daemon/dist/preact-compat.js +1 -1
  24. package/daemon/dist/runtime.js +1 -1
  25. package/daemon/src/components.ts +13 -0
  26. package/daemon/src/handlers/api.ts +13 -9
  27. package/daemon/src/handlers/files.ts +2 -3
  28. package/daemon/src/handlers/static.ts +1 -1
  29. package/daemon/src/handlers/uploads.ts +2 -3
  30. package/daemon/src/paths.ts +10 -24
  31. package/daemon/src/remote-feedback.test.ts +1 -1
  32. package/daemon/src/remote-feedback.ts +1 -1
  33. package/daemon/src/secrets-api.test.ts +2 -2
  34. package/daemon/src/server.ts +11 -10
  35. package/daemon/src/share-e2e.test.ts +1 -1
  36. package/daemon/src/share.test.ts +1 -1
  37. package/daemon/src/share.ts +1 -1
  38. package/daemon/tsconfig.json +1 -3
  39. package/package.json +11 -4
  40. package/.github/dark.png +0 -0
  41. package/.github/light.png +0 -0
  42. package/.github/workflows/ci.yml +0 -48
  43. package/.github/workflows/deploy-worker-prod.yml +0 -19
  44. package/.github/workflows/deploy-worker-stage.yml +0 -21
  45. package/.github/workflows/deploy-worker.yml +0 -72
  46. package/.github/workflows/release.yml +0 -36
  47. package/CLAUDE.md +0 -86
  48. package/bun.lock +0 -377
  49. package/daemon/client/AnnotationEditor.tsx +0 -315
  50. package/daemon/client/AnnotationProvider.tsx +0 -195
  51. package/daemon/client/AnnotationSidebar.tsx +0 -627
  52. package/daemon/client/FileIcon.tsx +0 -76
  53. package/daemon/client/PlanRenderer.tsx +0 -614
  54. package/daemon/client/Popover.tsx +0 -350
  55. package/daemon/client/RenderErrorContext.ts +0 -11
  56. package/daemon/client/ResponsePreview.tsx +0 -186
  57. package/daemon/client/SessionContext.tsx +0 -3
  58. package/daemon/client/annotationContext.ts +0 -114
  59. package/daemon/client/annotationDom.ts +0 -104
  60. package/daemon/client/annotationDraft.test.ts +0 -188
  61. package/daemon/client/annotationDraft.ts +0 -130
  62. package/daemon/client/compat/preact-all.ts +0 -4
  63. package/daemon/client/components/Callout.tsx +0 -26
  64. package/daemon/client/components/Checklist.tsx +0 -37
  65. package/daemon/client/components/Choice.tsx +0 -176
  66. package/daemon/client/components/CodeBlock.tsx +0 -35
  67. package/daemon/client/components/Diff.tsx +0 -65
  68. package/daemon/client/components/FilePreview.tsx +0 -88
  69. package/daemon/client/components/Image.tsx +0 -45
  70. package/daemon/client/components/Markdown.tsx +0 -44
  71. package/daemon/client/components/Mermaid.tsx +0 -565
  72. package/daemon/client/components/Note.tsx +0 -14
  73. package/daemon/client/components/Priority.tsx +0 -19
  74. package/daemon/client/components/ResponseNote.tsx +0 -19
  75. package/daemon/client/components/Section.tsx +0 -33
  76. package/daemon/client/components/Table.tsx +0 -35
  77. package/daemon/client/components/Task.tsx +0 -76
  78. package/daemon/client/components/UserInput.tsx +0 -174
  79. package/daemon/client/components/renderMarkdownChildren.tsx +0 -97
  80. package/daemon/client/generateMarkdown.ts +0 -273
  81. package/daemon/client/highlightRange.ts +0 -224
  82. package/daemon/client/popoverPosition.ts +0 -31
  83. package/daemon/client/runtime.ts +0 -123
  84. package/daemon/client/theme.css +0 -66
  85. package/daemon/client/useTextAnnotation.tsx +0 -188
  86. package/daemon/client/utils.ts +0 -13
  87. package/daemon/src/compiler.test.ts +0 -57
  88. package/daemon/src/compiler.ts +0 -360
  89. package/daemon/src/handlers/utils.ts +0 -14
  90. package/daemon/src/router.ts +0 -16
  91. package/daemon/src/session-secrets.test.ts +0 -83
  92. package/daemon/src/session.ts +0 -612
  93. package/daemon/src/watcher.ts +0 -71
  94. package/daemon/src/websocket.test.ts +0 -99
  95. package/daemon/src/websocket.ts +0 -211
  96. package/tsconfig.json +0 -14
  97. package/workers/canvas-share/README.md +0 -159
  98. package/workers/canvas-share/oblaka.ts +0 -56
  99. package/workers/canvas-share/package.json +0 -25
  100. package/workers/canvas-share/public/assets/.gitkeep +0 -0
  101. package/workers/canvas-share/public/assets/client.css +0 -2
  102. package/workers/canvas-share/public/assets/client.js +0 -91
  103. package/workers/canvas-share/public/assets/components.js +0 -65
  104. package/workers/canvas-share/public/assets/jsx-runtime.js +0 -3
  105. package/workers/canvas-share/public/assets/preact-compat.js +0 -1
  106. package/workers/canvas-share/public/assets/runtime.js +0 -1
  107. package/workers/canvas-share/public/index.html +0 -385
  108. package/workers/canvas-share/public/screenshot-dark.png +0 -0
  109. package/workers/canvas-share/public/screenshot-light.png +0 -0
  110. package/workers/canvas-share/src/index.test.ts +0 -209
  111. package/workers/canvas-share/src/index.ts +0 -518
  112. package/workers/canvas-share/src/types.ts +0 -119
  113. package/workers/canvas-share/src/util.ts +0 -156
  114. package/workers/canvas-share/src/validation.ts +0 -160
  115. package/workers/canvas-share/tsconfig.json +0 -16
@@ -1,350 +0,0 @@
1
- import React, { useEffect, useRef, useState, useCallback, useMemo, useContext } from "react";
2
- import { createPortal } from "react-dom";
3
- import { getPopoverPosition } from "./popoverPosition";
4
- import { AnnotationEditor, AttachButton } from "./AnnotationEditor";
5
- import { SessionContext } from "#canvas/runtime";
6
-
7
- interface PopoverProps {
8
- anchorEl: HTMLElement;
9
- scrollContainer?: HTMLElement | null;
10
- onClose: () => void;
11
- /** Selectors that should not trigger outside-click close */
12
- ignoreSelector?: string;
13
- zIndex?: string;
14
- padding?: string;
15
- width?: string;
16
- children: React.ReactNode;
17
- }
18
-
19
- export function Popover({ anchorEl, scrollContainer, onClose, ignoreSelector, zIndex = "50", padding = "12px", width = "280px", children }: PopoverProps) {
20
- const popRef = useRef<HTMLDivElement>(null);
21
- const onCloseRef = useRef(onClose);
22
- onCloseRef.current = onClose;
23
- const { style: posStyle, parent } = getPopoverPosition(anchorEl, scrollContainer);
24
-
25
- useEffect(() => {
26
- const timer = setTimeout(() => {
27
- const handler = (e: MouseEvent) => {
28
- if (popRef.current && !popRef.current.contains(e.target as Node)) {
29
- if (ignoreSelector && (e.target as HTMLElement).closest?.(ignoreSelector)) return;
30
- onCloseRef.current();
31
- document.removeEventListener("mousedown", handler);
32
- }
33
- };
34
- document.addEventListener("mousedown", handler);
35
- return () => document.removeEventListener("mousedown", handler);
36
- }, 0);
37
- return () => clearTimeout(timer);
38
- }, [ignoreSelector]);
39
-
40
- return createPortal(
41
- <div
42
- ref={popRef}
43
- style={{
44
- ...posStyle,
45
- zIndex,
46
- width,
47
- background: "var(--color-bg-elevated)",
48
- border: "1px solid var(--color-border-hover)",
49
- borderRadius: "8px",
50
- boxShadow: "0 4px 12px var(--color-shadow)",
51
- padding,
52
- fontFamily: "'Inter', sans-serif",
53
- }}
54
- >
55
- {children}
56
- </div>,
57
- parent,
58
- );
59
- }
60
-
61
- /* ── Create/Edit annotation popover ────────────────────────── */
62
-
63
- interface AnnotationPopoverProps {
64
- anchorEl: HTMLElement;
65
- scrollContainer?: HTMLElement | null;
66
- snippet: string;
67
- /** Max length for displayed snippet (default 100) */
68
- truncateAt?: number;
69
- /** Custom header instead of the default quoted snippet */
70
- header?: React.ReactNode;
71
- /** Initial note text (for edit mode) */
72
- initialNote?: string;
73
- /** Initial images (for edit mode) */
74
- initialImages?: string[];
75
- /** Called on confirm — passes note and images */
76
- onConfirm: (note: string, images: string[]) => void;
77
- /** Called on cancel / close without saving */
78
- onCancel: () => void;
79
- /** Called when annotation should be deleted (edit mode) */
80
- onDelete?: () => void;
81
- }
82
-
83
- const SUGGESTION_GROUPS: { label: string; items: string[] }[] = [
84
- { label: "Clarity", items: ["Unclear", "Why?", "Needs example", "Missing context"] },
85
- { label: "Scope", items: ["Overkill", "Out of scope", "Important", "Risky"] },
86
- { label: "Action", items: ["Simplify", "Rethink", "Expand on this", "Skip this"] },
87
- ];
88
- const ALL_SUGGESTIONS = SUGGESTION_GROUPS.flatMap((g) => g.items);
89
-
90
- export function AnnotationPopover({
91
- anchorEl, scrollContainer, snippet, truncateAt = 100, header,
92
- initialNote = "", initialImages, onConfirm, onCancel, onDelete,
93
- }: AnnotationPopoverProps) {
94
- const sessionId = useContext(SessionContext);
95
- const isEditMode = initialNote !== "" || !!onDelete;
96
- const [note, setNote] = useState(initialNote);
97
- const [images, setImages] = useState<string[]>(initialImages || []);
98
- const [selectedIdx, setSelectedIdx] = useState<number | null>(null);
99
- const [suppressed, setSuppressed] = useState(isEditMode);
100
- const [deleteHover, setDeleteHover] = useState(false);
101
- const textareaRef = useRef<HTMLTextAreaElement | null>(null);
102
- const openFilePickerRef = useRef<(() => void) | null>(null);
103
-
104
- const submit = useCallback(() => {
105
- if (note.trim() || images.length > 0) onConfirm(note.trim(), images);
106
- else if (isEditMode && onDelete) onDelete();
107
- else onCancel();
108
- }, [note, images, onConfirm, onCancel, isEditMode, onDelete]);
109
-
110
- const handleClose = useCallback(() => {
111
- if (isEditMode) {
112
- if (!note.trim() && images.length === 0 && onDelete) { onDelete(); onCancel(); return; }
113
- onConfirm(note, images);
114
- return;
115
- }
116
- onCancel();
117
- }, [note, images, isEditMode, onConfirm, onCancel, onDelete]);
118
-
119
- // Suggestions (only in create mode)
120
- const showSuggestions = !isEditMode;
121
- const matches = note.trim().length > 0
122
- ? ALL_SUGGESTIONS.filter((s) => s.toLowerCase().startsWith(note.trim().toLowerCase()))
123
- : ALL_SUGGESTIONS;
124
- const filtered = !showSuggestions || suppressed ? [] : matches.length === 1 && matches[0].toLowerCase() === note.trim().toLowerCase() ? [] : matches;
125
- const matchSet = new Set(filtered);
126
-
127
- const filteredGroups = SUGGESTION_GROUPS
128
- .map((g) => ({ label: g.label, items: g.items.filter((s) => matchSet.has(s)) }))
129
- .filter((g) => g.items.length > 0);
130
-
131
- const colItems = filteredGroups.map((g) => g.items);
132
- const flatFiltered = colItems.flat();
133
-
134
- const selectedCol = useMemo(() => {
135
- if (selectedIdx === null) return 0;
136
- let count = 0;
137
- for (let c = 0; c < colItems.length; c++) {
138
- if (selectedIdx < count + colItems[c].length) return c;
139
- count += colItems[c].length;
140
- }
141
- return 0;
142
- }, [selectedIdx, colItems]);
143
- const selectedRow = useMemo(() => {
144
- if (selectedIdx === null) return 0;
145
- let count = 0;
146
- for (let c = 0; c < selectedCol; c++) count += colItems[c].length;
147
- return selectedIdx - count;
148
- }, [selectedIdx, selectedCol, colItems]);
149
-
150
- const colRowToFlat = (col: number, row: number) => {
151
- let idx = 0;
152
- for (let c = 0; c < col; c++) idx += colItems[c].length;
153
- return idx + row;
154
- };
155
-
156
- const [prevNote, setPrevNote] = useState(note);
157
- if (note !== prevNote) {
158
- setPrevNote(note);
159
- setSelectedIdx(null);
160
- setSuppressed(isEditMode);
161
- }
162
-
163
- const applySuggestion = useCallback((s: string) => {
164
- setNote(s);
165
- }, []);
166
-
167
- const truncated = snippet.length > truncateAt ? snippet.slice(0, truncateAt) + "..." : snippet;
168
-
169
- const handleKeyDown = useCallback((e: React.KeyboardEvent<HTMLTextAreaElement>) => {
170
- if (flatFiltered.length > 0 && (e.key === "ArrowDown" || e.key === "ArrowUp" || e.key === "ArrowLeft" || e.key === "ArrowRight")) {
171
- e.preventDefault();
172
- if (selectedIdx === null) { setSelectedIdx(0); return; }
173
- if (e.key === "ArrowDown") {
174
- const newRow = Math.min(selectedRow + 1, colItems[selectedCol].length - 1);
175
- setSelectedIdx(colRowToFlat(selectedCol, newRow));
176
- } else if (e.key === "ArrowUp") {
177
- const newRow = Math.max(selectedRow - 1, 0);
178
- setSelectedIdx(colRowToFlat(selectedCol, newRow));
179
- } else if (e.key === "ArrowRight") {
180
- const newCol = Math.min(selectedCol + 1, colItems.length - 1);
181
- const newRow = Math.min(selectedRow, colItems[newCol].length - 1);
182
- setSelectedIdx(colRowToFlat(newCol, newRow));
183
- } else if (e.key === "ArrowLeft") {
184
- const newCol = Math.max(selectedCol - 1, 0);
185
- const newRow = Math.min(selectedRow, colItems[newCol].length - 1);
186
- setSelectedIdx(colRowToFlat(newCol, newRow));
187
- }
188
- return;
189
- }
190
- if (selectedIdx !== null && flatFiltered.length > 0) {
191
- if (e.key === "Tab") { e.preventDefault(); applySuggestion(flatFiltered[selectedIdx]); return; }
192
- if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); onConfirm(flatFiltered[selectedIdx], images); return; }
193
- }
194
- if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); submit(); return; }
195
- if (e.key === "Escape") {
196
- if (flatFiltered.length > 0 && !suppressed) { setSuppressed(true); }
197
- else if (isEditMode) { handleClose(); }
198
- else { onCancel(); }
199
- }
200
- }, [flatFiltered, selectedIdx, selectedRow, selectedCol, colItems, colRowToFlat, applySuggestion, onConfirm, images, submit, suppressed, isEditMode, handleClose, onCancel]);
201
-
202
- return (
203
- <Popover
204
- anchorEl={anchorEl}
205
- scrollContainer={scrollContainer}
206
- onClose={isEditMode ? handleClose : onCancel}
207
- ignoreSelector={isEditMode ? "[data-annotation-id]" : undefined}
208
- zIndex={isEditMode ? "60" : "50"}
209
- padding={isEditMode ? "10px 12px" : "12px"}
210
- width="420px"
211
- >
212
- {!isEditMode && (header ?? (
213
- <div style={{ fontSize: "11px", color: "var(--color-text-tertiary)", fontStyle: "italic", marginBottom: "8px", lineHeight: "1.4", display: "-webkit-box", WebkitLineClamp: 2, WebkitBoxOrient: "vertical", overflow: "hidden" }}>
214
- &ldquo;{truncated}&rdquo;
215
- </div>
216
- ))}
217
-
218
- <AnnotationEditor
219
- note={note}
220
- onNoteChange={setNote}
221
- images={images}
222
- onAddImage={(path) => setImages((prev) => [...prev, path])}
223
- onRemoveImage={(path) => setImages((prev) => prev.filter((p) => p !== path))}
224
- sessionId={sessionId}
225
- autoFocus
226
- autoResize={isEditMode}
227
- textareaRef={textareaRef}
228
- onKeyDown={handleKeyDown}
229
- attachButton="none"
230
- openFilePickerRef={openFilePickerRef}
231
- textareaStyle={isEditMode ? {
232
- width: "100%", background: "transparent", border: "none",
233
- color: "var(--color-text-primary)", fontFamily: "'Inter', sans-serif",
234
- fontSize: "13px", lineHeight: "1.5", resize: "none", outline: "none",
235
- overflow: "hidden",
236
- } : {
237
- width: "100%", minHeight: "60px", background: "transparent", border: "none",
238
- color: "var(--color-text-primary)", fontFamily: "'Inter', sans-serif",
239
- fontSize: "13px", lineHeight: "1.5", resize: "vertical", outline: "none",
240
- }}
241
- placeholder="Add your note..."
242
- />
243
-
244
- {/* Action buttons */}
245
- {/* Action buttons */}
246
- <div style={{ display: "flex", alignItems: "center", gap: "8px", marginTop: isEditMode ? "6px" : "8px" }}>
247
- <AttachButton onClick={() => openFilePickerRef.current?.()} size={14} />
248
- <div style={{ flex: 1 }} />
249
- {isEditMode ? (
250
- <button
251
- onClick={() => { if (onDelete) onDelete(); onCancel(); }}
252
- onMouseEnter={() => setDeleteHover(true)}
253
- onMouseLeave={() => setDeleteHover(false)}
254
- style={{ fontSize: "11px", color: deleteHover ? "var(--color-accent-red)" : "var(--color-text-tertiary)", background: "none", border: "none", cursor: "pointer", padding: "2px 0", fontFamily: "'Inter', sans-serif" }}
255
- >Delete</button>
256
- ) : (
257
- <>
258
- <button onClick={onCancel} style={{ fontSize: "11px", color: "var(--color-text-tertiary)", background: "none", border: "none", cursor: "pointer", padding: "4px 12px", fontFamily: "'Inter', sans-serif" }}>Cancel</button>
259
- <button onClick={submit} style={{ fontSize: "11px", fontWeight: 500, padding: "4px 12px", borderRadius: "6px", background: "var(--color-highlight-bg)", color: "var(--color-text-primary)", border: "1px solid var(--color-highlight-border)", cursor: "pointer", fontFamily: "'Inter', sans-serif" }}>Add</button>
260
- </>
261
- )}
262
- </div>
263
-
264
- {/* Suggestions (create mode only) */}
265
- {flatFiltered.length > 0 && (
266
- <div style={{ marginTop: "6px", borderTop: "1px solid var(--color-border-subtle)", paddingTop: "8px", display: "flex", gap: "4px" }}>
267
- {filteredGroups.map((group) => (
268
- <div key={group.label} style={{ flex: 1 }}>
269
- <div style={{ fontSize: "10px", fontWeight: 600, color: "var(--color-text-disabled)", textTransform: "uppercase", letterSpacing: "0.05em", padding: "2px 6px", marginBottom: "2px", fontFamily: "'Inter', sans-serif" }}>
270
- {group.label}
271
- </div>
272
- {group.items.map((s) => {
273
- const flatIdx = flatFiltered.indexOf(s);
274
- return (
275
- <div
276
- key={s}
277
- onMouseDown={(e) => { e.preventDefault(); e.stopPropagation(); applySuggestion(s); }}
278
- onMouseEnter={() => setSelectedIdx(flatIdx)}
279
- style={{
280
- padding: "3px 6px",
281
- fontSize: "12px",
282
- borderRadius: "4px",
283
- fontFamily: "'Inter', sans-serif",
284
- color: flatIdx === selectedIdx ? "var(--color-text-primary)" : "var(--color-text-secondary)",
285
- background: flatIdx === selectedIdx ? "var(--color-highlight-selected)" : "transparent",
286
- cursor: "pointer",
287
- }}
288
- >
289
- <span style={{ fontWeight: 500 }}>{s.slice(0, note.trim().length)}</span>{s.slice(note.trim().length)}
290
- </div>
291
- );
292
- })}
293
- </div>
294
- ))}
295
- </div>
296
- )}
297
- </Popover>
298
- );
299
- }
300
-
301
- /* ── Legacy exports for backwards compatibility ───────────── */
302
-
303
- interface AnnotationCreatePopoverProps {
304
- anchorEl: HTMLElement;
305
- scrollContainer?: HTMLElement | null;
306
- snippet: string;
307
- truncateAt?: number;
308
- header?: React.ReactNode;
309
- onAdd: (note: string, images?: string[]) => void;
310
- onCancel: () => void;
311
- }
312
-
313
- export function AnnotationCreatePopover({ anchorEl, scrollContainer, snippet, truncateAt, header, onAdd, onCancel }: AnnotationCreatePopoverProps) {
314
- return (
315
- <AnnotationPopover
316
- anchorEl={anchorEl}
317
- scrollContainer={scrollContainer}
318
- snippet={snippet}
319
- truncateAt={truncateAt}
320
- header={header}
321
- onConfirm={(note, images) => onAdd(note, images)}
322
- onCancel={onCancel}
323
- />
324
- );
325
- }
326
-
327
- interface AnnotationEditPopoverProps {
328
- anchorEl: HTMLElement;
329
- scrollContainer?: HTMLElement | null;
330
- initialNote: string;
331
- initialImages?: string[];
332
- onUpdate: (note: string, images: string[]) => void;
333
- onDelete: () => void;
334
- onClose: () => void;
335
- }
336
-
337
- export function AnnotationEditPopover({ anchorEl, scrollContainer, initialNote, initialImages, onUpdate, onDelete, onClose }: AnnotationEditPopoverProps) {
338
- return (
339
- <AnnotationPopover
340
- anchorEl={anchorEl}
341
- scrollContainer={scrollContainer}
342
- snippet=""
343
- initialNote={initialNote}
344
- initialImages={initialImages}
345
- onConfirm={(note, images) => { onUpdate(note, images); onClose(); }}
346
- onDelete={onDelete}
347
- onCancel={onClose}
348
- />
349
- );
350
- }
@@ -1,11 +0,0 @@
1
- import { createContext } from "react";
2
-
3
- export interface CanvasRenderError {
4
- revision: number;
5
- filename: string;
6
- message: string;
7
- stack?: string;
8
- componentStack?: string;
9
- }
10
-
11
- export const RenderErrorContext = createContext<(error: CanvasRenderError) => void>(() => {});
@@ -1,186 +0,0 @@
1
- import React, { useEffect, useState, useRef, useMemo } from "react";
2
- import { marked } from "marked";
3
- import { useAnnotations } from "./AnnotationProvider";
4
- import { generateMarkdown, getMissingRequiredLabels } from "./generateMarkdown";
5
-
6
- interface ResponsePreviewProps {
7
- open: boolean;
8
- onClose: () => void;
9
- onSubmit: (feedback: string) => void;
10
- includedRemoteIds: Set<string>;
11
- onToggleRemoteId: (id: string) => void;
12
- }
13
-
14
- export function ResponsePreview({ open, onClose, onSubmit, includedRemoteIds, onToggleRemoteId }: ResponsePreviewProps) {
15
- const { annotations, generalNote, responses, feedbackEntries } = useAnnotations();
16
- const [editedText, setEditedText] = useState("");
17
- const [manuallyEdited, setManuallyEdited] = useState(false);
18
- const [editMode, setEditMode] = useState(false);
19
- const [validationError, setValidationError] = useState<string | null>(null);
20
- const textareaRef = useRef<HTMLTextAreaElement>(null);
21
- const [prevOpen, setPrevOpen] = useState(false);
22
-
23
- const remoteAnnotations = useMemo(
24
- () => annotations.filter((a) => a.source === "remote"),
25
- [annotations],
26
- );
27
-
28
- if (open && !prevOpen) {
29
- setManuallyEdited(false);
30
- setEditMode(false);
31
- setValidationError(null);
32
- }
33
- if (!open && prevOpen) {
34
- setManuallyEdited(false);
35
- setEditMode(false);
36
- setValidationError(null);
37
- }
38
- if (open !== prevOpen) {
39
- setPrevOpen(open);
40
- }
41
-
42
- const generatedText = useMemo(
43
- () => generateMarkdown(annotations, generalNote, responses, feedbackEntries, includedRemoteIds),
44
- [annotations, generalNote, responses, feedbackEntries, includedRemoteIds],
45
- );
46
- const text = manuallyEdited ? editedText : generatedText;
47
-
48
- useEffect(() => {
49
- if (open && editMode && textareaRef.current) {
50
- textareaRef.current.focus();
51
- }
52
- }, [open, editMode]);
53
-
54
- useEffect(() => {
55
- if (!open) return;
56
- const handleKeyDown = (e: KeyboardEvent) => {
57
- if (e.key === "Escape") onClose();
58
- };
59
- window.addEventListener("keydown", handleKeyDown);
60
- return () => window.removeEventListener("keydown", handleKeyDown);
61
- }, [open, onClose]);
62
-
63
- if (!open) return null;
64
-
65
- const hasContent = text.trim().length > 0;
66
-
67
- return (
68
- <div className="fixed inset-0 z-50 bg-black/50 backdrop-blur-sm flex items-center justify-center p-8" onClick={onClose}>
69
- <div className="bg-bg-surface border border-border-medium rounded-xl shadow-lg w-full max-w-2xl flex flex-col" style={{ height: "80vh" }} onClick={(e) => e.stopPropagation()}>
70
- {/* Header */}
71
- <div className="flex items-center justify-between px-5 py-3 border-b border-border-subtle flex-shrink-0">
72
- <div className="flex items-center gap-3">
73
- <span className="text-[13px] font-medium text-text-primary font-body">Response preview</span>
74
- {manuallyEdited && (
75
- <button onClick={() => { setManuallyEdited(false); setEditMode(false); }} className="text-[11px] text-text-tertiary hover:text-text-secondary font-body">
76
- Reset
77
- </button>
78
- )}
79
- </div>
80
- <div className="flex items-center gap-2">
81
- {/* Mode toggle */}
82
- <div className="flex items-center bg-bg-base rounded-md p-0.5">
83
- <button
84
- onClick={() => setEditMode(false)}
85
- className={`px-2.5 py-1 rounded text-[11px] font-body font-medium transition-colors ${
86
- !editMode ? "bg-bg-elevated text-text-primary shadow-sm" : "text-text-tertiary hover:text-text-secondary"
87
- }`}
88
- >
89
- Preview
90
- </button>
91
- <button
92
- onClick={() => setEditMode(true)}
93
- className={`px-2.5 py-1 rounded text-[11px] font-body font-medium transition-colors ${
94
- editMode ? "bg-bg-elevated text-text-primary shadow-sm" : "text-text-tertiary hover:text-text-secondary"
95
- }`}
96
- >
97
- Edit
98
- </button>
99
- </div>
100
- <button onClick={onClose} className="text-text-tertiary hover:text-text-secondary w-7 h-7 flex items-center justify-center rounded-md hover:bg-border-subtle transition-colors">
101
- &#x2715;
102
- </button>
103
- </div>
104
- </div>
105
-
106
- {/* Content */}
107
- {editMode ? (
108
- <textarea
109
- ref={textareaRef}
110
- value={text}
111
- onChange={(e) => { setEditedText(e.target.value); setManuallyEdited(true); }}
112
- className="flex-1 w-full bg-bg-base p-5 text-[13px] font-mono text-text-code resize-none focus:outline-none leading-relaxed placeholder:text-text-disabled"
113
- placeholder="Your feedback will appear here..."
114
- />
115
- ) : (
116
- <div className="flex-1 overflow-y-auto p-5">
117
- <MarkdownPreview text={text} />
118
- </div>
119
- )}
120
-
121
- {/* Remote feedback summary */}
122
- {remoteAnnotations.length > 0 && (
123
- <div className="px-5 py-2 border-t border-border-subtle flex-shrink-0">
124
- <span className="text-[12px] text-text-secondary font-body">
125
- Reviewer feedback: {includedRemoteIds.size}/{remoteAnnotations.length} included
126
- </span>
127
- </div>
128
- )}
129
-
130
- {/* Actions */}
131
- {validationError && (
132
- <div className="px-5 py-2 text-[12px] text-accent-red font-body border-t border-border-subtle flex-shrink-0 flex items-center justify-between gap-2">
133
- <span>{validationError}</span>
134
- <button
135
- onClick={() => {
136
- setValidationError(null);
137
- onSubmit(text);
138
- }}
139
- className="text-[11px] text-text-tertiary hover:text-text-secondary font-body whitespace-nowrap underline"
140
- >
141
- Submit anyway
142
- </button>
143
- </div>
144
- )}
145
- <div className="flex justify-end gap-2 px-5 py-3 border-t border-border-subtle flex-shrink-0">
146
- <button
147
- onClick={onClose}
148
- className="px-4 py-2 rounded-lg text-[13px] font-body text-text-tertiary hover:text-text-secondary transition-colors"
149
- >
150
- Cancel
151
- </button>
152
- <button
153
- onClick={() => {
154
- const allMissing = getMissingRequiredLabels(responses, feedbackEntries);
155
- if (allMissing.length > 0) {
156
- setValidationError(`Please answer: ${allMissing.join(", ")}`);
157
- return;
158
- }
159
- setValidationError(null);
160
- onSubmit(text);
161
- }}
162
- disabled={!hasContent}
163
- className={`px-6 py-2 rounded-lg font-body text-[13px] font-medium transition-all ${
164
- hasContent
165
- ? "bg-btn-primary text-btn-primary-text hover:opacity-90"
166
- : "bg-bg-input text-text-disabled cursor-default"
167
- }`}
168
- >
169
- Submit feedback
170
- </button>
171
- </div>
172
- </div>
173
- </div>
174
- );
175
- }
176
-
177
- const UPLOADS_PATH_RE = /!\[([^\]]*)\]\((\/tmp\/agent-canvas\/uploads\/([^)]+))\)/g;
178
-
179
- function rewriteUploadPaths(md: string): string {
180
- return md.replace(UPLOADS_PATH_RE, (_match, alt, _fullPath, filename) => `![${alt}](/api/uploads/${filename})`);
181
- }
182
-
183
- export function MarkdownPreview({ text }: { text: string }) {
184
- const html = useMemo(() => marked.parse(rewriteUploadPaths(text), { async: false }) as string, [text]);
185
- return <div className="prose-canvas font-body text-body text-text-primary leading-relaxed" dangerouslySetInnerHTML={{ __html: html }} />;
186
- }
@@ -1,3 +0,0 @@
1
- import { createContext } from "react";
2
-
3
- export const SessionContext = createContext<string>("");
@@ -1,114 +0,0 @@
1
- import type { AnnotationContext } from "./AnnotationProvider";
2
-
3
- /**
4
- * Extract disambiguation context from a Range:
5
- * - surrounding text (before/after the selection)
6
- * - hierarchy path (Section title → Task label)
7
- */
8
- export function extractContext(range: Range, planContainer: HTMLElement): AnnotationContext {
9
- const before = getSurroundingText(range, "before", 60);
10
- const after = getSurroundingText(range, "after", 60);
11
- const hierarchy = getHierarchy(range.startContainer, planContainer);
12
-
13
- return { before, after, hierarchy };
14
- }
15
-
16
- function getSurroundingText(range: Range, direction: "before" | "after", maxChars: number): string {
17
- const block = getBlockParent(direction === "before" ? range.startContainer : range.endContainer);
18
- if (!block) return "";
19
-
20
- let text = "";
21
-
22
- if (direction === "before") {
23
- const walker = document.createTreeWalker(block, NodeFilter.SHOW_TEXT);
24
- let node: Text | null;
25
- while ((node = walker.nextNode() as Text | null)) {
26
- if (node === range.startContainer) {
27
- text += (node.textContent || "").slice(0, range.startOffset);
28
- break;
29
- }
30
- if (node.parentElement?.hasAttribute("data-annotation-id")) continue;
31
- text += node.textContent || "";
32
- }
33
- if (text.length > maxChars) {
34
- text = text.slice(-maxChars);
35
- const spaceIdx = text.indexOf(" ");
36
- if (spaceIdx > 0) text = text.slice(spaceIdx + 1);
37
- }
38
- } else {
39
- const walker = document.createTreeWalker(block, NodeFilter.SHOW_TEXT);
40
- let found = false;
41
- let node: Text | null;
42
- while ((node = walker.nextNode() as Text | null)) {
43
- if (node === range.endContainer) {
44
- text += (node.textContent || "").slice(range.endOffset);
45
- found = true;
46
- continue;
47
- }
48
- if (found) {
49
- if (node.parentElement?.hasAttribute("data-annotation-id")) continue;
50
- text += node.textContent || "";
51
- }
52
- }
53
- if (text.length > maxChars) {
54
- text = text.slice(0, maxChars);
55
- const spaceIdx = text.lastIndexOf(" ");
56
- if (spaceIdx > 0) text = text.slice(0, spaceIdx);
57
- }
58
- }
59
-
60
- return text.trim();
61
- }
62
-
63
- function getBlockParent(node: Node): Element | null {
64
- const blockTags = new Set(["DIV", "P", "LI", "PRE", "BLOCKQUOTE", "SECTION", "ARTICLE"]);
65
- let el = node.parentElement;
66
- while (el) {
67
- if (blockTags.has(el.tagName)) return el;
68
- el = el.parentElement;
69
- }
70
- return null;
71
- }
72
-
73
- function getHierarchy(node: Node, planContainer: HTMLElement): string[] {
74
- const path: string[] = [];
75
- let el = node instanceof Element ? node : node.parentElement;
76
-
77
- while (el && el !== planContainer) {
78
- const taskId = el.getAttribute?.("data-task-id");
79
- if (taskId) {
80
- const label = el.querySelector(".font-semibold");
81
- if (label) path.unshift(label.textContent?.trim() || taskId);
82
- }
83
-
84
- if (el.tagName === "DIV" && el.querySelector(":scope > button .font-heading, :scope > div .font-heading")) {
85
- const heading = el.querySelector(".font-heading");
86
- if (heading) path.unshift(heading.textContent?.trim() || "");
87
- }
88
-
89
- el = el.parentElement;
90
- }
91
-
92
- return path;
93
- }
94
-
95
- /**
96
- * Format a snippet with surrounding context for disambiguation.
97
- * Short snippets get before/after context.
98
- */
99
- export function formatSnippetInContext(ann: { snippet: string; context?: AnnotationContext }): string {
100
- const snippet = ann.snippet.trim();
101
- const ctx = ann.context;
102
-
103
- const isShort = snippet.length < 30;
104
-
105
- if (ctx && isShort && (ctx.before || ctx.after)) {
106
- const parts: string[] = [];
107
- if (ctx.before) parts.push(`...${ctx.before} `);
108
- parts.push(`**${snippet}**`);
109
- if (ctx.after) parts.push(` ${ctx.after}...`);
110
- return parts.join("");
111
- }
112
-
113
- return snippet;
114
- }