@codeworksh/aikit 0.3.0 → 0.4.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.
Files changed (41) hide show
  1. package/README.md +73 -81
  2. package/{agent → dist/pack/agent}/codemode/drivers/drivers.d.mts +1 -1
  3. package/dist/pack/codemode-BEDXG7WN.d.mts +8736 -0
  4. package/{index.d.mts → dist/pack/index.d.mts} +3 -3
  5. package/dist/pack/index.mjs +8226 -0
  6. package/dist/pack/index.mjs.map +1 -0
  7. package/package.json +32 -22
  8. package/src/agent/agent.ts +790 -0
  9. package/src/agent/codemode/codemode.ts +561 -0
  10. package/src/agent/codemode/drivers/drivers.ts +1 -0
  11. package/src/agent/codemode/drivers/quickjs-wasi-driver.ts +149 -0
  12. package/src/agent/codemode/index.ts +1 -0
  13. package/src/agent/loop.ts +903 -0
  14. package/src/agent.ts +11 -0
  15. package/src/event/event.ts +277 -0
  16. package/src/index.ts +6 -0
  17. package/src/llm.ts +31 -0
  18. package/src/message/message.ts +311 -0
  19. package/src/model/catalog.ts +94 -0
  20. package/src/model/model.ts +250 -0
  21. package/src/model/transform.ts +59 -0
  22. package/src/provider/provider.ts +60 -0
  23. package/src/provider/providers/anthropic/index.ts +792 -0
  24. package/src/provider/providers/github/github.ts +34 -0
  25. package/src/provider/providers/openai/completions.ts +953 -0
  26. package/src/provider/providers/utils.ts +7 -0
  27. package/src/provider/register.ts +6 -0
  28. package/src/provider/stream.ts +278 -0
  29. package/src/stream.ts +72 -0
  30. package/src/utils/eventstream.ts +95 -0
  31. package/src/utils/helpers.ts +24 -0
  32. package/src/utils/jsonparse.ts +32 -0
  33. package/src/utils/overflow.ts +121 -0
  34. package/src/utils/sanitize.ts +25 -0
  35. package/src/utils/validation.ts +87 -0
  36. package/LICENSE +0 -21
  37. package/codemode-z3ZIw-5q.d.mts +0 -8234
  38. package/index.mjs +0 -2694
  39. package/index.mjs.map +0 -1
  40. /package/{agent → dist/pack/agent}/codemode/drivers/drivers.mjs +0 -0
  41. /package/{agent → dist/pack/agent}/codemode/drivers/drivers.mjs.map +0 -0
