@ax-llm/ax 11.0.47 → 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.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ReadableStream as ReadableStream$1 } from 'node:stream/web';
2
- import { Span, Tracer } from '@opentelemetry/api';
2
+ import { Span, Context, Tracer } from '@opentelemetry/api';
3
3
  import { ReadableStream } from 'stream/web';
4
4
 
5
5
  interface RetryConfig {
@@ -332,7 +332,7 @@ type AxRateLimiterFunction = <T = unknown>(reqFunc: () => Promise<T | ReadableSt
332
332
  }>) => Promise<T | ReadableStream$1<T>>;
333
333
  type AxAIPromptConfig = {
334
334
  stream?: boolean;
335
- thinkingTokenBudget?: 'minimal' | 'low' | 'medium' | 'high';
335
+ thinkingTokenBudget?: 'minimal' | 'low' | 'medium' | 'high' | 'highest';
336
336
  };
337
337
  type AxAIServiceOptions = {
338
338
  debug?: boolean;
@@ -351,6 +351,7 @@ type AxAIServiceActionOptions<TModel = unknown, TEmbedModel = unknown> = {
351
351
  debug?: boolean;
352
352
  debugHideSystemPrompt?: boolean;
353
353
  hideThought?: boolean;
354
+ traceContext?: Context;
354
355
  };
355
356
  interface AxAIService<TModel = unknown, TEmbedModel = unknown> {
356
357
  getId(): string;
@@ -1691,7 +1692,7 @@ type AxProgramForwardOptions = {
1691
1692
  fastFail?: boolean;
1692
1693
  debug?: boolean;
1693
1694
  debugHideSystemPrompt?: boolean;
1694
- thinkingTokenBudget?: 'minimal' | 'low' | 'medium' | 'high';
1695
+ thinkingTokenBudget?: 'minimal' | 'low' | 'medium' | 'high' | 'highest';
1695
1696
  traceLabel?: string;
1696
1697
  };
1697
1698
  type AxProgramStreamingForwardOptions = Omit<AxProgramForwardOptions, 'stream'>;
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ReadableStream as ReadableStream$1 } from 'node:stream/web';
2
- import { Span, Tracer } from '@opentelemetry/api';
2
+ import { Span, Context, Tracer } from '@opentelemetry/api';
3
3
  import { ReadableStream } from 'stream/web';
4
4
 
5
5
  interface RetryConfig {
@@ -332,7 +332,7 @@ type AxRateLimiterFunction = <T = unknown>(reqFunc: () => Promise<T | ReadableSt
332
332
  }>) => Promise<T | ReadableStream$1<T>>;
333
333
  type AxAIPromptConfig = {
334
334
  stream?: boolean;
335
- thinkingTokenBudget?: 'minimal' | 'low' | 'medium' | 'high';
335
+ thinkingTokenBudget?: 'minimal' | 'low' | 'medium' | 'high' | 'highest';
336
336
  };
337
337
  type AxAIServiceOptions = {
338
338
  debug?: boolean;
@@ -351,6 +351,7 @@ type AxAIServiceActionOptions<TModel = unknown, TEmbedModel = unknown> = {
351
351
  debug?: boolean;
352
352
  debugHideSystemPrompt?: boolean;
353
353
  hideThought?: boolean;
354
+ traceContext?: Context;
354
355
  };
355
356
  interface AxAIService<TModel = unknown, TEmbedModel = unknown> {
356
357
  getId(): string;
@@ -1691,7 +1692,7 @@ type AxProgramForwardOptions = {
1691
1692
  fastFail?: boolean;
1692
1693
  debug?: boolean;
1693
1694
  debugHideSystemPrompt?: boolean;
1694
- thinkingTokenBudget?: 'minimal' | 'low' | 'medium' | 'high';
1695
+ thinkingTokenBudget?: 'minimal' | 'low' | 'medium' | 'high' | 'highest';
1695
1696
  traceLabel?: string;
1696
1697
  };
1697
1698
  type AxProgramStreamingForwardOptions = Omit<AxProgramForwardOptions, 'stream'>;
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // ai/base.ts
2
- import { SpanKind } from "@opentelemetry/api";
2
+ import { context, SpanKind } from "@opentelemetry/api";
3
3
 
4
4
  // trace/trace.ts
5
5
  var axSpanAttributes = {
@@ -205,14 +205,14 @@ var defaultRetryConfig = {
205
205
  var defaultTimeoutMs = 3e4;
206
206
  var textDecoderStream = TextDecoderStreamNative ?? TextDecoderStreamPolyfill;
207
207
  var AxAIServiceError = class extends Error {
208
- constructor(message, url, requestBody, context = {}) {
208
+ constructor(message, url, requestBody, context3 = {}) {
209
209
  super(message);
210
210
  this.url = url;
211
211
  this.requestBody = requestBody;
212
212
  this.name = this.constructor.name;
213
213
  this.timestamp = (/* @__PURE__ */ new Date()).toISOString();
214
214
  this.errorId = crypto.randomUUID();
215
- this.context = context;
215
+ this.context = context3;
216
216
  this.stack = this.toString();
217
217
  }
218
218
  timestamp;
@@ -234,11 +234,11 @@ var AxAIServiceError = class extends Error {
234
234
  }
235
235
  };
236
236
  var AxAIServiceStatusError = class extends AxAIServiceError {
237
- constructor(status, statusText, url, requestBody, context) {
237
+ constructor(status, statusText, url, requestBody, context3) {
238
238
  super(`HTTP ${status} - ${statusText}`, url, requestBody, {
239
239
  httpStatus: status,
240
240
  httpStatusText: statusText,
241
- ...context
241
+ ...context3
242
242
  });
243
243
  this.status = status;
244
244
  this.statusText = statusText;
@@ -246,11 +246,11 @@ var AxAIServiceStatusError = class extends AxAIServiceError {
246
246
  }
247
247
  };
248
248
  var AxAIServiceNetworkError = class extends AxAIServiceError {
249
- constructor(originalError, url, requestBody, context) {
249
+ constructor(originalError, url, requestBody, context3) {
250
250
  super(`Network Error: ${originalError.message}`, url, requestBody, {
251
251
  originalErrorName: originalError.name,
252
252
  originalErrorStack: originalError.stack,
253
- ...context
253
+ ...context3
254
254
  });
255
255
  this.originalError = originalError;
256
256
  this.name = this.constructor.name;
@@ -258,33 +258,33 @@ var AxAIServiceNetworkError = class extends AxAIServiceError {
258
258
  }
259
259
  };
260
260
  var AxAIServiceResponseError = class extends AxAIServiceError {
261
- constructor(message, url, requestBody, context) {
262
- super(message, url, requestBody, context);
261
+ constructor(message, url, requestBody, context3) {
262
+ super(message, url, requestBody, context3);
263
263
  this.name = this.constructor.name;
264
264
  }
265
265
  };
266
266
  var AxAIServiceStreamTerminatedError = class extends AxAIServiceError {
267
- constructor(url, requestBody, lastChunk, context) {
267
+ constructor(url, requestBody, lastChunk, context3) {
268
268
  super("Stream terminated unexpectedly by remote host", url, requestBody, {
269
269
  lastChunk,
270
- ...context
270
+ ...context3
271
271
  });
272
272
  this.lastChunk = lastChunk;
273
273
  this.name = this.constructor.name;
274
274
  }
275
275
  };
276
276
  var AxAIServiceTimeoutError = class extends AxAIServiceError {
277
- constructor(url, timeoutMs, requestBody, context) {
277
+ constructor(url, timeoutMs, requestBody, context3) {
278
278
  super(`Request timeout after ${timeoutMs}ms`, url, requestBody, {
279
279
  timeoutMs,
280
- ...context
280
+ ...context3
281
281
  });
282
282
  this.name = this.constructor.name;
283
283
  }
284
284
  };
285
285
  var AxAIServiceAuthenticationError = class extends AxAIServiceError {
286
- constructor(url, requestBody, context) {
287
- super("Authentication failed", url, requestBody, context);
286
+ constructor(url, requestBody, context3) {
287
+ super("Authentication failed", url, requestBody, context3);
288
288
  this.name = this.constructor.name;
289
289
  }
290
290
  };
@@ -330,14 +330,12 @@ var apiCall = async (api, json) => {
330
330
  );
331
331
  }
332
332
  }
333
- if (api.span?.isRecording()) {
334
- api.span.setAttributes({
335
- "http.request.method": api.put ? "PUT" : "POST",
336
- "url.full": apiUrl.href,
337
- "request.id": requestId,
338
- "request.startTime": metrics.startTime
339
- });
340
- }
333
+ api.span?.setAttributes({
334
+ "http.request.method": api.put ? "PUT" : "POST",
335
+ "url.full": apiUrl.href,
336
+ "request.id": requestId,
337
+ "request.startTime": metrics.startTime
338
+ });
341
339
  let attempt = 0;
342
340
  while (true) {
343
341
  const controller = new AbortController();
@@ -364,16 +362,14 @@ var apiCall = async (api, json) => {
364
362
  const delay = calculateRetryDelay(attempt, retryConfig);
365
363
  attempt++;
366
364
  updateRetryMetrics(metrics);
367
- if (api.span?.isRecording()) {
368
- api.span.addEvent("retry", {
369
- attempt,
370
- delay,
371
- status: res.status,
372
- "metrics.startTime": metrics.startTime,
373
- "metrics.retryCount": metrics.retryCount,
374
- "metrics.lastRetryTime": metrics.lastRetryTime
375
- });
376
- }
365
+ api.span?.addEvent("retry", {
366
+ attempt,
367
+ delay,
368
+ status: res.status,
369
+ "metrics.startTime": metrics.startTime,
370
+ "metrics.retryCount": metrics.retryCount,
371
+ "metrics.lastRetryTime": metrics.lastRetryTime
372
+ });
377
373
  await new Promise((resolve) => setTimeout(resolve, delay));
378
374
  continue;
379
375
  }
@@ -399,12 +395,10 @@ var apiCall = async (api, json) => {
399
395
  );
400
396
  }
401
397
  }
402
- if (api.span?.isRecording()) {
403
- api.span.setAttributes({
404
- "response.time": Date.now() - metrics.startTime,
405
- "response.retries": metrics.retryCount
406
- });
407
- }
398
+ api.span?.setAttributes({
399
+ "response.time": Date.now() - metrics.startTime,
400
+ "response.retries": metrics.retryCount
401
+ });
408
402
  return resJson;
409
403
  }
410
404
  if (!res.body) {
@@ -424,15 +418,11 @@ var apiCall = async (api, json) => {
424
418
  metrics.streamChunks = chunkCount;
425
419
  metrics.lastChunkTime = Date.now();
426
420
  controller2.enqueue(chunk);
427
- },
428
- flush() {
429
- if (api.span?.isRecording()) {
430
- api.span.setAttributes({
431
- "stream.chunks": chunkCount,
432
- "stream.duration": Date.now() - metrics.startTime,
433
- "response.retries": metrics.retryCount
434
- });
435
- }
421
+ api.span?.addEvent("stream.chunk", {
422
+ "stream.chunks": chunkCount,
423
+ "stream.duration": Date.now() - metrics.startTime,
424
+ "response.retries": metrics.retryCount
425
+ });
436
426
  }
437
427
  });
438
428
  let closed = false;
@@ -517,16 +507,14 @@ var apiCall = async (api, json) => {
517
507
  const delay = calculateRetryDelay(attempt, retryConfig);
518
508
  attempt++;
519
509
  updateRetryMetrics(metrics);
520
- if (api.span?.isRecording()) {
521
- api.span.addEvent("retry", {
522
- attempt,
523
- delay,
524
- error: error.message,
525
- "metrics.startTime": metrics.startTime,
526
- "metrics.retryCount": metrics.retryCount,
527
- "metrics.lastRetryTime": metrics.lastRetryTime
528
- });
529
- }
510
+ api.span?.addEvent("retry", {
511
+ attempt,
512
+ delay,
513
+ error: error.message,
514
+ "metrics.startTime": metrics.startTime,
515
+ "metrics.retryCount": metrics.retryCount,
516
+ "metrics.lastRetryTime": metrics.lastRetryTime
517
+ });
530
518
  await new Promise((resolve) => setTimeout(resolve, delay));
531
519
  continue;
532
520
  }
@@ -927,7 +915,7 @@ var AxBaseAI = class {
927
915
  modelConfig.stream = false;
928
916
  }
929
917
  if (this.tracer) {
930
- return await this.tracer?.startActiveSpan(
918
+ return await this.tracer.startActiveSpan(
931
919
  "AI Chat Request",
932
920
  {
933
921
  kind: SpanKind.SERVER,
@@ -945,6 +933,7 @@ var AxBaseAI = class {
945
933
  [axSpanAttributes.LLM_REQUEST_LLM_IS_STREAMING]: modelConfig.stream
946
934
  }
947
935
  },
936
+ options?.traceContext ?? context.active(),
948
937
  async (span) => {
949
938
  return await this._chat2(model, modelConfig, req, options, span);
950
939
  }
@@ -1126,6 +1115,7 @@ var AxBaseAI = class {
1126
1115
  [axSpanAttributes.LLM_REQUEST_MODEL]: embedModel
1127
1116
  }
1128
1117
  },
1118
+ options?.traceContext ?? context.active(),
1129
1119
  async (span) => {
1130
1120
  try {
1131
1121
  return await this._embed2(embedModel, req, options, span);
@@ -2076,6 +2066,10 @@ var AxAIOpenAIImpl = class {
2076
2066
  break;
2077
2067
  case "high":
2078
2068
  reqValue.reasoning_effort = "high";
2069
+ break;
2070
+ case "highest":
2071
+ reqValue.reasoning_effort = "high";
2072
+ break;
2079
2073
  }
2080
2074
  }
2081
2075
  if (this.chatReqUpdater) {
@@ -3114,16 +3108,19 @@ var AxAIGoogleGeminiImpl = class {
3114
3108
  if (config.thinkingTokenBudget) {
3115
3109
  switch (config.thinkingTokenBudget) {
3116
3110
  case "minimal":
3117
- thinkingConfig.thinkingBudget = 0;
3111
+ thinkingConfig.thinkingBudget = 200;
3118
3112
  break;
3119
3113
  case "low":
3120
- thinkingConfig.thinkingBudget = 1024;
3114
+ thinkingConfig.thinkingBudget = 800;
3121
3115
  break;
3122
3116
  case "medium":
3123
- thinkingConfig.thinkingBudget = 4096;
3117
+ thinkingConfig.thinkingBudget = 5e3;
3124
3118
  break;
3125
3119
  case "high":
3126
- thinkingConfig.thinkingBudget = 8192;
3120
+ thinkingConfig.thinkingBudget = 1e4;
3121
+ break;
3122
+ case "highest":
3123
+ thinkingConfig.thinkingBudget = 24500;
3127
3124
  break;
3128
3125
  }
3129
3126
  }
@@ -4189,7 +4186,11 @@ var AxAIGrok = class extends AxAIOpenAIBase {
4189
4186
 
4190
4187
  // dsp/generate.ts
4191
4188
  import { ReadableStream as ReadableStream3 } from "node:stream/web";
4192
- import { SpanKind as SpanKind2 } from "@opentelemetry/api";
4189
+ import {
4190
+ context as context2,
4191
+ SpanKind as SpanKind2,
4192
+ trace
4193
+ } from "@opentelemetry/api";
4193
4194
 
4194
4195
  // ai/util.ts
4195
4196
  function mergeFunctionCalls(functionCalls, functionCallDeltas) {
@@ -4524,9 +4525,9 @@ var SignatureParser = class {
4524
4525
  };
4525
4526
  } catch (error) {
4526
4527
  const errorMessage = error instanceof Error ? error.message : "Unknown error";
4527
- const context = this.getErrorContext();
4528
+ const context3 = this.getErrorContext();
4528
4529
  throw new Error(`${errorMessage}
4529
- ${context}`);
4530
+ ${context3}`);
4530
4531
  }
4531
4532
  }
4532
4533
  getErrorContext() {
@@ -6625,7 +6626,8 @@ var AxGen = class extends AxProgramWithSignature {
6625
6626
  async forwardSendRequest({
6626
6627
  ai,
6627
6628
  mem,
6628
- options
6629
+ options,
6630
+ traceContext
6629
6631
  }) {
6630
6632
  const {
6631
6633
  sessionId,
@@ -6658,7 +6660,8 @@ var AxGen = class extends AxProgramWithSignature {
6658
6660
  rateLimiter,
6659
6661
  stream,
6660
6662
  debug: false,
6661
- thinkingTokenBudget
6663
+ thinkingTokenBudget,
6664
+ traceContext
6662
6665
  }
6663
6666
  );
6664
6667
  return res;
@@ -6667,7 +6670,8 @@ var AxGen = class extends AxProgramWithSignature {
6667
6670
  ai,
6668
6671
  mem,
6669
6672
  options,
6670
- span
6673
+ span,
6674
+ traceContext
6671
6675
  }) {
6672
6676
  const { sessionId, traceId, functions: _functions } = options ?? {};
6673
6677
  const fastFail = options?.fastFail ?? this.options?.fastFail;
@@ -6676,7 +6680,8 @@ var AxGen = class extends AxProgramWithSignature {
6676
6680
  const res = await this.forwardSendRequest({
6677
6681
  ai,
6678
6682
  mem,
6679
- options
6683
+ options,
6684
+ traceContext
6680
6685
  });
6681
6686
  if (res instanceof ReadableStream3) {
6682
6687
  yield* this.processStreamingResponse({
@@ -6914,7 +6919,7 @@ Content: ${result.content}`
6914
6919
  }
6915
6920
  return { ...this.values };
6916
6921
  }
6917
- async *_forward2(ai, values, options, span) {
6922
+ async *_forward2(ai, values, options, span, traceContext) {
6918
6923
  const stopFunction = (options?.stopFunction ?? this.options?.stopFunction)?.toLowerCase();
6919
6924
  const maxRetries = options.maxRetries ?? this.options?.maxRetries ?? 10;
6920
6925
  const maxSteps = options.maxSteps ?? this.options?.maxSteps ?? 10;
@@ -6938,7 +6943,13 @@ Content: ${result.content}`
6938
6943
  multiStepLoop: for (let n = 0; n < maxSteps; n++) {
6939
6944
  for (let errCount = 0; errCount < maxRetries; errCount++) {
6940
6945
  try {
6941
- const generator = this.forwardCore({ options, ai, mem, span });
6946
+ const generator = this.forwardCore({
6947
+ options,
6948
+ ai,
6949
+ mem,
6950
+ span,
6951
+ traceContext
6952
+ });
6942
6953
  for await (const delta of generator) {
6943
6954
  if (delta !== void 0) {
6944
6955
  yield { version: errCount, delta };
@@ -7047,6 +7058,8 @@ Content: ${result.content}`
7047
7058
  kind: SpanKind2.SERVER,
7048
7059
  attributes
7049
7060
  });
7061
+ const currentContext = context2.active();
7062
+ const traceContext = trace.setSpan(currentContext, span);
7050
7063
  try {
7051
7064
  if (!this.excludeContentFromTrace) {
7052
7065
  span.addEvent("input", { content: JSON.stringify(values, null, 2) });
@@ -7058,7 +7071,8 @@ Content: ${result.content}`
7058
7071
  ...options,
7059
7072
  functions
7060
7073
  },
7061
- span
7074
+ span,
7075
+ traceContext
7062
7076
  );
7063
7077
  if (!this.excludeContentFromTrace) {
7064
7078
  span.addEvent("output", {
@@ -7943,7 +7957,7 @@ var AxDBBase = class {
7943
7957
  if (!this.tracer) {
7944
7958
  return await this._upsert(req, update);
7945
7959
  }
7946
- return await this.tracer?.startActiveSpan(
7960
+ return await this.tracer.startActiveSpan(
7947
7961
  "DB Upsert Request",
7948
7962
  {
7949
7963
  kind: SpanKind3.SERVER,
@@ -7977,7 +7991,7 @@ var AxDBBase = class {
7977
7991
  if (!this.tracer) {
7978
7992
  return await this._batchUpsert(req, update);
7979
7993
  }
7980
- return await this.tracer?.startActiveSpan(
7994
+ return await this.tracer.startActiveSpan(
7981
7995
  "DB Batch Upsert Request",
7982
7996
  {
7983
7997
  kind: SpanKind3.SERVER,
@@ -8005,7 +8019,7 @@ var AxDBBase = class {
8005
8019
  if (!this.tracer) {
8006
8020
  return await this._query(req);
8007
8021
  }
8008
- return await this.tracer?.startActiveSpan(
8022
+ return await this.tracer.startActiveSpan(
8009
8023
  "DB Query Request",
8010
8024
  {
8011
8025
  kind: SpanKind3.SERVER,
@@ -8982,24 +8996,24 @@ var AxJSInterpreter = class {
8982
8996
  this.permissions = permissions ?? [];
8983
8997
  }
8984
8998
  codeInterpreterJavascript(code) {
8985
- const context = { console };
8999
+ const context3 = { console };
8986
9000
  if (this.permissions.includes("node:fs" /* FS */)) {
8987
- context.fs = _fs;
9001
+ context3.fs = _fs;
8988
9002
  }
8989
9003
  if (this.permissions.includes("net" /* NET */)) {
8990
- context.http = _http;
8991
- context.https = _https;
9004
+ context3.http = _http;
9005
+ context3.https = _https;
8992
9006
  }
8993
9007
  if (this.permissions.includes("os" /* OS */)) {
8994
- context.os = _os;
9008
+ context3.os = _os;
8995
9009
  }
8996
9010
  if (this.permissions.includes("crypto" /* CRYPTO */)) {
8997
- context.crypto = _crypto;
9011
+ context3.crypto = _crypto;
8998
9012
  }
8999
9013
  if (this.permissions.includes("process" /* PROCESS */)) {
9000
- context.process = _process;
9014
+ context3.process = _process;
9001
9015
  }
9002
- return runInNewContext(`(function() { ${code} })()`, context);
9016
+ return runInNewContext(`(function() { ${code} })()`, context3);
9003
9017
  }
9004
9018
  toFunction() {
9005
9019
  return {
@@ -9782,9 +9796,9 @@ var colorLog6 = new ColorLog();
9782
9796
  var AxSimpleClassifierClass = class {
9783
9797
  name;
9784
9798
  context;
9785
- constructor(name, context) {
9799
+ constructor(name, context3) {
9786
9800
  this.name = name;
9787
- this.context = context;
9801
+ this.context = context3;
9788
9802
  }
9789
9803
  getName() {
9790
9804
  return this.name;
@@ -11788,20 +11802,20 @@ var AxRAG = class extends AxChainOfThought {
11788
11802
  this.register(this.genQuery);
11789
11803
  }
11790
11804
  async forward(ai, { question }, options) {
11791
- let context = [];
11805
+ let context3 = [];
11792
11806
  for (let i = 0; i < this.maxHops; i++) {
11793
11807
  const { query } = await this.genQuery.forward(
11794
11808
  ai,
11795
11809
  {
11796
- context,
11810
+ context: context3,
11797
11811
  question
11798
11812
  },
11799
11813
  options
11800
11814
  );
11801
11815
  const val = await this.queryFn(query);
11802
- context = AxStringUtil.dedup([...context, val]);
11816
+ context3 = AxStringUtil.dedup([...context3, val]);
11803
11817
  }
11804
- return super.forward(ai, { context, question }, options);
11818
+ return super.forward(ai, { context: context3, question }, options);
11805
11819
  }
11806
11820
  };
11807
11821
  export {