|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { isAgentToolBlock } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/utils' |
| 3 | +import { JevBlock } from '@/blocks/blocks/jev' |
| 4 | +import { prepareToolExecution, transformBlockTool } from '@/providers/utils' |
| 5 | +import { jevChoiceTool, jevEvaluateTool, jevNoulTool, jevScoreTool } from '@/tools/jev' |
| 6 | +import { getSubBlocksForToolInput } from '@/tools/params' |
| 7 | +import { prepareToolRequest } from '@/tools/request-transport' |
| 8 | +import type { ToolConfig } from '@/tools/types' |
| 9 | + |
| 10 | +vi.unmock('@/tools/metadata') |
| 11 | + |
| 12 | +const TOOLS: Record<string, ToolConfig> = { |
| 13 | + jev_choice: jevChoiceTool, |
| 14 | + jev_score: jevScoreTool, |
| 15 | + jev_noul: jevNoulTool, |
| 16 | + jev_evaluate: jevEvaluateTool, |
| 17 | +} |
| 18 | + |
| 19 | +const CASES = [ |
| 20 | + { |
| 21 | + operation: 'jev_choice', |
| 22 | + args: { criteria: { billing: 'Payments', technical: 'Bugs' } }, |
| 23 | + questions: { |
| 24 | + result: { |
| 25 | + type: 'choice', |
| 26 | + instructions: 'Question', |
| 27 | + criteria: { billing: 'Payments', technical: 'Bugs' }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + }, |
| 31 | + { |
| 32 | + operation: 'jev_score', |
| 33 | + args: { criteria: ['Low', 'High'] }, |
| 34 | + questions: { |
| 35 | + result: { type: 'score', instructions: 'Question', criteria: ['Low', 'High'] }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + operation: 'jev_noul', |
| 40 | + args: { criteria: { true: 'Urgent', false: 'Routine' } }, |
| 41 | + questions: { |
| 42 | + result: { |
| 43 | + type: 'noul', |
| 44 | + instructions: 'Question', |
| 45 | + criteria: { true: 'Urgent', false: 'Routine' }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + operation: 'jev_evaluate', |
| 51 | + args: { questions: { urgent: { type: 'noul', instructions: 'Is this urgent?' } } }, |
| 52 | + questions: { urgent: { type: 'noul', instructions: 'Is this urgent?' } }, |
| 53 | + }, |
| 54 | +] |
| 55 | + |
| 56 | +async function agentTool(operation: string, params: Record<string, unknown>) { |
| 57 | + const tool = await transformBlockTool( |
| 58 | + { type: 'jev', operation, params }, |
| 59 | + { |
| 60 | + selectedOperation: operation, |
| 61 | + getAllBlocks: () => [JevBlock], |
| 62 | + getTool: (id) => TOOLS[id], |
| 63 | + } |
| 64 | + ) |
| 65 | + if (!tool) throw new Error(`Missing Agent tool for ${operation}`) |
| 66 | + return tool |
| 67 | +} |
| 68 | + |
| 69 | +describe('Jev Agent tools', () => { |
| 70 | + it.each(CASES)('offers editable inputs for $operation in the Agent picker', ({ operation }) => { |
| 71 | + expect(isAgentToolBlock(JevBlock)).toBe(true) |
| 72 | + const fields = getSubBlocksForToolInput( |
| 73 | + operation, |
| 74 | + 'jev', |
| 75 | + { operation }, |
| 76 | + undefined, |
| 77 | + JevBlock |
| 78 | + )?.subBlocks |
| 79 | + const input = operation === 'jev_evaluate' ? 'questions' : 'criteria' |
| 80 | + expect(fields?.find((field) => field.id === input)?.paramVisibility).toBe('user-or-llm') |
| 81 | + expect(fields?.find((field) => field.id === 'apiKey')?.paramVisibility).toBe('user-only') |
| 82 | + }) |
| 83 | + |
| 84 | + describe.each(['model', 'user'] as const)('%s-supplied decision inputs', (source) => { |
| 85 | + it.each(CASES)( |
| 86 | + 'prepares $operation through the shared Agent adapter', |
| 87 | + async ({ operation, args, questions }) => { |
| 88 | + const userParams: Record<string, unknown> = { |
| 89 | + apiKey: 'test-key', |
| 90 | + state: '42', |
| 91 | + instructions: 'Question', |
| 92 | + } |
| 93 | + if (source === 'user') { |
| 94 | + for (const [key, value] of Object.entries(args)) { |
| 95 | + userParams[key] = JSON.stringify(value) |
| 96 | + } |
| 97 | + } |
| 98 | + const tool = await agentTool(operation, userParams) |
| 99 | + expect(tool.id).toBe(operation) |
| 100 | + expect(tool.parameters.properties).not.toHaveProperty('apiKey') |
| 101 | + expect(tool.modelBlockedParams).toContain('apiKey') |
| 102 | + const { toolParams } = prepareToolExecution( |
| 103 | + tool, |
| 104 | + { ...(source === 'model' ? args : {}), apiKey: 'model-key' }, |
| 105 | + {}, |
| 106 | + 'test-call' |
| 107 | + ) |
| 108 | + const request = prepareToolRequest(TOOLS[operation], toolParams) |
| 109 | + expect(request.headers.get('Authorization')).toBe('Bearer test-key') |
| 110 | + expect(JSON.parse(request.body!)).toEqual({ |
| 111 | + state: '42', |
| 112 | + model: 'jev-1.13.0', |
| 113 | + questions, |
| 114 | + }) |
| 115 | + } |
| 116 | + ) |
| 117 | + }) |
| 118 | + |
| 119 | + it('advertises the Score rubric as a bounded array to the Agent model', async () => { |
| 120 | + const tool = await agentTool('jev_score', { apiKey: 'test-key' }) |
| 121 | + expect(tool.parameters.properties.criteria).toMatchObject({ |
| 122 | + type: 'array', |
| 123 | + minItems: 2, |
| 124 | + maxItems: 10, |
| 125 | + }) |
| 126 | + }) |
| 127 | +}) |
0 commit comments