@parcha/agentrun-serve 0.0.0-stage → 0.1.0-beta.30
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/CHANGELOG.md +748 -0
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +2190 -2
- package/dist/adapters.d.ts +234 -0
- package/dist/adapters.js +585 -0
- package/dist/agents.d.ts +130 -0
- package/dist/agents.js +308 -0
- package/dist/app.d.ts +7 -0
- package/dist/app.js +384 -0
- package/dist/background.d.ts +28 -0
- package/dist/background.js +376 -0
- package/dist/bound-files.d.ts +4 -0
- package/dist/bound-files.js +164 -0
- package/dist/box/record.d.ts +6 -0
- package/dist/box/record.js +41 -0
- package/dist/box/sweep.d.ts +53 -0
- package/dist/box/sweep.js +193 -0
- package/dist/box-egress.d.ts +98 -0
- package/dist/box-egress.js +259 -0
- package/dist/box-image.d.ts +98 -0
- package/dist/box-image.js +285 -0
- package/dist/box-relay-vm.d.ts +80 -0
- package/dist/box-relay-vm.js +389 -0
- package/dist/box-relay.d.ts +45 -0
- package/dist/box-relay.js +227 -0
- package/dist/boxes.d.ts +72 -0
- package/dist/boxes.js +189 -0
- package/dist/browser-cost.d.ts +41 -0
- package/dist/browser-cost.js +158 -0
- package/dist/cap.d.ts +332 -0
- package/dist/cap.js +992 -0
- package/dist/catalog.d.ts +20 -0
- package/dist/catalog.js +53 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +167 -0
- package/dist/coding-tools.d.ts +35 -0
- package/dist/coding-tools.js +87 -0
- package/dist/console/THIRD-PARTY-NOTICES.txt +1721 -0
- package/dist/console/assets/index-34jfpIbQ.js +481 -0
- package/dist/console/assets/index-DUHbWcIP.css +1 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-400-normal-BEIGL1Tu.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-400-normal-CIEtRijs.woff +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-600-normal-BB5xRk30.woff +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-600-normal-EVf6-Yzo.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-ext-400-normal-BHyURHCj.woff +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-ext-400-normal-C_uLvvQ5.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-ext-600-normal-Dj6eZy5f.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-ext-600-normal-DpeVePZf.woff +0 -0
- package/dist/console/assets/jetbrains-mono-greek-400-normal-C190GLew.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-greek-400-normal-CNojz0t3.woff +0 -0
- package/dist/console/assets/jetbrains-mono-greek-600-normal-3KAm9r92.woff +0 -0
- package/dist/console/assets/jetbrains-mono-greek-600-normal-H7WoG9Et.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-latin-400-normal-B11XCQ5g.woff +0 -0
- package/dist/console/assets/jetbrains-mono-latin-400-normal-V6pRDFza.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-latin-600-normal-C8RAYTDA.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-latin-600-normal-DXv3JcnD.woff +0 -0
- package/dist/console/assets/jetbrains-mono-latin-ext-400-normal-Bc8Ftmh3.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-latin-ext-400-normal-DHMOLxAi.woff +0 -0
- package/dist/console/assets/jetbrains-mono-latin-ext-600-normal-BfB_LPfz.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-latin-ext-600-normal-DiQlUoE6.woff +0 -0
- package/dist/console/assets/jetbrains-mono-vietnamese-400-normal-BeU3T8wf.woff +0 -0
- package/dist/console/assets/jetbrains-mono-vietnamese-400-normal-ByoDsISC.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-vietnamese-600-normal-CSpzN4cI.woff +0 -0
- package/dist/console/assets/jetbrains-mono-vietnamese-600-normal-Dtizs43-.woff2 +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-400-normal-Cph9ettZ.woff2 +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-400-normal-DfGZKnlb.woff +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-600-normal-BGBUNdzT.woff2 +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-600-normal-COje25QW.woff +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-ext-400-normal-4l4lq8zr.woff2 +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-ext-400-normal-BGzXWJeR.woff +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-ext-600-normal-8_E36CQj.woff2 +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-ext-600-normal-C27an7Zc.woff +0 -0
- package/dist/console/index.html +15 -0
- package/dist/content-filter.d.ts +7 -0
- package/dist/content-filter.js +23 -0
- package/dist/daytona-api.d.ts +21 -0
- package/dist/daytona-api.js +101 -0
- package/dist/decision-rows.d.ts +19 -0
- package/dist/decision-rows.js +123 -0
- package/dist/decisions.d.ts +94 -0
- package/dist/decisions.js +184 -0
- package/dist/effort-guidance.d.ts +5 -0
- package/dist/effort-guidance.js +14 -0
- package/dist/environment.d.ts +5 -0
- package/dist/environment.js +11 -0
- package/dist/errors.d.ts +74 -0
- package/dist/errors.js +69 -0
- package/dist/exa-keyless.d.ts +35 -0
- package/dist/exa-keyless.js +202 -0
- package/dist/fetch-page.d.ts +114 -0
- package/dist/fetch-page.js +182 -0
- package/dist/files.d.ts +65 -0
- package/dist/files.js +158 -0
- package/dist/grade.d.ts +93 -0
- package/dist/grade.js +328 -0
- package/dist/graph-served.d.ts +23 -0
- package/dist/graph-served.js +75 -0
- package/dist/graph-view.d.ts +102 -0
- package/dist/graph-view.js +219 -0
- package/dist/harness-app.d.ts +40 -0
- package/dist/harness-app.js +708 -0
- package/dist/harness-opencode/adapter.d.ts +45 -0
- package/dist/harness-opencode/adapter.js +272 -0
- package/dist/harness-opencode/binary.d.ts +18 -0
- package/dist/harness-opencode/binary.js +87 -0
- package/dist/harness-opencode/config.d.ts +24 -0
- package/dist/harness-opencode/config.js +61 -0
- package/dist/harness-opencode/harness.d.ts +77 -0
- package/dist/harness-opencode/harness.js +620 -0
- package/dist/harness-opencode/host/mcp.d.ts +40 -0
- package/dist/harness-opencode/host/mcp.js +208 -0
- package/dist/harness-opencode/host/policy.d.ts +71 -0
- package/dist/harness-opencode/host/policy.js +419 -0
- package/dist/harness-opencode/host/server.d.ts +55 -0
- package/dist/harness-opencode/host/server.js +165 -0
- package/dist/harness-opencode/index.d.ts +9 -0
- package/dist/harness-opencode/index.js +6 -0
- package/dist/harness-opencode/verify.d.ts +32 -0
- package/dist/harness-opencode/verify.js +75 -0
- package/dist/harness-opencode/versions.d.ts +33 -0
- package/dist/harness-opencode/versions.js +64 -0
- package/dist/harness.d.ts +56 -0
- package/dist/harness.js +77 -0
- package/dist/home-path.d.ts +27 -0
- package/dist/home-path.js +78 -0
- package/dist/host-tools.d.ts +30 -0
- package/dist/host-tools.js +154 -0
- package/dist/host.d.ts +39 -0
- package/dist/host.js +85 -0
- package/dist/hosted-search.d.ts +27 -0
- package/dist/hosted-search.js +133 -0
- package/dist/import-sources.d.ts +104 -0
- package/dist/import-sources.js +629 -0
- package/dist/import-summary.d.ts +25 -0
- package/dist/import-summary.js +75 -0
- package/dist/import-worker.d.ts +26 -0
- package/dist/import-worker.js +41 -0
- package/dist/import.d.ts +145 -0
- package/dist/import.js +1222 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +6 -0
- package/dist/key-check.d.ts +37 -0
- package/dist/key-check.js +112 -0
- package/dist/key-refusal.d.ts +51 -0
- package/dist/key-refusal.js +191 -0
- package/dist/learn/check.d.ts +108 -0
- package/dist/learn/check.js +277 -0
- package/dist/learn/grade.d.ts +80 -0
- package/dist/learn/grade.js +445 -0
- package/dist/learn/index.d.ts +7 -0
- package/dist/learn/index.js +11 -0
- package/dist/learn/invisible.d.ts +5 -0
- package/dist/learn/invisible.js +42 -0
- package/dist/learn/notes.d.ts +59 -0
- package/dist/learn/notes.js +147 -0
- package/dist/learn/reflect.d.ts +49 -0
- package/dist/learn/reflect.js +213 -0
- package/dist/learn/secrets.d.ts +18 -0
- package/dist/learn/secrets.js +131 -0
- package/dist/learn/skills.d.ts +97 -0
- package/dist/learn/skills.js +271 -0
- package/dist/learned-workflow.d.ts +59 -0
- package/dist/learned-workflow.js +191 -0
- package/dist/learning-routes.d.ts +196 -0
- package/dist/learning-routes.js +609 -0
- package/dist/learning-run.d.ts +306 -0
- package/dist/learning-run.js +819 -0
- package/dist/learning-words.d.ts +76 -0
- package/dist/learning-words.js +174 -0
- package/dist/learning.d.ts +418 -0
- package/dist/learning.js +946 -0
- package/dist/llm.d.ts +40 -0
- package/dist/llm.js +37 -0
- package/dist/llms.txt +37 -0
- package/dist/mcp-servers.d.ts +118 -0
- package/dist/mcp-servers.js +450 -0
- package/dist/mcp.d.ts +51 -0
- package/dist/mcp.js +392 -0
- package/dist/model.d.ts +186 -0
- package/dist/model.js +485 -0
- package/dist/monid.d.ts +74 -0
- package/dist/monid.js +277 -0
- package/dist/openapi/agentrun-v2.openapi.json +4806 -0
- package/dist/openapi.d.ts +10 -0
- package/dist/openapi.js +1727 -0
- package/dist/owner.d.ts +55 -0
- package/dist/owner.js +222 -0
- package/dist/packs.d.ts +71 -0
- package/dist/packs.js +180 -0
- package/dist/page.d.ts +32 -0
- package/dist/page.js +133 -0
- package/dist/paid-calls.d.ts +50 -0
- package/dist/paid-calls.js +110 -0
- package/dist/pause.d.ts +70 -0
- package/dist/pause.js +97 -0
- package/dist/pdf-pages.d.ts +44 -0
- package/dist/pdf-pages.js +147 -0
- package/dist/place.d.ts +69 -0
- package/dist/place.js +137 -0
- package/dist/print.d.ts +20 -0
- package/dist/print.js +29 -0
- package/dist/propose.d.ts +40 -0
- package/dist/propose.js +112 -0
- package/dist/quiet-sqlite-warning.d.ts +1 -0
- package/dist/quiet-sqlite-warning.js +14 -0
- package/dist/request-start.d.ts +22 -0
- package/dist/request-start.js +350 -0
- package/dist/result.d.ts +3 -0
- package/dist/result.js +5 -0
- package/dist/rows.d.ts +46 -0
- package/dist/rows.js +121 -0
- package/dist/run-box.d.ts +186 -0
- package/dist/run-box.js +590 -0
- package/dist/run-manifest.d.ts +27 -0
- package/dist/run-manifest.js +60 -0
- package/dist/runnable.d.ts +83 -0
- package/dist/runnable.js +170 -0
- package/dist/runs.d.ts +1062 -0
- package/dist/runs.js +3020 -0
- package/dist/secret-cut.d.ts +27 -0
- package/dist/secret-cut.js +109 -0
- package/dist/secrets.d.ts +62 -0
- package/dist/secrets.js +158 -0
- package/dist/server-lock.d.ts +44 -0
- package/dist/server-lock.js +140 -0
- package/dist/server.d.ts +722 -0
- package/dist/server.js +2118 -0
- package/dist/sessions.d.ts +4 -0
- package/dist/sessions.js +105 -0
- package/dist/sign-ins.d.ts +99 -0
- package/dist/sign-ins.js +288 -0
- package/dist/skills.d.ts +43 -0
- package/dist/skills.js +96 -0
- package/dist/stats.d.ts +106 -0
- package/dist/stats.js +103 -0
- package/dist/tool-gate.d.ts +266 -0
- package/dist/tool-gate.js +554 -0
- package/dist/tool-keys.d.ts +187 -0
- package/dist/tool-keys.js +315 -0
- package/dist/tool-plane.d.ts +228 -0
- package/dist/tool-plane.js +400 -0
- package/dist/tools.d.ts +202 -0
- package/dist/tools.js +372 -0
- package/dist/trigger-secrets.d.ts +32 -0
- package/dist/trigger-secrets.js +143 -0
- package/dist/triggers.d.ts +439 -0
- package/dist/triggers.js +863 -0
- package/dist/turn-end.d.ts +24 -0
- package/dist/turn-end.js +15 -0
- package/dist/turns.d.ts +34 -0
- package/dist/turns.js +202 -0
- package/dist/uploads.d.ts +104 -0
- package/dist/uploads.js +474 -0
- package/dist/usage-lines.d.ts +26 -0
- package/dist/usage-lines.js +28 -0
- package/dist/usage.d.ts +29 -0
- package/dist/usage.js +58 -0
- package/dist/validate.d.ts +57 -0
- package/dist/validate.js +184 -0
- package/dist/web-fetch.d.ts +64 -0
- package/dist/web-fetch.js +262 -0
- package/dist/web-search.d.ts +54 -0
- package/dist/web-search.js +136 -0
- package/dist/webhooks.d.ts +189 -0
- package/dist/webhooks.js +503 -0
- package/dist/workspace-paths.d.ts +33 -0
- package/dist/workspace-paths.js +104 -0
- package/dist/workspace.d.ts +88 -0
- package/dist/workspace.js +369 -0
- package/launcher/agentrun-serve +109 -0
- package/package.json +100 -4
- package/skills/agentrun-serve/SKILL.md +128 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { assertWorkflowCapabilities, desugarWorkflow, inspectWorkflow, validateWorkflow } from "@parcha/agentrun-dsl";
|
|
2
|
+
/** Where a skill and a workflow draft live in the folder. */
|
|
3
|
+
export declare const SKILLS_DIR = "skills";
|
|
4
|
+
export declare const WORKFLOWS_DIR = "workflows";
|
|
5
|
+
/** The shared skills convention's name: lowercase letters, digits and single dashes, at most 64. */
|
|
6
|
+
export declare const SKILL_NAME: RegExp;
|
|
7
|
+
/** A workflow's name: lowercase letters, digits, dashes and underscores, at most 64, starting with a letter or digit. */
|
|
8
|
+
export declare const WORKFLOW_NAME: RegExp;
|
|
9
|
+
/** A skill's `SKILL.md`, a workflow's `workflow.json` or its `README.md`, by the folder it names. */
|
|
10
|
+
export type LearnedPlace = {
|
|
11
|
+
readonly kind: "skill" | "workflow" | "readme";
|
|
12
|
+
readonly name: string;
|
|
13
|
+
};
|
|
14
|
+
/** Where `path` sits among the skills and workflows, its folder's name as written (unchecked); undefined when it is
|
|
15
|
+
* not a skill's or a workflow's file. */
|
|
16
|
+
export declare function learnedPlace(path: string): LearnedPlace | undefined;
|
|
17
|
+
/** Whether `name` is a good name for a folder of `kind`. */
|
|
18
|
+
export declare const learnedName: (kind: LearnedPlace["kind"], name: string) => boolean;
|
|
19
|
+
/** The keys a SKILL.md's or a draft README's front matter sets, in order (each line at its left edge that names one);
|
|
20
|
+
* undefined for a text with no front matter. */
|
|
21
|
+
export declare function frontKeys(text: string): string[] | undefined;
|
|
22
|
+
/** The front matter keys a learned skill or draft README may hold: what a skill is (its name and when to use it). Claude
|
|
23
|
+
* Code reads directives from more (hooks, allowed-tools, context, agent, model, ...): a hook is a shell command its CLI
|
|
24
|
+
* runs. `status` is the host's (a held one). */
|
|
25
|
+
export declare const LEARNED_KEYS: readonly string[];
|
|
26
|
+
/** `text`, a learned skill's SKILL.md, as a run is given it: front matter with its name and its description alone,
|
|
27
|
+
* written again whatever else its file holds (servedSkill is the one door a learned skill reaches a harness by), then its
|
|
28
|
+
* body as it is. A text with no front matter is as it is. */
|
|
29
|
+
export declare function servedSkill(text: string): string;
|
|
30
|
+
/** Why a `SKILL.md` for the skill `name`, or a draft's `README.md` for the workflow `name`, is out of shape; empty when
|
|
31
|
+
* it is not. Its front matter names the folder's name and a description. */
|
|
32
|
+
export declare function skillProblems(text: string, name: string): string[];
|
|
33
|
+
/** A SKILL.md's or a draft README's front matter as a reader shows it: its name, description and status, each absent
|
|
34
|
+
* when it is not there; undefined for a text with no front matter. */
|
|
35
|
+
export declare function learnedFront(text: string): {
|
|
36
|
+
name?: string;
|
|
37
|
+
description?: string;
|
|
38
|
+
status?: string;
|
|
39
|
+
} | undefined;
|
|
40
|
+
/** Whether a SKILL.md or a draft's README.md sets `status`, which only the host does (`held`). */
|
|
41
|
+
export declare const setsStatus: (text: string) => boolean;
|
|
42
|
+
/** A skill or a draft the grader held for its owner's OK: its SKILL.md, or its draft's README.md, says `status:
|
|
43
|
+
* pending`. No run is given a held skill or workflow until a person approves it. */
|
|
44
|
+
export declare const isHeld: (text: string) => boolean;
|
|
45
|
+
/** `text`, a SKILL.md or a draft's README.md, as the host merges one the grader held for its owner's OK: `status:
|
|
46
|
+
* pending` last in its front matter, with LF line ends. Throws on a text with no front matter, which the checker never
|
|
47
|
+
* passes. */
|
|
48
|
+
export declare function held(text: string): string;
|
|
49
|
+
/** `text`, a held SKILL.md or draft README.md, as the host leaves it when its owner approves it: its front matter with
|
|
50
|
+
* no `status` line, so it is given to every later run. Throws on a text with no front matter. */
|
|
51
|
+
export declare function released(text: string): string;
|
|
52
|
+
/** What a SKILL.md or a draft's README.md says of itself: `pending` while it waits for its owner's OK, `retired` once a
|
|
53
|
+
* person retired it, else `active`. A run is given only an active one. */
|
|
54
|
+
export declare function learnedStatus(text: string): "active" | "pending" | "retired";
|
|
55
|
+
/** `text`, a SKILL.md or a draft's README.md, as a person's retire leaves it: `status: retired` last in its front matter
|
|
56
|
+
* (any status line before it dropped), so no later run is given it. Throws on a text with no front matter. */
|
|
57
|
+
export declare function retiredLearned(text: string): string;
|
|
58
|
+
/** A draft's `README.md` as the host writes it, from the workflow's name and the one-line description its block gives. */
|
|
59
|
+
export declare const readmeOf: (name: string, description: string) => string;
|
|
60
|
+
/** A workflow block's two files: its `description:` front matter written as the draft's README.md (readmeOf), and the
|
|
61
|
+
* JSON after it as its workflow.json. A block with no front matter, or none that says what the draft is for, gives a
|
|
62
|
+
* README with no description, which the checker refuses; what the JSON holds is the checker's to judge. */
|
|
63
|
+
export declare function draftFiles(name: string, block: string): {
|
|
64
|
+
json: string;
|
|
65
|
+
readme: string;
|
|
66
|
+
};
|
|
67
|
+
/** The refusal for a draft that names a tool when the host gave no tool list: the check fails closed. */
|
|
68
|
+
export declare const NO_TOOL_LIST = "no tool list was given, so a draft that names a tool is refused";
|
|
69
|
+
export type DraftProblem = {
|
|
70
|
+
readonly rule: "workflow" | "no_tool_list" | "executable_node";
|
|
71
|
+
readonly detail: string;
|
|
72
|
+
};
|
|
73
|
+
/** What a draft is read with: the workflow DSL's own validation and inspection. The caller loads the DSL (`await
|
|
74
|
+
* import("@parcha/agentrun-dsl")`), which the server only ever loads when it needs it, and hands it here. */
|
|
75
|
+
export type DraftReader = {
|
|
76
|
+
readonly validateWorkflow: typeof validateWorkflow;
|
|
77
|
+
readonly inspectWorkflow: typeof inspectWorkflow;
|
|
78
|
+
readonly desugarWorkflow: typeof desugarWorkflow;
|
|
79
|
+
readonly assertWorkflowCapabilities: typeof assertWorkflowCapabilities;
|
|
80
|
+
};
|
|
81
|
+
/** What a learned workflow's model node is refused with when it names a tool: it runs in a session of its own, with no
|
|
82
|
+
* tool (learned-workflow.ts). Refused at learn time and before a call runs any node, never after earlier nodes spent. */
|
|
83
|
+
export declare const MODEL_NODE_TOOLS = "a learned workflow's model node runs without tools";
|
|
84
|
+
/** What a learned workflow is refused with when a node needs more than its runner gives the DSL (learned-workflow.ts:
|
|
85
|
+
* runNode and runEffect alone): a judge, pick or sift node, a route by question, a verify or an ask needs a decision
|
|
86
|
+
* provider (runJudge) a run is not given. Refused at learn time and before a call runs any node, never after. */
|
|
87
|
+
export declare const LEARNED_NODES = "a learned workflow runs model and tool-call nodes";
|
|
88
|
+
/** The most turns a learned workflow's model node runs (learned-workflow.ts hands its runner this cap): an agent node
|
|
89
|
+
* that asks for more is refused at learn time and before a call, never when it comes up after earlier nodes spent. */
|
|
90
|
+
export declare const LEARNED_MAX_TURNS = 8;
|
|
91
|
+
/** Why `workflows/<name>/workflow.json` is not a draft the host can run; empty when it is. The DSL's own
|
|
92
|
+
* `validateWorkflow` judges it, with `executeCode: false` so no code it holds is run (a code node is syntax-checked),
|
|
93
|
+
* and with `tools`, so a node that names a tool outside them is its error: a harness's own tool (Bash, exec_command,
|
|
94
|
+
* apply_patch) is never on the list. A model node that names any tool is refused (MODEL_NODE_TOOLS), and a node the
|
|
95
|
+
* learned runner cannot run (LEARNED_NODES: a judge's). With no list it
|
|
96
|
+
* fails closed: a draft the DSL refuses only once given an empty list names a tool. */
|
|
97
|
+
export declare function draftProblems(dsl: DraftReader, text: string, tools?: readonly string[], gated?: Readonly<Record<string, string>>): DraftProblem[];
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/** Where a skill and a workflow draft live in the folder. */
|
|
2
|
+
export const SKILLS_DIR = "skills";
|
|
3
|
+
export const WORKFLOWS_DIR = "workflows";
|
|
4
|
+
/** The shared skills convention's name: lowercase letters, digits and single dashes, at most 64. */
|
|
5
|
+
export const SKILL_NAME = /^(?=.{1,64}$)[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
6
|
+
/** A workflow's name: lowercase letters, digits, dashes and underscores, at most 64, starting with a letter or digit. */
|
|
7
|
+
export const WORKFLOW_NAME = /^[a-z0-9][a-z0-9_-]{0,63}$/;
|
|
8
|
+
const PLACES = [
|
|
9
|
+
[/^skills\/([^/]+)\/SKILL\.md$/, "skill"],
|
|
10
|
+
[/^workflows\/([^/]+)\/workflow\.json$/, "workflow"],
|
|
11
|
+
[/^workflows\/([^/]+)\/README\.md$/, "readme"],
|
|
12
|
+
];
|
|
13
|
+
/** Where `path` sits among the skills and workflows, its folder's name as written (unchecked); undefined when it is
|
|
14
|
+
* not a skill's or a workflow's file. */
|
|
15
|
+
export function learnedPlace(path) {
|
|
16
|
+
for (const [pattern, kind] of PLACES) {
|
|
17
|
+
const name = pattern.exec(path)?.[1];
|
|
18
|
+
if (name !== undefined)
|
|
19
|
+
return { kind, name };
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
/** Whether `name` is a good name for a folder of `kind`. */
|
|
24
|
+
export const learnedName = (kind, name) => (kind === "skill" ? SKILL_NAME : WORKFLOW_NAME).test(name);
|
|
25
|
+
/** A SKILL.md's or a draft README's front matter: its lines, where it closes (the index of its closing `\n---\n`, in
|
|
26
|
+
* `lf`, the text with LF line ends) and each key's value, a folded or literal block value read as one line. A string
|
|
27
|
+
* is why there is none. */
|
|
28
|
+
function frontOf(text) {
|
|
29
|
+
const lf = text.replace(/\r\n/g, "\n");
|
|
30
|
+
if (!lf.startsWith("---\n"))
|
|
31
|
+
return "it does not start with front matter";
|
|
32
|
+
const end = lf.indexOf("\n---\n", 3);
|
|
33
|
+
if (end < 0)
|
|
34
|
+
return "its front matter is not closed";
|
|
35
|
+
const lines = lf.slice(4, end).split("\n");
|
|
36
|
+
const value = (key) => {
|
|
37
|
+
const at = lines.findIndex((line) => line.startsWith(`${key}:`));
|
|
38
|
+
if (at < 0)
|
|
39
|
+
return undefined;
|
|
40
|
+
const own = lines[at].slice(key.length + 1).trim();
|
|
41
|
+
const more = [];
|
|
42
|
+
for (const line of lines.slice(at + 1))
|
|
43
|
+
if (/^\s+\S/.test(line))
|
|
44
|
+
more.push(line.trim());
|
|
45
|
+
else
|
|
46
|
+
break;
|
|
47
|
+
if (/^[>|][-+]?$/.test(own))
|
|
48
|
+
return more.join(" ").trim();
|
|
49
|
+
// A plain value folds the indented lines after it in, one space each, as YAML reads it: one that wraps, or one
|
|
50
|
+
// written entirely on the next lines. A quoted one is read as its own line says.
|
|
51
|
+
// A comment line (`# ...`) is no part of a plain value; a block keeps its lines as written.
|
|
52
|
+
const words = more.filter((line) => !line.startsWith("#"));
|
|
53
|
+
if (!own)
|
|
54
|
+
return words.join(" ").trim();
|
|
55
|
+
if (words.length && !/^["']/.test(own))
|
|
56
|
+
return [own, ...words].join(" ").trim();
|
|
57
|
+
return own.replace(/^(["'])(.*)\1$/, "$2").trim();
|
|
58
|
+
};
|
|
59
|
+
return { lf, end, value };
|
|
60
|
+
}
|
|
61
|
+
/** The keys a SKILL.md's or a draft README's front matter sets, in order (each line at its left edge that names one);
|
|
62
|
+
* undefined for a text with no front matter. */
|
|
63
|
+
export function frontKeys(text) {
|
|
64
|
+
const front = frontOf(text);
|
|
65
|
+
if (typeof front === "string")
|
|
66
|
+
return undefined;
|
|
67
|
+
return front.lf.slice(4, front.end).split("\n").flatMap((line) => /^([^\s#:][^:]*):/.exec(line)?.[1] ?? []).map((key) => key.trim());
|
|
68
|
+
}
|
|
69
|
+
/** The front matter keys a learned skill or draft README may hold: what a skill is (its name and when to use it). Claude
|
|
70
|
+
* Code reads directives from more (hooks, allowed-tools, context, agent, model, ...): a hook is a shell command its CLI
|
|
71
|
+
* runs. `status` is the host's (a held one). */
|
|
72
|
+
export const LEARNED_KEYS = ["name", "description"];
|
|
73
|
+
/** `text`, a learned skill's SKILL.md, as a run is given it: front matter with its name and its description alone,
|
|
74
|
+
* written again whatever else its file holds (servedSkill is the one door a learned skill reaches a harness by), then its
|
|
75
|
+
* body as it is. A text with no front matter is as it is. */
|
|
76
|
+
export function servedSkill(text) {
|
|
77
|
+
const front = frontOf(text);
|
|
78
|
+
if (typeof front === "string")
|
|
79
|
+
return text;
|
|
80
|
+
const description = front.value("description") ?? "";
|
|
81
|
+
// Quoted only where YAML would read it as more than one plain line of text.
|
|
82
|
+
const plain = /^[^\s\-?:,[\]{}#&*!|>'"%@`][^\n]*$/.test(description) && !/: |\s#|:$/.test(description);
|
|
83
|
+
const lines = [`name: ${front.value("name") ?? ""}`, `description: ${plain ? description : JSON.stringify(description)}`];
|
|
84
|
+
return `---\n${lines.join("\n")}\n---\n${front.lf.slice(front.end + 5)}`;
|
|
85
|
+
}
|
|
86
|
+
/** Why a `SKILL.md` for the skill `name`, or a draft's `README.md` for the workflow `name`, is out of shape; empty when
|
|
87
|
+
* it is not. Its front matter names the folder's name and a description. */
|
|
88
|
+
export function skillProblems(text, name) {
|
|
89
|
+
const front = frontOf(text);
|
|
90
|
+
if (typeof front === "string")
|
|
91
|
+
return [front];
|
|
92
|
+
const out = [];
|
|
93
|
+
if (front.value("name") !== name)
|
|
94
|
+
out.push(`name is not the folder's name, ${name}`);
|
|
95
|
+
if (!front.value("description"))
|
|
96
|
+
out.push("description is missing or empty");
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
/** A SKILL.md's or a draft README's front matter as a reader shows it: its name, description and status, each absent
|
|
100
|
+
* when it is not there; undefined for a text with no front matter. */
|
|
101
|
+
export function learnedFront(text) {
|
|
102
|
+
const front = frontOf(text);
|
|
103
|
+
if (typeof front === "string")
|
|
104
|
+
return undefined;
|
|
105
|
+
const [name, description, status] = ["name", "description", "status"].map((key) => front.value(key));
|
|
106
|
+
return { ...(name === undefined ? {} : { name }), ...(description === undefined ? {} : { description }), ...(status === undefined ? {} : { status }) };
|
|
107
|
+
}
|
|
108
|
+
/** Whether a SKILL.md or a draft's README.md sets `status`, which only the host does (`held`). */
|
|
109
|
+
export const setsStatus = (text) => {
|
|
110
|
+
const front = frontOf(text);
|
|
111
|
+
return typeof front !== "string" && front.value("status") !== undefined;
|
|
112
|
+
};
|
|
113
|
+
/** A skill or a draft the grader held for its owner's OK: its SKILL.md, or its draft's README.md, says `status:
|
|
114
|
+
* pending`. No run is given a held skill or workflow until a person approves it. */
|
|
115
|
+
export const isHeld = (text) => {
|
|
116
|
+
const front = frontOf(text);
|
|
117
|
+
return typeof front !== "string" && front.value("status") === "pending";
|
|
118
|
+
};
|
|
119
|
+
/** `text`, a SKILL.md or a draft's README.md, as the host merges one the grader held for its owner's OK: `status:
|
|
120
|
+
* pending` last in its front matter, with LF line ends. Throws on a text with no front matter, which the checker never
|
|
121
|
+
* passes. */
|
|
122
|
+
export function held(text) {
|
|
123
|
+
const front = frontOf(text);
|
|
124
|
+
if (typeof front === "string")
|
|
125
|
+
throw new Error(`it cannot be held: ${front}`);
|
|
126
|
+
return `${front.lf.slice(0, front.end)}\nstatus: pending${front.lf.slice(front.end)}`;
|
|
127
|
+
}
|
|
128
|
+
/** `text`, a held SKILL.md or draft README.md, as the host leaves it when its owner approves it: its front matter with
|
|
129
|
+
* no `status` line, so it is given to every later run. Throws on a text with no front matter. */
|
|
130
|
+
export function released(text) {
|
|
131
|
+
const front = frontOf(text);
|
|
132
|
+
if (typeof front === "string")
|
|
133
|
+
throw new Error(`it cannot be approved: ${front}`);
|
|
134
|
+
const kept = front.lf.slice(4, front.end).split("\n").filter((line) => !line.startsWith("status:"));
|
|
135
|
+
return `---\n${kept.join("\n")}${front.lf.slice(front.end)}`;
|
|
136
|
+
}
|
|
137
|
+
/** What a SKILL.md or a draft's README.md says of itself: `pending` while it waits for its owner's OK, `retired` once a
|
|
138
|
+
* person retired it, else `active`. A run is given only an active one. */
|
|
139
|
+
export function learnedStatus(text) {
|
|
140
|
+
const front = frontOf(text);
|
|
141
|
+
const status = typeof front === "string" ? undefined : front.value("status");
|
|
142
|
+
return status === "pending" ? "pending" : status === "retired" ? "retired" : "active";
|
|
143
|
+
}
|
|
144
|
+
/** `text`, a SKILL.md or a draft's README.md, as a person's retire leaves it: `status: retired` last in its front matter
|
|
145
|
+
* (any status line before it dropped), so no later run is given it. Throws on a text with no front matter. */
|
|
146
|
+
export function retiredLearned(text) {
|
|
147
|
+
const front = frontOf(text);
|
|
148
|
+
if (typeof front === "string")
|
|
149
|
+
throw new Error(`it cannot be retired: ${front}`);
|
|
150
|
+
const kept = front.lf.slice(4, front.end).split("\n").filter((line) => !line.startsWith("status:"));
|
|
151
|
+
return `---\n${[...kept, "status: retired"].join("\n")}${front.lf.slice(front.end)}`;
|
|
152
|
+
}
|
|
153
|
+
/** A draft's `README.md` as the host writes it, from the workflow's name and the one-line description its block gives. */
|
|
154
|
+
export const readmeOf = (name, description) => `---\nname: ${name}\ndescription: ${description}\n---\n`;
|
|
155
|
+
/** A workflow block's two files: its `description:` front matter written as the draft's README.md (readmeOf), and the
|
|
156
|
+
* JSON after it as its workflow.json. A block with no front matter, or none that says what the draft is for, gives a
|
|
157
|
+
* README with no description, which the checker refuses; what the JSON holds is the checker's to judge. */
|
|
158
|
+
export function draftFiles(name, block) {
|
|
159
|
+
const front = frontOf(block);
|
|
160
|
+
if (typeof front === "string")
|
|
161
|
+
return { json: block, readme: readmeOf(name, "") };
|
|
162
|
+
const description = (front.value("description") ?? "").replace(/\s+/g, " ").trim();
|
|
163
|
+
return { json: front.lf.slice(front.end + "\n---\n".length), readme: readmeOf(name, description) };
|
|
164
|
+
}
|
|
165
|
+
/** The refusal for a draft that names a tool when the host gave no tool list: the check fails closed. */
|
|
166
|
+
export const NO_TOOL_LIST = "no tool list was given, so a draft that names a tool is refused";
|
|
167
|
+
/** The value at a JSON Pointer (RFC 6901) in `doc`. */
|
|
168
|
+
const at = (doc, pointer) => pointer.split("/").slice(1).reduce((value, key) => value?.[key.replace(/~1/g, "/").replace(/~0/g, "~")], doc);
|
|
169
|
+
/** A draft carries no node that runs code: a `code` node runs in the host's process unless the host isolates it, and a
|
|
170
|
+
* call via shell or executor runs a command. The DSL's inspection lists each node and its pointer. */
|
|
171
|
+
const executableNodes = (dsl, doc) => dsl.inspectWorkflow(doc).nodes.flatMap((node) => {
|
|
172
|
+
if (node.kind === "code")
|
|
173
|
+
return [{ rule: "executable_node", detail: `the code node at ${node.path} runs code in the host's process; a learned draft holds no code node` }];
|
|
174
|
+
const via = node.kind === "call" ? at(doc, node.path)?.via : undefined;
|
|
175
|
+
return via === "shell" || via === "executor" ? [{ rule: "executable_node", detail: `the call node at ${node.path} runs via ${via}; a learned draft never runs a command` }] : [];
|
|
176
|
+
});
|
|
177
|
+
/** What a learned workflow's model node is refused with when it names a tool: it runs in a session of its own, with no
|
|
178
|
+
* tool (learned-workflow.ts). Refused at learn time and before a call runs any node, never after earlier nodes spent. */
|
|
179
|
+
export const MODEL_NODE_TOOLS = "a learned workflow's model node runs without tools";
|
|
180
|
+
/** A draft's model nodes that name a tool: only a call node runs one. The DSL's inspection lists each node and its
|
|
181
|
+
* pointer; a call node's `tool` is the DSL's to check against the list. */
|
|
182
|
+
const toolingNodes = (dsl, doc) => dsl.inspectWorkflow(doc).nodes.flatMap((node) => {
|
|
183
|
+
if (node.kind === "call")
|
|
184
|
+
return [];
|
|
185
|
+
const tools = at(doc, node.path)?.tools;
|
|
186
|
+
return Array.isArray(tools) && tools.length ? [{ rule: "workflow", detail: `the model node at ${node.path} names a tool (${tools.map(String).join(", ").slice(0, 200)}): ${MODEL_NODE_TOOLS}` }] : [];
|
|
187
|
+
});
|
|
188
|
+
/** What a learned workflow is refused with when a node needs more than its runner gives the DSL (learned-workflow.ts:
|
|
189
|
+
* runNode and runEffect alone): a judge, pick or sift node, a route by question, a verify or an ask needs a decision
|
|
190
|
+
* provider (runJudge) a run is not given. Refused at learn time and before a call runs any node, never after. */
|
|
191
|
+
export const LEARNED_NODES = "a learned workflow runs model and tool-call nodes";
|
|
192
|
+
/** A draft's call nodes that call a tool in `gated` (by name, what the gate holds it to: ask or deny), a composed
|
|
193
|
+
* workflow node's inner ones too (the DSL's inspection lists them, by their pointer under it): the run would park on it
|
|
194
|
+
* or refuse it, and a call node cannot wait for an approval. Worded as the server words a workflow revision's call node
|
|
195
|
+
* on such a tool before its run starts (runnable.ts), with the node's place. */
|
|
196
|
+
const gatedNodes = (dsl, doc, gated) => dsl.inspectWorkflow(doc).nodes.flatMap((node) => {
|
|
197
|
+
if (node.kind !== "call")
|
|
198
|
+
return [];
|
|
199
|
+
const tool = at(doc, node.path)?.tool;
|
|
200
|
+
const setting = typeof tool === "string" ? gated[tool] : undefined;
|
|
201
|
+
if (!setting)
|
|
202
|
+
return [];
|
|
203
|
+
const words = setting === "deny"
|
|
204
|
+
? `This workflow's call node calls ${tool}, which is set to deny on this server.`
|
|
205
|
+
: `This workflow's call node calls ${tool}, which is set to ask, and a workflow's call node can't wait for an approval: set it to allow for this agent.`;
|
|
206
|
+
return [{ rule: "workflow", detail: `${words} (the call node at ${node.path})` }];
|
|
207
|
+
});
|
|
208
|
+
/** The most turns a learned workflow's model node runs (learned-workflow.ts hands its runner this cap): an agent node
|
|
209
|
+
* that asks for more is refused at learn time and before a call, never when it comes up after earlier nodes spent. */
|
|
210
|
+
export const LEARNED_MAX_TURNS = 8;
|
|
211
|
+
/** A draft's agent nodes that ask what the learned runner refuses when the node comes up (the pi runner's request-time
|
|
212
|
+
* checks, with the options learned-workflow.ts hands it; learn-runner-parity.test.mjs holds the two together): more
|
|
213
|
+
* turns than LEARNED_MAX_TURNS, or `context: "fork"` (every node starts a fresh session, with no transcript to fork). */
|
|
214
|
+
const longNodes = (dsl, doc) => dsl.inspectWorkflow(doc).nodes.flatMap((node) => {
|
|
215
|
+
const fields = at(doc, node.path) ?? {};
|
|
216
|
+
const out = [];
|
|
217
|
+
if (typeof fields.max_turns === "number" && fields.max_turns > LEARNED_MAX_TURNS)
|
|
218
|
+
out.push({ rule: "workflow", detail: `the agent node at ${node.path} asks max_turns ${fields.max_turns}: a learned workflow's model node runs at most ${LEARNED_MAX_TURNS} turns` });
|
|
219
|
+
if (fields.context === "fork")
|
|
220
|
+
out.push({ rule: "workflow", detail: `the agent node at ${node.path} asks context fork: a learned workflow's model node starts a fresh session` });
|
|
221
|
+
return out;
|
|
222
|
+
});
|
|
223
|
+
/** The node the learned runner cannot run, as the DSL's own check says it (assertWorkflowCapabilities, which runWorkflow
|
|
224
|
+
* makes before any node runs) with the runner's deps: none when it can run every one. */
|
|
225
|
+
const unrunnableNodes = (dsl, doc) => {
|
|
226
|
+
const workflow = dsl.desugarWorkflow(doc);
|
|
227
|
+
const runner = { runNode: async () => ({}), runEffect: async () => ({}) };
|
|
228
|
+
try {
|
|
229
|
+
dsl.assertWorkflowCapabilities(workflow.root, runner, workflow);
|
|
230
|
+
return [];
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
return [{ rule: "workflow", detail: `${error.message.slice(0, 200)}: ${LEARNED_NODES}` }];
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
const issuesOf = (dsl, doc, tools) => {
|
|
237
|
+
const valid = dsl.validateWorkflow(doc, { executeCode: false, ...(tools ? { tools } : {}) });
|
|
238
|
+
return valid.ok ? [] : valid.issues;
|
|
239
|
+
};
|
|
240
|
+
/** Why `workflows/<name>/workflow.json` is not a draft the host can run; empty when it is. The DSL's own
|
|
241
|
+
* `validateWorkflow` judges it, with `executeCode: false` so no code it holds is run (a code node is syntax-checked),
|
|
242
|
+
* and with `tools`, so a node that names a tool outside them is its error: a harness's own tool (Bash, exec_command,
|
|
243
|
+
* apply_patch) is never on the list. A model node that names any tool is refused (MODEL_NODE_TOOLS), and a node the
|
|
244
|
+
* learned runner cannot run (LEARNED_NODES: a judge's). With no list it
|
|
245
|
+
* fails closed: a draft the DSL refuses only once given an empty list names a tool. */
|
|
246
|
+
export function draftProblems(dsl, text, tools, gated = {}) {
|
|
247
|
+
let doc;
|
|
248
|
+
try {
|
|
249
|
+
doc = JSON.parse(text);
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
return [{ rule: "workflow", detail: "it does not parse as JSON" }];
|
|
253
|
+
}
|
|
254
|
+
// A tool in `gated` is the agent's own, held back by the gate: on the list here, so the refusal is the gate's words
|
|
255
|
+
// (gatedNodes), not the DSL's for a tool it was never given.
|
|
256
|
+
const listed = tools ? [...tools, ...Object.keys(gated)] : undefined;
|
|
257
|
+
const issues = issuesOf(dsl, doc, listed);
|
|
258
|
+
if (issues.length)
|
|
259
|
+
return issues.slice(0, 5).map((issue) => ({ rule: "workflow", detail: `the workflow DSL refuses it: ${issue.code} at ${issue.path || "/"}: ${issue.message.slice(0, 200)}` }));
|
|
260
|
+
let out;
|
|
261
|
+
try {
|
|
262
|
+
out = [...toolingNodes(dsl, doc), ...executableNodes(dsl, doc), ...unrunnableNodes(dsl, doc), ...longNodes(dsl, doc), ...gatedNodes(dsl, doc, gated)];
|
|
263
|
+
}
|
|
264
|
+
catch (error) {
|
|
265
|
+
// The inspection reads the whole document, as validation need not: what it cannot read is refused, never passed.
|
|
266
|
+
return [{ rule: "workflow", detail: `the workflow DSL cannot inspect it: ${error.message.slice(0, 200)}` }];
|
|
267
|
+
}
|
|
268
|
+
if (!tools && issuesOf(dsl, doc, []).length)
|
|
269
|
+
out.push({ rule: "no_tool_list", detail: NO_TOOL_LIST });
|
|
270
|
+
return out;
|
|
271
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type ToolRegistration } from "@earendil-works/pi-durable";
|
|
2
|
+
import { type LearnedWorkflow } from "./learning.js";
|
|
3
|
+
import type { ModelRef } from "./model.js";
|
|
4
|
+
import { type Ledger } from "./paid-calls.js";
|
|
5
|
+
import type { Plane } from "./tool-plane.js";
|
|
6
|
+
import type { Complete } from "./llm.js";
|
|
7
|
+
export declare const LEARNED_WORKFLOW_TOOL = "learned_workflow";
|
|
8
|
+
/** One line of a learned workflow's output for its row: an object's fields as `name value`, each value whole or, when
|
|
9
|
+
* longer than VALUE_MAX, only as its size, and whole fields only until LINE_MAX. Nothing is ever cut inside a value:
|
|
10
|
+
* the timeline redacts the line as it says it (its summary's words), and its redactor matches a held value only whole,
|
|
11
|
+
* so a cut value could leave a prefix it would not find. Newlines are flattened, so the one line it keeps holds every
|
|
12
|
+
* field. An output already cut for the model (learnedText) says so. */
|
|
13
|
+
export declare function outputLine(text: string): string | undefined;
|
|
14
|
+
/** The models a learned workflow's model nodes run on: the run's own (capped and marked: app.ts), as pi-ai's. */
|
|
15
|
+
type RunModels = {
|
|
16
|
+
getModel(provider: string, id: string): unknown;
|
|
17
|
+
getModels(): readonly unknown[];
|
|
18
|
+
completeSimple(model: never, context: never, options?: never): Promise<unknown>;
|
|
19
|
+
};
|
|
20
|
+
export type LearnedWorkflowRun = {
|
|
21
|
+
/** The learned workflows the run's pin names (pinnedWorkflows). */
|
|
22
|
+
workflows: readonly LearnedWorkflow[];
|
|
23
|
+
/** The tools the agent's revision grants: a workflow's call node may name only those a call node runs here
|
|
24
|
+
* (tools.ts learnedWorkflowTools), and of those, none `plane` resolves unavailable. */
|
|
25
|
+
granted: readonly string[];
|
|
26
|
+
/** The run's plane (tool-plane.ts planeOf), as its harness and its tools read it. */
|
|
27
|
+
plane?: Plane;
|
|
28
|
+
env: NodeJS.ProcessEnv;
|
|
29
|
+
work: string;
|
|
30
|
+
models: RunModels;
|
|
31
|
+
/** The run's model a model node of `tier` runs on: the tier's, else the run's own. */
|
|
32
|
+
modelOf(tier: string | undefined): ModelRef | undefined;
|
|
33
|
+
/** The `llm` tool's completion, for a call node that names it. */
|
|
34
|
+
complete?: Complete;
|
|
35
|
+
/** The run's gate's permission of a tool now (tool-gate.ts RunGate policy): a call node's tool set to ask or deny is
|
|
36
|
+
* refused before any node runs, and asked again as each call node runs, a composed workflow's inner ones too, so one
|
|
37
|
+
* set to either since the call began is refused there, never run. None: every tool allowed (a test's run). */
|
|
38
|
+
policy?: (tool: string) => string;
|
|
39
|
+
};
|
|
40
|
+
/** The most characters of a learned workflow's output its answer holds, monid's bound (monid.ts CLIP): within the
|
|
41
|
+
* host's result bound, which each CLI is told to pass whole (host-bounds.test). */
|
|
42
|
+
export declare const LEARNED_CLIP = 60000;
|
|
43
|
+
/** `text`, a learned workflow's output as JSON, cut at LEARNED_CLIP characters (never inside a surrogate pair), saying
|
|
44
|
+
* so. */
|
|
45
|
+
export declare function learnedText(text: string): string;
|
|
46
|
+
/** What a learned workflow's model node is refused with when it names a tool: it runs in a session of its own here.
|
|
47
|
+
* The check before the run refuses such a workflow first (draftProblems); this one holds should it not. */
|
|
48
|
+
export declare const NODE_TOOLS_REFUSED = "a learned workflow's model node runs without tools here";
|
|
49
|
+
/** The tool's description: what it does, and each workflow by name with the line its README says what it does with. */
|
|
50
|
+
export declare const learnedWorkflowDescription: (workflows: readonly LearnedWorkflow[]) => string;
|
|
51
|
+
/** The run's `learned_workflow` tool over `run.workflows`. */
|
|
52
|
+
export declare function learnedWorkflowTool(run: LearnedWorkflowRun): ToolRegistration;
|
|
53
|
+
/** `flow` run on `input` for `run`: its output as JSON, or why it did not run. Never throws, but on an abort. `ledger`:
|
|
54
|
+
* the tool call's, which each call node's paid requests are booked under, as a call of its own within it. */
|
|
55
|
+
export declare function runLearned(run: LearnedWorkflowRun, flow: LearnedWorkflow, input: unknown, signal?: AbortSignal, ledger?: Ledger): Promise<{
|
|
56
|
+
text: string;
|
|
57
|
+
isError: boolean;
|
|
58
|
+
}>;
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { defineTool } from "@earendil-works/pi-durable";
|
|
2
|
+
import { registerLabel, registerOutcome } from "@parcha/pi-durable-agent/timeline";
|
|
3
|
+
import { sideRequests } from "./cap.js";
|
|
4
|
+
import { draftProblems, LEARNED_MAX_TURNS } from "./learn/index.js";
|
|
5
|
+
import { workflowLine } from "./learning.js";
|
|
6
|
+
import { ledgerOf } from "./paid-calls.js";
|
|
7
|
+
import { forModel } from "./result.js";
|
|
8
|
+
import { deniedText } from "./tool-gate.js";
|
|
9
|
+
import { allows, CALL_TOOLS, learnedDraftTools, toolOf } from "./tools.js";
|
|
10
|
+
export const LEARNED_WORKFLOW_TOOL = "learned_workflow";
|
|
11
|
+
/** The most of a learned workflow's output its result line shows, and the longest value it shows as written. */
|
|
12
|
+
const LINE_MAX = 120;
|
|
13
|
+
const VALUE_MAX = 60;
|
|
14
|
+
/** One line of a learned workflow's output for its row: an object's fields as `name value`, each value whole or, when
|
|
15
|
+
* longer than VALUE_MAX, only as its size, and whole fields only until LINE_MAX. Nothing is ever cut inside a value:
|
|
16
|
+
* the timeline redacts the line as it says it (its summary's words), and its redactor matches a held value only whole,
|
|
17
|
+
* so a cut value could leave a prefix it would not find. Newlines are flattened, so the one line it keeps holds every
|
|
18
|
+
* field. An output already cut for the model (learnedText) says so. */
|
|
19
|
+
export function outputLine(text) {
|
|
20
|
+
const clipped = /\n\[the workflow's output is cut at (\d+) of (\d+) characters\]$/.exec(text);
|
|
21
|
+
if (clipped)
|
|
22
|
+
return `its output is long, cut at ${clipped[1]} of ${clipped[2]} characters`;
|
|
23
|
+
let output;
|
|
24
|
+
try {
|
|
25
|
+
output = JSON.parse(text);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
const said = (value) => {
|
|
31
|
+
const flat = (typeof value === "string" ? value : JSON.stringify(value) ?? String(value)).replace(/\s+/g, " ").trim();
|
|
32
|
+
return flat.length > VALUE_MAX ? `(${flat.length} characters)` : flat;
|
|
33
|
+
};
|
|
34
|
+
if (!output || typeof output !== "object" || Array.isArray(output))
|
|
35
|
+
return said(output);
|
|
36
|
+
const fields = [];
|
|
37
|
+
let length = 0;
|
|
38
|
+
for (const [key, value] of Object.entries(output)) {
|
|
39
|
+
const field = `${key} ${said(value)}`;
|
|
40
|
+
if (fields.length && length + 2 + field.length > LINE_MAX) {
|
|
41
|
+
fields.push("…");
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
fields.push(field);
|
|
45
|
+
length += (fields.length > 1 ? 2 : 0) + field.length;
|
|
46
|
+
}
|
|
47
|
+
return fields.length ? fields.join(", ") : undefined;
|
|
48
|
+
}
|
|
49
|
+
// The timeline's words for a learned workflow's call: the workflow it ran (its name), and, once it returns, its output.
|
|
50
|
+
registerLabel(LEARNED_WORKFLOW_TOOL, (a) => (typeof a.name === "string" && a.name ? `Ran the learned workflow ${a.name}` : "Ran a learned workflow"), undefined, { name: "Run a learned workflow" });
|
|
51
|
+
registerOutcome(LEARNED_WORKFLOW_TOOL, (result) => {
|
|
52
|
+
const line = outputLine(result.text);
|
|
53
|
+
return line === undefined ? undefined : { failed: false, text: line };
|
|
54
|
+
});
|
|
55
|
+
/** The most characters of a learned workflow's output its answer holds, monid's bound (monid.ts CLIP): within the
|
|
56
|
+
* host's result bound, which each CLI is told to pass whole (host-bounds.test). */
|
|
57
|
+
export const LEARNED_CLIP = 60_000;
|
|
58
|
+
/** `text`, a learned workflow's output as JSON, cut at LEARNED_CLIP characters (never inside a surrogate pair), saying
|
|
59
|
+
* so. */
|
|
60
|
+
export function learnedText(text) {
|
|
61
|
+
if (text.length <= LEARNED_CLIP)
|
|
62
|
+
return text;
|
|
63
|
+
const end = /[\uD800-\uDBFF]/.test(text[LEARNED_CLIP - 1] ?? "") ? LEARNED_CLIP - 1 : LEARNED_CLIP;
|
|
64
|
+
return `${text.slice(0, end)}\n[the workflow's output is cut at ${end} of ${text.length} characters]`;
|
|
65
|
+
}
|
|
66
|
+
/** What a learned workflow's model node is refused with when it names a tool: it runs in a session of its own here.
|
|
67
|
+
* The check before the run refuses such a workflow first (draftProblems); this one holds should it not. */
|
|
68
|
+
export const NODE_TOOLS_REFUSED = "a learned workflow's model node runs without tools here";
|
|
69
|
+
/** The tool's description: what it does, and each workflow by name with the line its README says what it does with. */
|
|
70
|
+
export const learnedWorkflowDescription = (workflows) => [
|
|
71
|
+
"Run a workflow this agent learned, as one step: give its name and its input, a JSON object as the workflow's input",
|
|
72
|
+
"schema says. It answers the workflow's output as JSON, or why it did not run. The workflows:",
|
|
73
|
+
...workflows.map(workflowLine),
|
|
74
|
+
].join("\n");
|
|
75
|
+
/** One model request as a pi-ai event stream, sent as a side request through `models.completeSimple`: marked and costed
|
|
76
|
+
* on the run's timeline under `learned_workflow`, and under the run's cap. */
|
|
77
|
+
async function sideStream(models, model, context, options) {
|
|
78
|
+
const { createAssistantMessageEventStream } = await import("@earendil-works/pi-ai");
|
|
79
|
+
const stream = createAssistantMessageEventStream();
|
|
80
|
+
sideRequests.set(context, { label: LEARNED_WORKFLOW_TOOL, extraInputTokens: 0, maxFeeUsd: 0, feeUsd: () => 0 });
|
|
81
|
+
void (async () => {
|
|
82
|
+
try {
|
|
83
|
+
const message = (await models.completeSimple(model, context, options));
|
|
84
|
+
if (message.stopReason === "error" || message.stopReason === "aborted")
|
|
85
|
+
stream.push({ type: "error", reason: message.stopReason, error: message });
|
|
86
|
+
else
|
|
87
|
+
stream.push({ type: "done", reason: message.stopReason, message });
|
|
88
|
+
stream.end(message);
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
const failed = { role: "assistant", content: [], stopReason: "error", errorMessage: String(error?.message ?? error), timestamp: Date.now(), usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } } };
|
|
92
|
+
stream.push({ type: "error", reason: "error", error: failed });
|
|
93
|
+
stream.end(failed);
|
|
94
|
+
}
|
|
95
|
+
})();
|
|
96
|
+
return stream;
|
|
97
|
+
}
|
|
98
|
+
/** The run's `learned_workflow` tool over `run.workflows`. */
|
|
99
|
+
export function learnedWorkflowTool(run) {
|
|
100
|
+
const names = run.workflows.map(({ name }) => name);
|
|
101
|
+
return defineTool({
|
|
102
|
+
name: LEARNED_WORKFLOW_TOOL,
|
|
103
|
+
description: learnedWorkflowDescription(run.workflows),
|
|
104
|
+
// Never run again after a crash (pi-durable's "unsafe"; a HostTool's "never": host-tools.ts REPLAY).
|
|
105
|
+
replay: "unsafe",
|
|
106
|
+
parameters: {
|
|
107
|
+
type: "object",
|
|
108
|
+
additionalProperties: false,
|
|
109
|
+
required: ["name", "input"],
|
|
110
|
+
properties: {
|
|
111
|
+
name: { type: "string", enum: names, description: "The learned workflow to run." },
|
|
112
|
+
input: { type: "object", description: "The workflow's input, as its input schema says." },
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
execute: async (args, api, context) => {
|
|
116
|
+
const { name, input } = (args ?? {});
|
|
117
|
+
const flow = run.workflows.find((workflow) => workflow.name === name);
|
|
118
|
+
if (!flow)
|
|
119
|
+
return forModel(`No learned workflow of this agent is named ${JSON.stringify(name)}. Its workflows: ${names.join(", ")}.`, { isError: true });
|
|
120
|
+
const said = await runLearned(run, flow, input, context?.abortSignal, ledgerOf(api, context));
|
|
121
|
+
return forModel(said.text, { isError: said.isError });
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/** `flow` run on `input` for `run`: its output as JSON, or why it did not run. Never throws, but on an abort. `ledger`:
|
|
126
|
+
* the tool call's, which each call node's paid requests are booked under, as a call of its own within it. */
|
|
127
|
+
export async function runLearned(run, flow, input, signal, ledger) {
|
|
128
|
+
const refuse = (why) => ({ text: `The learned workflow ${flow.name} was not run: ${why}`, isError: true });
|
|
129
|
+
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
130
|
+
return refuse("its input must be a JSON object");
|
|
131
|
+
const dsl = await import("@parcha/agentrun-dsl");
|
|
132
|
+
// Checked again at the call, against the agent's own tools as its run has them, before any node runs: what merged under
|
|
133
|
+
// another revision may name one it has no more.
|
|
134
|
+
// The tools the gate allows now; one set to ask or deny is refused in the words a workflow revision's call node is.
|
|
135
|
+
const { tools, gated } = learnedDraftTools(run.granted, (name) => run.policy?.(name) ?? "allow", run.plane);
|
|
136
|
+
const problems = draftProblems(dsl, flow.workflow, tools, gated);
|
|
137
|
+
if (problems.length)
|
|
138
|
+
return refuse(problems.map(({ detail }) => detail).join("; "));
|
|
139
|
+
const doc = JSON.parse(flow.workflow);
|
|
140
|
+
const { createPiHostRunner } = await import("@parcha/agentrun-pi");
|
|
141
|
+
const runners = new Map();
|
|
142
|
+
const runnerFor = async (tier) => {
|
|
143
|
+
const known = runners.get(tier ?? "default");
|
|
144
|
+
if (known)
|
|
145
|
+
return known;
|
|
146
|
+
const ref = run.modelOf(tier);
|
|
147
|
+
if (!ref)
|
|
148
|
+
throw new Error("the run has no model for its model nodes");
|
|
149
|
+
const model = run.models.getModel(ref.provider, ref.modelId);
|
|
150
|
+
if (!model)
|
|
151
|
+
throw new Error(`the model ${ref.provider}/${ref.modelId} is not known here`);
|
|
152
|
+
const registry = { getAll: () => run.models.getModels(), streamSimple: (m, ctx, options) => sideStream(run.models, m, ctx, options) };
|
|
153
|
+
const runner = createPiHostRunner({ cwd: run.work, model: model, modelRegistry: registry }, { tools: [], maxTurns: LEARNED_MAX_TURNS });
|
|
154
|
+
runners.set(tier ?? "default", runner);
|
|
155
|
+
return runner;
|
|
156
|
+
};
|
|
157
|
+
const runNode = async (request) => {
|
|
158
|
+
if (request.tools?.length)
|
|
159
|
+
throw new Error(`${NODE_TOOLS_REFUSED}: its node "${request.label}" names ${request.tools.join(", ")}`);
|
|
160
|
+
// Its own signal, which the DSL ties to the call's (a sibling's failure, a deadline, the call's stop).
|
|
161
|
+
return (await runnerFor(request.tier))(request);
|
|
162
|
+
};
|
|
163
|
+
const runEffect = async ({ node, input: given, signal: own, executionPath, item }) => {
|
|
164
|
+
const tool = node.via === "tool" ? node.tool : undefined;
|
|
165
|
+
const effect = tool && allows(CALL_TOOLS, tool) && tools.includes(tool) ? toolOf(tool)?.effect : undefined;
|
|
166
|
+
if (!effect)
|
|
167
|
+
throw new Error(`call node "${node.label}": this agent runs no tool ${tool ?? `via ${node.via}`}`);
|
|
168
|
+
// The gate's one rule, read as each call node runs, as a workflow revision's call node reads it (app.ts runEffect):
|
|
169
|
+
// a tool set to deny since the call began is refused in the gate's words, one set to ask too, since a call node
|
|
170
|
+
// cannot wait for an approval. Neither runs.
|
|
171
|
+
const permission = run.policy?.(tool) ?? "allow";
|
|
172
|
+
if (permission === "deny")
|
|
173
|
+
throw new Error(deniedText(tool));
|
|
174
|
+
if (permission === "ask")
|
|
175
|
+
throw new Error(`This ${tool} call needs a person's approval, which a workflow's call node cannot wait for; it was not run.`);
|
|
176
|
+
// Its paid requests booked under the tool's call, as a call of its own: the node, its execution path, its item in a map.
|
|
177
|
+
const booked = ledger ? { ledger: { call: `${ledger.call}/${node.label}@${executionPath ?? ""}${item ? `#${item.index}` : ""}`, record: ledger.record } } : {};
|
|
178
|
+
return effect(given, { signal: own, env: run.env, work: run.work, ...(run.complete ? { complete: run.complete } : {}), ...booked });
|
|
179
|
+
};
|
|
180
|
+
try {
|
|
181
|
+
const result = await dsl.runWorkflow(doc, input, { runNode: runNode, runEffect: runEffect, ...(signal ? { signal } : {}) });
|
|
182
|
+
if (result.status !== "complete")
|
|
183
|
+
return refuse(`it ended ${result.status}`);
|
|
184
|
+
return { text: learnedText(JSON.stringify(result.output ?? null)), isError: false };
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
if (signal?.aborted)
|
|
188
|
+
throw error;
|
|
189
|
+
return { text: `The learned workflow ${flow.name} failed: ${String(error?.message ?? error).slice(0, 1000)}`, isError: true };
|
|
190
|
+
}
|
|
191
|
+
}
|