Skip to content

Commit 917fb4d

Browse files
authored
Updates (#1745)
1 parent 14b83b5 commit 917fb4d

File tree

5 files changed

+207
-157
lines changed

5 files changed

+207
-157
lines changed

src/extension/agents/copilotcli/node/copilotcliAgentManager.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,22 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import type { ModelProvider } from '@github/copilot/sdk';
7+
import * as l10n from '@vscode/l10n';
78
import type * as vscode from 'vscode';
89
import { ILogService } from '../../../../platform/log/common/logService';
910
import { Disposable } from '../../../../util/vs/base/common/lifecycle';
10-
import { IInstantiationService } from '../../../../util/vs/platform/instantiation/common/instantiation';
1111
import { CopilotCLIPromptResolver } from './copilotcliPromptResolver';
12-
import { CopilotCLISession } from './copilotcliSession';
1312
import { ICopilotCLISessionService } from './copilotcliSessionService';
1413

1514
export class CopilotCLIAgentManager extends Disposable {
1615
constructor(
1716
private readonly promptResolver: CopilotCLIPromptResolver,
1817
@ILogService private readonly logService: ILogService,
19-
@IInstantiationService private readonly instantiationService: IInstantiationService,
2018
@ICopilotCLISessionService private readonly sessionService: ICopilotCLISessionService,
2119
) {
2220
super();
2321
}
2422

25-
/**
26-
* Find session by SDK session ID
27-
*/
28-
public findSession(sessionId: string): CopilotCLISession | undefined {
29-
return this.sessionService.findSessionWrapper<CopilotCLISession>(sessionId);
30-
}
31-
3223
async handleRequest(
3324
copilotcliSessionId: string | undefined,
3425
request: vscode.ChatRequest,
@@ -43,14 +34,15 @@ export class CopilotCLIAgentManager extends Disposable {
4334

4435
const { prompt, attachments } = await this.promptResolver.resolvePrompt(request, token);
4536
// Check if we already have a session wrapper
46-
let session = copilotcliSessionId ? this.sessionService.findSessionWrapper<CopilotCLISession>(copilotcliSessionId) : undefined;
37+
let session = copilotcliSessionId ? await this.sessionService.getSession(copilotcliSessionId, modelId, false, token) : undefined;
4738

4839
if (session) {
4940
this.logService.trace(`[CopilotCLIAgentManager] Reusing CopilotCLI session ${copilotcliSessionId}.`);
41+
} else if (copilotcliSessionId) {
42+
stream.warning(l10n.t('Chat session not found.'));
43+
return { copilotcliSessionId: undefined };
5044
} else {
51-
const sdkSession = await this.sessionService.getOrCreateSDKSession(copilotcliSessionId, prompt);
52-
session = this.instantiationService.createInstance(CopilotCLISession, sdkSession);
53-
this.sessionService.trackSessionWrapper(sdkSession.sessionId, session);
45+
session = await this.sessionService.createSession(prompt, modelId, token);
5446
}
5547

5648
await session.invoke(prompt, attachments, request.toolInvocationToken, stream, modelId, workingDirectory, token);

src/extension/agents/copilotcli/node/copilotcliSession.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import { ILogService } from '../../../../platform/log/common/logService';
1010
import { IWorkspaceService } from '../../../../platform/workspace/common/workspaceService';
1111
import { CancellationToken } from '../../../../util/vs/base/common/cancellation';
1212
import { DisposableStore } from '../../../../util/vs/base/common/lifecycle';
13-
import { ChatResponseThinkingProgressPart, ChatSessionStatus, EventEmitter, LanguageModelTextPart } from '../../../../vscodeTypes';
13+
import { ChatRequestTurn2, ChatResponseThinkingProgressPart, ChatResponseTurn2, ChatSessionStatus, EventEmitter, LanguageModelTextPart } from '../../../../vscodeTypes';
1414
import { IToolsService } from '../../../tools/common/toolsService';
1515
import { ExternalEditTracker } from '../../common/externalEditTracker';
1616
import { getAffectedUrisForEditTool } from '../common/copilotcliTools';
1717
import { ICopilotCLISDK } from './copilotCli';
18-
import { processToolExecutionComplete, processToolExecutionStart } from './copilotcliToolInvocationFormatter';
18+
import { buildChatHistoryFromEvents, processToolExecutionComplete, processToolExecutionStart } from './copilotcliToolInvocationFormatter';
1919
import { getCopilotLogger } from './logger';
2020
import { getConfirmationToolParams, PermissionRequest } from './permissionHelpers';
2121

@@ -32,6 +32,14 @@ export class CopilotCLISession extends DisposableStore {
3232

3333
public readonly onDidChangeStatus = this._statusChange.event;
3434

35+
private _aborted?: boolean;
36+
public get aborted(): boolean {
37+
return this._aborted ?? false;
38+
}
39+
private readonly _onDidAbort = this.add(new EventEmitter<void>());
40+
41+
public readonly onDidAbort = this._onDidAbort.event;
42+
3543
constructor(
3644
private readonly _sdkSession: Session,
3745
@ILogService private readonly logService: ILogService,
@@ -128,6 +136,28 @@ export class CopilotCLISession extends DisposableStore {
128136
}
129137
}
130138

139+
addUserMessage(content: string) {
140+
this._sdkSession.addEvent({ type: 'user.message', data: { content } });
141+
}
142+
143+
addUserAssistantMessage(content: string) {
144+
this._sdkSession.addEvent({
145+
type: 'assistant.message', data: {
146+
messageId: `msg_${Date.now()}`,
147+
content
148+
}
149+
});
150+
}
151+
152+
public getSelectedModelId() {
153+
return this._sdkSession.getSelectedModel();
154+
}
155+
156+
public async getChatHistory(): Promise<(ChatRequestTurn2 | ChatResponseTurn2)[]> {
157+
const events = await this._sdkSession.getEvents();
158+
return buildChatHistoryFromEvents(events);
159+
}
160+
131161
private _toolNames = new Map<string, string>();
132162
private _processEvent(
133163
event: SessionEvent,

0 commit comments

Comments
 (0)