@ai-setting/roy-agent-core 1.4.12 → 1.4.13

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 (57) hide show
  1. package/dist/index.d.ts +7825 -0
  2. package/dist/index.js +13073 -19300
  3. package/dist/shared/chunk-0q6s9wm6.js +249 -0
  4. package/dist/shared/chunk-1aakcfp1.js +15 -0
  5. package/dist/shared/chunk-1d4rwms4.js +25 -0
  6. package/dist/shared/chunk-1pf5mfgd.js +82 -0
  7. package/dist/shared/chunk-1qwabsm0.js +154 -0
  8. package/dist/shared/chunk-25x2pdtp.js +107 -0
  9. package/dist/shared/chunk-2b5kbhx3.js +366 -0
  10. package/dist/shared/chunk-91bas8w5.js +20 -0
  11. package/dist/shared/chunk-9qzt1v1p.js +10 -0
  12. package/dist/shared/chunk-a9qmy3sc.js +296 -0
  13. package/dist/shared/chunk-g6j5n3gv.js +549 -0
  14. package/dist/shared/chunk-hs7tbmje.js +24 -0
  15. package/dist/shared/chunk-mf5xqbdh.js +14 -0
  16. package/dist/shared/chunk-q9j99fsm.js +368 -0
  17. package/dist/{env/workflow/engine/index.js → shared/chunk-rncy3rtd.js} +330 -1581
  18. package/dist/shared/chunk-t1rh6jtm.js +205 -0
  19. package/dist/{env/task/hooks/index.js → shared/chunk-wbkh7wat.js} +3 -18
  20. package/dist/shared/chunk-yqmx37vm.js +10 -0
  21. package/dist/shared/chunk-ze20rksg.js +102 -0
  22. package/package.json +7 -7
  23. package/dist/config/index.js +0 -1670
  24. package/dist/env/agent/index.js +0 -3035
  25. package/dist/env/commands/index.js +0 -1685
  26. package/dist/env/debug/formatters/index.js +0 -639
  27. package/dist/env/debug/index.js +0 -2300
  28. package/dist/env/hook/index.js +0 -273
  29. package/dist/env/index.js +0 -12591
  30. package/dist/env/llm/index.js +0 -2736
  31. package/dist/env/log-trace/index.js +0 -1779
  32. package/dist/env/mcp/index.js +0 -2173
  33. package/dist/env/mcp/tool/index.js +0 -1149
  34. package/dist/env/memory/built-in/index.js +0 -225
  35. package/dist/env/memory/index.js +0 -2171
  36. package/dist/env/memory/plugin/index.js +0 -1263
  37. package/dist/env/prompt/index.js +0 -2107
  38. package/dist/env/session/index.js +0 -3594
  39. package/dist/env/session/storage/index.js +0 -2049
  40. package/dist/env/skill/index.js +0 -1635
  41. package/dist/env/skill/tool/index.js +0 -114
  42. package/dist/env/task/delegate/index.js +0 -1844
  43. package/dist/env/task/index.js +0 -3578
  44. package/dist/env/task/plugins/index.js +0 -1626
  45. package/dist/env/task/storage/index.js +0 -1464
  46. package/dist/env/task/tools/index.js +0 -344
  47. package/dist/env/task/tools/operation/index.js +0 -270
  48. package/dist/env/tool/built-in/index.js +0 -1151
  49. package/dist/env/tool/index.js +0 -2284
  50. package/dist/env/workflow/decorators/index.js +0 -449
  51. package/dist/env/workflow/index.js +0 -6214
  52. package/dist/env/workflow/nodes/index.js +0 -650
  53. package/dist/env/workflow/service/index.js +0 -262
  54. package/dist/env/workflow/storage/index.js +0 -1236
  55. package/dist/env/workflow/tools/index.js +0 -1081
  56. package/dist/env/workflow/types/index.js +0 -479
  57. package/dist/env/workflow/utils/index.js +0 -1631
