@nones/agentflow-strategy-goal 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 +140 -2
- package/package.json +42 -4
- package/src/agent.ts +46 -0
- package/src/index.ts +17 -0
- package/src/internal.ts +289 -0
- package/src/parse.ts +170 -0
- package/src/protocol.ts +76 -0
- package/src/strategy.ts +220 -0
- package/src/types.ts +27 -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,141 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@nones/agentflow-strategy-goal`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A same-session goal loop for AgentFlow. The agent receives a goal and a
|
|
4
|
+
structured completion protocol; each reply continues the same native session
|
|
5
|
+
until the goal is reached, blocked or a configured budget is exhausted.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
bun add @nones/agentflow-strategy-goal
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Bun 1.4.5 or newer is required.
|
|
14
|
+
|
|
15
|
+
## Input
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
{
|
|
19
|
+
agent: AgentSpec;
|
|
20
|
+
goal: string;
|
|
21
|
+
maxTurns?: number; // integer >= 1, no default
|
|
22
|
+
timeoutMs?: number; // integer >= 0, no default
|
|
23
|
+
verifier?: string | { name: string; evidence?: unknown };
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Compile and run
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { goalStrategy } from "@nones/agentflow-strategy-goal";
|
|
31
|
+
|
|
32
|
+
const plan = goalStrategy.compile({
|
|
33
|
+
agent: {
|
|
34
|
+
id: "fixer",
|
|
35
|
+
prompt: "Work in small verified steps.",
|
|
36
|
+
cwd: process.cwd(),
|
|
37
|
+
},
|
|
38
|
+
goal: "Make `bun test apps/cli` pass without changing unrelated files.",
|
|
39
|
+
maxTurns: 6,
|
|
40
|
+
verifier: "tests-pass",
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Through the CLI, pass the goal and its agent to the `goal` strategy:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
const run = await flow.start({
|
|
48
|
+
strategy: "goal",
|
|
49
|
+
input: {
|
|
50
|
+
agent: {
|
|
51
|
+
id: "fixer",
|
|
52
|
+
prompt: "Work in small verified steps.",
|
|
53
|
+
cwd: process.cwd(),
|
|
54
|
+
},
|
|
55
|
+
goal: "Make `bun test apps/cli` pass.",
|
|
56
|
+
maxTurns: 6,
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
When composing a server from the project directory, register both the strategy
|
|
62
|
+
and any verifier. This verifier checks the test exit code in the server:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import type { ServeOptions } from "@nones/agentflow-core";
|
|
66
|
+
import { goalStrategy } from "@nones/agentflow-strategy-goal";
|
|
67
|
+
|
|
68
|
+
const strategyOptions = {
|
|
69
|
+
strategies: [goalStrategy],
|
|
70
|
+
verifiers: {
|
|
71
|
+
"tests-pass": async () => {
|
|
72
|
+
const tests = Bun.spawn([process.execPath, "test", "apps/cli"], {
|
|
73
|
+
stdout: "ignore",
|
|
74
|
+
stderr: "ignore",
|
|
75
|
+
});
|
|
76
|
+
return (await tests.exited) === 0;
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
} satisfies Pick<ServeOptions, "strategies" | "verifiers">;
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
The compiled agent has a prompt containing the original prompt, `Goal: <goal>`
|
|
83
|
+
and the completion protocol, plus `strategy: "goal"` and the validated settings
|
|
84
|
+
in `strategyInput`. Continuation decisions never change the session ID, so every
|
|
85
|
+
turn stays in the same native session.
|
|
86
|
+
|
|
87
|
+
## Completion protocol
|
|
88
|
+
|
|
89
|
+
Every reply must end with exactly one block:
|
|
90
|
+
|
|
91
|
+
```text
|
|
92
|
+
<<<AGENTFLOW_GOAL>>>
|
|
93
|
+
{"status":"continue","reason":"short reason"}
|
|
94
|
+
<<<AGENTFLOW_GOAL_END>>>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`status` must be exactly `continue`, `reached` or `blocked`. `reason` is required
|
|
98
|
+
for `blocked`. A `reached` reply may include `evidence` for a configured
|
|
99
|
+
verifier. Prose before the block is allowed. Text after the end marker, multiple
|
|
100
|
+
blocks, unknown statuses, malformed JSON and missing blocked reasons are
|
|
101
|
+
rejected; a rejection pauses the agent with an `invalid goal response: ...`
|
|
102
|
+
diagnostic.
|
|
103
|
+
|
|
104
|
+
For compatibility, a reply containing no protocol markers may instead be exactly
|
|
105
|
+
one status word (`continue`, `reached` or `blocked`) or a JSON status object,
|
|
106
|
+
optionally in a single fenced block. Once a protocol marker appears, only the
|
|
107
|
+
canonical tagged form is accepted.
|
|
108
|
+
|
|
109
|
+
## Decisions
|
|
110
|
+
|
|
111
|
+
- `reached` becomes `complete` after the configured verifier passes. Without a
|
|
112
|
+
verifier, it completes directly.
|
|
113
|
+
- A configured verifier must be present as `TurnContext.verify`. If it is
|
|
114
|
+
missing, throws, returns a non-boolean or rejects the evidence, the goal does
|
|
115
|
+
not complete. A rejection becomes a `continue` decision with feedback until a
|
|
116
|
+
budget is exhausted.
|
|
117
|
+
- `continue` becomes a continuation prompt containing the protocol again unless
|
|
118
|
+
a budget is exhausted.
|
|
119
|
+
- `blocked` becomes `paused`.
|
|
120
|
+
- Failed turns become `failed`.
|
|
121
|
+
|
|
122
|
+
`TurnContext.turns` is the 1-based current goal turn. An explicit `maxTurns` is
|
|
123
|
+
checked after a `reached` reply is handled, so a successful final turn
|
|
124
|
+
completes; a `continue` on the final turn pauses. `timeoutMs` is compared against
|
|
125
|
+
`TurnContext.elapsedMs` in the same way. Without `maxTurns` or `timeoutMs`,
|
|
126
|
+
continuation may run until the goal is reached or blocked.
|
|
127
|
+
|
|
128
|
+
The package also exports `parseGoalReply`, `GoalResponseError`, `buildGoalPrompt`,
|
|
129
|
+
`goalProtocolText`, `appendGoalProtocol` and the `GOAL_PROTOCOL_*` constants for
|
|
130
|
+
custom integrations.
|
|
131
|
+
|
|
132
|
+
## Documentation
|
|
133
|
+
|
|
134
|
+
- [Goal example](https://github.com/nones-tools/agentflow/blob/main/examples/goal.ts)
|
|
135
|
+
- [Core contracts](https://github.com/nones-tools/agentflow/blob/main/docs/contracts.md)
|
|
136
|
+
- [Repository README](https://github.com/nones-tools/agentflow/blob/main/README.md)
|
|
137
|
+
- [Issues](https://github.com/nones-tools/agentflow/issues)
|
|
138
|
+
|
|
139
|
+
## License
|
|
140
|
+
|
|
141
|
+
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-goal",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "Same-session goal AgentFlow 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/goal"
|
|
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,46 @@
|
|
|
1
|
+
import type { AgentSpec } from "@nones/agentflow-core";
|
|
2
|
+
import { normalizeGoalInput } from "./internal";
|
|
3
|
+
import { buildGoalPrompt } from "./protocol";
|
|
4
|
+
import type { GoalInput, GoalSettings } from "./types";
|
|
5
|
+
|
|
6
|
+
function goalSettings(input: GoalInput): GoalSettings {
|
|
7
|
+
const settings: GoalSettings = { goal: input.goal };
|
|
8
|
+
if (input.maxTurns !== undefined) {
|
|
9
|
+
settings.maxTurns = input.maxTurns;
|
|
10
|
+
}
|
|
11
|
+
if (input.timeoutMs !== undefined) {
|
|
12
|
+
settings.timeoutMs = input.timeoutMs;
|
|
13
|
+
}
|
|
14
|
+
if (input.verifier !== undefined) {
|
|
15
|
+
settings.verifier = input.verifier;
|
|
16
|
+
}
|
|
17
|
+
return settings;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Build the same single goal agent that {@link goalStrategy.compile} produces.
|
|
22
|
+
*
|
|
23
|
+
* The builder shares the standalone strategy normalization and protocol, so an
|
|
24
|
+
* embedded goal agent is validated and serialized exactly like a goal run.
|
|
25
|
+
*/
|
|
26
|
+
export function goalAgent(input: GoalInput): AgentSpec {
|
|
27
|
+
return buildGoalAgent(input);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Unknown-input entry used by `goalStrategy.compile`. The public builder keeps
|
|
32
|
+
* its explicit `GoalInput` type while the runtime still validates the value.
|
|
33
|
+
*/
|
|
34
|
+
export function buildGoalAgent(input: unknown): AgentSpec {
|
|
35
|
+
const normalized = normalizeGoalInput(input);
|
|
36
|
+
return {
|
|
37
|
+
...normalized.agent,
|
|
38
|
+
prompt: buildGoalPrompt(
|
|
39
|
+
normalized.agent.prompt,
|
|
40
|
+
normalized.goal,
|
|
41
|
+
normalized.verifier,
|
|
42
|
+
),
|
|
43
|
+
strategy: "goal",
|
|
44
|
+
strategyInput: goalSettings(normalized),
|
|
45
|
+
};
|
|
46
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { goalAgent } from "./agent";
|
|
2
|
+
export { goalStrategy } from "./strategy";
|
|
3
|
+
export {
|
|
4
|
+
GOAL_PROTOCOL_END,
|
|
5
|
+
GOAL_PROTOCOL_MARKER,
|
|
6
|
+
appendGoalProtocol,
|
|
7
|
+
buildGoalPrompt,
|
|
8
|
+
goalProtocolText,
|
|
9
|
+
} from "./protocol";
|
|
10
|
+
export { GoalResponseError, parseGoalReply } from "./parse";
|
|
11
|
+
export type {
|
|
12
|
+
GoalInput,
|
|
13
|
+
GoalReply,
|
|
14
|
+
GoalSettings,
|
|
15
|
+
GoalStatus,
|
|
16
|
+
GoalVerifierConfig,
|
|
17
|
+
} from "./types";
|
package/src/internal.ts
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import type { AgentSpec } from "@nones/agentflow-core";
|
|
2
|
+
import type { GoalInput, GoalSettings, GoalVerifierConfig } from "./types";
|
|
3
|
+
|
|
4
|
+
const ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]*$/;
|
|
5
|
+
|
|
6
|
+
const AGENT_FIELDS = [
|
|
7
|
+
"id",
|
|
8
|
+
"prompt",
|
|
9
|
+
"cwd",
|
|
10
|
+
"profile",
|
|
11
|
+
"preset",
|
|
12
|
+
"model",
|
|
13
|
+
"settings",
|
|
14
|
+
"strategy",
|
|
15
|
+
"strategyInput",
|
|
16
|
+
] as const;
|
|
17
|
+
|
|
18
|
+
const GOAL_SETTINGS_FIELDS = [
|
|
19
|
+
"goal",
|
|
20
|
+
"maxTurns",
|
|
21
|
+
"timeoutMs",
|
|
22
|
+
"verifier",
|
|
23
|
+
] as const;
|
|
24
|
+
const GOAL_INPUT_FIELDS = [
|
|
25
|
+
"agent",
|
|
26
|
+
"goal",
|
|
27
|
+
"maxTurns",
|
|
28
|
+
"timeoutMs",
|
|
29
|
+
"verifier",
|
|
30
|
+
] as const;
|
|
31
|
+
const VERIFIER_FIELDS = ["name", "evidence"] as const;
|
|
32
|
+
|
|
33
|
+
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
34
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
const prototype = Object.getPrototypeOf(value);
|
|
38
|
+
return prototype === Object.prototype || prototype === null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function fail(path: string, message: string): never {
|
|
42
|
+
throw new TypeError(`${path}: ${message}`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function expectRecord(
|
|
46
|
+
value: unknown,
|
|
47
|
+
path: string,
|
|
48
|
+
): Record<string, unknown> {
|
|
49
|
+
if (!isRecord(value)) {
|
|
50
|
+
fail(path, "must be a plain object");
|
|
51
|
+
}
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function assertOnlyKeys(
|
|
56
|
+
record: Record<string, unknown>,
|
|
57
|
+
allowed: readonly string[],
|
|
58
|
+
path: string,
|
|
59
|
+
): void {
|
|
60
|
+
const allowedKeys = new Set(allowed);
|
|
61
|
+
for (const key of Object.keys(record)) {
|
|
62
|
+
if (!allowedKeys.has(key)) {
|
|
63
|
+
fail(`${path}.${key}`, "is not a recognized field");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function expectString(value: unknown, path: string): string {
|
|
69
|
+
if (typeof value !== "string") {
|
|
70
|
+
fail(path, "must be a string");
|
|
71
|
+
}
|
|
72
|
+
if (value.trim().length === 0) {
|
|
73
|
+
fail(path, "must not be empty");
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function expectOptionalString(
|
|
79
|
+
value: unknown,
|
|
80
|
+
path: string,
|
|
81
|
+
): string | undefined {
|
|
82
|
+
if (value === undefined) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
return expectString(value, path);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function expectId(value: unknown, path: string): string {
|
|
89
|
+
const id = expectString(value, path);
|
|
90
|
+
if (!ID_PATTERN.test(id)) {
|
|
91
|
+
fail(
|
|
92
|
+
path,
|
|
93
|
+
"must start with an alphanumeric character and contain only letters, digits, '.', '_', ':' or '-'",
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
return id;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function assertJsonValue(
|
|
100
|
+
value: unknown,
|
|
101
|
+
path: string,
|
|
102
|
+
seen: Set<object> = new Set<object>(),
|
|
103
|
+
): void {
|
|
104
|
+
if (value === null) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
switch (typeof value) {
|
|
108
|
+
case "string":
|
|
109
|
+
case "boolean":
|
|
110
|
+
return;
|
|
111
|
+
case "number":
|
|
112
|
+
if (!Number.isFinite(value)) {
|
|
113
|
+
fail(path, "must be a finite number");
|
|
114
|
+
}
|
|
115
|
+
return;
|
|
116
|
+
case "object": {
|
|
117
|
+
if (seen.has(value)) {
|
|
118
|
+
fail(path, "must not contain circular references");
|
|
119
|
+
}
|
|
120
|
+
if (Object.getOwnPropertySymbols(value).length > 0) {
|
|
121
|
+
fail(path, "must not contain symbol keys");
|
|
122
|
+
}
|
|
123
|
+
seen.add(value);
|
|
124
|
+
if (Array.isArray(value)) {
|
|
125
|
+
value.forEach((item, index) =>
|
|
126
|
+
assertJsonValue(item, `${path}[${index}]`, seen),
|
|
127
|
+
);
|
|
128
|
+
} else if (isRecord(value)) {
|
|
129
|
+
for (const [key, item] of Object.entries(value)) {
|
|
130
|
+
assertJsonValue(item, `${path}.${key}`, seen);
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
fail(path, "must be JSON-serializable");
|
|
134
|
+
}
|
|
135
|
+
seen.delete(value);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
default:
|
|
139
|
+
fail(path, `must be JSON-serializable (got ${typeof value})`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function cloneJson<T>(value: unknown, path: string): T {
|
|
144
|
+
assertJsonValue(value, path);
|
|
145
|
+
return JSON.parse(JSON.stringify(value)) as T;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function normalizeAgent(value: unknown, path: string): AgentSpec {
|
|
149
|
+
const record = expectRecord(value, path);
|
|
150
|
+
assertOnlyKeys(record, AGENT_FIELDS, path);
|
|
151
|
+
|
|
152
|
+
const id = expectId(record.id, `${path}.id`);
|
|
153
|
+
const prompt = expectString(record.prompt, `${path}.prompt`);
|
|
154
|
+
const cwd = expectString(record.cwd, `${path}.cwd`);
|
|
155
|
+
|
|
156
|
+
const spec: AgentSpec = { id, prompt, cwd };
|
|
157
|
+
|
|
158
|
+
const profile = expectOptionalString(record.profile, `${path}.profile`);
|
|
159
|
+
if (profile !== undefined) {
|
|
160
|
+
spec.profile = profile;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const preset = expectOptionalString(record.preset, `${path}.preset`);
|
|
164
|
+
if (preset !== undefined) {
|
|
165
|
+
spec.preset = preset;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const model = expectOptionalString(record.model, `${path}.model`);
|
|
169
|
+
if (model !== undefined) {
|
|
170
|
+
spec.model = model;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (record.settings !== undefined) {
|
|
174
|
+
const settings = expectRecord(record.settings, `${path}.settings`);
|
|
175
|
+
spec.settings = cloneJson<Record<string, unknown>>(
|
|
176
|
+
settings,
|
|
177
|
+
`${path}.settings`,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const strategy = expectOptionalString(record.strategy, `${path}.strategy`);
|
|
182
|
+
if (strategy !== undefined) {
|
|
183
|
+
spec.strategy = strategy;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (record.strategyInput !== undefined) {
|
|
187
|
+
spec.strategyInput = cloneJson<unknown>(
|
|
188
|
+
record.strategyInput,
|
|
189
|
+
`${path}.strategyInput`,
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return spec;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function expectOptionalInteger(
|
|
197
|
+
value: unknown,
|
|
198
|
+
path: string,
|
|
199
|
+
minimum: number,
|
|
200
|
+
): number | undefined {
|
|
201
|
+
if (value === undefined) {
|
|
202
|
+
return undefined;
|
|
203
|
+
}
|
|
204
|
+
if (
|
|
205
|
+
typeof value !== "number" ||
|
|
206
|
+
!Number.isInteger(value) ||
|
|
207
|
+
value < minimum
|
|
208
|
+
) {
|
|
209
|
+
fail(path, `must be an integer greater than or equal to ${minimum}`);
|
|
210
|
+
}
|
|
211
|
+
return value;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function normalizeVerifier(value: unknown, path: string): GoalVerifierConfig {
|
|
215
|
+
if (typeof value === "string") {
|
|
216
|
+
return expectString(value, path);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const record = expectRecord(value, path);
|
|
220
|
+
assertOnlyKeys(record, VERIFIER_FIELDS, path);
|
|
221
|
+
|
|
222
|
+
const name = expectString(record.name, `${path}.name`);
|
|
223
|
+
const verifier: { name: string; evidence?: unknown } = { name };
|
|
224
|
+
if (record.evidence !== undefined) {
|
|
225
|
+
verifier.evidence = cloneJson<unknown>(record.evidence, `${path}.evidence`);
|
|
226
|
+
}
|
|
227
|
+
return verifier;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function readGoalFields(
|
|
231
|
+
record: Record<string, unknown>,
|
|
232
|
+
path: string,
|
|
233
|
+
): GoalSettings {
|
|
234
|
+
const goal = expectString(record.goal, `${path}.goal`);
|
|
235
|
+
const settings: GoalSettings = { goal };
|
|
236
|
+
|
|
237
|
+
const maxTurns = expectOptionalInteger(
|
|
238
|
+
record.maxTurns,
|
|
239
|
+
`${path}.maxTurns`,
|
|
240
|
+
1,
|
|
241
|
+
);
|
|
242
|
+
if (maxTurns !== undefined) {
|
|
243
|
+
settings.maxTurns = maxTurns;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const timeoutMs = expectOptionalInteger(
|
|
247
|
+
record.timeoutMs,
|
|
248
|
+
`${path}.timeoutMs`,
|
|
249
|
+
0,
|
|
250
|
+
);
|
|
251
|
+
if (timeoutMs !== undefined) {
|
|
252
|
+
settings.timeoutMs = timeoutMs;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (record.verifier !== undefined) {
|
|
256
|
+
settings.verifier = normalizeVerifier(record.verifier, `${path}.verifier`);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return settings;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Validate goal settings. Used both for full strategy input and for the
|
|
264
|
+
* `agent.strategyInput` payload of an embedded goal agent.
|
|
265
|
+
*/
|
|
266
|
+
export function normalizeGoalSettings(
|
|
267
|
+
value: unknown,
|
|
268
|
+
path = "goal settings",
|
|
269
|
+
): GoalSettings {
|
|
270
|
+
const record = expectRecord(value, path);
|
|
271
|
+
assertOnlyKeys(record, GOAL_SETTINGS_FIELDS, path);
|
|
272
|
+
return readGoalFields(record, path);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** Validate the input of the top-level goal strategy. */
|
|
276
|
+
export function normalizeGoalInput(value: unknown): GoalInput {
|
|
277
|
+
const path = "goal strategy input";
|
|
278
|
+
const record = expectRecord(value, path);
|
|
279
|
+
assertOnlyKeys(record, GOAL_INPUT_FIELDS, path);
|
|
280
|
+
|
|
281
|
+
if (record.agent === undefined) {
|
|
282
|
+
fail(`${path}.agent`, "is required");
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return {
|
|
286
|
+
agent: normalizeAgent(record.agent, `${path}.agent`),
|
|
287
|
+
...readGoalFields(record, path),
|
|
288
|
+
};
|
|
289
|
+
}
|
package/src/parse.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { isRecord } from "./internal";
|
|
2
|
+
import { GOAL_PROTOCOL_END, GOAL_PROTOCOL_MARKER } from "./protocol";
|
|
3
|
+
import type { GoalReply, GoalStatus } from "./types";
|
|
4
|
+
|
|
5
|
+
export class GoalResponseError extends Error {
|
|
6
|
+
constructor(message: string) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "GoalResponseError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function protocolError(message: string): never {
|
|
13
|
+
throw new GoalResponseError(message);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isGoalStatus(value: unknown): value is GoalStatus {
|
|
17
|
+
return value === "continue" || value === "reached" || value === "blocked";
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function validateReplyObject(parsed: unknown): GoalReply {
|
|
21
|
+
if (!isRecord(parsed)) {
|
|
22
|
+
protocolError("goal status block must be a JSON object");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (!isGoalStatus(parsed.status)) {
|
|
26
|
+
protocolError(
|
|
27
|
+
'goal status must be exactly "continue", "reached" or "blocked"',
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let reason: string | undefined;
|
|
32
|
+
if (parsed.reason !== undefined) {
|
|
33
|
+
if (typeof parsed.reason !== "string") {
|
|
34
|
+
protocolError('goal status "reason" must be a string');
|
|
35
|
+
}
|
|
36
|
+
if (parsed.reason.trim().length === 0) {
|
|
37
|
+
protocolError('goal status "reason" must not be empty');
|
|
38
|
+
}
|
|
39
|
+
reason = parsed.reason;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (parsed.status === "blocked" && reason === undefined) {
|
|
43
|
+
protocolError('blocked goal status requires a non-empty "reason"');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const reply: GoalReply = { status: parsed.status };
|
|
47
|
+
if (reason !== undefined) {
|
|
48
|
+
reply.reason = reason;
|
|
49
|
+
}
|
|
50
|
+
if ("evidence" in parsed) {
|
|
51
|
+
reply.evidence = parsed.evidence;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return reply;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function parsePayload(payload: string): GoalReply {
|
|
58
|
+
let text = payload.trim();
|
|
59
|
+
if (text.length === 0) {
|
|
60
|
+
protocolError("goal status block is empty");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (text.startsWith("```")) {
|
|
64
|
+
const fenceLines = text.split(/\r?\n/);
|
|
65
|
+
const opening = fenceLines[0]!.trim();
|
|
66
|
+
const closing = fenceLines[fenceLines.length - 1]?.trim();
|
|
67
|
+
if (!/^```[A-Za-z0-9_-]*$/.test(opening)) {
|
|
68
|
+
protocolError("goal status block has an invalid code fence");
|
|
69
|
+
}
|
|
70
|
+
if (fenceLines.length < 2 || closing !== "```") {
|
|
71
|
+
protocolError("goal status block has an unterminated code fence");
|
|
72
|
+
}
|
|
73
|
+
text = fenceLines.slice(1, -1).join("\n").trim();
|
|
74
|
+
if (text.length === 0) {
|
|
75
|
+
protocolError("goal status block is empty");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let parsed: unknown = undefined;
|
|
80
|
+
try {
|
|
81
|
+
parsed = JSON.parse(text);
|
|
82
|
+
} catch {
|
|
83
|
+
protocolError("goal status block is not valid JSON");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return validateReplyObject(parsed);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function parseTaggedReply(text: string): GoalReply {
|
|
90
|
+
const markerIndex = text.indexOf(GOAL_PROTOCOL_MARKER);
|
|
91
|
+
if (
|
|
92
|
+
markerIndex === -1 ||
|
|
93
|
+
text.indexOf(GOAL_PROTOCOL_MARKER, markerIndex + 1) !== -1
|
|
94
|
+
) {
|
|
95
|
+
protocolError(
|
|
96
|
+
`goal reply must contain exactly one ${GOAL_PROTOCOL_MARKER}`,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const endIndex = text.indexOf(GOAL_PROTOCOL_END);
|
|
101
|
+
if (endIndex === -1 || text.indexOf(GOAL_PROTOCOL_END, endIndex + 1) !== -1) {
|
|
102
|
+
protocolError(`goal reply must contain exactly one ${GOAL_PROTOCOL_END}`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (markerIndex >= endIndex) {
|
|
106
|
+
protocolError("goal status block markers are out of order");
|
|
107
|
+
}
|
|
108
|
+
if (text.slice(endIndex + GOAL_PROTOCOL_END.length).trim().length > 0) {
|
|
109
|
+
protocolError("goal status block must be the last content in the reply");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return parsePayload(
|
|
113
|
+
text.slice(markerIndex + GOAL_PROTOCOL_MARKER.length, endIndex),
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Compatibility fallback for a reply that is exactly a status word, a JSON
|
|
119
|
+
* status object, or a fenced JSON status object. It is only used when the
|
|
120
|
+
* tagged protocol markers are absent, so it cannot hide a malformed tagged
|
|
121
|
+
* block.
|
|
122
|
+
*/
|
|
123
|
+
function parseLooseReply(text: string): GoalReply {
|
|
124
|
+
if (text === "continue" || text === "reached") {
|
|
125
|
+
return { status: text };
|
|
126
|
+
}
|
|
127
|
+
if (text === "blocked") {
|
|
128
|
+
return {
|
|
129
|
+
status: "blocked",
|
|
130
|
+
reason: "goal reported a blocker without details",
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (text.startsWith("```")) {
|
|
135
|
+
return parsePayload(text);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
let parsed: unknown = undefined;
|
|
139
|
+
try {
|
|
140
|
+
parsed = JSON.parse(text);
|
|
141
|
+
} catch {
|
|
142
|
+
protocolError("goal reply is not a recognized status response");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return validateReplyObject(parsed);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Parse an untrusted model reply. The canonical form is a tagged block at the
|
|
150
|
+
* end of the reply. A reply containing either marker must use the tagged form;
|
|
151
|
+
* otherwise an exact status word or JSON status object is also accepted.
|
|
152
|
+
*/
|
|
153
|
+
export function parseGoalReply(text: unknown): GoalReply {
|
|
154
|
+
if (typeof text !== "string") {
|
|
155
|
+
protocolError("goal reply must be a string");
|
|
156
|
+
}
|
|
157
|
+
const trimmed = text.trim();
|
|
158
|
+
if (trimmed.length === 0) {
|
|
159
|
+
protocolError("goal reply is empty");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (
|
|
163
|
+
trimmed.includes(GOAL_PROTOCOL_MARKER) ||
|
|
164
|
+
trimmed.includes(GOAL_PROTOCOL_END)
|
|
165
|
+
) {
|
|
166
|
+
return parseTaggedReply(text);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return parseLooseReply(trimmed);
|
|
170
|
+
}
|
package/src/protocol.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { GoalVerifierConfig } from "./types";
|
|
2
|
+
|
|
3
|
+
export const GOAL_PROTOCOL_MARKER = "<<<AGENTFLOW_GOAL>>>";
|
|
4
|
+
export const GOAL_PROTOCOL_END = "<<<AGENTFLOW_GOAL_END>>>";
|
|
5
|
+
|
|
6
|
+
export function verifierName(
|
|
7
|
+
verifier?: GoalVerifierConfig,
|
|
8
|
+
): string | undefined {
|
|
9
|
+
if (verifier === undefined) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
return typeof verifier === "string" ? verifier : verifier.name;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function goalProtocolText(verifier?: GoalVerifierConfig): string {
|
|
16
|
+
const name = verifierName(verifier);
|
|
17
|
+
const lines = [
|
|
18
|
+
"AgentFlow goal protocol:",
|
|
19
|
+
"Finish every reply with exactly one goal status block. The block must be the last content in the reply.",
|
|
20
|
+
'The "status" value must be exactly "continue", "reached" or "blocked". Use "reached" only when the goal is fully achieved, and "blocked" when operator input is required. "reason" is required for "blocked". Use valid JSON, emit exactly one status block, and put no text after the end marker.',
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
if (name !== undefined) {
|
|
24
|
+
lines.push(
|
|
25
|
+
`A verifier named "${name}" must accept your evidence before a "reached" status completes. Put the evidence in the "evidence" field of the status block.`,
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
lines.push(
|
|
30
|
+
GOAL_PROTOCOL_MARKER,
|
|
31
|
+
'{"status":"continue","reason":"short reason"}',
|
|
32
|
+
GOAL_PROTOCOL_END,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
return lines.join("\n");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function protocolMarkerIndex(prompt: string): number {
|
|
39
|
+
return prompt.search(/^<<<AGENTFLOW_GOAL>>>[ \t]*\r?$/mu);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Append the completion protocol unless the prompt already contains it.
|
|
44
|
+
*/
|
|
45
|
+
export function appendGoalProtocol(
|
|
46
|
+
prompt: string,
|
|
47
|
+
verifier?: GoalVerifierConfig,
|
|
48
|
+
): string {
|
|
49
|
+
if (protocolMarkerIndex(prompt) !== -1) {
|
|
50
|
+
return prompt;
|
|
51
|
+
}
|
|
52
|
+
return `${prompt.trimEnd()}\n\n${goalProtocolText(verifier)}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Build the first-turn prompt. The goal is placed before any existing protocol
|
|
57
|
+
* block so the completion protocol remains last.
|
|
58
|
+
*/
|
|
59
|
+
export function buildGoalPrompt(
|
|
60
|
+
agentPrompt: string,
|
|
61
|
+
goal: string,
|
|
62
|
+
verifier?: GoalVerifierConfig,
|
|
63
|
+
): string {
|
|
64
|
+
const goalSection = `Goal:\n${goal}`;
|
|
65
|
+
const markerIndex = protocolMarkerIndex(agentPrompt);
|
|
66
|
+
if (markerIndex !== -1) {
|
|
67
|
+
const head = agentPrompt.slice(0, markerIndex).trimEnd();
|
|
68
|
+
const tail = agentPrompt.slice(markerIndex);
|
|
69
|
+
return `${head}\n\n${goalSection}\n\n${tail}`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return appendGoalProtocol(
|
|
73
|
+
`${agentPrompt.trimEnd()}\n\n${goalSection}`,
|
|
74
|
+
verifier,
|
|
75
|
+
);
|
|
76
|
+
}
|
package/src/strategy.ts
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
RunPlan,
|
|
3
|
+
Strategy,
|
|
4
|
+
TurnContext,
|
|
5
|
+
TurnDecision,
|
|
6
|
+
} from "@nones/agentflow-core";
|
|
7
|
+
import { buildGoalAgent } from "./agent";
|
|
8
|
+
import { isRecord, normalizeGoalSettings } from "./internal";
|
|
9
|
+
import { parseGoalReply } from "./parse";
|
|
10
|
+
import { goalProtocolText, verifierName } from "./protocol";
|
|
11
|
+
import type { GoalReply, GoalSettings, GoalVerifierConfig } from "./types";
|
|
12
|
+
|
|
13
|
+
const WAVE_ID = "wave-1";
|
|
14
|
+
|
|
15
|
+
function diagnostic(error: unknown): string {
|
|
16
|
+
if (error instanceof Error) {
|
|
17
|
+
return error.message;
|
|
18
|
+
}
|
|
19
|
+
return String(error);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function paused(reason: string): TurnDecision {
|
|
23
|
+
return { status: "paused", reason };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function failed(reason: string): TurnDecision {
|
|
27
|
+
return { status: "failed", reason };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function continuationPrompt(settings: GoalSettings, note?: string): string {
|
|
31
|
+
const lines = ["Continue working toward the goal."];
|
|
32
|
+
if (note !== undefined) {
|
|
33
|
+
lines.push("", note);
|
|
34
|
+
}
|
|
35
|
+
lines.push("", goalProtocolText(settings.verifier));
|
|
36
|
+
return lines.join("\n");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function budgetExhausted(
|
|
40
|
+
settings: GoalSettings,
|
|
41
|
+
turns: number,
|
|
42
|
+
elapsedMs: number,
|
|
43
|
+
): string | undefined {
|
|
44
|
+
if (settings.maxTurns !== undefined && turns >= settings.maxTurns) {
|
|
45
|
+
return `turn budget of ${settings.maxTurns} is exhausted`;
|
|
46
|
+
}
|
|
47
|
+
if (settings.timeoutMs !== undefined && elapsedMs >= settings.timeoutMs) {
|
|
48
|
+
return `time budget of ${settings.timeoutMs}ms is exhausted`;
|
|
49
|
+
}
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function verifierEvidence(
|
|
54
|
+
verifier: GoalVerifierConfig | undefined,
|
|
55
|
+
reply: GoalReply,
|
|
56
|
+
): unknown {
|
|
57
|
+
if (
|
|
58
|
+
verifier !== undefined &&
|
|
59
|
+
typeof verifier !== "string" &&
|
|
60
|
+
"evidence" in verifier
|
|
61
|
+
) {
|
|
62
|
+
return verifier.evidence;
|
|
63
|
+
}
|
|
64
|
+
return reply.evidence;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function invalidContext(context: TurnContext): string | undefined {
|
|
68
|
+
if (!isRecord(context)) {
|
|
69
|
+
return "turn context must be an object";
|
|
70
|
+
}
|
|
71
|
+
if (!isRecord(context.agent)) {
|
|
72
|
+
return "turn context agent must be an object";
|
|
73
|
+
}
|
|
74
|
+
if (!isRecord(context.result)) {
|
|
75
|
+
return "turn context result must be an object";
|
|
76
|
+
}
|
|
77
|
+
if (
|
|
78
|
+
typeof context.turns !== "number" ||
|
|
79
|
+
!Number.isInteger(context.turns) ||
|
|
80
|
+
context.turns < 0
|
|
81
|
+
) {
|
|
82
|
+
return "turn context turns must be a non-negative integer";
|
|
83
|
+
}
|
|
84
|
+
if (
|
|
85
|
+
typeof context.elapsedMs !== "number" ||
|
|
86
|
+
!Number.isFinite(context.elapsedMs) ||
|
|
87
|
+
context.elapsedMs < 0
|
|
88
|
+
) {
|
|
89
|
+
return "turn context elapsedMs must be a non-negative number";
|
|
90
|
+
}
|
|
91
|
+
if (typeof context.result.status !== "string") {
|
|
92
|
+
return "turn result status must be a string";
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function reachDecision(
|
|
98
|
+
settings: GoalSettings,
|
|
99
|
+
reply: GoalReply,
|
|
100
|
+
context: TurnContext,
|
|
101
|
+
turns: number,
|
|
102
|
+
): Promise<TurnDecision> {
|
|
103
|
+
const name = verifierName(settings.verifier);
|
|
104
|
+
if (name === undefined) {
|
|
105
|
+
return {
|
|
106
|
+
status: "complete",
|
|
107
|
+
...(reply.reason !== undefined ? { reason: reply.reason } : {}),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (typeof context.verify !== "function") {
|
|
112
|
+
return paused(
|
|
113
|
+
`verifier "${name}" is configured but no verifier is registered in the runtime`,
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let verified: unknown;
|
|
118
|
+
try {
|
|
119
|
+
verified = await context.verify(
|
|
120
|
+
name,
|
|
121
|
+
verifierEvidence(settings.verifier, reply),
|
|
122
|
+
);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
return paused(`verifier "${name}" failed: ${diagnostic(error)}`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (typeof verified !== "boolean") {
|
|
128
|
+
return paused(`verifier "${name}" did not return a boolean`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (verified) {
|
|
132
|
+
return {
|
|
133
|
+
status: "complete",
|
|
134
|
+
...(reply.reason !== undefined ? { reason: reply.reason } : {}),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const budget = budgetExhausted(settings, turns, context.elapsedMs);
|
|
139
|
+
if (budget !== undefined) {
|
|
140
|
+
return paused(`verifier "${name}" rejected the evidence and ${budget}`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
status: "continue",
|
|
145
|
+
reason: `verification failed: ${name}`,
|
|
146
|
+
prompt: continuationPrompt(
|
|
147
|
+
settings,
|
|
148
|
+
`The verifier "${name}" rejected the evidence from the previous turn. Re-examine the evidence and continue the work.`,
|
|
149
|
+
),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Goal strategy. Its input is `{ agent, goal, maxTurns?, timeoutMs?, verifier? }`.
|
|
155
|
+
* Compilation appends the completion protocol to the first prompt and stores the
|
|
156
|
+
* goal settings on the agent so every decision can read them. The session is
|
|
157
|
+
* retained by core because decisions never carry a session override.
|
|
158
|
+
*/
|
|
159
|
+
export const goalStrategy: Strategy = {
|
|
160
|
+
id: "goal",
|
|
161
|
+
singleAdapter: true,
|
|
162
|
+
compile(input: unknown): RunPlan {
|
|
163
|
+
return {
|
|
164
|
+
waves: [{ id: WAVE_ID, agents: [buildGoalAgent(input)] }],
|
|
165
|
+
};
|
|
166
|
+
},
|
|
167
|
+
|
|
168
|
+
async decide(context: TurnContext): Promise<TurnDecision> {
|
|
169
|
+
const contextError = invalidContext(context);
|
|
170
|
+
if (contextError !== undefined) {
|
|
171
|
+
return paused(contextError);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const result = context.result;
|
|
175
|
+
if (result.status === "failed") {
|
|
176
|
+
return failed(result.error?.trim() || "goal turn failed");
|
|
177
|
+
}
|
|
178
|
+
if (result.status !== "succeeded") {
|
|
179
|
+
return paused(`goal turn ended with status "${result.status}"`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
let settings: GoalSettings;
|
|
183
|
+
try {
|
|
184
|
+
settings = normalizeGoalSettings(
|
|
185
|
+
context.agent.strategyInput,
|
|
186
|
+
"agent.strategyInput",
|
|
187
|
+
);
|
|
188
|
+
} catch (error) {
|
|
189
|
+
return paused(`invalid goal settings: ${diagnostic(error)}`);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
let reply: GoalReply;
|
|
193
|
+
try {
|
|
194
|
+
reply = parseGoalReply(result.text);
|
|
195
|
+
} catch (error) {
|
|
196
|
+
return paused(`invalid goal response: ${diagnostic(error)}`);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const turns = Math.max(1, context.turns);
|
|
200
|
+
|
|
201
|
+
if (reply.status === "reached") {
|
|
202
|
+
return reachDecision(settings, reply, context, turns);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (reply.status === "blocked") {
|
|
206
|
+
return paused(`blocked: ${reply.reason}`);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const budget = budgetExhausted(settings, turns, context.elapsedMs);
|
|
210
|
+
if (budget !== undefined) {
|
|
211
|
+
return paused(`goal is not complete and ${budget}`);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return {
|
|
215
|
+
status: "continue",
|
|
216
|
+
...(reply.reason !== undefined ? { reason: reply.reason } : {}),
|
|
217
|
+
prompt: continuationPrompt(settings),
|
|
218
|
+
};
|
|
219
|
+
},
|
|
220
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { AgentSpec } from "@nones/agentflow-core";
|
|
2
|
+
|
|
3
|
+
export type GoalVerifierConfig =
|
|
4
|
+
| string
|
|
5
|
+
| {
|
|
6
|
+
name: string;
|
|
7
|
+
evidence?: unknown;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export interface GoalSettings {
|
|
11
|
+
goal: string;
|
|
12
|
+
maxTurns?: number;
|
|
13
|
+
timeoutMs?: number;
|
|
14
|
+
verifier?: GoalVerifierConfig;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface GoalInput extends GoalSettings {
|
|
18
|
+
agent: AgentSpec;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type GoalStatus = "continue" | "reached" | "blocked";
|
|
22
|
+
|
|
23
|
+
export interface GoalReply {
|
|
24
|
+
status: GoalStatus;
|
|
25
|
+
reason?: string;
|
|
26
|
+
evidence?: unknown;
|
|
27
|
+
}
|