-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtoolCallRound.ts
More file actions
115 lines (103 loc) · 3.63 KB
/
toolCallRound.ts
File metadata and controls
115 lines (103 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { FetchSuccess } from '../../../platform/chat/common/commonTypes';
import { APIUsage } from '../../../platform/networking/common/openai';
import { isEncryptedThinkingDelta, ThinkingData, ThinkingDelta } from '../../../platform/thinking/common/thinking';
import { generateUuid } from '../../../util/vs/base/common/uuid';
import { IToolCall, IToolCallRound } from './intents';
/**
* Represents a round of tool calling from the AI assistant.
* Each round contains the assistant's response text, any tool calls it made,
* and retry information if there were input validation issues.
*/
export class ToolCallRound implements IToolCallRound {
public summary: string | undefined;
/**
* Creates a ToolCallRound from an existing IToolCallRound object.
* Prefer this over using a constructor overload to keep construction explicit.
*/
public static create(params: Omit<IToolCallRound, 'id'> & { id?: string }): ToolCallRound {
const round = new ToolCallRound(
params.response,
params.toolCalls,
params.toolInputRetry,
params.id,
params.statefulMarker,
params.thinking,
params.usage
);
round.summary = params.summary;
return round;
}
/**
* @param response The text response from the assistant
* @param toolCalls The tool calls made by the assistant
* @param toolInputRetry The number of times this round has been retried due to tool input validation failures
* @param id A stable identifier for this round
* @param statefulMarker Optional stateful marker used with the responses API
* @param thinking Optional thinking data from the model
* @param usage Optional API usage data for this round
*/
constructor(
public readonly response: string,
public readonly toolCalls: IToolCall[] = [],
public readonly toolInputRetry: number = 0,
public readonly id: string = ToolCallRound.generateID(),
public readonly statefulMarker?: string,
public readonly thinking?: ThinkingData,
public readonly usage?: APIUsage
) { }
private static generateID(): string {
return generateUuid();
}
}
export class ThinkingDataItem implements ThinkingData {
public text: string | string[] = '';
public metadata?: { [key: string]: any };
public tokens?: number;
public encrypted?: string;
static createOrUpdate(item: ThinkingDataItem | undefined, delta: ThinkingDelta) {
if (!item) {
item = new ThinkingDataItem(delta.id ?? generateUuid());
}
item.update(delta);
return item;
}
constructor(
public id: string
) { }
public update(delta: ThinkingDelta): void {
if (delta.id && this.id !== delta.id) {
this.id = delta.id;
}
if (isEncryptedThinkingDelta(delta)) {
this.encrypted = delta.encrypted;
}
if (delta.text !== undefined) {
// handles all possible text states
if (Array.isArray(delta.text)) {
if (Array.isArray(this.text)) {
this.text.push(...delta.text);
} else if (this.text) {
this.text = [this.text, ...delta.text];
} else {
this.text = [...delta.text];
}
} else {
if (Array.isArray(this.text)) {
this.text.push(delta.text);
} else {
this.text += delta.text;
}
}
}
if (delta.metadata) {
this.metadata = delta.metadata;
}
}
public updateWithFetchResult(fetchResult: FetchSuccess<unknown>): void {
this.tokens = fetchResult.usage?.completion_tokens_details?.reasoning_tokens;
}
}