import { AppShotCapture, type FileAsset, type ResourceRef, type SessionAuthorizationActor, } from "@opengeni/contracts"; import { sql } from "drizzle-orm"; import { rawRows, withRlsContext, type Database } from "./database"; /** Only call during verified human initial/Send/Steer acceptance, in its transaction. */ export async function acceptSessionFileAttachments( db: Database, input: { accountId: string; workspaceId: string; sessionId: string; turnId: string; subjectId: string; resources: ResourceRef[]; }, ): Promise { const ids = [...new Set(input.resources.flatMap((r) => (r.kind === "file" ? [r.fileId] : [])))]; if (!ids.length) return; await withRlsContext(db, input, async (tx) => { await tx.execute(sql`select opengeni_private.accept_session_file_attachments( ${input.accountId}::uuid,${input.workspaceId}::uuid,${input.sessionId}::uuid,${input.turnId}::uuid, ${input.subjectId},ARRAY(select jsonb_array_elements_text(${JSON.stringify(ids)}::jsonb))::uuid[])`); }); } /** Called only after canonical archive events commit within the same transaction. * The existing SQL lifecycle proves the immutable importer and completed upload; * passing NULL never creates or authorizes an execution turn. */ export async function acceptArchivedSessionFileAttachments( db: Database, input: { accountId: string; workspaceId: string; sessionId: string; subjectId: string; fileIds: readonly string[]; }, ): Promise { const ids = [...new Set(input.fileIds)]; if (!ids.length) return; await withRlsContext(db, input, async (tx) => { for (let offset = 0; offset < ids.length; offset += 1000) { await tx.execute(sql`select opengeni_private.accept_session_file_attachments( ${input.accountId}::uuid,${input.workspaceId}::uuid,${input.sessionId}::uuid,NULL::uuid, ${input.subjectId},ARRAY(select jsonb_array_elements_text(${JSON.stringify(ids.slice(offset, offset + 1000))}::jsonb))::uuid[])`); } }); } export type SessionAttachmentReadAccess = { sessionId: string; authorityEpoch: number; actor: Pick & Partial<{ callerSessionId: string; turnId: string; attemptId: string; executionGeneration: number; }>; }; /** Core must prove session authorization; workers supply their live own-session attempt. */ export async function readSessionFileAttachments( db: Database, input: { accountId: string; workspaceId: string; fileIds: readonly string[]; access: SessionAttachmentReadAccess; }, ): Promise { if (!input.fileIds.length) return []; return withRlsContext(db, input, async (tx) => { const rows = await rawRows & { appShot: unknown }>( tx, sql` select id,case when private_owner_subject_ids is null then 'workspace' else 'personal' end as scope,workspace_id as "workspaceId",status,filename, safe_filename as "safeFilename",content_type as "contentType",size_bytes::double precision as "sizeBytes", sha256,bucket,object_key as "objectKey",created_at::text as "createdAt",updated_at::text as "updatedAt", app_shot as "appShot" from opengeni_private.read_session_file_attachments( ${input.accountId}::uuid,${input.workspaceId}::uuid,${input.access.sessionId}::uuid, ${input.access.authorityEpoch},ARRAY(select jsonb_array_elements_text(${JSON.stringify([...new Set(input.fileIds)])}::jsonb))::uuid[], ${JSON.stringify(input.access.actor)}::jsonb)`, ); return rows.map(({ appShot, ...row }) => { const capture = appShot == null ? null : AppShotCapture.safeParse(appShot); return { ...row, createdAt: new Date(row.createdAt).toISOString(), updatedAt: new Date(row.updatedAt).toISOString(), ...(capture?.success ? { appShot: capture.data } : {}), }; }); }); }