From 9aa3e7fe6420d2c05a9634edb48ac6cf3e44ed87 Mon Sep 17 00:00:00 2001 From: Nate Date: Sun, 20 Jul 2025 18:30:09 -0700 Subject: [PATCH] feat: remote command --- src/commands/remote.ts | 62 +++++++++++++++++++++++++++++++ src/env.ts | 5 ++- src/index.ts | 9 +++++ src/ui/hooks/useConfigSelector.ts | 3 +- src/util/git.ts | 47 +++++++++++++++++++++++ 5 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 src/commands/remote.ts create mode 100644 src/util/git.ts diff --git a/src/commands/remote.ts b/src/commands/remote.ts new file mode 100644 index 000000000..488021075 --- /dev/null +++ b/src/commands/remote.ts @@ -0,0 +1,62 @@ +import chalk from "chalk"; +import { + getAccessToken, + isAuthenticatedConfig, + loadAuthConfig, +} from "../auth/workos.js"; +import { env } from "../env.js"; + +export async function remote(prompt: string) { + console.info(chalk.white("Setting up remote development environment...")); + + try { + const authConfig = loadAuthConfig(); + + if (!isAuthenticatedConfig(authConfig)) { + console.error( + chalk.red("Not authenticated. Please run 'cn login' first.") + ); + process.exit(1); + } + + const accessToken = getAccessToken(authConfig); + + const response = await fetch(new URL("agents/devboxes", env.apiBase), { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${accessToken}`, + }, + body: JSON.stringify({ + cUserId: authConfig.userId, + repoUrl: "https://github.com/continuedev/amplified.dev", // getRepoUrl(), + name: `devbox-${Date.now()}`, + prompt: prompt, + }), + }); + + if (!response.ok) { + const errorText = await response.text(); + throw new Error( + `Failed to create remote environment: ${response.status} ${errorText}` + ); + } + + const result = await response.json(); + + console.info( + chalk.green("✅ Remote development environment created successfully!") + ); + + if (result.url) { + console.info( + chalk.white(`Access your environment at: ${result.url}:${result.port}`) + ); + } + } catch (error: any) { + console.error( + chalk.red(`Failed to create remote environment: ${error.message}`) + ); + process.exit(1); + } +} diff --git a/src/env.ts b/src/env.ts index 6c79148f5..2f5da8d0c 100644 --- a/src/env.ts +++ b/src/env.ts @@ -4,6 +4,7 @@ dotenv.config(); export const env = { apiBase: process.env.CONTINUE_API_BASE ?? "https://api.continue.dev/", - workOsClientId: "client_01J0FW6XN8N2XJAECF7NE0Y65J", - appUrl: "https://hub.continue.dev", + workOsClientId: + process.env.WORKOS_CLIENT_ID ?? "client_01J0FW6XN8N2XJAECF7NE0Y65J", + appUrl: process.env.HUB_URL ?? "https://hub.continue.dev", }; diff --git a/src/index.ts b/src/index.ts index 0a9505347..154362b92 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import { Command } from "commander"; import { chat } from "./commands/chat.js"; import { login } from "./commands/login.js"; import { logout } from "./commands/logout.js"; +import { remote } from "./commands/remote.js"; import { configureConsoleForHeadless } from "./util/consoleOverride.js"; import logger from "./util/logger.js"; import { getVersion } from "./version.js"; @@ -87,6 +88,14 @@ program await logout(); }); +// Remote subcommand +program + .command("remote ") + .description("Launch a remote instance of the cn agent") + .action(async (prompt: string) => { + await remote(prompt); + }); + // Handle unknown commands program.on("command:*", () => { console.error(`Error: Unknown command '${program.args.join(" ")}'\n`); diff --git a/src/ui/hooks/useConfigSelector.ts b/src/ui/hooks/useConfigSelector.ts index 6e54837b8..dc8108872 100644 --- a/src/ui/hooks/useConfigSelector.ts +++ b/src/ui/hooks/useConfigSelector.ts @@ -47,7 +47,8 @@ export function useConfigSelector({ if (config.type === "create") { // Open the web browser to create new assistant - const url = "https://hub.continue.dev/new?type=assistant"; + const url = new URL("https://hub.continue.dev/new"); + url.searchParams.set("type", "assistant"); try { let command: string; diff --git a/src/util/git.ts b/src/util/git.ts new file mode 100644 index 000000000..02c1605fd --- /dev/null +++ b/src/util/git.ts @@ -0,0 +1,47 @@ +import { execSync } from "child_process"; + +/** + * Get the git remote URL for the current repository + * @param remote The remote name (defaults to 'origin') + * @returns The remote URL or null if not found + */ +export function getGitRemoteUrl(remote: string = "origin"): string | null { + try { + const result = execSync(`git remote get-url ${remote}`, { + encoding: "utf-8", + cwd: process.cwd(), + stdio: "pipe", + }); + return result.trim(); + } catch { + return null; + } +} + +/** + * Check if current directory is a git repository + */ +export function isGitRepo(): boolean { + try { + execSync("git rev-parse --is-inside-work-tree", { + stdio: "ignore", + cwd: process.cwd(), + }); + return true; + } catch { + return false; + } +} + +/** + * Get the current repository URL, falling back to local path if not a git repo + * @returns Git remote URL or local path + */ +export function getRepoUrl(): string { + if (!isGitRepo()) { + return process.cwd(); + } + + const remoteUrl = getGitRemoteUrl(); + return remoteUrl || process.cwd(); +} \ No newline at end of file