Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/copilotcli/node/test/testHelpers.ts
13406 views
1
/*---------------------------------------------------------------------------------------------
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 type { SessionOptions, SweCustomAgent } from '@github/copilot/sdk';
7
import type { CancellationToken, Uri } from 'vscode';
8
import { Event } from '../../../../../util/vs/base/common/event';
9
import { Disposable, IDisposable } from '../../../../../util/vs/base/common/lifecycle';
10
import { URI } from '../../../../../util/vs/base/common/uri';
11
import { generateUuid } from '../../../../../util/vs/base/common/uuid';
12
import { CLIAgentInfo, CopilotCLIModelInfo, ICopilotCLIAgents, ICopilotCLIModels } from '../copilotCli';
13
import { ICopilotCLIImageSupport } from '../copilotCLIImageSupport';
14
import { ICopilotCLISkills } from '../copilotCLISkills';
15
import { ICopilotCLIMCPHandler } from '../mcpHandler';
16
17
export class MockCliSdkSession {
18
public emittedEvents: { event: string; content: string | undefined }[] = [];
19
public aborted = false;
20
public messages: {}[] = [];
21
public events: {}[] = [];
22
public title: string | undefined;
23
public name: string | undefined;
24
public readonly renameSession = async (name: string): Promise<void> => {
25
this.title = name;
26
this.name = name;
27
this.summary = name;
28
};
29
public readonly updateSessionSummary = async (summary: string): Promise<void> => {
30
if (!this.name) {
31
this.title = summary;
32
}
33
this.summary = summary;
34
};
35
public summary?: string;
36
constructor(public readonly sessionId: string, public readonly startTime: Date) { }
37
getChatContextMessages(): Promise<{}[]> { return Promise.resolve(this.messages); }
38
getEvents(): {}[] { return this.events; }
39
getSelectedModel(): Promise<string | undefined> { return Promise.resolve(undefined); }
40
isAbortable(): boolean { return !this.aborted; }
41
abort(): Promise<void> {
42
this.aborted = true;
43
return Promise.resolve();
44
}
45
emit(event: string, args: { content: string | undefined }): void {
46
this.emittedEvents.push({ event, content: args.content });
47
}
48
clearCustomAgent() {
49
return;
50
}
51
setPermissionsRequired(_required: boolean): void {
52
// no-op in tests
53
}
54
}
55
56
export class MockSkillLocations implements ICopilotCLISkills {
57
declare _serviceBrand: undefined;
58
private readonly locations: Uri[];
59
constructor(locations: Uri[] = []) {
60
this.locations = locations;
61
}
62
async getSkillsLocations(_token: CancellationToken): Promise<Uri[]> {
63
return this.locations;
64
}
65
}
66
67
export class MockCliSdkSessionManager {
68
public sessions = new Map<string, MockCliSdkSession>();
69
constructor(public readonly opts: {}) { }
70
createSession(_options: SessionOptions & { sessionId?: string }) {
71
const id = _options.sessionId ?? `sess_${generateUuid()}`;
72
const s = new MockCliSdkSession(id, new Date());
73
this.sessions.set(id, s);
74
return Promise.resolve(s);
75
}
76
getSession(opts: SessionOptions & { sessionId: string }, _writable: boolean) {
77
if (opts && opts.sessionId && this.sessions.has(opts.sessionId)) {
78
return Promise.resolve(this.sessions.get(opts.sessionId));
79
}
80
return Promise.resolve(undefined);
81
}
82
listSessions() {
83
return Promise.resolve(Array.from(this.sessions.values()).map(s => ({ sessionId: s.sessionId, startTime: s.startTime, modifiedTime: s.startTime, summary: s.summary, name: s.name })));
84
}
85
getSessionMetadata({ sessionId }: { sessionId: string }) {
86
const session = this.sessions.get(sessionId);
87
return Promise.resolve(session ? { sessionId: session.sessionId, startTime: session.startTime, modifiedTime: session.startTime, summary: session.summary, name: session.name, isRemote: false } : undefined);
88
}
89
deleteSession(id: string) { this.sessions.delete(id); return Promise.resolve(); }
90
closeSession(_id: string) { return Promise.resolve(); }
91
forkSession(sourceId: string, _toEventId?: string): Promise<{ sessionId: string }> {
92
const newId = `${sourceId}-fork-${generateUuid()}`;
93
const source = this.sessions.get(sourceId);
94
const s = new MockCliSdkSession(newId, source?.startTime ?? new Date());
95
this.sessions.set(newId, s);
96
return Promise.resolve({ sessionId: newId });
97
}
98
async loadDeferredRepoHooks(session: unknown) {
99
//
100
}
101
}
102
103
export class NullCopilotCLIAgents implements ICopilotCLIAgents {
104
_serviceBrand: undefined;
105
readonly onDidChangeAgents: Event<void> = Event.None;
106
async getAgents(): Promise<readonly CLIAgentInfo[]> {
107
return [];
108
}
109
async getSessionAgent(_sessionId: string): Promise<string | undefined> {
110
return undefined;
111
}
112
resolveAgent(_agentId: string): Promise<SweCustomAgent | undefined> {
113
return Promise.resolve(undefined);
114
}
115
}
116
117
export class NullICopilotCLIImageSupport implements ICopilotCLIImageSupport {
118
_serviceBrand: undefined;
119
storeImage(_imageData: Uint8Array, _mimeType: string): Promise<URI> {
120
return Promise.resolve(URI.file('/dev/null'));
121
}
122
isTrustedImage(_imageUri: URI): boolean {
123
return false;
124
}
125
}
126
127
export class NullCopilotCLIMCPHandler implements ICopilotCLIMCPHandler {
128
_serviceBrand: undefined;
129
async loadMcpConfig(_resource: URI): Promise<{ mcpConfig: Record<string, NonNullable<SessionOptions['mcpServers']>[string]> | undefined; disposable: IDisposable }> {
130
return { mcpConfig: undefined, disposable: Disposable.None };
131
}
132
}
133
134
export class NullCopilotCLIModels implements ICopilotCLIModels {
135
_serviceBrand: undefined;
136
async resolveModel(_modelId: string): Promise<string | undefined> { return undefined; }
137
async getDefaultModel(): Promise<string | undefined> { return undefined; }
138
async setDefaultModel(_modelId: string | undefined): Promise<void> { return; }
139
async getModels(): Promise<CopilotCLIModelInfo[]> { return []; }
140
registerLanguageModelChatProvider(): void { return; }
141
}
142
143