avantgate 2.1.0 → 2.3.0
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/README.md +10 -22
- package/dist/chunk-Z6QOMLZ2.mjs +55 -0
- package/dist/control-layer.d.ts +20 -2
- package/dist/control-layer.d.ts.map +1 -1
- package/dist/custom-terms.d.ts +8 -0
- package/dist/custom-terms.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +367 -34
- package/dist/index.mjs +358 -34
- package/dist/middleware.d.ts +15 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/presets.d.ts +13 -0
- package/dist/presets.d.ts.map +1 -0
- package/dist/secret-guard.d.ts +5 -0
- package/dist/secret-guard.d.ts.map +1 -1
- package/dist/types.d.ts +47 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/ui/index.d.ts +1 -0
- package/dist/ui/index.d.ts.map +1 -1
- package/dist/ui/index.js +65 -0
- package/dist/ui/index.mjs +6 -0
- package/dist/ui/react/index.mjs +4 -51
- package/package.json +10 -13
package/dist/index.js
CHANGED
|
@@ -39,6 +39,7 @@ __export(index_exports, {
|
|
|
39
39
|
ConfigurationError: () => ConfigurationError,
|
|
40
40
|
DEFAULT_MODEL_PRICES: () => DEFAULT_MODEL_PRICES,
|
|
41
41
|
HttpProviderClient: () => HttpProviderClient,
|
|
42
|
+
PRESETS: () => PRESETS,
|
|
42
43
|
PricingRegistry: () => PricingRegistry,
|
|
43
44
|
PromptBuilder: () => PromptBuilder,
|
|
44
45
|
PromptRegistry: () => PromptRegistry,
|
|
@@ -51,16 +52,24 @@ __export(index_exports, {
|
|
|
51
52
|
WorkflowValidationError: () => WorkflowValidationError,
|
|
52
53
|
ZenLLMControlLayer: () => AvantGateControlLayer,
|
|
53
54
|
applyOutputGuards: () => applyOutputGuards,
|
|
55
|
+
applySingleAfterMiddleware: () => applySingleAfterMiddleware,
|
|
56
|
+
applySingleBeforeMiddleware: () => applySingleBeforeMiddleware,
|
|
54
57
|
calculateCostUSD: () => calculateCostUSD,
|
|
55
58
|
createAvantGate: () => createAvantGate,
|
|
59
|
+
createDevPermissiveConfig: () => createDevPermissiveConfig,
|
|
60
|
+
createEnterpriseStrictConfig: () => createEnterpriseStrictConfig,
|
|
56
61
|
createHttpProviderClient: () => createHttpProviderClient,
|
|
57
62
|
createLLMControlLayer: () => createLLMControlLayer,
|
|
63
|
+
createLaunchSafeConfig: () => createLaunchSafeConfig,
|
|
58
64
|
defineWorkflow: () => defineWorkflow,
|
|
65
|
+
executeAfterResponseHooks: () => executeAfterResponseHooks,
|
|
66
|
+
executeBeforeRequestHooks: () => executeBeforeRequestHooks,
|
|
59
67
|
executeWorkflow: () => executeWorkflow,
|
|
60
68
|
extractAndCleanJSON: () => extractAndCleanJSON,
|
|
61
69
|
isWorkflowAbortSignal: () => isWorkflowAbortSignal,
|
|
62
70
|
resolveModelPrice: () => resolveModelPrice,
|
|
63
71
|
resolveModelPriceAsync: () => resolveModelPriceAsync,
|
|
72
|
+
sanitizeCustomTerms: () => sanitizeCustomTerms,
|
|
64
73
|
sanitizePII: () => sanitizePII,
|
|
65
74
|
sanitizeSecrets: () => sanitizeSecrets,
|
|
66
75
|
validateUserInput: () => validateUserInput,
|
|
@@ -1172,6 +1181,70 @@ var createHttpProviderClient = (config) => {
|
|
|
1172
1181
|
return new HttpProviderClient(config);
|
|
1173
1182
|
};
|
|
1174
1183
|
|
|
1184
|
+
// src/custom-terms.ts
|
|
1185
|
+
var DEFAULT_MASKS = {
|
|
1186
|
+
PROJECT: "[REDACTED_PROJECT]",
|
|
1187
|
+
COMPANY: "[REDACTED_COMPANY]",
|
|
1188
|
+
INFRA: "[REDACTED_INFRA]",
|
|
1189
|
+
CUSTOM: "[REDACTED_CUSTOM]"
|
|
1190
|
+
};
|
|
1191
|
+
var escapeRegex = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1192
|
+
var resolveTermDefinition = (entry) => {
|
|
1193
|
+
if (typeof entry === "string") {
|
|
1194
|
+
return {
|
|
1195
|
+
term: entry.trim(),
|
|
1196
|
+
mask: DEFAULT_MASKS.CUSTOM,
|
|
1197
|
+
category: "CUSTOM",
|
|
1198
|
+
caseSensitive: false
|
|
1199
|
+
};
|
|
1200
|
+
}
|
|
1201
|
+
const category = entry.category ?? "CUSTOM";
|
|
1202
|
+
return {
|
|
1203
|
+
term: entry.term.trim(),
|
|
1204
|
+
mask: entry.mask ?? (DEFAULT_MASKS[category] ?? DEFAULT_MASKS.CUSTOM),
|
|
1205
|
+
category,
|
|
1206
|
+
caseSensitive: Boolean(entry.caseSensitive)
|
|
1207
|
+
};
|
|
1208
|
+
};
|
|
1209
|
+
var buildTermRegex = (def) => {
|
|
1210
|
+
const escaped = escapeRegex(def.term);
|
|
1211
|
+
const leadingBoundary = /^\w/.test(def.term) ? "\\b" : "";
|
|
1212
|
+
const trailingBoundary = /\w$/.test(def.term) ? "\\b" : "";
|
|
1213
|
+
return new RegExp(`${leadingBoundary}${escaped}${trailingBoundary}`, def.caseSensitive ? "g" : "gi");
|
|
1214
|
+
};
|
|
1215
|
+
var replaceTermsInText = (initialText, definitions) => {
|
|
1216
|
+
let currentText = initialText;
|
|
1217
|
+
let totalMaskedCount = 0;
|
|
1218
|
+
const matchedTermsSet = /* @__PURE__ */ new Set();
|
|
1219
|
+
for (const item of definitions) {
|
|
1220
|
+
const regex = buildTermRegex(item);
|
|
1221
|
+
let matchCount = 0;
|
|
1222
|
+
currentText = currentText.replace(regex, () => {
|
|
1223
|
+
matchCount++;
|
|
1224
|
+
return item.mask;
|
|
1225
|
+
});
|
|
1226
|
+
if (matchCount > 0) {
|
|
1227
|
+
totalMaskedCount += matchCount;
|
|
1228
|
+
matchedTermsSet.add(item.term);
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
return {
|
|
1232
|
+
text: currentText,
|
|
1233
|
+
maskedCount: totalMaskedCount,
|
|
1234
|
+
matchedTerms: Array.from(matchedTermsSet)
|
|
1235
|
+
};
|
|
1236
|
+
};
|
|
1237
|
+
var sanitizeCustomTerms = (input, terms) => {
|
|
1238
|
+
if (!input || !terms || terms.length === 0) {
|
|
1239
|
+
return { text: input, maskedCount: 0, matchedTerms: [] };
|
|
1240
|
+
}
|
|
1241
|
+
const normalized = terms.map(resolveTermDefinition).filter((t) => t.term.length > 0).sort((a, b) => b.term.length - a.term.length);
|
|
1242
|
+
if (normalized.length === 0) {
|
|
1243
|
+
return { text: input, maskedCount: 0, matchedTerms: [] };
|
|
1244
|
+
}
|
|
1245
|
+
return replaceTermsInText(input, normalized);
|
|
1246
|
+
};
|
|
1247
|
+
|
|
1175
1248
|
// src/secret-guard.ts
|
|
1176
1249
|
var SECRET_PATTERNS = [
|
|
1177
1250
|
// 1. Clés privées (RSA, OpenSSH, EC, DSA)
|
|
@@ -1252,6 +1325,7 @@ var sanitizeSecrets = (input) => {
|
|
|
1252
1325
|
var applyOutputGuards = (rawOutput, options) => {
|
|
1253
1326
|
let currentText = rawOutput;
|
|
1254
1327
|
let piiCount = 0;
|
|
1328
|
+
let customTermsCount = 0;
|
|
1255
1329
|
if (options?.maskPII) {
|
|
1256
1330
|
const piiResult = sanitizePII(currentText);
|
|
1257
1331
|
currentText = piiResult.text;
|
|
@@ -1259,6 +1333,11 @@ var applyOutputGuards = (rawOutput, options) => {
|
|
|
1259
1333
|
}
|
|
1260
1334
|
const secretResult = sanitizeSecrets(currentText);
|
|
1261
1335
|
currentText = secretResult.text;
|
|
1336
|
+
if (options?.customRedactionTerms && options.customRedactionTerms.length > 0) {
|
|
1337
|
+
const customResult = sanitizeCustomTerms(currentText, options.customRedactionTerms);
|
|
1338
|
+
currentText = customResult.text;
|
|
1339
|
+
customTermsCount = customResult.maskedCount;
|
|
1340
|
+
}
|
|
1262
1341
|
if (secretResult.hasSecrets && options?.secretLeakAction === "BLOCK") {
|
|
1263
1342
|
const types = secretResult.detections.map((d) => d.type).join(", ");
|
|
1264
1343
|
throw new SecretLeakBlockedError(
|
|
@@ -1268,13 +1347,71 @@ var applyOutputGuards = (rawOutput, options) => {
|
|
|
1268
1347
|
}
|
|
1269
1348
|
return {
|
|
1270
1349
|
text: currentText,
|
|
1271
|
-
sanitized: piiCount > 0 || secretResult.hasSecrets,
|
|
1350
|
+
sanitized: piiCount > 0 || secretResult.hasSecrets || customTermsCount > 0,
|
|
1272
1351
|
secretDetected: secretResult.hasSecrets,
|
|
1273
1352
|
piiMaskedCount: piiCount,
|
|
1353
|
+
customTermsMaskedCount: customTermsCount,
|
|
1274
1354
|
detections: secretResult.detections
|
|
1275
1355
|
};
|
|
1276
1356
|
};
|
|
1277
1357
|
|
|
1358
|
+
// src/middleware.ts
|
|
1359
|
+
var applySingleBeforeMiddleware = async (middleware, current, model, temperature) => {
|
|
1360
|
+
if (!middleware.beforeRequest) return current;
|
|
1361
|
+
const result = await middleware.beforeRequest({
|
|
1362
|
+
messages: current.messages,
|
|
1363
|
+
model,
|
|
1364
|
+
temperature,
|
|
1365
|
+
metadata: current.metadata
|
|
1366
|
+
});
|
|
1367
|
+
if (!result) return current;
|
|
1368
|
+
return {
|
|
1369
|
+
messages: result.messages ?? current.messages,
|
|
1370
|
+
shortCircuit: result.shortCircuit,
|
|
1371
|
+
metadata: current.metadata
|
|
1372
|
+
};
|
|
1373
|
+
};
|
|
1374
|
+
var executeBeforeRequestHooks = async (middlewares, context) => {
|
|
1375
|
+
if (!middlewares || middlewares.length === 0) {
|
|
1376
|
+
return { messages: context.messages, metadata: context.metadata };
|
|
1377
|
+
}
|
|
1378
|
+
let current = {
|
|
1379
|
+
messages: context.messages,
|
|
1380
|
+
metadata: context.metadata
|
|
1381
|
+
};
|
|
1382
|
+
for (const middleware of middlewares) {
|
|
1383
|
+
current = await applySingleBeforeMiddleware(middleware, current, context.model, context.temperature);
|
|
1384
|
+
if (current.shortCircuit) return current;
|
|
1385
|
+
}
|
|
1386
|
+
return current;
|
|
1387
|
+
};
|
|
1388
|
+
var applySingleAfterMiddleware = async (middleware, current, baseContext) => {
|
|
1389
|
+
if (!middleware.afterResponse) return current;
|
|
1390
|
+
const result = await middleware.afterResponse({
|
|
1391
|
+
...baseContext,
|
|
1392
|
+
responseText: current.responseText,
|
|
1393
|
+
metadata: current.metadata
|
|
1394
|
+
});
|
|
1395
|
+
if (!result) return current;
|
|
1396
|
+
return {
|
|
1397
|
+
responseText: result.responseText ?? current.responseText,
|
|
1398
|
+
metadata: result.metadata ?? current.metadata
|
|
1399
|
+
};
|
|
1400
|
+
};
|
|
1401
|
+
var executeAfterResponseHooks = async (middlewares, context) => {
|
|
1402
|
+
if (!middlewares || middlewares.length === 0) {
|
|
1403
|
+
return { responseText: context.responseText, metadata: context.metadata };
|
|
1404
|
+
}
|
|
1405
|
+
let current = {
|
|
1406
|
+
responseText: context.responseText,
|
|
1407
|
+
metadata: context.metadata
|
|
1408
|
+
};
|
|
1409
|
+
for (const middleware of middlewares) {
|
|
1410
|
+
current = await applySingleAfterMiddleware(middleware, current, context);
|
|
1411
|
+
}
|
|
1412
|
+
return current;
|
|
1413
|
+
};
|
|
1414
|
+
|
|
1278
1415
|
// src/control-layer.ts
|
|
1279
1416
|
var AvantGateControlLayer = class {
|
|
1280
1417
|
config;
|
|
@@ -1335,21 +1472,27 @@ var AvantGateControlLayer = class {
|
|
|
1335
1472
|
if (!guard.valid) {
|
|
1336
1473
|
throw new Error(`[AvantGate Security Guard] Request blocked: ${guard.blockedReason}`);
|
|
1337
1474
|
}
|
|
1475
|
+
let processed = userQuery;
|
|
1476
|
+
if (this.config.security?.customRedactionTerms && this.config.security.customRedactionTerms.length > 0) {
|
|
1477
|
+
processed = sanitizeCustomTerms(processed, this.config.security.customRedactionTerms).text;
|
|
1478
|
+
}
|
|
1338
1479
|
if (this.config.security?.maskPII) {
|
|
1339
|
-
return sanitizePII(
|
|
1480
|
+
return sanitizePII(processed).text;
|
|
1340
1481
|
}
|
|
1341
|
-
return
|
|
1482
|
+
return processed;
|
|
1342
1483
|
};
|
|
1343
1484
|
applyOutputSecurityGuards = (rawOutput) => {
|
|
1344
1485
|
const isDlpEnabled = this.config.security?.outputDLP ?? this.config.security?.maskPII ?? false;
|
|
1345
1486
|
const shouldCheckSecrets = this.config.security?.blockSecretLeaks ?? true;
|
|
1346
|
-
|
|
1487
|
+
const customTerms = this.config.security?.customRedactionTerms;
|
|
1488
|
+
if (!isDlpEnabled && !shouldCheckSecrets && (!customTerms || customTerms.length === 0)) {
|
|
1347
1489
|
return rawOutput;
|
|
1348
1490
|
}
|
|
1349
1491
|
const guardResult = applyOutputGuards(rawOutput, {
|
|
1350
1492
|
maskPII: isDlpEnabled,
|
|
1351
1493
|
blockSecretLeaks: shouldCheckSecrets,
|
|
1352
|
-
secretLeakAction: this.config.security?.secretLeakAction ?? "REDACT"
|
|
1494
|
+
secretLeakAction: this.config.security?.secretLeakAction ?? "REDACT",
|
|
1495
|
+
customRedactionTerms: customTerms
|
|
1353
1496
|
});
|
|
1354
1497
|
return guardResult.text;
|
|
1355
1498
|
};
|
|
@@ -1512,28 +1655,103 @@ var AvantGateControlLayer = class {
|
|
|
1512
1655
|
attempts: result.attempts
|
|
1513
1656
|
});
|
|
1514
1657
|
};
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1658
|
+
prepareInputMessages = (options) => {
|
|
1659
|
+
if (options.messages && options.messages.length > 0) {
|
|
1660
|
+
return this.sanitizeIncomingMessages(options.messages);
|
|
1661
|
+
}
|
|
1662
|
+
if (options.userQuery !== void 0) {
|
|
1663
|
+
const sanitizedQuery = this.applySecurityGuards(options.userQuery);
|
|
1664
|
+
return this.buildMessages(options.systemPrompt, sanitizedQuery);
|
|
1665
|
+
}
|
|
1666
|
+
throw new ConfigurationError("[AvantGate Input Guard] Either 'userQuery' or 'messages' must be provided.");
|
|
1667
|
+
};
|
|
1668
|
+
handleShortCircuit = async (shortCircuit, targetModel) => {
|
|
1669
|
+
const sanitizedText = this.applyOutputSecurityGuards(shortCircuit.text);
|
|
1670
|
+
const result = {
|
|
1671
|
+
text: sanitizedText,
|
|
1672
|
+
tokens: {
|
|
1673
|
+
prompt: shortCircuit.tokens?.promptTokens ?? 0,
|
|
1674
|
+
completion: shortCircuit.tokens?.completionTokens ?? 0,
|
|
1675
|
+
total: shortCircuit.tokens?.totalTokens ?? 0
|
|
1676
|
+
},
|
|
1677
|
+
costUSD: shortCircuit.costUSD ?? 0,
|
|
1678
|
+
modelUsed: `${targetModel}:shortcircuit`,
|
|
1679
|
+
failoverOccurred: false,
|
|
1680
|
+
attempts: 0
|
|
1681
|
+
};
|
|
1682
|
+
await this.notifyAuditSink(result);
|
|
1683
|
+
return result;
|
|
1684
|
+
};
|
|
1685
|
+
dispatchPromptExecution = async (messages, temperature, providerOverride) => {
|
|
1686
|
+
const query = messages[messages.length - 1]?.content ?? "";
|
|
1687
|
+
if (providerOverride) {
|
|
1688
|
+
return this.executeOverrideProvider(providerOverride, messages, query, temperature);
|
|
1689
|
+
}
|
|
1690
|
+
if (this.getProviderChain().length === 0) {
|
|
1522
1691
|
if (this.config.mockSimulation) {
|
|
1523
|
-
const
|
|
1524
|
-
|
|
1525
|
-
|
|
1692
|
+
const sim = this.executeSimulation(query);
|
|
1693
|
+
return {
|
|
1694
|
+
responseText: sim.text,
|
|
1695
|
+
usage: { promptTokens: sim.tokens.prompt, completionTokens: sim.tokens.completion, totalTokens: sim.tokens.total },
|
|
1696
|
+
modelUsed: sim.modelUsed,
|
|
1697
|
+
failoverOccurred: false,
|
|
1698
|
+
attempts: 1
|
|
1699
|
+
};
|
|
1526
1700
|
}
|
|
1527
1701
|
throw new ConfigurationError(
|
|
1528
1702
|
"[AvantGate Configuration Error] No active LLM provider configured. Provide a client implementing LLMProviderPort or configure credentials (apiKey / baseUrl)."
|
|
1529
1703
|
);
|
|
1530
1704
|
}
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1705
|
+
return this.executeProviderPipeline(messages, query, temperature);
|
|
1706
|
+
};
|
|
1707
|
+
finalizeExecutionResult = async (output, responseText) => {
|
|
1708
|
+
const sanitizedText = this.applyOutputSecurityGuards(responseText);
|
|
1709
|
+
const hitTokens = output.usage.promptCacheHitTokens ?? 0;
|
|
1710
|
+
const costUSD = this.calculateCost(output.modelUsed, output.usage.promptTokens, output.usage.completionTokens, hitTokens);
|
|
1711
|
+
this.checkPostExecutionBudget(output.usage.totalTokens, costUSD);
|
|
1712
|
+
const tokens = { prompt: output.usage.promptTokens, completion: output.usage.completionTokens, total: output.usage.totalTokens };
|
|
1713
|
+
const result = {
|
|
1714
|
+
text: sanitizedText,
|
|
1715
|
+
tokens,
|
|
1716
|
+
costUSD,
|
|
1717
|
+
modelUsed: output.modelUsed,
|
|
1718
|
+
failoverOccurred: output.failoverOccurred,
|
|
1719
|
+
attempts: output.attempts
|
|
1720
|
+
};
|
|
1534
1721
|
await this.notifyAuditSink(result);
|
|
1535
1722
|
return result;
|
|
1536
1723
|
};
|
|
1724
|
+
runAfterHooks = async (beforeState, output) => {
|
|
1725
|
+
const tokens = { prompt: output.usage.promptTokens, completion: output.usage.completionTokens, total: output.usage.totalTokens };
|
|
1726
|
+
const costUSD = this.calculateCost(output.modelUsed, output.usage.promptTokens, output.usage.completionTokens);
|
|
1727
|
+
return executeAfterResponseHooks(this.config.middlewares, {
|
|
1728
|
+
messages: beforeState.messages,
|
|
1729
|
+
responseText: output.responseText,
|
|
1730
|
+
modelUsed: output.modelUsed,
|
|
1731
|
+
tokens,
|
|
1732
|
+
costUSD,
|
|
1733
|
+
attempts: output.attempts,
|
|
1734
|
+
failoverOccurred: output.failoverOccurred,
|
|
1735
|
+
metadata: beforeState.metadata
|
|
1736
|
+
});
|
|
1737
|
+
};
|
|
1738
|
+
execute = async (options) => {
|
|
1739
|
+
const inputMessages = this.prepareInputMessages(options);
|
|
1740
|
+
const beforeState = await executeBeforeRequestHooks(this.config.middlewares, {
|
|
1741
|
+
messages: inputMessages,
|
|
1742
|
+
model: this.config.primary.model,
|
|
1743
|
+
temperature: options.temperature,
|
|
1744
|
+
metadata: options.metadata
|
|
1745
|
+
});
|
|
1746
|
+
if (beforeState.shortCircuit) {
|
|
1747
|
+
return this.handleShortCircuit(beforeState.shortCircuit, this.config.primary.model);
|
|
1748
|
+
}
|
|
1749
|
+
await this.verifyPromptBudget(beforeState.messages, this.config.primary.model);
|
|
1750
|
+
const output = await this.dispatchPromptExecution(beforeState.messages, options.temperature, options.providerOverride);
|
|
1751
|
+
const afterState = await this.runAfterHooks(beforeState, output);
|
|
1752
|
+
return this.finalizeExecutionResult(output, afterState.responseText);
|
|
1753
|
+
};
|
|
1754
|
+
executePrompt = this.execute;
|
|
1537
1755
|
executeStructured = async (options) => {
|
|
1538
1756
|
const rawResult = await this.execute({
|
|
1539
1757
|
userQuery: options.userQuery,
|
|
@@ -1668,28 +1886,56 @@ var AvantGateControlLayer = class {
|
|
|
1668
1886
|
const delay = initialDelay * Math.pow(factor, attempt);
|
|
1669
1887
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
1670
1888
|
};
|
|
1671
|
-
|
|
1672
|
-
const
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1889
|
+
handleStructuredShortCircuit = async (shortCircuit, options, targetModel) => {
|
|
1890
|
+
const { parsedData, sanitizedResponse } = this.parseAndValidateStructuredResult(
|
|
1891
|
+
shortCircuit.text,
|
|
1892
|
+
options
|
|
1893
|
+
);
|
|
1894
|
+
const tokens = {
|
|
1895
|
+
prompt: shortCircuit.tokens?.promptTokens ?? 0,
|
|
1896
|
+
completion: shortCircuit.tokens?.completionTokens ?? 0,
|
|
1897
|
+
total: shortCircuit.tokens?.totalTokens ?? 0
|
|
1898
|
+
};
|
|
1899
|
+
const result = {
|
|
1900
|
+
data: parsedData,
|
|
1901
|
+
rawText: sanitizedResponse,
|
|
1902
|
+
tokens,
|
|
1903
|
+
costUSD: shortCircuit.costUSD ?? 0,
|
|
1904
|
+
modelUsed: `${targetModel}:shortcircuit`,
|
|
1905
|
+
failoverOccurred: false
|
|
1906
|
+
};
|
|
1907
|
+
await this.notifyAuditSinkSafe(sanitizedResponse, result, 0);
|
|
1908
|
+
return result;
|
|
1909
|
+
};
|
|
1910
|
+
runStructuredAfterHooks = async (messages, output, attempt) => {
|
|
1911
|
+
const tokens = { prompt: output.promptTokens, completion: output.completionTokens, total: output.promptTokens + output.completionTokens };
|
|
1912
|
+
const costUSD = this.calculateCost(output.modelUsed, output.promptTokens, output.completionTokens);
|
|
1913
|
+
return executeAfterResponseHooks(this.config.middlewares, {
|
|
1914
|
+
messages,
|
|
1915
|
+
responseText: output.responseText,
|
|
1916
|
+
modelUsed: output.modelUsed,
|
|
1917
|
+
tokens,
|
|
1918
|
+
costUSD,
|
|
1919
|
+
attempts: attempt,
|
|
1920
|
+
failoverOccurred: output.failoverOccurred
|
|
1921
|
+
});
|
|
1922
|
+
};
|
|
1923
|
+
executeStructuredLoop = async (options, messages, targetModel, maxRetries) => {
|
|
1676
1924
|
let lastError;
|
|
1677
|
-
let
|
|
1678
|
-
let
|
|
1925
|
+
let accPrompt = 0;
|
|
1926
|
+
let accComp = 0;
|
|
1679
1927
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
1680
1928
|
try {
|
|
1681
|
-
const output = await this.dispatchProviderAttempt(options,
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
const
|
|
1685
|
-
|
|
1686
|
-
options
|
|
1687
|
-
);
|
|
1929
|
+
const output = await this.dispatchProviderAttempt(options, messages, targetModel);
|
|
1930
|
+
accPrompt += output.promptTokens;
|
|
1931
|
+
accComp += output.completionTokens;
|
|
1932
|
+
const afterState = await this.runStructuredAfterHooks(messages, output, attempt + 1);
|
|
1933
|
+
const { parsedData, sanitizedResponse } = this.parseAndValidateStructuredResult(afterState.responseText, options);
|
|
1688
1934
|
const result = this.buildStructuredResult({
|
|
1689
1935
|
data: parsedData,
|
|
1690
1936
|
rawText: sanitizedResponse,
|
|
1691
|
-
promptTokens:
|
|
1692
|
-
completionTokens:
|
|
1937
|
+
promptTokens: accPrompt,
|
|
1938
|
+
completionTokens: accComp,
|
|
1693
1939
|
modelUsed: output.modelUsed,
|
|
1694
1940
|
failoverOccurred: output.failoverOccurred
|
|
1695
1941
|
});
|
|
@@ -1702,6 +1948,21 @@ var AvantGateControlLayer = class {
|
|
|
1702
1948
|
}
|
|
1703
1949
|
throw lastError;
|
|
1704
1950
|
};
|
|
1951
|
+
generateStructuredOutput = async (options) => {
|
|
1952
|
+
const maxRetries = options.maxRetries ?? this.config.retryOptions?.maxRetries ?? 2;
|
|
1953
|
+
const targetModel = options.model ?? this.config.primary.model;
|
|
1954
|
+
const processedMessages = this.sanitizeIncomingMessages(options.messages);
|
|
1955
|
+
const beforeState = await executeBeforeRequestHooks(this.config.middlewares, {
|
|
1956
|
+
messages: processedMessages,
|
|
1957
|
+
model: targetModel,
|
|
1958
|
+
temperature: options.temperature
|
|
1959
|
+
});
|
|
1960
|
+
if (beforeState.shortCircuit) {
|
|
1961
|
+
return this.handleStructuredShortCircuit(beforeState.shortCircuit, options, targetModel);
|
|
1962
|
+
}
|
|
1963
|
+
await this.verifyPromptBudget(beforeState.messages, targetModel);
|
|
1964
|
+
return this.executeStructuredLoop(options, beforeState.messages, targetModel, maxRetries);
|
|
1965
|
+
};
|
|
1705
1966
|
};
|
|
1706
1967
|
var createLLMControlLayer = (config) => {
|
|
1707
1968
|
return new AvantGateControlLayer(config);
|
|
@@ -2609,6 +2870,69 @@ var defineWorkflow = (config) => {
|
|
|
2609
2870
|
}
|
|
2610
2871
|
};
|
|
2611
2872
|
};
|
|
2873
|
+
|
|
2874
|
+
// src/presets.ts
|
|
2875
|
+
var createLaunchSafeConfig = (options) => {
|
|
2876
|
+
const defaultSecurity = {
|
|
2877
|
+
detectPromptInjection: true,
|
|
2878
|
+
maskPII: true,
|
|
2879
|
+
outputDLP: true,
|
|
2880
|
+
blockSecretLeaks: true,
|
|
2881
|
+
secretLeakAction: "REDACT"
|
|
2882
|
+
};
|
|
2883
|
+
const defaultRetries = { maxRetries: 2, initialDelayMs: 300, backoffFactor: 1.5 };
|
|
2884
|
+
return {
|
|
2885
|
+
maxTokenBudget: 8192,
|
|
2886
|
+
maxCostUSD: 0.5,
|
|
2887
|
+
...options,
|
|
2888
|
+
retryOptions: { ...defaultRetries, ...options.retryOptions },
|
|
2889
|
+
security: { ...defaultSecurity, ...options.security }
|
|
2890
|
+
};
|
|
2891
|
+
};
|
|
2892
|
+
var createEnterpriseStrictConfig = (options) => {
|
|
2893
|
+
const defaultSecurity = {
|
|
2894
|
+
detectPromptInjection: true,
|
|
2895
|
+
maskPII: true,
|
|
2896
|
+
outputDLP: true,
|
|
2897
|
+
blockSecretLeaks: true,
|
|
2898
|
+
secretLeakAction: "BLOCK"
|
|
2899
|
+
};
|
|
2900
|
+
const defaultRetries = {
|
|
2901
|
+
maxRetries: 3,
|
|
2902
|
+
initialDelayMs: 500,
|
|
2903
|
+
backoffFactor: 2
|
|
2904
|
+
};
|
|
2905
|
+
return {
|
|
2906
|
+
maxTokenBudget: 4096,
|
|
2907
|
+
maxCostUSD: 0.15,
|
|
2908
|
+
...options,
|
|
2909
|
+
retryOptions: { ...defaultRetries, ...options.retryOptions },
|
|
2910
|
+
security: { ...defaultSecurity, ...options.security }
|
|
2911
|
+
};
|
|
2912
|
+
};
|
|
2913
|
+
var createDevPermissiveConfig = (options) => {
|
|
2914
|
+
const defaultSecurity = {
|
|
2915
|
+
detectPromptInjection: false,
|
|
2916
|
+
maskPII: false,
|
|
2917
|
+
outputDLP: false,
|
|
2918
|
+
blockSecretLeaks: false
|
|
2919
|
+
};
|
|
2920
|
+
const defaultRetries = {
|
|
2921
|
+
maxRetries: 1,
|
|
2922
|
+
initialDelayMs: 200,
|
|
2923
|
+
backoffFactor: 1
|
|
2924
|
+
};
|
|
2925
|
+
return {
|
|
2926
|
+
...options,
|
|
2927
|
+
retryOptions: { ...defaultRetries, ...options.retryOptions },
|
|
2928
|
+
security: { ...defaultSecurity, ...options.security }
|
|
2929
|
+
};
|
|
2930
|
+
};
|
|
2931
|
+
var PRESETS = {
|
|
2932
|
+
LAUNCH_SAFE: createLaunchSafeConfig,
|
|
2933
|
+
ENTERPRISE_STRICT: createEnterpriseStrictConfig,
|
|
2934
|
+
DEV_PERMISSIVE: createDevPermissiveConfig
|
|
2935
|
+
};
|
|
2612
2936
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2613
2937
|
0 && (module.exports = {
|
|
2614
2938
|
AvantGateBudgetExceededError,
|
|
@@ -2620,6 +2944,7 @@ var defineWorkflow = (config) => {
|
|
|
2620
2944
|
ConfigurationError,
|
|
2621
2945
|
DEFAULT_MODEL_PRICES,
|
|
2622
2946
|
HttpProviderClient,
|
|
2947
|
+
PRESETS,
|
|
2623
2948
|
PricingRegistry,
|
|
2624
2949
|
PromptBuilder,
|
|
2625
2950
|
PromptRegistry,
|
|
@@ -2632,16 +2957,24 @@ var defineWorkflow = (config) => {
|
|
|
2632
2957
|
WorkflowValidationError,
|
|
2633
2958
|
ZenLLMControlLayer,
|
|
2634
2959
|
applyOutputGuards,
|
|
2960
|
+
applySingleAfterMiddleware,
|
|
2961
|
+
applySingleBeforeMiddleware,
|
|
2635
2962
|
calculateCostUSD,
|
|
2636
2963
|
createAvantGate,
|
|
2964
|
+
createDevPermissiveConfig,
|
|
2965
|
+
createEnterpriseStrictConfig,
|
|
2637
2966
|
createHttpProviderClient,
|
|
2638
2967
|
createLLMControlLayer,
|
|
2968
|
+
createLaunchSafeConfig,
|
|
2639
2969
|
defineWorkflow,
|
|
2970
|
+
executeAfterResponseHooks,
|
|
2971
|
+
executeBeforeRequestHooks,
|
|
2640
2972
|
executeWorkflow,
|
|
2641
2973
|
extractAndCleanJSON,
|
|
2642
2974
|
isWorkflowAbortSignal,
|
|
2643
2975
|
resolveModelPrice,
|
|
2644
2976
|
resolveModelPriceAsync,
|
|
2977
|
+
sanitizeCustomTerms,
|
|
2645
2978
|
sanitizePII,
|
|
2646
2979
|
sanitizeSecrets,
|
|
2647
2980
|
validateUserInput,
|