@nones/agentflow-strategy-agent 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 +10 -2
- package/package.json +38 -4
- package/src/agent.ts +26 -0
- package/src/index.ts +3 -0
- package/src/internal.ts +221 -0
- package/src/strategy.ts +44 -0
package/README.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Agent strategy
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`agent(spec)` validates and deep-clones a serializable `AgentSpec`. It accepts
|
|
4
|
+
`id`, `prompt`, `cwd`, `profile`, `model`, `settings`, `strategy`, and
|
|
5
|
+
`strategyInput`. Unknown fields, invalid IDs, non-empty string violations,
|
|
6
|
+
non-JSON settings, circular values, and non-finite numbers are rejected.
|
|
7
|
+
|
|
8
|
+
`agentStrategy.compile({ agent, artifacts? })` returns a one-wave `RunPlan`
|
|
9
|
+
containing the normalized agent. Artifacts use the contract shape and are
|
|
10
|
+
validated for unique IDs, `json`/`text`/`file` types, boolean `required`, and
|
|
11
|
+
object `schema` values.
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nones/agentflow-strategy-agent",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.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.1.0"
|
|
39
|
+
}
|
|
40
|
+
}
|
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,221 @@
|
|
|
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
|
+
"settings",
|
|
12
|
+
"strategy",
|
|
13
|
+
"strategyInput",
|
|
14
|
+
] as const;
|
|
15
|
+
|
|
16
|
+
const ARTIFACT_FIELDS = ["id", "type", "required", "schema"] as const;
|
|
17
|
+
|
|
18
|
+
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
19
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
const prototype = Object.getPrototypeOf(value);
|
|
23
|
+
return prototype === Object.prototype || prototype === null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function fail(path: string, message: string): never {
|
|
27
|
+
throw new TypeError(`${path}: ${message}`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function expectRecord(
|
|
31
|
+
value: unknown,
|
|
32
|
+
path: string,
|
|
33
|
+
): Record<string, unknown> {
|
|
34
|
+
if (!isRecord(value)) {
|
|
35
|
+
fail(path, "must be a plain object");
|
|
36
|
+
}
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function assertOnlyKeys(
|
|
41
|
+
record: Record<string, unknown>,
|
|
42
|
+
allowed: readonly string[],
|
|
43
|
+
path: string,
|
|
44
|
+
): void {
|
|
45
|
+
const allowedKeys = new Set(allowed);
|
|
46
|
+
for (const key of Object.keys(record)) {
|
|
47
|
+
if (!allowedKeys.has(key)) {
|
|
48
|
+
fail(`${path}.${key}`, "is not a recognized field");
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function expectString(value: unknown, path: string): string {
|
|
54
|
+
if (typeof value !== "string") {
|
|
55
|
+
fail(path, "must be a string");
|
|
56
|
+
}
|
|
57
|
+
if (value.trim().length === 0) {
|
|
58
|
+
fail(path, "must not be empty");
|
|
59
|
+
}
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function expectOptionalString(
|
|
64
|
+
value: unknown,
|
|
65
|
+
path: string,
|
|
66
|
+
): string | undefined {
|
|
67
|
+
if (value === undefined) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
return expectString(value, path);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function expectId(value: unknown, path: string): string {
|
|
74
|
+
const id = expectString(value, path);
|
|
75
|
+
if (!ID_PATTERN.test(id)) {
|
|
76
|
+
fail(
|
|
77
|
+
path,
|
|
78
|
+
"must start with an alphanumeric character and contain only letters, digits, '.', '_', ':' or '-'",
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return id;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function assertJsonValue(
|
|
85
|
+
value: unknown,
|
|
86
|
+
path: string,
|
|
87
|
+
seen: Set<object> = new Set<object>(),
|
|
88
|
+
): void {
|
|
89
|
+
if (value === null) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
switch (typeof value) {
|
|
93
|
+
case "string":
|
|
94
|
+
case "boolean":
|
|
95
|
+
return;
|
|
96
|
+
case "number":
|
|
97
|
+
if (!Number.isFinite(value)) {
|
|
98
|
+
fail(path, "must be a finite number");
|
|
99
|
+
}
|
|
100
|
+
return;
|
|
101
|
+
case "object": {
|
|
102
|
+
if (seen.has(value)) {
|
|
103
|
+
fail(path, "must not contain circular references");
|
|
104
|
+
}
|
|
105
|
+
if (Object.getOwnPropertySymbols(value).length > 0) {
|
|
106
|
+
fail(path, "must not contain symbol keys");
|
|
107
|
+
}
|
|
108
|
+
seen.add(value);
|
|
109
|
+
if (Array.isArray(value)) {
|
|
110
|
+
value.forEach((item, index) =>
|
|
111
|
+
assertJsonValue(item, `${path}[${index}]`, seen),
|
|
112
|
+
);
|
|
113
|
+
} else if (isRecord(value)) {
|
|
114
|
+
for (const [key, item] of Object.entries(value)) {
|
|
115
|
+
assertJsonValue(item, `${path}.${key}`, seen);
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
fail(path, "must be JSON-serializable");
|
|
119
|
+
}
|
|
120
|
+
seen.delete(value);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
default:
|
|
124
|
+
fail(path, `must be JSON-serializable (got ${typeof value})`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function cloneJson<T>(value: unknown, path: string): T {
|
|
129
|
+
assertJsonValue(value, path);
|
|
130
|
+
return JSON.parse(JSON.stringify(value)) as T;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function normalizeAgent(value: unknown, path: string): AgentSpec {
|
|
134
|
+
const record = expectRecord(value, path);
|
|
135
|
+
assertOnlyKeys(record, AGENT_FIELDS, path);
|
|
136
|
+
|
|
137
|
+
const id = expectId(record.id, `${path}.id`);
|
|
138
|
+
const prompt = expectString(record.prompt, `${path}.prompt`);
|
|
139
|
+
const cwd = expectString(record.cwd, `${path}.cwd`);
|
|
140
|
+
|
|
141
|
+
const spec: AgentSpec = { id, prompt, cwd };
|
|
142
|
+
|
|
143
|
+
const profile = expectOptionalString(record.profile, `${path}.profile`);
|
|
144
|
+
if (profile !== undefined) {
|
|
145
|
+
spec.profile = profile;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const model = expectOptionalString(record.model, `${path}.model`);
|
|
149
|
+
if (model !== undefined) {
|
|
150
|
+
spec.model = model;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (record.settings !== undefined) {
|
|
154
|
+
const settings = expectRecord(record.settings, `${path}.settings`);
|
|
155
|
+
spec.settings = cloneJson<Record<string, unknown>>(
|
|
156
|
+
settings,
|
|
157
|
+
`${path}.settings`,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const strategy = expectOptionalString(record.strategy, `${path}.strategy`);
|
|
162
|
+
if (strategy !== undefined) {
|
|
163
|
+
spec.strategy = strategy;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (record.strategyInput !== undefined) {
|
|
167
|
+
spec.strategyInput = cloneJson<unknown>(
|
|
168
|
+
record.strategyInput,
|
|
169
|
+
`${path}.strategyInput`,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return spec;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function normalizeArtifacts(
|
|
177
|
+
value: unknown,
|
|
178
|
+
path: string,
|
|
179
|
+
): ArtifactDeclaration[] {
|
|
180
|
+
if (!Array.isArray(value)) {
|
|
181
|
+
fail(path, "must be an array");
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const ids = new Set<string>();
|
|
185
|
+
|
|
186
|
+
return value.map((item, index) => {
|
|
187
|
+
const itemPath = `${path}[${index}]`;
|
|
188
|
+
const record = expectRecord(item, itemPath);
|
|
189
|
+
assertOnlyKeys(record, ARTIFACT_FIELDS, itemPath);
|
|
190
|
+
|
|
191
|
+
const id = expectId(record.id, `${itemPath}.id`);
|
|
192
|
+
if (ids.has(id)) {
|
|
193
|
+
fail(`${itemPath}.id`, `duplicate artifact id "${id}"`);
|
|
194
|
+
}
|
|
195
|
+
ids.add(id);
|
|
196
|
+
|
|
197
|
+
const type = record.type;
|
|
198
|
+
if (type !== "json" && type !== "text" && type !== "file") {
|
|
199
|
+
fail(`${itemPath}.type`, 'must be "json", "text" or "file"');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const artifact: ArtifactDeclaration = { id, type };
|
|
203
|
+
|
|
204
|
+
if (record.required !== undefined) {
|
|
205
|
+
if (typeof record.required !== "boolean") {
|
|
206
|
+
fail(`${itemPath}.required`, "must be a boolean");
|
|
207
|
+
}
|
|
208
|
+
artifact.required = record.required;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (record.schema !== undefined) {
|
|
212
|
+
const schema = expectRecord(record.schema, `${itemPath}.schema`);
|
|
213
|
+
artifact.schema = cloneJson<Record<string, unknown>>(
|
|
214
|
+
schema,
|
|
215
|
+
`${itemPath}.schema`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return artifact;
|
|
220
|
+
});
|
|
221
|
+
}
|
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
|
+
};
|