@ax-llm/ax 11.0.17 → 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
  };
@@ -4435,8 +4498,6 @@ var validateValue = (field, value) => {
4435
4498
  switch (expectedType) {
4436
4499
  case "code":
4437
4500
  return typeof val === "string";
4438
- case "json":
4439
- return typeof val === "object";
4440
4501
  case "string":
4441
4502
  return typeof val === "string";
4442
4503
  case "number":
@@ -4570,7 +4631,9 @@ function mergeDeltas(base, delta) {
4570
4631
  for (const key of Object.keys(delta)) {
4571
4632
  const baseValue = base[key];
4572
4633
  const deltaValue = delta[key];
4573
- if ((baseValue === void 0 || Array.isArray(baseValue)) && Array.isArray(deltaValue)) {
4634
+ if (baseValue === void 0 && Array.isArray(deltaValue)) {
4635
+ base[key] = [...deltaValue];
4636
+ } else if (Array.isArray(baseValue) && Array.isArray(deltaValue)) {
4574
4637
  base[key] = [...baseValue ?? [], ...deltaValue];
4575
4638
  } else if ((baseValue === void 0 || typeof baseValue === "string") && typeof deltaValue === "string") {
4576
4639
  base[key] = (baseValue ?? "") + deltaValue;
@@ -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,31 +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 = {};
5434
- }
5435
- if (fieldIsArray) {
5436
- if (xstate.streamedIndex[fieldName] === void 0) {
5437
- xstate.streamedIndex[fieldName] = 0;
5438
- }
5439
- return null;
5440
- }
5441
- if (fieldTypeName !== "string" && fieldTypeName !== "code") {
5442
- if (xstate.streamedIndex[fieldName] === void 0) {
5443
- xstate.streamedIndex[fieldName] = 0;
5444
- }
5445
- 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;
5446
5497
  }
5447
5498
  const pos = xstate.streamedIndex[fieldName] ?? 0;
5448
- const s = xstate.s + pos;
5449
5499
  const isFirstChunk = pos === 0;
5450
- const d1 = content.substring(s);
5500
+ const d1 = content.substring(s + pos, e);
5501
+ if (d1.length === 0) {
5502
+ return;
5503
+ }
5451
5504
  let d2 = d1.replace(/\s+$/, "");
5452
5505
  if (xstate.currField?.type?.name === "code") {
5453
5506
  d2 = d2.replace(/\s*```\s*$/, "");
@@ -5457,40 +5510,45 @@ function processStreamingDelta(content, xstate) {
5457
5510
  d3 = d3.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/, "");
5458
5511
  }
5459
5512
  if (d3.length > 0) {
5513
+ yield { [fieldName]: d3 };
5460
5514
  xstate.streamedIndex[fieldName] = pos + d2.length;
5461
5515
  }
5462
- return d3;
5463
5516
  }
5464
- function getStreamingDelta(content, xstate) {
5465
- if (xstate.lastDelta) {
5466
- processStreamingDelta(xstate.lastDelta, xstate);
5467
- 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;
5468
5522
  }
5469
- return processStreamingDelta(content, xstate);
5470
- }
5471
- function* streamValues(sig, values, xstate, delta) {
5472
- if (!xstate.currField) {
5473
- return;
5474
- }
5475
- const fieldName = xstate.currField.name;
5476
- if (delta && delta.length > 0) {
5477
- yield { [fieldName]: delta };
5523
+ if (!xstate.currField || xstate.currField.isInternal) {
5478
5524
  return;
5479
5525
  }
5526
+ yield* yieldDelta(
5527
+ content,
5528
+ xstate.currField,
5529
+ xstate.s,
5530
+ content.length,
5531
+ xstate
5532
+ );
5533
+ const outputFields = sig.getOutputFields();
5480
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
+ }
5481
5539
  const value = values[key];
5482
5540
  if (Array.isArray(value)) {
5483
- if (xstate.streamedIndex?.[key] === void 0) {
5484
- throw new Error("Streamed index is not set for array field " + key);
5485
- }
5486
- const s = xstate.streamedIndex[key];
5541
+ const s = xstate.streamedIndex?.[key] ?? 0;
5487
5542
  const v = value.slice(s);
5488
5543
  if (v && v.length > 0) {
5489
5544
  yield { [key]: v };
5490
5545
  xstate.streamedIndex[key] = s + 1;
5491
5546
  }
5492
- } else {
5547
+ continue;
5548
+ }
5549
+ if (!xstate.streamedIndex[key]) {
5493
5550
  yield { [key]: value };
5551
+ xstate.streamedIndex[key] = 1;
5494
5552
  }
5495
5553
  }
5496
5554
  }
@@ -5982,6 +6040,7 @@ var AxGen = class extends AxProgramWithSignature {
5982
6040
  const values = {};
5983
6041
  const xstate = {
5984
6042
  extractedFields: [],
6043
+ streamedIndex: {},
5985
6044
  s: -1
5986
6045
  };
5987
6046
  let content = "";
@@ -6017,11 +6076,7 @@ var AxGen = class extends AxProgramWithSignature {
6017
6076
  content,
6018
6077
  streamingValidation
6019
6078
  );
6020
- if (skip) {
6021
- continue;
6022
- }
6023
6079
  assertStreamingAssertions(this.streamingAsserts, xstate, content);
6024
- assertAssertions(this.asserts, values);
6025
6080
  if (this.streamingFieldProcessors.length !== 0) {
6026
6081
  await processStreamingFieldProcessors(
6027
6082
  this.streamingFieldProcessors,
@@ -6032,8 +6087,11 @@ var AxGen = class extends AxProgramWithSignature {
6032
6087
  sessionId
6033
6088
  );
6034
6089
  }
6035
- const delta = getStreamingDelta(content, xstate);
6036
- 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);
6037
6095
  }
6038
6096
  if (result.finishReason === "length") {
6039
6097
  throw new Error("Max tokens reached before completion");
@@ -6076,8 +6134,7 @@ var AxGen = class extends AxProgramWithSignature {
6076
6134
  true
6077
6135
  );
6078
6136
  }
6079
- const delta = getStreamingDelta(content, xstate);
6080
- yield* streamValues(this.signature, values, xstate, delta);
6137
+ yield* streamValues(this.signature, content, values, xstate);
6081
6138
  }
6082
6139
  }
6083
6140
  async processResponse({
@@ -6131,6 +6188,12 @@ var AxGen = class extends AxProgramWithSignature {
6131
6188
  throw new Error("Max tokens reached before completion");
6132
6189
  }
6133
6190
  }
6191
+ const publicValues = { ...values };
6192
+ for (const field of this.signature.getOutputFields()) {
6193
+ if (field.isInternal) {
6194
+ delete publicValues[field.name];
6195
+ }
6196
+ }
6134
6197
  return { ...values };
6135
6198
  }
6136
6199
  async *_forward2(ai, values, options, span) {
@@ -8015,6 +8078,125 @@ var AxJSInterpreter = class {
8015
8078
  }
8016
8079
  };
8017
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
+
8018
8200
  // dsp/router.ts
8019
8201
  var colorLog6 = new ColorLog();
8020
8202
  var AxRoute = class {
@@ -8284,126 +8466,6 @@ var AxEmbeddingAdapter = class {
8284
8466
  }
8285
8467
  };
8286
8468
 
8287
- // ai/mock/api.ts
8288
- var AxMockAIService = class {
8289
- constructor(config = {}) {
8290
- this.config = config;
8291
- this.config.id = this.config.id ?? crypto.randomUUID();
8292
- }
8293
- options = {};
8294
- metrics = {
8295
- latency: {
8296
- chat: { mean: 0, p95: 0, p99: 0, samples: [] },
8297
- embed: { mean: 0, p95: 0, p99: 0, samples: [] }
8298
- },
8299
- errors: {
8300
- chat: { count: 0, rate: 0, total: 0 },
8301
- embed: { count: 0, rate: 0, total: 0 }
8302
- }
8303
- };
8304
- getName() {
8305
- return this.config.name ?? "mock-ai-service";
8306
- }
8307
- getId() {
8308
- return this.config.id ?? "mock-ai-service-id";
8309
- }
8310
- getModelInfo() {
8311
- return {
8312
- name: "mock-model",
8313
- provider: "mock-provider",
8314
- promptTokenCostPer1M: 100,
8315
- completionTokenCostPer1M: 100,
8316
- ...this.config.modelInfo
8317
- };
8318
- }
8319
- getEmbedModelInfo() {
8320
- return this.config.embedModelInfo;
8321
- }
8322
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
8323
- getFeatures(_model) {
8324
- return {
8325
- functions: this.config.features?.functions ?? false,
8326
- streaming: this.config.features?.streaming ?? false
8327
- };
8328
- }
8329
- getModelList() {
8330
- return this.config.models;
8331
- }
8332
- getMetrics() {
8333
- return this.metrics;
8334
- }
8335
- async chat(req, _options) {
8336
- if (this.config.latencyMs) {
8337
- await new Promise((resolve) => setTimeout(resolve, this.config.latencyMs));
8338
- }
8339
- if (this.config.shouldError) {
8340
- throw new Error(this.config.errorMessage ?? "Mock chat error");
8341
- }
8342
- this.updateMetrics("chat");
8343
- if (typeof this.config.chatResponse === "function") {
8344
- return this.config.chatResponse(req);
8345
- }
8346
- return this.config.chatResponse ?? {
8347
- results: [
8348
- {
8349
- content: "Mock response",
8350
- finishReason: "stop"
8351
- }
8352
- ],
8353
- modelUsage: {
8354
- promptTokens: 10,
8355
- completionTokens: 5,
8356
- totalTokens: 15
8357
- }
8358
- };
8359
- }
8360
- async embed(req, _options) {
8361
- if (this.config.latencyMs) {
8362
- await new Promise((resolve) => setTimeout(resolve, this.config.latencyMs));
8363
- }
8364
- if (this.config.shouldError) {
8365
- throw new Error(this.config.errorMessage ?? "Mock embed error");
8366
- }
8367
- this.updateMetrics("embed");
8368
- if (typeof this.config.embedResponse === "function") {
8369
- return this.config.embedResponse(req);
8370
- }
8371
- return this.config.embedResponse ?? {
8372
- embeddings: [[0.1, 0.2, 0.3]],
8373
- modelUsage: {
8374
- promptTokens: 5,
8375
- completionTokens: 0,
8376
- totalTokens: 5
8377
- }
8378
- };
8379
- }
8380
- setOptions(options) {
8381
- this.options = options;
8382
- }
8383
- getOptions() {
8384
- return this.options;
8385
- }
8386
- updateMetrics(type) {
8387
- const latency = this.config.latencyMs ?? 0;
8388
- this.metrics.latency[type].samples.push(latency);
8389
- const samples = this.metrics.latency[type].samples;
8390
- this.metrics.latency[type].mean = samples.reduce((a, b) => a + b, 0) / samples.length;
8391
- if (samples.length > 0) {
8392
- const sortedSamples = [...samples].sort((a, b) => a - b);
8393
- const p95Index = Math.max(0, Math.floor(sortedSamples.length * 0.95) - 1);
8394
- this.metrics.latency[type].p95 = sortedSamples[p95Index] ?? latency;
8395
- const p99Index = Math.max(0, Math.floor(sortedSamples.length * 0.99) - 1);
8396
- this.metrics.latency[type].p99 = sortedSamples[p99Index] ?? latency;
8397
- }
8398
- if (this.config.shouldError) {
8399
- this.metrics.errors[type].count++;
8400
- this.metrics.errors[type].total++;
8401
- const totalRequests = this.metrics.latency[type].samples.length;
8402
- this.metrics.errors[type].rate = totalRequests > 0 ? this.metrics.errors[type].count / totalRequests : 0;
8403
- }
8404
- }
8405
- };
8406
-
8407
8469
  // prompts/rag.ts
8408
8470
  var AxRAG = class extends AxChainOfThought {
8409
8471
  genQuery;