@nones/agentflow-strategy-workflow 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/README.md +20 -2
- package/package.json +38 -4
- package/src/index.ts +1 -0
- package/src/internal.ts +303 -0
- package/src/strategy.ts +15 -0
package/README.md
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Workflow strategy
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`workflowStrategy.compile(input)` accepts a `RunPlan` shape:
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
{
|
|
7
|
+
waves: WaveSpec[];
|
|
8
|
+
profile?: string;
|
|
9
|
+
artifacts?: ArtifactDeclaration[];
|
|
10
|
+
sealed?: boolean;
|
|
11
|
+
}
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
It validates and deeply clones the plan. Wave IDs must be unique, agent IDs must
|
|
15
|
+
be unique across the whole run (agents are addressed by ID), every wave must
|
|
16
|
+
contain at least one agent, and artifacts follow the same rules as the agent
|
|
17
|
+
strategy. An empty unsealed plan is a valid idle queue; a sealed empty plan is
|
|
18
|
+
preserved.
|
|
19
|
+
|
|
20
|
+
`workflowStrategy.singleAdapter` is `true`, so core can reject profile changes
|
|
21
|
+
that would resolve to a different adapter within one run.
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nones/agentflow-strategy-workflow",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Wave-based AgentFlow workflow strategy",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"module": "./src/index.ts",
|
|
8
|
+
"types": "./src/index.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./src/index.ts",
|
|
12
|
+
"import": "./src/index.ts",
|
|
13
|
+
"default": "./src/index.ts"
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"src",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"engines": {
|
|
22
|
+
"bun": ">=1.4.5"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public",
|
|
26
|
+
"registry": "https://registry.npmjs.org/"
|
|
27
|
+
},
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/nones-tools/agentflow.git",
|
|
31
|
+
"directory": "strategy/workflow"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "bun build ./src/index.ts --target=bun --outdir=.tmp/build --packages external",
|
|
35
|
+
"test": "bun test"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@nones/agentflow-core": "^0.1.0"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { workflowStrategy } from "./strategy";
|
package/src/internal.ts
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AgentSpec,
|
|
3
|
+
ArtifactDeclaration,
|
|
4
|
+
RunPlan,
|
|
5
|
+
WaveSpec,
|
|
6
|
+
} from "@nones/agentflow-core";
|
|
7
|
+
|
|
8
|
+
const ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]*$/;
|
|
9
|
+
|
|
10
|
+
const PLAN_FIELDS = ["waves", "profile", "artifacts", "sealed"] as const;
|
|
11
|
+
const WAVE_FIELDS = ["id", "agents"] as const;
|
|
12
|
+
const AGENT_FIELDS = [
|
|
13
|
+
"id",
|
|
14
|
+
"prompt",
|
|
15
|
+
"cwd",
|
|
16
|
+
"profile",
|
|
17
|
+
"model",
|
|
18
|
+
"settings",
|
|
19
|
+
"strategy",
|
|
20
|
+
"strategyInput",
|
|
21
|
+
] as const;
|
|
22
|
+
const ARTIFACT_FIELDS = ["id", "type", "required", "schema"] as const;
|
|
23
|
+
|
|
24
|
+
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
25
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
const prototype = Object.getPrototypeOf(value);
|
|
29
|
+
return prototype === Object.prototype || prototype === null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function fail(path: string, message: string): never {
|
|
33
|
+
throw new TypeError(`${path}: ${message}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function expectRecord(
|
|
37
|
+
value: unknown,
|
|
38
|
+
path: string,
|
|
39
|
+
): Record<string, unknown> {
|
|
40
|
+
if (!isRecord(value)) {
|
|
41
|
+
fail(path, "must be a plain object");
|
|
42
|
+
}
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function assertOnlyKeys(
|
|
47
|
+
record: Record<string, unknown>,
|
|
48
|
+
allowed: readonly string[],
|
|
49
|
+
path: string,
|
|
50
|
+
): void {
|
|
51
|
+
const allowedKeys = new Set(allowed);
|
|
52
|
+
for (const key of Object.keys(record)) {
|
|
53
|
+
if (!allowedKeys.has(key)) {
|
|
54
|
+
fail(`${path}.${key}`, "is not a recognized field");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function expectString(value: unknown, path: string): string {
|
|
60
|
+
if (typeof value !== "string") {
|
|
61
|
+
fail(path, "must be a string");
|
|
62
|
+
}
|
|
63
|
+
if (value.trim().length === 0) {
|
|
64
|
+
fail(path, "must not be empty");
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function expectOptionalString(
|
|
70
|
+
value: unknown,
|
|
71
|
+
path: string,
|
|
72
|
+
): string | undefined {
|
|
73
|
+
if (value === undefined) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
return expectString(value, path);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function expectId(value: unknown, path: string): string {
|
|
80
|
+
const id = expectString(value, path);
|
|
81
|
+
if (!ID_PATTERN.test(id)) {
|
|
82
|
+
fail(
|
|
83
|
+
path,
|
|
84
|
+
"must start with an alphanumeric character and contain only letters, digits, '.', '_', ':' or '-'",
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
return id;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function assertJsonValue(
|
|
91
|
+
value: unknown,
|
|
92
|
+
path: string,
|
|
93
|
+
seen: Set<object> = new Set<object>(),
|
|
94
|
+
): void {
|
|
95
|
+
if (value === null) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
switch (typeof value) {
|
|
99
|
+
case "string":
|
|
100
|
+
case "boolean":
|
|
101
|
+
return;
|
|
102
|
+
case "number":
|
|
103
|
+
if (!Number.isFinite(value)) {
|
|
104
|
+
fail(path, "must be a finite number");
|
|
105
|
+
}
|
|
106
|
+
return;
|
|
107
|
+
case "object": {
|
|
108
|
+
if (seen.has(value)) {
|
|
109
|
+
fail(path, "must not contain circular references");
|
|
110
|
+
}
|
|
111
|
+
if (Object.getOwnPropertySymbols(value).length > 0) {
|
|
112
|
+
fail(path, "must not contain symbol keys");
|
|
113
|
+
}
|
|
114
|
+
seen.add(value);
|
|
115
|
+
if (Array.isArray(value)) {
|
|
116
|
+
value.forEach((item, index) =>
|
|
117
|
+
assertJsonValue(item, `${path}[${index}]`, seen),
|
|
118
|
+
);
|
|
119
|
+
} else if (isRecord(value)) {
|
|
120
|
+
for (const [key, item] of Object.entries(value)) {
|
|
121
|
+
assertJsonValue(item, `${path}.${key}`, seen);
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
fail(path, "must be JSON-serializable");
|
|
125
|
+
}
|
|
126
|
+
seen.delete(value);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
default:
|
|
130
|
+
fail(path, `must be JSON-serializable (got ${typeof value})`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function cloneJson<T>(value: unknown, path: string): T {
|
|
135
|
+
assertJsonValue(value, path);
|
|
136
|
+
return JSON.parse(JSON.stringify(value)) as T;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function normalizeAgent(value: unknown, path: string): AgentSpec {
|
|
140
|
+
const record = expectRecord(value, path);
|
|
141
|
+
assertOnlyKeys(record, AGENT_FIELDS, path);
|
|
142
|
+
|
|
143
|
+
const id = expectId(record.id, `${path}.id`);
|
|
144
|
+
const prompt = expectString(record.prompt, `${path}.prompt`);
|
|
145
|
+
const cwd = expectString(record.cwd, `${path}.cwd`);
|
|
146
|
+
|
|
147
|
+
const spec: AgentSpec = { id, prompt, cwd };
|
|
148
|
+
|
|
149
|
+
const profile = expectOptionalString(record.profile, `${path}.profile`);
|
|
150
|
+
if (profile !== undefined) {
|
|
151
|
+
spec.profile = profile;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const model = expectOptionalString(record.model, `${path}.model`);
|
|
155
|
+
if (model !== undefined) {
|
|
156
|
+
spec.model = model;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (record.settings !== undefined) {
|
|
160
|
+
const settings = expectRecord(record.settings, `${path}.settings`);
|
|
161
|
+
spec.settings = cloneJson<Record<string, unknown>>(
|
|
162
|
+
settings,
|
|
163
|
+
`${path}.settings`,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const strategy = expectOptionalString(record.strategy, `${path}.strategy`);
|
|
168
|
+
if (strategy !== undefined) {
|
|
169
|
+
spec.strategy = strategy;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (record.strategyInput !== undefined) {
|
|
173
|
+
spec.strategyInput = cloneJson<unknown>(
|
|
174
|
+
record.strategyInput,
|
|
175
|
+
`${path}.strategyInput`,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return spec;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function normalizeArtifacts(
|
|
183
|
+
value: unknown,
|
|
184
|
+
path: string,
|
|
185
|
+
): ArtifactDeclaration[] {
|
|
186
|
+
if (!Array.isArray(value)) {
|
|
187
|
+
fail(path, "must be an array");
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const ids = new Set<string>();
|
|
191
|
+
|
|
192
|
+
return value.map((item, index) => {
|
|
193
|
+
const itemPath = `${path}[${index}]`;
|
|
194
|
+
const record = expectRecord(item, itemPath);
|
|
195
|
+
assertOnlyKeys(record, ARTIFACT_FIELDS, itemPath);
|
|
196
|
+
|
|
197
|
+
const id = expectId(record.id, `${itemPath}.id`);
|
|
198
|
+
if (ids.has(id)) {
|
|
199
|
+
fail(`${itemPath}.id`, `duplicate artifact id "${id}"`);
|
|
200
|
+
}
|
|
201
|
+
ids.add(id);
|
|
202
|
+
|
|
203
|
+
const type = record.type;
|
|
204
|
+
if (type !== "json" && type !== "text" && type !== "file") {
|
|
205
|
+
fail(`${itemPath}.type`, 'must be "json", "text" or "file"');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const artifact: ArtifactDeclaration = { id, type };
|
|
209
|
+
|
|
210
|
+
if (record.required !== undefined) {
|
|
211
|
+
if (typeof record.required !== "boolean") {
|
|
212
|
+
fail(`${itemPath}.required`, "must be a boolean");
|
|
213
|
+
}
|
|
214
|
+
artifact.required = record.required;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (record.schema !== undefined) {
|
|
218
|
+
const schema = expectRecord(record.schema, `${itemPath}.schema`);
|
|
219
|
+
artifact.schema = cloneJson<Record<string, unknown>>(
|
|
220
|
+
schema,
|
|
221
|
+
`${itemPath}.schema`,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return artifact;
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Validate and clone an untrusted RunPlan-shaped value. Wave IDs and agent IDs
|
|
231
|
+
* must be unique across the whole plan because core addresses agents by ID.
|
|
232
|
+
*/
|
|
233
|
+
export function normalizeRunPlan(
|
|
234
|
+
value: unknown,
|
|
235
|
+
path = "workflow input",
|
|
236
|
+
): RunPlan {
|
|
237
|
+
const record = expectRecord(value, path);
|
|
238
|
+
assertOnlyKeys(record, PLAN_FIELDS, path);
|
|
239
|
+
|
|
240
|
+
if (record.waves === undefined) {
|
|
241
|
+
fail(`${path}.waves`, "is required");
|
|
242
|
+
}
|
|
243
|
+
if (!Array.isArray(record.waves)) {
|
|
244
|
+
fail(`${path}.waves`, "must be an array");
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const waveIds = new Set<string>();
|
|
248
|
+
const agentIds = new Set<string>();
|
|
249
|
+
|
|
250
|
+
const waves: WaveSpec[] = record.waves.map((waveValue, waveIndex) => {
|
|
251
|
+
const wavePath = `${path}.waves[${waveIndex}]`;
|
|
252
|
+
const waveRecord = expectRecord(waveValue, wavePath);
|
|
253
|
+
assertOnlyKeys(waveRecord, WAVE_FIELDS, wavePath);
|
|
254
|
+
|
|
255
|
+
const id = expectId(waveRecord.id, `${wavePath}.id`);
|
|
256
|
+
if (waveIds.has(id)) {
|
|
257
|
+
fail(`${wavePath}.id`, `duplicate wave id "${id}"`);
|
|
258
|
+
}
|
|
259
|
+
waveIds.add(id);
|
|
260
|
+
|
|
261
|
+
if (waveRecord.agents === undefined) {
|
|
262
|
+
fail(`${wavePath}.agents`, "is required");
|
|
263
|
+
}
|
|
264
|
+
if (!Array.isArray(waveRecord.agents)) {
|
|
265
|
+
fail(`${wavePath}.agents`, "must be an array");
|
|
266
|
+
}
|
|
267
|
+
if (waveRecord.agents.length === 0) {
|
|
268
|
+
fail(`${wavePath}.agents`, "must contain at least one agent");
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const agents = waveRecord.agents.map((agentValue, agentIndex) => {
|
|
272
|
+
const agentPath = `${wavePath}.agents[${agentIndex}]`;
|
|
273
|
+
const spec = normalizeAgent(agentValue, agentPath);
|
|
274
|
+
if (agentIds.has(spec.id)) {
|
|
275
|
+
fail(`${agentPath}.id`, `duplicate agent id "${spec.id}" across waves`);
|
|
276
|
+
}
|
|
277
|
+
agentIds.add(spec.id);
|
|
278
|
+
return spec;
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
return { id, agents };
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
const plan: RunPlan = { waves };
|
|
285
|
+
|
|
286
|
+
const profile = expectOptionalString(record.profile, `${path}.profile`);
|
|
287
|
+
if (profile !== undefined) {
|
|
288
|
+
plan.profile = profile;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (record.artifacts !== undefined) {
|
|
292
|
+
plan.artifacts = normalizeArtifacts(record.artifacts, `${path}.artifacts`);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (record.sealed !== undefined) {
|
|
296
|
+
if (typeof record.sealed !== "boolean") {
|
|
297
|
+
fail(`${path}.sealed`, "must be a boolean");
|
|
298
|
+
}
|
|
299
|
+
plan.sealed = record.sealed;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return plan;
|
|
303
|
+
}
|
package/src/strategy.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { RunPlan, Strategy } from "@nones/agentflow-core";
|
|
2
|
+
import { normalizeRunPlan } from "./internal";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Workflow strategy. Its input is already a RunPlan; compilation validates and
|
|
6
|
+
* clones the value. `singleAdapter` is true because a workflow run must use one
|
|
7
|
+
* adapter ID across every agent and every append operation.
|
|
8
|
+
*/
|
|
9
|
+
export const workflowStrategy: Strategy = {
|
|
10
|
+
id: "workflow",
|
|
11
|
+
singleAdapter: true,
|
|
12
|
+
compile(input: unknown): RunPlan {
|
|
13
|
+
return normalizeRunPlan(input);
|
|
14
|
+
},
|
|
15
|
+
};
|