@ax-llm/ax 11.0.51 → 11.0.53

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.js CHANGED
@@ -292,10 +292,25 @@ var AxAIServiceStreamTerminatedError = class extends AxAIServiceError {
292
292
  };
293
293
  var AxAIServiceTimeoutError = class extends AxAIServiceError {
294
294
  constructor(url, timeoutMs, requestBody, context3) {
295
- super(`Request timeout after ${timeoutMs}ms`, url, requestBody, void 0, {
296
- timeoutMs,
297
- ...context3
298
- });
295
+ super(
296
+ `Request timed out after ${timeoutMs}ms`,
297
+ url,
298
+ requestBody,
299
+ void 0,
300
+ { timeoutMs, ...context3 }
301
+ );
302
+ this.name = this.constructor.name;
303
+ }
304
+ };
305
+ var AxAIServiceAbortedError = class extends AxAIServiceError {
306
+ constructor(url, reason, requestBody, context3) {
307
+ super(
308
+ `Request aborted${reason ? `: ${reason}` : ""}`,
309
+ url,
310
+ requestBody,
311
+ void 0,
312
+ { abortReason: reason, ...context3 }
313
+ );
299
314
  this.name = this.constructor.name;
300
315
  }
301
316
  };
@@ -355,9 +370,34 @@ var apiCall = async (api, json) => {
355
370
  });
356
371
  let attempt = 0;
