-
Notifications
You must be signed in to change notification settings - Fork 1
E 661 improve unknown command error message #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nainglinnkhant
merged 12 commits into
main
from
e-661-improve-unknown-command-error-message
Oct 3, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
09db054
chore: update sdk and rename to stores
nainglinnkhant d669825
chore: update file/folder names
nainglinnkhant ff44c19
chore: add changeset and update changelog
nainglinnkhant 0c2dae0
docs: update changelog
nainglinnkhant 36beec3
feat: improve unknown command error message
nainglinnkhant 11f43e8
feat: notify users when a newer version is available
nainglinnkhant 00af10a
test: add tests for command suggestions
nainglinnkhant 7b82ce4
Merge branch 'main' into e-661-improve-unknown-command-error-message
nainglinnkhant 3e1c802
chore: remove MXBAI_NO_UPDATE_CHECK
nainglinnkhant be0bf04
fix: add adjustments
nainglinnkhant 3e88e67
Merge branch 'main' into e-661-improve-unknown-command-error-message
nainglinnkhant c5cf811
chore(release): add changeset
nainglinnkhant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@mixedbread/cli": minor | ||
| --- | ||
|
|
||
| - Improve unkown command error message | ||
| - Notify users when a newer version is released |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import chalk from "chalk"; | ||
|
|
||
| function levenshteinDistance(a: string, b: string): number { | ||
| const matrix: number[][] = []; | ||
|
|
||
| for (let i = 0; i <= b.length; i++) { | ||
| matrix[i] = [i]; | ||
| } | ||
|
|
||
| for (let j = 0; j <= a.length; j++) { | ||
| matrix[0][j] = j; | ||
| } | ||
|
|
||
| for (let i = 1; i <= b.length; i++) { | ||
| for (let j = 1; j <= a.length; j++) { | ||
| if (b.charAt(i - 1) === a.charAt(j - 1)) { | ||
| matrix[i][j] = matrix[i - 1][j - 1]; | ||
| } else { | ||
| matrix[i][j] = Math.min( | ||
| matrix[i - 1][j - 1] + 1, | ||
| matrix[i][j - 1] + 1, | ||
| matrix[i - 1][j] + 1 | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return matrix[b.length][a.length]; | ||
| } | ||
|
|
||
| export function findSimilarCommands( | ||
| input: string, | ||
| availableCommands: string[], | ||
| threshold = 3 | ||
| ): string[] { | ||
| const suggestions = availableCommands | ||
| .map((cmd) => ({ | ||
| command: cmd, | ||
| distance: levenshteinDistance(input.toLowerCase(), cmd.toLowerCase()), | ||
| })) | ||
| .filter((item) => item.distance <= threshold) | ||
| .sort((a, b) => a.distance - b.distance) | ||
| .map((item) => item.command); | ||
|
|
||
| return suggestions; | ||
| } | ||
|
|
||
| export function formatUnknownCommandError( | ||
| unknownCommand: string, | ||
| availableCommands: string[] | ||
| ): string { | ||
| let message = `${chalk.red("\n✗")} Unknown command: ${chalk.yellow(unknownCommand)}`; | ||
|
|
||
| if (unknownCommand === "vs") { | ||
| message += `\n\n${chalk.yellow("⚠")} ${chalk.green("vs")} command was deprecated and renamed to ${chalk.green("store")} since ${chalk.bold("v2.0.0")}.`; | ||
| message += `\n\nSee: https://github.com/mixedbread-ai/openbread/blob/main/packages/cli/CHANGELOG.md`; | ||
| return message; | ||
| } | ||
|
|
||
| const suggestions = findSimilarCommands(unknownCommand, availableCommands); | ||
|
|
||
| if (suggestions.length > 0) { | ||
| message += `\n\n${chalk.cyan("Did you mean one of these?")}`; | ||
| for (const suggestion of suggestions) { | ||
| message += `\n ${chalk.green(suggestion)}`; | ||
| } | ||
| } else { | ||
| message += `\n\n${chalk.cyan("Available commands:")}`; | ||
| for (const cmd of availableCommands) { | ||
| message += `\n ${chalk.green(cmd)}`; | ||
| } | ||
| } | ||
|
|
||
| message += `\n\n${chalk.gray("Run 'mxbai --help' for more information.")}`; | ||
|
|
||
| return message; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; | ||
| import { homedir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import chalk from "chalk"; | ||
|
|
||
| interface UpdateCheckCache { | ||
| lastCheck: number; | ||
| latestVersion: string; | ||
| } | ||
|
|
||
| const CACHE_DIR = join(homedir(), ".config", "mxbai"); | ||
| const CACHE_FILE = join(CACHE_DIR, "update-check.json"); | ||
| const CHECK_INTERVAL = 24 * 60 * 60 * 1000; // 24 hours | ||
| const REGISTRY_URL = "https://registry.npmjs.org/@mixedbread/cli/latest"; | ||
| const TIMEOUT = 2000; | ||
|
|
||
| /** | ||
| * Simple semantic version comparison | ||
| * Returns true if versionA is less than versionB | ||
| * Ignores pre-release versions (only compares major.minor.patch) | ||
| */ | ||
| function isVersionLessThan(versionA: string, versionB: string): boolean { | ||
| const parseVersion = (v: string) => { | ||
| const cleaned = v.replace(/^v/, ""); | ||
| const mainVersion = cleaned.split("-")[0]; | ||
| const [major, minor, patch] = mainVersion | ||
| .split(".") | ||
| .map((p) => parseInt(p, 10) || 0); | ||
| return { major, minor, patch }; | ||
| }; | ||
|
|
||
| const a = parseVersion(versionA); | ||
| const b = parseVersion(versionB); | ||
|
|
||
| // Compare major.minor.patch | ||
| if (a.major !== b.major) return a.major < b.major; | ||
| if (a.minor !== b.minor) return a.minor < b.minor; | ||
| return a.patch < b.patch; | ||
| } | ||
|
|
||
| async function fetchLatestVersion(): Promise<string> { | ||
| const response = await fetch(REGISTRY_URL, { | ||
| headers: { Accept: "application/json" }, | ||
| signal: AbortSignal.timeout(TIMEOUT), | ||
|
nainglinnkhant marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`Failed to fetch latest version: ${response.statusText}`); | ||
| } | ||
|
|
||
| const json = await response.json(); | ||
| return json.version; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| function readCache(): UpdateCheckCache | null { | ||
| if (!existsSync(CACHE_FILE)) return null; | ||
| const data = readFileSync(CACHE_FILE, "utf-8"); | ||
| return JSON.parse(data); | ||
| } | ||
|
|
||
| function writeCache(cache: UpdateCheckCache): void { | ||
| if (!existsSync(CACHE_DIR)) { | ||
| mkdirSync(CACHE_DIR, { recursive: true }); | ||
| } | ||
| writeFileSync(CACHE_FILE, JSON.stringify(cache, null, 2)); | ||
| } | ||
|
|
||
| function formatUpdateBanner( | ||
| currentVersion: string, | ||
| latestVersion: string | ||
| ): string { | ||
| const contentWidth = 92; // Width of actual content | ||
| const horizontalPadding = " "; // 2 spaces on each side | ||
|
|
||
| const pad = (text: string) => { | ||
| const stripped = stripAnsi(text); | ||
| const padding = contentWidth - stripped.length; | ||
| return text + " ".repeat(Math.max(0, padding)); | ||
| }; | ||
|
|
||
| const totalWidth = contentWidth + horizontalPadding.length * 2; | ||
| const border = "─".repeat(totalWidth); | ||
|
|
||
| return [ | ||
| `╭${border}╮`, | ||
| `│${horizontalPadding}${pad("")}${horizontalPadding}│`, | ||
| `│${horizontalPadding}${pad(chalk.bold(`Update available: ${chalk.red(currentVersion)} → ${chalk.green(latestVersion)}`))}${horizontalPadding}│`, | ||
| `│${horizontalPadding}${pad("")}${horizontalPadding}│`, | ||
| `│${horizontalPadding}${pad(`Run: ${chalk.cyan("npm install -g @mixedbread/cli@latest")}`)}${horizontalPadding}│`, | ||
| `│${horizontalPadding}${pad("")}${horizontalPadding}│`, | ||
| `│${horizontalPadding}${pad(`Changelog: ${chalk.gray("https://github.com/mixedbread-ai/openbread/blob/main/packages/cli/CHANGELOG.md")}`)}${horizontalPadding}│`, | ||
| `│${horizontalPadding}${pad("")}${horizontalPadding}│`, | ||
| `╰${border}╯`, | ||
| ].join("\n"); | ||
| } | ||
|
|
||
| /** | ||
| * Strip ANSI escape codes for length calculation | ||
| * Handles all ANSI escape sequences including color codes | ||
| */ | ||
| function stripAnsi(str: string): string { | ||
| // biome-ignore lint/suspicious/noControlCharactersInRegex: intentional escape sequence for ANSI codes | ||
| return str.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, ""); | ||
| } | ||
|
|
||
| /** | ||
| * Check for updates and display notification if available | ||
| * This runs asynchronously and doesn't block command execution | ||
| */ | ||
| export async function checkForUpdates(currentVersion: string): Promise<void> { | ||
| // Skip in CI or non-TTY environments | ||
| if (process.env.CI || !process.stdout.isTTY) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const cache = readCache(); | ||
| const now = Date.now(); | ||
|
|
||
| // Check if we need to fetch fresh data | ||
| let latestVersion: string; | ||
| if (!cache || now - cache.lastCheck > CHECK_INTERVAL) { | ||
| latestVersion = await fetchLatestVersion(); | ||
| writeCache({ lastCheck: now, latestVersion }); | ||
| } else { | ||
| latestVersion = cache.latestVersion; | ||
| } | ||
|
|
||
| if (isVersionLessThan(currentVersion, latestVersion)) { | ||
| console.log(formatUpdateBanner(currentVersion, latestVersion)); | ||
| console.log(); // Add spacing after banner | ||
| } | ||
| } catch { | ||
| // Silently fail - update checks should never break the CLI | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.