| 519 | * @returns The provider tool config or null if transform fails |
| 520 | */ |
| 521 | export async function transformBlockTool( |
| 522 | block: any, |
| 523 | options: { |
| 524 | selectedOperation?: string |
| 525 | getAllBlocks: () => any[] |
| 526 | getTool: (toolId: string) => any |
| 527 | getToolAsync?: (toolId: string) => Promise<any> |
| 528 | canonicalModes?: Record<string, 'basic' | 'advanced'> |
| 529 | } |
| 530 | ): Promise<ProviderToolConfig | null> { |
| 531 | const { selectedOperation, getAllBlocks, getTool, getToolAsync, canonicalModes } = options |
| 532 | |
| 533 | const blockDef = getAllBlocks().find((b: any) => b.type === block.type) |
| 534 | if (!blockDef) { |
| 535 | logger.warn(`Block definition not found for type: ${block.type}`) |
| 536 | return null |
| 537 | } |
| 538 | |
| 539 | let toolId: string | null = null |
| 540 | |
| 541 | if ((blockDef.tools?.access?.length || 0) > 1) { |
| 542 | if (selectedOperation && blockDef.tools?.config?.tool) { |
| 543 | try { |
| 544 | toolId = blockDef.tools.config.tool({ |
| 545 | ...block.params, |
| 546 | operation: selectedOperation, |
| 547 | }) |
| 548 | } catch (error) { |
| 549 | logger.error('Error selecting tool for block', { |
| 550 | blockType: block.type, |
| 551 | operation: selectedOperation, |
| 552 | error, |
| 553 | }) |
| 554 | return null |
| 555 | } |
| 556 | } else { |
| 557 | toolId = blockDef.tools.access[0] |
| 558 | } |
| 559 | } else { |
| 560 | toolId = blockDef.tools?.access?.[0] || null |
| 561 | } |
| 562 | |
| 563 | if (!toolId) { |
| 564 | logger.warn(`No tool ID found for block: ${block.type}`) |
| 565 | return null |
| 566 | } |
| 567 | |
| 568 | let toolConfig: any |
| 569 | |
| 570 | if (isCustomTool(toolId) && getToolAsync) { |
| 571 | toolConfig = await getToolAsync(toolId) |
| 572 | } else { |
| 573 | toolConfig = getTool(toolId) |
| 574 | } |
| 575 | |
| 576 | if (!toolConfig) { |
| 577 | logger.warn(`Tool config not found for ID: ${toolId}`) |
| 578 | return null |