@intentius/chant-lexicon-fountain 0.124.97 → 0.124.99
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/dist/acp/command-line.d.ts +0 -1
- package/dist/acp/host.d.ts +0 -1
- package/dist/acp/index.d.ts +0 -1
- package/dist/acp/jsonrpc.d.ts +0 -1
- package/dist/acp/output.d.ts +0 -1
- package/dist/acp/protocol.d.ts +0 -1
- package/dist/acp/serve.d.ts +0 -1
- package/dist/acp/server.d.ts +0 -1
- package/dist/acp/turn.d.ts +0 -1
- package/dist/allowlist-refs.d.ts +0 -1
- package/dist/audit-entities.d.ts +0 -1
- package/dist/codegen/docs-cli.d.ts +0 -1
- package/dist/codegen/docs.d.ts +0 -1
- package/dist/codegen/generate-cli.d.ts +0 -1
- package/dist/codegen/generate-lexicon.d.ts +0 -1
- package/dist/codegen/generate-typescript.d.ts +0 -1
- package/dist/codegen/generate.d.ts +0 -1
- package/dist/codegen/naming.d.ts +0 -1
- package/dist/codegen/package.d.ts +0 -1
- package/dist/composites/box.d.ts +0 -1
- package/dist/composites/concierge-stack.d.ts +0 -1
- package/dist/composites/steward.d.ts +0 -1
- package/dist/config.d.ts +0 -1
- package/dist/coverage.d.ts +0 -1
- package/dist/deep-observe-hooks.d.ts +0 -1
- package/dist/deep-observe.d.ts +0 -1
- package/dist/describe-resources.d.ts +0 -1
- package/dist/detect.d.ts +0 -1
- package/dist/entity-props.d.ts +0 -1
- package/dist/export-resources.d.ts +0 -1
- package/dist/generated/index.d.ts +0 -1
- package/dist/generated/runtime.d.ts +0 -1
- package/dist/import/generator.d.ts +0 -1
- package/dist/import/local-agents.d.ts +0 -1
- package/dist/import/parser.d.ts +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/init-templates.d.ts +0 -1
- package/dist/integrity.json +2 -2
- package/dist/lint/audit-catalog.d.ts +0 -1
- package/dist/lint/audit-lineage.d.ts +0 -1
- package/dist/lint/post-synth/ftn010-networking-explicit.d.ts +0 -1
- package/dist/lint/post-synth/ftn011-no-unrestricted-networking.d.ts +0 -1
- package/dist/lint/post-synth/ftn012-no-cloud-credential-env.d.ts +0 -1
- package/dist/lint/post-synth/ftn013-substitution-resolvable.d.ts +0 -1
- package/dist/lint/post-synth/ftn014-vault-shadowing.d.ts +0 -1
- package/dist/lint/post-synth/ftn015-mcp-secret-env-substitution.d.ts +0 -1
- package/dist/lint/post-synth/ftn016-runtime-model-valid.d.ts +0 -1
- package/dist/lint/post-synth/ftn017-unique-resource-names.d.ts +0 -1
- package/dist/lint/post-synth/ftn023-acp-runtime-command.d.ts +0 -1
- package/dist/lint/post-synth/ftn024-setup-timeout-range.d.ts +0 -1
- package/dist/lint/post-synth/ftn025-removed-upstream.d.ts +0 -1
- package/dist/lint/post-synth/index.d.ts +0 -1
- package/dist/lint/rules/ftn001-no-secret-literals.d.ts +0 -1
- package/dist/lint/rules/index.d.ts +0 -1
- package/dist/live-identity.d.ts +0 -1
- package/dist/lsp/completions.d.ts +0 -1
- package/dist/lsp/hover.d.ts +0 -1
- package/dist/lsp/lexicon-index.d.ts +0 -1
- package/dist/manifest.json +1 -1
- package/dist/op/activities/fountain-apply.d.ts +0 -1
- package/dist/op/activities/fountain-prompt.d.ts +0 -1
- package/dist/op/activities/fountain-run.d.ts +0 -1
- package/dist/op/activities/index.d.ts +0 -1
- package/dist/op/run-prompt.d.ts +0 -1
- package/dist/op/runtime.d.ts +0 -1
- package/dist/package-cli.d.ts +0 -1
- package/dist/plugin.d.ts +0 -1
- package/dist/reference-catalog.d.ts +0 -1
- package/dist/retired.d.ts +0 -1
- package/dist/serializer.d.ts +0 -1
- package/dist/spec/fetch.d.ts +0 -1
- package/dist/spec/parse.d.ts +0 -1
- package/dist/validate-cli.d.ts +0 -1
- package/dist/validate.d.ts +0 -1
- package/package.json +3 -4
- package/dist/acp/command-line.d.ts.map +0 -1
- package/dist/acp/host.d.ts.map +0 -1
- package/dist/acp/index.d.ts.map +0 -1
- package/dist/acp/jsonrpc.d.ts.map +0 -1
- package/dist/acp/output.d.ts.map +0 -1
- package/dist/acp/protocol.d.ts.map +0 -1
- package/dist/acp/serve.d.ts.map +0 -1
- package/dist/acp/server.d.ts.map +0 -1
- package/dist/acp/turn.d.ts.map +0 -1
- package/dist/allowlist-refs.d.ts.map +0 -1
- package/dist/audit-entities.d.ts.map +0 -1
- package/dist/codegen/docs-cli.d.ts.map +0 -1
- package/dist/codegen/docs.d.ts.map +0 -1
- package/dist/codegen/generate-cli.d.ts.map +0 -1
- package/dist/codegen/generate-lexicon.d.ts.map +0 -1
- package/dist/codegen/generate-typescript.d.ts.map +0 -1
- package/dist/codegen/generate.d.ts.map +0 -1
- package/dist/codegen/naming.d.ts.map +0 -1
- package/dist/codegen/package.d.ts.map +0 -1
- package/dist/composites/box.d.ts.map +0 -1
- package/dist/composites/concierge-stack.d.ts.map +0 -1
- package/dist/composites/steward.d.ts.map +0 -1
- package/dist/config.d.ts.map +0 -1
- package/dist/coverage.d.ts.map +0 -1
- package/dist/deep-observe-hooks.d.ts.map +0 -1
- package/dist/deep-observe.d.ts.map +0 -1
- package/dist/describe-resources.d.ts.map +0 -1
- package/dist/detect.d.ts.map +0 -1
- package/dist/entity-props.d.ts.map +0 -1
- package/dist/export-resources.d.ts.map +0 -1
- package/dist/generated/index.d.ts.map +0 -1
- package/dist/generated/runtime.d.ts.map +0 -1
- package/dist/import/generator.d.ts.map +0 -1
- package/dist/import/local-agents.d.ts.map +0 -1
- package/dist/import/parser.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/init-templates.d.ts.map +0 -1
- package/dist/lint/audit-catalog.d.ts.map +0 -1
- package/dist/lint/audit-lineage.d.ts.map +0 -1
- package/dist/lint/post-synth/ftn010-networking-explicit.d.ts.map +0 -1
- package/dist/lint/post-synth/ftn011-no-unrestricted-networking.d.ts.map +0 -1
- package/dist/lint/post-synth/ftn012-no-cloud-credential-env.d.ts.map +0 -1
- package/dist/lint/post-synth/ftn013-substitution-resolvable.d.ts.map +0 -1
- package/dist/lint/post-synth/ftn014-vault-shadowing.d.ts.map +0 -1
- package/dist/lint/post-synth/ftn015-mcp-secret-env-substitution.d.ts.map +0 -1
- package/dist/lint/post-synth/ftn016-runtime-model-valid.d.ts.map +0 -1
- package/dist/lint/post-synth/ftn017-unique-resource-names.d.ts.map +0 -1
- package/dist/lint/post-synth/ftn023-acp-runtime-command.d.ts.map +0 -1
- package/dist/lint/post-synth/ftn024-setup-timeout-range.d.ts.map +0 -1
- package/dist/lint/post-synth/ftn025-removed-upstream.d.ts.map +0 -1
- package/dist/lint/post-synth/index.d.ts.map +0 -1
- package/dist/lint/rules/ftn001-no-secret-literals.d.ts.map +0 -1
- package/dist/lint/rules/index.d.ts.map +0 -1
- package/dist/live-identity.d.ts.map +0 -1
- package/dist/lsp/completions.d.ts.map +0 -1
- package/dist/lsp/hover.d.ts.map +0 -1
- package/dist/lsp/lexicon-index.d.ts.map +0 -1
- package/dist/op/activities/fountain-apply.d.ts.map +0 -1
- package/dist/op/activities/fountain-prompt.d.ts.map +0 -1
- package/dist/op/activities/fountain-run.d.ts.map +0 -1
- package/dist/op/activities/index.d.ts.map +0 -1
- package/dist/op/run-prompt.d.ts.map +0 -1
- package/dist/op/runtime.d.ts.map +0 -1
- package/dist/package-cli.d.ts.map +0 -1
- package/dist/plugin.d.ts.map +0 -1
- package/dist/reference-catalog.d.ts.map +0 -1
- package/dist/retired.d.ts.map +0 -1
- package/dist/serializer.d.ts.map +0 -1
- package/dist/spec/fetch.d.ts.map +0 -1
- package/dist/spec/parse.d.ts.map +0 -1
- package/dist/validate-cli.d.ts.map +0 -1
- package/dist/validate.d.ts.map +0 -1
- package/src/acp/acp.test.ts +0 -850
- package/src/acp/command-line.ts +0 -192
- package/src/acp/gate-resolution.test.ts +0 -81
- package/src/acp/host.ts +0 -121
- package/src/acp/index.ts +0 -64
- package/src/acp/jsonrpc.ts +0 -239
- package/src/acp/output.ts +0 -59
- package/src/acp/protocol.ts +0 -164
- package/src/acp/serve.ts +0 -67
- package/src/acp/server.ts +0 -376
- package/src/acp/turn.ts +0 -349
- package/src/allowlist-refs.ts +0 -138
- package/src/audit-entities.test.ts +0 -157
- package/src/audit-entities.ts +0 -43
- package/src/codegen/docs-cli.ts +0 -7
- package/src/codegen/docs.ts +0 -139
- package/src/codegen/generate-cli.ts +0 -8
- package/src/codegen/generate-lexicon.ts +0 -70
- package/src/codegen/generate-typescript.ts +0 -69
- package/src/codegen/generate.ts +0 -109
- package/src/codegen/naming.ts +0 -45
- package/src/codegen/package.ts +0 -36
- package/src/composites/box.ts +0 -321
- package/src/composites/composites.test.ts +0 -530
- package/src/composites/concierge-stack.ts +0 -71
- package/src/composites/steward.ts +0 -285
- package/src/config.test.ts +0 -160
- package/src/config.ts +0 -148
- package/src/coverage.test.ts +0 -127
- package/src/coverage.ts +0 -278
- package/src/deep-observe-hooks.ts +0 -238
- package/src/deep-observe.test.ts +0 -684
- package/src/deep-observe.ts +0 -346
- package/src/describe-resources.test.ts +0 -215
- package/src/describe-resources.ts +0 -181
- package/src/detect.ts +0 -43
- package/src/entity-props.ts +0 -22
- package/src/export-resources.ts +0 -159
- package/src/generated/index.d.ts +0 -87
- package/src/generated/index.ts +0 -10
- package/src/generated/runtime.ts +0 -4
- package/src/import/generator.ts +0 -128
- package/src/import/local-agents.test.ts +0 -216
- package/src/import/local-agents.ts +0 -364
- package/src/import/parser.ts +0 -90
- package/src/import/roundtrip.test.ts +0 -256
- package/src/index.ts +0 -68
- package/src/init-templates.e2e.test.ts +0 -171
- package/src/init-templates.ts +0 -223
- package/src/lint/audit-catalog.ts +0 -145
- package/src/lint/audit-lineage.ts +0 -15
- package/src/lint/post-synth/ftn010-networking-explicit.ts +0 -37
- package/src/lint/post-synth/ftn011-no-unrestricted-networking.ts +0 -36
- package/src/lint/post-synth/ftn012-no-cloud-credential-env.ts +0 -58
- package/src/lint/post-synth/ftn013-substitution-resolvable.ts +0 -93
- package/src/lint/post-synth/ftn014-vault-shadowing.ts +0 -61
- package/src/lint/post-synth/ftn015-mcp-secret-env-substitution.ts +0 -49
- package/src/lint/post-synth/ftn016-runtime-model-valid.ts +0 -89
- package/src/lint/post-synth/ftn017-unique-resource-names.ts +0 -43
- package/src/lint/post-synth/ftn023-acp-runtime-command.ts +0 -58
- package/src/lint/post-synth/ftn024-setup-timeout-range.ts +0 -48
- package/src/lint/post-synth/ftn025-removed-upstream.ts +0 -65
- package/src/lint/post-synth/index.ts +0 -27
- package/src/lint/post-synth/post-synth.test.ts +0 -261
- package/src/lint/rules/ftn001-no-secret-literals.ts +0 -126
- package/src/lint/rules/index.ts +0 -7
- package/src/lint/rules/rules.test.ts +0 -161
- package/src/live-identity.ts +0 -153
- package/src/lsp/completions.test.ts +0 -67
- package/src/lsp/completions.ts +0 -11
- package/src/lsp/hover.test.ts +0 -65
- package/src/lsp/hover.ts +0 -68
- package/src/lsp/lexicon-index.ts +0 -46
- package/src/op/activities/fountain-apply.test.ts +0 -666
- package/src/op/activities/fountain-apply.ts +0 -458
- package/src/op/activities/fountain-prompt.test.ts +0 -117
- package/src/op/activities/fountain-prompt.ts +0 -197
- package/src/op/activities/fountain-run.test.ts +0 -295
- package/src/op/activities/fountain-run.ts +0 -292
- package/src/op/activities/index.ts +0 -38
- package/src/op/activities/op-runtime.test.ts +0 -194
- package/src/op/run-prompt.ts +0 -118
- package/src/op/runtime.test.ts +0 -1471
- package/src/op/runtime.ts +0 -1464
- package/src/package-cli.ts +0 -26
- package/src/plugin.test.ts +0 -88
- package/src/plugin.ts +0 -234
- package/src/reference-catalog.ts +0 -27
- package/src/retired.ts +0 -55
- package/src/serializer.test.ts +0 -262
- package/src/serializer.ts +0 -191
- package/src/skills/chant-fountain-locked-sandboxes.md +0 -34
- package/src/skills/chant-fountain-ops.md +0 -166
- package/src/skills/chant-fountain-secrets.md +0 -29
- package/src/skills/chant-fountain.md +0 -69
- package/src/spec/fetch.ts +0 -113
- package/src/spec/parse.ts +0 -484
- package/src/spec/spec.e2e.test.ts +0 -171
- package/src/validate-cli.ts +0 -13
- package/src/validate.ts +0 -39
- /package/{src → dist}/generated/lexicon-fountain.json +0 -0
- /package/{src → dist}/spec/fountain-openapi.snapshot.json +0 -0
package/src/acp/command-line.ts
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A prompt is a chant command line (#2125).
|
|
3
|
-
*
|
|
4
|
-
* That is the whole convention, and the reason a fountain thread reads as an
|
|
5
|
-
* environment's shell history: each turn is `chant run prod-apply --env prod`
|
|
6
|
-
* or `chant lifecycle diff --live`, so scrolling the thread is scrolling what
|
|
7
|
-
* was done to the environment.
|
|
8
|
-
*
|
|
9
|
-
* It is a *command line*, not a shell line. This module splits on whitespace
|
|
10
|
-
* with quote awareness and stops there: no expansion, no substitution, no
|
|
11
|
-
* pipes, no `&&`, no globbing, no `$VAR`. A prompt that does not parse to a
|
|
12
|
-
* chant verb is refused before anything runs, which is what keeps an agent
|
|
13
|
-
* that can be prompted by anyone from being an agent that can run anything.
|
|
14
|
-
* The verb list is core's own registry, so the set of things this accepts is
|
|
15
|
-
* exactly the set `chant --help` prints.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import type { ParsedArgs, CommandDef } from "@intentius/chant/cli/registry";
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Split a command line into argv the way a shell would *quote* it, and in no
|
|
22
|
-
* other way.
|
|
23
|
-
*
|
|
24
|
-
* Single and double quotes group, a backslash escapes the next character
|
|
25
|
-
* inside double quotes and outside them, and nothing else is special. An
|
|
26
|
-
* unterminated quote is an error rather than a silent join, because the
|
|
27
|
-
* silent reading changes which arguments a verb receives.
|
|
28
|
-
*/
|
|
29
|
-
export function tokenize(text: string): string[] {
|
|
30
|
-
const tokens: string[] = [];
|
|
31
|
-
let current = "";
|
|
32
|
-
let has = false;
|
|
33
|
-
let quote: '"' | "'" | undefined;
|
|
34
|
-
|
|
35
|
-
for (let i = 0; i < text.length; i++) {
|
|
36
|
-
const ch = text[i];
|
|
37
|
-
|
|
38
|
-
if (ch === "\\" && quote !== "'" && i + 1 < text.length) {
|
|
39
|
-
current += text[++i];
|
|
40
|
-
has = true;
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
if (quote) {
|
|
44
|
-
if (ch === quote) quote = undefined;
|
|
45
|
-
else current += ch;
|
|
46
|
-
has = true;
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
if (ch === '"' || ch === "'") {
|
|
50
|
-
quote = ch;
|
|
51
|
-
has = true;
|
|
52
|
-
continue;
|
|
53
|
-
}
|
|
54
|
-
if (/\s/.test(ch)) {
|
|
55
|
-
if (has) tokens.push(current);
|
|
56
|
-
current = "";
|
|
57
|
-
has = false;
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
|
-
current += ch;
|
|
61
|
-
has = true;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (quote) throw new Error(`unterminated ${quote === '"' ? "double" : "single"} quote`);
|
|
65
|
-
if (has) tokens.push(current);
|
|
66
|
-
return tokens;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/** A prompt that resolved to something chant can run. */
|
|
70
|
-
export type ChantCommand =
|
|
71
|
-
| {
|
|
72
|
-
/** `chant run <op>` — the path that goes through the op runtime provider. */
|
|
73
|
-
kind: "op-run";
|
|
74
|
-
op: string;
|
|
75
|
-
args: ParsedArgs;
|
|
76
|
-
argv: string[];
|
|
77
|
-
}
|
|
78
|
-
| {
|
|
79
|
-
/** Any other chant verb — run through its own registry handler. */
|
|
80
|
-
kind: "verb";
|
|
81
|
-
name: string;
|
|
82
|
-
def: CommandDef;
|
|
83
|
-
compound: boolean;
|
|
84
|
-
args: ParsedArgs;
|
|
85
|
-
argv: string[];
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
/** Either a command, or the reason the prompt is not one. */
|
|
89
|
-
export type CommandLineParse =
|
|
90
|
-
| { ok: true; command: ChantCommand }
|
|
91
|
-
| { ok: false; message: string; hint: string };
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Parse one prompt into a runnable chant command, or refuse it.
|
|
95
|
-
*
|
|
96
|
-
* Core's registry and argv parser are imported here rather than at module
|
|
97
|
-
* load: nothing about declaring the fountain lexicon should pull in every CLI
|
|
98
|
-
* handler, and only a live `chant acp` turn ever reaches this.
|
|
99
|
-
*/
|
|
100
|
-
export async function parseChantCommandLine(text: string): Promise<CommandLineParse> {
|
|
101
|
-
let tokens: string[];
|
|
102
|
-
try {
|
|
103
|
-
tokens = tokenize(text);
|
|
104
|
-
} catch (err) {
|
|
105
|
-
return {
|
|
106
|
-
ok: false,
|
|
107
|
-
message: err instanceof Error ? err.message : String(err),
|
|
108
|
-
hint: "A prompt is a chant command line — quote arguments that contain spaces.",
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (tokens.length === 0) {
|
|
113
|
-
return { ok: false, message: "the prompt was empty", hint: usageHint() };
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// `chant` may lead, and usually does — it is what a person types. Stripping
|
|
117
|
-
// it here means the same string works whether it was copied from a terminal
|
|
118
|
-
// or typed as a bare verb.
|
|
119
|
-
const argv = tokens[0] === "chant" ? tokens.slice(1) : tokens;
|
|
120
|
-
if (argv.length === 0) {
|
|
121
|
-
return { ok: false, message: "the prompt named no chant command", hint: usageHint() };
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const { parseArgs, commandRegistry } = await import("@intentius/chant/cli/main");
|
|
125
|
-
const { resolveCommand } = await import("@intentius/chant/cli/registry");
|
|
126
|
-
|
|
127
|
-
let args: ParsedArgs;
|
|
128
|
-
try {
|
|
129
|
-
args = parseArgs(argv);
|
|
130
|
-
} catch (err) {
|
|
131
|
-
// A flag problem is only worth reporting for a real chant verb. For
|
|
132
|
-
// anything else (`rm -rf /`), core's unknown-flag refusal (#4369) would
|
|
133
|
-
// mask the reason that matters, which is that the prompt is not chant.
|
|
134
|
-
if (!isChantVerb(parseArgs, resolveCommand, commandRegistry, argv[0])) {
|
|
135
|
-
return {
|
|
136
|
-
ok: false,
|
|
137
|
-
message: `"${argv[0]}" is not a chant command`,
|
|
138
|
-
hint: usageHint(),
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
return {
|
|
142
|
-
ok: false,
|
|
143
|
-
message: err instanceof Error ? err.message : String(err),
|
|
144
|
-
hint: usageHint(),
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const match = resolveCommand(args, commandRegistry);
|
|
149
|
-
if (!match) {
|
|
150
|
-
return {
|
|
151
|
-
ok: false,
|
|
152
|
-
message: `"${argv[0]}" is not a chant command`,
|
|
153
|
-
hint: usageHint(),
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// `chant run <op>` is the one form that takes the provider path with a
|
|
158
|
-
// progress sink. Every other `run` word — `run list`, `run status <op>` —
|
|
159
|
-
// is a registered compound verb, so `resolveCommand` has already told us
|
|
160
|
-
// which this is and no second list of subcommands has to be kept in step
|
|
161
|
-
// with core's.
|
|
162
|
-
if (args.command === "run" && !match.compound && args.path && args.path !== ".") {
|
|
163
|
-
return { ok: true, command: { kind: "op-run", op: args.path, args, argv } };
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return {
|
|
167
|
-
ok: true,
|
|
168
|
-
command: { kind: "verb", name: match.def.name, def: match.def, compound: match.compound, args, argv },
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/** Whether the leading word alone resolves to a registered chant verb. */
|
|
173
|
-
function isChantVerb(
|
|
174
|
-
parseArgs: (argv: string[]) => ParsedArgs,
|
|
175
|
-
resolveCommand: (args: ParsedArgs, registry: CommandDef[]) => unknown,
|
|
176
|
-
registry: CommandDef[],
|
|
177
|
-
word: string,
|
|
178
|
-
): boolean {
|
|
179
|
-
try {
|
|
180
|
-
return resolveCommand(parseArgs([word]), registry) !== null;
|
|
181
|
-
} catch {
|
|
182
|
-
return false;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function usageHint(): string {
|
|
187
|
-
return (
|
|
188
|
-
"A prompt is a chant command line, not a shell line — for example " +
|
|
189
|
-
'"chant run prod-apply --env prod" or "chant lifecycle diff --live". ' +
|
|
190
|
-
'Run "chant --help" for the verb list.'
|
|
191
|
-
);
|
|
192
|
-
}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect } from "vitest";
|
|
2
|
-
import { readFileSync } from "node:fs";
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* chant#2400 — an ACP session cannot clear a gate it produced.
|
|
7
|
-
*
|
|
8
|
-
* The `Steward` composite ships `permission_policy: { default: "auto_allow" }`,
|
|
9
|
-
* with a comment explaining that chant's own gates are where a human belongs.
|
|
10
|
-
* On the `--durable-requests` path that was not true. A gate became a
|
|
11
|
-
* `session/request_permission` (`./turn.ts`'s `gatedReply`), `auto_allow`
|
|
12
|
-
* answered it without anyone, and `AcpServer.resume` then wrote a real gate
|
|
13
|
-
* resolution and re-ran the command, which walked through the gate. The
|
|
14
|
-
* composite's doc described a separation its configuration removed, which is
|
|
15
|
-
* how it survived review: a reader finds an argument rather than a smell.
|
|
16
|
-
*
|
|
17
|
-
* chant#2384 did not close it, and the reason is the interesting part.
|
|
18
|
-
* `createChantHost`'s `resolveGate` called `appendGateResolution` DIRECTLY,
|
|
19
|
-
* writing a line that looked like an approval while skipping every check one
|
|
20
|
-
* carries: #2300's plan binding, so the resolution named no plan and
|
|
21
|
-
* authorised whatever the next run produced rather than what anyone read; the
|
|
22
|
-
* standing-pending-fact requirement, so it could answer a gate nothing had
|
|
23
|
-
* reached; and #2384's origin rule, which is the one that bites here.
|
|
24
|
-
*
|
|
25
|
-
* Routed through `recordGateApproval`, the same function `chant approve`
|
|
26
|
-
* calls, the rule applies: the pending fact and the resolution are both
|
|
27
|
-
* authored by the same ACP session, so it refuses. The property is now a
|
|
28
|
-
* consequence of the mechanism rather than of how a composite is configured.
|
|
29
|
-
*/
|
|
30
|
-
const HOST = fileURLToPath(new URL("./host.ts", import.meta.url));
|
|
31
|
-
const STEWARD = fileURLToPath(new URL("../composites/steward.ts", import.meta.url));
|
|
32
|
-
|
|
33
|
-
describe("an ACP gate resolution goes through the same door as chant approve (chant#2400)", () => {
|
|
34
|
-
const host = readFileSync(HOST, "utf8");
|
|
35
|
-
|
|
36
|
-
test("resolveGate calls recordGateApproval, not appendGateResolution", () => {
|
|
37
|
-
expect(host).toContain("recordGateApproval");
|
|
38
|
-
|
|
39
|
-
// The direct call is the defect. Writing the ledger line without the
|
|
40
|
-
// checks around it produces a record indistinguishable from an approval,
|
|
41
|
-
// which is worse than failing to write one.
|
|
42
|
-
//
|
|
43
|
-
// Matched on the import rather than the bare name, so the comment above
|
|
44
|
-
// `resolveGate` can go on explaining what it no longer does. A test that
|
|
45
|
-
// fails on its own subject being mentioned teaches people to stop
|
|
46
|
-
// mentioning it.
|
|
47
|
-
const importsLedgerWriter = /import\("@intentius\/chant\/lifecycle\/gate-ledger"\)/.test(host)
|
|
48
|
-
&& /\bappendGateResolution\s*[,}]/.test(host);
|
|
49
|
-
expect(
|
|
50
|
-
importsLedgerWriter,
|
|
51
|
-
"resolveGate must not append a resolution directly — that skips the plan binding, the " +
|
|
52
|
-
"standing-pending-fact requirement and the origin rule",
|
|
53
|
-
).toBe(false);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
test("it names the ACP channel, so the origin rule has something to compare", () => {
|
|
57
|
-
// Without an origin the rule cannot fire: `sameOriginRefusal` matches
|
|
58
|
-
// positively, so an unlabelled resolution is never refused.
|
|
59
|
-
expect(host).toMatch(/origin:\s*"acp"/);
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
test("a refusal is surfaced rather than swallowed", () => {
|
|
63
|
-
// `recordGateApproval` reports failure by returning `{ ok: false }` and
|
|
64
|
-
// printing. A caller that ignored that would carry on and re-run the
|
|
65
|
-
// command, which is exactly the bypass this closes.
|
|
66
|
-
expect(host).toMatch(/outcome\.ok/);
|
|
67
|
-
expect(host).toContain("chant approve");
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
test("the Steward's doc no longer claims a separation its configuration removes", () => {
|
|
71
|
-
const steward = readFileSync(STEWARD, "utf8");
|
|
72
|
-
|
|
73
|
-
// The configuration is unchanged and deliberately so: `auto_allow` is right
|
|
74
|
-
// for ordinary tool calls on a machine with nobody at the keyboard. What
|
|
75
|
-
// changed is that it can no longer clear a gate, and the doc has to say
|
|
76
|
-
// which of those it means.
|
|
77
|
-
expect(steward).toContain('permission_policy: { default: "auto_allow" }');
|
|
78
|
-
expect(steward).toMatch(/cannot clear a gate/);
|
|
79
|
-
expect(steward).toMatch(/chant#2384|origin rule/);
|
|
80
|
-
});
|
|
81
|
-
});
|
package/src/acp/host.ts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* What a turn needs from chant, stated once (#2125).
|
|
3
|
-
*
|
|
4
|
-
* The ACP server owns the protocol — sessions, updates, stop reasons — and
|
|
5
|
-
* nothing else. Everything it does *to a project* goes through this seam:
|
|
6
|
-
* finding an Op, starting it on a runtime provider, running any other chant
|
|
7
|
-
* verb, and recording a gate resolution. The conformance test drives the
|
|
8
|
-
* protocol through a stub host, so the tool-call ordering, the cancel path and
|
|
9
|
-
* the gated reply are tested without a checkout, a git branch or a real Op.
|
|
10
|
-
*
|
|
11
|
-
* The real implementation is `createChantHost`, and it is deliberately thin:
|
|
12
|
-
* `createLocalOpRuntime` from core is the provider (#2121), `discoverOps` is
|
|
13
|
-
* the lookup, and a verb runs through core's own registry handler rather than
|
|
14
|
-
* a reimplementation of it. Nothing here shells out — a prompt is parsed and
|
|
15
|
-
* dispatched in this process, so there is no line for a shell to interpret.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import type { OpConfig } from "@intentius/chant/op/types";
|
|
19
|
-
import type { OpRunHandle, OpRunStartOptions } from "@intentius/chant/op/runtime";
|
|
20
|
-
import type { ChantCommand } from "./command-line";
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* What one `*.op.ts` scan found.
|
|
24
|
-
*
|
|
25
|
-
* `errors` is carried rather than dropped because the failure mode it names is
|
|
26
|
-
* indistinguishable from the innocent one: an Op file that will not import
|
|
27
|
-
* reports as "no such Op", and a person reading that in a thread has been told
|
|
28
|
-
* the wrong thing. `chant run` prints these as warnings; so does a turn.
|
|
29
|
-
*/
|
|
30
|
-
export interface OpLookup {
|
|
31
|
-
/** The Op the prompt named, when the scan found it. */
|
|
32
|
-
config?: OpConfig;
|
|
33
|
-
/** Every Op name the scan did find, for the "not declared" reply's hint. */
|
|
34
|
-
names: string[];
|
|
35
|
-
/** Files the scan could not read as an Op. */
|
|
36
|
-
errors: string[];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** The project-facing half of a turn. */
|
|
40
|
-
export interface ChantHost {
|
|
41
|
-
/** Where chant runs — the session's `cwd`. */
|
|
42
|
-
readonly cwd: string;
|
|
43
|
-
/** Scan the project for Ops and pick out the one this name declares. */
|
|
44
|
-
findOp(name: string): Promise<OpLookup>;
|
|
45
|
-
/** Start an Op on the resolved runtime provider. */
|
|
46
|
-
startOp(op: OpConfig, opts: OpRunStartOptions): Promise<OpRunHandle>;
|
|
47
|
-
/** Run a non-`run` chant verb. Returns its exit code; output goes to the process writers. */
|
|
48
|
-
runVerb(command: Extract<ChantCommand, { kind: "verb" }>, signal: AbortSignal): Promise<number>;
|
|
49
|
-
/** Record a gate resolution, the way `chant approve` does (durable-request resume). */
|
|
50
|
-
resolveGate(op: string, gate: string, resolvedBy: string): Promise<void>;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** How the real host identifies itself on a gate resolution it writes. */
|
|
54
|
-
export const ACP_APPROVER = "chant-acp";
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* The real host, bound to one session's working directory.
|
|
58
|
-
*
|
|
59
|
-
* Core's machinery reads the project from `process.cwd()`, so a turn changes
|
|
60
|
-
* into `cwd` for its duration (see ./turn.ts) rather than threading a path
|
|
61
|
-
* through every call — which is also why turns are serialized. Every import
|
|
62
|
-
* below is dynamic: declaring the fountain lexicon must not pull core's CLI
|
|
63
|
-
* handlers into a `chant build`.
|
|
64
|
-
*/
|
|
65
|
-
export function createChantHost(cwd: string): ChantHost {
|
|
66
|
-
return {
|
|
67
|
-
cwd,
|
|
68
|
-
|
|
69
|
-
async findOp(name) {
|
|
70
|
-
const { discoverOps } = await import("@intentius/chant/op/discover");
|
|
71
|
-
const { ops, errors } = await discoverOps({ cwd });
|
|
72
|
-
return {
|
|
73
|
-
...(ops.get(name) ? { config: ops.get(name)?.config } : {}),
|
|
74
|
-
names: [...ops.keys()],
|
|
75
|
-
errors,
|
|
76
|
-
};
|
|
77
|
-
},
|
|
78
|
-
|
|
79
|
-
async startOp(op, opts) {
|
|
80
|
-
const { createLocalOpRuntime } = await import("@intentius/chant/op/runtimes/local");
|
|
81
|
-
return createLocalOpRuntime({ projectPath: cwd }).start(op, opts);
|
|
82
|
-
},
|
|
83
|
-
|
|
84
|
-
async runVerb(command) {
|
|
85
|
-
const { loadPlugins, resolveProjectLexicons } = await import("@intentius/chant/cli/plugins");
|
|
86
|
-
const plugins = command.def.requiresPlugins
|
|
87
|
-
? await loadPlugins(await resolveProjectLexicons(cwd).catch(() => [])).catch(() => [])
|
|
88
|
-
: [];
|
|
89
|
-
return command.def.handler({
|
|
90
|
-
args: command.args,
|
|
91
|
-
plugins,
|
|
92
|
-
serializers: plugins.map((p) => p.serializer),
|
|
93
|
-
});
|
|
94
|
-
},
|
|
95
|
-
|
|
96
|
-
async resolveGate(op, gate, resolvedBy) {
|
|
97
|
-
// chant#2400 — through `recordGateApproval`, the same function `chant
|
|
98
|
-
// approve` calls, rather than straight to `appendGateResolution`.
|
|
99
|
-
//
|
|
100
|
-
// Going direct wrote a line that looked like an approval and was missing
|
|
101
|
-
// every check one carries. It skipped #2300's plan binding, so the
|
|
102
|
-
// resolution named no plan and authorised whatever the next run produced
|
|
103
|
-
// rather than what anyone had read. It skipped the standing-pending-fact
|
|
104
|
-
// requirement, so it could answer a gate nothing had reached. And it
|
|
105
|
-
// skipped chant#2384's origin rule, which is the one that matters here:
|
|
106
|
-
// the pending fact and this resolution are both authored by the same ACP
|
|
107
|
-
// session, so the model that produced the gate was resolving it.
|
|
108
|
-
//
|
|
109
|
-
// With the rule applied, that case now refuses, which is the point. A
|
|
110
|
-
// Steward's gate reaches a person or it does not clear.
|
|
111
|
-
const { recordGateApproval } = await import("@intentius/chant/cli/handlers/operator");
|
|
112
|
-
const outcome = await recordGateApproval(op, gate, { actor: resolvedBy, origin: "acp" });
|
|
113
|
-
if (!outcome.ok) {
|
|
114
|
-
throw new Error(
|
|
115
|
-
`the resolution for gate "${gate}" on "${op}" was refused. A gate reached over ACP cannot ` +
|
|
116
|
-
`also be resolved over ACP — approve it with \`chant approve ${op} ${gate}\` at a shell.`,
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
},
|
|
120
|
-
};
|
|
121
|
-
}
|
package/src/acp/index.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `chant acp` — the CLI mount for the ACP server (#2125).
|
|
3
|
-
*
|
|
4
|
-
* A command group with a `defaultVerb`, so the bare `chant acp` an editor or a
|
|
5
|
-
* fountain Agent spawns (`runtime_command: "chant acp"`) reaches `serve`
|
|
6
|
-
* without the client having to know chant's verb grammar. `chant acp serve`
|
|
7
|
-
* is the same thing spelled out.
|
|
8
|
-
*
|
|
9
|
-
* The server itself is behind a dynamic import: mounting the group happens
|
|
10
|
-
* every time a plugin loads, and nothing about `chant build` should pay for a
|
|
11
|
-
* JSON-RPC peer it will never open.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import type { CommandGroup, CommandGroupContext } from "@intentius/chant/cli/command-group";
|
|
15
|
-
import { splitJoinedFlags, unknownFlagError } from "@intentius/chant/cli/command-group";
|
|
16
|
-
|
|
17
|
-
const BOOLEAN_FLAGS = new Set(["--durable-requests"]);
|
|
18
|
-
|
|
19
|
-
/** The verb group `chant acp` mounts. */
|
|
20
|
-
export function acpCommandGroup(): CommandGroup {
|
|
21
|
-
return {
|
|
22
|
-
name: "acp",
|
|
23
|
-
description: "Serve chant as an Agent Client Protocol agent over stdio",
|
|
24
|
-
defaultVerb: "serve",
|
|
25
|
-
commands: [
|
|
26
|
-
{
|
|
27
|
-
name: "serve",
|
|
28
|
-
description: "Speak ACP on stdin/stdout; each prompt is one chant command line",
|
|
29
|
-
handler: serveHandler,
|
|
30
|
-
},
|
|
31
|
-
],
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async function serveHandler(ctx: CommandGroupContext): Promise<number> {
|
|
36
|
-
let durableRequests = false;
|
|
37
|
-
let steward: string | undefined;
|
|
38
|
-
const args = splitJoinedFlags(ctx.rawArgs, BOOLEAN_FLAGS);
|
|
39
|
-
for (let i = 0; i < args.length; i++) {
|
|
40
|
-
const arg = args[i];
|
|
41
|
-
if (arg === "--durable-requests") durableRequests = true;
|
|
42
|
-
else if (arg === "--steward") {
|
|
43
|
-
// The Steward composite's Agent runs `chant acp --steward <name>`
|
|
44
|
-
// (chant#2749), so every turn in this session is that steward's.
|
|
45
|
-
const name = args[++i];
|
|
46
|
-
if (!name || name.startsWith("--")) {
|
|
47
|
-
console.error(`"chant acp --steward" takes the steward's name.`);
|
|
48
|
-
return 1;
|
|
49
|
-
}
|
|
50
|
-
steward = name;
|
|
51
|
-
} else {
|
|
52
|
-
// stdout carries the protocol and nothing else, so a usage error goes
|
|
53
|
-
// to stderr — the one place a spawned agent can say anything.
|
|
54
|
-
console.error(
|
|
55
|
-
unknownFlagError(arg, `"chant acp" accepts --durable-requests and --steward <name>.`).message,
|
|
56
|
-
);
|
|
57
|
-
return 1;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const { serveAcpOverStdio } = await import("./serve");
|
|
62
|
-
await serveAcpOverStdio({ durableRequests, ...(steward ? { steward } : {}) });
|
|
63
|
-
return 0;
|
|
64
|
-
}
|
package/src/acp/jsonrpc.ts
DELETED
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Newline-delimited JSON-RPC 2.0, both directions (#2125).
|
|
3
|
-
*
|
|
4
|
-
* ACP frames one JSON value per line. That is the whole transport, which is
|
|
5
|
-
* why this is ~150 lines of local code instead of a dependency: an agent that
|
|
6
|
-
* speaks the protocol needs a line reader, a writer that serializes its two
|
|
7
|
-
* writers, and a table of outbound requests waiting on answers.
|
|
8
|
-
*
|
|
9
|
-
* The peer is stream-agnostic on purpose. `chant acp` hands it the process's
|
|
10
|
-
* stdin and stdout; the conformance test hands it a pair of in-memory pipes
|
|
11
|
-
* and drives the client side in the same process, so the test exercises the
|
|
12
|
-
* real framing rather than a mock of it.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import type { Readable, Writable } from "node:stream";
|
|
16
|
-
|
|
17
|
-
/** The four JSON-RPC message kinds, told apart by which fields are present. */
|
|
18
|
-
export interface JsonRpcMessage {
|
|
19
|
-
jsonrpc?: string;
|
|
20
|
-
id?: string | number | null;
|
|
21
|
-
method?: string;
|
|
22
|
-
params?: unknown;
|
|
23
|
-
result?: unknown;
|
|
24
|
-
error?: { code: number; message: string; data?: unknown };
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** An error a handler wants reported with a specific JSON-RPC code. */
|
|
28
|
-
export class JsonRpcError extends Error {
|
|
29
|
-
constructor(
|
|
30
|
-
readonly code: number,
|
|
31
|
-
message: string,
|
|
32
|
-
readonly data?: unknown,
|
|
33
|
-
) {
|
|
34
|
-
super(message);
|
|
35
|
-
this.name = "JsonRpcError";
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** What the peer dispatches incoming traffic to. */
|
|
40
|
-
export interface JsonRpcHandler {
|
|
41
|
-
/** Answer a request. The returned value is encoded as `result`. */
|
|
42
|
-
request(method: string, params: unknown): Promise<unknown>;
|
|
43
|
-
/** Handle a notification. JSON-RPC forbids a reply, so a rejection is dropped. */
|
|
44
|
-
notify(method: string, params: unknown): void;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** A line-delimited byte channel, so the peer never names a stream type. */
|
|
48
|
-
export interface LineTransport {
|
|
49
|
-
send(line: string): void;
|
|
50
|
-
onLine(cb: (line: string) => void): void;
|
|
51
|
-
onClose(cb: () => void): void;
|
|
52
|
-
close(): void;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* A transport over a pair of Node streams — stdin and stdout, for `chant acp`.
|
|
57
|
-
*
|
|
58
|
-
* The writer is bound here, at construction, and that is load-bearing rather
|
|
59
|
-
* than tidy. A turn replaces `process.stdout.write` to stream a command's
|
|
60
|
-
* output (../acp/output.ts); if this looked the method up per call it would
|
|
61
|
-
* find the interceptor, every protocol line the turn emits would come back as
|
|
62
|
-
* another message chunk to emit, and the first notification sent inside a turn
|
|
63
|
-
* would recurse until V8 refused to grow the string. Binding once means the
|
|
64
|
-
* protocol always reaches the real stdout.
|
|
65
|
-
*/
|
|
66
|
-
export function streamTransport(input: Readable, output: Writable): LineTransport {
|
|
67
|
-
const write = output.write.bind(output);
|
|
68
|
-
let buffer = "";
|
|
69
|
-
let lineCb: ((line: string) => void) | undefined;
|
|
70
|
-
let closeCb: (() => void) | undefined;
|
|
71
|
-
|
|
72
|
-
input.setEncoding("utf8");
|
|
73
|
-
input.on("data", (chunk: string) => {
|
|
74
|
-
buffer += chunk;
|
|
75
|
-
let nl = buffer.indexOf("\n");
|
|
76
|
-
while (nl >= 0) {
|
|
77
|
-
const line = buffer.slice(0, nl);
|
|
78
|
-
buffer = buffer.slice(nl + 1);
|
|
79
|
-
if (line.trim().length > 0) lineCb?.(line);
|
|
80
|
-
nl = buffer.indexOf("\n");
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
input.on("end", () => closeCb?.());
|
|
84
|
-
input.on("close", () => closeCb?.());
|
|
85
|
-
|
|
86
|
-
return {
|
|
87
|
-
send(line) {
|
|
88
|
-
write(line + "\n");
|
|
89
|
-
},
|
|
90
|
-
onLine(cb) {
|
|
91
|
-
lineCb = cb;
|
|
92
|
-
},
|
|
93
|
-
onClose(cb) {
|
|
94
|
-
closeCb = cb;
|
|
95
|
-
},
|
|
96
|
-
close() {
|
|
97
|
-
input.destroy();
|
|
98
|
-
},
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/** An in-memory transport pair — one end for each side of a connection. */
|
|
103
|
-
export function memoryTransportPair(): [LineTransport, LineTransport] {
|
|
104
|
-
const cbs: Array<((line: string) => void) | undefined> = [undefined, undefined];
|
|
105
|
-
const closes: Array<(() => void) | undefined> = [undefined, undefined];
|
|
106
|
-
let closed = false;
|
|
107
|
-
|
|
108
|
-
const end = (self: 0 | 1): LineTransport => ({
|
|
109
|
-
send(line) {
|
|
110
|
-
if (closed) return;
|
|
111
|
-
// Deliver asynchronously so a `send` inside a handler never re-enters
|
|
112
|
-
// that handler through the peer's own dispatch — the same ordering a
|
|
113
|
-
// real pipe gives, without the pipe.
|
|
114
|
-
queueMicrotask(() => cbs[self === 0 ? 1 : 0]?.(line));
|
|
115
|
-
},
|
|
116
|
-
onLine(cb) {
|
|
117
|
-
cbs[self] = cb;
|
|
118
|
-
},
|
|
119
|
-
onClose(cb) {
|
|
120
|
-
closes[self] = cb;
|
|
121
|
-
},
|
|
122
|
-
close() {
|
|
123
|
-
if (closed) return;
|
|
124
|
-
closed = true;
|
|
125
|
-
queueMicrotask(() => {
|
|
126
|
-
closes[0]?.();
|
|
127
|
-
closes[1]?.();
|
|
128
|
-
});
|
|
129
|
-
},
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
return [end(0), end(1)];
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* One connection, serving a handler and able to originate requests of its own.
|
|
137
|
-
*
|
|
138
|
-
* The agent has two writers — request responses, and the `session/update`
|
|
139
|
-
* notifications a turn emits while a response is still pending. `send` is the
|
|
140
|
-
* single funnel for both, so two interleaved writes can never produce one
|
|
141
|
-
* unparseable line.
|
|
142
|
-
*/
|
|
143
|
-
export class JsonRpcPeer {
|
|
144
|
-
private nextId = 1;
|
|
145
|
-
private readonly pending = new Map<
|
|
146
|
-
number,
|
|
147
|
-
{ resolve: (v: unknown) => void; reject: (e: unknown) => void }
|
|
148
|
-
>();
|
|
149
|
-
private closed = false;
|
|
150
|
-
|
|
151
|
-
constructor(
|
|
152
|
-
private readonly transport: LineTransport,
|
|
153
|
-
private readonly handler: JsonRpcHandler,
|
|
154
|
-
) {
|
|
155
|
-
transport.onLine((line) => void this.receive(line));
|
|
156
|
-
transport.onClose(() => this.shutdown());
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/** Send a notification. Fire and forget, by definition. */
|
|
160
|
-
notify(method: string, params: unknown): void {
|
|
161
|
-
this.write({ jsonrpc: "2.0", method, params });
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/** Send a request and resolve with its `result`, or reject with its `error`. */
|
|
165
|
-
request(method: string, params: unknown): Promise<unknown> {
|
|
166
|
-
const id = this.nextId++;
|
|
167
|
-
return new Promise((resolve, reject) => {
|
|
168
|
-
this.pending.set(id, { resolve, reject });
|
|
169
|
-
this.write({ jsonrpc: "2.0", id, method, params });
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/** Stop serving and fail every outbound request still waiting. */
|
|
174
|
-
close(): void {
|
|
175
|
-
this.transport.close();
|
|
176
|
-
this.shutdown();
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
private shutdown(): void {
|
|
180
|
-
if (this.closed) return;
|
|
181
|
-
this.closed = true;
|
|
182
|
-
for (const [, waiter] of this.pending) {
|
|
183
|
-
waiter.reject(new Error("the connection closed before the request was answered"));
|
|
184
|
-
}
|
|
185
|
-
this.pending.clear();
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
private write(message: JsonRpcMessage): void {
|
|
189
|
-
if (this.closed) return;
|
|
190
|
-
this.transport.send(JSON.stringify(message));
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
private async receive(line: string): Promise<void> {
|
|
194
|
-
let message: JsonRpcMessage;
|
|
195
|
-
try {
|
|
196
|
-
message = JSON.parse(line) as JsonRpcMessage;
|
|
197
|
-
} catch {
|
|
198
|
-
this.write({
|
|
199
|
-
jsonrpc: "2.0",
|
|
200
|
-
id: null,
|
|
201
|
-
error: { code: -32700, message: "the line was not JSON" },
|
|
202
|
-
});
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// A response to something we asked.
|
|
207
|
-
if (message.method === undefined && message.id !== undefined && message.id !== null) {
|
|
208
|
-
const waiter = this.pending.get(Number(message.id));
|
|
209
|
-
if (!waiter) return;
|
|
210
|
-
this.pending.delete(Number(message.id));
|
|
211
|
-
if (message.error) waiter.reject(new JsonRpcError(message.error.code, message.error.message));
|
|
212
|
-
else waiter.resolve(message.result);
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
if (!message.method) return;
|
|
217
|
-
|
|
218
|
-
// A notification: no id, no reply.
|
|
219
|
-
if (message.id === undefined || message.id === null) {
|
|
220
|
-
try {
|
|
221
|
-
this.handler.notify(message.method, message.params);
|
|
222
|
-
} catch {
|
|
223
|
-
// JSON-RPC has nowhere to put this, and a stdio agent's stdout is
|
|
224
|
-
// protocol only.
|
|
225
|
-
}
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const id = message.id;
|
|
230
|
-
try {
|
|
231
|
-
const result = await this.handler.request(message.method, message.params);
|
|
232
|
-
this.write({ jsonrpc: "2.0", id, result: result ?? null });
|
|
233
|
-
} catch (err) {
|
|
234
|
-
const code = err instanceof JsonRpcError ? err.code : -32603;
|
|
235
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
236
|
-
this.write({ jsonrpc: "2.0", id, error: { code, message: msg } });
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
}
|