@llm4ts/shell 3.0.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Cli.d.ts +1 -1
- package/dist/Cli.d.ts.map +1 -1
- package/dist/Cli.js +8 -1
- package/dist/Cli.js.map +1 -1
- package/dist/OperatorTools.d.ts +2 -0
- package/dist/OperatorTools.d.ts.map +1 -1
- package/dist/ToolCommand.d.ts +26 -0
- package/dist/ToolCommand.d.ts.map +1 -0
- package/dist/ToolCommand.js +207 -0
- package/dist/ToolCommand.js.map +1 -0
- package/flows/lib/epic-stories.js +6 -1
- package/kits/soap-ace/flows/lib/soap/Auth.js +4 -301
- package/kits/soap-ace/flows/lib/soap/Transport.js +4 -303
- package/kits/soap-ace/flows/lib/soap/Wsdl.js +31 -0
- package/kits/soap-ace/tools/soap-call.js +130 -0
- package/kits/soap-ace/tools/xml-validate.js +60 -0
- package/package.json +4 -4
- package/src/Cli.ts +7 -1
- package/src/ToolCommand.ts +349 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// Call one prepared sample request of a discovered SOAP service and report
|
|
2
|
+
// the outcome: `llm4ts tool soap-call --service <name> --operation <op>
|
|
3
|
+
// --request <sample>` (ADR 0048). Reads what soap-discover and soap-sample
|
|
4
|
+
// wrote under <project>/.llm4ts/soap/<service>/ — the catalog, the
|
|
5
|
+
// operations file, the auth profile, the sample request — and goes through
|
|
6
|
+
// the same call policy: an unclassified or mutating operation is refused
|
|
7
|
+
// (there is no --allow-mutating here), the request is validated before it
|
|
8
|
+
// is sent, the exchange is masked and written beside the sample as always.
|
|
9
|
+
import { resolve } from "node:path";
|
|
10
|
+
import * as Effect from "effect/Effect";
|
|
11
|
+
import * as Schema from "effect/Schema";
|
|
12
|
+
import { nodeSecretSource } from "@llm4ts/flow/endpoint/Auth";
|
|
13
|
+
import { SoapTransport } from "@llm4ts/flow/endpoint/Transport";
|
|
14
|
+
import { ToolContext, ToolError, defineTool, report } from "@llm4ts/flow/tools/Tool";
|
|
15
|
+
import { makeNodeWorkspace } from "@llm4ts/runner/NodeWorkspace";
|
|
16
|
+
import { runToolMain } from "@llm4ts/runner/ToolMain";
|
|
17
|
+
import { callOperation } from "../flows/lib/soap/Call.js";
|
|
18
|
+
import { loadAuthProfile, loadCatalog, loadOperationsFile, profilePath } from "../flows/lib/soap/Discover.js";
|
|
19
|
+
import { makeDirectoryStubTransport } from "../flows/lib/soap/Stub.js";
|
|
20
|
+
import { makeTokenSource } from "../flows/lib/soap/Sts.js";
|
|
21
|
+
const name = "soap-call";
|
|
22
|
+
const Input = Schema.Struct({
|
|
23
|
+
service: Schema.String,
|
|
24
|
+
operation: Schema.String,
|
|
25
|
+
request: Schema.String,
|
|
26
|
+
env: Schema.optionalKey(Schema.String)
|
|
27
|
+
});
|
|
28
|
+
export const soapCall = defineTool({
|
|
29
|
+
name,
|
|
30
|
+
purpose: "Call one prepared sample request of a discovered SOAP service (read operations only) and report the status, fault and schema findings",
|
|
31
|
+
args: {
|
|
32
|
+
service: {
|
|
33
|
+
description: "Service name under .llm4ts/soap/ (from soap-discover)",
|
|
34
|
+
kind: "string",
|
|
35
|
+
required: true
|
|
36
|
+
},
|
|
37
|
+
operation: {
|
|
38
|
+
description: "Operation name in the service catalog",
|
|
39
|
+
kind: "string",
|
|
40
|
+
required: true
|
|
41
|
+
},
|
|
42
|
+
request: {
|
|
43
|
+
description: "Sample request name under samples/<operation>/ (from soap-sample init)",
|
|
44
|
+
kind: "string",
|
|
45
|
+
required: true
|
|
46
|
+
},
|
|
47
|
+
env: {
|
|
48
|
+
description: "Environment whose auth profile to use (dev, test, uat); default auth.json",
|
|
49
|
+
kind: "string"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
input: Input,
|
|
53
|
+
run: (input) => Effect.gen(function* () {
|
|
54
|
+
const context = yield* ToolContext;
|
|
55
|
+
const live = yield* SoapTransport;
|
|
56
|
+
const fail = (message) => new ToolError({ tool: name, message });
|
|
57
|
+
const workspace = yield* makeNodeWorkspace(context.projectRoot).pipe(Effect.mapError((error) => fail(error.message)));
|
|
58
|
+
const catalog = yield* loadCatalog(workspace, input.service).pipe(Effect.mapError(() => fail(`no catalog for service '${input.service}'; run soap-discover first`)));
|
|
59
|
+
const operations = yield* loadOperationsFile(workspace, input.service).pipe(Effect.mapError((error) => fail(error.message)));
|
|
60
|
+
const profile = yield* loadAuthProfile(workspace, input.service, input.env).pipe(Effect.mapError((error) => fail(error.message)));
|
|
61
|
+
if (profile === undefined) {
|
|
62
|
+
return yield* fail(`no profile at ${profilePath(input.service, input.env)}`);
|
|
63
|
+
}
|
|
64
|
+
// `LLM4TS_SOAP_STUB=<dir>` answers from recorded responses, as soap-sample does.
|
|
65
|
+
const stub = context.environment.LLM4TS_SOAP_STUB?.trim();
|
|
66
|
+
const transport = stub === undefined || stub === ""
|
|
67
|
+
? live
|
|
68
|
+
: makeDirectoryStubTransport(resolve(context.projectRoot, stub), catalog);
|
|
69
|
+
const secrets = nodeSecretSource(context.environment);
|
|
70
|
+
const token = profile.sts === undefined
|
|
71
|
+
? undefined
|
|
72
|
+
: yield* makeTokenSource({
|
|
73
|
+
workspace,
|
|
74
|
+
service: input.service,
|
|
75
|
+
config: profile.sts,
|
|
76
|
+
secrets,
|
|
77
|
+
transport
|
|
78
|
+
});
|
|
79
|
+
const outcome = yield* callOperation({
|
|
80
|
+
workspace,
|
|
81
|
+
catalog,
|
|
82
|
+
service: input.service,
|
|
83
|
+
operation: input.operation,
|
|
84
|
+
name: input.request,
|
|
85
|
+
profile,
|
|
86
|
+
operations,
|
|
87
|
+
secrets,
|
|
88
|
+
transport,
|
|
89
|
+
...(token === undefined ? {} : { token }),
|
|
90
|
+
allowMutating: undefined,
|
|
91
|
+
confirm: () => Effect.succeed(false)
|
|
92
|
+
}).pipe(Effect.result);
|
|
93
|
+
if (outcome._tag === "Failure") {
|
|
94
|
+
const failure = outcome.failure;
|
|
95
|
+
if (failure._tag === "RequestInvalid") {
|
|
96
|
+
return report(name, failure.issues.map((issue) => ({
|
|
97
|
+
path: `request ${issue.path}`,
|
|
98
|
+
detail: issue.detail
|
|
99
|
+
})), "");
|
|
100
|
+
}
|
|
101
|
+
if (failure._tag === "CallRefused") {
|
|
102
|
+
return report(name, [{ path: input.operation, detail: `refused: ${failure.detail}` }], "");
|
|
103
|
+
}
|
|
104
|
+
return yield* fail(failure.message);
|
|
105
|
+
}
|
|
106
|
+
const { exchange, path } = outcome.success;
|
|
107
|
+
const issues = [
|
|
108
|
+
...(exchange.fault === undefined
|
|
109
|
+
? []
|
|
110
|
+
: [
|
|
111
|
+
{
|
|
112
|
+
path: input.operation,
|
|
113
|
+
detail: `fault ${exchange.fault.code}: ${exchange.fault.reason}`
|
|
114
|
+
}
|
|
115
|
+
]),
|
|
116
|
+
...exchange.responseIssues.map((issue) => ({
|
|
117
|
+
path: `response ${issue.path}`,
|
|
118
|
+
detail: issue.detail
|
|
119
|
+
}))
|
|
120
|
+
];
|
|
121
|
+
const status = exchange.response?.status;
|
|
122
|
+
return report(name, issues, `${input.operation}/${input.request} answered ${status ?? "?"} in ${exchange.response?.elapsedMs ?? 0} ms; exchange at ${path}`, {
|
|
123
|
+
status: status ?? null,
|
|
124
|
+
elapsedMs: exchange.response?.elapsedMs ?? null,
|
|
125
|
+
exchange: path,
|
|
126
|
+
masked: exchange.masking.entries.reduce((sum, entry) => sum + entry.count, 0)
|
|
127
|
+
});
|
|
128
|
+
})
|
|
129
|
+
});
|
|
130
|
+
runToolMain(soapCall);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Validate an XML document against an XSD: `llm4ts tool xml-validate --schema
|
|
2
|
+
// <xsd> --document <xml>` (ADR 0048). Imports and includes of the schema are
|
|
3
|
+
// followed from its own directory; the document's root element must be a
|
|
4
|
+
// global element the schema declares (or `--root <local-name>` names one).
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as Schema from "effect/Schema";
|
|
7
|
+
import { defineTool, readToolFile, report, ToolError } from "@llm4ts/flow/tools/Tool";
|
|
8
|
+
import { runToolMain } from "@llm4ts/runner/ToolMain";
|
|
9
|
+
import { splitClark } from "../flows/lib/soap/Catalog.js";
|
|
10
|
+
import { instanceFromXml, validateInstance } from "../flows/lib/soap/Instance.js";
|
|
11
|
+
import { parseXml } from "../flows/lib/soap/Xml.js";
|
|
12
|
+
import { DocumentLoader, makeFileDocumentLoader, readSchemaCatalog } from "../flows/lib/soap/Wsdl.js";
|
|
13
|
+
const name = "xml-validate";
|
|
14
|
+
const Input = Schema.Struct({
|
|
15
|
+
schema: Schema.String,
|
|
16
|
+
document: Schema.String,
|
|
17
|
+
root: Schema.optionalKey(Schema.String)
|
|
18
|
+
});
|
|
19
|
+
export const xmlValidate = defineTool({
|
|
20
|
+
name,
|
|
21
|
+
purpose: "Validate an XML document against an XSD schema (imports and includes followed)",
|
|
22
|
+
args: {
|
|
23
|
+
schema: { description: "Path of the XSD file", kind: "path", required: true },
|
|
24
|
+
document: { description: "Path of the XML document to check", kind: "path", required: true },
|
|
25
|
+
root: {
|
|
26
|
+
description: "Local name of the global element to validate against; default: the document's root",
|
|
27
|
+
kind: "string"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
input: Input,
|
|
31
|
+
run: (input) => Effect.gen(function* () {
|
|
32
|
+
const catalog = yield* readSchemaCatalog(input.schema).pipe(Effect.provideService(DocumentLoader, makeFileDocumentLoader()), Effect.mapError((error) => new ToolError({
|
|
33
|
+
tool: name,
|
|
34
|
+
message: `cannot read schema ${input.schema}: ${error.message}`
|
|
35
|
+
})));
|
|
36
|
+
const text = yield* readToolFile(name, input.document);
|
|
37
|
+
const xml = yield* parseXml(text, { source: input.document }).pipe(Effect.mapError((error) => new ToolError({ tool: name, message: `${input.document}: ${error.message}` })));
|
|
38
|
+
const wanted = input.root ?? xml.name.local;
|
|
39
|
+
const element = catalog.elements.find((candidate) => splitClark(candidate.name).local === wanted);
|
|
40
|
+
if (element === undefined) {
|
|
41
|
+
const known = catalog.elements
|
|
42
|
+
.map((candidate) => splitClark(candidate.name).local)
|
|
43
|
+
.sort()
|
|
44
|
+
.slice(0, 20)
|
|
45
|
+
.join(", ");
|
|
46
|
+
return yield* new ToolError({
|
|
47
|
+
tool: name,
|
|
48
|
+
message: `${input.schema} declares no global element '${wanted}'; known: ${known}`
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
const read = instanceFromXml(catalog, element, xml);
|
|
52
|
+
const issues = [...read.issues, ...validateInstance(catalog, element, read.value)];
|
|
53
|
+
const unresolved = catalog.openQuestions.map((question) => ({
|
|
54
|
+
path: "(schema)",
|
|
55
|
+
detail: `${question.code}: ${question.detail}`
|
|
56
|
+
}));
|
|
57
|
+
return report(name, [...unresolved, ...issues], `${input.document} is valid against ${input.schema}`);
|
|
58
|
+
})
|
|
59
|
+
});
|
|
60
|
+
runToolMain(xmlValidate);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@llm4ts/shell",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "Interactive shell and CLI for llm4ts: flow discovery, run-a-flow, and view",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -61,9 +61,9 @@
|
|
|
61
61
|
"dependencies": {
|
|
62
62
|
"@effect/platform-node": "4.0.0",
|
|
63
63
|
"@effect/platform-node-shared": "4.0.0",
|
|
64
|
-
"@llm4ts/core": "3.
|
|
65
|
-
"@llm4ts/
|
|
66
|
-
"@llm4ts/
|
|
64
|
+
"@llm4ts/core": "3.2.0",
|
|
65
|
+
"@llm4ts/flow": "3.2.0",
|
|
66
|
+
"@llm4ts/runner": "3.2.0"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
69
|
"effect": "4.0.0"
|
package/src/Cli.ts
CHANGED
|
@@ -56,6 +56,7 @@ import { mainMenu } from "./Menu.ts"
|
|
|
56
56
|
import { ModDir, refineSession } from "./Refine.ts"
|
|
57
57
|
import { packageVersion } from "./Package.ts"
|
|
58
58
|
import { ShellUsageError } from "./ShellErrors.ts"
|
|
59
|
+
import { runToolRun, toolCommand } from "./ToolCommand.ts"
|
|
59
60
|
|
|
60
61
|
export { ShellUsageError }
|
|
61
62
|
|
|
@@ -1075,6 +1076,7 @@ export const shellCommand = Command.make("llm4ts", {}, () =>
|
|
|
1075
1076
|
rosterCommand,
|
|
1076
1077
|
geminiBridgeCommand,
|
|
1077
1078
|
mcpCommand,
|
|
1079
|
+
toolCommand,
|
|
1078
1080
|
doctorCommand
|
|
1079
1081
|
])
|
|
1080
1082
|
)
|
|
@@ -1082,4 +1084,8 @@ export const shellCommand = Command.make("llm4ts", {}, () =>
|
|
|
1082
1084
|
export const runShellCommand = (
|
|
1083
1085
|
argv: ReadonlyArray<string>
|
|
1084
1086
|
): Effect.Effect<void, unknown, Command.Environment> =>
|
|
1085
|
-
|
|
1087
|
+
// `tool run <name> --flag value …` carries the tool's own options, which the
|
|
1088
|
+
// declared parser would reject; it is routed on the raw arguments (ADR 0048).
|
|
1089
|
+
argv[0] === "tool" && argv[1] === "run" && argv[2] !== undefined && !argv[2].startsWith("-")
|
|
1090
|
+
? runToolRun(argv[2], argv.slice(3))
|
|
1091
|
+
: Command.runWith(shellCommand, { version: packageVersion })(argv)
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { spawn } from "node:child_process"
|
|
2
|
+
import { homedir } from "node:os"
|
|
3
|
+
import { fileURLToPath } from "node:url"
|
|
4
|
+
import * as Console from "effect/Console"
|
|
5
|
+
import * as Effect from "effect/Effect"
|
|
6
|
+
import * as Schema from "effect/Schema"
|
|
7
|
+
import { Argument, Command, Flag } from "effect/cli"
|
|
8
|
+
import type { ToolPreset } from "@llm4ts/flow/Pack"
|
|
9
|
+
import type { ToolArgsError } from "@llm4ts/flow/tools/Tool"
|
|
10
|
+
import {
|
|
11
|
+
ToolError,
|
|
12
|
+
ToolReport,
|
|
13
|
+
ToolSpec,
|
|
14
|
+
capReport,
|
|
15
|
+
parseToolArgs,
|
|
16
|
+
renderReport,
|
|
17
|
+
renderSpec,
|
|
18
|
+
specOf
|
|
19
|
+
} from "@llm4ts/flow/tools/Tool"
|
|
20
|
+
import {
|
|
21
|
+
builtinKitsDir,
|
|
22
|
+
discoverKits,
|
|
23
|
+
kitTierPaths,
|
|
24
|
+
type DiscoveredKit,
|
|
25
|
+
type KitTierPaths
|
|
26
|
+
} from "@llm4ts/runner/Kits"
|
|
27
|
+
import * as Layer from "effect/Layer"
|
|
28
|
+
import { SoapTransport, makeNodeSoapTransport } from "@llm4ts/flow/endpoint/Transport"
|
|
29
|
+
import { ToolContext } from "@llm4ts/flow/tools/Tool"
|
|
30
|
+
import { NodePlainFileStoreLive } from "@llm4ts/runner/NodePlainFileStore"
|
|
31
|
+
import { openPack } from "@llm4ts/runner/Packs"
|
|
32
|
+
import { ChildToolOutput } from "@llm4ts/runner/ToolMain"
|
|
33
|
+
import {
|
|
34
|
+
PresetError,
|
|
35
|
+
builtinSpecs,
|
|
36
|
+
catalogEntries,
|
|
37
|
+
catalogJson,
|
|
38
|
+
findEntry,
|
|
39
|
+
projectRootOf,
|
|
40
|
+
renderCatalog,
|
|
41
|
+
reportExitCode,
|
|
42
|
+
resolvePreset,
|
|
43
|
+
type CatalogEntry,
|
|
44
|
+
type ToolCatalog
|
|
45
|
+
} from "@llm4ts/runner/Tools"
|
|
46
|
+
import { flowNodeArgs } from "./FlowProcess.ts"
|
|
47
|
+
import { ShellUsageError } from "./ShellErrors.ts"
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* `llm4ts tool list | describe <name> | run <name> [args]` (ADR 0048): the
|
|
51
|
+
* one subcommand a coding agent may run inside a task. The project is found
|
|
52
|
+
* from the worktree through git, never through `LLM4TS_*` variables.
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
export class ToolChildError extends Schema.TaggedError<ToolChildError>()("ToolChildError", {
|
|
56
|
+
tool: Schema.String,
|
|
57
|
+
message: Schema.String
|
|
58
|
+
}) {}
|
|
59
|
+
|
|
60
|
+
// The same directory `Cli.ts` computes; repeated here so this module does not
|
|
61
|
+
// import the command module that imports it.
|
|
62
|
+
const builtinFlowsDir = (): string => fileURLToPath(new URL("../flows", import.meta.url))
|
|
63
|
+
|
|
64
|
+
const kitTiersFor = (root: string): KitTierPaths => {
|
|
65
|
+
const builtin = builtinKitsDir(builtinFlowsDir())
|
|
66
|
+
return kitTierPaths({
|
|
67
|
+
cwd: root,
|
|
68
|
+
homeDir: homedir(),
|
|
69
|
+
...(builtin === undefined ? {} : { builtinDir: builtin })
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const loadCatalog = (packReference: string | undefined): Effect.Effect<ToolCatalog, never> =>
|
|
74
|
+
Effect.gen(function* () {
|
|
75
|
+
const root = projectRootOf(process.cwd())
|
|
76
|
+
const tiers = kitTiersFor(root)
|
|
77
|
+
const kits = discoverKits(tiers)
|
|
78
|
+
const entries = catalogEntries(kits)
|
|
79
|
+
const presets = yield* presetsOf(kits, root, packReference, tiers)
|
|
80
|
+
return { entries, presets }
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
const presetsOf = (
|
|
84
|
+
kits: ReadonlyArray<DiscoveredKit>,
|
|
85
|
+
root: string,
|
|
86
|
+
packReference: string | undefined,
|
|
87
|
+
kitTiers: KitTierPaths
|
|
88
|
+
): Effect.Effect<ReadonlyArray<ToolPreset & { readonly pack: string }>, never> =>
|
|
89
|
+
Effect.gen(function* () {
|
|
90
|
+
const references =
|
|
91
|
+
packReference !== undefined
|
|
92
|
+
? [packReference]
|
|
93
|
+
: kits.flatMap((kit) => kit.packs.map((pack) => `${kit.name}/${pack}`))
|
|
94
|
+
const presets: Array<ToolPreset & { readonly pack: string }> = []
|
|
95
|
+
for (const reference of references) {
|
|
96
|
+
const opened = yield* openPack({
|
|
97
|
+
environment: { LLM4TS_PACK: reference },
|
|
98
|
+
launchDir: root,
|
|
99
|
+
flowDir: builtinFlowsDir(),
|
|
100
|
+
kits: kitTiers
|
|
101
|
+
}).pipe(Effect.option)
|
|
102
|
+
if (opened._tag === "Some") {
|
|
103
|
+
for (const preset of opened.value.pack.tools ?? []) {
|
|
104
|
+
presets.push({ ...preset, pack: opened.value.pack.name })
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return presets
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
/** A kit tool runs as a Node child speaking the `runToolMain` protocol. */
|
|
112
|
+
const runChild = (
|
|
113
|
+
file: string,
|
|
114
|
+
args: ReadonlyArray<string>,
|
|
115
|
+
stdin: string | undefined
|
|
116
|
+
): Effect.Effect<
|
|
117
|
+
{ readonly stdout: string; readonly stderr: string; readonly code: number },
|
|
118
|
+
never
|
|
119
|
+
> =>
|
|
120
|
+
Effect.callback((resume) => {
|
|
121
|
+
const child = spawn(process.execPath, [...flowNodeArgs(file), file, ...args], {
|
|
122
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
123
|
+
env: { ...process.env }
|
|
124
|
+
})
|
|
125
|
+
let stdout = ""
|
|
126
|
+
let stderr = ""
|
|
127
|
+
child.stdout.setEncoding("utf8")
|
|
128
|
+
child.stderr.setEncoding("utf8")
|
|
129
|
+
child.stdout.on("data", (chunk: string) => (stdout += chunk))
|
|
130
|
+
child.stderr.on("data", (chunk: string) => (stderr += chunk))
|
|
131
|
+
child.on("error", (error) => resume(Effect.succeed({ stdout, stderr: error.message, code: 2 })))
|
|
132
|
+
child.on("close", (code) => resume(Effect.succeed({ stdout, stderr, code: code ?? 2 })))
|
|
133
|
+
if (stdin !== undefined) {
|
|
134
|
+
child.stdin.write(stdin)
|
|
135
|
+
}
|
|
136
|
+
child.stdin.end()
|
|
137
|
+
return Effect.sync(() => child.kill())
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
const decodeChildJson = <A, E, RD>(
|
|
141
|
+
schema: Schema.Codec<A, E, RD>,
|
|
142
|
+
tool: string,
|
|
143
|
+
output: { readonly stdout: string; readonly stderr: string }
|
|
144
|
+
): Effect.Effect<A, ToolChildError, RD> =>
|
|
145
|
+
Schema.decodeUnknownEffect(Schema.fromJsonString(schema))(output.stdout.trim()).pipe(
|
|
146
|
+
Effect.mapError(
|
|
147
|
+
() =>
|
|
148
|
+
new ToolChildError({
|
|
149
|
+
tool,
|
|
150
|
+
message: `tool printed no report${output.stderr.trim().length === 0 ? "" : `: ${output.stderr.trim()}`}`
|
|
151
|
+
})
|
|
152
|
+
)
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
const specFor = (entry: CatalogEntry): Effect.Effect<ToolSpec, ToolChildError> =>
|
|
156
|
+
entry._tag === "builtin"
|
|
157
|
+
? Effect.succeed(specOf(entry.tool))
|
|
158
|
+
: Effect.flatMap(runChild(entry.file, ["--describe"], undefined), (output) =>
|
|
159
|
+
decodeChildJson(ToolSpec, entry.name, output)
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
const toolLayer = (projectRoot: string) =>
|
|
163
|
+
Layer.mergeAll(
|
|
164
|
+
NodePlainFileStoreLive,
|
|
165
|
+
Layer.succeed(ToolContext)({ projectRoot, environment: process.env }),
|
|
166
|
+
Layer.succeed(SoapTransport)(makeNodeSoapTransport())
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
const execute = (
|
|
170
|
+
entry: CatalogEntry,
|
|
171
|
+
values: Record<string, unknown>,
|
|
172
|
+
projectRoot: string
|
|
173
|
+
): Effect.Effect<ToolReport, ToolError | ToolArgsError | ToolChildError> =>
|
|
174
|
+
entry._tag === "builtin"
|
|
175
|
+
? entry.tool.execute(values).pipe(Effect.provide(toolLayer(projectRoot)))
|
|
176
|
+
: Effect.gen(function* () {
|
|
177
|
+
const output = yield* runChild(
|
|
178
|
+
entry.file,
|
|
179
|
+
["--project", projectRoot],
|
|
180
|
+
JSON.stringify(values)
|
|
181
|
+
)
|
|
182
|
+
const decoded = yield* decodeChildJson(ChildToolOutput, entry.name, output)
|
|
183
|
+
return "error" in decoded
|
|
184
|
+
? yield* new ToolError({ tool: entry.name, message: decoded.error })
|
|
185
|
+
: decoded
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
const specsOf = (
|
|
189
|
+
catalog: ToolCatalog,
|
|
190
|
+
describeKits: boolean
|
|
191
|
+
): Effect.Effect<ReadonlyMap<string, ToolSpec>, never> =>
|
|
192
|
+
Effect.gen(function* () {
|
|
193
|
+
const specs = new Map(builtinSpecs())
|
|
194
|
+
if (describeKits) {
|
|
195
|
+
for (const entry of catalog.entries) {
|
|
196
|
+
if (entry._tag === "kit") {
|
|
197
|
+
const spec = yield* specFor(entry).pipe(Effect.option)
|
|
198
|
+
if (spec._tag === "Some") {
|
|
199
|
+
specs.set(entry.name, spec.value)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return specs
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
const packFlag = Flag.String("pack").pipe(
|
|
208
|
+
Flag.optional,
|
|
209
|
+
Flag.withDescription(
|
|
210
|
+
"Only this pack's presets (kit/pack, a name, or a path); default: every pack in reach"
|
|
211
|
+
)
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
const listCommand = Command.make(
|
|
215
|
+
"list",
|
|
216
|
+
{
|
|
217
|
+
json: Flag.Boolean("json").pipe(
|
|
218
|
+
Flag.withDefault(false),
|
|
219
|
+
Flag.withDescription("Emit the catalog as JSON")
|
|
220
|
+
),
|
|
221
|
+
pack: packFlag
|
|
222
|
+
},
|
|
223
|
+
(config) =>
|
|
224
|
+
Effect.gen(function* () {
|
|
225
|
+
const catalog = yield* loadCatalog(
|
|
226
|
+
config.pack._tag === "Some" ? config.pack.value : undefined
|
|
227
|
+
)
|
|
228
|
+
const specs = yield* specsOf(catalog, true)
|
|
229
|
+
yield* Console.log(
|
|
230
|
+
config.json
|
|
231
|
+
? JSON.stringify(catalogJson(catalog, specs), null, 2)
|
|
232
|
+
: renderCatalog(catalog, specs)
|
|
233
|
+
)
|
|
234
|
+
})
|
|
235
|
+
).pipe(Command.withDescription("List the tools and presets this project can run"))
|
|
236
|
+
|
|
237
|
+
const describeCommand = Command.make(
|
|
238
|
+
"describe",
|
|
239
|
+
{
|
|
240
|
+
name: Argument.String("name").pipe(Argument.withDescription("A tool or preset name")),
|
|
241
|
+
pack: packFlag
|
|
242
|
+
},
|
|
243
|
+
(config) =>
|
|
244
|
+
Effect.gen(function* () {
|
|
245
|
+
const catalog = yield* loadCatalog(
|
|
246
|
+
config.pack._tag === "Some" ? config.pack.value : undefined
|
|
247
|
+
)
|
|
248
|
+
const resolved = yield* resolvePreset(catalog, config.name, [])
|
|
249
|
+
const entry = resolved?.entry ?? findEntry(catalog.entries, config.name)
|
|
250
|
+
if (entry === undefined) {
|
|
251
|
+
return yield* new ShellUsageError({
|
|
252
|
+
message: `no tool or preset named '${config.name}'; run \`llm4ts tool list\``
|
|
253
|
+
})
|
|
254
|
+
}
|
|
255
|
+
const spec = yield* specFor(entry)
|
|
256
|
+
const header =
|
|
257
|
+
resolved === undefined
|
|
258
|
+
? []
|
|
259
|
+
: [
|
|
260
|
+
`${config.name}: preset of pack ${resolved.preset.pack} — runs \`${resolved.preset.command.join(" ")}\``,
|
|
261
|
+
""
|
|
262
|
+
]
|
|
263
|
+
yield* Console.log([...header, renderSpec(spec)].join("\n"))
|
|
264
|
+
})
|
|
265
|
+
).pipe(Command.withDescription("Show a tool's purpose and arguments"))
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* `tool run <name> …` takes the tool's own `--flag value` pairs, which a
|
|
269
|
+
* declared command would reject as unknown options, so `runShellCommand`
|
|
270
|
+
* routes it here before `effect/cli` parses anything. `--all`, `--text` and
|
|
271
|
+
* `--pack <ref>` are picked out; everything else goes to the tool.
|
|
272
|
+
*/
|
|
273
|
+
export const runToolRun = (
|
|
274
|
+
name: string,
|
|
275
|
+
argv: ReadonlyArray<string>
|
|
276
|
+
): Effect.Effect<void, ShellUsageError> =>
|
|
277
|
+
Effect.gen(function* () {
|
|
278
|
+
let all = false
|
|
279
|
+
let text = false
|
|
280
|
+
let pack: string | undefined
|
|
281
|
+
const rest: Array<string> = []
|
|
282
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
283
|
+
const token = argv[index] ?? ""
|
|
284
|
+
if (token === "--all") all = true
|
|
285
|
+
else if (token === "--text") text = true
|
|
286
|
+
else if (token === "--pack" && index + 1 < argv.length) {
|
|
287
|
+
pack = argv[index + 1]
|
|
288
|
+
index += 1
|
|
289
|
+
} else if (token.startsWith("--pack=")) pack = token.slice("--pack=".length)
|
|
290
|
+
else rest.push(token)
|
|
291
|
+
}
|
|
292
|
+
const projectRoot = projectRootOf(process.cwd())
|
|
293
|
+
const catalog = yield* loadCatalog(pack)
|
|
294
|
+
const resolved = yield* resolvePreset(catalog, name, rest)
|
|
295
|
+
const entry = resolved?.entry ?? findEntry(catalog.entries, name)
|
|
296
|
+
if (entry === undefined) {
|
|
297
|
+
return yield* new ShellUsageError({
|
|
298
|
+
message: `no tool or preset named '${name}'; run \`llm4ts tool list\``
|
|
299
|
+
})
|
|
300
|
+
}
|
|
301
|
+
const spec = yield* specFor(entry)
|
|
302
|
+
const values = yield* parseToolArgs(spec, resolved?.argv ?? rest)
|
|
303
|
+
const report = capReport(yield* execute(entry, values, projectRoot), all)
|
|
304
|
+
yield* Console.log(
|
|
305
|
+
text ? renderReport(report) : JSON.stringify(Schema.encodeSync(ToolReport)(report))
|
|
306
|
+
)
|
|
307
|
+
process.exitCode = reportExitCode(report)
|
|
308
|
+
}).pipe(
|
|
309
|
+
Effect.catchTags({
|
|
310
|
+
ToolArgsError: (error) => Effect.fail(new ShellUsageError({ message: error.message })),
|
|
311
|
+
PresetError: (error) =>
|
|
312
|
+
Effect.fail(new ShellUsageError({ message: `${error.preset}: ${error.message}` })),
|
|
313
|
+
ToolError: (error) =>
|
|
314
|
+
Effect.fail(new ShellUsageError({ message: `${error.tool}: ${error.message}` })),
|
|
315
|
+
ToolChildError: (error) =>
|
|
316
|
+
Effect.fail(new ShellUsageError({ message: `${error.tool}: ${error.message}` }))
|
|
317
|
+
})
|
|
318
|
+
)
|
|
319
|
+
|
|
320
|
+
// Declared for `--help` only: `runShellCommand` hands `tool run` to
|
|
321
|
+
// `runToolRun` with the raw arguments before this parser sees them.
|
|
322
|
+
const runCommand = Command.make(
|
|
323
|
+
"run",
|
|
324
|
+
{
|
|
325
|
+
name: Argument.String("name").pipe(Argument.withDescription("A tool or preset name")),
|
|
326
|
+
args: Argument.String("args").pipe(
|
|
327
|
+
Argument.variadic(),
|
|
328
|
+
Argument.withDescription(
|
|
329
|
+
"The tool's --flag value pairs (a preset's fixed ones come first); --all prints every issue, --text plain text, --pack <ref> one pack's presets"
|
|
330
|
+
)
|
|
331
|
+
)
|
|
332
|
+
},
|
|
333
|
+
(config) => runToolRun(config.name, config.args)
|
|
334
|
+
).pipe(Command.withDescription("Run a tool or preset; JSON report on stdout, exit 1 on issues"))
|
|
335
|
+
|
|
336
|
+
export const toolCommand = Command.make("tool", {}, () =>
|
|
337
|
+
Effect.fail(
|
|
338
|
+
new ShellUsageError({
|
|
339
|
+
message: "try `llm4ts tool list`, `llm4ts tool describe <name>` or `llm4ts tool run <name>`"
|
|
340
|
+
})
|
|
341
|
+
)
|
|
342
|
+
).pipe(
|
|
343
|
+
Command.withDescription(
|
|
344
|
+
"Run the project's tools — validate, compare, check — instead of improvising a script (ADR 0048)"
|
|
345
|
+
),
|
|
346
|
+
Command.withSubcommands([listCommand, describeCommand, runCommand])
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
export { PresetError }
|