| 29 | * Supports both legacy router (block-based) and router_v2 (port-based). |
| 30 | */ |
| 31 | export class RouterBlockHandler implements BlockHandler { |
| 32 | constructor(private pathTracker?: any) {} |
| 33 | |
| 34 | canHandle(block: SerializedBlock): boolean { |
| 35 | return block.metadata?.id === BlockType.ROUTER || block.metadata?.id === BlockType.ROUTER_V2 |
| 36 | } |
| 37 | |
| 38 | async execute( |
| 39 | ctx: ExecutionContext, |
| 40 | block: SerializedBlock, |
| 41 | inputs: Record<string, any> |
| 42 | ): Promise<BlockOutput> { |
| 43 | const isV2 = isRouterV2BlockType(block.metadata?.id) |
| 44 | |
| 45 | if (isV2) { |
| 46 | return this.executeV2(ctx, block, inputs) |
| 47 | } |
| 48 | |
| 49 | return this.executeLegacy(ctx, block, inputs) |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Execute legacy router (block-based routing). |
| 54 | */ |
| 55 | private async executeLegacy( |
| 56 | ctx: ExecutionContext, |
| 57 | block: SerializedBlock, |
| 58 | inputs: Record<string, any> |
| 59 | ): Promise<BlockOutput> { |
| 60 | const targetBlocks = this.getTargetBlocks(ctx, block) |
| 61 | |
| 62 | const routerConfig = { |
| 63 | prompt: inputs.prompt, |
| 64 | model: inputs.model || ROUTER.DEFAULT_MODEL, |
| 65 | apiKey: inputs.apiKey, |
| 66 | vertexProject: inputs.vertexProject, |
| 67 | vertexLocation: inputs.vertexLocation, |
| 68 | vertexCredential: inputs.vertexCredential, |
| 69 | bedrockAccessKeyId: inputs.bedrockAccessKeyId, |
| 70 | bedrockSecretKey: inputs.bedrockSecretKey, |
| 71 | bedrockRegion: inputs.bedrockRegion, |
| 72 | } |
| 73 | |
| 74 | await validateModelProvider(ctx.userId, ctx.workspaceId, routerConfig.model, ctx) |
| 75 | |
| 76 | const providerId = getProviderFromModel(routerConfig.model) |
| 77 | |
| 78 | try { |
| 79 | const url = new URL('/api/providers', getInternalApiBaseUrl()) |
| 80 | if (ctx.userId) url.searchParams.set('userId', ctx.userId) |
| 81 | |
| 82 | const messages = [{ role: 'user', content: routerConfig.prompt }] |
| 83 | const systemPrompt = generateRouterPrompt(routerConfig.prompt, targetBlocks) |
| 84 | |
| 85 | let finalApiKey: string | undefined = routerConfig.apiKey |
| 86 | if (providerId === 'vertex' && routerConfig.vertexCredential) { |
| 87 | finalApiKey = await resolveVertexCredential( |
| 88 | routerConfig.vertexCredential, |
nothing calls this directly
no outgoing calls
no test coverage detected