avantgate 1.2.2 → 1.4.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 +8 -3
- package/dist/agent/index.js +54 -14
- package/dist/agent/index.mjs +60 -289
- package/dist/agent/isolated-tool.d.ts.map +1 -1
- package/dist/agent/registry.d.ts +7 -2
- package/dist/agent/registry.d.ts.map +1 -1
- package/dist/agent/strategy.d.ts +18 -2
- package/dist/agent/strategy.d.ts.map +1 -1
- package/dist/agent/types.d.ts +13 -6
- package/dist/agent/types.d.ts.map +1 -1
- package/dist/chunk-5VA3EKPJ.mjs +344 -0
- package/dist/chunk-DTM2OLCG.mjs +252 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +478 -0
- package/dist/index.mjs +13 -3
- package/dist/workflow/define-workflow.d.ts +3 -0
- package/dist/workflow/define-workflow.d.ts.map +1 -0
- package/dist/workflow/engine.d.ts +3 -0
- package/dist/workflow/engine.d.ts.map +1 -0
- package/dist/workflow/errors.d.ts +24 -0
- package/dist/workflow/errors.d.ts.map +1 -0
- package/dist/workflow/index.d.ts +5 -0
- package/dist/workflow/index.d.ts.map +1 -0
- package/dist/workflow/index.js +558 -0
- package/dist/workflow/index.mjs +13 -0
- package/dist/workflow/types.d.ts +64 -0
- package/dist/workflow/types.d.ts.map +1 -0
- package/package.json +13 -4
- package/dist/chunk-DVCF4CSV.mjs +0 -56
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import {
|
|
2
|
+
StepSuspendedError,
|
|
3
|
+
createIsolatedTool
|
|
4
|
+
} from "./chunk-5VA3EKPJ.mjs";
|
|
5
|
+
|
|
6
|
+
// src/workflow/errors.ts
|
|
7
|
+
var WorkflowSagaRollbackError = class extends Error {
|
|
8
|
+
workflowId;
|
|
9
|
+
failedStepId;
|
|
10
|
+
originalError;
|
|
11
|
+
compensatedSteps;
|
|
12
|
+
failedCompensations;
|
|
13
|
+
constructor(params) {
|
|
14
|
+
const errorDetails = params.originalError instanceof Error ? params.originalError.message : String(params.originalError);
|
|
15
|
+
super(
|
|
16
|
+
`\xC9chec de l'\xE9tape '${params.failedStepId}' dans le workflow '${params.workflowId}'. Rollback Saga ex\xE9cut\xE9 sur [${params.compensatedSteps.join(", ")}]. Cause initiale: ${errorDetails}`
|
|
17
|
+
);
|
|
18
|
+
this.name = "WorkflowSagaRollbackError";
|
|
19
|
+
this.workflowId = params.workflowId;
|
|
20
|
+
this.failedStepId = params.failedStepId;
|
|
21
|
+
this.originalError = params.originalError;
|
|
22
|
+
this.compensatedSteps = params.compensatedSteps;
|
|
23
|
+
this.failedCompensations = params.failedCompensations ?? [];
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var WorkflowValidationError = class extends Error {
|
|
27
|
+
workflowId;
|
|
28
|
+
issues;
|
|
29
|
+
constructor(workflowId, issues) {
|
|
30
|
+
super(`Validation des entr\xE9es \xE9chou\xE9e pour le workflow '${workflowId}'.`);
|
|
31
|
+
this.name = "WorkflowValidationError";
|
|
32
|
+
this.workflowId = workflowId;
|
|
33
|
+
this.issues = issues;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// src/workflow/engine.ts
|
|
38
|
+
function generateRunId(workflowId) {
|
|
39
|
+
const timestamp = Date.now();
|
|
40
|
+
const randomSuffix = Math.random().toString(36).substring(2, 9);
|
|
41
|
+
return `run_${workflowId}_${timestamp}_${randomSuffix}`;
|
|
42
|
+
}
|
|
43
|
+
function validateInput(config, input) {
|
|
44
|
+
const parseResult = config.inputSchema.safeParse(input);
|
|
45
|
+
if (!parseResult.success) {
|
|
46
|
+
throw new WorkflowValidationError(config.id, parseResult.error.issues);
|
|
47
|
+
}
|
|
48
|
+
return parseResult.data;
|
|
49
|
+
}
|
|
50
|
+
function createStepContext(params) {
|
|
51
|
+
const { workflowId, stepId, runId, context, stepResults } = params;
|
|
52
|
+
return {
|
|
53
|
+
workflowId,
|
|
54
|
+
stepId,
|
|
55
|
+
runId,
|
|
56
|
+
tenantId: context?.tenantId,
|
|
57
|
+
userId: context?.userId,
|
|
58
|
+
services: context?.services ?? {},
|
|
59
|
+
storage: context?.storage,
|
|
60
|
+
getStepResult: (id) => stepResults[id],
|
|
61
|
+
waitForApproval: async (options) => {
|
|
62
|
+
if (context?.storage) {
|
|
63
|
+
const existing = await context.storage.getStep(workflowId, stepId);
|
|
64
|
+
if (existing && existing.status === "COMPLETED") {
|
|
65
|
+
return existing.result ?? options?.defaultResult;
|
|
66
|
+
}
|
|
67
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
68
|
+
await context.storage.saveStep({
|
|
69
|
+
workflowId,
|
|
70
|
+
stepId,
|
|
71
|
+
runId,
|
|
72
|
+
status: "WAITING_APPROVAL",
|
|
73
|
+
metadata: options?.metadata,
|
|
74
|
+
createdAt: now,
|
|
75
|
+
updatedAt: now
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
throw new StepSuspendedError(stepId, workflowId, options?.prompt);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
async function rollbackSaga(params) {
|
|
83
|
+
const {
|
|
84
|
+
workflowId,
|
|
85
|
+
failedStepId,
|
|
86
|
+
originalError,
|
|
87
|
+
completedSteps,
|
|
88
|
+
input,
|
|
89
|
+
runId,
|
|
90
|
+
context,
|
|
91
|
+
stepResults
|
|
92
|
+
} = params;
|
|
93
|
+
const compensatedSteps = [];
|
|
94
|
+
const failedCompensations = [];
|
|
95
|
+
for (let i = completedSteps.length - 1; i >= 0; i--) {
|
|
96
|
+
const { step, result } = completedSteps[i];
|
|
97
|
+
if (!step.compensate) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const stepCtx = createStepContext({
|
|
101
|
+
workflowId,
|
|
102
|
+
stepId: step.id,
|
|
103
|
+
runId,
|
|
104
|
+
context,
|
|
105
|
+
stepResults
|
|
106
|
+
});
|
|
107
|
+
try {
|
|
108
|
+
await step.compensate(result, input, stepCtx);
|
|
109
|
+
compensatedSteps.push(step.id);
|
|
110
|
+
if (context?.storage) {
|
|
111
|
+
await context.storage.updateStepStatus(workflowId, step.id, "FAILED", {
|
|
112
|
+
metadata: { compensated: true }
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
} catch (compensationError) {
|
|
116
|
+
failedCompensations.push({ stepId: step.id, error: compensationError });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
throw new WorkflowSagaRollbackError({
|
|
120
|
+
workflowId,
|
|
121
|
+
failedStepId,
|
|
122
|
+
originalError,
|
|
123
|
+
compensatedSteps,
|
|
124
|
+
failedCompensations
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
async function executeWorkflow(config, rawInput, context) {
|
|
128
|
+
const input = validateInput(config, rawInput);
|
|
129
|
+
const runId = context?.runId ?? generateRunId(config.id);
|
|
130
|
+
const stepResults = {};
|
|
131
|
+
const completedSteps = [];
|
|
132
|
+
for (const step of config.steps) {
|
|
133
|
+
const stepCtx = createStepContext({
|
|
134
|
+
workflowId: config.id,
|
|
135
|
+
stepId: step.id,
|
|
136
|
+
runId,
|
|
137
|
+
context,
|
|
138
|
+
stepResults
|
|
139
|
+
});
|
|
140
|
+
try {
|
|
141
|
+
const result = await step.execute(input, stepCtx);
|
|
142
|
+
stepResults[step.id] = result;
|
|
143
|
+
completedSteps.push({ step, result });
|
|
144
|
+
if (context?.storage) {
|
|
145
|
+
await context.storage.updateStepStatus(config.id, step.id, "COMPLETED", { result });
|
|
146
|
+
}
|
|
147
|
+
} catch (error) {
|
|
148
|
+
if (error instanceof StepSuspendedError) {
|
|
149
|
+
return {
|
|
150
|
+
workflowId: config.id,
|
|
151
|
+
runId,
|
|
152
|
+
status: "WAITING_APPROVAL",
|
|
153
|
+
stepResults
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
await rollbackSaga({
|
|
157
|
+
workflowId: config.id,
|
|
158
|
+
failedStepId: step.id,
|
|
159
|
+
originalError: error,
|
|
160
|
+
completedSteps,
|
|
161
|
+
input,
|
|
162
|
+
runId,
|
|
163
|
+
context,
|
|
164
|
+
stepResults
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const finalOutput = config.outputDto ? config.outputDto(stepResults, input) : stepResults;
|
|
169
|
+
return {
|
|
170
|
+
workflowId: config.id,
|
|
171
|
+
runId,
|
|
172
|
+
status: "COMPLETED",
|
|
173
|
+
stepResults,
|
|
174
|
+
output: finalOutput
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// src/workflow/define-workflow.ts
|
|
179
|
+
function resolveWorkflowImpact(config, options) {
|
|
180
|
+
if (options?.impact) {
|
|
181
|
+
return options.impact;
|
|
182
|
+
}
|
|
183
|
+
return config.impact ?? "MUTATIVE";
|
|
184
|
+
}
|
|
185
|
+
function resolveRequireApproval(impact, options) {
|
|
186
|
+
if (options?.requireApproval !== void 0) {
|
|
187
|
+
return options.requireApproval;
|
|
188
|
+
}
|
|
189
|
+
return impact === "DESTRUCTIVE";
|
|
190
|
+
}
|
|
191
|
+
function buildToolExecutionContext(options, toolCtx) {
|
|
192
|
+
const tenantId = typeof toolCtx?.tenantId === "string" ? toolCtx.tenantId : options?.defaultContext?.tenantId;
|
|
193
|
+
const userId = typeof toolCtx?.userId === "string" ? toolCtx.userId : options?.defaultContext?.userId;
|
|
194
|
+
return {
|
|
195
|
+
tenantId,
|
|
196
|
+
userId,
|
|
197
|
+
services: options?.defaultContext?.services ?? {},
|
|
198
|
+
storage: options?.storage ?? options?.defaultContext?.storage
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function defineWorkflow(config) {
|
|
202
|
+
const workflowImpact = config.impact ?? "MUTATIVE";
|
|
203
|
+
return {
|
|
204
|
+
id: config.id,
|
|
205
|
+
name: config.name,
|
|
206
|
+
description: config.description,
|
|
207
|
+
impact: workflowImpact,
|
|
208
|
+
roles: config.roles,
|
|
209
|
+
config,
|
|
210
|
+
execute: async (input, context) => {
|
|
211
|
+
return await executeWorkflow(config, input, context);
|
|
212
|
+
},
|
|
213
|
+
asTool: (options) => {
|
|
214
|
+
const impact = resolveWorkflowImpact(config, options);
|
|
215
|
+
const requireApproval = resolveRequireApproval(impact, options);
|
|
216
|
+
const tool = createIsolatedTool({
|
|
217
|
+
name: config.id,
|
|
218
|
+
description: config.description ?? config.name,
|
|
219
|
+
roles: config.roles,
|
|
220
|
+
impact,
|
|
221
|
+
requireApproval,
|
|
222
|
+
parameters: config.inputSchema,
|
|
223
|
+
invalidationTags: typeof config.invalidationTags === "function" ? async (args, res) => {
|
|
224
|
+
const resolver = config.invalidationTags;
|
|
225
|
+
return await resolver(args, res ?? {});
|
|
226
|
+
} : config.invalidationTags,
|
|
227
|
+
execute: async (args, toolCtx) => {
|
|
228
|
+
const executionContext = buildToolExecutionContext(options, toolCtx);
|
|
229
|
+
const result = await executeWorkflow(config, args, executionContext);
|
|
230
|
+
return result.output;
|
|
231
|
+
},
|
|
232
|
+
llmDto: (result) => result
|
|
233
|
+
});
|
|
234
|
+
Object.assign(tool, {
|
|
235
|
+
_isWorkflow: true,
|
|
236
|
+
workflowId: config.id,
|
|
237
|
+
workflowConfig: {
|
|
238
|
+
totalSteps: config.steps.length,
|
|
239
|
+
impact
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
return tool;
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export {
|
|
248
|
+
WorkflowSagaRollbackError,
|
|
249
|
+
WorkflowValidationError,
|
|
250
|
+
executeWorkflow,
|
|
251
|
+
defineWorkflow
|
|
252
|
+
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC;AACrC,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,yBAAyB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC;AACrC,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,yBAAyB,CAAC;AACxC,cAAc,YAAY,CAAC"}
|