( originalDescription: string, table: TableSummary, toolId: string )
| 30 | * Enriches a table tool description with table information based on the operation type. |
| 31 | */ |
| 32 | export function enrichTableToolDescription( |
| 33 | originalDescription: string, |
| 34 | table: TableSummary, |
| 35 | toolId: string |
| 36 | ): string { |
| 37 | if (!table.columns || table.columns.length === 0) { |
| 38 | return originalDescription |
| 39 | } |
| 40 | |
| 41 | const columnList = table.columns.map((col) => ` - ${col.name} (${col.type})`).join('\n') |
| 42 | |
| 43 | if (FILTER_OPERATIONS.has(toolId)) { |
| 44 | const stringCols = table.columns.filter((c) => c.type === 'string') |
| 45 | const numberCols = table.columns.filter((c) => c.type === 'number') |
| 46 | |
| 47 | let filterExample = '' |
| 48 | if (stringCols.length > 0 && numberCols.length > 0) { |
| 49 | filterExample = ` |
| 50 | |
| 51 | Example filter: {"${stringCols[0].name}": {"$eq": "value"}, "${numberCols[0].name}": {"$lt": 50}}` |
| 52 | } else if (stringCols.length > 0) { |
| 53 | filterExample = ` |
| 54 | |
| 55 | Example filter: {"${stringCols[0].name}": {"$eq": "value"}}` |
| 56 | } |
| 57 | |
| 58 | let sortExample = '' |
| 59 | if (toolId === 'table_query_rows' && numberCols.length > 0) { |
| 60 | sortExample = ` |
| 61 | Example sort: {"${numberCols[0].name}": "desc"} for highest first, {"${numberCols[0].name}": "asc"} for lowest first` |
| 62 | } |
| 63 | |
| 64 | const queryInstructions = |
| 65 | toolId === 'table_query_rows' |
| 66 | ? ` |
| 67 | INSTRUCTIONS: |
| 68 | 1. ALWAYS include a filter based on the user's question - queries without filters will fail |
| 69 | 2. Construct the filter yourself from the user's question - do NOT ask for confirmation |
| 70 | 3. Use exact match ($eq) by default unless the user specifies otherwise |
| 71 | 4. For ranking queries (highest, lowest, Nth, top N): |
| 72 | - ALWAYS use sort with the relevant column (e.g., {"salary": "desc"} for highest salary) |
| 73 | - Use limit to get only the needed rows (e.g., limit=1 for highest, limit=2 for second highest) |
| 74 | - For "second highest X", use sort: {"X": "desc"} with limit: 2, then take the second result |
| 75 | 5. Only use limit=1000 when you need ALL matching rows` |
| 76 | : ` |
| 77 | INSTRUCTIONS: |
| 78 | 1. ALWAYS include a filter based on the user's question - queries without filters will fail |
| 79 | 2. Construct the filter yourself from the user's question - do NOT ask for confirmation |
| 80 | 3. Use exact match ($eq) by default unless the user specifies otherwise` |
| 81 | |
| 82 | return `${originalDescription} |
| 83 | ${queryInstructions} |
| 84 | |
| 85 | Table "${table.name}" columns: |
| 86 | ${columnList} |
| 87 | ${filterExample}${sortExample}` |
| 88 | } |
| 89 |
no test coverage detected