@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.cjs +261 -194
- package/index.cjs.map +1 -1
- package/index.d.cts +47 -47
- package/index.d.ts +47 -47
- package/index.js +261 -194
- package/index.js.map +1 -1
- package/package.json +1 -1
package/index.cjs
CHANGED
|
@@ -4087,7 +4087,10 @@ var SignatureParser = class {
|
|
|
4087
4087
|
this.skipWhitespace();
|
|
4088
4088
|
const optionalDesc = this.parseParsedString();
|
|
4089
4089
|
this.skipWhitespace();
|
|
4090
|
-
const inputs = this.parseFieldList(
|
|
4090
|
+
const inputs = this.parseFieldList(
|
|
4091
|
+
this.parseInputField.bind(this),
|
|
4092
|
+
"input"
|
|
4093
|
+
);
|
|
4091
4094
|
this.skipWhitespace();
|
|
4092
4095
|
if (this.position >= this.input.length) {
|
|
4093
4096
|
throw new Error(
|
|
@@ -4101,7 +4104,10 @@ var SignatureParser = class {
|
|
|
4101
4104
|
'Incomplete signature: No output fields specified after "->"'
|
|
4102
4105
|
);
|
|
4103
4106
|
}
|
|
4104
|
-
const outputs = this.parseFieldList(
|
|
4107
|
+
const outputs = this.parseFieldList(
|
|
4108
|
+
this.parseOutputField.bind(this),
|
|
4109
|
+
"output"
|
|
4110
|
+
);
|
|
4105
4111
|
return {
|
|
4106
4112
|
desc: optionalDesc?.trim(),
|
|
4107
4113
|
inputs,
|
|
@@ -4163,11 +4169,75 @@ ${pointer}`;
|
|
|
4163
4169
|
}
|
|
4164
4170
|
return fields;
|
|
4165
4171
|
}
|
|
4166
|
-
|
|
4172
|
+
// -------------------------------
|
|
4173
|
+
// Parse input fields (no "class" type and no internal flag)
|
|
4174
|
+
// -------------------------------
|
|
4175
|
+
parseInputField() {
|
|
4167
4176
|
this.skipWhitespace();
|
|
4168
4177
|
const name = this.parseParsedIdentifier();
|
|
4169
4178
|
this.currentFieldName = name;
|
|
4170
|
-
|
|
4179
|
+
let isOptional = void 0;
|
|
4180
|
+
while (true) {
|
|
4181
|
+
if (this.match("?")) {
|
|
4182
|
+
isOptional = true;
|
|
4183
|
+
continue;
|
|
4184
|
+
}
|
|
4185
|
+
if (this.match("!")) {
|
|
4186
|
+
throw new Error(
|
|
4187
|
+
`Input field "${name}" does not support the internal marker "!"`
|
|
4188
|
+
);
|
|
4189
|
+
}
|
|
4190
|
+
break;
|
|
4191
|
+
}
|
|
4192
|
+
let type;
|
|
4193
|
+
this.skipWhitespace();
|
|
4194
|
+
if (this.match(":")) {
|
|
4195
|
+
this.skipWhitespace();
|
|
4196
|
+
if (/^class\b/.test(this.input.slice(this.position))) {
|
|
4197
|
+
throw new Error(
|
|
4198
|
+
`Input field "${name}" does not support the "class" type`
|
|
4199
|
+
);
|
|
4200
|
+
} else {
|
|
4201
|
+
try {
|
|
4202
|
+
const typeName = this.parseTypeNotClass();
|
|
4203
|
+
const isArray = this.match("[]");
|
|
4204
|
+
type = { name: typeName, isArray };
|
|
4205
|
+
} catch (error) {
|
|
4206
|
+
throw new Error(
|
|
4207
|
+
`Input field "${name}": ${error instanceof Error ? error.message : "Unknown error"}`
|
|
4208
|
+
);
|
|
4209
|
+
}
|
|
4210
|
+
}
|
|
4211
|
+
}
|
|
4212
|
+
this.skipWhitespace();
|
|
4213
|
+
const desc = this.parseParsedString();
|
|
4214
|
+
return {
|
|
4215
|
+
name,
|
|
4216
|
+
desc: desc?.trim(),
|
|
4217
|
+
type,
|
|
4218
|
+
isOptional
|
|
4219
|
+
};
|
|
4220
|
+
}
|
|
4221
|
+
// -------------------------------
|
|
4222
|
+
// Parse output fields (supports both "class" type and the internal marker)
|
|
4223
|
+
// -------------------------------
|
|
4224
|
+
parseOutputField() {
|
|
4225
|
+
this.skipWhitespace();
|
|
4226
|
+
const name = this.parseParsedIdentifier();
|
|
4227
|
+
this.currentFieldName = name;
|
|
4228
|
+
let isOptional = false;
|
|
4229
|
+
let isInternal = false;
|
|
4230
|
+
while (true) {
|
|
4231
|
+
if (this.match("?")) {
|
|
4232
|
+
isOptional = true;
|
|
4233
|
+
continue;
|
|
4234
|
+
}
|
|
4235
|
+
if (this.match("!")) {
|
|
4236
|
+
isInternal = true;
|
|
4237
|
+
continue;
|
|
4238
|
+
}
|
|
4239
|
+
break;
|
|
4240
|
+
}
|
|
4171
4241
|
let type;
|
|
4172
4242
|
this.skipWhitespace();
|
|
4173
4243
|
if (this.match(":")) {
|
|
@@ -4175,16 +4245,16 @@ ${pointer}`;
|
|
|
4175
4245
|
if (this.match("class")) {
|
|
4176
4246
|
const isArray = this.match("[]");
|
|
4177
4247
|
this.skipWhitespace();
|
|
4178
|
-
const
|
|
4179
|
-
if (!
|
|
4248
|
+
const classNamesString = this.parseParsedString();
|
|
4249
|
+
if (!classNamesString) {
|
|
4180
4250
|
throw new Error(
|
|
4181
|
-
`
|
|
4251
|
+
`Output field "${name}": Expected class names in quotes after "class" type. Example: class "MyClass1, MyClass2"`
|
|
4182
4252
|
);
|
|
4183
4253
|
}
|
|
4184
|
-
const classes =
|
|
4254
|
+
const classes = classNamesString.split(/[,\s]+/).map((s) => s.trim()).filter((s) => s.length > 0);
|
|
4185
4255
|
if (classes.length === 0) {
|
|
4186
4256
|
throw new Error(
|
|
4187
|
-
`
|
|
4257
|
+
`Output field "${name}": Empty class list provided. At least one class name is required`
|
|
4188
4258
|
);
|
|
4189
4259
|
}
|
|
4190
4260
|
type = { name: "class", isArray, classes };
|
|
@@ -4195,28 +4265,20 @@ ${pointer}`;
|
|
|
4195
4265
|
type = { name: typeName, isArray };
|
|
4196
4266
|
} catch (error) {
|
|
4197
4267
|
throw new Error(
|
|
4198
|
-
`
|
|
4268
|
+
`Output field "${name}": ${error instanceof Error ? error.message : "Unknown error"}`
|
|
4199
4269
|
);
|
|
4200
4270
|
}
|
|
4201
4271
|
}
|
|
4202
4272
|
}
|
|
4203
4273
|
this.skipWhitespace();
|
|
4204
4274
|
const desc = this.parseParsedString();
|
|
4205
|
-
|
|
4206
|
-
|
|
4207
|
-
|
|
4208
|
-
|
|
4209
|
-
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
} else {
|
|
4213
|
-
return {
|
|
4214
|
-
name,
|
|
4215
|
-
desc: desc?.trim(),
|
|
4216
|
-
type,
|
|
4217
|
-
isOptional
|
|
4218
|
-
};
|
|
4219
|
-
}
|
|
4275
|
+
return {
|
|
4276
|
+
name,
|
|
4277
|
+
desc: desc?.trim(),
|
|
4278
|
+
type,
|
|
4279
|
+
isOptional,
|
|
4280
|
+
isInternal
|
|
4281
|
+
};
|
|
4220
4282
|
}
|
|
4221
4283
|
parseTypeNotClass() {
|
|
4222
4284
|
const types = [
|
|
@@ -4370,8 +4432,9 @@ var AxSignature = class _AxSignature {
|
|
|
4370
4432
|
name: field.name,
|
|
4371
4433
|
title,
|
|
4372
4434
|
description: "desc" in field ? field.desc : void 0,
|
|
4373
|
-
|
|
4374
|
-
|
|
4435
|
+
type: field.type ?? { name: "string", isArray: false },
|
|
4436
|
+
..."isInternal" in field ? { isInternal: field.isInternal } : {},
|
|
4437
|
+
..."isOptional" in field ? { isOptional: field.isOptional } : {}
|
|
4375
4438
|
};
|
|
4376
4439
|
};
|
|
4377
4440
|
parseField = (field) => {
|
|
@@ -4405,7 +4468,7 @@ var AxSignature = class _AxSignature {
|
|
|
4405
4468
|
getOutputFields = () => this.outputFields;
|
|
4406
4469
|
getDescription = () => this.description;
|
|
4407
4470
|
toTitle = (name) => {
|
|
4408
|
-
let result = name.
|
|
4471
|
+
let result = name.replace(/_/g, " ");
|
|
4409
4472
|
result = result.replace(/([A-Z]|[0-9]+)/g, " $1").trim();
|
|
4410
4473
|
return result.charAt(0).toUpperCase() + result.slice(1);
|
|
4411
4474
|
};
|
|
@@ -5407,7 +5470,7 @@ var formatDateWithTimezone = (date) => {
|
|
|
5407
5470
|
|
|
5408
5471
|
// dsp/extract.ts
|
|
5409
5472
|
var extractValues = (sig, values, content) => {
|
|
5410
|
-
const xstate = { extractedFields: [], s: -1 };
|
|
5473
|
+
const xstate = { extractedFields: [], streamedIndex: {}, s: -1 };
|
|
5411
5474
|
streamingExtractValues(sig, values, xstate, content);
|
|
5412
5475
|
streamingExtractFinalValue(sig, values, xstate, content);
|
|
5413
5476
|
};
|
|
@@ -5461,7 +5524,7 @@ var streamingExtractValues = (sig, values, xstate, content, streamingValidation
|
|
|
5461
5524
|
if (parsedValue !== void 0) {
|
|
5462
5525
|
values[xstate.currField.name] = parsedValue;
|
|
5463
5526
|
}
|
|
5464
|
-
xstate.
|
|
5527
|
+
xstate.prevField = { field: xstate.currField, s: xstate.s, e };
|
|
5465
5528
|
}
|
|
5466
5529
|
checkMissingRequiredFields(xstate, values, index);
|
|
5467
5530
|
xstate.s = e + prefixLen;
|
|
@@ -5470,6 +5533,9 @@ var streamingExtractValues = (sig, values, xstate, content, streamingValidation
|
|
|
5470
5533
|
if (!xstate.extractedFields.includes(field)) {
|
|
5471
5534
|
xstate.extractedFields.push(field);
|
|
5472
5535
|
}
|
|
5536
|
+
if (xstate.streamedIndex[field.name] === void 0) {
|
|
5537
|
+
xstate.streamedIndex[field.name] = 0;
|
|
5538
|
+
}
|
|
5473
5539
|
}
|
|
5474
5540
|
};
|
|
5475
5541
|
var streamingExtractFinalValue = (sig, values, xstate, content) => {
|
|
@@ -5525,25 +5591,18 @@ var convertValueToType = (field, val) => {
|
|
|
5525
5591
|
return val;
|
|
5526
5592
|
}
|
|
5527
5593
|
};
|
|
5528
|
-
function
|
|
5529
|
-
|
|
5530
|
-
|
|
5531
|
-
|
|
5532
|
-
|
|
5533
|
-
const { isArray: fieldIsArray, name: fieldTypeName } = fieldType ?? {};
|
|
5534
|
-
if (!xstate.streamedIndex) {
|
|
5535
|
-
xstate.streamedIndex = { [fieldName]: 0 };
|
|
5536
|
-
}
|
|
5537
|
-
if (fieldIsArray) {
|
|
5538
|
-
return null;
|
|
5539
|
-
}
|
|
5540
|
-
if (fieldTypeName !== "string" && fieldTypeName !== "code") {
|
|
5541
|
-
return null;
|
|
5594
|
+
function* yieldDelta(content, field, s, e, xstate) {
|
|
5595
|
+
const { name: fieldName, isInternal } = field;
|
|
5596
|
+
const { isArray: fieldIsArray, name: fieldTypeName } = field.type ?? {};
|
|
5597
|
+
if (isInternal || fieldIsArray || fieldTypeName && fieldTypeName !== "string" && fieldTypeName !== "code") {
|
|
5598
|
+
return;
|
|
5542
5599
|
}
|
|
5543
5600
|
const pos = xstate.streamedIndex[fieldName] ?? 0;
|
|
5544
|
-
const s = xstate.s + pos;
|
|
5545
5601
|
const isFirstChunk = pos === 0;
|
|
5546
|
-
const d1 = content.substring(s);
|
|
5602
|
+
const d1 = content.substring(s + pos, e);
|
|
5603
|
+
if (d1.length === 0) {
|
|
5604
|
+
return;
|
|
5605
|
+
}
|
|
5547
5606
|
let d2 = d1.replace(/\s+$/, "");
|
|
5548
5607
|
if (xstate.currField?.type?.name === "code") {
|
|
5549
5608
|
d2 = d2.replace(/\s*```\s*$/, "");
|
|
@@ -5553,41 +5612,45 @@ function processStreamingDelta(content, xstate) {
|
|
|
5553
5612
|
d3 = d3.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/, "");
|
|
5554
5613
|
}
|
|
5555
5614
|
if (d3.length > 0) {
|
|
5615
|
+
yield { [fieldName]: d3 };
|
|
5556
5616
|
xstate.streamedIndex[fieldName] = pos + d2.length;
|
|
5557
5617
|
}
|
|
5558
|
-
return d3;
|
|
5559
5618
|
}
|
|
5560
|
-
function
|
|
5561
|
-
if (xstate.
|
|
5562
|
-
|
|
5563
|
-
|
|
5619
|
+
function* streamValues(sig, content, values, xstate) {
|
|
5620
|
+
if (xstate.prevField && !xstate.prevField.field.isInternal) {
|
|
5621
|
+
const { field, s, e } = xstate.prevField;
|
|
5622
|
+
yield* yieldDelta(content, field, s, e, xstate);
|
|
5623
|
+
xstate.prevField = void 0;
|
|
5564
5624
|
}
|
|
5565
|
-
|
|
5566
|
-
}
|
|
5567
|
-
function* streamValues(sig, values, xstate, delta) {
|
|
5568
|
-
if (!xstate.currField) {
|
|
5569
|
-
return;
|
|
5570
|
-
}
|
|
5571
|
-
const fieldName = xstate.currField.name;
|
|
5572
|
-
if (delta && delta.length > 0) {
|
|
5573
|
-
yield { [fieldName]: delta };
|
|
5625
|
+
if (!xstate.currField || xstate.currField.isInternal) {
|
|
5574
5626
|
return;
|
|
5575
5627
|
}
|
|
5628
|
+
yield* yieldDelta(
|
|
5629
|
+
content,
|
|
5630
|
+
xstate.currField,
|
|
5631
|
+
xstate.s,
|
|
5632
|
+
content.length,
|
|
5633
|
+
xstate
|
|
5634
|
+
);
|
|
5635
|
+
const outputFields = sig.getOutputFields();
|
|
5576
5636
|
for (const key of Object.keys(values)) {
|
|
5637
|
+
const field = outputFields.find((f) => f.name === key);
|
|
5638
|
+
if (!field || field.isInternal) {
|
|
5639
|
+
continue;
|
|
5640
|
+
}
|
|
5577
5641
|
const value = values[key];
|
|
5578
5642
|
if (Array.isArray(value)) {
|
|
5579
5643
|
const s = xstate.streamedIndex?.[key] ?? 0;
|
|
5580
5644
|
const v = value.slice(s);
|
|
5581
5645
|
if (v && v.length > 0) {
|
|
5582
5646
|
yield { [key]: v };
|
|
5583
|
-
|
|
5584
|
-
xstate.streamedIndex[key] = s + 1;
|
|
5585
|
-
} else {
|
|
5586
|
-
xstate.streamedIndex = { [key]: s + 1 };
|
|
5587
|
-
}
|
|
5647
|
+
xstate.streamedIndex[key] = s + 1;
|
|
5588
5648
|
}
|
|
5589
|
-
|
|
5649
|
+
continue;
|
|
5650
|
+
}
|
|
5651
|
+
if (!xstate.streamedIndex[key]) {
|
|
5590
5652
|
yield { [key]: value };
|
|
5653
|
+
xstate.streamedIndex[key] = 1;
|
|
5591
5654
|
}
|
|
5592
5655
|
}
|
|
5593
5656
|
}
|
|
@@ -6079,6 +6142,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6079
6142
|
const values = {};
|
|
6080
6143
|
const xstate = {
|
|
6081
6144
|
extractedFields: [],
|
|
6145
|
+
streamedIndex: {},
|
|
6082
6146
|
s: -1
|
|
6083
6147
|
};
|
|
6084
6148
|
let content = "";
|
|
@@ -6114,11 +6178,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6114
6178
|
content,
|
|
6115
6179
|
streamingValidation
|
|
6116
6180
|
);
|
|
6117
|
-
if (skip) {
|
|
6118
|
-
continue;
|
|
6119
|
-
}
|
|
6120
6181
|
assertStreamingAssertions(this.streamingAsserts, xstate, content);
|
|
6121
|
-
assertAssertions(this.asserts, values);
|
|
6122
6182
|
if (this.streamingFieldProcessors.length !== 0) {
|
|
6123
6183
|
await processStreamingFieldProcessors(
|
|
6124
6184
|
this.streamingFieldProcessors,
|
|
@@ -6129,8 +6189,11 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6129
6189
|
sessionId
|
|
6130
6190
|
);
|
|
6131
6191
|
}
|
|
6132
|
-
|
|
6133
|
-
|
|
6192
|
+
yield* streamValues(this.signature, content, values, xstate);
|
|
6193
|
+
if (skip) {
|
|
6194
|
+
continue;
|
|
6195
|
+
}
|
|
6196
|
+
assertAssertions(this.asserts, values);
|
|
6134
6197
|
}
|
|
6135
6198
|
if (result.finishReason === "length") {
|
|
6136
6199
|
throw new Error("Max tokens reached before completion");
|
|
@@ -6173,8 +6236,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6173
6236
|
true
|
|
6174
6237
|
);
|
|
6175
6238
|
}
|
|
6176
|
-
|
|
6177
|
-
yield* streamValues(this.signature, values, xstate, delta);
|
|
6239
|
+
yield* streamValues(this.signature, content, values, xstate);
|
|
6178
6240
|
}
|
|
6179
6241
|
}
|
|
6180
6242
|
async processResponse({
|
|
@@ -6228,6 +6290,12 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6228
6290
|
throw new Error("Max tokens reached before completion");
|
|
6229
6291
|
}
|
|
6230
6292
|
}
|
|
6293
|
+
const publicValues = { ...values };
|
|
6294
|
+
for (const field of this.signature.getOutputFields()) {
|
|
6295
|
+
if (field.isInternal) {
|
|
6296
|
+
delete publicValues[field.name];
|
|
6297
|
+
}
|
|
6298
|
+
}
|
|
6231
6299
|
return { ...values };
|
|
6232
6300
|
}
|
|
6233
6301
|
async *_forward2(ai, values, options, span) {
|
|
@@ -8112,6 +8180,125 @@ var AxJSInterpreter = class {
|
|
|
8112
8180
|
}
|
|
8113
8181
|
};
|
|
8114
8182
|
|
|
8183
|
+
// ai/mock/api.ts
|
|
8184
|
+
var AxMockAIService = class {
|
|
8185
|
+
constructor(config = {}) {
|
|
8186
|
+
this.config = config;
|
|
8187
|
+
this.config.id = this.config.id ?? crypto.randomUUID();
|
|
8188
|
+
}
|
|
8189
|
+
metrics = {
|
|
8190
|
+
latency: {
|
|
8191
|
+
chat: { mean: 0, p95: 0, p99: 0, samples: [] },
|
|
8192
|
+
embed: { mean: 0, p95: 0, p99: 0, samples: [] }
|
|
8193
|
+
},
|
|
8194
|
+
errors: {
|
|
8195
|
+
chat: { count: 0, rate: 0, total: 0 },
|
|
8196
|
+
embed: { count: 0, rate: 0, total: 0 }
|
|
8197
|
+
}
|
|
8198
|
+
};
|
|
8199
|
+
getName() {
|
|
8200
|
+
return this.config.name ?? "mock-ai-service";
|
|
8201
|
+
}
|
|
8202
|
+
getId() {
|
|
8203
|
+
return this.config.id ?? "mock-ai-service-id";
|
|
8204
|
+
}
|
|
8205
|
+
getModelInfo() {
|
|
8206
|
+
return {
|
|
8207
|
+
name: "mock-model",
|
|
8208
|
+
provider: "mock-provider",
|
|
8209
|
+
promptTokenCostPer1M: 100,
|
|
8210
|
+
completionTokenCostPer1M: 100,
|
|
8211
|
+
...this.config.modelInfo
|
|
8212
|
+
};
|
|
8213
|
+
}
|
|
8214
|
+
getEmbedModelInfo() {
|
|
8215
|
+
return this.config.embedModelInfo;
|
|
8216
|
+
}
|
|
8217
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
8218
|
+
getFeatures(_model) {
|
|
8219
|
+
return {
|
|
8220
|
+
functions: this.config.features?.functions ?? false,
|
|
8221
|
+
streaming: this.config.features?.streaming ?? false
|
|
8222
|
+
};
|
|
8223
|
+
}
|
|
8224
|
+
getModelList() {
|
|
8225
|
+
return this.config.models;
|
|
8226
|
+
}
|
|
8227
|
+
getMetrics() {
|
|
8228
|
+
return this.metrics;
|
|
8229
|
+
}
|
|
8230
|
+
async chat(req, options) {
|
|
8231
|
+
if (this.config.latencyMs) {
|
|
8232
|
+
await new Promise((resolve) => setTimeout(resolve, this.config.latencyMs));
|
|
8233
|
+
}
|
|
8234
|
+
if (this.config.shouldError) {
|
|
8235
|
+
throw new Error(this.config.errorMessage ?? "Mock chat error");
|
|
8236
|
+
}
|
|
8237
|
+
this.updateMetrics("chat");
|
|
8238
|
+
if (typeof this.config.chatResponse === "function") {
|
|
8239
|
+
return await this.config.chatResponse(req);
|
|
8240
|
+
}
|
|
8241
|
+
return this.config.chatResponse ?? {
|
|
8242
|
+
results: [
|
|
8243
|
+
{
|
|
8244
|
+
content: "Mock response",
|
|
8245
|
+
finishReason: "stop"
|
|
8246
|
+
}
|
|
8247
|
+
],
|
|
8248
|
+
modelUsage: {
|
|
8249
|
+
promptTokens: 10,
|
|
8250
|
+
completionTokens: 5,
|
|
8251
|
+
totalTokens: 15
|
|
8252
|
+
}
|
|
8253
|
+
};
|
|
8254
|
+
}
|
|
8255
|
+
async embed(req, _options) {
|
|
8256
|
+
if (this.config.latencyMs) {
|
|
8257
|
+
await new Promise((resolve) => setTimeout(resolve, this.config.latencyMs));
|
|
8258
|
+
}
|
|
8259
|
+
if (this.config.shouldError) {
|
|
8260
|
+
throw new Error(this.config.errorMessage ?? "Mock embed error");
|
|
8261
|
+
}
|
|
8262
|
+
this.updateMetrics("embed");
|
|
8263
|
+
if (typeof this.config.embedResponse === "function") {
|
|
8264
|
+
return this.config.embedResponse(req);
|
|
8265
|
+
}
|
|
8266
|
+
return this.config.embedResponse ?? {
|
|
8267
|
+
embeddings: [[0.1, 0.2, 0.3]],
|
|
8268
|
+
modelUsage: {
|
|
8269
|
+
promptTokens: 5,
|
|
8270
|
+
completionTokens: 0,
|
|
8271
|
+
totalTokens: 5
|
|
8272
|
+
}
|
|
8273
|
+
};
|
|
8274
|
+
}
|
|
8275
|
+
setOptions(options) {
|
|
8276
|
+
this.config.options = options;
|
|
8277
|
+
}
|
|
8278
|
+
getOptions() {
|
|
8279
|
+
return this.config.options ?? {};
|
|
8280
|
+
}
|
|
8281
|
+
updateMetrics(type) {
|
|
8282
|
+
const latency = this.config.latencyMs ?? 0;
|
|
8283
|
+
this.metrics.latency[type].samples.push(latency);
|
|
8284
|
+
const samples = this.metrics.latency[type].samples;
|
|
8285
|
+
this.metrics.latency[type].mean = samples.reduce((a, b) => a + b, 0) / samples.length;
|
|
8286
|
+
if (samples.length > 0) {
|
|
8287
|
+
const sortedSamples = [...samples].sort((a, b) => a - b);
|
|
8288
|
+
const p95Index = Math.max(0, Math.floor(sortedSamples.length * 0.95) - 1);
|
|
8289
|
+
this.metrics.latency[type].p95 = sortedSamples[p95Index] ?? latency;
|
|
8290
|
+
const p99Index = Math.max(0, Math.floor(sortedSamples.length * 0.99) - 1);
|
|
8291
|
+
this.metrics.latency[type].p99 = sortedSamples[p99Index] ?? latency;
|
|
8292
|
+
}
|
|
8293
|
+
if (this.config.shouldError) {
|
|
8294
|
+
this.metrics.errors[type].count++;
|
|
8295
|
+
this.metrics.errors[type].total++;
|
|
8296
|
+
const totalRequests = this.metrics.latency[type].samples.length;
|
|
8297
|
+
this.metrics.errors[type].rate = totalRequests > 0 ? this.metrics.errors[type].count / totalRequests : 0;
|
|
8298
|
+
}
|
|
8299
|
+
}
|
|
8300
|
+
};
|
|
8301
|
+
|
|
8115
8302
|
// dsp/router.ts
|
|
8116
8303
|
var colorLog6 = new ColorLog();
|
|
8117
8304
|
var AxRoute = class {
|
|
@@ -8381,126 +8568,6 @@ var AxEmbeddingAdapter = class {
|
|
|
8381
8568
|
}
|
|
8382
8569
|
};
|
|
8383
8570
|
|
|
8384
|
-
// ai/mock/api.ts
|
|
8385
|
-
var AxMockAIService = class {
|
|
8386
|
-
constructor(config = {}) {
|
|
8387
|
-
this.config = config;
|
|
8388
|
-
this.config.id = this.config.id ?? crypto.randomUUID();
|
|
8389
|
-
}
|
|
8390
|
-
options = {};
|
|
8391
|
-
metrics = {
|
|
8392
|
-
latency: {
|
|
8393
|
-
chat: { mean: 0, p95: 0, p99: 0, samples: [] },
|
|
8394
|
-
embed: { mean: 0, p95: 0, p99: 0, samples: [] }
|
|
8395
|
-
},
|
|
8396
|
-
errors: {
|
|
8397
|
-
chat: { count: 0, rate: 0, total: 0 },
|
|
8398
|
-
embed: { count: 0, rate: 0, total: 0 }
|
|
8399
|
-
}
|
|
8400
|
-
};
|
|
8401
|
-
getName() {
|
|
8402
|
-
return this.config.name ?? "mock-ai-service";
|
|
8403
|
-
}
|
|
8404
|
-
getId() {
|
|
8405
|
-
return this.config.id ?? "mock-ai-service-id";
|
|
8406
|
-
}
|
|
8407
|
-
getModelInfo() {
|
|
8408
|
-
return {
|
|
8409
|
-
name: "mock-model",
|
|
8410
|
-
provider: "mock-provider",
|
|
8411
|
-
promptTokenCostPer1M: 100,
|
|
8412
|
-
completionTokenCostPer1M: 100,
|
|
8413
|
-
...this.config.modelInfo
|
|
8414
|
-
};
|
|
8415
|
-
}
|
|
8416
|
-
getEmbedModelInfo() {
|
|
8417
|
-
return this.config.embedModelInfo;
|
|
8418
|
-
}
|
|
8419
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
8420
|
-
getFeatures(_model) {
|
|
8421
|
-
return {
|
|
8422
|
-
functions: this.config.features?.functions ?? false,
|
|
8423
|
-
streaming: this.config.features?.streaming ?? false
|
|
8424
|
-
};
|
|
8425
|
-
}
|
|
8426
|
-
getModelList() {
|
|
8427
|
-
return this.config.models;
|
|
8428
|
-
}
|
|
8429
|
-
getMetrics() {
|
|
8430
|
-
return this.metrics;
|
|
8431
|
-
}
|
|
8432
|
-
async chat(req, _options) {
|
|
8433
|
-
if (this.config.latencyMs) {
|
|
8434
|
-
await new Promise((resolve) => setTimeout(resolve, this.config.latencyMs));
|
|
8435
|
-
}
|
|
8436
|
-
if (this.config.shouldError) {
|
|
8437
|
-
throw new Error(this.config.errorMessage ?? "Mock chat error");
|
|
8438
|
-
}
|
|
8439
|
-
this.updateMetrics("chat");
|
|
8440
|
-
if (typeof this.config.chatResponse === "function") {
|
|
8441
|
-
return this.config.chatResponse(req);
|
|
8442
|
-
}
|
|
8443
|
-
return this.config.chatResponse ?? {
|
|
8444
|
-
results: [
|
|
8445
|
-
{
|
|
8446
|
-
content: "Mock response",
|
|
8447
|
-
finishReason: "stop"
|
|
8448
|
-
}
|
|
8449
|
-
],
|
|
8450
|
-
modelUsage: {
|
|
8451
|
-
promptTokens: 10,
|
|
8452
|
-
completionTokens: 5,
|
|
8453
|
-
totalTokens: 15
|
|
8454
|
-
}
|
|
8455
|
-
};
|
|
8456
|
-
}
|
|
8457
|
-
async embed(req, _options) {
|
|
8458
|
-
if (this.config.latencyMs) {
|
|
8459
|
-
await new Promise((resolve) => setTimeout(resolve, this.config.latencyMs));
|
|
8460
|
-
}
|
|
8461
|
-
if (this.config.shouldError) {
|
|
8462
|
-
throw new Error(this.config.errorMessage ?? "Mock embed error");
|
|
8463
|
-
}
|
|
8464
|
-
this.updateMetrics("embed");
|
|
8465
|
-
if (typeof this.config.embedResponse === "function") {
|
|
8466
|
-
return this.config.embedResponse(req);
|
|
8467
|
-
}
|
|
8468
|
-
return this.config.embedResponse ?? {
|
|
8469
|
-
embeddings: [[0.1, 0.2, 0.3]],
|
|
8470
|
-
modelUsage: {
|
|
8471
|
-
promptTokens: 5,
|
|
8472
|
-
completionTokens: 0,
|
|
8473
|
-
totalTokens: 5
|
|
8474
|
-
}
|
|
8475
|
-
};
|
|
8476
|
-
}
|
|
8477
|
-
setOptions(options) {
|
|
8478
|
-
this.options = options;
|
|
8479
|
-
}
|
|
8480
|
-
getOptions() {
|
|
8481
|
-
return this.options;
|
|
8482
|
-
}
|
|
8483
|
-
updateMetrics(type) {
|
|
8484
|
-
const latency = this.config.latencyMs ?? 0;
|
|
8485
|
-
this.metrics.latency[type].samples.push(latency);
|
|
8486
|
-
const samples = this.metrics.latency[type].samples;
|
|
8487
|
-
this.metrics.latency[type].mean = samples.reduce((a, b) => a + b, 0) / samples.length;
|
|
8488
|
-
if (samples.length > 0) {
|
|
8489
|
-
const sortedSamples = [...samples].sort((a, b) => a - b);
|
|
8490
|
-
const p95Index = Math.max(0, Math.floor(sortedSamples.length * 0.95) - 1);
|
|
8491
|
-
this.metrics.latency[type].p95 = sortedSamples[p95Index] ?? latency;
|
|
8492
|
-
const p99Index = Math.max(0, Math.floor(sortedSamples.length * 0.99) - 1);
|
|
8493
|
-
this.metrics.latency[type].p99 = sortedSamples[p99Index] ?? latency;
|
|
8494
|
-
}
|
|
8495
|
-
if (this.config.shouldError) {
|
|
8496
|
-
this.metrics.errors[type].count++;
|
|
8497
|
-
this.metrics.errors[type].total++;
|
|
8498
|
-
const totalRequests = this.metrics.latency[type].samples.length;
|
|
8499
|
-
this.metrics.errors[type].rate = totalRequests > 0 ? this.metrics.errors[type].count / totalRequests : 0;
|
|
8500
|
-
}
|
|
8501
|
-
}
|
|
8502
|
-
};
|
|
8503
|
-
|
|
8504
8571
|
// prompts/rag.ts
|
|
8505
8572
|
var AxRAG = class extends AxChainOfThought {
|
|
8506
8573
|
genQuery;
|