feat: stream files as binary frames over WebSocket (#659)
Replace base64-in-JSON file transfers with binary WebSocket frames to
unblock the JS thread when assistant markdown messages contain images.
Receive cost (JSON.parse over multi-MB base64) and persist cost (base64
re-encode to disk) both removed.
- Split daemon-client `exploreFileSystem` into `listDirectory` + `readFile`;
`readFile` returns `{ bytes, mime, size, modifiedAt }`. Encoding stays
private to the seam.
- New `packages/server/src/shared/binary-frames/` module hosts terminal
frames (moved from `terminal-stream-protocol.ts`) and new file-transfer
opcodes (`FileBegin` / `FileChunk` / `FileEnd`) with request-id correlation.
- Add optional `acceptBinary` to `FileExplorerRequestSchema`. New daemons
emit binary frames when set; legacy JSON path remains for old clients.
Compat fallback lives only inside `readFile`.
- New `persistAttachmentFromBytes` writes bytes directly: native via
`expo-file-system` `File.write(Uint8Array)`, web via Blob -> IndexedDB,
desktop via new `write_attachment_bytes` IPC. `persistAttachmentFromBase64`
removed.
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { createIndexedDbAttachmentStore } from "./indexeddb-attachment-store";
|
||||
|
||||
type Listener = () => void;
|
||||
|
||||
class FakeRequest<T = unknown> {
|
||||
result!: T;
|
||||
error: Error | null = null;
|
||||
private listeners = new Map<string, Listener[]>();
|
||||
|
||||
addEventListener(event: string, listener: Listener): void {
|
||||
this.listeners.set(event, [...(this.listeners.get(event) ?? []), listener]);
|
||||
}
|
||||
|
||||
emit(event: string): void {
|
||||
for (const listener of this.listeners.get(event) ?? []) {
|
||||
listener();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class FakeObjectStore {
|
||||
constructor(private readonly onPut: (record: unknown) => void) {}
|
||||
|
||||
put(record: unknown): FakeRequest<unknown> {
|
||||
this.onPut(record);
|
||||
const request = new FakeRequest<unknown>();
|
||||
queueMicrotask(() => request.emit("success"));
|
||||
return request;
|
||||
}
|
||||
}
|
||||
|
||||
class FakeTransaction {
|
||||
error: Error | null = null;
|
||||
|
||||
constructor(private readonly store: FakeObjectStore) {}
|
||||
|
||||
objectStore(): FakeObjectStore {
|
||||
return this.store;
|
||||
}
|
||||
|
||||
addEventListener(): void {}
|
||||
}
|
||||
|
||||
class FakeDatabase {
|
||||
readonly objectStoreNames = {
|
||||
contains: () => true,
|
||||
};
|
||||
|
||||
constructor(private readonly store: FakeObjectStore) {}
|
||||
|
||||
createObjectStore(): void {}
|
||||
|
||||
transaction(): FakeTransaction {
|
||||
return new FakeTransaction(this.store);
|
||||
}
|
||||
|
||||
close(): void {}
|
||||
}
|
||||
|
||||
describe("indexeddb attachment store", () => {
|
||||
let storedRecord: unknown;
|
||||
|
||||
beforeEach(() => {
|
||||
storedRecord = null;
|
||||
Object.defineProperty(globalThis, "indexedDB", {
|
||||
configurable: true,
|
||||
value: {
|
||||
open: () => {
|
||||
const store = new FakeObjectStore((record) => {
|
||||
storedRecord = record;
|
||||
});
|
||||
const request = new FakeRequest<FakeDatabase>();
|
||||
request.result = new FakeDatabase(store);
|
||||
queueMicrotask(() => request.emit("success"));
|
||||
return request;
|
||||
},
|
||||
} as unknown as IDBFactory,
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
Reflect.deleteProperty(globalThis, "indexedDB");
|
||||
});
|
||||
|
||||
it("stores raw byte sources as a Blob", async () => {
|
||||
const store = createIndexedDbAttachmentStore();
|
||||
const bytes = new Uint8Array([0, 1, 2, 3]);
|
||||
|
||||
const attachment = await store.save({
|
||||
id: "att_bytes",
|
||||
mimeType: "image/png",
|
||||
fileName: "image.png",
|
||||
source: { kind: "bytes", bytes },
|
||||
});
|
||||
|
||||
expect(storedRecord).toEqual({
|
||||
id: "att_bytes",
|
||||
blob: new Blob([bytes], { type: "image/png" }),
|
||||
createdAt: expect.any(Number),
|
||||
fileName: "image.png",
|
||||
});
|
||||
expect(attachment).toMatchObject({
|
||||
id: "att_bytes",
|
||||
mimeType: "image/png",
|
||||
storageType: "web-indexeddb",
|
||||
storageKey: "att_bytes",
|
||||
fileName: "image.png",
|
||||
byteSize: 4,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -74,17 +74,18 @@ function runTx<T>(
|
||||
});
|
||||
}
|
||||
|
||||
function base64ToBlob(input: { base64: string; mimeType: string }): Blob {
|
||||
const binary = atob(input.base64);
|
||||
const bytes = new Uint8Array(binary.length);
|
||||
for (let index = 0; index < binary.length; index += 1) {
|
||||
bytes[index] = binary.charCodeAt(index);
|
||||
}
|
||||
return new Blob([bytes], { type: input.mimeType });
|
||||
}
|
||||
|
||||
async function sourceToBlob(input: SaveAttachmentInput): Promise<{ blob: Blob; mimeType: string }> {
|
||||
const source = input.source;
|
||||
if (source.kind === "bytes") {
|
||||
const mimeType = normalizeMimeType(input.mimeType);
|
||||
const buffer = new ArrayBuffer(source.bytes.byteLength);
|
||||
new Uint8Array(buffer).set(source.bytes);
|
||||
return {
|
||||
blob: new Blob([buffer], { type: mimeType }),
|
||||
mimeType,
|
||||
};
|
||||
}
|
||||
|
||||
if (source.kind === "blob") {
|
||||
const mimeType = normalizeMimeType(input.mimeType ?? source.blob.type);
|
||||
const blob =
|
||||
@@ -105,14 +106,6 @@ async function sourceToBlob(input: SaveAttachmentInput): Promise<{ blob: Blob; m
|
||||
};
|
||||
}
|
||||
|
||||
if (source.kind === "base64") {
|
||||
const mimeType = normalizeMimeType(input.mimeType);
|
||||
return {
|
||||
blob: base64ToBlob({ base64: source.base64, mimeType }),
|
||||
mimeType,
|
||||
};
|
||||
}
|
||||
|
||||
const response = await fetch(source.uri);
|
||||
const blob = await response.blob();
|
||||
const mimeType = normalizeMimeType(input.mimeType ?? blob.type);
|
||||
|
||||
Reference in New Issue
Block a user