|
| 1 | +import { EnrichmentIcon } from '@/components/icons' |
| 2 | +import type { BlockConfig, OutputFieldDefinition, ParamType } from '@/blocks/types' |
| 3 | +import { IntegrationType } from '@/blocks/types' |
| 4 | +import { ALL_ENRICHMENTS, getEnrichment } from '@/enrichments' |
| 5 | +import { mapFieldType } from '@/enrichments/providers' |
| 6 | +import type { EnrichmentOutputField } from '@/enrichments/types' |
| 7 | +import type { EnrichmentRunResponse } from '@/tools/enrichment/types' |
| 8 | + |
| 9 | +/** Stable subBlock id for an enrichment input (unique across enrichments). */ |
| 10 | +const inputFieldId = (enrichmentId: string, inputId: string) => `${enrichmentId}__${inputId}` |
| 11 | + |
| 12 | +// One input field per (enrichment, input), shown only for its enrichment. |
| 13 | +const inputSubBlocks = ALL_ENRICHMENTS.flatMap((enrichment) => |
| 14 | + enrichment.inputs.map((input) => ({ |
| 15 | + id: inputFieldId(enrichment.id, input.id), |
| 16 | + title: input.name, |
| 17 | + type: 'short-input' as const, |
| 18 | + placeholder: input.description ?? `Enter ${input.name.toLowerCase()}`, |
| 19 | + condition: { field: 'operation', value: enrichment.id }, |
| 20 | + required: input.required ? ({ field: 'operation', value: enrichment.id } as const) : undefined, |
| 21 | + })) |
| 22 | +) |
| 23 | + |
| 24 | +// Block input schema: the operation plus every per-enrichment input field. |
| 25 | +const blockInputs: Record<string, { type: ParamType; description: string }> = { |
| 26 | + operation: { type: 'string', description: 'Enrichment to run' }, |
| 27 | +} |
| 28 | +for (const enrichment of ALL_ENRICHMENTS) { |
| 29 | + for (const input of enrichment.inputs) { |
| 30 | + blockInputs[inputFieldId(enrichment.id, input.id)] = { |
| 31 | + type: mapFieldType(input.type), |
| 32 | + description: `${input.name} (for ${enrichment.name})`, |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// Union of all enrichment outputs, each shown only for the enrichment(s) that |
| 38 | +// produce it. |
| 39 | +const outputProducers = new Map<string, { field: EnrichmentOutputField; operations: string[] }>() |
| 40 | +for (const enrichment of ALL_ENRICHMENTS) { |
| 41 | + for (const output of enrichment.outputs) { |
| 42 | + const entry = outputProducers.get(output.id) ?? { field: output, operations: [] } |
| 43 | + entry.operations.push(enrichment.id) |
| 44 | + outputProducers.set(output.id, entry) |
| 45 | + } |
| 46 | +} |
| 47 | +// Seed the enrichment outputs first so the reserved `matched` / `provider` |
| 48 | +// keys (assigned below) always win if a future enrichment ever declares an |
| 49 | +// output id that collides with them. |
| 50 | +const blockOutputs: Record<string, OutputFieldDefinition> = {} |
| 51 | +for (const [id, { field, operations }] of outputProducers) { |
| 52 | + blockOutputs[id] = { |
| 53 | + type: mapFieldType(field.type), |
| 54 | + description: field.name, |
| 55 | + condition: { field: 'operation', value: operations }, |
| 56 | + } |
| 57 | +} |
| 58 | +blockOutputs.matched = { |
| 59 | + type: 'boolean', |
| 60 | + description: 'Whether the enrichment found a result', |
| 61 | +} |
| 62 | +blockOutputs.provider = { |
| 63 | + type: 'string', |
| 64 | + description: 'Provider whose result was returned (e.g. "Hunter", "People Data Labs")', |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Enrichment block — runs a code-defined Sim enrichment (Work Email, Phone |
| 69 | + * Number, Company Domain, Company Info, …) and returns its outputs. Generated |
| 70 | + * from the enrichment registry, so new enrichments appear automatically. Runs |
| 71 | + * on the workspace's hosted / BYOK key (injected server-side); no credential. |
| 72 | + */ |
| 73 | +export const EnrichmentBlock: BlockConfig<EnrichmentRunResponse> = { |
| 74 | + type: 'enrichment', |
| 75 | + name: 'Data Enrichment', |
| 76 | + description: 'Enrich data with a Sim enrichment', |
| 77 | + longDescription: |
| 78 | + 'Run a Sim enrichment to look up data — work email, phone number, company domain, company info, and more — from the fields you map in. Uses the same provider cascade as table enrichments.', |
| 79 | + docsLink: 'https://docs.sim.ai/tools/enrichment', |
| 80 | + category: 'tools', |
| 81 | + integrationType: IntegrationType.Sales, |
| 82 | + tags: ['enrichment'], |
| 83 | + bgColor: '#9333EA', |
| 84 | + icon: EnrichmentIcon, |
| 85 | + |
| 86 | + subBlocks: [ |
| 87 | + { |
| 88 | + id: 'operation', |
| 89 | + title: 'Enrichment', |
| 90 | + type: 'dropdown', |
| 91 | + options: ALL_ENRICHMENTS.map((e) => ({ label: e.name, id: e.id })), |
| 92 | + value: () => ALL_ENRICHMENTS[0]?.id ?? '', |
| 93 | + }, |
| 94 | + ...inputSubBlocks, |
| 95 | + ], |
| 96 | + |
| 97 | + tools: { |
| 98 | + access: ['enrichment_run'], |
| 99 | + config: { |
| 100 | + tool: () => 'enrichment_run', |
| 101 | + params: (params) => { |
| 102 | + const enrichment = getEnrichment(params.operation) |
| 103 | + const inputs: Record<string, unknown> = {} |
| 104 | + if (enrichment) { |
| 105 | + for (const input of enrichment.inputs) { |
| 106 | + const value = params[inputFieldId(enrichment.id, input.id)] |
| 107 | + if (value !== undefined && value !== '') inputs[input.id] = value |
| 108 | + } |
| 109 | + } |
| 110 | + return { enrichmentId: params.operation, inputs } |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + |
| 115 | + inputs: blockInputs, |
| 116 | + outputs: blockOutputs, |
| 117 | +} |
0 commit comments