@llm4ts/shell 3.2.5 → 3.3.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.
Files changed (48) hide show
  1. package/dist/Cli.d.ts +11 -3
  2. package/dist/Cli.d.ts.map +1 -1
  3. package/dist/Cli.js +125 -38
  4. package/dist/Cli.js.map +1 -1
  5. package/dist/Config.d.ts +238 -0
  6. package/dist/Config.d.ts.map +1 -0
  7. package/dist/Config.js +594 -0
  8. package/dist/Config.js.map +1 -0
  9. package/dist/ConfigCommand.d.ts +180 -0
  10. package/dist/ConfigCommand.d.ts.map +1 -0
  11. package/dist/ConfigCommand.js +903 -0
  12. package/dist/ConfigCommand.js.map +1 -0
  13. package/dist/ConfigEdit.d.ts +79 -0
  14. package/dist/ConfigEdit.d.ts.map +1 -0
  15. package/dist/ConfigEdit.js +110 -0
  16. package/dist/ConfigEdit.js.map +1 -0
  17. package/dist/ConfigTrust.d.ts +45 -0
  18. package/dist/ConfigTrust.d.ts.map +1 -0
  19. package/dist/ConfigTrust.js +72 -0
  20. package/dist/ConfigTrust.js.map +1 -0
  21. package/dist/FlowLaunch.d.ts +2 -3
  22. package/dist/FlowLaunch.d.ts.map +1 -1
  23. package/dist/FlowLaunch.js +1 -5
  24. package/dist/FlowLaunch.js.map +1 -1
  25. package/dist/FlowProcess.d.ts +3 -1
  26. package/dist/FlowProcess.d.ts.map +1 -1
  27. package/dist/FlowProcess.js.map +1 -1
  28. package/dist/Menu.d.ts +1 -1
  29. package/dist/Menu.d.ts.map +1 -1
  30. package/dist/Menu.js +33 -6
  31. package/dist/Menu.js.map +1 -1
  32. package/dist/RunSupervisor.d.ts +4 -3
  33. package/dist/RunSupervisor.d.ts.map +1 -1
  34. package/dist/RunSupervisor.js +26 -13
  35. package/dist/RunSupervisor.js.map +1 -1
  36. package/kits/j2ee-nextjs/README.md +2 -1
  37. package/kits/j2ee-nextjs/fixtures/demo-bank/RUNBOOK.md +32 -14
  38. package/kits/j2ee-nextjs/fixtures/demo-bank/reset-demo.mjs +3 -1
  39. package/package.json +8 -4
  40. package/src/Cli.ts +173 -45
  41. package/src/Config.ts +868 -0
  42. package/src/ConfigCommand.ts +1423 -0
  43. package/src/ConfigEdit.ts +180 -0
  44. package/src/ConfigTrust.ts +145 -0
  45. package/src/FlowLaunch.ts +3 -8
  46. package/src/FlowProcess.ts +3 -1
  47. package/src/Menu.ts +40 -7
  48. package/src/RunSupervisor.ts +26 -14
