@saadnvd1/agent-os 0.3.672 → 0.3.673
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/app/api/files/upload-temp/route.test.ts +144 -0
- package/app/api/files/upload-temp/route.ts +52 -21
- package/app/api/sessions/[id]/route.ts +2 -0
- package/app/api/sessions/route.ts +6 -2
- package/components/Chat/ChatPanel.tsx +9 -2
- package/components/Chat/FileChip.tsx +67 -0
- package/components/Chat/Messages.tsx +8 -0
- package/components/Chat/Queue.tsx +6 -0
- package/components/Chat/composer/Attachments.tsx +16 -2
- package/components/Chat/composer/ComposerBody.tsx +35 -14
- package/components/Chat/composer/Toolbar.tsx +11 -8
- package/components/Chat/composer/editor.test.ts +1 -1
- package/components/Chat/composer/paste.ts +5 -4
- package/components/Chat/composer/useAttachFiles.ts +142 -0
- package/components/Chat/composer/useComposerEditor.ts +3 -3
- package/components/Chat/useDraft.ts +9 -2
- package/components/Draft/DraftPanel.tsx +7 -4
- package/components/Terminal/index.tsx +6 -5
- package/data/chat/useChat.ts +10 -3
- package/data/client-config/queries.ts +18 -0
- package/data/sessions/queries.ts +2 -1
- package/lib/attachments/check.ts +50 -0
- package/lib/attachments/message.ts +49 -0
- package/lib/attachments/policy.ts +116 -0
- package/lib/attachments/store.ts +92 -0
- package/lib/attachments/types.ts +8 -0
- package/lib/chat/client-send.ts +6 -2
- package/lib/chat/driver.ts +4 -1
- package/lib/chat/drivers/claude.ts +2 -0
- package/lib/chat/events.ts +13 -1
- package/lib/chat/pending.ts +2 -1
- package/lib/chat/queued.ts +24 -7
- package/lib/chat/runner.ts +11 -2
- package/lib/chat/worker/host.ts +7 -3
- package/lib/chat/worker/protocol.ts +2 -0
- package/lib/config/client-config-types.ts +14 -3
- package/lib/config/client-config.ts +1 -1
- package/lib/db/migrations/20261019T1200_chat_queue_files.ts +14 -0
- package/lib/file-upload.ts +61 -27
- package/lib/intake/intake.ts +2 -17
- package/lib/intake/sniff.ts +1 -1
- package/lib/sessions/launch.ts +12 -6
- package/package.json +1 -1
- package/components/Chat/composer/useImageDrop.ts +0 -52
package/lib/chat/client-send.ts
CHANGED
|
@@ -1,25 +1,29 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { resolveFiles } from "../attachments/store";
|
|
2
|
+
import type { ChatFile, ChatImage } from "./events";
|
|
2
3
|
import type { CompactFirst } from "./stale";
|
|
3
4
|
|
|
4
|
-
// What a browser's send may carry: its text and
|
|
5
|
+
// What a browser's send may carry: its text, images and files, and whether the
|
|
5
6
|
// reader chose to compact a stale chat first. Who a message is from (`from`,
|
|
6
7
|
// `peer`, `origin`) is set by the server alone, never by a client. Its own
|
|
7
8
|
// id for the message is kept only in a shape no server id takes.
|
|
8
9
|
export function clientSend(msg: unknown): {
|
|
9
10
|
text: string;
|
|
10
11
|
images?: ChatImage[];
|
|
12
|
+
files?: ChatFile[];
|
|
11
13
|
compact?: CompactFirst;
|
|
12
14
|
id?: string;
|
|
13
15
|
} {
|
|
14
16
|
const m = (msg ?? {}) as {
|
|
15
17
|
text?: unknown;
|
|
16
18
|
images?: unknown;
|
|
19
|
+
files?: unknown;
|
|
17
20
|
compact?: unknown;
|
|
18
21
|
clientId?: unknown;
|
|
19
22
|
};
|
|
20
23
|
return {
|
|
21
24
|
text: typeof m.text === "string" ? m.text : "",
|
|
22
25
|
images: Array.isArray(m.images) ? (m.images as ChatImage[]) : undefined,
|
|
26
|
+
files: resolveFiles(m.files),
|
|
23
27
|
...(m.compact === true && { compact: "yes" as const }),
|
|
24
28
|
...(isClientId(m.clientId) && { id: m.clientId }),
|
|
25
29
|
};
|
package/lib/chat/driver.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
ApprovalDecision,
|
|
7
7
|
ChatAccess,
|
|
8
8
|
ChatCommand,
|
|
9
|
+
ChatFile,
|
|
9
10
|
ChatImage,
|
|
10
11
|
ChatItem,
|
|
11
12
|
ChatModel,
|
|
@@ -88,7 +89,9 @@ export interface ChatConversation {
|
|
|
88
89
|
send(
|
|
89
90
|
text: string,
|
|
90
91
|
images?: ChatImage[],
|
|
91
|
-
|
|
92
|
+
// `files`: saved attachments; their paths are already in `text`, and a
|
|
93
|
+
// driver may also hand some to the model directly.
|
|
94
|
+
options?: { now?: boolean; files?: ChatFile[] }
|
|
92
95
|
): string | undefined;
|
|
93
96
|
// A command the driver answers itself, without starting a turn (/mcp):
|
|
94
97
|
// the items to show, or null to send the text as a message.
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { documentBlocks } from "../../attachments/message";
|
|
1
2
|
import { sessionBaseEnv } from "../../agents/server-env";
|
|
2
3
|
import { randomUUID } from "crypto";
|
|
3
4
|
import {
|
|
@@ -320,6 +321,7 @@ export const claudeDriver: ChatDriver = {
|
|
|
320
321
|
data: img.data,
|
|
321
322
|
},
|
|
322
323
|
})),
|
|
324
|
+
...documentBlocks(sendOptions?.files),
|
|
323
325
|
{ type: "text" as const, text },
|
|
324
326
|
];
|
|
325
327
|
const checkpoint = randomUUID();
|
package/lib/chat/events.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { CallUsage } from "../usage/types";
|
|
2
2
|
import type { SearchResult } from "./find";
|
|
3
|
+
import type { ChatFile } from "../attachments/types";
|
|
4
|
+
|
|
5
|
+
export type { ChatFile };
|
|
3
6
|
|
|
4
7
|
/**
|
|
5
8
|
* The provider-neutral shape of a chat conversation. Every driver (Claude
|
|
@@ -152,6 +155,8 @@ export type ChatItem =
|
|
|
152
155
|
kind: "user";
|
|
153
156
|
text: string;
|
|
154
157
|
images?: ChatImage[];
|
|
158
|
+
// Saved in the session's attachments folder, by reference.
|
|
159
|
+
files?: ChatFile[];
|
|
155
160
|
// Who sent it through the agent bus, when not typed here.
|
|
156
161
|
from?: string;
|
|
157
162
|
// Sent by another agent session: shown as its body, while the agent
|
|
@@ -282,6 +287,7 @@ export interface QueuedMessage {
|
|
|
282
287
|
id: string;
|
|
283
288
|
text: string;
|
|
284
289
|
imageCount?: number;
|
|
290
|
+
fileCount?: number;
|
|
285
291
|
createdAt: number;
|
|
286
292
|
}
|
|
287
293
|
|
|
@@ -413,7 +419,13 @@ export type ChatServerMessage =
|
|
|
413
419
|
// What a browser sends.
|
|
414
420
|
export type ChatClientMessage =
|
|
415
421
|
// `clientId` (`user-c-…`) becomes the message's id: a resend is a no-op.
|
|
416
|
-
| {
|
|
422
|
+
| {
|
|
423
|
+
type: "send";
|
|
424
|
+
text: string;
|
|
425
|
+
images?: ChatImage[];
|
|
426
|
+
files?: ChatFile[];
|
|
427
|
+
clientId?: string;
|
|
428
|
+
}
|
|
417
429
|
| { type: "queue_edit"; id: string; text: string }
|
|
418
430
|
| { type: "queue_move"; id: string; by: -1 | 1 }
|
|
419
431
|
| { type: "queue_delete"; id: string }
|
package/lib/chat/pending.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ChatImage, ChatState } from "./events";
|
|
1
|
+
import type { ChatFile, ChatImage, ChatState } from "./events";
|
|
2
2
|
|
|
3
3
|
// A message the composer sent that isn't in the chat or its queue yet. It
|
|
4
4
|
// shows as a bubble at once, and stays (as failed, with Retry) if it never
|
|
@@ -7,6 +7,7 @@ export interface PendingSend {
|
|
|
7
7
|
id: string;
|
|
8
8
|
text: string;
|
|
9
9
|
images?: ChatImage[];
|
|
10
|
+
files?: ChatFile[];
|
|
10
11
|
compact?: boolean;
|
|
11
12
|
// "sending": not taken in yet. "accepted": the server has it and is
|
|
12
13
|
// starting the agent.
|
package/lib/chat/queued.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { db } from "../db";
|
|
2
2
|
import { HELD_SQL } from "./hold";
|
|
3
|
-
import type { ChatImage, QueuedMessage, SentBy } from "./events";
|
|
3
|
+
import type { ChatFile, ChatImage, QueuedMessage, SentBy } from "./events";
|
|
4
4
|
|
|
5
5
|
// Messages written while a turn runs, kept in SQLite so a reload or a
|
|
6
6
|
// restart never loses them. The worker sends them in order when the turn
|
|
@@ -15,6 +15,7 @@ interface Row {
|
|
|
15
15
|
id: string;
|
|
16
16
|
text: string;
|
|
17
17
|
images: string | null;
|
|
18
|
+
files: string | null;
|
|
18
19
|
sent_by: string | null;
|
|
19
20
|
created_at: number;
|
|
20
21
|
}
|
|
@@ -25,19 +26,21 @@ export function listQueue(sessionId: string): QueuedMessage[] {
|
|
|
25
26
|
return (
|
|
26
27
|
db
|
|
27
28
|
.prepare(
|
|
28
|
-
`SELECT id, text, image_count, created_at
|
|
29
|
+
`SELECT id, text, image_count, files, created_at
|
|
29
30
|
FROM chat_queue WHERE session_id = ? ORDER BY position, created_at`
|
|
30
31
|
)
|
|
31
32
|
.all(sessionId) as {
|
|
32
33
|
id: string;
|
|
33
34
|
text: string;
|
|
34
35
|
image_count: number;
|
|
36
|
+
files: string | null;
|
|
35
37
|
created_at: number;
|
|
36
38
|
}[]
|
|
37
39
|
).map((r) => ({
|
|
38
40
|
id: r.id,
|
|
39
41
|
text: r.text,
|
|
40
42
|
imageCount: r.image_count || undefined,
|
|
43
|
+
fileCount: r.files ? (JSON.parse(r.files) as unknown[]).length : undefined,
|
|
41
44
|
createdAt: r.created_at,
|
|
42
45
|
}));
|
|
43
46
|
}
|
|
@@ -45,7 +48,12 @@ export function listQueue(sessionId: string): QueuedMessage[] {
|
|
|
45
48
|
// At the back. A retried send with the same id is only queued once.
|
|
46
49
|
export function enqueue(
|
|
47
50
|
sessionId: string,
|
|
48
|
-
m: {
|
|
51
|
+
m: {
|
|
52
|
+
id: string;
|
|
53
|
+
text: string;
|
|
54
|
+
images?: ChatImage[];
|
|
55
|
+
files?: ChatFile[];
|
|
56
|
+
} & SentBy
|
|
49
57
|
): void {
|
|
50
58
|
const images = m.images?.length ? JSON.stringify(m.images) : null;
|
|
51
59
|
const sentBy: SentBy = { from: m.from, peer: m.peer, origin: m.origin };
|
|
@@ -59,8 +67,8 @@ export function enqueue(
|
|
|
59
67
|
if (n >= MAX_QUEUED)
|
|
60
68
|
throw new Error(`The queue is full (${MAX_QUEUED} messages)`);
|
|
61
69
|
db.prepare(
|
|
62
|
-
`INSERT OR IGNORE INTO chat_queue (id, session_id, position, text, images, image_count, sent_by, created_at)
|
|
63
|
-
VALUES (?, ?, (SELECT COALESCE(MAX(position), 0) + 1 FROM chat_queue WHERE session_id = ?), ?, ?, ?, ?, ?)`
|
|
70
|
+
`INSERT OR IGNORE INTO chat_queue (id, session_id, position, text, images, image_count, files, sent_by, created_at)
|
|
71
|
+
VALUES (?, ?, (SELECT COALESCE(MAX(position), 0) + 1 FROM chat_queue WHERE session_id = ?), ?, ?, ?, ?, ?, ?)`
|
|
64
72
|
).run(
|
|
65
73
|
m.id,
|
|
66
74
|
sessionId,
|
|
@@ -68,6 +76,7 @@ export function enqueue(
|
|
|
68
76
|
m.text,
|
|
69
77
|
images,
|
|
70
78
|
m.images?.length ?? 0,
|
|
79
|
+
m.files?.length ? JSON.stringify(m.files) : null,
|
|
71
80
|
m.from || m.peer || m.origin ? JSON.stringify(sentBy) : null,
|
|
72
81
|
Date.now()
|
|
73
82
|
);
|
|
@@ -80,13 +89,20 @@ export function claimNext(
|
|
|
80
89
|
sessionId: string,
|
|
81
90
|
// That message only, if it's still there.
|
|
82
91
|
id?: string
|
|
83
|
-
):
|
|
92
|
+
):
|
|
93
|
+
| ({
|
|
94
|
+
id: string;
|
|
95
|
+
text: string;
|
|
96
|
+
images?: ChatImage[];
|
|
97
|
+
files?: ChatFile[];
|
|
98
|
+
} & SentBy)
|
|
99
|
+
| null {
|
|
84
100
|
const row = db
|
|
85
101
|
.prepare(
|
|
86
102
|
`DELETE FROM chat_queue WHERE session_id = @sid AND NOT ${HELD_SQL} AND id = (
|
|
87
103
|
SELECT id FROM chat_queue WHERE session_id = @sid AND (@id IS NULL OR id = @id)
|
|
88
104
|
ORDER BY position, created_at LIMIT 1
|
|
89
|
-
) RETURNING id, text, images, sent_by, created_at`
|
|
105
|
+
) RETURNING id, text, images, files, sent_by, created_at`
|
|
90
106
|
)
|
|
91
107
|
.get({ sid: sessionId, id: id ?? null }) as Row | undefined;
|
|
92
108
|
if (!row) return null;
|
|
@@ -94,6 +110,7 @@ export function claimNext(
|
|
|
94
110
|
id: row.id,
|
|
95
111
|
text: row.text,
|
|
96
112
|
images: row.images ? (JSON.parse(row.images) as ChatImage[]) : undefined,
|
|
113
|
+
files: row.files ? (JSON.parse(row.files) as ChatFile[]) : undefined,
|
|
97
114
|
...(row.sent_by ? (JSON.parse(row.sent_by) as SentBy) : {}),
|
|
98
115
|
};
|
|
99
116
|
}
|
package/lib/chat/runner.ts
CHANGED
|
@@ -19,6 +19,7 @@ import type { CompactFirst } from "./stale";
|
|
|
19
19
|
import type {
|
|
20
20
|
ApprovalDecision,
|
|
21
21
|
ChatContext,
|
|
22
|
+
ChatFile,
|
|
22
23
|
ChatImage,
|
|
23
24
|
ChatServerMessage,
|
|
24
25
|
ChatState,
|
|
@@ -405,7 +406,12 @@ function refuseAway(sessionId: string): void {
|
|
|
405
406
|
// started): the message waits in the queue instead of starting a turn.
|
|
406
407
|
function queuedIfHeld(
|
|
407
408
|
sessionId: string,
|
|
408
|
-
m: {
|
|
409
|
+
m: {
|
|
410
|
+
id: string;
|
|
411
|
+
text: string;
|
|
412
|
+
images?: ChatImage[];
|
|
413
|
+
files?: ChatFile[];
|
|
414
|
+
} & SentBy
|
|
409
415
|
): boolean {
|
|
410
416
|
refuseAway(sessionId);
|
|
411
417
|
if (!chatHold(sessionId)) return false;
|
|
@@ -429,6 +435,7 @@ export async function sendChat(
|
|
|
429
435
|
input: {
|
|
430
436
|
text: string;
|
|
431
437
|
images?: ChatImage[];
|
|
438
|
+
files?: ChatFile[];
|
|
432
439
|
// Typed in the composer: waits in the queue while a turn runs.
|
|
433
440
|
queue?: boolean;
|
|
434
441
|
// Compact a stale chat first (lib/chat/stale).
|
|
@@ -438,7 +445,7 @@ export async function sendChat(
|
|
|
438
445
|
} & SentBy
|
|
439
446
|
): Promise<void> {
|
|
440
447
|
const text = input.text.trim();
|
|
441
|
-
if (!text && !input.images?.length) return;
|
|
448
|
+
if (!text && !input.images?.length && !input.files?.length) return;
|
|
442
449
|
if (input.id && taken(sessionId, input.id)) return;
|
|
443
450
|
refuseAway(sessionId);
|
|
444
451
|
const id = input.id ?? `user-${Date.now()}-${randomUUID().slice(0, 5)}`;
|
|
@@ -449,6 +456,7 @@ export async function sendChat(
|
|
|
449
456
|
id,
|
|
450
457
|
text,
|
|
451
458
|
images: input.images,
|
|
459
|
+
files: input.files,
|
|
452
460
|
...sentBy(input),
|
|
453
461
|
});
|
|
454
462
|
emitQueue(sessionId);
|
|
@@ -463,6 +471,7 @@ export async function sendChat(
|
|
|
463
471
|
id,
|
|
464
472
|
text,
|
|
465
473
|
images: input.images,
|
|
474
|
+
files: input.files,
|
|
466
475
|
...sentBy(input),
|
|
467
476
|
queue: input.queue,
|
|
468
477
|
compact: input.compact,
|
package/lib/chat/worker/host.ts
CHANGED
|
@@ -10,10 +10,12 @@ import { agentEnv } from "../../agents/launch";
|
|
|
10
10
|
import { sessionRunningBrief } from "../../project-config/session";
|
|
11
11
|
import { BUS_BRIEF } from "../../agents/brief";
|
|
12
12
|
import { resolveModelForAgent } from "../../model-catalog";
|
|
13
|
+
import { agentText } from "../../attachments/message";
|
|
13
14
|
import { chatDriverFor } from "../drivers";
|
|
14
15
|
import type { ChatConversation } from "../driver";
|
|
15
16
|
import type {
|
|
16
17
|
ChatContext,
|
|
18
|
+
ChatFile,
|
|
17
19
|
ChatImage,
|
|
18
20
|
ChatItem,
|
|
19
21
|
ChatState,
|
|
@@ -560,6 +562,7 @@ export class ChatHost {
|
|
|
560
562
|
id: string;
|
|
561
563
|
text: string;
|
|
562
564
|
images?: ChatImage[];
|
|
565
|
+
files?: ChatFile[];
|
|
563
566
|
} & SentBy,
|
|
564
567
|
now = false
|
|
565
568
|
): void {
|
|
@@ -577,7 +580,7 @@ export class ChatHost {
|
|
|
577
580
|
if (now) this.nowPending++;
|
|
578
581
|
this.stopping = null;
|
|
579
582
|
const note = this.refreshedNote ?? (this.restartedNote && RESTARTED_NOTE);
|
|
580
|
-
const text = note ? `${note}\n\n${m.text}` : m.text;
|
|
583
|
+
const text = agentText(note ? `${note}\n\n${m.text}` : m.text, m.files);
|
|
581
584
|
if (this.refreshedNote) {
|
|
582
585
|
this.refreshedNote = null;
|
|
583
586
|
db.prepare(`UPDATE sessions SET chat_refreshed = 0 WHERE id = ?`).run(
|
|
@@ -592,13 +595,14 @@ export class ChatHost {
|
|
|
592
595
|
}
|
|
593
596
|
if (/^\/compact\b/.test(m.text.trimStart())) this.setCompacting(true);
|
|
594
597
|
const checkpoint = now
|
|
595
|
-
? this.conversation.send(text, m.images, { now })
|
|
596
|
-
: this.conversation.send(text, m.images);
|
|
598
|
+
? this.conversation.send(text, m.images, { now, files: m.files })
|
|
599
|
+
: this.conversation.send(text, m.images, { files: m.files });
|
|
597
600
|
const user: ChatItem = {
|
|
598
601
|
id: m.id,
|
|
599
602
|
kind: "user",
|
|
600
603
|
text: m.text,
|
|
601
604
|
images: m.images,
|
|
605
|
+
files: m.files,
|
|
602
606
|
from: m.from,
|
|
603
607
|
peer: m.peer,
|
|
604
608
|
origin: m.origin,
|
|
@@ -9,6 +9,7 @@ import path from "path";
|
|
|
9
9
|
import type {
|
|
10
10
|
ApprovalDecision,
|
|
11
11
|
ChatAccess,
|
|
12
|
+
ChatFile,
|
|
12
13
|
ChatImage,
|
|
13
14
|
ChatItem,
|
|
14
15
|
ChatState,
|
|
@@ -32,6 +33,7 @@ export type WorkerCommand =
|
|
|
32
33
|
id: string;
|
|
33
34
|
text: string;
|
|
34
35
|
images?: ChatImage[];
|
|
36
|
+
files?: ChatFile[];
|
|
35
37
|
from?: string;
|
|
36
38
|
peer?: PeerMessage;
|
|
37
39
|
origin?: ChatOrigin;
|
|
@@ -28,10 +28,23 @@ export interface ClientConfig {
|
|
|
28
28
|
runTriggers: Record<string, string>;
|
|
29
29
|
features: ClientFeatures;
|
|
30
30
|
intake: IntakeLimits;
|
|
31
|
-
attachments:
|
|
31
|
+
attachments: AttachmentConfig;
|
|
32
32
|
voice: VoiceLimits;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
// What a chat accepts, served from lib/attachments/policy.ts. Sizes in bytes.
|
|
36
|
+
export interface AttachmentConfig {
|
|
37
|
+
max: number;
|
|
38
|
+
maxBytes: number;
|
|
39
|
+
maxImageBytes: number;
|
|
40
|
+
pdfInlineBytes: number;
|
|
41
|
+
types: {
|
|
42
|
+
ext: string;
|
|
43
|
+
mediaType: string;
|
|
44
|
+
kind: "image" | "pdf" | "sheet" | "doc" | "text";
|
|
45
|
+
}[];
|
|
46
|
+
}
|
|
47
|
+
|
|
35
48
|
// Server kill switches for the phone's new sends.
|
|
36
49
|
export interface ClientFeatures {
|
|
37
50
|
shakeToReport: boolean;
|
|
@@ -66,8 +79,6 @@ export const INTAKE_LIMITS: IntakeLimits = {
|
|
|
66
79
|
logMaxLines: 200,
|
|
67
80
|
};
|
|
68
81
|
|
|
69
|
-
export const ATTACHMENT_LIMITS = { max: 8 };
|
|
70
|
-
|
|
71
82
|
export const VOICE_LIMITS: VoiceLimits = {
|
|
72
83
|
maxPhrases: 100,
|
|
73
84
|
holdMinMs: 250,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ATTACHMENT_LIMITS } from "../attachments/policy";
|
|
1
2
|
import { ACCESS_LEVELS, DEFAULT_NEW_CHAT_ACCESS } from "../chat/access-levels";
|
|
2
3
|
import { CHAT_ACCESS } from "../chat/events";
|
|
3
4
|
import {
|
|
@@ -7,7 +8,6 @@ import {
|
|
|
7
8
|
import { TASK_STATE_LABELS } from "../tasks/state-labels";
|
|
8
9
|
import { cachedCheck } from "../updates/check";
|
|
9
10
|
import {
|
|
10
|
-
ATTACHMENT_LIMITS,
|
|
11
11
|
CLIENT_CONFIG_VERSION,
|
|
12
12
|
INTAKE_LIMITS,
|
|
13
13
|
VOICE_LIMITS,
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Migration } from "../migration-types";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
key: "20261019T1200_chat_queue_files",
|
|
5
|
+
up: (db) => {
|
|
6
|
+
// Files attached to a queued message, as a JSON list of references to
|
|
7
|
+
// the session's attachments folder (never the bytes).
|
|
8
|
+
const cols = db.prepare(`PRAGMA table_info(chat_queue)`).all() as {
|
|
9
|
+
name: string;
|
|
10
|
+
}[];
|
|
11
|
+
if (!cols.some((c) => c.name === "files"))
|
|
12
|
+
db.exec(`ALTER TABLE chat_queue ADD COLUMN files TEXT`);
|
|
13
|
+
},
|
|
14
|
+
} satisfies Migration;
|
package/lib/file-upload.ts
CHANGED
|
@@ -1,34 +1,68 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* Converts the file to base64 and POSTs to /api/files/upload-temp.
|
|
4
|
-
*
|
|
5
|
-
* @param file - The file to upload
|
|
6
|
-
* @returns The path to the uploaded file, or null if upload failed
|
|
7
|
-
*/
|
|
8
|
-
export async function uploadFileToTemp(file: File): Promise<string | null> {
|
|
9
|
-
const buffer = await file.arrayBuffer();
|
|
10
|
-
const base64 = btoa(
|
|
11
|
-
new Uint8Array(buffer).reduce(
|
|
12
|
-
(data, byte) => data + String.fromCharCode(byte),
|
|
13
|
-
""
|
|
14
|
-
)
|
|
15
|
-
);
|
|
1
|
+
import type { AttachmentKind } from "./attachments/policy";
|
|
2
|
+
import type { ChatFile } from "./attachments/types";
|
|
16
3
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
4
|
+
export type UploadResult =
|
|
5
|
+
| { ok: true; file: ChatFile; kind: AttachmentKind }
|
|
6
|
+
| { ok: false; error: string };
|
|
7
|
+
|
|
8
|
+
function toBase64(file: File): Promise<string> {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
const reader = new FileReader();
|
|
11
|
+
reader.onload = () => resolve(String(reader.result).split(",")[1] ?? "");
|
|
12
|
+
reader.onerror = () => reject(reader.error);
|
|
13
|
+
reader.readAsDataURL(file);
|
|
25
14
|
});
|
|
15
|
+
}
|
|
26
16
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Upload a file through /api/files/upload-temp. With a `sessionId` (or
|
|
19
|
+
* "draft") the server checks it against its attachment limits and keeps it in
|
|
20
|
+
* that session's attachments folder; without one it goes to temp storage.
|
|
21
|
+
* The error is the server's own message, ready to show.
|
|
22
|
+
*/
|
|
23
|
+
export async function uploadFile(
|
|
24
|
+
file: File,
|
|
25
|
+
sessionId?: string
|
|
26
|
+
): Promise<UploadResult> {
|
|
27
|
+
try {
|
|
28
|
+
const res = await fetch("/api/files/upload-temp", {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: { "Content-Type": "application/json" },
|
|
31
|
+
body: JSON.stringify({
|
|
32
|
+
filename: file.name || `file-${Date.now()}`,
|
|
33
|
+
base64: await toBase64(file),
|
|
34
|
+
mimeType: file.type || "application/octet-stream",
|
|
35
|
+
sessionId,
|
|
36
|
+
}),
|
|
37
|
+
});
|
|
38
|
+
const data = await res.json().catch(() => ({}));
|
|
39
|
+
if (res.ok && data.path)
|
|
40
|
+
return {
|
|
41
|
+
ok: true,
|
|
42
|
+
kind: data.kind,
|
|
43
|
+
file: {
|
|
44
|
+
path: data.path,
|
|
45
|
+
name: data.name,
|
|
46
|
+
size: data.size,
|
|
47
|
+
mediaType: data.mediaType,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
ok: false,
|
|
52
|
+
error: data.error ?? `Couldn't upload ${file.name} (${res.status})`,
|
|
53
|
+
};
|
|
54
|
+
} catch {
|
|
55
|
+
return { ok: false, error: `Couldn't upload ${file.name}` };
|
|
30
56
|
}
|
|
57
|
+
}
|
|
31
58
|
|
|
32
|
-
|
|
59
|
+
/** The saved path, or null when the upload failed (terminal and picker). */
|
|
60
|
+
export async function uploadFileToTemp(
|
|
61
|
+
file: File,
|
|
62
|
+
sessionId?: string
|
|
63
|
+
): Promise<string | null> {
|
|
64
|
+
const r = await uploadFile(file, sessionId);
|
|
65
|
+
if (r.ok) return r.file.path;
|
|
66
|
+
console.error("Upload failed:", r.error);
|
|
33
67
|
return null;
|
|
34
68
|
}
|
package/lib/intake/intake.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { getWorkspace } from "../workspaces";
|
|
|
8
8
|
import { INTAKE_LIMITS, MAX_INLINE_IMAGE_BYTES } from "./limits";
|
|
9
9
|
import { buildIntakeMessage, type IntakeMeta } from "./message";
|
|
10
10
|
import { sniff } from "./sniff";
|
|
11
|
+
import { safeName } from "../attachments/store";
|
|
11
12
|
|
|
12
13
|
export class IntakeError extends Error {
|
|
13
14
|
constructor(
|
|
@@ -39,23 +40,7 @@ const day = (t: number) => new Date(t).toISOString().slice(0, 10);
|
|
|
39
40
|
const workspaceRoot = (workspaceId: string) =>
|
|
40
41
|
path.join(dataRoot(), "intake", segment(workspaceId));
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
export function safeName(name: string, taken: Set<string>, n: number): string {
|
|
44
|
-
let base = path.basename(name.replace(/\\/g, "/"));
|
|
45
|
-
base = base
|
|
46
|
-
.replace(/[^a-zA-Z0-9.-]/g, "_")
|
|
47
|
-
.replace(/^\.+/, "")
|
|
48
|
-
.slice(0, 80);
|
|
49
|
-
if (!base) base = `file-${n}`;
|
|
50
|
-
let out = base;
|
|
51
|
-
for (let i = 2; taken.has(out); i++) {
|
|
52
|
-
const dot = base.lastIndexOf(".");
|
|
53
|
-
out =
|
|
54
|
-
dot > 0 ? `${base.slice(0, dot)}-${i}${base.slice(dot)}` : `${base}-${i}`;
|
|
55
|
-
}
|
|
56
|
-
taken.add(out);
|
|
57
|
-
return out;
|
|
58
|
-
}
|
|
43
|
+
export { safeName };
|
|
59
44
|
|
|
60
45
|
function checkMeta(meta: IntakeMeta) {
|
|
61
46
|
if (!meta || typeof meta !== "object") throw new IntakeError(400, "Bad meta");
|
package/lib/intake/sniff.ts
CHANGED
|
@@ -9,7 +9,7 @@ const BLOCKED_NAME =
|
|
|
9
9
|
const starts = (b: Buffer, sig: number[], at = 0) =>
|
|
10
10
|
sig.every((v, i) => b[at + i] === v);
|
|
11
11
|
|
|
12
|
-
function isText(b: Buffer): boolean {
|
|
12
|
+
export function isText(b: Buffer): boolean {
|
|
13
13
|
const head = b.subarray(0, 8192);
|
|
14
14
|
if (head.includes(0)) return false;
|
|
15
15
|
try {
|
package/lib/sessions/launch.ts
CHANGED
|
@@ -10,7 +10,7 @@ import os from "os";
|
|
|
10
10
|
import { randomUUID } from "crypto";
|
|
11
11
|
import { db, queries, type Session } from "../db";
|
|
12
12
|
import type { AgentType } from "../providers";
|
|
13
|
-
import type { ChatAccess, ChatImage } from "../chat/events";
|
|
13
|
+
import type { ChatAccess, ChatFile, ChatImage } from "../chat/events";
|
|
14
14
|
import { resolveModelForAgent } from "../model-catalog";
|
|
15
15
|
import { parseOverride, resolveRole, setSessionModel } from "../models/resolve";
|
|
16
16
|
import { ANTHROPIC } from "../models/roles";
|
|
@@ -69,6 +69,7 @@ export interface LaunchInput {
|
|
|
69
69
|
// The first message.
|
|
70
70
|
prompt?: string | null;
|
|
71
71
|
images?: ChatImage[];
|
|
72
|
+
files?: ChatFile[];
|
|
72
73
|
name?: string | null;
|
|
73
74
|
view?: "chat" | "terminal";
|
|
74
75
|
parentSessionId?: string | null;
|
|
@@ -103,9 +104,14 @@ function firstMessage(projectPrompt: string | null | undefined, text: string) {
|
|
|
103
104
|
}
|
|
104
105
|
|
|
105
106
|
// Waits in the queue, so neither a restart nor a slow setup loses it.
|
|
106
|
-
function queueFirst(
|
|
107
|
+
function queueFirst(
|
|
108
|
+
sessionId: string,
|
|
109
|
+
text: string,
|
|
110
|
+
images?: ChatImage[],
|
|
111
|
+
files?: ChatFile[]
|
|
112
|
+
) {
|
|
107
113
|
const id = `user-${Date.now()}-${randomUUID().slice(0, 5)}`;
|
|
108
|
-
enqueue(sessionId, { id, text, images });
|
|
114
|
+
enqueue(sessionId, { id, text, images, files });
|
|
109
115
|
return id;
|
|
110
116
|
}
|
|
111
117
|
|
|
@@ -201,8 +207,8 @@ async function launch(input: LaunchInput, id: string): Promise<Launched> {
|
|
|
201
207
|
// A linked machine's AgentOS starts it there; it is mirrored here.
|
|
202
208
|
const link = remote ? hostLink(runOn) : null;
|
|
203
209
|
if (link) {
|
|
204
|
-
if (input.images?.length)
|
|
205
|
-
throw new Error(`Images can't be sent to ${link.hostName} yet`);
|
|
210
|
+
if (input.images?.length || input.files?.length)
|
|
211
|
+
throw new Error(`Images and files can't be sent to ${link.hostName} yet`);
|
|
206
212
|
return startOnPeer(link, {
|
|
207
213
|
id,
|
|
208
214
|
project: scratch ? null : (project ?? null),
|
|
@@ -305,7 +311,7 @@ async function launch(input: LaunchInput, id: string): Promise<Launched> {
|
|
|
305
311
|
: "";
|
|
306
312
|
const chatFirst =
|
|
307
313
|
view === "chat" && message
|
|
308
|
-
? queueFirst(id, message, input.images)
|
|
314
|
+
? queueFirst(id, message, input.images, input.files)
|
|
309
315
|
: undefined;
|
|
310
316
|
|
|
311
317
|
if (useWorktree) {
|
package/package.json
CHANGED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
3
|
-
import { useState } from "react";
|
|
4
|
-
import type { ChatImage } from "@/lib/chat/events";
|
|
5
|
-
import { isImage } from "./paste";
|
|
6
|
-
|
|
7
|
-
function readImage(file: File): Promise<ChatImage> {
|
|
8
|
-
return new Promise((resolve, reject) => {
|
|
9
|
-
const reader = new FileReader();
|
|
10
|
-
reader.onload = () => {
|
|
11
|
-
const [meta, data] = String(reader.result).split(",");
|
|
12
|
-
resolve({ mediaType: meta.slice(5, meta.indexOf(";")), data });
|
|
13
|
-
};
|
|
14
|
-
reader.onerror = reject;
|
|
15
|
-
reader.readAsDataURL(file);
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const carriesFiles = (e: React.DragEvent) =>
|
|
20
|
-
[...e.dataTransfer.types].includes("Files");
|
|
21
|
-
|
|
22
|
-
// Images picked, pasted or dropped onto the composer, and the outline
|
|
23
|
-
// shown while something is dragged over it.
|
|
24
|
-
export function useImageDrop(onImages: (images: ChatImage[]) => void) {
|
|
25
|
-
const [dragging, setDragging] = useState(false);
|
|
26
|
-
|
|
27
|
-
const addFiles = async (picked: Iterable<File>) => {
|
|
28
|
-
const read = await Promise.all([...picked].filter(isImage).map(readImage));
|
|
29
|
-
if (read.length) onImages(read);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const dropProps = {
|
|
33
|
-
onDragOver: (e: React.DragEvent) => {
|
|
34
|
-
if (!carriesFiles(e)) return;
|
|
35
|
-
e.preventDefault();
|
|
36
|
-
e.dataTransfer.dropEffect = "copy";
|
|
37
|
-
setDragging(true);
|
|
38
|
-
},
|
|
39
|
-
onDragLeave: (e: React.DragEvent) => {
|
|
40
|
-
if (!e.currentTarget.contains(e.relatedTarget as Node | null))
|
|
41
|
-
setDragging(false);
|
|
42
|
-
},
|
|
43
|
-
onDrop: (e: React.DragEvent) => {
|
|
44
|
-
if (!carriesFiles(e)) return;
|
|
45
|
-
e.preventDefault();
|
|
46
|
-
setDragging(false);
|
|
47
|
-
void addFiles(e.dataTransfer.files);
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
return { dragging, dropProps, addFiles };
|
|
52
|
-
}
|