@ai-setting/roy-agent-core 1.5.13-test → 1.5.15-test

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/package.json +1 -1
  2. package/dist/config/index.d.ts +0 -1250
  3. package/dist/config/index.js +0 -32
  4. package/dist/env/agent/index.d.ts +0 -2279
  5. package/dist/env/agent/index.js +0 -24
  6. package/dist/env/commands/index.d.ts +0 -1131
  7. package/dist/env/commands/index.js +0 -14
  8. package/dist/env/debug/formatters/index.d.ts +0 -236
  9. package/dist/env/debug/formatters/index.js +0 -11
  10. package/dist/env/debug/index.d.ts +0 -1652
  11. package/dist/env/debug/index.js +0 -26
  12. package/dist/env/hook/index.d.ts +0 -279
  13. package/dist/env/hook/index.js +0 -29
  14. package/dist/env/index.d.ts +0 -3481
  15. package/dist/env/index.js +0 -82
  16. package/dist/env/llm/index.d.ts +0 -1760
  17. package/dist/env/llm/index.js +0 -40
  18. package/dist/env/log-trace/index.d.ts +0 -1574
  19. package/dist/env/log-trace/index.js +0 -83
  20. package/dist/env/mcp/index.d.ts +0 -1331
  21. package/dist/env/mcp/index.js +0 -39
  22. package/dist/env/mcp/tool/index.d.ts +0 -183
  23. package/dist/env/mcp/tool/index.js +0 -14
  24. package/dist/env/memory/built-in/index.d.ts +0 -232
  25. package/dist/env/memory/built-in/index.js +0 -11
  26. package/dist/env/memory/index.d.ts +0 -1799
  27. package/dist/env/memory/index.js +0 -56
  28. package/dist/env/memory/plugin/index.d.ts +0 -747
  29. package/dist/env/memory/plugin/index.js +0 -36
  30. package/dist/env/prompt/index.d.ts +0 -1164
  31. package/dist/env/prompt/index.js +0 -20
  32. package/dist/env/session/index.d.ts +0 -1908
  33. package/dist/env/session/index.js +0 -25
  34. package/dist/env/session/storage/index.d.ts +0 -564
  35. package/dist/env/session/storage/index.js +0 -18
  36. package/dist/env/skill/index.d.ts +0 -1266
  37. package/dist/env/skill/index.js +0 -34
  38. package/dist/env/skill/tool/index.d.ts +0 -193
  39. package/dist/env/skill/tool/index.js +0 -9
  40. package/dist/env/task/delegate/index.d.ts +0 -1612
  41. package/dist/env/task/delegate/index.js +0 -18
  42. package/dist/env/task/events/index.d.ts +0 -171
  43. package/dist/env/task/events/index.js +0 -7
  44. package/dist/env/task/hooks/index.d.ts +0 -624
  45. package/dist/env/task/hooks/index.js +0 -7
  46. package/dist/env/task/index.d.ts +0 -1553
  47. package/dist/env/task/index.js +0 -34
  48. package/dist/env/task/plugins/index.d.ts +0 -466
  49. package/dist/env/task/plugins/index.js +0 -23
  50. package/dist/env/task/storage/index.d.ts +0 -241
  51. package/dist/env/task/storage/index.js +0 -14
  52. package/dist/env/task/tools/index.d.ts +0 -1485
  53. package/dist/env/task/tools/index.js +0 -17
  54. package/dist/env/task/tools/operation/index.d.ts +0 -1484
  55. package/dist/env/task/tools/operation/index.js +0 -15
  56. package/dist/env/tool/built-in/index.d.ts +0 -218
  57. package/dist/env/tool/built-in/index.js +0 -25
  58. package/dist/env/tool/index.d.ts +0 -1396
  59. package/dist/env/tool/index.js +0 -39
  60. package/dist/env/workflow/decorators/index.d.ts +0 -2161
  61. package/dist/env/workflow/decorators/index.js +0 -27
  62. package/dist/env/workflow/engine/index.d.ts +0 -3453
  63. package/dist/env/workflow/engine/index.js +0 -28
  64. package/dist/env/workflow/index.d.ts +0 -3546
  65. package/dist/env/workflow/index.js +0 -136
  66. package/dist/env/workflow/nodes/index.d.ts +0 -2092
  67. package/dist/env/workflow/nodes/index.js +0 -19
  68. package/dist/env/workflow/service/index.d.ts +0 -227
  69. package/dist/env/workflow/service/index.js +0 -13
  70. package/dist/env/workflow/storage/index.d.ts +0 -165
  71. package/dist/env/workflow/storage/index.js +0 -27
  72. package/dist/env/workflow/tools/index.d.ts +0 -416
  73. package/dist/env/workflow/tools/index.js +0 -159
  74. package/dist/env/workflow/types/index.d.ts +0 -2255
  75. package/dist/env/workflow/types/index.js +0 -98
  76. package/dist/env/workflow/utils/index.d.ts +0 -2031
  77. package/dist/env/workflow/utils/index.js +0 -637
  78. package/dist/index.d.ts +0 -7858
  79. package/dist/index.js +0 -399
  80. package/dist/shared/@ai-setting/roy-agent-core-0rtxwr28.js +0 -258
  81. package/dist/shared/@ai-setting/roy-agent-core-0vbdz0x7.js +0 -36
  82. package/dist/shared/@ai-setting/roy-agent-core-1akcqxj9.js +0 -349
  83. package/dist/shared/@ai-setting/roy-agent-core-1ce3fqrk.js +0 -117
  84. package/dist/shared/@ai-setting/roy-agent-core-2dhd60aw.js +0 -11
  85. package/dist/shared/@ai-setting/roy-agent-core-3jywqmdd.js +0 -393
  86. package/dist/shared/@ai-setting/roy-agent-core-3rr5k71j.js +0 -200
  87. package/dist/shared/@ai-setting/roy-agent-core-44hnfb02.js +0 -299
  88. package/dist/shared/@ai-setting/roy-agent-core-4t40mkpv.js +0 -206
  89. package/dist/shared/@ai-setting/roy-agent-core-4txzpsbt.js +0 -393
  90. package/dist/shared/@ai-setting/roy-agent-core-5x94xmt6.js +0 -350
  91. package/dist/shared/@ai-setting/roy-agent-core-69jskqjg.js +0 -180
  92. package/dist/shared/@ai-setting/roy-agent-core-6kvtahqv.js +0 -408
  93. package/dist/shared/@ai-setting/roy-agent-core-7fgf85wc.js +0 -284
  94. package/dist/shared/@ai-setting/roy-agent-core-81w1963m.js +0 -762
  95. package/dist/shared/@ai-setting/roy-agent-core-8gxth0eh.js +0 -10
  96. package/dist/shared/@ai-setting/roy-agent-core-92z6t4he.js +0 -14
  97. package/dist/shared/@ai-setting/roy-agent-core-93zfb3r1.js +0 -922
  98. package/dist/shared/@ai-setting/roy-agent-core-9yxb3ty9.js +0 -15
  99. package/dist/shared/@ai-setting/roy-agent-core-b0x5dda6.js +0 -1130
  100. package/dist/shared/@ai-setting/roy-agent-core-bcbqy27c.js +0 -14
  101. package/dist/shared/@ai-setting/roy-agent-core-bvr1761x.js +0 -653
  102. package/dist/shared/@ai-setting/roy-agent-core-ctdhjv68.js +0 -93
  103. package/dist/shared/@ai-setting/roy-agent-core-d7cyjkf7.js +0 -872
  104. package/dist/shared/@ai-setting/roy-agent-core-dh9d7a3m.js +0 -11
  105. package/dist/shared/@ai-setting/roy-agent-core-e25xkv53.js +0 -64
  106. package/dist/shared/@ai-setting/roy-agent-core-eajcvp4e.js +0 -378
  107. package/dist/shared/@ai-setting/roy-agent-core-f7q2x5z6.js +0 -492
  108. package/dist/shared/@ai-setting/roy-agent-core-fs0mn2jk.js +0 -52
  109. package/dist/shared/@ai-setting/roy-agent-core-g1s2h0e5.js +0 -171
  110. package/dist/shared/@ai-setting/roy-agent-core-g99pxzn5.js +0 -862
  111. package/dist/shared/@ai-setting/roy-agent-core-gbqcyegm.js +0 -1387
  112. package/dist/shared/@ai-setting/roy-agent-core-gjq1yk68.js +0 -208
  113. package/dist/shared/@ai-setting/roy-agent-core-gq20wsgv.js +0 -139
  114. package/dist/shared/@ai-setting/roy-agent-core-gwc4h96n.js +0 -534
  115. package/dist/shared/@ai-setting/roy-agent-core-jfh9q2qh.js +0 -204
  116. package/dist/shared/@ai-setting/roy-agent-core-jvatggbb.js +0 -603
  117. package/dist/shared/@ai-setting/roy-agent-core-kkbwepqb.js +0 -97
  118. package/dist/shared/@ai-setting/roy-agent-core-pjr12nnd.js +0 -587
  119. package/dist/shared/@ai-setting/roy-agent-core-psv4v63c.js +0 -176
  120. package/dist/shared/@ai-setting/roy-agent-core-psvxt4c9.js +0 -60
  121. package/dist/shared/@ai-setting/roy-agent-core-qqceba6k.js +0 -442
  122. package/dist/shared/@ai-setting/roy-agent-core-qxhq8ven.js +0 -57
  123. package/dist/shared/@ai-setting/roy-agent-core-qxnbvgwe.js +0 -66
  124. package/dist/shared/@ai-setting/roy-agent-core-r9ezzemr.js +0 -10
  125. package/dist/shared/@ai-setting/roy-agent-core-rhmtwnw1.js +0 -267
  126. package/dist/shared/@ai-setting/roy-agent-core-rvv6ydff.js +0 -584
  127. package/dist/shared/@ai-setting/roy-agent-core-rvxg1wps.js +0 -102
  128. package/dist/shared/@ai-setting/roy-agent-core-satmq6sh.js +0 -549
  129. package/dist/shared/@ai-setting/roy-agent-core-sx7wsvnn.js +0 -15
  130. package/dist/shared/@ai-setting/roy-agent-core-t94ktchq.js +0 -213
  131. package/dist/shared/@ai-setting/roy-agent-core-vf215qfv.js +0 -812
  132. package/dist/shared/@ai-setting/roy-agent-core-vkz81f7v.js +0 -1316
  133. package/dist/shared/@ai-setting/roy-agent-core-vn2bc59q.js +0 -1205
  134. package/dist/shared/@ai-setting/roy-agent-core-wa1kzqky.js +0 -328
  135. package/dist/shared/@ai-setting/roy-agent-core-wft9ra24.js +0 -20
  136. package/dist/shared/@ai-setting/roy-agent-core-wrcy0h6z.js +0 -2098
  137. package/dist/shared/@ai-setting/roy-agent-core-xq8hhqb8.js +0 -419
  138. package/dist/shared/@ai-setting/roy-agent-core-xs5rsgat.js +0 -368
  139. package/dist/shared/@ai-setting/roy-agent-core-zbkpc41z.js +0 -377
  140. package/dist/shared/@ai-setting/roy-agent-core-zgypchmt.js +0 -172
  141. package/dist/shared/@ai-setting/roy-agent-core-zpn0bqa8.js +0 -103
@@ -1,1316 +0,0 @@
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 };