package/index.mjs DELETED
@@ -1,2694 +0,0 @@
1
- import { Filesystem, NamedError, lazy } from "@codeworksh/utils";
2
- import { Type, TypeGuard } from "@sinclair/typebox";
3
- import { capitalize, keys, mapValues, pick, pipe } from "remeda";
4
- import Ajv from "ajv";
5
- import addFormats from "ajv-formats";
6
- import { uuidv7 } from "uuidv7";
7
- import Anthropic from "@anthropic-ai/sdk";
8
- import { parse } from "partial-json";
9
- import { transform } from "esbuild";
10
- //#region src/provider/provider.ts
11
- let Provider;
12
- (function(_Provider) {
13
- const KnownProviderEnum = _Provider.KnownProviderEnum = {
14
- anthropic: "anthropic",
15
- openai: "openai"
16
- };
17
- const KnownProviderSchema = _Provider.KnownProviderSchema = Type.Union([Type.Literal(KnownProviderEnum.anthropic), Type.Literal(KnownProviderEnum.openai)]);
18
- _Provider.Info = Type.Object({
19
- id: KnownProviderSchema,
20
- name: Type.String(),
21
- env: Type.Array(Type.String()),
22
- key: Type.Optional(Type.String()),
23
- options: Type.Optional(Type.Record(Type.String(), Type.Any()))
24
- });
25
- _Provider.ReasoningEffortMapSchema = Type.Object({
26
- minimal: Type.Optional(Type.String()),
27
- low: Type.Optional(Type.String()),
28
- medium: Type.Optional(Type.String()),
29
- high: Type.Optional(Type.String()),
30
- xhigh: Type.Optional(Type.String())
31
- });
32
- _Provider.OpenRouterRoutingSchema = Type.Object({
33
- only: Type.Optional(Type.Array(Type.String())),
34
- order: Type.Optional(Type.Array(Type.String()))
35
- });
36
- _Provider.VercelGatewayRoutingSchema = Type.Object({
37
- only: Type.Optional(Type.Array(Type.String())),
38
- order: Type.Optional(Type.Array(Type.String()))
39
- });
40
- })(Provider || (Provider = {}));
41
- //#endregion
42
- //#region src/model/catalog.ts
43
- let ModelCatalog;
44
- (function(_ModelCatalog) {
45
- function modelsDevURL() {
46
- return process.env.OPENCODE_MODELS_URL || "https://models.dev";
47
- }
48
- function modelsDevPath() {
49
- return process.env.CODEWORK_AIKIT_MODELS_PATH;
50
- }
51
- const modelsDevData = _ModelCatalog.modelsDevData = lazy(async () => {
52
- const path = modelsDevPath();
53
- if (path) {
54
- const result = await Filesystem.readJson(path).catch(() => void 0);
55
- if (result) return result;
56
- }
57
- const json = await fetch(`${modelsDevURL()}/api.json`).then((x) => x.text());
58
- return JSON.parse(json);
59
- });
60
- async function get() {
61
- return modelsDevData();
62
- }
63
- _ModelCatalog.get = get;
64
- async function refresh() {
65
- const result = await fetch(`${modelsDevURL()}/api.json`, { signal: AbortSignal.timeout(10 * 1e3) }).catch(() => void 0);
66
- if (!result?.ok) return;
67
- const json = await result.text();
68
- const path = modelsDevPath();
69
- if (path) await Filesystem.write(path, json);
70
- modelsDevData.reset();
71
- }
72
- _ModelCatalog.refresh = refresh;
73
- ModelCatalog.refresh();
74
- setInterval(async () => {
75
- await ModelCatalog.refresh();
76
- }, 60 * 1e3 * 60).unref();
77
- })(ModelCatalog || (ModelCatalog = {}));
78
- //#endregion
79
- //#region src/model/model.ts
80
- let Model;
81
- (function(_Model) {
82
- const KnownProtocolEnum = _Model.KnownProtocolEnum = {
83
- anthropicMessages: "anthropic-messages",
84
- openaiCompletions: "openai-completions",
85
- openaiResponses: "openai-responses"
86
- };
87
- _Model.KnownProtocolSchema = Type.Union([
88
- Type.Literal(KnownProtocolEnum.anthropicMessages),
89
- Type.Literal(KnownProtocolEnum.openaiCompletions),
90
- Type.Literal(KnownProtocolEnum.openaiResponses)
91
- ]);
92
- const InputSchema = Type.Array(Type.Union([Type.Literal("text"), Type.Literal("image")]));
93
- const CostSchema = Type.Object({
94
- input: Type.Number(),
95
- output: Type.Number(),
96
- cacheRead: Type.Number(),
97
- cacheWrite: Type.Number()
98
- });
99
- const HeadersSchema = Type.Optional(Type.Record(Type.String(), Type.String()));
100
- const BaseSchema = _Model.BaseSchema = Type.Object({
101
- id: Type.String(),
102
- name: Type.String(),
103
- provider: Provider.Info,
104
- baseUrl: Type.String(),
105
- reasoning: Type.Boolean(),
106
- input: InputSchema,
107
- cost: CostSchema,
108
- contextWindow: Type.Number(),
109
- maxTokens: Type.Number(),
110
- headers: HeadersSchema
111
- });
112
- const OpenAICompletionsCompatSchema = _Model.OpenAICompletionsCompatSchema = Type.Object({
113
- supportsStore: Type.Optional(Type.Boolean()),
114
- supportsDeveloperRole: Type.Optional(Type.Boolean()),
115
- supportsReasoningEffort: Type.Optional(Type.Boolean()),
116
- reasoningEffortMap: Type.Optional(Provider.ReasoningEffortMapSchema),
117
- supportsUsageInStreaming: Type.Optional(Type.Boolean()),
118
- maxTokensField: Type.Optional(Type.Union([Type.Literal("max_completion_tokens"), Type.Literal("max_tokens")])),
119
- requiresToolResultName: Type.Optional(Type.Boolean()),
120
- requiresAssistantAfterToolResult: Type.Optional(Type.Boolean()),
121
- requiresThinkingAsText: Type.Optional(Type.Boolean()),
122
- thinkingFormat: Type.Optional(Type.Union([
123
- Type.Literal("openai"),
124
- Type.Literal("openrouter"),
125
- Type.Literal("zai"),
126
- Type.Literal("qwen"),
127
- Type.Literal("qwen-chat-template")
128
- ])),
129
- openRouterRouting: Type.Optional(Provider.OpenRouterRoutingSchema),
130
- vercelGatewayRouting: Type.Optional(Provider.VercelGatewayRoutingSchema),
131
- supportsStrictMode: Type.Optional(Type.Boolean())
132
- });
133
- const OpenAIResponsesCompatSchema = _Model.OpenAIResponsesCompatSchema = Type.Object({});
134
- _Model.OpenAICompatSchema = Type.Union([OpenAICompletionsCompatSchema, OpenAIResponsesCompatSchema]);
135
- const AnthropicSchema = _Model.AnthropicSchema = Type.Composite([BaseSchema, Type.Object({ protocol: Type.Literal(KnownProtocolEnum.anthropicMessages) })]);
136
- const OpenAICompletionsSchema = _Model.OpenAICompletionsSchema = Type.Composite([BaseSchema, Type.Object({
137
- protocol: Type.Literal(KnownProtocolEnum.openaiCompletions),
138
- compat: Type.Optional(OpenAICompletionsCompatSchema)
139
- })]);
140
- const OpenAIResponsesSchema = _Model.OpenAIResponsesSchema = Type.Composite([BaseSchema, Type.Object({
141
- protocol: Type.Literal(KnownProtocolEnum.openaiResponses),
142
- compat: Type.Optional(OpenAIResponsesCompatSchema)
143
- })]);
144
- _Model.Schema = Type.Union([
145
- AnthropicSchema,
146
- OpenAICompletionsSchema,
147
- OpenAIResponsesSchema
148
- ]);
149
- const BUILTINS = keys(Provider.KnownProviderEnum);
150
- function calculateCost(model, usage) {
151
- usage.cost.input = usage.input / 1e6 * model.cost.input;
152
- usage.cost.output = usage.output / 1e6 * model.cost.output;
153
- usage.cost.cacheRead = usage.cacheRead / 1e6 * model.cost.cacheRead;
154
- usage.cost.cacheWrite = usage.cacheWrite / 1e6 * model.cost.cacheWrite;
155
- usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
156
- }
157
- _Model.calculateCost = calculateCost;
158
- function resolveProtocol(providerId) {
159
- switch (providerId) {
160
- case Provider.KnownProviderEnum.anthropic: return KnownProtocolEnum.anthropicMessages;
161
- case Provider.KnownProviderEnum.openai: return KnownProtocolEnum.openaiResponses;
162
- }
163
- }
164
- function normalizeInput(input) {
165
- const normalized = /* @__PURE__ */ new Set();
166
- for (const modality of input ?? ["text"]) if (modality === "text" || modality === "image") normalized.add(modality);
167
- if (normalized.size === 0) normalized.add("text");
168
- return [...normalized];
169
- }
170
- function toProviderInfo(providerId, provider) {
171
- return {
172
- id: providerId,
173
- name: provider.name,
174
- env: provider.env,
175
- key: provider.key,
176
- options: provider.headers
177
- };
178
- }
179
- function toModelValue(providerId, provider, model) {
180
- const baseUrl = model.baseUrl ?? provider.baseUrl ?? provider.api;
181
- return {
182
- id: model.id,
183
- name: model.name,
184
- provider: toProviderInfo(providerId, provider),
185
- baseUrl,
186
- reasoning: Boolean(model.reasoning),
187
- input: normalizeInput(model.modalities.input),
188
- cost: {
189
- input: model.cost?.input ?? 0,
190
- output: model.cost?.output ?? 0,
191
- cacheRead: model.cost?.cache_read ?? 0,
192
- cacheWrite: model.cost?.cache_write ?? 0
193
- },
194
- contextWindow: model.limit?.context ?? 0,
195
- maxTokens: model.limit?.output ?? 0,
196
- headers: model.headers ?? provider.headers,
197
- protocol: resolveProtocol(providerId)
198
- };
199
- }
200
- async function getBuiltInModels() {
201
- return pipe(await ModelCatalog.get(), pick(BUILTINS), mapValues((provider, providerId) => mapValues(provider.models, (model) => toModelValue(providerId, provider, model))));
202
- }
203
- _Model.getBuiltInModels = getBuiltInModels;
204
- const registry = _Model.registry = lazy(async () => {
205
- const registry = /* @__PURE__ */ new Map();
206
- const models = await getBuiltInModels();
207
- for (const [provider, value] of Object.entries(models)) {
208
- const providerModels = /* @__PURE__ */ new Map();
209
- for (const [id, model] of Object.entries(value)) providerModels.set(id, model);
210
- registry.set(provider, providerModels);
211
- }
212
- return registry;
213
- });
214
- async function getModel(provider, model) {
215
- return (await registry()).get(provider)?.get(model);
216
- }
217
- _Model.getModel = getModel;
218
- async function getProviders() {
219
- const data = await registry();
220
- return Array.from(data.keys());
221
- }
222
- _Model.getProviders = getProviders;
223
- async function getModels(provider) {
224
- const models = (await registry()).get(provider);
225
- return models ? Array.from(models.values()) : [];
226
- }
227
- _Model.getModels = getModels;
228
- function isGpt5OrLater(modelID) {
229
- const match = /^gpt-(\d+)/.exec(modelID);
230
- if (!match) return false;
231
- return Number(match[1]) >= 5;
232
- }
233
- function supportsXhigh(model) {
234
- return isGpt5OrLater(model.id);
235
- }
236
- _Model.supportsXhigh = supportsXhigh;
237
- function modelsAreEqual(a, b) {
238
- if (!a || !b) return false;
239
- return a.id === b.id && a.provider.id === b.provider.id;
240
- }
241
- _Model.modelsAreEqual = modelsAreEqual;
242
- })(Model || (Model = {}));
243
- //#endregion
244
- //#region src/provider/stream.ts
245
- let Stream;
246
- (function(_Stream) {
247
- const ProtocolProviderNotFoundError = _Stream.ProtocolProviderNotFoundError = NamedError.create("ProtocolProviderNotFoundError", Type.Object({ protocol: Model.KnownProtocolSchema }));
248
- const ProtocolMismatchError = _Stream.ProtocolMismatchError = NamedError.create("ProtocolMismatchError", Type.Object({
249
- actual: Model.KnownProtocolSchema,
250
- expected: Model.KnownProtocolSchema
251
- }));
252
- const ThinkingLevelEnum = _Stream.ThinkingLevelEnum = {
253
- off: "off",
254
- minimal: "minimal",
255
- low: "low",
256
- medium: "medium",
257
- high: "high",
258
- xhigh: "xhigh"
259
- };
260
- const ThinkingLevelSchema = _Stream.ThinkingLevelSchema = Type.Union([
261
- Type.Literal(ThinkingLevelEnum.off),
262
- Type.Literal(ThinkingLevelEnum.minimal),
263
- Type.Literal(ThinkingLevelEnum.low),
264
- Type.Literal(ThinkingLevelEnum.medium),
265
- Type.Literal(ThinkingLevelEnum.high),
266
- Type.Literal(ThinkingLevelEnum.xhigh)
267
- ]);
268
- const ThinkingBudgetsSchema = _Stream.ThinkingBudgetsSchema = Type.Object({
269
- off: Type.Optional(Type.Number()),
270
- minimal: Type.Optional(Type.Number()),
271
- low: Type.Optional(Type.Number()),
272
- medium: Type.Optional(Type.Number()),
273
- high: Type.Optional(Type.Number())
274
- });
275
- const CacheRetentionSchema = _Stream.CacheRetentionSchema = Type.Union([
276
- Type.Literal("none"),
277
- Type.Literal("short"),
278
- Type.Literal("long")
279
- ]);
280
- const TransportSchema = _Stream.TransportSchema = Type.Union([
281
- Type.Literal("sse"),
282
- Type.Literal("websocket"),
283
- Type.Literal("auto")
284
- ]);
285
- const OptionsSchema = _Stream.OptionsSchema = Type.Object({
286
- temperature: Type.Optional(Type.Number()),
287
- maxTokens: Type.Optional(Type.Number()),
288
- signal: Type.Optional(Type.Unsafe()),
289
- apiKey: Type.Optional(Type.String()),
290
- transport: Type.Optional(TransportSchema),
291
- cacheRetention: Type.Optional(CacheRetentionSchema),
292
- sessionId: Type.Optional(Type.String()),
293
- onPayload: Type.Optional(Type.Unsafe()),
294
- headers: Type.Optional(Type.Record(Type.String(), Type.String())),
295
- maxRetryDelayMs: Type.Optional(Type.Number()),
296
- metadata: Type.Optional(Type.Record(Type.String(), Type.Unknown()))
297
- });
298
- _Stream.SimpleOptionsSchema = Type.Composite([OptionsSchema, Type.Object({
299
- reasoning: Type.Optional(ThinkingLevelSchema),
300
- thinkingBudgets: Type.Optional(ThinkingBudgetsSchema)
301
- })]);
302
- function buildBaseOptions(model, options, apiKey) {
303
- return {
304
- temperature: options?.temperature,
305
- maxTokens: options?.maxTokens || Math.min(model.maxTokens, 32e3),
306
- signal: options?.signal,
307
- apiKey: apiKey || options?.apiKey,
308
- transport: options?.transport,
309
- cacheRetention: options?.cacheRetention,
310
- sessionId: options?.sessionId,
311
- headers: options?.headers,
312
- onPayload: options?.onPayload,
313
- maxRetryDelayMs: options?.maxRetryDelayMs,
314
- metadata: options?.metadata
315
- };
316
- }
317
- _Stream.buildBaseOptions = buildBaseOptions;
318
- function clampReasoning(effort) {
319
- return effort === ThinkingLevelEnum.xhigh ? ThinkingLevelEnum.high : effort;
320
- }
321
- _Stream.clampReasoning = clampReasoning;
322
- function adjustMaxTokensForThinking(baseMaxTokens, modelMaxTokens, reasoningLevel, customBudgets) {
323
- const budgets = {
324
- minimal: 1024,
325
- low: 2048,
326
- medium: 8192,
327
- high: 16384,
328
- ...customBudgets
329
- };
330
- const minOutputTokens = 1024;
331
- let thinkingBudget = budgets[clampReasoning(reasoningLevel)];
332
- const maxTokens = Math.min(baseMaxTokens + thinkingBudget, modelMaxTokens);
333
- if (maxTokens <= thinkingBudget) thinkingBudget = Math.max(0, maxTokens - minOutputTokens);
334
- return {
335
- maxTokens,
336
- thinkingBudget
337
- };
338
- }
339
- _Stream.adjustMaxTokensForThinking = adjustMaxTokensForThinking;
340
- const protocolProviderRegistry = /* @__PURE__ */ new Map();
341
- function wrapStream(protocol, stream) {
342
- return (model, context, options) => {
343
- if (model.protocol !== protocol) throw new ProtocolMismatchError({
344
- actual: model.protocol,
345
- expected: protocol
346
- });
347
- return stream(model, context, options);
348
- };
349
- }
350
- function wrapStreamSimple(protocol, streamSimple) {
351
- return (model, context, options) => {
352
- if (model.protocol !== protocol) throw new ProtocolMismatchError({
353
- actual: model.protocol,
354
- expected: protocol
355
- });
356
- return streamSimple(model, context, options);
357
- };
358
- }
359
- function registerProtocolProvider(provider, sourceId) {
360
- protocolProviderRegistry.set(provider.protocol, {
361
- provider: {
362
- protocol: provider.protocol,
363
- stream: wrapStream(provider.protocol, provider.stream),
364
- streamSimple: wrapStreamSimple(provider.protocol, provider.streamSimple)
365
- },
366
- sourceId
367
- });
368
- }
369
- _Stream.registerProtocolProvider = registerProtocolProvider;
370
- function getProtocolProvider(protocol) {
371
- return protocolProviderRegistry.get(protocol)?.provider;
372
- }
373
- _Stream.getProtocolProvider = getProtocolProvider;
374
- function resolveProtocolProvider(model) {
375
- const provider = getProtocolProvider(model.protocol);
376
- if (!provider) throw new ProtocolProviderNotFoundError({ protocol: model.protocol });
377
- return provider;
378
- }
379
- _Stream.resolveProtocolProvider = resolveProtocolProvider;
380
- function stream(model, context, options) {
381
- return resolveProtocolProvider(model).stream(model, context, options);
382
- }
383
- _Stream.stream = stream;
384
- async function complete(model, context, options) {
385
- return stream(model, context, options).result();
386
- }
387
- _Stream.complete = complete;
388
- function streamSimple(model, context, options) {
389
- return resolveProtocolProvider(model).streamSimple(model, context, options);
390
- }
391
- _Stream.streamSimple = streamSimple;
392
- async function completeSimple(model, context, options) {
393
- return streamSimple(model, context, options).result();
394
- }
395
- _Stream.completeSimple = completeSimple;
396
- function getApiProviders() {
397
- return Array.from(protocolProviderRegistry.values(), (entry) => entry.provider);
398
- }
399
- _Stream.getApiProviders = getApiProviders;
400
- function unregisterProtocolProviders(sourceId) {
401
- for (const [api, entry] of protocolProviderRegistry.entries()) if (entry.sourceId === sourceId) protocolProviderRegistry.delete(api);
402
- }
403
- _Stream.unregisterProtocolProviders = unregisterProtocolProviders;
404
- function clearProtocolProviders() {
405
- protocolProviderRegistry.clear();
406
- }
407
- _Stream.clearProtocolProviders = clearProtocolProviders;
408
- })(Stream || (Stream = {}));
409
- //#endregion
410
- //#region src/utils/eventstream.ts
411
- var EventStream = class {
412
- queue = [];
413
- waiting = [];
414
- done = false;
415
- finalResultPromise;
416
- resolveFinalResult;
417
- constructor(isComplete, extractResult) {
418
- this.isComplete = isComplete;
419
- this.extractResult = extractResult;
420
- this.finalResultPromise = new Promise((resolve) => {
421
- this.resolveFinalResult = resolve;
422
- });
423
- }
424
- push(event) {
425
- if (this.done) return;
426
- if (this.isComplete(event)) {
427
- this.done = true;
428
- this.resolveFinalResult(this.extractResult(event));
429
- }
430
- const waiter = this.waiting.shift();
431
- if (waiter) waiter({
432
- value: event,
433
- done: false
434
- });
435
- else this.queue.push(event);
436
- }
437
- end(result) {
438
- this.done = true;
439
- if (result !== void 0) this.resolveFinalResult(result);
440
- while (this.waiting.length > 0) this.waiting.shift()({
441
- value: void 0,
442
- done: true
443
- });
444
- }
445
- async *[Symbol.asyncIterator]() {
446
- while (true) if (this.queue.length > 0) yield this.queue.shift();
447
- else if (this.done) return;
448
- else {
449
- const result = await new Promise((resolve) => this.waiting.push(resolve));
450
- if (result.done) return;
451
- yield result.value;
452
- }
453
- }
454
- result() {
455
- return this.finalResultPromise;
456
- }
457
- };
458
- var AssistantMessageEventStream = class extends EventStream {
459
- constructor() {
460
- super((event) => event.type === "done" || event.type === "error", (event) => {
461
- if (event.type === "done") return event.message;
462
- else if (event.type === "error") return event.error;
463
- throw new Error("Unexpected event type for final result");
464
- });
465
- }
466
- };
467
- //#endregion
468
- //#region src/utils/validation.ts
469
- const ajv = new Ajv({
470
- allErrors: true,
471
- strict: false,
472
- coerceTypes: true
473
- });
474
- addFormats(ajv);
475
- /**
476
- * Validates an arbitrary value against a TypeBox schema and returns the coerced value.
477
- */
478
- function validateSchema(schema, value, label) {
479
- const validate = ajv.compile(schema);
480
- const input = structuredClone(value);
481
- if (validate(input)) return input;
482
- const errors = validate.errors?.map((err) => {
483
- return ` - ${err.instancePath ? err.instancePath.substring(1) : err.params?.missingProperty || "root"}: ${err.message}`;
484
- }).join("\n") || "Unknown Validation Error";
485
- throw new Error([
486
- `Validation Failed For ${label}`,
487
- `${errors}\n`,
488
- "Received Value:",
489
- `${JSON.stringify(value, null, 2)}\n`
490
- ].join("\n"));
491
- }
492
- /**
493
- * Validates tool call arguments against the tool's TypeBox schema
494
- */
495
- function validateToolArguments(tool, toolExecution) {
496
- return validateSchema(tool.parameters, toolExecution.rawArgs, `Tool "${toolExecution.name}"`);
497
- }
498
- //#endregion
499
- //#region src/agent/loop.ts
500
- let Loop;
501
- (function(_Loop) {
502
- function createAgentStream() {
503
- return new EventStream((event) => event.type === "agent.end", (event) => event.type === "agent.end" ? event.messages : []);
504
- }
505
- const AgentContextError = _Loop.AgentContextError = NamedError.create("AgentContextError", Type.Object({ message: Type.String() }));
506
- function snapshotAssistantMessage(message) {
507
- return structuredClone(message);
508
- }
509
- function snapshotAssistantPartEvent(message, partIndex) {
510
- const snapshot = snapshotAssistantMessage(message);
511
- const part = snapshot.parts[partIndex];
512
- if (!part) throw new Error(`Assistant message part at index ${partIndex} was not found`);
513
- return {
514
- message: snapshot,
515
- part
516
- };
517
- }
518
- async function emitEvent(emit, event) {
519
- await emit(event);
520
- }
521
- async function streamAssistantResponse(config, context, emit, streamFn, signal) {
522
- let messages = context.messages;
523
- if (config.transformContext) messages = await config.transformContext(messages, signal);
524
- const llmMessages = await config.convertToLlm(messages);
525
- const llmContext = {
526
- systemPrompt: context.systemPrompt,
527
- messages: llmMessages,
528
- tools: context.tools
529
- };
530
- const streamFunction = streamFn || Stream.streamSimple;
531
- const resolvedApiKey = (config.getApiKey ? await config.getApiKey(config.model.provider) : void 0) || config.apiKey;
532
- const events = await streamFunction(config.model, llmContext, {
533
- ...config,
534
- apiKey: resolvedApiKey,
535
- signal
536
- });
537
- let partialMessage = null;
538
- let started = false;
539
- const ensureMessageStarted = async (message) => {
540
- if (started) return;
541
- started = true;
542
- await emitEvent(emit, {
543
- type: "message.start",
544
- message: snapshotAssistantMessage(message)
545
- });
546
- };
547
- const emitMessageUpdate = async (message) => {
548
- await emitEvent(emit, {
549
- type: "message.update",
550
- message: snapshotAssistantMessage(message)
551
- });
552
- };
553
- const emitPartStart = async (message, partIndex) => {
554
- await emitEvent(emit, {
555
- type: "message.part.start",
556
- partIndex,
557
- ...snapshotAssistantPartEvent(message, partIndex)
558
- });
559
- };
560
- const emitPartUpdate = async (message, partIndex) => {
561
- await emitEvent(emit, {
562
- type: "message.part.update",
563
- partIndex,
564
- ...snapshotAssistantPartEvent(message, partIndex),
565
- source: "llm"
566
- });
567
- };
568
- const emitPartEnd = async (message, partIndex) => {
569
- await emitEvent(emit, {
570
- type: "message.part.end",
571
- partIndex,
572
- ...snapshotAssistantPartEvent(message, partIndex)
573
- });
574
- };
575
- for await (const event of events) switch (event.type) {
576
- case "start":
577
- partialMessage = event.partial;
578
- await ensureMessageStarted(partialMessage);
579
- break;
580
- case "text.start":
581
- case "thinking.start":
582
- case "toolcall.start":
583
- partialMessage = event.partial;
584
- await ensureMessageStarted(partialMessage);
585
- await emitPartStart(partialMessage, event.partIndex);
586
- break;
587
- case "text.delta":
588
- case "thinking.delta":
589
- case "toolcall.delta":
590
- partialMessage = event.partial;
591
- await ensureMessageStarted(partialMessage);
592
- await emitPartUpdate(partialMessage, event.partIndex);
593
- break;
594
- case "text.end":
595
- case "thinking.end":
596
- case "toolcall.end":
597
- partialMessage = event.partial;
598
- await ensureMessageStarted(partialMessage);
599
- await emitPartEnd(partialMessage, event.partIndex);
600
- await emitMessageUpdate(partialMessage);
601
- break;
602
- case "done":
603
- await ensureMessageStarted(event.message);
604
- await emitEvent(emit, {
605
- type: "message.end",
606
- message: snapshotAssistantMessage(event.message)
607
- });
608
- return event.message;
609
- case "error":
610
- await ensureMessageStarted(event.error);
611
- await emitEvent(emit, {
612
- type: "message.end",
613
- message: snapshotAssistantMessage(event.error)
614
- });
615
- return event.error;
616
- }
617
- const finalMessage = await events.result();
618
- await ensureMessageStarted(finalMessage);
619
- await emitEvent(emit, {
620
- type: "message.end",
621
- message: snapshotAssistantMessage(finalMessage)
622
- });
623
- return finalMessage;
624
- }
625
- async function executeToolCalls(config, currentContext, message, toolCalls, emit, signal) {
626
- if (config.toolExecution === "sequential") return executeToolCallsSequential(config, currentContext, message, toolCalls, emit, signal);
627
- return executeToolCallsParallel(config, currentContext, message, toolCalls, emit, signal);
628
- }
629
- async function executeToolCallsSequential(config, currentContext, message, toolCalls, emit, signal) {
630
- for (const toolCall of toolCalls) {
631
- const toolCallInFlight = {
632
- callID: toolCall.callID,
633
- name: toolCall.name,
634
- rawArgs: toolCall.arguments
635
- };
636
- await emitEvent(emit, {
637
- type: "tool.execution.start",
638
- ...toolCallInFlight
639
- });
640
- const prepared = await prepareToolCall(config, currentContext, message, toolCallInFlight, signal);
641
- if (prepared.error) {
642
- const error = {
643
- status: "error",
644
- result: {
645
- content: [{
646
- type: "text",
647
- text: prepared.error.message
648
- }],
649
- details: prepared.error.details,
650
- isError: true
651
- }
652
- };
653
- await emitToolCallTerminalResult(toolCall, prepared.runnable, message, emit, error);
654
- } else {
655
- const runnable = {
656
- toolCall,
657
- runnable: prepared.runnable,
658
- tool: prepared.tool
659
- };
660
- const executed = await executeRunnableToolCall(runnable, message, emit, signal);
661
- if (executed.error) {
662
- const error = {
663
- status: "error",
664
- result: {
665
- content: [{
666
- type: "text",
667
- text: executed.error.message
668
- }],
669
- details: executed.error.details,
670
- isError: true
671
- }
672
- };
673
- await emitToolCallTerminalResult(runnable.toolCall, runnable.runnable, message, emit, error);
674
- } else await finalizeExecutedToolCall(config, currentContext, runnable.toolCall, runnable.runnable, message, emit, executed.result);
675
- }
676
- }
677
- }
678
- async function executeToolCallsParallel(config, currentContext, message, toolCalls, emit, signal) {
679
- const runnables = [];
680
- for (const toolCall of toolCalls) {
681
- const toolCallInFlight = {
682
- callID: toolCall.callID,
683
- name: toolCall.name,
684
- rawArgs: toolCall.arguments
685
- };
686
- await emitEvent(emit, {
687
- type: "tool.execution.start",
688
- ...toolCallInFlight
689
- });
690
- const prepared = await prepareToolCall(config, currentContext, message, toolCallInFlight, signal);
691
- if (prepared.error) {
692
- const error = {
693
- status: "error",
694
- result: {
695
- content: [{
696
- type: "text",
697
- text: prepared.error.message
698
- }],
699
- details: prepared.error.details,
700
- isError: true
701
- }
702
- };
703
- await emitToolCallTerminalResult(toolCall, prepared.runnable, message, emit, error);
704
- } else runnables.push({
705
- toolCall,
706
- runnable: prepared.runnable,
707
- tool: prepared.tool
708
- });
709
- }
710
- const runningExecutions = runnables.map((runnable) => ({
711
- runnable,
712
- execution: executeRunnableToolCall(runnable, message, emit, signal)
713
- }));
714
- for (const running of runningExecutions) {
715
- const executed = await running.execution;
716
- const { runnable } = running;
717
- if (executed.error) {
718
- const error = {
719
- status: "error",
720
- result: {
721
- content: [{
722
- type: "text",
723
- text: executed.error.message
724
- }],
725
- details: executed.error.details,
726
- isError: true
727
- }
728
- };
729
- await emitToolCallTerminalResult(runnable.toolCall, runnable.runnable, message, emit, error);
730
- } else await finalizeExecutedToolCall(config, currentContext, runnable.toolCall, runnable.runnable, message, emit, executed.result);
731
- }
732
- }
733
- async function prepareToolCall(config, currentContext, assistantMessage, toolCallInFlight, signal) {
734
- const tool = currentContext.tools?.find((t) => t.name === toolCallInFlight.name);
735
- if (!tool) return {
736
- error: {
737
- kind: "error",
738
- message: `Tool ${toolCallInFlight.name} Not Found`
739
- },
740
- runnable: toolCallInFlight
741
- };
742
- try {
743
- toolCallInFlight.args = validateToolArguments(tool, toolCallInFlight);
744
- if (config.beforeToolExecution) {
745
- const beforeResult = await config.beforeToolExecution({
746
- context: currentContext,
747
- assistantMessage,
748
- toolCall: toolCallInFlight
749
- }, signal);
750
- if (beforeResult?.block) return {
751
- error: {
752
- kind: "blocked",
753
- message: beforeResult?.reason || "Tool Execution was Blocked",
754
- details: beforeResult?.details
755
- },
756
- runnable: toolCallInFlight
757
- };
758
- }
759
- return {
760
- runnable: toolCallInFlight,
761
- tool
762
- };
763
- } catch (err) {
764
- return {
765
- error: {
766
- kind: "error",
767
- message: err instanceof Error ? err.message : String(err)
768
- },
769
- runnable: toolCallInFlight
770
- };
771
- }
772
- }
773
- async function executeRunnableToolCall(toolCallRunnable, message, emit, signal) {
774
- const { toolCall, runnable, tool } = toolCallRunnable;
775
- const { partIndex, ...pendingPart } = toolCall;
776
- try {
777
- return { result: await tool.execute(runnable.callID, runnable.args, signal, async (result) => {
778
- const runningResult = {
779
- status: "running",
780
- partial: result.partial
781
- };
782
- await emitEvent(emit, {
783
- type: "tool.execution.update",
784
- ...runnable,
785
- ...runningResult
786
- });
787
- const runningPart = {
788
- ...pendingPart,
789
- ...runningResult
790
- };
791
- message.parts[partIndex] = runningPart;
792
- await emitEvent(emit, {
793
- type: "message.part.update",
794
- message,
795
- partIndex,
796
- part: runningPart,
797
- source: "tool"
798
- });
799
- }) };
800
- } catch (err) {
801
- return { error: { message: err instanceof Error ? err.message : String(err) } };
802
- }
803
- }
804
- async function emitToolCallTerminalResult(toolCall, runnable, message, emit, result) {
805
- const { partIndex, ...pendingPart } = toolCall;
806
- const { time } = pendingPart;
807
- await emitEvent(emit, {
808
- type: "tool.execution.end",
809
- ...runnable,
810
- ...result
811
- });
812
- const part = {
813
- ...pendingPart,
814
- ...result,
815
- time: {
816
- start: time.start,
817
- end: Date.now()
818
- }
819
- };
820
- message.parts[partIndex] = part;
821
- await emitEvent(emit, {
822
- type: "message.part.start",
823
- message,
824
- partIndex,
825
- part
826
- });
827
- await emitEvent(emit, {
828
- type: "message.part.end",
829
- message,
830
- partIndex,
831
- part
832
- });
833
- await emitEvent(emit, {
834
- type: "message.update",
835
- message
836
- });
837
- }
838
- async function finalizeExecutedToolCall(config, currentContext, toolCall, runnable, message, emit, result, signal) {
839
- if (config.afterToolExecution) {
840
- const afterResult = await config.afterToolExecution({
841
- context: currentContext,
842
- assistantMessage: message,
843
- toolCall: runnable,
844
- result
845
- }, signal);
846
- if (afterResult) {
847
- await emitToolCallTerminalResult(toolCall, runnable, message, emit, afterResult);
848
- return;
849
- }
850
- }
851
- await emitToolCallTerminalResult(toolCall, runnable, message, emit, result);
852
- }
853
- /**
854
- * Main core loop shared by run and runContinue.
855
- */
856
- async function runLoop(config, currentContext, newMessages, emit, streamFn, signal) {
857
- let firstTurn = true;
858
- let pendingMessages = await config.getSteeringMessages?.() || [];
859
- while (true) {
860
- let hasMoreToolCalls = true;
861
- while (hasMoreToolCalls || pendingMessages.length > 0) {
862
- if (!firstTurn) await emitEvent(emit, { type: "turn.start" });
863
- else firstTurn = false;
864
- if (pendingMessages.length > 0) {
865
- for (const message of pendingMessages) {
866
- await emitEvent(emit, {
867
- type: "message.start",
868
- message
869
- });
870
- for (const [partIndex, part] of message.parts.entries()) {
871
- await emitEvent(emit, {
872
- type: "message.part.start",
873
- message,
874
- partIndex,
875
- part
876
- });
877
- await emitEvent(emit, {
878
- type: "message.part.end",
879
- message,
880
- partIndex,
881
- part
882
- });
883
- await emitEvent(emit, {
884
- type: "message.update",
885
- message
886
- });
887
- }
888
- await emitEvent(emit, {
889
- type: "message.end",
890
- message
891
- });
892
- currentContext.messages.push(message);
893
- newMessages.push(message);
894
- }
895
- pendingMessages = [];
896
- }
897
- const message = await streamAssistantResponse(config, currentContext, emit, streamFn, signal);
898
- if (message.stopReason === "error" || message.stopReason === "aborted") {
899
- currentContext.messages.push(message);
900
- newMessages.push(message);
901
- await emitEvent(emit, {
902
- type: "turn.end",
903
- message
904
- });
905
- await emitEvent(emit, {
906
- type: "agent.end",
907
- messages: newMessages
908
- });
909
- return;
910
- }
911
- const toolCalls = message.parts.map((part, index) => ({
912
- ...part,
913
- partIndex: index
914
- })).filter((part) => part.type === "toolCall" && part.status === "pending");
915
- hasMoreToolCalls = toolCalls.length > 0;
916
- if (hasMoreToolCalls) await executeToolCalls(config, currentContext, message, toolCalls, emit, signal);
917
- currentContext.messages.push(message);
918
- newMessages.push(message);
919
- await emitEvent(emit, {
920
- type: "turn.end",
921
- message
922
- });
923
- pendingMessages = await config.getSteeringMessages?.() || [];
924
- }
925
- const followUpMessages = await config.getFollowUpMessages?.() || [];
926
- if (followUpMessages.length > 0) {
927
- pendingMessages = followUpMessages;
928
- continue;
929
- }
930
- break;
931
- }
932
- await emitEvent(emit, {
933
- type: "agent.end",
934
- messages: newMessages
935
- });
936
- }
937
- async function runAgentLoop(config, context, prompts, emit, streamFn, signal) {
938
- /**
939
- * maintain 2 arrays to represent state, `context` the actual full state sent to the LLM,
940
- * `newMessages` for intermediate current loop state.
941
- * think of newMessages as subset of context.messages for the running current loop.
942
- * they do move at the same speed staring with initial user prompts.
943
- * */
944
- const newMessages = [...prompts];
945
- const currentContext = {
946
- ...context,
947
- messages: [...context.messages, ...prompts]
948
- };
949
- await emitEvent(emit, { type: "agent.start" });
950
- await emitEvent(emit, { type: "turn.start" });
951
- for (const prompt of prompts) {
952
- await emitEvent(emit, {
953
- type: "message.start",
954
- message: prompt
955
- });
956
- for (const [partIndex, part] of prompt.parts.entries()) {
957
- await emitEvent(emit, {
958
- type: "message.part.start",
959
- message: prompt,
960
- partIndex,
961
- part
962
- });
963
- await emitEvent(emit, {
964
- type: "message.part.end",
965
- message: prompt,
966
- partIndex,
967
- part
968
- });
969
- }
970
- await emitEvent(emit, {
971
- type: "message.end",
972
- message: prompt
973
- });
974
- }
975
- await runLoop(config, currentContext, newMessages, emit, streamFn, signal);
976
- return newMessages;
977
- }
978
- _Loop.runAgentLoop = runAgentLoop;
979
- async function runAgentLoopContinue(config, context, emit, streamFn, signal) {
980
- if (context.messages.length === 0) throw new AgentContextError({ message: "connot continue: no messages in context" });
981
- if (context.messages[context.messages.length - 1]?.role === "assistant") throw new AgentContextError({ message: "cannot continue from message role: assistant" });
982
- const newMessages = [];
983
- const currentContext = { ...context };
984
- await emitEvent(emit, { type: "agent.start" });
985
- await emitEvent(emit, { type: "turn.start" });
986
- await runLoop(config, currentContext, newMessages, emit, streamFn, signal);
987
- return newMessages;
988
- }
989
- _Loop.runAgentLoopContinue = runAgentLoopContinue;
990
- function run(config, context, prompts, streamFn, signal) {
991
- const stream = createAgentStream();
992
- runAgentLoop(config, context, prompts, async (event) => {
993
- stream.push(event);
994
- }, streamFn, signal).then((messages) => {
995
- stream.end(messages);
996
- });
997
- return stream;
998
- }
999
- _Loop.run = run;
1000
- function runContinue(config, context, streamFn, signal) {
1001
- const stream = createAgentStream();
1002
- runAgentLoopContinue(config, context, async (event) => {
1003
- stream.push(event);
1004
- }, streamFn, signal).then((messages) => {
1005
- stream.end(messages);
1006
- });
1007
- return stream;
1008
- }
1009
- _Loop.runContinue = runContinue;
1010
- })(Loop || (Loop = {}));
1011
- //#endregion
1012
- //#region src/agent.ts
1013
- const agent = {
1014
- loop: Loop.run,
1015
- loopContinue: Loop.runContinue
1016
- };
1017
- //#endregion
1018
- //#region src/message/message.ts
1019
- let Message;
1020
- (function(_Message) {
1021
- const TextContentSchema = _Message.TextContentSchema = Type.Object({
1022
- type: Type.Literal("text"),
1023
- text: Type.String(),
1024
- textSignature: Type.Optional(Type.String())
1025
- });
1026
- const ImageContentSchema = _Message.ImageContentSchema = Type.Object({
1027
- type: Type.Literal("image"),
1028
- data: Type.String(),
1029
- mimeType: Type.String()
1030
- });
1031
- const ThinkingContentSchema = _Message.ThinkingContentSchema = Type.Object({
1032
- type: Type.Literal("thinking"),
1033
- thinking: Type.String(),
1034
- thinkingSignature: Type.Optional(Type.String()),
1035
- redacted: Type.Optional(Type.Boolean())
1036
- });
1037
- const ToolCallBaseSchema = _Message.ToolCallBaseSchema = Type.Object({
1038
- type: Type.Literal("toolCall"),
1039
- callID: Type.String(),
1040
- name: Type.String(),
1041
- arguments: Type.Record(Type.String(), Type.Any()),
1042
- thoughtSignature: Type.Optional(Type.String()),
1043
- time: Type.Object({
1044
- start: Type.Number(),
1045
- end: Type.Number()
1046
- })
1047
- });
1048
- const ToolSuccessResult = _Message.ToolSuccessResult = Type.Object({
1049
- content: Type.Array(Type.Union([TextContentSchema, ImageContentSchema])),
1050
- details: Type.Optional(Type.Any()),
1051
- isError: Type.Literal(false)
1052
- });
1053
- const ToolErrorResult = _Message.ToolErrorResult = Type.Object({
1054
- content: Type.Array(Type.Union([TextContentSchema, ImageContentSchema])),
1055
- details: Type.Optional(Type.Any()),
1056
- isError: Type.Literal(true)
1057
- });
1058
- const ToolRunningPartial = _Message.ToolRunningPartial = Type.Object({
1059
- content: Type.Optional(Type.Array(Type.Union([TextContentSchema, ImageContentSchema]))),
1060
- details: Type.Optional(Type.Any())
1061
- });
1062
- const ToolStatusEnum = _Message.ToolStatusEnum = {
1063
- pending: "pending",
1064
- running: "running",
1065
- completed: "completed",
1066
- error: "error",
1067
- skipped: "skipped",
1068
- aborted: "aborted"
1069
- };
1070
- const ToolRunningSchema = _Message.ToolRunningSchema = Type.Object({
1071
- status: Type.Literal(ToolStatusEnum.running),
1072
- partial: Type.Optional(ToolRunningPartial)
1073
- });
1074
- const ToolCompletedSchema = _Message.ToolCompletedSchema = Type.Object({
1075
- status: Type.Literal(ToolStatusEnum.completed),
1076
- result: ToolSuccessResult
1077
- });
1078
- const ToolErrorSchema = _Message.ToolErrorSchema = Type.Object({
1079
- status: Type.Literal(ToolStatusEnum.error),
1080
- result: ToolErrorResult
1081
- });
1082
- const ToolSkippedSchema = _Message.ToolSkippedSchema = Type.Object({
1083
- status: Type.Literal(ToolStatusEnum.skipped),
1084
- result: ToolErrorResult
1085
- });
1086
- const ToolAbortedSchema = _Message.ToolAbortedSchema = Type.Object({
1087
- status: Type.Literal(ToolStatusEnum.aborted),
1088
- result: ToolErrorResult
1089
- });
1090
- const ToolCallPendingPart = Type.Intersect([ToolCallBaseSchema, Type.Object({ status: Type.Literal(ToolStatusEnum.pending) })]);
1091
- const ToolCallRunningPart = Type.Composite([ToolCallBaseSchema, ToolRunningSchema]);
1092
- const ToolCallCompletedPart = Type.Composite([ToolCallBaseSchema, ToolCompletedSchema]);
1093
- const ToolCallErrorPart = Type.Composite([ToolCallBaseSchema, ToolErrorSchema]);
1094
- const ToolCallSkippedPart = Type.Composite([ToolCallBaseSchema, ToolSkippedSchema]);
1095
- const ToolCallAbortedPart = Type.Composite([ToolCallBaseSchema, ToolAbortedSchema]);
1096
- const ToolCallSchema = _Message.ToolCallSchema = Type.Union([
1097
- ToolCallPendingPart,
1098
- ToolCallRunningPart,
1099
- ToolCallCompletedPart,
1100
- ToolCallErrorPart,
1101
- ToolCallSkippedPart,
1102
- ToolCallAbortedPart
1103
- ]);
1104
- const UsageSchema = _Message.UsageSchema = Type.Object({
1105
- input: Type.Number(),
1106
- output: Type.Number(),
1107
- cacheRead: Type.Number(),
1108
- cacheWrite: Type.Number(),
1109
- totalTokens: Type.Number(),
1110
- cost: Type.Object({
1111
- input: Type.Number(),
1112
- output: Type.Number(),
1113
- cacheRead: Type.Number(),
1114
- cacheWrite: Type.Number(),
1115
- total: Type.Number()
1116
- })
1117
- });
1118
- const StopReasonSchema = _Message.StopReasonSchema = Type.Union([
1119
- Type.Literal("stop"),
1120
- Type.Literal("length"),
1121
- Type.Literal("toolUse"),
1122
- Type.Literal("error"),
1123
- Type.Literal("aborted")
1124
- ]);
1125
- const UserMessageSchema = _Message.UserMessageSchema = Type.Object({
1126
- messageId: Type.String(),
1127
- role: Type.Literal("user"),
1128
- time: Type.Object({ created: Type.Number() }),
1129
- parts: Type.Array(Type.Union([TextContentSchema, ImageContentSchema]))
1130
- });
1131
- const AssistantMessageSchema = _Message.AssistantMessageSchema = Type.Object({
1132
- role: Type.Literal("assistant"),
1133
- protocol: Model.KnownProtocolSchema,
1134
- provider: Provider.Info,
1135
- model: Type.String(),
1136
- usage: UsageSchema,
1137
- stopReason: StopReasonSchema,
1138
- errorMessage: Type.Optional(Type.String()),
1139
- time: Type.Object({
1140
- created: Type.Number(),
1141
- completed: Type.Number()
1142
- }),
1143
- parts: Type.Array(Type.Union([
1144
- TextContentSchema,
1145
- ImageContentSchema,
1146
- ThinkingContentSchema,
1147
- ToolCallSchema
1148
- ])),
1149
- responseId: Type.Optional(Type.String()),
1150
- messageId: Type.String()
1151
- });
1152
- const MessageSchema = _Message.MessageSchema = Type.Union([UserMessageSchema, AssistantMessageSchema]);
1153
- function createMessageId() {
1154
- return uuidv7();
1155
- }
1156
- _Message.createMessageId = createMessageId;
1157
- function createUserMessage(message) {
1158
- const { messageId = createMessageId(), ...rest } = message;
1159
- return {
1160
- messageId,
1161
- ...rest
1162
- };
1163
- }
1164
- _Message.createUserMessage = createUserMessage;
1165
- function createAssistantMessage(message) {
1166
- const { messageId = createMessageId(), ...rest } = message;
1167
- return {
1168
- messageId,
1169
- ...rest
1170
- };
1171
- }
1172
- _Message.createAssistantMessage = createAssistantMessage;
1173
- const ToolSchema = _Message.ToolSchema = Type.Object({
1174
- name: Type.String(),
1175
- description: Type.String(),
1176
- parameters: Type.Unsafe()
1177
- });
1178
- function defineTool(tool) {
1179
- return tool;
1180
- }
1181
- _Message.defineTool = defineTool;
1182
- _Message.ContextSchema = Type.Object({
1183
- systemPrompt: Type.Optional(Type.String()),
1184
- messages: Type.Array(MessageSchema),
1185
- tools: Type.Optional(Type.Array(ToolSchema))
1186
- });
1187
- function transformMessages(messages, model, normalizeToolCallId) {
1188
- const toolCallIDMap = /* @__PURE__ */ new Map();
1189
- const transformed = [];
1190
- for (const msg of messages) {
1191
- if (msg.role === "user") {
1192
- transformed.push(msg);
1193
- continue;
1194
- }
1195
- const assistantMsg = msg;
1196
- const isSameModel = assistantMsg.provider.id === model.provider.id && assistantMsg.protocol === model.protocol && assistantMsg.model === model.id;
1197
- const parts = assistantMsg.parts.flatMap((block) => {
1198
- if (block.type === "thinking") {
1199
- if (isSameModel && block.thinkingSignature) return block;
1200
- if (!block.thinking || block.thinking.trim() === "") return [];
1201
- if (isSameModel) return block;
1202
- return {
1203
- type: "text",
1204
- text: block.thinking
1205
- };
1206
- }
1207
- if (block.type === "text") {
1208
- if (isSameModel) return block;
1209
- return {
1210
- type: "text",
1211
- text: block.text
1212
- };
1213
- }
1214
- if (block.type === "toolCall") {
1215
- const toolCall = block;
1216
- let normalizedToolCall = toolCall;
1217
- if (!isSameModel && toolCall.thoughtSignature) {
1218
- normalizedToolCall = { ...toolCall };
1219
- delete normalizedToolCall.thoughtSignature;
1220
- }
1221
- const normalizedID = toolCallIDMap.get(block.callID);
1222
- if (normalizedID && normalizedID !== block.callID) normalizedToolCall = {
1223
- ...normalizedToolCall,
1224
- callID: normalizedID
1225
- };
1226
- else if (!isSameModel && normalizeToolCallId) {
1227
- const nextID = normalizeToolCallId(toolCall.callID, model, assistantMsg);
1228
- if (nextID !== toolCall.callID) {
1229
- toolCallIDMap.set(toolCall.callID, nextID);
1230
- normalizedToolCall = {
1231
- ...normalizedToolCall,
1232
- callID: nextID
1233
- };
1234
- }
1235
- }
1236
- return normalizedToolCall;
1237
- }
1238
- return block;
1239
- });
1240
- transformed.push({
1241
- ...assistantMsg,
1242
- parts
1243
- });
1244
- }
1245
- return transformed;
1246
- }
1247
- _Message.transformMessages = transformMessages;
1248
- })(Message || (Message = {}));
1249
- //#endregion
1250
- //#region src/utils/jsonparse.ts
1251
- /**
1252
- * @description Helper function to parse partial jsons during LLM streaming.
1253
- */
1254
- /**
1255
- * Attempts to parse potentially incomplete JSON during streaming.
1256
- * Always returns a valid object, even if the JSON is incomplete.
1257
- *
1258
- * @param partialJson The partial JSON string from streaming
1259
- * @returns Parsed object or empty object if parsing fails
1260
- */
1261
- function parseStreamingJson(partialJson) {
1262
- if (!partialJson || partialJson.trim() === "") return {};
1263
- try {
1264
- return JSON.parse(partialJson);
1265
- } catch {
1266
- try {
1267
- return parse(partialJson) ?? {};
1268
- } catch {
1269
- return {};
1270
- }
1271
- }
1272
- }
1273
- //#endregion
1274
- //#region src/utils/sanitize.ts
1275
- /**
1276
- * Removes unpaired Unicode surrogate characters from a string.
1277
- *
1278
- * Unpaired surrogates (high surrogates 0xD800-0xDBFF without matching low surrogates 0xDC00-0xDFFF,
1279
- * or vice versa) cause JSON serialization errors in many API providers.
1280
- *
1281
- * Valid emoji and other characters outside the Basic Multilingual Plane use properly paired
1282
- * surrogates and will NOT be affected by this function.
1283
- *
1284
- * @param text - The text to sanitize
1285
- * @returns The sanitized text with unpaired surrogates removed
1286
- *
1287
- * @example
1288
- * // Valid emoji (properly paired surrogates) are preserved
1289
- * sanitizeSurrogates("Hello 🙈 World") // => "Hello 🙈 World"
1290
- *
1291
- * // Unpaired high surrogate is removed
1292
- * const unpaired = String.fromCharCode(0xD83D); // high surrogate without low
1293
- * sanitizeSurrogates(`Text ${unpaired} here`) // => "Text here"
1294
- */
1295
- function sanitizeSurrogates(text) {
1296
- return text.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, "");
1297
- }
1298
- //#endregion
1299
- //#region src/provider/providers/anthropic/index.ts
1300
- function getEnvApiKey(_provider) {}
1301
- function resolveCacheRetention(cacheRetention) {
1302
- if (cacheRetention) return cacheRetention;
1303
- if (typeof process !== "undefined" && process.env.CODEWORK_CACHE_RETENTION === "long") return "long";
1304
- return "short";
1305
- }
1306
- function getCacheControl(baseUrl, cacheRetention) {
1307
- const retention = resolveCacheRetention(cacheRetention);
1308
- if (retention === "none") return { retention };
1309
- const ttl = retention === "long" && baseUrl.includes("api.anthropic.com") ? "1h" : void 0;
1310
- return {
1311
- retention,
1312
- cacheControl: {
1313
- type: "ephemeral",
1314
- ...ttl && { ttl }
1315
- }
1316
- };
1317
- }
1318
- const claudeCodeVersion = "2.1.2";
1319
- const ccToolLookup = new Map([
1320
- "Read",
1321
- "Write",
1322
- "Edit",
1323
- "Bash",
1324
- "Grep",
1325
- "Glob",
1326
- "AskUserQuestion",
1327
- "EnterPlanMode",
1328
- "ExitPlanMode",
1329
- "KillShell",
1330
- "NotebookEdit",
1331
- "Skill",
1332
- "Task",
1333
- "TaskOutput",
1334
- "TodoWrite",
1335
- "WebFetch",
1336
- "WebSearch"
1337
- ].map((t) => [t.toLowerCase(), t]));
1338
- const toClaudeCodeName = (name) => ccToolLookup.get(name.toLowerCase()) ?? name;
1339
- const fromClaudeCodeName = (name, tools) => {
1340
- if (tools && tools.length > 0) {
1341
- const lowerName = name.toLowerCase();
1342
- const matchedTool = tools.find((tool) => tool.name.toLowerCase() === lowerName);
1343
- if (matchedTool) return matchedTool.name;
1344
- }
1345
- return name;
1346
- };
1347
- function convertContentBlocks(content) {
1348
- if (!content.some((c) => c.type === "image")) return sanitizeSurrogates(content.map((c) => c.text).join("\n"));
1349
- const blocks = content.map((block) => {
1350
- if (block.type === "text") return {
1351
- type: "text",
1352
- text: sanitizeSurrogates(block.text)
1353
- };
1354
- return {
1355
- type: "image",
1356
- source: {
1357
- type: "base64",
1358
- media_type: block.mimeType,
1359
- data: block.data
1360
- }
1361
- };
1362
- });
1363
- if (!blocks.some((b) => b.type === "text")) blocks.unshift({
1364
- type: "text",
1365
- text: "(see attached image)"
1366
- });
1367
- return blocks;
1368
- }
1369
- function mergeHeaders(...headerSources) {
1370
- const merged = {};
1371
- for (const headers of headerSources) if (headers) Object.assign(merged, headers);
1372
- return merged;
1373
- }
1374
- function isOAuthToken(apiKey) {
1375
- return apiKey.includes("sk-ant-oat");
1376
- }
1377
- function createClient(model, apiKey, interleavedThinking, optionsHeaders) {
1378
- const betaFeatures = ["fine-grained-tool-streaming-2025-05-14"];
1379
- if (interleavedThinking) betaFeatures.push("interleaved-thinking-2025-05-14");
1380
- if (isOAuthToken(apiKey)) {
1381
- const defaultHeaders = mergeHeaders({
1382
- accept: "application/json",
1383
- "anthropic-dangerous-direct-browser-access": "true",
1384
- "anthropic-beta": `claude-code-20250219,oauth-2025-04-20,${betaFeatures.join(",")}`,
1385
- "user-agent": `claude-cli/${claudeCodeVersion} (external, cli)`,
1386
- "x-app": "cli"
1387
- }, model.headers, optionsHeaders);
1388
- return {
1389
- client: new Anthropic({
1390
- apiKey: null,
1391
- authToken: apiKey,
1392
- baseURL: model.baseUrl,
1393
- defaultHeaders,
1394
- dangerouslyAllowBrowser: true
1395
- }),
1396
- isOAuthToken: true
1397
- };
1398
- }
1399
- const defaultHeaders = mergeHeaders({
1400
- accept: "application/json",
1401
- "anthropic-dangerous-direct-browser-access": "true",
1402
- "anthropic-beta": betaFeatures.join(",")
1403
- }, model.headers, optionsHeaders);
1404
- return {
1405
- client: new Anthropic({
1406
- apiKey,
1407
- baseURL: model.baseUrl,
1408
- dangerouslyAllowBrowser: true,
1409
- defaultHeaders
1410
- }),
1411
- isOAuthToken: false
1412
- };
1413
- }
1414
- function normalizeToolCallId(id) {
1415
- return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
1416
- }
1417
- function assertReplayableAssistant(message) {
1418
- let sawToolCall = false;
1419
- let sawNonTerminalToolCall = false;
1420
- let sawTerminalToolCall = false;
1421
- for (const part of message.parts) {
1422
- if (part.type === "image") throw new Error("Anthropic replay does not support assistant image parts");
1423
- if (part.type === "toolCall") {
1424
- sawToolCall = true;
1425
- if (part.status === "pending" || part.status === "running") sawNonTerminalToolCall = true;
1426
- else sawTerminalToolCall = true;
1427
- continue;
1428
- }
1429
- if (sawToolCall) throw new Error("Assistant parts after a toolCall are not replayable for Anthropic");
1430
- }
1431
- if (sawToolCall && message.stopReason !== "toolUse") throw new Error("Assistant message with tool calls must have stopReason 'toolUse'");
1432
- if (!sawToolCall && message.stopReason === "toolUse") throw new Error("Assistant message with stopReason 'toolUse' must contain at least one toolCall");
1433
- if (sawTerminalToolCall && sawNonTerminalToolCall) throw new Error("Assistant message cannot mix terminal and non-terminal toolCall states");
1434
- }
1435
- function buildSyntheticToolResult(block) {
1436
- return {
1437
- type: "tool_result",
1438
- tool_use_id: block.callID,
1439
- content: convertContentBlocks([{
1440
- type: "text",
1441
- text: [
1442
- "<result>",
1443
- "No Result Provided",
1444
- "</result>"
1445
- ].join("\n")
1446
- }]),
1447
- is_error: true
1448
- };
1449
- }
1450
- function convertMessages(messages, model, isOAuthToken, cacheControl) {
1451
- const params = [];
1452
- const transformedMessages = Message.transformMessages(messages, model, normalizeToolCallId);
1453
- for (const msg of transformedMessages) {
1454
- if (msg.role === "user") {
1455
- const blocks = msg.parts.map((item) => {
1456
- if (item.type === "text") return {
1457
- type: "text",
1458
- text: sanitizeSurrogates(item.text)
1459
- };
1460
- return {
1461
- type: "image",
1462
- source: {
1463
- type: "base64",
1464
- media_type: item.mimeType,
1465
- data: item.data
1466
- }
1467
- };
1468
- });
1469
- let filteredBlocks = !model.input.includes("image") ? blocks.filter((b) => b.type !== "image") : blocks;
1470
- filteredBlocks = filteredBlocks.filter((b) => b.type === "text" ? b.text.trim().length > 0 : true);
1471
- if (filteredBlocks.length === 0) continue;
1472
- params.push({
1473
- role: "user",
1474
- content: filteredBlocks
1475
- });
1476
- continue;
1477
- }
1478
- if (msg.stopReason === "error" || msg.stopReason === "aborted") continue;
1479
- assertReplayableAssistant(msg);
1480
- const assistantBlocks = [];
1481
- const toolResults = [];
1482
- for (const block of msg.parts) if (block.type === "text") {
1483
- if (block.text.trim().length === 0) continue;
1484
- assistantBlocks.push({
1485
- type: "text",
1486
- text: sanitizeSurrogates(block.text)
1487
- });
1488
- } else if (block.type === "thinking") {
1489
- if (block.thinking.trim().length === 0) continue;
1490
- if (!block.thinkingSignature || block.thinkingSignature.trim().length === 0) assistantBlocks.push({
1491
- type: "text",
1492
- text: sanitizeSurrogates(block.thinking)
1493
- });
1494
- else assistantBlocks.push({
1495
- type: "thinking",
1496
- thinking: sanitizeSurrogates(block.thinking),
1497
- signature: block.thinkingSignature
1498
- });
1499
- } else if (block.type === "toolCall") {
1500
- assistantBlocks.push({
1501
- type: "tool_use",
1502
- id: block.callID,
1503
- name: isOAuthToken ? toClaudeCodeName(block.name) : block.name,
1504
- input: block.arguments ?? {}
1505
- });
1506
- if (block.status === "pending" || block.status === "running") toolResults.push(buildSyntheticToolResult(block));
1507
- else toolResults.push({
1508
- type: "tool_result",
1509
- tool_use_id: block.callID,
1510
- content: convertContentBlocks(block.result.content),
1511
- is_error: block.result.isError
1512
- });
1513
- }
1514
- if (assistantBlocks.length > 0) params.push({
1515
- role: "assistant",
1516
- content: assistantBlocks
1517
- });
1518
- if (toolResults.length > 0) params.push({
1519
- role: "user",
1520
- content: toolResults
1521
- });
1522
- }
1523
- if (cacheControl && params.length > 0) {
1524
- const lastMessage = params[params.length - 1];
1525
- if (lastMessage.role === "user") {
1526
- if (Array.isArray(lastMessage.content)) {
1527
- const lastBlock = lastMessage.content[lastMessage.content.length - 1];
1528
- if (lastBlock && (lastBlock.type === "text" || lastBlock.type === "image" || lastBlock.type === "tool_result")) lastBlock.cache_control = cacheControl;
1529
- } else if (typeof lastMessage.content === "string") lastMessage.content = [{
1530
- type: "text",
1531
- text: lastMessage.content,
1532
- cache_control: cacheControl
1533
- }];
1534
- }
1535
- }
1536
- return params;
1537
- }
1538
- function convertTools(tools, isOAuthToken) {
1539
- if (!tools) return [];
1540
- return tools.map((tool) => {
1541
- const jsonSchema = tool.parameters;
1542
- return {
1543
- name: isOAuthToken ? toClaudeCodeName(tool.name) : tool.name,
1544
- description: tool.description,
1545
- input_schema: {
1546
- type: "object",
1547
- properties: jsonSchema.properties || {},
1548
- required: jsonSchema.required || []
1549
- }
1550
- };
1551
- });
1552
- }
1553
- function mapStopReason(reason) {
1554
- switch (reason) {
1555
- case "end_turn": return "stop";
1556
- case "max_tokens": return "length";
1557
- case "tool_use": return "toolUse";
1558
- case "pause_turn":
1559
- case "stop_sequence": return "stop";
1560
- case "refusal":
1561
- case "sensitive": return "error";
1562
- default: throw new Error("Unhandled Anthropic stop reason");
1563
- }
1564
- }
1565
- function buildParams(model, context, isOAuthToken, options) {
1566
- const { cacheControl } = getCacheControl(model.baseUrl, options?.cacheRetention);
1567
- const params = {
1568
- model: model.id,
1569
- messages: convertMessages(context.messages, model, isOAuthToken, cacheControl),
1570
- max_tokens: options?.maxTokens || model.maxTokens / 3 | 0,
1571
- stream: true
1572
- };
1573
- if (isOAuthToken) {
1574
- params.system = [{
1575
- type: "text",
1576
- text: "You are Claude Code, Anthropic's official CLI for Claude.",
1577
- ...cacheControl ? { cache_control: cacheControl } : {}
1578
- }];
1579
- if (context.systemPrompt) params.system.push({
1580
- type: "text",
1581
- text: sanitizeSurrogates(context.systemPrompt),
1582
- ...cacheControl ? { cache_control: cacheControl } : {}
1583
- });
1584
- } else if (context.systemPrompt) params.system = [{
1585
- type: "text",
1586
- text: sanitizeSurrogates(context.systemPrompt),
1587
- ...cacheControl ? { cache_control: cacheControl } : {}
1588
- }];
1589
- if (options?.temperature !== void 0 && !options?.thinkingEnabled) params.temperature = options.temperature;
1590
- if (context.tools) params.tools = convertTools(context.tools, isOAuthToken);
1591
- if (options?.thinkingEnabled && model.reasoning) params.thinking = {
1592
- type: "enabled",
1593
- budget_tokens: options.thinkingBudgetTokens || 1024
1594
- };
1595
- if (options?.metadata) {
1596
- const userId = options.metadata.user_id;
1597
- if (typeof userId === "string") params.metadata = { user_id: userId };
1598
- }
1599
- if (options?.toolChoice) params.tool_choice = typeof options.toolChoice === "string" ? { type: options.toolChoice } : options.toolChoice;
1600
- return params;
1601
- }
1602
- const streamAnthropic = (model, context, options) => {
1603
- const stream = new AssistantMessageEventStream();
1604
- (async () => {
1605
- const output = Message.createAssistantMessage({
1606
- role: "assistant",
1607
- parts: [],
1608
- protocol: model.protocol,
1609
- provider: model.provider,
1610
- model: model.id,
1611
- usage: {
1612
- input: 0,
1613
- output: 0,
1614
- cacheRead: 0,
1615
- cacheWrite: 0,
1616
- totalTokens: 0,
1617
- cost: {
1618
- input: 0,
1619
- output: 0,
1620
- cacheRead: 0,
1621
- cacheWrite: 0,
1622
- total: 0
1623
- }
1624
- },
1625
- stopReason: "stop",
1626
- time: {
1627
- created: Date.now(),
1628
- completed: Date.now()
1629
- }
1630
- });
1631
- try {
1632
- let client;
1633
- let isOAuthToken;
1634
- if (options?.client) {
1635
- client = options.client;
1636
- isOAuthToken = false;
1637
- } else {
1638
- const apiKey = options?.apiKey ?? getEnvApiKey(model.provider);
1639
- if (!apiKey) throw new Error(`No API key for provider: ${model.provider.id}`);
1640
- const created = createClient(model, apiKey, options?.interleavedThinking ?? true, options?.headers);
1641
- client = created.client;
1642
- isOAuthToken = created.isOAuthToken;
1643
- }
1644
- let params = buildParams(model, context, isOAuthToken, options);
1645
- const nextParams = await options?.onPayload?.(params, model);
1646
- if (nextParams !== void 0) params = nextParams;
1647
- const anthropicStream = client.messages.stream({
1648
- ...params,
1649
- stream: true
1650
- }, { signal: options?.signal });
1651
- stream.push({
1652
- type: "start",
1653
- partial: output
1654
- });
1655
- const blocks = output.parts;
1656
- for await (const event of anthropicStream) if (event.type === "message_start") {
1657
- output.responseId = event.message.id;
1658
- output.usage.input = event.message.usage.input_tokens || 0;
1659
- output.usage.output = event.message.usage.output_tokens || 0;
1660
- output.usage.cacheRead = event.message.usage.cache_read_input_tokens || 0;
1661
- output.usage.cacheWrite = event.message.usage.cache_creation_input_tokens || 0;
1662
- output.usage.totalTokens = output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
1663
- Model.calculateCost(model, output.usage);
1664
- } else if (event.type === "content_block_start") {
1665
- if (event.content_block.type === "text") {
1666
- const block = {
1667
- type: "text",
1668
- text: "",
1669
- index: event.index
1670
- };
1671
- output.parts.push(block);
1672
- stream.push({
1673
- type: "text.start",
1674
- partIndex: output.parts.length - 1,
1675
- partial: output
1676
- });
1677
- } else if (event.content_block.type === "thinking") {
1678
- const block = {
1679
- type: "thinking",
1680
- thinking: "",
1681
- thinkingSignature: "",
1682
- index: event.index
1683
- };
1684
- output.parts.push(block);
1685
- stream.push({
1686
- type: "thinking.start",
1687
- partIndex: output.parts.length - 1,
1688
- partial: output
1689
- });
1690
- } else if (event.content_block.type === "redacted_thinking") {
1691
- const block = {
1692
- type: "thinking",
1693
- thinking: "[Reasoning redacted]",
1694
- thinkingSignature: event.content_block.data,
1695
- redacted: true,
1696
- index: event.index
1697
- };
1698
- output.parts.push(block);
1699
- stream.push({
1700
- type: "thinking.start",
1701
- partIndex: output.parts.length - 1,
1702
- partial: output
1703
- });
1704
- } else if (event.content_block.type === "tool_use") {
1705
- const block = {
1706
- type: "toolCall",
1707
- callID: event.content_block.id,
1708
- name: isOAuthToken ? fromClaudeCodeName(event.content_block.name, context.tools) : event.content_block.name,
1709
- arguments: event.content_block.input ?? {},
1710
- partialJson: "",
1711
- status: "pending",
1712
- time: {
1713
- start: Date.now(),
1714
- end: Date.now()
1715
- },
1716
- index: event.index
1717
- };
1718
- output.parts.push(block);
1719
- stream.push({
1720
- type: "toolcall.start",
1721
- partIndex: output.parts.length - 1,
1722
- partial: output
1723
- });
1724
- }
1725
- } else if (event.type === "content_block_delta") {
1726
- const partIndex = blocks.findIndex((b) => b.index === event.index);
1727
- const block = blocks[partIndex];
1728
- if (!block) continue;
1729
- if (event.delta.type === "text_delta" && block.type === "text") {
1730
- block.text += event.delta.text;
1731
- stream.push({
1732
- type: "text.delta",
1733
- partIndex,
1734
- delta: event.delta.text,
1735
- partial: output
1736
- });
1737
- } else if (event.delta.type === "thinking_delta" && block.type === "thinking") {
1738
- block.thinking += event.delta.thinking;
1739
- stream.push({
1740
- type: "thinking.delta",
1741
- partIndex,
1742
- delta: event.delta.thinking,
1743
- partial: output
1744
- });
1745
- } else if (event.delta.type === "input_json_delta" && block.type === "toolCall") {
1746
- block.partialJson += event.delta.partial_json;
1747
- block.arguments = parseStreamingJson(block.partialJson);
1748
- stream.push({
1749
- type: "toolcall.delta",
1750
- partIndex,
1751
- delta: event.delta.partial_json,
1752
- partial: output
1753
- });
1754
- } else if (event.delta.type === "signature_delta" && block.type === "thinking") {
1755
- block.thinkingSignature = block.thinkingSignature || "";
1756
- block.thinkingSignature += event.delta.signature;
1757
- }
1758
- } else if (event.type === "content_block_stop") {
1759
- const partIndex = blocks.findIndex((b) => b.index === event.index);
1760
- const block = blocks[partIndex];
1761
- if (block) {
1762
- delete block.index;
1763
- if (block.type === "text") stream.push({
1764
- type: "text.end",
1765
- partIndex,
1766
- content: block.text,
1767
- partial: output
1768
- });
1769
- else if (block.type === "thinking") stream.push({
1770
- type: "thinking.end",
1771
- partIndex,
1772
- content: block.thinking,
1773
- partial: output
1774
- });
1775
- else if (block.type === "toolCall") {
1776
- block.arguments = parseStreamingJson(block.partialJson);
1777
- delete block.partialJson;
1778
- stream.push({
1779
- type: "toolcall.end",
1780
- partIndex,
1781
- toolCall: block,
1782
- partial: output
1783
- });
1784
- }
1785
- }
1786
- } else if (event.type === "message_delta") {
1787
- if (event.delta.stop_reason) output.stopReason = mapStopReason(event.delta.stop_reason);
1788
- if (event.usage.input_tokens != null) output.usage.input = event.usage.input_tokens;
1789
- if (event.usage.output_tokens != null) output.usage.output = event.usage.output_tokens;
1790
- if (event.usage.cache_read_input_tokens != null) output.usage.cacheRead = event.usage.cache_read_input_tokens;
1791
- if (event.usage.cache_creation_input_tokens != null) output.usage.cacheWrite = event.usage.cache_creation_input_tokens;
1792
- output.usage.totalTokens = output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
1793
- Model.calculateCost(model, output.usage);
1794
- }
1795
- if (options?.signal?.aborted) throw new Error("Request was aborted");
1796
- if (output.stopReason === "aborted" || output.stopReason === "error") throw new Error("An unknown error occurred");
1797
- output.time.completed = Date.now();
1798
- stream.push({
1799
- type: "done",
1800
- reason: output.stopReason,
1801
- message: output
1802
- });
1803
- stream.end();
1804
- } catch (error) {
1805
- for (const block of output.parts) delete block.index;
1806
- output.time.completed = Date.now();
1807
- output.stopReason = options?.signal?.aborted ? "aborted" : "error";
1808
- output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
1809
- stream.push({
1810
- type: "error",
1811
- reason: output.stopReason,
1812
- error: output
1813
- });
1814
- stream.end();
1815
- }
1816
- })();
1817
- return stream;
1818
- };
1819
- const streamSimpleAnthropic = (model, context, options) => {
1820
- const apiKey = options?.apiKey || getEnvApiKey(model.provider);
1821
- if (!apiKey) throw new Error(`No API key for provider: ${model.provider.id}`);
1822
- const base = Stream.buildBaseOptions(model, options, apiKey);
1823
- if (!options?.reasoning) return streamAnthropic(model, context, {
1824
- ...base,
1825
- thinkingEnabled: false
1826
- });
1827
- const adjusted = Stream.adjustMaxTokensForThinking(base.maxTokens || 0, model.maxTokens, options.reasoning, options.thinkingBudgets);
1828
- return streamAnthropic(model, context, {
1829
- ...base,
1830
- maxTokens: adjusted.maxTokens,
1831
- thinkingEnabled: true,
1832
- thinkingBudgetTokens: adjusted.thinkingBudget
1833
- });
1834
- };
1835
- //#endregion
1836
- //#region src/provider/register.ts
1837
- const providerModules = [["providers/anthropic/index.ts", {
1838
- protocol: Model.KnownProtocolEnum.anthropicMessages,
1839
- stream: streamAnthropic,
1840
- streamSimple: streamSimpleAnthropic
1841
- }]];
1842
- for (const [path, provider] of providerModules) Stream.registerProtocolProvider(provider, path);
1843
- //#endregion
1844
- //#region src/agent/agent.ts
1845
- let Agent;
1846
- (function(_Agent) {
1847
- _Agent.ToolTerminalResultSchema = Type.Union([Message.ToolCompletedSchema, Message.ToolErrorSchema]);
1848
- _Agent.ToolResultSchema = Type.Union([
1849
- Message.ToolRunningSchema,
1850
- Message.ToolCompletedSchema,
1851
- Message.ToolErrorSchema
1852
- ]);
1853
- function defineTool(tool) {
1854
- return tool;
1855
- }
1856
- _Agent.defineTool = defineTool;
1857
- _Agent.ToolCallInFlightSchema = Type.Object({
1858
- callID: Type.String(),
1859
- name: Type.String(),
1860
- rawArgs: Type.Record(Type.String(), Type.Unknown()),
1861
- args: Type.Optional(Type.Unknown())
1862
- });
1863
- const ModelNotFoundErr = _Agent.ModelNotFoundErr = NamedError.create("ModelNotFoundErr", Type.Object({
1864
- message: Type.String(),
1865
- provider: Type.String(),
1866
- model: Type.String()
1867
- }));
1868
- const ModelNotConfiguredErr = _Agent.ModelNotConfiguredErr = NamedError.create("ModelNotConfiguredErr", Type.Object({ message: Type.String() }));
1869
- const AgentInStreamingErr = _Agent.AgentInStreamingErr = NamedError.create("AgentInStreamingErr", Type.Object({
1870
- message: Type.String(),
1871
- name: Type.String()
1872
- }));
1873
- class Instance {
1874
- _state;
1875
- listeners = /* @__PURE__ */ new Set();
1876
- abortController;
1877
- convertToLlm;
1878
- transformContext;
1879
- steeringQueue = [];
1880
- followUpQueue = [];
1881
- steeringMode;
1882
- followUpMode;
1883
- streamFn;
1884
- _sessionId;
1885
- getApiKey;
1886
- _onPayload;
1887
- runningPrompt;
1888
- resolveRunningPrompt;
1889
- _thinkingBudgets;
1890
- _transport;
1891
- _maxRetryDelayMs;
1892
- _toolExecution;
1893
- _beforeToolExecution;
1894
- _afterToolExecution;
1895
- constructor(name, model, opts = {}) {
1896
- this._state = {
1897
- systemPrompt: "",
1898
- thinkingLevel: "off",
1899
- tools: [],
1900
- messages: [],
1901
- isStreaming: false,
1902
- streamMessage: null,
1903
- pendingToolCalls: /* @__PURE__ */ new Set(),
1904
- error: void 0,
1905
- ...opts.initialState,
1906
- name,
1907
- model
1908
- };
1909
- /**
1910
- * Transformation logic to prepare messages for the LLM.
1911
- * * @remarks
1912
- * Currently just forwards messages as-is. Modify this logic when
1913
- * specific formatting or filtering is required.
1914
- * A good example is using user message parts with custom content
1915
- * * @default
1916
- * forwards messages as is.
1917
- */
1918
- this.convertToLlm = opts.convertToLlm || ((messages) => messages);
1919
- this.transformContext = opts.transformContext;
1920
- this.steeringMode = opts.steeringMode || "one-at-a-time";
1921
- this.followUpMode = opts.followUpMode || "one-at-a-time";
1922
- this.streamFn = opts.streamFn || Stream.streamSimple;
1923
- this._sessionId = opts.sessionId;
1924
- this.getApiKey = opts.getApiKey;
1925
- this._onPayload = opts.onPayload;
1926
- this._thinkingBudgets = opts.thinkingBudgets;
1927
- this._transport = opts.transport ?? "sse";
1928
- this._maxRetryDelayMs = opts.maxRetryDelayMs;
1929
- this._toolExecution = opts.toolExecution ?? "parallel";
1930
- this._beforeToolExecution = opts.beforeToolExecution;
1931
- this._afterToolExecution = opts.afterToolExecution;
1932
- }
1933
- /**
1934
- * Get the current session ID used for provider caching.
1935
- */
1936
- get sessionId() {
1937
- return this._sessionId;
1938
- }
1939
- /**
1940
- * Set the session ID for provider caching.
1941
- * Call this when switching sessions (new session, branch, resume, etc).
1942
- */
1943
- set sessionId(value) {
1944
- this._sessionId = value;
1945
- }
1946
- /**
1947
- * Get the current thinking budgets.
1948
- */
1949
- get thinkingBudgets() {
1950
- return this._thinkingBudgets;
1951
- }
1952
- /**
1953
- * Set custom thinking budgets for token-based providers.
1954
- */
1955
- set thinkingBudgets(value) {
1956
- this._thinkingBudgets = value;
1957
- }
1958
- /**
1959
- * Get the current preferred transport.
1960
- */
1961
- get transport() {
1962
- return this._transport;
1963
- }
1964
- /**
1965
- * Set the preferred transport.
1966
- */
1967
- setTransport(value) {
1968
- this._transport = value;
1969
- }
1970
- /**
1971
- * Get the current max retry delay in milliseconds.
1972
- */
1973
- get maxRetryDelayMs() {
1974
- return this._maxRetryDelayMs;
1975
- }
1976
- /**
1977
- * Set the maximum delay to wait for server-requested retries.
1978
- * Set to 0 to disable the cap.
1979
- */
1980
- set maxRetryDelayMs(value) {
1981
- this._maxRetryDelayMs = value;
1982
- }
1983
- get toolExecution() {
1984
- return this._toolExecution;
1985
- }
1986
- setToolExecution(value) {
1987
- this._toolExecution = value;
1988
- }
1989
- setBeforeToolExecution(value) {
1990
- this._beforeToolExecution = value;
1991
- }
1992
- setAfterToolExecution(value) {
1993
- this._afterToolExecution = value;
1994
- }
1995
- get state() {
1996
- return this._state;
1997
- }
1998
- subscribe(fn) {
1999
- this.listeners.add(fn);
2000
- return () => this.listeners.delete(fn);
2001
- }
2002
- setSystemPrompt(v) {
2003
- this._state.systemPrompt = v;
2004
- }
2005
- setModel(m) {
2006
- this._state.model = m;
2007
- }
2008
- setThinkingLevel(l) {
2009
- this._state.thinkingLevel = l;
2010
- }
2011
- setSteeringMode(mode) {
2012
- this.steeringMode = mode;
2013
- }
2014
- getSteeringMode() {
2015
- return this.steeringMode;
2016
- }
2017
- setFollowUpMode(mode) {
2018
- this.followUpMode = mode;
2019
- }
2020
- getFollowUpMode() {
2021
- return this.followUpMode;
2022
- }
2023
- setTools(t) {
2024
- this._state.tools = t;
2025
- }
2026
- replaceMessages(ms) {
2027
- this._state.messages = ms.slice();
2028
- }
2029
- appendMessage(m) {
2030
- this._state.messages = [...this._state.messages, m];
2031
- }
2032
- findMessageIndexById(messageId) {
2033
- return this._state.messages.findIndex((message) => message.messageId === messageId);
2034
- }
2035
- replaceMessageById(message) {
2036
- const messageIndex = this.findMessageIndexById(message.messageId);
2037
- if (messageIndex === -1) return false;
2038
- const messages = this._state.messages.slice();
2039
- messages[messageIndex] = message;
2040
- this._state.messages = messages;
2041
- return true;
2042
- }
2043
- upsertMessage(message) {
2044
- if (this.replaceMessageById(message)) return;
2045
- this.appendMessage(message);
2046
- }
2047
- patchStoredAssistantPart(messageId, partIndex, part) {
2048
- const messageIndex = this.findMessageIndexById(messageId);
2049
- if (messageIndex === -1) return false;
2050
- const messages = this._state.messages.slice();
2051
- const existing = messages[messageIndex];
2052
- if (!existing || existing.role !== "assistant") return false;
2053
- const nextParts = existing.parts.slice();
2054
- nextParts[partIndex] = part;
2055
- messages[messageIndex] = {
2056
- ...existing,
2057
- parts: nextParts
2058
- };
2059
- this._state.messages = messages;
2060
- return true;
2061
- }
2062
- /**
2063
- * Queue a steering message while the agent is running.
2064
- * Delivered after the current assistant turn finishes executing its tool calls,
2065
- * before the next LLM call.
2066
- */
2067
- steer(m) {
2068
- this.steeringQueue.push(m);
2069
- }
2070
- /**
2071
- * Queue a follow-up message to be processed after the agent finishes.
2072
- * Delivered only when agent has no more tool calls or steering messages.
2073
- */
2074
- followUp(m) {
2075
- this.followUpQueue.push(m);
2076
- }
2077
- clearSteeringQueue() {
2078
- this.steeringQueue = [];
2079
- }
2080
- clearFollowUpQueue() {
2081
- this.followUpQueue = [];
2082
- }
2083
- clearAllQueues() {
2084
- this.steeringQueue = [];
2085
- this.followUpQueue = [];
2086
- }
2087
- hasQueuedMessages() {
2088
- return this.steeringQueue.length > 0 || this.followUpQueue.length > 0;
2089
- }
2090
- dequeueSteeringMessages() {
2091
- if (this.steeringMode === "one-at-a-time") {
2092
- if (this.steeringQueue.length > 0) {
2093
- const first = this.steeringQueue[0];
2094
- this.steeringQueue = this.steeringQueue.slice(1);
2095
- return [first];
2096
- }
2097
- return [];
2098
- }
2099
- const steering = this.steeringQueue.slice();
2100
- this.steeringQueue = [];
2101
- return steering;
2102
- }
2103
- dequeueFollowUpMessages() {
2104
- if (this.followUpMode === "one-at-a-time") {
2105
- if (this.followUpQueue.length > 0) {
2106
- const first = this.followUpQueue[0];
2107
- this.followUpQueue = this.followUpQueue.slice(1);
2108
- return [first];
2109
- }
2110
- return [];
2111
- }
2112
- const followUp = this.followUpQueue.slice();
2113
- this.followUpQueue = [];
2114
- return followUp;
2115
- }
2116
- clearMessages() {
2117
- this._state.messages = [];
2118
- }
2119
- abort() {
2120
- this.abortController?.abort();
2121
- }
2122
- waitForIdle() {
2123
- return this.runningPrompt ?? Promise.resolve();
2124
- }
2125
- reset() {
2126
- this._state.messages = [];
2127
- this._state.isStreaming = false;
2128
- this._state.streamMessage = null;
2129
- this._state.pendingToolCalls = /* @__PURE__ */ new Set();
2130
- this._state.error = void 0;
2131
- this.steeringQueue = [];
2132
- this.followUpQueue = [];
2133
- }
2134
- getName() {
2135
- return this._state.name;
2136
- }
2137
- ensureIdle() {
2138
- if (this._state.isStreaming) throw new AgentInStreamingErr({
2139
- message: "agent is already processing a prompt. Use steer() or followup() to queue messages, or wait for completion.",
2140
- name: this._state.name
2141
- });
2142
- }
2143
- async _runLoop(messages, opts) {
2144
- const model = this._state.model;
2145
- if (!model) throw new ModelNotConfiguredErr({ message: "no model configured yet" });
2146
- this.runningPrompt = new Promise((resolve) => {
2147
- this.resolveRunningPrompt = resolve;
2148
- });
2149
- this.abortController = new AbortController();
2150
- this._state.isStreaming = true;
2151
- this._state.streamMessage = null;
2152
- this._state.error = void 0;
2153
- const reasoning = this.state.thinkingLevel === "off" ? void 0 : this._state.thinkingLevel;
2154
- const context = {
2155
- systemPrompt: this._state.systemPrompt,
2156
- messages: this._state.messages.slice(),
2157
- tools: this._state.tools
2158
- };
2159
- let skipInitialSteeringPoll = opts?.skipInitialSteeringPoll === true;
2160
- const config = {
2161
- model,
2162
- reasoning,
2163
- sessionId: this._sessionId,
2164
- onPayload: this._onPayload,
2165
- transport: this._transport,
2166
- thinkingBudgets: this._thinkingBudgets,
2167
- maxRetryDelayMs: this._maxRetryDelayMs,
2168
- toolExecution: this._toolExecution,
2169
- beforeToolExecution: this._beforeToolExecution,
2170
- afterToolExecution: this._afterToolExecution,
2171
- convertToLlm: this.convertToLlm,
2172
- transformContext: this.transformContext,
2173
- getApiKey: this.getApiKey,
2174
- getSteeringMessages: async () => {
2175
- if (skipInitialSteeringPoll) {
2176
- skipInitialSteeringPoll = false;
2177
- return [];
2178
- }
2179
- return this.dequeueSteeringMessages();
2180
- },
2181
- getFollowUpMessages: async () => this.dequeueFollowUpMessages()
2182
- };
2183
- try {
2184
- if (messages) await Loop.runAgentLoop(config, context, messages, async (e) => this._processLoopEvent(e), this.streamFn, this.abortController.signal);
2185
- else await Loop.runAgentLoopContinue(config, context, async (e) => this._processLoopEvent(e), this.streamFn, this.abortController.signal);
2186
- } catch (err) {
2187
- const message = Message.createAssistantMessage({
2188
- role: "assistant",
2189
- protocol: model.protocol,
2190
- provider: model.provider,
2191
- model: model.id,
2192
- usage: {
2193
- input: 0,
2194
- output: 0,
2195
- cacheRead: 0,
2196
- cacheWrite: 0,
2197
- totalTokens: 0,
2198
- cost: {
2199
- input: 0,
2200
- output: 0,
2201
- cacheRead: 0,
2202
- cacheWrite: 0,
2203
- total: 0
2204
- }
2205
- },
2206
- stopReason: this.abortController.signal.aborted ? "aborted" : "error",
2207
- errorMessage: err?.message || String(err),
2208
- time: {
2209
- created: Date.now(),
2210
- completed: Date.now()
2211
- },
2212
- parts: []
2213
- });
2214
- this.appendMessage(message);
2215
- this._state.error = err?.message || String(err);
2216
- this.emit({
2217
- type: "agent.end",
2218
- messages: [message]
2219
- });
2220
- } finally {
2221
- this._state.isStreaming = false;
2222
- this._state.streamMessage = null;
2223
- this._state.pendingToolCalls = /* @__PURE__ */ new Set();
2224
- this.abortController = void 0;
2225
- this.resolveRunningPrompt?.();
2226
- this.runningPrompt = void 0;
2227
- this.resolveRunningPrompt = void 0;
2228
- }
2229
- }
2230
- async loop(messages) {
2231
- this.ensureIdle();
2232
- await this._runLoop(messages);
2233
- }
2234
- async loopContinue() {
2235
- this.ensureIdle();
2236
- await this._runLoop();
2237
- }
2238
- async prompt(messages, images) {
2239
- this.ensureIdle();
2240
- if (!this._state.model) throw new ModelNotConfiguredErr({ message: "no model configured yet." });
2241
- const message = Message.createUserMessage({
2242
- role: "user",
2243
- time: { created: Date.now() },
2244
- parts: [...messages, ...images ?? []]
2245
- });
2246
- await this.loop([message]);
2247
- }
2248
- _processLoopEvent(event) {
2249
- switch (event.type) {
2250
- case "message.start":
2251
- this._state.streamMessage = event.message;
2252
- break;
2253
- case "message.part.start":
2254
- case "message.part.update":
2255
- case "message.part.end":
2256
- this._state.streamMessage = event.message;
2257
- if (event.message.role === "assistant") this.patchStoredAssistantPart(event.message.messageId, event.partIndex, event.part);
2258
- break;
2259
- case "message.update":
2260
- this._state.streamMessage = event.message;
2261
- this.replaceMessageById(event.message);
2262
- break;
2263
- case "message.end":
2264
- this._state.streamMessage = null;
2265
- this.upsertMessage(event.message);
2266
- break;
2267
- case "tool.execution.start": {
2268
- const pendingToolCalls = new Set(this._state.pendingToolCalls);
2269
- pendingToolCalls.add(event.callID);
2270
- this._state.pendingToolCalls = pendingToolCalls;
2271
- break;
2272
- }
2273
- case "tool.execution.end": {
2274
- const pendingToolCalls = new Set(this._state.pendingToolCalls);
2275
- pendingToolCalls.delete(event.callID);
2276
- this._state.pendingToolCalls = pendingToolCalls;
2277
- break;
2278
- }
2279
- case "turn.end":
2280
- if (event.message.role === "assistant" && event.message.parts.length > 0) this._state.error = event.message.errorMessage;
2281
- break;
2282
- case "agent.end":
2283
- this._state.isStreaming = false;
2284
- this._state.streamMessage = null;
2285
- break;
2286
- }
2287
- this.emit(event);
2288
- }
2289
- emit(e) {
2290
- for (const listener of this.listeners) listener(e);
2291
- }
2292
- }
2293
- _Agent.Instance = Instance;
2294
- async function create(options) {
2295
- let resolvedModel;
2296
- if ("provider" in options) {
2297
- const result = await Model.getModel(options.provider, options.model);
2298
- if (!result) throw new ModelNotFoundErr({
2299
- message: "model not found or not configured yet",
2300
- provider: options.provider,
2301
- model: options.model
2302
- });
2303
- resolvedModel = result;
2304
- } else resolvedModel = options.model;
2305
- return new Instance(options.name ?? "main", resolvedModel, options);
2306
- }
2307
- _Agent.create = create;
2308
- })(Agent || (Agent = {}));
2309
- //#endregion
2310
- //#region src/agent/codemode/codemode.ts
2311
- let CodeMode;
2312
- (function(_CodeMode) {
2313
- const ToolDefinitionErr = _CodeMode.ToolDefinitionErr = NamedError.create("ToolDefinitionErr", Type.Object({ message: Type.String() }));
2314
- const toolFnPrefix = process.env.CODEWORK_CODEMODE_FN_PREFIX ?? "external_";
2315
- const SandboxExecutionInputSchema = _CodeMode.SandboxExecutionInputSchema = Type.Object({ typescriptCode: Type.String({ description: "TypeScript code to execute in the sandbox." }) });
2316
- const SandboxExecutionOutputSchema = _CodeMode.SandboxExecutionOutputSchema = Type.Object({
2317
- result: Type.Optional(Type.Unknown()),
2318
- logs: Type.Optional(Type.Array(Type.String()))
2319
- });
2320
- const SandboxExecutionErrorSchema = _CodeMode.SandboxExecutionErrorSchema = Type.Object({
2321
- message: Type.String(),
2322
- name: Type.Optional(Type.String()),
2323
- stack: Type.Optional(Type.String()),
2324
- line: Type.Optional(Type.Number()),
2325
- logs: Type.Optional(Type.Array(Type.String()))
2326
- });
2327
- function withMessageInStack(message, stack) {
2328
- if (!stack) return void 0;
2329
- return stack.includes(message) ? stack : `${message}\n${stack}`;
2330
- }
2331
- function normalizeExecutionError(error) {
2332
- if (error instanceof Error) return {
2333
- name: error.name,
2334
- message: error.message,
2335
- stack: withMessageInStack(error.message, error.stack),
2336
- line: parseErrorLine(error.stack)
2337
- };
2338
- if (typeof error === "object" && error !== null) {
2339
- const candidate = error;
2340
- return {
2341
- name: typeof candidate.name === "string" ? candidate.name : void 0,
2342
- message: typeof candidate.message === "string" ? candidate.message : JSON.stringify(candidate) || "Unknown execution error",
2343
- stack: typeof candidate.stack === "string" ? withMessageInStack(typeof candidate.message === "string" ? candidate.message : JSON.stringify(candidate) || "Unknown execution error", candidate.stack) : void 0,
2344
- line: typeof candidate.line === "number" ? candidate.line : parseErrorLine(candidate.stack)
2345
- };
2346
- }
2347
- return {
2348
- name: "Error",
2349
- message: String(error)
2350
- };
2351
- }
2352
- function parseErrorLine(stack) {
2353
- if (typeof stack !== "string") return void 0;
2354
- const lineMatch = stack.match(/:(\d+)(?::\d+)?\)?$/m);
2355
- return lineMatch ? Number(lineMatch[1]) : void 0;
2356
- }
2357
- function serializeForContent(value) {
2358
- return JSON.stringify(value, (_key, currentValue) => typeof currentValue === "bigint" ? currentValue.toString() : currentValue, 2);
2359
- }
2360
- function toolContentToText(result) {
2361
- return result.content.filter((part) => part.type === "text" && typeof part.text === "string").map((part) => part.text).join("\n").trim();
2362
- }
2363
- function unwrapToolResult(tool, result) {
2364
- if (result.status === "completed") {
2365
- if (tool.outputSchema) return validateSchema(tool.outputSchema, result.result.details, `Tool "${tool.name}" output`);
2366
- return result.result.details;
2367
- }
2368
- if (tool.errorSchema && result.result.details !== void 0) validateSchema(tool.errorSchema, result.result.details, `Tool "${tool.name}" error`);
2369
- const message = toolContentToText(result.result) || `Tool "${tool.name}" failed`;
2370
- const error = new Error(message);
2371
- if (result.result.details !== void 0) error.cause = result.result.details;
2372
- throw error;
2373
- }
2374
- function toolToBinding(tool, prefix = "") {
2375
- const inputSchema = TypeGuard.IsSchema(tool.parameters) ? tool.parameters : Type.Object({});
2376
- const outputSchema = TypeGuard.IsSchema(tool.outputSchema) ? tool.outputSchema : void 0;
2377
- const errorSchema = TypeGuard.IsSchema(tool.errorSchema) ? tool.errorSchema : void 0;
2378
- if (!("execute" in tool) || typeof tool.execute !== "function") throw new ToolDefinitionErr({ message: `tool "${tool.name}" does not have an execute function. code mode requires tools with implementation` });
2379
- return {
2380
- name: `${prefix}${tool.name}`,
2381
- description: tool.description,
2382
- inputSchema,
2383
- outputSchema,
2384
- errorSchema,
2385
- execute: async (callID, params, signal) => {
2386
- const validatedParams = validateSchema(inputSchema, params, `Tool "${tool.name}" input`);
2387
- return unwrapToolResult(tool, await tool.execute(callID, validatedParams, signal));
2388
- }
2389
- };
2390
- }
2391
- function toolsForBinding(tools, prefix) {
2392
- const bindings = {};
2393
- for (const tool of tools) {
2394
- const name = `${prefix}${tool.name}`;
2395
- bindings[name] = toolToBinding(tool, prefix);
2396
- }
2397
- return bindings;
2398
- }
2399
- function indent(value, depth = 1) {
2400
- const prefix = " ".repeat(depth);
2401
- return value.split("\n").map((line) => `${prefix}${line}`).join("\n");
2402
- }
2403
- function toPropertyKey(name) {
2404
- return /^[$A-Z_a-z][$\w]*$/i.test(name) ? name : JSON.stringify(name);
2405
- }
2406
- function schemaToTypeScript(schema) {
2407
- if (TypeGuard.IsAny(schema)) return "any";
2408
- if (TypeGuard.IsUnknown(schema)) return "unknown";
2409
- if (TypeGuard.IsString(schema)) return "string";
2410
- if (TypeGuard.IsNumber(schema) || TypeGuard.IsInteger(schema)) return "number";
2411
- if (TypeGuard.IsBoolean(schema)) return "boolean";
2412
- if (TypeGuard.IsNull(schema)) return "null";
2413
- if (TypeGuard.IsUndefined(schema) || TypeGuard.IsVoid(schema)) return "undefined";
2414
- if (TypeGuard.IsLiteral(schema)) return JSON.stringify(schema.const);
2415
- if (TypeGuard.IsArray(schema)) return `Array<${schemaToTypeScript(schema.items)}>`;
2416
- if (TypeGuard.IsTuple(schema)) return `[${(schema.items ?? []).map((item) => schemaToTypeScript(item)).join(", ")}]`;
2417
- if (TypeGuard.IsUnion(schema)) return schema.anyOf.map((item) => schemaToTypeScript(item)).join(" | ");
2418
- if (TypeGuard.IsIntersect(schema)) return schema.allOf.map((item) => schemaToTypeScript(item)).join(" & ");
2419
- if (TypeGuard.IsRecord(schema)) return `Record<string, ${schemaToTypeScript(Object.values(schema.patternProperties ?? {}).find(TypeGuard.IsSchema) ?? (schema.additionalProperties && TypeGuard.IsSchema(schema.additionalProperties) ? schema.additionalProperties : Type.Unknown()))}>`;
2420
- if (TypeGuard.IsObject(schema)) {
2421
- const required = new Set(schema.required ?? []);
2422
- const properties = Object.entries(schema.properties ?? {}).map(([key, value]) => {
2423
- const optional = required.has(key) ? "" : "?";
2424
- return `${toPropertyKey(key)}${optional}: ${schemaToTypeScript(value)};`;
2425
- });
2426
- if (schema.additionalProperties === true) properties.push("[key: string]: unknown;");
2427
- else if (schema.additionalProperties && TypeGuard.IsSchema(schema.additionalProperties)) properties.push(`[key: string]: ${schemaToTypeScript(schema.additionalProperties)};`);
2428
- if (properties.length === 0) return "{}";
2429
- return `{\n${indent(properties.join("\n"))}\n}`;
2430
- }
2431
- return "unknown";
2432
- }
2433
- function generateTypeStubs(bindings, options = {}) {
2434
- const { includeDescriptions = true } = options;
2435
- const declarations = [];
2436
- for (const [name, binding] of Object.entries(bindings)) {
2437
- const inputTypeName = `${capitalize(name)}Input`;
2438
- const outputTypeName = `${capitalize(name)}Output`;
2439
- declarations.push(`type ${inputTypeName} = ${schemaToTypeScript(binding.inputSchema)};`);
2440
- let outputTypeRef = "unknown";
2441
- if (binding.outputSchema !== void 0) {
2442
- declarations.push(`type ${outputTypeName} = ${schemaToTypeScript(binding.outputSchema)};`);
2443
- outputTypeRef = outputTypeName;
2444
- }
2445
- const description = includeDescriptions && binding.description ? `/** ${binding.description} */\n` : "";
2446
- declarations.push(`${description}declare function ${name}(input: ${inputTypeName}): Promise<${outputTypeRef}>;`);
2447
- }
2448
- return declarations.join("\n\n");
2449
- }
2450
- _CodeMode.generateTypeStubs = generateTypeStubs;
2451
- function buildExampleSnippet() {
2452
- return [
2453
- "```typescript",
2454
- "const numbers = [12, 24, 36];",
2455
- "const total = numbers.reduce((sum, value) => sum + value, 0);",
2456
- "",
2457
- "return {",
2458
- " count: numbers.length,",
2459
- " average: total / numbers.length,",
2460
- "};",
2461
- "```"
2462
- ];
2463
- }
2464
- function generateSystemPrompt(config) {
2465
- const fnPrefix = config.fnPrefix ?? toolFnPrefix;
2466
- const bindings = toolsForBinding(config.tools, fnPrefix);
2467
- const typeStubs = generateTypeStubs(bindings);
2468
- return [
2469
- "## Code Execution Tool",
2470
- "",
2471
- "You have access to `sandbox_execute_typescript` which runs TypeScript code in a sandboxed environment.",
2472
- "",
2473
- "### When to Use",
2474
- "",
2475
- "Use `sandbox_execute_typescript` when you need to:",
2476
- "- Process data with loops, conditionals, or complex logic",
2477
- "- Make multiple tool calls in parallel with `Promise.all(...)`",
2478
- "- Transform, filter, or aggregate data",
2479
- "- Perform calculations or data analysis",
2480
- "",
2481
- "For simple operations, prefer calling tools directly.",
2482
- "",
2483
- "### Available External APIs",
2484
- "",
2485
- "Inside your TypeScript code, you can call these async functions:",
2486
- "",
2487
- Object.entries(bindings).map(([name, binding]) => `- \`${name}(input)\`: ${binding.description}`).join("\n") || "- No external APIs available for this run.",
2488
- "",
2489
- "### Type Definitions",
2490
- "",
2491
- "```typescript",
2492
- typeStubs,
2493
- "```",
2494
- "",
2495
- "### Example",
2496
- "",
2497
- ...buildExampleSnippet(),
2498
- "",
2499
- "### Important Notes",
2500
- "",
2501
- `- All\`${fnPrefix}\`*calls are async, so always use \`await\``,
2502
- "- Return a value to pass results back to you",
2503
- "- Use `console.log()` for debugging (logs are captured)",
2504
- "- The sandbox is isolated - no network access or file system",
2505
- "- Each execution is isolated and does not share state with previous runs",
2506
- ""
2507
- ].join("\n");
2508
- }
2509
- _CodeMode.generateSystemPrompt = generateSystemPrompt;
2510
- async function transpileTypeScript(typescriptCode) {
2511
- return (await transform([
2512
- "(async () => {",
2513
- " const __codemodeNormalizeError = (error: unknown) => {",
2514
- " if (error instanceof Error) return { name: error.name, message: error.message, stack: error.stack };",
2515
- " if (typeof error === 'object' && error !== null) {",
2516
- " const candidate = error as { name?: unknown; message?: unknown; stack?: unknown };",
2517
- " return {",
2518
- " name: typeof candidate.name === 'string' ? candidate.name : 'Error',",
2519
- " message:",
2520
- " typeof candidate.message === 'string'",
2521
- " ? candidate.message",
2522
- " : JSON.stringify(candidate) || 'Unknown execution error',",
2523
- " stack: typeof candidate.stack === 'string' ? candidate.stack : undefined,",
2524
- " };",
2525
- " }",
2526
- " return { name: 'Error', message: String(error) };",
2527
- " };",
2528
- " try {",
2529
- typescriptCode,
2530
- " } catch (error) {",
2531
- " throw __codemodeNormalizeError(error);",
2532
- " }",
2533
- "})()"
2534
- ].join("\n"), {
2535
- loader: "ts",
2536
- target: "es2022",
2537
- sourcefile: "script.ts",
2538
- minify: false,
2539
- keepNames: false
2540
- })).code;
2541
- }
2542
- function buildToolDescription(tools, fnPrefix = toolFnPrefix) {
2543
- return [
2544
- "Execute TypeScript code in a secure sandbox environment. ",
2545
- "The code can use these external API functions:",
2546
- "<functions>",
2547
- ` ${tools.map((tool) => `${fnPrefix}${tool.name}`).join("\n")}`,
2548
- "</functions>",
2549
- `All ${fnPrefix}* calls are async and must be awaited. `,
2550
- "Return a value to pass results back. Use console.log() for debugging."
2551
- ].join("\n");
2552
- }
2553
- function createCodeModeTool(config) {
2554
- if (config.tools.length === 0) throw new ToolDefinitionErr({ message: "at least one tool must be provided to `createCodeModeTool`" });
2555
- return Agent.defineTool({
2556
- name: "sandbox_execute_typescript",
2557
- label: "Sandbox TypeScript",
2558
- description: buildToolDescription(config.tools, config.fnPrefix),
2559
- parameters: SandboxExecutionInputSchema,
2560
- outputSchema: SandboxExecutionOutputSchema,
2561
- errorSchema: SandboxExecutionErrorSchema,
2562
- async execute(_callID, params, signal, onUpdate) {
2563
- const bindings = toolsForBinding(config.tools, config.fnPrefix ?? toolFnPrefix);
2564
- let context;
2565
- try {
2566
- await onUpdate?.({
2567
- status: "running",
2568
- partial: { content: [{
2569
- type: "text",
2570
- text: "Transpiling TypeScript for sandbox execution"
2571
- }] }
2572
- });
2573
- const javascriptCode = await transpileTypeScript(params.typescriptCode);
2574
- await onUpdate?.({
2575
- status: "running",
2576
- partial: { content: [{
2577
- type: "text",
2578
- text: "Executing code in sandbox runtime"
2579
- }] }
2580
- });
2581
- context = await config.driver.createContext({
2582
- bindings,
2583
- timeout: config.timeout,
2584
- memoryLimit: config.memoryLimit,
2585
- signal
2586
- });
2587
- const executionResult = await context.execute(javascriptCode);
2588
- if (executionResult.success) {
2589
- const details = {
2590
- result: executionResult.value,
2591
- ...executionResult.logs && executionResult.logs.length > 0 ? { logs: executionResult.logs } : {}
2592
- };
2593
- return {
2594
- status: "completed",
2595
- result: {
2596
- content: [{
2597
- type: "text",
2598
- text: serializeForContent(details)
2599
- }],
2600
- details,
2601
- isError: false
2602
- }
2603
- };
2604
- }
2605
- const details = {
2606
- message: executionResult.error?.message || "sandbox execution failed",
2607
- ...executionResult.error?.name ? { name: executionResult.error.name } : {},
2608
- ...executionResult.error?.stack ? { stack: executionResult.error.stack } : {},
2609
- ...executionResult.error?.line != null ? { line: executionResult.error.line } : {},
2610
- ...executionResult.logs && executionResult.logs.length > 0 ? { logs: executionResult.logs } : {}
2611
- };
2612
- return {
2613
- status: "error",
2614
- result: {
2615
- content: [{
2616
- type: "text",
2617
- text: serializeForContent(details)
2618
- }],
2619
- details,
2620
- isError: true
2621
- }
2622
- };
2623
- } catch (error) {
2624
- const normalizedError = normalizeExecutionError(error);
2625
- const details = {
2626
- message: normalizedError.message,
2627
- ...normalizedError.name ? { name: normalizedError.name } : {},
2628
- ...normalizedError.stack ? { stack: normalizedError.stack } : {},
2629
- ...normalizedError.line != null ? { line: normalizedError.line } : {}
2630
- };
2631
- return {
2632
- status: "error",
2633
- result: {
2634
- content: [{
2635
- type: "text",
2636
- text: serializeForContent(details)
2637
- }],
2638
- details,
2639
- isError: true
2640
- }
2641
- };
2642
- } finally {
2643
- await context?.dispose();
2644
- }
2645
- }
2646
- });
2647
- }
2648
- _CodeMode.createCodeModeTool = createCodeModeTool;
2649
- async function create(config) {
2650
- return {
2651
- tool: createCodeModeTool(config),
2652
- systemPrompt: generateSystemPrompt(config)
2653
- };
2654
- }
2655
- _CodeMode.create = create;
2656
- })(CodeMode || (CodeMode = {}));
2657
- //#endregion
2658
- //#region src/llm.ts
2659
- const llmImpl = async (provider, model) => Model.getModel(provider, model);
2660
- const llm = Object.assign(llmImpl, {
2661
- model: Model.getModel,
2662
- models: Model.getModels,
2663
- providers: Model.getProviders,
2664
- registry: Model.registry,
2665
- supportsXhigh: Model.supportsXhigh,
2666
- modelsAreEqual: Model.modelsAreEqual
2667
- });
2668
- //#endregion
2669
- //#region src/stream.ts
2670
- const streamImpl = (model, context, options) => Stream.stream(model, context, options);
2671
- /**
2672
- * Start a streaming assistant response for a model/context pair.
2673
- *
2674
- * This is the main streaming entrypoint.
2675
- * Attached helpers:
2676
- *
2677
- * - `stream.complete(...)` resolves the final assistant message without manually iterating events.
2678
- * - `stream.simple(...)` uses `Stream.SimpleOptions` for higher-level reasoning controls.
2679
- * - `stream.completeSimple(...)` is the non-streaming counterpart to `stream.simple(...)`.
2680
- * - `stream.resolveProtocolProvider(...)` exposes protocol resolution for advanced integrations.
2681
- */
2682
- const stream = Object.assign(streamImpl, {
2683
- complete: Stream.complete,
2684
- simple: Stream.streamSimple,
2685
- completeSimple: Stream.completeSimple,
2686
- resolveProtocolProvider: Stream.resolveProtocolProvider
2687
- });
2688
- Stream.complete;
2689
- Stream.streamSimple;
2690
- Stream.completeSimple;
2691
- //#endregion
2692
- export { Agent, CodeMode, Message, agent, llm, stream };
2693
-
2694
- //# sourceMappingURL=index.mjs.map