@agentropy/workflow-conductor 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +36 -2
- package/dist/index.d.ts +91 -0
- package/dist/index.js +223 -0
- package/package.json +43 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @agentropy/workflow-conductor
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Conductor workflow engine for Agentropy.** Part of [Agentropy](https://github.com/bassemZohdy/agentropy), a TypeScript framework for durable, capability-driven multi-agent work. Version 0.x: the API can still change between minor versions.
|
|
4
|
+
|
|
5
|
+
A `WorkflowEngine` adapter for Conductor over its REST API: it starts a run per task with an idempotent correlation ID, reports finished runs through webhooks or polling, and keeps each run bound to its tenant. It passes the shared workflow-engine conformance suite.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @agentropy/workflow-conductor
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Node 24 or newer.
|
|
14
|
+
|
|
15
|
+
## Use
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { ConductorWorkflowEngine } from '@agentropy/workflow-conductor';
|
|
19
|
+
|
|
20
|
+
const engine = new ConductorWorkflowEngine({
|
|
21
|
+
baseUrl: 'https://conductor.internal',
|
|
22
|
+
fetch,
|
|
23
|
+
headers: async () => ({ 'x-authorization': await token() }),
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Pass the engine to `createLocalApp` through its `workflow` option.
|
|
28
|
+
|
|
29
|
+
## Learn more
|
|
30
|
+
|
|
31
|
+
- [Conformance kit](https://github.com/bassemZohdy/agentropy/blob/main/docs/conformance-kit.md)
|
|
32
|
+
- [Capability contract](https://github.com/bassemZohdy/agentropy/blob/main/docs/contracts/capability-v1.md)
|
|
33
|
+
- [Every export](https://github.com/bassemZohdy/agentropy/blob/main/docs/api.md)
|
|
34
|
+
|
|
35
|
+
## License
|
|
36
|
+
|
|
37
|
+
Apache-2.0. See the [repository](https://github.com/bassemZohdy/agentropy) for the source, issues and contributing guide.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @agentropy/workflow-conductor — WorkflowEngine adapter for Conductor (Conductor OSS and Orkes Conductor) over
|
|
3
|
+
* its REST API (FEAT-028.6, M3-01). The second workflow adapter after the in-memory fake; it shows how an external
|
|
4
|
+
* engine plugs into the WorkflowEngine SPI without Agentropy taking on any workflow semantics.
|
|
5
|
+
*
|
|
6
|
+
* Mapping:
|
|
7
|
+
* - `start` → `POST /api/workflow` with `{ name, version?, input, correlationId }`. The correlation ID is
|
|
8
|
+
* `<tenantId>:<idempotencyKey>`, which also scopes reads to the tenant.
|
|
9
|
+
* - Idempotency (`idempotency` option): `server` (default) also sends `idempotencyKey` +
|
|
10
|
+
* `idempotencyStrategy: RETURN_EXISTING` (enforced by servers that support it, e.g. Orkes); `lookup` first asks
|
|
11
|
+
* `GET /api/workflow/{name}/correlated/{correlationId}` and reuses an existing run (for servers without
|
|
12
|
+
* idempotency keys; two truly concurrent starts can still race).
|
|
13
|
+
* - `status` → `GET /api/workflow/{id}?includeTasks=false`; RUNNING/PAUSED → running, COMPLETED → completed
|
|
14
|
+
* (`output`), FAILED → failed `TaskFailed`, TIMED_OUT → failed `Timeout`, TERMINATED → failed `TaskCancelled`.
|
|
15
|
+
* A run of another tenant, or an unknown run, is `failed` with code `NotFound` (no disclosure).
|
|
16
|
+
* - Completion: Conductor reports through a status webhook (`handleStatusEvent`) or the adapter polls the runs it
|
|
17
|
+
* started (`poll`). `reconcile(runIds)` re-checks runs after a restart, since the adapter keeps no state of its
|
|
18
|
+
* own. Each terminal run is reported to `onFinished` once per adapter instance; the runtime's event ingress
|
|
19
|
+
* deduplicates across instances.
|
|
20
|
+
*
|
|
21
|
+
* Live findings (Conductor OSS 3.15.0 standalone, `conductor-live.test.ts`): the start request's `idempotencyKey` is
|
|
22
|
+
* accepted but not enforced (the same key starts another run), and the correlation search returned nothing for a
|
|
23
|
+
* started run (still empty 20 s later), so `lookup` finds no earlier run there. Once-per-key is therefore
|
|
24
|
+
* the runtime's job: `workflow.start` is journaled by its step key (`workflowOperationHandlers` with a store).
|
|
25
|
+
* An engine that enforces the key (Orkes) makes `server` mode exact.
|
|
26
|
+
*
|
|
27
|
+
* Not supported by the SPI and therefore not mapped: pause/resume, terminate, task-level visibility.
|
|
28
|
+
*/
|
|
29
|
+
import { type WorkflowEngine, type WorkflowRun } from '@agentropy/core';
|
|
30
|
+
export interface ConductorHttpResponse {
|
|
31
|
+
status: number;
|
|
32
|
+
text(): Promise<string>;
|
|
33
|
+
}
|
|
34
|
+
/** The only transport the adapter needs; `globalThis.fetch` fits. */
|
|
35
|
+
export type ConductorFetch = (url: string, init: {
|
|
36
|
+
method: string;
|
|
37
|
+
headers: Record<string, string>;
|
|
38
|
+
body?: string;
|
|
39
|
+
}) => Promise<ConductorHttpResponse>;
|
|
40
|
+
export interface ConductorEngineOptions {
|
|
41
|
+
/** Base URL of the Conductor server, e.g. `https://conductor.internal`. */
|
|
42
|
+
baseUrl: string;
|
|
43
|
+
fetch: ConductorFetch;
|
|
44
|
+
/** Extra headers per request, e.g. an Orkes `X-Authorization` token. Values are never logged or returned. */
|
|
45
|
+
headers?: () => Promise<Record<string, string>> | Record<string, string>;
|
|
46
|
+
/** Workflow version per name; latest when absent. */
|
|
47
|
+
versions?: Record<string, number>;
|
|
48
|
+
idempotency?: 'server' | 'lookup';
|
|
49
|
+
/**
|
|
50
|
+
* Finished runs remembered so that a repeated status event or poll does not report one twice (default 10000, the
|
|
51
|
+
* oldest forgotten first). A repeated event for a run older than that is reported again.
|
|
52
|
+
*/
|
|
53
|
+
maxReported?: number;
|
|
54
|
+
}
|
|
55
|
+
export declare class ConductorWorkflowEngine implements WorkflowEngine {
|
|
56
|
+
private readonly o;
|
|
57
|
+
readonly id = "conductor";
|
|
58
|
+
private readonly listeners;
|
|
59
|
+
/** Runs started by this instance that have not finished yet: runId → tenant. */
|
|
60
|
+
private readonly open;
|
|
61
|
+
private readonly reported;
|
|
62
|
+
private readonly maxReported;
|
|
63
|
+
constructor(o: ConductorEngineOptions);
|
|
64
|
+
private call;
|
|
65
|
+
start(workflow: string, input: unknown, ctx: {
|
|
66
|
+
tenantId: string;
|
|
67
|
+
taskId: string;
|
|
68
|
+
idempotencyKey: string;
|
|
69
|
+
}): Promise<WorkflowRun>;
|
|
70
|
+
/** The run, `'gone'` when Conductor says it does not exist (deleted or archived), or undefined when unknown. */
|
|
71
|
+
private fetchRun;
|
|
72
|
+
private owned;
|
|
73
|
+
status(runId: string, ctx: {
|
|
74
|
+
tenantId: string;
|
|
75
|
+
}): Promise<WorkflowRun>;
|
|
76
|
+
onFinished(listener: (run: WorkflowRun & {
|
|
77
|
+
tenantId: string;
|
|
78
|
+
}) => void): void;
|
|
79
|
+
/**
|
|
80
|
+
* Conductor workflow status listener (webhook) payload. Untrusted input: the run is re-read from Conductor
|
|
81
|
+
* before anything is reported, so a forged payload cannot complete a run.
|
|
82
|
+
*/
|
|
83
|
+
handleStatusEvent(payload: unknown): Promise<boolean>;
|
|
84
|
+
/** Re-check every run this instance started and report the finished ones. Returns how many finished. */
|
|
85
|
+
poll(): Promise<number>;
|
|
86
|
+
/** After a restart: re-check runs recorded elsewhere (e.g. in task checkpoints). */
|
|
87
|
+
reconcile(runIds: string[]): Promise<number>;
|
|
88
|
+
private track;
|
|
89
|
+
private check;
|
|
90
|
+
private report;
|
|
91
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @agentropy/workflow-conductor — WorkflowEngine adapter for Conductor (Conductor OSS and Orkes Conductor) over
|
|
3
|
+
* its REST API (FEAT-028.6, M3-01). The second workflow adapter after the in-memory fake; it shows how an external
|
|
4
|
+
* engine plugs into the WorkflowEngine SPI without Agentropy taking on any workflow semantics.
|
|
5
|
+
*
|
|
6
|
+
* Mapping:
|
|
7
|
+
* - `start` → `POST /api/workflow` with `{ name, version?, input, correlationId }`. The correlation ID is
|
|
8
|
+
* `<tenantId>:<idempotencyKey>`, which also scopes reads to the tenant.
|
|
9
|
+
* - Idempotency (`idempotency` option): `server` (default) also sends `idempotencyKey` +
|
|
10
|
+
* `idempotencyStrategy: RETURN_EXISTING` (enforced by servers that support it, e.g. Orkes); `lookup` first asks
|
|
11
|
+
* `GET /api/workflow/{name}/correlated/{correlationId}` and reuses an existing run (for servers without
|
|
12
|
+
* idempotency keys; two truly concurrent starts can still race).
|
|
13
|
+
* - `status` → `GET /api/workflow/{id}?includeTasks=false`; RUNNING/PAUSED → running, COMPLETED → completed
|
|
14
|
+
* (`output`), FAILED → failed `TaskFailed`, TIMED_OUT → failed `Timeout`, TERMINATED → failed `TaskCancelled`.
|
|
15
|
+
* A run of another tenant, or an unknown run, is `failed` with code `NotFound` (no disclosure).
|
|
16
|
+
* - Completion: Conductor reports through a status webhook (`handleStatusEvent`) or the adapter polls the runs it
|
|
17
|
+
* started (`poll`). `reconcile(runIds)` re-checks runs after a restart, since the adapter keeps no state of its
|
|
18
|
+
* own. Each terminal run is reported to `onFinished` once per adapter instance; the runtime's event ingress
|
|
19
|
+
* deduplicates across instances.
|
|
20
|
+
*
|
|
21
|
+
* Live findings (Conductor OSS 3.15.0 standalone, `conductor-live.test.ts`): the start request's `idempotencyKey` is
|
|
22
|
+
* accepted but not enforced (the same key starts another run), and the correlation search returned nothing for a
|
|
23
|
+
* started run (still empty 20 s later), so `lookup` finds no earlier run there. Once-per-key is therefore
|
|
24
|
+
* the runtime's job: `workflow.start` is journaled by its step key (`workflowOperationHandlers` with a store).
|
|
25
|
+
* An engine that enforces the key (Orkes) makes `server` mode exact.
|
|
26
|
+
*
|
|
27
|
+
* Not supported by the SPI and therefore not mapped: pause/resume, terminate, task-level visibility.
|
|
28
|
+
*/
|
|
29
|
+
import { AgentropyError, } from '@agentropy/core';
|
|
30
|
+
/**
|
|
31
|
+
* The tenant half of a correlation ID is percent-encoded, so the first `:` always ends it: tenant IDs may contain
|
|
32
|
+
* `:`, and `a:b` must not be taken for tenant `a`. IDs of tenants without special characters are unchanged.
|
|
33
|
+
*/
|
|
34
|
+
const tenantPrefix = (tenantId) => encodeURIComponent(tenantId);
|
|
35
|
+
const tenantOfCorrelation = (correlationId) => {
|
|
36
|
+
const prefix = correlationId?.split(':', 1)[0];
|
|
37
|
+
if (!prefix || prefix === correlationId)
|
|
38
|
+
return undefined;
|
|
39
|
+
try {
|
|
40
|
+
return decodeURIComponent(prefix);
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const TERMINAL_CODE = {
|
|
47
|
+
FAILED: 'TaskFailed',
|
|
48
|
+
TIMED_OUT: 'Timeout',
|
|
49
|
+
TERMINATED: 'TaskCancelled',
|
|
50
|
+
};
|
|
51
|
+
export class ConductorWorkflowEngine {
|
|
52
|
+
o;
|
|
53
|
+
id = 'conductor';
|
|
54
|
+
listeners = [];
|
|
55
|
+
/** Runs started by this instance that have not finished yet: runId → tenant. */
|
|
56
|
+
open = new Map();
|
|
57
|
+
reported = new Set();
|
|
58
|
+
maxReported;
|
|
59
|
+
constructor(o) {
|
|
60
|
+
this.o = o;
|
|
61
|
+
this.maxReported = Math.max(1, Math.floor(o.maxReported ?? 10_000));
|
|
62
|
+
if (!/^https?:\/\//.test(o.baseUrl))
|
|
63
|
+
throw new AgentropyError('InvalidConfiguration', 'Conductor baseUrl must be an http(s) URL');
|
|
64
|
+
}
|
|
65
|
+
async call(method, path, body) {
|
|
66
|
+
let res;
|
|
67
|
+
try {
|
|
68
|
+
res = await this.o.fetch(`${this.o.baseUrl.replace(/\/$/, '')}${path}`, {
|
|
69
|
+
method,
|
|
70
|
+
headers: {
|
|
71
|
+
accept: 'application/json',
|
|
72
|
+
...(body !== undefined ? { 'content-type': 'application/json' } : {}),
|
|
73
|
+
...(await this.o.headers?.()),
|
|
74
|
+
},
|
|
75
|
+
...(body !== undefined ? { body: JSON.stringify(body) } : {}),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
throw new AgentropyError('ProviderUnavailable', 'Conductor is unreachable', { details: { provider: this.id } });
|
|
80
|
+
}
|
|
81
|
+
const text = await res.text();
|
|
82
|
+
if (res.status === 401 || res.status === 403)
|
|
83
|
+
throw new AgentropyError('AuthorityExpired', 'Conductor refused the credentials', { details: { provider: this.id, kind: 'credential' } });
|
|
84
|
+
if (res.status >= 500 || res.status === 429)
|
|
85
|
+
throw new AgentropyError('ProviderUnavailable', 'Conductor error', {
|
|
86
|
+
details: { provider: this.id, status: res.status },
|
|
87
|
+
});
|
|
88
|
+
let parsed = text;
|
|
89
|
+
try {
|
|
90
|
+
parsed = JSON.parse(text);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
/* plain-text responses (the start call returns the workflow ID as text) */
|
|
94
|
+
}
|
|
95
|
+
return { status: res.status, body: parsed };
|
|
96
|
+
}
|
|
97
|
+
async start(workflow, input, ctx) {
|
|
98
|
+
const correlationId = `${tenantPrefix(ctx.tenantId)}:${ctx.idempotencyKey}`;
|
|
99
|
+
const name = encodeURIComponent(workflow);
|
|
100
|
+
if (this.o.idempotency === 'lookup') {
|
|
101
|
+
const found = await this.call('GET', `/api/workflow/${name}/correlated/${encodeURIComponent(correlationId)}?includeClosed=true&includeTasks=false`);
|
|
102
|
+
const existing = Array.isArray(found.body) ? found.body[0] : undefined;
|
|
103
|
+
if (existing)
|
|
104
|
+
return this.track(existing, ctx.tenantId);
|
|
105
|
+
}
|
|
106
|
+
const version = this.o.versions?.[workflow];
|
|
107
|
+
const res = await this.call('POST', '/api/workflow', {
|
|
108
|
+
name: workflow,
|
|
109
|
+
...(version !== undefined ? { version } : {}),
|
|
110
|
+
input: input ?? {},
|
|
111
|
+
correlationId,
|
|
112
|
+
...(this.o.idempotency !== 'lookup'
|
|
113
|
+
? {
|
|
114
|
+
idempotencyKey: correlationId,
|
|
115
|
+
idempotencyStrategy: 'RETURN_EXISTING',
|
|
116
|
+
}
|
|
117
|
+
: {}),
|
|
118
|
+
});
|
|
119
|
+
if (res.status === 404)
|
|
120
|
+
throw new AgentropyError('CapabilityUnavailable', `Workflow ${workflow} is not registered in Conductor`, { details: { provider: this.id } });
|
|
121
|
+
if (res.status >= 400 || typeof res.body !== 'string' || !res.body)
|
|
122
|
+
throw new AgentropyError('Internal', 'Conductor rejected the start', {
|
|
123
|
+
details: { provider: this.id, status: res.status },
|
|
124
|
+
});
|
|
125
|
+
const runId = res.body.trim().replace(/^"|"$/g, '');
|
|
126
|
+
this.open.set(runId, ctx.tenantId);
|
|
127
|
+
return { runId, state: 'running' };
|
|
128
|
+
}
|
|
129
|
+
/** The run, `'gone'` when Conductor says it does not exist (deleted or archived), or undefined when unknown. */
|
|
130
|
+
async fetchRun(runId) {
|
|
131
|
+
const res = await this.call('GET', `/api/workflow/${encodeURIComponent(runId)}?includeTasks=false`);
|
|
132
|
+
if (res.status === 404)
|
|
133
|
+
return 'gone';
|
|
134
|
+
if (typeof res.body !== 'object' || res.body === null)
|
|
135
|
+
return undefined;
|
|
136
|
+
return res.body;
|
|
137
|
+
}
|
|
138
|
+
owned(w, tenantId) {
|
|
139
|
+
return (typeof w === 'object' && tenantOfCorrelation(w.correlationId) === tenantId);
|
|
140
|
+
}
|
|
141
|
+
async status(runId, ctx) {
|
|
142
|
+
const w = await this.fetchRun(runId);
|
|
143
|
+
if (typeof w !== 'object' || !this.owned(w, ctx.tenantId))
|
|
144
|
+
return { runId, state: 'failed', code: 'NotFound' };
|
|
145
|
+
return toRun(w);
|
|
146
|
+
}
|
|
147
|
+
onFinished(listener) {
|
|
148
|
+
this.listeners.push(listener);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Conductor workflow status listener (webhook) payload. Untrusted input: the run is re-read from Conductor
|
|
152
|
+
* before anything is reported, so a forged payload cannot complete a run.
|
|
153
|
+
*/
|
|
154
|
+
async handleStatusEvent(payload) {
|
|
155
|
+
const id = payload?.workflowId;
|
|
156
|
+
if (typeof id !== 'string' || !id)
|
|
157
|
+
return false;
|
|
158
|
+
return this.check(id);
|
|
159
|
+
}
|
|
160
|
+
/** Re-check every run this instance started and report the finished ones. Returns how many finished. */
|
|
161
|
+
async poll() {
|
|
162
|
+
let n = 0;
|
|
163
|
+
for (const runId of [...this.open.keys()])
|
|
164
|
+
if (await this.check(runId))
|
|
165
|
+
n++;
|
|
166
|
+
return n;
|
|
167
|
+
}
|
|
168
|
+
/** After a restart: re-check runs recorded elsewhere (e.g. in task checkpoints). */
|
|
169
|
+
async reconcile(runIds) {
|
|
170
|
+
let n = 0;
|
|
171
|
+
for (const runId of runIds)
|
|
172
|
+
if (await this.check(runId))
|
|
173
|
+
n++;
|
|
174
|
+
return n;
|
|
175
|
+
}
|
|
176
|
+
track(w, tenantId) {
|
|
177
|
+
const run = toRun(w);
|
|
178
|
+
if (run.state === 'running')
|
|
179
|
+
this.open.set(w.workflowId, tenantId);
|
|
180
|
+
return run;
|
|
181
|
+
}
|
|
182
|
+
async check(runId) {
|
|
183
|
+
const w = await this.fetchRun(runId);
|
|
184
|
+
if (!w)
|
|
185
|
+
return false;
|
|
186
|
+
if (w === 'gone') {
|
|
187
|
+
// A run this instance started that Conductor no longer has can never finish: report it, once, as failed. A
|
|
188
|
+
// status event or a reconcile list names runs of unknown tenants and is never trusted to say so.
|
|
189
|
+
const tenantId = this.open.get(runId);
|
|
190
|
+
if (tenantId === undefined)
|
|
191
|
+
return false;
|
|
192
|
+
return this.report({ runId, state: 'failed', code: 'NotFound' }, tenantId);
|
|
193
|
+
}
|
|
194
|
+
const tenantId = tenantOfCorrelation(w.correlationId);
|
|
195
|
+
const run = toRun(w);
|
|
196
|
+
if (run.state === 'running' || !tenantId)
|
|
197
|
+
return false;
|
|
198
|
+
return this.report(run, tenantId);
|
|
199
|
+
}
|
|
200
|
+
report(run, tenantId) {
|
|
201
|
+
this.open.delete(run.runId);
|
|
202
|
+
if (this.reported.has(run.runId))
|
|
203
|
+
return false;
|
|
204
|
+
this.reported.add(run.runId);
|
|
205
|
+
if (this.reported.size > this.maxReported)
|
|
206
|
+
this.reported.delete(this.reported.values().next().value);
|
|
207
|
+
for (const l of this.listeners)
|
|
208
|
+
l({ ...run, tenantId });
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
function toRun(w) {
|
|
213
|
+
if (w.status === 'COMPLETED')
|
|
214
|
+
return {
|
|
215
|
+
runId: w.workflowId,
|
|
216
|
+
state: 'completed',
|
|
217
|
+
...(w.output !== undefined ? { output: w.output } : {}),
|
|
218
|
+
};
|
|
219
|
+
const code = TERMINAL_CODE[w.status];
|
|
220
|
+
if (code)
|
|
221
|
+
return { runId: w.workflowId, state: 'failed', code };
|
|
222
|
+
return { runId: w.workflowId, state: 'running' };
|
|
223
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentropy/workflow-conductor",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Agentropy WorkflowEngine adapter for Conductor.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/bassemZohdy/agentropy.git",
|
|
9
|
+
"directory": "packages/workflow-conductor"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/bassemZohdy/agentropy/tree/main/packages/workflow-conductor#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/bassemZohdy/agentropy/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"agents",
|
|
17
|
+
"workflow",
|
|
18
|
+
"conductor",
|
|
19
|
+
"orchestration",
|
|
20
|
+
"agentropy"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=24"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"LICENSE"
|
|
35
|
+
],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@agentropy/core": "0.1.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@agentropy/testkit": "0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc -b",
|
|
44
|
+
"typecheck": "tsc --noEmit"
|
|
45
|
+
}
|
|
6
46
|
}
|