@useavalon/caelence-agent 0.2.2-canary.4 → 0.2.2-canary.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/desktop/src/App.tsx +116 -33
- package/desktop/src/ComposerAttach.tsx +2 -26
- package/desktop/src/FilesPanel.tsx +68 -0
- package/desktop/src/api.ts +6 -1
- package/desktop/src/composer-files.test.ts +2 -0
- package/desktop/src/composer-files.ts +10 -0
- package/desktop/src/user-display.test.ts +42 -0
- package/desktop/src/user-display.ts +20 -0
- package/package.json +1 -1
- package/src/cli/tui.tsx +40 -5
- package/src/core/approval.test.ts +22 -0
- package/src/core/approval.ts +20 -0
- package/src/desktop/bridge.ts +26 -6
- package/src/desktop/upload-message.test.ts +53 -0
- package/src/desktop/upload-message.ts +74 -0
- package/src/desktop/uploads.test.ts +34 -0
- package/src/desktop/uploads.ts +178 -13
- package/src/index.ts +1 -0
- package/src/init.test.ts +21 -0
- package/src/runtime.ts +6 -1
package/desktop/src/App.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ArrowUpRightIcon } from "@phosphor-icons/react/dist/csr/ArrowUpRight";
|
|
2
2
|
import { CaretDownIcon } from "@phosphor-icons/react/dist/csr/CaretDown";
|
|
3
3
|
import { CheckIcon } from "@phosphor-icons/react/dist/csr/Check";
|
|
4
|
+
import { FileIcon } from "@phosphor-icons/react/dist/csr/File";
|
|
4
5
|
import { FolderSimpleIcon } from "@phosphor-icons/react/dist/csr/FolderSimple";
|
|
5
6
|
import { GearIcon } from "@phosphor-icons/react/dist/csr/Gear";
|
|
6
7
|
import { MicrophoneIcon } from "@phosphor-icons/react/dist/csr/Microphone";
|
|
@@ -85,6 +86,7 @@ import {
|
|
|
85
86
|
isMicPermissionDenied,
|
|
86
87
|
pickRecorderMime,
|
|
87
88
|
} from "./dictation";
|
|
89
|
+
import { FilesPanel, homePath } from "./FilesPanel";
|
|
88
90
|
import { FloatNotice } from "./FloatNotice";
|
|
89
91
|
import { integrationMatches } from "./integration-search";
|
|
90
92
|
import { LogoMark } from "./LogoMark";
|
|
@@ -116,10 +118,11 @@ import {
|
|
|
116
118
|
thoughtTools,
|
|
117
119
|
} from "./thought";
|
|
118
120
|
import { eventCountSuffix } from "./tool-label";
|
|
121
|
+
import { joinUploadDisplay, userBubbleContent } from "./user-display";
|
|
119
122
|
|
|
120
123
|
type ThemeChoice = "system" | "light" | "dark";
|
|
121
124
|
type UiStatus = "boot" | "idle" | "running" | "approval" | "picker";
|
|
122
|
-
type MainView = "chat" | "settings" | "integrations" | "skills";
|
|
125
|
+
type MainView = "chat" | "settings" | "integrations" | "skills" | "files";
|
|
123
126
|
|
|
124
127
|
interface PickerState {
|
|
125
128
|
title: string;
|
|
@@ -141,6 +144,14 @@ interface PendingApproval {
|
|
|
141
144
|
input: Record<string, unknown>;
|
|
142
145
|
}
|
|
143
146
|
|
|
147
|
+
function isUploadEditPending(pending: PendingApproval): boolean {
|
|
148
|
+
return (
|
|
149
|
+
(pending.toolName === "edit_file" || pending.toolName === "write_file") &&
|
|
150
|
+
typeof pending.input.originalPath === "string" &&
|
|
151
|
+
pending.input.originalPath.trim().length > 0
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
144
155
|
function pickerSelectedId(kind: SlashPickerKind, current: DesktopState): string | undefined {
|
|
145
156
|
if (kind === "model") return current.modelId;
|
|
146
157
|
if (kind === "mode") return current.mode;
|
|
@@ -154,6 +165,7 @@ function mainViewTitle(view: MainView): string {
|
|
|
154
165
|
if (view === "settings") return "Settings";
|
|
155
166
|
if (view === "integrations") return "Integrations";
|
|
156
167
|
if (view === "skills") return "Skills";
|
|
168
|
+
if (view === "files") return "Files";
|
|
157
169
|
return "Chat";
|
|
158
170
|
}
|
|
159
171
|
|
|
@@ -277,10 +289,12 @@ function DeskSidePanel(
|
|
|
277
289
|
bridge: BridgeClient | null;
|
|
278
290
|
theme: ThemeChoice;
|
|
279
291
|
skillQuery: string;
|
|
292
|
+
uploads: Array<{ name: string; rel: string; sourcePath?: string }>;
|
|
280
293
|
onTheme: (theme: ThemeChoice) => void;
|
|
281
294
|
onSaved: (state: DesktopState) => void;
|
|
282
295
|
onNotice: (text: string) => void;
|
|
283
296
|
onSkillQuery: (value: string) => void;
|
|
297
|
+
onOpenFile: (path: string, reveal: boolean) => void;
|
|
284
298
|
}>,
|
|
285
299
|
): React.ReactElement | null {
|
|
286
300
|
if (!props.bridge) return null;
|
|
@@ -308,6 +322,9 @@ function DeskSidePanel(
|
|
|
308
322
|
/>
|
|
309
323
|
);
|
|
310
324
|
}
|
|
325
|
+
if (props.view === "files") {
|
|
326
|
+
return <FilesPanel items={props.uploads} onOpen={props.onOpenFile} />;
|
|
327
|
+
}
|
|
311
328
|
return null;
|
|
312
329
|
}
|
|
313
330
|
|
|
@@ -437,7 +454,11 @@ export function App(): React.ReactElement {
|
|
|
437
454
|
const [pending, setPending] = useState<PendingApproval | null>(null);
|
|
438
455
|
const [listening, setListening] = useState(false);
|
|
439
456
|
const [queue, setQueue] = useState<QueuedMessage[]>([]);
|
|
440
|
-
const [editingUser, setEditingUser] = useState<{
|
|
457
|
+
const [editingUser, setEditingUser] = useState<{
|
|
458
|
+
index: number;
|
|
459
|
+
text: string;
|
|
460
|
+
suffix?: string;
|
|
461
|
+
} | null>(null);
|
|
441
462
|
const [editingQueuedId, setEditingQueuedId] = useState<string | null>(null);
|
|
442
463
|
const [notice, setNotice] = useState<{ text: string; id: number } | null>(null);
|
|
443
464
|
const [attachments, setAttachments] = useState<ComposerAttachment[]>([]);
|
|
@@ -919,8 +940,8 @@ export function App(): React.ReactElement {
|
|
|
919
940
|
|
|
920
941
|
const commitEdit = useCallback(async () => {
|
|
921
942
|
if (!bridge || !editingUser) return;
|
|
922
|
-
const text = editingUser.text.
|
|
923
|
-
if (!text) return;
|
|
943
|
+
const text = joinUploadDisplay(editingUser.text, editingUser.suffix);
|
|
944
|
+
if (!text.trim()) return;
|
|
924
945
|
const index = editingUser.index;
|
|
925
946
|
setEditingUser(null);
|
|
926
947
|
skipDrainRef.current = true;
|
|
@@ -1118,7 +1139,7 @@ export function App(): React.ReactElement {
|
|
|
1118
1139
|
}, [renamingId, sessionMenu]);
|
|
1119
1140
|
|
|
1120
1141
|
const decide = useCallback(
|
|
1121
|
-
async (decision: "yes" | "no" | "always") => {
|
|
1142
|
+
async (decision: "yes" | "no" | "always" | "copy" | "original") => {
|
|
1122
1143
|
if (!bridge || !pending) return;
|
|
1123
1144
|
await approve(bridge, {
|
|
1124
1145
|
callId: pending.callId,
|
|
@@ -1419,6 +1440,15 @@ export function App(): React.ReactElement {
|
|
|
1419
1440
|
</ul>
|
|
1420
1441
|
)}
|
|
1421
1442
|
</section>
|
|
1443
|
+
<button
|
|
1444
|
+
type="button"
|
|
1445
|
+
className={`desk-settings-btn ${view === "files" ? "is-on" : ""}`}
|
|
1446
|
+
aria-label="Files"
|
|
1447
|
+
onClick={() => setView((current) => (current === "files" ? "chat" : "files"))}
|
|
1448
|
+
>
|
|
1449
|
+
<FileIcon size={16} weight="regular" />
|
|
1450
|
+
<span>Files</span>
|
|
1451
|
+
</button>
|
|
1422
1452
|
<button
|
|
1423
1453
|
type="button"
|
|
1424
1454
|
className={`desk-settings-btn ${view === "skills" ? "is-on" : ""}`}
|
|
@@ -1475,10 +1505,16 @@ export function App(): React.ReactElement {
|
|
|
1475
1505
|
bridge={bridge}
|
|
1476
1506
|
theme={theme}
|
|
1477
1507
|
skillQuery={skillQuery}
|
|
1508
|
+
uploads={state.uploads ?? []}
|
|
1478
1509
|
onTheme={setTheme}
|
|
1479
1510
|
onSaved={(next) => setState(next)}
|
|
1480
1511
|
onNotice={showNotice}
|
|
1481
1512
|
onSkillQuery={setSkillQuery}
|
|
1513
|
+
onOpenFile={(path, reveal) => {
|
|
1514
|
+
void openLocalFile(path, reveal, bridge).catch((err: unknown) => {
|
|
1515
|
+
noticeError(err);
|
|
1516
|
+
});
|
|
1517
|
+
}}
|
|
1482
1518
|
/>
|
|
1483
1519
|
) : (
|
|
1484
1520
|
<div className="desk-transcript" ref={transcriptRef}>
|
|
@@ -1510,7 +1546,14 @@ export function App(): React.ReactElement {
|
|
|
1510
1546
|
}
|
|
1511
1547
|
onStartEdit={
|
|
1512
1548
|
line.type === "user"
|
|
1513
|
-
? () =>
|
|
1549
|
+
? () => {
|
|
1550
|
+
const shown = userBubbleContent(line.text, line.attachments);
|
|
1551
|
+
setEditingUser({
|
|
1552
|
+
index: line.userTurnIndex,
|
|
1553
|
+
text: shown.text,
|
|
1554
|
+
suffix: shown.suffix,
|
|
1555
|
+
});
|
|
1556
|
+
}
|
|
1514
1557
|
: undefined
|
|
1515
1558
|
}
|
|
1516
1559
|
onEditChange={(text) =>
|
|
@@ -1541,31 +1584,70 @@ export function App(): React.ReactElement {
|
|
|
1541
1584
|
|
|
1542
1585
|
{view === "chat" && pending ? (
|
|
1543
1586
|
<section className="desk-approve" aria-label="Approval needed">
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1587
|
+
{isUploadEditPending(pending) ? (
|
|
1588
|
+
<>
|
|
1589
|
+
<p>Edit this file where?</p>
|
|
1590
|
+
<pre className="cel-code">
|
|
1591
|
+
{typeof pending.input.fileName === "string" ? pending.input.fileName : "File"}
|
|
1592
|
+
{"\n"}Copy:{" "}
|
|
1593
|
+
{typeof pending.input.copyPath === "string"
|
|
1594
|
+
? pending.input.copyPath
|
|
1595
|
+
: approvalPreview}
|
|
1596
|
+
{"\n"}Original: {homePath(String(pending.input.originalPath))}
|
|
1597
|
+
</pre>
|
|
1598
|
+
<div className="desk-approve-actions">
|
|
1599
|
+
<button
|
|
1600
|
+
type="button"
|
|
1601
|
+
className="cel-btn cel-btn--secondary cel-btn--compact"
|
|
1602
|
+
onClick={() => void decide("copy")}
|
|
1603
|
+
>
|
|
1604
|
+
Copy
|
|
1605
|
+
</button>
|
|
1606
|
+
<button
|
|
1607
|
+
type="button"
|
|
1608
|
+
className="cel-btn cel-btn--secondary cel-btn--compact"
|
|
1609
|
+
onClick={() => void decide("original")}
|
|
1610
|
+
>
|
|
1611
|
+
Original
|
|
1612
|
+
</button>
|
|
1613
|
+
<button
|
|
1614
|
+
type="button"
|
|
1615
|
+
className="cel-btn cel-btn--tertiary cel-btn--compact"
|
|
1616
|
+
onClick={() => void decide("no")}
|
|
1617
|
+
>
|
|
1618
|
+
Cancel
|
|
1619
|
+
</button>
|
|
1620
|
+
</div>
|
|
1621
|
+
</>
|
|
1622
|
+
) : (
|
|
1623
|
+
<>
|
|
1624
|
+
<p>Approval needed · {pending.toolName}</p>
|
|
1625
|
+
<pre className="cel-code">{approvalPreview}</pre>
|
|
1626
|
+
<div className="desk-approve-actions">
|
|
1627
|
+
<button
|
|
1628
|
+
type="button"
|
|
1629
|
+
className="cel-btn cel-btn--secondary cel-btn--compact"
|
|
1630
|
+
onClick={() => void decide("yes")}
|
|
1631
|
+
>
|
|
1632
|
+
Approve
|
|
1633
|
+
</button>
|
|
1634
|
+
<button
|
|
1635
|
+
type="button"
|
|
1636
|
+
className="cel-btn cel-btn--secondary cel-btn--compact"
|
|
1637
|
+
onClick={() => void decide("no")}
|
|
1638
|
+
>
|
|
1639
|
+
Deny
|
|
1640
|
+
</button>
|
|
1641
|
+
<button
|
|
1642
|
+
type="button"
|
|
1643
|
+
className="cel-btn cel-btn--tertiary cel-btn--compact"
|
|
1644
|
+
onClick={() => void decide("always")}
|
|
1645
|
+
>
|
|
1646
|
+
Always this session
|
|
1647
|
+
</button>
|
|
1648
|
+
</div>
|
|
1649
|
+
</>
|
|
1650
|
+
)}
|
|
1569
1651
|
</section>
|
|
1570
1652
|
) : null}
|
|
1571
1653
|
|
|
@@ -2360,6 +2442,7 @@ function UserTranscriptLine(
|
|
|
2360
2442
|
}>,
|
|
2361
2443
|
): React.ReactElement {
|
|
2362
2444
|
const editing = props.editing != null;
|
|
2445
|
+
const shown = userBubbleContent(props.line.text, props.line.attachments);
|
|
2363
2446
|
return (
|
|
2364
2447
|
<article className={`desk-line desk-line--user${editing ? " is-editing" : ""}`}>
|
|
2365
2448
|
<p className="desk-line__who">
|
|
@@ -2414,8 +2497,8 @@ function UserTranscriptLine(
|
|
|
2414
2497
|
</>
|
|
2415
2498
|
) : (
|
|
2416
2499
|
<>
|
|
2417
|
-
<AttachmentThumbs items={
|
|
2418
|
-
{
|
|
2500
|
+
<AttachmentThumbs items={shown.attachments} variant="chat" />
|
|
2501
|
+
{shown.text ? <p>{shown.text}</p> : null}
|
|
2419
2502
|
</>
|
|
2420
2503
|
)}
|
|
2421
2504
|
</div>
|
|
@@ -16,7 +16,7 @@ export function AttachmentThumbs(
|
|
|
16
16
|
<ul className="desk-attach-previews" aria-label="Attached files">
|
|
17
17
|
{props.items.map((item, index) => (
|
|
18
18
|
<li key={item.id ?? `${item.name}-${index}`} className="desk-attach-preview">
|
|
19
|
-
<ChatPreview name={item.name}
|
|
19
|
+
<ChatPreview name={item.name} />
|
|
20
20
|
</li>
|
|
21
21
|
))}
|
|
22
22
|
</ul>
|
|
@@ -43,31 +43,7 @@ export function AttachmentThumbs(
|
|
|
43
43
|
);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
function ChatPreview(
|
|
47
|
-
props: Readonly<{
|
|
48
|
-
name: string;
|
|
49
|
-
kind: ComposerFileKind;
|
|
50
|
-
previewUrl?: string;
|
|
51
|
-
}>,
|
|
52
|
-
): React.ReactElement {
|
|
53
|
-
if (props.kind === "image" && props.previewUrl) {
|
|
54
|
-
return (
|
|
55
|
-
<>
|
|
56
|
-
<img className="desk-preview desk-preview--img" src={props.previewUrl} alt={props.name} />
|
|
57
|
-
<p className="desk-line__caption">{props.name}</p>
|
|
58
|
-
</>
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
if (props.kind === "video" && props.previewUrl) {
|
|
62
|
-
return (
|
|
63
|
-
<>
|
|
64
|
-
<video className="desk-preview desk-preview--img" controls src={props.previewUrl}>
|
|
65
|
-
<track kind="captions" srcLang="en" label="Captions" src="data:text/vtt,WEBVTT" />
|
|
66
|
-
</video>
|
|
67
|
-
<p className="desk-line__caption">{props.name}</p>
|
|
68
|
-
</>
|
|
69
|
-
);
|
|
70
|
-
}
|
|
46
|
+
function ChatPreview(props: Readonly<{ name: string }>): React.ReactElement {
|
|
71
47
|
return (
|
|
72
48
|
<span className="desk-attach-file">
|
|
73
49
|
<FileGlyph size={16} weight="regular" aria-hidden="true" />
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { FolderSimpleIcon } from "@phosphor-icons/react/dist/csr/FolderSimple";
|
|
2
|
+
import type { ReactElement } from "react";
|
|
3
|
+
|
|
4
|
+
export interface UploadItem {
|
|
5
|
+
name: string;
|
|
6
|
+
rel: string;
|
|
7
|
+
sourcePath?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function homePath(path: string): string {
|
|
11
|
+
if (path.startsWith("/Users/")) {
|
|
12
|
+
const cut = path.indexOf("/", 7);
|
|
13
|
+
if (cut > 0) return `~${path.slice(cut)}`;
|
|
14
|
+
}
|
|
15
|
+
return path;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function FilesPanel(
|
|
19
|
+
props: Readonly<{
|
|
20
|
+
items: UploadItem[];
|
|
21
|
+
onOpen: (path: string, reveal: boolean) => void;
|
|
22
|
+
}>,
|
|
23
|
+
): ReactElement {
|
|
24
|
+
return (
|
|
25
|
+
<div className="desk-integrations">
|
|
26
|
+
<p className="desk-integrations-lead">Files you attached. Copies stay in this project.</p>
|
|
27
|
+
{props.items.length === 0 ? (
|
|
28
|
+
<p className="desk-side-empty">No uploaded files</p>
|
|
29
|
+
) : (
|
|
30
|
+
<ul className="desk-integration-list">
|
|
31
|
+
{props.items.map((item) => (
|
|
32
|
+
<li key={item.rel}>
|
|
33
|
+
<span className="desk-integration-glyph desk-integration-mark" aria-hidden="true">
|
|
34
|
+
{fileGlyph(item.name)}
|
|
35
|
+
</span>
|
|
36
|
+
<div>
|
|
37
|
+
<p>{item.name}</p>
|
|
38
|
+
<em>{item.sourcePath ? homePath(item.sourcePath) : "Copy in this project"}</em>
|
|
39
|
+
</div>
|
|
40
|
+
<div className="desk-integration-actions">
|
|
41
|
+
<button
|
|
42
|
+
type="button"
|
|
43
|
+
className="cel-btn cel-btn--secondary cel-btn--compact"
|
|
44
|
+
onClick={() => props.onOpen(item.rel, false)}
|
|
45
|
+
>
|
|
46
|
+
Open
|
|
47
|
+
</button>
|
|
48
|
+
<button
|
|
49
|
+
type="button"
|
|
50
|
+
className="cel-btn cel-btn--quiet cel-btn--compact"
|
|
51
|
+
aria-label={`Show ${item.name} in folder`}
|
|
52
|
+
onClick={() => props.onOpen(item.rel, true)}
|
|
53
|
+
>
|
|
54
|
+
<FolderSimpleIcon size={16} weight="regular" />
|
|
55
|
+
</button>
|
|
56
|
+
</div>
|
|
57
|
+
</li>
|
|
58
|
+
))}
|
|
59
|
+
</ul>
|
|
60
|
+
)}
|
|
61
|
+
</div>
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function fileGlyph(name: string): string {
|
|
66
|
+
const match = /\.([a-z0-9]{1,4})$/i.exec(name.trim());
|
|
67
|
+
return (match?.[1] ?? "file").slice(0, 4).toUpperCase();
|
|
68
|
+
}
|
package/desktop/src/api.ts
CHANGED
|
@@ -30,6 +30,7 @@ export interface DesktopState {
|
|
|
30
30
|
skills: Array<{ name: string; source: string }>;
|
|
31
31
|
busy: boolean;
|
|
32
32
|
hasApiKey: boolean;
|
|
33
|
+
uploads?: Array<{ name: string; rel: string; sourcePath?: string }>;
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
export interface PickerItem {
|
|
@@ -427,7 +428,11 @@ export function deleteSession(
|
|
|
427
428
|
|
|
428
429
|
export function approve(
|
|
429
430
|
bridge: BridgeClient,
|
|
430
|
-
body: {
|
|
431
|
+
body: {
|
|
432
|
+
callId: string;
|
|
433
|
+
decision: "yes" | "no" | "always" | "copy" | "original";
|
|
434
|
+
toolName?: string;
|
|
435
|
+
},
|
|
431
436
|
): Promise<{ ok: boolean }> {
|
|
432
437
|
return request(bridge, "/approve", { method: "POST", body: JSON.stringify(body) });
|
|
433
438
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
2
|
import {
|
|
3
3
|
attachmentKind,
|
|
4
|
+
createAttachment,
|
|
4
5
|
droppedPaths,
|
|
5
6
|
fileExtension,
|
|
6
7
|
filesFromTransfer,
|
|
@@ -32,6 +33,7 @@ describe("composer files", () => {
|
|
|
32
33
|
const transfer = { files: [file] } as unknown as DataTransfer;
|
|
33
34
|
expect(filesFromTransfer(transfer).map((item) => item.name)).toEqual(["a.txt"]);
|
|
34
35
|
expect(droppedPaths(transfer)).toEqual(["/tmp/a.txt"]);
|
|
36
|
+
expect(createAttachment(file).sourcePath).toBe("/tmp/a.txt");
|
|
35
37
|
});
|
|
36
38
|
|
|
37
39
|
test("merges unique files and rejects oversized extras", () => {
|
|
@@ -12,6 +12,7 @@ export interface ComposerAttachment {
|
|
|
12
12
|
size: number;
|
|
13
13
|
kind: ComposerFileKind;
|
|
14
14
|
previewUrl?: string;
|
|
15
|
+
sourcePath?: string;
|
|
15
16
|
file: File;
|
|
16
17
|
}
|
|
17
18
|
|
|
@@ -19,6 +20,7 @@ export interface TurnAttachmentPayload {
|
|
|
19
20
|
name: string;
|
|
20
21
|
mime: string;
|
|
21
22
|
data: string;
|
|
23
|
+
sourcePath?: string;
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
export interface AttachmentPreview {
|
|
@@ -62,6 +64,11 @@ export function droppedPaths(transfer: DataTransfer | null): string[] {
|
|
|
62
64
|
return paths;
|
|
63
65
|
}
|
|
64
66
|
|
|
67
|
+
export function nativeFilePath(file: File): string | undefined {
|
|
68
|
+
const path = (file as File & { path?: string }).path?.trim();
|
|
69
|
+
return path || undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
65
72
|
export function createAttachment(file: File): ComposerAttachment {
|
|
66
73
|
const mime = file.type || "application/octet-stream";
|
|
67
74
|
const kind = attachmentKind(file.name, mime);
|
|
@@ -72,6 +79,7 @@ export function createAttachment(file: File): ComposerAttachment {
|
|
|
72
79
|
typeof URL.createObjectURL === "function"
|
|
73
80
|
? URL.createObjectURL(file)
|
|
74
81
|
: undefined;
|
|
82
|
+
const sourcePath = nativeFilePath(file);
|
|
75
83
|
return {
|
|
76
84
|
id: `${file.name}-${file.size}-${file.lastModified}-${Math.random().toString(36).slice(2, 8)}`,
|
|
77
85
|
name: file.name || "file",
|
|
@@ -79,6 +87,7 @@ export function createAttachment(file: File): ComposerAttachment {
|
|
|
79
87
|
size: file.size,
|
|
80
88
|
kind,
|
|
81
89
|
...(previewUrl ? { previewUrl } : {}),
|
|
90
|
+
...(sourcePath ? { sourcePath } : {}),
|
|
82
91
|
file,
|
|
83
92
|
};
|
|
84
93
|
}
|
|
@@ -130,6 +139,7 @@ export async function serializeComposerAttachments(
|
|
|
130
139
|
name: item.name,
|
|
131
140
|
mime: item.mime,
|
|
132
141
|
data: await blobToBase64(item.file),
|
|
142
|
+
...(item.sourcePath ? { sourcePath: item.sourcePath } : {}),
|
|
133
143
|
})),
|
|
134
144
|
);
|
|
135
145
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { composeUploadMessage } from "../../src/desktop/upload-message.ts";
|
|
3
|
+
import { userBubbleContent } from "./user-display.ts";
|
|
4
|
+
|
|
5
|
+
describe("user bubble display", () => {
|
|
6
|
+
test("shows a document chip instead of the uploaded file text", () => {
|
|
7
|
+
const composed = composeUploadMessage("can you add a title to this doc", [
|
|
8
|
+
{
|
|
9
|
+
name: "zeugg-design-handover.md",
|
|
10
|
+
rel: ".harness/uploads/1-0-zeugg-design-handover.md",
|
|
11
|
+
mime: "text/markdown",
|
|
12
|
+
excerpt: "# Zeugg Design System — Handover\n\nDark, technical, precise.",
|
|
13
|
+
},
|
|
14
|
+
]);
|
|
15
|
+
const shown = userBubbleContent(composed);
|
|
16
|
+
expect(shown.text).toBe("can you add a title to this doc");
|
|
17
|
+
expect(shown.text).not.toContain("Zeugg Design System");
|
|
18
|
+
expect(shown.attachments).toEqual([
|
|
19
|
+
{
|
|
20
|
+
name: "zeugg-design-handover.md",
|
|
21
|
+
mime: "",
|
|
22
|
+
kind: "file",
|
|
23
|
+
},
|
|
24
|
+
]);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("keeps live composer chips and still strips a dumped excerpt", () => {
|
|
28
|
+
const composed = composeUploadMessage("edit this", [
|
|
29
|
+
{
|
|
30
|
+
name: "notes.md",
|
|
31
|
+
rel: ".harness/uploads/9-0-notes.md",
|
|
32
|
+
mime: "text/markdown",
|
|
33
|
+
excerpt: "lots of notes",
|
|
34
|
+
},
|
|
35
|
+
]);
|
|
36
|
+
const shown = userBubbleContent(composed, [
|
|
37
|
+
{ name: "notes.md", mime: "text/markdown", kind: "file" },
|
|
38
|
+
]);
|
|
39
|
+
expect(shown.text).toBe("edit this");
|
|
40
|
+
expect(shown.attachments).toEqual([{ name: "notes.md", mime: "text/markdown", kind: "file" }]);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { joinUploadDisplay, parseUploadDisplay } from "../../src/desktop/upload-message.ts";
|
|
2
|
+
import { type AttachmentPreview, attachmentKind } from "./composer-files.ts";
|
|
3
|
+
|
|
4
|
+
export function userBubbleContent(
|
|
5
|
+
text: string,
|
|
6
|
+
attachments: AttachmentPreview[] = [],
|
|
7
|
+
): { text: string; attachments: AttachmentPreview[]; suffix: string } {
|
|
8
|
+
const shown = parseUploadDisplay(text);
|
|
9
|
+
const chips =
|
|
10
|
+
attachments.length > 0
|
|
11
|
+
? attachments
|
|
12
|
+
: shown.attachments.map((file) => ({
|
|
13
|
+
name: file.name,
|
|
14
|
+
mime: file.mime ?? "",
|
|
15
|
+
kind: attachmentKind(file.name, file.mime ?? ""),
|
|
16
|
+
}));
|
|
17
|
+
return { text: shown.prompt, attachments: chips, suffix: shown.suffix };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { joinUploadDisplay };
|
package/package.json
CHANGED
package/src/cli/tui.tsx
CHANGED
|
@@ -3,6 +3,7 @@ import type React from "react";
|
|
|
3
3
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
4
4
|
import { resolveOpenRouter } from "../config.ts";
|
|
5
5
|
import type { ApprovalRequest } from "../core/approval.ts";
|
|
6
|
+
import { isUploadEditRequest } from "../core/approval.ts";
|
|
6
7
|
import { errorMessage } from "../core/errors.ts";
|
|
7
8
|
import type { AgentEvent } from "../core/events.ts";
|
|
8
9
|
import type { Session } from "../core/session.ts";
|
|
@@ -389,10 +390,15 @@ function SlashMenu(
|
|
|
389
390
|
}
|
|
390
391
|
|
|
391
392
|
function ApprovalCard(props: Readonly<{ theme: Theme; req: ApprovalRequest }>): React.ReactElement {
|
|
393
|
+
const upload = isUploadEditRequest(props.req);
|
|
392
394
|
const command =
|
|
393
395
|
typeof props.req.input.command === "string"
|
|
394
396
|
? props.req.input.command
|
|
395
397
|
: JSON.stringify(props.req.input);
|
|
398
|
+
const copyPath =
|
|
399
|
+
typeof props.req.input.copyPath === "string" ? props.req.input.copyPath : undefined;
|
|
400
|
+
const originalPath =
|
|
401
|
+
typeof props.req.input.originalPath === "string" ? props.req.input.originalPath : undefined;
|
|
396
402
|
return (
|
|
397
403
|
<Box
|
|
398
404
|
flexDirection="column"
|
|
@@ -401,9 +407,21 @@ function ApprovalCard(props: Readonly<{ theme: Theme; req: ApprovalRequest }>):
|
|
|
401
407
|
borderColor={props.theme.warning}
|
|
402
408
|
paddingX={1}
|
|
403
409
|
>
|
|
404
|
-
<Text color={props.theme.warning}>
|
|
405
|
-
|
|
406
|
-
|
|
410
|
+
<Text color={props.theme.warning}>
|
|
411
|
+
{upload ? "Edit this file where?" : `Approval needed · ${props.req.toolName}`}
|
|
412
|
+
</Text>
|
|
413
|
+
{upload ? (
|
|
414
|
+
<>
|
|
415
|
+
<Text color={props.theme.ink}>Copy: {copyPath}</Text>
|
|
416
|
+
<Text color={props.theme.ink}>Original: {originalPath}</Text>
|
|
417
|
+
<Text color={props.theme.muted}>c copy · o original · n cancel</Text>
|
|
418
|
+
</>
|
|
419
|
+
) : (
|
|
420
|
+
<>
|
|
421
|
+
<Text color={props.theme.ink}>{command}</Text>
|
|
422
|
+
<Text color={props.theme.muted}>y approve · n deny · a always this session</Text>
|
|
423
|
+
</>
|
|
424
|
+
)}
|
|
407
425
|
</Box>
|
|
408
426
|
);
|
|
409
427
|
}
|
|
@@ -418,7 +436,17 @@ function pickerSelectedValue(picker: SlashPickerKind, harness: HarnessRuntime):
|
|
|
418
436
|
return undefined;
|
|
419
437
|
}
|
|
420
438
|
|
|
421
|
-
function approvalDecision(
|
|
439
|
+
function approvalDecision(
|
|
440
|
+
input: string,
|
|
441
|
+
escaped: boolean,
|
|
442
|
+
upload: boolean,
|
|
443
|
+
): "yes" | "no" | "always" | "copy" | "original" | undefined {
|
|
444
|
+
if (upload) {
|
|
445
|
+
if (input === "c" || input === "C") return "copy";
|
|
446
|
+
if (input === "o" || input === "O") return "original";
|
|
447
|
+
if (input === "n" || input === "N" || escaped) return "no";
|
|
448
|
+
return undefined;
|
|
449
|
+
}
|
|
422
450
|
if (input === "y" || input === "Y") return "yes";
|
|
423
451
|
if (input === "a" || input === "A") return "always";
|
|
424
452
|
if (input === "n" || input === "N" || escaped) return "no";
|
|
@@ -664,9 +692,16 @@ function App(props: Readonly<{ harness: HarnessRuntime }>): React.ReactElement {
|
|
|
664
692
|
useInput(
|
|
665
693
|
(input, key) => {
|
|
666
694
|
if (status === "approval" && pending) {
|
|
667
|
-
const decision = approvalDecision(input, key.escape);
|
|
695
|
+
const decision = approvalDecision(input, key.escape, isUploadEditRequest(pending.req));
|
|
668
696
|
if (!decision) return;
|
|
669
697
|
if (decision === "always") harness.allowRiskyAlways(pending.req.toolName);
|
|
698
|
+
if (
|
|
699
|
+
decision === "original" &&
|
|
700
|
+
typeof pending.req.input.originalPath === "string" &&
|
|
701
|
+
pending.req.input.originalPath.trim()
|
|
702
|
+
) {
|
|
703
|
+
pending.req.input.path = pending.req.input.originalPath;
|
|
704
|
+
}
|
|
670
705
|
pending.resolve(decision !== "no");
|
|
671
706
|
setPending(null);
|
|
672
707
|
setStatus("running");
|
|
@@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test";
|
|
|
2
2
|
import {
|
|
3
3
|
createExecApprovalGate,
|
|
4
4
|
isLikelyDestructive,
|
|
5
|
+
isUploadEditRequest,
|
|
5
6
|
parseExecApprovalPolicy,
|
|
6
7
|
} from "../core/approval.ts";
|
|
7
8
|
|
|
@@ -99,4 +100,25 @@ describe("exec approval", () => {
|
|
|
99
100
|
await auto.request({ callId: "2", toolName: "edit_file", input: { path: "a.ts" } }),
|
|
100
101
|
).toBe(true);
|
|
101
102
|
});
|
|
103
|
+
|
|
104
|
+
test("asks where to edit an uploaded file with an original path", async () => {
|
|
105
|
+
const input: Record<string, unknown> = { path: ".harness/uploads/1-note.txt" };
|
|
106
|
+
const asked: string[] = [];
|
|
107
|
+
const gate = createExecApprovalGate({
|
|
108
|
+
policy: "prompt",
|
|
109
|
+
resolveUploadEdit: (path) =>
|
|
110
|
+
path.includes("uploads")
|
|
111
|
+
? { rel: path, sourcePath: "/tmp/note.txt", name: "note.txt" }
|
|
112
|
+
: undefined,
|
|
113
|
+
ask: async (req) => {
|
|
114
|
+
asked.push(req.toolName);
|
|
115
|
+
expect(isUploadEditRequest(req)).toBe(true);
|
|
116
|
+
req.input.path = req.input.originalPath;
|
|
117
|
+
return true;
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
expect(await gate.request({ callId: "1", toolName: "edit_file", input })).toBe(true);
|
|
121
|
+
expect(asked).toEqual(["edit_file"]);
|
|
122
|
+
expect(input.path).toBe("/tmp/note.txt");
|
|
123
|
+
});
|
|
102
124
|
});
|
package/src/core/approval.ts
CHANGED
|
@@ -20,6 +20,13 @@ export function isFileWriteTool(toolName: string): boolean {
|
|
|
20
20
|
return toolName === "edit_file" || toolName === "write_file";
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
export function isUploadEditRequest(req: ApprovalRequest): boolean {
|
|
24
|
+
return (
|
|
25
|
+
isFileWriteTool(req.toolName) &&
|
|
26
|
+
typeof req.input.originalPath === "string" &&
|
|
27
|
+
req.input.originalPath.trim().length > 0
|
|
28
|
+
);
|
|
29
|
+
}
|
|
23
30
|
/** Host-side denylist — local exec is the machine, not a sandbox. */
|
|
24
31
|
export const DESTRUCTIVE_COMMAND_PATTERNS: readonly RegExp[] = [
|
|
25
32
|
/\bsudo\b/,
|
|
@@ -57,6 +64,9 @@ export interface ExecApprovalGateDeps {
|
|
|
57
64
|
/** Optional event sink so the TUI can show the permission card. */
|
|
58
65
|
emit?: AgentEventEmitter;
|
|
59
66
|
alwaysAllow?: Set<string>;
|
|
67
|
+
resolveUploadEdit?: (
|
|
68
|
+
path: string,
|
|
69
|
+
) => { rel: string; sourcePath: string; name: string } | undefined;
|
|
60
70
|
}
|
|
61
71
|
|
|
62
72
|
async function promptAsk(deps: ExecApprovalGateDeps, req: ApprovalRequest): Promise<boolean> {
|
|
@@ -74,6 +84,16 @@ export function createExecApprovalGate(deps: ExecApprovalGateDeps): ApprovalGate
|
|
|
74
84
|
const always = deps.alwaysAllow ?? new Set<string>();
|
|
75
85
|
return {
|
|
76
86
|
async request(req) {
|
|
87
|
+
if (isFileWriteTool(req.toolName) && deps.resolveUploadEdit) {
|
|
88
|
+
const path = typeof req.input.path === "string" ? req.input.path : "";
|
|
89
|
+
const hit = deps.resolveUploadEdit(path);
|
|
90
|
+
if (hit) {
|
|
91
|
+
req.input.copyPath = hit.rel;
|
|
92
|
+
req.input.originalPath = hit.sourcePath;
|
|
93
|
+
req.input.fileName = hit.name;
|
|
94
|
+
return promptAsk(deps, req);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
77
97
|
if (isFileWriteTool(req.toolName)) {
|
|
78
98
|
if (always.has("write_file") || always.has("edit_file") || always.has(req.toolName)) {
|
|
79
99
|
return true;
|
package/src/desktop/bridge.ts
CHANGED
|
@@ -48,7 +48,7 @@ import {
|
|
|
48
48
|
import { openResolvedPath, resolveLocalPath } from "./open-local.ts";
|
|
49
49
|
import { readPreviewCache, writePreviewCache } from "./preview-cache.ts";
|
|
50
50
|
import { applyStoredOpenRouterKey, maskSecret, writeOpenRouterKey } from "./secrets.ts";
|
|
51
|
-
import { composeUploadMessage, parseIncomingUploads, saveUploads } from "./uploads.ts";
|
|
51
|
+
import { composeUploadMessage, listUploads, parseIncomingUploads, saveUploads } from "./uploads.ts";
|
|
52
52
|
|
|
53
53
|
export interface DesktopState {
|
|
54
54
|
name: string;
|
|
@@ -69,6 +69,7 @@ export interface DesktopState {
|
|
|
69
69
|
skills: Array<{ name: string; source: string }>;
|
|
70
70
|
busy: boolean;
|
|
71
71
|
hasApiKey: boolean;
|
|
72
|
+
uploads?: Array<{ name: string; rel: string; sourcePath?: string }>;
|
|
72
73
|
}
|
|
73
74
|
|
|
74
75
|
export interface BridgeReady {
|
|
@@ -255,6 +256,11 @@ function snapshot(harness: HarnessRuntime, busy: boolean, title: string): Deskto
|
|
|
255
256
|
skills: harness.skills.map((skill) => ({ name: skill.name, source: skill.source })),
|
|
256
257
|
busy,
|
|
257
258
|
hasApiKey: harness.hasApiKey,
|
|
259
|
+
uploads: listUploads(harness.cwd).map((item) => ({
|
|
260
|
+
name: item.name,
|
|
261
|
+
rel: item.rel,
|
|
262
|
+
...(item.sourcePath ? { sourcePath: item.sourcePath } : {}),
|
|
263
|
+
})),
|
|
258
264
|
};
|
|
259
265
|
}
|
|
260
266
|
|
|
@@ -278,7 +284,7 @@ export async function startDesktopBridge(options: StartDesktopBridgeOptions): Pr
|
|
|
278
284
|
let title = "New chat";
|
|
279
285
|
let busy = false;
|
|
280
286
|
let abort: AbortController | undefined;
|
|
281
|
-
const pending = new Map<string, (
|
|
287
|
+
const pending = new Map<string, (decision: string) => void>();
|
|
282
288
|
const oauthWaiters = new Map<string, (result: OAuthCallback) => void>();
|
|
283
289
|
|
|
284
290
|
const refreshLiveNames = (): void => {
|
|
@@ -692,13 +698,13 @@ export async function startDesktopBridge(options: StartDesktopBridgeOptions): Pr
|
|
|
692
698
|
if (decision === "always")
|
|
693
699
|
harness.allowRiskyAlways(typeof body.toolName === "string" ? body.toolName : "exec");
|
|
694
700
|
pending.delete(callId);
|
|
695
|
-
resolve(decision === "
|
|
701
|
+
resolve(typeof decision === "string" ? decision : "no");
|
|
696
702
|
return json({ ok: true });
|
|
697
703
|
};
|
|
698
704
|
|
|
699
705
|
const route_post_abort = async (_req: Request, _url: URL): Promise<Response> => {
|
|
700
706
|
abort?.abort();
|
|
701
|
-
for (const resolve of pending.values()) resolve(
|
|
707
|
+
for (const resolve of pending.values()) resolve("no");
|
|
702
708
|
pending.clear();
|
|
703
709
|
return json({ ok: true });
|
|
704
710
|
};
|
|
@@ -767,7 +773,21 @@ export async function startDesktopBridge(options: StartDesktopBridgeOptions): Pr
|
|
|
767
773
|
signal: abort?.signal,
|
|
768
774
|
approvalAsk: (req: ApprovalRequest) =>
|
|
769
775
|
new Promise<boolean>((resolve) => {
|
|
770
|
-
pending.set(req.callId,
|
|
776
|
+
pending.set(req.callId, (decision) => {
|
|
777
|
+
if (
|
|
778
|
+
decision === "original" &&
|
|
779
|
+
typeof req.input.originalPath === "string" &&
|
|
780
|
+
req.input.originalPath.trim()
|
|
781
|
+
) {
|
|
782
|
+
req.input.path = req.input.originalPath;
|
|
783
|
+
}
|
|
784
|
+
resolve(
|
|
785
|
+
decision === "yes" ||
|
|
786
|
+
decision === "always" ||
|
|
787
|
+
decision === "copy" ||
|
|
788
|
+
decision === "original",
|
|
789
|
+
);
|
|
790
|
+
});
|
|
771
791
|
}),
|
|
772
792
|
...(editUserTurn !== undefined ? { editUserTurn } : {}),
|
|
773
793
|
})
|
|
@@ -872,7 +892,7 @@ export async function startDesktopBridge(options: StartDesktopBridgeOptions): Pr
|
|
|
872
892
|
ready,
|
|
873
893
|
stop: async () => {
|
|
874
894
|
abort?.abort();
|
|
875
|
-
for (const resolve of pending.values()) resolve(
|
|
895
|
+
for (const resolve of pending.values()) resolve("no");
|
|
876
896
|
pending.clear();
|
|
877
897
|
for (const resolve of oauthWaiters.values()) resolve({ error: "cancelled" });
|
|
878
898
|
oauthWaiters.clear();
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { composeUploadMessage, joinUploadDisplay, parseUploadDisplay } from "./upload-message.ts";
|
|
3
|
+
|
|
4
|
+
describe("upload message display", () => {
|
|
5
|
+
test("strips an excerpted text file from the visible prompt", () => {
|
|
6
|
+
const composed = composeUploadMessage("can you add a title to this doc", [
|
|
7
|
+
{
|
|
8
|
+
name: "zeugg-design-handover.md",
|
|
9
|
+
rel: ".harness/uploads/1-0-zeugg-design-handover.md",
|
|
10
|
+
mime: "text/markdown",
|
|
11
|
+
excerpt: "# Zeugg Design System\n\nDark, technical, precise.",
|
|
12
|
+
original: "~/Downloads/zeugg-design-handover.md",
|
|
13
|
+
},
|
|
14
|
+
]);
|
|
15
|
+
expect(composed).toContain("# Zeugg Design System");
|
|
16
|
+
const shown = parseUploadDisplay(composed);
|
|
17
|
+
expect(shown.prompt).toBe("can you add a title to this doc");
|
|
18
|
+
expect(shown.attachments).toEqual([
|
|
19
|
+
{
|
|
20
|
+
name: "zeugg-design-handover.md",
|
|
21
|
+
rel: ".harness/uploads/1-0-zeugg-design-handover.md",
|
|
22
|
+
},
|
|
23
|
+
]);
|
|
24
|
+
expect(shown.suffix).toContain("Attached `");
|
|
25
|
+
expect(joinUploadDisplay("add a title", shown.suffix)).toContain("add a title");
|
|
26
|
+
expect(joinUploadDisplay("add a title", shown.suffix)).toContain("# Zeugg Design System");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("keeps a plain prompt unchanged", () => {
|
|
30
|
+
expect(parseUploadDisplay("just a question")).toEqual({
|
|
31
|
+
prompt: "just a question",
|
|
32
|
+
attachments: [],
|
|
33
|
+
suffix: "",
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("parses a binary attachment without an excerpt", () => {
|
|
38
|
+
const composed = composeUploadMessage("", [
|
|
39
|
+
{
|
|
40
|
+
name: "shot.png",
|
|
41
|
+
rel: ".harness/uploads/2-0-shot.png",
|
|
42
|
+
mime: "image/png",
|
|
43
|
+
},
|
|
44
|
+
]);
|
|
45
|
+
expect(parseUploadDisplay(composed).attachments).toEqual([
|
|
46
|
+
{
|
|
47
|
+
name: "shot.png",
|
|
48
|
+
rel: ".harness/uploads/2-0-shot.png",
|
|
49
|
+
mime: "image/png",
|
|
50
|
+
},
|
|
51
|
+
]);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export interface UploadMessageFile {
|
|
2
|
+
name: string;
|
|
3
|
+
rel: string;
|
|
4
|
+
mime: string;
|
|
5
|
+
excerpt?: string;
|
|
6
|
+
original?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ParsedUploadAttachment {
|
|
10
|
+
name: string;
|
|
11
|
+
rel: string;
|
|
12
|
+
mime?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const ATTACHED_MARK = /^Attached `(\.harness\/uploads\/[^`]+)` /m;
|
|
16
|
+
const ATTACHED_BLOCK =
|
|
17
|
+
/^Attached `(\.harness\/uploads\/[^`]+)` \(([^)]+)\)\.(?: Original: `([^`]+)`\.)?(?: Use that project path\.)?(?:\n\n```\n[\s\S]*?\n```)?/;
|
|
18
|
+
|
|
19
|
+
export function composeUploadMessage(text: string, files: UploadMessageFile[]): string {
|
|
20
|
+
if (files.length === 0) return text;
|
|
21
|
+
const blocks = files.map((file) => {
|
|
22
|
+
const original = file.original ? ` Original: \`${file.original}\`.` : "";
|
|
23
|
+
if (file.excerpt !== undefined) {
|
|
24
|
+
return `Attached \`${file.rel}\` (${file.name}).${original}\n\n\`\`\`\n${file.excerpt}\n\`\`\``;
|
|
25
|
+
}
|
|
26
|
+
return `Attached \`${file.rel}\` (${file.name}, ${file.mime}).${original} Use that project path.`;
|
|
27
|
+
});
|
|
28
|
+
const body = text.trim();
|
|
29
|
+
return body ? `${body}\n\n${blocks.join("\n\n")}` : blocks.join("\n\n");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function parseUploadDisplay(text: string): {
|
|
33
|
+
prompt: string;
|
|
34
|
+
attachments: ParsedUploadAttachment[];
|
|
35
|
+
suffix: string;
|
|
36
|
+
} {
|
|
37
|
+
const match = ATTACHED_MARK.exec(text);
|
|
38
|
+
if (!match || match.index === undefined) {
|
|
39
|
+
return { prompt: text, attachments: [], suffix: "" };
|
|
40
|
+
}
|
|
41
|
+
const prompt = text.slice(0, match.index).trim();
|
|
42
|
+
const suffix = text.slice(match.index).trim();
|
|
43
|
+
return { prompt, attachments: parseAttachedBlocks(suffix), suffix };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function joinUploadDisplay(prompt: string, suffix?: string): string {
|
|
47
|
+
const body = prompt.trim();
|
|
48
|
+
const extra = suffix?.trim() ?? "";
|
|
49
|
+
if (!extra) return body;
|
|
50
|
+
return body ? `${body}\n\n${extra}` : extra;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function parseAttachedBlocks(suffix: string): ParsedUploadAttachment[] {
|
|
54
|
+
const attachments: ParsedUploadAttachment[] = [];
|
|
55
|
+
let rest = suffix.trim();
|
|
56
|
+
while (rest) {
|
|
57
|
+
const match = ATTACHED_BLOCK.exec(rest);
|
|
58
|
+
if (match == null) break;
|
|
59
|
+
if (match.index !== 0) break;
|
|
60
|
+
const rel = match[1] ?? "";
|
|
61
|
+
const paren = match[2] ?? "";
|
|
62
|
+
attachments.push({ rel, ...splitNameMime(paren) });
|
|
63
|
+
rest = rest.slice(match[0].length).trimStart();
|
|
64
|
+
}
|
|
65
|
+
return attachments;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function splitNameMime(paren: string): { name: string; mime?: string } {
|
|
69
|
+
const cut = paren.lastIndexOf(", ");
|
|
70
|
+
if (cut > 0 && paren.slice(cut + 2).includes("/")) {
|
|
71
|
+
return { name: paren.slice(0, cut), mime: paren.slice(cut + 2) };
|
|
72
|
+
}
|
|
73
|
+
return { name: paren };
|
|
74
|
+
}
|
|
@@ -5,6 +5,8 @@ import { join } from "node:path";
|
|
|
5
5
|
import {
|
|
6
6
|
composeUploadMessage,
|
|
7
7
|
isTextUpload,
|
|
8
|
+
listUploads,
|
|
9
|
+
matchUploadEdit,
|
|
8
10
|
parseIncomingUploads,
|
|
9
11
|
safeUploadName,
|
|
10
12
|
saveUploads,
|
|
@@ -20,6 +22,11 @@ describe("uploads", () => {
|
|
|
20
22
|
"nope",
|
|
21
23
|
]),
|
|
22
24
|
).toEqual([{ name: "a.txt", mime: "text/plain", data: "YQ==" }]);
|
|
25
|
+
expect(
|
|
26
|
+
parseIncomingUploads([
|
|
27
|
+
{ name: "cv.md", mime: "text/markdown", data: "YQ==", sourcePath: "/Users/mads/cv.md" },
|
|
28
|
+
])[0]?.sourcePath,
|
|
29
|
+
).toBe("/Users/mads/cv.md");
|
|
23
30
|
});
|
|
24
31
|
|
|
25
32
|
test("treats source and markdown as text", () => {
|
|
@@ -50,6 +57,33 @@ describe("uploads", () => {
|
|
|
50
57
|
expect(composeUploadMessage("look", saved)).toContain(saved[0]?.rel ?? "");
|
|
51
58
|
expect(composeUploadMessage("look", saved)).toContain("```\nhello\n```");
|
|
52
59
|
expect(composeUploadMessage("", saved)).toContain(saved[1]?.rel ?? "");
|
|
60
|
+
expect(listUploads(cwd).map((item) => item.name)).toEqual(["shot.png", "note.txt"]);
|
|
61
|
+
} finally {
|
|
62
|
+
await rm(cwd, { recursive: true, force: true });
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("keeps the original path and matches it for edit prompts", async () => {
|
|
67
|
+
const cwd = await mkdtemp(join(tmpdir(), "harness-uploads-"));
|
|
68
|
+
const original = join(cwd, "source.md");
|
|
69
|
+
try {
|
|
70
|
+
await Bun.write(original, "draft");
|
|
71
|
+
const saved = saveUploads(cwd, [
|
|
72
|
+
{
|
|
73
|
+
name: "source.md",
|
|
74
|
+
mime: "text/markdown",
|
|
75
|
+
data: Buffer.from("draft").toString("base64"),
|
|
76
|
+
sourcePath: original,
|
|
77
|
+
},
|
|
78
|
+
]);
|
|
79
|
+
expect(saved[0]?.sourcePath).toBe(original);
|
|
80
|
+
expect(composeUploadMessage("edit", saved)).toContain("Original:");
|
|
81
|
+
expect(matchUploadEdit(cwd, saved[0]?.rel ?? "")).toEqual({
|
|
82
|
+
rel: saved[0]?.rel ?? "",
|
|
83
|
+
sourcePath: original,
|
|
84
|
+
name: "source.md",
|
|
85
|
+
});
|
|
86
|
+
expect(matchUploadEdit(cwd, original)?.rel).toBe(saved[0]?.rel);
|
|
53
87
|
} finally {
|
|
54
88
|
await rm(cwd, { recursive: true, force: true });
|
|
55
89
|
}
|
package/src/desktop/uploads.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
-
import {
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { isAbsolute, resolve } from "node:path";
|
|
3
4
|
import { toProjectRel } from "../tools/paths.ts";
|
|
5
|
+
import { composeUploadMessage as buildUploadMessage } from "./upload-message.ts";
|
|
4
6
|
|
|
5
7
|
export const MAX_UPLOADS = 8;
|
|
6
8
|
export const MAX_UPLOAD_BYTES = 10 * 1024 * 1024;
|
|
7
9
|
const TEXT_EXCERPT_BYTES = 100_000;
|
|
10
|
+
const INDEX_NAME = "index.json";
|
|
11
|
+
const MAX_LISTED = 40;
|
|
8
12
|
|
|
9
13
|
const TEXT_MIME =
|
|
10
14
|
/^(text\/|application\/(json|xml|javascript|sql|yaml|x-yaml|toml|x-sh|x-httpd-php))/i;
|
|
@@ -15,6 +19,7 @@ export interface IncomingUpload {
|
|
|
15
19
|
name: string;
|
|
16
20
|
mime: string;
|
|
17
21
|
data: string;
|
|
22
|
+
sourcePath?: string;
|
|
18
23
|
}
|
|
19
24
|
|
|
20
25
|
export interface SavedUpload {
|
|
@@ -22,6 +27,15 @@ export interface SavedUpload {
|
|
|
22
27
|
rel: string;
|
|
23
28
|
mime: string;
|
|
24
29
|
excerpt?: string;
|
|
30
|
+
sourcePath?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface UploadRecord {
|
|
34
|
+
name: string;
|
|
35
|
+
rel: string;
|
|
36
|
+
mime: string;
|
|
37
|
+
sourcePath?: string;
|
|
38
|
+
addedAt: string;
|
|
25
39
|
}
|
|
26
40
|
|
|
27
41
|
export function parseIncomingUploads(raw: unknown): IncomingUpload[] {
|
|
@@ -37,12 +51,38 @@ export function parseIncomingUploads(raw: unknown): IncomingUpload[] {
|
|
|
37
51
|
? rec.mime.trim()
|
|
38
52
|
: "application/octet-stream";
|
|
39
53
|
if (!name || !data) continue;
|
|
40
|
-
|
|
54
|
+
const sourcePath = parseSourcePath(rec.sourcePath);
|
|
55
|
+
out.push({ name, mime, data, ...(sourcePath ? { sourcePath } : {}) });
|
|
41
56
|
if (out.length >= MAX_UPLOADS) break;
|
|
42
57
|
}
|
|
43
58
|
return out;
|
|
44
59
|
}
|
|
45
60
|
|
|
61
|
+
export function parseSourcePath(raw: unknown): string | undefined {
|
|
62
|
+
if (typeof raw !== "string") return undefined;
|
|
63
|
+
const trimmed = raw.trim();
|
|
64
|
+
if (!trimmed || trimmed.includes("\0") || /\n|\r/.test(trimmed) || trimmed.length > 1024) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
if (trimmed.startsWith("file://")) return undefined;
|
|
68
|
+
if (trimmed === "~" || trimmed.startsWith("~/")) return trimmed;
|
|
69
|
+
if (isAbsolute(trimmed)) return trimmed;
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function expandSourcePath(raw: string): string {
|
|
74
|
+
if (raw === "~") return homedir();
|
|
75
|
+
if (raw.startsWith("~/")) return resolve(homedir(), raw.slice(2));
|
|
76
|
+
return resolve(raw);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function displaySourcePath(abs: string): string {
|
|
80
|
+
const home = homedir();
|
|
81
|
+
if (abs === home) return "~";
|
|
82
|
+
if (abs.startsWith(`${home}/`)) return `~${abs.slice(home.length)}`;
|
|
83
|
+
return abs;
|
|
84
|
+
}
|
|
85
|
+
|
|
46
86
|
export function isTextUpload(name: string, mime: string): boolean {
|
|
47
87
|
return TEXT_MIME.test(mime) || TEXT_EXT.test(name);
|
|
48
88
|
}
|
|
@@ -53,11 +93,121 @@ export function safeUploadName(name: string): string {
|
|
|
53
93
|
return (base || "file").slice(0, 120);
|
|
54
94
|
}
|
|
55
95
|
|
|
96
|
+
function uploadsDir(cwd: string): string {
|
|
97
|
+
return resolve(cwd, ".harness", "uploads");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function storedDisplayName(filename: string): string {
|
|
101
|
+
return filename.replace(/^\d+-\d+-/, "") || filename;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function readIndex(dir: string): UploadRecord[] {
|
|
105
|
+
try {
|
|
106
|
+
const parsed: unknown = JSON.parse(readFileSync(resolve(dir, INDEX_NAME), "utf8"));
|
|
107
|
+
if (!parsed || typeof parsed !== "object") return [];
|
|
108
|
+
const files = (parsed as { files?: unknown }).files;
|
|
109
|
+
if (!Array.isArray(files)) return [];
|
|
110
|
+
const out: UploadRecord[] = [];
|
|
111
|
+
for (const item of files) {
|
|
112
|
+
if (!item || typeof item !== "object") continue;
|
|
113
|
+
const rec = item as Record<string, unknown>;
|
|
114
|
+
const name = typeof rec.name === "string" ? rec.name : "";
|
|
115
|
+
const rel = typeof rec.rel === "string" ? rec.rel : "";
|
|
116
|
+
if (!name || !rel) continue;
|
|
117
|
+
const mime = typeof rec.mime === "string" ? rec.mime : "application/octet-stream";
|
|
118
|
+
const addedAt = typeof rec.addedAt === "string" ? rec.addedAt : "";
|
|
119
|
+
const sourcePath = parseSourcePath(rec.sourcePath);
|
|
120
|
+
out.push({
|
|
121
|
+
name,
|
|
122
|
+
rel,
|
|
123
|
+
mime,
|
|
124
|
+
addedAt,
|
|
125
|
+
...(sourcePath ? { sourcePath } : {}),
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return out;
|
|
129
|
+
} catch {
|
|
130
|
+
return [];
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function writeIndex(dir: string, files: UploadRecord[]): void {
|
|
135
|
+
writeFileSync(resolve(dir, INDEX_NAME), `${JSON.stringify({ files }, null, 2)}\n`);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function listUploads(cwd: string): UploadRecord[] {
|
|
139
|
+
const dir = uploadsDir(cwd);
|
|
140
|
+
const byRel = new Map<string, UploadRecord>();
|
|
141
|
+
for (const item of readIndex(dir)) byRel.set(item.rel, item);
|
|
142
|
+
try {
|
|
143
|
+
for (const name of readdirSync(dir)) {
|
|
144
|
+
if (name === INDEX_NAME) continue;
|
|
145
|
+
const abs = resolve(dir, name);
|
|
146
|
+
if (!statSync(abs).isFile()) continue;
|
|
147
|
+
const rel = toProjectRel(cwd, abs);
|
|
148
|
+
if (byRel.has(rel)) continue;
|
|
149
|
+
byRel.set(rel, {
|
|
150
|
+
name: storedDisplayName(name),
|
|
151
|
+
rel,
|
|
152
|
+
mime: "application/octet-stream",
|
|
153
|
+
addedAt: new Date(statSync(abs).mtimeMs).toISOString(),
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
} catch {
|
|
157
|
+
// no uploads folder yet
|
|
158
|
+
}
|
|
159
|
+
return [...byRel.values()]
|
|
160
|
+
.sort((a, b) => (a.addedAt < b.addedAt ? 1 : a.addedAt > b.addedAt ? -1 : 0))
|
|
161
|
+
.slice(0, MAX_LISTED);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function listUploadOriginals(cwd: string): string[] {
|
|
165
|
+
const out: string[] = [];
|
|
166
|
+
for (const item of listUploads(cwd)) {
|
|
167
|
+
if (!item.sourcePath) continue;
|
|
168
|
+
out.push(expandSourcePath(item.sourcePath));
|
|
169
|
+
}
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function matchUploadEdit(
|
|
174
|
+
cwd: string,
|
|
175
|
+
path: string,
|
|
176
|
+
): { rel: string; sourcePath: string; name: string } | undefined {
|
|
177
|
+
const trimmed = path.trim();
|
|
178
|
+
if (!trimmed) return undefined;
|
|
179
|
+
const root = resolve(cwd);
|
|
180
|
+
let abs: string | undefined;
|
|
181
|
+
try {
|
|
182
|
+
abs =
|
|
183
|
+
trimmed.startsWith("~") || isAbsolute(trimmed)
|
|
184
|
+
? expandSourcePath(trimmed)
|
|
185
|
+
: resolve(root, trimmed);
|
|
186
|
+
} catch {
|
|
187
|
+
abs = undefined;
|
|
188
|
+
}
|
|
189
|
+
for (const item of listUploads(cwd)) {
|
|
190
|
+
if (!item.sourcePath) continue;
|
|
191
|
+
const original = expandSourcePath(item.sourcePath);
|
|
192
|
+
const copyAbs = resolve(root, item.rel);
|
|
193
|
+
if (
|
|
194
|
+
trimmed === item.rel ||
|
|
195
|
+
trimmed === item.sourcePath ||
|
|
196
|
+
abs === original ||
|
|
197
|
+
abs === copyAbs
|
|
198
|
+
) {
|
|
199
|
+
return { rel: item.rel, sourcePath: original, name: item.name };
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return undefined;
|
|
203
|
+
}
|
|
204
|
+
|
|
56
205
|
export function saveUploads(cwd: string, items: IncomingUpload[]): SavedUpload[] {
|
|
57
206
|
if (items.length === 0) return [];
|
|
58
|
-
const dir =
|
|
207
|
+
const dir = uploadsDir(cwd);
|
|
59
208
|
mkdirSync(dir, { recursive: true });
|
|
60
209
|
const saved: SavedUpload[] = [];
|
|
210
|
+
const records = listUploads(cwd);
|
|
61
211
|
for (const item of items) {
|
|
62
212
|
const bytes = Buffer.from(item.data, "base64");
|
|
63
213
|
if (bytes.byteLength === 0 || bytes.byteLength > MAX_UPLOAD_BYTES) continue;
|
|
@@ -69,24 +219,39 @@ export function saveUploads(cwd: string, items: IncomingUpload[]): SavedUpload[]
|
|
|
69
219
|
isTextUpload(item.name, item.mime) && bytes.byteLength <= TEXT_EXCERPT_BYTES
|
|
70
220
|
? bytes.toString("utf8")
|
|
71
221
|
: undefined;
|
|
222
|
+
const sourcePath = item.sourcePath
|
|
223
|
+
? existsSync(expandSourcePath(item.sourcePath))
|
|
224
|
+
? expandSourcePath(item.sourcePath)
|
|
225
|
+
: undefined
|
|
226
|
+
: undefined;
|
|
72
227
|
saved.push({
|
|
73
228
|
name: item.name,
|
|
74
229
|
rel,
|
|
75
230
|
mime: item.mime,
|
|
76
231
|
...(excerpt !== undefined ? { excerpt } : {}),
|
|
232
|
+
...(sourcePath ? { sourcePath } : {}),
|
|
233
|
+
});
|
|
234
|
+
records.unshift({
|
|
235
|
+
name: item.name,
|
|
236
|
+
rel,
|
|
237
|
+
mime: item.mime,
|
|
238
|
+
addedAt: new Date().toISOString(),
|
|
239
|
+
...(sourcePath ? { sourcePath } : {}),
|
|
77
240
|
});
|
|
78
241
|
}
|
|
242
|
+
writeIndex(dir, records.slice(0, MAX_LISTED));
|
|
79
243
|
return saved;
|
|
80
244
|
}
|
|
81
245
|
|
|
82
246
|
export function composeUploadMessage(text: string, files: SavedUpload[]): string {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
247
|
+
return buildUploadMessage(
|
|
248
|
+
text,
|
|
249
|
+
files.map((file) => ({
|
|
250
|
+
name: file.name,
|
|
251
|
+
rel: file.rel,
|
|
252
|
+
mime: file.mime,
|
|
253
|
+
...(file.excerpt !== undefined ? { excerpt: file.excerpt } : {}),
|
|
254
|
+
...(file.sourcePath ? { original: displaySourcePath(file.sourcePath) } : {}),
|
|
255
|
+
})),
|
|
256
|
+
);
|
|
92
257
|
}
|
package/src/index.ts
CHANGED
package/src/init.test.ts
CHANGED
|
@@ -68,4 +68,25 @@ describe("local tools", () => {
|
|
|
68
68
|
await rm(cwd, { recursive: true, force: true });
|
|
69
69
|
}
|
|
70
70
|
});
|
|
71
|
+
|
|
72
|
+
test("can write an uploaded original outside cwd when allowlisted", async () => {
|
|
73
|
+
const cwd = await mkdtemp(join(tmpdir(), "harness-tools-"));
|
|
74
|
+
const outside = await mkdtemp(join(tmpdir(), "harness-orig-"));
|
|
75
|
+
try {
|
|
76
|
+
const original = join(outside, "brief.md");
|
|
77
|
+
await Bun.write(original, "hello");
|
|
78
|
+
const mcp = createLocalTools({ cwd, extraWriteAbsolutes: () => [original] });
|
|
79
|
+
const written = await mcp.callTool("write_file", { path: original, content: "edited\n" });
|
|
80
|
+
expect(written.isError).toBeFalsy();
|
|
81
|
+
expect(await readFile(original, "utf8")).toBe("edited\n");
|
|
82
|
+
const blocked = await mcp.callTool("write_file", {
|
|
83
|
+
path: join(outside, "other.md"),
|
|
84
|
+
content: "nope",
|
|
85
|
+
});
|
|
86
|
+
expect(blocked.isError).toBe(true);
|
|
87
|
+
} finally {
|
|
88
|
+
await rm(cwd, { recursive: true, force: true });
|
|
89
|
+
await rm(outside, { recursive: true, force: true });
|
|
90
|
+
}
|
|
91
|
+
});
|
|
71
92
|
});
|
package/src/runtime.ts
CHANGED
|
@@ -37,6 +37,7 @@ import {
|
|
|
37
37
|
createConstructAgent,
|
|
38
38
|
} from "./core/strands.ts";
|
|
39
39
|
import { addUsage, type TokenUsage } from "./core/usage.ts";
|
|
40
|
+
import { listUploadOriginals, matchUploadEdit } from "./desktop/uploads.ts";
|
|
40
41
|
import { type ChatFn, createOpenRouterChat } from "./evals/runner.ts";
|
|
41
42
|
import { buildRemoteMcpTools } from "./integrations/remote-mcp.ts";
|
|
42
43
|
import { linkedMcpSources } from "./integrations/store.ts";
|
|
@@ -172,7 +173,10 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
172
173
|
});
|
|
173
174
|
let systemPrompt = composePrompt(mode);
|
|
174
175
|
const store = createFileSessionStore(cwd);
|
|
175
|
-
const local = createLocalTools({
|
|
176
|
+
const local = createLocalTools({
|
|
177
|
+
cwd,
|
|
178
|
+
extraWriteAbsolutes: () => listUploadOriginals(cwd),
|
|
179
|
+
});
|
|
176
180
|
const todos = createMemoryTodoStore();
|
|
177
181
|
const hooks = createHookRunner({ cwd, hooks: config.hooks });
|
|
178
182
|
void hooks.sessionStart();
|
|
@@ -447,6 +451,7 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
447
451
|
policy,
|
|
448
452
|
alwaysAllow,
|
|
449
453
|
emit,
|
|
454
|
+
resolveUploadEdit: (path) => matchUploadEdit(cwd, path),
|
|
450
455
|
ask: async (req) => {
|
|
451
456
|
if (turnOptions?.approvalAsk) return turnOptions.approvalAsk(req);
|
|
452
457
|
if (policy === "prompt") return false;
|