Skip to content

Commit 07b51fa

Browse files
authored
adopt provideNewChatSessionItem (#7632)
1 parent 79118a8 commit 07b51fa

3 files changed

Lines changed: 61 additions & 12 deletions

File tree

src/@types/vscode.proposed.chatSessionsProvider.d.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,29 @@ declare module 'vscode' {
3333
*/
3434
readonly onDidChangeChatSessionItems: Event<void>;
3535

36-
// /**
37-
// * Create a new chat session item
38-
// */
39-
// provideNewChatSessionItem(context: {
40-
// // This interface should be extracted
41-
// readonly triggerChat?: {
42-
// readonly prompt: string;
43-
// readonly history: ReadonlyArray<ChatRequestTurn | ChatResponseTurn>;
44-
// };
45-
// }, token: CancellationToken): Thenable<ChatSessionItem> | ChatSessionItem;
36+
/**
37+
* Creates a new chat session.
38+
*
39+
* @param options Options for the new session including an optional initial prompt and history
40+
* @param token A cancellation token
41+
* @returns Metadata for the chat session
42+
*/
43+
provideNewChatSessionItem?(options: {
44+
/**
45+
* Initial prompt to initiate the session
46+
*/
47+
readonly prompt?: string;
48+
49+
/**
50+
* History to initialize the session with
51+
*/
52+
readonly history?: ReadonlyArray<ChatRequestTurn | ChatResponseTurn>;
53+
54+
/**
55+
* Additional metadata to use for session creation
56+
*/
57+
metadata?: any;
58+
}, token: CancellationToken): ProviderResult<ChatSessionItem>;
4659

4760
/**
4861
* Provides a list of chat sessions.
@@ -109,6 +122,7 @@ declare module 'vscode' {
109122
* If not set, then the session will be considered read-only and no requests can be made.
110123
*/
111124
// TODO: Should we introduce our own type for `ChatRequestHandler` since not all field apply to chat sessions?
125+
// TODO: Revisit this to align with code.
112126
readonly requestHandler: ChatRequestHandler | undefined;
113127
}
114128

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ async function deferredActivate(context: vscode.ExtensionContext, showPRControll
433433
return await copilotRemoteAgentManager.provideChatSessionContent(id, token);
434434
};
435435
onDidChangeChatSessionItems = copilotRemoteAgentManager.onDidChangeChatSessions;
436+
provideNewChatSessionItem = async (options: { prompt?: string; history: ReadonlyArray<vscode.ChatRequestTurn | vscode.ChatResponseTurn>; metadata?: any; }, token: vscode.CancellationToken): Promise<vscode.ChatSessionItem> => {
437+
return await copilotRemoteAgentManager.provideNewChatSessionItem(options, token);
438+
};
436439
}();
437440

438441
context.subscriptions.push(vscode.chat?.registerChatSessionItemProvider(

src/github/copilotRemoteAgent.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,40 @@ export class CopilotRemoteAgentManager extends Disposable {
666666
return this._stateModel.getCounts();
667667
}
668668

669+
public async provideNewChatSessionItem(options: { prompt?: string; history: ReadonlyArray<vscode.ChatRequestTurn | vscode.ChatResponseTurn>; metadata?: any; }, _token: vscode.CancellationToken): Promise<ChatSessionWithPR> {
670+
const { prompt } = options;
671+
if (!prompt) {
672+
throw new Error(`Prompt is expected to provide a new chat session item`);
673+
}
674+
const result = await this.invokeRemoteAgent(
675+
prompt,
676+
prompt,
677+
false,
678+
);
679+
if (result.state !== 'success') {
680+
Logger.error(`Failed to provide new chat session item: ${result.error}`, CopilotRemoteAgentManager.ID);
681+
throw new Error(`Failed to provide new chat session item: ${result.error}`);
682+
}
683+
684+
const { number } = result;
685+
686+
const session = await this.findPullRequestById(number, true);
687+
if (!session) {
688+
throw new Error(`Failed to find session for pull request: ${number}`);
689+
}
690+
const timeline = await session.getCopilotTimelineEvents(session);
691+
const status = copilotEventToStatus(mostRecentCopilotEvent(timeline));
692+
const tooltip = await issueMarkdown(session, this.context, this.repositoriesManager);
693+
return {
694+
id: `${session.number}`,
695+
label: session.title || `Session ${session.number}`,
696+
iconPath: this.getIconForSession(status),
697+
description: `${dateFromNow(session.createdAt)}`,
698+
pullRequest: session,
699+
tooltip,
700+
};
701+
}
702+
669703
public async provideChatSessions(token: vscode.CancellationToken): Promise<ChatSessionWithPR[]> {
670704
try {
671705
const capi = await this.copilotApi;
@@ -700,8 +734,6 @@ export class CopilotRemoteAgentManager extends Disposable {
700734
return [];
701735
}
702736

703-
704-
705737
public async provideChatSessionContent(id: string, token: vscode.CancellationToken): Promise<vscode.ChatSession> {
706738
try {
707739
const capi = await this.copilotApi;

0 commit comments

Comments
 (0)