|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 8 | +import { DelegateToAgentTool } from './delegate-to-agent-tool.js'; |
| 9 | +import { AgentRegistry } from './registry.js'; |
| 10 | +import type { Config } from '../config/config.js'; |
| 11 | +import type { AgentDefinition } from './types.js'; |
| 12 | +import { SubagentInvocation } from './invocation.js'; |
| 13 | +import type { MessageBus } from '../confirmation-bus/message-bus.js'; |
| 14 | +import { MessageBusType } from '../confirmation-bus/types.js'; |
| 15 | +import { DELEGATE_TO_AGENT_TOOL_NAME } from '../tools/tool-names.js'; |
| 16 | + |
| 17 | +vi.mock('./invocation.js', () => ({ |
| 18 | + SubagentInvocation: vi.fn().mockImplementation(() => ({ |
| 19 | + execute: vi |
| 20 | + .fn() |
| 21 | + .mockResolvedValue({ content: [{ type: 'text', text: 'Success' }] }), |
| 22 | + })), |
| 23 | +})); |
| 24 | + |
| 25 | +describe('DelegateToAgentTool', () => { |
| 26 | + let registry: AgentRegistry; |
| 27 | + let config: Config; |
| 28 | + let tool: DelegateToAgentTool; |
| 29 | + let messageBus: MessageBus; |
| 30 | + |
| 31 | + const mockAgentDef: AgentDefinition = { |
| 32 | + name: 'test_agent', |
| 33 | + description: 'A test agent', |
| 34 | + promptConfig: {}, |
| 35 | + modelConfig: { model: 'test-model', temp: 0, top_p: 0 }, |
| 36 | + inputConfig: { |
| 37 | + inputs: { |
| 38 | + arg1: { type: 'string', description: 'Argument 1', required: true }, |
| 39 | + arg2: { type: 'number', description: 'Argument 2', required: false }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + runConfig: { max_turns: 1, max_time_minutes: 1 }, |
| 43 | + toolConfig: { tools: [] }, |
| 44 | + }; |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + config = { |
| 48 | + getDebugMode: () => false, |
| 49 | + modelConfigService: { |
| 50 | + registerRuntimeModelConfig: vi.fn(), |
| 51 | + }, |
| 52 | + } as unknown as Config; |
| 53 | + |
| 54 | + registry = new AgentRegistry(config); |
| 55 | + // Manually register the mock agent (bypassing protected method for testing) |
| 56 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 57 | + (registry as any).agents.set(mockAgentDef.name, mockAgentDef); |
| 58 | + |
| 59 | + messageBus = { |
| 60 | + publish: vi.fn(), |
| 61 | + subscribe: vi.fn(), |
| 62 | + unsubscribe: vi.fn(), |
| 63 | + } as unknown as MessageBus; |
| 64 | + |
| 65 | + tool = new DelegateToAgentTool(registry, config, messageBus); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should use dynamic description from registry', () => { |
| 69 | + // registry has mockAgentDef registered in beforeEach |
| 70 | + expect(tool.description).toContain( |
| 71 | + 'Delegates a task to a specialized sub-agent', |
| 72 | + ); |
| 73 | + expect(tool.description).toContain( |
| 74 | + `- **${mockAgentDef.name}**: ${mockAgentDef.description}`, |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should validate agent_name exists in registry', async () => { |
| 79 | + // Zod validation happens at build time now (or rather, build validates the schema) |
| 80 | + // Since we use discriminated union, an invalid agent_name won't match any option. |
| 81 | + expect(() => |
| 82 | + tool.build({ |
| 83 | + agent_name: 'non_existent_agent', |
| 84 | + }), |
| 85 | + ).toThrow(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should validate correct arguments', async () => { |
| 89 | + const invocation = tool.build({ |
| 90 | + agent_name: 'test_agent', |
| 91 | + arg1: 'valid', |
| 92 | + }); |
| 93 | + |
| 94 | + const result = await invocation.execute(new AbortController().signal); |
| 95 | + expect(result).toEqual({ content: [{ type: 'text', text: 'Success' }] }); |
| 96 | + expect(SubagentInvocation).toHaveBeenCalledWith( |
| 97 | + { arg1: 'valid' }, |
| 98 | + mockAgentDef, |
| 99 | + config, |
| 100 | + messageBus, |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should throw error for missing required argument', async () => { |
| 105 | + // Missing arg1 should fail Zod validation |
| 106 | + expect(() => |
| 107 | + tool.build({ |
| 108 | + agent_name: 'test_agent', |
| 109 | + arg2: 123, |
| 110 | + }), |
| 111 | + ).toThrow(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should throw error for invalid argument type', async () => { |
| 115 | + // arg1 should be string, passing number |
| 116 | + expect(() => |
| 117 | + tool.build({ |
| 118 | + agent_name: 'test_agent', |
| 119 | + arg1: 123, |
| 120 | + }), |
| 121 | + ).toThrow(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should allow optional arguments to be omitted', async () => { |
| 125 | + const invocation = tool.build({ |
| 126 | + agent_name: 'test_agent', |
| 127 | + arg1: 'valid', |
| 128 | + // arg2 is optional |
| 129 | + }); |
| 130 | + |
| 131 | + await expect( |
| 132 | + invocation.execute(new AbortController().signal), |
| 133 | + ).resolves.toBeDefined(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should throw error if an agent has an input named "agent_name"', () => { |
| 137 | + const invalidAgentDef: AgentDefinition = { |
| 138 | + ...mockAgentDef, |
| 139 | + name: 'invalid_agent', |
| 140 | + inputConfig: { |
| 141 | + inputs: { |
| 142 | + agent_name: { |
| 143 | + type: 'string', |
| 144 | + description: 'Conflict', |
| 145 | + required: true, |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }; |
| 150 | + |
| 151 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 152 | + (registry as any).agents.set(invalidAgentDef.name, invalidAgentDef); |
| 153 | + |
| 154 | + expect(() => new DelegateToAgentTool(registry, config)).toThrow( |
| 155 | + "Agent 'invalid_agent' cannot have an input parameter named 'agent_name' as it is a reserved parameter for delegation.", |
| 156 | + ); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should use correct tool name "delegate_to_agent" when requesting confirmation', async () => { |
| 160 | + const invocation = tool.build({ |
| 161 | + agent_name: 'test_agent', |
| 162 | + arg1: 'valid', |
| 163 | + }); |
| 164 | + |
| 165 | + // Trigger confirmation check |
| 166 | + const p = invocation.shouldConfirmExecute(new AbortController().signal); |
| 167 | + void p; |
| 168 | + |
| 169 | + expect(messageBus.publish).toHaveBeenCalledWith( |
| 170 | + expect.objectContaining({ |
| 171 | + type: MessageBusType.TOOL_CONFIRMATION_REQUEST, |
| 172 | + toolCall: expect.objectContaining({ |
| 173 | + name: DELEGATE_TO_AGENT_TOOL_NAME, |
| 174 | + }), |
| 175 | + }), |
| 176 | + ); |
| 177 | + }); |
| 178 | +}); |
0 commit comments