Skip to content

Commit 536950d

Browse files
Fix session resource aliasing in chat sessions (#298374)
first fix
1 parent d59ed46 commit 536950d

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

src/vs/workbench/api/browser/mainThreadChatAgents2.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,22 @@ export class MainThreadChatAgents2 extends Disposable implements MainThreadChatA
207207
if (newItem) {
208208
chatSessionResource = newItem.resource;
209209
isUntitled = false;
210+
211+
// Update the model's contributed session with the resolved resource
212+
// so subsequent requests don't re-invoke newChatSessionItemHandler
213+
// and getChatSessionFromInternalUri returns the real resource.
214+
chatSession?.setContributedChatSession({
215+
chatSessionType: contributedSession.chatSessionType,
216+
chatSessionResource,
217+
isUntitled: false,
218+
initialSessionOptions: contributedSession.initialSessionOptions,
219+
});
220+
221+
// Register alias so session-option lookups work with the new resource
222+
this._chatSessionService.registerSessionResourceAlias(
223+
contributedSession.chatSessionResource,
224+
chatSessionResource
225+
);
210226
}
211227
}
212228

src/vs/workbench/contrib/chat/browser/chatSessions/chatSessions.contribution.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ export class ChatSessionsService extends Disposable implements IChatSessionsServ
300300
private readonly _sessionTypeInputPlaceholders: Map<string, string> = new Map();
301301

302302
private readonly _sessions = new ResourceMap<ContributedChatSessionData>();
303+
private readonly _resourceAliases = new ResourceMap<URI>(); // real resource -> untitled resource
303304

304305
private readonly _hasCanDelegateProvidersKey: IContextKey<boolean>;
305306

@@ -1078,20 +1079,33 @@ export class ChatSessionsService extends Disposable implements IChatSessionsServ
10781079
}
10791080

10801081
public hasAnySessionOptions(sessionResource: URI): boolean {
1081-
const session = this._sessions.get(sessionResource);
1082+
const session = this._sessions.get(this._resolveResource(sessionResource));
10821083
return !!session && !!session.options && Object.keys(session.options).length > 0;
10831084
}
10841085

10851086
public getSessionOption(sessionResource: URI, optionId: string): string | IChatSessionProviderOptionItem | undefined {
1086-
const session = this._sessions.get(sessionResource);
1087+
const session = this._sessions.get(this._resolveResource(sessionResource));
10871088
return session?.getOption(optionId);
10881089
}
10891090

10901091
public setSessionOption(sessionResource: URI, optionId: string, value: string | IChatSessionProviderOptionItem): boolean {
1091-
const session = this._sessions.get(sessionResource);
1092+
const session = this._sessions.get(this._resolveResource(sessionResource));
10921093
return !!session?.setOption(optionId, value);
10931094
}
10941095

1096+
/**
1097+
* Resolve a resource through the alias map. If the resource is a real
1098+
* resource that has been aliased to an untitled resource, return the
1099+
* untitled resource (the canonical key in {@link _sessions}).
1100+
*/
1101+
private _resolveResource(resource: URI): URI {
1102+
return this._resourceAliases.get(resource) ?? resource;
1103+
}
1104+
1105+
public registerSessionResourceAlias(untitledResource: URI, realResource: URI): void {
1106+
this._resourceAliases.set(realResource, untitledResource);
1107+
}
1108+
10951109
/**
10961110
* Store option groups for a session type
10971111
*/
@@ -1134,7 +1148,7 @@ export class ChatSessionsService extends Disposable implements IChatSessionsServ
11341148
for (const u of updates) {
11351149
this.setSessionOption(sessionResource, u.optionId, u.value);
11361150
}
1137-
this._onDidChangeSessionOptions.fire(sessionResource);
1151+
this._onDidChangeSessionOptions.fire(this._resolveResource(sessionResource));
11381152
this._logService.trace(`[ChatSessionsService] notifySessionOptionsChange: finished for ${sessionResource}`);
11391153
}
11401154

src/vs/workbench/contrib/chat/common/chatSessionsService.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ export interface IChatSessionsService {
306306
* Returns undefined if the controller doesn't have a handler or if no controller is registered.
307307
*/
308308
createNewChatSessionItem(chatSessionType: string, request: IChatAgentRequest, token: CancellationToken): Promise<IChatSessionItem | undefined>;
309+
310+
/**
311+
* Registers an alias so that session-option lookups by the real resource
312+
* are redirected to the canonical (untitled) resource in the internal session map.
313+
*/
314+
registerSessionResourceAlias(untitledResource: URI, realResource: URI): void;
309315
}
310316

311317
export function isSessionInProgressStatus(state: ChatSessionStatus): boolean {

src/vs/workbench/contrib/chat/test/common/mockChatSessionsService.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ export class MockChatSessionsService implements IChatSessionsService {
229229
return undefined;
230230
}
231231

232+
registerSessionResourceAlias(_untitledResource: URI, _realResource: URI): void {
233+
// noop
234+
}
235+
232236
registerChatModelChangeListeners(chatService: IChatService, chatSessionType: string, onChange: () => void): IDisposable {
233237
// Store the emitter so tests can trigger it
234238
this.onChange = onChange;

0 commit comments

Comments
 (0)