chatModel.ts ×14

Frontier kind: Code frontier

unlabeled · c_6f56ec38f118

100 tests · 44957 LOC · 194 files · introduces 0 tests · 99 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
14 ranges99 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
4178 ranges44957 lines · 194 files · Browse complete extent
All tests (intent)
100 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: 99 introduced LOC across 14 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/workbench/contrib/chat/common/model/chatModel.ts 99 introduced LOC · 14 ranges

Open complete file

2213
2214 constructor(initialState: IChatModelInputState | undefined, private readonly logger: ILogService, private readonly sessionId: string) {
2215 > this._state = observableValueOpts({ debugName: 'inputModelState', equalsFn: equals }, initialState); chatModel.ts
2216 > this.state = this._state;
2217 > }
2218
2219 setState(state: Partial<IChatModelInputState>): void {
2524 private readonly _initialLocation: ChatAgentLocation;
2525 get initialLocation(): ChatAgentLocation {
2526 > return this._initialLocation; chatModel.ts
2527 > }
2528
2529 private readonly _canUseTools: boolean = true;
2540
2541 constructor(
2542 > dataRef: ISerializedChatDataReference | undefined, chatModel.ts
2543 > initialModelProps: { initialLocation: ChatAgentLocation; canUseTools: boolean; inputState?: ISerializableChatModelInputState; resource?: URI; disableBackgroundKeepAlive?: boolean; isReadOnly?: IObservable<boolean> },
2544 > @ILogService private readonly logService: ILogService,
2545 > @IChatAgentService private readonly chatAgentService: IChatAgentService,
2546 > @IChatEditingService private readonly chatEditingService: IChatEditingService,
2547 > @IChatService private readonly chatService: IChatService,
2548 > ) {
2549 > super();
2550 >
2551 > const initialData = dataRef?.value;
2552 > const isValidExportedData = isExportableSessionData(initialData);
2553 > const isValidFullData = isValidExportedData && isSerializableSessionData(initialData);
2554 > if (initialData && !isValidExportedData) {
2555 this.logService.warn(`ChatModel#constructor: Loaded malformed session data: ${JSON.stringify(initialData)}`);
2556 }
2557 > chatModel.ts
2558 > this._isImported = !!initialData && isValidExportedData && !isValidFullData;
2559 >
2560 > // Set the session resource and id
2561 > if (initialModelProps.resource) {
2562 // prefer using the provided resource if provided
2563 this._sessionId = chatSessionResourceToId(initialModelProps.resource);
2564 this._sessionResource = initialModelProps.resource;
2565 > } else if (isValidFullData) { chatModel.ts
2566 // Otherwise use the serialized id. This is only valid for local chat sessions
2567 this._sessionId = initialData.sessionId;
2573 this._sessionResource = LocalChatSessionUri.forSession(this._sessionId);
2574 }
2575 > chatModel.ts
2576 > this._disableBackgroundKeepAlive = initialModelProps.disableBackgroundKeepAlive ?? false;
2577 >
2578 > this._timestamp = (isValidFullData && initialData.creationDate) || Date.now();
2579 > this._requests = initialData ? this._deserialize(initialData) : [];
2580 > this._customTitle = isValidFullData ? initialData.customTitle : undefined;
2581 >
2582 > // Initialize input model from serialized data (undefined for new chats)
2583 > const serializedInputState = initialModelProps.inputState || (isValidFullData && initialData.inputState ? initialData.inputState : undefined);
2584 > this.inputModel = new InputModel(serializedInputState && {
2585 attachments: (serializedInputState.attachments ?? []).map(IChatRequestVariableEntry.fromExport),
2586 mode: serializedInputState.mode,
2594 selections: serializedInputState.selections,
2595 permissionLevel: serializedInputState.permissionLevel,
2596 > }, this.logService, this._sessionId); chatModel.ts
2597 >
2598 > this.dataSerializer = dataRef?.serializer;
2599 > this._initialResponderUsername = initialData?.responderUsername;
2600 >
2601 > this._repoData = isValidFullData && initialData.repoData ? initialData.repoData : undefined;
2602 >
2603 > this._workingDirectory = isValidFullData && initialData.workingDirectory ? URI.parse(initialData.workingDirectory) : undefined;
2604 >
2605 > // Hydrate pending requests from serialized data
2606 > if (isValidFullData && initialData.pendingRequests) {
2607 this._pendingRequests = this._deserializePendingRequests(initialData.pendingRequests);
2608 }
2609 > chatModel.ts
2610 > this._initialLocation = initialData?.initialLocation ?? initialModelProps.initialLocation;
2611 >
2612 > this._canUseTools = initialModelProps.canUseTools;
2613 > this.isReadOnly = initialModelProps.isReadOnly ?? constObservable(false);
2614 >
2615 > this.lastRequestObs = observableFromEvent(this, this.onDidChange, () => this._requests.at(-1));
2616 >
2617 > this._register(autorun(reader => {
2618 > const request = this.lastRequestObs.read(reader);
2619 > if (!request?.response) {
2620 return;
2621 }
2628 this._onDidChange.fire({ kind: 'completedRequest', request });
2629 }));
2630 > })); chatModel.ts
2631 >
2632 > this.requestInProgress = this.lastRequestObs.map((request, r) => {
2633 > return request?.response?.isInProgress.read(r) ?? false;
2634 > });
2635 >
2636 > this.hasActiveRequest = this.lastRequestObs.map((request, r) => {
2637 return request?.response?.isIncomplete.read(r) ?? false;
2638 > }); chatModel.ts
2639 >
2640 > this.requestNeedsInput = this.lastRequestObs.map((request, r) => {
2641 > const pendingInfo = request?.response?.isPendingConfirmation.read(r);
2642 > if (!pendingInfo) {
2643 > return undefined;
2644 > }
2645 return {
2646 title: this.title,
2647 detail: pendingInfo.detail,
2648 };
2649 > }); chatModel.ts
2650 >
2651 > // Retain a reference to itself when a request is in progress, so the ChatModel stays alive in the background
2652 > // only while running a request. TODO also keep it alive for 5min or so so we don't have to dispose/restore too often?
2653 > if (this.initialLocation === ChatAgentLocation.Chat && !initialModelProps.disableBackgroundKeepAlive) {
2654 > const selfRef = this._register(new MutableDisposable<IChatModelReference>());
2655 > this._register(autorun(r => {
2656 > const inProgress = this.requestInProgress.read(r);
2657 > const needsInput = this.requestNeedsInput.read(r);
2658 > const shouldStayAlive = inProgress || !!needsInput;
2659 > if (shouldStayAlive && !selfRef.value) {
2660 selfRef.value = chatService.acquireExistingSession(this._sessionResource, 'ChatModel#requestInProgressKeepAlive');
2661 > } else if (!shouldStayAlive && selfRef.value) { chatModel.ts
2662 selfRef.clear();
2663 }
2664 > })); chatModel.ts
2665 > }
2666 > }
2667
2668 startEditingSession(isGlobalEditingSession?: boolean, transferFromSession?: IChatEditingSession): void {
3156
3157 override dispose() {
3158 > this._requests.forEach(r => r.response?.dispose()); chatModel.ts
3159 > this._onDidDispose.fire();
3160 >
3161 > super.dispose();
3162 >
3163 > // Null out heavy fields to break retention chains. Even after disposal,
3164 > // stale references (closures, cached templates, etc.) may prevent GC
3165 > // from collecting this object. Clearing these fields ensures the
3166 > // conversation data, serialization snapshot, and editing session are
3167 > // freed regardless.
3168 > this._requests.length = 0;
3169 > this.dataSerializer = undefined;
3170 > this._editingSession = undefined;
3171 > }
3172 }
3173