@anureo/workflow 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +172 -2
- package/package.json +36 -4
- package/src/agent-outcome.d.ts +15 -0
- package/src/agent-outcome.js +28 -0
- package/src/agent.d.ts +49 -0
- package/src/agent.js +206 -0
- package/src/index.d.ts +6 -0
- package/src/index.js +5 -0
- package/src/output-schema.d.ts +27 -0
- package/src/output-schema.js +192 -0
- package/src/parallel.d.ts +18 -0
- package/src/parallel.js +25 -0
- package/src/persistence/atomic.d.ts +9 -0
- package/src/persistence/atomic.js +48 -0
- package/src/persistence/journal.d.ts +54 -0
- package/src/persistence/journal.js +106 -0
- package/src/persistence/json.d.ts +17 -0
- package/src/persistence/json.js +75 -0
- package/src/persistence/read-json.d.ts +11 -0
- package/src/persistence/read-json.js +46 -0
- package/src/persistence/records.d.ts +92 -0
- package/src/persistence/records.js +70 -0
- package/src/persistence/replay.d.ts +11 -0
- package/src/persistence/replay.js +79 -0
- package/src/persistence/store.d.ts +45 -0
- package/src/persistence/store.js +238 -0
- package/src/pipeline.d.ts +41 -0
- package/src/pipeline.js +24 -0
- package/src/run-context.d.ts +16 -0
- package/src/run-context.js +13 -0
- package/src/test-helpers.d.ts +11 -0
- package/src/test-helpers.js +21 -0
- package/src/workflow-state.d.ts +44 -0
- package/src/workflow-state.js +96 -0
- package/src/workflow-types.d.ts +39 -0
- package/src/workflow-types.js +0 -0
- package/src/workflow.d.ts +17 -0
- package/src/workflow.js +104 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Yoichi
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,173 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @anureo/workflow
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Directly imported primitives for ordinary TypeScript workflows:
|
|
4
|
+
`agent`, `parallel`, `pipeline`, and optional `workflow`. The host creates SDK Clients, imports its
|
|
5
|
+
workflow functions, awaits their results, and handles errors with normal TS.
|
|
6
|
+
|
|
7
|
+
See the [workflow authoring reference](../../docs/workflow/SKILL.md) for current
|
|
8
|
+
APIs, composition patterns, failure handling, and application-owned recovery.
|
|
9
|
+
|
|
10
|
+
The [Workflow primitives reference](../../docs/workflow/README.md) defines
|
|
11
|
+
parameters and result semantics for `agent`, `parallel`, and `pipeline`,
|
|
12
|
+
and records current support.
|
|
13
|
+
|
|
14
|
+
See the [composition comparison](../../draft/docs/workflow-composition-differences.md)
|
|
15
|
+
and [alignment plan](../../draft/docs/workflow-composition-plan.md) for Claude Code
|
|
16
|
+
semantics, the implemented changes, and retained host differences (Chinese).
|
|
17
|
+
|
|
18
|
+
This workspace package uses the revised native lifecycle API and is pending
|
|
19
|
+
publication with that revision. Requires Node.js 22+; TypeScript loading and
|
|
20
|
+
compilation are provided by your project (for example, Bun, tsx, or tsc).
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
Create an initialized Client using `createClient` and your chosen adapter as
|
|
25
|
+
described in the [SDK README](../../README.md). A workflow is just a function:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import type { Client } from '@anureo/sdk'
|
|
29
|
+
import { agent, parallel, pipeline } from '@anureo/workflow'
|
|
30
|
+
|
|
31
|
+
export async function reviewFiles(client: Client, files: readonly string[], cwd: string) {
|
|
32
|
+
const options = { client, cwd }
|
|
33
|
+
const verified = await pipeline(
|
|
34
|
+
files,
|
|
35
|
+
file => agent(`Review ${file}; include code locations and evidence.`, options),
|
|
36
|
+
(findings, file) => agent(`Independently verify ${file}:\n${findings}`, options),
|
|
37
|
+
)
|
|
38
|
+
if (verified.some(value => value === null)) throw new Error('Some file reviews failed')
|
|
39
|
+
const [summary, tests] = await parallel([
|
|
40
|
+
() => agent(`Summarize: ${JSON.stringify(verified)}`, options),
|
|
41
|
+
() => agent(`Suggest regression tests: ${JSON.stringify(verified)}`, options),
|
|
42
|
+
])
|
|
43
|
+
if (summary === null || tests === null) throw new Error('Review synthesis failed')
|
|
44
|
+
return { verified, summary, tests }
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
In this example, `pipeline` and `parallel` convert internal `agent` rejections to
|
|
49
|
+
`null`; `pipeline` also skips the affected item's remaining stages. The two
|
|
50
|
+
explicit checks then throw summary errors for `null` results, so those errors do
|
|
51
|
+
not preserve the original agent failure classification.
|
|
52
|
+
|
|
53
|
+
Import `reviewFiles` from another TS file and call it with a Client. Calls do not
|
|
54
|
+
require workflow registration, source-string execution, ambient context, or runtime setup.
|
|
55
|
+
Use [durable workflow steps](../../docs/workflow/persistence.md) when a function
|
|
56
|
+
needs a persisted run ID and recoverable results.
|
|
57
|
+
|
|
58
|
+
## agent
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { z } from 'zod'
|
|
62
|
+
import { agent } from '@anureo/workflow'
|
|
63
|
+
|
|
64
|
+
const options = { client, cwd: process.cwd() }
|
|
65
|
+
const text: string | null = await agent('Inspect the authentication module.', options)
|
|
66
|
+
const findings = await agent('List authentication issues.', {
|
|
67
|
+
...options,
|
|
68
|
+
schema: z.strictObject({ issues: z.array(z.string()) }),
|
|
69
|
+
})
|
|
70
|
+
// findings: { issues: string[] } | null
|
|
71
|
+
if (findings !== null) console.log(findings.issues)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Each call owns a fresh Session, submits once, waits for its native terminal
|
|
75
|
+
result, and releases its resources. `client` and `cwd` are required; `model` and
|
|
76
|
+
`instructions` are optional. The caller owns the borrowed Client.
|
|
77
|
+
|
|
78
|
+
`schema` accepts a Zod 4 object schema. The library converts it to draft-07 JSON
|
|
79
|
+
Schema, requires effective session `turn.outputSchema` support, validates the raw JSON
|
|
80
|
+
result, then validates it with the original Zod schema and returns `z.output<S>`.
|
|
81
|
+
This package depends on Zod 4.6.7. Applications that import Zod to author schemas
|
|
82
|
+
should also declare it as their own dependency. Schema-associated types infer business fields;
|
|
83
|
+
there is no unbound result-type generic or raw JSON Schema overload.
|
|
84
|
+
|
|
85
|
+
Use closed objects and required fields; nullable fields, array properties, enums,
|
|
86
|
+
nested unions and local recursion are supported. Optional properties, open
|
|
87
|
+
objects, records, tuples, value transformations and non-JSON types are rejected.
|
|
88
|
+
Defaults do not fill omitted model answers. Async refinements are supported
|
|
89
|
+
locally; their failures reject without replaying input. See the
|
|
90
|
+
[full schema and failure contract](../../docs/workflow/README.md#schema-mode).
|
|
91
|
+
|
|
92
|
+
Confirmed native failed terminals for authentication, usage/rate limits, and
|
|
93
|
+
service unavailability return `null`. Other failures and ordinary interruptions
|
|
94
|
+
reject. Trusted adapters may explicitly report user skip or classifier startup
|
|
95
|
+
denial; bundled adapters do not infer these reasons. Unknown execution outcomes
|
|
96
|
+
retain `outcomeUnknown: true` and always reject. No input is automatically retried.
|
|
97
|
+
|
|
98
|
+
Cleanup is attempted for successful, failed and discarded executions. A cleanup
|
|
99
|
+
failure rejects with `RESOURCE_RELEASE_FAILED`, or preserves a primary execution
|
|
100
|
+
exception with attached `details.cleanupErrors`. Releasing observation does not
|
|
101
|
+
prove the backend stopped. Use SDK Session/Turn APIs for raw items, usage, event
|
|
102
|
+
observation, multi-turn context, and exact execution controls.
|
|
103
|
+
|
|
104
|
+
Run `bun run example:workflow-schema [all|codex|claude]` for the
|
|
105
|
+
[Zod example](../../examples/workflow-structured.ts) using authenticated local
|
|
106
|
+
CLIs and real model requests.
|
|
107
|
+
|
|
108
|
+
## parallel
|
|
109
|
+
|
|
110
|
+
`parallel([() => taskA(), () => taskB()])` starts every callback and returns
|
|
111
|
+
input-ordered results. Callbacks may return ordinary values or Promises; inline
|
|
112
|
+
heterogeneous tuples preserve each result's type. An empty batch returns `[]`.
|
|
113
|
+
|
|
114
|
+
The array and every entry are validated before any callback starts. The call
|
|
115
|
+
waits for all callbacks to settle and uses `null` at each failed position,
|
|
116
|
+
including synchronous throws. Result types include `null` at every position.
|
|
117
|
+
Invalid arguments still reject the call. Use `value !== null` to filter failures
|
|
118
|
+
without dropping valid `0`, `false`, or empty-string results.
|
|
119
|
+
There is no implicit interruption, shared scheduler, or concurrency limit.
|
|
120
|
+
|
|
121
|
+
## pipeline
|
|
122
|
+
|
|
123
|
+
`pipeline(items, stage1, stage2, ...)` runs each item through its own sequence of
|
|
124
|
+
stages. A fast item can enter stage two while another item is still in stage one.
|
|
125
|
+
Results retain input order. Empty input returns `[]` before stage validation.
|
|
126
|
+
Zero stages are allowed and return the awaited inputs, with failures as `null`.
|
|
127
|
+
|
|
128
|
+
Every stage receives `(prevResult, originalItem, index)`. The first stage gets
|
|
129
|
+
the awaited input value; later stages get the awaited prior result. The original
|
|
130
|
+
input entry and zero-based index remain available throughout. Stages may change
|
|
131
|
+
types and pass ordinary TS values, including objects and Maps, without a JSON
|
|
132
|
+
serialization boundary.
|
|
133
|
+
|
|
134
|
+
Inline callbacks have contextual type inference for up to six stages. Longer
|
|
135
|
+
chains accept typed stage functions with no runtime stage-count limit. Incompatible
|
|
136
|
+
stage inputs are compile-time errors. A `null` input or stage result skips that
|
|
137
|
+
item's remaining stages. An item failure also skips its remaining stages and
|
|
138
|
+
becomes `null`. The returned Promise waits for all item chains to settle; its
|
|
139
|
+
result type always includes `null`. Stage input types exclude only `null`, so
|
|
140
|
+
`undefined` and other falsy values continue normally. Invalid composition
|
|
141
|
+
arguments still reject the call.
|
|
142
|
+
|
|
143
|
+
## Durable workflow
|
|
144
|
+
|
|
145
|
+
`workflow(options, body)` saves an initial manifest before passing explicit
|
|
146
|
+
`runId` and `input` to the body. Every `agent()` call made inside the body is
|
|
147
|
+
journaled automatically: identical calls on a `resumeFrom` run return their
|
|
148
|
+
saved results without opening new sessions, and any changed call invalidates
|
|
149
|
+
the calls after it. Existing three-primitive functions keep their independent
|
|
150
|
+
behavior, and `agent()` outside `workflow()` is unchanged.
|
|
151
|
+
|
|
152
|
+
The [persistence reference](../../docs/workflow/persistence.md) defines the
|
|
153
|
+
journal contract, cache keys, error handling and file guarantees.
|
|
154
|
+
Try `bun run example:workflow-persistence` and then repeat with its printed run
|
|
155
|
+
ID: the resumed run opens zero new sessions. This example uses a scripted
|
|
156
|
+
backend and makes no model requests.
|
|
157
|
+
|
|
158
|
+
## Examples
|
|
159
|
+
|
|
160
|
+
Run `bun run example:workflow` from the repository. The ordinary
|
|
161
|
+
[TypeScript example](../../examples/workflow.ts) directly imports all three
|
|
162
|
+
primitives and uses the Codex adapter. It requires a configured, authenticated
|
|
163
|
+
Codex CLI on PATH (`CODEX_BIN` can override its path). It prints every agent
|
|
164
|
+
input and output; each prompt requests arithmetic without tools.
|
|
165
|
+
|
|
166
|
+
For `[2, 3, 4]`, the pipeline asks the model to double then square each value, expecting
|
|
167
|
+
`[16, 36, 64]`. Two parallel agent calls then calculate the sum `116` and maximum `64`.
|
|
168
|
+
The eight agent calls each use an independent Session.
|
|
169
|
+
|
|
170
|
+
The [migration plan](../../draft/docs/workflow-primitives-plan.md) records the removed
|
|
171
|
+
framework and the [development record](../../draft/docs/development.md#workflow-agent-zod)
|
|
172
|
+
contains current verification evidence. The package has only its root entry;
|
|
173
|
+
there is no `/mcp` subentry.
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anureo/workflow",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Agent execution, composition, and durable workflow steps over the Anureo SDK",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/hi-youichi/anureo-sdk.git",
|
|
9
|
+
"directory": "packages/workflow"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./src/index.d.ts",
|
|
18
|
+
"import": "./src/index.js",
|
|
19
|
+
"default": "./src/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@anureo/sdk": "0.3.0",
|
|
24
|
+
"@anureo/protocol": "0.3.0",
|
|
25
|
+
"ajv": "8.20.0",
|
|
26
|
+
"zod": "4.6.7"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=22"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"src",
|
|
33
|
+
"bin",
|
|
34
|
+
"schema",
|
|
35
|
+
"protocol",
|
|
36
|
+
"examples"
|
|
37
|
+
]
|
|
38
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { HarnessError } from '@anureo/protocol';
|
|
2
|
+
import { HarnessSdkError } from '@anureo/sdk';
|
|
3
|
+
/**
|
|
4
|
+
* Let classifyAgentFailure identify terminal candidates for agent's null result after cleanup.
|
|
5
|
+
* Only known outcomes qualify. Interrupted terminals require an explicit skip, and failed
|
|
6
|
+
* terminals require a startup denial or a code from the terminal API allowlist.
|
|
7
|
+
*/
|
|
8
|
+
export declare function isDroppedOutcome(error: HarnessError, status: 'failed' | 'interrupted'): boolean;
|
|
9
|
+
/** Check the dedicated code and adapter disposition required for a known startup denial. */
|
|
10
|
+
export declare function isStartupBlocked(error: Pick<HarnessSdkError, 'code' | 'outcomeUnknown' | 'details'>): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Recognize a thrown SDK startup denial. openAgentSession returns null for a matching rejection
|
|
13
|
+
* from session creation. observeAgentTurn records a drop candidate and leaves settlement to cleanup.
|
|
14
|
+
*/
|
|
15
|
+
export declare function isStartupBlockException(cause: unknown): cause is HarnessSdkError;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { HarnessSdkError } from '@anureo/sdk';
|
|
2
|
+
/**
|
|
3
|
+
* Let classifyAgentFailure identify terminal candidates for agent's null result after cleanup.
|
|
4
|
+
* Only known outcomes qualify. Interrupted terminals require an explicit skip, and failed
|
|
5
|
+
* terminals require a startup denial or a code from the terminal API allowlist.
|
|
6
|
+
*/
|
|
7
|
+
export function isDroppedOutcome(error, status) {
|
|
8
|
+
if (error.outcomeUnknown)
|
|
9
|
+
return false;
|
|
10
|
+
if (status === 'interrupted')
|
|
11
|
+
return error.details?.workflowDisposition === 'skipped';
|
|
12
|
+
return isStartupBlocked(error) || terminalApiErrors.has(error.code);
|
|
13
|
+
}
|
|
14
|
+
/** Check the dedicated code and adapter disposition required for a known startup denial. */
|
|
15
|
+
export function isStartupBlocked(error) {
|
|
16
|
+
return !error.outcomeUnknown && error.code === 'AGENT_STARTUP_BLOCKED' && error.details?.workflowDisposition === 'startup_blocked';
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Recognize a thrown SDK startup denial. openAgentSession returns null for a matching rejection
|
|
20
|
+
* from session creation. observeAgentTurn records a drop candidate and leaves settlement to cleanup.
|
|
21
|
+
*/
|
|
22
|
+
export function isStartupBlockException(cause) {
|
|
23
|
+
return cause instanceof HarnessSdkError && isStartupBlocked(cause);
|
|
24
|
+
}
|
|
25
|
+
// Only apply to native failed terminal results, never open/submit/observation failures.
|
|
26
|
+
const terminalApiErrors = new Set([
|
|
27
|
+
'AUTH_REQUIRED', 'AUTH_FORBIDDEN', 'USAGE_LIMIT_EXCEEDED', 'RATE_LIMITED', 'NATIVE_SERVICE_UNAVAILABLE',
|
|
28
|
+
]);
|
package/src/agent.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type Client } from '@anureo/sdk';
|
|
2
|
+
import type { z } from 'zod';
|
|
3
|
+
/** Configure an independent agent call. Keep the client usable until settlement and dispose it separately. */
|
|
4
|
+
export interface AgentOptions<S extends z.ZodObject = z.ZodObject> {
|
|
5
|
+
/** Initialized, borrowed client selecting the backend; agent releases only its own session. */
|
|
6
|
+
client: Client;
|
|
7
|
+
/** Working directory forwarded to the fresh session. */
|
|
8
|
+
cwd: string;
|
|
9
|
+
/** Backend model selection; omission leaves selection to the session/backend. */
|
|
10
|
+
model?: string;
|
|
11
|
+
/** Session instructions, separate from the input prompt. */
|
|
12
|
+
instructions?: string;
|
|
13
|
+
/** Permission mode forwarded to the fresh session. Accepted values and defaults depend on the backend. */
|
|
14
|
+
permissionMode?: string;
|
|
15
|
+
/** Portable Zod object schema with closed objects and required JSON fields. Omit to return text. */
|
|
16
|
+
schema?: S;
|
|
17
|
+
/**
|
|
18
|
+
* With a schema, ask for JSON in the prompt when the session lacks structured output.
|
|
19
|
+
* Without this opt-in, an unsupported session rejects before submission.
|
|
20
|
+
*/
|
|
21
|
+
outputFallback?: 'json-text';
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Run a prompt in a fresh session and return the SDK's final text, which can be empty.
|
|
25
|
+
* Await the associated turn's terminal result. Once a session is acquired, close and dispose it
|
|
26
|
+
* before settling, including when submission or observation fails.
|
|
27
|
+
* Return null for confirmed terminal authentication, usage/rate-limit or native service unavailability,
|
|
28
|
+
* explicit skip or startup denial; unknown outcomes, other execution failures and cleanup failures reject.
|
|
29
|
+
* Cleanup failures preserve a primary execution error with details.cleanupErrors, or reject with
|
|
30
|
+
* RESOURCE_RELEASE_FAILED. Input is never automatically retried or interrupted, and releasing
|
|
31
|
+
* observation does not confirm native execution stopped.
|
|
32
|
+
* Inside workflow(), journal calls in invocation order and reuse eligible saved results on resume
|
|
33
|
+
* without opening a session. New executions wait for terminal journal publication. A previous
|
|
34
|
+
* unknown outcome rejects with WORKFLOW_RECONCILIATION_REQUIRED. The caller still owns the client.
|
|
35
|
+
*/
|
|
36
|
+
export declare function agent(prompt: string, options: AgentOptions<never> & {
|
|
37
|
+
schema?: undefined;
|
|
38
|
+
}): Promise<string | null>;
|
|
39
|
+
/**
|
|
40
|
+
* Return locally validated typed output with the same lifecycle and journal behavior as text mode.
|
|
41
|
+
* Unsupported schemas reject before opening. Validation awaits async refinements for fresh and
|
|
42
|
+
* replayed results. Missing, malformed or invalid JSON rejects without replaying input.
|
|
43
|
+
* Defaults never fill omitted answers.
|
|
44
|
+
*/
|
|
45
|
+
export declare function agent<S extends z.ZodObject>(prompt: string, options: AgentOptions<S> & {
|
|
46
|
+
schema: S;
|
|
47
|
+
}): Promise<z.output<S> | null>;
|
|
48
|
+
/** Select text or typed output from an optional schema, retaining agent's cleanup and failure contract. */
|
|
49
|
+
export declare function agent<S extends z.ZodObject>(prompt: string, options: AgentOptions<S>): Promise<string | z.output<S> | null>;
|