chatModelStore.ts ×16

Frontier kind: Code frontier

unlabeled · c_478fe2aca61e

85 tests · 22574 LOC · 120 files · introduces 0 tests · 106 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
16 ranges106 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
2507 ranges22574 lines · 120 files · Browse complete extent
All tests (intent)
85 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

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 native relationship evidence on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

1 file ranked by introduced lines: 106 introduced LOC across 16 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/workbench/contrib/chat/common/model/chatModelStore.ts 106 introduced LOC · 16 ranges

Open complete file

1 > /*--------------------------------------------------------------------------------------------- chatModelStore.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 { Emitter } from '../../../../../base/common/event.js';
7 > import { Disposable, IReference, ReferenceCollection } from '../../../../../base/common/lifecycle.js';
8 > import { IObservable, ObservableMap } from '../../../../../base/common/observable.js';
9 > import { URI } from '../../../../../base/common/uri.js';
10 > import { ILogService } from '../../../../../platform/log/common/log.js';
11 > import { ChatAgentLocation } from '../constants.js';
12 > import { IChatEditingSession, ModifiedFileEntryState } from '../editing/chatEditingService.js';
13 > import { ChatModel, ISerializableChatModelInputState, ISerializedChatDataReference } from './chatModel.js';
14 >
15 > export interface IStartSessionProps {
16 > readonly initialData?: ISerializedChatDataReference;
17 > readonly location: ChatAgentLocation;
18 > readonly sessionResource: URI;
19 > readonly canUseTools: boolean;
20 > readonly transferEditingSession?: IChatEditingSession;
21 > readonly disableBackgroundKeepAlive?: boolean;
22 > readonly inputState?: ISerializableChatModelInputState;
23 > readonly isReadOnly?: IObservable<boolean>;
24 > }
25 >
26 > export interface ChatModelStoreDelegate {
27 > createModel: (props: IStartSessionProps) => ChatModel;
28 > willDisposeModel: (model: ChatModel) => Promise<void>;
29 > }
30 >
31 > export interface IChatModelReferenceDebugHolder {
32 > readonly holder: string;
33 > readonly count: number;
34 > }
35 >
36 > export interface IChatModelReferenceDebugInfo {
37 > readonly sessionResource: URI;
38 > readonly title: string;
39 > readonly createdBy: string;
40 > readonly initialLocation: ChatAgentLocation;
41 > readonly isImported: boolean;
42 > readonly willKeepAlive: boolean;
43 > readonly hasPendingEdits: boolean;
44 > readonly pendingDisposal: boolean;
45 > readonly referenceCount: number;
46 > readonly holders: readonly IChatModelReferenceDebugHolder[];
47 > }
48 >
49 > export interface IChatModelReferenceDebugSnapshot {
50 > readonly totalModels: number;
51 > readonly totalReferences: number;
52 > readonly models: readonly IChatModelReferenceDebugInfo[];
53 > }
54 >
55 > export class ChatModelStore extends Disposable {
56 > private readonly _refCollection: ReferenceCollection<ChatModel>;
57 >
58 > private readonly _models = new ObservableMap<string, ChatModel>();
59 > private readonly _modelsToDispose = new Set<string>();
60 > private readonly _pendingDisposals = new Set<Promise<void>>();
61 > private readonly _modelCreateOwners = new Map<string, string>();
62 > private readonly _referenceOwners = new Map<string, Map<number, string>>();
63 > private _referenceOwnerIds = 0;
64 >
65 > private readonly _onDidDisposeModel = this._register(new Emitter<ChatModel>());
66 > public readonly onDidDisposeModel = this._onDidDisposeModel.event;
67 >
68 > private readonly _onDidCreateModel = this._register(new Emitter<ChatModel>());
69 > public readonly onDidCreateModel = this._onDidCreateModel.event;
70 >
71 > constructor(
72 private readonly delegate: ChatModelStoreDelegate,
73 @ILogService private readonly logService: ILogService,
85 }();
86 }
88 > public get observable() {
89 return this._models.observable;
90 }
92 > public values(): Iterable<ChatModel> {
93 return this._models.values();
94 }
96 > /**
97 > * Get a ChatModel directly without acquiring a reference.
98 > */
99 > public get(uri: URI): ChatModel | undefined {
100 return this._models.get(this.toKey(uri));
101 }
103 > public has(uri: URI): boolean {
104 return this._models.has(this.toKey(uri));
105 }
107 > public acquireExisting(uri: URI, debugOwner?: string): IReference<ChatModel> | undefined {
108 const key = this.toKey(uri);
109 if (!this._models.has(key)) {
113 return this.wrapReference(key, this._refCollection.acquire(key, undefined, debugOwner), debugOwner);
114 }
116 > public acquireOrCreate(props: IStartSessionProps, debugOwner?: string): IReference<ChatModel> {
117 const key = this.toKey(props.sessionResource);
118 return this.wrapReference(key, this._refCollection.acquire(key, props, debugOwner), debugOwner);
119 }
121 > public getReferenceDebugSnapshot(): IChatModelReferenceDebugSnapshot {
122 const models = Array.from(this._models.values())
123 .map(model => {
154 };
155 }
157 > private createReferencedObject(key: string, props?: IStartSessionProps, debugOwner?: string): ChatModel {
158 this._modelsToDispose.delete(key);
159 const existingModel = this._models.get(key);
176 return model;
177 }
179 > private destroyReferencedObject(key: string, object: ChatModel): void {
180 this._modelsToDispose.add(key);
181 const promise = this.doDestroyReferencedObject(key, object);
185 });
186 }
188 > private async doDestroyReferencedObject(key: string, object: ChatModel): Promise<void> {
189 try {
190 await this.delegate.willDisposeModel(object);
203 }
204 }
206 > private wrapReference(key: string, reference: IReference<ChatModel>, debugOwner?: string): IReference<ChatModel> {
207 const ownerId = ++this._referenceOwnerIds;
208 let ownerEntries = this._referenceOwners.get(key);
236 return wrapped;
237 }
239 > /**
240 > * For test use only
241 > */
242 > async waitForModelDisposals(): Promise<void> {
243 await Promise.all(this._pendingDisposals);
244 }
246 > private toKey(uri: URI): string {
247 return uri.toString();
248 }
250 > override dispose(): void {
251 super.dispose();
252 this._models.forEach(model => model.dispose());
253 }