File size: 9,058 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { AgentTool } from './agent-tool.js';
import { makeFakeConfig } from '../test-utils/config.js';
import { createMockMessageBus } from '../test-utils/mock-message-bus.js';
import type { Config } from '../config/config.js';
import type { MessageBus } from '../confirmation-bus/message-bus.js';
import { LocalSubagentInvocation } from './local-invocation.js';
import { RemoteAgentInvocation } from './remote-invocation.js';
import { LocalSessionInvocation } from './local-session-invocation.js';
import { RemoteSessionInvocation } from './remote-session-invocation.js';
import { BrowserAgentInvocation } from './browser/browserAgentInvocation.js';
import { BROWSER_AGENT_NAME } from './browser/browserAgentDefinition.js';
import { AgentRegistry } from './registry.js';
import type { LocalAgentDefinition, RemoteAgentDefinition } from './types.js';

vi.mock('./local-invocation.js');
vi.mock('./remote-invocation.js');
vi.mock('./local-session-invocation.js');
vi.mock('./remote-session-invocation.js');
vi.mock('./browser/browserAgentInvocation.js');

describe('AgentTool', () => {
  let mockConfig: Config;
  let mockMessageBus: MessageBus;
  let tool: AgentTool;

  const testLocalDefinition: LocalAgentDefinition = {
    kind: 'local',
    name: 'TestLocalAgent',
    description: 'A local test agent.',
    inputConfig: {
      inputSchema: {
        type: 'object',
        properties: { objective: { type: 'string' } },
      },
    },
    modelConfig: { model: 'test', generateContentConfig: {} },
    runConfig: { maxTimeMinutes: 1 },
    promptConfig: { systemPrompt: 'test' },
  };

  const testRemoteDefinition: RemoteAgentDefinition = {
    kind: 'remote',
    name: 'TestRemoteAgent',
    description: 'A remote test agent.',
    inputConfig: {
      inputSchema: {
        type: 'object',
        properties: { query: { type: 'string' } },
      },
    },
    agentCardUrl: 'http://example.com/agent',
  };

  beforeEach(() => {
    vi.clearAllMocks();
    mockConfig = makeFakeConfig();
    mockMessageBus = createMockMessageBus();
    tool = new AgentTool(mockConfig, mockMessageBus);

    // Mock AgentRegistry
    const registry = new AgentRegistry(mockConfig);
    vi.spyOn(mockConfig, 'getAgentRegistry').mockReturnValue(registry);

    vi.spyOn(registry, 'getDefinition').mockImplementation((name: string) => {
      if (name === 'TestLocalAgent') return testLocalDefinition;
      if (name === 'TestRemoteAgent') return testRemoteDefinition;
      if (name === BROWSER_AGENT_NAME) {
        return {
          kind: 'remote',
          name: BROWSER_AGENT_NAME,
          displayName: 'Browser Agent',
          description: 'Browser Agent Description',
          inputConfig: {
            inputSchema: {
              type: 'object',
              properties: { task: { type: 'string' } },
            },
          },
          agentCardUrl: 'http://example.com',
        };
      }
      return undefined;
    });
  });

  it('should map prompt to objective for local agent', async () => {
    const params = { agent_name: 'TestLocalAgent', prompt: 'Do something' };
    const invocation = tool['createInvocation'](params, mockMessageBus);

    // Trigger deferred instantiation
    await invocation.shouldConfirmExecute(new AbortController().signal);

    expect(LocalSubagentInvocation).toHaveBeenCalledWith(
      testLocalDefinition,
      mockConfig,
      { objective: 'Do something' },
      mockMessageBus,
    );
  });

  it('should map prompt to query for remote agent', async () => {
    const params = {
      agent_name: 'TestRemoteAgent',
      prompt: 'Search something',
    };
    const invocation = tool['createInvocation'](params, mockMessageBus);

    // Trigger deferred instantiation
    await invocation.shouldConfirmExecute(new AbortController().signal);

    expect(RemoteAgentInvocation).toHaveBeenCalledWith(
      testRemoteDefinition,
      mockConfig,
      { query: 'Search something' },
      mockMessageBus,
    );
  });

  it('should throw error for unknown subagent', () => {
    const params = { agent_name: 'UnknownAgent', prompt: 'Hello' };
    expect(() => {
      tool['createInvocation'](params, mockMessageBus);
    }).toThrow("Subagent 'UnknownAgent' not found.");
  });

  it('should map prompt to task and use BrowserAgentInvocation for browser agent', async () => {
    const params = { agent_name: BROWSER_AGENT_NAME, prompt: 'Open page' };
    const invocation = tool['createInvocation'](params, mockMessageBus);

    // Trigger deferred instantiation
    await invocation.shouldConfirmExecute(new AbortController().signal);

    expect(BrowserAgentInvocation).toHaveBeenCalledWith(
      mockConfig,
      { task: 'Open page' },
      mockMessageBus,
      'invoke_agent',
      'Invoke Browser Agent',
    );
  });

  describe('agentSessionSubagentEnabled feature flag', () => {
    it('should use LocalSessionInvocation when flag is enabled for local agent', async () => {
      vi.spyOn(mockConfig, 'isAgentSessionSubagentEnabled').mockReturnValue(
        true,
      );
      tool = new AgentTool(mockConfig, mockMessageBus);

      const params = {
        agent_name: 'TestLocalAgent',
        prompt: 'Do something',
      };
      const invocation = tool['createInvocation'](params, mockMessageBus);
      await invocation.shouldConfirmExecute(new AbortController().signal);

      expect(LocalSessionInvocation).toHaveBeenCalledWith(
        testLocalDefinition,
        mockConfig,
        { objective: 'Do something' },
        mockMessageBus,
        undefined,
      );
      expect(LocalSubagentInvocation).not.toHaveBeenCalled();
    });

    it('should use RemoteSessionInvocation when flag is enabled for remote agent', async () => {
      vi.spyOn(mockConfig, 'isAgentSessionSubagentEnabled').mockReturnValue(
        true,
      );
      tool = new AgentTool(mockConfig, mockMessageBus);

      const params = {
        agent_name: 'TestRemoteAgent',
        prompt: 'Search something',
      };
      const invocation = tool['createInvocation'](params, mockMessageBus);
      await invocation.shouldConfirmExecute(new AbortController().signal);

      expect(RemoteSessionInvocation).toHaveBeenCalledWith(
        testRemoteDefinition,
        mockConfig,
        { query: 'Search something' },
        mockMessageBus,
        undefined,
      );
      expect(RemoteAgentInvocation).not.toHaveBeenCalled();
    });

    it('should use legacy invocations when flag is disabled (default)', async () => {
      vi.spyOn(mockConfig, 'isAgentSessionSubagentEnabled').mockReturnValue(
        false,
      );
      tool = new AgentTool(mockConfig, mockMessageBus);

      const localParams = {
        agent_name: 'TestLocalAgent',
        prompt: 'Do something',
      };
      const localInv = tool['createInvocation'](localParams, mockMessageBus);
      await localInv.shouldConfirmExecute(new AbortController().signal);

      expect(LocalSubagentInvocation).toHaveBeenCalled();
      expect(LocalSessionInvocation).not.toHaveBeenCalled();

      vi.clearAllMocks();

      const remoteParams = {
        agent_name: 'TestRemoteAgent',
        prompt: 'Search',
      };
      const remoteInv = tool['createInvocation'](remoteParams, mockMessageBus);
      await remoteInv.shouldConfirmExecute(new AbortController().signal);

      expect(RemoteAgentInvocation).toHaveBeenCalled();
      expect(RemoteSessionInvocation).not.toHaveBeenCalled();
    });

    it('should thread onAgentEvent to session invocations', async () => {
      vi.spyOn(mockConfig, 'isAgentSessionSubagentEnabled').mockReturnValue(
        true,
      );
      const onEvent = vi.fn();
      tool = new AgentTool(mockConfig, mockMessageBus, onEvent);

      const params = {
        agent_name: 'TestLocalAgent',
        prompt: 'Do something',
      };
      const invocation = tool['createInvocation'](params, mockMessageBus);
      await invocation.shouldConfirmExecute(new AbortController().signal);

      expect(LocalSessionInvocation).toHaveBeenCalledWith(
        testLocalDefinition,
        mockConfig,
        { objective: 'Do something' },
        mockMessageBus,
        { onAgentEvent: onEvent },
      );
    });

    it('should always use BrowserAgentInvocation for browser agent regardless of flag', async () => {
      vi.spyOn(mockConfig, 'isAgentSessionSubagentEnabled').mockReturnValue(
        true,
      );
      tool = new AgentTool(mockConfig, mockMessageBus);

      const params = {
        agent_name: BROWSER_AGENT_NAME,
        prompt: 'Open page',
      };
      const invocation = tool['createInvocation'](params, mockMessageBus);
      await invocation.shouldConfirmExecute(new AbortController().signal);

      expect(BrowserAgentInvocation).toHaveBeenCalled();
      expect(LocalSessionInvocation).not.toHaveBeenCalled();
      expect(RemoteSessionInvocation).not.toHaveBeenCalled();
    });
  });
});