agent-accelerator 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1220 -0
- package/SYSTEM_PROMPT.md +14 -0
- package/SYSTEM_PROMPT_AGENT.md +34 -0
- package/SYSTEM_PROMPT_TOOLS.md +9 -0
- package/bunfig.toml +2 -0
- package/package.json +59 -0
- package/src/agent/agent.ts +615 -0
- package/src/agent/context.ts +161 -0
- package/src/agent/delegation.ts +481 -0
- package/src/agent/loop.ts +569 -0
- package/src/agent/subagent.ts +83 -0
- package/src/ai-sdk/converters.ts +342 -0
- package/src/ai-sdk/errors.ts +122 -0
- package/src/ai-sdk/executor.ts +454 -0
- package/src/ai-sdk/index.ts +55 -0
- package/src/ai-sdk/model-provider.ts +303 -0
- package/src/ai-sdk/options.ts +306 -0
- package/src/ai-sdk/provider.ts +415 -0
- package/src/ai-sdk/registry.ts +416 -0
- package/src/data/README.md +84 -0
- package/src/index.ts +190 -0
- package/src/models/catalog-cache.ts +273 -0
- package/src/models/catalog.ts +503 -0
- package/src/streaming/event-stream.ts +211 -0
- package/src/streaming/sse-parser.ts +97 -0
- package/src/tokens/counter.ts +136 -0
- package/src/tools/executor.ts +365 -0
- package/src/tools/schema.ts +221 -0
- package/src/tools/tool.ts +101 -0
- package/src/types/agent.ts +87 -0
- package/src/types/core.ts +86 -0
- package/src/types/message.ts +115 -0
- package/src/types/model.ts +212 -0
- package/src/types/provider-payloads.ts +434 -0
- package/src/types/response.ts +158 -0
- package/src/types/tool.ts +61 -0
- package/src/utils/base64.ts +27 -0
- package/src/utils/cache.ts +146 -0
- package/src/utils/env.ts +78 -0
- package/src/utils/headers.ts +110 -0
- package/src/utils/media.ts +137 -0
- package/src/utils/serialization.ts +91 -0
- package/src/utils/session.ts +26 -0
- package/src/utils/thought-signature.ts +27 -0
- package/tsconfig.json +31 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider wire payload and schema types for complete backwards compatibility.
|
|
3
|
+
* All providers use Vercel AI SDK under the hood, but these types allow existing
|
|
4
|
+
* applications and tests to type-check requests and responses seamlessly.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// ============================================================================
|
|
8
|
+
// Google AI Studio Payload Types
|
|
9
|
+
// ============================================================================
|
|
10
|
+
|
|
11
|
+
export type GoogleThinkingLevel = "OFF" | "MINIMAL" | "LOW" | "MEDIUM" | "HIGH";
|
|
12
|
+
|
|
13
|
+
export interface GoogleThinkingConfig {
|
|
14
|
+
thinkingLevel?: GoogleThinkingLevel;
|
|
15
|
+
includeThoughts?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type GoogleFunctionCallingMode = "AUTO" | "NONE" | "ANY";
|
|
19
|
+
|
|
20
|
+
export interface GoogleFunctionCallingConfig {
|
|
21
|
+
mode: GoogleFunctionCallingMode;
|
|
22
|
+
allowedFunctionNames?: string[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface GoogleToolConfig {
|
|
26
|
+
functionCallingConfig?: GoogleFunctionCallingConfig;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface GoogleFunctionDeclaration {
|
|
30
|
+
name: string;
|
|
31
|
+
description?: string;
|
|
32
|
+
parameters?: Record<string, unknown>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface GoogleTool {
|
|
36
|
+
functionDeclarations?: GoogleFunctionDeclaration[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface GoogleBlob {
|
|
40
|
+
mimeType: string;
|
|
41
|
+
data: string; // base64
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface GooglePart {
|
|
45
|
+
text?: string;
|
|
46
|
+
inlineData?: GoogleBlob;
|
|
47
|
+
functionCall?: {
|
|
48
|
+
name: string;
|
|
49
|
+
args: Record<string, unknown>;
|
|
50
|
+
id?: string;
|
|
51
|
+
thoughtSignature?: string;
|
|
52
|
+
};
|
|
53
|
+
functionResponse?: {
|
|
54
|
+
name: string;
|
|
55
|
+
response: Record<string, unknown>;
|
|
56
|
+
id?: string;
|
|
57
|
+
};
|
|
58
|
+
thought?: boolean;
|
|
59
|
+
thoughtSignature?: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface GoogleContent {
|
|
63
|
+
role: "user" | "model" | "function";
|
|
64
|
+
parts: GooglePart[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface GoogleGenerationConfig {
|
|
68
|
+
temperature?: number;
|
|
69
|
+
topP?: number;
|
|
70
|
+
topK?: number;
|
|
71
|
+
candidateCount?: number;
|
|
72
|
+
maxOutputTokens?: number;
|
|
73
|
+
stopSequences?: string[];
|
|
74
|
+
responseMimeType?: string;
|
|
75
|
+
thinkingConfig?: GoogleThinkingConfig;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface GoogleCandidate {
|
|
79
|
+
content?: GoogleContent;
|
|
80
|
+
finishReason?: string;
|
|
81
|
+
index?: number;
|
|
82
|
+
safetyRatings?: Array<Record<string, unknown>>;
|
|
83
|
+
groundingMetadata?: Record<string, unknown>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface GoogleUsageMetadata {
|
|
87
|
+
promptTokenCount?: number;
|
|
88
|
+
candidatesTokenCount?: number;
|
|
89
|
+
totalTokenCount?: number;
|
|
90
|
+
cachedContentTokenCount?: number;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface GoogleGenerateContentRequest {
|
|
94
|
+
contents: GoogleContent[];
|
|
95
|
+
systemInstruction?: GoogleContent | { parts: GooglePart[] };
|
|
96
|
+
tools?: GoogleTool[];
|
|
97
|
+
toolConfig?: GoogleToolConfig;
|
|
98
|
+
generationConfig?: GoogleGenerationConfig;
|
|
99
|
+
cachedContent?: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface GoogleGenerateContentResponse {
|
|
103
|
+
candidates?: GoogleCandidate[];
|
|
104
|
+
usageMetadata?: GoogleUsageMetadata;
|
|
105
|
+
modelVersion?: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// ============================================================================
|
|
109
|
+
// OpenCode Payload Types
|
|
110
|
+
// ============================================================================
|
|
111
|
+
|
|
112
|
+
export type OpenCodeReasoningEffort = "low" | "medium" | "high";
|
|
113
|
+
export type OpenCodePromptCacheRetention = "none" | "1h" | "24h";
|
|
114
|
+
|
|
115
|
+
export interface OpenCodeCacheControl {
|
|
116
|
+
type: "ephemeral";
|
|
117
|
+
ttl?: "1h";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface OpenCodeUsage {
|
|
121
|
+
prompt_tokens?: number;
|
|
122
|
+
completion_tokens?: number;
|
|
123
|
+
total_tokens?: number;
|
|
124
|
+
prompt_tokens_details?: {
|
|
125
|
+
cached_tokens?: number;
|
|
126
|
+
cache_read?: number;
|
|
127
|
+
cache_creation?: number;
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface OpenCodeChatRequest {
|
|
132
|
+
model: string;
|
|
133
|
+
messages: any[];
|
|
134
|
+
tools?: any[];
|
|
135
|
+
tool_choice?: any;
|
|
136
|
+
temperature?: number;
|
|
137
|
+
top_p?: number;
|
|
138
|
+
max_tokens?: number;
|
|
139
|
+
stop?: string | string[];
|
|
140
|
+
stream?: boolean;
|
|
141
|
+
reasoning_effort?: OpenCodeReasoningEffort;
|
|
142
|
+
prompt_cache_retention?: OpenCodePromptCacheRetention;
|
|
143
|
+
session_id?: string;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface OpenCodeChatResponse {
|
|
147
|
+
id?: string;
|
|
148
|
+
object?: string;
|
|
149
|
+
created?: number;
|
|
150
|
+
model?: string;
|
|
151
|
+
choices?: Array<{
|
|
152
|
+
index?: number;
|
|
153
|
+
message?: any;
|
|
154
|
+
delta?: any;
|
|
155
|
+
finish_reason?: string;
|
|
156
|
+
}>;
|
|
157
|
+
usage?: OpenCodeUsage;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export interface OpenCodeResponsesInputItem {
|
|
161
|
+
role: string;
|
|
162
|
+
content: any;
|
|
163
|
+
cache_control?: OpenCodeCacheControl;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export interface OpenCodeResponsesOutputItem {
|
|
167
|
+
id?: string;
|
|
168
|
+
type?: string;
|
|
169
|
+
status?: string;
|
|
170
|
+
content?: any;
|
|
171
|
+
name?: string;
|
|
172
|
+
call_id?: string;
|
|
173
|
+
arguments?: string;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export interface OpenCodeResponsesRequest {
|
|
177
|
+
model: string;
|
|
178
|
+
input: OpenCodeResponsesInputItem[];
|
|
179
|
+
tools?: any[];
|
|
180
|
+
parameters?: Record<string, unknown>;
|
|
181
|
+
session_id?: string;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export interface OpenCodeResponsesResponse {
|
|
185
|
+
id?: string;
|
|
186
|
+
output?: OpenCodeResponsesOutputItem[];
|
|
187
|
+
usage?: OpenCodeUsage;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// ============================================================================
|
|
191
|
+
// OpenRouter Payload Types
|
|
192
|
+
// ============================================================================
|
|
193
|
+
|
|
194
|
+
export interface OpenRouterProviderRouting {
|
|
195
|
+
order?: string[];
|
|
196
|
+
allow_fallbacks?: boolean;
|
|
197
|
+
require_parameters?: boolean;
|
|
198
|
+
data_collection?: "allow" | "deny";
|
|
199
|
+
ignore?: string[];
|
|
200
|
+
quantizations?: string[];
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export interface OpenRouterReasoning {
|
|
204
|
+
effort?: "high" | "medium" | "low";
|
|
205
|
+
max_tokens?: number;
|
|
206
|
+
exclude?: boolean;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export interface OpenRouterParameters {
|
|
210
|
+
temperature?: number;
|
|
211
|
+
top_p?: number;
|
|
212
|
+
top_k?: number;
|
|
213
|
+
max_tokens?: number;
|
|
214
|
+
seed?: number;
|
|
215
|
+
stop?: string | string[];
|
|
216
|
+
repetition_penalty?: number;
|
|
217
|
+
presence_penalty?: number;
|
|
218
|
+
frequency_penalty?: number;
|
|
219
|
+
reasoning?: OpenRouterReasoning;
|
|
220
|
+
provider?: OpenRouterProviderRouting;
|
|
221
|
+
transforms?: string[];
|
|
222
|
+
models?: string[];
|
|
223
|
+
route?: "fallback";
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export interface OpenRouterUsage {
|
|
227
|
+
prompt_tokens?: number;
|
|
228
|
+
completion_tokens?: number;
|
|
229
|
+
total_tokens?: number;
|
|
230
|
+
cached_tokens?: number;
|
|
231
|
+
prompt_tokens_details?: {
|
|
232
|
+
cached_tokens?: number;
|
|
233
|
+
};
|
|
234
|
+
total_cost?: number;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export interface OpenRouterChatRequest {
|
|
238
|
+
model: string;
|
|
239
|
+
messages: any[];
|
|
240
|
+
tools?: any[];
|
|
241
|
+
tool_choice?: any;
|
|
242
|
+
stream?: boolean;
|
|
243
|
+
temperature?: number;
|
|
244
|
+
top_p?: number;
|
|
245
|
+
top_k?: number;
|
|
246
|
+
max_tokens?: number;
|
|
247
|
+
seed?: number;
|
|
248
|
+
stop?: string | string[];
|
|
249
|
+
reasoning?: OpenRouterReasoning;
|
|
250
|
+
provider?: OpenRouterProviderRouting;
|
|
251
|
+
transforms?: string[];
|
|
252
|
+
models?: string[];
|
|
253
|
+
route?: "fallback";
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export interface OpenRouterResponse {
|
|
257
|
+
id?: string;
|
|
258
|
+
model?: string;
|
|
259
|
+
choices?: Array<{
|
|
260
|
+
index?: number;
|
|
261
|
+
message?: any;
|
|
262
|
+
delta?: any;
|
|
263
|
+
finish_reason?: string;
|
|
264
|
+
}>;
|
|
265
|
+
usage?: OpenRouterUsage;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// ============================================================================
|
|
269
|
+
// OpenAI Payload Types
|
|
270
|
+
// ============================================================================
|
|
271
|
+
|
|
272
|
+
export type OpenAIMessageRole = "system" | "user" | "assistant" | "tool";
|
|
273
|
+
export type OpenAIReasoningEffort = "low" | "medium" | "high";
|
|
274
|
+
export type OpenAIServiceTier = "auto" | "default";
|
|
275
|
+
|
|
276
|
+
export interface OpenAITextPart {
|
|
277
|
+
type: "text";
|
|
278
|
+
text: string;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export interface OpenAIImageUrlPart {
|
|
282
|
+
type: "image_url";
|
|
283
|
+
image_url: {
|
|
284
|
+
url: string;
|
|
285
|
+
detail?: "auto" | "low" | "high";
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export interface OpenAIInputAudioPart {
|
|
290
|
+
type: "input_audio";
|
|
291
|
+
input_audio: {
|
|
292
|
+
data: string;
|
|
293
|
+
format: "wav" | "mp3";
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export interface OpenAIVideoUrlPart {
|
|
298
|
+
type: "video_url";
|
|
299
|
+
video_url: {
|
|
300
|
+
url: string;
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export type OpenAIContentPart =
|
|
305
|
+
| OpenAITextPart
|
|
306
|
+
| OpenAIImageUrlPart
|
|
307
|
+
| OpenAIInputAudioPart
|
|
308
|
+
| OpenAIVideoUrlPart;
|
|
309
|
+
|
|
310
|
+
export interface OpenAIToolCall {
|
|
311
|
+
id: string;
|
|
312
|
+
type: "function";
|
|
313
|
+
function: {
|
|
314
|
+
name: string;
|
|
315
|
+
arguments: string;
|
|
316
|
+
};
|
|
317
|
+
extra_content?: {
|
|
318
|
+
google?: {
|
|
319
|
+
thought_signature?: string;
|
|
320
|
+
};
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export interface OpenAIMessage {
|
|
325
|
+
role: OpenAIMessageRole;
|
|
326
|
+
content: string | OpenAIContentPart[];
|
|
327
|
+
name?: string;
|
|
328
|
+
tool_call_id?: string;
|
|
329
|
+
tool_calls?: OpenAIToolCall[];
|
|
330
|
+
reasoning_content?: string;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export interface OpenAITool {
|
|
334
|
+
type: "function";
|
|
335
|
+
function: {
|
|
336
|
+
name: string;
|
|
337
|
+
description?: string;
|
|
338
|
+
parameters?: Record<string, unknown>;
|
|
339
|
+
strict?: boolean;
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
export type OpenAIToolChoice =
|
|
344
|
+
| "none"
|
|
345
|
+
| "auto"
|
|
346
|
+
| "required"
|
|
347
|
+
| { type: "function"; function: { name: string } };
|
|
348
|
+
|
|
349
|
+
export interface OpenAIUsage {
|
|
350
|
+
prompt_tokens: number;
|
|
351
|
+
completion_tokens: number;
|
|
352
|
+
total_tokens: number;
|
|
353
|
+
prompt_tokens_details?: {
|
|
354
|
+
cached_tokens?: number;
|
|
355
|
+
};
|
|
356
|
+
completion_tokens_details?: {
|
|
357
|
+
reasoning_tokens?: number;
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export interface OpenAIChoice {
|
|
362
|
+
index: number;
|
|
363
|
+
message: {
|
|
364
|
+
role: "assistant";
|
|
365
|
+
content: string | null;
|
|
366
|
+
reasoning_content?: string;
|
|
367
|
+
tool_calls?: OpenAIToolCall[];
|
|
368
|
+
};
|
|
369
|
+
finish_reason: "stop" | "length" | "tool_calls" | "content_filter" | null;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
export interface OpenAIDelta {
|
|
373
|
+
role?: string;
|
|
374
|
+
content?: string | null;
|
|
375
|
+
reasoning_content?: string;
|
|
376
|
+
tool_calls?: Array<{
|
|
377
|
+
index: number;
|
|
378
|
+
id?: string;
|
|
379
|
+
type?: "function";
|
|
380
|
+
function?: {
|
|
381
|
+
name?: string;
|
|
382
|
+
arguments?: string;
|
|
383
|
+
};
|
|
384
|
+
extra_content?: {
|
|
385
|
+
google?: {
|
|
386
|
+
thought_signature?: string;
|
|
387
|
+
};
|
|
388
|
+
};
|
|
389
|
+
}>;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
export interface OpenAIChunkChoice {
|
|
393
|
+
index: number;
|
|
394
|
+
delta: OpenAIDelta;
|
|
395
|
+
finish_reason: "stop" | "length" | "tool_calls" | "content_filter" | null;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
export interface OpenAIChatCompletionRequest {
|
|
399
|
+
model: string;
|
|
400
|
+
messages: OpenAIMessage[];
|
|
401
|
+
tools?: OpenAITool[];
|
|
402
|
+
tool_choice?: OpenAIToolChoice;
|
|
403
|
+
temperature?: number;
|
|
404
|
+
top_p?: number;
|
|
405
|
+
max_tokens?: number;
|
|
406
|
+
max_completion_tokens?: number;
|
|
407
|
+
stop?: string | string[];
|
|
408
|
+
stream?: boolean;
|
|
409
|
+
stream_options?: { include_usage?: boolean };
|
|
410
|
+
reasoning_effort?: OpenAIReasoningEffort;
|
|
411
|
+
service_tier?: OpenAIServiceTier;
|
|
412
|
+
response_format?: { type: "text" | "json_object" };
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export interface OpenAIChatCompletionResponse {
|
|
416
|
+
id: string;
|
|
417
|
+
object: string;
|
|
418
|
+
created: number;
|
|
419
|
+
model: string;
|
|
420
|
+
choices: OpenAIChoice[];
|
|
421
|
+
usage?: OpenAIUsage;
|
|
422
|
+
service_tier?: string;
|
|
423
|
+
system_fingerprint?: string;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export interface OpenAIChatCompletionChunk {
|
|
427
|
+
id: string;
|
|
428
|
+
object: string;
|
|
429
|
+
created: number;
|
|
430
|
+
model: string;
|
|
431
|
+
choices: OpenAIChunkChoice[];
|
|
432
|
+
usage?: OpenAIUsage;
|
|
433
|
+
system_fingerprint?: string;
|
|
434
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type { TokenUsage } from "./core.ts";
|
|
2
|
+
import type { ToolCallRecord, ToolResultRecord } from "./tool.ts";
|
|
3
|
+
import type { ProviderRawData, ProviderId } from "./model.ts";
|
|
4
|
+
|
|
5
|
+
/** Per-worker result and usage metadata attached to an AgentResponse. */
|
|
6
|
+
export interface SubAgentExecutionMetadata {
|
|
7
|
+
name: string;
|
|
8
|
+
role?: string;
|
|
9
|
+
task: string;
|
|
10
|
+
model: string;
|
|
11
|
+
provider: ProviderId | string;
|
|
12
|
+
durationMs: number;
|
|
13
|
+
usage: TokenUsage;
|
|
14
|
+
turns: number;
|
|
15
|
+
finishReason?: string;
|
|
16
|
+
responseId?: string;
|
|
17
|
+
text: string;
|
|
18
|
+
thinking?: string;
|
|
19
|
+
toolCalls?: ToolCallRecord[];
|
|
20
|
+
raw?: ProviderRawData;
|
|
21
|
+
isError?: boolean;
|
|
22
|
+
error?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** JSON representation returned by AgentResponse.toJSON(). */
|
|
26
|
+
export interface AgentResponseJSON {
|
|
27
|
+
text: string;
|
|
28
|
+
thinking?: string;
|
|
29
|
+
thoughtSignature?: string;
|
|
30
|
+
toolCalls?: ToolCallRecord[];
|
|
31
|
+
toolResults?: ToolResultRecord[];
|
|
32
|
+
subagents?: SubAgentExecutionMetadata[];
|
|
33
|
+
usage: TokenUsage;
|
|
34
|
+
responseId?: string;
|
|
35
|
+
model: string;
|
|
36
|
+
provider: ProviderId | string;
|
|
37
|
+
finishReason?: string;
|
|
38
|
+
durationMs: number;
|
|
39
|
+
raw: ProviderRawData;
|
|
40
|
+
turns: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Normalized final result returned by every Agent run. */
|
|
44
|
+
export class AgentResponse {
|
|
45
|
+
readonly text: string;
|
|
46
|
+
readonly thinking?: string;
|
|
47
|
+
readonly thoughtSignature?: string;
|
|
48
|
+
readonly toolCalls: ToolCallRecord[];
|
|
49
|
+
readonly toolResults: ToolResultRecord[];
|
|
50
|
+
readonly subagents: SubAgentExecutionMetadata[];
|
|
51
|
+
readonly usage: TokenUsage;
|
|
52
|
+
readonly responseId?: string;
|
|
53
|
+
readonly model: string;
|
|
54
|
+
readonly provider: ProviderId | string;
|
|
55
|
+
readonly finishReason?: string;
|
|
56
|
+
readonly durationMs: number;
|
|
57
|
+
readonly raw: ProviderRawData;
|
|
58
|
+
readonly turns: number;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Constructs a normalized response. Agent runs create this automatically;
|
|
62
|
+
* construct one directly when adapting a custom provider integration.
|
|
63
|
+
*
|
|
64
|
+
* @param data Response text, model metadata, usage, timing, and optional
|
|
65
|
+
* tool/sub-agent records.
|
|
66
|
+
*/
|
|
67
|
+
constructor(data: {
|
|
68
|
+
text: string;
|
|
69
|
+
thinking?: string;
|
|
70
|
+
thoughtSignature?: string;
|
|
71
|
+
toolCalls?: ToolCallRecord[];
|
|
72
|
+
toolResults?: ToolResultRecord[];
|
|
73
|
+
subagents?: SubAgentExecutionMetadata[];
|
|
74
|
+
usage: TokenUsage;
|
|
75
|
+
responseId?: string;
|
|
76
|
+
model: string;
|
|
77
|
+
provider: ProviderId | string;
|
|
78
|
+
finishReason?: string;
|
|
79
|
+
durationMs: number;
|
|
80
|
+
raw: ProviderRawData;
|
|
81
|
+
turns?: number;
|
|
82
|
+
}) {
|
|
83
|
+
this.text = data.text;
|
|
84
|
+
this.thinking = data.thinking;
|
|
85
|
+
this.thoughtSignature = data.thoughtSignature;
|
|
86
|
+
this.toolCalls = data.toolCalls ?? [];
|
|
87
|
+
this.toolResults = data.toolResults ?? [];
|
|
88
|
+
this.subagents = data.subagents ?? [];
|
|
89
|
+
this.usage = data.usage;
|
|
90
|
+
this.responseId = data.responseId;
|
|
91
|
+
this.model = data.model;
|
|
92
|
+
this.provider = data.provider;
|
|
93
|
+
this.finishReason = data.finishReason;
|
|
94
|
+
this.durationMs = data.durationMs;
|
|
95
|
+
this.raw = data.raw;
|
|
96
|
+
this.turns = data.turns ?? 1;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Returns the final assistant text. */
|
|
100
|
+
toString(): string {
|
|
101
|
+
return this.text;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Returns a JSON-safe representation including usage, tools, and raw wire data. */
|
|
105
|
+
toJSON(): AgentResponseJSON {
|
|
106
|
+
return {
|
|
107
|
+
text: this.text,
|
|
108
|
+
thinking: this.thinking,
|
|
109
|
+
thoughtSignature: this.thoughtSignature,
|
|
110
|
+
toolCalls: this.toolCalls.length > 0 ? this.toolCalls : undefined,
|
|
111
|
+
toolResults: this.toolResults.length > 0 ? this.toolResults : undefined,
|
|
112
|
+
subagents: this.subagents.length > 0 ? this.subagents : undefined,
|
|
113
|
+
usage: this.usage,
|
|
114
|
+
responseId: this.responseId,
|
|
115
|
+
model: this.model,
|
|
116
|
+
provider: this.provider,
|
|
117
|
+
finishReason: this.finishReason,
|
|
118
|
+
durationMs: this.durationMs,
|
|
119
|
+
raw: this.raw,
|
|
120
|
+
turns: this.turns,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Event names emitted by AssistantMessageEventStream. */
|
|
126
|
+
export type StreamEventType =
|
|
127
|
+
| "start"
|
|
128
|
+
| "text_start"
|
|
129
|
+
| "text_delta"
|
|
130
|
+
| "text_end"
|
|
131
|
+
| "thinking_start"
|
|
132
|
+
| "thinking_delta"
|
|
133
|
+
| "thinking_end"
|
|
134
|
+
| "tool_call_start"
|
|
135
|
+
| "tool_call_delta"
|
|
136
|
+
| "tool_call_complete"
|
|
137
|
+
| "tool_result"
|
|
138
|
+
| "subagent_complete"
|
|
139
|
+
| "usage"
|
|
140
|
+
| "done"
|
|
141
|
+
| "error";
|
|
142
|
+
|
|
143
|
+
/** Payload for one streaming text, thinking, tool, usage, or lifecycle event. */
|
|
144
|
+
export interface StreamEvent {
|
|
145
|
+
type: StreamEventType;
|
|
146
|
+
delta?: string;
|
|
147
|
+
thinkingDelta?: string;
|
|
148
|
+
toolCall?: ToolCallRecord;
|
|
149
|
+
toolResult?: ToolResultRecord;
|
|
150
|
+
subagent?: SubAgentExecutionMetadata;
|
|
151
|
+
usage?: TokenUsage;
|
|
152
|
+
responseId?: string;
|
|
153
|
+
finishReason?: string;
|
|
154
|
+
error?: Error | unknown;
|
|
155
|
+
partialText?: string;
|
|
156
|
+
partialThinking?: string;
|
|
157
|
+
raw?: ProviderRawData;
|
|
158
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export interface ToolExecutionContext {
|
|
4
|
+
/** Unique ID for the model-generated invocation. */
|
|
5
|
+
toolCallId: string;
|
|
6
|
+
/** Name of the owning agent, when invoked through an Agent. */
|
|
7
|
+
agentName?: string;
|
|
8
|
+
/** Abort signal for parent cancellation or a tool timeout. */
|
|
9
|
+
signal?: AbortSignal;
|
|
10
|
+
/** Session identifier associated with this invocation. */
|
|
11
|
+
sessionId?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type ToolExecuteFn<TInput = any, TOutput = any> = (
|
|
15
|
+
input: TInput,
|
|
16
|
+
context: ToolExecutionContext
|
|
17
|
+
) => Promise<TOutput> | TOutput;
|
|
18
|
+
|
|
19
|
+
export interface ToolDefinition<TInput = any, TOutput = any> {
|
|
20
|
+
/** Optional public name; record keys are used when omitted. */
|
|
21
|
+
name?: string;
|
|
22
|
+
/** Description shown to the model when selecting tools. */
|
|
23
|
+
description: string;
|
|
24
|
+
/** Zod schema (or JSON Schema object) used to validate the model input. */
|
|
25
|
+
input?: z.ZodType<TInput> | Record<string, unknown>;
|
|
26
|
+
/** Explicit JSON Schema, taking precedence over `input` for provider declarations. */
|
|
27
|
+
parameters?: Record<string, unknown>;
|
|
28
|
+
/** Whether providers should enforce strict function-argument validation. */
|
|
29
|
+
strict?: boolean;
|
|
30
|
+
/** Maximum best-effort wall-clock time for one execution attempt in milliseconds. 0/undefined means no bound. Cancellation is cooperative via `signal`. */
|
|
31
|
+
timeoutMs?: number;
|
|
32
|
+
/** Maximum total attempts for transient failures. 0/undefined means no configured retry limit. */
|
|
33
|
+
maxTries?: number | string;
|
|
34
|
+
/** Maximum simultaneous calls for this tool. 0/undefined uses the global pool. */
|
|
35
|
+
maxConcurrency?: number | string;
|
|
36
|
+
/** Function executed after validation; its result is made JSON-safe for model context. */
|
|
37
|
+
execute: ToolExecuteFn<TInput, TOutput>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface StandardToolDeclaration {
|
|
41
|
+
name: string;
|
|
42
|
+
description: string;
|
|
43
|
+
parameters: Record<string, unknown>;
|
|
44
|
+
strict?: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ToolCallRecord {
|
|
48
|
+
id: string;
|
|
49
|
+
name: string;
|
|
50
|
+
arguments: Record<string, unknown>;
|
|
51
|
+
rawArguments?: string;
|
|
52
|
+
thoughtSignature?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface ToolResultRecord {
|
|
56
|
+
id: string;
|
|
57
|
+
name: string;
|
|
58
|
+
result: unknown;
|
|
59
|
+
isError?: boolean;
|
|
60
|
+
durationMs?: number;
|
|
61
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** Runtime-agnostic base64 helpers — no Node Buffer, safe in browsers. */
|
|
2
|
+
|
|
3
|
+
export function base64ToBytes(b64: string): Uint8Array {
|
|
4
|
+
const clean = b64.replace(/\s/g, "");
|
|
5
|
+
const nodeBuffer = (globalThis as any)?.Buffer;
|
|
6
|
+
if (nodeBuffer && typeof nodeBuffer.from === "function") {
|
|
7
|
+
const buf = nodeBuffer.from(clean, "base64");
|
|
8
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
9
|
+
}
|
|
10
|
+
const bin = atob(clean);
|
|
11
|
+
const out = new Uint8Array(bin.length);
|
|
12
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
13
|
+
return out;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function bytesToBase64(bytes: Uint8Array): string {
|
|
17
|
+
const nodeBuffer = (globalThis as any)?.Buffer;
|
|
18
|
+
if (nodeBuffer && typeof nodeBuffer.from === "function") {
|
|
19
|
+
return nodeBuffer.from(bytes).toString("base64");
|
|
20
|
+
}
|
|
21
|
+
let bin = "";
|
|
22
|
+
const CHUNK = 0x8000;
|
|
23
|
+
for (let i = 0; i < bytes.length; i += CHUNK) {
|
|
24
|
+
bin += String.fromCharCode(...bytes.subarray(i, i + CHUNK));
|
|
25
|
+
}
|
|
26
|
+
return btoa(bin);
|
|
27
|
+
}
|