| 1 | import chalk from "chalk"; |
| 2 | |
| 3 | import { getAccessToken, loadAuthConfig } from "../auth/workos.js"; |
| 4 | import { env } from "../env.js"; |
| 5 | import { telemetryService } from "../telemetry/telemetryService.js"; |
| 6 | import { startRemoteTUIChat } from "../ui/index.js"; |
| 7 | import { getRepoUrl } from "../util/git.js"; |
| 8 | import { logger } from "../util/logger.js"; |
| 9 | import { readStdinSync } from "../util/stdin.js"; |
| 10 | |
| 11 | export async function remote( |
| 12 | prompt: string | undefined, |
| 13 | options: { |
| 14 | url?: string; |
| 15 | idempotencyKey?: string; |
| 16 | start?: boolean; |
| 17 | branch?: string; |
| 18 | repo?: string; |
| 19 | config?: string; |
| 20 | } = {}, |
| 21 | ) { |
| 22 | // Check if prompt should come from stdin instead of parameter |
| 23 | let actualPrompt = prompt; |
| 24 | if (prompt) { |
| 25 | // If prompt is provided, still check for stdin and combine them |
| 26 | const stdinInput = readStdinSync(); |
| 27 | if (stdinInput) { |
| 28 | // Combine stdin and prompt argument - stdin comes first in XML block |
| 29 | actualPrompt = `<stdin>\n${stdinInput}\n</stdin>\n\n${prompt}`; |
| 30 | } |
| 31 | } else { |
| 32 | // Try to read from stdin (for piped input like: echo "hello" | cn remote -s) |
| 33 | const stdinInput = readStdinSync(); |
| 34 | if (stdinInput) { |
| 35 | actualPrompt = stdinInput; |
| 36 | } |
| 37 | } |
| 38 | // If --url is provided, connect directly to that URL |
| 39 | if (options.url) { |
| 40 | if (options.start) { |
| 41 | // In start mode, output connection details as JSON and exit |
| 42 | console.log( |
| 43 | JSON.stringify({ |
| 44 | status: "success", |
| 45 | message: "Remote environment connection details", |
| 46 | url: options.url, |
| 47 | mode: "direct_url", |
| 48 | }), |
| 49 | ); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | console.info( |
| 54 | chalk.white(`Connecting to remote environment at: ${options.url}`), |
| 55 | ); |
| 56 | |
| 57 | // Record session start |
| 58 | telemetryService.recordSessionStart(); |
| 59 | telemetryService.startActiveTime(); |
| 60 | |
| 61 | try { |
| 62 | // Start the TUI in remote mode |
| 63 | await startRemoteTUIChat(options.url, actualPrompt); |
| 64 | } finally { |
| 65 | telemetryService.stopActiveTime(); |
| 66 | } |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | try { |
| 71 | const authConfig = loadAuthConfig(); |
| 72 | |
| 73 | // Check if we have any valid authentication (file-based or environment variable) |
| 74 | if (!authConfig || !getAccessToken(authConfig)) { |
| 75 | console.error( |
| 76 | chalk.red("Not authenticated. Please run 'cn login' first."), |
| 77 | ); |
| 78 | process.exit(1); |
| 79 | } |
| 80 | |
| 81 | const accessToken = getAccessToken(authConfig); |
| 82 | |
| 83 | const requestBody: any = { |
| 84 | repoUrl: options.repo || getRepoUrl(), |
| 85 | name: `devbox-${Date.now()}`, |
| 86 | prompt: actualPrompt, |
| 87 | idempotencyKey: options.idempotencyKey, |
| 88 | agent: options.config, |
| 89 | config: options.config, |
| 90 | }; |
| 91 | |
| 92 | // Add branchName to request body if branch option is provided |
| 93 | if (options.branch) { |
| 94 | requestBody.branchName = options.branch; |
| 95 | } |
| 96 | |
| 97 | const response = await fetch(new URL("agents/devboxes", env.apiBase), { |
| 98 | method: "POST", |
| 99 | headers: { |
| 100 | "Content-Type": "application/json", |
| 101 | Authorization: `Bearer ${accessToken}`, |
| 102 | }, |
| 103 | body: JSON.stringify(requestBody), |
| 104 | }); |
| 105 | |
| 106 | if (!response.ok) { |
| 107 | const errorText = await response.text(); |
| 108 | throw new Error( |
| 109 | `Failed to create remote environment: ${response.status} ${errorText}`, |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | const result = await response.json(); |
| 114 | |
| 115 | if (options.start) { |
| 116 | // In start mode, output connection details as JSON and exit |
| 117 | console.log( |
| 118 | JSON.stringify({ |
| 119 | status: "success", |
| 120 | message: "Remote development environment created successfully", |
| 121 | url: `${env.appUrl}/agents/${result.id}`, |
| 122 | containerUrl: result.url, |
| 123 | containerPort: result.port, |
| 124 | name: requestBody.name, |
| 125 | mode: "new_environment", |
| 126 | }), |
| 127 | ); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | console.info( |
| 132 | chalk.green("✅ Remote development environment created successfully!"), |
| 133 | ); |
| 134 | |
| 135 | if (result.url && result.port) { |
| 136 | const remoteUrl = result.url; |
| 137 | console.info( |
| 138 | chalk.white(`Connecting to remote environment at: ${remoteUrl}`), |
| 139 | ); |
| 140 | |
| 141 | // Record session start |
| 142 | telemetryService.recordSessionStart(); |
| 143 | telemetryService.startActiveTime(); |
| 144 | |
| 145 | try { |
| 146 | // Start the TUI in remote mode (prompt is optional) |
| 147 | await startRemoteTUIChat(remoteUrl, actualPrompt); |
| 148 | } finally { |
| 149 | telemetryService.stopActiveTime(); |
| 150 | } |
| 151 | } else { |
| 152 | throw new Error( |
| 153 | "No URL or port returned from remote environment creation", |
| 154 | ); |
| 155 | } |
| 156 | } catch (error: any) { |
| 157 | logger.error( |
| 158 | chalk.red(`Failed to create remote environment: ${error.message}`), |
| 159 | ); |
| 160 | process.exit(1); |
| 161 | } |
| 162 | } |