@ax-llm/ax 11.0.18 → 11.0.19

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
@@ -3985,7 +3985,10 @@ var SignatureParser = class {
3985
3985
  this.skipWhitespace();
3986
3986
  const optionalDesc = this.parseParsedString();
3987
3987
  this.skipWhitespace();
3988
- const inputs = this.parseFieldList(this.parseField.bind(this), "input");
3988
+ const inputs = this.parseFieldList(
3989
+ this.parseInputField.bind(this),
3990
+ "input"
3991
+ );
3989
3992
  this.skipWhitespace();
3990
3993
  if (this.position >= this.input.length) {
3991
3994
  throw new Error(
@@ -3999,7 +4002,10 @@ var SignatureParser = class {
3999
4002
  'Incomplete signature: No output fields specified after "->"'
4000
4003
  );
4001
4004
  }
4002
- const outputs = this.parseFieldList(this.parseField.bind(this), "output");
4005
+ const outputs = this.parseFieldList(
4006
+ this.parseOutputField.bind(this),
4007
+ "output"
4008
+ );
4003
4009
  return {
4004
4010
  desc: optionalDesc?.trim(),
4005
4011
  inputs,
@@ -4061,11 +4067,75 @@ ${pointer}`;
4061
4067
  }
4062
4068
  return fields;
4063
4069
  }
4064
- parseField() {
4070
+ // -------------------------------
4071
+ // Parse input fields (no "class" type and no internal flag)
4072
+ // -------------------------------
4073
+ parseInputField() {
4065
4074
  this.skipWhitespace();
4066
4075
  const name = this.parseParsedIdentifier();
4067
4076
  this.currentFieldName = name;
4068
- const isOptional = this.match("?");
4077
+ let isOptional = void 0;
4078
+ while (true) {
4079
+ if (this.match("?")) {
4080
+ isOptional = true;
4081
+ continue;
4082
+ }
4083
+ if (this.match("!")) {
4084
+ throw new Error(
4085
+ `Input field "${name}" does not support the internal marker "!"`
4086
+ );
4087
+ }
4088
+ break;
4089
+ }
4090
+ let type;
4091
+ this.skipWhitespace();
4092
+ if (this.match(":")) {
4093
+ this.skipWhitespace();
4094
+ if (/^class\b/.test(this.input.slice(this.position))) {
4095
+ throw new Error(
4096
+ `Input field "${name}" does not support the "class" type`
4097
+ );
4098
+ } else {
4099
+ try {
4100
+ const typeName = this.parseTypeNotClass();
4101
+ const isArray = this.match("[]");
4102
+ type = { name: typeName, isArray };
4103
+ } catch (error) {
4104
+ throw new Error(
4105
+ `Input field "${name}": ${error instanceof Error ? error.message : "Unknown error"}`
4106
+ );
4107
+ }
4108
+ }
4109
+ }
4110
+ this.skipWhitespace();
4111
+ const desc = this.parseParsedString();
4112
+ return {
4113
+ name,
4114
+ desc: desc?.trim(),
4115
+ type,
4116
+ isOptional
4117
+ };
4118
+ }
4119
+ // -------------------------------
4120
+ // Parse output fields (supports both "class" type and the internal marker)
4121
+ // -------------------------------
4122
+ parseOutputField() {
4123
+ this.skipWhitespace();
4124
+ const name = this.parseParsedIdentifier();
4125
+ this.currentFieldName = name;
4126
+ let isOptional = false;
4127
+ let isInternal = false;
4128
+ while (true) {
4129
+ if (this.match("?")) {
4130
+ isOptional = true;
4131
+ continue;
4132
+ }
4133
+ if (this.match("!")) {
4134
+ isInternal = true;
4135
+ continue;
4136
+ }
4137
+ break;
4138
+ }
4069
4139
  let type;
4070
4140
  this.skipWhitespace();
4071
4141
  if (this.match(":")) {
@@ -4073,16 +4143,16 @@ ${pointer}`;
4073
4143
  if (this.match("class")) {
4074
4144
  const isArray = this.match("[]");
4075
4145
  this.skipWhitespace();
4076
- const desc2 = this.parseParsedString();
4077
- if (!desc2) {
4146
+ const classNamesString = this.parseParsedString();
4147
+ if (!classNamesString) {
4078
4148
  throw new Error(
4079
- `Field "${name}": Expected class names in quotes after "class" type. Example: class "MyClass1, MyClass2"`
4149
+ `Output field "${name}": Expected class names in quotes after "class" type. Example: class "MyClass1, MyClass2"`
4080
4150
  );
4081
4151
  }
4082
- const classes = desc2.split(/[,\s]+/).map((s) => s.trim()).filter((s) => s.length > 0);
4152
+ const classes = classNamesString.split(/[,\s]+/).map((s) => s.trim()).filter((s) => s.length > 0);
4083
4153
  if (classes.length === 0) {
4084
4154
  throw new Error(
4085
- `Field "${name}": Empty class list provided. At least one class name is required`
4155
+ `Output field "${name}": Empty class list provided. At least one class name is required`
4086
4156
  );
4087
4157
  }
4088
4158
  type = { name: "class", isArray, classes };
@@ -4093,28 +4163,20 @@ ${pointer}`;
4093
4163
  type = { name: typeName, isArray };
4094
4164
  } catch (error) {
4095
4165
  throw new Error(
4096
- `Field "${name}": ${error instanceof Error ? error.message : "Unknown error"}`
4166
+ `Output field "${name}": ${error instanceof Error ? error.message : "Unknown error"}`
4097
4167
  );
4098
4168
  }
4099
4169
  }
4100
4170
  }
4101
4171
  this.skipWhitespace();
4102
4172
  const desc = this.parseParsedString();
4103
- if (type?.name === "class") {
4104
- return {
4105
- name,
4106
- desc: desc?.trim(),
4107
- type,
4108
- isOptional
4109
- };
4110
- } else {
4111
- return {
4112
- name,
4113
- desc: desc?.trim(),
4114
- type,
4115
- isOptional
4116
- };
4117
- }
4173
+ return {
4174
+ name,
4175
+ desc: desc?.trim(),
4176
+ type,
4177
+ isOptional,
4178
+ isInternal
4179
+ };
4118
4180
  }
4119
4181
  parseTypeNotClass() {
4120
4182
  const types = [
@@ -4268,8 +4330,9 @@ var AxSignature = class _AxSignature {
4268
4330
  name: field.name,
4269
4331
  title,
4270
4332
  description: "desc" in field ? field.desc : void 0,
4271
- isOptional: field.isOptional,
4272
- type: field.type ?? { name: "string", isArray: false }
4333
+ type: field.type ?? { name: "string", isArray: false },
4334
+ ..."isInternal" in field ? { isInternal: field.isInternal } : {},
4335
+ ..."isOptional" in field ? { isOptional: field.isOptional } : {}
4273
4336
  };
4274
4337
  };
4275
4338
  parseField = (field) => {
@@ -4303,7 +4366,7 @@ var AxSignature = class _AxSignature {
4303
4366
  getOutputFields = () => this.outputFields;
4304
4367
  getDescription = () => this.description;
4305
4368
  toTitle = (name) => {
4306
- let result = name.replaceAll("_", " ");
4369
+ let result = name.replace(/_/g, " ");
4307
4370
  result = result.replace(/([A-Z]|[0-9]+)/g, " $1").trim();
4308
4371
  return result.charAt(0).toUpperCase() + result.slice(1);
4309
4372
  };
@@ -5305,7 +5368,7 @@ var formatDateWithTimezone = (date) => {
5305
5368
 
5306
5369
  // dsp/extract.ts
5307
5370
  var extractValues = (sig, values, content) => {
5308
- const xstate = { extractedFields: [], s: -1 };
5371
+ const xstate = { extractedFields: [], streamedIndex: {}, s: -1 };
5309
5372
  streamingExtractValues(sig, values, xstate, content);
5310
5373
  streamingExtractFinalValue(sig, values, xstate, content);
5311
5374
  };
@@ -5359,7 +5422,7 @@ var streamingExtractValues = (sig, values, xstate, content, streamingValidation
5359
5422
  if (parsedValue !== void 0) {
5360
5423
  values[xstate.currField.name] = parsedValue;
5361
5424
  }
5362
- xstate.lastDelta = val;
5425
+ xstate.prevField = { field: xstate.currField, s: xstate.s, e };
5363
5426
  }
5364
5427
  checkMissingRequiredFields(xstate, values, index);
5365
5428
  xstate.s = e + prefixLen;
@@ -5368,6 +5431,9 @@ var streamingExtractValues = (sig, values, xstate, content, streamingValidation
5368
5431
  if (!xstate.extractedFields.includes(field)) {
5369
5432
  xstate.extractedFields.push(field);
5370
5433
  }
5434
+ if (xstate.streamedIndex[field.name] === void 0) {
5435
+ xstate.streamedIndex[field.name] = 0;
5436
+ }
5371
5437
  }
5372
5438
  };
5373
5439
  var streamingExtractFinalValue = (sig, values, xstate, content) => {
@@ -5423,25 +5489,18 @@ var convertValueToType = (field, val) => {
5423
5489
  return val;
5424
5490
  }
5425
5491
  };
5426
- function processStreamingDelta(content, xstate) {
5427
- if (!xstate.currField) {
5428
- return null;
5429
- }
5430
- const { name: fieldName, type: fieldType } = xstate.currField ?? {};
5431
- const { isArray: fieldIsArray, name: fieldTypeName } = fieldType ?? {};
5432
- if (!xstate.streamedIndex) {
5433
- xstate.streamedIndex = { [fieldName]: 0 };
5434
- }
5435
- if (fieldIsArray) {
5436
- return null;
5437
- }
5438
- if (fieldTypeName !== "string" && fieldTypeName !== "code") {
5439
- return null;
5492
+ function* yieldDelta(content, field, s, e, xstate) {
5493
+ const { name: fieldName, isInternal } = field;
5494
+ const { isArray: fieldIsArray, name: fieldTypeName } = field.type ?? {};
5495
+ if (isInternal || fieldIsArray || fieldTypeName && fieldTypeName !== "string" && fieldTypeName !== "code") {
5496
+ return;
5440
5497
  }
5441
5498
  const pos = xstate.streamedIndex[fieldName] ?? 0;
5442
- const s = xstate.s + pos;
5443
5499
  const isFirstChunk = pos === 0;
5444
- const d1 = content.substring(s);
5500
+ const d1 = content.substring(s + pos, e);
5501
+ if (d1.length === 0) {
5502
+ return;
5503
+ }
5445
5504
  let d2 = d1.replace(/\s+$/, "");
5446
5505
  if (xstate.currField?.type?.name === "code") {
5447
5506
  d2 = d2.replace(/\s*```\s*$/, "");
@@ -5451,41 +5510,45 @@ function processStreamingDelta(content, xstate) {
5451
5510
  d3 = d3.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/, "");
5452
5511
  }
5453
5512
  if (d3.length > 0) {
5513
+ yield { [fieldName]: d3 };
5454
5514
  xstate.streamedIndex[fieldName] = pos + d2.length;
5455
5515
  }
5456
- return d3;
5457
5516
  }
5458
- function getStreamingDelta(content, xstate) {
5459
- if (xstate.lastDelta) {
5460
- processStreamingDelta(xstate.lastDelta, xstate);
5461
- xstate.lastDelta = void 0;
5517
+ function* streamValues(sig, content, values, xstate) {
5518
+ if (xstate.prevField && !xstate.prevField.field.isInternal) {
5519
+ const { field, s, e } = xstate.prevField;
5520
+ yield* yieldDelta(content, field, s, e, xstate);
5521
+ xstate.prevField = void 0;
5462
5522
  }
5463
- return processStreamingDelta(content, xstate);
5464
- }
5465
- function* streamValues(sig, values, xstate, delta) {
5466
- if (!xstate.currField) {
5467
- return;
5468
- }
5469
- const fieldName = xstate.currField.name;
5470
- if (delta && delta.length > 0) {
5471
- yield { [fieldName]: delta };
5523
+ if (!xstate.currField || xstate.currField.isInternal) {
5472
5524
  return;
5473
5525
  }
5526
+ yield* yieldDelta(
5527
+ content,
5528
+ xstate.currField,
5529
+ xstate.s,
5530
+ content.length,
5531
+ xstate
5532
+ );
5533
+ const outputFields = sig.getOutputFields();
5474
5534
  for (const key of Object.keys(values)) {
5535
+ const field = outputFields.find((f) => f.name === key);
5536
+ if (!field || field.isInternal) {
5537
+ continue;
5538
+ }
5475
5539
  const value = values[key];
5476
5540
  if (Array.isArray(value)) {
5477
5541
  const s = xstate.streamedIndex?.[key] ?? 0;
5478
5542
  const v = value.slice(s);
5479
5543
  if (v && v.length > 0) {
5480
5544
  yield { [key]: v };
5481
- if (xstate.streamedIndex) {
5482
- xstate.streamedIndex[key] = s + 1;
5483
- } else {
5484
- xstate.streamedIndex = { [key]: s + 1 };
5485
- }
5545
+ xstate.streamedIndex[key] = s + 1;
5486
5546
  }
5487
- } else {
5547
+ continue;
5548
+ }
5549
+ if (!xstate.streamedIndex[key]) {
5488
5550
  yield { [key]: value };
5551
+ xstate.streamedIndex[key] = 1;
5489
5552
  }
5490
5553
  }
5491
5554
  }
@@ -5977,6 +6040,7 @@ var AxGen = class extends AxProgramWithSignature {
5977
6040
  const values = {};
5978
6041
  const xstate = {
5979
6042
  extractedFields: [],
6043
+ streamedIndex: {},
5980
6044
  s: -1
5981
6045
  };
5982
6046
  let content = "";
@@ -6012,11 +6076,7 @@ var AxGen = class extends AxProgramWithSignature {
6012
6076
  content,
6013
6077
  streamingValidation
6014
6078
  );
6015
- if (skip) {
6016
- continue;
6017
- }
6018
6079
  assertStreamingAssertions(this.streamingAsserts, xstate, content);
6019
- assertAssertions(this.asserts, values);
6020
6080
  if (this.streamingFieldProcessors.length !== 0) {
6021
6081
  await processStreamingFieldProcessors(
6022
6082
  this.streamingFieldProcessors,
@@ -6027,8 +6087,11 @@ var AxGen = class extends AxProgramWithSignature {
6027
6087
  sessionId
6028
6088
  );
6029
6089
  }
6030
- const delta = getStreamingDelta(content, xstate);
6031
- yield* streamValues(this.signature, values, xstate, delta);
6090
+ yield* streamValues(this.signature, content, values, xstate);
6091
+ if (skip) {
6092
+ continue;
6093
+ }
6094
+ assertAssertions(this.asserts, values);
6032
6095
  }
6033
6096
  if (result.finishReason === "length") {
6034
6097
  throw new Error("Max tokens reached before completion");
@@ -6071,8 +6134,7 @@ var AxGen = class extends AxProgramWithSignature {
6071
6134
  true
6072
6135
  );
6073
6136
  }
6074
- const delta = getStreamingDelta(content, xstate);
6075
- yield* streamValues(this.signature, values, xstate, delta);
6137
+ yield* streamValues(this.signature, content, values, xstate);
6076
6138
  }
6077
6139
  }
6078
6140
  async processResponse({
@@ -6126,6 +6188,12 @@ var AxGen = class extends AxProgramWithSignature {
6126
6188
  throw new Error("Max tokens reached before completion");
6127
6189
  }
6128
6190
  }
6191
+ const publicValues = { ...values };
6192
+ for (const field of this.signature.getOutputFields()) {
6193
+ if (field.isInternal) {
6194
+ delete publicValues[field.name];
6195
+ }
6196
+ }
6129
6197
  return { ...values };
6130
6198
  }
6131
6199
  async *_forward2(ai, values, options, span) {
@@ -8010,6 +8078,125 @@ var AxJSInterpreter = class {
8010
8078
  }
8011
8079
  };
8012
8080
 
8081
+ // ai/mock/api.ts
8082
+ var AxMockAIService = class {
8083
+ constructor(config = {}) {
8084
+ this.config = config;
8085
+ this.config.id = this.config.id ?? crypto.randomUUID();
8086
+ }
8087
+ metrics = {
8088
+ latency: {
8089
+ chat: { mean: 0, p95: 0, p99: 0, samples: [] },
8090
+ embed: { mean: 0, p95: 0, p99: 0, samples: [] }
8091
+ },
8092
+ errors: {
8093
+ chat: { count: 0, rate: 0, total: 0 },
8094
+ embed: { count: 0, rate: 0, total: 0 }
8095
+ }
8096
+ };
8097
+ getName() {
8098
+ return this.config.name ?? "mock-ai-service";
8099
+ }
8100
+ getId() {
8101
+ return this.config.id ?? "mock-ai-service-id";
8102
+ }
8103
+ getModelInfo() {
8104
+ return {
8105
+ name: "mock-model",
8106
+ provider: "mock-provider",
8107
+ promptTokenCostPer1M: 100,
8108
+ completionTokenCostPer1M: 100,
8109
+ ...this.config.modelInfo
8110
+ };
8111
+ }
8112
+ getEmbedModelInfo() {
8113
+ return this.config.embedModelInfo;
8114
+ }
8115
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
8116
+ getFeatures(_model) {
8117
+ return {
8118
+ functions: this.config.features?.functions ?? false,
8119
+ streaming: this.config.features?.streaming ?? false
8120
+ };
8121
+ }
8122
+ getModelList() {
8123
+ return this.config.models;
8124
+ }
8125
+ getMetrics() {
8126
+ return this.metrics;
8127
+ }
8128
+ async chat(req, options) {
8129
+ if (this.config.latencyMs) {
8130
+ await new Promise((resolve) => setTimeout(resolve, this.config.latencyMs));
8131
+ }
8132
+ if (this.config.shouldError) {
8133
+ throw new Error(this.config.errorMessage ?? "Mock chat error");
8134
+ }
8135
+ this.updateMetrics("chat");
8136
+ if (typeof this.config.chatResponse === "function") {
8137
+ return await this.config.chatResponse(req);
8138
+ }
8139
+ return this.config.chatResponse ?? {
8140
+ results: [
8141
+ {
8142
+ content: "Mock response",
8143
+ finishReason: "stop"
8144
+ }
8145
+ ],
8146
+ modelUsage: {
8147
+ promptTokens: 10,
8148
+ completionTokens: 5,
8149
+ totalTokens: 15
8150
+ }
8151
+ };
8152
+ }
8153
+ async embed(req, _options) {
8154
+ if (this.config.latencyMs) {
8155
+ await new Promise((resolve) => setTimeout(resolve, this.config.latencyMs));
8156
+ }
8157
+ if (this.config.shouldError) {
8158
+ throw new Error(this.config.errorMessage ?? "Mock embed error");
8159
+ }
8160
+ this.updateMetrics("embed");
8161
+ if (typeof this.config.embedResponse === "function") {
8162
+ return this.config.embedResponse(req);
8163
+ }
8164
+ return this.config.embedResponse ?? {
8165
+ embeddings: [[0.1, 0.2, 0.3]],
8166
+ modelUsage: {
8167
+ promptTokens: 5,
8168
+ completionTokens: 0,
8169
+ totalTokens: 5
8170
+ }
8171
+ };
8172
+ }
8173
+ setOptions(options) {
8174
+ this.config.options = options;
8175
+ }
8176
+ getOptions() {
8177
+ return this.config.options ?? {};
8178
+ }
8179
+ updateMetrics(type) {
8180
+ const latency = this.config.latencyMs ?? 0;
8181
+ this.metrics.latency[type].samples.push(latency);
8182
+ const samples = this.metrics.latency[type].samples;
8183
+ this.metrics.latency[type].mean = samples.reduce((a, b) => a + b, 0) / samples.length;
8184
+ if (samples.length > 0) {
8185
+ const sortedSamples = [...samples].sort((a, b) => a - b);
8186
+ const p95Index = Math.max(0, Math.floor(sortedSamples.length * 0.95) - 1);
8187
+ this.metrics.latency[type].p95 = sortedSamples[p95Index] ?? latency;
8188
+ const p99Index = Math.max(0, Math.floor(sortedSamples.length * 0.99) - 1);
8189
+ this.metrics.latency[type].p99 = sortedSamples[p99Index] ?? latency;
8190
+ }
8191
+ if (this.config.shouldError) {
8192
+ this.metrics.errors[type].count++;
8193
+ this.metrics.errors[type].total++;
8194
+ const totalRequests = this.metrics.latency[type].samples.length;
8195
+ this.metrics.errors[type].rate = totalRequests > 0 ? this.metrics.errors[type].count / totalRequests : 0;
8196
+ }
8197
+ }
8198
+ };
8199
+
8013
8200
  // dsp/router.ts
8014
8201
  var colorLog6 = new ColorLog();
8015
8202
  var AxRoute = class {
@@ -8279,126 +8466,6 @@ var AxEmbeddingAdapter = class {
8279
8466
  }
8280
8467
  };
8281
8468
 
8282
- // ai/mock/api.ts
8283
- var AxMockAIService = class {
8284
- constructor(config = {}) {
8285
- this.config = config;
8286
- this.config.id = this.config.id ?? crypto.randomUUID();
8287
- }
8288
- options = {};
8289
- metrics = {
8290
- latency: {
8291
- chat: { mean: 0, p95: 0, p99: 0, samples: [] },
8292
- embed: { mean: 0, p95: 0, p99: 0, samples: [] }
8293
- },
8294
- errors: {
8295
- chat: { count: 0, rate: 0, total: 0 },
8296
- embed: { count: 0, rate: 0, total: 0 }
8297
- }
8298
- };
8299
- getName() {
8300
- return this.config.name ?? "mock-ai-service";
8301
- }
8302
- getId() {
8303
- return this.config.id ?? "mock-ai-service-id";
8304
- }
8305
- getModelInfo() {
8306
- return {
8307
- name: "mock-model",
8308
- provider: "mock-provider",
8309
- promptTokenCostPer1M: 100,
8310
- completionTokenCostPer1M: 100,
8311
- ...this.config.modelInfo
8312
- };
8313
- }
8314
- getEmbedModelInfo() {
8315
- return this.config.embedModelInfo;
8316
- }
8317
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8318
- getFeatures(_model) {
8319
- return {
8320
- functions: this.config.features?.functions ?? false,
8321
- streaming: this.config.features?.streaming ?? false
8322
- };
8323
- }
8324
- getModelList() {
8325
- return this.config.models;
8326
- }
8327
- getMetrics() {
8328
- return this.metrics;
8329
- }
8330
- async chat(req, _options) {
8331
- if (this.config.latencyMs) {
8332
- await new Promise((resolve) => setTimeout(resolve, this.config.latencyMs));
8333
- }
8334
- if (this.config.shouldError) {
8335
- throw new Error(this.config.errorMessage ?? "Mock chat error");
8336
- }
8337
- this.updateMetrics("chat");
8338
- if (typeof this.config.chatResponse === "function") {
8339
- return this.config.chatResponse(req);
8340
- }
8341
- return this.config.chatResponse ?? {
8342
- results: [
8343
- {
8344
- content: "Mock response",
8345
- finishReason: "stop"
8346
- }
8347
- ],
8348
- modelUsage: {
8349
- promptTokens: 10,
8350
- completionTokens: 5,
8351
- totalTokens: 15
8352
- }
8353
- };
8354
- }
8355
- async embed(req, _options) {
8356
- if (this.config.latencyMs) {
8357
- await new Promise((resolve) => setTimeout(resolve, this.config.latencyMs));
8358
- }
8359
- if (this.config.shouldError) {
8360
- throw new Error(this.config.errorMessage ?? "Mock embed error");
8361
- }
8362
- this.updateMetrics("embed");
8363
- if (typeof this.config.embedResponse === "function") {
8364
- return this.config.embedResponse(req);
8365
- }
8366
- return this.config.embedResponse ?? {
8367
- embeddings: [[0.1, 0.2, 0.3]],
8368
- modelUsage: {
8369
- promptTokens: 5,
8370
- completionTokens: 0,
8371
- totalTokens: 5
8372
- }
8373
- };
8374
- }
8375
- setOptions(options) {
8376
- this.options = options;
8377
- }
8378
- getOptions() {
8379
- return this.options;
8380
- }
8381
- updateMetrics(type) {
8382
- const latency = this.config.latencyMs ?? 0;
8383
- this.metrics.latency[type].samples.push(latency);
8384
- const samples = this.metrics.latency[type].samples;
8385
- this.metrics.latency[type].mean = samples.reduce((a, b) => a + b, 0) / samples.length;
8386
- if (samples.length > 0) {
8387
- const sortedSamples = [...samples].sort((a, b) => a - b);
8388
- const p95Index = Math.max(0, Math.floor(sortedSamples.length * 0.95) - 1);
8389
- this.metrics.latency[type].p95 = sortedSamples[p95Index] ?? latency;
8390
- const p99Index = Math.max(0, Math.floor(sortedSamples.length * 0.99) - 1);
8391
- this.metrics.latency[type].p99 = sortedSamples[p99Index] ?? latency;
8392
- }
8393
- if (this.config.shouldError) {
8394
- this.metrics.errors[type].count++;
8395
- this.metrics.errors[type].total++;
8396
- const totalRequests = this.metrics.latency[type].samples.length;
8397
- this.metrics.errors[type].rate = totalRequests > 0 ? this.metrics.errors[type].count / totalRequests : 0;
8398
- }
8399
- }
8400
- };
8401
-
8402
8469
  // prompts/rag.ts
8403
8470
  var AxRAG = class extends AxChainOfThought {
8404
8471
  genQuery;