src/vs/platform/agentHost/common/agentHostClientByokLmChannel.ts
114 LOC · 108 covered · 6 uncovered · 18 ranges · 9 concepts · 8 introducers · 6 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.
/*---------------------------------------------------------------------------------------------
agentHostClientByokLmChannel.ts ×5
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CancellationToken } from '../../../base/common/cancellation.js';
import { Throttler } from '../../../base/common/async.js';
import { Emitter, Event } from '../../../base/common/event.js';
import { DisposableStore } from '../../../base/common/lifecycle.js';
import { IChannel, IServerChannel } from '../../../base/parts/ipc/common/ipc.js';
import { ILogService } from '../../log/common/log.js';
import {
IAgentHostByokLmHandler,
IByokLmBridgeConnection,
IByokLmChatRequest,
IByokLmChatResult,
IByokLmModelInfo,
} from './agentHostByokLm.js';
/**
* IPC channel name used for in-process agent-host → renderer reverse BYOK
* language-model RPCs. The renderer registers a server channel under this
* name on its `MessagePortClient`; the agent host reaches it via
* `server.getChannel(name, c => c.ctx === clientId)` on its
* `UtilityProcessServer`.
*/
export const AGENT_HOST_CLIENT_BYOK_LM_CHANNEL = 'agentHostClientByokLm';
/**
* Wraps an {@link IChannel} (obtained from the agent host's
* `UtilityProcessServer.getChannel`) into an {@link IByokLmBridgeConnection}
* suitable for the node-side {@link IByokLmProxyService}. This is the node end
* of the bridge: `chat()` ships the request to the renderer and resolves with
* the buffered completion the renderer produced from the LM API, and
* `onDidChangeModels` is the renderer's pushed model snapshot stream.
*/
export function createAgentHostClientByokLmConnection(channel: IChannel): IByokLmBridgeConnection {
chat: (request) => channel.call('chat', request) as Promise<IByokLmChatResult>,
onDidChangeModels: channel.listen<IByokLmModelInfo[]>('models'),
};
}
/**
* Server-side channel for in-process reverse BYOK LM RPCs from the local agent
* host. Thin adapter — forwards `chat` calls to the renderer's
* {@link IAgentHostByokLmHandler} (backed by `ILanguageModelsService`) and
* serves the pushed `models` snapshot stream.
*/
export class AgentHostClientByokLmChannel implements IServerChannel {
constructor(
@IAgentHostByokLmHandler private readonly _handler: IAgentHostByokLmHandler,
@ILogService private readonly _logService: ILogService,
) { }
listen<T>(_ctx: unknown, event: string): Event<T> {
}
throw new Error(`No event '${event}' on AgentHostClientByokLmChannel`);
agentHostClientByokLmChannel.ts ×1
/**
* A snapshot stream of the renderer's BYOK models: emits the current models
* when a subscriber attaches, then re-emits whenever the handler reports a
* change. Enumeration is renderer-local, so the node side only ever receives.
*
* A {@link Throttler} serializes overlapping publishes and coalesces bursts,
* so a slow enumeration can't fire a stale snapshot after a newer one.
*/
private _modelsSnapshotEvent(): Event<IByokLmModelInfo[]> {
const throttler = store.add(new Throttler());
const emitter = store.add(new Emitter<IByokLmModelInfo[]>({
onDidAddFirstListener: () => {
if (this._handler.onDidChangeModels) {
store.add(this._handler.onDidChangeModels(() => void publish()));
}
void publish();
},
onDidRemoveLastListener: () => store.dispose(),
}));
const publish = () => {
if (store.isDisposed) {
return; // avoid a floating rejection from a disposed throttler
}
try {
const models = await this._handler.listModels(CancellationToken.None);
if (!store.isDisposed) {
emitter.fire(models);
}
} catch (err) {
// Leave the snapshot unpublished (the connection stays non-serving);
// surface the error so renderer-side failures are diagnosable.
this._logService.warn('AgentHostClientByokLmChannel: failed to enumerate BYOK models from the renderer', err);
}
};
return emitter.event;
}
async call<T>(_ctx: unknown, command: string, arg?: unknown): Promise<T> {
case 'chat': {
const result = await this._handler.chat(arg as IByokLmChatRequest, CancellationToken.None);
agentHostClientByokLmChannel.ts ×1
return result as T;
}
throw new Error(`Unknown command '${command}' on AgentHostClientByokLmChannel`);
agentHostClientByokLmChannel.ts ×1