Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompt/common/toolCallRound.ts
13399 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
import { FetchSuccess } from '../../../platform/chat/common/commonTypes';
6
import { OpenAIContextManagementResponse } from '../../../platform/networking/common/openai';
7
import { isEncryptedThinkingDelta, ThinkingData, ThinkingDelta } from '../../../platform/thinking/common/thinking';
8
import { generateUuid } from '../../../util/vs/base/common/uuid';
9
import { IToolCall, IToolCallRound } from './intents';
10
11
12
/**
13
* Represents a round of tool calling from the AI assistant.
14
* Each round contains the assistant's response text, any tool calls it made,
15
* and retry information if there were input validation issues.
16
*/
17
export class ToolCallRound implements IToolCallRound {
18
public summary: string | undefined;
19
public phase?: string;
20
public phaseModelId?: string;
21
22
/**
23
* Creates a ToolCallRound from an existing IToolCallRound object.
24
* Prefer this over using a constructor overload to keep construction explicit.
25
*/
26
public static create(params: Omit<IToolCallRound, 'id'> & { id?: string }): ToolCallRound {
27
const round = new ToolCallRound(
28
params.response,
29
params.toolCalls,
30
params.toolInputRetry,
31
params.id,
32
params.statefulMarker,
33
params.thinking,
34
params.timestamp,
35
params.compaction,
36
);
37
round.summary = params.summary;
38
round.phase = params.phase;
39
round.phaseModelId = params.phaseModelId;
40
return round;
41
}
42
43
/**
44
* @param response The text response from the assistant
45
* @param toolCalls The tool calls made by the assistant
46
* @param toolInputRetry The number of times this round has been retried due to tool input validation failures
47
* @param id A stable identifier for this round
48
* @param statefulMarker Optional stateful marker used with the responses API
49
* @param thinking Optional thinking/reasoning data
50
* @param timestamp Epoch millis when this round started (defaults to `Date.now()`)
51
*/
52
constructor(
53
public readonly response: string,
54
public readonly toolCalls: IToolCall[] = [],
55
public readonly toolInputRetry: number = 0,
56
public readonly id: string = ToolCallRound.generateID(),
57
public readonly statefulMarker?: string,
58
public readonly thinking?: ThinkingData,
59
public readonly timestamp: number = Date.now(),
60
public readonly compaction?: OpenAIContextManagementResponse,
61
) { }
62
63
private static generateID(): string {
64
return generateUuid();
65
}
66
}
67
68
export class ThinkingDataItem implements ThinkingData {
69
public text: string | string[] = '';
70
public metadata?: { [key: string]: any };
71
public tokens?: number;
72
public encrypted?: string;
73
74
static createOrUpdate(item: ThinkingDataItem | undefined, delta: ThinkingDelta) {
75
if (!item) {
76
item = new ThinkingDataItem(delta.id ?? generateUuid());
77
}
78
79
item.update(delta);
80
return item;
81
}
82
83
constructor(
84
public id: string
85
) { }
86
87
public update(delta: ThinkingDelta): void {
88
if (delta.id && this.id !== delta.id) {
89
this.id = delta.id;
90
}
91
if (isEncryptedThinkingDelta(delta)) {
92
this.encrypted = delta.encrypted;
93
}
94
if (delta.text !== undefined) {
95
96
// handles all possible text states
97
if (Array.isArray(delta.text)) {
98
if (Array.isArray(this.text)) {
99
this.text.push(...delta.text);
100
} else if (this.text) {
101
this.text = [this.text, ...delta.text];
102
} else {
103
this.text = [...delta.text];
104
}
105
} else {
106
if (Array.isArray(this.text)) {
107
this.text.push(delta.text);
108
} else {
109
this.text += delta.text;
110
}
111
}
112
}
113
if (delta.metadata) {
114
this.metadata = delta.metadata;
115
}
116
}
117
118
public updateWithFetchResult(fetchResult: FetchSuccess<unknown>): void {
119
this.tokens = fetchResult.usage?.completion_tokens_details?.reasoning_tokens;
120
}
121
}
122