@llm4ts/flow 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.
- package/dist/Settings.d.ts +79 -0
- package/dist/Settings.d.ts.map +1 -0
- package/dist/Settings.js +175 -0
- package/dist/Settings.js.map +1 -0
- package/dist/settings/Registry.d.ts +625 -0
- package/dist/settings/Registry.d.ts.map +1 -0
- package/dist/settings/Registry.js +741 -0
- package/dist/settings/Registry.js.map +1 -0
- package/dist/settings/Value.d.ts +51 -0
- package/dist/settings/Value.d.ts.map +1 -0
- package/dist/settings/Value.js +102 -0
- package/dist/settings/Value.js.map +1 -0
- package/package.json +3 -2
- package/src/Settings.ts +270 -0
- package/src/settings/Registry.ts +796 -0
- package/src/settings/Value.ts +243 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import * as Duration from "effect/Duration"
|
|
2
|
+
import * as Effect from "effect/Effect"
|
|
3
|
+
import * as Schema from "effect/Schema"
|
|
4
|
+
import * as SchemaIssue from "effect/SchemaIssue"
|
|
5
|
+
import * as SchemaTransformation from "effect/SchemaTransformation"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* How a setting's raw string reads. One instance per grammar, shared by every
|
|
9
|
+
* setting of that grammar, so `on` means the same for all toggles. The same
|
|
10
|
+
* decoding applies whatever the source: flag, environment or file.
|
|
11
|
+
*
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export interface SettingValue<A> {
|
|
15
|
+
/** Docs, `config show` and errors print this: "on or off", "a positive integer". */
|
|
16
|
+
readonly grammar: string
|
|
17
|
+
readonly schema: Schema.Codec<A, string>
|
|
18
|
+
/** "unset": an empty string reads as unset. "value": empty means something. */
|
|
19
|
+
readonly empty: "unset" | "value"
|
|
20
|
+
/** A filesystem path: `~` expanded and made absolute when it comes from a file or `config set`. */
|
|
21
|
+
readonly path: boolean
|
|
22
|
+
/** llm4ts runs it as a command (gates, setup, test-one); docs and `config show` say so. */
|
|
23
|
+
readonly runs: boolean
|
|
24
|
+
/** Every value the grammar reads, as it is written; empty when the grammar is open. */
|
|
25
|
+
readonly choices: ReadonlyArray<string>
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface Traits {
|
|
29
|
+
readonly empty?: "unset" | "value"
|
|
30
|
+
readonly path?: boolean
|
|
31
|
+
readonly runs?: boolean
|
|
32
|
+
readonly choices?: ReadonlyArray<string>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const make = <A>(
|
|
36
|
+
grammar: string,
|
|
37
|
+
target: Schema.Codec<A, A>,
|
|
38
|
+
read: (trimmed: string) => A | undefined,
|
|
39
|
+
write: (value: A) => string,
|
|
40
|
+
traits: Traits = {}
|
|
41
|
+
): SettingValue<A> => ({
|
|
42
|
+
grammar,
|
|
43
|
+
schema: Schema.String.pipe(
|
|
44
|
+
Schema.decodeTo(
|
|
45
|
+
target,
|
|
46
|
+
SchemaTransformation.transformEffect({
|
|
47
|
+
decode: (raw: string) => {
|
|
48
|
+
const value = read(raw.trim())
|
|
49
|
+
// The issue never carries the raw text: a secret must not leak through an error.
|
|
50
|
+
return value === undefined
|
|
51
|
+
? Effect.fail(new SchemaIssue.InvalidValue({ message: grammar }))
|
|
52
|
+
: Effect.succeed(value)
|
|
53
|
+
},
|
|
54
|
+
encode: (value: A) => Effect.succeed(write(value))
|
|
55
|
+
})
|
|
56
|
+
)
|
|
57
|
+
),
|
|
58
|
+
empty: traits.empty ?? "unset",
|
|
59
|
+
path: traits.path ?? false,
|
|
60
|
+
runs: traits.runs ?? false,
|
|
61
|
+
choices: traits.choices ?? []
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
const integerAtLeast =
|
|
65
|
+
(minimum: number) =>
|
|
66
|
+
(text: string): number | undefined => {
|
|
67
|
+
if (!/^\d+$/u.test(text)) {
|
|
68
|
+
return undefined
|
|
69
|
+
}
|
|
70
|
+
const value = Number.parseInt(text, 10)
|
|
71
|
+
return Number.isSafeInteger(value) && value >= minimum ? value : undefined
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const nonEmpty = (text: string): string | undefined => (text.length === 0 ? undefined : text)
|
|
75
|
+
|
|
76
|
+
const listOf =
|
|
77
|
+
(separator: string) =>
|
|
78
|
+
(text: string): ReadonlyArray<string> | undefined => {
|
|
79
|
+
const parts = text
|
|
80
|
+
.split(separator)
|
|
81
|
+
.map((part) => part.trim())
|
|
82
|
+
.filter((part) => part.length > 0)
|
|
83
|
+
return parts.length === 0 ? undefined : parts
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const onWords: ReadonlyArray<string> = ["1", "true", "on", "yes"]
|
|
87
|
+
const offWords: ReadonlyArray<string> = ["0", "false", "off", "no"]
|
|
88
|
+
|
|
89
|
+
/** 1|true|on|yes and 0|false|off|no, any case: the one boolean grammar. */
|
|
90
|
+
export const toggle: SettingValue<boolean> = make(
|
|
91
|
+
"on or off",
|
|
92
|
+
Schema.Boolean,
|
|
93
|
+
(text) => {
|
|
94
|
+
const word = text.toLowerCase()
|
|
95
|
+
return onWords.includes(word) ? true : offWords.includes(word) ? false : undefined
|
|
96
|
+
},
|
|
97
|
+
(value) => (value ? "on" : "off"),
|
|
98
|
+
{ choices: ["on", "off"] }
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
/** An integer of at least 1. */
|
|
102
|
+
export const positive: SettingValue<number> = make(
|
|
103
|
+
"a positive integer",
|
|
104
|
+
Schema.Number,
|
|
105
|
+
integerAtLeast(1),
|
|
106
|
+
String
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
/** An integer of at least 0; 0 turns the feature off. */
|
|
110
|
+
export const count: SettingValue<number> = make(
|
|
111
|
+
"a whole number, 0 or more",
|
|
112
|
+
Schema.Number,
|
|
113
|
+
integerAtLeast(0),
|
|
114
|
+
String
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
/** Whole seconds, at least 1. */
|
|
118
|
+
export const seconds: SettingValue<Duration.Duration> = make(
|
|
119
|
+
"a positive number of seconds",
|
|
120
|
+
Schema.Duration,
|
|
121
|
+
(text) => {
|
|
122
|
+
const value = integerAtLeast(1)(text)
|
|
123
|
+
return value === undefined ? undefined : Duration.seconds(value)
|
|
124
|
+
},
|
|
125
|
+
(value) => String(Math.round(Duration.toSeconds(value)))
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
/** Minutes above 0; a fraction is allowed (`0.5`). */
|
|
129
|
+
export const minutes: SettingValue<Duration.Duration> = make(
|
|
130
|
+
"a positive number of minutes",
|
|
131
|
+
Schema.Duration,
|
|
132
|
+
(text) => {
|
|
133
|
+
if (!/^(\d+\.?\d*|\.\d+)$/u.test(text)) {
|
|
134
|
+
return undefined
|
|
135
|
+
}
|
|
136
|
+
const value = Number.parseFloat(text)
|
|
137
|
+
return Number.isFinite(value) && value > 0 ? Duration.minutes(value) : undefined
|
|
138
|
+
},
|
|
139
|
+
(value) => String(Duration.toMinutes(value))
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
const spanUnits: Readonly<Record<string, number>> = { s: 1, m: 60, h: 3600 }
|
|
143
|
+
|
|
144
|
+
/** A span such as `90s`, `5m`, `1h`; a bare number is seconds. */
|
|
145
|
+
export const span: SettingValue<Duration.Duration> = make(
|
|
146
|
+
"a span such as 90s, 5m or 1h",
|
|
147
|
+
Schema.Duration,
|
|
148
|
+
(text) => {
|
|
149
|
+
const match = /^(\d+)\s*([smh])?$/iu.exec(text)
|
|
150
|
+
if (match === null) {
|
|
151
|
+
return undefined
|
|
152
|
+
}
|
|
153
|
+
const amount = Number.parseInt(match[1] ?? "", 10)
|
|
154
|
+
const unit = spanUnits[(match[2] ?? "s").toLowerCase()]
|
|
155
|
+
return unit === undefined || !Number.isSafeInteger(amount)
|
|
156
|
+
? undefined
|
|
157
|
+
: Duration.seconds(amount * unit)
|
|
158
|
+
},
|
|
159
|
+
(value) => `${Math.round(Duration.toSeconds(value))}s`
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
/** Non-empty text, trimmed. */
|
|
163
|
+
export const text: SettingValue<string> = make("text", Schema.String, nonEmpty, (value) => value)
|
|
164
|
+
|
|
165
|
+
export const path: SettingValue<string> = make(
|
|
166
|
+
"a path",
|
|
167
|
+
Schema.String,
|
|
168
|
+
nonEmpty,
|
|
169
|
+
(value) => value,
|
|
170
|
+
{ path: true }
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
export const url: SettingValue<string> = make(
|
|
174
|
+
"an http or https URL",
|
|
175
|
+
Schema.String,
|
|
176
|
+
(value) => (/^https?:\/\//iu.test(value) && URL.canParse(value) ? value : undefined),
|
|
177
|
+
(value) => value
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
/** One command line. */
|
|
181
|
+
export const command: SettingValue<string> = make(
|
|
182
|
+
"a command line",
|
|
183
|
+
Schema.String,
|
|
184
|
+
nonEmpty,
|
|
185
|
+
(value) => value,
|
|
186
|
+
{ runs: true }
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
/** One command line, where an empty value means "run nothing" instead of "unset". */
|
|
190
|
+
export const commandOrNone: SettingValue<string> = make(
|
|
191
|
+
"a command line; empty runs none",
|
|
192
|
+
Schema.String,
|
|
193
|
+
(value) => value,
|
|
194
|
+
(value) => value,
|
|
195
|
+
{ runs: true, empty: "value" }
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
/** `cmd; cmd`. */
|
|
199
|
+
export const commands: SettingValue<ReadonlyArray<string>> = make(
|
|
200
|
+
"command lines separated by ;",
|
|
201
|
+
Schema.Array(Schema.String),
|
|
202
|
+
listOf(";"),
|
|
203
|
+
(value) => value.join("; "),
|
|
204
|
+
{ runs: true }
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
/** Comma-separated names. */
|
|
208
|
+
export const names: SettingValue<ReadonlyArray<string>> = make(
|
|
209
|
+
"names separated by commas",
|
|
210
|
+
Schema.Array(Schema.String),
|
|
211
|
+
listOf(","),
|
|
212
|
+
(value) => value.join(",")
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
/** `key=value;key=value`, extra CLI flags for a seat; a bare `key` is a boolean flag. */
|
|
216
|
+
export const cliFlags: SettingValue<Readonly<Record<string, string>>> = make(
|
|
217
|
+
"key=value pairs separated by ;",
|
|
218
|
+
Schema.Record(Schema.String, Schema.String),
|
|
219
|
+
(value) => {
|
|
220
|
+
const entries = (listOf(";")(value) ?? []).map((entry): readonly [string, string] => {
|
|
221
|
+
// The first `=` splits, so a value may itself carry `=`.
|
|
222
|
+
const at = entry.indexOf("=")
|
|
223
|
+
return at < 0 ? [entry, ""] : [entry.slice(0, at).trim(), entry.slice(at + 1).trim()]
|
|
224
|
+
})
|
|
225
|
+
return entries.length === 0 || entries.some(([key]) => key.length === 0)
|
|
226
|
+
? undefined
|
|
227
|
+
: Object.fromEntries(entries)
|
|
228
|
+
},
|
|
229
|
+
(value) =>
|
|
230
|
+
Object.entries(value)
|
|
231
|
+
.map(([key, flag]) => (flag.length === 0 ? key : `${key}=${flag}`))
|
|
232
|
+
.join(";")
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
/** One of `choices`, any case; choices are written in lower case. */
|
|
236
|
+
export const oneOf = <const L extends ReadonlyArray<string>>(choices: L): SettingValue<L[number]> =>
|
|
237
|
+
make<L[number]>(
|
|
238
|
+
choices.join(", ").replace(/, ([^,]*)$/u, " or $1"),
|
|
239
|
+
Schema.Literals(choices),
|
|
240
|
+
(value) => choices.find((choice) => choice === value.toLowerCase()),
|
|
241
|
+
(value) => value,
|
|
242
|
+
{ choices }
|
|
243
|
+
)
|