1
>
/*---------------------------------------------------------------------------------------------
claudeSdkMessageRouter.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import type { SDKMessage } from '@anthropic-ai/claude-agent-sdk';
7
>
import { Emitter, Event } from '../../../../base/common/event.js';
8
>
import { Disposable, IReference } from '../../../../base/common/lifecycle.js';
9
>
import { URI } from '../../../../base/common/uri.js';
10
>
import { IInstantiationService } from '../../../instantiation/common/instantiation.js';
11
>
import { ILogService } from '../../../log/common/log.js';
12
>
import { AgentSignal } from '../../common/agentService.js';
13
>
import { ISessionDatabase } from '../../common/sessionDataService.js';
14
>
import { ClaudeFileEditObserver } from './claudeFileEditObserver.js';
15
>
import { ClaudeMapperState, mapSDKMessageToAgentSignals } from './claudeMapSessionEvents.js';
16
>
import type { SubagentRegistry } from './claudeSubagentRegistry.js';
17
>
18
>
/**
19
>
* Per-message router. Awaits file-edit observation for `type: 'user'`
20
>
* messages so the cached edit lands before {@link mapSDKMessageToAgentSignals}
21
>
* reads it via `state.takeFileEdit`, then fires mapped signals on
22
>
* {@link onDidProduceSignal}. Mapper failures are logged but never thrown.
23
>
*
24
>
* Owns the per-session {@link ClaudeFileEditObserver} (Phase 8) and
25
>
* {@link ClaudeMapperState} (Phase 7) — both are private to the
26
>
* message-handling pipeline and have no other consumers. Phase 12
27
>
* subagent correlation state lives on {@link IClaudeSubagentResolver}
28
>
* (host-singleton, keyed by parent session URI), which the router
29
>
* forwards into every mapper invocation.
30
>
*/
31
>
export class ClaudeSdkMessageRouter extends Disposable {
32
>
33
>
private readonly _onDidProduceSignal = this._register(new Emitter<AgentSignal>());
34
>
readonly onDidProduceSignal: Event<AgentSignal> = this._onDidProduceSignal.event;
35
>
36
>
private readonly _editObserver: ClaudeFileEditObserver;
37
>
private readonly _mapperState = new ClaudeMapperState();
38
>
39
>
private _clientToolOwner: ((toolName: string) => string | undefined) | undefined;
40
>
41
>
constructor(
42
sessionUri: URI,
43
private readonly _chatChannelUri: URI,