@ai-setting/roy-agent-core 1.5.15-test → 1.5.17-beta.1

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 (141) hide show
  1. package/dist/config/index.d.ts +1250 -0
  2. package/dist/config/index.js +32 -0
  3. package/dist/env/agent/index.d.ts +2279 -0
  4. package/dist/env/agent/index.js +24 -0
  5. package/dist/env/commands/index.d.ts +1131 -0
  6. package/dist/env/commands/index.js +14 -0
  7. package/dist/env/debug/formatters/index.d.ts +236 -0
  8. package/dist/env/debug/formatters/index.js +11 -0
  9. package/dist/env/debug/index.d.ts +1652 -0
  10. package/dist/env/debug/index.js +26 -0
  11. package/dist/env/hook/index.d.ts +279 -0
  12. package/dist/env/hook/index.js +29 -0
  13. package/dist/env/index.d.ts +3481 -0
  14. package/dist/env/index.js +82 -0
  15. package/dist/env/llm/index.d.ts +1760 -0
  16. package/dist/env/llm/index.js +40 -0
  17. package/dist/env/log-trace/index.d.ts +1574 -0
  18. package/dist/env/log-trace/index.js +83 -0
  19. package/dist/env/mcp/index.d.ts +1331 -0
  20. package/dist/env/mcp/index.js +39 -0
  21. package/dist/env/mcp/tool/index.d.ts +183 -0
  22. package/dist/env/mcp/tool/index.js +14 -0
  23. package/dist/env/memory/built-in/index.d.ts +232 -0
  24. package/dist/env/memory/built-in/index.js +11 -0
  25. package/dist/env/memory/index.d.ts +1799 -0
  26. package/dist/env/memory/index.js +56 -0
  27. package/dist/env/memory/plugin/index.d.ts +747 -0
  28. package/dist/env/memory/plugin/index.js +36 -0
  29. package/dist/env/prompt/index.d.ts +1164 -0
  30. package/dist/env/prompt/index.js +20 -0
  31. package/dist/env/session/index.d.ts +1908 -0
  32. package/dist/env/session/index.js +25 -0
  33. package/dist/env/session/storage/index.d.ts +564 -0
  34. package/dist/env/session/storage/index.js +18 -0
  35. package/dist/env/skill/index.d.ts +1266 -0
  36. package/dist/env/skill/index.js +34 -0
  37. package/dist/env/skill/tool/index.d.ts +193 -0
  38. package/dist/env/skill/tool/index.js +9 -0
  39. package/dist/env/task/delegate/index.d.ts +1612 -0
  40. package/dist/env/task/delegate/index.js +18 -0
  41. package/dist/env/task/events/index.d.ts +171 -0
  42. package/dist/env/task/events/index.js +7 -0
  43. package/dist/env/task/hooks/index.d.ts +624 -0
  44. package/dist/env/task/hooks/index.js +7 -0
  45. package/dist/env/task/index.d.ts +1553 -0
  46. package/dist/env/task/index.js +34 -0
  47. package/dist/env/task/plugins/index.d.ts +466 -0
  48. package/dist/env/task/plugins/index.js +23 -0
  49. package/dist/env/task/storage/index.d.ts +241 -0
  50. package/dist/env/task/storage/index.js +14 -0
  51. package/dist/env/task/tools/index.d.ts +1485 -0
  52. package/dist/env/task/tools/index.js +17 -0
  53. package/dist/env/task/tools/operation/index.d.ts +1484 -0
  54. package/dist/env/task/tools/operation/index.js +15 -0
  55. package/dist/env/tool/built-in/index.d.ts +218 -0
  56. package/dist/env/tool/built-in/index.js +25 -0
  57. package/dist/env/tool/index.d.ts +1396 -0
  58. package/dist/env/tool/index.js +39 -0
  59. package/dist/env/workflow/decorators/index.d.ts +2161 -0
  60. package/dist/env/workflow/decorators/index.js +27 -0
  61. package/dist/env/workflow/engine/index.d.ts +3453 -0
  62. package/dist/env/workflow/engine/index.js +28 -0
  63. package/dist/env/workflow/index.d.ts +3546 -0
  64. package/dist/env/workflow/index.js +136 -0
  65. package/dist/env/workflow/nodes/index.d.ts +2092 -0
  66. package/dist/env/workflow/nodes/index.js +19 -0
  67. package/dist/env/workflow/service/index.d.ts +227 -0
  68. package/dist/env/workflow/service/index.js +13 -0
  69. package/dist/env/workflow/storage/index.d.ts +165 -0
  70. package/dist/env/workflow/storage/index.js +27 -0
  71. package/dist/env/workflow/tools/index.d.ts +416 -0
  72. package/dist/env/workflow/tools/index.js +159 -0
  73. package/dist/env/workflow/types/index.d.ts +2255 -0
  74. package/dist/env/workflow/types/index.js +98 -0
  75. package/dist/env/workflow/utils/index.d.ts +2031 -0
  76. package/dist/env/workflow/utils/index.js +637 -0
  77. package/dist/index.d.ts +7858 -0
  78. package/dist/index.js +399 -0
  79. package/dist/shared/@ai-setting/roy-agent-core-0rtxwr28.js +258 -0
  80. package/dist/shared/@ai-setting/roy-agent-core-0vbdz0x7.js +36 -0
  81. package/dist/shared/@ai-setting/roy-agent-core-1akcqxj9.js +349 -0
  82. package/dist/shared/@ai-setting/roy-agent-core-1ce3fqrk.js +117 -0
  83. package/dist/shared/@ai-setting/roy-agent-core-2dhd60aw.js +11 -0
  84. package/dist/shared/@ai-setting/roy-agent-core-3jywqmdd.js +393 -0
  85. package/dist/shared/@ai-setting/roy-agent-core-3rr5k71j.js +200 -0
  86. package/dist/shared/@ai-setting/roy-agent-core-44hnfb02.js +299 -0
  87. package/dist/shared/@ai-setting/roy-agent-core-4t40mkpv.js +206 -0
  88. package/dist/shared/@ai-setting/roy-agent-core-4txzpsbt.js +393 -0
  89. package/dist/shared/@ai-setting/roy-agent-core-5x94xmt6.js +350 -0
  90. package/dist/shared/@ai-setting/roy-agent-core-69jskqjg.js +180 -0
  91. package/dist/shared/@ai-setting/roy-agent-core-6kvtahqv.js +408 -0
  92. package/dist/shared/@ai-setting/roy-agent-core-7fgf85wc.js +284 -0
  93. package/dist/shared/@ai-setting/roy-agent-core-81w1963m.js +762 -0
  94. package/dist/shared/@ai-setting/roy-agent-core-8gxth0eh.js +10 -0
  95. package/dist/shared/@ai-setting/roy-agent-core-92z6t4he.js +14 -0
  96. package/dist/shared/@ai-setting/roy-agent-core-93zfb3r1.js +922 -0
  97. package/dist/shared/@ai-setting/roy-agent-core-9yxb3ty9.js +15 -0
  98. package/dist/shared/@ai-setting/roy-agent-core-b0x5dda6.js +1130 -0
  99. package/dist/shared/@ai-setting/roy-agent-core-bcbqy27c.js +14 -0
  100. package/dist/shared/@ai-setting/roy-agent-core-bvr1761x.js +653 -0
  101. package/dist/shared/@ai-setting/roy-agent-core-ctdhjv68.js +93 -0
  102. package/dist/shared/@ai-setting/roy-agent-core-d7cyjkf7.js +872 -0
  103. package/dist/shared/@ai-setting/roy-agent-core-dh9d7a3m.js +11 -0
  104. package/dist/shared/@ai-setting/roy-agent-core-e25xkv53.js +64 -0
  105. package/dist/shared/@ai-setting/roy-agent-core-eajcvp4e.js +378 -0
  106. package/dist/shared/@ai-setting/roy-agent-core-f7q2x5z6.js +492 -0
  107. package/dist/shared/@ai-setting/roy-agent-core-fs0mn2jk.js +52 -0
  108. package/dist/shared/@ai-setting/roy-agent-core-g1s2h0e5.js +171 -0
  109. package/dist/shared/@ai-setting/roy-agent-core-g99pxzn5.js +862 -0
  110. package/dist/shared/@ai-setting/roy-agent-core-gbqcyegm.js +1387 -0
  111. package/dist/shared/@ai-setting/roy-agent-core-gjq1yk68.js +208 -0
  112. package/dist/shared/@ai-setting/roy-agent-core-gq20wsgv.js +139 -0
  113. package/dist/shared/@ai-setting/roy-agent-core-gwc4h96n.js +534 -0
  114. package/dist/shared/@ai-setting/roy-agent-core-jfh9q2qh.js +204 -0
  115. package/dist/shared/@ai-setting/roy-agent-core-jvatggbb.js +603 -0
  116. package/dist/shared/@ai-setting/roy-agent-core-kkbwepqb.js +97 -0
  117. package/dist/shared/@ai-setting/roy-agent-core-pjr12nnd.js +587 -0
  118. package/dist/shared/@ai-setting/roy-agent-core-psv4v63c.js +176 -0
  119. package/dist/shared/@ai-setting/roy-agent-core-psvxt4c9.js +60 -0
  120. package/dist/shared/@ai-setting/roy-agent-core-qqceba6k.js +442 -0
  121. package/dist/shared/@ai-setting/roy-agent-core-qxhq8ven.js +57 -0
  122. package/dist/shared/@ai-setting/roy-agent-core-qxnbvgwe.js +66 -0
  123. package/dist/shared/@ai-setting/roy-agent-core-r9ezzemr.js +10 -0
  124. package/dist/shared/@ai-setting/roy-agent-core-rhmtwnw1.js +267 -0
  125. package/dist/shared/@ai-setting/roy-agent-core-rvv6ydff.js +584 -0
  126. package/dist/shared/@ai-setting/roy-agent-core-rvxg1wps.js +102 -0
  127. package/dist/shared/@ai-setting/roy-agent-core-satmq6sh.js +549 -0
  128. package/dist/shared/@ai-setting/roy-agent-core-sx7wsvnn.js +15 -0
  129. package/dist/shared/@ai-setting/roy-agent-core-t94ktchq.js +213 -0
  130. package/dist/shared/@ai-setting/roy-agent-core-vf215qfv.js +812 -0
  131. package/dist/shared/@ai-setting/roy-agent-core-vkz81f7v.js +1316 -0
  132. package/dist/shared/@ai-setting/roy-agent-core-vn2bc59q.js +1205 -0
  133. package/dist/shared/@ai-setting/roy-agent-core-wa1kzqky.js +328 -0
  134. package/dist/shared/@ai-setting/roy-agent-core-wft9ra24.js +20 -0
  135. package/dist/shared/@ai-setting/roy-agent-core-wrcy0h6z.js +2098 -0
  136. package/dist/shared/@ai-setting/roy-agent-core-xq8hhqb8.js +419 -0
  137. package/dist/shared/@ai-setting/roy-agent-core-xs5rsgat.js +368 -0
  138. package/dist/shared/@ai-setting/roy-agent-core-zbkpc41z.js +377 -0
  139. package/dist/shared/@ai-setting/roy-agent-core-zgypchmt.js +172 -0
  140. package/dist/shared/@ai-setting/roy-agent-core-zpn0bqa8.js +103 -0
  141. package/package.json +29 -8
