6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (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 / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
156 lines
5.6 KiB
TypeScript
156 lines
5.6 KiB
TypeScript
import { isPlainRecord } from '@sim/utils/object'
|
|
import type { FirecrawlFormat, ScrapeOptions } from '@/tools/firecrawl/types'
|
|
|
|
type ProjectedFormat = Record<string, unknown>
|
|
|
|
function modelVisibleFormatFields(format: FirecrawlFormat): ProjectedFormat {
|
|
if (!isPlainRecord(format) || typeof format.type !== 'string') return {}
|
|
|
|
const selected: ProjectedFormat = {}
|
|
if (format.type === 'json' || format.type === 'changeTracking') {
|
|
if (Object.hasOwn(format, 'prompt')) selected.prompt = format.prompt
|
|
if (Object.hasOwn(format, 'schema')) selected.schema = format.schema
|
|
}
|
|
if (format.type === 'question' && Object.hasOwn(format, 'question')) {
|
|
selected.question = format.question
|
|
}
|
|
return selected
|
|
}
|
|
|
|
function haveExactKeys(left: Record<string, unknown>, right: Record<string, unknown>): boolean {
|
|
const leftKeys = Object.keys(left)
|
|
const rightKeys = Object.keys(right)
|
|
return leftKeys.length === rightKeys.length && leftKeys.every((key) => Object.hasOwn(right, key))
|
|
}
|
|
|
|
/** Selects only Firecrawl format fields documented as model instructions or schemas. */
|
|
export function selectFirecrawlFormatModelInput(
|
|
formats: FirecrawlFormat[] | undefined
|
|
): ProjectedFormat[] | undefined {
|
|
return formats?.map(modelVisibleFormatFields)
|
|
}
|
|
|
|
/** Returns whether any requested output format sends page content through a model. */
|
|
export function hasFirecrawlModelInputFormat(formats: FirecrawlFormat[] | undefined): boolean {
|
|
return (
|
|
formats?.some(
|
|
(format) =>
|
|
(typeof format === 'string' &&
|
|
(format === 'json' ||
|
|
format === 'changeTracking' ||
|
|
format === 'question' ||
|
|
format === 'summary')) ||
|
|
(isPlainRecord(format) &&
|
|
(format.type === 'json' ||
|
|
format.type === 'changeTracking' ||
|
|
format.type === 'question' ||
|
|
format.type === 'summary'))
|
|
) ?? false
|
|
)
|
|
}
|
|
|
|
function isPdfFile(input: unknown): boolean {
|
|
if (!isPlainRecord(input)) return false
|
|
if (
|
|
typeof input.type === 'string' &&
|
|
input.type.split(';', 1)[0].trim().toLowerCase() === 'application/pdf'
|
|
) {
|
|
return true
|
|
}
|
|
|
|
return [input.name, input.path, input.url, input.key].some((candidate) => {
|
|
if (typeof candidate !== 'string') return false
|
|
return candidate.split(/[?#]/, 1)[0].trim().toLowerCase().endsWith('.pdf')
|
|
})
|
|
}
|
|
|
|
function hasModelBoundPdfParser(parsers: unknown): boolean {
|
|
if (!Array.isArray(parsers)) return false
|
|
return parsers.some((parser) => {
|
|
if (typeof parser === 'string') return parser.trim().toLowerCase() === 'pdf'
|
|
if (
|
|
!isPlainRecord(parser) ||
|
|
typeof parser.type !== 'string' ||
|
|
parser.type.trim().toLowerCase() !== 'pdf'
|
|
) {
|
|
return false
|
|
}
|
|
return typeof parser.mode !== 'string' || parser.mode.trim().toLowerCase() !== 'fast'
|
|
})
|
|
}
|
|
|
|
/** Returns whether Firecrawl can send the uploaded document through generation or OCR. */
|
|
export function hasFirecrawlParseModelInput(params: {
|
|
file: unknown
|
|
formats?: unknown
|
|
parsers?: unknown
|
|
}): boolean {
|
|
const formats = Array.isArray(params.formats)
|
|
? (params.formats.filter(
|
|
(format) => typeof format === 'string' || isPlainRecord(format)
|
|
) as FirecrawlFormat[])
|
|
: undefined
|
|
if (hasFirecrawlModelInputFormat(formats)) return true
|
|
if (Array.isArray(params.parsers) && params.parsers.length > 0) {
|
|
return hasModelBoundPdfParser(params.parsers)
|
|
}
|
|
return isPdfFile(params.file)
|
|
}
|
|
|
|
/** Reapplies projected Firecrawl format leaves without changing format controls or array shape. */
|
|
export function applyFirecrawlFormatModelInput(
|
|
original: FirecrawlFormat[] | undefined,
|
|
projected: unknown
|
|
): FirecrawlFormat[] | undefined {
|
|
if (original === undefined) {
|
|
if (projected !== undefined) throw new Error('Unexpected projected Firecrawl formats')
|
|
return undefined
|
|
}
|
|
if (!Array.isArray(projected) || projected.length !== original.length) {
|
|
throw new Error('Projected Firecrawl formats do not match the original formats')
|
|
}
|
|
|
|
return original.map((format, index) => {
|
|
const expected = modelVisibleFormatFields(format)
|
|
const projectedFormat = projected[index]
|
|
if (!isPlainRecord(projectedFormat) || !haveExactKeys(expected, projectedFormat)) {
|
|
throw new Error('Projected Firecrawl format fields do not match the original format')
|
|
}
|
|
return isPlainRecord(format) ? { ...format, ...projectedFormat } : format
|
|
})
|
|
}
|
|
|
|
/** Selects nested model-visible fields from Firecrawl scrape options. */
|
|
export function selectFirecrawlScrapeOptionsModelInput(
|
|
options: ScrapeOptions | undefined
|
|
): Record<string, unknown> | undefined {
|
|
if (options === undefined) return undefined
|
|
return Object.hasOwn(options, 'formats')
|
|
? { formats: selectFirecrawlFormatModelInput(options.formats) }
|
|
: {}
|
|
}
|
|
|
|
/** Reapplies projected scrape-option fields while preserving every non-model option. */
|
|
export function applyFirecrawlScrapeOptionsModelInput(
|
|
original: ScrapeOptions | undefined,
|
|
projected: unknown
|
|
): ScrapeOptions | undefined {
|
|
if (original === undefined) {
|
|
if (projected !== undefined) throw new Error('Unexpected projected Firecrawl scrape options')
|
|
return undefined
|
|
}
|
|
if (!isPlainRecord(projected)) {
|
|
throw new Error('Projected Firecrawl scrape options are invalid')
|
|
}
|
|
|
|
const expected = selectFirecrawlScrapeOptionsModelInput(original) ?? {}
|
|
if (!haveExactKeys(expected, projected)) {
|
|
throw new Error('Projected Firecrawl scrape options do not match the original options')
|
|
}
|
|
if (!Object.hasOwn(projected, 'formats')) return { ...original }
|
|
return {
|
|
...original,
|
|
formats: applyFirecrawlFormatModelInput(original.formats, projected.formats),
|
|
}
|
|
}
|