avantgate 1.8.0 → 1.9.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/dist/agent/telemetry/types.d.ts +8 -0
- package/dist/agent/telemetry/types.d.ts.map +1 -1
- package/dist/client/client.d.ts +4 -4
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/index.d.ts +7 -3
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +224 -15
- package/dist/client/index.mjs +216 -13
- package/dist/client/task-runner.d.ts +13 -0
- package/dist/client/task-runner.d.ts.map +1 -0
- package/dist/client/task-types.d.ts +45 -0
- package/dist/client/task-types.d.ts.map +1 -0
- package/dist/client/task-utils.d.ts +29 -0
- package/dist/client/task-utils.d.ts.map +1 -0
- package/dist/client/useTask.d.ts +6 -0
- package/dist/client/useTask.d.ts.map +1 -0
- package/dist/client/useTelemetry.d.ts +10 -0
- package/dist/client/useTelemetry.d.ts.map +1 -0
- package/dist/control-layer.d.ts +10 -10
- package/dist/control-layer.d.ts.map +1 -1
- package/dist/index.js +128 -93
- package/dist/index.mjs +128 -93
- package/package.json +4 -3
- package/dist/client/useAvantGateTelemetry.d.ts +0 -14
- package/dist/client/useAvantGateTelemetry.d.ts.map +0 -1
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context provided to task handlers during execution.
|
|
3
|
+
*/
|
|
4
|
+
export interface TaskExecutionContext {
|
|
5
|
+
parentRunId?: string;
|
|
6
|
+
attempt: number;
|
|
7
|
+
taskId: string;
|
|
8
|
+
tenantId?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Configuration options for the useTask hook.
|
|
12
|
+
*/
|
|
13
|
+
export interface UseTaskOptions<TInput, TOutput> {
|
|
14
|
+
taskName: string;
|
|
15
|
+
tenantId?: string;
|
|
16
|
+
tenantSalt?: string;
|
|
17
|
+
maxManualRetries?: number;
|
|
18
|
+
handler: (input: TInput, context: TaskExecutionContext) => Promise<TOutput>;
|
|
19
|
+
onSuccess?: (data: TOutput) => void;
|
|
20
|
+
onError?: (error: Error) => void;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* State snapshot of the task lifecycle.
|
|
24
|
+
*/
|
|
25
|
+
export interface TaskState<TOutput> {
|
|
26
|
+
isRunning: boolean;
|
|
27
|
+
isFailed: boolean;
|
|
28
|
+
isSuccess: boolean;
|
|
29
|
+
attempts: number;
|
|
30
|
+
error: Error | null;
|
|
31
|
+
data: TOutput | null;
|
|
32
|
+
runId: string | null;
|
|
33
|
+
parentRunId: string | null;
|
|
34
|
+
taskId: string;
|
|
35
|
+
tenantId: string | null;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Return interface of useTask.
|
|
39
|
+
*/
|
|
40
|
+
export interface UseTaskReturn<TInput, TOutput> extends TaskState<TOutput> {
|
|
41
|
+
run: (input: TInput) => Promise<TOutput>;
|
|
42
|
+
retry: () => Promise<TOutput>;
|
|
43
|
+
reset: () => void;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=task-types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"task-types.d.ts","sourceRoot":"","sources":["../../src/client/task-types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc,CAAC,MAAM,EAAE,OAAO;IAC7C,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,oBAAoB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAC5E,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC;IACpC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CAClC;AAED;;GAEG;AACH,MAAM,WAAW,SAAS,CAAC,OAAO;IAChC,SAAS,EAAE,OAAO,CAAC;IACnB,QAAQ,EAAE,OAAO,CAAC;IAClB,SAAS,EAAE,OAAO,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,IAAI,EAAE,OAAO,GAAG,IAAI,CAAC;IACrB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,MAAM,EAAE,OAAO,CAAE,SAAQ,SAAS,CAAC,OAAO,CAAC;IACxE,GAAG,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IACzC,KAAK,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9B,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { TaskState } from "./task-types";
|
|
2
|
+
/**
|
|
3
|
+
* Derives an opaque task identifier using HMAC-SHA256 with tenant salt.
|
|
4
|
+
* Why: Guarantees Zero-Knowledge Telemetry by obfuscating confidential business operations
|
|
5
|
+
* (e.g. "wire_transfer", "patient_biopsy") in logs while preserving deterministic correlation.
|
|
6
|
+
*/
|
|
7
|
+
export declare const deriveOpaqueTaskId: (taskName: string, tenantSalt?: string) => string;
|
|
8
|
+
/**
|
|
9
|
+
* Generates a scoped unique run ID for a task attempt.
|
|
10
|
+
* Why: Provides deterministic prefixing for APM tracing and Cockpit session linking.
|
|
11
|
+
*/
|
|
12
|
+
export declare const generateRunId: (taskId: string) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Creates the initial default state for a task.
|
|
15
|
+
* Why: Reusable reset baseline ensuring consistency across mount and reset cycles.
|
|
16
|
+
*/
|
|
17
|
+
export declare const createInitialTaskState: <TOutput>(taskId: string, tenantId?: string) => TaskState<TOutput>;
|
|
18
|
+
export interface RetryValidationParams {
|
|
19
|
+
isRunning: boolean;
|
|
20
|
+
attemptCount: number;
|
|
21
|
+
maxRetries: number;
|
|
22
|
+
hasPreviousInput: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Validates whether a retry attempt is permitted.
|
|
26
|
+
* Why: Protects against FinOps token budget exhaustion and UI double-click race conditions.
|
|
27
|
+
*/
|
|
28
|
+
export declare const validateRetryEligibility: ({ isRunning, attemptCount, maxRetries, hasPreviousInput, }: RetryValidationParams) => void;
|
|
29
|
+
//# sourceMappingURL=task-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"task-utils.d.ts","sourceRoot":"","sources":["../../src/client/task-utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,GAAI,UAAU,MAAM,EAAE,aAAa,MAAM,KAAG,MAS1E,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,aAAa,GAAI,QAAQ,MAAM,KAAG,MAI9C,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,sBAAsB,GAAI,OAAO,EAC5C,QAAQ,MAAM,EACd,WAAW,MAAM,KAChB,SAAS,CAAC,OAAO,CAWlB,CAAC;AAEH,MAAM,WAAW,qBAAqB;IACpC,SAAS,EAAE,OAAO,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,gBAAgB,EAAE,OAAO,CAAC;CAC3B;AAED;;;GAGG;AACH,eAAO,MAAM,wBAAwB,GAAI,4DAKtC,qBAAqB,KAAG,IAU1B,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { TaskState, TaskExecutionContext, UseTaskOptions, UseTaskReturn } from "./task-types";
|
|
2
|
+
import { deriveOpaqueTaskId } from "./task-utils";
|
|
3
|
+
import { createTaskRunner, type TaskRunner, type TaskRunnerOptions } from "./task-runner";
|
|
4
|
+
export { deriveOpaqueTaskId, createTaskRunner, type TaskState, type TaskExecutionContext, type UseTaskOptions, type UseTaskReturn, type TaskRunner, type TaskRunnerOptions, };
|
|
5
|
+
export declare const useTask: <TInput, TOutput>(options: UseTaskOptions<TInput, TOutput>) => UseTaskReturn<TInput, TOutput>;
|
|
6
|
+
//# sourceMappingURL=useTask.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTask.d.ts","sourceRoot":"","sources":["../../src/client/useTask.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,oBAAoB,EACpB,cAAc,EACd,aAAa,EACd,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EACL,gBAAgB,EAChB,KAAK,UAAU,EACf,KAAK,iBAAiB,EACvB,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,KAAK,SAAS,EACd,KAAK,oBAAoB,EACzB,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,iBAAiB,GACvB,CAAC;AAEF,eAAO,MAAM,OAAO,GAAI,MAAM,EAAE,OAAO,EACrC,SAAS,cAAc,CAAC,MAAM,EAAE,OAAO,CAAC,KACvC,aAAa,CAAC,MAAM,EAAE,OAAO,CAgD/B,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type TrackDataRenderedOptions, type TrackFeedbackOptions, type TrackSecurityAlertDetails } from "./client";
|
|
2
|
+
import type { BrowserTelemetryExporterConfig, ClientSecurityAlertEvent } from "./types";
|
|
3
|
+
export type { TrackDataRenderedOptions, TrackFeedbackOptions, TrackSecurityAlertDetails, };
|
|
4
|
+
export declare const useTelemetry: (config: BrowserTelemetryExporterConfig) => {
|
|
5
|
+
trackSecurityAlert: (alertType: ClientSecurityAlertEvent["alertType"], details: TrackSecurityAlertDetails) => void;
|
|
6
|
+
trackClientDataRendered: (toolId: string, options?: TrackDataRenderedOptions) => void;
|
|
7
|
+
trackFeedback: (feedback: TrackFeedbackOptions) => void;
|
|
8
|
+
exporter: import("./browser-exporter").BrowserTelemetryExporter | undefined;
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=useTelemetry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTelemetry.d.ts","sourceRoot":"","sources":["../../src/client/useTelemetry.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,wBAAwB,EAC7B,KAAK,oBAAoB,EACzB,KAAK,yBAAyB,EAC/B,MAAM,UAAU,CAAC;AAClB,OAAO,KAAK,EACV,8BAA8B,EAC9B,wBAAwB,EACzB,MAAM,SAAS,CAAC;AAEjB,YAAY,EACV,wBAAwB,EACxB,oBAAoB,EACpB,yBAAyB,GAC1B,CAAC;AAEF,eAAO,MAAM,YAAY,GAAI,QAAQ,8BAA8B;oCAuBlD,wBAAwB,CAAC,WAAW,CAAC,WACvC,yBAAyB,KACjC,IAAI;sCAQG,MAAM,YACJ,wBAAwB,KACjC,IAAI;8BAOI,oBAAoB,KAAG,IAAI;;CAezC,CAAC"}
|
package/dist/control-layer.d.ts
CHANGED
|
@@ -19,18 +19,12 @@ export declare class AvantGateControlLayer {
|
|
|
19
19
|
private executeProviderPipeline;
|
|
20
20
|
private assembleResult;
|
|
21
21
|
private notifyAuditSink;
|
|
22
|
-
/**
|
|
23
|
-
* Exécute une requête avec garde d'entrée, masquage PII, garde pré-vol et calcul des coûts.
|
|
24
|
-
*/
|
|
25
22
|
execute: (options: {
|
|
26
23
|
userQuery: string;
|
|
27
24
|
systemPrompt?: string;
|
|
28
25
|
temperature?: number;
|
|
29
26
|
providerOverride?: LLMProviderPort;
|
|
30
27
|
}) => Promise<ExecutionResult>;
|
|
31
|
-
/**
|
|
32
|
-
* Exécute une requête et valide/répare le résultat selon un schéma Zod.
|
|
33
|
-
*/
|
|
34
28
|
executeStructured: <T>(options: {
|
|
35
29
|
userQuery: string;
|
|
36
30
|
systemPrompt?: string;
|
|
@@ -38,10 +32,16 @@ export declare class AvantGateControlLayer {
|
|
|
38
32
|
temperature?: number;
|
|
39
33
|
providerOverride?: LLMProviderPort;
|
|
40
34
|
}) => Promise<StructuredExecutionResult<T>>;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
35
|
+
private sanitizeIncomingMessages;
|
|
36
|
+
private verifyPromptBudget;
|
|
37
|
+
private dispatchOverrideAttempt;
|
|
38
|
+
private dispatchSimulatedAttempt;
|
|
39
|
+
private dispatchPipelineAttempt;
|
|
40
|
+
private dispatchProviderAttempt;
|
|
41
|
+
private parseAndValidateStructuredResult;
|
|
42
|
+
private buildStructuredResult;
|
|
43
|
+
private notifyAuditSinkSafe;
|
|
44
|
+
private waitRetryBackoff;
|
|
45
45
|
generateStructuredOutput: <T>(options: GenerateStructuredOutputOptions<T>) => Promise<StructuredExecutionResult<T>>;
|
|
46
46
|
}
|
|
47
47
|
export declare const createLLMControlLayer: (config: ControlLayerConfig) => AvantGateControlLayer;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"control-layer.d.ts","sourceRoot":"","sources":["../src/control-layer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EACV,kBAAkB,EAClB,eAAe,EACf,yBAAyB,EACzB,+BAA+B,EAC/B,e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AG,qBAIf,CAAC;AACrD,OAAO,EAAE,qBAAqB,IAAI,kBAAkB,EAAE,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1419,19 +1419,20 @@ var AvantGateControlLayer = class {
|
|
|
1419
1419
|
this.config.emergencyFallback
|
|
1420
1420
|
].filter((provider) => Boolean(provider?.client));
|
|
1421
1421
|
};
|
|
1422
|
-
executeSimulation = (query, systemPrompt) => {
|
|
1422
|
+
executeSimulation = (query, systemPrompt, modelOverride) => {
|
|
1423
|
+
const targetModel = modelOverride ?? this.config.primary.model;
|
|
1423
1424
|
const promptTokens = Math.ceil(((systemPrompt?.length ?? 0) + query.length) / 4);
|
|
1424
1425
|
const completionTokens = 50;
|
|
1425
|
-
const cost = this.calculateCost(
|
|
1426
|
+
const cost = this.calculateCost(targetModel, promptTokens, completionTokens);
|
|
1426
1427
|
return {
|
|
1427
|
-
text: `[AvantGate In-Process Engine] Response simulation for model: ${
|
|
1428
|
+
text: `[AvantGate In-Process Engine] Response simulation for model: ${targetModel}`,
|
|
1428
1429
|
tokens: {
|
|
1429
1430
|
prompt: promptTokens,
|
|
1430
1431
|
completion: completionTokens,
|
|
1431
1432
|
total: promptTokens + completionTokens
|
|
1432
1433
|
},
|
|
1433
1434
|
costUSD: cost,
|
|
1434
|
-
modelUsed:
|
|
1435
|
+
modelUsed: targetModel,
|
|
1435
1436
|
failoverOccurred: false,
|
|
1436
1437
|
attempts: 1
|
|
1437
1438
|
};
|
|
@@ -1511,9 +1512,6 @@ var AvantGateControlLayer = class {
|
|
|
1511
1512
|
attempts: result.attempts
|
|
1512
1513
|
});
|
|
1513
1514
|
};
|
|
1514
|
-
/**
|
|
1515
|
-
* Exécute une requête avec garde d'entrée, masquage PII, garde pré-vol et calcul des coûts.
|
|
1516
|
-
*/
|
|
1517
1515
|
execute = async (options) => {
|
|
1518
1516
|
const sanitizedQuery = this.applySecurityGuards(options.userQuery);
|
|
1519
1517
|
const messages = this.buildMessages(options.systemPrompt, sanitizedQuery);
|
|
@@ -1536,9 +1534,6 @@ var AvantGateControlLayer = class {
|
|
|
1536
1534
|
await this.notifyAuditSink(result);
|
|
1537
1535
|
return result;
|
|
1538
1536
|
};
|
|
1539
|
-
/**
|
|
1540
|
-
* Exécute une requête et valide/répare le résultat selon un schéma Zod.
|
|
1541
|
-
*/
|
|
1542
1537
|
executeStructured = async (options) => {
|
|
1543
1538
|
const rawResult = await this.execute({
|
|
1544
1539
|
userQuery: options.userQuery,
|
|
@@ -1562,107 +1557,147 @@ var AvantGateControlLayer = class {
|
|
|
1562
1557
|
failoverOccurred: rawResult.failoverOccurred
|
|
1563
1558
|
};
|
|
1564
1559
|
};
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
* Gère le failover multi-fournisseurs, les retries, la validation Zod et la normalisation financière.
|
|
1568
|
-
*/
|
|
1569
|
-
generateStructuredOutput = async (options) => {
|
|
1570
|
-
const maxRetries = options.maxRetries ?? this.config.retryOptions?.maxRetries ?? 2;
|
|
1571
|
-
const modelToUse = options.model ?? this.config.primary.model;
|
|
1572
|
-
const processedMessages = options.messages.map((msg) => {
|
|
1560
|
+
sanitizeIncomingMessages = (messages) => {
|
|
1561
|
+
return messages.map((msg) => {
|
|
1573
1562
|
if (msg.role === "user") {
|
|
1574
1563
|
return { ...msg, content: this.applySecurityGuards(msg.content) };
|
|
1575
1564
|
}
|
|
1576
1565
|
return msg;
|
|
1577
1566
|
});
|
|
1578
|
-
|
|
1567
|
+
};
|
|
1568
|
+
verifyPromptBudget = async (messages, model) => {
|
|
1569
|
+
const promptLength = messages.reduce((sum, msg) => sum + msg.content.length, 0);
|
|
1579
1570
|
const estimatedPromptTokens = Math.ceil(promptLength / 4);
|
|
1580
|
-
await this.checkPreflightBudget(estimatedPromptTokens,
|
|
1571
|
+
await this.checkPreflightBudget(estimatedPromptTokens, model);
|
|
1572
|
+
};
|
|
1573
|
+
dispatchOverrideAttempt = async (override, messages, model, temperature) => {
|
|
1574
|
+
const res = await override.complete({ model, messages, temperature: temperature ?? 0.1 });
|
|
1575
|
+
const usage = this.resolveTokens(res.usage, JSON.stringify(messages), res.text);
|
|
1576
|
+
return {
|
|
1577
|
+
responseText: res.text,
|
|
1578
|
+
promptTokens: usage.promptTokens,
|
|
1579
|
+
completionTokens: usage.completionTokens,
|
|
1580
|
+
modelUsed: model,
|
|
1581
|
+
failoverOccurred: false
|
|
1582
|
+
};
|
|
1583
|
+
};
|
|
1584
|
+
dispatchSimulatedAttempt = (messages, modelOverride) => {
|
|
1585
|
+
if (!this.config.mockSimulation) {
|
|
1586
|
+
throw new ConfigurationError(
|
|
1587
|
+
"[AvantGate Configuration Error] No active LLM provider configured. Provide a client implementing LLMProviderPort or configure credentials (apiKey / baseUrl)."
|
|
1588
|
+
);
|
|
1589
|
+
}
|
|
1590
|
+
const sim = this.executeSimulation(JSON.stringify(messages), void 0, modelOverride);
|
|
1591
|
+
const responseText = sim.text.includes("{") ? sim.text : JSON.stringify({ simulation: true, model: sim.modelUsed });
|
|
1592
|
+
return {
|
|
1593
|
+
responseText,
|
|
1594
|
+
promptTokens: sim.tokens.prompt,
|
|
1595
|
+
completionTokens: sim.tokens.completion,
|
|
1596
|
+
modelUsed: sim.modelUsed,
|
|
1597
|
+
failoverOccurred: false
|
|
1598
|
+
};
|
|
1599
|
+
};
|
|
1600
|
+
dispatchPipelineAttempt = async (messages, modelOverride, temperature) => {
|
|
1601
|
+
const output = await this.executeProviderPipeline(
|
|
1602
|
+
messages,
|
|
1603
|
+
JSON.stringify(messages),
|
|
1604
|
+
temperature ?? 0.1,
|
|
1605
|
+
modelOverride
|
|
1606
|
+
);
|
|
1607
|
+
return {
|
|
1608
|
+
responseText: output.responseText,
|
|
1609
|
+
promptTokens: output.usage.promptTokens,
|
|
1610
|
+
completionTokens: output.usage.completionTokens,
|
|
1611
|
+
modelUsed: output.modelUsed,
|
|
1612
|
+
failoverOccurred: output.failoverOccurred
|
|
1613
|
+
};
|
|
1614
|
+
};
|
|
1615
|
+
dispatchProviderAttempt = async (options, messages, targetModel) => {
|
|
1616
|
+
if (options.providerOverride) {
|
|
1617
|
+
return this.dispatchOverrideAttempt(options.providerOverride, messages, targetModel, options.temperature);
|
|
1618
|
+
}
|
|
1619
|
+
if (this.getProviderChain().length === 0) {
|
|
1620
|
+
return this.dispatchSimulatedAttempt(messages, options.model);
|
|
1621
|
+
}
|
|
1622
|
+
return this.dispatchPipelineAttempt(messages, options.model ?? targetModel, options.temperature);
|
|
1623
|
+
};
|
|
1624
|
+
parseAndValidateStructuredResult = (rawText, options) => {
|
|
1625
|
+
const sanitizedResponse = this.applyOutputSecurityGuards(rawText);
|
|
1626
|
+
const isFinancial = options.financialNormalizer ?? Boolean(
|
|
1627
|
+
this.config.features?.finance?.enableFrenchAccounting || this.config.features?.finance
|
|
1628
|
+
);
|
|
1629
|
+
const parsedData = validateWithZod(sanitizedResponse, options.schema, {
|
|
1630
|
+
financialNormalizer: isFinancial,
|
|
1631
|
+
jurisdiction: this.config.features?.finance?.jurisdiction
|
|
1632
|
+
});
|
|
1633
|
+
return { parsedData, sanitizedResponse };
|
|
1634
|
+
};
|
|
1635
|
+
buildStructuredResult = (params) => {
|
|
1636
|
+
const totalTokens = params.promptTokens + params.completionTokens;
|
|
1637
|
+
const costUSD = this.calculateCost(params.modelUsed, params.promptTokens, params.completionTokens);
|
|
1638
|
+
this.checkPostExecutionBudget(totalTokens, costUSD);
|
|
1639
|
+
return {
|
|
1640
|
+
data: params.data,
|
|
1641
|
+
rawText: params.rawText,
|
|
1642
|
+
tokens: {
|
|
1643
|
+
prompt: params.promptTokens,
|
|
1644
|
+
completion: params.completionTokens,
|
|
1645
|
+
total: totalTokens
|
|
1646
|
+
},
|
|
1647
|
+
costUSD,
|
|
1648
|
+
modelUsed: params.modelUsed,
|
|
1649
|
+
failoverOccurred: params.failoverOccurred
|
|
1650
|
+
};
|
|
1651
|
+
};
|
|
1652
|
+
notifyAuditSinkSafe = async (sanitizedText, result, attempts) => {
|
|
1653
|
+
await this.notifyAuditSink({
|
|
1654
|
+
text: sanitizedText,
|
|
1655
|
+
tokens: result.tokens,
|
|
1656
|
+
costUSD: result.costUSD,
|
|
1657
|
+
modelUsed: result.modelUsed,
|
|
1658
|
+
failoverOccurred: result.failoverOccurred,
|
|
1659
|
+
attempts
|
|
1660
|
+
});
|
|
1661
|
+
};
|
|
1662
|
+
waitRetryBackoff = async (attempt, maxRetries) => {
|
|
1663
|
+
if (attempt >= maxRetries) {
|
|
1664
|
+
return;
|
|
1665
|
+
}
|
|
1666
|
+
const initialDelay = this.config.retryOptions?.initialDelayMs ?? 200;
|
|
1667
|
+
const factor = this.config.retryOptions?.backoffFactor ?? 1.5;
|
|
1668
|
+
const delay = initialDelay * Math.pow(factor, attempt);
|
|
1669
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
1670
|
+
};
|
|
1671
|
+
generateStructuredOutput = async (options) => {
|
|
1672
|
+
const maxRetries = options.maxRetries ?? this.config.retryOptions?.maxRetries ?? 2;
|
|
1673
|
+
const targetModel = options.model ?? this.config.primary.model;
|
|
1674
|
+
const processedMessages = this.sanitizeIncomingMessages(options.messages);
|
|
1675
|
+
await this.verifyPromptBudget(processedMessages, targetModel);
|
|
1581
1676
|
let lastError;
|
|
1582
1677
|
let accumulatedPromptTokens = 0;
|
|
1583
1678
|
let accumulatedCompletionTokens = 0;
|
|
1584
|
-
let failoverOccurred = false;
|
|
1585
|
-
let modelUsed = modelToUse;
|
|
1586
1679
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
1587
1680
|
try {
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
messages: processedMessages,
|
|
1595
|
-
temperature: options.temperature ?? 0.1
|
|
1596
|
-
});
|
|
1597
|
-
responseText = res.text;
|
|
1598
|
-
const usage = this.resolveTokens(res.usage, JSON.stringify(processedMessages), responseText);
|
|
1599
|
-
attemptPromptTokens = usage.promptTokens;
|
|
1600
|
-
attemptCompletionTokens = usage.completionTokens;
|
|
1601
|
-
modelUsed = modelToUse;
|
|
1602
|
-
} else if (this.getProviderChain().length === 0) {
|
|
1603
|
-
if (this.config.mockSimulation) {
|
|
1604
|
-
const sim = this.executeSimulation(JSON.stringify(processedMessages));
|
|
1605
|
-
responseText = sim.text;
|
|
1606
|
-
attemptPromptTokens = sim.tokens.prompt;
|
|
1607
|
-
attemptCompletionTokens = sim.tokens.completion;
|
|
1608
|
-
} else {
|
|
1609
|
-
throw new ConfigurationError(
|
|
1610
|
-
"[AvantGate Configuration Error] No active LLM provider configured. Provide a client implementing LLMProviderPort or configure credentials (apiKey / baseUrl)."
|
|
1611
|
-
);
|
|
1612
|
-
}
|
|
1613
|
-
} else {
|
|
1614
|
-
const output = await this.executeProviderPipeline(
|
|
1615
|
-
processedMessages,
|
|
1616
|
-
JSON.stringify(processedMessages),
|
|
1617
|
-
options.temperature ?? 0.1,
|
|
1618
|
-
options.model
|
|
1619
|
-
);
|
|
1620
|
-
responseText = output.responseText;
|
|
1621
|
-
attemptPromptTokens = output.usage.promptTokens;
|
|
1622
|
-
attemptCompletionTokens = output.usage.completionTokens;
|
|
1623
|
-
modelUsed = output.modelUsed;
|
|
1624
|
-
failoverOccurred = output.failoverOccurred;
|
|
1625
|
-
}
|
|
1626
|
-
accumulatedPromptTokens += attemptPromptTokens;
|
|
1627
|
-
accumulatedCompletionTokens += attemptCompletionTokens;
|
|
1628
|
-
const sanitizedResponse = this.applyOutputSecurityGuards(responseText);
|
|
1629
|
-
const isFinancial = options.financialNormalizer ?? Boolean(
|
|
1630
|
-
this.config.features?.finance?.enableFrenchAccounting || this.config.features?.finance
|
|
1681
|
+
const output = await this.dispatchProviderAttempt(options, processedMessages, targetModel);
|
|
1682
|
+
accumulatedPromptTokens += output.promptTokens;
|
|
1683
|
+
accumulatedCompletionTokens += output.completionTokens;
|
|
1684
|
+
const { parsedData, sanitizedResponse } = this.parseAndValidateStructuredResult(
|
|
1685
|
+
output.responseText,
|
|
1686
|
+
options
|
|
1631
1687
|
);
|
|
1632
|
-
const
|
|
1633
|
-
financialNormalizer: isFinancial,
|
|
1634
|
-
jurisdiction: this.config.features?.finance?.jurisdiction
|
|
1635
|
-
});
|
|
1636
|
-
const totalTokens = accumulatedPromptTokens + accumulatedCompletionTokens;
|
|
1637
|
-
const costUSD = this.calculateCost(modelUsed, accumulatedPromptTokens, accumulatedCompletionTokens);
|
|
1638
|
-
this.checkPostExecutionBudget(totalTokens, costUSD);
|
|
1639
|
-
const result = {
|
|
1688
|
+
const result = this.buildStructuredResult({
|
|
1640
1689
|
data: parsedData,
|
|
1641
1690
|
rawText: sanitizedResponse,
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
},
|
|
1647
|
-
costUSD,
|
|
1648
|
-
modelUsed,
|
|
1649
|
-
failoverOccurred
|
|
1650
|
-
};
|
|
1651
|
-
await this.notifyAuditSink({
|
|
1652
|
-
text: responseText,
|
|
1653
|
-
tokens: result.tokens,
|
|
1654
|
-
costUSD: result.costUSD,
|
|
1655
|
-
modelUsed: result.modelUsed,
|
|
1656
|
-
failoverOccurred: result.failoverOccurred,
|
|
1657
|
-
attempts: attempt + 1
|
|
1691
|
+
promptTokens: accumulatedPromptTokens,
|
|
1692
|
+
completionTokens: accumulatedCompletionTokens,
|
|
1693
|
+
modelUsed: output.modelUsed,
|
|
1694
|
+
failoverOccurred: output.failoverOccurred
|
|
1658
1695
|
});
|
|
1696
|
+
await this.notifyAuditSinkSafe(sanitizedResponse, result, attempt + 1);
|
|
1659
1697
|
return result;
|
|
1660
1698
|
} catch (err) {
|
|
1661
1699
|
lastError = err;
|
|
1662
|
-
|
|
1663
|
-
const delay = (this.config.retryOptions?.initialDelayMs ?? 200) * Math.pow(this.config.retryOptions?.backoffFactor ?? 1.5, attempt);
|
|
1664
|
-
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
1665
|
-
}
|
|
1700
|
+
await this.waitRetryBackoff(attempt, maxRetries);
|
|
1666
1701
|
}
|
|
1667
1702
|
}
|
|
1668
1703
|
throw lastError;
|