Skip to content

Commit 2e624c2

Browse files
committed
reduced type confusion
1 parent 7093209 commit 2e624c2

File tree

3 files changed

+29
-38
lines changed

3 files changed

+29
-38
lines changed

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

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

8-
/**
9-
* Table schema information used for LLM enrichment.
10-
*/
11-
export interface TableSchemaInfo {
12-
name: string
13-
columns: Array<{ name: string; type: string }>
14-
}
8+
import type { TableSummary } from '../types'
159

1610
/**
1711
* Operations that use filters and need filter-specific enrichment.
@@ -33,28 +27,28 @@ export const DATA_OPERATIONS = new Set([
3327
])
3428

3529
/**
36-
* Enriches a table tool description with schema information based on the operation type.
30+
* Enriches a table tool description with table information based on the operation type.
3731
*
3832
* @param originalDescription - The original tool description
39-
* @param tableSchema - The table schema with name and columns
33+
* @param table - The table summary with name and columns
4034
* @param toolId - The tool identifier to determine operation type
4135
* @returns Enriched description with table-specific instructions
4236
*/
4337
export function enrichTableToolDescription(
4438
originalDescription: string,
45-
tableSchema: TableSchemaInfo,
39+
table: TableSummary,
4640
toolId: string
4741
): string {
48-
if (!tableSchema.columns || tableSchema.columns.length === 0) {
42+
if (!table.columns || table.columns.length === 0) {
4943
return originalDescription
5044
}
5145

52-
const columnList = tableSchema.columns.map((col) => ` - ${col.name} (${col.type})`).join('\n')
46+
const columnList = table.columns.map((col) => ` - ${col.name} (${col.type})`).join('\n')
5347

5448
// Filter-based operations: emphasize filter usage
5549
if (FILTER_OPERATIONS.has(toolId)) {
56-
const stringCols = tableSchema.columns.filter((c) => c.type === 'string')
57-
const numberCols = tableSchema.columns.filter((c) => c.type === 'number')
50+
const stringCols = table.columns.filter((c) => c.type === 'string')
51+
const numberCols = table.columns.filter((c) => c.type === 'number')
5852

5953
let filterExample = ''
6054
if (stringCols.length > 0 && numberCols.length > 0) {
@@ -96,14 +90,14 @@ INSTRUCTIONS:
9690
return `${originalDescription}
9791
${queryInstructions}
9892
99-
Table "${tableSchema.name}" columns:
93+
Table "${table.name}" columns:
10094
${columnList}
10195
${filterExample}${sortExample}`
10296
}
10397

10498
// Data operations: show columns for data construction
10599
if (DATA_OPERATIONS.has(toolId)) {
106-
const exampleCols = tableSchema.columns.slice(0, 3)
100+
const exampleCols = table.columns.slice(0, 3)
107101
const dataExample = exampleCols.reduce(
108102
(obj, col) => {
109103
obj[col.name] = col.type === 'number' ? 123 : col.type === 'boolean' ? true : 'example'
@@ -114,7 +108,7 @@ ${filterExample}${sortExample}`
114108

115109
return `${originalDescription}
116110
117-
Table "${tableSchema.name}" available columns:
111+
Table "${table.name}" available columns:
118112
${columnList}
119113
120114
Pass the "data" parameter with an object like: ${JSON.stringify(dataExample)}`
@@ -123,31 +117,31 @@ Pass the "data" parameter with an object like: ${JSON.stringify(dataExample)}`
123117
// Default: just show columns
124118
return `${originalDescription}
125119
126-
Table "${tableSchema.name}" columns:
120+
Table "${table.name}" columns:
127121
${columnList}`
128122
}
129123

130124
/**
131125
* Enriches LLM tool parameters with table-specific information.
132126
*
133127
* @param llmSchema - The original LLM schema with properties and required fields
134-
* @param tableSchema - The table schema with name and columns
128+
* @param table - The table summary with name and columns
135129
* @param toolId - The tool identifier to determine operation type
136130
* @returns Enriched schema with updated property descriptions and required fields
137131
*/
138132
export function enrichTableToolParameters(
139133
llmSchema: { properties?: Record<string, any>; required?: string[] },
140-
tableSchema: TableSchemaInfo,
134+
table: TableSummary,
141135
toolId: string
142136
): { properties: Record<string, any>; required: string[] } {
143-
if (!tableSchema.columns || tableSchema.columns.length === 0) {
137+
if (!table.columns || table.columns.length === 0) {
144138
return {
145139
properties: llmSchema.properties || {},
146140
required: llmSchema.required || [],
147141
}
148142
}
149143

150-
const columnNames = tableSchema.columns.map((c) => c.name).join(', ')
144+
const columnNames = table.columns.map((c) => c.name).join(', ')
151145
const enrichedProperties = { ...llmSchema.properties }
152146
const enrichedRequired = llmSchema.required ? [...llmSchema.required] : []
153147

@@ -182,7 +176,7 @@ export function enrichTableToolParameters(
182176

183177
// Enrich data parameter for insert/update operations
184178
if (enrichedProperties.data && DATA_OPERATIONS.has(toolId)) {
185-
const exampleCols = tableSchema.columns.slice(0, 2)
179+
const exampleCols = table.columns.slice(0, 2)
186180
const exampleData = exampleCols.reduce(
187181
(obj: Record<string, unknown>, col: { name: string; type: string }) => {
188182
obj[col.name] = col.type === 'number' ? 123 : col.type === 'boolean' ? true : 'value'

apps/sim/lib/table/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ export interface TableDefinition {
4848
/** Minimal table info for UI components. */
4949
export type TableInfo = Pick<TableDefinition, 'id' | 'name' | 'schema'>
5050

51+
/** Simplified table summary for LLM enrichment and display contexts. */
52+
export interface TableSummary {
53+
name: string
54+
columns: Array<Pick<ColumnDefinition, 'name' | 'type'>>
55+
}
56+
5157
export interface TableRow {
5258
id: string
5359
data: RowData

apps/sim/lib/table/wand.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,14 @@
55
*/
66

77
import { createLogger } from '@sim/logger'
8-
import type { TableSchema } from './types'
8+
import type { TableInfo } from './types'
99

1010
const logger = createLogger('TableWandContext')
1111

12-
interface TableSchemaResponse {
13-
data?: {
14-
table?: {
15-
id: string
16-
name?: string | null
17-
schema?: TableSchema
18-
}
19-
}
20-
table?: {
21-
id: string
22-
name?: string | null
23-
schema?: TableSchema
24-
}
12+
/** API response wrapper for table info. Handles both direct and nested response formats. */
13+
interface TableInfoResponse {
14+
data?: { table?: TableInfo }
15+
table?: TableInfo
2516
}
2617

2718
/**
@@ -44,7 +35,7 @@ export async function fetchTableSchemaContext({
4435
return null
4536
}
4637

47-
const result = (await response.json()) as TableSchemaResponse
38+
const result = (await response.json()) as TableInfoResponse
4839
const table = result.data?.table ?? result.table
4940
const schema = table?.schema
5041

0 commit comments

Comments
 (0)