@llm4ts/shell 3.2.6 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -0
- package/dist/Cli.d.ts +11 -3
- package/dist/Cli.d.ts.map +1 -1
- package/dist/Cli.js +127 -39
- package/dist/Cli.js.map +1 -1
- package/dist/CoderChoice.d.ts +2 -4
- package/dist/CoderChoice.d.ts.map +1 -1
- package/dist/CoderChoice.js +8 -12
- package/dist/CoderChoice.js.map +1 -1
- package/dist/Config.d.ts +238 -0
- package/dist/Config.d.ts.map +1 -0
- package/dist/Config.js +594 -0
- package/dist/Config.js.map +1 -0
- package/dist/ConfigCommand.d.ts +180 -0
- package/dist/ConfigCommand.d.ts.map +1 -0
- package/dist/ConfigCommand.js +903 -0
- package/dist/ConfigCommand.js.map +1 -0
- package/dist/ConfigEdit.d.ts +79 -0
- package/dist/ConfigEdit.d.ts.map +1 -0
- package/dist/ConfigEdit.js +110 -0
- package/dist/ConfigEdit.js.map +1 -0
- package/dist/ConfigTrust.d.ts +45 -0
- package/dist/ConfigTrust.d.ts.map +1 -0
- package/dist/ConfigTrust.js +72 -0
- package/dist/ConfigTrust.js.map +1 -0
- package/dist/FlowLaunch.d.ts +2 -3
- package/dist/FlowLaunch.d.ts.map +1 -1
- package/dist/FlowLaunch.js +1 -5
- package/dist/FlowLaunch.js.map +1 -1
- package/dist/FlowProcess.d.ts +3 -1
- package/dist/FlowProcess.d.ts.map +1 -1
- package/dist/FlowProcess.js.map +1 -1
- package/dist/Menu.d.ts +1 -1
- package/dist/Menu.d.ts.map +1 -1
- package/dist/Menu.js +33 -6
- package/dist/Menu.js.map +1 -1
- package/dist/OperatorConfig.d.ts.map +1 -1
- package/dist/OperatorConfig.js +2 -1
- package/dist/OperatorConfig.js.map +1 -1
- package/dist/OperatorTools.d.ts +2 -0
- package/dist/OperatorTools.d.ts.map +1 -1
- package/dist/Refine.d.ts.map +1 -1
- package/dist/Refine.js +3 -2
- package/dist/Refine.js.map +1 -1
- package/dist/RunSupervisor.d.ts +4 -3
- package/dist/RunSupervisor.d.ts.map +1 -1
- package/dist/RunSupervisor.js +26 -13
- package/dist/RunSupervisor.js.map +1 -1
- package/flows/epic-design.js +7 -5
- package/flows/epic-retro.js +3 -2
- package/flows/implement.js +3 -2
- package/flows/issue-pr.js +3 -2
- package/flows/judge-suite.js +2 -2
- package/flows/lib/debt.js +2 -2
- package/flows/lib/epic-retro.js +3 -6
- package/flows/lib/epic-stories.js +54 -115
- package/flows/lib/modernize-extract.js +3 -6
- package/flows/lib/pack-fork.js +8 -10
- package/flows/lib/port.js +8 -10
- package/flows/local.js +3 -2
- package/flows/modernize-bench.js +5 -5
- package/flows/modernize-extract.js +8 -7
- package/flows/modernize-implement.js +4 -6
- package/flows/modernize-pack-check.js +2 -1
- package/flows/modernize-pack-upgrade.js +2 -1
- package/flows/modernize-refine.js +6 -5
- package/flows/modernize-review.js +2 -2
- package/flows/modernize-seed.js +8 -8
- package/flows/modernize-survey.js +4 -7
- package/flows/modernize-verify.js +2 -2
- package/flows/pack-fork.js +4 -3
- package/flows/port-compile.js +4 -3
- package/flows/port-files.js +2 -2
- package/flows/port-guide.js +5 -4
- package/flows/port-ledger.js +2 -2
- package/flows/port-tests.js +5 -4
- package/flows/roundtable.js +2 -2
- package/flows/sdd.js +8 -13
- package/flows/taskforce.js +5 -4
- package/kits/j2ee-nextjs/README.md +2 -1
- package/kits/j2ee-nextjs/fixtures/demo-bank/RUNBOOK.md +32 -14
- package/kits/j2ee-nextjs/fixtures/demo-bank/reset-demo.mjs +3 -1
- package/kits/j2ee-nextjs/flows/convert-all.js +8 -7
- package/kits/j2ee-nextjs/flows/convert-feature.js +2 -2
- package/kits/j2ee-nextjs/flows/convert-page.js +2 -2
- package/kits/j2ee-nextjs/flows/lib/convert.js +7 -10
- package/kits/soap-ace/flows/soap-design.js +5 -8
- package/kits/soap-ace/flows/soap-discover.js +4 -3
- package/kits/soap-ace/flows/soap-epic.js +4 -7
- package/kits/soap-ace/flows/soap-explore.js +8 -7
- package/kits/soap-ace/flows/soap-sample.js +10 -10
- package/kits/soap-ace/tools/soap-call.js +3 -2
- package/package.json +8 -4
- package/src/Cli.ts +176 -46
- package/src/CoderChoice.ts +10 -16
- package/src/Config.ts +868 -0
- package/src/ConfigCommand.ts +1423 -0
- package/src/ConfigEdit.ts +180 -0
- package/src/ConfigTrust.ts +145 -0
- package/src/FlowLaunch.ts +3 -8
- package/src/FlowProcess.ts +3 -1
- package/src/Menu.ts +40 -7
- package/src/OperatorConfig.ts +2 -1
- package/src/Refine.ts +3 -2
- package/src/RunSupervisor.ts +26 -14
package/src/Config.ts
ADDED
|
@@ -0,0 +1,868 @@
|
|
|
1
|
+
// The config files: the environment, written down. Two dotenv files hold
|
|
2
|
+
// `LLM4TS_*` names, one per user and one per clone; `resolveConfig` layers
|
|
3
|
+
// them under the process environment and the flags, and `flowEnvironment`
|
|
4
|
+
// hands the result to a flow child. No message built here carries a value,
|
|
5
|
+
// so a secret cannot leak through one.
|
|
6
|
+
import { homedir } from "node:os"
|
|
7
|
+
import { basename, extname, isAbsolute, join, resolve } from "node:path"
|
|
8
|
+
import * as Brand from "effect/Brand"
|
|
9
|
+
import * as Effect from "effect/Effect"
|
|
10
|
+
import * as Result from "effect/Result"
|
|
11
|
+
import * as Schema from "effect/Schema"
|
|
12
|
+
import type { PlainFileStoreShape } from "@llm4ts/flow/Persistence"
|
|
13
|
+
import { ignoringRunState } from "@llm4ts/flow/RunState"
|
|
14
|
+
import {
|
|
15
|
+
closestSettingName,
|
|
16
|
+
readSetting,
|
|
17
|
+
settingFacts,
|
|
18
|
+
settingNames,
|
|
19
|
+
type Environment,
|
|
20
|
+
type SettingFacts,
|
|
21
|
+
type SettingName
|
|
22
|
+
} from "@llm4ts/flow/Settings"
|
|
23
|
+
import {
|
|
24
|
+
configTrustPath,
|
|
25
|
+
contentHash,
|
|
26
|
+
distrustNotice,
|
|
27
|
+
launchRefusal,
|
|
28
|
+
loadTrustStore,
|
|
29
|
+
trustDecision,
|
|
30
|
+
unusableStoreNotice,
|
|
31
|
+
type ProjectTrust
|
|
32
|
+
} from "./ConfigTrust.ts"
|
|
33
|
+
import { ShellUsageError } from "./ShellErrors.ts"
|
|
34
|
+
|
|
35
|
+
export type FileLayer = "project" | "global"
|
|
36
|
+
|
|
37
|
+
export type Source =
|
|
38
|
+
| { readonly layer: "flag"; readonly flag: string }
|
|
39
|
+
| { readonly layer: "environment" }
|
|
40
|
+
| { readonly layer: FileLayer; readonly path: string; readonly line: number }
|
|
41
|
+
|
|
42
|
+
export type ProblemSeverity = "error" | "warning" | "note"
|
|
43
|
+
|
|
44
|
+
export type ProblemKind =
|
|
45
|
+
| "malformed"
|
|
46
|
+
| "unknown"
|
|
47
|
+
| "unreadable"
|
|
48
|
+
| "misplaced"
|
|
49
|
+
| "duplicate"
|
|
50
|
+
| "conflict"
|
|
51
|
+
| "shadowed"
|
|
52
|
+
| "untrusted"
|
|
53
|
+
|
|
54
|
+
export interface ConfigProblem {
|
|
55
|
+
readonly severity: ProblemSeverity
|
|
56
|
+
readonly kind: ProblemKind
|
|
57
|
+
/** Starts with where the problem is; never holds a value. */
|
|
58
|
+
readonly message: string
|
|
59
|
+
readonly name?: string
|
|
60
|
+
/** For an unknown name: the setting it most likely meant. */
|
|
61
|
+
readonly closest?: SettingName
|
|
62
|
+
readonly source?: Source
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type ConfigLine =
|
|
66
|
+
| { readonly _tag: "Entry"; readonly line: number; readonly name: string; readonly value: string }
|
|
67
|
+
| { readonly _tag: "Other"; readonly line: number }
|
|
68
|
+
| { readonly _tag: "Malformed"; readonly line: number }
|
|
69
|
+
|
|
70
|
+
export interface ConfigFile {
|
|
71
|
+
readonly path: string
|
|
72
|
+
readonly lines: ReadonlyArray<ConfigLine>
|
|
73
|
+
readonly problems: ReadonlyArray<ConfigProblem>
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface FlagAssignment {
|
|
77
|
+
readonly name: string
|
|
78
|
+
readonly value: string
|
|
79
|
+
/** The flag that supplied it: `--set`, `--pack`. */
|
|
80
|
+
readonly flag: string
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** One layer's value for a setting; a file's path value is already absolute. */
|
|
84
|
+
export interface Layered {
|
|
85
|
+
readonly source: Source
|
|
86
|
+
readonly raw: string
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface ResolvedSetting extends Layered {
|
|
90
|
+
readonly name: SettingName
|
|
91
|
+
/** Lower layers that also set it, highest first. */
|
|
92
|
+
readonly shadowed: ReadonlyArray<Layered>
|
|
93
|
+
/** Renderers print "set", never `raw`. */
|
|
94
|
+
readonly secret: boolean
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface Resolution {
|
|
98
|
+
readonly facts: ReadonlyArray<SettingFacts>
|
|
99
|
+
/** Settings some layer sets, in registry order; the rest are at their fallback. */
|
|
100
|
+
readonly settings: ReadonlyMap<SettingName, ResolvedSetting>
|
|
101
|
+
readonly problems: ReadonlyArray<ConfigProblem>
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export class ConfigFileError extends Schema.TaggedError<ConfigFileError>()("ConfigFileError", {
|
|
105
|
+
path: Schema.String,
|
|
106
|
+
message: Schema.String
|
|
107
|
+
}) {}
|
|
108
|
+
|
|
109
|
+
/** Every registry entry, in registry order. */
|
|
110
|
+
export const registryFacts: ReadonlyArray<SettingFacts> = settingNames.map(settingFacts)
|
|
111
|
+
|
|
112
|
+
export const homeOf = (environment: Environment): string => environment.HOME ?? homedir()
|
|
113
|
+
|
|
114
|
+
/** `${XDG_CONFIG_HOME:-~/.config}/llm4ts/config.env`. */
|
|
115
|
+
export const globalConfigPath = (environment: Environment): string =>
|
|
116
|
+
join(environment.XDG_CONFIG_HOME ?? join(homeOf(environment), ".config"), "llm4ts", "config.env")
|
|
117
|
+
|
|
118
|
+
/** `<repo>/.llm4ts/config.env`: local to the clone, above the global file. */
|
|
119
|
+
export const projectConfigPath = (repo: string): string => join(repo, ".llm4ts", "config.env")
|
|
120
|
+
|
|
121
|
+
export const describeSource = (source: Source): string =>
|
|
122
|
+
source.layer === "flag"
|
|
123
|
+
? source.flag
|
|
124
|
+
: source.layer === "environment"
|
|
125
|
+
? "the environment"
|
|
126
|
+
: `${source.path}:${source.line}`
|
|
127
|
+
|
|
128
|
+
type ParsedLine =
|
|
129
|
+
| {
|
|
130
|
+
readonly _tag: "Entry"
|
|
131
|
+
readonly name: string
|
|
132
|
+
readonly value: string
|
|
133
|
+
readonly exported: string
|
|
134
|
+
readonly comment: string
|
|
135
|
+
}
|
|
136
|
+
| { readonly _tag: "Other" }
|
|
137
|
+
| { readonly _tag: "Malformed" }
|
|
138
|
+
|
|
139
|
+
const entryPattern = /^(export\s+)?([A-Za-z_][A-Za-z0-9_]*)=(.*)$/u
|
|
140
|
+
|
|
141
|
+
const parseValue = (
|
|
142
|
+
rest: string
|
|
143
|
+
): { readonly value: string; readonly comment: string } | undefined => {
|
|
144
|
+
const quote = rest[0]
|
|
145
|
+
if (quote === '"' || quote === "'") {
|
|
146
|
+
const end = rest.indexOf(quote, 1)
|
|
147
|
+
if (end < 0) {
|
|
148
|
+
return undefined
|
|
149
|
+
}
|
|
150
|
+
const tail = rest.slice(end + 1)
|
|
151
|
+
return tail.length === 0 || /^\s+#/u.test(tail)
|
|
152
|
+
? { value: rest.slice(1, end), comment: tail }
|
|
153
|
+
: undefined
|
|
154
|
+
}
|
|
155
|
+
const at = rest.search(/\s#/u)
|
|
156
|
+
const body = at < 0 ? rest : rest.slice(0, at)
|
|
157
|
+
return { value: body.trim(), comment: at < 0 ? "" : rest.slice(body.trimEnd().length) }
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const parseLine = (text: string): ParsedLine => {
|
|
161
|
+
const trimmed = text.trim()
|
|
162
|
+
if (trimmed.length === 0 || trimmed.startsWith("#")) {
|
|
163
|
+
return { _tag: "Other" }
|
|
164
|
+
}
|
|
165
|
+
const match = entryPattern.exec(trimmed)
|
|
166
|
+
const parsed = match === null ? undefined : parseValue(match[3] ?? "")
|
|
167
|
+
return match === null || parsed === undefined
|
|
168
|
+
? { _tag: "Malformed" }
|
|
169
|
+
: { _tag: "Entry", name: match[2] ?? "", exported: match[1] ?? "", ...parsed }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* The strict dotenv subset: blank, `#` comment, or `[export ]NAME=value[ # comment]`
|
|
174
|
+
* with the value unquoted or wholly quoted. No escapes, no interpolation, no
|
|
175
|
+
* multi-line values; anything else is a malformed line.
|
|
176
|
+
*/
|
|
177
|
+
export const parseConfigFile = (text: string, path: string): ConfigFile => {
|
|
178
|
+
const rawLines = text.split("\n")
|
|
179
|
+
if (rawLines.at(-1) === "") {
|
|
180
|
+
rawLines.pop()
|
|
181
|
+
}
|
|
182
|
+
const lines = rawLines.map((raw, index): ConfigLine => {
|
|
183
|
+
const parsed = parseLine(raw)
|
|
184
|
+
const line = index + 1
|
|
185
|
+
return parsed._tag === "Entry"
|
|
186
|
+
? { _tag: "Entry", line, name: parsed.name, value: parsed.value }
|
|
187
|
+
: { _tag: parsed._tag, line }
|
|
188
|
+
})
|
|
189
|
+
return {
|
|
190
|
+
path,
|
|
191
|
+
lines,
|
|
192
|
+
problems: lines
|
|
193
|
+
.filter((line) => line._tag === "Malformed")
|
|
194
|
+
.map(
|
|
195
|
+
(line): ConfigProblem => ({
|
|
196
|
+
severity: "error",
|
|
197
|
+
kind: "malformed",
|
|
198
|
+
message: `${path}:${line.line}: not a NAME=value line`
|
|
199
|
+
})
|
|
200
|
+
)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** A leading `~/` expanded, then made absolute against `base`. */
|
|
205
|
+
export const expandPath = (raw: string, base: string, home: string): string => {
|
|
206
|
+
const trimmed = raw.trim()
|
|
207
|
+
if (trimmed === "~") {
|
|
208
|
+
return home
|
|
209
|
+
}
|
|
210
|
+
if (trimmed.startsWith("~/")) {
|
|
211
|
+
return join(home, trimmed.slice(2))
|
|
212
|
+
}
|
|
213
|
+
return isAbsolute(trimmed) ? trimmed : resolve(base, trimmed)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const misplacedInFile = {
|
|
217
|
+
run: (name: string) => `is set per run and is ignored in a file; use --set ${name}=<value>`,
|
|
218
|
+
secret: () => "is a secret and is ignored in a file; export it in the environment",
|
|
219
|
+
internal: () => "is set by llm4ts and is ignored in a file"
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const refusedFromFlag = {
|
|
223
|
+
secret: "is a secret: export it in the environment",
|
|
224
|
+
internal: "is set by llm4ts"
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export const unknownText = (name: string, closest: SettingName | undefined): string =>
|
|
228
|
+
closest === undefined
|
|
229
|
+
? `unknown setting ${name}`
|
|
230
|
+
: `unknown setting ${name}; did you mean ${closest}?`
|
|
231
|
+
|
|
232
|
+
const sameValue = (name: SettingName, left: string, right: string): boolean => {
|
|
233
|
+
if (left.trim() === right.trim()) {
|
|
234
|
+
return true
|
|
235
|
+
}
|
|
236
|
+
const [a, b] = [readSetting(name, left), readSetting(name, right)]
|
|
237
|
+
return (
|
|
238
|
+
Result.isSuccess(a) &&
|
|
239
|
+
Result.isSuccess(b) &&
|
|
240
|
+
typeof a.success === "boolean" &&
|
|
241
|
+
a.success === b.success
|
|
242
|
+
)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export interface ResolveInput {
|
|
246
|
+
readonly facts: ReadonlyArray<SettingFacts>
|
|
247
|
+
readonly flags: ReadonlyArray<FlagAssignment>
|
|
248
|
+
readonly environment: Environment
|
|
249
|
+
/** `undefined`: the file is absent. */
|
|
250
|
+
readonly project: ConfigFile | undefined
|
|
251
|
+
readonly global: ConfigFile | undefined
|
|
252
|
+
/** Relative paths in the project file resolve against it. */
|
|
253
|
+
readonly repo: string
|
|
254
|
+
/** `~/` and relative paths in the global file resolve against it. */
|
|
255
|
+
readonly home: string
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Pure: flag > environment > project file > global file. Judges each
|
|
260
|
+
* assignment against the registry and keeps what each winner shadows. An
|
|
261
|
+
* empty value counts as unset unless the grammar gives empty a meaning. A
|
|
262
|
+
* value the grammar does not read is dropped from a flag or a file (an
|
|
263
|
+
* error) and kept from the environment (a warning): an export keeps the
|
|
264
|
+
* meaning it has today.
|
|
265
|
+
*/
|
|
266
|
+
export const resolveConfig = (input: ResolveInput): Resolution => {
|
|
267
|
+
const known = new Map<string, SettingFacts>(input.facts.map((fact) => [fact.name, fact]))
|
|
268
|
+
const problems: Array<ConfigProblem> = []
|
|
269
|
+
const layers = new Map<SettingName, Array<Layered>>()
|
|
270
|
+
|
|
271
|
+
const judge = (name: string, raw: string, source: Source): void => {
|
|
272
|
+
const where = describeSource(source)
|
|
273
|
+
const fact = known.get(name)
|
|
274
|
+
if (fact === undefined) {
|
|
275
|
+
const suggestion = closestSettingName(name)
|
|
276
|
+
const closest =
|
|
277
|
+
suggestion !== undefined && known.has(suggestion) && suggestion !== name
|
|
278
|
+
? suggestion
|
|
279
|
+
: undefined
|
|
280
|
+
const text = unknownText(name, closest)
|
|
281
|
+
problems.push({
|
|
282
|
+
severity:
|
|
283
|
+
source.layer === "flag"
|
|
284
|
+
? "error"
|
|
285
|
+
: source.layer !== "environment" || closest !== undefined
|
|
286
|
+
? "warning"
|
|
287
|
+
: "note",
|
|
288
|
+
kind: "unknown",
|
|
289
|
+
message:
|
|
290
|
+
source.layer === "flag" || source.layer === "environment"
|
|
291
|
+
? `${where}: ${text}`
|
|
292
|
+
: `${where}: ${text}; ignored`,
|
|
293
|
+
name,
|
|
294
|
+
...(closest === undefined ? {} : { closest }),
|
|
295
|
+
source
|
|
296
|
+
})
|
|
297
|
+
return
|
|
298
|
+
}
|
|
299
|
+
if (source.layer === "flag" && (fact.scope === "secret" || fact.scope === "internal")) {
|
|
300
|
+
problems.push({
|
|
301
|
+
severity: "error",
|
|
302
|
+
kind: "misplaced",
|
|
303
|
+
message: `${where}: ${name} ${refusedFromFlag[fact.scope]}`,
|
|
304
|
+
name,
|
|
305
|
+
source
|
|
306
|
+
})
|
|
307
|
+
return
|
|
308
|
+
}
|
|
309
|
+
const inFile = source.layer === "project" || source.layer === "global"
|
|
310
|
+
if (inFile && (fact.scope === "run" || fact.scope === "secret" || fact.scope === "internal")) {
|
|
311
|
+
problems.push({
|
|
312
|
+
severity: "warning",
|
|
313
|
+
kind: "misplaced",
|
|
314
|
+
message: `${where}: ${name} ${misplacedInFile[fact.scope](name)}`,
|
|
315
|
+
name,
|
|
316
|
+
source
|
|
317
|
+
})
|
|
318
|
+
return
|
|
319
|
+
}
|
|
320
|
+
const value =
|
|
321
|
+
inFile && fact.path && raw.trim().length > 0
|
|
322
|
+
? expandPath(raw, source.layer === "project" ? input.repo : input.home, input.home)
|
|
323
|
+
: raw
|
|
324
|
+
const read = readSetting(fact.name, value)
|
|
325
|
+
if (Result.isFailure(read)) {
|
|
326
|
+
problems.push({
|
|
327
|
+
severity: source.layer === "environment" ? "warning" : "error",
|
|
328
|
+
kind: "unreadable",
|
|
329
|
+
message: `${where}: ${name} must be ${read.failure}`,
|
|
330
|
+
name,
|
|
331
|
+
source
|
|
332
|
+
})
|
|
333
|
+
if (source.layer !== "environment") {
|
|
334
|
+
return
|
|
335
|
+
}
|
|
336
|
+
} else if (read.success === undefined) {
|
|
337
|
+
return
|
|
338
|
+
}
|
|
339
|
+
layers.set(fact.name, [...(layers.get(fact.name) ?? []), { source, raw: value }])
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const flagsByName = new Map<string, ReadonlyArray<FlagAssignment>>()
|
|
343
|
+
for (const flag of input.flags) {
|
|
344
|
+
flagsByName.set(flag.name, [...(flagsByName.get(flag.name) ?? []), flag])
|
|
345
|
+
}
|
|
346
|
+
for (const [name, assignments] of flagsByName) {
|
|
347
|
+
const [only, ...others] = assignments
|
|
348
|
+
if (only === undefined) {
|
|
349
|
+
continue
|
|
350
|
+
}
|
|
351
|
+
if (others.length > 0) {
|
|
352
|
+
const flags = [...new Set(assignments.map((assignment) => assignment.flag))]
|
|
353
|
+
problems.push({
|
|
354
|
+
severity: "error",
|
|
355
|
+
kind: "conflict",
|
|
356
|
+
message:
|
|
357
|
+
flags.length === 1
|
|
358
|
+
? `${only.flag}: ${name} is set more than once`
|
|
359
|
+
: `${name} is set by ${flags.join(" and ")}; use one`,
|
|
360
|
+
name
|
|
361
|
+
})
|
|
362
|
+
continue
|
|
363
|
+
}
|
|
364
|
+
judge(name, only.value, { layer: "flag", flag: only.flag })
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
for (const fact of input.facts) {
|
|
368
|
+
const raw = input.environment[fact.name]
|
|
369
|
+
if (raw !== undefined) {
|
|
370
|
+
judge(fact.name, raw, { layer: "environment" })
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
for (const name of Object.keys(input.environment).sort()) {
|
|
374
|
+
if (name.startsWith("LLM4TS_") && !known.has(name) && input.environment[name] !== undefined) {
|
|
375
|
+
judge(name, "", { layer: "environment" })
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const judgeFile = (layer: FileLayer, file: ConfigFile | undefined): void => {
|
|
380
|
+
if (file === undefined) {
|
|
381
|
+
return
|
|
382
|
+
}
|
|
383
|
+
problems.push(...file.problems)
|
|
384
|
+
const last = new Map<string, number>()
|
|
385
|
+
for (const line of file.lines) {
|
|
386
|
+
if (line._tag === "Entry") {
|
|
387
|
+
last.set(line.name, line.line)
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
for (const line of file.lines) {
|
|
391
|
+
if (line._tag !== "Entry") {
|
|
392
|
+
continue
|
|
393
|
+
}
|
|
394
|
+
const source: Source = { layer, path: file.path, line: line.line }
|
|
395
|
+
const winner = last.get(line.name)
|
|
396
|
+
if (winner !== line.line) {
|
|
397
|
+
problems.push({
|
|
398
|
+
severity: "warning",
|
|
399
|
+
kind: "duplicate",
|
|
400
|
+
message: `${describeSource(source)}: ${line.name} is set again on line ${winner}; the last line wins`,
|
|
401
|
+
name: line.name,
|
|
402
|
+
source
|
|
403
|
+
})
|
|
404
|
+
continue
|
|
405
|
+
}
|
|
406
|
+
judge(line.name, line.value, source)
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
judgeFile("project", input.project)
|
|
410
|
+
judgeFile("global", input.global)
|
|
411
|
+
|
|
412
|
+
const settings = new Map<SettingName, ResolvedSetting>()
|
|
413
|
+
for (const fact of input.facts) {
|
|
414
|
+
const [winner, ...shadowed] = layers.get(fact.name) ?? []
|
|
415
|
+
if (winner === undefined) {
|
|
416
|
+
continue
|
|
417
|
+
}
|
|
418
|
+
settings.set(fact.name, {
|
|
419
|
+
name: fact.name,
|
|
420
|
+
...winner,
|
|
421
|
+
shadowed,
|
|
422
|
+
secret: fact.scope === "secret"
|
|
423
|
+
})
|
|
424
|
+
const below = shadowed[0]
|
|
425
|
+
if (
|
|
426
|
+
winner.source.layer === "environment" &&
|
|
427
|
+
below !== undefined &&
|
|
428
|
+
!sameValue(fact.name, winner.raw, below.raw)
|
|
429
|
+
) {
|
|
430
|
+
problems.push({
|
|
431
|
+
severity: "note",
|
|
432
|
+
kind: "shadowed",
|
|
433
|
+
message: `${fact.name} from the environment overrides ${describeSource(below.source)}`,
|
|
434
|
+
name: fact.name,
|
|
435
|
+
source: below.source
|
|
436
|
+
})
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return { facts: input.facts, settings, problems }
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** The only environment a flow child may get; only `flowEnvironment` makes one. */
|
|
443
|
+
export type FlowEnvironment = Environment & Brand.Brand<"@llm4ts/shell/FlowEnvironment">
|
|
444
|
+
|
|
445
|
+
const brandFlowEnvironment = Brand.nominal<FlowEnvironment>()
|
|
446
|
+
|
|
447
|
+
// Today's readers disagree on toggle spellings; every one reads this pair.
|
|
448
|
+
const projected = (setting: ResolvedSetting): string => {
|
|
449
|
+
const read = readSetting(setting.name, setting.raw)
|
|
450
|
+
return Result.isSuccess(read) && typeof read.success === "boolean"
|
|
451
|
+
? read.success
|
|
452
|
+
? "1"
|
|
453
|
+
: "off"
|
|
454
|
+
: setting.raw
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* What an in-process verb reads: `base`, plus each value a file or a flag
|
|
459
|
+
* supplies (never a default). A value that comes from the environment is
|
|
460
|
+
* left exactly as `base` holds it.
|
|
461
|
+
*/
|
|
462
|
+
export const resolvedEnvironment = (resolution: Resolution, base: Environment): Environment => ({
|
|
463
|
+
...base,
|
|
464
|
+
...Object.fromEntries(
|
|
465
|
+
[...resolution.settings.values()]
|
|
466
|
+
.filter((setting) => setting.source.layer !== "environment")
|
|
467
|
+
.map((setting): readonly [string, string] => [setting.name, projected(setting)])
|
|
468
|
+
)
|
|
469
|
+
})
|
|
470
|
+
|
|
471
|
+
/** The child's environment: the resolved one, minus `shellOnly` settings, plus `extra`. */
|
|
472
|
+
export const flowEnvironment = (
|
|
473
|
+
resolution: Resolution,
|
|
474
|
+
base: Environment,
|
|
475
|
+
extra: Environment = {}
|
|
476
|
+
): FlowEnvironment => {
|
|
477
|
+
const shellOnly = new Set<string>(
|
|
478
|
+
resolution.facts.filter((fact) => fact.shellOnly).map((fact) => fact.name)
|
|
479
|
+
)
|
|
480
|
+
return brandFlowEnvironment({
|
|
481
|
+
...Object.fromEntries(
|
|
482
|
+
Object.entries(resolvedEnvironment(resolution, base)).filter(([name]) => !shellOnly.has(name))
|
|
483
|
+
),
|
|
484
|
+
...extra
|
|
485
|
+
})
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/** The settings `flow` cannot start without that no layer sets. */
|
|
489
|
+
export const missingRequired = (
|
|
490
|
+
resolution: Resolution,
|
|
491
|
+
flow: string
|
|
492
|
+
): ReadonlyArray<SettingFacts> =>
|
|
493
|
+
resolution.facts.filter(
|
|
494
|
+
(fact) => fact.requiredBy.includes(flow) && !resolution.settings.has(fact.name)
|
|
495
|
+
)
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* How a value is written in a file: unquoted, double-quoted when it holds
|
|
499
|
+
* whitespace or `#`, single-quoted when it holds `"`. The grammar has no
|
|
500
|
+
* escapes, so a value with both quote kinds or a line break has no spelling.
|
|
501
|
+
*/
|
|
502
|
+
const writtenValue = (value: string): Result.Result<string, string> => {
|
|
503
|
+
if (/[\n\r]/u.test(value)) {
|
|
504
|
+
return Result.fail("a value cannot span lines")
|
|
505
|
+
}
|
|
506
|
+
if (value.includes('"')) {
|
|
507
|
+
return value.includes("'")
|
|
508
|
+
? Result.fail(`a value cannot hold both ' and "`)
|
|
509
|
+
: Result.succeed(`'${value}'`)
|
|
510
|
+
}
|
|
511
|
+
return Result.succeed(/[\s#]/u.test(value) || value.startsWith("'") ? `"${value}"` : value)
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
export interface ConfigChange {
|
|
515
|
+
readonly name: string
|
|
516
|
+
/** `undefined` removes the name. */
|
|
517
|
+
readonly value: string | undefined
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Pure: `text` after setting or removing names. A set rewrites the name's
|
|
522
|
+
* last line in place, keeping its `export` and its comment, or appends a
|
|
523
|
+
* line; a removal drops every line of the name. Every other line stays byte
|
|
524
|
+
* for byte, and setting the current value returns `text` itself. The
|
|
525
|
+
* failure names the setting and the reason, never the value.
|
|
526
|
+
*/
|
|
527
|
+
export const editConfigText = (
|
|
528
|
+
text: string,
|
|
529
|
+
changes: ReadonlyArray<ConfigChange>
|
|
530
|
+
): Result.Result<string, string> => {
|
|
531
|
+
let lines: ReadonlyArray<string> = text.split("\n")
|
|
532
|
+
let changed = false
|
|
533
|
+
for (const change of changes) {
|
|
534
|
+
const holds = (raw: string): boolean => {
|
|
535
|
+
const parsed = parseLine(raw)
|
|
536
|
+
return parsed._tag === "Entry" && parsed.name === change.name
|
|
537
|
+
}
|
|
538
|
+
if (change.value === undefined) {
|
|
539
|
+
const kept = lines.filter((raw) => !holds(raw))
|
|
540
|
+
changed ||= kept.length !== lines.length
|
|
541
|
+
lines = kept
|
|
542
|
+
continue
|
|
543
|
+
}
|
|
544
|
+
const written = writtenValue(change.value)
|
|
545
|
+
if (Result.isFailure(written)) {
|
|
546
|
+
return Result.fail(`${change.name}: ${written.failure}`)
|
|
547
|
+
}
|
|
548
|
+
const at = lines.reduce((found, raw, index) => (holds(raw) ? index : found), -1)
|
|
549
|
+
const current = lines[at]
|
|
550
|
+
const parsed = current === undefined ? undefined : parseLine(current)
|
|
551
|
+
if (current === undefined || parsed?._tag !== "Entry") {
|
|
552
|
+
const entry = `${change.name}=${written.success}`
|
|
553
|
+
lines = lines.at(-1) === "" ? [...lines.slice(0, -1), entry, ""] : [...lines, entry, ""]
|
|
554
|
+
changed = true
|
|
555
|
+
continue
|
|
556
|
+
}
|
|
557
|
+
if (parsed.value === change.value) {
|
|
558
|
+
continue
|
|
559
|
+
}
|
|
560
|
+
const indent = current.slice(0, current.length - current.trimStart().length)
|
|
561
|
+
const ending = current.endsWith("\r") ? "\r" : ""
|
|
562
|
+
const rewritten = `${indent}${parsed.exported}${change.name}=${written.success}${parsed.comment}${ending}`
|
|
563
|
+
lines = lines.map((raw, index) => (index === at ? rewritten : raw))
|
|
564
|
+
changed = true
|
|
565
|
+
}
|
|
566
|
+
return Result.succeed(changed ? lines.join("\n") : text)
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
export interface PlannedSet {
|
|
570
|
+
readonly name: SettingName
|
|
571
|
+
readonly value: string
|
|
572
|
+
/** The file `config set` writes it to. */
|
|
573
|
+
readonly target: FileLayer
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
const refusedBySet = {
|
|
577
|
+
run: (name: string) => `${name} is set per run: use llm4ts run --set ${name}=<value>`,
|
|
578
|
+
secret: (name: string) =>
|
|
579
|
+
`${name} is a secret: export it in the environment; it is never written to a file`,
|
|
580
|
+
internal: (name: string) => `${name} is set by llm4ts`
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/** Why no file may hold the setting; `undefined` when one may. */
|
|
584
|
+
export const storeRefusal = (fact: SettingFacts): string | undefined =>
|
|
585
|
+
fact.scope === "run" || fact.scope === "secret" || fact.scope === "internal"
|
|
586
|
+
? refusedBySet[fact.scope](fact.name)
|
|
587
|
+
: undefined
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Pure: `config set` pairs checked against the registry and routed to a
|
|
591
|
+
* file: a global setting to the global file, a project setting to the
|
|
592
|
+
* project file, unless `target` forces one. Every refusal is reported, not
|
|
593
|
+
* only the first.
|
|
594
|
+
*/
|
|
595
|
+
export const planSet = (
|
|
596
|
+
facts: ReadonlyArray<SettingFacts>,
|
|
597
|
+
assignments: ReadonlyArray<{ readonly name: string; readonly value: string }>,
|
|
598
|
+
target?: FileLayer
|
|
599
|
+
): Result.Result<ReadonlyArray<PlannedSet>, ReadonlyArray<string>> => {
|
|
600
|
+
const known = new Map<string, SettingFacts>(facts.map((fact) => [fact.name, fact]))
|
|
601
|
+
const planned: Array<PlannedSet> = []
|
|
602
|
+
const refusals: Array<string> = []
|
|
603
|
+
for (const { name, value } of assignments) {
|
|
604
|
+
const fact = known.get(name)
|
|
605
|
+
if (fact === undefined) {
|
|
606
|
+
const suggestion = closestSettingName(name)
|
|
607
|
+
refusals.push(
|
|
608
|
+
unknownText(
|
|
609
|
+
name,
|
|
610
|
+
suggestion !== undefined && known.has(suggestion) ? suggestion : undefined
|
|
611
|
+
)
|
|
612
|
+
)
|
|
613
|
+
continue
|
|
614
|
+
}
|
|
615
|
+
const refusal = storeRefusal(fact)
|
|
616
|
+
if (refusal !== undefined) {
|
|
617
|
+
refusals.push(refusal)
|
|
618
|
+
continue
|
|
619
|
+
}
|
|
620
|
+
const read = readSetting(fact.name, value)
|
|
621
|
+
if (Result.isFailure(read)) {
|
|
622
|
+
refusals.push(`${name} must be ${read.failure}`)
|
|
623
|
+
continue
|
|
624
|
+
}
|
|
625
|
+
if (read.success === undefined) {
|
|
626
|
+
refusals.push(`${name} needs a value; llm4ts config unset ${name} removes it`)
|
|
627
|
+
continue
|
|
628
|
+
}
|
|
629
|
+
const written = writtenValue(value)
|
|
630
|
+
if (Result.isFailure(written)) {
|
|
631
|
+
refusals.push(`${name}: ${written.failure}`)
|
|
632
|
+
continue
|
|
633
|
+
}
|
|
634
|
+
planned.push({
|
|
635
|
+
name: fact.name,
|
|
636
|
+
value,
|
|
637
|
+
target: target ?? (fact.scope === "project" ? "project" : "global")
|
|
638
|
+
})
|
|
639
|
+
}
|
|
640
|
+
return refusals.length > 0 ? Result.fail(refusals) : Result.succeed(planned)
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
const readFile = (files: PlainFileStoreShape, path: string) =>
|
|
644
|
+
files
|
|
645
|
+
.read(path)
|
|
646
|
+
.pipe(
|
|
647
|
+
Effect.mapError((error) =>
|
|
648
|
+
ConfigFileError.make({ path, message: `cannot read ${path}: ${error.message}` })
|
|
649
|
+
)
|
|
650
|
+
)
|
|
651
|
+
|
|
652
|
+
export interface LoadedConfig {
|
|
653
|
+
readonly resolution: Resolution
|
|
654
|
+
/** `undefined`: the file is absent, or it is not trusted and none of it is read. */
|
|
655
|
+
readonly project: ConfigFile | undefined
|
|
656
|
+
readonly global: ConfigFile | undefined
|
|
657
|
+
/**
|
|
658
|
+
* How the project file stands against the user's trust list. Only a
|
|
659
|
+
* `trusted` file reaches the resolution: the file can set commands llm4ts
|
|
660
|
+
* runs, and anyone who hands over a directory can put one in it.
|
|
661
|
+
*/
|
|
662
|
+
readonly projectTrust: ProjectTrust
|
|
663
|
+
/** The hash of the project file as it was read; `undefined` when absent. */
|
|
664
|
+
readonly projectHash: string | undefined
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/**
|
|
668
|
+
* Reads both files for `repo`, parses and resolves. An absent file is no
|
|
669
|
+
* problem. The project file is hashed from the text that is then parsed, so
|
|
670
|
+
* the content that was judged is the content that is used.
|
|
671
|
+
*/
|
|
672
|
+
export const loadConfig = Effect.fn("@llm4ts/shell/Config.loadConfig")(function* (options: {
|
|
673
|
+
readonly repo: string
|
|
674
|
+
readonly environment: Environment
|
|
675
|
+
readonly flags: ReadonlyArray<FlagAssignment>
|
|
676
|
+
readonly files: PlainFileStoreShape
|
|
677
|
+
}): Effect.fn.Return<LoadedConfig, ConfigFileError> {
|
|
678
|
+
const globalPath = globalConfigPath(options.environment)
|
|
679
|
+
const globalText = yield* readFile(options.files, globalPath)
|
|
680
|
+
const global = globalText === undefined ? undefined : parseConfigFile(globalText, globalPath)
|
|
681
|
+
const projectPath = projectConfigPath(options.repo)
|
|
682
|
+
const projectText = yield* readFile(options.files, projectPath)
|
|
683
|
+
const projectHash = projectText === undefined ? undefined : contentHash(projectText)
|
|
684
|
+
const trustPath = configTrustPath(options.environment)
|
|
685
|
+
const store =
|
|
686
|
+
projectHash === undefined ? undefined : yield* loadTrustStore(options.files, trustPath)
|
|
687
|
+
const projectTrust: ProjectTrust =
|
|
688
|
+
projectHash === undefined || store === undefined
|
|
689
|
+
? "absent"
|
|
690
|
+
: trustDecision(store, options.repo, projectHash)
|
|
691
|
+
const project =
|
|
692
|
+
projectText !== undefined && projectTrust === "trusted"
|
|
693
|
+
? parseConfigFile(projectText, projectPath)
|
|
694
|
+
: undefined
|
|
695
|
+
const resolution = resolveConfig({
|
|
696
|
+
facts: registryFacts,
|
|
697
|
+
flags: options.flags,
|
|
698
|
+
environment: options.environment,
|
|
699
|
+
project,
|
|
700
|
+
global,
|
|
701
|
+
repo: options.repo,
|
|
702
|
+
home: homeOf(options.environment)
|
|
703
|
+
})
|
|
704
|
+
const notice = (message: string): ConfigProblem => ({
|
|
705
|
+
severity: "warning",
|
|
706
|
+
kind: "untrusted",
|
|
707
|
+
message
|
|
708
|
+
})
|
|
709
|
+
const distrust =
|
|
710
|
+
projectTrust === "untrusted" || projectTrust === "changed"
|
|
711
|
+
? [
|
|
712
|
+
...(store?._tag === "Unusable"
|
|
713
|
+
? [notice(unusableStoreNotice(trustPath, store.reason))]
|
|
714
|
+
: []),
|
|
715
|
+
notice(distrustNotice(projectPath, options.repo, projectTrust))
|
|
716
|
+
]
|
|
717
|
+
: []
|
|
718
|
+
return {
|
|
719
|
+
resolution:
|
|
720
|
+
distrust.length === 0
|
|
721
|
+
? resolution
|
|
722
|
+
: { ...resolution, problems: [...resolution.problems, ...distrust] },
|
|
723
|
+
project,
|
|
724
|
+
global,
|
|
725
|
+
projectTrust,
|
|
726
|
+
projectHash
|
|
727
|
+
}
|
|
728
|
+
})
|
|
729
|
+
|
|
730
|
+
/** `convert-page` for `/flows/convert-page.ts`: the name `requiredBy` and `LLM4TS_FLOW` carry. */
|
|
731
|
+
export const flowNameOf = (flowPath: string): string => basename(flowPath, extname(flowPath))
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* Pure: what a launch refuses over (every error) and what it prints first:
|
|
735
|
+
* each warning, and each environment value that overrides a file. An unknown
|
|
736
|
+
* environment name with no close setting stays silent: a user's own flow may
|
|
737
|
+
* read it.
|
|
738
|
+
*/
|
|
739
|
+
export const launchProblems = (
|
|
740
|
+
resolution: Resolution
|
|
741
|
+
): { readonly blocking: ReadonlyArray<string>; readonly notices: ReadonlyArray<string> } => ({
|
|
742
|
+
blocking: resolution.problems
|
|
743
|
+
.filter((problem) => problem.severity === "error")
|
|
744
|
+
.map((problem) => problem.message),
|
|
745
|
+
notices: resolution.problems.flatMap((problem) =>
|
|
746
|
+
problem.severity === "warning"
|
|
747
|
+
? [`warning: ${problem.message}`]
|
|
748
|
+
: problem.kind === "shadowed"
|
|
749
|
+
? [`note: ${problem.message}`]
|
|
750
|
+
: []
|
|
751
|
+
)
|
|
752
|
+
})
|
|
753
|
+
|
|
754
|
+
/** Pure: what `flow` lacks, with each setting's doc and the two ways to set it. */
|
|
755
|
+
export const renderMissing = (
|
|
756
|
+
flow: string,
|
|
757
|
+
repo: string,
|
|
758
|
+
missing: ReadonlyArray<SettingFacts>
|
|
759
|
+
): string =>
|
|
760
|
+
missing
|
|
761
|
+
.flatMap((fact) => [
|
|
762
|
+
`${flow} needs ${fact.name}: ${fact.doc}`,
|
|
763
|
+
` for this repository: llm4ts config set --repo ${repo} ${fact.name}=<value>`,
|
|
764
|
+
` for this run only: llm4ts run ${flow} --set ${fact.name}=<value>`
|
|
765
|
+
])
|
|
766
|
+
.join("\n")
|
|
767
|
+
|
|
768
|
+
/** Pure: why a launch for `repo` stops over its project file; `undefined` when it may go on. */
|
|
769
|
+
export const launchTrustRefusal = (loaded: LoadedConfig, repo: string): string | undefined =>
|
|
770
|
+
loaded.projectTrust === "untrusted" || loaded.projectTrust === "changed"
|
|
771
|
+
? launchRefusal(projectConfigPath(repo), repo, loaded.projectTrust)
|
|
772
|
+
: undefined
|
|
773
|
+
|
|
774
|
+
export interface LaunchPlan {
|
|
775
|
+
readonly resolution: Resolution
|
|
776
|
+
/** Printed to stderr before the child starts. */
|
|
777
|
+
readonly notices: ReadonlyArray<string>
|
|
778
|
+
readonly environment: FlowEnvironment
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* The one way to a flow child's environment: loads the config for `repo`,
|
|
783
|
+
* refuses on a project file that is not trusted, on any error or on a
|
|
784
|
+
* setting `flow` cannot start without, and
|
|
785
|
+
* projects the rest, naming the flow in `LLM4TS_FLOW`.
|
|
786
|
+
*/
|
|
787
|
+
export const prepareLaunch = Effect.fn("@llm4ts/shell/Config.prepareLaunch")(function* (options: {
|
|
788
|
+
readonly repo: string
|
|
789
|
+
readonly flow: string
|
|
790
|
+
readonly base: Environment
|
|
791
|
+
readonly flags: ReadonlyArray<FlagAssignment>
|
|
792
|
+
readonly files: PlainFileStoreShape
|
|
793
|
+
readonly extra?: Environment
|
|
794
|
+
}): Effect.fn.Return<LaunchPlan, ShellUsageError> {
|
|
795
|
+
const loaded = yield* loadConfig({
|
|
796
|
+
repo: options.repo,
|
|
797
|
+
environment: options.base,
|
|
798
|
+
flags: options.flags,
|
|
799
|
+
files: options.files
|
|
800
|
+
}).pipe(Effect.mapError((error) => new ShellUsageError({ message: error.message })))
|
|
801
|
+
const refusal = launchTrustRefusal(loaded, options.repo)
|
|
802
|
+
if (refusal !== undefined) {
|
|
803
|
+
return yield* new ShellUsageError({ message: refusal })
|
|
804
|
+
}
|
|
805
|
+
const { resolution } = loaded
|
|
806
|
+
const { blocking, notices } = launchProblems(resolution)
|
|
807
|
+
if (blocking.length > 0) {
|
|
808
|
+
return yield* new ShellUsageError({ message: blocking.join("\n") })
|
|
809
|
+
}
|
|
810
|
+
const missing = missingRequired(resolution, options.flow)
|
|
811
|
+
if (missing.length > 0) {
|
|
812
|
+
return yield* new ShellUsageError({
|
|
813
|
+
message: renderMissing(options.flow, options.repo, missing)
|
|
814
|
+
})
|
|
815
|
+
}
|
|
816
|
+
return {
|
|
817
|
+
resolution,
|
|
818
|
+
notices,
|
|
819
|
+
environment: flowEnvironment(resolution, options.base, {
|
|
820
|
+
...options.extra,
|
|
821
|
+
LLM4TS_FLOW: options.flow
|
|
822
|
+
})
|
|
823
|
+
}
|
|
824
|
+
})
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* Applies `changes` to the file at `path` and replaces it atomically; the
|
|
828
|
+
* first write under a `.llm4ts/` directory leaves its `.gitignore`, as a
|
|
829
|
+
* run's first write does. Succeeds with the text written, or `undefined`
|
|
830
|
+
* when nothing changed: setting the current values writes nothing. With
|
|
831
|
+
* `expect`, a file whose content is not the one that was judged (its hash,
|
|
832
|
+
* `undefined` for absent) is left alone.
|
|
833
|
+
*/
|
|
834
|
+
export const writeConfig = Effect.fn("@llm4ts/shell/Config.writeConfig")(function* (options: {
|
|
835
|
+
readonly files: PlainFileStoreShape
|
|
836
|
+
readonly path: string
|
|
837
|
+
readonly changes: ReadonlyArray<ConfigChange>
|
|
838
|
+
readonly expect?: { readonly hash: string | undefined }
|
|
839
|
+
}): Effect.fn.Return<string | undefined, ConfigFileError> {
|
|
840
|
+
const { path } = options
|
|
841
|
+
const files = ignoringRunState(options.files)
|
|
842
|
+
const found = yield* readFile(files, path)
|
|
843
|
+
if (
|
|
844
|
+
options.expect !== undefined &&
|
|
845
|
+
options.expect.hash !== (found === undefined ? undefined : contentHash(found))
|
|
846
|
+
) {
|
|
847
|
+
return yield* ConfigFileError.make({
|
|
848
|
+
path,
|
|
849
|
+
message: `${path} changed while llm4ts was working; nothing was written`
|
|
850
|
+
})
|
|
851
|
+
}
|
|
852
|
+
const before = found ?? ""
|
|
853
|
+
const edited = editConfigText(before, options.changes)
|
|
854
|
+
if (Result.isFailure(edited)) {
|
|
855
|
+
return yield* ConfigFileError.make({ path, message: edited.failure })
|
|
856
|
+
}
|
|
857
|
+
if (edited.success === before) {
|
|
858
|
+
return undefined
|
|
859
|
+
}
|
|
860
|
+
yield* files
|
|
861
|
+
.writeAtomic(path, edited.success)
|
|
862
|
+
.pipe(
|
|
863
|
+
Effect.mapError((error) =>
|
|
864
|
+
ConfigFileError.make({ path, message: `cannot write ${path}: ${error.message}` })
|
|
865
|
+
)
|
|
866
|
+
)
|
|
867
|
+
return edited.success
|
|
868
|
+
})
|