Elffuss BraidHub

kikocis/continue public

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