Skip to content

Commit cca1772

Browse files
committed
simplify
1 parent e4dd14d commit cca1772

File tree

2 files changed

+105
-47
lines changed

2 files changed

+105
-47
lines changed

apps/sim/lib/table/llm/enrichment.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,92 @@
55
* with table-specific information so LLMs can construct proper queries.
66
*/
77

8+
import { createLogger } from '@sim/logger'
89
import type { TableSummary } from '../types'
910

11+
const logger = createLogger('TableLLMEnrichment')
12+
13+
export interface TableEnrichmentContext {
14+
workspaceId: string
15+
workflowId: string
16+
executeTool: (toolId: string, params: Record<string, any>) => Promise<any>
17+
}
18+
19+
export interface TableEnrichmentResult {
20+
description: string
21+
parameters: {
22+
properties: Record<string, any>
23+
required: string[]
24+
}
25+
}
26+
27+
/**
28+
* Enriches a table tool for LLM consumption by fetching its schema
29+
* and injecting column information into the description and parameters.
30+
*
31+
* @param toolId - The table tool ID (e.g., 'table_query_rows')
32+
* @param originalDescription - The tool's original description
33+
* @param llmSchema - The original LLM schema
34+
* @param userProvidedParams - Parameters provided by the user (must include tableId)
35+
* @param context - Execution context with workspaceId, workflowId, and executeTool
36+
* @returns Enriched description and parameters, or null if enrichment not applicable
37+
*/
38+
export async function enrichTableToolForLLM(
39+
toolId: string,
40+
originalDescription: string,
41+
llmSchema: { properties?: Record<string, any>; required?: string[] },
42+
userProvidedParams: Record<string, any>,
43+
context: TableEnrichmentContext
44+
): Promise<TableEnrichmentResult | null> {
45+
const { tableId } = userProvidedParams
46+
47+
// Need a tableId to fetch schema
48+
if (!tableId) {
49+
return null
50+
}
51+
52+
try {
53+
logger.info(`Fetching schema for table ${tableId}`)
54+
55+
const schemaResult = await context.executeTool('table_get_schema', {
56+
tableId,
57+
_context: {
58+
workspaceId: context.workspaceId,
59+
workflowId: context.workflowId,
60+
},
61+
})
62+
63+
if (!schemaResult.success || !schemaResult.output) {
64+
logger.warn(`Failed to fetch table schema: ${schemaResult.error}`)
65+
return null
66+
}
67+
68+
const tableSchema: TableSummary = {
69+
name: schemaResult.output.name,
70+
columns: schemaResult.output.columns || [],
71+
}
72+
73+
// Apply enrichment using the existing utility functions
74+
const enrichedDescription = enrichTableToolDescription(originalDescription, tableSchema, toolId)
75+
76+
const enrichedParams = enrichTableToolParameters(llmSchema, tableSchema, toolId)
77+
78+
logger.info(`Enriched ${toolId} with ${tableSchema.columns.length} columns`)
79+
80+
return {
81+
description: enrichedDescription,
82+
parameters: {
83+
properties: enrichedParams.properties,
84+
required:
85+
enrichedParams.required.length > 0 ? enrichedParams.required : llmSchema.required || [],
86+
},
87+
}
88+
} catch (error) {
89+
logger.warn(`Error fetching table schema:`, error)
90+
return null
91+
}
92+
}
93+
1094
/**
1195
* Operations that use filters and need filter-specific enrichment.
1296
*/

apps/sim/providers/utils.ts

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ChatCompletionChunk } from 'openai/resources/chat/completions'
33
import type { CompletionUsage } from 'openai/resources/completions'
44
import { env } from '@/lib/core/config/env'
55
import { isHosted } from '@/lib/core/config/feature-flags'
6-
import { enrichTableToolDescription, enrichTableToolParameters } from '@/lib/table/llm'
6+
import { enrichTableToolForLLM } from '@/lib/table/llm'
77
import { isCustomTool } from '@/executor/constants'
88
import {
99
getComputerUseModels,
@@ -497,60 +497,34 @@ export async function transformBlockTool(
497497
uniqueToolId = `${toolConfig.id}_${userProvidedParams.knowledgeBaseId}`
498498
}
499499

500-
// Enrich table tool descriptions with schema information
501-
let enrichedDescription = toolConfig.description
502-
let enrichedLlmSchema = llmSchema
503-
if (
504-
toolId.startsWith('table_') &&
505-
userProvidedParams.tableId &&
506-
workspaceId &&
507-
workflowId &&
508-
executeTool
509-
) {
510-
try {
511-
logger.info(`[transformBlockTool] Fetching schema for table ${userProvidedParams.tableId}`)
512-
const schemaResult = await executeTool('table_get_schema', {
513-
tableId: userProvidedParams.tableId,
514-
_context: { workspaceId, workflowId },
515-
})
516-
517-
if (schemaResult.success && schemaResult.output) {
518-
const tableSchema = {
519-
name: schemaResult.output.name,
520-
columns: schemaResult.output.columns || [],
521-
}
522-
523-
// Enrich description and parameters using lib/table utilities
524-
enrichedDescription = enrichTableToolDescription(
525-
toolConfig.description,
526-
tableSchema,
527-
toolId
528-
)
529-
const enrichedParams = enrichTableToolParameters(llmSchema, tableSchema, toolId)
530-
enrichedLlmSchema = {
531-
...llmSchema,
532-
properties: enrichedParams.properties,
533-
required:
534-
enrichedParams.required.length > 0 ? enrichedParams.required : llmSchema.required,
535-
}
536-
537-
logger.info(
538-
`[transformBlockTool] Enriched ${toolId} with ${tableSchema.columns.length} columns`
539-
)
540-
} else {
541-
logger.warn(`[transformBlockTool] Failed to fetch table schema: ${schemaResult.error}`)
500+
// Apply table tool enrichment if applicable
501+
let finalDescription = toolConfig.description
502+
let finalSchema = llmSchema
503+
504+
if (toolId.startsWith('table_') && workspaceId && workflowId && executeTool) {
505+
const result = await enrichTableToolForLLM(
506+
toolId,
507+
toolConfig.description,
508+
llmSchema,
509+
userProvidedParams,
510+
{
511+
workspaceId,
512+
workflowId,
513+
executeTool,
542514
}
543-
} catch (error) {
544-
logger.warn(`[transformBlockTool] Error fetching table schema:`, error)
515+
)
516+
if (result) {
517+
finalDescription = result.description
518+
finalSchema = { ...llmSchema, ...result.parameters }
545519
}
546520
}
547521

548522
return {
549523
id: uniqueToolId,
550524
name: toolConfig.name,
551-
description: enrichedDescription,
525+
description: finalDescription,
552526
params: userProvidedParams,
553-
parameters: enrichedLlmSchema,
527+
parameters: finalSchema,
554528
}
555529
}
556530

0 commit comments

Comments
 (0)