sailfork / t3code-github / apps/ server/ src/ attachmentStore.ts

// @effect-diagnostics nodeBuiltinImport:off
import * as NodeCrypto from "node:crypto";
import * as NodeFS from "node:fs";
import * as NodePath from "node:path";

import type { ChatAttachment } from "@t3tools/contracts";
import {
  htmlRenderFromToolItem,
  mcpAppFromToolItem,
  previewRecordingIdFromToolItem,
} from "@t3tools/shared/toolOutput";

import {
  normalizeAttachmentRelativePath,
  resolveAttachmentRelativePath,
} from "./attachmentPaths.ts";
import { inferImageExtension, SAFE_IMAGE_FILE_EXTENSIONS } from "./imageMime.ts";

const ATTACHMENT_FILENAME_EXTENSIONS = [...SAFE_IMAGE_FILE_EXTENSIONS, ".bin"];
export const ATTACHMENT_ID_THREAD_SEGMENT_MAX_CHARS = 80;
const ATTACHMENT_ID_THREAD_SEGMENT_PATTERN = "[a-z0-9_]+(?:-[a-z0-9_]+)*";
const ATTACHMENT_ID_UUID_PATTERN = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
const ATTACHMENT_ID_FILE_EXTENSION_PATTERN = "[a-z0-9]{1,10}";
const ATTACHMENT_ID_PATTERN = new RegExp(
  `^(${ATTACHMENT_ID_THREAD_SEGMENT_PATTERN})-(${ATTACHMENT_ID_UUID_PATTERN})(?:-(${ATTACHMENT_ID_FILE_EXTENSION_PATTERN}))?

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	apps/server/src/attachmentStore.ts at main · sailfork/t3code-github · Sailfork
	
		
, "i", ); export const PENDING_ATTACHMENT_THREAD_SEGMENT = "pending"; const PENDING_ATTACHMENT_MAX_AGE_MS = 24 * 60 * 60 * 1000; const PARTIAL_UPLOAD_MAX_AGE_MS = 60 * 60 * 1000; export function toSafeThreadAttachmentSegment(threadId: string): string | null { const segment = threadId .trim() .toLowerCase() .replace(/[^a-z0-9_-]+/gi, "-") .replace(/-+/g, "-") .replace(/^[-_]+|[-_]+$/g, "") .slice(0, ATTACHMENT_ID_THREAD_SEGMENT_MAX_CHARS) .replace(/[-_]+$/g, ""); if (segment.length === 0) { return null; } return segment === PENDING_ATTACHMENT_THREAD_SEGMENT ? "_pending" : segment; } export function attachmentFileExtension(fileName: string): string { const extension = NodePath.extname(fileName).toLowerCase(); // .part is reserved for pending uploads; the stale-upload sweep would delete stored files. if (extension === ".part" || !/^\.[a-z0-9]{1,10}$/.test(extension)) { return ".bin"; } return extension; } function attachmentIdExtensionSuffix(extension: string | undefined): string { if (!extension) { return ""; } const normalized = extension.replace(/^\./, "").toLowerCase(); return new RegExp(`^${ATTACHMENT_ID_FILE_EXTENSION_PATTERN} apps/server/src/attachmentStore.ts at main · sailfork/t3code-github · Sailfork
).test(normalized) ? `-${normalized}` : "-bin"; } export function createPendingAttachmentId(extension?: string): string { return `${PENDING_ATTACHMENT_THREAD_SEGMENT}-${NodeCrypto.randomUUID()}${attachmentIdExtensionSuffix(extension)}`; } export function parseAttachmentUuid(attachmentId: string): string | null { const normalizedId = normalizeAttachmentRelativePath(attachmentId); if (!normalizedId || normalizedId.includes("/") || normalizedId.includes(".")) { return null; } return normalizedId.match(ATTACHMENT_ID_PATTERN)?.[2]?.toLowerCase() ?? null; } export function parseAttachmentFileExtension(attachmentId: string): string | null { const normalizedId = normalizeAttachmentRelativePath(attachmentId); if (!normalizedId || normalizedId.includes("/") || normalizedId.includes(".")) { return null; } return normalizedId.match(ATTACHMENT_ID_PATTERN)?.[3]?.toLowerCase() ?? null; } export function createAttachmentId(threadId: string, extension?: string): string | null { const threadSegment = toSafeThreadAttachmentSegment(threadId); if (!threadSegment) { return null; } return `${threadSegment}-${NodeCrypto.randomUUID()}${attachmentIdExtensionSuffix(extension)}`; } export function createDeterministicAttachmentId( threadId: string, stableKey: string, ): string | null { const threadSegment = toSafeThreadAttachmentSegment(threadId); if (!threadSegment) return null; const hash = NodeCrypto.createHash("sha256") .update(JSON.stringify([threadId, stableKey])) .digest("hex") .slice(0, 32); const uuid = `${hash.slice(0, 8)}-${hash.slice(8, 12)}-${hash.slice(12, 16)}-${hash.slice(16, 20)}-${hash.slice(20)}`; return `${threadSegment}-${uuid}`; } /** Retries keep ids; changed bytes must get new ids because clients cache images by id. */ export function chatAttachmentStableKey( messageId: string, index: number, bytes: Uint8Array, ): string { const digest = NodeCrypto.createHash("sha256").update(bytes).digest("hex"); return `${messageId}:${index}:${digest}`; } export function parseThreadSegmentFromAttachmentId(attachmentId: string): string | null { const normalizedId = normalizeAttachmentRelativePath(attachmentId); if (!normalizedId || normalizedId.includes("/") || normalizedId.includes(".")) { return null; } const match = normalizedId.match(ATTACHMENT_ID_PATTERN); if (!match) { return null; } return match[1]?.toLowerCase() ?? null; } /** Only this thread's ids qualify, preserving source pages when deleting a fork. */ export function threadHtmlRenderAttachmentIds( threadId: string, items: Iterable<{ readonly toolName: string | null | undefined; readonly output?: unknown }>, ) { const segment = toSafeThreadAttachmentSegment(threadId); if (segment === null) return []; return Array.from(items).flatMap((item) => { const attachmentId = htmlRenderFromToolItem(item)?.attachmentId ?? mcpAppFromToolItem(item)?.attachmentId ?? previewRecordingIdFromToolItem(item); return attachmentId !== undefined && parseThreadSegmentFromAttachmentId(attachmentId) === segment ? [attachmentId] : []; }); } export function attachmentRelativePath(attachment: ChatAttachment): string | null { switch (attachment.type) { case "image": { const extension = inferImageExtension({ mimeType: attachment.mimeType, fileName: attachment.name, }); return `${attachment.id}${extension}`; } case "file": return `${attachment.id}${attachmentFileExtension(attachment.name)}`; default: return null; } } export function resolveAttachmentPath(input: { readonly attachmentsDir: string; readonly attachment: ChatAttachment; }): string | null { const relativePath = attachmentRelativePath(input.attachment); if (!relativePath) { return null; } return resolveAttachmentRelativePath({ attachmentsDir: input.attachmentsDir, relativePath, }); } export function resolveAttachmentPathById(input: { readonly attachmentsDir: string; readonly attachmentId: string; }): string | null { const normalizedId = normalizeAttachmentRelativePath(input.attachmentId); if (!normalizedId || normalizedId.includes("/") || normalizedId.includes(".")) { return null; } const fileExtension = parseAttachmentFileExtension(normalizedId); if (fileExtension) { const filePath = resolveAttachmentRelativePath({ attachmentsDir: input.attachmentsDir, relativePath: `${normalizedId}.${fileExtension.toLowerCase()}`, }); return filePath && NodeFS.existsSync(filePath) ? filePath : null; } for (const extension of ATTACHMENT_FILENAME_EXTENSIONS) { const maybePath = resolveAttachmentRelativePath({ attachmentsDir: input.attachmentsDir, relativePath: `${normalizedId}${extension}`, }); if (maybePath && NodeFS.existsSync(maybePath)) { return maybePath; } } return null; } export type AttachmentClaimPlan = | { readonly ok: true; readonly finalId: string; readonly currentPath: string; readonly finalPath: string; } | { readonly ok: false; readonly reason: string }; export function planAttachmentClaim(input: { readonly attachmentsDir: string; readonly threadId: string; readonly attachmentId: string; }): AttachmentClaimPlan { const uuid = parseAttachmentUuid(input.attachmentId); const requestedSegment = parseThreadSegmentFromAttachmentId(input.attachmentId); if (!uuid || !requestedSegment) { return { ok: false, reason: "invalid attachment id" }; } if (!toSafeThreadAttachmentSegment(input.threadId)) { return { ok: false, reason: "invalid thread id" }; } if (requestedSegment !== PENDING_ATTACHMENT_THREAD_SEGMENT) { return { ok: false, reason: "attachment must be a pending upload" }; } const currentPath = resolveAttachmentPathById({ attachmentsDir: input.attachmentsDir, attachmentId: input.attachmentId, }); if (!currentPath) { return { ok: false, reason: "attachment not found (removed or expired)" }; } const fileExtension = parseAttachmentFileExtension(input.attachmentId) ?? undefined; const finalId = createAttachmentId(input.threadId, fileExtension); if (!finalId) { return { ok: false, reason: "failed to create attachment id" }; } const expectedFinalPath = resolveAttachmentRelativePath({ attachmentsDir: input.attachmentsDir, relativePath: `${finalId}${NodePath.extname(currentPath)}`, }); if (!expectedFinalPath) { return { ok: false, reason: "failed to resolve attachment path" }; } return { ok: true, finalId, currentPath, finalPath: expectedFinalPath, }; } export function sweepStalePendingAttachments(input: { readonly attachmentsDir: string; readonly nowMs: number; }): { readonly deleted: number } { let entries: string[]; try { entries = NodeFS.readdirSync(input.attachmentsDir); } catch { return { deleted: 0 }; } let deleted = 0; for (const entry of entries) { const isPartial = entry.endsWith(".part"); if (!isPartial) { const attachmentId = parseAttachmentIdFromRelativePath( entry.endsWith(".txt") && entry.slice(0, -4).includes(".") ? entry.slice(0, -4) : entry, ); if ( !attachmentId || parseThreadSegmentFromAttachmentId(attachmentId) !== PENDING_ATTACHMENT_THREAD_SEGMENT ) { continue; } } const resolved = resolveAttachmentRelativePath({ attachmentsDir: input.attachmentsDir, relativePath: entry, }); if (!resolved) { continue; } try { const maxAgeMs = isPartial ? PARTIAL_UPLOAD_MAX_AGE_MS : PENDING_ATTACHMENT_MAX_AGE_MS; if (input.nowMs - NodeFS.statSync(resolved).mtimeMs > maxAgeMs) { NodeFS.unlinkSync(resolved); deleted += 1; } } catch { continue; } } return { deleted }; } function parseAttachmentIdFromRelativePath(relativePath: string): string | null { const normalized = normalizeAttachmentRelativePath(relativePath); if (!normalized || normalized.includes("/")) { return null; } const extensionIndex = normalized.lastIndexOf("."); if (extensionIndex <= 0) { return null; } const id = normalized.slice(0, extensionIndex); return id.length > 0 && !id.includes(".") ? id : null; }
apps/server/src/attachmentStore.ts at main · sailfork/t3code-github · Sailfork
,\n \"i\",\n);\n\nexport const PENDING_ATTACHMENT_THREAD_SEGMENT = \"pending\";\nconst PENDING_ATTACHMENT_MAX_AGE_MS = 24 * 60 * 60 * 1000;\nconst PARTIAL_UPLOAD_MAX_AGE_MS = 60 * 60 * 1000;\n\nexport function toSafeThreadAttachmentSegment(threadId: string): string | null {\n const segment = threadId\n .trim()\n .toLowerCase()\n .replace(/[^a-z0-9_-]+/gi, \"-\")\n .replace(/-+/g, \"-\")\n .replace(/^[-_]+|[-_]+$/g, \"\")\n .slice(0, ATTACHMENT_ID_THREAD_SEGMENT_MAX_CHARS)\n .replace(/[-_]+$/g, \"\");\n if (segment.length === 0) {\n return null;\n }\n return segment === PENDING_ATTACHMENT_THREAD_SEGMENT ? \"_pending\" : segment;\n}\n\nexport function attachmentFileExtension(fileName: string): string {\n const extension = NodePath.extname(fileName).toLowerCase();\n // .part is reserved for pending uploads; the stale-upload sweep would delete stored files.\n if (extension === \".part\" || !/^\\.[a-z0-9]{1,10}$/.test(extension)) {\n return \".bin\";\n }\n return extension;\n}\n\nfunction attachmentIdExtensionSuffix(extension: string | undefined): string {\n if (!extension) {\n return \"\";\n }\n const normalized = extension.replace(/^\\./, \"\").toLowerCase();\n return new RegExp(`^${ATTACHMENT_ID_FILE_EXTENSION_PATTERN} apps/server/src/attachmentStore.ts at main · sailfork/t3code-github · Sailfork
).test(normalized)\n ? `-${normalized}`\n : \"-bin\";\n}\n\nexport function createPendingAttachmentId(extension?: string): string {\n return `${PENDING_ATTACHMENT_THREAD_SEGMENT}-${NodeCrypto.randomUUID()}${attachmentIdExtensionSuffix(extension)}`;\n}\n\nexport function parseAttachmentUuid(attachmentId: string): string | null {\n const normalizedId = normalizeAttachmentRelativePath(attachmentId);\n if (!normalizedId || normalizedId.includes(\"/\") || normalizedId.includes(\".\")) {\n return null;\n }\n return normalizedId.match(ATTACHMENT_ID_PATTERN)?.[2]?.toLowerCase() ?? null;\n}\n\nexport function parseAttachmentFileExtension(attachmentId: string): string | null {\n const normalizedId = normalizeAttachmentRelativePath(attachmentId);\n if (!normalizedId || normalizedId.includes(\"/\") || normalizedId.includes(\".\")) {\n return null;\n }\n return normalizedId.match(ATTACHMENT_ID_PATTERN)?.[3]?.toLowerCase() ?? null;\n}\n\nexport function createAttachmentId(threadId: string, extension?: string): string | null {\n const threadSegment = toSafeThreadAttachmentSegment(threadId);\n if (!threadSegment) {\n return null;\n }\n return `${threadSegment}-${NodeCrypto.randomUUID()}${attachmentIdExtensionSuffix(extension)}`;\n}\n\nexport function createDeterministicAttachmentId(\n threadId: string,\n stableKey: string,\n): string | null {\n const threadSegment = toSafeThreadAttachmentSegment(threadId);\n if (!threadSegment) return null;\n const hash = NodeCrypto.createHash(\"sha256\")\n .update(JSON.stringify([threadId, stableKey]))\n .digest(\"hex\")\n .slice(0, 32);\n const uuid = `${hash.slice(0, 8)}-${hash.slice(8, 12)}-${hash.slice(12, 16)}-${hash.slice(16, 20)}-${hash.slice(20)}`;\n return `${threadSegment}-${uuid}`;\n}\n\n/** Retries keep ids; changed bytes must get new ids because clients cache images by id. */\nexport function chatAttachmentStableKey(\n messageId: string,\n index: number,\n bytes: Uint8Array,\n): string {\n const digest = NodeCrypto.createHash(\"sha256\").update(bytes).digest(\"hex\");\n return `${messageId}:${index}:${digest}`;\n}\n\nexport function parseThreadSegmentFromAttachmentId(attachmentId: string): string | null {\n const normalizedId = normalizeAttachmentRelativePath(attachmentId);\n if (!normalizedId || normalizedId.includes(\"/\") || normalizedId.includes(\".\")) {\n return null;\n }\n const match = normalizedId.match(ATTACHMENT_ID_PATTERN);\n if (!match) {\n return null;\n }\n return match[1]?.toLowerCase() ?? null;\n}\n\n/** Only this thread's ids qualify, preserving source pages when deleting a fork. */\nexport function threadHtmlRenderAttachmentIds(\n threadId: string,\n items: Iterable\u003c{ readonly toolName: string | null | undefined; readonly output?: unknown }>,\n) {\n const segment = toSafeThreadAttachmentSegment(threadId);\n if (segment === null) return [];\n return Array.from(items).flatMap((item) => {\n const attachmentId =\n htmlRenderFromToolItem(item)?.attachmentId ??\n mcpAppFromToolItem(item)?.attachmentId ??\n previewRecordingIdFromToolItem(item);\n return attachmentId !== undefined &&\n parseThreadSegmentFromAttachmentId(attachmentId) === segment\n ? [attachmentId]\n : [];\n });\n}\n\nexport function attachmentRelativePath(attachment: ChatAttachment): string | null {\n switch (attachment.type) {\n case \"image\": {\n const extension = inferImageExtension({\n mimeType: attachment.mimeType,\n fileName: attachment.name,\n });\n return `${attachment.id}${extension}`;\n }\n case \"file\":\n return `${attachment.id}${attachmentFileExtension(attachment.name)}`;\n default:\n return null;\n }\n}\n\nexport function resolveAttachmentPath(input: {\n readonly attachmentsDir: string;\n readonly attachment: ChatAttachment;\n}): string | null {\n const relativePath = attachmentRelativePath(input.attachment);\n if (!relativePath) {\n return null;\n }\n return resolveAttachmentRelativePath({\n attachmentsDir: input.attachmentsDir,\n relativePath,\n });\n}\n\nexport function resolveAttachmentPathById(input: {\n readonly attachmentsDir: string;\n readonly attachmentId: string;\n}): string | null {\n const normalizedId = normalizeAttachmentRelativePath(input.attachmentId);\n if (!normalizedId || normalizedId.includes(\"/\") || normalizedId.includes(\".\")) {\n return null;\n }\n const fileExtension = parseAttachmentFileExtension(normalizedId);\n if (fileExtension) {\n const filePath = resolveAttachmentRelativePath({\n attachmentsDir: input.attachmentsDir,\n relativePath: `${normalizedId}.${fileExtension.toLowerCase()}`,\n });\n return filePath && NodeFS.existsSync(filePath) ? filePath : null;\n }\n for (const extension of ATTACHMENT_FILENAME_EXTENSIONS) {\n const maybePath = resolveAttachmentRelativePath({\n attachmentsDir: input.attachmentsDir,\n relativePath: `${normalizedId}${extension}`,\n });\n if (maybePath && NodeFS.existsSync(maybePath)) {\n return maybePath;\n }\n }\n return null;\n}\n\nexport type AttachmentClaimPlan =\n | {\n readonly ok: true;\n readonly finalId: string;\n readonly currentPath: string;\n readonly finalPath: string;\n }\n | { readonly ok: false; readonly reason: string };\n\nexport function planAttachmentClaim(input: {\n readonly attachmentsDir: string;\n readonly threadId: string;\n readonly attachmentId: string;\n}): AttachmentClaimPlan {\n const uuid = parseAttachmentUuid(input.attachmentId);\n const requestedSegment = parseThreadSegmentFromAttachmentId(input.attachmentId);\n if (!uuid || !requestedSegment) {\n return { ok: false, reason: \"invalid attachment id\" };\n }\n\n if (!toSafeThreadAttachmentSegment(input.threadId)) {\n return { ok: false, reason: \"invalid thread id\" };\n }\n if (requestedSegment !== PENDING_ATTACHMENT_THREAD_SEGMENT) {\n return { ok: false, reason: \"attachment must be a pending upload\" };\n }\n\n const currentPath = resolveAttachmentPathById({\n attachmentsDir: input.attachmentsDir,\n attachmentId: input.attachmentId,\n });\n if (!currentPath) {\n return { ok: false, reason: \"attachment not found (removed or expired)\" };\n }\n const fileExtension = parseAttachmentFileExtension(input.attachmentId) ?? undefined;\n const finalId = createAttachmentId(input.threadId, fileExtension);\n if (!finalId) {\n return { ok: false, reason: \"failed to create attachment id\" };\n }\n\n const expectedFinalPath = resolveAttachmentRelativePath({\n attachmentsDir: input.attachmentsDir,\n relativePath: `${finalId}${NodePath.extname(currentPath)}`,\n });\n if (!expectedFinalPath) {\n return { ok: false, reason: \"failed to resolve attachment path\" };\n }\n return {\n ok: true,\n finalId,\n currentPath,\n finalPath: expectedFinalPath,\n };\n}\n\nexport function sweepStalePendingAttachments(input: {\n readonly attachmentsDir: string;\n readonly nowMs: number;\n}): { readonly deleted: number } {\n let entries: string[];\n try {\n entries = NodeFS.readdirSync(input.attachmentsDir);\n } catch {\n return { deleted: 0 };\n }\n\n let deleted = 0;\n for (const entry of entries) {\n const isPartial = entry.endsWith(\".part\");\n if (!isPartial) {\n const attachmentId = parseAttachmentIdFromRelativePath(\n entry.endsWith(\".txt\") && entry.slice(0, -4).includes(\".\") ? entry.slice(0, -4) : entry,\n );\n if (\n !attachmentId ||\n parseThreadSegmentFromAttachmentId(attachmentId) !== PENDING_ATTACHMENT_THREAD_SEGMENT\n ) {\n continue;\n }\n }\n\n const resolved = resolveAttachmentRelativePath({\n attachmentsDir: input.attachmentsDir,\n relativePath: entry,\n });\n if (!resolved) {\n continue;\n }\n try {\n const maxAgeMs = isPartial ? PARTIAL_UPLOAD_MAX_AGE_MS : PENDING_ATTACHMENT_MAX_AGE_MS;\n if (input.nowMs - NodeFS.statSync(resolved).mtimeMs > maxAgeMs) {\n NodeFS.unlinkSync(resolved);\n deleted += 1;\n }\n } catch {\n continue;\n }\n }\n\n return { deleted };\n}\n\nfunction parseAttachmentIdFromRelativePath(relativePath: string): string | null {\n const normalized = normalizeAttachmentRelativePath(relativePath);\n if (!normalized || normalized.includes(\"/\")) {\n return null;\n }\n const extensionIndex = normalized.lastIndexOf(\".\");\n if (extensionIndex \u003c= 0) {\n return null;\n }\n const id = normalized.slice(0, extensionIndex);\n return id.length > 0 && !id.includes(\".\") ? id : null;\n}\n","preview":null,"history":null,"version":"a2b531e3fa5b02b5d0a9e05c86431111dcb3f763"},"/_session?shell=1":null}