Skip to content

Commit 870203c

Browse files
refactor(enrichment): share mapFieldType helper between block and tool
1 parent 0012c2e commit 870203c

3 files changed

Lines changed: 29 additions & 29 deletions

File tree

apps/sim/blocks/blocks/enrichment.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,10 @@ import { EnrichmentIcon } from '@/components/icons'
22
import type { BlockConfig, OutputFieldDefinition, ParamType } from '@/blocks/types'
33
import { IntegrationType } from '@/blocks/types'
44
import { ALL_ENRICHMENTS, getEnrichment } from '@/enrichments'
5-
import type { EnrichmentInputField, EnrichmentOutputField } from '@/enrichments/types'
5+
import { mapFieldType } from '@/enrichments/providers'
6+
import type { EnrichmentOutputField } from '@/enrichments/types'
67
import type { EnrichmentRunResponse } from '@/tools/enrichment/types'
78

8-
/** Maps an enrichment input/output column type to a block field type. */
9-
function fieldType(type: EnrichmentInputField['type'] | EnrichmentOutputField['type']): ParamType {
10-
if (type === 'number') return 'number'
11-
if (type === 'boolean') return 'boolean'
12-
if (type === 'json') return 'json'
13-
return 'string'
14-
}
15-
169
/** Stable subBlock id for an enrichment input (unique across enrichments). */
1710
const inputFieldId = (enrichmentId: string, inputId: string) => `${enrichmentId}__${inputId}`
1811

@@ -35,7 +28,7 @@ const blockInputs: Record<string, { type: ParamType; description: string }> = {
3528
for (const enrichment of ALL_ENRICHMENTS) {
3629
for (const input of enrichment.inputs) {
3730
blockInputs[inputFieldId(enrichment.id, input.id)] = {
38-
type: fieldType(input.type),
31+
type: mapFieldType(input.type),
3932
description: `${input.name} (for ${enrichment.name})`,
4033
}
4134
}
@@ -60,7 +53,7 @@ const blockOutputs: Record<string, OutputFieldDefinition> = {
6053
}
6154
for (const [id, { field, operations }] of outputProducers) {
6255
blockOutputs[id] = {
63-
type: fieldType(field.type),
56+
type: mapFieldType(field.type),
6457
description: field.name,
6558
condition: { field: 'operation', value: operations },
6659
}

apps/sim/enrichments/providers.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1-
import type { EnrichmentProvider } from '@/enrichments/types'
1+
import type {
2+
EnrichmentInputField,
3+
EnrichmentOutputField,
4+
EnrichmentProvider,
5+
} from '@/enrichments/types'
6+
7+
/**
8+
* Narrow union of the field types enrichments declare. Assignable to both the
9+
* tool `OutputType` and the block `ParamType` unions, so a single mapping
10+
* function feeds both sides without per-call casts.
11+
*/
12+
export type EnrichmentFieldType = 'string' | 'number' | 'boolean' | 'json'
13+
14+
/** Maps an enrichment input/output column type to a block/tool field type. */
15+
export function mapFieldType(
16+
type: EnrichmentInputField['type'] | EnrichmentOutputField['type']
17+
): EnrichmentFieldType {
18+
if (type === 'number') return 'number'
19+
if (type === 'boolean') return 'boolean'
20+
if (type === 'json') return 'json'
21+
return 'string'
22+
}
223

324
/** Coerces an unknown input value to a trimmed string (`''` when nullish). */
425
export function str(value: unknown): string {

apps/sim/tools/enrichment/run.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
import { ALL_ENRICHMENTS } from '@/enrichments'
2-
import type { EnrichmentOutputField } from '@/enrichments/types'
2+
import { mapFieldType } from '@/enrichments/providers'
33
import type { EnrichmentRunParams, EnrichmentRunResponse } from '@/tools/enrichment/types'
4-
import type { OutputProperty, OutputType, ToolConfig } from '@/tools/types'
5-
6-
/** Maps an enrichment output's column type to a tool OutputType. */
7-
function toOutputType(type: EnrichmentOutputField['type']): OutputType {
8-
switch (type) {
9-
case 'number':
10-
return 'number'
11-
case 'boolean':
12-
return 'boolean'
13-
case 'json':
14-
return 'json'
15-
default:
16-
return 'string'
17-
}
18-
}
4+
import type { OutputProperty, ToolConfig } from '@/tools/types'
195

206
/** Union of every distinct output across all registry enrichments. */
217
const enrichmentOutputs: Record<string, OutputProperty> = {}
228
for (const enrichment of ALL_ENRICHMENTS) {
239
for (const output of enrichment.outputs) {
2410
if (!enrichmentOutputs[output.id]) {
2511
enrichmentOutputs[output.id] = {
26-
type: toOutputType(output.type),
12+
type: mapFieldType(output.type),
2713
description: `${output.name} (from the selected enrichment)`,
2814
optional: true,
2915
}

0 commit comments

Comments
 (0)