@@ -1,2736 +0,0 @@
1
- // @bun
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- function __accessProp(key) {
7
- return this[key];
8
- }
9
- var __toCommonJS = (from) => {
10
- var entry = (__moduleCache ??= new WeakMap).get(from), desc;
11
- if (entry)
12
- return entry;
13
- entry = __defProp({}, "__esModule", { value: true });
14
- if (from && typeof from === "object" || typeof from === "function") {
15
- for (var key of __getOwnPropNames(from))
16
- if (!__hasOwnProp.call(entry, key))
17
- __defProp(entry, key, {
18
- get: __accessProp.bind(from, key),
19
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
20
- });
21
- }
22
- __moduleCache.set(from, entry);
23
- return entry;
24
- };
25
- var __moduleCache;
26
- var __returnValue = (v) => v;
27
- function __exportSetter(name, newValue) {
28
- this[name] = __returnValue.bind(null, newValue);
29
- }
30
- var __export = (target, all) => {
31
- for (var name in all)
32
- __defProp(target, name, {
33
- get: all[name],
34
- enumerable: true,
35
- configurable: true,
36
- set: __exportSetter.bind(all, name)
37
- });
38
- };
39
- var __legacyDecorateClassTS = function(decorators, target, key, desc) {
40
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
41
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
42
- r = Reflect.decorate(decorators, target, key, desc);
43
- else
44
- for (var i = decorators.length - 1;i >= 0; i--)
45
- if (d = decorators[i])
46
- r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
47
- return c > 3 && r && Object.defineProperty(target, key, r), r;
48
- };
49
- var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
50
- var __require = import.meta.require;
51
-
52
- // packages/core/src/env/log-trace/span-storage.ts
53
- var exports_span_storage = {};
54
- __export(exports_span_storage, {
55
- SQLiteSpanStorage: () => SQLiteSpanStorage
56
- });
57
-
58
- class SQLiteSpanStorage {
59
- db = null;
60
- dbPath;
61
- initialized = false;
62
- constructor(dbPath) {
63
- if (!dbPath) {
64
- throw new Error("SQLiteSpanStorage requires a valid dbPath parameter");
65
- }
66
- if (dbPath === ":memory:") {
67
- throw new Error("SQLiteSpanStorage does not support :memory: mode. Use a file path instead.");
68
- }
69
- this.dbPath = dbPath;
70
- }
71
- async initialize() {
72
- if (this.initialized)
73
- return;
74
- const { Database } = __require("bun:sqlite");
75
- const fs = __require("fs");
76
- const path = __require("path");
77
- const dir = path.dirname(this.dbPath);
78
- if (!fs.existsSync(dir)) {
79
- fs.mkdirSync(dir, { recursive: true });
80
- }
81
- this.db = new Database(this.dbPath);
82
- this.db.run("PRAGMA journal_mode=WAL");
83
- this.db.run("PRAGMA busy_timeout=5000");
84
- this.db.run("PRAGMA synchronous=NORMAL");
85
- this.db.run(`
86
- CREATE TABLE IF NOT EXISTS span (
87
- span_id TEXT PRIMARY KEY,
88
- trace_id TEXT NOT NULL,
89
- parent_span_id TEXT,
90
- name TEXT NOT NULL,
91
- kind TEXT NOT NULL,
92
- status TEXT NOT NULL,
93
- start_time INTEGER NOT NULL,
94
- end_time INTEGER,
95
- attributes TEXT,
96
- result TEXT,
97
- error TEXT,
98
- time_created INTEGER NOT NULL
99
- )
100
- `);
101
- this.db.run("CREATE INDEX IF NOT EXISTS idx_span_trace ON span(trace_id)");
102
- this.db.run("CREATE INDEX IF NOT EXISTS idx_span_parent ON span(parent_span_id)");
103
- this.initialized = true;
104
- }
105
- save(span) {
106
- if (!this.db)
107
- return;
108
- const stmt = this.db.prepare(`
109
- INSERT OR REPLACE INTO span
110
- (span_id, trace_id, parent_span_id, name, kind, status, start_time, end_time, attributes, result, error, time_created)
111
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
112
- `);
113
- const resultValue = span.result !== undefined && span.result !== null ? typeof span.result === "string" ? span.result : JSON.stringify(span.result) : null;
114
- stmt.run(span.spanId, span.traceId, span.parentSpanId || null, span.name, span.kind, span.status, span.startTime, span.endTime || null, JSON.stringify(span.attributes), resultValue, span.error || null, Date.now());
115
- }
116
- saveBatch(spans) {
117
- for (const span of spans) {
118
- this.save(span);
119
- }
120
- }
121
- findByTraceId(traceId) {
122
- if (!this.db)
123
- return [];
124
- const rows = this.db.prepare("SELECT * FROM span WHERE trace_id = ?").all(traceId);
125
- return this.buildTree(rows.map(this.rowToSpan));
126
- }
127
- findBySpanId(spanId) {
128
- if (!this.db)
129
- return;
130
- const row = this.db.prepare("SELECT * FROM span WHERE span_id = ?").get(spanId);
131
- return row ? this.rowToSpan(row) : undefined;
132
- }
133
- listTraces(limit = 10) {
134
- if (!this.db)
135
- return [];
136
- const rows = this.db.prepare(`
137
- SELECT trace_id,
138
- MIN(start_time) as start_time,
139
- MAX(end_time) as end_time,
140
- COUNT(*) as span_count
141
- FROM span
142
- GROUP BY trace_id
143
- ORDER BY start_time DESC
144
- LIMIT ?
145
- `).all(limit);
146
- return rows.map((row) => ({
147
- traceId: row.trace_id,
148
- rootSpanName: "unknown",
149
- startTime: row.start_time,
150
- endTime: row.end_time,
151
- duration: row.end_time - row.start_time,
152
- spanCount: row.span_count,
153
- status: "ok"
154
- }));
155
- }
156
- deleteByTraceId(traceId) {
157
- if (this.db) {
158
- this.db.prepare("DELETE FROM span WHERE trace_id = ?").run(traceId);
159
- }
160
- }
161
- close() {
162
- if (this.db) {
163
- this.db.close();
164
- this.db = null;
165
- }
166
- }
167
- rowToSpan(row) {
168
- return {
169
- traceId: row.trace_id,
170
- spanId: row.span_id,
171
- parentSpanId: row.parent_span_id,
172
- name: row.name,
173
- kind: row.kind,
174
- status: row.status,
175
- startTime: row.start_time,
176
- endTime: row.end_time,
177
- attributes: row.attributes ? JSON.parse(row.attributes) : {},
178
- result: row.result || undefined,
179
- error: row.error || undefined
180
- };
181
- }
182
- buildTree(spans) {
183
- const spanMap = new Map;
184
- const roots = [];
185
- for (const span of spans) {
186
- spanMap.set(span.spanId, { ...span, children: [] });
187
- }
188
- for (const span of spanMap.values()) {
189
- if (span.parentSpanId) {
190
- const parent = spanMap.get(span.parentSpanId);
191
- if (parent) {
192
- parent.children.push(span);
193
- } else {
194
- roots.push(span);
195
- }
196
- } else {
197
- roots.push(span);
198
- }
199
- }
200
- return roots;
201
- }
202
- }
203
-
204
- // packages/core/src/env/log-trace/opentelemetry/propagation.ts
205
- function padLeft(str, length) {
206
- return str.padStart(length, "0");
207
- }
208
- function isValidHex(str) {
209
- return /^[0-9a-f]+$/i.test(str);
210
- }
211
- function serialize(context) {
212
- const traceId = padLeft(context.traceId.toLowerCase(), TRACE_ID_LENGTH);
213
- const spanId = padLeft(context.spanId.toLowerCase(), PARENT_SPAN_ID_LENGTH);
214
- const parts = [
215
- TRACE_CONTEXT_VERSION,
216
- traceId,
217
- spanId,
218
- TRACE_FLAGS_SAMPLED
219
- ];
220
- return parts.join("-");
221
- }
222
- function parse(traceparent) {
223
- if (!traceparent || typeof traceparent !== "string") {
224
- return;
225
- }
226
- const trimmed = traceparent.trim();
227
- const parts = trimmed.split("-");
228
- if (parts.length !== 4) {
229
- return;
230
- }
231
- const [version, traceId, senderSpanId, flags] = parts;
232
- if (version !== TRACE_CONTEXT_VERSION) {
233
- if (version > TRACE_CONTEXT_VERSION) {
234
- return;
235
- }
236
- }
237
- if (traceId.length !== TRACE_ID_LENGTH || !isValidHex(traceId)) {
238
- return;
239
- }
240
- if (senderSpanId.length !== PARENT_SPAN_ID_LENGTH || !isValidHex(senderSpanId)) {
241
- return;
242
- }
243
- if (flags.length !== FLAGS_LENGTH || !isValidHex(flags)) {
244
- return;
245
- }
246
- return {
247
- traceId: traceId.toLowerCase(),
248
- spanId: senderSpanId.toLowerCase()
249
- };
250
- }
251
- var TRACE_CONTEXT_VERSION = "00", TRACE_FLAGS_SAMPLED = "01", TRACEPARENT_HEADER = "TRACEPARENT", TRACE_ID_LENGTH = 32, PARENT_SPAN_ID_LENGTH = 16, FLAGS_LENGTH = 2, propagation;
252
- var init_propagation = __esm(() => {
253
- propagation = {
254
- inject(carrier, context) {
255
- carrier[TRACEPARENT_HEADER] = serialize(context);
256
- },
257
- extract(carrier) {
258
- const traceparent = carrier[TRACEPARENT_HEADER];
259
- if (!traceparent) {
260
- return;
261
- }
262
- return parse(traceparent);
263
- },
264
- getTraceparentHeader() {
265
- return TRACEPARENT_HEADER;
266
- }
267
- };
268
- });
269
-
270
- // packages/core/src/env/log-trace/types.ts
271
- var SpanKind, SpanStatus;
272
- var init_types = __esm(() => {
273
- ((SpanKind2) => {
274
- SpanKind2["CLIENT"] = "client";
275
- SpanKind2["SERVER"] = "server";
276
- SpanKind2["INTERNAL"] = "internal";
277
- })(SpanKind ||= {});
278
- ((SpanStatus2) => {
279
- SpanStatus2["OK"] = "ok";
280
- SpanStatus2["ERROR"] = "error";
281
- })(SpanStatus ||= {});
282
- });
283
-
284
- // packages/core/src/env/log-trace/opentelemetry/tracer-provider.ts
285
- var exports_tracer_provider = {};
286
- __export(exports_tracer_provider, {
287
- resetTracerProvider: () => resetTracerProvider,
288
- getTracerProvider: () => getTracerProvider,
289
- OTelTracerProvider: () => OTelTracerProvider
290
- });
291
-
292
- class OTelSpanImpl {
293
- name;
294
- kind;
295
- spanContext;
296
- attributes = {};
297
- startTime;
298
- endTime;
299
- error;
300
- storage;
301
- parentSpanContext;
302
- onEnd;
303
- constructor(name, spanContext, storage, parentSpanContext, onEnd) {
304
- this.name = name;
305
- this.kind = "internal";
306
- this.spanContext = spanContext;
307
- this.parentSpanContext = parentSpanContext;
308
- this.startTime = Date.now();
309
- this.storage = storage;
310
- this.onEnd = onEnd;
311
- }
312
- setAttribute(key, value) {
313
- this.attributes[key] = value;
314
- }
315
- addEvent(name, attributes) {
316
- const events = this.attributes._events || [];
317
- events.push({ name, attributes });
318
- this.attributes._events = events;
319
- }
320
- end(result, error) {
321
- this.endTime = Date.now();
322
- if (error) {
323
- this.error = error.message;
324
- }
325
- this.storage.save({
326
- traceId: this.spanContext.traceId,
327
- spanId: this.spanContext.spanId,
328
- parentSpanId: this.spanContext.parentSpanId,
329
- name: this.name,
330
- kind: this.kind,
331
- status: error ? "error" /* ERROR */ : "ok" /* OK */,
332
- startTime: this.startTime,
333
- endTime: this.endTime,
334
- attributes: this.attributes,
335
- result,
336
- error: this.error
337
- });
338
- this.onEnd?.();
339
- }
340
- }
341
-
342
- class OTelTracerImpl {
343
- name;
344
- version;
345
- storage;
346
- currentContext;
347
- activeSpans = new Map;
348
- onSpanEndCallback;
349
- provider;
350
- constructor(name, version, storage, provider) {
351
- this.name = name;
352
- this.version = version;
353
- this.storage = storage;
354
- this.provider = provider;
355
- }
356
- setOnSpanEndCallback(callback) {
357
- this.onSpanEndCallback = callback;
358
- }
359
- getActiveSpanCount() {
360
- return this.activeSpans.size;
361
- }
362
- startSpan(name, options) {
363
- const parentFromOptions = options?.parent;
364
- const hasExplicitParent = options && "parent" in options;
365
- const globalContext = this.provider.getGlobalContext();
366
- let effectiveParentContext;
367
- if (hasExplicitParent) {
368
- effectiveParentContext = parentFromOptions;
369
- } else if (this.currentContext) {
370
- effectiveParentContext = this.currentContext;
371
- } else if (globalContext) {
372
- effectiveParentContext = globalContext;
373
- }
374
- const traceId = effectiveParentContext?.traceId || this.generateTraceId();
375
- const spanId = this.generateSpanId();
376
- let parentSpanId;
377
- if (hasExplicitParent && parentFromOptions) {
378
- parentSpanId = parentFromOptions.parentSpanId || parentFromOptions.spanId;
379
- } else if (this.currentContext) {
380
- parentSpanId = this.currentContext.spanId;
381
- } else if (globalContext) {
382
- parentSpanId = globalContext.spanId;
383
- }
384
- const spanContext = {
385
- traceId,
386
- spanId,
387
- parentSpanId
388
- };
389
- this.currentContext = {
390
- traceId,
391
- spanId,
392
- parentSpanId
393
- };
394
- const span = new OTelSpanImpl(name, spanContext, this.storage, effectiveParentContext, () => this.handleSpanEnd(spanContext.spanId, effectiveParentContext?.spanId));
395
- if (options?.attributes) {
396
- for (const [key, value] of Object.entries(options.attributes)) {
397
- span.setAttribute(key, value);
398
- }
399
- }
400
- this.activeSpans.set(spanId, span);
401
- return span;
402
- }
403
- injectToEnv(env) {
404
- if (this.currentContext) {
405
- propagation.inject(env, this.currentContext);
406
- }
407
- }
408
- getCurrentContext() {
409
- return this.currentContext;
410
- }
411
- setCurrentContext(context) {
412
- this.currentContext = context;
413
- }
414
- endSpan(span) {
415
- const spanContext = span.spanContext;
416
- this.activeSpans.delete(spanContext.spanId);
417
- const currentTraceId = this.currentContext?.traceId;
418
- if (spanContext.parentSpanId) {
419
- const parentSpan = this.activeSpans.get(spanContext.parentSpanId);
420
- if (parentSpan) {
421
- const parentContext = {
422
- traceId: spanContext.traceId,
423
- spanId: parentSpan.spanContext.spanId,
424
- parentSpanId: parentSpan.spanContext.parentSpanId
425
- };
426
- this.currentContext = parentContext;
427
- return parentContext;
428
- }
429
- if (currentTraceId) {
430
- const parentContext = {
431
- traceId: currentTraceId,
432
- spanId: spanContext.parentSpanId,
433
- parentSpanId: undefined
434
- };
435
- this.currentContext = parentContext;
436
- return parentContext;
437
- }
438
- }
439
- this.currentContext = undefined;
440
- return;
441
- }
442
- generateTraceId() {
443
- const timestamp = Date.now().toString(16).padStart(12, "0");
444
- const random = Array.from({ length: 5 }, () => Math.floor(Math.random() * 4294967295).toString(16).padStart(8, "0")).join("");
445
- return (timestamp + random).slice(0, 32).padStart(32, "0");
446
- }
447
- generateSpanId() {
448
- return Math.floor(Math.random() * 18446744073709552000).toString(16).padStart(16, "0");
449
- }
450
- handleSpanEnd(spanId, parentSpanId) {
451
- this.activeSpans.delete(spanId);
452
- const currentTraceId = this.currentContext?.traceId;
453
- if (parentSpanId) {
454
- const parentSpan = this.activeSpans.get(parentSpanId);
455
- if (parentSpan) {
456
- this.currentContext = {
457
- traceId: parentSpan.spanContext.traceId,
458
- spanId: parentSpan.spanContext.spanId,
459
- parentSpanId: parentSpan.spanContext.parentSpanId
460
- };
461
- return;
462
- }
463
- if (currentTraceId) {
464
- this.currentContext = {
465
- traceId: currentTraceId,
466
- spanId: parentSpanId,
467
- parentSpanId: undefined
468
- };
469
- return;
470
- }
471
- }
472
- this.currentContext = undefined;
473
- }
474
- }
475
-
476
- class OTelTracerProvider {
477
- tracers = new Map;
478
- storage;
479
- initialized = false;
480
- globalContext;
481
- constructor(storage, dbPath) {
482
- this.storage = storage || new SQLiteSpanStorage(dbPath || getDefaultDbPath());
483
- }
484
- async initialize() {
485
- if (this.initialized)
486
- return;
487
- await this.storage.initialize();
488
- this.initialized = true;
489
- }
490
- isInitialized() {
491
- return this.initialized;
492
- }
493
- getGlobalContext() {
494
- return this.globalContext;
495
- }
496
- setGlobalContext(context) {
497
- this.globalContext = context;
498
- }
499
- getTracer(name, version) {
500
- const key = `${name}@${version || "0.0.0"}`;
501
- let tracer = this.tracers.get(key);
502
- if (!tracer) {
503
- tracer = new OTelTracerImpl(name, version, this.storage, this);
504
- this.tracers.set(key, tracer);
505
- if (this.initialized) {
506
- this.restoreFromEnv(tracer);
507
- }
508
- }
509
- return tracer;
510
- }
511
- restoreFromEnv(tracer) {
512
- const extracted = propagation.extract(process.env);
513
- if (extracted) {
514
- tracer.setCurrentContext({
515
- traceId: extracted.traceId,
516
- spanId: extracted.spanId,
517
- parentSpanId: undefined
518
- });
519
- }
520
- }
521
- shutdown() {
522
- for (const tracer of this.tracers.values()) {}
523
- this.tracers.clear();
524
- this.storage.close();
525
- this.initialized = false;
526
- this.globalContext = undefined;
527
- delete process.env["TRACEPARENT"];
528
- delete process.env["TRACE_ID"];
529
- delete process.env["LOG_TRACE_REQUEST_ID"];
530
- }
531
- getStorage() {
532
- return this.storage;
533
- }
534
- }
535
- function getTracerProvider() {
536
- if (!providerInstance) {
537
- providerInstance = new OTelTracerProvider;
538
- }
539
- return providerInstance;
540
- }
541
- function resetTracerProvider() {
542
- if (providerInstance) {
543
- providerInstance.shutdown();
544
- providerInstance = null;
545
- }
546
- }
547
- function getDefaultDbPath() {
548
- const os = __require("os");
549
- const path = __require("path");
550
- const home = os.homedir();
551
- const dataHome = process.env.XDG_DATA_HOME || path.join(home, ".local", "share");
552
- return path.join(dataHome, "roy-agent", "traces.db");
553
- }
554
- var providerInstance = null;
555
- var init_tracer_provider = __esm(() => {
556
- init_propagation();
557
- init_types();
558
- });
559
-
560
- // packages/core/src/env/log-trace/logger.ts
561
- import { appendFileSync, existsSync, mkdirSync } from "fs";
562
- import { join } from "path";
563
- function simplifyFilePath(fullPath) {
564
- let path = fullPath.replace(/\\/g, "/");
565
- const bunfsMatch = path.match(/\/\$bunfs\/root\/(.+)$/);
566
- if (bunfsMatch) {
567
- const virtualPath = bunfsMatch[1];
568
- const packagesMatch = virtualPath.match(/(packages\/[^/]+\/src\/[^/]+\/.+)$/);
569
- if (packagesMatch) {
570
- return packagesMatch[1];
571
- }
572
- return virtualPath;
573
- }
574
- const fileProtocolMatch = path.match(/@roy-agent\+core@file\+([^/]+)\/node_modules\/@roy-agent\/core\/(.+)$/);
575
- if (fileProtocolMatch) {
576
- const rootPkg = fileProtocolMatch[1].replace(/\+/g, "/");
577
- const remaining = fileProtocolMatch[2];
578
- const prefix = rootPkg;
579
- const suffix = remaining.startsWith("src/") ? remaining : `src/${remaining}`;
580
- return `${prefix}/${suffix}`;
581
- }
582
- const packagesRootMatch = path.match(/(packages\/[^/]+\/src\/[^/]+\/.+)$/);
583
- if (packagesRootMatch) {
584
- return packagesRootMatch[1];
585
- }
586
- const rootMarkers = ["packages/core/src", "packages/core", "packages"];
587
- for (const marker of rootMarkers) {
588
- const idx = path.indexOf(marker);
589
- if (idx !== -1) {
590
- return path.substring(idx);
591
- }
592
- }
593
- return path;
594
- }
595
- function getDefaultLogDir() {
596
- const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
597
- try {
598
- const xdg = __require("xdg-basedir");
599
- if (xdg.xdgData) {
600
- return join(xdg.xdgData, "roy-agent", "logs");
601
- }
602
- } catch {}
603
- return join(home, ".local", "share", "roy-agent", "logs");
604
- }
605
- function isQuietMode() {
606
- return quietMode;
607
- }
608
- function setQuietMode(enabled) {
609
- quietMode = enabled;
610
- }
611
- function setConfigComponent(component) {
612
- configComponentInstance = component;
613
- }
614
- function getLogLevel() {
615
- if (configComponentInstance) {
616
- const level = configComponentInstance.get("log_trace.logging.level");
617
- if (level && ["debug", "info", "warn", "error"].includes(level)) {
618
- return level;
619
- }
620
- }
621
- const envLevel = process.env.LOG_LEVEL;
622
- if (envLevel && ["debug", "info", "warn", "error"].includes(envLevel)) {
623
- return envLevel;
624
- }
625
- return "info";
626
- }
627
- function getCategoryLogLevel(category) {
628
- if (configComponentInstance) {
629
- const levels = configComponentInstance.get("log_trace.logging.levels");
630
- if (levels && typeof levels === "object") {
631
- const categoryLevel = levels[category];
632
- if (categoryLevel && ["debug", "info", "warn", "error"].includes(categoryLevel)) {
633
- return categoryLevel;
634
- }
635
- }
636
- const directLevel = configComponentInstance.get(`log_trace.logging.levels.${category}`);
637
- if (directLevel && ["debug", "info", "warn", "error"].includes(directLevel)) {
638
- return directLevel;
639
- }
640
- }
641
- const envKey = `LOG_LEVEL_${category.toUpperCase().replace(/:/g, "_")}`;
642
- const envLevel = process.env[envKey];
643
- if (envLevel && ["debug", "info", "warn", "error"].includes(envLevel)) {
644
- return envLevel;
645
- }
646
- return getLogLevel();
647
- }
648
- function isAbsolutePath(path) {
649
- if (path.startsWith("/")) {
650
- return true;
651
- }
652
- if (/^[a-zA-Z]:[/\\]/.test(path)) {
653
- return true;
654
- }
655
- return false;
656
- }
657
- function expandHomeDir(path) {
658
- if (path.startsWith("~")) {
659
- const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
660
- return join(home, path.slice(1));
661
- }
662
- return path;
663
- }
664
- function getLogFile() {
665
- if (configComponentInstance) {
666
- const file = configComponentInstance.get("log_trace.logging.file");
667
- if (file) {
668
- return file;
669
- }
670
- }
671
- return "app.log";
672
- }
673
- function getLogDir() {
674
- if (configComponentInstance) {
675
- const dir = configComponentInstance.get("log_trace.logging.dir");
676
- if (dir) {
677
- return dir;
678
- }
679
- }
680
- return getDefaultLogDir();
681
- }
682
- function getMaxOutput() {
683
- if (configComponentInstance) {
684
- const maxOutput = configComponentInstance.get("log-trace.logging.maxOutput");
685
- if (typeof maxOutput === "number") {
686
- return maxOutput;
687
- }
688
- }
689
- return;
690
- }
691
- function setLogDirOverride(dir) {
692
- logDirOverride = dir;
693
- }
694
- function getLogDirOverride() {
695
- return logDirOverride;
696
- }
697
-
698
- class Logger {
699
- prefix;
700
- constructor(prefix) {
701
- this.prefix = prefix;
702
- }
703
- shouldLog(level) {
704
- const categoryLevel = getCategoryLogLevel(this.prefix);
705
- return levelPriority[level] >= levelPriority[categoryLevel];
706
- }
707
- ensureLogDirectory(dir) {
708
- if (!existsSync(dir)) {
709
- try {
710
- mkdirSync(dir, { recursive: true });
711
- } catch (err) {
712
- console.error("[Logger] Failed to create log directory:", dir, err);
713
- }
714
- }
715
- }
716
- getCallerLocation() {
717
- const originalLimit = Error.stackTraceLimit;
718
- Error.stackTraceLimit = 10;
719
- const err = new Error;
720
- Error.captureStackTrace(err, this.formatMessage);
721
- const stack = err.stack?.split(`
722
- `) || [];
723
- Error.stackTraceLimit = originalLimit;
724
- for (let i = 1;i < stack.length; i++) {
725
- const line = stack[i];
726
- if (line.includes("at ") && !line.includes("logger.ts") && !line.includes("formatMessage")) {
727
- const match = line.match(/at\s+.+\s+\((.+):(\d+):\d+\)/) || line.match(/at\s+(.+):(\d+):\d+/);
728
- if (match) {
729
- const filePath = match[1];
730
- const relativePath = this.getRelativePath(filePath);
731
- return {
732
- file: relativePath,
733
- line: parseInt(match[2], 10)
734
- };
735
- }
736
- }
737
- }
738
- return null;
739
- }
740
- getRelativePath(fullPath) {
741
- return simplifyFilePath(fullPath);
742
- }
743
- formatMessage(level, message, data) {
744
- const now = new Date;
745
- const timestamp = now.toLocaleString("zh-CN", {
746
- timeZone: "Asia/Shanghai",
747
- year: "numeric",
748
- month: "2-digit",
749
- day: "2-digit",
750
- hour: "2-digit",
751
- minute: "2-digit",
752
- second: "2-digit",
753
- hour12: false
754
- }).replace(/\//g, "-") + "." + String(now.getMilliseconds()).padStart(3, "0");
755
- const prefix = this.prefix ? `[${this.prefix}]` : "";
756
- let traceIdStr = "";
757
- try {
758
- const provider = getTracerProvider();
759
- const tracer = provider.getTracer("roy-tracer");
760
- const context = tracer.getCurrentContext();
761
- if (context?.traceId) {
762
- traceIdStr = `[traceId=${context.traceId}]`;
763
- }
764
- } catch {}
765
- let locationStr = "";
766
- if (data && typeof data === "object" && "callerLocation" in data) {
767
- const logData = data;
768
- locationStr = logData.callerLocation ? ` [${logData.callerLocation}]` : "";
769
- const { callerLocation: _callerLocation, ...rest } = logData;
770
- data = Object.keys(rest).length > 0 ? rest : undefined;
771
- } else {
772
- const location = this.getCallerLocation();
773
- if (location) {
774
- locationStr = ` [${location.file}:${location.line}]`;
775
- }
776
- }
777
- let formatted = `${timestamp} [${level.toUpperCase()}]${traceIdStr}${locationStr}${prefix} ${message}`;
778
- if (data !== undefined) {
779
- if (typeof data === "object") {
780
- formatted += " " + JSON.stringify(data).replace(/\n/g, "");
781
- } else {
782
- formatted += " " + String(data);
783
- }
784
- }
785
- const maxOutput = getMaxOutput();
786
- if (maxOutput && maxOutput > 0 && formatted.length > maxOutput) {
787
- formatted = formatted.substring(0, maxOutput) + " [TRUNCATED]";
788
- }
789
- return formatted;
790
- }
791
- writeToFile(message) {
792
- try {
793
- const dir = getLogDir();
794
- const filename = getLogFile();
795
- const expandedDir = expandHomeDir(dir);
796
- const resolvedDir = isAbsolutePath(expandedDir) ? expandedDir : join(process.cwd(), expandedDir);
797
- this.ensureLogDirectory(resolvedDir);
798
- const logFile = join(resolvedDir, filename);
799
- appendFileSync(logFile, message + `
800
- `, "utf-8");
801
- } catch (err) {
802
- console.error("[Logger] Failed to write to log file:", err);
803
- }
804
- }
805
- log(level, message, data) {
806
- if (!this.shouldLog(level))
807
- return;
808
- const formatted = this.formatMessage(level, message, data);
809
- this.writeToFile(formatted);
810
- if (!isQuietMode()) {
811
- const consoleMethod = level === "error" ? console.error : level === "warn" ? console.warn : level === "info" ? console.log : console.debug;
812
- consoleMethod(formatted);
813
- }
814
- }
815
- debug(message, data) {
816
- this.log("debug", message, data);
817
- }
818
- info(message, data) {
819
- this.log("info", message, data);
820
- }
821
- warn(message, data) {
822
- this.log("warn", message, data);
823
- }
824
- error(message, data) {
825
- this.log("error", message, data);
826
- }
827
- }
828
- function createLogger(prefix) {
829
- if (!loggerCache.has(prefix)) {
830
- loggerCache.set(prefix, new Logger(prefix));
831
- }
832
- return loggerCache.get(prefix);
833
- }
834
- function getLogLevels() {
835
- return ["debug", "info", "warn", "error"];
836
- }
837
- var configComponentInstance = null, quietMode = false, logDirOverride, levelPriority, loggerCache;
838
- var init_logger = __esm(() => {
839
- init_tracer_provider();
840
- levelPriority = {
841
- debug: 0,
842
- info: 1,
843
- warn: 2,
844
- error: 3
845
- };
846
- loggerCache = new Map;
847
- });
848
-
849
- // packages/core/src/env/log-trace/decorator.ts
850
- function Traced(options) {
851
- return function(target, propertyKey, descriptor) {
852
- const originalFn = descriptor.value;
853
- const spanName = options?.name || propertyKey;
854
- descriptor.value = wrapFunction(originalFn, spanName, {
855
- recordParams: options?.recordParams ?? true,
856
- recordResult: options?.recordResult ?? false,
857
- recordError: options?.recordError ?? true,
858
- log: options?.log ?? false,
859
- maxLogSize: options?.maxLogSize,
860
- paramFilter: options?.paramFilter
861
- });
862
- return descriptor;
863
- };
864
- }
865
- function TracedAs(name, options) {
866
- return Traced({ name, ...options });
867
- }
868
- function TracedLightweight(options) {
869
- return Traced({ recordParams: false, recordResult: false, log: options?.log ?? false, maxLogSize: options?.maxLogSize });
870
- }
871
- function wrapFunction(fn, name, options) {
872
- const shouldLog = options?.log ?? false;
873
- const getMaxLogSize = () => {
874
- if (options?.maxLogSize !== undefined) {
875
- return options.maxLogSize;
876
- }
877
- const configMaxOutput = getMaxOutput();
878
- return configMaxOutput !== undefined ? configMaxOutput : 500;
879
- };
880
- const truncate = (obj) => {
881
- if (obj === undefined)
882
- return;
883
- if (obj === null)
884
- return null;
885
- const maxLogSize = getMaxLogSize();
886
- let str;
887
- try {
888
- str = typeof obj === "string" ? obj : JSON.stringify(obj, null, 0).replace(/\n/g, "");
889
- } catch (e) {
890
- str = `[Object with circular reference: ${e instanceof Error ? e.message : String(e)}]`;
891
- }
892
- if (maxLogSize > 0 && str.length > maxLogSize) {
893
- return str.slice(0, maxLogSize) + " [TRUNCATED]";
894
- }
895
- try {
896
- return JSON.parse(str);
897
- } catch {
898
- return str;
899
- }
900
- };
901
- const truncateString = (obj) => {
902
- if (obj === undefined)
903
- return "undefined";
904
- if (obj === null)
905
- return "null";
906
- const maxLogSize = getMaxLogSize();
907
- let str;
908
- try {
909
- str = typeof obj === "string" ? obj : JSON.stringify(obj, null, 0).replace(/\n/g, "");
910
- } catch (e) {
911
- str = `[Object with circular reference: ${e instanceof Error ? e.message : String(e)}]`;
912
- }
913
- return maxLogSize > 0 && str.length > maxLogSize ? str.slice(0, maxLogSize) + " [TRUNCATED]" : str;
914
- };
915
- const TRACE_LOG_PREFIX = "[TRACE]";
916
- const getCallerLocation = () => {
917
- const originalLimit = Error.stackTraceLimit;
918
- Error.stackTraceLimit = 15;
919
- const err = new Error;
920
- Error.captureStackTrace(err, getCallerLocation);
921
- const stack = err.stack?.split(`
922
- `) || [];
923
- Error.stackTraceLimit = originalLimit;
924
- for (let i = 1;i < stack.length; i++) {
925
- const line = stack[i];
926
- if (line.includes("decorator.ts") || line.includes("decorator.js") || line.includes("logFn") || line.includes("getCallerLocation"))
927
- continue;
928
- const match = line.match(/at\s+.+\s+\((.+):(\d+):\d+\)/) || line.match(/at\s+(.+):(\d+):\d+/);
929
- if (match) {
930
- const filePath = match[1];
931
- if (!filePath || filePath === "native")
932
- continue;
933
- const relativePath = simplifyFilePath(filePath);
934
- return `${relativePath}:${match[2]}`;
935
- }
936
- }
937
- return "";
938
- };
939
- const logFn = (event, argsOrData, callerLocation) => {
940
- if (!shouldLog)
941
- return;
942
- const logger2 = createLogger("traced:" + name);
943
- const tag = event === "enter" ? ">>>" : event === "quit" ? "<<<" : "!!!";
944
- const prefix = `${TRACE_LOG_PREFIX} ${tag} ${name}`;
945
- const originalFnLocation = callerLocation || getCallerLocation();
946
- const logMessage = (msg, data) => {
947
- if (data !== undefined) {
948
- logger2.info(`${prefix} ${msg}: ${truncateString(data)}`, { callerLocation: originalFnLocation });
949
- } else {
950
- logger2.info(`${prefix} ${msg}`, { callerLocation: originalFnLocation });
951
- }
952
- };
953
- if (event === "enter") {
954
- logMessage("enter", argsOrData);
955
- } else if (event === "quit") {
956
- logMessage("quit", options?.recordResult ? argsOrData : undefined);
957
- } else {
958
- logMessage("error", argsOrData);
959
- }
960
- };
961
- return function(...args) {
962
- const tracer = getTracerProvider().getTracer("roy-tracer");
963
- const attributes = {};
964
- if (options?.recordParams !== false) {
965
- if (options?.paramFilter) {
966
- attributes["params"] = truncate(options.paramFilter(args));
967
- } else {
968
- attributes["params"] = truncate(args);
969
- }
970
- }
971
- const callerLocation = getCallerLocation();
972
- logFn("enter", args, callerLocation);
973
- const span = tracer.startSpan(name, { attributes });
974
- try {
975
- const result = fn.call(this, ...args);
976
- if (result && typeof result === "object" && typeof result.then === "function") {
977
- return result.then((resolved) => {
978
- span.setAttribute("result", JSON.stringify(truncate(resolved)));
979
- span.end(resolved);
980
- logFn("quit", options?.recordResult ? resolved : undefined, callerLocation);
981
- return resolved;
982
- }, (rejected) => {
983
- span.setAttribute("error", String(rejected));
984
- span.end(undefined, rejected instanceof Error ? rejected : new Error(String(rejected)));
985
- logFn("error", rejected.message, callerLocation);
986
- throw rejected;
987
- });
988
- }
989
- span.setAttribute("result", JSON.stringify(truncate(result)));
990
- span.end(result);
991
- logFn("quit", options?.recordResult ? result : undefined, callerLocation);
992
- return result;
993
- } catch (error) {
994
- span.setAttribute("error", String(error));
995
- span.end(undefined, error instanceof Error ? error : new Error(String(error)));
996
- logFn("error", error.message, callerLocation);
997
- throw error;
998
- }
999
- };
1000
- }
1001
- var init_decorator = __esm(() => {
1002
- init_tracer_provider();
1003
- init_logger();
1004
- });
1005
-
1006
- // packages/core/src/env/hook/types.ts
1007
- function createHook(meta, fn) {
1008
- return {
1009
- ...meta,
1010
- execute: fn
1011
- };
1012
- }
1013
- function createPriorityHook(name, fn, priority) {
1014
- return createHook({ name, priority }, fn);
1015
- }
1016
- // packages/core/src/env/hook/hook-manager.ts
1017
- class HookManager {
1018
- _hooks = new Map;
1019
- componentName;
1020
- componentVersion;
1021
- defaultPriority;
1022
- constructor(options = {}) {
1023
- this.componentName = options.componentName ?? "unknown";
1024
- this.componentVersion = options.componentVersion ?? "0.0.0";
1025
- this.defaultPriority = options.defaultPriority ?? 0;
1026
- }
1027
- register(hookPoint, hook) {
1028
- const hooks = this.getOrCreateHooks(hookPoint);
1029
- hooks.push(hook);
1030
- }
1031
- registerMany(hookPoint, hooks) {
1032
- const hookList = this.getOrCreateHooks(hookPoint);
1033
- hookList.push(...hooks);
1034
- }
1035
- unregister(hookPoint, name) {
1036
- const hooks = this._hooks.get(hookPoint);
1037
- if (!hooks)
1038
- return false;
1039
- const index = hooks.findIndex((h) => h.name === name);
1040
- if (index === -1)
1041
- return false;
1042
- hooks.splice(index, 1);
1043
- return true;
1044
- }
1045
- unregisterAll(hookPoint) {
1046
- this._hooks.delete(hookPoint);
1047
- }
1048
- async execute(hookPoint, data, metadata = {}) {
1049
- const hooks = this._hooks.get(hookPoint);
1050
- if (!hooks || hooks.length === 0)
1051
- return;
1052
- const sortedHooks = this.sortHooks(hooks);
1053
- const ctx = this.createContext(hookPoint, data, metadata, "before");
1054
- for (const hook of sortedHooks) {
1055
- await this.safeExecute(hook, ctx);
1056
- }
1057
- }
1058
- async executeAndCollect(hookPoint, data, metadata = {}) {
1059
- const hooks = this._hooks.get(hookPoint);
1060
- if (!hooks || hooks.length === 0)
1061
- return [];
1062
- const sortedHooks = this.sortHooks(hooks);
1063
- const ctx = this.createContext(hookPoint, data, metadata, "before");
1064
- const results = [];
1065
- for (const hook of sortedHooks) {
1066
- const result = await this.safeExecuteAndReturn(hook, ctx);
1067
- if (result !== undefined) {
1068
- results.push(result);
1069
- }
1070
- }
1071
- return results;
1072
- }
1073
- count(hookPoint) {
1074
- return this._hooks.get(hookPoint)?.length ?? 0;
1075
- }
1076
- clear() {
1077
- this._hooks.clear();
1078
- }
1079
- getHookPoints() {
1080
- return Array.from(this._hooks.keys());
1081
- }
1082
- hasHooks(hookPoint) {
1083
- return this.count(hookPoint) > 0;
1084
- }
1085
- setComponentInfo(name, version) {
1086
- this.componentName = name;
1087
- this.componentVersion = version;
1088
- }
1089
- get hooks() {
1090
- return this._hooks;
1091
- }
1092
- async executeWithIntervention(hookPoint, data, metadata = {}) {
1093
- const hooks = this._hooks.get(hookPoint);
1094
- if (!hooks || hooks.length === 0) {
1095
- return { stopped: false, results: [] };
1096
- }
1097
- const sortedHooks = this.sortHooks(hooks);
1098
- const ctx = this.createContext(hookPoint, data, metadata, "before");
1099
- const results = [];
1100
- let stopped = false;
1101
- let action;
1102
- for (const hook of sortedHooks) {
1103
- if (stopped)
1104
- break;
1105
- const result = await this.safeExecuteAndReturn(hook, ctx);
1106
- results.push(result);
1107
- if (result && typeof result === "object" && "stopped" in result) {
1108
- const hookResult = result;
1109
- if (hookResult.stopped) {
1110
- stopped = true;
1111
- action = hookResult.action;
1112
- }
1113
- }
1114
- }
1115
- return { stopped, action, results };
1116
- }
1117
- getOrCreateHooks(hookPoint) {
1118
- let hooks = this._hooks.get(hookPoint);
1119
- if (!hooks) {
1120
- hooks = [];
1121
- this._hooks.set(hookPoint, hooks);
1122
- }
1123
- return hooks;
1124
- }
1125
- sortHooks(hooks) {
1126
- return [...hooks].sort((a, b) => {
1127
- const priorityA = a.priority ?? this.defaultPriority;
1128
- const priorityB = b.priority ?? this.defaultPriority;
1129
- return priorityA - priorityB;
1130
- });
1131
- }
1132
- createContext(hookPoint, data, metadata, phase) {
1133
- return {
1134
- component: {
1135
- name: this.componentName,
1136
- version: this.componentVersion
1137
- },
1138
- data,
1139
- metadata,
1140
- phase,
1141
- hookPoint
1142
- };
1143
- }
1144
- async safeExecute(hook, ctx) {
1145
- try {
1146
- await hook.execute(ctx);
1147
- } catch (error) {
1148
- console.error(`Hook "${hook.name}" failed:`, error);
1149
- }
1150
- }
1151
- async safeExecuteAndReturn(hook, ctx) {
1152
- try {
1153
- return await hook.execute(ctx);
1154
- } catch (error) {
1155
- console.error(`Hook "${hook.name}" failed:`, error);
1156
- return;
1157
- }
1158
- }
1159
- }
1160
- // packages/core/src/env/hook/global-hook-manager.ts
1161
- var globalHookManager = new HookManager;
1162
- var AgentHookPoints = {
1163
- BEFORE_START: "agent:before.start",
1164
- BEFORE_LLM: "agent:before.llm",
1165
- AFTER_LLM: "agent:after.llm",
1166
- BEFORE_TOOL: "agent:before.tool",
1167
- AFTER_TOOL: "agent:after.tool",
1168
- ON_ITERATION: "agent:on.iteration",
1169
- ON_THRESHOLD: "agent:on.threshold",
1170
- AFTER_COMPLETE: "agent:after.complete",
1171
- ON_ERROR: "agent:on.error"
1172
- };
1173
- var LLMHookPoints = {
1174
- BEFORE_INVOKE: "llm:before.invoke",
1175
- AFTER_INVOKE: "llm:after.invoke",
1176
- ON_STREAM: "llm:on.stream"
1177
- };
1178
- var ToolHookPoints = {
1179
- BEFORE_EXECUTE: "tool:before.execute",
1180
- AFTER_EXECUTE: "tool:after.execute",
1181
- BEFORE_REGISTER: "tool:before.register",
1182
- AFTER_REGISTER: "tool:after.register",
1183
- ON_ERROR: "tool:on.error"
1184
- };
1185
- var hookPointAliases = {
1186
- "before.tool": "tool:before.execute",
1187
- "after.tool": "tool:after.execute",
1188
- "llm.before-invoke": "llm:before.invoke",
1189
- "llm.after-invoke": "llm:after.invoke",
1190
- "llm.stream": "llm:on.stream"
1191
- };
1192
- function setupAliasHooks() {
1193
- const originalRegister = globalHookManager.register.bind(globalHookManager);
1194
- globalHookManager.register = function(hookPoint, hook) {
1195
- originalRegister(hookPoint, hook);
1196
- const alias = hookPointAliases[hookPoint];
1197
- if (alias) {
1198
- originalRegister(alias, hook);
1199
- }
1200
- };
1201
- }
1202
- setupAliasHooks();
1203
- async function executeAgentHook(hookPoint, data, metadata = {}) {
1204
- await globalHookManager.execute(hookPoint, data, metadata);
1205
- }
1206
- async function executeAgentHookWithIntervention(hookPoint, data, metadata = {}) {
1207
- return globalHookManager.executeWithIntervention(hookPoint, data, metadata);
1208
- }
1209
- async function executeLLMHook(hookPoint, data, metadata = {}) {
1210
- await globalHookManager.execute(hookPoint, data, metadata);
1211
- }
1212
- async function executeToolHook(hookPoint, data, metadata = {}) {
1213
- await globalHookManager.execute(hookPoint, data, metadata);
1214
- }
1215
- // packages/core/src/env/llm/types.ts
1216
- import { z } from "zod";
1217
- var ProviderCapabilitiesSchema = z.object({
1218
- reasoning: z.boolean().optional(),
1219
- toolcall: z.boolean().optional(),
1220
- interleaved: z.object({
1221
- field: z.string()
1222
- }).optional(),
1223
- thinkingInText: z.object({
1224
- enabled: z.boolean().optional(),
1225
- tags: z.array(z.string()).optional(),
1226
- removeFromOutput: z.boolean().optional()
1227
- }).optional()
1228
- });
1229
- var ModelLimitsSchema = z.object({
1230
- contextWindow: z.number().int().positive(),
1231
- compactionThreshold: z.number().min(0).max(1).optional(),
1232
- maxOutputTokens: z.number().int().positive().optional()
1233
- });
1234
- var ProviderConfigSchema = z.object({
1235
- provider: z.string(),
1236
- name: z.string().optional(),
1237
- description: z.string().optional(),
1238
- apiKey: z.string(),
1239
- baseURL: z.string().optional(),
1240
- models: z.array(z.string()).optional(),
1241
- defaultModel: z.string().optional(),
1242
- temperature: z.number().min(0).max(2).optional(),
1243
- maxTokens: z.number().int().positive().optional(),
1244
- capabilities: ProviderCapabilitiesSchema.optional(),
1245
- limits: z.record(z.string(), ModelLimitsSchema).optional(),
1246
- providerOptions: z.record(z.unknown()).optional(),
1247
- options: z.record(z.unknown()).optional()
1248
- });
1249
- var LLMDefaultConfigSchema = z.object({
1250
- temperature: z.number().min(0).max(2).optional(),
1251
- maxTokens: z.number().int().positive().optional()
1252
- });
1253
- var LLMConfigSchema = z.object({
1254
- default: LLMDefaultConfigSchema.optional(),
1255
- defaultModel: z.string().optional(),
1256
- providers: z.record(z.string(), ProviderConfigSchema),
1257
- defaultProvider: z.string(),
1258
- temperature: z.number().min(0).max(2).default(0.7),
1259
- maxTokens: z.number().int().positive().optional(),
1260
- topP: z.number().min(0).max(1).optional(),
1261
- stream: z.boolean().default(true),
1262
- options: z.record(z.unknown()).optional()
1263
- });
1264
- // packages/core/src/env/llm/provider.ts
1265
- class ProviderManager {
1266
- providers = new Map;
1267
- async initialize(config) {
1268
- this.providers.clear();
1269
- for (const [id, providerConfig] of Object.entries(config.providers)) {
1270
- const resolvedConfig = await this.resolveProviderConfig(providerConfig);
1271
- const provider = this.createProvider(id, resolvedConfig);
1272
- this.providers.set(id, provider);
1273
- }
1274
- }
1275
- getProvider(id) {
1276
- return this.providers.get(id);
1277
- }
1278
- listProviders() {
1279
- return Array.from(this.providers.values());
1280
- }
1281
- async resolveProviderConfig(config) {
1282
- return {
1283
- ...config,
1284
- apiKey: await this.resolveEnvVar(config.apiKey),
1285
- baseURL: config.baseURL ? await this.resolveEnvVar(config.baseURL) : undefined
1286
- };
1287
- }
1288
- async resolveEnvVar(value) {
1289
- const envVarPattern = /\$\{([^}]+)\}/g;
1290
- const matches = [...value.matchAll(envVarPattern)];
1291
- if (matches.length === 0) {
1292
- return value;
1293
- }
1294
- let result = value;
1295
- for (const match of matches) {
1296
- const varName = match[1];
1297
- const envValue = process.env[varName];
1298
- if (envValue === undefined) {
1299
- throw new Error(`Environment variable ${varName} is not set`);
1300
- }
1301
- result = result.replace(match[0], envValue);
1302
- }
1303
- return result;
1304
- }
1305
- createProvider(id, config) {
1306
- return {
1307
- id,
1308
- provider: null,
1309
- models: config.models || []
1310
- };
1311
- }
1312
- createAIProvider(id, config, createFn) {
1313
- const providerInstance = createFn(config.apiKey, config.baseURL);
1314
- return {
1315
- id,
1316
- provider: providerInstance,
1317
- models: config.models || []
1318
- };
1319
- }
1320
- }
1321
- // packages/core/src/env/llm/transform.ts
1322
- class LLMTransform {
1323
- normalizeMessages(messages, providerType) {
1324
- return messages.map((msg) => {
1325
- switch (providerType) {
1326
- case "anthropic":
1327
- return this.normalizeForAnthropic(msg);
1328
- case "openai":
1329
- case "openai-compatible":
1330
- default:
1331
- return this.normalizeForOpenAI(msg);
1332
- }
1333
- });
1334
- }
1335
- normalizeForOpenAI(msg) {
1336
- return {
1337
- role: msg.role,
1338
- content: msg.content,
1339
- toolCalls: msg.toolCalls,
1340
- toolCallId: msg.toolCallId
1341
- };
1342
- }
1343
- normalizeForAnthropic(msg) {
1344
- if (msg.role === "tool") {
1345
- return {
1346
- role: "user",
1347
- content: msg.content,
1348
- toolCallId: msg.toolCallId
1349
- };
1350
- }
1351
- return msg;
1352
- }
1353
- extractToolSchema(tool) {
1354
- return {
1355
- name: tool.name,
1356
- description: tool.description,
1357
- parameters: tool.parameters
1358
- };
1359
- }
1360
- zodToJsonSchema(schema) {
1361
- return this.extractZodSchema(schema);
1362
- }
1363
- extractZodSchema(schema) {
1364
- const def = schema._def;
1365
- if (!def)
1366
- return {};
1367
- const typeName = def.typeName || "";
1368
- const type = def.type || "";
1369
- const isV3 = typeName.startsWith("Zod");
1370
- const isV4 = !isV3 && type !== "";
1371
- if (isV3 && typeName === "ZodObject" || isV4 && type === "object") {
1372
- let shape;
1373
- if (isV3) {
1374
- const shapeFn = def.shape;
1375
- shape = shapeFn ? shapeFn() : undefined;
1376
- } else {
1377
- shape = def.shape;
1378
- }
1379
- const properties = {};
1380
- const required = [];
1381
- if (shape) {
1382
- for (const [key, value] of Object.entries(shape)) {
1383
- properties[key] = this.extractZodSchema(value);
1384
- let isOptional = false;
1385
- if (isV3) {
1386
- const zodType = value;
1387
- isOptional = typeof zodType.isOptional === "function" ? zodType.isOptional() : false;
1388
- } else {
1389
- const innerDef = value._def;
1390
- isOptional = innerDef?.defaultValue !== undefined;
1391
- }
1392
- if (!isOptional) {
1393
- required.push(key);
1394
- }
1395
- }
1396
- }
1397
- return {
1398
- type: "object",
1399
- properties,
1400
- ...required.length > 0 ? { required } : {}
1401
- };
1402
- }
1403
- if (isV3 && typeName === "ZodEnum" || isV4 && type === "enum") {
1404
- const zodV3Enum = def;
1405
- const zodV4Enum = def;
1406
- const values = zodV3Enum.values || Object.values(zodV4Enum.entries || {});
1407
- return {
1408
- enum: values
1409
- };
1410
- }
1411
- if (isV3 && typeName === "ZodNativeEnum") {
1412
- const enumDef = def;
1413
- const values = Object.values(enumDef.values || {});
1414
- return {
1415
- enum: values
1416
- };
1417
- }
1418
- if (isV3 && typeName === "ZodString" || isV4 && type === "string") {
1419
- return { type: "string" };
1420
- }
1421
- if (isV3 && (typeName === "ZodNumber" || typeName === "ZodBigInt") || isV4 && (type === "number" || type === "bigint")) {
1422
- return { type: "number" };
1423
- }
1424
- if (isV3 && typeName === "ZodBoolean" || isV4 && type === "boolean") {
1425
- return { type: "boolean" };
1426
- }
1427
- if (isV3 && typeName === "ZodArray" || isV4 && type === "array") {
1428
- if (isV3) {
1429
- const itemsType = def.type;
1430
- if (itemsType) {
1431
- return {
1432
- type: "array",
1433
- items: this.extractZodSchema(itemsType)
1434
- };
1435
- }
1436
- }
1437
- const shape = def.shape;
1438
- if (shape) {
1439
- const itemKey = Object.keys(shape)[0];
1440
- const items = this.extractZodSchema(shape[itemKey]);
1441
- return {
1442
- type: "array",
1443
- items
1444
- };
1445
- }
1446
- return {
1447
- type: "array",
1448
- items: {}
1449
- };
1450
- }
1451
- if (isV3 && typeName === "ZodOptional" || isV4 && type === "optional") {
1452
- if (isV3) {
1453
- const innerType = def.innerType;
1454
- if (innerType) {
1455
- return this.extractZodSchema(innerType);
1456
- }
1457
- }
1458
- const shape = def.shape;
1459
- if (shape) {
1460
- const innerKey = Object.keys(shape).find((k) => k !== "undefinedType");
1461
- if (innerKey) {
1462
- return this.extractZodSchema(shape[innerKey]);
1463
- }
1464
- }
1465
- return {};
1466
- }
1467
- if (isV3 && typeName === "ZodNullable") {
1468
- const innerType = def.innerType;
1469
- if (innerType) {
1470
- return this.extractZodSchema(innerType);
1471
- }
1472
- }
1473
- return {};
1474
- }
1475
- formatToolResult(result) {
1476
- return {
1477
- role: "tool",
1478
- tool_call_id: result.toolCallId,
1479
- content: result.result,
1480
- ...result.isError ? { isError: true } : {}
1481
- };
1482
- }
1483
- parseToolArguments(args) {
1484
- if (!args || args.trim() === "") {
1485
- return {};
1486
- }
1487
- try {
1488
- return JSON.parse(args);
1489
- } catch {
1490
- return {};
1491
- }
1492
- }
1493
- serializeToolArguments(args) {
1494
- return JSON.stringify(args);
1495
- }
1496
- }
1497
- // packages/core/src/env/llm/invoke.ts
1498
- init_logger();
1499
- import { randomUUID } from "crypto";
1500
- import { streamText, jsonSchema } from "ai";
1501
- import { createOpenAI } from "@ai-sdk/openai";
1502
- import { createAnthropic } from "@ai-sdk/anthropic";
1503
- import { createGoogleGenerativeAI } from "@ai-sdk/google";
1504
- import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
1505
- import { zodToJsonSchema } from "zod-to-json-schema";
1506
- var logger = createLogger("llm:invoke");
1507
- var MAX_RETRIES = 3;
1508
- function parseModelString(model) {
1509
- if (!model) {
1510
- return { providerId: "openai", modelId: "gpt-4o" };
1511
- }
1512
- const parts = model.split("/");
1513
- if (parts.length >= 2) {
1514
- return { providerId: parts[0], modelId: parts.slice(1).join("/") };
1515
- }
1516
- return { providerId: "openai", modelId: model };
1517
- }
1518
- function extractUsageInfo(usage) {
1519
- if (!usage)
1520
- return;
1521
- if (usage.inputTokens && typeof usage.inputTokens === "object") {
1522
- return {
1523
- promptTokens: usage.inputTokens.total ?? 0,
1524
- completionTokens: usage.outputTokens?.total ?? 0,
1525
- totalTokens: usage.totalTokens ?? 0
1526
- };
1527
- }
1528
- if (typeof usage.promptTokens === "number") {
1529
- return {
1530
- promptTokens: usage.promptTokens,
1531
- completionTokens: usage.completionTokens ?? 0,
1532
- totalTokens: usage.totalTokens ?? 0
1533
- };
1534
- }
1535
- if (usage.inputTokenDetails || usage.outputTokenDetails) {
1536
- return {
1537
- promptTokens: usage.inputTokenDetails?.tokens ?? 0,
1538
- completionTokens: usage.outputTokenDetails?.tokens ?? 0,
1539
- totalTokens: usage.totalTokens ?? 0
1540
- };
1541
- }
1542
- if (usage.raw && typeof usage.raw.total_tokens === "number") {
1543
- return {
1544
- promptTokens: 0,
1545
- completionTokens: 0,
1546
- totalTokens: usage.raw.total_tokens
1547
- };
1548
- }
1549
- return;
1550
- }
1551
- function convertToSDKMessages(messages) {
1552
- return messages.map((msg) => {
1553
- if (msg.role === "assistant" && msg.toolCalls && msg.toolCalls.length > 0) {
1554
- const toolCallParts = msg.toolCalls.map((tc) => {
1555
- let input = {};
1556
- if (tc.function?.arguments) {
1557
- if (typeof tc.function.arguments === "string") {
1558
- try {
1559
- input = JSON.parse(tc.function.arguments);
1560
- } catch {
1561
- input = tc.function.arguments;
1562
- }
1563
- } else {
1564
- input = tc.function.arguments;
1565
- }
1566
- } else if (tc.arguments) {
1567
- if (typeof tc.arguments === "string") {
1568
- try {
1569
- input = JSON.parse(tc.arguments);
1570
- } catch {
1571
- input = tc.arguments;
1572
- }
1573
- } else {
1574
- input = tc.arguments;
1575
- }
1576
- }
1577
- return {
1578
- type: "tool-call",
1579
- toolCallId: tc.id,
1580
- toolName: tc.function?.name || tc.name || "unknown",
1581
- input
1582
- };
1583
- });
1584
- return {
1585
- role: "assistant",
1586
- content: toolCallParts
1587
- };
1588
- }
1589
- if (msg.role === "tool") {
1590
- if (!msg.toolCallId) {
1591
- logger.warn("Tool message missing toolCallId", { msg });
1592
- }
1593
- let outputValue = "";
1594
- const content = msg.content;
1595
- if (typeof content === "string") {
1596
- outputValue = content;
1597
- } else if (Array.isArray(content)) {
1598
- const toolResultPart = content.find((p) => p?.type === "tool-result");
1599
- if (toolResultPart) {
1600
- outputValue = typeof toolResultPart.output === "string" ? toolResultPart.output : JSON.stringify(toolResultPart.output);
1601
- } else {
1602
- outputValue = JSON.stringify(content);
1603
- }
1604
- } else {
1605
- outputValue = JSON.stringify(content);
1606
- }
1607
- const converted = {
1608
- role: "tool",
1609
- content: [{
1610
- type: "tool-result",
1611
- toolCallId: msg.toolCallId || "unknown",
1612
- toolName: msg.name || "unknown",
1613
- output: {
1614
- type: "text",
1615
- value: outputValue
1616
- }
1617
- }]
1618
- };
1619
- return converted;
1620
- }
1621
- return msg;
1622
- });
1623
- }
1624
- function convertToolsToSDK(tools) {
1625
- const result = {};
1626
- for (const tool of tools) {
1627
- const jsonSchemaObj = extractToolSchema(tool.parameters);
1628
- result[tool.name] = {
1629
- description: tool.description || "",
1630
- inputSchema: jsonSchema(jsonSchemaObj)
1631
- };
1632
- }
1633
- return result;
1634
- }
1635
- function extractToolSchema(parameters) {
1636
- if (parameters && typeof parameters === "object" && "_def" in parameters) {
1637
- const zodSchema = parameters;
1638
- const schema = zodToJsonSchema(zodSchema, "zod");
1639
- if ("$ref" in schema && schema.definitions) {
1640
- const def = schema.definitions.zod;
1641
- if (def && def.type === "object" && def.properties) {
1642
- return {
1643
- type: "object",
1644
- properties: def.properties,
1645
- required: def.required,
1646
- additionalProperties: true
1647
- };
1648
- }
1649
- }
1650
- return schema;
1651
- }
1652
- return parameters;
1653
- }
1654
- function generateProviderOptions(providerType, options) {
1655
- const result = {
1656
- providerOptions: {}
1657
- };
1658
- if (options.temperature !== undefined) {
1659
- result.temperature = options.temperature;
1660
- }
1661
- if (options.maxTokens !== undefined) {
1662
- result.maxTokens = options.maxTokens;
1663
- }
1664
- switch (providerType) {
1665
- case "anthropic":
1666
- break;
1667
- case "openai-compatible":
1668
- result.providerOptions.includeUsage = true;
1669
- break;
1670
- }
1671
- return result;
1672
- }
1673
- function createEnvEventEmitter(env, context) {
1674
- if (!env) {
1675
- return null;
1676
- }
1677
- return (event) => {
1678
- try {
1679
- env.pushEnvEvent({
1680
- id: randomUUID(),
1681
- type: `llm.${event.type}`,
1682
- timestamp: Date.now(),
1683
- metadata: {
1684
- source: "llm.invoke",
1685
- sessionId: context?.sessionId,
1686
- messageId: context?.messageId
1687
- },
1688
- payload: event
1689
- });
1690
- } catch (err) {
1691
- logger.warn("Failed to push env event", { error: String(err), eventType: event.type });
1692
- }
1693
- };
1694
- }
1695
- function processThinkingStream(textDelta, config, state) {
1696
- if (!config.enabled || !textDelta) {
1697
- return {
1698
- cleanedText: textDelta,
1699
- isThinkingTagOpen: state.isOpen,
1700
- currentThinkingContent: state.content,
1701
- reasoningEvents: []
1702
- };
1703
- }
1704
- const tags = config.tags || ["thinking"];
1705
- let remainingText = textDelta;
1706
- const reasoningEvents = [];
1707
- let isOpen = state.isOpen;
1708
- let currentContent = state.content;
1709
- for (const tag of tags) {
1710
- const openTag = `<${tag}>`;
1711
- const closeTag = `</${tag}>`;
1712
- let text = remainingText;
1713
- let result = "";
1714
- const openIndex = text.toLowerCase().indexOf(openTag.toLowerCase());
1715
- const closeIndex = text.toLowerCase().indexOf(closeTag.toLowerCase());
1716
- if (openIndex !== -1 && (closeIndex === -1 || openIndex < closeIndex)) {
1717
- const beforeOpen = text.substring(0, openIndex);
1718
- const afterOpen = text.substring(openIndex + openTag.length);
1719
- if (!isOpen) {
1720
- isOpen = true;
1721
- currentContent = "";
1722
- reasoningEvents.push("");
1723
- }
1724
- result += beforeOpen;
1725
- const innerCloseIndex = afterOpen.toLowerCase().indexOf(closeTag.toLowerCase());
1726
- if (innerCloseIndex !== -1) {
1727
- const thinkingContent = afterOpen.substring(0, innerCloseIndex);
1728
- const afterClose = afterOpen.substring(innerCloseIndex + closeTag.length);
1729
- currentContent += thinkingContent;
1730
- reasoningEvents.push(currentContent);
1731
- isOpen = false;
1732
- currentContent = "";
1733
- result += afterClose;
1734
- } else {
1735
- currentContent += afterOpen;
1736
- reasoningEvents.push(currentContent);
1737
- result += "";
1738
- }
1739
- remainingText = result;
1740
- } else if (closeIndex !== -1) {
1741
- const beforeClose = text.substring(0, closeIndex);
1742
- const afterClose = text.substring(closeIndex + closeTag.length);
1743
- if (isOpen) {
1744
- currentContent += beforeClose;
1745
- reasoningEvents.push(currentContent);
1746
- isOpen = false;
1747
- currentContent = "";
1748
- result = afterClose;
1749
- } else {
1750
- result = text;
1751
- }
1752
- remainingText = result;
1753
- }
1754
- }
1755
- return {
1756
- cleanedText: remainingText,
1757
- isThinkingTagOpen: isOpen,
1758
- currentThinkingContent: currentContent,
1759
- reasoningEvents
1760
- };
1761
- }
1762
- async function invoke(config, options, ctx) {
1763
- try {
1764
- const emitToEnv = createEnvEventEmitter(options.env, options.context);
1765
- const { providerId, modelId } = parseModelString(options.model || config.model);
1766
- const provider = createProviderInstance(providerId, config.apiKey, config.baseURL);
1767
- if (!provider) {
1768
- throw new Error(`Failed to create provider for ${providerId}`);
1769
- }
1770
- const messages = convertToSDKMessages(options.messages);
1771
- const toolCallIds = new Set;
1772
- const assistantToolCalls = [];
1773
- const toolResultIds = [];
1774
- for (const msg of messages) {
1775
- if (msg.role === "assistant") {
1776
- const assistantContent = Array.isArray(msg.content) ? msg.content : [];
1777
- for (const part of assistantContent) {
1778
- if (part && typeof part === "object" && part.type === "tool-call") {
1779
- const tc = part.toolCall || part;
1780
- assistantToolCalls.push({ id: tc.id, name: tc.name || tc.function && tc.function.name });
1781
- toolCallIds.add(tc.id);
1782
- }
1783
- }
1784
- }
1785
- if (msg.role === "tool") {
1786
- const toolContent = Array.isArray(msg.content) ? msg.content : [];
1787
- for (const part of toolContent) {
1788
- if (part && typeof part === "object" && part.type === "tool-result") {
1789
- toolResultIds.push(part.toolCallId);
1790
- }
1791
- }
1792
- }
1793
- }
1794
- const missingToolResults = assistantToolCalls.filter((tc) => !toolResultIds.includes(tc.id));
1795
- const orphanedToolResults = toolResultIds.filter((id) => !toolCallIds.has(id));
1796
- if (assistantToolCalls.length > 0 || toolResultIds.length > 0) {
1797
- logger.debug("[invoke] ToolCallId matching analysis:", {
1798
- assistantToolCalls,
1799
- toolResultIds,
1800
- missingToolResults: missingToolResults.length > 0 ? missingToolResults : "none",
1801
- orphanedToolResults: orphanedToolResults.length > 0 ? orphanedToolResults : "none"
1802
- });
1803
- }
1804
- const tools = options.tools && options.tools.length > 0 ? convertToolsToSDK(options.tools) : undefined;
1805
- const providerOptions = generateProviderOptions(providerId, {
1806
- temperature: options.temperature,
1807
- maxTokens: options.maxTokens
1808
- });
1809
- const model = `${providerId}/${modelId}`;
1810
- if (emitToEnv) {
1811
- emitToEnv({ type: "start", metadata: { model } });
1812
- }
1813
- const streamTextOptions = {
1814
- model: provider.languageModel(modelId),
1815
- messages,
1816
- tools,
1817
- temperature: providerOptions.temperature,
1818
- maxTokens: providerOptions.maxTokens,
1819
- abortSignal: ctx.abort,
1820
- maxRetries: MAX_RETRIES,
1821
- streamOptions: {
1822
- includeUsage: true
1823
- },
1824
- allowSystemInMessages: true
1825
- };
1826
- const result = await streamText(streamTextOptions);
1827
- let fullContent = "";
1828
- let reasoningContent = "";
1829
- const toolCalls = [];
1830
- let usageInfo;
1831
- let isThinkingTagOpen = false;
1832
- let currentThinkingContent = "";
1833
- for await (const part of result.fullStream) {
1834
- const streamPart = part;
1835
- switch (streamPart.type) {
1836
- case "text-delta":
1837
- const textDelta = streamPart.text;
1838
- const thinkingConfig = config.options?.thinkingInText;
1839
- if (thinkingConfig?.enabled) {
1840
- const thinkingResult = processThinkingStream(textDelta, thinkingConfig, {
1841
- isOpen: isThinkingTagOpen,
1842
- content: currentThinkingContent
1843
- });
1844
- isThinkingTagOpen = thinkingResult.isThinkingTagOpen;
1845
- currentThinkingContent = thinkingResult.currentThinkingContent;
1846
- fullContent += thinkingResult.cleanedText;
1847
- for (const reasoningDelta2 of thinkingResult.reasoningEvents) {
1848
- reasoningContent = reasoningDelta2;
1849
- if (emitToEnv) {
1850
- emitToEnv({ type: "reasoning", content: reasoningContent });
1851
- }
1852
- }
1853
- if (emitToEnv) {
1854
- emitToEnv({ type: "text", content: fullContent, delta: thinkingResult.cleanedText });
1855
- }
1856
- } else {
1857
- fullContent += textDelta;
1858
- if (emitToEnv) {
1859
- emitToEnv({ type: "text", content: fullContent, delta: textDelta });
1860
- }
1861
- }
1862
- break;
1863
- case "reasoning-delta":
1864
- const reasoningDelta = streamPart.text;
1865
- reasoningContent += reasoningDelta;
1866
- if (emitToEnv) {
1867
- emitToEnv({ type: "reasoning", content: reasoningContent });
1868
- }
1869
- break;
1870
- case "tool-call":
1871
- const toolInput = streamPart.input;
1872
- const toolCallId = streamPart.toolCallId;
1873
- const toolName = streamPart.toolName;
1874
- toolCalls.push({
1875
- id: toolCallId,
1876
- name: toolName,
1877
- args: toolInput
1878
- });
1879
- if (emitToEnv) {
1880
- emitToEnv({
1881
- type: "tool_call",
1882
- toolCall: {
1883
- id: toolCallId,
1884
- name: toolName,
1885
- arguments: JSON.stringify(toolInput)
1886
- }
1887
- });
1888
- }
1889
- break;
1890
- case "finish-step":
1891
- const stepUsage = extractUsageInfo(streamPart.usage);
1892
- if (stepUsage) {
1893
- usageInfo = stepUsage;
1894
- }
1895
- break;
1896
- case "error":
1897
- throw streamPart.error;
1898
- case "finish":
1899
- usageInfo = extractUsageInfo(streamPart.totalUsage);
1900
- if (!usageInfo && streamPart.usage) {
1901
- usageInfo = extractUsageInfo(streamPart.usage);
1902
- }
1903
- break;
1904
- }
1905
- }
1906
- if (emitToEnv) {
1907
- emitToEnv({
1908
- type: "completed",
1909
- content: fullContent,
1910
- metadata: {
1911
- model,
1912
- usage: usageInfo
1913
- }
1914
- });
1915
- }
1916
- if (toolCalls.length > 0) {
1917
- const firstToolCall = toolCalls[0];
1918
- return {
1919
- toolCallId: firstToolCall.id,
1920
- result: JSON.stringify({
1921
- content: fullContent,
1922
- reasoning: reasoningContent || undefined,
1923
- tool_calls: toolCalls.map((tc) => ({
1924
- id: tc.id,
1925
- function: {
1926
- name: tc.name,
1927
- arguments: JSON.stringify(tc.args)
1928
- }
1929
- }))
1930
- })
1931
- };
1932
- }
1933
- return {
1934
- toolCallId: "invoke-" + Date.now(),
1935
- result: JSON.stringify({
1936
- content: fullContent,
1937
- reasoning: reasoningContent || undefined,
1938
- usage: usageInfo,
1939
- model
1940
- })
1941
- };
1942
- } catch (error) {
1943
- logger.error("Error during invocation", {
1944
- error: error instanceof Error ? error.message : String(error),
1945
- stack: error instanceof Error ? error.stack : undefined
1946
- });
1947
- return {
1948
- toolCallId: "error-" + Date.now(),
1949
- result: error instanceof Error ? error.message : String(error),
1950
- isError: true
1951
- };
1952
- }
1953
- }
1954
- function createProviderInstance(providerId, apiKey, baseURL) {
1955
- try {
1956
- if (providerId === "openai") {
1957
- const openai = createOpenAI({
1958
- apiKey,
1959
- baseURL
1960
- });
1961
- return openai;
1962
- }
1963
- if (providerId === "anthropic") {
1964
- const anthropic = createAnthropic({
1965
- apiKey
1966
- });
1967
- return anthropic;
1968
- }
1969
- if (providerId === "google") {
1970
- const google = createGoogleGenerativeAI({
1971
- apiKey
1972
- });
1973
- return google;
1974
- }
1975
- const defaultProvider = createOpenAICompatible({
1976
- name: providerId,
1977
- apiKey,
1978
- baseURL: baseURL || "",
1979
- includeUsage: true
1980
- });
1981
- return defaultProvider;
1982
- } catch (error) {
1983
- logger.error(`Failed to create provider ${providerId}`, { error: String(error) });
1984
- return null;
1985
- }
1986
- }
1987
- async function invokeNonStream(config, options, ctx) {
1988
- const { providerId, modelId } = parseModelString(options.model || config.model);
1989
- const provider = createProviderInstance(providerId, config.apiKey, config.baseURL);
1990
- if (!provider) {
1991
- throw new Error(`Failed to create provider for ${providerId}`);
1992
- }
1993
- const messages = convertToSDKMessages(options.messages);
1994
- const tools = options.tools && options.tools.length > 0 ? convertToolsToSDK(options.tools) : undefined;
1995
- const providerOptions = generateProviderOptions(providerId, {
1996
- temperature: options.temperature,
1997
- maxTokens: options.maxTokens
1998
- });
1999
- const { generateText } = await import("ai");
2000
- const genTextFn = generateText;
2001
- const result = await genTextFn({
2002
- model: provider.languageModel(modelId),
2003
- messages,
2004
- tools,
2005
- temperature: providerOptions.temperature,
2006
- maxTokens: providerOptions.maxTokens,
2007
- abortSignal: ctx.abort,
2008
- allowSystemInMessages: true
2009
- });
2010
- const usage = result.usage;
2011
- const usageInfo = usage ? {
2012
- promptTokens: usage.promptTokens ?? 0,
2013
- completionTokens: usage.completionTokens ?? 0,
2014
- totalTokens: usage.totalTokens ?? 0
2015
- } : undefined;
2016
- if (options.env) {
2017
- options.env.pushEnvEvent({
2018
- id: randomUUID(),
2019
- type: "llm.completed",
2020
- timestamp: Date.now(),
2021
- metadata: {
2022
- source: "llm.invokeNonStream",
2023
- sessionId: options.context?.sessionId,
2024
- messageId: options.context?.messageId
2025
- },
2026
- payload: {
2027
- type: "completed",
2028
- content: result.text,
2029
- metadata: {
2030
- model: `${providerId}/${modelId}`,
2031
- usage: usageInfo
2032
- }
2033
- }
2034
- });
2035
- }
2036
- return {
2037
- content: result.text,
2038
- finishReason: result.finishReason === "stop" ? "stop" : "tool-calls",
2039
- model: `${providerId}/${modelId}`,
2040
- usage: usageInfo
2041
- };
2042
- }
2043
- function createInvokeConfig(model, apiKey, baseURL) {
2044
- return {
2045
- model,
2046
- apiKey,
2047
- baseURL
2048
- };
2049
- }
2050
- // packages/core/src/env/llm/hooks.ts
2051
- class LLMHookManager {
2052
- registrations = [];
2053
- register(type, handler, name) {
2054
- const registration = {
2055
- type,
2056
- name
2057
- };
2058
- switch (type) {
2059
- case "before-invoke":
2060
- registration.beforeInvoke = handler;
2061
- break;
2062
- case "after-invoke":
2063
- registration.afterInvoke = handler;
2064
- break;
2065
- case "stream":
2066
- registration.stream = handler;
2067
- break;
2068
- }
2069
- this.registrations.push(registration);
2070
- this.registerToGlobal(registration);
2071
- }
2072
- registerToGlobal(reg) {
2073
- const hookName = `llm:${reg.name || `anon-${reg.type}-${this.registrations.indexOf(reg)}`}`;
2074
- if (reg.beforeInvoke) {
2075
- globalHookManager.register(LLMHookPoints.BEFORE_INVOKE, {
2076
- name: hookName,
2077
- priority: 0,
2078
- execute: async (ctx) => {
2079
- await reg.beforeInvoke(ctx.data);
2080
- }
2081
- });
2082
- }
2083
- if (reg.afterInvoke) {
2084
- globalHookManager.register(LLMHookPoints.AFTER_INVOKE, {
2085
- name: hookName,
2086
- priority: 0,
2087
- execute: async (ctx) => {
2088
- const { request, providerId, model, metadata, result } = ctx.data;
2089
- await reg.afterInvoke({ request, providerId, model, metadata }, result);
2090
- }
2091
- });
2092
- }
2093
- if (reg.stream) {
2094
- globalHookManager.register(LLMHookPoints.ON_STREAM, {
2095
- name: hookName,
2096
- priority: 0,
2097
- execute: async (ctx) => {
2098
- const { request, providerId, model, metadata, event } = ctx.data;
2099
- await reg.stream({ request, providerId, model, metadata }, event);
2100
- }
2101
- });
2102
- }
2103
- }
2104
- unregister(hook) {
2105
- const index = this.registrations.findIndex((h) => {
2106
- return h.beforeInvoke === hook || h.afterInvoke === hook || h.stream === hook;
2107
- });
2108
- if (index !== -1) {
2109
- const reg = this.registrations[index];
2110
- this.unregisterFromGlobal(reg);
2111
- this.registrations.splice(index, 1);
2112
- }
2113
- }
2114
- unregisterFromGlobal(reg) {
2115
- const hookName = `llm:${reg.name || `anon-${reg.type}`}`;
2116
- globalHookManager.unregister(LLMHookPoints.BEFORE_INVOKE, hookName);
2117
- globalHookManager.unregister(LLMHookPoints.AFTER_INVOKE, hookName);
2118
- globalHookManager.unregister(LLMHookPoints.ON_STREAM, hookName);
2119
- }
2120
- unregisterByName(name) {
2121
- const hookName = `llm:${name}`;
2122
- globalHookManager.unregister(LLMHookPoints.BEFORE_INVOKE, hookName);
2123
- globalHookManager.unregister(LLMHookPoints.AFTER_INVOKE, hookName);
2124
- globalHookManager.unregister(LLMHookPoints.ON_STREAM, hookName);
2125
- this.registrations = this.registrations.filter((h) => h.name !== name);
2126
- }
2127
- unregisterByType(type) {
2128
- const filtered = this.registrations.filter((h) => h.type === type);
2129
- for (const reg of filtered) {
2130
- this.unregisterFromGlobal(reg);
2131
- }
2132
- this.registrations = this.registrations.filter((h) => h.type !== type);
2133
- }
2134
- clear() {
2135
- for (const reg of this.registrations) {
2136
- this.unregisterFromGlobal(reg);
2137
- }
2138
- this.registrations = [];
2139
- }
2140
- getHooks() {
2141
- return [...this.registrations];
2142
- }
2143
- getHooksByType(type) {
2144
- return this.registrations.filter((h) => h.type === type);
2145
- }
2146
- }
2147
-
2148
- class LLMHooks {
2149
- hookManager = new LLMHookManager;
2150
- register(type, handler, name) {
2151
- this.hookManager.register(type, handler, name);
2152
- }
2153
- unregister(hook) {
2154
- this.hookManager.unregister(hook);
2155
- }
2156
- unregisterByName(name) {
2157
- this.hookManager.unregisterByName(name);
2158
- }
2159
- unregisterByType(type) {
2160
- this.hookManager.unregisterByType(type);
2161
- }
2162
- clear() {
2163
- this.hookManager.clear();
2164
- }
2165
- getHooks() {
2166
- return this.hookManager.getHooks();
2167
- }
2168
- getHooksByType(type) {
2169
- return this.hookManager.getHooksByType(type);
2170
- }
2171
- async executeBeforeInvoke(context) {
2172
- await globalHookManager.execute(LLMHookPoints.BEFORE_INVOKE, context);
2173
- }
2174
- async executeAfterInvoke(context, result) {
2175
- await globalHookManager.execute(LLMHookPoints.AFTER_INVOKE, { ...context, result });
2176
- }
2177
- async executeStream(context, event) {
2178
- await globalHookManager.execute(LLMHookPoints.ON_STREAM, { ...context, event });
2179
- }
2180
- }
2181
- // packages/core/src/env/component.ts
2182
- class BaseComponent {
2183
- _status = "created";
2184
- _enabled = true;
2185
- env;
2186
- hookManager;
2187
- constructor() {
2188
- this.hookManager = new HookManager;
2189
- }
2190
- getStatus() {
2191
- return this._status;
2192
- }
2193
- getConfig() {
2194
- return {
2195
- name: this.name,
2196
- version: this.version,
2197
- enabled: this._enabled,
2198
- env: this.env
2199
- };
2200
- }
2201
- isEnvInitialized() {
2202
- return this._status !== "created";
2203
- }
2204
- setStatus(status) {
2205
- this._status = status;
2206
- }
2207
- getEnv() {
2208
- return this.env;
2209
- }
2210
- async init(config) {
2211
- if (config?.env) {
2212
- this.env = config.env;
2213
- }
2214
- this.setStatus("initializing");
2215
- try {
2216
- if (config?.name)
2217
- Object.defineProperty(this, "name", { value: config.name, writable: false });
2218
- if (config?.version)
2219
- Object.defineProperty(this, "version", { value: config.version, writable: false });
2220
- } catch {}
2221
- if (config?.enabled !== undefined)
2222
- this._enabled = config.enabled;
2223
- this.hookManager.setComponentInfo(this.name, this.version);
2224
- await this.onInit();
2225
- this.setStatus("running");
2226
- }
2227
- async start() {
2228
- if (this._started)
2229
- return;
2230
- this._started = true;
2231
- await this.onStart();
2232
- this.setStatus("running");
2233
- }
2234
- async stop() {
2235
- this.setStatus("stopping");
2236
- this.hookManager.clear();
2237
- await this.onStop();
2238
- this.setStatus("stopped");
2239
- }
2240
- async onInit() {}
2241
- async onStart() {}
2242
- async onStop() {}
2243
- registerHook(hookPoint, hook) {
2244
- this.hookManager.register(hookPoint, hook);
2245
- }
2246
- addHook(hookPoint, name, fn, priority) {
2247
- this.hookManager.register(hookPoint, createHook({ name, priority }, fn));
2248
- }
2249
- removeHook(hookPoint, name) {
2250
- return this.hookManager.unregister(hookPoint, name);
2251
- }
2252
- async executeHooks(hookPoint, data, metadata) {
2253
- await this.hookManager.execute(hookPoint, data, metadata);
2254
- }
2255
- getConfigComponent() {
2256
- return this.env?.getComponent("config");
2257
- }
2258
- getRuntimeConfig(key, defaultValue) {
2259
- const configComponent = this.getConfigComponent();
2260
- if (configComponent) {
2261
- const value = configComponent.get(key);
2262
- if (value !== undefined) {
2263
- return value;
2264
- }
2265
- }
2266
- return defaultValue;
2267
- }
2268
- }
2269
-
2270
- // packages/core/src/env/llm/llm.ts
2271
- init_logger();
2272
- init_decorator();
2273
-
2274
- // packages/core/src/config/env-key.ts
2275
- function toEnvKey(key, prefix) {
2276
- let keyNormalized = key.replace(/[.-]/g, "_");
2277
- keyNormalized = keyNormalized.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/_+/g, "_").toUpperCase();
2278
- if (prefix) {
2279
- const separator = prefix.endsWith("_") ? "" : "_";
2280
- return `${prefix}${separator}${keyNormalized}`;
2281
- }
2282
- return keyNormalized;
2283
- }
2284
- function fromEnvKey(envKey, prefix) {
2285
- if (!prefix) {
2286
- return envKey;
2287
- }
2288
- if (envKey.startsWith(prefix)) {
2289
- const afterPrefix = envKey.slice(prefix.length);
2290
- if (afterPrefix.startsWith("_")) {
2291
- return afterPrefix.slice(1);
2292
- }
2293
- return afterPrefix;
2294
- }
2295
- return envKey;
2296
- }
2297
- function envKeyToConfigKey(envKey, prefix, componentName) {
2298
- const prefixUpper = prefix.toUpperCase();
2299
- if (!envKey.startsWith(prefixUpper)) {
2300
- return;
2301
- }
2302
- const componentUpperNormalized = componentName.replace(/-/g, "_").toUpperCase();
2303
- let keyPart = envKey.slice(prefixUpper.length);
2304
- if (keyPart.startsWith("_")) {
2305
- keyPart = keyPart.slice(1);
2306
- }
2307
- if (!keyPart) {
2308
- return;
2309
- }
2310
- const firstUnderscore = keyPart.indexOf("_");
2311
- let restPart;
2312
- if (firstUnderscore === -1) {
2313
- restPart = keyPart;
2314
- } else {
2315
- const firstPart = keyPart.slice(0, firstUnderscore);
2316
- const remaining = keyPart.slice(firstUnderscore + 1);
2317
- if (firstPart === componentUpperNormalized) {
2318
- restPart = remaining;
2319
- } else {
2320
- restPart = keyPart;
2321
- }
2322
- }
2323
- if (!restPart) {
2324
- return;
2325
- }
2326
- const restPartConverted = restPart.replace(/_/g, ".");
2327
- return `${componentName}.${restPartConverted.toLowerCase()}`;
2328
- }
2329
-
2330
- // packages/core/src/env/errors.ts
2331
- var ErrorCodes = {
2332
- AGENT_NOT_FOUND: "AGENT_001",
2333
- AGENT_RUN_FAILED: "AGENT_002",
2334
- AGENT_TIMEOUT: "AGENT_003",
2335
- AGENT_CONFIG_INVALID: "AGENT_004",
2336
- TOOL_NOT_FOUND: "TOOL_001",
2337
- TOOL_DISABLED: "TOOL_002",
2338
- TOOL_EXECUTION_FAILED: "TOOL_003",
2339
- TOOL_VALIDATION_FAILED: "TOOL_004",
2340
- LLM_PROVIDER_NOT_FOUND: "LLM_001",
2341
- LLM_INVOCATION_FAILED: "LLM_002",
2342
- LLM_TIMEOUT: "LLM_003",
2343
- LLM_CONFIG_INVALID: "LLM_004",
2344
- COMPONENT_NOT_FOUND: "COMP_001",
2345
- COMPONENT_INIT_FAILED: "COMP_002",
2346
- COMPONENT_NOT_RUNNING: "COMP_003",
2347
- CONFIG_NOT_FOUND: "CONFIG_001",
2348
- CONFIG_INVALID: "CONFIG_002",
2349
- CONFIG_SOURCE_ERROR: "CONFIG_003",
2350
- CONTEXT_THRESHOLD_EXCEEDED: "CTX_001",
2351
- CONTEXT_COMPACTION_FAILED: "CTX_002",
2352
- MCP_INIT_FAILED: "MCP_001",
2353
- MCP_SERVER_ERROR: "MCP_002",
2354
- MCP_TOOL_ERROR: "MCP_003"
2355
- };
2356
-
2357
- class RoyError extends Error {
2358
- code;
2359
- details;
2360
- constructor(message, code, details) {
2361
- super(message);
2362
- this.code = code;
2363
- this.details = details;
2364
- this.name = this.constructor.name;
2365
- Error.captureStackTrace(this, this.constructor);
2366
- }
2367
- }
2368
-
2369
- class AgentError extends RoyError {
2370
- constructor(message, code = ErrorCodes.AGENT_NOT_FOUND, details) {
2371
- super(message, code, details);
2372
- }
2373
- }
2374
-
2375
- class ToolError extends RoyError {
2376
- toolName;
2377
- constructor(message, code = ErrorCodes.TOOL_NOT_FOUND, toolName, details) {
2378
- super(message, code, details);
2379
- this.toolName = toolName;
2380
- }
2381
- }
2382
-
2383
- class LLMError extends RoyError {
2384
- provider;
2385
- constructor(message, code = ErrorCodes.LLM_INVOCATION_FAILED, provider, details) {
2386
- super(message, code, details);
2387
- this.provider = provider;
2388
- }
2389
- }
2390
-
2391
- class ComponentError extends RoyError {
2392
- componentName;
2393
- constructor(message, code = ErrorCodes.COMPONENT_NOT_FOUND, componentName, details) {
2394
- super(message, code, details);
2395
- this.componentName = componentName;
2396
- }
2397
- }
2398
-
2399
- class ContextError extends RoyError {
2400
- sessionId;
2401
- usage;
2402
- contextWindow;
2403
- constructor(message, code = ErrorCodes.CONTEXT_THRESHOLD_EXCEEDED, sessionId, usage, contextWindow, details) {
2404
- super(message, code, details);
2405
- this.sessionId = sessionId;
2406
- this.usage = usage;
2407
- this.contextWindow = contextWindow;
2408
- }
2409
- }
2410
- function isRoyError(error) {
2411
- return error instanceof RoyError;
2412
- }
2413
- function getErrorCode(error) {
2414
- if (error instanceof RoyError) {
2415
- return error.code;
2416
- }
2417
- return;
2418
- }
2419
-
2420
- // packages/core/src/env/llm/llm.ts
2421
- var logger2 = createLogger("llm");
2422
-
2423
- class LLMComponent extends BaseComponent {
2424
- name = "llm";
2425
- version = "2.0.0";
2426
- configComponent;
2427
- configWatcher;
2428
- constructor() {
2429
- super();
2430
- }
2431
- async init(config) {
2432
- await super.init(config);
2433
- const options = config.options;
2434
- if (!options?.configComponent) {
2435
- throw new Error("ConfigComponent is required for LLMComponent initialization");
2436
- }
2437
- this.configComponent = options.configComponent;
2438
- await this.registerConfig(options);
2439
- this.setStatus("running");
2440
- }
2441
- async registerConfig(options) {
2442
- const configComponent = options.configComponent;
2443
- if (!configComponent)
2444
- return;
2445
- const { configPath, envPrefix, config } = options;
2446
- const prefix = envPrefix !== undefined ? envPrefix : "LLM";
2447
- const llmKeys = [
2448
- "llm.defaultModel",
2449
- "llm.defaultProvider",
2450
- "llm.temperature",
2451
- "llm.maxTokens",
2452
- "llm.topP",
2453
- "llm.stream",
2454
- "llm.providers",
2455
- "llm.limits",
2456
- "llm.capabilities"
2457
- ];
2458
- let fileSource = null;
2459
- if (configPath) {
2460
- configComponent.registerSource({
2461
- type: "file",
2462
- relativePath: configPath,
2463
- optional: true,
2464
- watch: false
2465
- });
2466
- configComponent.ensureSourcesCreated?.();
2467
- const sources = configComponent.getSources?.() || [];
2468
- fileSource = sources.find((s) => s.name === "file");
2469
- if (fileSource) {
2470
- for (const key of llmKeys) {
2471
- const value = fileSource.read?.(key);
2472
- if (value !== undefined) {
2473
- await configComponent.set(key, value);
2474
- }
2475
- }
2476
- }
2477
- }
2478
- configComponent.registerSource({
2479
- type: "env",
2480
- envPrefix: prefix,
2481
- priority: 20,
2482
- watch: false
2483
- });
2484
- configComponent.ensureSourcesCreated?.();
2485
- const sourcesAfterEnv = configComponent.getSources?.() || [];
2486
- const envSource = sourcesAfterEnv.find((s) => s.name === "env");
2487
- if (envSource) {
2488
- for (const key of llmKeys) {
2489
- const value = envSource.read?.(key);
2490
- if (value !== undefined) {
2491
- await configComponent.set(key, value);
2492
- }
2493
- }
2494
- }
2495
- for (const key of llmKeys) {
2496
- const envKey = toEnvKey(key, prefix);
2497
- const value = process.env[envKey];
2498
- if (value !== undefined) {
2499
- await configComponent.set(key, value);
2500
- }
2501
- }
2502
- const loadedKeys = new Set(llmKeys);
2503
- for (const envKey of Object.keys(process.env)) {
2504
- const configKey = envKeyToConfigKey(envKey, prefix, "llm");
2505
- if (!configKey)
2506
- continue;
2507
- if (loadedKeys.has(configKey))
2508
- continue;
2509
- loadedKeys.add(configKey);
2510
- const value = process.env[envKey];
2511
- if (value !== undefined) {
2512
- await configComponent.set(configKey, value);
2513
- }
2514
- }
2515
- if (config) {
2516
- const flatConfig = this.flattenConfig(config);
2517
- for (const [key, value] of Object.entries(flatConfig)) {
2518
- await configComponent.set(key, value);
2519
- }
2520
- }
2521
- this.registerConfigWatcher(configComponent);
2522
- }
2523
- flattenConfig(obj, prefix = "llm") {
2524
- const result = {};
2525
- for (const [key, value] of Object.entries(obj)) {
2526
- const fullKey = `${prefix}.${key}`;
2527
- if (value && typeof value === "object" && !Array.isArray(value)) {
2528
- Object.assign(result, this.flattenConfig(value, fullKey));
2529
- } else {
2530
- result[fullKey] = value;
2531
- }
2532
- }
2533
- return result;
2534
- }
2535
- registerConfigWatcher(configComponent) {
2536
- if (typeof configComponent.watch !== "function") {
2537
- logger2.debug("ConfigComponent does not support watch, hot reload disabled");
2538
- return;
2539
- }
2540
- this.configWatcher = configComponent.watch("llm.*", (event) => {
2541
- this.onConfigChange(event);
2542
- });
2543
- logger2.debug("Config hot reload watcher registered for llm.*");
2544
- }
2545
- onConfigChange(event) {
2546
- logger2.info(`LLM config changed: ${event.key}`);
2547
- if (event.key === "llm.providers" || event.key.startsWith("llm.providers.")) {
2548
- logger2.info(`LLM provider config changed, will use new config on next call`);
2549
- } else if (event.key === "llm.defaultModel") {
2550
- logger2.info(`LLM default model changed to: ${event.newValue}`);
2551
- } else if (event.key === "llm.defaultProvider") {
2552
- logger2.info(`LLM default provider changed to: ${event.newValue}`);
2553
- } else {
2554
- logger2.debug(`LLM config updated: ${event.key}`);
2555
- }
2556
- }
2557
- async onStart() {
2558
- if (this._status === "stopped") {
2559
- this.setStatus("running");
2560
- }
2561
- }
2562
- async onStop() {
2563
- this.configWatcher?.();
2564
- this.configWatcher = undefined;
2565
- this.setStatus("stopped");
2566
- }
2567
- getLLMConfig(key, defaultValue) {
2568
- if (this.configComponent) {
2569
- const value = this.configComponent.get(key);
2570
- if (value !== undefined) {
2571
- return value;
2572
- }
2573
- }
2574
- return defaultValue;
2575
- }
2576
- resolveRequest(request) {
2577
- const defaultModel = this.getLLMConfig("llm.defaultModel", "gpt-4o");
2578
- const defaultProvider = this.getLLMConfig("llm.defaultProvider", "openai");
2579
- const defaultTemperature = this.getLLMConfig("llm.temperature", 0.7);
2580
- const defaultMaxTokens = this.getLLMConfig("llm.maxTokens", 4096);
2581
- const defaultTopP = this.getLLMConfig("llm.topP", 1);
2582
- const model = request.model || defaultModel;
2583
- const providerId = request.providerId || defaultProvider;
2584
- const apiKey = this.getApiKey(providerId);
2585
- if (!apiKey) {
2586
- throw new Error(`API key not found for provider ${providerId}. Set providers.${providerId}.apiKey in config, or use \${env://...} protocol in config file.`);
2587
- }
2588
- const messages = request.messages.map((msg) => ({
2589
- role: msg.role,
2590
- content: msg.content,
2591
- name: msg.name,
2592
- toolCallId: msg.toolCallId,
2593
- toolCalls: msg.toolCalls
2594
- }));
2595
- return {
2596
- providerId,
2597
- model,
2598
- invokeConfig: {
2599
- model: `${providerId}/${model}`,
2600
- apiKey,
2601
- baseURL: this.getProvider(providerId)?.baseURL
2602
- },
2603
- messages,
2604
- temperature: request.temperature ?? defaultTemperature,
2605
- maxTokens: request.maxTokens ?? defaultMaxTokens,
2606
- topP: request.topP ?? defaultTopP,
2607
- abortSignal: request.abortSignal
2608
- };
2609
- }
2610
- async invoke(request) {
2611
- const startTime = Date.now();
2612
- if (this._status !== "running") {
2613
- throw new Error("LLMComponent is not running");
2614
- }
2615
- const ctx = this.resolveRequest(request);
2616
- let output = {
2617
- content: "",
2618
- reasoning: undefined,
2619
- finishReason: "stop"
2620
- };
2621
- let finishUsage;
2622
- const result = await invoke(ctx.invokeConfig, {
2623
- messages: ctx.messages,
2624
- model: ctx.invokeConfig.model,
2625
- temperature: ctx.temperature,
2626
- maxTokens: ctx.maxTokens,
2627
- tools: request.tools,
2628
- env: this.env,
2629
- context: {
2630
- sessionId: request.context?.sessionId,
2631
- messageId: request.context?.messageId
2632
- }
2633
- }, { abort: ctx.abortSignal });
2634
- if (result.isError) {
2635
- throw new Error(result.result);
2636
- }
2637
- try {
2638
- const parsed = JSON.parse(result.result);
2639
- output.content = parsed.content || "";
2640
- output.reasoning = parsed.reasoning || undefined;
2641
- output.finishReason = parsed.tool_calls?.length ? "tool-calls" : "stop";
2642
- output.toolCalls = parsed.tool_calls || [];
2643
- if (parsed.usage) {
2644
- finishUsage = {
2645
- promptTokens: parsed.usage.promptTokens ?? parsed.usage.inputTokens ?? 0,
2646
- completionTokens: parsed.usage.completionTokens ?? parsed.usage.outputTokens ?? 0,
2647
- totalTokens: parsed.usage.totalTokens ?? 0
2648
- };
2649
- }
2650
- output.usage = finishUsage;
2651
- } catch {
2652
- output.content = result.result;
2653
- output.finishReason = "stop";
2654
- }
2655
- const latencyMs = Date.now() - startTime;
2656
- if (!request.skipThresholdCheck) {
2657
- const sessionId = request.context?.sessionId;
2658
- const contextError = this.checkContextThreshold(finishUsage, ctx.providerId, ctx.model, sessionId);
2659
- if (contextError) {
2660
- throw contextError;
2661
- }
2662
- }
2663
- return {
2664
- output,
2665
- providerId: ctx.providerId,
2666
- model: ctx.model,
2667
- latencyMs
2668
- };
2669
- }
2670
- getApiKey(providerId) {
2671
- const providers = this.getLLMConfig("llm.providers", {});
2672
- return providers?.[providerId]?.apiKey;
2673
- }
2674
- getProvider(id) {
2675
- const providers = this.getLLMConfig("llm.providers", {});
2676
- return providers?.[id];
2677
- }
2678
- listProviders() {
2679
- const providers = this.getLLMConfig("llm.providers", {});
2680
- return Object.keys(providers || {});
2681
- }
2682
- getModelLimits(providerId, modelId) {
2683
- const provider = this.getProvider(providerId);
2684
- if (!provider?.limits)
2685
- return;
2686
- const targetModel = modelId || provider.defaultModel;
2687
- if (!targetModel)
2688
- return;
2689
- return provider.limits[targetModel];
2690
- }
2691
- getContextThresholdConfig(providerId, modelId) {
2692
- const defaults = {
2693
- contextWindow: 128000,
2694
- thresholdRatio: 0.9
2695
- };
2696
- const limits = this.getModelLimits(providerId, modelId);
2697
- if (!limits)
2698
- return defaults;
2699
- return {
2700
- contextWindow: limits.contextWindow || defaults.contextWindow,
2701
- thresholdRatio: limits.compactionThreshold ?? defaults.thresholdRatio
2702
- };
2703
- }
2704
- checkContextThreshold(usage, providerId, modelId, sessionId) {
2705
- if (!usage)
2706
- return;
2707
- const config = this.getContextThresholdConfig(providerId, modelId);
2708
- const threshold = config.contextWindow * config.thresholdRatio;
2709
- if (usage.totalTokens >= threshold) {
2710
- return new ContextError(`Context threshold exceeded: ${usage.totalTokens}/${config.contextWindow} (${(usage.totalTokens / config.contextWindow * 100).toFixed(1)}%)`, ErrorCodes.CONTEXT_THRESHOLD_EXCEEDED, sessionId, {
2711
- promptTokens: usage.promptTokens ?? 0,
2712
- completionTokens: usage.completionTokens ?? 0,
2713
- totalTokens: usage.totalTokens
2714
- }, config.contextWindow);
2715
- }
2716
- return;
2717
- }
2718
- }
2719
- __legacyDecorateClassTS([
2720
- TracedAs("llm.component.invoke", { recordParams: true, recordResult: true, log: true })
2721
- ], LLMComponent.prototype, "invoke", null);
2722
- export {
2723
- parseModelString,
2724
- invokeNonStream,
2725
- invoke,
2726
- createInvokeConfig,
2727
- ProviderManager,
2728
- ProviderConfigSchema,
2729
- ProviderCapabilitiesSchema,
2730
- ModelLimitsSchema,
2731
- LLMTransform,
2732
- LLMHooks,
2733
- LLMDefaultConfigSchema,
2734
- LLMConfigSchema,
2735
- LLMComponent
2736
- };