Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/inlineEdits/common/utils/stringifyChatMessages.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 { Raw } from '@vscode/prompt-tsx';
7
8
export function stringifyChatMessages(messages: Raw.ChatMessage[]) {
9
return messages.map(stringifyMessage).join('\n');
10
}
11
12
function stringifyMessage({ role, content }: Raw.ChatMessage) {
13
if (role !== Raw.ChatRole.User && role !== Raw.ChatRole.System) {
14
return 'omitted because of non-user and non-system role'; // should be impossible
15
}
16
17
const roleStr = role === Raw.ChatRole.User ? 'User' : 'System';
18
19
const textContentPart = content.at(0);
20
if (textContentPart?.type !== Raw.ChatCompletionContentPartKind.Text) {
21
return 'omitted because of non-text content'; // should be impossible
22
}
23
24
return (
25
`${roleStr}
26
------
27
${textContentPart.text}
28
==================`);
29
}
30
31