Table tools

This commit is contained in:
Siddharth Ganesan
2026-03-09 10:21:07 -07:00
parent 301fdb94ff
commit 4593a8a471
3 changed files with 134 additions and 0 deletions

View File

@@ -46,6 +46,10 @@ const WRITE_ACTIONS: Record<string, string[]> = {
'delete_row',
'update_rows_by_filter',
'delete_rows_by_filter',
'add_column',
'rename_column',
'delete_column',
'update_column',
],
manage_custom_tool: ['add', 'edit', 'delete'],
manage_mcp_tool: ['add', 'edit', 'delete'],

View File

@@ -2,8 +2,10 @@ import { createLogger } from '@sim/logger'
import type { BaseServerTool, ServerToolContext } from '@/lib/copilot/tools/server/base-tool'
import type { UserTableArgs, UserTableResult } from '@/lib/copilot/tools/shared/schemas'
import {
addTableColumn,
batchInsertRows,
createTable,
deleteColumn,
deleteRow,
deleteRowsByFilter,
deleteTable,
@@ -11,6 +13,9 @@ import {
getTableById,
insertRow,
queryRows,
renameColumn,
updateColumnConstraints,
updateColumnType,
updateRow,
updateRowsByFilter,
} from '@/lib/table/service'
@@ -637,6 +642,113 @@ export const userTableServerTool: BaseServerTool<UserTableArgs, UserTableResult>
}
}
case 'add_column': {
if (!args.tableId) {
return { success: false, message: 'Table ID is required' }
}
const col = (args as Record<string, unknown>).column as
| {
name: string
type: string
required?: boolean
unique?: boolean
position?: number
}
| undefined
if (!col?.name || !col?.type) {
return {
success: false,
message: 'column with name and type is required for add_column',
}
}
const requestId = crypto.randomUUID().slice(0, 8)
const updated = await addTableColumn(args.tableId, col, requestId)
return {
success: true,
message: `Added column "${col.name}" (${col.type}) to table`,
data: { schema: updated.schema },
}
}
case 'rename_column': {
if (!args.tableId) {
return { success: false, message: 'Table ID is required' }
}
const colName = (args as Record<string, unknown>).columnName as string | undefined
const newColName = (args as Record<string, unknown>).newName as string | undefined
if (!colName || !newColName) {
return { success: false, message: 'columnName and newName are required' }
}
const requestId = crypto.randomUUID().slice(0, 8)
const updated = await renameColumn(
{ tableId: args.tableId, columnName: colName, newName: newColName },
requestId
)
return {
success: true,
message: `Renamed column "${colName}" to "${newColName}"`,
data: { schema: updated.schema },
}
}
case 'delete_column': {
if (!args.tableId) {
return { success: false, message: 'Table ID is required' }
}
const colName = (args as Record<string, unknown>).columnName as string | undefined
if (!colName) {
return { success: false, message: 'columnName is required' }
}
const requestId = crypto.randomUUID().slice(0, 8)
const updated = await deleteColumn(
{ tableId: args.tableId, columnName: colName },
requestId
)
return {
success: true,
message: `Deleted column "${colName}"`,
data: { schema: updated.schema },
}
}
case 'update_column': {
if (!args.tableId) {
return { success: false, message: 'Table ID is required' }
}
const colName = (args as Record<string, unknown>).columnName as string | undefined
if (!colName) {
return { success: false, message: 'columnName is required' }
}
const newType = (args as Record<string, unknown>).newType as string | undefined
const reqFlag = (args as Record<string, unknown>).required as boolean | undefined
const uniqFlag = (args as Record<string, unknown>).unique as boolean | undefined
if (newType === undefined && reqFlag === undefined && uniqFlag === undefined) {
return {
success: false,
message: 'At least one of newType, required, or unique must be provided',
}
}
const requestId = crypto.randomUUID().slice(0, 8)
let result: TableDefinition | undefined
if (newType !== undefined) {
result = await updateColumnType(
{ tableId: args.tableId, columnName: colName, newType },
requestId
)
}
if (reqFlag !== undefined || uniqFlag !== undefined) {
result = await updateColumnConstraints(
{ tableId: args.tableId, columnName: colName, required: reqFlag, unique: uniqFlag },
requestId
)
}
return {
success: true,
message: `Updated column "${colName}"`,
data: { schema: result?.schema },
}
}
default:
return { success: false, message: `Unknown operation: ${operation}` }
}

View File

@@ -113,6 +113,10 @@ export const UserTableArgsSchema = z.object({
'delete_row',
'update_rows_by_filter',
'delete_rows_by_filter',
'add_column',
'rename_column',
'delete_column',
'update_column',
]),
args: z
.object({
@@ -128,6 +132,20 @@ export const UserTableArgsSchema = z.object({
limit: z.number().optional(),
offset: z.number().optional(),
filePath: z.string().optional(),
column: z
.object({
name: z.string(),
type: z.string(),
required: z.boolean().optional(),
unique: z.boolean().optional(),
position: z.number().optional(),
})
.optional(),
columnName: z.string().optional(),
newName: z.string().optional(),
newType: z.string().optional(),
required: z.boolean().optional(),
unique: z.boolean().optional(),
})
.optional(),
})