| 3 | type ConfigIssue = { message: string; path: string[] } |
| 4 | |
| 5 | export function cliErrorMessage(input: unknown): string | undefined { |
| 6 | if (input instanceof Error && isRecord(input.cause) && "body" in input.cause) { |
| 7 | const formatted = cliErrorMessage(input.cause.body) |
| 8 | if (formatted) return formatted |
| 9 | } |
| 10 | |
| 11 | if (tagged(input, "CliError")) { |
| 12 | if (typeof input.exitCode === "number") process.exitCode = input.exitCode |
| 13 | return field(input, "message") ?? "" |
| 14 | } |
| 15 | if (tagged(input, "AccountServiceError") || tagged(input, "AccountTransportError")) { |
| 16 | return field(input, "message") ?? "" |
| 17 | } |
| 18 | |
| 19 | const model = configData(input, "ProviderModelNotFoundError") |
| 20 | if (model) { |
| 21 | const suggestions = Array.isArray(model.suggestions) |
| 22 | ? model.suggestions.filter((item): item is string => typeof item === "string") |
| 23 | : [] |
| 24 | return [ |
| 25 | `Model not found: ${field(model, "providerID")}/${field(model, "modelID")}`, |
| 26 | ...(suggestions.length ? ["Did you mean: " + suggestions.join(", ")] : []), |
| 27 | "Try: `opencode models` to list available models", |
| 28 | "Or check your config (opencode.json) provider/model names", |
| 29 | ].join("\n") |
| 30 | } |
| 31 | |
| 32 | const provider = configData(input, "ProviderInitError") |
| 33 | if (provider) |
| 34 | return `Failed to initialize provider "${field(provider, "providerID")}". Check credentials and configuration.` |
| 35 | |
| 36 | const json = configData(input, "ConfigJsonError") |
| 37 | if (json) { |
| 38 | const message = field(json, "message") |
| 39 | return `Config file at ${field(json, "path")} is not valid JSON(C)` + (message ? `: ${message}` : "") |
| 40 | } |
| 41 | |
| 42 | const directory = configData(input, "ConfigDirectoryTypoError") |
| 43 | if (directory) { |
| 44 | return `Directory "${field(directory, "dir")}" in ${field(directory, "path")} is not valid. Rename the directory to "${field(directory, "suggestion")}" or remove it. This is a common typo.` |
| 45 | } |
| 46 | |
| 47 | const frontmatter = configData(input, "ConfigFrontmatterError") |
| 48 | if (frontmatter) return field(frontmatter, "message") ?? "" |
| 49 | |
| 50 | const invalid = configData(input, "ConfigInvalidError") |
| 51 | if (invalid) { |
| 52 | const path = field(invalid, "path") |
| 53 | const message = field(invalid, "message") |
| 54 | const issues = Array.isArray(invalid.issues) |
| 55 | ? invalid.issues.filter((issue): issue is ConfigIssue => { |
| 56 | return ( |
| 57 | isRecord(issue) && |
| 58 | typeof issue.message === "string" && |
| 59 | Array.isArray(issue.path) && |
| 60 | issue.path.every((item) => typeof item === "string") |
| 61 | ) |
| 62 | }) |