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