From 5af71e0405750218693f19f19f08e144e67b8873 Mon Sep 17 00:00:00 2001 From: Hugo Dutka Date: Wed, 15 Oct 2025 20:27:57 +0200 Subject: [PATCH] fix: dont crash on browser open when xdg-open not available --- bun.lock | 3 +++ packages/blink/package.json | 1 + packages/blink/src/cli/chat.ts | 3 ++- packages/blink/src/cli/lib/auth.ts | 26 ++--------------------- packages/blink/src/cli/lib/util.ts | 26 +++++++++++++++++++++++ packages/blink/src/cli/setup-slack-app.ts | 12 +++++------ packages/blink/src/edit/agent.ts | 15 +++---------- 7 files changed, 42 insertions(+), 44 deletions(-) create mode 100644 packages/blink/src/cli/lib/util.ts diff --git a/bun.lock b/bun.lock index 3cae12f..f575b3d 100644 --- a/bun.lock +++ b/bun.lock @@ -26,6 +26,7 @@ "@ai-sdk/openai-compatible": "^1.0.19", "@ai-sdk/react": "^2.0.35", "@ai-sdk/xai": "^2.0.16", + "@blink-sdk/compute": "^0.0.15", "@blink-sdk/compute-protocol": "^0.0.6", "@blink.so/api": "^1.0.0", "@clack/prompts": "^0.11.0", @@ -2594,6 +2595,8 @@ "app-builder-lib/tar": ["tar@6.2.1", "", { "dependencies": { "chownr": "^2.0.0", "fs-minipass": "^2.0.0", "minipass": "^5.0.0", "minizlib": "^2.1.1", "mkdirp": "^1.0.3", "yallist": "^4.0.0" } }, "sha512-DZ4yORTwrbTj/7MZYq2w+/ZFdI6OZ/f9SFHR+71gIVUZhOQPHzVCLpvRnPgyaMpfWxxk/4ONva3GQSyNIKRv6A=="], + "blink/@blink-sdk/compute": ["@blink-sdk/compute@0.0.15", "", { "peerDependencies": { "@blink-sdk/compute-protocol": ">= 0.0.6", "ai": ">= 5", "zod": ">= 4" } }, "sha512-G4OekwJIyZzVfJqcK35huomIZvfnDJMLwDsOGVrcIIhQ2xVwqixb1xZpj4jBtmDmbh+FbjjHplHp+CKRF7mamA=="], + "blink/@blink.so/api": ["@blink.so/api@1.0.0", "", { "optionalDependencies": { "@blink-sdk/compute-protocol": ">= 0.0.2" }, "peerDependencies": { "ai": ">= 5", "react": ">= 18", "zod": ">= 4" }, "optionalPeers": ["react"] }, "sha512-mBYfopecR+XaMw/W78H6aGgKyZMh99YwcGAU17LVAL+kk4uweJX3cul7958C3f8ovKSeXuXA33t64DbjY3Zi8w=="], "body-parser/iconv-lite": ["iconv-lite@0.6.3", "", { "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" } }, "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw=="], diff --git a/packages/blink/package.json b/packages/blink/package.json index 62ea394..a1278bb 100644 --- a/packages/blink/package.json +++ b/packages/blink/package.json @@ -80,6 +80,7 @@ "@ai-sdk/openai-compatible": "^1.0.19", "@ai-sdk/react": "^2.0.35", "@ai-sdk/xai": "^2.0.16", + "@blink-sdk/compute": "^0.0.15", "@blink-sdk/compute-protocol": "^0.0.6", "@blink.so/api": "^1.0.0", "@clack/prompts": "^0.11.0", diff --git a/packages/blink/src/cli/chat.ts b/packages/blink/src/cli/chat.ts index 4be860b..f3c5284 100644 --- a/packages/blink/src/cli/chat.ts +++ b/packages/blink/src/cli/chat.ts @@ -1,6 +1,7 @@ import open from "open"; import { WebSocket } from "ws"; import { WorkspaceConnect } from "./connect"; +import { openUrl } from "./lib/util"; export default async function chat() { const id = crypto.randomUUID(); @@ -21,7 +22,7 @@ export default async function chat() { }); const url = `https://blink.so/legacy-auth?id=${id}&type=workspace`; console.log(`Opening the following URL in your browser: ${url}`); - await open(url); + await openUrl(url); const token = await tokenPromise; diff --git a/packages/blink/src/cli/lib/auth.ts b/packages/blink/src/cli/lib/auth.ts index 4f2c494..2c816ee 100644 --- a/packages/blink/src/cli/lib/auth.ts +++ b/packages/blink/src/cli/lib/auth.ts @@ -11,6 +11,7 @@ import XDGAppPaths from "xdg-app-paths"; import chalk from "chalk"; import { spinner } from "@clack/prompts"; import open from "open"; +import { openUrl } from "./util"; /** * Gets the auth token for the Blink CLI. @@ -128,29 +129,6 @@ function setupEnterKeyListener(onEnter: () => void): StdinCleanup { return { cleanup }; } -/** - * Opens the browser at the given URL and handles errors. - */ -async function openBrowser(url: string): Promise { - try { - const subprocess = await open(url); - // Catch spawn errors without waiting for the browser to close - subprocess.once("error", (_err: Error) => { - console.log( - chalk.yellow( - `Could not open the browser. Please visit the URL manually: ${url}` - ) - ); - }); - } catch (_err) { - console.log( - chalk.yellow( - `Could not open the browser. Please visit the URL manually: ${url}` - ) - ); - } -} - /** * Login makes the CLI output the URL to authenticate with Blink. * It returns a valid auth token. @@ -184,7 +162,7 @@ export async function login(): Promise { // Wait for authUrl to be initialized before opening await authUrlInitializedPromise; - await openBrowser(authUrl!); + await openUrl(authUrl!); } }); diff --git a/packages/blink/src/cli/lib/util.ts b/packages/blink/src/cli/lib/util.ts new file mode 100644 index 0000000..0e4e863 --- /dev/null +++ b/packages/blink/src/cli/lib/util.ts @@ -0,0 +1,26 @@ +import open from "open"; +import chalk from "chalk"; + +export async function openUrl( + url: string, + errorMessage: string | undefined = undefined +): Promise { + try { + const proc = await open(url); + proc.once("error", (_error) => { + console.log( + chalk.yellow( + errorMessage ?? + `Could not open the browser. Please visit the URL manually: ${url}` + ) + ); + }); + } catch (_error) { + console.log( + chalk.yellow( + errorMessage ?? + `Could not open the browser. Please visit the URL manually: ${url}` + ) + ); + } +} diff --git a/packages/blink/src/cli/setup-slack-app.ts b/packages/blink/src/cli/setup-slack-app.ts index 676b60c..d269108 100644 --- a/packages/blink/src/cli/setup-slack-app.ts +++ b/packages/blink/src/cli/setup-slack-app.ts @@ -17,6 +17,7 @@ import Client from "@blink.so/api"; import crypto from "crypto"; import chalk from "chalk"; import util from "node:util"; +import { openUrl } from "./lib/util"; export async function verifySlackCredentials( botToken: string @@ -366,13 +367,10 @@ export async function setupSlackApp( } if (shouldOpen) { - try { - await open(slackAppUrl); - } catch (error) { - log.warn( - `Could not automatically open browser. Please visit the URL manually.` - ); - } + await openUrl( + slackAppUrl, + "Could not open the browser. Please visit the URL manually." + ); } // Ask for app ID diff --git a/packages/blink/src/edit/agent.ts b/packages/blink/src/edit/agent.ts index c6009c7..eabe81b 100644 --- a/packages/blink/src/edit/agent.ts +++ b/packages/blink/src/edit/agent.ts @@ -25,6 +25,7 @@ import { } from "./tools/create-github-app"; import { createSlackApp, createSlackAppSchema } from "./tools/create-slack-app"; import { TSServer } from "./tsserver"; +import { openUrl } from "../cli/lib/util"; export interface EditAgent { agent: Agent; @@ -234,12 +235,7 @@ GITHUB_PRIVATE_KEY="${btoa(data.pem)}" ); // Open the URL in the browser - const opened = await open(url); - opened.once("error", (err) => { - console.log( - `Could not open the browser. Please visit the URL manually: ${url}` - ); - }); + const opened = await openUrl(url); return `Opening GitHub App creation URL in browser: ${url}`; }, @@ -260,12 +256,7 @@ You MUST GUIDE THE USER through these steps - do not provide all the steps at on const url = createSlackApp(args); // Open the URL in the browser - const opened = await open(url); - opened.once("error", (err) => { - console.log( - `Could not open the browser. Please visit the URL manually: ${url}` - ); - }); + await openUrl(url); return `Opened Slack App creation URL in browser: ${url}`; },