@@ -0,0 +1,180 @@
1
+ // The decisions of `llm4ts config edit`, as pure functions: which rows the
2
+ // picker lists, what a setting offers, how its value is asked, which file a
3
+ // value goes to. The prompts that ask live in ConfigCommand.ts. Only settings
4
+ // a file may hold are listed, so no row can carry a secret.
5
+ import * as Result from "effect/Result"
6
+ import {
7
+ closestSettingName,
8
+ groupTitles,
9
+ type SettingFacts,
10
+ type SettingName
11
+ } from "@llm4ts/flow/Settings"
12
+ import { storeRefusal, unknownText, type FileLayer, type Resolution } from "./Config.ts"
13
+
14
+ /** The settings a config file may hold, in registry order. */
15
+ export const editableFacts = (facts: ReadonlyArray<SettingFacts>): ReadonlyArray<SettingFacts> =>
16
+ facts.filter((fact) => fact.scope === "global" || fact.scope === "project")
17
+
18
+ export interface PickerRow {
19
+ readonly name: SettingName
20
+ /** Name, value and source in columns; the picker filters on it. */
21
+ readonly title: string
22
+ /** The group's title. */
23
+ readonly description: string
24
+ }
25
+
26
+ const valueWidth = 30
27
+
28
+ const clipped = (text: string): string =>
29
+ text.length > valueWidth ? `${text.slice(0, valueWidth - 1)}…` : text
30
+
31
+ /**
32
+ * The picker's rows: every setting some layer sets, then the rest, each part
33
+ * in registry order, so the groups stay together.
34
+ */
35
+ export const pickerRows = (resolution: Resolution): ReadonlyArray<PickerRow> => {
36
+ const editable = editableFacts(resolution.facts)
37
+ const titles = new Map<string, string>(groupTitles)
38
+ const nameWidth = Math.max(0, ...editable.map((fact) => fact.name.length))
39
+ const row = (fact: SettingFacts): PickerRow => {
40
+ const setting = resolution.settings.get(fact.name)
41
+ const value =
42
+ setting === undefined
43
+ ? `(default: ${fact.fallback.text})`
44
+ : setting.raw.length === 0
45
+ ? "(empty)"
46
+ : setting.raw
47
+ return {
48
+ name: fact.name,
49
+ title: `${fact.name.padEnd(nameWidth)} ${clipped(value).padEnd(valueWidth)} ${setting?.source.layer ?? "default"}`,
50
+ description: titles.get(fact.group) ?? fact.group
51
+ }
52
+ }
53
+ return [
54
+ ...editable.filter((fact) => resolution.settings.has(fact.name)),
55
+ ...editable.filter((fact) => !resolution.settings.has(fact.name))
56
+ ].map(row)
57
+ }
58
+
59
+ /** One line on the settings the picker leaves out and how each kind is set. */
60
+ export const unlistedHint =
61
+ "not listed: per-run settings (llm4ts run <flow> --set NAME=value) and secrets (export them in your shell)"
62
+
63
+ /**
64
+ * What `config edit [NAME]` opens: the picker (`undefined`) or one setting.
65
+ * Refuses a name that is not a setting, with the closest one, a setting no
66
+ * file may hold, with where it is set instead, and a session off a terminal.
67
+ */
68
+ export const editRequest = (
69
+ facts: ReadonlyArray<SettingFacts>,
70
+ request: { readonly name?: string; readonly interactive: boolean }
71
+ ): Result.Result<SettingName | undefined, string> => {
72
+ const fact =
73
+ request.name === undefined ? undefined : facts.find((known) => known.name === request.name)
74
+ if (request.name !== undefined && fact === undefined) {
75
+ return Result.fail(unknownText(request.name, closestSettingName(request.name)))
76
+ }
77
+ const refusal = fact === undefined ? undefined : storeRefusal(fact)
78
+ if (refusal !== undefined) {
79
+ return Result.fail(refusal)
80
+ }
81
+ return request.interactive
82
+ ? Result.succeed(fact?.name)
83
+ : Result.fail(
84
+ `config edit asks its questions on a terminal; here use llm4ts config set ${fact?.name ?? "NAME"}=value`
85
+ )
86
+ }
87
+
88
+ export type SettingAction =
89
+ | { readonly kind: "change" }
90
+ | { readonly kind: "remove"; readonly layer: FileLayer }
91
+ | { readonly kind: "back" }
92
+
93
+ export interface ActionChoice {
94
+ readonly title: string
95
+ readonly action: SettingAction
96
+ }
97
+
98
+ /** What an open setting offers; a removal only for a file that holds it. */
99
+ export const settingActions = (held: ReadonlyArray<FileLayer>): ReadonlyArray<ActionChoice> => [
100
+ { title: "change the value", action: { kind: "change" } },
101
+ ...held.map(
102
+ (layer): ActionChoice => ({
103
+ title: `remove it from the ${layer} file`,
104
+ action: { kind: "remove", layer }
105
+ })
106
+ ),
107
+ { title: "back", action: { kind: "back" } }
108
+ ]
109
+
110
+ export type ValuePrompt =
111
+ | { readonly kind: "toggle" }
112
+ | { readonly kind: "choices"; readonly choices: ReadonlyArray<string> }
113
+ | { readonly kind: "seat" }
114
+ | { readonly kind: "text" }
115
+
116
+ /** The settings that name a coding agent, asked from the agents `config init` lists. */
117
+ export const seatSettings: ReadonlyArray<SettingName> = ["LLM4TS_CODER", "LLM4TS_REASONER"]
118
+
119
+ /** How a setting's new value is asked. */
120
+ export const valuePrompt = (fact: SettingFacts): ValuePrompt =>
121
+ fact.toggle
122
+ ? { kind: "toggle" }
123
+ : fact.choices.length > 0
124
+ ? { kind: "choices", choices: fact.choices }
125
+ : seatSettings.includes(fact.name)
126
+ ? { kind: "seat" }
127
+ : { kind: "text" }
128
+
129
+ /**
130
+ * The files a new value may go to, the default first; one file means there
131
+ * is nothing to ask. A global setting edited inside a repository may be meant
132
+ * for that repository only, and defaults to the project file once that file
133
+ * holds it, where a global line would stay shadowed.
134
+ */
135
+ export const saveTargets = (
136
+ fact: SettingFacts,
137
+ where: {
138
+ readonly inRepository: boolean
139
+ /** The project file is not trusted, so nothing is written to it. */
140
+ readonly projectRefused: boolean
141
+ readonly held: ReadonlyArray<FileLayer>
142
+ }
143
+ ): ReadonlyArray<FileLayer> =>
144
+ fact.scope !== "global"
145
+ ? ["project"]
146
+ : !where.inRepository || where.projectRefused
147
+ ? ["global"]
148
+ : where.held.includes("project")
149
+ ? ["project", "global"]
150
+ : ["global", "project"]
151
+
152
+ export const targetTitles: Readonly<Record<FileLayer, string>> = {
153
+ global: "every repository (global)",
154
+ project: "this repository only"
155
+ }
156
+
157
+ /** The line shown when an export decides the value whatever a file says. */
158
+ export const environmentNote = (resolution: Resolution, name: SettingName): string | undefined =>
159
+ resolution.settings.get(name)?.source.layer === "environment"
160
+ ? `${name} is exported in this shell: the export keeps winning over whatever is saved here.`
161
+ : undefined
162
+
163
+ export interface SessionChange {
164
+ readonly name: SettingName
165
+ readonly layer: FileLayer
166
+ readonly kind: "set" | "removed"
167
+ }
168
+
169
+ /** One line on what the session left in the files; the last change to a line counts. */
170
+ export const sessionSummary = (changes: ReadonlyArray<SessionChange>): string => {
171
+ const last = new Map<string, SessionChange>()
172
+ for (const change of changes) {
173
+ last.set(`${change.layer} ${change.name}`, change)
174
+ }
175
+ return last.size === 0
176
+ ? "nothing changed"
177
+ : `changed: ${[...last.values()]
178
+ .map((change) => `${change.kind} ${change.name} (${change.layer})`)
179
+ .join(", ")}`
180
+ }
@@ -0,0 +1,145 @@
1
+ // The trust list: which project config files this user has accepted. A
2
+ // project file can set commands llm4ts runs, so it is used only when the
3
+ // user trusted exactly that content for exactly that repository. The list
4
+ // lives outside every repository; nothing a repository holds can add to it.
5
+ import { createHash } from "node:crypto"
6
+ import { homedir } from "node:os"
7
+ import { join, resolve } from "node:path"
8
+ import * as Effect from "effect/Effect"
9
+ import * as Result from "effect/Result"
10
+ import * as Schema from "effect/Schema"
11
+ import type { PersistenceError } from "@llm4ts/flow/FlowError"
12
+ import type { PlainFileStoreShape } from "@llm4ts/flow/Persistence"
13
+ import type { Environment } from "@llm4ts/flow/Settings"
14
+
15
+ /** A repository's absolute path to the SHA-256 of its trusted project file. */
16
+ export type TrustRecords = Readonly<Record<string, string>>
17
+
18
+ const TrustRecordsJson = Schema.fromJsonString(Schema.Record(Schema.String, Schema.String))
19
+
20
+ export type TrustStore =
21
+ | { readonly _tag: "Loaded"; readonly records: TrustRecords }
22
+ | { readonly _tag: "Unusable"; readonly reason: string }
23
+
24
+ /** How a project file stands against the list; `absent`: there is no file. */
25
+ export type ProjectTrust = "absent" | "trusted" | "untrusted" | "changed"
26
+
27
+ /** A project file that exists and is not used. */
28
+ export type Distrust = "untrusted" | "changed"
29
+
30
+ /** `${XDG_STATE_HOME:-~/.local/state}/llm4ts/config-trust.json`. */
31
+ export const configTrustPath = (environment: Environment): string =>
32
+ join(
33
+ environment.XDG_STATE_HOME ?? join(environment.HOME ?? homedir(), ".local", "state"),
34
+ "llm4ts",
35
+ "config-trust.json"
36
+ )
37
+
38
+ /** The SHA-256 of the text llm4ts parses, in hex. */
39
+ export const contentHash = (text: string): string =>
40
+ createHash("sha256").update(text, "utf8").digest("hex")
41
+
42
+ /** Pure: the list as stored; an absent file is an empty list. */
43
+ export const readTrustStore = (text: string | undefined): TrustStore => {
44
+ if (text === undefined) {
45
+ return { _tag: "Loaded", records: {} }
46
+ }
47
+ const decoded = Schema.decodeUnknownResult(TrustRecordsJson)(text)
48
+ return Result.isSuccess(decoded)
49
+ ? { _tag: "Loaded", records: decoded.success }
50
+ : { _tag: "Unusable", reason: "not a JSON object of repository paths and hashes" }
51
+ }
52
+
53
+ /**
54
+ * Pure: trusted only when the list holds this hash for this repository's
55
+ * absolute path. A list that cannot be used trusts nothing.
56
+ */
57
+ export const trustDecision = (
58
+ store: TrustStore,
59
+ repo: string,
60
+ hash: string
61
+ ): Distrust | "trusted" => {
62
+ if (store._tag === "Unusable") {
63
+ return "untrusted"
64
+ }
65
+ const key = resolve(repo)
66
+ const recorded = Object.hasOwn(store.records, key) ? store.records[key] : undefined
67
+ return recorded === undefined ? "untrusted" : recorded === hash ? "trusted" : "changed"
68
+ }
69
+
70
+ /** Never fails: a list that cannot be read is `Unusable`. */
71
+ export const loadTrustStore = (
72
+ files: PlainFileStoreShape,
73
+ path: string
74
+ ): Effect.Effect<TrustStore> =>
75
+ files.read(path).pipe(
76
+ Effect.map(readTrustStore),
77
+ Effect.catch((error) => Effect.succeed<TrustStore>({ _tag: "Unusable", reason: error.message }))
78
+ )
79
+
80
+ const save = (files: PlainFileStoreShape, path: string, records: TrustRecords) =>
81
+ files.writeAtomic(
82
+ path,
83
+ `${JSON.stringify(
84
+ Object.fromEntries(Object.entries(records).sort(([a], [b]) => a.localeCompare(b))),
85
+ undefined,
86
+ 2
87
+ )}\n`
88
+ )
89
+
90
+ /** Records `hash` for `repo`, keeping every other repository's record; an unusable list is replaced. */
91
+ export const recordTrust = Effect.fn("@llm4ts/shell/ConfigTrust.record")(function* (
92
+ files: PlainFileStoreShape,
93
+ path: string,
94
+ repo: string,
95
+ hash: string
96
+ ): Effect.fn.Return<void, PersistenceError> {
97
+ const store = yield* loadTrustStore(files, path)
98
+ yield* save(files, path, {
99
+ ...(store._tag === "Loaded" ? store.records : {}),
100
+ [resolve(repo)]: hash
101
+ })
102
+ })
103
+
104
+ /** Removes the record of `repo`; succeeds with whether there was one. */
105
+ export const forgetTrust = Effect.fn("@llm4ts/shell/ConfigTrust.forget")(function* (
106
+ files: PlainFileStoreShape,
107
+ path: string,
108
+ repo: string
109
+ ): Effect.fn.Return<boolean, PersistenceError> {
110
+ const store = yield* loadTrustStore(files, path)
111
+ const key = resolve(repo)
112
+ if (store._tag === "Unusable" || !Object.hasOwn(store.records, key)) {
113
+ return false
114
+ }
115
+ yield* save(
116
+ files,
117
+ path,
118
+ Object.fromEntries(Object.entries(store.records).filter(([held]) => held !== key))
119
+ )
120
+ return true
121
+ })
122
+
123
+ /** The one command that reviews and accepts the project file of `repo`. */
124
+ export const trustCommand = (repo: string): string => `llm4ts config trust --repo ${resolve(repo)}`
125
+
126
+ const standing = (projectPath: string, trust: Distrust): string =>
127
+ trust === "changed"
128
+ ? `${projectPath} has changed since it was trusted`
129
+ : `${projectPath} is new and not trusted`
130
+
131
+ /** What every verb that goes on without the file says. */
132
+ export const distrustNotice = (projectPath: string, repo: string, trust: Distrust): string =>
133
+ `${standing(projectPath, trust)}, so llm4ts did not read it. Review and accept it: ${trustCommand(repo)}`
134
+
135
+ /** Why a launch stops: a run must not go on other settings than the file says. */
136
+ export const launchRefusal = (projectPath: string, repo: string, trust: Distrust): string =>
137
+ `${distrustNotice(projectPath, repo, trust)}\nNothing was started.`
138
+
139
+ /** Why `config set`, `unset`, `init` and `edit` leave the file alone. */
140
+ export const writeRefusal = (projectPath: string, repo: string, trust: Distrust): string =>
141
+ `${standing(projectPath, trust)}, so llm4ts writes nothing to it. Review and accept it first: ${trustCommand(repo)}`
142
+
143
+ /** Said once per load when the list itself cannot be used. */
144
+ export const unusableStoreNotice = (storePath: string, reason: string): string =>
145
+ `${storePath}: the trust list cannot be used (${reason}), so no project file is trusted; llm4ts config trust writes a new list`
package/src/FlowLaunch.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { spawnSync } from "node:child_process"
2
2
  import { constants } from "node:os"
