avantgate 1.4.0 → 1.6.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 +1 -0
- package/dist/agent/types.d.ts +1 -1
- package/dist/agent/types.d.ts.map +1 -1
- package/dist/{chunk-DTM2OLCG.mjs → chunk-4XWO3NZW.mjs} +69 -18
- package/dist/client/browser-exporter.d.ts +32 -0
- package/dist/client/browser-exporter.d.ts.map +1 -0
- package/dist/client/client.d.ts +37 -0
- package/dist/client/client.d.ts.map +1 -0
- package/dist/client/index.d.ts +5 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +259 -0
- package/dist/client/index.mjs +230 -0
- package/dist/client/types.d.ts +59 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/useAvantGateTelemetry.d.ts +14 -0
- package/dist/client/useAvantGateTelemetry.d.ts.map +1 -0
- package/dist/index.js +71 -18
- package/dist/index.mjs +6 -2
- package/dist/workflow/define-workflow.d.ts +1 -1
- package/dist/workflow/define-workflow.d.ts.map +1 -1
- package/dist/workflow/engine.d.ts +1 -1
- package/dist/workflow/engine.d.ts.map +1 -1
- package/dist/workflow/errors.d.ts +7 -0
- package/dist/workflow/errors.d.ts.map +1 -1
- package/dist/workflow/index.d.ts +1 -1
- package/dist/workflow/index.d.ts.map +1 -1
- package/dist/workflow/index.js +73 -20
- package/dist/workflow/index.mjs +7 -3
- package/dist/workflow/types.d.ts +4 -1
- package/dist/workflow/types.d.ts.map +1 -1
- package/package.json +20 -4
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
// src/client/browser-exporter.ts
|
|
2
|
+
var DEFAULT_ENDPOINT = "http://localhost:3000/api/v1/ingest/events";
|
|
3
|
+
var DEFAULT_INTERVAL_MS = 3e3;
|
|
4
|
+
var DEFAULT_MAX_BATCH_SIZE = 20;
|
|
5
|
+
var BrowserTelemetryExporter = class {
|
|
6
|
+
publicKey;
|
|
7
|
+
endpoint;
|
|
8
|
+
agentName;
|
|
9
|
+
runId;
|
|
10
|
+
batchIntervalMs;
|
|
11
|
+
maxBatchSize;
|
|
12
|
+
fetchFn;
|
|
13
|
+
onError;
|
|
14
|
+
buffer = [];
|
|
15
|
+
timer = null;
|
|
16
|
+
isFlushing = false;
|
|
17
|
+
isDestroyed = false;
|
|
18
|
+
handleVisibilityChange = () => {
|
|
19
|
+
if (typeof document !== "undefined" && document.visibilityState === "hidden") {
|
|
20
|
+
void this.flush();
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
handleBeforeUnload = () => {
|
|
24
|
+
void this.flush();
|
|
25
|
+
};
|
|
26
|
+
constructor(config) {
|
|
27
|
+
this.publicKey = config.publicKey;
|
|
28
|
+
this.endpoint = config.endpoint ?? DEFAULT_ENDPOINT;
|
|
29
|
+
this.agentName = config.agentName;
|
|
30
|
+
this.runId = config.runId;
|
|
31
|
+
this.batchIntervalMs = config.batchIntervalMs ?? DEFAULT_INTERVAL_MS;
|
|
32
|
+
this.maxBatchSize = config.maxBatchSize ?? DEFAULT_MAX_BATCH_SIZE;
|
|
33
|
+
this.fetchFn = config.fetchFn ?? (typeof fetch !== "undefined" ? fetch.bind(globalThis) : void 0);
|
|
34
|
+
this.onError = config.onError;
|
|
35
|
+
this.startTimer();
|
|
36
|
+
this.setupBrowserListeners();
|
|
37
|
+
}
|
|
38
|
+
updateContext = (context) => {
|
|
39
|
+
if (context.runId !== void 0) {
|
|
40
|
+
this.runId = context.runId;
|
|
41
|
+
}
|
|
42
|
+
if (context.agentName !== void 0) {
|
|
43
|
+
this.agentName = context.agentName;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
getRunId = () => this.runId;
|
|
47
|
+
getAgentName = () => this.agentName;
|
|
48
|
+
startTimer() {
|
|
49
|
+
if (this.batchIntervalMs <= 0 || typeof setInterval === "undefined") {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
this.timer = setInterval(() => {
|
|
53
|
+
void this.flush();
|
|
54
|
+
}, this.batchIntervalMs);
|
|
55
|
+
}
|
|
56
|
+
setupBrowserListeners() {
|
|
57
|
+
if (typeof document !== "undefined" && typeof document.addEventListener === "function") {
|
|
58
|
+
document.addEventListener("visibilitychange", this.handleVisibilityChange);
|
|
59
|
+
}
|
|
60
|
+
if (typeof window !== "undefined" && typeof window.addEventListener === "function") {
|
|
61
|
+
window.addEventListener("beforeunload", this.handleBeforeUnload);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
emit = (event) => {
|
|
65
|
+
if (this.isDestroyed) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.buffer.push(event);
|
|
69
|
+
if (this.buffer.length >= this.maxBatchSize) {
|
|
70
|
+
void this.flush();
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
flush = async () => {
|
|
74
|
+
if (this.isFlushing || this.buffer.length === 0 || !this.fetchFn) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
this.isFlushing = true;
|
|
78
|
+
const batch = [...this.buffer];
|
|
79
|
+
this.buffer = [];
|
|
80
|
+
const payload = {
|
|
81
|
+
runId: this.runId,
|
|
82
|
+
agentName: this.agentName,
|
|
83
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
84
|
+
events: batch
|
|
85
|
+
};
|
|
86
|
+
try {
|
|
87
|
+
await this.dispatchPayload(payload);
|
|
88
|
+
} catch (err) {
|
|
89
|
+
this.handleFlushError(err, batch);
|
|
90
|
+
} finally {
|
|
91
|
+
this.isFlushing = false;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
dispatchPayload = async (payload) => {
|
|
95
|
+
if (!this.fetchFn) {
|
|
96
|
+
throw new Error("No fetch implementation available in current environment");
|
|
97
|
+
}
|
|
98
|
+
const response = await this.fetchFn(this.endpoint, {
|
|
99
|
+
method: "POST",
|
|
100
|
+
headers: {
|
|
101
|
+
"Content-Type": "application/json",
|
|
102
|
+
Authorization: `Bearer ${this.publicKey}`
|
|
103
|
+
},
|
|
104
|
+
body: JSON.stringify(payload),
|
|
105
|
+
keepalive: true
|
|
106
|
+
});
|
|
107
|
+
if (!response.ok) {
|
|
108
|
+
throw new Error(`Browser telemetry ingestion failed: ${response.status} ${response.statusText}`);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
handleFlushError = (err, failedBatch) => {
|
|
112
|
+
if (!this.isDestroyed && this.buffer.length < this.maxBatchSize * 2) {
|
|
113
|
+
this.buffer.unshift(...failedBatch);
|
|
114
|
+
}
|
|
115
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
116
|
+
if (this.onError) {
|
|
117
|
+
this.onError(error);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
destroy = () => {
|
|
121
|
+
this.isDestroyed = true;
|
|
122
|
+
if (this.timer) {
|
|
123
|
+
clearInterval(this.timer);
|
|
124
|
+
this.timer = null;
|
|
125
|
+
}
|
|
126
|
+
if (typeof document !== "undefined" && typeof document.removeEventListener === "function") {
|
|
127
|
+
document.removeEventListener("visibilitychange", this.handleVisibilityChange);
|
|
128
|
+
}
|
|
129
|
+
if (typeof window !== "undefined" && typeof window.removeEventListener === "function") {
|
|
130
|
+
window.removeEventListener("beforeunload", this.handleBeforeUnload);
|
|
131
|
+
}
|
|
132
|
+
void this.flush();
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// src/client/client.ts
|
|
137
|
+
var createAvantGateClient = (config) => {
|
|
138
|
+
const exporter = new BrowserTelemetryExporter(config);
|
|
139
|
+
const trackSecurityAlert = (alertType, details) => {
|
|
140
|
+
const snippet = details.snippet ?? details.matchedPatternSnippet;
|
|
141
|
+
exporter.emit({
|
|
142
|
+
type: "CLIENT_SECURITY_ALERT",
|
|
143
|
+
alertType,
|
|
144
|
+
inputLength: details.inputLength,
|
|
145
|
+
snippet,
|
|
146
|
+
matchedPatternSnippet: snippet,
|
|
147
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
const trackClientDataRendered = (toolId, options) => {
|
|
151
|
+
exporter.emit({
|
|
152
|
+
type: "CLIENT_DATA_RENDERED",
|
|
153
|
+
toolId,
|
|
154
|
+
channel: options?.channel,
|
|
155
|
+
renderedItemCount: options?.renderedItemCount,
|
|
156
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
const trackFeedback = (feedback) => {
|
|
160
|
+
exporter.emit({
|
|
161
|
+
type: "USER_FEEDBACK",
|
|
162
|
+
rating: feedback.rating,
|
|
163
|
+
feedbackTag: feedback.tag,
|
|
164
|
+
userComment: feedback.comment,
|
|
165
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
return {
|
|
169
|
+
trackSecurityAlert,
|
|
170
|
+
trackClientDataRendered,
|
|
171
|
+
trackFeedback,
|
|
172
|
+
updateContext: exporter.updateContext,
|
|
173
|
+
flush: exporter.flush,
|
|
174
|
+
destroy: exporter.destroy,
|
|
175
|
+
exporter
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// src/client/useAvantGateTelemetry.ts
|
|
180
|
+
import { useRef, useEffect, useCallback, useMemo } from "react";
|
|
181
|
+
var useAvantGateTelemetry = (config) => {
|
|
182
|
+
const clientRef = useRef(null);
|
|
183
|
+
if (!clientRef.current) {
|
|
184
|
+
clientRef.current = createAvantGateClient(config);
|
|
185
|
+
}
|
|
186
|
+
useEffect(() => {
|
|
187
|
+
clientRef.current?.updateContext({
|
|
188
|
+
runId: config.runId,
|
|
189
|
+
agentName: config.agentName
|
|
190
|
+
});
|
|
191
|
+
}, [config.runId, config.agentName]);
|
|
192
|
+
useEffect(() => {
|
|
193
|
+
return () => {
|
|
194
|
+
clientRef.current?.destroy();
|
|
195
|
+
clientRef.current = null;
|
|
196
|
+
};
|
|
197
|
+
}, []);
|
|
198
|
+
const trackSecurityAlert = useCallback(
|
|
199
|
+
(alertType, details) => {
|
|
200
|
+
clientRef.current?.trackSecurityAlert(alertType, details);
|
|
201
|
+
},
|
|
202
|
+
[]
|
|
203
|
+
);
|
|
204
|
+
const trackClientDataRendered = useCallback(
|
|
205
|
+
(toolId, options) => {
|
|
206
|
+
clientRef.current?.trackClientDataRendered(toolId, options);
|
|
207
|
+
},
|
|
208
|
+
[]
|
|
209
|
+
);
|
|
210
|
+
const trackFeedback = useCallback(
|
|
211
|
+
(feedback) => {
|
|
212
|
+
clientRef.current?.trackFeedback(feedback);
|
|
213
|
+
},
|
|
214
|
+
[]
|
|
215
|
+
);
|
|
216
|
+
return useMemo(
|
|
217
|
+
() => ({
|
|
218
|
+
trackSecurityAlert,
|
|
219
|
+
trackClientDataRendered,
|
|
220
|
+
trackFeedback,
|
|
221
|
+
exporter: clientRef.current?.exporter
|
|
222
|
+
}),
|
|
223
|
+
[trackSecurityAlert, trackClientDataRendered, trackFeedback]
|
|
224
|
+
);
|
|
225
|
+
};
|
|
226
|
+
export {
|
|
227
|
+
BrowserTelemetryExporter,
|
|
228
|
+
createAvantGateClient,
|
|
229
|
+
useAvantGateTelemetry
|
|
230
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client Security Alert Event emitted when a local pre-flight guardrail detects an anomaly
|
|
3
|
+
* (prompt injection attempt, hardcoded API key, unredacted PII) before network egress.
|
|
4
|
+
*/
|
|
5
|
+
export interface ClientSecurityAlertEvent {
|
|
6
|
+
type: "CLIENT_SECURITY_ALERT";
|
|
7
|
+
alertType: "POTENTIAL_PROMPT_INJECTION" | "SUSPECTED_SECRET_INPUT" | "SUSPECTED_PII_INPUT";
|
|
8
|
+
inputLength: number;
|
|
9
|
+
snippet?: string;
|
|
10
|
+
matchedPatternSnippet?: string;
|
|
11
|
+
timestamp: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Client Data Rendered Event emitted when tool output or stream chunks are rendered in UI.
|
|
15
|
+
*/
|
|
16
|
+
export interface ClientDataRenderedEvent {
|
|
17
|
+
type: "CLIENT_DATA_RENDERED";
|
|
18
|
+
toolId: string;
|
|
19
|
+
channel?: "SOCKET" | "STREAM" | "HTTP";
|
|
20
|
+
renderedItemCount?: number;
|
|
21
|
+
timestamp: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* User Feedback Event emitted when a user rates or comments on an agent response.
|
|
25
|
+
*/
|
|
26
|
+
export interface UserFeedbackEvent {
|
|
27
|
+
type: "USER_FEEDBACK";
|
|
28
|
+
rating: "POSITIVE" | "NEGATIVE";
|
|
29
|
+
feedbackTag?: "HELPFUL" | "INACCURATE" | "UNSAFE" | "OFF_TOPIC";
|
|
30
|
+
userComment?: string;
|
|
31
|
+
timestamp: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Union of all supported front-end client telemetry events.
|
|
35
|
+
*/
|
|
36
|
+
export type ClientTelemetryEvent = ClientSecurityAlertEvent | ClientDataRenderedEvent | UserFeedbackEvent;
|
|
37
|
+
/**
|
|
38
|
+
* Configuration options for BrowserTelemetryExporter.
|
|
39
|
+
*/
|
|
40
|
+
export interface BrowserTelemetryExporterConfig {
|
|
41
|
+
publicKey: string;
|
|
42
|
+
endpoint?: string;
|
|
43
|
+
agentName?: string;
|
|
44
|
+
runId?: string;
|
|
45
|
+
batchIntervalMs?: number;
|
|
46
|
+
maxBatchSize?: number;
|
|
47
|
+
fetchFn?: typeof fetch;
|
|
48
|
+
onError?: (error: Error) => void;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Ingest payload format expected by POST /api/v1/ingest/events according to DESIGN-019.
|
|
52
|
+
*/
|
|
53
|
+
export interface ClientTelemetryIngestPayload {
|
|
54
|
+
runId?: string;
|
|
55
|
+
agentName?: string;
|
|
56
|
+
timestamp: string;
|
|
57
|
+
events: ClientTelemetryEvent[];
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/client/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,wBAAwB;IACvC,IAAI,EAAE,uBAAuB,CAAC;IAC9B,SAAS,EAAE,4BAA4B,GAAG,wBAAwB,GAAG,qBAAqB,CAAC;IAC3F,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC,IAAI,EAAE,sBAAsB,CAAC;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,QAAQ,GAAG,QAAQ,GAAG,MAAM,CAAC;IACvC,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,eAAe,CAAC;IACtB,MAAM,EAAE,UAAU,GAAG,UAAU,CAAC;IAChC,WAAW,CAAC,EAAE,SAAS,GAAG,YAAY,GAAG,QAAQ,GAAG,WAAW,CAAC;IAChE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAC5B,wBAAwB,GACxB,uBAAuB,GACvB,iBAAiB,CAAC;AAEtB;;GAEG;AACH,MAAM,WAAW,8BAA8B;IAC7C,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,OAAO,KAAK,CAAC;IACvB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CAClC;AAED;;GAEG;AACH,MAAM,WAAW,4BAA4B;IAC3C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,oBAAoB,EAAE,CAAC;CAChC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type TrackSecurityAlertDetails, type TrackDataRenderedOptions, type TrackFeedbackOptions } from "./client";
|
|
2
|
+
import type { BrowserTelemetryExporterConfig, ClientSecurityAlertEvent } from "./types";
|
|
3
|
+
export type { TrackSecurityAlertDetails, TrackDataRenderedOptions, TrackFeedbackOptions, };
|
|
4
|
+
/**
|
|
5
|
+
* Lightweight React Hook for front-end telemetry and client-side security event tracking.
|
|
6
|
+
* Encapsulates lifecycle management, useRef persistence, useCallback and useMemo optimization.
|
|
7
|
+
*/
|
|
8
|
+
export declare const useAvantGateTelemetry: (config: BrowserTelemetryExporterConfig) => {
|
|
9
|
+
trackSecurityAlert: (alertType: ClientSecurityAlertEvent["alertType"], details: TrackSecurityAlertDetails) => void;
|
|
10
|
+
trackClientDataRendered: (toolId: string, options?: TrackDataRenderedOptions) => void;
|
|
11
|
+
trackFeedback: (feedback: TrackFeedbackOptions) => void;
|
|
12
|
+
exporter: import("./browser-exporter").BrowserTelemetryExporter | undefined;
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=useAvantGateTelemetry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAvantGateTelemetry.d.ts","sourceRoot":"","sources":["../../src/client/useAvantGateTelemetry.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,yBAAyB,EAC9B,KAAK,wBAAwB,EAC7B,KAAK,oBAAoB,EAC1B,MAAM,UAAU,CAAC;AAClB,OAAO,KAAK,EACV,8BAA8B,EAC9B,wBAAwB,EACzB,MAAM,SAAS,CAAC;AAEjB,YAAY,EACV,yBAAyB,EACzB,wBAAwB,EACxB,oBAAoB,GACrB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,qBAAqB,GAAI,QAAQ,8BAA8B;oCAuB3D,wBAAwB,CAAC,WAAW,CAAC,WACvC,yBAAyB,KACjC,IAAI;sCAQG,MAAM,YACJ,wBAAwB,KACjC,IAAI;8BAOI,oBAAoB,KAAG,IAAI;;CAezC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -33,6 +33,7 @@ __export(index_exports, {
|
|
|
33
33
|
PromptRegistry: () => PromptRegistry,
|
|
34
34
|
PromptTemplate: () => PromptTemplate,
|
|
35
35
|
SEED_MODEL_PRICES: () => SEED_MODEL_PRICES,
|
|
36
|
+
WorkflowAbortSignal: () => WorkflowAbortSignal,
|
|
36
37
|
WorkflowSagaRollbackError: () => WorkflowSagaRollbackError,
|
|
37
38
|
WorkflowValidationError: () => WorkflowValidationError,
|
|
38
39
|
ZenLLMControlLayer: () => AvantGateControlLayer,
|
|
@@ -43,6 +44,7 @@ __export(index_exports, {
|
|
|
43
44
|
defineWorkflow: () => defineWorkflow,
|
|
44
45
|
executeWorkflow: () => executeWorkflow,
|
|
45
46
|
extractAndCleanJSON: () => extractAndCleanJSON,
|
|
47
|
+
isWorkflowAbortSignal: () => isWorkflowAbortSignal,
|
|
46
48
|
resolveModelPrice: () => resolveModelPrice,
|
|
47
49
|
resolveModelPriceAsync: () => resolveModelPriceAsync,
|
|
48
50
|
sanitizePII: () => sanitizePII,
|
|
@@ -1893,21 +1895,35 @@ var WorkflowValidationError = class extends Error {
|
|
|
1893
1895
|
this.issues = issues;
|
|
1894
1896
|
}
|
|
1895
1897
|
};
|
|
1898
|
+
var WorkflowAbortSignal = class extends Error {
|
|
1899
|
+
__isWorkflowAbort = true;
|
|
1900
|
+
reason;
|
|
1901
|
+
payload;
|
|
1902
|
+
constructor(reason, payload) {
|
|
1903
|
+
super(`Workflow aborted: ${reason}`);
|
|
1904
|
+
this.name = "WorkflowAbortSignal";
|
|
1905
|
+
this.reason = reason;
|
|
1906
|
+
this.payload = payload;
|
|
1907
|
+
}
|
|
1908
|
+
};
|
|
1909
|
+
var isWorkflowAbortSignal = (value) => {
|
|
1910
|
+
return typeof value === "object" && value !== null && value.__isWorkflowAbort === true;
|
|
1911
|
+
};
|
|
1896
1912
|
|
|
1897
1913
|
// src/workflow/engine.ts
|
|
1898
|
-
|
|
1914
|
+
var generateRunId = (workflowId) => {
|
|
1899
1915
|
const timestamp = Date.now();
|
|
1900
1916
|
const randomSuffix = Math.random().toString(36).substring(2, 9);
|
|
1901
1917
|
return `run_${workflowId}_${timestamp}_${randomSuffix}`;
|
|
1902
|
-
}
|
|
1903
|
-
|
|
1918
|
+
};
|
|
1919
|
+
var validateInput = (config, input) => {
|
|
1904
1920
|
const parseResult = config.inputSchema.safeParse(input);
|
|
1905
1921
|
if (!parseResult.success) {
|
|
1906
1922
|
throw new WorkflowValidationError(config.id, parseResult.error.issues);
|
|
1907
1923
|
}
|
|
1908
1924
|
return parseResult.data;
|
|
1909
|
-
}
|
|
1910
|
-
|
|
1925
|
+
};
|
|
1926
|
+
var createStepContext = (params) => {
|
|
1911
1927
|
const { workflowId, stepId, runId, context, stepResults } = params;
|
|
1912
1928
|
return {
|
|
1913
1929
|
workflowId,
|
|
@@ -1936,10 +1952,13 @@ function createStepContext(params) {
|
|
|
1936
1952
|
});
|
|
1937
1953
|
}
|
|
1938
1954
|
throw new StepSuspendedError(stepId, workflowId, options?.prompt);
|
|
1955
|
+
},
|
|
1956
|
+
abort: (reason, payload) => {
|
|
1957
|
+
throw new WorkflowAbortSignal(reason, payload);
|
|
1939
1958
|
}
|
|
1940
1959
|
};
|
|
1941
|
-
}
|
|
1942
|
-
async
|
|
1960
|
+
};
|
|
1961
|
+
var rollbackSaga = async (params) => {
|
|
1943
1962
|
const {
|
|
1944
1963
|
workflowId,
|
|
1945
1964
|
failedStepId,
|
|
@@ -1983,12 +2002,38 @@ async function rollbackSaga(params) {
|
|
|
1983
2002
|
compensatedSteps,
|
|
1984
2003
|
failedCompensations
|
|
1985
2004
|
});
|
|
1986
|
-
}
|
|
1987
|
-
async
|
|
2005
|
+
};
|
|
2006
|
+
var executeWorkflow = async (config, rawInput, context) => {
|
|
1988
2007
|
const input = validateInput(config, rawInput);
|
|
1989
2008
|
const runId = context?.runId ?? generateRunId(config.id);
|
|
1990
2009
|
const stepResults = {};
|
|
1991
2010
|
const completedSteps = [];
|
|
2011
|
+
const handleAbort = async (stepId, signal) => {
|
|
2012
|
+
stepResults[stepId] = {
|
|
2013
|
+
aborted: true,
|
|
2014
|
+
reason: signal.reason,
|
|
2015
|
+
payload: signal.payload
|
|
2016
|
+
};
|
|
2017
|
+
if (context?.storage) {
|
|
2018
|
+
await context.storage.updateStepStatus(config.id, stepId, "ABORTED", {
|
|
2019
|
+
result: signal.payload,
|
|
2020
|
+
metadata: { abortReason: signal.reason }
|
|
2021
|
+
});
|
|
2022
|
+
}
|
|
2023
|
+
const abortOutput = signal.payload ?? {
|
|
2024
|
+
aborted: true,
|
|
2025
|
+
reason: signal.reason
|
|
2026
|
+
};
|
|
2027
|
+
return {
|
|
2028
|
+
workflowId: config.id,
|
|
2029
|
+
runId,
|
|
2030
|
+
status: "ABORTED",
|
|
2031
|
+
stepResults,
|
|
2032
|
+
abortReason: signal.reason,
|
|
2033
|
+
abortPayload: signal.payload,
|
|
2034
|
+
output: abortOutput
|
|
2035
|
+
};
|
|
2036
|
+
};
|
|
1992
2037
|
for (const step of config.steps) {
|
|
1993
2038
|
const stepCtx = createStepContext({
|
|
1994
2039
|
workflowId: config.id,
|
|
@@ -1999,6 +2044,9 @@ async function executeWorkflow(config, rawInput, context) {
|
|
|
1999
2044
|
});
|
|
2000
2045
|
try {
|
|
2001
2046
|
const result = await step.execute(input, stepCtx);
|
|
2047
|
+
if (isWorkflowAbortSignal(result)) {
|
|
2048
|
+
return await handleAbort(step.id, result);
|
|
2049
|
+
}
|
|
2002
2050
|
stepResults[step.id] = result;
|
|
2003
2051
|
completedSteps.push({ step, result });
|
|
2004
2052
|
if (context?.storage) {
|
|
@@ -2013,6 +2061,9 @@ async function executeWorkflow(config, rawInput, context) {
|
|
|
2013
2061
|
stepResults
|
|
2014
2062
|
};
|
|
2015
2063
|
}
|
|
2064
|
+
if (isWorkflowAbortSignal(error)) {
|
|
2065
|
+
return await handleAbort(step.id, error);
|
|
2066
|
+
}
|
|
2016
2067
|
await rollbackSaga({
|
|
2017
2068
|
workflowId: config.id,
|
|
2018
2069
|
failedStepId: step.id,
|
|
@@ -2033,22 +2084,22 @@ async function executeWorkflow(config, rawInput, context) {
|
|
|
2033
2084
|
stepResults,
|
|
2034
2085
|
output: finalOutput
|
|
2035
2086
|
};
|
|
2036
|
-
}
|
|
2087
|
+
};
|
|
2037
2088
|
|
|
2038
2089
|
// src/workflow/define-workflow.ts
|
|
2039
|
-
|
|
2090
|
+
var resolveWorkflowImpact = (config, options) => {
|
|
2040
2091
|
if (options?.impact) {
|
|
2041
2092
|
return options.impact;
|
|
2042
2093
|
}
|
|
2043
2094
|
return config.impact ?? "MUTATIVE";
|
|
2044
|
-
}
|
|
2045
|
-
|
|
2095
|
+
};
|
|
2096
|
+
var resolveRequireApproval = (impact, options) => {
|
|
2046
2097
|
if (options?.requireApproval !== void 0) {
|
|
2047
2098
|
return options.requireApproval;
|
|
2048
2099
|
}
|
|
2049
2100
|
return impact === "DESTRUCTIVE";
|
|
2050
|
-
}
|
|
2051
|
-
|
|
2101
|
+
};
|
|
2102
|
+
var buildToolExecutionContext = (options, toolCtx) => {
|
|
2052
2103
|
const tenantId = typeof toolCtx?.tenantId === "string" ? toolCtx.tenantId : options?.defaultContext?.tenantId;
|
|
2053
2104
|
const userId = typeof toolCtx?.userId === "string" ? toolCtx.userId : options?.defaultContext?.userId;
|
|
2054
2105
|
return {
|
|
@@ -2057,8 +2108,8 @@ function buildToolExecutionContext(options, toolCtx) {
|
|
|
2057
2108
|
services: options?.defaultContext?.services ?? {},
|
|
2058
2109
|
storage: options?.storage ?? options?.defaultContext?.storage
|
|
2059
2110
|
};
|
|
2060
|
-
}
|
|
2061
|
-
|
|
2111
|
+
};
|
|
2112
|
+
var defineWorkflow = (config) => {
|
|
2062
2113
|
const workflowImpact = config.impact ?? "MUTATIVE";
|
|
2063
2114
|
return {
|
|
2064
2115
|
id: config.id,
|
|
@@ -2102,7 +2153,7 @@ function defineWorkflow(config) {
|
|
|
2102
2153
|
return tool;
|
|
2103
2154
|
}
|
|
2104
2155
|
};
|
|
2105
|
-
}
|
|
2156
|
+
};
|
|
2106
2157
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2107
2158
|
0 && (module.exports = {
|
|
2108
2159
|
AvantGateBudgetExceededError,
|
|
@@ -2118,6 +2169,7 @@ function defineWorkflow(config) {
|
|
|
2118
2169
|
PromptRegistry,
|
|
2119
2170
|
PromptTemplate,
|
|
2120
2171
|
SEED_MODEL_PRICES,
|
|
2172
|
+
WorkflowAbortSignal,
|
|
2121
2173
|
WorkflowSagaRollbackError,
|
|
2122
2174
|
WorkflowValidationError,
|
|
2123
2175
|
ZenLLMControlLayer,
|
|
@@ -2128,6 +2180,7 @@ function defineWorkflow(config) {
|
|
|
2128
2180
|
defineWorkflow,
|
|
2129
2181
|
executeWorkflow,
|
|
2130
2182
|
extractAndCleanJSON,
|
|
2183
|
+
isWorkflowAbortSignal,
|
|
2131
2184
|
resolveModelPrice,
|
|
2132
2185
|
resolveModelPriceAsync,
|
|
2133
2186
|
sanitizePII,
|
package/dist/index.mjs
CHANGED
|
@@ -3,11 +3,13 @@ import {
|
|
|
3
3
|
validateWithZod
|
|
4
4
|
} from "./chunk-CO26LNFD.mjs";
|
|
5
5
|
import {
|
|
6
|
+
WorkflowAbortSignal,
|
|
6
7
|
WorkflowSagaRollbackError,
|
|
7
8
|
WorkflowValidationError,
|
|
8
9
|
defineWorkflow,
|
|
9
|
-
executeWorkflow
|
|
10
|
-
|
|
10
|
+
executeWorkflow,
|
|
11
|
+
isWorkflowAbortSignal
|
|
12
|
+
} from "./chunk-4XWO3NZW.mjs";
|
|
11
13
|
import {
|
|
12
14
|
sanitizePII
|
|
13
15
|
} from "./chunk-5VA3EKPJ.mjs";
|
|
@@ -1049,6 +1051,7 @@ export {
|
|
|
1049
1051
|
PromptRegistry,
|
|
1050
1052
|
PromptTemplate,
|
|
1051
1053
|
SEED_MODEL_PRICES,
|
|
1054
|
+
WorkflowAbortSignal,
|
|
1052
1055
|
WorkflowSagaRollbackError,
|
|
1053
1056
|
WorkflowValidationError,
|
|
1054
1057
|
AvantGateControlLayer as ZenLLMControlLayer,
|
|
@@ -1059,6 +1062,7 @@ export {
|
|
|
1059
1062
|
defineWorkflow,
|
|
1060
1063
|
executeWorkflow,
|
|
1061
1064
|
extractAndCleanJSON,
|
|
1065
|
+
isWorkflowAbortSignal,
|
|
1062
1066
|
resolveModelPrice,
|
|
1063
1067
|
resolveModelPriceAsync,
|
|
1064
1068
|
sanitizePII,
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { WorkflowConfig, WorkflowInstance } from "./types";
|
|
2
|
-
export declare
|
|
2
|
+
export declare const defineWorkflow: <TInput = any, TOutput = any>(config: WorkflowConfig<TInput, TOutput>) => WorkflowInstance<TInput, TOutput>;
|
|
3
3
|
//# sourceMappingURL=define-workflow.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"define-workflow.d.ts","sourceRoot":"","sources":["../../src/workflow/define-workflow.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAEV,cAAc,EAGd,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAmCjB,
|
|
1
|
+
{"version":3,"file":"define-workflow.d.ts","sourceRoot":"","sources":["../../src/workflow/define-workflow.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAEV,cAAc,EAGd,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAmCjB,eAAO,MAAM,cAAc,GAAI,MAAM,GAAG,GAAG,EAAE,OAAO,GAAG,GAAG,EACxD,QAAQ,cAAc,CAAC,MAAM,EAAE,OAAO,CAAC,KACtC,gBAAgB,CAAC,MAAM,EAAE,OAAO,CA8DlC,CAAA"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { WorkflowConfig, WorkflowExecutionContext, WorkflowExecutionResult } from "./types";
|
|
2
|
-
export declare
|
|
2
|
+
export declare const executeWorkflow: <TInput, TOutput>(config: WorkflowConfig<TInput, TOutput>, rawInput: TInput, context?: WorkflowExecutionContext) => Promise<WorkflowExecutionResult<TOutput>>;
|
|
3
3
|
//# sourceMappingURL=engine.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../../src/workflow/engine.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../../src/workflow/engine.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EACV,cAAc,EACd,wBAAwB,EACxB,uBAAuB,EAGxB,MAAM,SAAS,CAAC;AAyHjB,eAAO,MAAM,eAAe,GAAU,MAAM,EAAE,OAAO,EACnD,QAAQ,cAAc,CAAC,MAAM,EAAE,OAAO,CAAC,EACvC,UAAU,MAAM,EAChB,UAAU,wBAAwB,KACjC,OAAO,CAAC,uBAAuB,CAAC,OAAO,CAAC,CAmG1C,CAAA"}
|
|
@@ -21,4 +21,11 @@ export declare class WorkflowValidationError extends Error {
|
|
|
21
21
|
readonly issues: unknown[];
|
|
22
22
|
constructor(workflowId: string, issues: unknown[]);
|
|
23
23
|
}
|
|
24
|
+
export declare class WorkflowAbortSignal<TPayload = unknown> extends Error {
|
|
25
|
+
readonly __isWorkflowAbort = true;
|
|
26
|
+
readonly reason: string;
|
|
27
|
+
readonly payload?: TPayload;
|
|
28
|
+
constructor(reason: string, payload?: TPayload);
|
|
29
|
+
}
|
|
30
|
+
export declare const isWorkflowAbortSignal: (value: unknown) => value is WorkflowAbortSignal;
|
|
24
31
|
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/workflow/errors.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,wBAAwB;IACvC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,OAAO,CAAC;CAChB;AAED,qBAAa,yBAA0B,SAAQ,KAAK;IAClD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;IAChC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,EAAE,CAAC;IACpC,QAAQ,CAAC,mBAAmB,EAAE,wBAAwB,EAAE,CAAC;gBAE7C,MAAM,EAAE;QAClB,UAAU,EAAE,MAAM,CAAC;QACnB,YAAY,EAAE,MAAM,CAAC;QACrB,aAAa,EAAE,OAAO,CAAC;QACvB,gBAAgB,EAAE,MAAM,EAAE,CAAC;QAC3B,mBAAmB,CAAC,EAAE,wBAAwB,EAAE,CAAC;KAClD;CAiBF;AAED,qBAAa,uBAAwB,SAAQ,KAAK;IAChD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC;gBAEf,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE;CAMlD"}
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/workflow/errors.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,wBAAwB;IACvC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,OAAO,CAAC;CAChB;AAED,qBAAa,yBAA0B,SAAQ,KAAK;IAClD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;IAChC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,EAAE,CAAC;IACpC,QAAQ,CAAC,mBAAmB,EAAE,wBAAwB,EAAE,CAAC;gBAE7C,MAAM,EAAE;QAClB,UAAU,EAAE,MAAM,CAAC;QACnB,YAAY,EAAE,MAAM,CAAC;QACrB,aAAa,EAAE,OAAO,CAAC;QACvB,gBAAgB,EAAE,MAAM,EAAE,CAAC;QAC3B,mBAAmB,CAAC,EAAE,wBAAwB,EAAE,CAAC;KAClD;CAiBF;AAED,qBAAa,uBAAwB,SAAQ,KAAK;IAChD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC;gBAEf,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE;CAMlD;AAED,qBAAa,mBAAmB,CAAC,QAAQ,GAAG,OAAO,CAAE,SAAQ,KAAK;IAChE,QAAQ,CAAC,iBAAiB,QAAQ;IAClC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC;gBAEhB,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ;CAM/C;AAED,eAAO,MAAM,qBAAqB,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,mBAM/D,CAAC"}
|
package/dist/workflow/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { defineWorkflow } from "./define-workflow";
|
|
2
2
|
export { executeWorkflow } from "./engine";
|
|
3
|
-
export { WorkflowSagaRollbackError, WorkflowValidationError, type FailedCompensationRecord, } from "./errors";
|
|
3
|
+
export { WorkflowSagaRollbackError, WorkflowValidationError, WorkflowAbortSignal, isWorkflowAbortSignal, type FailedCompensationRecord, } from "./errors";
|
|
4
4
|
export type { WorkflowAsToolOptions, WorkflowConfig, WorkflowExecutionContext, WorkflowExecutionResult, WorkflowInstance, WorkflowStepConfig, WorkflowStepContext, } from "./types";
|
|
5
5
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/workflow/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EACL,yBAAyB,EACzB,uBAAuB,EACvB,KAAK,wBAAwB,GAC9B,MAAM,UAAU,CAAC;AAClB,YAAY,EACV,qBAAqB,EACrB,cAAc,EACd,wBAAwB,EACxB,uBAAuB,EACvB,gBAAgB,EAChB,kBAAkB,EAClB,mBAAmB,GACpB,MAAM,SAAS,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/workflow/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EACL,yBAAyB,EACzB,uBAAuB,EACvB,mBAAmB,EACnB,qBAAqB,EACrB,KAAK,wBAAwB,GAC9B,MAAM,UAAU,CAAC;AAClB,YAAY,EACV,qBAAqB,EACrB,cAAc,EACd,wBAAwB,EACxB,uBAAuB,EACvB,gBAAgB,EAChB,kBAAkB,EAClB,mBAAmB,GACpB,MAAM,SAAS,CAAC"}
|