Files
simstudioai--sim/apps/sim/lib/copilot/tools/server/files/doc-compiled-store.ts
T
wehub-resource-sync d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

72 lines
2.4 KiB
TypeScript

import { createHash } from 'node:crypto'
import { createLogger } from '@sim/logger'
import { getErrorMessage, toError } from '@sim/utils/errors'
import { downloadFile, uploadFile } from '@/lib/uploads/core/storage-service'
const logger = createLogger('CopilotDocCompiledStore')
/**
* Compiled-artifact store for Python-generated documents.
*
* The Python doc path keeps the SOURCE as the primary file (the agent reads and
* edits it exactly like the JS path). The compiled binary is stored as its own
* S3 object, content-addressed by (workspaceId, sha256(source), ext) — the hash
* is in the key, so when the source changes the key changes. Every read path
* (serve, preview, /compiled) loads the artifact for the current source hash and
* recompiles only when it is absent. No fileId in the key means any site with
* the source (e.g. the serve route) can find it. S3 is cheap; stale artifacts
* are inert.
*/
function compiledArtifactKey(workspaceId: string, source: string, ext: string): string {
const hash = createHash('sha256').update(source, 'utf-8').digest('hex')
return `copilot-doc-compiled/${workspaceId}/${hash}.${ext}`
}
/** Loads the compiled binary for the current source, or null if not yet built. */
export async function loadCompiledDoc(
workspaceId: string,
source: string,
ext: string
): Promise<Buffer | null> {
const key = compiledArtifactKey(workspaceId, source, ext)
try {
return await downloadFile({ key, context: 'copilot' })
} catch {
return null
}
}
/**
* Stores the compiled binary as the source's associated S3 artifact.
*
* Throws on failure (does not swallow): the serve route is load-only and cannot
* self-heal a missing artifact, so a silent store failure would make a write
* report success while leaving the document unrenderable. Propagating lets the
* write fail honestly so the caller (and the agent) can retry.
*/
export async function storeCompiledDoc(
workspaceId: string,
source: string,
ext: string,
contentType: string,
binary: Buffer
): Promise<void> {
const key = compiledArtifactKey(workspaceId, source, ext)
try {
await uploadFile({
file: binary,
fileName: `doc.${ext}`,
contentType,
context: 'copilot',
customKey: key,
preserveKey: true,
})
} catch (err) {
logger.error('Failed to store compiled doc artifact', {
key,
error: getErrorMessage(err),
})
throw toError(err)
}
}