forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
280 lines (250 loc) · 10.1 KB
/
Copy pathconfig.ts
File metadata and controls
280 lines (250 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { EOL } from "os"
import path from "path"
import { Effect } from "effect"
import * as prompts from "@clack/prompts"
import type { Argv } from "yargs"
import { AppFileSystem } from "@opencode-ai/core/filesystem"
import { Flag } from "@opencode-ai/core/flag/flag"
import { Global } from "@opencode-ai/core/global"
import { ConfigPaths } from "@/config/paths"
import { ConfigRepair } from "@/config/repair"
import { cmd } from "./cmd"
import { effectCmd, fail } from "../effect-cmd"
type Scope = "global" | "project" | "custom"
type ConfigFile = {
path: string
scope: Scope
}
type ScopeArgs = {
global?: boolean
project?: boolean
}
export const ConfigCommand = cmd({
command: "config",
describe: "manage configuration files",
builder: (yargs) =>
yargs.command(ConfigCheckCommand).command(ConfigRepairCommand).command(ConfigResetCommand).demandCommand(),
async handler() {},
})
const ConfigCheckCommand = effectCmd({
command: "check",
describe: "validate configuration files",
builder: (yargs) => scopeOptions(yargs),
instance: false,
handler: Effect.fn("Cli.config.check")(function* (args: ScopeArgs) {
yield* runConfigInspection({ repair: false, scopes: selectedScopes(args) })
}),
})
const ConfigRepairCommand = effectCmd({
command: "repair",
describe: "remove safely repairable configuration errors",
builder: (yargs) => scopeOptions(yargs),
instance: false,
handler: Effect.fn("Cli.config.repair")(function* (args: ScopeArgs) {
yield* runConfigInspection({ repair: true, scopes: selectedScopes(args) })
}),
})
const ConfigResetCommand = effectCmd({
command: "reset",
describe: "back up and reset config files to an empty config",
builder: (yargs) =>
scopeOptions(yargs)
.option("yes", {
describe: "skip confirmation prompt",
type: "boolean",
})
.option("all", {
describe: "reset valid config files too",
type: "boolean",
}),
instance: false,
handler: Effect.fn("Cli.config.reset")(function* (args: ScopeArgs & { yes?: boolean; all?: boolean }) {
const files = (yield* collectConfigFiles(selectedScopes(args))).filter((file) => file.scope !== "custom")
if (!files.length) {
process.stdout.write("No config files found to reset." + EOL)
return
}
const targets = yield* Effect.forEach(files, (file) =>
Effect.gen(function* () {
const text = (yield* AppFileSystem.Service.use((fs) => fs.readFileStringSafe(file.path)).pipe(Effect.orDie)) ?? ""
return {
file,
text,
inspection: yield* ConfigRepair.inspect(text, file.path),
}
}),
).pipe(Effect.map((items) => (args.all ? items : items.filter((item) => !item.inspection.valid))))
if (!targets.length) {
process.stdout.write("No invalid config files found. Use `opencode config reset --all` to reset valid config too." + EOL)
return
}
process.stdout.write(
`The following config ${targets.length === 1 ? "file" : "files"} will be backed up and reset:${EOL}`,
)
for (const target of targets) {
process.stdout.write(` - ${formatFile(target.file)}${target.inspection.valid ? " (valid)" : " (invalid)"}${EOL}`)
}
if (!args.yes) {
if (!process.stdin.isTTY) {
return yield* fail("Refusing to reset config without confirmation. Re-run with `--yes` to apply.")
}
const confirm = yield* Effect.promise(() =>
prompts.confirm({
message: "Reset these config files?",
initialValue: false,
}),
)
if (prompts.isCancel(confirm) || !confirm) {
prompts.outro("Cancelled")
return
}
}
const fs = yield* AppFileSystem.Service
for (const target of targets) {
const backup = `${target.file.path}.${new Date().toISOString().replace(/[:.]/g, "-")}.bak`
yield* fs.writeFileString(backup, target.text).pipe(Effect.orDie)
yield* fs
.writeFileString(target.file.path, JSON.stringify({ $schema: "https://opencode.ai/config.json" }, null, 2) + EOL)
.pipe(Effect.orDie)
process.stdout.write(`${formatFile(target.file)}: reset (backup: ${backup})${EOL}`)
process.stdout.write(
` Tip: start opencode again, then ask the agent to repair useful settings from ${backup}.${EOL}`,
)
process.stdout.write(` Run: ${restoreCommand(backup)}${EOL}`)
}
}),
})
const runConfigInspection = Effect.fn("Cli.config.inspect")(function* (input: { repair: boolean; scopes: Scope[] }) {
const fs = yield* AppFileSystem.Service
const files = yield* collectConfigFiles(input.scopes)
if (!files.length) {
process.stdout.write("No config files found" + EOL)
return
}
const results = yield* Effect.forEach(files, (file) =>
Effect.gen(function* () {
const before = (yield* fs.readFileStringSafe(file.path).pipe(Effect.orDie)) ?? ""
const inspection = yield* ConfigRepair.inspect(before, file.path)
if (!input.repair || !inspection.fixPaths.length) return { file, inspection, fixed: false, fixedPaths: [] }
const after = ConfigRepair.applyFixes(before, inspection)
const backup = `${file.path}.${new Date().toISOString().replace(/[:.]/g, "-")}.bak`
if (after !== before) {
yield* fs.writeFileString(backup, before).pipe(Effect.orDie)
yield* fs.writeFileString(file.path, after).pipe(Effect.orDie)
}
return {
file,
inspection: yield* ConfigRepair.inspect(after, file.path),
fixed: after !== before,
fixedPaths: inspection.fixPaths,
backup,
}
}),
)
for (const result of results) {
if (result.inspection.valid && !result.fixed) {
process.stdout.write(`${formatFile(result.file)}: ok${EOL}`)
continue
}
if (result.inspection.valid) {
process.stdout.write(`${formatFile(result.file)}: repaired ${result.fixedPaths.map(formatPath).join(", ")}${EOL}`)
process.stdout.write(` backup: ${result.backup}${EOL}`)
continue
}
process.stdout.write(`${formatFile(result.file)}: invalid${EOL}`)
if (result.inspection.message) process.stdout.write(` ${result.inspection.message}${EOL}`)
for (const issue of result.inspection.issues) {
process.stdout.write(` - ${issue.message}${issue.path.length ? ` (${issue.path.join(".")})` : ""}${EOL}`)
}
if (result.inspection.fixPaths.length && !input.repair) {
process.stdout.write(` repairable: ${result.inspection.fixPaths.map(formatPath).join(", ")}${EOL}`)
process.stdout.write(" run: opencode config repair" + EOL)
continue
}
if (result.inspection.candidatePaths.length) {
process.stdout.write(" automatic repair skipped because removing the reported fields did not make the file valid" + EOL)
process.stdout.write(recoveryHint(result.file))
continue
}
process.stdout.write(" automatic repair is not available." + EOL)
process.stdout.write(recoveryHint(result.file))
}
if (results.every((result) => result.inspection.valid)) process.stdout.write(`Config files are valid (${results.length} checked).${EOL}`)
if (results.some((result) => !result.inspection.valid)) process.exitCode = 1
})
const collectConfigFiles = Effect.fn("Cli.config.files")(function* (scopes: Scope[]) {
const fs = yield* AppFileSystem.Service
const directories = yield* ConfigPaths.directories(process.cwd()).pipe(Effect.orDie)
const projectFiles = !Flag.OPENCODE_DISABLE_PROJECT_CONFIG
? yield* ConfigPaths.files("opencode", process.cwd()).pipe(Effect.orDie)
: []
const configDirFiles = directories
.filter((dir) => dir.endsWith(".opencode") || dir === Flag.OPENCODE_CONFIG_DIR)
.flatMap((dir) =>
ConfigPaths.fileInDirectory(dir, "opencode").map((file) => ({ path: file, scope: "project" as const })),
)
const candidates = dedupeFiles([
...(scopes.includes("global")
? [
{ path: path.join(Global.Path.config, "config.json"), scope: "global" as const },
...ConfigPaths.fileInDirectory(Global.Path.config, "opencode").map((file) => ({
path: file,
scope: "global" as const,
})),
]
: []),
...(scopes.includes("project")
? [
...projectFiles.map((file) => ({ path: file, scope: "project" as const })),
...configDirFiles,
]
: []),
...(scopes.includes("custom") && Flag.OPENCODE_CONFIG
? [{ path: Flag.OPENCODE_CONFIG, scope: "custom" as const }]
: []),
])
return (
yield* Effect.forEach(candidates, (file) =>
fs.existsSafe(file.path).pipe(Effect.map((exists) => (exists ? file : undefined))),
)
).filter((file): file is ConfigFile => file !== undefined)
})
function scopeOptions(yargs: Argv) {
return yargs
.option("global", {
describe: "only use global config",
type: "boolean",
})
.option("project", {
describe: "only use project config",
type: "boolean",
})
}
function selectedScopes(args: ScopeArgs): Scope[] {
if (args.global && !args.project) return ["global"]
if (args.project && !args.global) return ["project"]
return ["global", "project", "custom"]
}
function dedupeFiles(files: ConfigFile[]) {
return Array.from(new Map(files.map((file) => [file.path, file])).values())
}
function formatFile(file: ConfigFile) {
return `${file.path} (${file.scope})`
}
function formatPath(path: string[]) {
return path.join(".")
}
function recoveryHint(file: ConfigFile) {
if (file.scope === "custom") {
return ` Safest recovery: move this file aside, start opencode, then ask the agent to inspect the backup and restore the settings you still want.${EOL}`
}
return ` Safest recovery: run \`opencode config reset --${file.scope} --yes\`, then run the printed \`opencode run\` command to restore wanted settings from the backup.${EOL}`
}
function restoreCommand(backup: string) {
return `opencode run ${shellQuote(
`I reset my opencode config because it was invalid. Please inspect the backup at ${backup}, repair the useful settings into the current opencode config, remove or correct invalid fields, and run opencode config check when you are done. Explain what you changed.`,
)}`
}
function shellQuote(value: string) {
return `'${value.replace(/'/g, "'\\''")}'`
}