-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(custom-blocks): render and execute custom blocks as agent tools #5811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
8a3644c
fix(custom-blocks): render and execute custom blocks as agent tools
TheodoreSpeaks 4ab45cf
fix(custom-blocks): address review findings on agent-tool execution
TheodoreSpeaks 2db4e6c
fix(custom-blocks): move executor tool id out of the custom-tool name…
TheodoreSpeaks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
apps/sim/executor/handlers/workflow/custom-block-tool-runner.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockExecute } = vi.hoisted(() => ({ mockExecute: vi.fn() })) | ||
|
|
||
| vi.mock('@/executor/handlers/workflow/workflow-handler', () => ({ | ||
| WorkflowBlockHandler: class { | ||
| execute = mockExecute | ||
| }, | ||
| aggregateChildCost: (spans: Array<{ cost?: { total?: number } }>) => | ||
| spans.reduce((sum, span) => sum + (span?.cost?.total ?? 0), 0), | ||
| })) | ||
|
|
||
| import { ChildWorkflowError } from '@/executor/errors/child-workflow-error' | ||
| import { | ||
| buildCustomBlockExecutionContext, | ||
| runCustomBlockTool, | ||
| } from '@/executor/handlers/workflow/custom-block-tool-runner' | ||
|
|
||
| describe('buildCustomBlockExecutionContext', () => { | ||
| it('carries consumer identity, inherits the call chain, and is fully scaffolded', () => { | ||
| const ctx = buildCustomBlockExecutionContext({ | ||
| workspaceId: 'ws-consumer', | ||
| userId: 'u-consumer', | ||
| workflowId: 'wf-parent', | ||
| callChain: ['wf-parent'], | ||
| billingAttribution: { actorUserId: 'u-consumer', workspaceId: 'ws-consumer' } as any, | ||
| }) | ||
|
|
||
| expect(ctx.workspaceId).toBe('ws-consumer') | ||
| expect(ctx.userId).toBe('u-consumer') | ||
| // Inherited (not reset) so the handler's depth guard keeps bounding recursion. | ||
| expect(ctx.callChain).toEqual(['wf-parent']) | ||
| // metadata must be a real object — the handler reads it unconditionally. | ||
| expect(ctx.metadata).toBeTypeOf('object') | ||
| expect(ctx.metadata.billingAttribution).toEqual({ | ||
| actorUserId: 'u-consumer', | ||
| workspaceId: 'ws-consumer', | ||
| }) | ||
| expect(ctx.metadata.executionMode).toBe('sync') | ||
| // Non-optional scaffolding present. | ||
| expect(ctx.blockStates).toBeInstanceOf(Map) | ||
| expect(ctx.executedBlocks).toBeInstanceOf(Set) | ||
| expect(ctx.completedLoops).toBeInstanceOf(Set) | ||
| expect(ctx.activeExecutionPath).toBeInstanceOf(Set) | ||
| expect(ctx.decisions.router).toBeInstanceOf(Map) | ||
| expect(ctx.decisions.condition).toBeInstanceOf(Map) | ||
| expect(Array.isArray(ctx.blockLogs)).toBe(true) | ||
| expect(ctx.executionId).toBeTruthy() | ||
| }) | ||
|
|
||
| it('defaults the call chain to [] when none is provided', () => { | ||
| expect(buildCustomBlockExecutionContext({}).callChain).toEqual([]) | ||
| }) | ||
| }) | ||
|
|
||
| describe('runCustomBlockTool', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('runs the handler with the synthetic ctx and returns its projected output', async () => { | ||
| mockExecute.mockResolvedValue({ success: true, result: { answer: 'hi' }, cost: { total: 0.5 } }) | ||
|
|
||
| const res = await runCustomBlockTool({ | ||
| blockType: 'custom_block_abc', | ||
| inputMapping: '{"field-question":"hi"}', | ||
| _context: { workspaceId: 'ws-consumer', userId: 'u-consumer' }, | ||
| }) | ||
|
|
||
| expect(res.success).toBe(true) | ||
| expect(res.output.cost).toEqual({ total: 0.5 }) | ||
|
|
||
| const [ctxArg, blockArg, inputsArg] = mockExecute.mock.calls[0] | ||
| expect(ctxArg.workspaceId).toBe('ws-consumer') | ||
| expect(blockArg.metadata.id).toBe('custom_block_abc') | ||
| expect(inputsArg).toEqual({ inputMapping: '{"field-question":"hi"}' }) | ||
| }) | ||
|
|
||
| it('surfaces a handler failure as a clean tool error', async () => { | ||
| mockExecute.mockRejectedValue(new Error('This block’s workflow is not deployed.')) | ||
|
|
||
| const res = await runCustomBlockTool({ blockType: 'custom_block_abc', _context: {} }) | ||
|
|
||
| expect(res.success).toBe(false) | ||
| expect(res.error).toContain('not deployed') | ||
| }) | ||
|
|
||
| it('rolls up already-incurred child cost when the run fails', async () => { | ||
| const err: any = new Error('child blew up') | ||
| err.name = 'ChildWorkflowError' | ||
| err.childTraceSpans = [{ id: 's1', name: 'child', type: 'agent', cost: { total: 0.25 } }] | ||
| Object.setPrototypeOf(err, ChildWorkflowError.prototype) | ||
| mockExecute.mockRejectedValue(err) | ||
|
|
||
| const res = await runCustomBlockTool({ blockType: 'custom_block_abc', _context: {} }) | ||
|
|
||
| expect(res.success).toBe(false) | ||
| // Partial spend must not be recorded as zero-cost. | ||
| expect((res.output as any).cost.total).toBeGreaterThan(0) | ||
| }) | ||
|
|
||
| it('rejects a missing block type without invoking the handler', async () => { | ||
| const res = await runCustomBlockTool({ _context: {} }) | ||
| expect(res.success).toBe(false) | ||
| expect(mockExecute).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.