Files
sim/apps/sim/lib/copilot/tools/server/blocks/get-block-options.ts

80 lines
2.7 KiB
TypeScript

import type { BaseServerTool } from '@/lib/copilot/tools/server/base-tool'
import {
type GetBlockOptionsInputType,
GetBlockOptionsResult,
type GetBlockOptionsResultType,
} from '@/lib/copilot/tools/shared/schemas'
import { createLogger } from '@/lib/logs/console/logger'
import { registry as blockRegistry } from '@/blocks/registry'
import { tools as toolsRegistry } from '@/tools/registry'
export const getBlockOptionsServerTool: BaseServerTool<
GetBlockOptionsInputType,
GetBlockOptionsResultType
> = {
name: 'get_block_options',
async execute({ blockId }: GetBlockOptionsInputType): Promise<GetBlockOptionsResultType> {
const logger = createLogger('GetBlockOptionsServerTool')
logger.debug('Executing get_block_options', { blockId })
const blockConfig = blockRegistry[blockId]
if (!blockConfig) {
throw new Error(`Block not found: ${blockId}`)
}
const operations: { id: string; name: string; description?: string }[] = []
// Check if block has an operation dropdown to determine available operations
const operationSubBlock = blockConfig.subBlocks?.find((sb) => sb.id === 'operation')
if (operationSubBlock && Array.isArray(operationSubBlock.options)) {
// Block has operations - get tool info for each operation
for (const option of operationSubBlock.options) {
const opId = typeof option === 'object' ? option.id : option
const opLabel = typeof option === 'object' ? option.label : option
// Try to resolve the tool for this operation
let toolDescription: string | undefined
try {
const toolSelector = blockConfig.tools?.config?.tool
if (typeof toolSelector === 'function') {
const toolId = toolSelector({ operation: opId })
const tool = toolsRegistry[toolId]
if (tool) {
toolDescription = tool.description
}
}
} catch {
// Tool resolution failed, continue without description
}
operations.push({
id: opId,
name: opLabel || opId,
description: toolDescription,
})
}
} else {
// No operation dropdown - list all accessible tools
const accessibleTools = blockConfig.tools?.access || []
for (const toolId of accessibleTools) {
const tool = toolsRegistry[toolId]
if (tool) {
operations.push({
id: toolId,
name: tool.name || toolId,
description: tool.description,
})
}
}
}
const result = {
blockId,
blockName: blockConfig.name,
operations,
}
return GetBlockOptionsResult.parse(result)
},
}