@nones/agentflow-strategy-agent 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +91 -2
- package/package.json +42 -4
- package/src/agent.ts +26 -0
- package/src/index.ts +3 -0
- package/src/internal.ts +226 -0
- package/src/strategy.ts +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 AgentFlow contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,92 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@nones/agentflow-strategy-agent`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The single-agent strategy for AgentFlow, plus the `agent()` builder for
|
|
4
|
+
validated, serializable agent specifications.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
bun add @nones/agentflow-strategy-agent
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Bun 1.4.5 or newer is required.
|
|
13
|
+
|
|
14
|
+
## Build an agent
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { agent } from "@nones/agentflow-strategy-agent";
|
|
18
|
+
|
|
19
|
+
const reviewer = agent({
|
|
20
|
+
id: "reviewer",
|
|
21
|
+
prompt: "Review the current diff and report concrete issues.",
|
|
22
|
+
cwd: process.cwd(),
|
|
23
|
+
model: "provider/model", // optional; profile.model is used otherwise
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Accepted fields are `id`, `prompt`, `cwd`, `profile`, `model`, `settings`,
|
|
28
|
+
`strategy` and `strategyInput`. Unknown fields, invalid IDs, empty required
|
|
29
|
+
strings, non-JSON settings, circular values and non-finite numbers are rejected.
|
|
30
|
+
The returned `AgentSpec` is a fresh object; nested settings and strategy input
|
|
31
|
+
are JSON-cloned.
|
|
32
|
+
|
|
33
|
+
## Compile a run plan
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { agent, agentStrategy } from "@nones/agentflow-strategy-agent";
|
|
37
|
+
|
|
38
|
+
const plan = agentStrategy.compile({
|
|
39
|
+
agent: agent({
|
|
40
|
+
id: "reviewer",
|
|
41
|
+
prompt: "Review the current diff and write a JSON report.",
|
|
42
|
+
cwd: process.cwd(),
|
|
43
|
+
}),
|
|
44
|
+
artifacts: [{ id: "report", type: "json", required: true }],
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Input is `{ agent, artifacts? }`. Output is a one-wave `RunPlan` with wave ID
|
|
49
|
+
`wave-1`. Artifact declarations must have unique IDs, a `json`, `text` or `file`
|
|
50
|
+
type, boolean `required` and an object `schema` when present.
|
|
51
|
+
|
|
52
|
+
## Use with AgentFlow
|
|
53
|
+
|
|
54
|
+
Start it through the CLI:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
const run = await flow.start({
|
|
58
|
+
strategy: "agent",
|
|
59
|
+
input: {
|
|
60
|
+
agent: {
|
|
61
|
+
id: "reviewer",
|
|
62
|
+
prompt: "Review the current diff.",
|
|
63
|
+
cwd: process.cwd(),
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Or register the strategy when composing a server programmatically:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import type { ServeOptions } from "@nones/agentflow-core";
|
|
73
|
+
import { agentStrategy } from "@nones/agentflow-strategy-agent";
|
|
74
|
+
|
|
75
|
+
const strategyOptions = {
|
|
76
|
+
strategies: [agentStrategy],
|
|
77
|
+
} satisfies Pick<ServeOptions, "strategies">;
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The strategy sets `singleAdapter: true`, so one run cannot mix provider
|
|
81
|
+
adapters.
|
|
82
|
+
|
|
83
|
+
## Documentation
|
|
84
|
+
|
|
85
|
+
- [Single-agent example](https://github.com/nones-tools/agentflow/blob/main/examples/agent.ts)
|
|
86
|
+
- [Core contracts](https://github.com/nones-tools/agentflow/blob/main/docs/contracts.md)
|
|
87
|
+
- [Repository README](https://github.com/nones-tools/agentflow/blob/main/README.md)
|
|
88
|
+
- [Issues](https://github.com/nones-tools/agentflow/issues)
|
|
89
|
+
|
|
90
|
+
## License
|
|
91
|
+
|
|
92
|
+
MIT. See [LICENSE](https://github.com/nones-tools/agentflow/blob/main/LICENSE).
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nones/agentflow-strategy-agent",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "Single-agent AgentFlow strategy and agent builder",
|
|
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/agent"
|
|
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.6.0"
|
|
39
|
+
},
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/nones-tools/agentflow/issues"
|
|
42
|
+
},
|
|
43
|
+
"license": "MIT"
|
|
44
|
+
}
|
package/src/agent.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { AgentSpec } from "@nones/agentflow-core";
|
|
2
|
+
import { normalizeAgent } from "./internal";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Input accepted by the reusable agent builder. The builder validates and
|
|
6
|
+
* normalizes the value, so callers can pass data from configuration without
|
|
7
|
+
* trusting its runtime shape.
|
|
8
|
+
*/
|
|
9
|
+
export interface AgentInput {
|
|
10
|
+
id: string;
|
|
11
|
+
prompt: string;
|
|
12
|
+
cwd: string;
|
|
13
|
+
profile?: string;
|
|
14
|
+
model?: string;
|
|
15
|
+
settings?: Record<string, unknown>;
|
|
16
|
+
strategy?: string;
|
|
17
|
+
strategyInput?: unknown;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Build a serializable AgentSpec. The returned value is a fresh object; nested
|
|
22
|
+
* settings and strategyInput values are JSON-cloned, never shared with input.
|
|
23
|
+
*/
|
|
24
|
+
export function agent(spec: AgentInput): AgentSpec {
|
|
25
|
+
return normalizeAgent(spec, "agent");
|
|
26
|
+
}
|
package/src/index.ts
ADDED
package/src/internal.ts
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import type { AgentSpec, ArtifactDeclaration } from "@nones/agentflow-core";
|
|
2
|
+
|
|
3
|
+
const ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]*$/;
|
|
4
|
+
|
|
5
|
+
const AGENT_FIELDS = [
|
|
6
|
+
"id",
|
|
7
|
+
"prompt",
|
|
8
|
+
"cwd",
|
|
9
|
+
"profile",
|
|
10
|
+
"model",
|
|
11
|
+
"preset",
|
|
12
|
+
"settings",
|
|
13
|
+
"strategy",
|
|
14
|
+
"strategyInput",
|
|
15
|
+
] as const;
|
|
16
|
+
|
|
17
|
+
const ARTIFACT_FIELDS = ["id", "type", "required", "schema"] as const;
|
|
18
|
+
|
|
19
|
+
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
20
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
const prototype = Object.getPrototypeOf(value);
|
|
24
|
+
return prototype === Object.prototype || prototype === null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function fail(path: string, message: string): never {
|
|
28
|
+
throw new TypeError(`${path}: ${message}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function expectRecord(
|
|
32
|
+
value: unknown,
|
|
33
|
+
path: string,
|
|
34
|
+
): Record<string, unknown> {
|
|
35
|
+
if (!isRecord(value)) {
|
|
36
|
+
fail(path, "must be a plain object");
|
|
37
|
+
}
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function assertOnlyKeys(
|
|
42
|
+
record: Record<string, unknown>,
|
|
43
|
+
allowed: readonly string[],
|
|
44
|
+
path: string,
|
|
45
|
+
): void {
|
|
46
|
+
const allowedKeys = new Set(allowed);
|
|
47
|
+
for (const key of Object.keys(record)) {
|
|
48
|
+
if (!allowedKeys.has(key)) {
|
|
49
|
+
fail(`${path}.${key}`, "is not a recognized field");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function expectString(value: unknown, path: string): string {
|
|
55
|
+
if (typeof value !== "string") {
|
|
56
|
+
fail(path, "must be a string");
|
|
57
|
+
}
|
|
58
|
+
if (value.trim().length === 0) {
|
|
59
|
+
fail(path, "must not be empty");
|
|
60
|
+
}
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function expectOptionalString(
|
|
65
|
+
value: unknown,
|
|
66
|
+
path: string,
|
|
67
|
+
): string | undefined {
|
|
68
|
+
if (value === undefined) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
return expectString(value, path);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function expectId(value: unknown, path: string): string {
|
|
75
|
+
const id = expectString(value, path);
|
|
76
|
+
if (!ID_PATTERN.test(id)) {
|
|
77
|
+
fail(
|
|
78
|
+
path,
|
|
79
|
+
"must start with an alphanumeric character and contain only letters, digits, '.', '_', ':' or '-'",
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
return id;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function assertJsonValue(
|
|
86
|
+
value: unknown,
|
|
87
|
+
path: string,
|
|
88
|
+
seen: Set<object> = new Set<object>(),
|
|
89
|
+
): void {
|
|
90
|
+
if (value === null) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
switch (typeof value) {
|
|
94
|
+
case "string":
|
|
95
|
+
case "boolean":
|
|
96
|
+
return;
|
|
97
|
+
case "number":
|
|
98
|
+
if (!Number.isFinite(value)) {
|
|
99
|
+
fail(path, "must be a finite number");
|
|
100
|
+
}
|
|
101
|
+
return;
|
|
102
|
+
case "object": {
|
|
103
|
+
if (seen.has(value)) {
|
|
104
|
+
fail(path, "must not contain circular references");
|
|
105
|
+
}
|
|
106
|
+
if (Object.getOwnPropertySymbols(value).length > 0) {
|
|
107
|
+
fail(path, "must not contain symbol keys");
|
|
108
|
+
}
|
|
109
|
+
seen.add(value);
|
|
110
|
+
if (Array.isArray(value)) {
|
|
111
|
+
value.forEach((item, index) =>
|
|
112
|
+
assertJsonValue(item, `${path}[${index}]`, seen),
|
|
113
|
+
);
|
|
114
|
+
} else if (isRecord(value)) {
|
|
115
|
+
for (const [key, item] of Object.entries(value)) {
|
|
116
|
+
assertJsonValue(item, `${path}.${key}`, seen);
|
|
117
|
+
}
|
|
118
|
+
} else {
|
|
119
|
+
fail(path, "must be JSON-serializable");
|
|
120
|
+
}
|
|
121
|
+
seen.delete(value);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
default:
|
|
125
|
+
fail(path, `must be JSON-serializable (got ${typeof value})`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function cloneJson<T>(value: unknown, path: string): T {
|
|
130
|
+
assertJsonValue(value, path);
|
|
131
|
+
return JSON.parse(JSON.stringify(value)) as T;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function normalizeAgent(value: unknown, path: string): AgentSpec {
|
|
135
|
+
const record = expectRecord(value, path);
|
|
136
|
+
assertOnlyKeys(record, AGENT_FIELDS, path);
|
|
137
|
+
|
|
138
|
+
const id = expectId(record.id, `${path}.id`);
|
|
139
|
+
const prompt = expectString(record.prompt, `${path}.prompt`);
|
|
140
|
+
const cwd = expectString(record.cwd, `${path}.cwd`);
|
|
141
|
+
|
|
142
|
+
const spec: AgentSpec = { id, prompt, cwd };
|
|
143
|
+
|
|
144
|
+
const profile = expectOptionalString(record.profile, `${path}.profile`);
|
|
145
|
+
if (profile !== undefined) {
|
|
146
|
+
spec.profile = profile;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const model = expectOptionalString(record.model, `${path}.model`);
|
|
150
|
+
if (model !== undefined) {
|
|
151
|
+
spec.model = model;
|
|
152
|
+
}
|
|
153
|
+
const preset = expectOptionalString(record.preset, `${path}.preset`);
|
|
154
|
+
if (preset !== undefined) {
|
|
155
|
+
spec.preset = preset;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (record.settings !== undefined) {
|
|
159
|
+
const settings = expectRecord(record.settings, `${path}.settings`);
|
|
160
|
+
spec.settings = cloneJson<Record<string, unknown>>(
|
|
161
|
+
settings,
|
|
162
|
+
`${path}.settings`,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const strategy = expectOptionalString(record.strategy, `${path}.strategy`);
|
|
167
|
+
if (strategy !== undefined) {
|
|
168
|
+
spec.strategy = strategy;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (record.strategyInput !== undefined) {
|
|
172
|
+
spec.strategyInput = cloneJson<unknown>(
|
|
173
|
+
record.strategyInput,
|
|
174
|
+
`${path}.strategyInput`,
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return spec;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function normalizeArtifacts(
|
|
182
|
+
value: unknown,
|
|
183
|
+
path: string,
|
|
184
|
+
): ArtifactDeclaration[] {
|
|
185
|
+
if (!Array.isArray(value)) {
|
|
186
|
+
fail(path, "must be an array");
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const ids = new Set<string>();
|
|
190
|
+
|
|
191
|
+
return value.map((item, index) => {
|
|
192
|
+
const itemPath = `${path}[${index}]`;
|
|
193
|
+
const record = expectRecord(item, itemPath);
|
|
194
|
+
assertOnlyKeys(record, ARTIFACT_FIELDS, itemPath);
|
|
195
|
+
|
|
196
|
+
const id = expectId(record.id, `${itemPath}.id`);
|
|
197
|
+
if (ids.has(id)) {
|
|
198
|
+
fail(`${itemPath}.id`, `duplicate artifact id "${id}"`);
|
|
199
|
+
}
|
|
200
|
+
ids.add(id);
|
|
201
|
+
|
|
202
|
+
const type = record.type;
|
|
203
|
+
if (type !== "json" && type !== "text" && type !== "file") {
|
|
204
|
+
fail(`${itemPath}.type`, 'must be "json", "text" or "file"');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const artifact: ArtifactDeclaration = { id, type };
|
|
208
|
+
|
|
209
|
+
if (record.required !== undefined) {
|
|
210
|
+
if (typeof record.required !== "boolean") {
|
|
211
|
+
fail(`${itemPath}.required`, "must be a boolean");
|
|
212
|
+
}
|
|
213
|
+
artifact.required = record.required;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (record.schema !== undefined) {
|
|
217
|
+
const schema = expectRecord(record.schema, `${itemPath}.schema`);
|
|
218
|
+
artifact.schema = cloneJson<Record<string, unknown>>(
|
|
219
|
+
schema,
|
|
220
|
+
`${itemPath}.schema`,
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return artifact;
|
|
225
|
+
});
|
|
226
|
+
}
|
package/src/strategy.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { AgentSpec, RunPlan, Strategy } from "@nones/agentflow-core";
|
|
2
|
+
import {
|
|
3
|
+
assertOnlyKeys,
|
|
4
|
+
expectRecord,
|
|
5
|
+
fail,
|
|
6
|
+
normalizeAgent,
|
|
7
|
+
normalizeArtifacts,
|
|
8
|
+
} from "./internal";
|
|
9
|
+
|
|
10
|
+
const STRATEGY_INPUT_FIELDS = ["agent", "artifacts"] as const;
|
|
11
|
+
|
|
12
|
+
const WAVE_ID = "wave-1";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Single-agent strategy. Its input is `{ agent, artifacts? }` and it compiles
|
|
16
|
+
* to a one-wave RunPlan.
|
|
17
|
+
*/
|
|
18
|
+
export const agentStrategy: Strategy = {
|
|
19
|
+
id: "agent",
|
|
20
|
+
singleAdapter: true,
|
|
21
|
+
compile(input: unknown): RunPlan {
|
|
22
|
+
const path = "agent strategy input";
|
|
23
|
+
const record = expectRecord(input, path);
|
|
24
|
+
assertOnlyKeys(record, STRATEGY_INPUT_FIELDS, path);
|
|
25
|
+
|
|
26
|
+
if (record.agent === undefined) {
|
|
27
|
+
fail(`${path}.agent`, "is required");
|
|
28
|
+
}
|
|
29
|
+
const spec: AgentSpec = normalizeAgent(record.agent, `${path}.agent`);
|
|
30
|
+
|
|
31
|
+
const plan: RunPlan = {
|
|
32
|
+
waves: [{ id: WAVE_ID, agents: [spec] }],
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
if (record.artifacts !== undefined) {
|
|
36
|
+
plan.artifacts = normalizeArtifacts(
|
|
37
|
+
record.artifacts,
|
|
38
|
+
`${path}.artifacts`,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return plan;
|
|
43
|
+
},
|
|
44
|
+
};
|