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.

1 > /*--------------------------------------------------------------------------------------------- claudeSdkMessageRouter.ts ×4
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, claudeSdkMessageRouter.ts ×1
43 > private readonly _chatChannelUri: URI,
44 > dbRef: IReference<ISessionDatabase>,
45 > private readonly _subagents: SubagentRegistry,
46 > clientToolOwner: ((toolName: string) => string | undefined) | undefined = undefined,
47 > @IInstantiationService instantiationService: IInstantiationService,
48 > @ILogService private readonly _logService: ILogService,
49 > ) {
50 > super();
51 > this._clientToolOwner = clientToolOwner;
52 > this._editObserver = this._register(
53 > instantiationService.createInstance(ClaudeFileEditObserver, sessionUri.toString(), dbRef),
54 > );
55 > }
57 > setClientToolOwner(clientToolOwner: ((toolName: string) => string | undefined) | undefined): void {
58 this._clientToolOwner = clientToolOwner;
59 }
61 > async handle(message: SDKMessage, turnId: string | undefined, turnDuration?: number): Promise<void> {
62 > if (message.type === 'assistant') { claudeSdkMessageRouter.ts ×4
63 > this._editObserver.observeAssistant(message); claudeSdkMessageRouter.ts ×1
64 > } else if (message.type === 'user' && turnId !== undefined) { claudeSdkMessageRouter.ts ×4
65 > await this._editObserver.observeUser(message, turnId, this._mapperState); claudeSdkMessageRouter.ts ×1
66 > }
67 > if (turnId === undefined) { claudeSdkMessageRouter.ts ×4
69 > }
71 > const signals = mapSDKMessageToAgentSignals(
72 > message,
73 > this._chatChannelUri,
74 > turnId,
75 > this._mapperState,
76 > this._logService,
77 > this._subagents,
78 > this._clientToolOwner,
79 > turnDuration,
80 > );
81 > for (const signal of signals) {
82 > this._onDidProduceSignal.fire(signal); claudeSdkMessageRouter.ts ×1
83 > }
84 > } catch (mapperErr) { claudeSdkMessageRouter.ts ×2
85 > this._logService.warn(`[ClaudeSdkMessageRouter] mapper threw, skipping message: ${mapperErr}`); claudeSdkMessageRouter.ts ×1
86 > }