Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/conversation/vscode-node/test/languageModelAccess.test.ts
13405 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 assert from 'assert';
7
import * as vscode from 'vscode';
8
import { IChatMLFetcher } from '../../../../platform/chat/common/chatMLFetcher';
9
import { MockChatMLFetcher } from '../../../../platform/chat/test/common/mockChatMLFetcher';
10
import { IEndpointProvider } from '../../../../platform/endpoint/common/endpointProvider';
11
import { IVSCodeExtensionContext } from '../../../../platform/extContext/common/extensionContext';
12
import { IChatEndpoint } from '../../../../platform/networking/common/networking';
13
import { ITestingServicesAccessor } from '../../../../platform/test/node/services';
14
import { CancellationToken } from '../../../../util/vs/base/common/cancellation';
15
import { IInstantiationService } from '../../../../util/vs/platform/instantiation/common/instantiation';
16
import { createExtensionTestingServices } from '../../../test/vscode-node/services';
17
import { CopilotLanguageModelWrapper } from '../languageModelAccess';
18
19
20
suite('CopilotLanguageModelWrapper', () => {
21
let accessor: ITestingServicesAccessor;
22
let instaService: IInstantiationService;
23
24
function createAccessor(vscodeExtensionContext?: IVSCodeExtensionContext) {
25
const testingServiceCollection = createExtensionTestingServices();
26
testingServiceCollection.define(IChatMLFetcher, new MockChatMLFetcher());
27
28
accessor = testingServiceCollection.createTestingAccessor();
29
instaService = accessor.get(IInstantiationService);
30
}
31
32
suite('validateRequest - invalid', () => {
33
let wrapper: CopilotLanguageModelWrapper;
34
let endpoint: IChatEndpoint;
35
setup(async () => {
36
createAccessor();
37
endpoint = await accessor.get(IEndpointProvider).getChatEndpoint('copilot-base');
38
wrapper = instaService.createInstance(CopilotLanguageModelWrapper);
39
});
40
41
const runTest = async (messages: vscode.LanguageModelChatMessage[], tools?: vscode.LanguageModelChatTool[], errMsg?: string) => {
42
await assert.rejects(
43
() => wrapper.provideLanguageModelResponse(endpoint, messages, { tools, requestInitiator: 'unknown', toolMode: vscode.LanguageModelChatToolMode.Auto }, vscode.extensions.all[0].id, { report: () => { } }, CancellationToken.None),
44
err => {
45
errMsg ??= 'Invalid request';
46
assert.ok(err instanceof Error, 'expected an Error');
47
assert.ok(err.message.includes(errMsg), `expected error to include "${errMsg}", got ${err.message}`);
48
return true;
49
}
50
);
51
};
52
53
test('empty', async () => {
54
await runTest([]);
55
});
56
57
test('bad tool name', async () => {
58
await runTest([vscode.LanguageModelChatMessage.User('hello')], [{ name: 'hello world', description: 'my tool' }], 'Invalid tool name');
59
});
60
});
61
62
suite('validateRequest - valid', () => {
63
let wrapper: CopilotLanguageModelWrapper;
64
let endpoint: IChatEndpoint;
65
setup(async () => {
66
createAccessor();
67
endpoint = await accessor.get(IEndpointProvider).getChatEndpoint('copilot-base');
68
wrapper = instaService.createInstance(CopilotLanguageModelWrapper);
69
});
70
const runTest = async (messages: vscode.LanguageModelChatMessage[], tools?: vscode.LanguageModelChatTool[]) => {
71
await wrapper.provideLanguageModelResponse(endpoint, messages, { tools, requestInitiator: 'unknown', toolMode: vscode.LanguageModelChatToolMode.Auto }, vscode.extensions.all[0].id, { report: () => { } }, CancellationToken.None);
72
};
73
74
test('simple', async () => {
75
await runTest([vscode.LanguageModelChatMessage.User('hello')]);
76
});
77
78
test('tool call and user message', async () => {
79
const toolCall = vscode.LanguageModelChatMessage.Assistant('');
80
toolCall.content = [new vscode.LanguageModelToolCallPart('id', 'func', { param: 123 })];
81
const toolResult = vscode.LanguageModelChatMessage.User('');
82
toolResult.content = [new vscode.LanguageModelToolResultPart('id', [new vscode.LanguageModelTextPart('result')])];
83
await runTest([toolCall, toolResult, vscode.LanguageModelChatMessage.User('user message')]);
84
});
85
86
test('good tool name', async () => {
87
await runTest([vscode.LanguageModelChatMessage.User('hello2')], [{ name: 'hello_world', description: 'my tool' }]);
88
});
89
});
90
});
91
92