src/vs/platform/agentHost/node/claude/claudeSdkMessageRouter.ts
88 LOC · 86 covered · 2 uncovered · 16 ranges · 408 concepts · 9 introducers · 221 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
claudeSdkMessageRouter.ts ×4
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { SDKMessage } from '@anthropic-ai/claude-agent-sdk';
import { Emitter, Event } from '../../../../base/common/event.js';
import { Disposable, IReference } from '../../../../base/common/lifecycle.js';
import { URI } from '../../../../base/common/uri.js';
import { IInstantiationService } from '../../../instantiation/common/instantiation.js';
import { ILogService } from '../../../log/common/log.js';
import { AgentSignal } from '../../common/agentService.js';
import { ISessionDatabase } from '../../common/sessionDataService.js';
import { ClaudeFileEditObserver } from './claudeFileEditObserver.js';
import { ClaudeMapperState, mapSDKMessageToAgentSignals } from './claudeMapSessionEvents.js';
import type { SubagentRegistry } from './claudeSubagentRegistry.js';
/**
* Per-message router. Awaits file-edit observation for `type: 'user'`
* messages so the cached edit lands before {@link mapSDKMessageToAgentSignals}
* reads it via `state.takeFileEdit`, then fires mapped signals on
* {@link onDidProduceSignal}. Mapper failures are logged but never thrown.
*
* Owns the per-session {@link ClaudeFileEditObserver} (Phase 8) and
* {@link ClaudeMapperState} (Phase 7) — both are private to the
* message-handling pipeline and have no other consumers. Phase 12
* subagent correlation state lives on {@link IClaudeSubagentResolver}
* (host-singleton, keyed by parent session URI), which the router
* forwards into every mapper invocation.
*/
export class ClaudeSdkMessageRouter extends Disposable {
private readonly _onDidProduceSignal = this._register(new Emitter<AgentSignal>());
readonly onDidProduceSignal: Event<AgentSignal> = this._onDidProduceSignal.event;
private readonly _editObserver: ClaudeFileEditObserver;
private readonly _mapperState = new ClaudeMapperState();
private _clientToolOwner: ((toolName: string) => string | undefined) | undefined;
constructor(
private readonly _chatChannelUri: URI,
dbRef: IReference<ISessionDatabase>,
private readonly _subagents: SubagentRegistry,
clientToolOwner: ((toolName: string) => string | undefined) | undefined = undefined,
@IInstantiationService instantiationService: IInstantiationService,
@ILogService private readonly _logService: ILogService,
) {
super();
this._clientToolOwner = clientToolOwner;
this._editObserver = this._register(
instantiationService.createInstance(ClaudeFileEditObserver, sessionUri.toString(), dbRef),
);
}
setClientToolOwner(clientToolOwner: ((toolName: string) => string | undefined) | undefined): void {
this._clientToolOwner = clientToolOwner;
}
async handle(message: SDKMessage, turnId: string | undefined, turnDuration?: number): Promise<void> {
await this._editObserver.observeUser(message, turnId, this._mapperState);
claudeSdkMessageRouter.ts ×1
}
}
const signals = mapSDKMessageToAgentSignals(
message,
this._chatChannelUri,
turnId,
this._mapperState,
this._logService,
this._subagents,
this._clientToolOwner,
turnDuration,
);
for (const signal of signals) {
}
this._logService.warn(`[ClaudeSdkMessageRouter] mapper threw, skipping message: ${mapperErr}`);
claudeSdkMessageRouter.ts ×1
}