Files
oh-my-pi/packages/coding-agent/src/session/blob-store.ts
T
can1357 aa69f5dd3d feat(coding-agent): refactored screenshot and image handling for PNG-only format and improved memory efficiency
- Changed screenshot format to always use PNG instead of supporting JPEG with quality parameter.
- Changed default extract_readable format from text to markdown.
- Changed screenshot storage to use temporary directory with Snowflake IDs instead of artifacts directory.
- Changed ResizedImage interface to return buffer as Uint8Array with lazy-loaded base64 data getter for improved memory efficiency.
- Removed JPEG quality parameter from screenshot options.
- Removed ability to save screenshots to custom paths or artifacts directory.
2026-02-06 09:20:32 +01:00

104 lines
2.9 KiB
TypeScript

import * as fs from "node:fs/promises";
import * as path from "node:path";
import { isEnoent, logger } from "@oh-my-pi/pi-utils";
const BLOB_PREFIX = "blob:sha256:";
export interface BlobPutResult {
hash: string;
path: string;
get ref(): string;
}
/**
* Content-addressed blob store for externalizing large binary data (images) from session JSONL files.
*
* Files are stored at `<dir>/<sha256-hex>` with no extension. The SHA-256 hash is computed
* over the raw binary data (not base64). Content-addressing makes writes idempotent and
* provides automatic deduplication across sessions.
*/
export class BlobStore {
constructor(readonly dir: string) {}
/**
* Write binary data to the blob store.
* @returns SHA-256 hex hash of the data
*/
async put(data: Buffer): Promise<BlobPutResult> {
const hash = new Bun.CryptoHasher("sha256").update(data).digest("hex");
const blobPath = path.join(this.dir, hash);
const result = {
hash,
path: blobPath,
get ref() {
return `${BLOB_PREFIX}${hash}`;
},
};
await Bun.write(blobPath, data);
return result;
}
/** Read blob by hash, returns Buffer or null if not found. */
async get(hash: string): Promise<Buffer | null> {
const blobPath = path.join(this.dir, hash);
try {
const file = Bun.file(blobPath);
const ab = await file.arrayBuffer();
return Buffer.from(ab);
} catch (err) {
if (isEnoent(err)) return null;
throw err;
}
}
/** Check if a blob exists. */
async has(hash: string): Promise<boolean> {
try {
await fs.access(path.join(this.dir, hash));
return true;
} catch {
return false;
}
}
}
/** Check if a data string is a blob reference. */
export function isBlobRef(data: string): boolean {
return data.startsWith(BLOB_PREFIX);
}
/** Extract the SHA-256 hash from a blob reference string. */
export function parseBlobRef(data: string): string | null {
if (!data.startsWith(BLOB_PREFIX)) return null;
return data.slice(BLOB_PREFIX.length);
}
/**
* Externalize an image's base64 data to the blob store, returning a blob reference.
* If the data is already a blob reference, returns it unchanged.
*/
export async function externalizeImageData(blobStore: BlobStore, base64Data: string): Promise<string> {
if (isBlobRef(base64Data)) return base64Data;
const buffer = Buffer.from(base64Data, "base64");
const { ref } = await blobStore.put(buffer);
return ref;
}
/**
* Resolve a blob reference back to base64 data.
* If the data is not a blob reference, returns it unchanged.
* If the blob is missing, logs a warning and returns a placeholder.
*/
export async function resolveImageData(blobStore: BlobStore, data: string): Promise<string> {
const hash = parseBlobRef(data);
if (!hash) return data;
const buffer = await blobStore.get(hash);
if (!buffer) {
logger.warn("Blob not found for image reference", { hash });
return data; // Return the ref as-is; downstream will see invalid base64 but won't crash
}
return buffer.toString("base64");
}