Path: blob/main/extensions/copilot/src/platform/networking/common/responseConvert.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 { assertNever } from '../../../util/vs/base/common/assert';6import { IResponseDelta, ResponsePart, ResponsePartKind } from './fetch';78/**9* Converts a ResponsePart to an IResponseDelta.10* For non-content parts, the text is set to an empty string.11* @param part The ResponsePart to convert12*/13export const toResponseDelta = (part: ResponsePart): IResponseDelta => {14switch (part.kind) {15case ResponsePartKind.ContentDelta:16return { text: part.delta };17case ResponsePartKind.Content:18return { text: part.content, logprobs: part.logProbs };19case ResponsePartKind.Annotation:20return {21text: '',22codeVulnAnnotations: part.codeVulnAnnotations,23ipCitations: part.ipCitations,24copilotReferences: part.copilotReferences25};26case ResponsePartKind.Confirmation:27return {28text: '',29copilotConfirmation: part,30};31case ResponsePartKind.Error:32return {33text: '',34copilotErrors: [part.error]35};36case ResponsePartKind.ToolCallDelta:37return {38text: '',39copilotToolCalls: [{40name: part.name,41arguments: part.delta,42id: part.partId43}]44};45case ResponsePartKind.ToolCall:46return {47text: '',48copilotToolCalls: [{49name: part.name,50arguments: part.arguments,51id: part.id52}]53};54case ResponsePartKind.ThinkingDelta:55return { text: '' };56case ResponsePartKind.Thinking:57return { text: '' }; // todo@karthiknadig/@connor4312: do we still need this back-compat with responses API?58default:59assertNever(part);60}61};6263const staticContentUUID = '8444605d-6c67-42c5-bbcb-a04b83f9f76e';646566/**67* Converts an IResponseDelta to a ResponsePart.68* For non-content deltas, the text is ignored.69* @param delta The IResponseDelta to convert70*/71export function* fromResponseDelta(delta: IResponseDelta): Iterable<ResponsePart> {72if (delta.text && delta.text.length > 0) {73yield {74kind: ResponsePartKind.ContentDelta,75partId: staticContentUUID,76delta: delta.text77};78}79if (delta.codeVulnAnnotations?.length || delta.ipCitations?.length || delta.copilotReferences?.length) {80yield {81kind: ResponsePartKind.Annotation,82codeVulnAnnotations: delta.codeVulnAnnotations,83ipCitations: delta.ipCitations,84copilotReferences: delta.copilotReferences85};86}87if (delta.copilotErrors && delta.copilotErrors.length > 0) {88yield {89kind: ResponsePartKind.Error,90error: delta.copilotErrors[0]91};92}93if (delta.copilotToolCalls && delta.copilotToolCalls.length > 0) {94for (const toolCall of delta.copilotToolCalls) {95yield {96kind: ResponsePartKind.ToolCall,97partId: toolCall.id,98name: toolCall.name,99arguments: toolCall.arguments,100id: toolCall.id101};102}103}104if (delta.thinking) {105yield {106kind: ResponsePartKind.ThinkingDelta,107partId: '', // Unknown, must be set by caller if needed108delta: delta.thinking109};110}111if (delta.copilotConfirmation) {112yield {113kind: ResponsePartKind.Confirmation,114title: delta.copilotConfirmation.title,115message: delta.copilotConfirmation.message,116confirmation: delta.copilotConfirmation.confirmation117};118}119}120121122