Path: blob/main/extensions/copilot/src/platform/chat/common/globalStringUtils.ts
13401 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { OpenAI, Raw } from '@vscode/prompt-tsx';6import { assertNever } from '../../../util/vs/base/common/assert';78/**9* Gets the text content part out of the message.10* In the event it is an `ChatCompletionContentPart`, it will extract out the `ChatCompletionContentPartText`.11**/12export function getTextPart(message: string | Raw.ChatCompletionContentPart[] | Raw.ChatCompletionContentPart | OpenAI.ChatCompletionContentPart[] | OpenAI.ChatCompletionContentPart): string {13if (!message) {14return '';15}1617if (typeof message === 'string') {18return message;19}2021if (!Array.isArray(message)) {22return message.type === Raw.ChatCompletionContentPartKind.Text ? message.text : '';23}2425return message.map(c => (c.type === Raw.ChatCompletionContentPartKind.Text || c.type === 'text') ? c.text : '').join('');26}272829export function toTextPart(message: string): Raw.ChatCompletionContentPartText {30return {31type: Raw.ChatCompletionContentPartKind.Text,32text: message33};34}3536export function toTextParts(message: string): Raw.ChatCompletionContentPartText[] {37return [toTextPart(message)];38}3940export function roleToString(role: Raw.ChatRole): 'system' | 'user' | 'assistant' | 'tool' {41switch (role) {42case Raw.ChatRole.System:43return 'system';44case Raw.ChatRole.User:45return 'user';46case Raw.ChatRole.Assistant:47return 'assistant';48case Raw.ChatRole.Tool:49return 'tool';50default:51assertNever(role, `unknown role (${role})`);52}53}545556