deepclause-sdk 0.4.0 → 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/README.md +134 -2
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +154 -28
- package/dist/agent.js.map +1 -1
- package/dist/cli/config.d.ts +150 -140
- package/dist/cli/config.d.ts.map +1 -1
- package/dist/cli/config.js +3 -0
- package/dist/cli/config.js.map +1 -1
- package/dist/cli/login.d.ts +2 -1
- package/dist/cli/login.d.ts.map +1 -1
- package/dist/cli/login.js +15 -2
- package/dist/cli/login.js.map +1 -1
- package/dist/cli/repl.d.ts +54 -1
- package/dist/cli/repl.d.ts.map +1 -1
- package/dist/cli/repl.js +437 -54
- package/dist/cli/repl.js.map +1 -1
- package/dist/cli/set-backend.d.ts +2 -2
- package/dist/cli/set-backend.d.ts.map +1 -1
- package/dist/cli/set-backend.js +54 -8
- package/dist/cli/set-backend.js.map +1 -1
- package/dist/cli/work.d.ts +16 -0
- package/dist/cli/work.d.ts.map +1 -0
- package/dist/cli/work.js +45 -0
- package/dist/cli/work.js.map +1 -0
- package/dist/index.d.ts +8 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/prolog-src/deepclause_mi.pl +2426 -253
- package/dist/prolog-src/deepclause_strings.pl +36 -3
- package/dist/repl/agent.d.ts +5 -0
- package/dist/repl/agent.d.ts.map +1 -1
- package/dist/repl/agent.js +19 -7
- package/dist/repl/agent.js.map +1 -1
- package/dist/repl/auth.d.ts +6 -3
- package/dist/repl/auth.d.ts.map +1 -1
- package/dist/repl/auth.js +55 -5
- package/dist/repl/auth.js.map +1 -1
- package/dist/repl/backend.d.ts +24 -0
- package/dist/repl/backend.d.ts.map +1 -1
- package/dist/repl/backend.js +292 -12
- package/dist/repl/backend.js.map +1 -1
- package/dist/repl/compaction.d.ts +2 -0
- package/dist/repl/compaction.d.ts.map +1 -1
- package/dist/repl/compaction.js +9 -0
- package/dist/repl/compaction.js.map +1 -1
- package/dist/repl/event-history.d.ts +4 -0
- package/dist/repl/event-history.d.ts.map +1 -0
- package/dist/repl/event-history.js +33 -0
- package/dist/repl/event-history.js.map +1 -0
- package/dist/repl/harness-tools.d.ts +25 -18
- package/dist/repl/harness-tools.d.ts.map +1 -1
- package/dist/repl/harness-tools.js +103 -34
- package/dist/repl/harness-tools.js.map +1 -1
- package/dist/repl/history.d.ts +52 -2
- package/dist/repl/history.d.ts.map +1 -1
- package/dist/repl/history.js +113 -4
- package/dist/repl/history.js.map +1 -1
- package/dist/repl/index.d.ts +27 -5
- package/dist/repl/index.d.ts.map +1 -1
- package/dist/repl/index.js +27 -0
- package/dist/repl/index.js.map +1 -1
- package/dist/repl/reference.d.ts +31 -0
- package/dist/repl/reference.d.ts.map +1 -0
- package/dist/repl/reference.js +143 -0
- package/dist/repl/reference.js.map +1 -0
- package/dist/repl/render.d.ts +3 -1
- package/dist/repl/render.d.ts.map +1 -1
- package/dist/repl/render.js +7 -0
- package/dist/repl/render.js.map +1 -1
- package/dist/repl/session.d.ts +128 -23
- package/dist/repl/session.d.ts.map +1 -1
- package/dist/repl/session.js +895 -196
- package/dist/repl/session.js.map +1 -1
- package/dist/repl/tui/autocomplete.d.ts.map +1 -1
- package/dist/repl/tui/autocomplete.js +5 -1
- package/dist/repl/tui/autocomplete.js.map +1 -1
- package/dist/repl/tui/renderer.d.ts +2 -1
- package/dist/repl/tui/renderer.d.ts.map +1 -1
- package/dist/repl/tui/renderer.js +20 -3
- package/dist/repl/tui/renderer.js.map +1 -1
- package/dist/repl/tui/status-bar.d.ts +5 -2
- package/dist/repl/tui/status-bar.d.ts.map +1 -1
- package/dist/repl/tui/status-bar.js +8 -3
- package/dist/repl/tui/status-bar.js.map +1 -1
- package/dist/repl/tui/tool-pane.d.ts +1 -1
- package/dist/repl/tui/tool-pane.d.ts.map +1 -1
- package/dist/repl/tui/tool-pane.js +13 -7
- package/dist/repl/tui/tool-pane.js.map +1 -1
- package/dist/repl/tui/work-view.d.ts +10 -0
- package/dist/repl/tui/work-view.d.ts.map +1 -0
- package/dist/repl/tui/work-view.js +28 -0
- package/dist/repl/tui/work-view.js.map +1 -0
- package/dist/runner.d.ts.map +1 -1
- package/dist/runner.js +50 -19
- package/dist/runner.js.map +1 -1
- package/dist/runtime-identity.json +10 -0
- package/dist/system/assets/docs/AGENT_PROMPT.md +265 -3008
- package/dist/system/assets/docs/DML_AUTHORING.md +81 -299
- package/dist/system/assets/docs/DML_BASICS.md +93 -0
- package/dist/system/assets/docs/DML_DEBUGGING.md +73 -0
- package/dist/system/assets/docs/DML_FORMS.md +94 -728
- package/dist/system/assets/docs/DML_MEDIA.md +63 -0
- package/dist/system/assets/docs/DML_PROCESSES.md +100 -0
- package/dist/system/assets/docs/DML_PYTHON.md +74 -0
- package/dist/system/assets/docs/DML_REFERENCE.md +224 -1971
- package/dist/system/assets/docs/DML_SHELL.md +107 -0
- package/dist/system/assets/docs/DML_TASKS.md +112 -0
- package/dist/system/assets/docs/HARNESS_EXAMPLES.md +255 -179
- package/dist/system/assets/docs/HARNESS_PATTERNS.md +65 -538
- package/dist/system/assets/docs/LOCAL_AGENT_PROMPT.md +186 -0
- package/dist/system/assets/docs/PROLOG_SNIPPETS.md +61 -154
- package/dist/system/assets/index.d.ts +1 -1
- package/dist/system/assets/index.d.ts.map +1 -1
- package/dist/system/assets/index.js +7 -0
- package/dist/system/assets/index.js.map +1 -1
- package/dist/system/environment.d.ts +1 -1
- package/dist/system/environment.d.ts.map +1 -1
- package/dist/system/environment.js +10 -6
- package/dist/system/environment.js.map +1 -1
- package/dist/system/runtime/shell-manager.d.ts +1 -1
- package/dist/system/runtime/shell-manager.d.ts.map +1 -1
- package/dist/types.d.ts +49 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/work/index.d.ts +82 -0
- package/dist/work/index.d.ts.map +1 -0
- package/dist/work/index.js +232 -0
- package/dist/work/index.js.map +1 -0
- package/package.json +12 -3
- package/src/prolog-src/deepclause_mi.pl +2426 -253
- package/src/prolog-src/deepclause_strings.pl +36 -3
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
**Effortlessly build efficient model-, task-, or domain-specific harnesses.**
|
|
4
4
|
|
|
5
|
+
**Web app:** [web.deepclause.ai](https://web.deepclause.ai)
|
|
6
|
+
|
|
5
7
|
Follow us on [X (@hilbertshelper)](https://x.com/hilbertshelper) · Blog: [deepclause.substack.com](https://deepclause.substack.com)
|
|
6
8
|
|
|
7
9
|
DeepClause is a **Prolog-based runtime for self-modifying agent harnesses**. A
|
|
@@ -76,9 +78,87 @@ dependencies and as conceptual foundations.
|
|
|
76
78
|
It is likely not worth it when a single prompt or a short script is enough, or
|
|
77
79
|
when there is no structure you would want to reuse, review, or reproduce.
|
|
78
80
|
|
|
81
|
+
## Develop and Work
|
|
82
|
+
|
|
83
|
+
DeepClause has two ways to use the same harness:
|
|
84
|
+
|
|
85
|
+
| | Develop | Work |
|
|
86
|
+
|---|---|---|
|
|
87
|
+
| Purpose | Author and edit the harness | Use the harness |
|
|
88
|
+
| Input | plain English, or `/goal <dml>` | chat with the declared tools, or run a declared workflow |
|
|
89
|
+
| Can change DML | yes, through transactional commits | no |
|
|
90
|
+
| Needs `shell/1` | no | yes |
|
|
91
|
+
|
|
92
|
+
**Develop is the default.** Describe a skill, tool, or change in plain English;
|
|
93
|
+
the authoring agent writes, tests, and commits it. **Work** is the stable,
|
|
94
|
+
non-authoring surface for a harness that is ready to use.
|
|
95
|
+
|
|
96
|
+
### Work needs a shell
|
|
97
|
+
|
|
98
|
+
A harness becomes usable in Work when it declares a `shell/1` fact:
|
|
99
|
+
|
|
100
|
+
```prolog
|
|
101
|
+
shell(_{
|
|
102
|
+
kind: agent,
|
|
103
|
+
system: "You are a concise writing assistant.",
|
|
104
|
+
host_tools: [],
|
|
105
|
+
tools: [
|
|
106
|
+
_{name: "text_length", goal: "length_of",
|
|
107
|
+
description: "Count characters in text.",
|
|
108
|
+
inputs: [_{name: "Text", type: string}],
|
|
109
|
+
outputs: [_{name: "N", type: integer}]}
|
|
110
|
+
],
|
|
111
|
+
workflows: [
|
|
112
|
+
_{name: "Uppercase", goal: "uppercase",
|
|
113
|
+
inputs: [_{name: "Text", type: string}],
|
|
114
|
+
outputs: [_{name: "Upper", type: string}]}
|
|
115
|
+
],
|
|
116
|
+
welcome: "Ask about text, or choose Uppercase."
|
|
117
|
+
}).
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
- `kind: agent` gives a chat assistant that can call only the declared tools and
|
|
121
|
+
workflows. `kind: workflow` gives a workflow-only surface with no assistant.
|
|
122
|
+
- `tools` are declared DML tools; `host_tools` lists host capabilities the
|
|
123
|
+
harness may use (for example `bash`). Host grants and sandbox boundaries still
|
|
124
|
+
apply.
|
|
125
|
+
- Workflow inputs and outputs are typed. Missing or invalid inputs fail before
|
|
126
|
+
execution; a failed, cancelled, or suspended workflow is not success.
|
|
127
|
+
- A harness without `shell/1` has no Work interface. Open Develop or an explicit
|
|
128
|
+
Run instead.
|
|
129
|
+
|
|
130
|
+
### Entering Work
|
|
131
|
+
|
|
132
|
+
In the TUI:
|
|
133
|
+
|
|
134
|
+
```text
|
|
135
|
+
/mode work [harness] open a Work conversation
|
|
136
|
+
/workflows list declared workflows
|
|
137
|
+
/workflow "Uppercase" {"Text":"hello"} run exactly that workflow
|
|
138
|
+
/mode develop [harness] return to authoring
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
From the CLI (assuming a harness named `example`):
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
deepclause repl --mode work --harness example
|
|
145
|
+
deepclause repl --mode work --harness example \
|
|
146
|
+
--workflow "Uppercase" --inputs '{"Text":"hello"}' --json
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
A manual `--workflow` run executes exactly that workflow and makes **zero
|
|
150
|
+
assistant calls**. An agent shell calls the model only when the conversation
|
|
151
|
+
needs it. Work and Develop are separate conversations with separate snapshots.
|
|
152
|
+
Work is pinned to the harness version it started with; to change the shell,
|
|
153
|
+
tools, or workflows, return to Develop (or open a fresh Work conversation after
|
|
154
|
+
the edit).
|
|
155
|
+
|
|
156
|
+
See [Work mode](docs/WORK_MODE.md) for the full contract and
|
|
157
|
+
[TUI/web reconciliation](docs/TUI_WEB_RECONCILIATION.md) for implementation status.
|
|
158
|
+
|
|
79
159
|
## The REPL: a living, self-modifying harness
|
|
80
160
|
|
|
81
|
-
|
|
161
|
+
In default Develop mode, the REPL keeps a persistent DML module ("the harness"). You talk to it in two ways:
|
|
82
162
|
|
|
83
163
|
| Input | Mode |
|
|
84
164
|
|---|---|
|
|
@@ -478,6 +558,58 @@ overrides it. Existing pi installations stay untouched.
|
|
|
478
558
|
|
|
479
559
|
### Local models (Ollama, llama.cpp, vLLM)
|
|
480
560
|
|
|
561
|
+
#### llama.cpp (router or single-model)
|
|
562
|
+
|
|
563
|
+
When `llama-server` runs in **router mode** (started without `--model` or `-m`),
|
|
564
|
+
the `pi` backend registers pi's built-in `llama.cpp` provider and discovers its
|
|
565
|
+
models live. Point it at the router with `LLAMA_BASE_URL`, or sign in once:
|
|
566
|
+
|
|
567
|
+
```bash
|
|
568
|
+
# one-off / environment-driven
|
|
569
|
+
export LLAMA_BASE_URL=http://127.0.0.1:8080
|
|
570
|
+
deepclause repl
|
|
571
|
+
|
|
572
|
+
# or persist the router URL + an optional API key and pick a default model
|
|
573
|
+
deepclause login --provider llama.cpp
|
|
574
|
+
|
|
575
|
+
# non-default host and port
|
|
576
|
+
deepclause login --provider llama.cpp --host 192.168.1.50 --port 9000
|
|
577
|
+
deepclause login --provider llama.cpp --base-url http://gpu-box:9000
|
|
578
|
+
deepclause set-backend pi --provider llama.cpp --host 192.168.1.50 --port 9000 \
|
|
579
|
+
--model llama.cpp/<id>
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
`--base-url` wins over `--host`/`--port`; a bare `--host` defaults the port to
|
|
583
|
+
`8080`, and `--port` alone defaults the host to `127.0.0.1`. Passing an explicit
|
|
584
|
+
endpoint re-runs the login, so the stored router URL is updated. The interactive
|
|
585
|
+
login prompt also accepts a full URL, a bare `host`, or `host:port` when these
|
|
586
|
+
flags are omitted, and `--provider llama.cpp` may be omitted when one of these
|
|
587
|
+
endpoint flags is present. For a router that requires an API key, add
|
|
588
|
+
`--api-key <key>`; the URL is resolved from the explicit flags, then
|
|
589
|
+
`LLAMA_BASE_URL`, then `http://127.0.0.1:8080`.
|
|
590
|
+
|
|
591
|
+
Inside the REPL, `/login` lists `llama.cpp`. Selecting it prompts for a base
|
|
592
|
+
URL, a `host:port`, or a host and then a port. You can also pass the endpoint
|
|
593
|
+
directly:
|
|
594
|
+
|
|
595
|
+
```text
|
|
596
|
+
/login llama.cpp --host 192.168.1.50 --port 9000
|
|
597
|
+
/login --base-url http://gpu-box:9000
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
After `/login`, `/model` includes the models discovered by that login (the
|
|
601
|
+
choice still applies on the next start). Use `/model llama.cpp/<id>` to set the
|
|
602
|
+
default, or pass `--model llama.cpp/<id>` for this run. Start the router with
|
|
603
|
+
`--jinja` when you need tool calling for `task/N`.
|
|
604
|
+
|
|
605
|
+
Single-model mode also works: when `/models` does not return router metadata,
|
|
606
|
+
DeepClause falls back to `/v1/models` and registers the reported model. If the
|
|
607
|
+
server exposes neither a model id nor a model path, it uses the placeholder id
|
|
608
|
+
`local-model`. Use `models.json` if you need to pin an exact id for a server
|
|
609
|
+
that rejects arbitrary model names.
|
|
610
|
+
|
|
611
|
+
#### Other OpenAI-compatible endpoints (Ollama, vLLM)
|
|
612
|
+
|
|
481
613
|
The default `pi` backend reads pi's `models.json`, which can declare **custom
|
|
482
614
|
OpenAI-compatible providers**, so a local server needs no SDK changes:
|
|
483
615
|
|
|
@@ -505,7 +637,7 @@ OpenAI-compatible providers**, so a local server needs no SDK changes:
|
|
|
505
637
|
|
|
506
638
|
```bash
|
|
507
639
|
deepclause set-backend pi --list # lists "loc (Local server) — configured [api-key]"
|
|
508
|
-
# and the model "loc/
|
|
640
|
+
# and the model "loc/llama3.1"
|
|
509
641
|
deepclause repl --model loc:llama3.1
|
|
510
642
|
```
|
|
511
643
|
|
package/dist/agent.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,KAAK,EACV,UAAU,EACV,eAAe,EAIf,QAAQ,EACR,aAAa,EACb,cAAc,EACd,aAAa,EACb,QAAQ,
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"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"}
|
package/dist/agent.js
CHANGED
|
@@ -306,16 +306,43 @@ function describeSchemaFields(typedVar) {
|
|
|
306
306
|
const fields = typedVar.fields;
|
|
307
307
|
if (!fields || fields.length === 0)
|
|
308
308
|
return undefined;
|
|
309
|
-
return fields
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
.
|
|
309
|
+
return fields.map(describeSchemaField).join(', ');
|
|
310
|
+
}
|
|
311
|
+
/** One field, including a nested schema's own fields. */
|
|
312
|
+
function describeSchemaField(field) {
|
|
313
|
+
const type = field.itemType ? `array<${field.itemType}>` : field.type;
|
|
314
|
+
const notes = [type];
|
|
315
|
+
if (field.required)
|
|
316
|
+
notes.push('required');
|
|
317
|
+
else if (field.default !== undefined)
|
|
318
|
+
notes.push(`default ${JSON.stringify(field.default)}`);
|
|
319
|
+
const head = `${field.name} (${notes.join(', ')})`;
|
|
320
|
+
return field.fields && field.fields.length > 0
|
|
321
|
+
? `${head} {${field.fields.map(describeSchemaField).join(', ')}}`
|
|
322
|
+
: head;
|
|
323
|
+
}
|
|
324
|
+
/** The type text shown to the nested agent, including a declared schema's fields. */
|
|
325
|
+
function describeOutputType(variable) {
|
|
326
|
+
const base = variable.type === 'array' && variable.itemType
|
|
327
|
+
? `array<${variable.itemType}>`
|
|
328
|
+
: variable.type;
|
|
329
|
+
const fields = describeSchemaFields(variable);
|
|
330
|
+
return fields ? `${base} {${fields}}` : base;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Match the `variable` argument of `set_result` to a requested output.
|
|
334
|
+
*
|
|
335
|
+
* Models occasionally omit it (providers differ in how they serialise an enum),
|
|
336
|
+
* and an empty name used to abort the whole task with `Unknown variable: `.
|
|
337
|
+
* With a single requested output the intent is unambiguous, so infer it; names
|
|
338
|
+
* are trimmed and compared case-insensitively, as `dict_field/3` does.
|
|
339
|
+
*/
|
|
340
|
+
function matchOutputVar(vars, raw) {
|
|
341
|
+
const wanted = typeof raw === 'string' ? raw.trim() : '';
|
|
342
|
+
if (!wanted)
|
|
343
|
+
return vars.length === 1 ? vars[0] : undefined;
|
|
344
|
+
return vars.find((variable) => variable.name === wanted)
|
|
345
|
+
?? vars.find((variable) => variable.name.toLowerCase() === wanted.toLowerCase());
|
|
319
346
|
}
|
|
320
347
|
/**
|
|
321
348
|
* A declared schema is checked here as well as in the runtime, so the agent is
|
|
@@ -329,20 +356,53 @@ function validateSchemaFields(typedVar, value) {
|
|
|
329
356
|
return null;
|
|
330
357
|
if (!isPlainObject(value))
|
|
331
358
|
return 'Expected an object value';
|
|
359
|
+
return validateFields(fields, value, '');
|
|
360
|
+
}
|
|
361
|
+
/** Validate a value against a field list, naming the nested path in the error. */
|
|
362
|
+
function validateFields(fields, value, prefix) {
|
|
363
|
+
if (!isPlainObject(value)) {
|
|
364
|
+
return prefix ? `Field "${prefix}" should be an object` : 'Expected an object value';
|
|
365
|
+
}
|
|
332
366
|
const record = value;
|
|
333
367
|
for (const field of fields) {
|
|
334
368
|
const present = Object.prototype.hasOwnProperty.call(record, field.name)
|
|
335
369
|
|| Object.keys(record).some((key) => normaliseFieldKey(key) === normaliseFieldKey(field.name));
|
|
370
|
+
const path = prefix ? `${prefix}.${field.name}` : field.name;
|
|
336
371
|
if (!present) {
|
|
337
372
|
if (field.required)
|
|
338
|
-
return `Missing required field "${
|
|
373
|
+
return `Missing required field "${path}"`;
|
|
339
374
|
continue;
|
|
340
375
|
}
|
|
341
376
|
const found = record[field.name] ?? Object.entries(record)
|
|
342
377
|
.find(([key]) => normaliseFieldKey(key) === normaliseFieldKey(field.name))?.[1];
|
|
343
|
-
|
|
344
|
-
|
|
378
|
+
const error = validateFieldValue(field, found, path);
|
|
379
|
+
if (error)
|
|
380
|
+
return error;
|
|
381
|
+
}
|
|
382
|
+
return null;
|
|
383
|
+
}
|
|
384
|
+
/** Validate one field value, recursing into a list element or nested schema. */
|
|
385
|
+
function validateFieldValue(field, value, path) {
|
|
386
|
+
if (field.itemType) {
|
|
387
|
+
if (!Array.isArray(value))
|
|
388
|
+
return `Field "${path}" should be array<${field.itemType}>`;
|
|
389
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
390
|
+
const item = value[index];
|
|
391
|
+
if (field.fields && field.fields.length > 0) {
|
|
392
|
+
const nested = validateFields(field.fields, item, `${path}[${index}]`);
|
|
393
|
+
if (nested)
|
|
394
|
+
return nested;
|
|
395
|
+
}
|
|
396
|
+
else if (!fieldTypeAccepts(field.itemType, item)) {
|
|
397
|
+
return `Field "${path}[${index}]" should be ${field.itemType}`;
|
|
398
|
+
}
|
|
345
399
|
}
|
|
400
|
+
return null;
|
|
401
|
+
}
|
|
402
|
+
if (!fieldTypeAccepts(field.type, value))
|
|
403
|
+
return `Field "${path}" should be ${field.type}`;
|
|
404
|
+
if (field.fields && field.fields.length > 0) {
|
|
405
|
+
return validateFields(field.fields, value, path);
|
|
346
406
|
}
|
|
347
407
|
return null;
|
|
348
408
|
}
|
|
@@ -526,15 +586,21 @@ async function runInjectedAgentLoop(options, normalizedOutputVars, combinedSyste
|
|
|
526
586
|
}];
|
|
527
587
|
if (normalizedOutputVars.length > 0) {
|
|
528
588
|
const resultTypes = normalizedOutputVars
|
|
529
|
-
.map((variable) => `${variable.name}: ${variable
|
|
589
|
+
.map((variable) => `${variable.name}: ${describeOutputType(variable)}`)
|
|
530
590
|
.join(', ');
|
|
531
591
|
backendTools.push({
|
|
532
592
|
name: 'set_result',
|
|
533
|
-
description: `Set one requested output variable before calling finish. Required results: ${resultTypes}.`,
|
|
593
|
+
description: `Set one requested output variable before calling finish. Required results: ${resultTypes}. Call it once per variable; the value must match the declared type.`,
|
|
534
594
|
parameters: {
|
|
535
595
|
type: 'object',
|
|
536
596
|
properties: {
|
|
537
|
-
variable: {
|
|
597
|
+
variable: {
|
|
598
|
+
type: 'string',
|
|
599
|
+
enum: requiredVarNames,
|
|
600
|
+
description: normalizedOutputVars.length === 1
|
|
601
|
+
? `The output variable name: ${requiredVarNames[0]}.`
|
|
602
|
+
: `One of: ${requiredVarNames.join(', ')}.`,
|
|
603
|
+
},
|
|
538
604
|
value: {
|
|
539
605
|
anyOf: [
|
|
540
606
|
{ type: 'string' },
|
|
@@ -543,6 +609,7 @@ async function runInjectedAgentLoop(options, normalizedOutputVars, combinedSyste
|
|
|
543
609
|
{ type: 'array' },
|
|
544
610
|
{ type: 'object' },
|
|
545
611
|
],
|
|
612
|
+
description: `The value. Expected: ${resultTypes}.`,
|
|
546
613
|
},
|
|
547
614
|
},
|
|
548
615
|
required: ['variable', 'value'],
|
|
@@ -580,6 +647,14 @@ async function runInjectedAgentLoop(options, normalizedOutputVars, combinedSyste
|
|
|
580
647
|
{ role: 'user', content: options.conversational ? options.taskDescription : `Subtask: ${options.taskDescription}` },
|
|
581
648
|
];
|
|
582
649
|
const maxIterations = Math.max(1, Number(process.env.DC_MAX_ITERATIONS) || 1000);
|
|
650
|
+
// A nested agent that cannot satisfy `set_result`/`finish` used to retry until
|
|
651
|
+
// the whole turn's model-call budget was gone (100 calls in one observed run).
|
|
652
|
+
// The validation message is the useful part; repeating it does not help, so
|
|
653
|
+
// after a few rejected attempts the task fails with it.
|
|
654
|
+
let rejectedAttempts = 0;
|
|
655
|
+
const maxRejectedAttempts = 4;
|
|
656
|
+
let lastRejection;
|
|
657
|
+
let lastSetResultError;
|
|
583
658
|
for (let iteration = 0; iteration < maxIterations && !state.finished; iteration++) {
|
|
584
659
|
if (options.signal?.aborted)
|
|
585
660
|
break;
|
|
@@ -654,12 +729,41 @@ async function runInjectedAgentLoop(options, normalizedOutputVars, combinedSyste
|
|
|
654
729
|
}
|
|
655
730
|
}
|
|
656
731
|
else if (call.name === 'set_result') {
|
|
657
|
-
|
|
658
|
-
|
|
732
|
+
// A provider can deliver a truncated tool call as `arguments: {}`. That
|
|
733
|
+
// is not a type error in the user's data: the value never arrived. Say
|
|
734
|
+
// so, and name the size problem, which is usually the cause.
|
|
735
|
+
const hasValue = Object.prototype.hasOwnProperty.call(call.arguments, 'value');
|
|
736
|
+
if (!hasValue) {
|
|
737
|
+
const typedVarNames = normalizedOutputVars.map((variable) => variable.name).join(', ') || 'none';
|
|
738
|
+
result = {
|
|
739
|
+
success: false,
|
|
740
|
+
error: `set_result received no "value" (the call arrived with no arguments). `
|
|
741
|
+
+ `Expected outputs: ${typedVarNames}. The value may be too large for one tool call — `
|
|
742
|
+
+ 'return a smaller object, or set the result in a script and return a path.',
|
|
743
|
+
};
|
|
744
|
+
messages.push({ role: 'tool', content: JSON.stringify(result), toolCallId: call.id, toolName: call.name });
|
|
745
|
+
rejectedAttempts += 1;
|
|
746
|
+
lastRejection = JSON.stringify(result);
|
|
747
|
+
lastSetResultError = lastRejection;
|
|
748
|
+
if (rejectedAttempts >= maxRejectedAttempts) {
|
|
749
|
+
throw new Error(`task protocol failed after ${maxRejectedAttempts} attempts: ${lastRejection}`);
|
|
750
|
+
}
|
|
751
|
+
continue;
|
|
752
|
+
}
|
|
753
|
+
const typedVar = matchOutputVar(normalizedOutputVars, call.arguments.variable);
|
|
754
|
+
const requested = typeof call.arguments.variable === 'string'
|
|
755
|
+
? call.arguments.variable.trim()
|
|
756
|
+
: '';
|
|
659
757
|
if (!typedVar) {
|
|
660
|
-
result = {
|
|
758
|
+
result = {
|
|
759
|
+
success: false,
|
|
760
|
+
error: requested
|
|
761
|
+
? `Unknown result variable "${requested}"; expected one of: ${requiredVarNames.join(', ')}.`
|
|
762
|
+
: `Missing "variable"; expected one of: ${requiredVarNames.join(', ')}.`,
|
|
763
|
+
};
|
|
661
764
|
}
|
|
662
765
|
else {
|
|
766
|
+
const variableName = typedVar.name;
|
|
663
767
|
const value = coerceTypedResultValue(typedVar, call.arguments.value);
|
|
664
768
|
const validationError = validateTypedResultValue(typedVar, value);
|
|
665
769
|
if (validationError) {
|
|
@@ -707,6 +811,33 @@ async function runInjectedAgentLoop(options, normalizedOutputVars, combinedSyste
|
|
|
707
811
|
toolCallId: call.id,
|
|
708
812
|
toolName: call.name,
|
|
709
813
|
});
|
|
814
|
+
// Count only rejections of the task protocol itself. A user tool that
|
|
815
|
+
// fails is the task's business and may be retried with a fixed argument.
|
|
816
|
+
const rejected = call.name === 'finish'
|
|
817
|
+
? result.finished === false
|
|
818
|
+
: call.name === 'set_result'
|
|
819
|
+
? result.success === false
|
|
820
|
+
: false;
|
|
821
|
+
if (rejected) {
|
|
822
|
+
rejectedAttempts += 1;
|
|
823
|
+
lastRejection = JSON.stringify(result);
|
|
824
|
+
if (call.name === 'set_result')
|
|
825
|
+
lastSetResultError = lastRejection;
|
|
826
|
+
}
|
|
827
|
+
else {
|
|
828
|
+
rejectedAttempts = 0;
|
|
829
|
+
}
|
|
830
|
+
if (rejectedAttempts >= maxRejectedAttempts) {
|
|
831
|
+
// `finish` is usually the last call, so quoting only it hides the
|
|
832
|
+
// `set_result` mistake that caused the loop. Report both and name the
|
|
833
|
+
// expected outputs.
|
|
834
|
+
const detail = [lastRejection, lastSetResultError && lastSetResultError !== lastRejection
|
|
835
|
+
? `last set_result: ${lastSetResultError}`
|
|
836
|
+
: undefined].filter(Boolean).join('; ');
|
|
837
|
+
throw new Error(`task protocol failed after ${maxRejectedAttempts} attempts: ${detail}. `
|
|
838
|
+
+ `Expected outputs: ${requiredVarNames.join(', ') || 'none'}. `
|
|
839
|
+
+ 'Return them with `set_result` using the declared type, or explain what is missing.');
|
|
840
|
+
}
|
|
710
841
|
}
|
|
711
842
|
if (toolCalls.length === 0 && !state.finished) {
|
|
712
843
|
if (options.conversational) {
|
|
@@ -789,11 +920,9 @@ export async function runAgentLoop(options) {
|
|
|
789
920
|
// Add set_result tool for output variables
|
|
790
921
|
if (normalizedOutputVars.length > 0) {
|
|
791
922
|
const varNames = normalizedOutputVars.map(v => v.name);
|
|
792
|
-
const typeSummary = normalizedOutputVars
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
return fields ? `${v.name}: array of objects, each ${fields}` : `${v.name}: ${renderedType}`;
|
|
796
|
-
}).join(', ');
|
|
923
|
+
const typeSummary = normalizedOutputVars
|
|
924
|
+
.map((variable) => `${variable.name}: ${describeOutputType(variable)}`)
|
|
925
|
+
.join(', ');
|
|
797
926
|
const variableSchema = varNames.length === 1
|
|
798
927
|
? z.literal(varNames[0])
|
|
799
928
|
: z.enum(varNames);
|
|
@@ -1484,10 +1613,7 @@ async function buildSystemPrompt(options) {
|
|
|
1484
1613
|
const resultSection = normalizedOutputVars.length > 0
|
|
1485
1614
|
? [
|
|
1486
1615
|
'You must set all named results before finishing successfully.',
|
|
1487
|
-
...normalizedOutputVars.map((variable) => {
|
|
1488
|
-
const typeStr = variable.type === 'array' && variable.itemType ? `array<${variable.itemType}>` : variable.type;
|
|
1489
|
-
return `- ${variable.name}: ${typeStr}`;
|
|
1490
|
-
}),
|
|
1616
|
+
...normalizedOutputVars.map((variable) => `- ${variable.name}: ${describeOutputType(variable)}`),
|
|
1491
1617
|
].join('\n')
|
|
1492
1618
|
: 'No named result variables are required. Call finish(true) as soon as the subtask is complete.';
|
|
1493
1619
|
const stallGuidance = [
|