@@ -0,0 +1,1316 @@
1
+ import {
2
+ ContextError,
3
+ ErrorCodes
4
+ } from "./roy-agent-core-ctdhjv68.js";
5
+ import {
6
+ envKeyToConfigKey,
7
+ toEnvKey
8
+ } from "./roy-agent-core-qxhq8ven.js";
9
+ import {
10
+ BaseComponent
11
+ } from "./roy-agent-core-kkbwepqb.js";
12
+ import {
13
+ LLMHookPoints,
14
+ globalHookManager
15
+ } from "./roy-agent-core-gjq1yk68.js";
16
+ import {
17
+ TracedAs,
18
+ init_decorator
19
+ } from "./roy-agent-core-zgypchmt.js";
20
+ import {
21
+ createLogger,
22
+ init_logger
23
+ } from "./roy-agent-core-44hnfb02.js";
24
+ import {
25
+ __legacyDecorateClassTS,
26
+ __require
27
+ } from "./roy-agent-core-fs0mn2jk.js";
28
+
29
+ // src/env/llm/types.ts
30
+ import { z } from "zod";
31
+ var ProviderCapabilitiesSchema = z.object({
32
+ reasoning: z.boolean().optional(),
33
+ toolcall: z.boolean().optional(),
34
+ interleaved: z.object({
35
+ field: z.string()
36
+ }).optional(),
37
+ thinkingInText: z.object({
38
+ enabled: z.boolean().optional(),
39
+ tags: z.array(z.string()).optional(),
40
+ removeFromOutput: z.boolean().optional()
41
+ }).optional()
42
+ });
43
+ var ModelLimitsSchema = z.object({
44
+ contextWindow: z.number().int().positive(),
45
+ compactionThreshold: z.number().min(0).max(1).optional(),
46
+ maxOutputTokens: z.number().int().positive().optional()
47
+ });
48
+ var ProviderConfigSchema = z.object({
49
+ provider: z.string(),
50
+ name: z.string().optional(),
51
+ description: z.string().optional(),
52
+ apiKey: z.string(),
53
+ baseURL: z.string().optional(),
54
+ models: z.array(z.string()).optional(),
55
+ defaultModel: z.string().optional(),
56
+ temperature: z.number().min(0).max(2).optional(),
57
+ maxTokens: z.number().int().positive().optional(),
58
+ capabilities: ProviderCapabilitiesSchema.optional(),
59
+ limits: z.record(z.string(), ModelLimitsSchema).optional(),
60
+ providerOptions: z.record(z.unknown()).optional(),
61
+ options: z.record(z.unknown()).optional()
62
+ });
63
+ var LLMDefaultConfigSchema = z.object({
64
+ temperature: z.number().min(0).max(2).optional(),
65
+ maxTokens: z.number().int().positive().optional()
66
+ });
67
+ var LLMConfigSchema = z.object({
68
+ default: LLMDefaultConfigSchema.optional(),
69
+ defaultModel: z.string().optional(),
70
+ providers: z.record(z.string(), ProviderConfigSchema),
71
+ defaultProvider: z.string(),
72
+ temperature: z.number().min(0).max(2).default(0.7),
73
+ maxTokens: z.number().int().positive().optional(),
74
+ topP: z.number().min(0).max(1).optional(),
75
+ stream: z.boolean().default(true),
76
+ options: z.record(z.unknown()).optional()
77
+ });
78
+ // src/env/llm/provider.ts
79
+ class ProviderManager {
80
+ providers = new Map;
81
+ async initialize(config) {
82
+ this.providers.clear();
83
+ for (const [id, providerConfig] of Object.entries(config.providers)) {
84
+ const resolvedConfig = await this.resolveProviderConfig(providerConfig);
85
+ const provider = this.createProvider(id, resolvedConfig);
86
+ this.providers.set(id, provider);
87
+ }
88
+ }
89
+ getProvider(id) {
90
+ return this.providers.get(id);
91
+ }
92
+ listProviders() {
93
+ return Array.from(this.providers.values());
94
+ }
95
+ async resolveProviderConfig(config) {
96
+ return {
97
+ ...config,
98
+ apiKey: await this.resolveEnvVar(config.apiKey),
99
+ baseURL: config.baseURL ? await this.resolveEnvVar(config.baseURL) : undefined
100
+ };
101
+ }
102
+ async resolveEnvVar(value) {
103
+ const envVarPattern = /\$\{([^}]+)\}/g;
104
+ const matches = [...value.matchAll(envVarPattern)];
105
+ if (matches.length === 0) {
106
+ return value;
107
+ }
108
+ let result = value;
109
+ for (const match of matches) {
110
+ const varName = match[1];
111
+ const envValue = process.env[varName];
112
+ if (envValue === undefined) {
113
+ throw new Error(`Environment variable ${varName} is not set`);
114
+ }
115
+ result = result.replace(match[0], envValue);
116
+ }
117
+ return result;
118
+ }
119
+ createProvider(id, config) {
120
+ return {
121
+ id,
122
+ provider: null,
123
+ models: config.models || []
124
+ };
125
+ }
126
+ createAIProvider(id, config, createFn) {
127
+ const providerInstance = createFn(config.apiKey, config.baseURL);
128
+ return {
129
+ id,
130
+ provider: providerInstance,
131
+ models: config.models || []
132
+ };
133
+ }
134
+ }
135
+ // src/env/llm/transform.ts
136
+ class LLMTransform {
137
+ normalizeMessages(messages, providerType) {
138
+ return messages.map((msg) => {
139
+ switch (providerType) {
140
+ case "anthropic":
141
+ return this.normalizeForAnthropic(msg);
142
+ case "openai":
143
+ case "openai-compatible":
144
+ default:
145
+ return this.normalizeForOpenAI(msg);
146
+ }
147
+ });
148
+ }
149
+ normalizeForOpenAI(msg) {
150
+ return {
151
+ role: msg.role,
152
+ content: msg.content,
153
+ toolCalls: msg.toolCalls,
154
+ toolCallId: msg.toolCallId
155
+ };
156
+ }
157
+ normalizeForAnthropic(msg) {
158
+ if (msg.role === "tool") {
159
+ return {
160
+ role: "user",
161
+ content: msg.content,
162
+ toolCallId: msg.toolCallId
163
+ };
164
+ }
165
+ return msg;
166
+ }
167
+ extractToolSchema(tool) {
168
+ return {
169
+ name: tool.name,
170
+ description: tool.description,
171
+ parameters: tool.parameters
172
+ };
173
+ }
174
+ zodToJsonSchema(schema) {
175
+ return this.extractZodSchema(schema);
176
+ }
177
+ extractZodSchema(schema) {
178
+ const def = schema._def;
179
+ if (!def)
180
+ return {};
181
+ const typeName = def.typeName || "";
182
+ const type = def.type || "";
183
+ const isV3 = typeName.startsWith("Zod");
184
+ const isV4 = !isV3 && type !== "";
185
+ if (isV3 && typeName === "ZodObject" || isV4 && type === "object") {
186
+ let shape;
187
+ if (isV3) {
188
+ const shapeFn = def.shape;
189
+ shape = shapeFn ? shapeFn() : undefined;
190
+ } else {
191
+ shape = def.shape;
192
+ }
193
+ const properties = {};
194
+ const required = [];
195
+ if (shape) {
196
+ for (const [key, value] of Object.entries(shape)) {
197
+ properties[key] = this.extractZodSchema(value);
198
+ let isOptional = false;
199
+ if (isV3) {
200
+ const zodType = value;
201
+ isOptional = typeof zodType.isOptional === "function" ? zodType.isOptional() : false;
202
+ } else {
203
+ const innerDef = value._def;
204
+ isOptional = innerDef?.defaultValue !== undefined;
205
+ }
206
+ if (!isOptional) {
207
+ required.push(key);
208
+ }
209
+ }
210
+ }
211
+ return {
212
+ type: "object",
213
+ properties,
214
+ ...required.length > 0 ? { required } : {}
215
+ };
216
+ }
217
+ if (isV3 && typeName === "ZodEnum" || isV4 && type === "enum") {
218
+ const zodV3Enum = def;
219
+ const zodV4Enum = def;
220
+ const values = zodV3Enum.values || Object.values(zodV4Enum.entries || {});
221
+ return {
222
+ enum: values
223
+ };
224
+ }
225
+ if (isV3 && typeName === "ZodNativeEnum") {
226
+ const enumDef = def;
227
+ const values = Object.values(enumDef.values || {});
228
+ return {
229
+ enum: values
230
+ };
231
+ }
232
+ if (isV3 && typeName === "ZodString" || isV4 && type === "string") {
233
+ return { type: "string" };
234
+ }
235
+ if (isV3 && (typeName === "ZodNumber" || typeName === "ZodBigInt") || isV4 && (type === "number" || type === "bigint")) {
236
+ return { type: "number" };
237
+ }
238
+ if (isV3 && typeName === "ZodBoolean" || isV4 && type === "boolean") {
239
+ return { type: "boolean" };
240
+ }
241
+ if (isV3 && typeName === "ZodArray" || isV4 && type === "array") {
242
+ if (isV3) {
243
+ const itemsType = def.type;
244
+ if (itemsType) {
245
+ return {
246
+ type: "array",
247
+ items: this.extractZodSchema(itemsType)
248
+ };
249
+ }
250
+ }
251
+ const shape = def.shape;
252
+ if (shape) {
253
+ const itemKey = Object.keys(shape)[0];
254
+ const items = this.extractZodSchema(shape[itemKey]);
255
+ return {
256
+ type: "array",
257
+ items
258
+ };
259
+ }
260
+ return {
261
+ type: "array",
262
+ items: {}
263
+ };
264
+ }
265
+ if (isV3 && typeName === "ZodOptional" || isV4 && type === "optional") {
266
+ if (isV3) {
267
+ const innerType = def.innerType;
268
+ if (innerType) {
269
+ return this.extractZodSchema(innerType);
270
+ }
271
+ }
272
+ const shape = def.shape;
273
+ if (shape) {
274
+ const innerKey = Object.keys(shape).find((k) => k !== "undefinedType");
275
+ if (innerKey) {
276
+ return this.extractZodSchema(shape[innerKey]);
277
+ }
278
+ }
279
+ return {};
280
+ }
281
+ if (isV3 && typeName === "ZodNullable") {
282
+ const innerType = def.innerType;
283
+ if (innerType) {
284
+ return this.extractZodSchema(innerType);
285
+ }
286
+ }
287
+ return {};
288
+ }
289
+ formatToolResult(result) {
290
+ return {
291
+ role: "tool",
292
+ tool_call_id: result.toolCallId,
293
+ content: result.result,
294
+ ...result.isError ? { isError: true } : {}
295
+ };
296
+ }
297
+ parseToolArguments(args) {
298
+ if (!args || args.trim() === "") {
299
+ return {};
300
+ }
301
+ try {
302
+ return JSON.parse(args);
303
+ } catch {
304
+ return {};
305
+ }
306
+ }
307
+ serializeToolArguments(args) {
308
+ return JSON.stringify(args);
309
+ }
310
+ }
311
+ // src/env/llm/invoke.ts
312
+ init_logger();
313
+ import { randomUUID } from "crypto";
314
+ import { streamText, jsonSchema } from "ai";
315
+ import { createOpenAI } from "@ai-sdk/openai";
316
+ import { createAnthropic } from "@ai-sdk/anthropic";
317
+ import { createGoogleGenerativeAI } from "@ai-sdk/google";
318
+ import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
319
+ import { zodToJsonSchema } from "zod-to-json-schema";
320
+ var logger = createLogger("llm:invoke");
321
+ var MAX_RETRIES = 3;
322
+ function parseModelString(model) {
323
+ if (!model) {
324
+ return { providerId: "openai", modelId: "gpt-4o" };
325
+ }
326
+ const parts = model.split("/");
327
+ if (parts.length >= 2) {
328
+ return { providerId: parts[0], modelId: parts.slice(1).join("/") };
329
+ }
330
+ return { providerId: "openai", modelId: model };
331
+ }
332
+ function extractUsageInfo(usage) {
333
+ if (!usage)
334
+ return;
335
+ if (usage.inputTokens && typeof usage.inputTokens === "object") {
336
+ return {
337
+ promptTokens: usage.inputTokens.total ?? 0,
338
+ completionTokens: usage.outputTokens?.total ?? 0,
339
+ totalTokens: usage.totalTokens ?? 0
340
+ };
341
+ }
342
+ if (typeof usage.promptTokens === "number") {
343
+ return {
344
+ promptTokens: usage.promptTokens,
345
+ completionTokens: usage.completionTokens ?? 0,
346
+ totalTokens: usage.totalTokens ?? 0
347
+ };
348
+ }
349
+ if (usage.inputTokenDetails || usage.outputTokenDetails) {
350
+ return {
351
+ promptTokens: usage.inputTokenDetails?.tokens ?? 0,
352
+ completionTokens: usage.outputTokenDetails?.tokens ?? 0,
353
+ totalTokens: usage.totalTokens ?? 0
354
+ };
355
+ }
356
+ if (usage.raw && typeof usage.raw.total_tokens === "number") {
357
+ return {
358
+ promptTokens: 0,
359
+ completionTokens: 0,
360
+ totalTokens: usage.raw.total_tokens
361
+ };
362
+ }
363
+ return;
364
+ }
365
+ function convertToSDKMessages(messages) {
366
+ return messages.map((msg) => {
367
+ if (msg.role === "assistant" && msg.toolCalls && msg.toolCalls.length > 0) {
368
+ const toolCallParts = msg.toolCalls.map((tc) => {
369
+ let input = {};
370
+ if (tc.function?.arguments) {
371
+ if (typeof tc.function.arguments === "string") {
372
+ try {
373
+ input = JSON.parse(tc.function.arguments);
374
+ } catch {
375
+ input = tc.function.arguments;
376
+ }
377
+ } else {
378
+ input = tc.function.arguments;
379
+ }
380
+ } else if (tc.arguments) {
381
+ if (typeof tc.arguments === "string") {
382
+ try {
383
+ input = JSON.parse(tc.arguments);
384
+ } catch {
385
+ input = tc.arguments;
386
+ }
387
+ } else {
388
+ input = tc.arguments;
389
+ }
390
+ }
391
+ return {
392
+ type: "tool-call",
393
+ toolCallId: tc.id,
394
+ toolName: tc.function?.name || tc.name || "unknown",
395
+ input
396
+ };
397
+ });
398
+ return {
399
+ role: "assistant",
400
+ content: toolCallParts
401
+ };
402
+ }
403
+ if (msg.role === "tool") {
404
+ if (!msg.toolCallId) {
405
+ logger.warn("Tool message missing toolCallId", { msg });
406
+ }
407
+ let outputValue = "";
408
+ const content = msg.content;
409
+ if (typeof content === "string") {
410
+ outputValue = content;
411
+ } else if (Array.isArray(content)) {
412
+ const toolResultPart = content.find((p) => p?.type === "tool-result");
413
+ if (toolResultPart) {
414
+ outputValue = typeof toolResultPart.output === "string" ? toolResultPart.output : JSON.stringify(toolResultPart.output);
415
+ } else {
416
+ outputValue = JSON.stringify(content);
417
+ }
418
+ } else {
419
+ outputValue = JSON.stringify(content);
420
+ }
421
+ const converted = {
422
+ role: "tool",
423
+ content: [{
424
+ type: "tool-result",
425
+ toolCallId: msg.toolCallId || "unknown",
426
+ toolName: msg.name || "unknown",
427
+ output: {
428
+ type: "text",
429
+ value: outputValue
430
+ }
431
+ }]
432
+ };
433
+ return converted;
434
+ }
435
+ return msg;
436
+ });
437
+ }
438
+ function convertToolsToSDK(tools) {
439
+ const result = {};
440
+ for (const tool of tools) {
441
+ const jsonSchemaObj = extractToolSchema(tool.parameters);
442
+ result[tool.name] = {
443
+ description: tool.description || "",
444
+ inputSchema: jsonSchema(jsonSchemaObj)
445
+ };
446
+ }
447
+ return result;
448
+ }
449
+ function extractToolSchema(parameters) {
450
+ if (parameters && typeof parameters === "object" && "_def" in parameters) {
451
+ const zodSchema = parameters;
452
+ const schema = zodToJsonSchema(zodSchema, "zod");
453
+ if ("$ref" in schema && schema.definitions) {
454
+ const def = schema.definitions.zod;
455
+ if (def && def.type === "object" && def.properties) {
456
+ return {
457
+ type: "object",
458
+ properties: def.properties,
459
+ required: def.required,
460
+ additionalProperties: true
461
+ };
462
+ }
463
+ }
464
+ return schema;
465
+ }
466
+ return parameters;
467
+ }
468
+ function generateProviderOptions(providerType, options) {
469
+ const result = {
470
+ providerOptions: {}
471
+ };
472
+ if (options.temperature !== undefined) {
473
+ result.temperature = options.temperature;
474
+ }
475
+ if (options.maxTokens !== undefined) {
476
+ result.maxTokens = options.maxTokens;
477
+ }
478
+ switch (providerType) {
479
+ case "anthropic":
480
+ break;
481
+ case "openai-compatible":
482
+ result.providerOptions.includeUsage = true;
483
+ break;
484
+ }
485
+ return result;
486
+ }
487
+ function createEnvEventEmitter(env, context) {
488
+ if (!env) {
489
+ logger.warn("[invoke] createEnvEventEmitter: env is null/undefined, streaming events will be disabled");
490
+ return null;
491
+ }
492
+ return (event) => {
493
+ try {
494
+ const fullType = `llm.${event.type}`;
495
+ logger.info(`[invoke] Pushing LLM event: ${fullType}`, {
496
+ hasPayload: !!event,
497
+ payloadType: typeof event
498
+ });
499
+ env.pushEnvEvent({
500
+ id: randomUUID(),
501
+ type: fullType,
502
+ timestamp: Date.now(),
503
+ metadata: {
504
+ source: "llm.invoke",
505
+ sessionId: context?.sessionId,
506
+ messageId: context?.messageId
507
+ },
508
+ payload: event
509
+ });
510
+ logger.info(`[invoke] LLM event pushed successfully: ${fullType}`);
511
+ } catch (err) {
512
+ logger.warn("Failed to push env event", { error: String(err), eventType: event.type });
513
+ }
514
+ };
515
+ }
516
+ function processThinkingStream(textDelta, config, state) {
517
+ if (!config.enabled || !textDelta) {
518
+ return {
519
+ cleanedText: textDelta,
520
+ isThinkingTagOpen: state.isOpen,
521
+ currentThinkingContent: state.content,
522
+ reasoningEvents: []
523
+ };
524
+ }
525
+ const tags = config.tags || ["thinking"];
526
+ let remainingText = textDelta;
527
+ const reasoningEvents = [];
528
+ let isOpen = state.isOpen;
529
+ let currentContent = state.content;
530
+ for (const tag of tags) {
531
+ const openTag = `<${tag}>`;
532
+ const closeTag = `</${tag}>`;
533
+ let text = remainingText;
534
+ let result = "";
535
+ const openIndex = text.toLowerCase().indexOf(openTag.toLowerCase());
536
+ const closeIndex = text.toLowerCase().indexOf(closeTag.toLowerCase());
537
+ if (openIndex !== -1 && (closeIndex === -1 || openIndex < closeIndex)) {
538
+ const beforeOpen = text.substring(0, openIndex);
539
+ const afterOpen = text.substring(openIndex + openTag.length);
540
+ if (!isOpen) {
541
+ isOpen = true;
542
+ currentContent = "";
543
+ reasoningEvents.push("");
544
+ }
545
+ result += beforeOpen;
546
+ const innerCloseIndex = afterOpen.toLowerCase().indexOf(closeTag.toLowerCase());
547
+ if (innerCloseIndex !== -1) {
548
+ const thinkingContent = afterOpen.substring(0, innerCloseIndex);
549
+ const afterClose = afterOpen.substring(innerCloseIndex + closeTag.length);
550
+ currentContent += thinkingContent;
551
+ reasoningEvents.push(currentContent);
552
+ isOpen = false;
553
+ currentContent = "";
554
+ result += afterClose;
555
+ } else {
556
+ currentContent += afterOpen;
557
+ reasoningEvents.push(currentContent);
558
+ result += "";
559
+ }
560
+ remainingText = result;
561
+ } else if (closeIndex !== -1) {
562
+ const beforeClose = text.substring(0, closeIndex);
563
+ const afterClose = text.substring(closeIndex + closeTag.length);
564
+ if (isOpen) {
565
+ currentContent += beforeClose;
566
+ reasoningEvents.push(currentContent);
567
+ isOpen = false;
568
+ currentContent = "";
569
+ result = afterClose;
570
+ } else {
571
+ result = text;
572
+ }
573
+ remainingText = result;
574
+ }
575
+ }
576
+ return {
577
+ cleanedText: remainingText,
578
+ isThinkingTagOpen: isOpen,
579
+ currentThinkingContent: currentContent,
580
+ reasoningEvents
581
+ };
582
+ }
583
+ async function invoke(config, options, ctx) {
584
+ try {
585
+ const emitToEnv = createEnvEventEmitter(options.env, options.context);
586
+ logger.info("[invoke] Created event emitter, env available:", !!options.env, "context:", options.context);
587
+ const { providerId, modelId } = parseModelString(options.model || config.model);
588
+ const provider = createProviderInstance(providerId, config.apiKey, config.baseURL);
589
+ if (!provider) {
590
+ throw new Error(`Failed to create provider for ${providerId}`);
591
+ }
592
+ const messages = convertToSDKMessages(options.messages);
593
+ const toolCallIds = new Set;
594
+ const assistantToolCalls = [];
595
+ const toolResultIds = [];
596
+ for (const msg of messages) {
597
+ if (msg.role === "assistant") {
598
+ const assistantContent = Array.isArray(msg.content) ? msg.content : [];
599
+ for (const part of assistantContent) {
600
+ if (part && typeof part === "object" && part.type === "tool-call") {
601
+ const tc = part.toolCall || part;
602
+ assistantToolCalls.push({ id: tc.id, name: tc.name || tc.function && tc.function.name });
603
+ toolCallIds.add(tc.id);
604
+ }
605
+ }
606
+ }
607
+ if (msg.role === "tool") {
608
+ const toolContent = Array.isArray(msg.content) ? msg.content : [];
609
+ for (const part of toolContent) {
610
+ if (part && typeof part === "object" && part.type === "tool-result") {
611
+ toolResultIds.push(part.toolCallId);
612
+ }
613
+ }
614
+ }
615
+ }
616
+ const missingToolResults = assistantToolCalls.filter((tc) => !toolResultIds.includes(tc.id));
617
+ const orphanedToolResults = toolResultIds.filter((id) => !toolCallIds.has(id));
618
+ if (assistantToolCalls.length > 0 || toolResultIds.length > 0) {
619
+ logger.debug("[invoke] ToolCallId matching analysis:", {
620
+ assistantToolCalls,
621
+ toolResultIds,
622
+ missingToolResults: missingToolResults.length > 0 ? missingToolResults : "none",
623
+ orphanedToolResults: orphanedToolResults.length > 0 ? orphanedToolResults : "none"
624
+ });
625
+ }
626
+ const tools = options.tools && options.tools.length > 0 ? convertToolsToSDK(options.tools) : undefined;
627
+ const providerOptions = generateProviderOptions(providerId, {
628
+ temperature: options.temperature,
629
+ maxTokens: options.maxTokens
630
+ });
631
+ const model = `${providerId}/${modelId}`;
632
+ if (emitToEnv) {
633
+ logger.info("[invoke] Emitting llm.start event");
634
+ emitToEnv({ type: "start", metadata: { model } });
635
+ } else {
636
+ logger.warn("[invoke] emitToEnv is null, skipping llm.start event");
637
+ }
638
+ const streamTextOptions = {
639
+ model: provider.languageModel(modelId),
640
+ messages,
641
+ tools,
642
+ temperature: providerOptions.temperature,
643
+ maxTokens: providerOptions.maxTokens,
644
+ abortSignal: ctx.abort,
645
+ maxRetries: MAX_RETRIES,
646
+ streamOptions: {
647
+ includeUsage: true
648
+ },
649
+ allowSystemInMessages: true
650
+ };
651
+ const result = await streamText(streamTextOptions);
652
+ let fullContent = "";
653
+ let reasoningContent = "";
654
+ const toolCalls = [];
655
+ let usageInfo;
656
+ let isThinkingTagOpen = false;
657
+ let currentThinkingContent = "";
658
+ for await (const part of result.fullStream) {
659
+ const streamPart = part;
660
+ switch (streamPart.type) {
661
+ case "text-delta":
662
+ const textDelta = streamPart.text;
663
+ const thinkingConfig = config.options?.thinkingInText;
664
+ if (thinkingConfig?.enabled) {
665
+ const thinkingResult = processThinkingStream(textDelta, thinkingConfig, {
666
+ isOpen: isThinkingTagOpen,
667
+ content: currentThinkingContent
668
+ });
669
+ isThinkingTagOpen = thinkingResult.isThinkingTagOpen;
670
+ currentThinkingContent = thinkingResult.currentThinkingContent;
671
+ fullContent += thinkingResult.cleanedText;
672
+ for (const reasoningDelta2 of thinkingResult.reasoningEvents) {
673
+ reasoningContent = reasoningDelta2;
674
+ if (emitToEnv) {
675
+ emitToEnv({ type: "reasoning", content: reasoningContent });
676
+ }
677
+ }
678
+ if (emitToEnv) {
679
+ emitToEnv({ type: "text", content: fullContent, delta: thinkingResult.cleanedText });
680
+ }
681
+ } else {
682
+ fullContent += textDelta;
683
+ if (emitToEnv) {
684
+ emitToEnv({ type: "text", content: fullContent, delta: textDelta });
685
+ }
686
+ }
687
+ break;
688
+ case "reasoning-delta":
689
+ const reasoningDelta = streamPart.text;
690
+ reasoningContent += reasoningDelta;
691
+ if (emitToEnv) {
692
+ emitToEnv({ type: "reasoning", content: reasoningContent });
693
+ }
694
+ break;
695
+ case "tool-call":
696
+ const toolInput = streamPart.input;
697
+ const toolCallId = streamPart.toolCallId;
698
+ const toolName = streamPart.toolName;
699
+ toolCalls.push({
700
+ id: toolCallId,
701
+ name: toolName,
702
+ args: toolInput
703
+ });
704
+ if (emitToEnv) {
705
+ emitToEnv({
706
+ type: "tool_call",
707
+ toolCall: {
708
+ id: toolCallId,
709
+ name: toolName,
710
+ arguments: JSON.stringify(toolInput)
711
+ }
712
+ });
713
+ }
714
+ break;
715
+ case "finish-step":
716
+ const stepUsage = extractUsageInfo(streamPart.usage);
717
+ if (stepUsage) {
718
+ usageInfo = stepUsage;
719
+ }
720
+ break;
721
+ case "error":
722
+ throw streamPart.error;
723
+ case "finish":
724
+ usageInfo = extractUsageInfo(streamPart.totalUsage);
725
+ if (!usageInfo && streamPart.usage) {
726
+ usageInfo = extractUsageInfo(streamPart.usage);
727
+ }
728
+ break;
729
+ }
730
+ }
731
+ if (emitToEnv) {
732
+ emitToEnv({
733
+ type: "completed",
734
+ content: fullContent,
735
+ metadata: {
736
+ model,
737
+ usage: usageInfo
738
+ }
739
+ });
740
+ }
741
+ if (toolCalls.length > 0) {
742
+ const firstToolCall = toolCalls[0];
743
+ return {
744
+ toolCallId: firstToolCall.id,
745
+ result: JSON.stringify({
746
+ content: fullContent,
747
+ reasoning: reasoningContent || undefined,
748
+ tool_calls: toolCalls.map((tc) => ({
749
+ id: tc.id,
750
+ function: {
751
+ name: tc.name,
752
+ arguments: JSON.stringify(tc.args)
753
+ }
754
+ }))
755
+ })
756
+ };
757
+ }
758
+ return {
759
+ toolCallId: "invoke-" + Date.now(),
760
+ result: JSON.stringify({
761
+ content: fullContent,
762
+ reasoning: reasoningContent || undefined,
763
+ usage: usageInfo,
764
+ model
765
+ })
766
+ };
767
+ } catch (error) {
768
+ logger.error("Error during invocation", {
769
+ error: error instanceof Error ? error.message : String(error),
770
+ stack: error instanceof Error ? error.stack : undefined
771
+ });
772
+ return {
773
+ toolCallId: "error-" + Date.now(),
774
+ result: error instanceof Error ? error.message : String(error),
775
+ isError: true
776
+ };
777
+ }
778
+ }
779
+ function createProviderInstance(providerId, apiKey, baseURL) {
780
+ try {
781
+ if (providerId === "openai") {
782
+ const openai = createOpenAI({
783
+ apiKey,
784
+ baseURL
785
+ });
786
+ return openai;
787
+ }
788
+ if (providerId === "anthropic") {
789
+ const anthropic = createAnthropic({
790
+ apiKey
791
+ });
792
+ return anthropic;
793
+ }
794
+ if (providerId === "google") {
795
+ const google = createGoogleGenerativeAI({
796
+ apiKey
797
+ });
798
+ return google;
799
+ }
800
+ const defaultProvider = createOpenAICompatible({
801
+ name: providerId,
802
+ apiKey,
803
+ baseURL: baseURL || "",
804
+ includeUsage: true
805
+ });
806
+ return defaultProvider;
807
+ } catch (error) {
808
+ logger.error(`Failed to create provider ${providerId}`, { error: String(error) });
809
+ return null;
810
+ }
811
+ }
812
+ async function invokeNonStream(config, options, ctx) {
813
+ const { providerId, modelId } = parseModelString(options.model || config.model);
814
+ const provider = createProviderInstance(providerId, config.apiKey, config.baseURL);
815
+ if (!provider) {
816
+ throw new Error(`Failed to create provider for ${providerId}`);
817
+ }
818
+ const messages = convertToSDKMessages(options.messages);
819
+ const tools = options.tools && options.tools.length > 0 ? convertToolsToSDK(options.tools) : undefined;
820
+ const providerOptions = generateProviderOptions(providerId, {
821
+ temperature: options.temperature,
822
+ maxTokens: options.maxTokens
823
+ });
824
+ const { generateText } = await import("ai");
825
+ const genTextFn = generateText;
826
+ const result = await genTextFn({
827
+ model: provider.languageModel(modelId),
828
+ messages,
829
+ tools,
830
+ temperature: providerOptions.temperature,
831
+ maxTokens: providerOptions.maxTokens,
832
+ abortSignal: ctx.abort,
833
+ allowSystemInMessages: true
834
+ });
835
+ const usage = result.usage;
836
+ const usageInfo = usage ? {
837
+ promptTokens: usage.promptTokens ?? 0,
838
+ completionTokens: usage.completionTokens ?? 0,
839
+ totalTokens: usage.totalTokens ?? 0
840
+ } : undefined;
841
+ if (options.env) {
842
+ options.env.pushEnvEvent({
843
+ id: randomUUID(),
844
+ type: "llm.completed",
845
+ timestamp: Date.now(),
846
+ metadata: {
847
+ source: "llm.invokeNonStream",
848
+ sessionId: options.context?.sessionId,
849
+ messageId: options.context?.messageId
850
+ },
851
+ payload: {
852
+ type: "completed",
853
+ content: result.text,
854
+ metadata: {
855
+ model: `${providerId}/${modelId}`,
856
+ usage: usageInfo
857
+ }
858
+ }
859
+ });
860
+ }
861
+ return {
862
+ content: result.text,
863
+ finishReason: result.finishReason === "stop" ? "stop" : "tool-calls",
864
+ model: `${providerId}/${modelId}`,
865
+ usage: usageInfo
866
+ };
867
+ }
868
+ function createInvokeConfig(model, apiKey, baseURL) {
869
+ return {
870
+ model,
871
+ apiKey,
872
+ baseURL
873
+ };
874
+ }
875
+ // src/env/llm/hooks.ts
876
+ class LLMHookManager {
877
+ registrations = [];
878
+ register(type, handler, name) {
879
+ const registration = {
880
+ type,
881
+ name
882
+ };
883
+ switch (type) {
884
+ case "before-invoke":
885
+ registration.beforeInvoke = handler;
886
+ break;
887
+ case "after-invoke":
888
+ registration.afterInvoke = handler;
889
+ break;
890
+ case "stream":
891
+ registration.stream = handler;
892
+ break;
893
+ }
894
+ this.registrations.push(registration);
895
+ this.registerToGlobal(registration);
896
+ }
897
+ registerToGlobal(reg) {
898
+ const hookName = `llm:${reg.name || `anon-${reg.type}-${this.registrations.indexOf(reg)}`}`;
899
+ if (reg.beforeInvoke) {
900
+ globalHookManager.register(LLMHookPoints.BEFORE_INVOKE, {
901
+ name: hookName,
902
+ priority: 0,
903
+ execute: async (ctx) => {
904
+ await reg.beforeInvoke(ctx.data);
905
+ }
906
+ });
907
+ }
908
+ if (reg.afterInvoke) {
909
+ globalHookManager.register(LLMHookPoints.AFTER_INVOKE, {
910
+ name: hookName,
911
+ priority: 0,
912
+ execute: async (ctx) => {
913
+ const { request, providerId, model, metadata, result } = ctx.data;
914
+ await reg.afterInvoke({ request, providerId, model, metadata }, result);
915
+ }
916
+ });
917
+ }
918
+ if (reg.stream) {
919
+ globalHookManager.register(LLMHookPoints.ON_STREAM, {
920
+ name: hookName,
921
+ priority: 0,
922
+ execute: async (ctx) => {
923
+ const { request, providerId, model, metadata, event } = ctx.data;
924
+ await reg.stream({ request, providerId, model, metadata }, event);
925
+ }
926
+ });
927
+ }
928
+ }
929
+ unregister(hook) {
930
+ const index = this.registrations.findIndex((h) => {
931
+ return h.beforeInvoke === hook || h.afterInvoke === hook || h.stream === hook;
932
+ });
933
+ if (index !== -1) {
934
+ const reg = this.registrations[index];
935
+ this.unregisterFromGlobal(reg);
936
+ this.registrations.splice(index, 1);
937
+ }
938
+ }
939
+ unregisterFromGlobal(reg) {
940
+ const hookName = `llm:${reg.name || `anon-${reg.type}`}`;
941
+ globalHookManager.unregister(LLMHookPoints.BEFORE_INVOKE, hookName);
942
+ globalHookManager.unregister(LLMHookPoints.AFTER_INVOKE, hookName);
943
+ globalHookManager.unregister(LLMHookPoints.ON_STREAM, hookName);
944
+ }
945
+ unregisterByName(name) {
946
+ const hookName = `llm:${name}`;
947
+ globalHookManager.unregister(LLMHookPoints.BEFORE_INVOKE, hookName);
948
+ globalHookManager.unregister(LLMHookPoints.AFTER_INVOKE, hookName);
949
+ globalHookManager.unregister(LLMHookPoints.ON_STREAM, hookName);
950
+ this.registrations = this.registrations.filter((h) => h.name !== name);
951
+ }
952
+ unregisterByType(type) {
953
+ const filtered = this.registrations.filter((h) => h.type === type);
954
+ for (const reg of filtered) {
955
+ this.unregisterFromGlobal(reg);
956
+ }
957
+ this.registrations = this.registrations.filter((h) => h.type !== type);
958
+ }
959
+ clear() {
960
+ for (const reg of this.registrations) {
961
+ this.unregisterFromGlobal(reg);
962
+ }
963
+ this.registrations = [];
964
+ }
965
+ getHooks() {
966
+ return [...this.registrations];
967
+ }
968
+ getHooksByType(type) {
969
+ return this.registrations.filter((h) => h.type === type);
970
+ }
971
+ }
972
+
973
+ class LLMHooks {
974
+ hookManager = new LLMHookManager;
975
+ register(type, handler, name) {
976
+ this.hookManager.register(type, handler, name);
977
+ }
978
+ unregister(hook) {
979
+ this.hookManager.unregister(hook);
980
+ }
981
+ unregisterByName(name) {
982
+ this.hookManager.unregisterByName(name);
983
+ }
984
+ unregisterByType(type) {
985
+ this.hookManager.unregisterByType(type);
986
+ }
987
+ clear() {
988
+ this.hookManager.clear();
989
+ }
990
+ getHooks() {
991
+ return this.hookManager.getHooks();
992
+ }
993
+ getHooksByType(type) {
994
+ return this.hookManager.getHooksByType(type);
995
+ }
996
+ async executeBeforeInvoke(context) {
997
+ await globalHookManager.execute(LLMHookPoints.BEFORE_INVOKE, context);
998
+ }
999
+ async executeAfterInvoke(context, result) {
1000
+ await globalHookManager.execute(LLMHookPoints.AFTER_INVOKE, { ...context, result });
1001
+ }
1002
+ async executeStream(context, event) {
1003
+ await globalHookManager.execute(LLMHookPoints.ON_STREAM, { ...context, event });
1004
+ }
1005
+ }
1006
+ // src/env/llm/llm.ts
1007
+ init_logger();
1008
+ init_decorator();
1009
+ var logger2 = createLogger("llm");
1010
+
1011
+ class LLMComponent extends BaseComponent {
1012
+ name = "llm";
1013
+ version = "2.0.0";
1014
+ configComponent;
1015
+ configWatcher;
1016
+ constructor() {
1017
+ super();
1018
+ }
1019
+ async init(config) {
1020
+ await super.init(config);
1021
+ const options = config.options;
1022
+ if (!options?.configComponent) {
1023
+ throw new Error("ConfigComponent is required for LLMComponent initialization");
1024
+ }
1025
+ this.configComponent = options.configComponent;
1026
+ await this.registerConfig(options);
1027
+ this.setStatus("running");
1028
+ }
1029
+ async registerConfig(options) {
1030
+ const configComponent = options.configComponent;
1031
+ if (!configComponent)
1032
+ return;
1033
+ const { configPath, envPrefix, config } = options;
1034
+ const prefix = envPrefix !== undefined ? envPrefix : "LLM";
1035
+ const llmKeys = [
1036
+ "llm.defaultModel",
1037
+ "llm.defaultProvider",
1038
+ "llm.temperature",
1039
+ "llm.maxTokens",
1040
+ "llm.topP",
1041
+ "llm.stream",
1042
+ "llm.providers",
1043
+ "llm.limits",
1044
+ "llm.capabilities"
1045
+ ];
1046
+ let fileSource = null;
1047
+ if (configPath) {
1048
+ configComponent.registerSource({
1049
+ type: "file",
1050
+ relativePath: configPath,
1051
+ optional: true,
1052
+ watch: false
1053
+ });
1054
+ configComponent.ensureSourcesCreated?.();
1055
+ const sources = configComponent.getSources?.() || [];
1056
+ fileSource = sources.find((s) => s.name === "file");
1057
+ if (fileSource) {
1058
+ for (const key of llmKeys) {
1059
+ const value = fileSource.read?.(key);
1060
+ if (value !== undefined) {
1061
+ await configComponent.set(key, value);
1062
+ }
1063
+ }
1064
+ }
1065
+ }
1066
+ configComponent.registerSource({
1067
+ type: "env",
1068
+ envPrefix: prefix,
1069
+ priority: 20,
1070
+ watch: false
1071
+ });
1072
+ configComponent.ensureSourcesCreated?.();
1073
+ const sourcesAfterEnv = configComponent.getSources?.() || [];
1074
+ const envSource = sourcesAfterEnv.find((s) => s.name === "env");
1075
+ if (envSource) {
1076
+ for (const key of llmKeys) {
1077
+ const value = envSource.read?.(key);
1078
+ if (value !== undefined) {
1079
+ await configComponent.set(key, value);
1080
+ }
1081
+ }
1082
+ }
1083
+ for (const key of llmKeys) {
1084
+ const envKey = toEnvKey(key, prefix);
1085
+ const value = process.env[envKey];
1086
+ if (value !== undefined) {
1087
+ await configComponent.set(key, value);
1088
+ }
1089
+ }
1090
+ const loadedKeys = new Set(llmKeys);
1091
+ for (const envKey of Object.keys(process.env)) {
1092
+ const configKey = envKeyToConfigKey(envKey, prefix, "llm");
1093
+ if (!configKey)
1094
+ continue;
1095
+ if (loadedKeys.has(configKey))
1096
+ continue;
1097
+ loadedKeys.add(configKey);
1098
+ const value = process.env[envKey];
1099
+ if (value !== undefined) {
1100
+ await configComponent.set(configKey, value);
1101
+ }
1102
+ }
1103
+ if (config) {
1104
+ const flatConfig = this.flattenConfig(config);
1105
+ for (const [key, value] of Object.entries(flatConfig)) {
1106
+ await configComponent.set(key, value);
1107
+ }
1108
+ }
1109
+ this.registerConfigWatcher(configComponent);
1110
+ }
1111
+ flattenConfig(obj, prefix = "llm") {
1112
+ const result = {};
1113
+ for (const [key, value] of Object.entries(obj)) {
1114
+ const fullKey = `${prefix}.${key}`;
1115
+ if (value && typeof value === "object" && !Array.isArray(value)) {
1116
+ Object.assign(result, this.flattenConfig(value, fullKey));
1117
+ } else {
1118
+ result[fullKey] = value;
1119
+ }
1120
+ }
1121
+ return result;
1122
+ }
1123
+ registerConfigWatcher(configComponent) {
1124
+ if (typeof configComponent.watch !== "function") {
1125
+ logger2.debug("ConfigComponent does not support watch, hot reload disabled");
1126
+ return;
1127
+ }
1128
+ this.configWatcher = configComponent.watch("llm.*", (event) => {
1129
+ this.onConfigChange(event);
1130
+ });
1131
+ logger2.debug("Config hot reload watcher registered for llm.*");
1132
+ }
1133
+ onConfigChange(event) {
1134
+ logger2.info(`LLM config changed: ${event.key}`);
1135
+ if (event.key === "llm.providers" || event.key.startsWith("llm.providers.")) {
1136
+ logger2.info(`LLM provider config changed, will use new config on next call`);
1137
+ } else if (event.key === "llm.defaultModel") {
1138
+ logger2.info(`LLM default model changed to: ${event.newValue}`);
1139
+ } else if (event.key === "llm.defaultProvider") {
1140
+ logger2.info(`LLM default provider changed to: ${event.newValue}`);
1141
+ } else {
1142
+ logger2.debug(`LLM config updated: ${event.key}`);
1143
+ }
1144
+ }
1145
+ async onStart() {
1146
+ if (this._status === "stopped") {
1147
+ this.setStatus("running");
1148
+ }
1149
+ }
1150
+ async onStop() {
1151
+ this.configWatcher?.();
1152
+ this.configWatcher = undefined;
1153
+ this.setStatus("stopped");
1154
+ }
1155
+ getLLMConfig(key, defaultValue) {
1156
+ if (this.configComponent) {
1157
+ const value = this.configComponent.get(key);
1158
+ if (value !== undefined) {
1159
+ return value;
1160
+ }
1161
+ }
1162
+ return defaultValue;
1163
+ }
1164
+ resolveRequest(request) {
1165
+ const defaultModel = this.getLLMConfig("llm.defaultModel", "gpt-4o");
1166
+ const defaultProvider = this.getLLMConfig("llm.defaultProvider", "openai");
1167
+ const defaultTemperature = this.getLLMConfig("llm.temperature", 0.7);
1168
+ const defaultMaxTokens = this.getLLMConfig("llm.maxTokens", 4096);
1169
+ const defaultTopP = this.getLLMConfig("llm.topP", 1);
1170
+ const model = request.model || defaultModel;
1171
+ const providerId = request.providerId || defaultProvider;
1172
+ const apiKey = this.getApiKey(providerId);
1173
+ if (!apiKey) {
1174
+ throw new Error(`API key not found for provider ${providerId}. Set providers.${providerId}.apiKey in config, or use \${env://...} protocol in config file.`);
1175
+ }
1176
+ const messages = request.messages.map((msg) => ({
1177
+ role: msg.role,
1178
+ content: msg.content,
1179
+ name: msg.name,
1180
+ toolCallId: msg.toolCallId,
1181
+ toolCalls: msg.toolCalls
1182
+ }));
1183
+ return {
1184
+ providerId,
1185
+ model,
1186
+ invokeConfig: {
1187
+ model: `${providerId}/${model}`,
1188
+ apiKey,
1189
+ baseURL: this.getProvider(providerId)?.baseURL
1190
+ },
1191
+ messages,
1192
+ temperature: request.temperature ?? defaultTemperature,
1193
+ maxTokens: request.maxTokens ?? defaultMaxTokens,
1194
+ topP: request.topP ?? defaultTopP,
1195
+ abortSignal: request.abortSignal
1196
+ };
1197
+ }
1198
+ async invoke(request) {
1199
+ const startTime = Date.now();
1200
+ if (this._status !== "running") {
1201
+ throw new Error("LLMComponent is not running");
1202
+ }
1203
+ const ctx = this.resolveRequest(request);
1204
+ let output = {
1205
+ content: "",
1206
+ reasoning: undefined,
1207
+ finishReason: "stop"
1208
+ };
1209
+ let finishUsage;
1210
+ const result = await invoke(ctx.invokeConfig, {
1211
+ messages: ctx.messages,
1212
+ model: ctx.invokeConfig.model,
1213
+ temperature: ctx.temperature,
1214
+ maxTokens: ctx.maxTokens,
1215
+ tools: request.tools,
1216
+ env: this.env,
1217
+ context: {
1218
+ sessionId: request.context?.sessionId,
1219
+ messageId: request.context?.messageId
1220
+ }
1221
+ }, { abort: ctx.abortSignal });
1222
+ if (result.isError) {
1223
+ throw new Error(result.result);
1224
+ }
1225
+ try {
1226
+ const parsed = JSON.parse(result.result);
1227
+ output.content = parsed.content || "";
1228
+ output.reasoning = parsed.reasoning || undefined;
1229
+ output.finishReason = parsed.tool_calls?.length ? "tool-calls" : "stop";
1230
+ output.toolCalls = parsed.tool_calls || [];
1231
+ if (parsed.usage) {
1232
+ finishUsage = {
1233
+ promptTokens: parsed.usage.promptTokens ?? parsed.usage.inputTokens ?? 0,
1234
+ completionTokens: parsed.usage.completionTokens ?? parsed.usage.outputTokens ?? 0,
1235
+ totalTokens: parsed.usage.totalTokens ?? 0
1236
+ };
1237
+ }
1238
+ output.usage = finishUsage;
1239
+ } catch {
1240
+ output.content = result.result;
1241
+ output.finishReason = "stop";
1242
+ }
1243
+ const latencyMs = Date.now() - startTime;
1244
+ if (!request.skipThresholdCheck) {
1245
+ const sessionId = request.context?.sessionId;
1246
+ const contextError = this.checkContextThreshold(finishUsage, ctx.providerId, ctx.model, sessionId);
1247
+ if (contextError) {
1248
+ contextError.llmOutput = {
1249
+ content: output.content,
1250
+ reasoning: output.reasoning,
1251
+ toolCalls: output.toolCalls,
1252
+ usage: output.usage
1253
+ };
1254
+ throw contextError;
1255
+ }
1256
+ }
1257
+ return {
1258
+ output,
1259
+ providerId: ctx.providerId,
1260
+ model: ctx.model,
1261
+ latencyMs
1262
+ };
1263
+ }
1264
+ getApiKey(providerId) {
1265
+ const providers = this.getLLMConfig("llm.providers", {});
1266
+ return providers?.[providerId]?.apiKey;
1267
+ }
1268
+ getProvider(id) {
1269
+ const providers = this.getLLMConfig("llm.providers", {});
1270
+ return providers?.[id];
1271
+ }
1272
+ listProviders() {
1273
+ const providers = this.getLLMConfig("llm.providers", {});
1274
+ return Object.keys(providers || {});
1275
+ }
1276
+ getModelLimits(providerId, modelId) {
1277
+ const provider = this.getProvider(providerId);
1278
+ if (!provider?.limits)
1279
+ return;
1280
+ const targetModel = modelId || provider.defaultModel;
1281
+ if (!targetModel)
1282
+ return;
1283
+ return provider.limits[targetModel];
1284
+ }
1285
+ getContextThresholdConfig(providerId, modelId) {
1286
+ const defaults = {
1287
+ contextWindow: 128000,
1288
+ thresholdRatio: 0.9
1289
+ };
1290
+ const limits = this.getModelLimits(providerId, modelId);
1291
+ if (!limits)
1292
+ return defaults;
1293
+ return {
1294
+ contextWindow: limits.contextWindow || defaults.contextWindow,
1295
+ thresholdRatio: limits.compactionThreshold ?? defaults.thresholdRatio
1296
+ };
1297
+ }
1298
+ checkContextThreshold(usage, providerId, modelId, sessionId) {
1299
+ if (!usage)
1300
+ return;
1301
+ const config = this.getContextThresholdConfig(providerId, modelId);
1302
+ const threshold = config.contextWindow * config.thresholdRatio;
1303
+ if (usage.totalTokens >= threshold) {
1304
+ return new ContextError(`Context threshold exceeded: ${usage.totalTokens}/${config.contextWindow} (${(usage.totalTokens / config.contextWindow * 100).toFixed(1)}%)`, ErrorCodes.CONTEXT_THRESHOLD_EXCEEDED, sessionId, {
1305
+ promptTokens: usage.promptTokens ?? 0,
1306
+ completionTokens: usage.completionTokens ?? 0,
1307
+ totalTokens: usage.totalTokens
1308
+ }, config.contextWindow);
1309
+ }
1310
+ return;
1311
+ }
1312
+ }
1313
+ __legacyDecorateClassTS([
1314
+ TracedAs("llm.component.invoke", { recordParams: true, recordResult: true, log: true })
1315
+ ], LLMComponent.prototype, "invoke", null);
1316
+ export { ProviderCapabilitiesSchema, ModelLimitsSchema, ProviderConfigSchema, LLMDefaultConfigSchema, LLMConfigSchema, ProviderManager, LLMTransform, parseModelString, invoke, invokeNonStream, createInvokeConfig, LLMHooks, LLMComponent };