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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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { type AgentLoopContext } from '../config/agent-loop-context.js';
import { MessageBusType } from '../confirmation-bus/types.js';
import {
  BaseToolInvocation,
  type ToolResult,
  type ExecuteOptions,
} from '../tools/tools.js';
import {
  type LocalAgentDefinition,
  type AgentInputs,
  type SubagentActivityEvent,
  type SubagentProgress,
  type SubagentActivityItem,
  AgentTerminateMode,
  SubagentActivityErrorType,
  SUBAGENT_REJECTED_ERROR_PREFIX,
  SUBAGENT_CANCELLED_ERROR_MESSAGE,
  isToolActivityError,
  SubagentState,
} from './types.js';
import { randomUUID } from 'node:crypto';
import type { MessageBus } from '../confirmation-bus/message-bus.js';
import {
  sanitizeThoughtContent,
  sanitizeToolArgs,
  sanitizeErrorMessage,
} from '../utils/agent-sanitization-utils.js';
import { checkExhaustive } from '../utils/checks.js';
import { LocalSubagentSession } from './local-subagent-protocol.js';
import type { AgentEvent } from '../agent/types.js';

const MAX_RECENT_ACTIVITY = 3;

/** Optional configuration for subagent invocations. */
export interface SubagentInvocationOptions {
  toolName?: string;
  toolDisplayName?: string;
  onAgentEvent?: (event: AgentEvent) => void;
}

/**
 * Session-based local subagent invocation.
 *
 * This class orchestrates the execution of a defined agent by:
 * 1. Using {@link LocalSubagentSession} as the execution engine.
 * 2. Bridging the agent's streaming activity (e.g., thoughts) to the tool's
 *    live output stream via the session's rawActivityCallback.
 * 3. Formatting the final result into a {@link ToolResult}.
 */
export class LocalSessionInvocation extends BaseToolInvocation<
  AgentInputs,
  ToolResult
