Files
simstudioai--sim/apps/sim/lib/core/utils/user-file.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

132 lines
3.5 KiB
TypeScript

import type { UserFile } from '@/executor/types'
export type UserFileLike = Pick<UserFile, 'id' | 'name' | 'url' | 'key'> &
Partial<Pick<UserFile, 'size' | 'type' | 'context' | 'base64'>>
/**
* Fields exposed for UserFile objects in UI (tag dropdown) and logs.
* Internal fields like 'key' and 'context' are not exposed.
*/
export const USER_FILE_DISPLAY_FIELDS = ['id', 'name', 'url', 'size', 'type', 'base64'] as const
export type UserFileDisplayField = (typeof USER_FILE_DISPLAY_FIELDS)[number]
/**
* Checks if a value matches the minimal UserFile shape.
*/
export function isUserFile(value: unknown): value is UserFileLike {
if (!value || typeof value !== 'object') {
return false
}
const candidate = value as Record<string, unknown>
return (
typeof candidate.id === 'string' &&
typeof candidate.key === 'string' &&
typeof candidate.url === 'string' &&
typeof candidate.name === 'string'
)
}
/**
* Checks if a value matches the full UserFile metadata shape.
*/
export function isUserFileWithMetadata(value: unknown): value is UserFile {
if (!isUserFile(value)) {
return false
}
const candidate = value as Record<string, unknown>
return typeof candidate.size === 'number' && typeof candidate.type === 'string'
}
/**
* Finds storage keys for UserFile objects embedded in a value.
*/
export function collectUserFileKeys(value: unknown): string[] {
const keys = new Set<string>()
collectUserFileKeysInto(value, keys, new WeakSet<object>())
return Array.from(keys)
}
function collectUserFileKeysInto(value: unknown, keys: Set<string>, seen: WeakSet<object>): void {
if (!value || typeof value !== 'object') {
return
}
if (seen.has(value)) {
return
}
seen.add(value)
if (isUserFileWithMetadata(value)) {
keys.add(value.key)
return
}
if (Array.isArray(value)) {
for (const item of value) {
collectUserFileKeysInto(item, keys, seen)
}
return
}
for (const item of Object.values(value)) {
collectUserFileKeysInto(item, keys, seen)
}
}
/**
* Checks if a value matches the display-safe UserFile metadata shape after internal fields are stripped.
*/
export function isUserFileDisplayMetadata(value: unknown): value is Record<string, unknown> {
if (!value || typeof value !== 'object' || Array.isArray(value)) {
return false
}
const candidate = value as Record<string, unknown>
const url = typeof candidate.url === 'string' ? candidate.url : ''
return (
typeof candidate.id === 'string' &&
typeof candidate.name === 'string' &&
url.length > 0 &&
typeof candidate.size === 'number' &&
typeof candidate.type === 'string' &&
(candidate.id.startsWith('file_') || url.includes('/api/files/serve/'))
)
}
/**
* Filters a UserFile object to only include display fields.
* Used for both UI display and log sanitization.
*/
export function filterUserFileForDisplay(data: Record<string, unknown>): Record<string, unknown> {
const filtered: Record<string, unknown> = {}
for (const field of USER_FILE_DISPLAY_FIELDS) {
if (field in data) {
filtered[field] = data[field]
}
}
return filtered
}
/**
* Extracts base64 content from either a raw base64 string or a UserFile object.
* Useful for tools that accept file input in either format.
* @returns The base64 string, or undefined if not found
*/
export function extractBase64FromFileInput(
input: string | UserFileLike | null | undefined
): string | undefined {
if (typeof input === 'string') {
return input
}
if (input?.base64) {
return input.base64
}
return undefined
}