Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/chat/common/globalStringUtils.ts
13401 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 { OpenAI, Raw } from '@vscode/prompt-tsx';
7
import { assertNever } from '../../../util/vs/base/common/assert';
8
9
/**
10
* Gets the text content part out of the message.
11
* In the event it is an `ChatCompletionContentPart`, it will extract out the `ChatCompletionContentPartText`.
12
**/
13
export function getTextPart(message: string | Raw.ChatCompletionContentPart[] | Raw.ChatCompletionContentPart | OpenAI.ChatCompletionContentPart[] | OpenAI.ChatCompletionContentPart): string {
14
if (!message) {
15
return '';
16
}
17
18
if (typeof message === 'string') {
19
return message;
20
}
21
22
if (!Array.isArray(message)) {
23
return message.type === Raw.ChatCompletionContentPartKind.Text ? message.text : '';
24
}
25
26
return message.map(c => (c.type === Raw.ChatCompletionContentPartKind.Text || c.type === 'text') ? c.text : '').join('');
27
}
28
29
30
export function toTextPart(message: string): Raw.ChatCompletionContentPartText {
31
return {
32
type: Raw.ChatCompletionContentPartKind.Text,
33
text: message
34
};
35
}
36
37
export function toTextParts(message: string): Raw.ChatCompletionContentPartText[] {
38
return [toTextPart(message)];
39
}
40
41
export function roleToString(role: Raw.ChatRole): 'system' | 'user' | 'assistant' | 'tool' {
42
switch (role) {
43
case Raw.ChatRole.System:
44
return 'system';
45
case Raw.ChatRole.User:
46
return 'user';
47
case Raw.ChatRole.Assistant:
48
return 'assistant';
49
case Raw.ChatRole.Tool:
50
return 'tool';
51
default:
52
assertNever(role, `unknown role (${role})`);
53
}
54
}
55
56