cloudaxe-opencode/packages/opencode/src/cli/error.ts

43 lines
1.8 KiB
TypeScript
Raw Normal View History

2025-10-30 15:56:30 +00:00
import { ConfigMarkdown } from "@/config/markdown"
2025-06-20 04:57:28 +00:00
import { Config } from "../config/config"
2025-06-23 01:10:05 +00:00
import { MCP } from "../mcp"
import { Provider } from "../provider/provider"
import { UI } from "./ui"
2025-06-20 04:57:28 +00:00
export function FormatError(input: unknown) {
2025-06-23 01:10:05 +00:00
if (MCP.Failed.isInstance(input))
return `MCP server "${input.data.name}" failed. Note, opencode does not support MCP authentication yet.`
if (Provider.ModelNotFoundError.isInstance(input)) {
feat(cli): suggest closest provider/model on not found ("Did you mean…")\n\nSummary\n- Add fuzzy suggestions to ProviderModelNotFoundError with up to 3 candidates\n- Normalize punctuation (e.g., 4.5 vs 4-5) and case to better match common typos\n- Support model-only input (no provider) by searching across all providers\n- Enhance CLI error formatter to display suggestions when present\n\nImplementation\n- provider.ts: use fuzzysort; add normalization by stripping non-alphanumerics; search by key for robust matches\n- provider.ts: when provider is unknown and model is empty, treat token as unqualified model and search across all providers' models; otherwise suggest provider matches\n- error.ts: print "Did you mean: <provider/model>, …" when suggestions exist\n\nExamples\n1) Typo in model ID\n $ bun run ./src/index.ts run --model anthropic/claude-haiu-4-5 "hi"\n Error: Model not found: anthropic/claude-haiu-4-5\n Did you mean: anthropic/claude-haiku-4-5, anthropic/claude-haiku-4-5-20251001\n Try: zai-coding-plan/glm-4.5-flash zai-coding-plan/glm-4.5 zai-coding-plan/glm-4.5-air zai-coding-plan/glm-4.5v zai-coding-plan/glm-4.6 opencode/big-pickle opencode/grok-code anthropic/claude-opus-4-0 anthropic/claude-3-5-sonnet-20241022 anthropic/claude-opus-4-1 anthropic/claude-haiku-4-5 anthropic/claude-3-5-sonnet-20240620 anthropic/claude-3-5-haiku-latest anthropic/claude-3-opus-20240229 anthropic/claude-sonnet-4-5 anthropic/claude-sonnet-4-5-20250929 anthropic/claude-sonnet-4-20250514 anthropic/claude-opus-4-20250514 anthropic/claude-3-5-haiku-20241022 anthropic/claude-3-haiku-20240307 anthropic/claude-3-7-sonnet-20250219 anthropic/claude-3-7-sonnet-latest anthropic/claude-sonnet-4-0 anthropic/claude-opus-4-1-20250805 anthropic/claude-3-sonnet-20240229 anthropic/claude-haiku-4-5-20251001 openai/gpt-4.1-nano openai/text-embedding-3-small openai/gpt-4 openai/o1-pro openai/gpt-4o-2024-05-13 openai/gpt-4o-2024-08-06 openai/gpt-4.1-mini openai/o3-deep-research openai/gpt-3.5-turbo openai/text-embedding-3-large openai/gpt-4-turbo openai/o1-preview openai/o3-mini openai/codex-mini-latest openai/gpt-5-nano openai/gpt-5-codex openai/gpt-4o openai/gpt-4.1 openai/o4-mini openai/o1 openai/gpt-5-mini openai/o1-mini openai/text-embedding-ada-002 openai/o3-pro openai/gpt-4o-2024-11-20 openai/o3 openai/o4-mini-deep-research openai/gpt-4o-mini openai/gpt-5 openai/gpt-5-pro to list available models\n Or check your config (opencode.json) provider/model names\n\n2) Dot vs dash (punctuation normalization)\n $ bun run ./src/index.ts run --model anthropic/claude-haiku-4.5 "hi"\n Error: Model not found: anthropic/claude-haiku-4.5\n Did you mean: anthropic/claude-haiku-4-5, anthropic/claude-haiku-4-5-20251001\n Try: zai-coding-plan/glm-4.5-flash zai-coding-plan/glm-4.5 zai-coding-plan/glm-4.5-air zai-coding-plan/glm-4.5v zai-coding-plan/glm-4.6 opencode/big-pickle opencode/grok-code anthropic/claude-opus-4-0 anthropic/claude-3-5-sonnet-20241022 anthropic/claude-opus-4-1 anthropic/claude-haiku-4-5 anthropic/claude-3-5-sonnet-20240620 anthropic/claude-3-5-haiku-latest anthropic/claude-3-opus-20240229 anthropic/claude-sonnet-4-5 anthropic/claude-sonnet-4-5-20250929 anthropic/claude-sonnet-4-20250514 anthropic/claude-opus-4-20250514 anthropic/claude-3-5-haiku-20241022 anthropic/claude-3-haiku-20240307 anthropic/claude-3-7-sonnet-20250219 anthropic/claude-3-7-sonnet-latest anthropic/claude-sonnet-4-0 anthropic/claude-opus-4-1-20250805 anthropic/claude-3-sonnet-20240229 anthropic/claude-haiku-4-5-20251001 openai/gpt-4.1-nano openai/text-embedding-3-small openai/gpt-4 openai/o1-pro openai/gpt-4o-2024-05-13 openai/gpt-4o-2024-08-06 openai/gpt-4.1-mini openai/o3-deep-research openai/gpt-3.5-turbo openai/text-embedding-3-large openai/gpt-4-turbo openai/o1-preview openai/o3-mini openai/codex-mini-latest openai/gpt-5-nano openai/gpt-5-codex openai/gpt-4o openai/gpt-4.1 openai/o4-mini openai/o1 openai/gpt-5-mini openai/o1-mini openai/text-embedding-ada-002 openai/o3-pro openai/gpt-4o-2024-11-20 openai/o3 openai/o4-mini-deep-research openai/gpt-4o-mini openai/gpt-5 openai/gpt-5-pro to list available models\n Or check your config (opencode.json) provider/model names\n\n3) Missing provider (model-only input)\n $ bun run ./src/index.ts run --model big-pickle "hi"\n Error: Model not found: big-pickle/\n Did you mean: opencode/big-pickle\n\n4) Correct model after suggestion\n $ bun run ./src/index.ts run --model opencode/big-pickle "hi"\n Hi! How can I help you with your opencode project today?\n\nNotes\n- Suggestions are hints only; behavior is unchanged (no auto-selection).\n- This runs locally as part of the CLI error path; performance impact is negligible (small in-memory scans).
2025-11-12 15:41:38 +00:00
const { providerID, modelID, suggestions } = input.data
return [
`Model not found: ${providerID}/${modelID}`,
feat(cli): suggest closest provider/model on not found ("Did you mean…")\n\nSummary\n- Add fuzzy suggestions to ProviderModelNotFoundError with up to 3 candidates\n- Normalize punctuation (e.g., 4.5 vs 4-5) and case to better match common typos\n- Support model-only input (no provider) by searching across all providers\n- Enhance CLI error formatter to display suggestions when present\n\nImplementation\n- provider.ts: use fuzzysort; add normalization by stripping non-alphanumerics; search by key for robust matches\n- provider.ts: when provider is unknown and model is empty, treat token as unqualified model and search across all providers' models; otherwise suggest provider matches\n- error.ts: print "Did you mean: <provider/model>, …" when suggestions exist\n\nExamples\n1) Typo in model ID\n $ bun run ./src/index.ts run --model anthropic/claude-haiu-4-5 "hi"\n Error: Model not found: anthropic/claude-haiu-4-5\n Did you mean: anthropic/claude-haiku-4-5, anthropic/claude-haiku-4-5-20251001\n Try: zai-coding-plan/glm-4.5-flash zai-coding-plan/glm-4.5 zai-coding-plan/glm-4.5-air zai-coding-plan/glm-4.5v zai-coding-plan/glm-4.6 opencode/big-pickle opencode/grok-code anthropic/claude-opus-4-0 anthropic/claude-3-5-sonnet-20241022 anthropic/claude-opus-4-1 anthropic/claude-haiku-4-5 anthropic/claude-3-5-sonnet-20240620 anthropic/claude-3-5-haiku-latest anthropic/claude-3-opus-20240229 anthropic/claude-sonnet-4-5 anthropic/claude-sonnet-4-5-20250929 anthropic/claude-sonnet-4-20250514 anthropic/claude-opus-4-20250514 anthropic/claude-3-5-haiku-20241022 anthropic/claude-3-haiku-20240307 anthropic/claude-3-7-sonnet-20250219 anthropic/claude-3-7-sonnet-latest anthropic/claude-sonnet-4-0 anthropic/claude-opus-4-1-20250805 anthropic/claude-3-sonnet-20240229 anthropic/claude-haiku-4-5-20251001 openai/gpt-4.1-nano openai/text-embedding-3-small openai/gpt-4 openai/o1-pro openai/gpt-4o-2024-05-13 openai/gpt-4o-2024-08-06 openai/gpt-4.1-mini openai/o3-deep-research openai/gpt-3.5-turbo openai/text-embedding-3-large openai/gpt-4-turbo openai/o1-preview openai/o3-mini openai/codex-mini-latest openai/gpt-5-nano openai/gpt-5-codex openai/gpt-4o openai/gpt-4.1 openai/o4-mini openai/o1 openai/gpt-5-mini openai/o1-mini openai/text-embedding-ada-002 openai/o3-pro openai/gpt-4o-2024-11-20 openai/o3 openai/o4-mini-deep-research openai/gpt-4o-mini openai/gpt-5 openai/gpt-5-pro to list available models\n Or check your config (opencode.json) provider/model names\n\n2) Dot vs dash (punctuation normalization)\n $ bun run ./src/index.ts run --model anthropic/claude-haiku-4.5 "hi"\n Error: Model not found: anthropic/claude-haiku-4.5\n Did you mean: anthropic/claude-haiku-4-5, anthropic/claude-haiku-4-5-20251001\n Try: zai-coding-plan/glm-4.5-flash zai-coding-plan/glm-4.5 zai-coding-plan/glm-4.5-air zai-coding-plan/glm-4.5v zai-coding-plan/glm-4.6 opencode/big-pickle opencode/grok-code anthropic/claude-opus-4-0 anthropic/claude-3-5-sonnet-20241022 anthropic/claude-opus-4-1 anthropic/claude-haiku-4-5 anthropic/claude-3-5-sonnet-20240620 anthropic/claude-3-5-haiku-latest anthropic/claude-3-opus-20240229 anthropic/claude-sonnet-4-5 anthropic/claude-sonnet-4-5-20250929 anthropic/claude-sonnet-4-20250514 anthropic/claude-opus-4-20250514 anthropic/claude-3-5-haiku-20241022 anthropic/claude-3-haiku-20240307 anthropic/claude-3-7-sonnet-20250219 anthropic/claude-3-7-sonnet-latest anthropic/claude-sonnet-4-0 anthropic/claude-opus-4-1-20250805 anthropic/claude-3-sonnet-20240229 anthropic/claude-haiku-4-5-20251001 openai/gpt-4.1-nano openai/text-embedding-3-small openai/gpt-4 openai/o1-pro openai/gpt-4o-2024-05-13 openai/gpt-4o-2024-08-06 openai/gpt-4.1-mini openai/o3-deep-research openai/gpt-3.5-turbo openai/text-embedding-3-large openai/gpt-4-turbo openai/o1-preview openai/o3-mini openai/codex-mini-latest openai/gpt-5-nano openai/gpt-5-codex openai/gpt-4o openai/gpt-4.1 openai/o4-mini openai/o1 openai/gpt-5-mini openai/o1-mini openai/text-embedding-ada-002 openai/o3-pro openai/gpt-4o-2024-11-20 openai/o3 openai/o4-mini-deep-research openai/gpt-4o-mini openai/gpt-5 openai/gpt-5-pro to list available models\n Or check your config (opencode.json) provider/model names\n\n3) Missing provider (model-only input)\n $ bun run ./src/index.ts run --model big-pickle "hi"\n Error: Model not found: big-pickle/\n Did you mean: opencode/big-pickle\n\n4) Correct model after suggestion\n $ bun run ./src/index.ts run --model opencode/big-pickle "hi"\n Hi! How can I help you with your opencode project today?\n\nNotes\n- Suggestions are hints only; behavior is unchanged (no auto-selection).\n- This runs locally as part of the CLI error path; performance impact is negligible (small in-memory scans).
2025-11-12 15:41:38 +00:00
...(Array.isArray(suggestions) && suggestions.length
? ["Did you mean: " + suggestions.join(", ")]
: []),
`Try: \`opencode models\` to list available models`,
`Or check your config (opencode.json) provider/model names`,
].join("\n")
}
if (Provider.InitError.isInstance(input)) {
return `Failed to initialize provider "${input.data.providerID}". Check credentials and configuration.`
}
if (Config.JsonError.isInstance(input)) {
return (
`Config file at ${input.data.path} is not valid JSON(C)` + (input.data.message ? `: ${input.data.message}` : "")
)
}
2025-10-17 19:34:37 +00:00
if (Config.ConfigDirectoryTypoError.isInstance(input)) {
return `Directory "${input.data.dir}" in ${input.data.path} is not valid. Use "${input.data.suggestion}" instead. This is a common typo.`
}
2025-10-30 15:56:30 +00:00
if (ConfigMarkdown.FrontmatterError.isInstance(input)) {
return `Failed to parse frontmatter in ${input.data.path}:\n${input.data.message}`
}
2025-06-20 04:57:28 +00:00
if (Config.InvalidError.isInstance(input))
return [
`Config file at ${input.data.path} is invalid` + (input.data.message ? `: ${input.data.message}` : ""),
...(input.data.issues?.map((issue) => "↳ " + issue.message + " " + issue.path.join(".")) ?? []),
2025-06-20 04:57:28 +00:00
].join("\n")
if (UI.CancelledError.isInstance(input)) return ""
2025-06-20 04:57:28 +00:00
}