Files
simstudioai--sim/apps/sim/tools/enrichment/run.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
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
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

94 lines
3.1 KiB
TypeScript

import { ALL_ENRICHMENTS } from '@/enrichments'
import { mapFieldType } from '@/enrichments/providers'
import type { EnrichmentRunParams, EnrichmentRunResponse } from '@/tools/enrichment/types'
import type { OutputProperty, ToolConfig } from '@/tools/types'
/** Union of every distinct output across all registry enrichments. */
const enrichmentOutputs: Record<string, OutputProperty> = {}
for (const enrichment of ALL_ENRICHMENTS) {
for (const output of enrichment.outputs) {
if (!enrichmentOutputs[output.id]) {
enrichmentOutputs[output.id] = {
type: mapFieldType(output.type),
description: `${output.name} (from the selected enrichment)`,
optional: true,
}
}
}
}
/**
* Runs a registry enrichment via `/api/tools/enrichment/run`. Selected and fed
* by the Enrichment block; the route runs the provider cascade with the
* workspace's hosted / BYOK key.
*/
export const enrichmentRunTool: ToolConfig<EnrichmentRunParams, EnrichmentRunResponse> = {
id: 'enrichment_run',
name: 'Run Enrichment',
description: 'Run a Sim enrichment (e.g. Work Email, Phone Number) and return its outputs',
version: '1.0.0',
params: {
enrichmentId: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Registry enrichment id (e.g. "work-email")',
},
inputs: {
type: 'json',
required: true,
visibility: 'user-or-llm',
description: "Map of the enrichment's input ids to values",
},
},
request: {
url: '/api/tools/enrichment/run',
method: 'POST',
headers: () => ({ 'Content-Type': 'application/json' }),
body: (params: EnrichmentRunParams & { _context?: { workspaceId?: string } }) => ({
enrichmentId: params.enrichmentId,
inputs: params.inputs ?? {},
workspaceId: params._context?.workspaceId,
}),
},
transformResponse: async (response: Response) => {
const data = await response.json()
if (!response.ok || data.error) {
return {
success: false,
output: { matched: false, provider: null },
error: data.error || `Enrichment failed (${response.status})`,
}
}
const result = (data.result ?? {}) as Record<string, unknown>
const cost = typeof data.cost === 'number' ? data.cost : 0
const provider = typeof data.provider === 'string' ? data.provider : null
return {
success: true,
output: {
...result,
matched: Boolean(data.matched),
provider,
// Surface hosted-key cost so the workflow logging session bills it,
// matching the convention used by hosted-key tools.
...(cost > 0 ? { cost: { total: cost } } : {}),
},
}
},
// Reserved keys go LAST so they always win if an enrichment ever declares an
// output id of `matched` or `provider` (later spread / assignment wins in JS).
outputs: {
...enrichmentOutputs,
matched: { type: 'boolean', description: 'Whether the enrichment found a result' },
provider: {
type: 'string',
description: 'Provider whose result was returned (e.g. "Hunter", "People Data Labs")',
optional: true,
},
},
}