> {
  private readonly _onAgentEvent?: (event: AgentEvent) => void;

  /**
   * @param definition The definition object that configures the agent.
   * @param context The agent loop context.
   * @param params The validated input parameters for the agent.
   * @param messageBus Message bus for policy enforcement.
   * @param options Optional overrides for tool name, display name, and event callback.
   */
  constructor(
    private readonly definition: LocalAgentDefinition,
    private readonly context: AgentLoopContext,
    params: AgentInputs,
    messageBus: MessageBus,
    options?: SubagentInvocationOptions,
  ) {
    super(
      params,
      messageBus,
      options?.toolName ?? definition.name,
      options?.toolDisplayName ?? definition.displayName,
    );
    this._onAgentEvent = options?.onAgentEvent;
  }

  /**
   * Returns a concise, human-readable description of the invocation.
   * Used for logging and display purposes.
   */
  getDescription(): string {
    const inputSummary = Object.entries(this.params)
      .map(([key, value]) => `${key}: ${String(value)}`)
      .join(', ');

    return `Running subagent '${this.definition.name}' with inputs: { ${inputSummary} }`;
  }

  private publishActivity(activity: SubagentActivityItem): void {
    void this.messageBus.publish({
      type: MessageBusType.SUBAGENT_ACTIVITY,
      subagentName: this.definition.displayName ?? this.definition.name,
      activity,
    });
  }

  /**
   * Executes the subagent.
   *
   * @param options Options for tool execution including signal and output updates.
   * @returns A `Promise` that resolves with the final `ToolResult`.
   */
  async execute(options: ExecuteOptions): Promise<ToolResult> {
    const { abortSignal: signal, updateOutput } = options;
    let recentActivity: SubagentActivityItem[] = [];

    // Raw SubagentActivityEvent handler — preserves all existing progress display logic.
    // Passed as rawActivityCallback to LocalSubagentSession so the protocol can call it
    // before translating to AgentEvents.
    const onActivity = (activity: SubagentActivityEvent): void => {
      if (!updateOutput) return;

      let updated = false;

      switch (activity.type) {
        case 'THOUGHT_CHUNK': {
          const rawText = activity.data['text'];
          const text = typeof rawText === 'string' ? rawText : '';
          const lastItem = recentActivity[recentActivity.length - 1];

          if (
            lastItem &&
            lastItem.type === 'thought' &&
            lastItem.status === SubagentState.RUNNING
          ) {
            lastItem.content = sanitizeThoughtContent(text);
          } else {
            recentActivity.push({
              id: randomUUID(),
              type: 'thought',
              content: sanitizeThoughtContent(text),
              status: SubagentState.RUNNING,
            });
          }
          updated = true;

          const latestThought = recentActivity[recentActivity.length - 1];
          if (latestThought) {
            this.publishActivity(latestThought);
          }
          break;
        }
        case 'TOOL_CALL_START': {
          const rawName = activity.data['name'];
          const name = typeof rawName === 'string' ? rawName.trim() : '';
          const displayName = activity.data['displayName']
            ? sanitizeErrorMessage(String(activity.data['displayName']).trim())
            : undefined;
          const description = activity.data['description']
            ? sanitizeErrorMessage(String(activity.data['description']))
            : undefined;
          const args = JSON.stringify(sanitizeToolArgs(activity.data['args']));
          const callId = activity.data['callId']
            ? String(activity.data['callId'])
            : randomUUID();
          recentActivity.push({
            id: callId,
            type: 'tool_call',
            content: name,
            displayName,
            description,
            args,
            status: SubagentState.RUNNING,
          });
          updated = true;

          const latestTool = recentActivity[recentActivity.length - 1];
          if (latestTool) {
            this.publishActivity(latestTool);
          }
          break;
        }
        case 'TOOL_CALL_END': {
          const data = activity.data['data'];
          const isError = isToolActivityError(data);

          const callId = activity.data['id']
            ? String(activity.data['id'])
            : undefined;

          if (callId) {
            for (let i = recentActivity.length - 1; i >= 0; i--) {
              if (
                recentActivity[i].type === 'tool_call' &&
                recentActivity[i].id === callId &&
                recentActivity[i].status === SubagentState.RUNNING
              ) {
                recentActivity[i].status = isError
                  ? SubagentState.ERROR
                  : SubagentState.COMPLETED;
                updated = true;

                this.publishActivity(recentActivity[i]);
                break;
              }
            }
          }
          break;
        }
        case 'ERROR': {
          const rawError = activity.data['error'];
          const error = typeof rawError === 'string' ? rawError.trim() : '';
          const errorType = activity.data['errorType'];
          const sanitizedError = sanitizeErrorMessage(error);
          const isCancellation =
            errorType === SubagentActivityErrorType.CANCELLED ||
            error === SUBAGENT_CANCELLED_ERROR_MESSAGE;
          const isRejection =
            errorType === SubagentActivityErrorType.REJECTED ||
            error.startsWith(SUBAGENT_REJECTED_ERROR_PREFIX);

          const callId = activity.data['callId']
            ? String(activity.data['callId'])
            : undefined;

          if (callId) {
            const targetStatus =
              isCancellation || isRejection
                ? SubagentState.CANCELLED
                : SubagentState.ERROR;

            for (let i = recentActivity.length - 1; i >= 0; i--) {
              if (
                recentActivity[i].type === 'tool_call' &&
                recentActivity[i].id === callId &&
                recentActivity[i].status === SubagentState.RUNNING
              ) {
                recentActivity[i].status = targetStatus;
                updated = true;
                break;
              }
            }
          }

          recentActivity.push({
            id: randomUUID(),
            type: 'thought',
            content:
              isCancellation || isRejection
                ? sanitizedError
                : `Error: ${sanitizedError}`,
            status:
              isCancellation || isRejection
                ? SubagentState.CANCELLED
                : SubagentState.ERROR,
          });
          updated = true;
          break;
        }
        default:
          checkExhaustive(activity.type);
          break;
      }

      if (updated) {
        // Keep only the last N items
        if (recentActivity.length > MAX_RECENT_ACTIVITY) {
          recentActivity = recentActivity.slice(-MAX_RECENT_ACTIVITY);
        }

        const progress: SubagentProgress = {
          isSubagentProgress: true,
          agentName: this.definition.name,
          recentActivity: [...recentActivity], // Copy to avoid mutation issues
          state: SubagentState.RUNNING,
        };

        updateOutput(progress);
      }
    };

    // Create session with the raw activity callback for rich progress display
    const session = new LocalSubagentSession(
      this.definition,
      this.context,
      this.messageBus,
      onActivity,
    );

    // Subscribe for parent session observability
    let unsubscribeParent: (() => void) | undefined;
    if (this._onAgentEvent) {
      unsubscribeParent = session.subscribe(this._onAgentEvent);
    }

    // Wire external abort signal to session abort
    const abortListener = () => void session.abort();
    signal.addEventListener('abort', abortListener, { once: true });

    try {
      if (updateOutput) {
        const initialProgress: SubagentProgress = {
          isSubagentProgress: true,
          agentName: this.definition.name,
          recentActivity: [],
          state: SubagentState.RUNNING,
        };
        updateOutput(initialProgress);
      }

      // Buffer non-query params, then send query as message to start execution
      const query = String(this.params['query'] ?? '');
      const otherParams = { ...this.params } as Record<string, unknown>;
      delete otherParams['query'];
      if (Object.keys(otherParams).length > 0) {
        await session.send({ update: { config: otherParams } });
      }
      await session.send({
        message: { content: [{ type: 'text', text: query }] },
      });

      const output = await session.getResult();

      if (output.terminate_reason === AgentTerminateMode.ABORTED) {
        const progress: SubagentProgress = {
          isSubagentProgress: true,
          agentName: this.definition.name,
          recentActivity: [...recentActivity],
          state: SubagentState.CANCELLED,
        };

        if (updateOutput) {
          updateOutput(progress);
        }

        const cancelError = new Error('Operation cancelled by user');
        cancelError.name = 'AbortError';
        throw cancelError;
      }

      const progress: SubagentProgress = {
        isSubagentProgress: true,
        agentName: this.definition.name,
        recentActivity: [...recentActivity],
        state: SubagentState.COMPLETED,
        result: output.result,
        terminateReason: output.terminate_reason,
      };

      if (updateOutput) {
        updateOutput(progress);
      }

      const resultContent = `Subagent '${this.definition.name}' finished.
Termination Reason: ${output.terminate_reason}
Result:
${output.result}`;

      return {
        llmContent: [{ text: resultContent }],
        returnDisplay: progress,
      };
    } catch (error) {
      const errorMessage =
        error instanceof Error ? error.message : String(error);

      const isAbort =
        (error instanceof Error && error.name === 'AbortError') ||
        errorMessage.includes('Aborted');

      // Mark any running items as error/cancelled
      for (const item of recentActivity) {
        if (item.status === SubagentState.RUNNING) {
          item.status = isAbort ? SubagentState.CANCELLED : SubagentState.ERROR;
        }
      }

      // Ensure the error is reflected in the recent activity for display
      if (!isAbort) {
        const lastActivity = recentActivity[recentActivity.length - 1];
        if (!lastActivity || lastActivity.status !== SubagentState.ERROR) {
          recentActivity.push({
            id: randomUUID(),
            type: 'thought',
            content: `Error: ${errorMessage}`,
            status: SubagentState.ERROR,
          });
          if (recentActivity.length > MAX_RECENT_ACTIVITY) {
            recentActivity = recentActivity.slice(-MAX_RECENT_ACTIVITY);
          }
        }
      }

      const progress: SubagentProgress = {
        isSubagentProgress: true,
        agentName: this.definition.name,
        recentActivity: [...recentActivity],
        state: isAbort ? SubagentState.CANCELLED : SubagentState.ERROR,
      };

      if (updateOutput) {
        updateOutput(progress);
      }

      if (isAbort) {
        throw error;
      }

      return {
        llmContent: `Subagent '${this.definition.name}' failed. Error: ${errorMessage}`,
        returnDisplay: progress,
      };
    } finally {
      signal.removeEventListener('abort', abortListener);
      unsubscribeParent?.();
    }
  }
}