3
3
  import * as Effect from "effect/Effect"
4
+ import type { FlowEnvironment } from "./Config.ts"
4
5
  import { FlowLaunchError, needsResolveFallback, nodeFlowProcess } from "./FlowProcess.ts"
5
6
 
6
7
  export { FlowLaunchError, needsResolveFallback }
@@ -8,10 +9,8 @@ export { FlowLaunchError, needsResolveFallback }
8
9
  export interface LaunchFlowOptions {
9
10
  readonly flowPath: string
10
11
  readonly taskArgs: ReadonlyArray<string>
11
- readonly environment?: Readonly<Record<string, string | undefined>>
12
+ readonly environment: FlowEnvironment
12
13
  readonly cwd?: string
13
- /** Overrides `LLM4TS_CODER` in the child for this run only. */
14
- readonly coder?: string
15
14
  /** `inherit` shares the terminal (the default); tests use `ignore`. */
16
15
  readonly stdio?: "inherit" | "ignore"
17
16
  }
@@ -40,10 +39,6 @@ const restoreTerminal = (stdio: "inherit" | "ignore"): void => {
40
39
  */
41
40
  export const launchFlow = (options: LaunchFlowOptions): Effect.Effect<number, FlowLaunchError> =>
42
41
  Effect.gen(function* () {
43
- const environment = { ...(options.environment ?? process.env) }
44
- if (options.coder !== undefined) {
45
- environment.LLM4TS_CODER = options.coder
46
- }
47
42
  const stdio = options.stdio ?? "inherit"
48
43
  const ignoreSigint = (): void => {}
49
44
  process.on("SIGINT", ignoreSigint)
@@ -54,7 +49,7 @@ export const launchFlow = (options: LaunchFlowOptions): Effect.Effect<number, Fl
54
49
  const handle = yield* nodeFlowProcess.start({
55
50
  flowPath: options.flowPath,
56
51
  args: options.taskArgs,
57
- environment,
52
+ environment: options.environment,
58
53
  stdio,
59
54
  ...(options.cwd === undefined ? {} : { cwd: options.cwd })
60
55
  })
@@ -6,6 +6,7 @@ import * as Deferred from "effect/Deferred"
6
6
  import * as Effect from "effect/Effect"
7
7
  import * as Ref from "effect/Ref"
8
8
  import * as Schema from "effect/Schema"
9
+ import type { FlowEnvironment } from "./Config.ts"
9
10
 
10
11
  export class FlowLaunchError extends Schema.TaggedError<FlowLaunchError>()("FlowLaunch", {
11
12
  message: Schema.String
@@ -46,7 +47,8 @@ const resolveFallbackUrl = (() => {
46
47
  export interface FlowProcessStart {
47
48
  readonly flowPath: string
48
49
  readonly args: ReadonlyArray<string>
49
- readonly environment: Readonly<Record<string, string | undefined>>
50
+ /** Made by `flowEnvironment` only, so no launcher can skip the config files. */
51
+ readonly environment: FlowEnvironment
50
52
  readonly cwd?: string
51
53
  /**
52
54
  * File paths are opened for append by the Node implementation. `ignore`
package/src/Menu.ts CHANGED
@@ -3,10 +3,16 @@ import * as Effect from "effect/Effect"
3
3
  import { FileSystem } from "effect/FileSystem"
4
4
  import { Prompt } from "effect/cli"
5
5
  import { coderChoices, findOnPath, resolvedCoderToken } from "./CoderChoice.ts"
6
+ import { launchTrustRefusal, loadConfig, prepareLaunch, resolvedEnvironment } from "./Config.ts"
7
+ import { openConfigEditor } from "./ConfigCommand.ts"
6
8
  import { discoverFlows, type DiscoveredFlow, type FlowTierPaths } from "./FlowCatalog.ts"
7
9
  import { launchFlow } from "./FlowLaunch.ts"
10
+ import { ShellUsageError } from "./ShellErrors.ts"
11
+ import { nodePlainFileStore } from "@llm4ts/runner/NodePlainFileStore"
8
12
 
9
- type MenuAction = "run" | "view" | "exit"
13
+ const launchDeps = { files: nodePlainFileStore }
14
+
15
+ type MenuAction = "run" | "view" | "configure" | "exit"
10
16
 
11
17
  const flowChoice = (flow: DiscoveredFlow): Prompt.SelectChoice<DiscoveredFlow> => {
12
18
  const shadows = flow.shadows.length === 0 ? "" : ` (shadows ${flow.shadows.join(", ")})`
@@ -37,9 +43,9 @@ const selectCoder = Effect.fn("@llm4ts/shell/Menu.selectCoder")(function* (
37
43
  const resolved = resolvedCoderToken(environment)
38
44
  const choices: Array<Prompt.SelectChoice<string | undefined>> = [
39
45
  {
40
- title: `environment default (${resolved})`,
46
+ title: `configured (${resolved})`,
41
47
  value: undefined,
42
- description: "Use LLM4TS_CODER as-is"
48
+ description: "LLM4TS_CODER from the environment or the config files"
43
49
  },
44
50
  ...coderChoices.map(
45
51
  (choice): Prompt.SelectChoice<string | undefined> => ({
@@ -67,11 +73,25 @@ const runFlowInteractive = Effect.fn("@llm4ts/shell/Menu.runFlowInteractive")(fu
67
73
  const task = yield* Prompt.run(
68
74
  Prompt.String({ message: "Task text (empty uses the flow's default)" })
69
75
  )
70
- const coder = yield* selectCoder(process.env)
76
+ const repo = process.cwd()
77
+ const settled = yield* loadConfig({ repo, environment: process.env, flags: [], ...launchDeps })
78
+ const refusal = launchTrustRefusal(settled, repo)
79
+ if (refusal !== undefined) {
80
+ return yield* new ShellUsageError({ message: refusal })
81
+ }
82
+ const coder = yield* selectCoder(resolvedEnvironment(settled.resolution, process.env))
83
+ const plan = yield* prepareLaunch({
84
+ repo,
85
+ flow: flow.name,
86
+ base: process.env,
87
+ flags: coder === undefined ? [] : [{ name: "LLM4TS_CODER", value: coder, flag: "the menu" }],
88
+ ...launchDeps
89
+ })
90
+ yield* Effect.forEach(plan.notices, (notice) => Console.error(notice), { discard: true })
71
91
  const exitCode = yield* launchFlow({
72
92
  flowPath: flow.path,
73
93
  taskArgs: task.trim().length === 0 ? [] : [task],
74
- ...(coder === undefined ? {} : { coder })
94
+ environment: plan.environment
75
95
  })
76
96
  yield* exitCode === 0
77
97
  ? Console.log(`${flow.name} completed`)
@@ -91,7 +111,7 @@ const viewFlowInteractive = Effect.fn("@llm4ts/shell/Menu.viewFlowInteractive")(
91
111
  })
92
112
 
93
113
  /**
94
- * The interactive main menu: run a flow, view a flow, exit. Ctrl-C inside a
114
+ * The interactive main menu: run a flow, view a flow, configure settings, exit. Ctrl-C inside a
95
115
  * prompt quits the menu cleanly rather than crashing the shell.
96
116
  */
97
117
  export const mainMenu = Effect.fn("@llm4ts/shell/Menu.mainMenu")(function* (tiers: FlowTierPaths) {
@@ -102,19 +122,32 @@ export const mainMenu = Effect.fn("@llm4ts/shell/Menu.mainMenu")(function* (tier
102
122
  choices: [
103
123
  { title: "Run a flow", value: "run" },
104
124
  { title: "View a flow", value: "view" },
125
+ { title: "Configure settings", value: "configure" },
105
126
  { title: "Exit", value: "exit" }
106
127
  ]
107
128
  })
108
129
  ).pipe(Effect.catchTag("QuitError", () => Effect.succeed<MenuAction>("exit")))
109
130
  switch (action) {
110
131
  case "run": {
111
- yield* runFlowInteractive(tiers).pipe(Effect.catchTag("QuitError", () => Effect.void))
132
+ yield* runFlowInteractive(tiers).pipe(
133
+ Effect.catchTag("QuitError", () => Effect.void),
134
+ Effect.catchTags({
135
+ ShellUsage: (error) => Console.error(error.message),
136
+ ConfigFileError: (error) => Console.error(error.message)
137
+ })
138
+ )
112
139
  break
113
140
  }
114
141
  case "view": {
115
142
  yield* viewFlowInteractive(tiers).pipe(Effect.catchTag("QuitError", () => Effect.void))
116
143
  break
117
144
  }
145
+ case "configure": {
146
+ yield* openConfigEditor(process.cwd()).pipe(
147
+ Effect.catchTag("ShellUsage", (error) => Console.error(error.message))
148
+ )
149
+ break
150
+ }
118
151
  case "exit": {
119
152
  return
120
153
  }
@@ -17,7 +17,8 @@ import {
17
17
  } from "@llm4ts/flow/OperatorSchemas"
18
18
  import type { OperatorKnobs } from "@llm4ts/flow/OperatorSchemas"
19
19
  import type { PlainFileStoreShape } from "@llm4ts/flow/Persistence"
20
- import type { FlowLaunchError, FlowProcessHandle, FlowProcessShape } from "./FlowProcess.ts"
20
+ import { prepareLaunch, type FlagAssignment } from "./Config.ts"
21
+ import { FlowLaunchError, type FlowProcessHandle, type FlowProcessShape } from "./FlowProcess.ts"
21
22
 
22
23
  /**
23
24
  * Where the operator keeps one repository's registry and run logs:
@@ -84,12 +85,18 @@ export const knobsToArgs = (_flow: string, knobs: OperatorKnobs): ReadonlyArray<
84
85
  ...(knobs.flags ?? [])
85
86
  ]
86
87
 
87
- /** The only knobs that reach the flow's environment. */
88
- export const knobsToEnvironment = (knobs: OperatorKnobs): Record<string, string> => ({
89
- ...(knobs.executors === undefined ? {} : { LLM4TS_EXECUTORS: knobs.executors.join(",") }),
90
- ...(knobs.pack === undefined ? {} : { LLM4TS_PACK: knobs.pack }),
91
- ...(knobs.coder === undefined ? {} : { LLM4TS_CODER: knobs.coder })
92
- })
88
+ /** The only knobs that become settings: the operator's flag layer over the config files. */
89
+ export const knobAssignments = (knobs: OperatorKnobs): ReadonlyArray<FlagAssignment> => [
90
+ ...(knobs.executors === undefined
91
+ ? []
92
+ : [{ name: "LLM4TS_EXECUTORS", value: knobs.executors.join(","), flag: "the executors knob" }]),
93
+ ...(knobs.pack === undefined
94
+ ? []
95
+ : [{ name: "LLM4TS_PACK", value: knobs.pack, flag: "the pack knob" }]),
96
+ ...(knobs.coder === undefined
97
+ ? []
98
+ : [{ name: "LLM4TS_CODER", value: knobs.coder, flag: "the coder knob" }])
99
+ ]
93
100
 
94
101
  const randomHex = (): string =>
95
102
  Math.floor(Math.random() * 0x10000)
@@ -243,6 +250,17 @@ export const makeRunSupervisor = (
243
250
  state: "starting",
244
251
  ...(requestId === undefined ? {} : { requestId })
245
252
  })
253
+ // Refused before anything is recorded: a bad knob, a broken config
254
+ // file, a project file that is not trusted, or a setting the flow
255
+ // cannot start without.
256
+ const plan = yield* prepareLaunch({
257
+ repo: request.repo.path,
258
+ flow: request.flow.name,
259
+ base: childEnvironment(request.baseEnvironment),
260
+ flags: knobAssignments(request.knobs),
261
+ files: options.files,
262
+ extra: { LLM4TS_RUN_ID: runId, LLM4TS_TRACE_PATH: tracePath }
263
+ }).pipe(Effect.mapError((error) => new FlowLaunchError({ message: error.message })))
246
264
  launching.add(runId)
247
265
  return yield* Effect.gen(function* () {
248
266
  yield* record(starting)
@@ -250,13 +268,7 @@ export const makeRunSupervisor = (
250
268
  .start({
251
269
  flowPath: request.flow.path,
252
270
  args,
253
- environment: {
254
- ...childEnvironment(request.baseEnvironment),
255
- ...knobsToEnvironment(request.knobs),
256
- LLM4TS_RUN_ID: runId,
257
- LLM4TS_TRACE_PATH: tracePath,
258
- LLM4TS_FLOW: request.flow.name
259
- },
271
+ environment: plan.environment,
260
272
  cwd: request.repo.path,
261
273
  stdio: { stdout: join(logDir, "stdout.log"), stderr: join(logDir, "stderr.log") },
262
274
  detached: true