357
372
  while (true) {
358
- const controller = new AbortController();
373
+ const combinedAbortController = new AbortController();
374
+ if (api.abortSignal) {
375
+ if (api.abortSignal.aborted) {
376
+ throw new AxAIServiceAbortedError(
377
+ apiUrl.href,
378
+ api.abortSignal.reason,
379
+ json,
380
+ { metrics }
381
+ );
382
+ }
383
+ const userAbortHandler = () => {
384
+ combinedAbortController.abort(
385
+ api.abortSignal.reason || "User aborted request"
386
+ );
387
+ };
388
+ api.abortSignal.addEventListener("abort", userAbortHandler, {
389
+ once: true
390
+ });
391
+ const originalAbort = combinedAbortController.abort.bind(
392
+ combinedAbortController
393
+ );
394
+ combinedAbortController.abort = (reason) => {
395
+ api.abortSignal.removeEventListener("abort", userAbortHandler);
396
+ originalAbort(reason);
397
+ };
398
+ }
359
399
  timeoutId = setTimeout(() => {
360
- controller.abort("Request timeout");
400
+ combinedAbortController.abort("Request timeout");
361
401
  }, timeoutMs);
362
402
  try {
363
403
  const res = await (api.fetch ?? fetch)(apiUrl, {
@@ -369,7 +409,7 @@ var apiCall = async (api, json) => {
369
409
  ...api.headers
370
410
  },
371
411
  body: JSON.stringify(json),
372
- signal: controller.signal
412
+ signal: combinedAbortController.signal
373
413
  });
374
414
  clearTimeout(timeoutId);
375
415
  if (res.status === 401 || res.status === 403) {
@@ -432,12 +472,12 @@ var apiCall = async (api, json) => {
432
472
  let lastChunk;
433
473
  let chunkCount = 0;
434
474
  const trackingStream = new TransformStream3({
435
- transform(chunk, controller2) {
475
+ transform(chunk, controller) {
436
476
  lastChunk = chunk;
437
477
  chunkCount++;
438
478
  metrics.streamChunks = chunkCount;
439
479
  metrics.lastChunkTime = Date.now();
440
- controller2.enqueue(chunk);
480
+ controller.enqueue(chunk);
441
481
  api.span?.addEvent("stream.chunk", {
442
482
  "stream.chunks": chunkCount,
443
483
  "stream.duration": Date.now() - metrics.startTime,
@@ -447,7 +487,7 @@ var apiCall = async (api, json) => {
447
487
  });
448
488
  let closed = false;
449
489
  return new ReadableStream2({
450
- start(controller2) {
490
+ start(controller) {
451
491
  const reader = res.body.pipeThrough(new textDecoderStream()).pipeThrough(new SSEParser()).pipeThrough(trackingStream).getReader();
452
492
  async function read() {
453
493
  try {
@@ -456,12 +496,12 @@ var apiCall = async (api, json) => {
456
496
  if (done) {
457
497
  if (!closed) {
458
498
  closed = true;
459
- controller2.close();
499
+ controller.close();
460
500
  }
461
501
  break;
462
502
  }
463
503
  if (closed) break;
464
- controller2.enqueue(value);
504
+ controller.enqueue(value);
465
505
  }
466
506
  } catch (e) {
467
507
  const error = e;
@@ -470,7 +510,7 @@ var apiCall = async (api, json) => {
470
510
  streamDuration: Date.now() - metrics.startTime
471
511
  };
472
512
  if (error.name === "AbortError" || error.message?.includes("aborted")) {
473
- controller2.error(
513
+ controller.error(
474
514
  new AxAIServiceStreamTerminatedError(
475
515
  apiUrl.href,
476
516
  json,
@@ -479,7 +519,7 @@ var apiCall = async (api, json) => {
479
519
  )
480
520
  );
481
521
  } else if (error instanceof TypeError && error.message.includes("cancelled")) {
482
- controller2.error(
522
+ controller.error(
483
523
  new AxAIServiceStreamTerminatedError(
484
524
  apiUrl.href,
485
525
  json,
@@ -491,7 +531,7 @@ var apiCall = async (api, json) => {
491
531
  )
492
532
  );
493
533
  } else {
494
- controller2.error(
534
+ controller.error(
495
535
  new AxAIServiceNetworkError(
496
536
  error,
497
537
  apiUrl.href,
@@ -518,9 +558,18 @@ var apiCall = async (api, json) => {
518
558
  });
519
559
  } catch (error) {
520
560
  if (error instanceof Error && error.name === "AbortError") {
521
- throw new AxAIServiceTimeoutError(apiUrl.href, timeoutMs, json, {
522
- metrics
523
- });
561
+ if (api.abortSignal?.aborted) {
562
+ throw new AxAIServiceAbortedError(
563
+ apiUrl.href,
564
+ api.abortSignal.reason,
565
+ json,
566
+ { metrics }
567
+ );
568
+ } else {
569
+ throw new AxAIServiceTimeoutError(apiUrl.href, timeoutMs, json, {
570
+ metrics
571
+ });
572
+ }
524
573
  }
525
574
  if (api.span?.isRecording()) {
526
575
  api.span.recordException(error);
@@ -720,13 +769,11 @@ var logResponseDelta = (delta) => {
720
769
 
721
770
  // ai/base.ts
722
771
  var axBaseAIDefaultConfig = () => structuredClone({
723
- maxTokens: 2e3,
724
772
  temperature: 0,
725
773
  topK: 40,
726
774
  topP: 0.9
727
775
  });
728
776
  var axBaseAIDefaultCreativeConfig = () => structuredClone({
729
- maxTokens: 2e3,
730
777
  temperature: 0.4,
731
778
  topP: 0.7,
732
779
  frequencyPenalty: 0.2
@@ -769,6 +816,7 @@ var AxBaseAI = class {
769
816
  timeout;
770
817
  excludeContentFromTrace;
771
818
  models;
819
+ abortSignal;
772
820
  modelInfo;
773
821
  modelUsage;
774
822
  embedModelUsage;
@@ -829,6 +877,7 @@ var AxBaseAI = class {
829
877
  this.timeout = options.timeout;
830
878
  this.tracer = options.tracer;
831
879
  this.excludeContentFromTrace = options.excludeContentFromTrace;
880
+ this.abortSignal = options.abortSignal;
832
881
  }
833
882
  getOptions() {
834
883
  return {
@@ -837,7 +886,8 @@ var AxBaseAI = class {
837
886
  fetch: this.fetch,
838
887
  tracer: this.tracer,
839
888
  timeout: this.timeout,
840
- excludeContentFromTrace: this.excludeContentFromTrace
889
+ excludeContentFromTrace: this.excludeContentFromTrace,
890
+ abortSignal: this.abortSignal
841
891
  };
842
892
  }
843
893
  getModelList() {
@@ -949,14 +999,14 @@ var AxBaseAI = class {
949
999
  [axSpanAttributes.LLM_SYSTEM]: this.name,
950
1000
  [axSpanAttributes.LLM_OPERATION_NAME]: "chat",
951
1001
  [axSpanAttributes.LLM_REQUEST_MODEL]: model,
952
- [axSpanAttributes.LLM_REQUEST_MAX_TOKENS]: modelConfig.maxTokens,
1002
+ [axSpanAttributes.LLM_REQUEST_MAX_TOKENS]: modelConfig.maxTokens ?? "Not set",
953
1003
  [axSpanAttributes.LLM_REQUEST_TEMPERATURE]: modelConfig.temperature,
954
- [axSpanAttributes.LLM_REQUEST_TOP_P]: modelConfig.topP,
955
- [axSpanAttributes.LLM_REQUEST_TOP_K]: modelConfig.topK,
956
- [axSpanAttributes.LLM_REQUEST_FREQUENCY_PENALTY]: modelConfig.frequencyPenalty,
957
- [axSpanAttributes.LLM_REQUEST_PRESENCE_PENALTY]: modelConfig.presencePenalty,
958
- [axSpanAttributes.LLM_REQUEST_STOP_SEQUENCES]: modelConfig.stopSequences?.join(", "),
959
- [axSpanAttributes.LLM_REQUEST_LLM_IS_STREAMING]: modelConfig.stream
1004
+ [axSpanAttributes.LLM_REQUEST_TOP_P]: modelConfig.topP ?? "Not set",
1005
+ [axSpanAttributes.LLM_REQUEST_TOP_K]: modelConfig.topK ?? "Not set",
1006
+ [axSpanAttributes.LLM_REQUEST_FREQUENCY_PENALTY]: modelConfig.frequencyPenalty ?? "Not set",
1007
+ [axSpanAttributes.LLM_REQUEST_PRESENCE_PENALTY]: modelConfig.presencePenalty ?? "Not set",
1008
+ [axSpanAttributes.LLM_REQUEST_STOP_SEQUENCES]: modelConfig.stopSequences?.join(", ") ?? "Not set",
1009
+ [axSpanAttributes.LLM_REQUEST_LLM_IS_STREAMING]: modelConfig.stream ?? "Not set"
960
1010
  }
961
1011
  },
962
1012
  options?.traceContext ?? context.active(),
@@ -1019,7 +1069,8 @@ var AxBaseAI = class {
1019
1069
  timeout: this.timeout,
1020
1070
  debug,
1021
1071
  fetch: this.fetch,
1022
- span
1072
+ span,
1073
+ abortSignal: options?.abortSignal ?? this.abortSignal
1023
1074
  },
1024
1075
  reqValue
1025
1076
  );
@@ -1176,7 +1227,8 @@ var AxBaseAI = class {
1176
1227
  debug,
1177
1228
  fetch: this.fetch,
1178
1229
  timeout: this.timeout,
1179
- span
1230
+ span,
1231
+ abortSignal: options?.abortSignal ?? this.abortSignal
1180
1232
  },
1181
1233
  reqValue
1182
1234
  );
@@ -2055,7 +2107,7 @@ var AxAIOpenAIImpl = class {
2055
2107
  response_format: this.config?.responseFormat ? { type: this.config.responseFormat } : void 0,
2056
2108
  tools,
2057
2109
  tool_choice: toolsChoice,
2058
- max_completion_tokens: req.modelConfig?.maxTokens ?? this.config.maxTokens ?? 500,
2110
+ max_completion_tokens: req.modelConfig?.maxTokens ?? this.config.maxTokens,
2059
2111
  temperature: req.modelConfig?.temperature ?? this.config.temperature,
2060
2112
  top_p: req.modelConfig?.topP ?? this.config.topP ?? 1,
2061
2113
  n: req.modelConfig?.n ?? this.config.n,
@@ -4611,7 +4663,6 @@ var axAIOpenAIResponsesDefaultConfig = () => ({
4611
4663
  model: "gpt-4o" /* GPT4O */,
4612
4664
  embedModel: "text-embedding-ada-002" /* TextEmbeddingAda002 */,
4613
4665
  temperature: 0.7,
4614
- maxTokens: 2048,
4615
4666
  topP: 1,
4616
4667
  stream: true
4617
4668
  // reasoningEffort: 'medium',
@@ -4778,7 +4829,7 @@ var AxAIRekaImpl = class {
4778
4829
  const reqValue = {
4779
4830
  model,
4780
4831
  messages,
4781
- max_tokens: req.modelConfig?.maxTokens ?? this.config.maxTokens ?? 500,
4832
+ max_tokens: req.modelConfig?.maxTokens ?? this.config.maxTokens,
4782
4833
  temperature: req.modelConfig?.temperature ?? this.config.temperature,
4783
4834
  top_k: req.modelConfig?.n ?? this.config.n,
4784
4835
  top_p: req.modelConfig?.topP ?? this.config.topP ?? 1,
@@ -5178,7 +5229,7 @@ var AxAIGrok = class extends AxAIOpenAIBase {
5178
5229
  };
5179
5230
 
5180
5231
  // dsp/generate.ts
5181
- import { ReadableStream as ReadableStream3 } from "node:stream/web";
5232
+ import { ReadableStream as ReadableStream3 } from "stream/web";
5182
5233
  import {
5183
5234
  context as context2,
5184
5235
  SpanKind as SpanKind2,
@@ -5473,6 +5524,8 @@ var validateValue = (field, value) => {
5473
5524
  const ft = field.type ?? { name: "string", isArray: false };
5474
5525
  const validateSingleValue = (expectedType, val) => {
5475
5526
  switch (expectedType) {
5527
+ case "class":
5528
+ return typeof val === "string";
5476
5529
  case "code":
5477
5530
  return typeof val === "string";
5478
5531
  case "string":
@@ -5511,7 +5564,7 @@ var validateValue = (field, value) => {
5511
5564
  }
5512
5565
  if (msg) {
5513
5566
  throw new Error(
5514
- `Validation failed: Expected '${field.name}' to be a ${msg} instead got '${value}'`
5567
+ `Validation failed: Expected '${field.name}' to be type '${msg}' instead got '${value}'`
5515
5568
  );
5516
5569
  }
5517
5570
  return;
@@ -5536,7 +5589,7 @@ var validateValue = (field, value) => {
5536
5589
  }
5537
5590
  if (msg) {
5538
5591
  throw new Error(
5539
- `Validation failed: Expected '${field.name}' to be a ${msg} instead got '${value}'`
5592
+ `Validation failed: Expected '${field.name}' to be type '${msg}' instead got '${value}'`
5540
5593
  );
5541
5594
  }
5542
5595
  return;
@@ -5557,8 +5610,9 @@ var validateValue = (field, value) => {
5557
5610
  isValid = validateSingleValue(ft.name, value);
5558
5611
  }
5559
5612
  if (!isValid) {
5613
+ const gotType = Array.isArray(value) ? "array" : typeof value;
5560
5614
  throw new Error(
5561
- `Validation failed: Expected '${field.name}' to be a ${field.type?.isArray ? "an array of " : ""}${ft.name} instead got '${typeof value}' (${value})`
5615
+ `Validation failed: Expected '${field.name}' to be a ${field.type?.isArray ? "an array of " : ""}${ft.name} instead got '${gotType}' (${JSON.stringify(value)})`
5562
5616
  );
5563
5617
  }
5564
5618
  };
@@ -5763,11 +5817,15 @@ var AxPromptTemplate = class {
5763
5817
  task;
5764
5818
  thoughtFieldName;
5765
5819
  functions;
5820
+ strictExamples;
5821
+ optionalOutputFields;
5766
5822
  constructor(sig, options, fieldTemplates) {
5767
5823
  this.sig = sig;
5768
5824
  this.fieldTemplates = fieldTemplates;
5769
5825
  this.thoughtFieldName = options?.thoughtFieldName ?? "thought";
5770
5826
  this.functions = options?.functions;
5827
+ this.strictExamples = options?.strictExamples ?? false;
5828
+ this.optionalOutputFields = options?.optionalOutputFields ?? [];
5771
5829
  const task = [];
5772
5830
  const inArgs = renderDescFields(this.sig.getInputFields());
5773
5831
  const outArgs = renderDescFields(this.sig.getOutputFields());
@@ -5937,13 +5995,32 @@ ${outputFields}`);
5937
5995
  };
5938
5996
  renderExamples = (data) => {
5939
5997
  const list = [];
5998
+ const inputExampleContext = {
5999
+ isExample: true,
6000
+ strictExamples: this.strictExamples,
6001
+ optionalOutputFields: this.optionalOutputFields,
6002
+ isInputField: true
6003
+ };
6004
+ const outputExampleContext = {
6005
+ isExample: true,
6006
+ strictExamples: this.strictExamples,
6007
+ optionalOutputFields: this.optionalOutputFields,
6008
+ isInputField: false
6009
+ };
5940
6010
  for (const [index, item] of data.entries()) {
5941
- const renderedInputItem = this.sig.getInputFields().map((field) => this.renderInField(field, item)).filter((v) => v !== void 0).flat();
5942
- const renderedOutputItem = this.sig.getOutputFields().map((field) => this.renderInField(field, item)).filter((v) => v !== void 0).flat();
6011
+ const renderedInputItem = this.sig.getInputFields().map((field) => this.renderInField(field, item, inputExampleContext)).filter((v) => v !== void 0).flat();
6012
+ const outputFields = this.sig.getOutputFields();
6013
+ const renderedOutputItem = outputFields.map((field) => this.renderInField(field, item, outputExampleContext)).filter((v) => v !== void 0).flat();
5943
6014
  if (renderedOutputItem.length === 0) {
5944
- throw new Error(
5945
- `Output fields are required in examples: index: ${index}, data: ${JSON.stringify(item)}`
6015
+ const missingFields = outputFields.filter((field) => !item[field.name]);
6016
+ const allMissingFieldsAreOptional = missingFields.every(
6017
+ (field) => this.optionalOutputFields.includes(field.name)
5946
6018
  );
6019
+ if (!allMissingFieldsAreOptional) {
6020
+ throw new Error(
6021
+ `Output fields are required in examples: index: ${index}, data: ${JSON.stringify(item)}`
6022
+ );
6023
+ }
5947
6024
  }
5948
6025
  const renderedItem = [...renderedInputItem, ...renderedOutputItem];
5949
6026
  if (index > 0 && renderedItem.length > 0 && renderedItem[0]?.type === "text") {
@@ -5963,9 +6040,26 @@ ${outputFields}`);
5963
6040
  };
5964
6041
  renderDemos = (data) => {
5965
6042
  const list = [];
5966
- const fields = [...this.sig.getInputFields(), ...this.sig.getOutputFields()];
6043
+ const inputFields = this.sig.getInputFields();
6044
+ const outputFields = this.sig.getOutputFields();
5967
6045
  for (const item of data) {
5968
- const renderedItem = fields.map((field) => this.renderInField(field, item)).filter((v) => v !== void 0).flat();
6046
+ const inputRenderedItems = inputFields.map(
6047
+ (field) => this.renderInField(field, item, {
6048
+ isExample: true,
6049
+ strictExamples: this.strictExamples,
6050
+ optionalOutputFields: this.optionalOutputFields,
6051
+ isInputField: true
6052
+ })
6053
+ ).filter((v) => v !== void 0).flat();
6054
+ const outputRenderedItems = outputFields.map(
6055
+ (field) => this.renderInField(field, item, {
6056
+ isExample: true,
6057
+ strictExamples: this.strictExamples,
6058
+ optionalOutputFields: this.optionalOutputFields,
6059
+ isInputField: false
6060
+ })
6061
+ ).filter((v) => v !== void 0).flat();
6062
+ const renderedItem = [...inputRenderedItems, ...outputRenderedItems];
5969
6063
  renderedItem.slice(0, -1).forEach((v) => {
5970
6064
  if ("text" in v) {
5971
6065
  v.text = v.text + "\n";
@@ -5979,15 +6073,15 @@ ${outputFields}`);
5979
6073
  return list;
5980
6074
  };
5981
6075
  renderInputFields = (values) => {
5982
- const renderedItems = this.sig.getInputFields().map((field) => this.renderInField(field, values)).filter((v) => v !== void 0).flat();
6076
+ const renderedItems = this.sig.getInputFields().map((field) => this.renderInField(field, values, void 0)).filter((v) => v !== void 0).flat();
5983
6077
  renderedItems.filter((v) => v.type === "text").forEach((v) => {
5984
6078
  v.text = v.text + "\n";
5985
6079
  });
5986
6080
  return renderedItems;
5987
6081
  };
5988
- renderInField = (field, values) => {
6082
+ renderInField = (field, values, context3) => {
5989
6083
  const value = values[field.name];
5990
- if (isEmptyValue(field, value)) {
6084
+ if (isEmptyValue(field, value, context3)) {
5991
6085
  return;
5992
6086
  }
5993
6087
  if (field.type) {
@@ -6171,15 +6265,34 @@ function combineConsecutiveStrings(separator) {
6171
6265
  return acc;
6172
6266
  };
6173
6267
  }
6174
- var isEmptyValue = (field, value) => {
6268
+ var isEmptyValue = (field, value, context3) => {
6175
6269
  if (typeof value === "boolean") {
6176
6270
  return false;
6177
6271
  }
6178
6272
  if (!value || (Array.isArray(value) || typeof value === "string") && value.length === 0) {
6179
- if (field.isOptional || field.isInternal) {
6273
+ if (context3?.isExample) {
6274
+ const isInputField = context3?.isInputField ?? true;
6275
+ if (isInputField) {
6276
+ if (!context3?.strictExamples) {
6277
+ return true;
6278
+ } else {
6279
+ if (field.isOptional || field.isInternal) {
6280
+ return true;
6281
+ }
6282
+ throw new Error(`Value for input field '${field.name}' is required.`);
6283
+ }
6284
+ } else {
6285
+ if (field.isOptional || field.isInternal || context3?.optionalOutputFields?.includes(field.name)) {
6286
+ return true;
6287
+ }
6288
+ throw new Error(`Value for output field '${field.name}' is required.`);
6289
+ }
6290
+ }
6291
+ if (field.isOptional || field.isInternal || context3?.optionalOutputFields?.includes(field.name)) {
6180
6292
  return true;
6181
6293
  }
6182
- throw new Error(`Value for input field '${field.name}' is required.`);
6294
+ const fieldType = context3?.isInputField !== false ? "input" : "output";
6295
+ throw new Error(`Value for ${fieldType} field '${field.name}' is required.`);
6183
6296
  }
6184
6297
  return false;
6185
6298
  };
@@ -7439,6 +7552,7 @@ var AxProgramWithSignature = class {
7439
7552
  signature;
7440
7553
  sigHash;
7441
7554
  examples;
7555
+ examplesOptions;
7442
7556
  demos;
7443
7557
  trace;
7444
7558
  usage = [];
@@ -7480,16 +7594,16 @@ var AxProgramWithSignature = class {
7480
7594
  this.key.id = [parentId, this.key.id].join("/");
7481
7595
  }
7482
7596
  }
7483
- setExamples(examples) {
7484
- this._setExamples(examples);
7597
+ setExamples(examples, options) {
7598
+ this._setExamples(examples, options);
7485
7599
  if (!("programId" in examples)) {
7486
7600
  return;
7487
7601
  }
7488
7602
  for (const child of this.children) {
7489
- child.setExamples(examples);
7603
+ child.setExamples(examples, options);
7490
7604
  }
7491
7605
  }
7492
- _setExamples(examples) {
7606
+ _setExamples(examples, options) {
7493
7607
  let traces = [];
7494
7608
  if ("programId" in examples && examples.programId === this.key.id) {
7495
7609
  traces = examples.traces;
@@ -7498,6 +7612,7 @@ var AxProgramWithSignature = class {
7498
7612
  traces = examples;
7499
7613
  }
7500
7614
  if (traces) {
7615
+ this.examplesOptions = options;
7501
7616
  const sig = this.signature;
7502
7617
  const fields = [...sig.getInputFields(), ...sig.getOutputFields()];
7503
7618
  this.examples = traces.map((e) => {
@@ -7578,12 +7693,12 @@ var AxProgram = class {
7578
7693
  this.key.id = [parentId, this.key.id].join("/");
7579
7694
  }
7580
7695
  }
7581
- setExamples(examples) {
7696
+ setExamples(examples, options) {
7582
7697
  if (!("programId" in examples)) {
7583
7698
  return;
7584
7699
  }
7585
7700
  for (const child of this.children) {
7586
- child.setExamples(examples);
7701
+ child.setExamples(examples, options);
7587
7702
  }
7588
7703
  }
7589
7704
  getTraces() {
@@ -7637,7 +7752,9 @@ var AxGen = class extends AxProgramWithSignature {
7637
7752
  this.thoughtFieldName = options?.thoughtFieldName ?? "thought";
7638
7753
  const promptTemplateOptions = {
7639
7754
  functions: options?.functions,
7640
- thoughtFieldName: this.thoughtFieldName
7755
+ thoughtFieldName: this.thoughtFieldName,
7756
+ strictExamples: options?.strictExamples,
7757
+ optionalOutputFields: options?.optionalOutputFields
7641
7758
  };
7642
7759
  this.promptTemplate = new (options?.promptTemplate ?? AxPromptTemplate)(
7643
7760
  this.signature,
@@ -7723,7 +7840,8 @@ var AxGen = class extends AxProgramWithSignature {
7723
7840
  stream,
7724
7841
  debug: false,
7725
7842
  thinkingTokenBudget,
7726
- traceContext
7843
+ traceContext,
7844
+ abortSignal: options?.abortSignal
7727
7845
  }
7728
7846
  );
7729
7847
  return res;
@@ -8000,7 +8118,9 @@ Content: ${result.content}`
8000
8118
  const promptTemplateClass = this.options?.promptTemplate ?? AxPromptTemplate;
8001
8119
  const currentPromptTemplateOptions = {
8002
8120
  functions: options.functions,
8003
- thoughtFieldName: this.thoughtFieldName
8121
+ thoughtFieldName: this.thoughtFieldName,
8122
+ strictExamples: this.options?.strictExamples,
8123
+ optionalOutputFields: this.options?.optionalOutputFields
8004
8124
  };
8005
8125
  this.promptTemplate = new promptTemplateClass(
8006
8126
  this.signature,
@@ -8186,6 +8306,22 @@ Content: ${result.content}`
8186
8306
  stream: true
8187
8307
  });
8188
8308
  }
8309
+ setExamples(examples, options) {
8310
+ super.setExamples(examples, options);
8311
+ if (options?.optionalOutputFields) {
8312
+ const promptTemplateClass = this.options?.promptTemplate ?? AxPromptTemplate;
8313
+ const currentPromptTemplateOptions = {
8314
+ functions: this.functions,
8315
+ thoughtFieldName: this.thoughtFieldName,
8316
+ strictExamples: this.options?.strictExamples,
8317
+ optionalOutputFields: options.optionalOutputFields
8318
+ };
8319
+ this.promptTemplate = new promptTemplateClass(
8320
+ this.signature,
8321
+ currentPromptTemplateOptions
8322
+ );
8323
+ }
8324
+ }
8189
8325
  };
8190
8326
  var AxGenerateError = class extends Error {
8191
8327
  details;
@@ -8316,8 +8452,8 @@ var AxAgent = class {
8316
8452
  this.func.parameters = addModelParameter(this.func.parameters, mm);
8317
8453
  }
8318
8454
  }
8319
- setExamples(examples) {
8320
- this.program.setExamples(examples);
8455
+ setExamples(examples, options) {
8456
+ this.program.setExamples(examples, options);
8321
8457
  }
8322
8458
  setId(id) {
8323
8459
  this.program.setId(id);
@@ -8519,7 +8655,7 @@ function pick(obj, keys) {
8519
8655
  }
8520
8656
 
8521
8657
  // docs/tika.ts
8522
- import { createReadStream } from "node:fs";
8658
+ import { createReadStream } from "fs";
8523
8659
  var AxApacheTika = class {
8524
8660
  tikaUrl;
8525
8661
  fetch;
@@ -9611,7 +9747,12 @@ var AxDBManager = class {
9611
9747
  const bs = options?.batchSize ?? 10;
9612
9748
  for (let i = 0; i < chunks.length; i += bs) {
9613
9749
  const batch = chunks.slice(i, i + bs);
9614
- const ret = await this.ai.embed({ texts: batch });
9750
+ const ret = await this.ai.embed(
9751
+ { texts: batch },
9752
+ {
9753
+ abortSignal: options?.abortSignal
9754
+ }
9755
+ );
9615
9756
  const embeddings = ret.embeddings.map((embedding, index) => ({
9616
9757
  id: `chunk_${Date.now() + index}`,
9617
9758
  // Unique ID for each chunk, adjusted by index
@@ -9627,7 +9768,10 @@ var AxDBManager = class {
9627
9768
  throw new Error(`Error processing text: ${error}`);
9628
9769
  }
9629
9770
  };
9630
- query = async (query, { topPercent } = {}) => {
9771
+ query = async (query, {
9772
+ topPercent,
9773
+ abortSignal
9774
+ } = {}) => {
9631
9775
  const texts = Array.isArray(query) ? query : [query];
9632
9776
  if (typeof texts[0] === "string" && this.rewriter) {
9633
9777
  for (const [i, text] of texts.entries()) {
@@ -9639,7 +9783,12 @@ var AxDBManager = class {
9639
9783
  }
9640
9784
  let queries;
9641
9785
  if (typeof texts[0] === "string") {
9642
- const embedResults = await this.ai.embed({ texts });
9786
+ const embedResults = await this.ai.embed(
9787
+ { texts },
9788
+ {
9789
+ abortSignal
9790
+ }
9791
+ );
9643
9792
  queries = embedResults.embeddings.map(
9644
9793
  (values) => this.db.query({ table, values })
9645
9794
  );
@@ -10057,13 +10206,13 @@ var AxHFDataLoader = class {
10057
10206
  };
10058
10207
 
10059
10208
  // funcs/code.ts
10060
- import * as _crypto from "node:crypto";
10061
- import * as _fs from "node:fs";
10062
- import * as _http from "node:http";
10063
- import * as _https from "node:https";
10064
- import * as _os from "node:os";
10065
- import * as _process from "node:process";
10066
- import { runInNewContext } from "node:vm";
10209
+ import * as _crypto from "crypto";
10210
+ import * as _fs from "fs";
10211
+ import * as _http from "http";
10212
+ import * as _https from "https";
10213
+ import * as _os from "os";
10214
+ import * as _process from "process";
10215
+ import { runInNewContext } from "vm";
10067
10216
  var AxJSInterpreterPermission = /* @__PURE__ */ ((AxJSInterpreterPermission2) => {
10068
10217
  AxJSInterpreterPermission2["FS"] = "node:fs";
10069
10218
  AxJSInterpreterPermission2["NET"] = "net";
@@ -11234,9 +11383,14 @@ var AxSimpleClassifier = class {
11234
11383
  setState(state) {
11235
11384
  this.db.setDB(state);
11236
11385
  }
11237
- setClasses = async (classes) => {
11386
+ setClasses = async (classes, options) => {
11238
11387
  for (const c of classes) {
11239
- const ret = await this.ai.embed({ texts: c.getContext() });
11388
+ const ret = await this.ai.embed(
11389
+ { texts: c.getContext() },
11390
+ {
11391
+ abortSignal: options?.abortSignal
11392
+ }
11393
+ );
11240
11394
  await this.db.upsert({
11241
11395
  id: c.getName(),
11242
11396
  table: "classes",
@@ -11245,7 +11399,12 @@ var AxSimpleClassifier = class {
11245
11399
  }
11246
11400
  };
11247
11401
  async forward(text, options) {
11248
- const { embeddings } = await this.ai.embed({ texts: [text] });
11402
+ const { embeddings } = await this.ai.embed(
11403
+ { texts: [text] },
11404
+ {
11405
+ abortSignal: options?.abortSignal
11406
+ }
11407
+ );
11249
11408
  const matches = await this.db.query({
11250
11409
  table: "classes",
11251
11410
  values: embeddings[0]
@@ -11320,6 +11479,79 @@ var AxTestPrompt = class {
11320
11479
  }
11321
11480
  };
11322
11481
 
11482
+ // ai/abortable.ts
11483
+ var AxAbortableAI = class {
11484
+ abortController;
11485
+ ai;
11486
+ constructor(ai) {
11487
+ this.ai = ai;
11488
+ this.abortController = new AbortController();
11489
+ }
11490
+ /**
11491
+ * Get the current abort signal
11492
+ */
11493
+ get signal() {
11494
+ return this.abortController.signal;
11495
+ }
11496
+ /**
11497
+ * Check if the request has been aborted
11498
+ */
11499
+ get aborted() {
11500
+ return this.abortController.signal.aborted;
11501
+ }
11502
+ /**
11503
+ * Abort the ongoing request
11504
+ * @param reason Optional reason for the abort
11505
+ */
11506
+ abort(reason) {
11507
+ this.abortController.abort(reason);
11508
+ }
11509
+ /**
11510
+ * Reset the abort controller to allow new requests
11511
+ * This creates a new AbortController, allowing fresh requests
11512
+ */
11513
+ reset() {
11514
+ this.abortController = new AbortController();
11515
+ }
11516
+ /**
11517
+ * Send a chat request with abort support
11518
+ */
11519
+ async chat(req, options) {
11520
+ return this.ai.chat(req, {
11521
+ ...options,
11522
+ abortSignal: this.abortController.signal
11523
+ });
11524
+ }
11525
+ /**
11526
+ * Send an embed request with abort support
11527
+ */
11528
+ async embed(req, options) {
11529
+ return this.ai.embed(req, {
11530
+ ...options,
11531
+ abortSignal: this.abortController.signal
11532
+ });
11533
+ }
11534
+ /**
11535
+ * Create a timeout-based abort after specified milliseconds
11536
+ * @param timeoutMs Timeout in milliseconds
11537
+ * @param reason Optional reason for the timeout abort
11538
+ * @returns Timeout ID that can be cleared
11539
+ */
11540
+ abortAfter(timeoutMs, reason = "Request timeout") {
11541
+ return setTimeout(() => {
11542
+ this.abort(reason);
11543
+ }, timeoutMs);
11544
+ }
11545
+ /**
11546
+ * Add an event listener for abort events
11547
+ */
11548
+ onAbort(callback) {
11549
+ this.abortController.signal.addEventListener("abort", () => {
11550
+ callback(this.abortController.signal.reason);
11551
+ });
11552
+ }
11553
+ };
11554
+
11323
11555
  // prompts/cot.ts
11324
11556
  var AxChainOfThought = class extends AxGen {
11325
11557
  constructor(signature, options) {
@@ -11446,7 +11678,10 @@ var AxEmbeddingAdapter = class {
11446
11678
  async embedAdapter(text, extra) {
11447
11679
  const embedRes = await this.aiService.embed(
11448
11680
  { texts: [text] },
11449
- { sessionId: extra?.sessionId }
11681
+ {
11682
+ sessionId: extra?.sessionId,
11683
+ abortSignal: extra?.abortSignal
11684
+ }
11450
11685
  );
11451
11686
  const embeds = embedRes.embeddings.at(0);
11452
11687
  if (!embeds) {
@@ -12915,8 +13150,8 @@ ${JSON.stringify(res, null, 2)}`
12915
13150
  };
12916
13151
 
12917
13152
  // mcp/stdioTransport.ts
12918
- import { spawn } from "node:child_process";
12919
- import readline from "node:readline";
13153
+ import { spawn } from "child_process";
13154
+ import readline from "readline";
12920
13155
  var AxMCPStdioTransport = class {
12921
13156
  process;
12922
13157
  rl;
@@ -13209,6 +13444,7 @@ export {
13209
13444
  AxAIOpenAIResponsesImpl,
13210
13445
  AxAIReka,
13211
13446
  AxAIRekaModel,
13447
+ AxAIServiceAbortedError,
13212
13448
  AxAIServiceAuthenticationError,
13213
13449
  AxAIServiceError,
13214
13450
  AxAIServiceNetworkError,
@@ -13217,6 +13453,7 @@ export {
13217
13453
  AxAIServiceStreamTerminatedError,
13218
13454
  AxAIServiceTimeoutError,
13219
13455
  AxAITogether,
13456
+ AxAbortableAI,
13220
13457
  AxAgent,
13221
13458
  AxApacheTika,
13222
13459
  AxAssertionError,