Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/test/node/testHelpers.ts
13399 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 { ChatContext, ChatPromptReference, ChatRequest, ChatRequestTurn, ChatResponseTurn, ExtendedChatResponsePart, Uri } from 'vscode';
7
import { ChatResponseStreamImpl } from '../../../util/common/chatResponseStreamImpl';
8
import { MarkdownString } from '../../../util/vs/base/common/htmlContent';
9
import { URI } from '../../../util/vs/base/common/uri';
10
import { generateUuid } from '../../../util/vs/base/common/uuid';
11
import * as vscodeTypes from '../../../vscodeTypes';
12
13
export class TestChatRequest implements ChatRequest {
14
public command: string | undefined;
15
public references: readonly ChatPromptReference[];
16
public location: vscodeTypes.ChatLocation;
17
public location2 = undefined;
18
public attempt: number;
19
public enableCommandDetection: boolean;
20
public isParticipantDetected: boolean;
21
public toolReferences = [];
22
public toolInvocationToken: never = undefined as never;
23
public model = null!;
24
public tools = new Map();
25
public id = generateUuid();
26
public sessionId = generateUuid();
27
public sessionResource = vscodeTypes.Uri.parse(`test://session/${this.sessionId}`);
28
public hasHooksEnabled = false;
29
30
constructor(
31
public prompt: string,
32
references?: ChatPromptReference[]
33
) {
34
this.references = references ?? [];
35
this.location = vscodeTypes.ChatLocation.Panel;
36
this.attempt = 0;
37
this.enableCommandDetection = false;
38
this.isParticipantDetected = false;
39
}
40
}
41
42
export class TestChatContext implements ChatContext {
43
readonly history: ReadonlyArray<ChatRequestTurn | ChatResponseTurn>;
44
readonly yieldRequested: boolean;
45
46
constructor(history: ReadonlyArray<ChatRequestTurn | ChatResponseTurn> = [], yieldRequested = false) {
47
this.history = history;
48
this.yieldRequested = yieldRequested;
49
}
50
}
51
52
export class MockChatResponseStream extends ChatResponseStreamImpl {
53
54
public output: string[] = [];
55
public uris: string[] = [];
56
public externalEditUris: Uri[] = [];
57
constructor(push: ((part: ExtendedChatResponsePart) => void) = () => { }) {
58
super(push, () => { }, undefined, undefined, undefined, () => Promise.resolve(undefined));
59
}
60
override markdown(content: string | MarkdownString): void {
61
this.output.push(typeof content === 'string' ? content : content.value);
62
}
63
override warning(content: string | MarkdownString): void {
64
super.warning(content);
65
this.output.push(typeof content === 'string' ? content : content.value);
66
}
67
override codeblockUri(uri: URI): void {
68
this.uris.push(uri.toString());
69
}
70
71
override async externalEdit(target: Uri | Uri[], callback: () => Thenable<void>): Promise<string> {
72
if (Array.isArray(target)) {
73
this.externalEditUris.push(...target);
74
} else {
75
this.externalEditUris.push(target);
76
}
77
await callback();
78
return '';
79
}
80
}
81
82