|
| 1 | +import type { OpenCodeClient, SessionMessageInfo } from "@opencode/client/promise" |
| 2 | +import type { MediaCache } from "@/runtime/server/media" |
| 3 | + |
| 4 | +/** A file in a call's result, by the message it is in, the call's ID and its position in the call's content. */ |
| 5 | +export type ToolFileInput = { |
| 6 | + sessionID: string |
| 7 | + messageID: string |
| 8 | + callID: string |
| 9 | + index: number |
| 10 | + mime: string |
| 11 | +} |
| 12 | + |
| 13 | +export type ToolFileCache = { |
| 14 | + peek(callID: string, index: number): string | undefined |
| 15 | + load(input: ToolFileInput, signal?: AbortSignal): Promise<string | undefined> |
| 16 | + dispose(): void |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Reads tool-result files the store keeps without their bytes (`omitToolFileData`) again from their message, one at a |
| 21 | + * time as they show. A file comes back as a `media:` ref when the server stores media in blobs, and otherwise inline; |
| 22 | + * the inline bytes become an object URL, so the message read is the only copy of them on the heap, and only until it |
| 23 | + * is collected. |
| 24 | + */ |
| 25 | +export function createToolFileCache(getApi: () => Pick<OpenCodeClient, "session">, media: MediaCache): ToolFileCache { |
| 26 | + const urls = new Map<string, string>() |
| 27 | + const owned = new Set<string>() |
| 28 | + const reads = new Map<string, Promise<SessionMessageInfo>>() |
| 29 | + |
| 30 | + let disposed = false |
| 31 | + |
| 32 | + // Tiles of one message ask together, so they share its read. |
| 33 | + const read = (sessionID: string, messageID: string) => { |
| 34 | + const key = `${sessionID}:${messageID}` |
| 35 | + |
| 36 | + const pending = |
| 37 | + reads.get(key) ?? |
| 38 | + getApi() |
| 39 | + .session.message.get({ sessionID, messageID }) |
| 40 | + .finally(() => reads.delete(key)) |
| 41 | + |
| 42 | + reads.set(key, pending) |
| 43 | + |
| 44 | + return pending |
| 45 | + } |
| 46 | + |
| 47 | + return { |
| 48 | + peek(callID, index) { |
| 49 | + if (disposed) return undefined |
| 50 | + |
| 51 | + return urls.get(`${callID}:${index}`) |
| 52 | + }, |
| 53 | + load(input, signal) { |
| 54 | + const key = `${input.callID}:${input.index}` |
| 55 | + const cached = urls.get(key) |
| 56 | + |
| 57 | + if (disposed || signal?.aborted) return Promise.resolve(undefined) |
| 58 | + |
| 59 | + if (cached) return Promise.resolve(cached) |
| 60 | + |
| 61 | + return read(input.sessionID, input.messageID).then((message) => { |
| 62 | + const file = toolFile(message, input.callID, input.index) |
| 63 | + |
| 64 | + if (!file || disposed || signal?.aborted) return undefined |
| 65 | + |
| 66 | + if (!file.uri.startsWith("data:")) { |
| 67 | + return media.load(file.uri, file.mime, signal).then((url) => { |
| 68 | + if (url && !disposed) urls.set(key, url) |
| 69 | + |
| 70 | + return url |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + const url = urls.get(key) ?? URL.createObjectURL(dataBlob(file.uri, file.mime)) |
| 75 | + |
| 76 | + urls.set(key, url) |
| 77 | + owned.add(url) |
| 78 | + |
| 79 | + return url |
| 80 | + }) |
| 81 | + }, |
| 82 | + dispose() { |
| 83 | + disposed = true |
| 84 | + reads.clear() |
| 85 | + owned.forEach((url) => URL.revokeObjectURL(url)) |
| 86 | + owned.clear() |
| 87 | + urls.clear() |
| 88 | + }, |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +function toolFile(message: SessionMessageInfo, callID: string, index: number) { |
| 93 | + if (message.type !== "assistant") return undefined |
| 94 | + const tool = message.content.findLast((part) => part.type === "tool" && part.id === callID) |
| 95 | + |
| 96 | + if (tool?.type !== "tool" || !("content" in tool.state)) return undefined |
| 97 | + const item = tool.state.content?.[index] |
| 98 | + |
| 99 | + return item?.type === "file" ? item : undefined |
| 100 | +} |
| 101 | + |
| 102 | +function dataBlob(uri: string, mime: string) { |
| 103 | + const comma = uri.indexOf(",") |
| 104 | + const body = uri.slice(comma + 1) |
| 105 | + |
| 106 | + if (!uri.slice(0, comma).endsWith(";base64")) return new Blob([decodeURIComponent(body)], { type: mime }) |
| 107 | + |
| 108 | + return new Blob([Uint8Array.from(atob(body), (char) => char.charCodeAt(0))], { type: mime }) |
| 109 | +} |
0 commit comments