@ax-llm/ax 11.0.46 → 11.0.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.cjs CHANGED
@@ -365,14 +365,14 @@ var defaultRetryConfig = {
365
365
  var defaultTimeoutMs = 3e4;
366
366
  var textDecoderStream = import_web3.TextDecoderStream ?? TextDecoderStreamPolyfill;
367
367
  var AxAIServiceError = class extends Error {
368
- constructor(message, url, requestBody, context = {}) {
368
+ constructor(message, url, requestBody, context3 = {}) {
369
369
  super(message);
370
370
  this.url = url;
371
371
  this.requestBody = requestBody;
372
372
  this.name = this.constructor.name;
373
373
  this.timestamp = (/* @__PURE__ */ new Date()).toISOString();
374
374
  this.errorId = crypto.randomUUID();
375
- this.context = context;
375
+ this.context = context3;
376
376
  this.stack = this.toString();
377
377
  }
378
378
  timestamp;
@@ -394,11 +394,11 @@ var AxAIServiceError = class extends Error {
394
394
  }
395
395
  };
396
396
  var AxAIServiceStatusError = class extends AxAIServiceError {
397
- constructor(status, statusText, url, requestBody, context) {
397
+ constructor(status, statusText, url, requestBody, context3) {
398
398
  super(`HTTP ${status} - ${statusText}`, url, requestBody, {
399
399
  httpStatus: status,
400
400
  httpStatusText: statusText,
401
- ...context
401
+ ...context3
402
402
  });
403
403
  this.status = status;
404
404
  this.statusText = statusText;
@@ -406,11 +406,11 @@ var AxAIServiceStatusError = class extends AxAIServiceError {
406
406
  }
407
407
  };
408
408
  var AxAIServiceNetworkError = class extends AxAIServiceError {
409
- constructor(originalError, url, requestBody, context) {
409
+ constructor(originalError, url, requestBody, context3) {
410
410
  super(`Network Error: ${originalError.message}`, url, requestBody, {
411
411
  originalErrorName: originalError.name,
412
412
  originalErrorStack: originalError.stack,
413
- ...context
413
+ ...context3
414
414
  });
415
415
  this.originalError = originalError;
416
416
  this.name = this.constructor.name;
@@ -418,33 +418,33 @@ var AxAIServiceNetworkError = class extends AxAIServiceError {
418
418
  }
419
419
  };
420
420
  var AxAIServiceResponseError = class extends AxAIServiceError {
421
- constructor(message, url, requestBody, context) {
422
- super(message, url, requestBody, context);
421
+ constructor(message, url, requestBody, context3) {
422
+ super(message, url, requestBody, context3);
423
423
  this.name = this.constructor.name;
424
424
  }
425
425
  };
426
426
  var AxAIServiceStreamTerminatedError = class extends AxAIServiceError {
427
- constructor(url, requestBody, lastChunk, context) {
427
+ constructor(url, requestBody, lastChunk, context3) {
428
428
  super("Stream terminated unexpectedly by remote host", url, requestBody, {
429
429
  lastChunk,
430
- ...context
430
+ ...context3
431
431
  });
432
432
  this.lastChunk = lastChunk;
433
433
  this.name = this.constructor.name;
434
434
  }
435
435
  };
436
436
  var AxAIServiceTimeoutError = class extends AxAIServiceError {
437
- constructor(url, timeoutMs, requestBody, context) {
437
+ constructor(url, timeoutMs, requestBody, context3) {
438
438
  super(`Request timeout after ${timeoutMs}ms`, url, requestBody, {
439
439
  timeoutMs,
440
- ...context
440
+ ...context3
441
441
  });
442
442
  this.name = this.constructor.name;
443
443
  }
444
444
  };
445
445
  var AxAIServiceAuthenticationError = class extends AxAIServiceError {
446
- constructor(url, requestBody, context) {
447
- super("Authentication failed", url, requestBody, context);
446
+ constructor(url, requestBody, context3) {
447
+ super("Authentication failed", url, requestBody, context3);
448
448
  this.name = this.constructor.name;
449
449
  }
450
450
  };
@@ -490,14 +490,12 @@ var apiCall = async (api, json) => {
490
490
  );
491
491
  }
492
492
  }
493
- if (api.span?.isRecording()) {
494
- api.span.setAttributes({
495
- "http.request.method": api.put ? "PUT" : "POST",
496
- "url.full": apiUrl.href,
497
- "request.id": requestId,
498
- "request.startTime": metrics.startTime
499
- });
500
- }
493
+ api.span?.setAttributes({
494
+ "http.request.method": api.put ? "PUT" : "POST",
495
+ "url.full": apiUrl.href,
496
+ "request.id": requestId,
497
+ "request.startTime": metrics.startTime
498
+ });
501
499
  let attempt = 0;
502
500
  while (true) {
503
501
  const controller = new AbortController();
@@ -524,16 +522,14 @@ var apiCall = async (api, json) => {
524
522
  const delay = calculateRetryDelay(attempt, retryConfig);
525
523
  attempt++;
526
524
  updateRetryMetrics(metrics);
527
- if (api.span?.isRecording()) {
528
- api.span.addEvent("retry", {
529
- attempt,
530
- delay,
531
- status: res.status,
532
- "metrics.startTime": metrics.startTime,
533
- "metrics.retryCount": metrics.retryCount,
534
- "metrics.lastRetryTime": metrics.lastRetryTime
535
- });
536
- }
525
+ api.span?.addEvent("retry", {
526
+ attempt,
527
+ delay,
528
+ status: res.status,
529
+ "metrics.startTime": metrics.startTime,
530
+ "metrics.retryCount": metrics.retryCount,
531
+ "metrics.lastRetryTime": metrics.lastRetryTime
532
+ });
537
533
  await new Promise((resolve) => setTimeout(resolve, delay));
538
534
  continue;
539
535
  }
@@ -559,12 +555,10 @@ var apiCall = async (api, json) => {
559
555
  );
560
556
  }
561
557
  }
562
- if (api.span?.isRecording()) {
563
- api.span.setAttributes({
564
- "response.time": Date.now() - metrics.startTime,
565
- "response.retries": metrics.retryCount
566
- });
567
- }
558
+ api.span?.setAttributes({
559
+ "response.time": Date.now() - metrics.startTime,
560
+ "response.retries": metrics.retryCount
561
+ });
568
562
  return resJson;
569
563
  }
570
564
  if (!res.body) {
@@ -584,15 +578,11 @@ var apiCall = async (api, json) => {
584
578
  metrics.streamChunks = chunkCount;
585
579
  metrics.lastChunkTime = Date.now();
586
580
  controller2.enqueue(chunk);
587
- },
588
- flush() {
589
- if (api.span?.isRecording()) {
590
- api.span.setAttributes({
591
- "stream.chunks": chunkCount,
592
- "stream.duration": Date.now() - metrics.startTime,
593
- "response.retries": metrics.retryCount
594
- });
595
- }
581
+ api.span?.addEvent("stream.chunk", {
582
+ "stream.chunks": chunkCount,
583
+ "stream.duration": Date.now() - metrics.startTime,
584
+ "response.retries": metrics.retryCount
585
+ });
596
586
  }
597
587
  });
598
588
  let closed = false;
@@ -677,16 +667,14 @@ var apiCall = async (api, json) => {
677
667
  const delay = calculateRetryDelay(attempt, retryConfig);
678
668
  attempt++;
679
669
  updateRetryMetrics(metrics);
680
- if (api.span?.isRecording()) {
681
- api.span.addEvent("retry", {
682
- attempt,
683
- delay,
684
- error: error.message,
685
- "metrics.startTime": metrics.startTime,
686
- "metrics.retryCount": metrics.retryCount,
687
- "metrics.lastRetryTime": metrics.lastRetryTime
688
- });
689
- }
670
+ api.span?.addEvent("retry", {
671
+ attempt,
672
+ delay,
673
+ error: error.message,
674
+ "metrics.startTime": metrics.startTime,
675
+ "metrics.retryCount": metrics.retryCount,
676
+ "metrics.lastRetryTime": metrics.lastRetryTime
677
+ });
690
678
  await new Promise((resolve) => setTimeout(resolve, delay));
691
679
  continue;
692
680
  }
@@ -1085,7 +1073,7 @@ var AxBaseAI = class {
1085
1073
  modelConfig.stream = false;
1086
1074
  }
1087
1075
  if (this.tracer) {
1088
- return await this.tracer?.startActiveSpan(
1076
+ return await this.tracer.startActiveSpan(
1089
1077
  "AI Chat Request",
1090
1078
  {
1091
1079
  kind: import_api2.SpanKind.SERVER,
@@ -1103,6 +1091,7 @@ var AxBaseAI = class {
1103
1091
  [axSpanAttributes.LLM_REQUEST_LLM_IS_STREAMING]: modelConfig.stream
1104
1092
  }
1105
1093
  },
1094
+ options?.traceContext ?? import_api2.context.active(),
1106
1095
  async (span) => {
1107
1096
  return await this._chat2(model, modelConfig, req, options, span);
1108
1097
  }
@@ -1284,6 +1273,7 @@ var AxBaseAI = class {
1284
1273
  [axSpanAttributes.LLM_REQUEST_MODEL]: embedModel
1285
1274
  }
1286
1275
  },
1276
+ options?.traceContext ?? import_api2.context.active(),
1287
1277
  async (span) => {
1288
1278
  try {
1289
1279
  return await this._embed2(embedModel, req, options, span);
@@ -2234,6 +2224,10 @@ var AxAIOpenAIImpl = class {
2234
2224
  break;
2235
2225
  case "high":
2236
2226
  reqValue.reasoning_effort = "high";
2227
+ break;
2228
+ case "highest":
2229
+ reqValue.reasoning_effort = "high";
2230
+ break;
2237
2231
  }
2238
2232
  }
2239
2233
  if (this.chatReqUpdater) {
@@ -3272,16 +3266,19 @@ var AxAIGoogleGeminiImpl = class {
3272
3266
  if (config.thinkingTokenBudget) {
3273
3267
  switch (config.thinkingTokenBudget) {
3274
3268
  case "minimal":
3275
- thinkingConfig.thinkingBudget = 0;
3269
+ thinkingConfig.thinkingBudget = 200;
3276
3270
  break;
3277
3271
  case "low":
3278
- thinkingConfig.thinkingBudget = 1024;
3272
+ thinkingConfig.thinkingBudget = 800;
3279
3273
  break;
3280
3274
  case "medium":
3281
- thinkingConfig.thinkingBudget = 4096;
3275
+ thinkingConfig.thinkingBudget = 5e3;
3282
3276
  break;
3283
3277
  case "high":
3284
- thinkingConfig.thinkingBudget = 8192;
3278
+ thinkingConfig.thinkingBudget = 1e4;
3279
+ break;
3280
+ case "highest":
3281
+ thinkingConfig.thinkingBudget = 24500;
3285
3282
  break;
3286
3283
  }
3287
3284
  }
@@ -4682,9 +4679,9 @@ var SignatureParser = class {
4682
4679
  };
4683
4680
  } catch (error) {
4684
4681
  const errorMessage = error instanceof Error ? error.message : "Unknown error";
4685
- const context = this.getErrorContext();
4682
+ const context3 = this.getErrorContext();
4686
4683
  throw new Error(`${errorMessage}
4687
- ${context}`);
4684
+ ${context3}`);
4688
4685
  }
4689
4686
  }
4690
4687
  getErrorContext() {
@@ -6783,7 +6780,8 @@ var AxGen = class extends AxProgramWithSignature {
6783
6780
  async forwardSendRequest({
6784
6781
  ai,
6785
6782
  mem,
6786
- options
6783
+ options,
6784
+ traceContext
6787
6785
  }) {
6788
6786
  const {
6789
6787
  sessionId,
@@ -6816,7 +6814,8 @@ var AxGen = class extends AxProgramWithSignature {
6816
6814
  rateLimiter,
6817
6815
  stream,
6818
6816
  debug: false,
6819
- thinkingTokenBudget
6817
+ thinkingTokenBudget,
6818
+ traceContext
6820
6819
  }
6821
6820
  );
6822
6821
  return res;
@@ -6825,7 +6824,8 @@ var AxGen = class extends AxProgramWithSignature {
6825
6824
  ai,
6826
6825
  mem,
6827
6826
  options,
6828
- span
6827
+ span,
6828
+ traceContext
6829
6829
  }) {
6830
6830
  const { sessionId, traceId, functions: _functions } = options ?? {};
6831
6831
  const fastFail = options?.fastFail ?? this.options?.fastFail;
@@ -6834,7 +6834,8 @@ var AxGen = class extends AxProgramWithSignature {
6834
6834
  const res = await this.forwardSendRequest({
6835
6835
  ai,
6836
6836
  mem,
6837
- options
6837
+ options,
6838
+ traceContext
6838
6839
  });
6839
6840
  if (res instanceof import_web5.ReadableStream) {
6840
6841
  yield* this.processStreamingResponse({
@@ -7072,7 +7073,7 @@ Content: ${result.content}`
7072
7073
  }
7073
7074
  return { ...this.values };
7074
7075
  }
7075
- async *_forward2(ai, values, options, span) {
7076
+ async *_forward2(ai, values, options, span, traceContext) {
7076
7077
  const stopFunction = (options?.stopFunction ?? this.options?.stopFunction)?.toLowerCase();
7077
7078
  const maxRetries = options.maxRetries ?? this.options?.maxRetries ?? 10;
7078
7079
  const maxSteps = options.maxSteps ?? this.options?.maxSteps ?? 10;
@@ -7096,7 +7097,13 @@ Content: ${result.content}`
7096
7097
  multiStepLoop: for (let n = 0; n < maxSteps; n++) {
7097
7098
  for (let errCount = 0; errCount < maxRetries; errCount++) {
7098
7099
  try {
7099
- const generator = this.forwardCore({ options, ai, mem, span });
7100
+ const generator = this.forwardCore({
7101
+ options,
7102
+ ai,
7103
+ mem,
7104
+ span,
7105
+ traceContext
7106
+ });
7100
7107
  for await (const delta of generator) {
7101
7108
  if (delta !== void 0) {
7102
7109
  yield { version: errCount, delta };
@@ -7176,7 +7183,7 @@ Content: ${result.content}`
7176
7183
  return false;
7177
7184
  }
7178
7185
  async *_forward1(ai, values, options) {
7179
- const tracer = options?.tracer ?? this.options?.tracer;
7186
+ const tracer = options?.tracer ?? this.options?.tracer ?? ai.getOptions().tracer;
7180
7187
  let functions = this.functions;
7181
7188
  if (options?.functions) {
7182
7189
  functions = parseFunctions(options.functions, this.functions);
@@ -7205,6 +7212,8 @@ Content: ${result.content}`
7205
7212
  kind: import_api22.SpanKind.SERVER,
7206
7213
  attributes
7207
7214
  });
7215
+ const currentContext = import_api22.context.active();
7216
+ const traceContext = import_api22.trace.setSpan(currentContext, span);
7208
7217
  try {
7209
7218
  if (!this.excludeContentFromTrace) {
7210
7219
  span.addEvent("input", { content: JSON.stringify(values, null, 2) });
@@ -7216,7 +7225,8 @@ Content: ${result.content}`
7216
7225
  ...options,
7217
7226
  functions
7218
7227
  },
7219
- span
7228
+ span,
7229
+ traceContext
7220
7230
  );
7221
7231
  if (!this.excludeContentFromTrace) {
7222
7232
  span.addEvent("output", {
@@ -8101,7 +8111,7 @@ var AxDBBase = class {
8101
8111
  if (!this.tracer) {
8102
8112
  return await this._upsert(req, update);
8103
8113
  }
8104
- return await this.tracer?.startActiveSpan(
8114
+ return await this.tracer.startActiveSpan(
8105
8115
  "DB Upsert Request",
8106
8116
  {
8107
8117
  kind: import_api23.SpanKind.SERVER,
@@ -8135,7 +8145,7 @@ var AxDBBase = class {
8135
8145
  if (!this.tracer) {
8136
8146
  return await this._batchUpsert(req, update);
8137
8147
  }
8138
- return await this.tracer?.startActiveSpan(
8148
+ return await this.tracer.startActiveSpan(
8139
8149
  "DB Batch Upsert Request",
8140
8150
  {
8141
8151
  kind: import_api23.SpanKind.SERVER,
@@ -8163,7 +8173,7 @@ var AxDBBase = class {
8163
8173
  if (!this.tracer) {
8164
8174
  return await this._query(req);
8165
8175
  }
8166
- return await this.tracer?.startActiveSpan(
8176
+ return await this.tracer.startActiveSpan(
8167
8177
  "DB Query Request",
8168
8178
  {
8169
8179
  kind: import_api23.SpanKind.SERVER,
@@ -9140,24 +9150,24 @@ var AxJSInterpreter = class {
9140
9150
  this.permissions = permissions ?? [];
9141
9151
  }
9142
9152
  codeInterpreterJavascript(code) {
9143
- const context = { console };
9153
+ const context3 = { console };
9144
9154
  if (this.permissions.includes("node:fs" /* FS */)) {
9145
- context.fs = _fs;
9155
+ context3.fs = _fs;
9146
9156
  }
9147
9157
  if (this.permissions.includes("net" /* NET */)) {
9148
- context.http = _http;
9149
- context.https = _https;
9158
+ context3.http = _http;
9159
+ context3.https = _https;
9150
9160
  }
9151
9161
  if (this.permissions.includes("os" /* OS */)) {
9152
- context.os = _os;
9162
+ context3.os = _os;
9153
9163
  }
9154
9164
  if (this.permissions.includes("crypto" /* CRYPTO */)) {
9155
- context.crypto = _crypto;
9165
+ context3.crypto = _crypto;
9156
9166
  }
9157
9167
  if (this.permissions.includes("process" /* PROCESS */)) {
9158
- context.process = _process;
9168
+ context3.process = _process;
9159
9169
  }
9160
- return (0, import_node_vm.runInNewContext)(`(function() { ${code} })()`, context);
9170
+ return (0, import_node_vm.runInNewContext)(`(function() { ${code} })()`, context3);
9161
9171
  }
9162
9172
  toFunction() {
9163
9173
  return {
@@ -9940,9 +9950,9 @@ var colorLog6 = new ColorLog();
9940
9950
  var AxSimpleClassifierClass = class {
9941
9951
  name;
9942
9952
  context;
9943
- constructor(name, context) {
9953
+ constructor(name, context3) {
9944
9954
  this.name = name;
9945
- this.context = context;
9955
+ this.context = context3;
9946
9956
  }
9947
9957
  getName() {
9948
9958
  return this.name;
@@ -11946,20 +11956,20 @@ var AxRAG = class extends AxChainOfThought {
11946
11956
  this.register(this.genQuery);
11947
11957
  }
11948
11958
  async forward(ai, { question }, options) {
11949
- let context = [];
11959
+ let context3 = [];
11950
11960
  for (let i = 0; i < this.maxHops; i++) {
11951
11961
  const { query } = await this.genQuery.forward(
11952
11962
  ai,
11953
11963
  {
11954
- context,
11964
+ context: context3,
11955
11965
  question
11956
11966
  },
11957
11967
  options
11958
11968
  );
11959
11969
  const val = await this.queryFn(query);
11960
- context = AxStringUtil.dedup([...context, val]);
11970
+ context3 = AxStringUtil.dedup([...context3, val]);
11961
11971
  }
11962
- return super.forward(ai, { context, question }, options);
11972
+ return super.forward(ai, { context: context3, question }, options);
11963
11973
  }
11964
11974
  };
11965
11975
  // Annotate the CommonJS export names for ESM import in node: