@computesdk/workbench 23.0.0 → 24.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -88
- package/dist/bin/workbench.js +56 -713
- package/dist/bin/workbench.js.map +1 -1
- package/dist/helpers.d.ts +0 -4
- package/dist/helpers.js +2 -2
- package/dist/helpers.js.map +1 -1
- package/dist/index.js +56 -713
- package/dist/index.js.map +1 -1
- package/package.json +23 -24
package/dist/index.js
CHANGED
|
@@ -34,8 +34,6 @@ function createState() {
|
|
|
34
34
|
currentSandbox: null,
|
|
35
35
|
sandboxCreatedAt: null,
|
|
36
36
|
availableProviders: [],
|
|
37
|
-
useDirectMode: false,
|
|
38
|
-
// Default to gateway mode
|
|
39
37
|
verbose: false,
|
|
40
38
|
// Enabled automatically for local provider
|
|
41
39
|
compute: null
|
|
@@ -114,29 +112,16 @@ __export(output_exports, {
|
|
|
114
112
|
showInfo: () => showInfo,
|
|
115
113
|
showWelcome: () => showWelcome
|
|
116
114
|
});
|
|
117
|
-
function showWelcome(availableProviders, currentProvider
|
|
115
|
+
function showWelcome(availableProviders, currentProvider) {
|
|
118
116
|
console.log(c.bold(c.cyan("\n\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557")));
|
|
119
117
|
console.log(c.bold(c.cyan("\u2551 ComputeSDK Workbench \u2551")));
|
|
120
118
|
console.log(c.bold(c.cyan("\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D\n")));
|
|
121
119
|
console.log(c.dim("Prompt shows connection status: > (disconnected) or provider:sandbox> (connected)\n"));
|
|
122
|
-
if (localDaemonRunning) {
|
|
123
|
-
console.log(c.green("Local daemon detected - auto-connecting...\n"));
|
|
124
|
-
}
|
|
125
120
|
if (availableProviders.length > 0) {
|
|
126
|
-
|
|
127
|
-
console.log(`Providers available: ${backendProviders.join(", ")}`);
|
|
121
|
+
console.log(`Providers available: ${availableProviders.join(", ")}`);
|
|
128
122
|
if (currentProvider) {
|
|
129
|
-
|
|
130
|
-
console.log(`Current provider: ${c.green("local")} (local daemon)
|
|
131
|
-
`);
|
|
132
|
-
} else if (useDirectMode) {
|
|
133
|
-
console.log(`Current provider: ${c.green(currentProvider)} (direct mode)
|
|
123
|
+
console.log(`Current provider: ${c.green(currentProvider)}
|
|
134
124
|
`);
|
|
135
|
-
} else {
|
|
136
|
-
const backendProvider = currentProvider === "gateway" ? backendProviders[0] || "auto" : currentProvider;
|
|
137
|
-
console.log(`Current provider: ${c.green(backendProvider)} (via gateway)
|
|
138
|
-
`);
|
|
139
|
-
}
|
|
140
125
|
} else {
|
|
141
126
|
console.log(`
|
|
142
127
|
${c.dim('Tip: Use "provider <name>" to select a provider')}
|
|
@@ -162,8 +147,6 @@ function showInfo(state) {
|
|
|
162
147
|
console.log(` Sandbox ID: ${c.cyan(state.currentSandbox.sandboxId)}`);
|
|
163
148
|
}
|
|
164
149
|
console.log(` Provider: ${c.green(state.currentProvider || "unknown")}`);
|
|
165
|
-
const modeLabel = state.useDirectMode ? "direct \u{1F517}" : "gateway \u{1F310}";
|
|
166
|
-
console.log(` Mode: ${c.blue(modeLabel)}`);
|
|
167
150
|
console.log(` Created: ${state.sandboxCreatedAt?.toLocaleString() || "unknown"}`);
|
|
168
151
|
console.log(` Uptime: ${formatUptime(state)}`);
|
|
169
152
|
console.log("");
|
|
@@ -187,33 +170,14 @@ function showHelp() {
|
|
|
187
170
|
${c.bold("ComputeSDK Workbench Commands")}
|
|
188
171
|
|
|
189
172
|
${c.bold("Provider Management:")}
|
|
190
|
-
${c.cyan("provider <name>")} Switch to provider
|
|
173
|
+
${c.cyan("provider <name>")} Switch to provider
|
|
191
174
|
${c.dim("Example: provider e2b")}
|
|
192
|
-
${c.cyan("provider direct <name>")} Connect directly to provider
|
|
193
|
-
${c.dim("Example: provider direct e2b")}
|
|
194
175
|
${c.cyan("providers")} List all providers with status
|
|
195
|
-
${c.cyan("mode <gateway|direct>")} Toggle default mode for next sandbox
|
|
196
|
-
|
|
197
|
-
${c.bold("Provider Modes:")}
|
|
198
|
-
${c.bold("Gateway (default)")}: Routes through ComputeSDK API, zero-config
|
|
199
|
-
\u2022 Requires: COMPUTESDK_API_KEY + provider credentials
|
|
200
|
-
\u2022 Usage: ${c.cyan("provider e2b")}
|
|
201
|
-
|
|
202
|
-
${c.bold("Direct")}: Connects directly to provider, requires packages
|
|
203
|
-
\u2022 Requires: Provider package installed + credentials
|
|
204
|
-
\u2022 Usage: ${c.cyan("provider direct e2b")}
|
|
205
176
|
|
|
206
177
|
${c.bold("Sandbox Management:")}
|
|
207
178
|
${c.cyan("restart")} Restart current sandbox
|
|
208
179
|
${c.cyan("destroy")} Destroy current sandbox
|
|
209
180
|
${c.cyan("info")} Show sandbox info (provider, uptime)
|
|
210
|
-
${c.cyan("connect <url> [token]")} Connect to sandbox via URL
|
|
211
|
-
|
|
212
|
-
${c.bold("Local Daemon:")}
|
|
213
|
-
${c.cyan("provider local")} Connect to local daemon's main sandbox
|
|
214
|
-
${c.cyan("provider local list")} List local sandboxes
|
|
215
|
-
${c.cyan("provider local <subdomain>")} Connect to specific local sandbox
|
|
216
|
-
${c.dim("Example: provider local separate-snail-qkktux")}
|
|
217
181
|
|
|
218
182
|
${c.bold("Environment:")}
|
|
219
183
|
${c.cyan("env")} Show environment/credentials status
|
|
@@ -248,53 +212,14 @@ ${c.bold("Running Commands:")}
|
|
|
248
212
|
${c.cyan('filesystem.exists("/path")')}
|
|
249
213
|
${c.cyan('filesystem.remove("/file")')}
|
|
250
214
|
|
|
251
|
-
${c.dim("Named Sandboxes (gateway mode only):")}
|
|
252
|
-
${c.cyan("create()")} ${c.dim("// Create & switch to new sandbox")}
|
|
253
|
-
${c.cyan('create({ namespace: "h" })')} ${c.dim("// Create with namespace & switch")}
|
|
254
|
-
${c.cyan('findOrCreate({ name: "my-app" })')} ${c.dim("// Find or create & switch")}
|
|
255
|
-
${c.cyan('find({ name: "my-app" })')} ${c.dim("// Find existing & switch")}
|
|
256
|
-
|
|
257
|
-
${c.dim("Note: Prompts before switching if you already have an active sandbox")}
|
|
258
|
-
|
|
259
|
-
${c.dim("Child Sandboxes (gateway mode only):")}
|
|
260
|
-
${c.cyan("child.create()")} ${c.dim("// Create child sandbox")}
|
|
261
|
-
${c.cyan("child.list()")} ${c.dim("// List all children")}
|
|
262
|
-
${c.cyan('child.retrieve("sandbox-id")')} ${c.dim("// Get child info")}
|
|
263
|
-
${c.cyan('child.destroy("sandbox-id")')} ${c.dim("// Delete child")}
|
|
264
|
-
|
|
265
215
|
${c.dim("Sandbox Methods:")}
|
|
266
216
|
${c.cyan("getUrl({ port: 3000 })")} ${c.dim("// Get public URL")}
|
|
267
|
-
${c.cyan(
|
|
217
|
+
${c.cyan('runCommand("echo hi")')} ${c.dim("// Execute command")}
|
|
268
218
|
${c.cyan("sandboxInfo()")} ${c.dim("// Get sandbox details")}
|
|
269
219
|
${c.cyan("ready()")} ${c.dim("// Get readiness status")}
|
|
270
220
|
${c.cyan("getInstance()")} ${c.dim("// Get native instance")}
|
|
271
|
-
|
|
272
|
-
${c.dim("Terminal (PTY & Exec):")}
|
|
273
|
-
${c.cyan("terminal.create({ pty: true })")} ${c.dim("// Create PTY terminal")}
|
|
274
|
-
${c.cyan("terminal.create({ pty: false })")} ${c.dim("// Create exec terminal")}
|
|
275
|
-
${c.cyan("terminal.list()")} ${c.dim("// List all terminals")}
|
|
276
|
-
${c.cyan("terminal.retrieve(id)")} ${c.dim("// Get terminal by ID")}
|
|
277
|
-
${c.cyan("terminal.destroy(id)")} ${c.dim("// Close terminal by ID")}
|
|
278
|
-
|
|
279
|
-
${c.dim("PTY Terminal:")}
|
|
280
|
-
${c.cyan("term = terminal.create({ pty: true })")}
|
|
281
|
-
${c.cyan('term.on("output", (data) => console.log(data))')}
|
|
282
|
-
${c.cyan('term.write("echo hello\\n")')}
|
|
283
|
-
${c.cyan("term.destroy()")}
|
|
284
|
-
|
|
285
|
-
${c.dim("Exec Terminal:")}
|
|
286
|
-
${c.cyan("exec = terminal.create({ pty: false })")}
|
|
287
|
-
${c.cyan('cmd = exec.command.run("ls -la")')}
|
|
288
|
-
${c.cyan("cmd.stdout")} ${c.dim("// view output")}
|
|
289
|
-
|
|
221
|
+
|
|
290
222
|
${c.dim('Note: No need to use "await" - promises are auto-awaited!')}
|
|
291
|
-
|
|
292
|
-
${c.dim("Compute CLI:")}
|
|
293
|
-
${c.cyan("compute.isSetup()")} ${c.dim("// Check if daemon is running")}
|
|
294
|
-
${c.cyan("compute.setup()")} ${c.dim("// Install + start daemon")}
|
|
295
|
-
${c.cyan("compute.install()")} ${c.dim("// Install compute CLI")}
|
|
296
|
-
${c.cyan("compute.start()")} ${c.dim("// Start daemon")}
|
|
297
|
-
${c.cyan("compute.health()")} ${c.dim("// Check daemon health")}
|
|
298
223
|
|
|
299
224
|
${c.bold("Shell Commands:")}
|
|
300
225
|
Use ${c.cyan("$")} prefix to run any shell command directly:
|
|
@@ -489,14 +414,11 @@ function showEnv() {
|
|
|
489
414
|
console.log(c.dim("Tip: Set credentials in your .env file"));
|
|
490
415
|
console.log("");
|
|
491
416
|
}
|
|
492
|
-
function autoDetectProvider(
|
|
417
|
+
function autoDetectProvider() {
|
|
493
418
|
const explicit = process.env.COMPUTESDK_PROVIDER?.toLowerCase();
|
|
494
419
|
if (explicit && isValidProvider(explicit) && isProviderReady(explicit)) {
|
|
495
420
|
return explicit;
|
|
496
421
|
}
|
|
497
|
-
if (forceGatewayMode) {
|
|
498
|
-
return isProviderReady("gateway") ? "gateway" : null;
|
|
499
|
-
}
|
|
500
422
|
for (const provider of PROVIDER_NAMES) {
|
|
501
423
|
if (isProviderReady(provider)) {
|
|
502
424
|
return provider;
|
|
@@ -533,8 +455,6 @@ Available: ${PROVIDER_NAMES.join(", ")}`;
|
|
|
533
455
|
async function loadProvider(providerName) {
|
|
534
456
|
try {
|
|
535
457
|
switch (providerName) {
|
|
536
|
-
case "gateway":
|
|
537
|
-
return await import("computesdk");
|
|
538
458
|
case "e2b":
|
|
539
459
|
return await import("@computesdk/e2b");
|
|
540
460
|
case "railway":
|
|
@@ -577,20 +497,12 @@ async function loadProvider(providerName) {
|
|
|
577
497
|
throw new Error(`Unknown provider: ${providerName}`);
|
|
578
498
|
}
|
|
579
499
|
} catch (error) {
|
|
580
|
-
if (providerName === "gateway") {
|
|
581
|
-
throw new Error(`Failed to load gateway provider from computesdk package.`);
|
|
582
|
-
}
|
|
583
500
|
throw new Error(
|
|
584
501
|
`Failed to load provider ${providerName}. Make sure to install it: npm install @computesdk/${providerName}`
|
|
585
502
|
);
|
|
586
503
|
}
|
|
587
504
|
}
|
|
588
505
|
function getProviderConfig(providerName) {
|
|
589
|
-
if (providerName === "gateway") {
|
|
590
|
-
const config = {};
|
|
591
|
-
if (process.env.COMPUTESDK_API_KEY) config.apiKey = process.env.COMPUTESDK_API_KEY;
|
|
592
|
-
return config;
|
|
593
|
-
}
|
|
594
506
|
return getProviderConfigFromEnv(providerName);
|
|
595
507
|
}
|
|
596
508
|
var SHARED_PROVIDER_NAMES, SHARED_PROVIDER_AUTH, PROVIDER_ENV_MAP, PROVIDER_NAMES, PROVIDER_AUTH;
|
|
@@ -679,14 +591,8 @@ var init_providers = __esm({
|
|
|
679
591
|
"secure-exec": {},
|
|
680
592
|
upstash: { apiKey: "UPSTASH_BOX_API_KEY" }
|
|
681
593
|
};
|
|
682
|
-
PROVIDER_NAMES =
|
|
683
|
-
|
|
684
|
-
...SHARED_PROVIDER_NAMES
|
|
685
|
-
];
|
|
686
|
-
PROVIDER_AUTH = {
|
|
687
|
-
gateway: [["COMPUTESDK_API_KEY"]],
|
|
688
|
-
...SHARED_PROVIDER_AUTH
|
|
689
|
-
};
|
|
594
|
+
PROVIDER_NAMES = SHARED_PROVIDER_NAMES;
|
|
595
|
+
PROVIDER_AUTH = SHARED_PROVIDER_AUTH;
|
|
690
596
|
}
|
|
691
597
|
});
|
|
692
598
|
|
|
@@ -695,24 +601,17 @@ var commands_exports = {};
|
|
|
695
601
|
__export(commands_exports, {
|
|
696
602
|
cleanupOnExit: () => cleanupOnExit,
|
|
697
603
|
confirmSandboxSwitch: () => confirmSandboxSwitch,
|
|
698
|
-
connectToLocal: () => connectToLocal,
|
|
699
|
-
connectToSandbox: () => connectToSandbox,
|
|
700
604
|
createSandbox: () => createSandbox,
|
|
701
605
|
defineProviderCommand: () => defineProviderCommand,
|
|
702
606
|
destroySandbox: () => destroySandbox,
|
|
703
607
|
ensureSandbox: () => ensureSandbox,
|
|
704
608
|
getComputeInstance: () => getComputeInstance,
|
|
705
|
-
isLocalDaemonRunning: () => isLocalDaemonRunning,
|
|
706
|
-
listLocalSandboxes: () => listLocalSandboxes,
|
|
707
609
|
restartSandbox: () => restartSandbox,
|
|
708
610
|
runCommand: () => runCommand,
|
|
709
|
-
showMode: () => showMode,
|
|
710
611
|
showVerbose: () => showVerbose,
|
|
711
612
|
switchProvider: () => switchProvider,
|
|
712
|
-
toggleMode: () => toggleMode,
|
|
713
613
|
toggleVerbose: () => toggleVerbose
|
|
714
614
|
});
|
|
715
|
-
import { createCompute } from "@computesdk/provider";
|
|
716
615
|
import { escapeArgs } from "@computesdk/cmd";
|
|
717
616
|
async function confirm(question, defaultYes = false, _state) {
|
|
718
617
|
const promptSuffix = defaultYes ? "(Y/n)" : "(y/N)";
|
|
@@ -777,12 +676,10 @@ async function getComputeInstance(state) {
|
|
|
777
676
|
if (state.compute) {
|
|
778
677
|
return state.compute;
|
|
779
678
|
}
|
|
780
|
-
const providerName = state.currentProvider || autoDetectProvider(
|
|
781
|
-
const useDirect = state.useDirectMode;
|
|
679
|
+
const providerName = state.currentProvider || autoDetectProvider();
|
|
782
680
|
if (!providerName) {
|
|
783
681
|
throw new Error("No provider configured.");
|
|
784
682
|
}
|
|
785
|
-
let compute2;
|
|
786
683
|
const exportName = providerName.replace(/-([a-z])/g, (_, c2) => c2.toUpperCase());
|
|
787
684
|
const providerModule = await loadProvider(providerName);
|
|
788
685
|
const providerFactory = providerModule[exportName] ?? providerModule[providerName];
|
|
@@ -791,46 +688,25 @@ async function getComputeInstance(state) {
|
|
|
791
688
|
}
|
|
792
689
|
const config = getProviderConfig(providerName);
|
|
793
690
|
const providerInstance = providerFactory(config);
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
} else {
|
|
799
|
-
const { compute: directCompute } = await import("computesdk");
|
|
800
|
-
compute2 = directCompute({
|
|
801
|
-
provider: providerInstance
|
|
802
|
-
});
|
|
803
|
-
}
|
|
691
|
+
const { compute: directCompute } = await import("computesdk");
|
|
692
|
+
const compute2 = directCompute({
|
|
693
|
+
provider: providerInstance
|
|
694
|
+
});
|
|
804
695
|
state.compute = compute2;
|
|
805
696
|
return compute2;
|
|
806
697
|
}
|
|
807
698
|
async function createSandbox(state) {
|
|
808
|
-
const providerName = state.currentProvider || autoDetectProvider(
|
|
809
|
-
const useDirect = state.useDirectMode;
|
|
699
|
+
const providerName = state.currentProvider || autoDetectProvider();
|
|
810
700
|
if (!providerName) {
|
|
811
701
|
logError('No provider configured. Run "env" to see setup instructions.');
|
|
812
702
|
throw new Error("No provider available");
|
|
813
703
|
}
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
actualProviderName = providerName;
|
|
819
|
-
if (!isProviderReady(providerName)) {
|
|
820
|
-
logError(`Provider ${providerName} is not fully configured for direct mode.`);
|
|
821
|
-
console.log(getProviderSetupHelp(providerName));
|
|
822
|
-
throw new Error("Provider not ready");
|
|
823
|
-
}
|
|
824
|
-
} else {
|
|
825
|
-
modeLabel = `${providerName} (via gateway)`;
|
|
826
|
-
actualProviderName = "gateway";
|
|
827
|
-
if (!isProviderReady("gateway")) {
|
|
828
|
-
logError("Gateway mode requires COMPUTESDK_API_KEY");
|
|
829
|
-
console.log(getProviderSetupHelp("gateway"));
|
|
830
|
-
throw new Error("Gateway not ready");
|
|
831
|
-
}
|
|
704
|
+
if (!isProviderReady(providerName)) {
|
|
705
|
+
logError(`Provider ${providerName} is not fully configured.`);
|
|
706
|
+
console.log(getProviderSetupHelp(providerName));
|
|
707
|
+
throw new Error("Provider not ready");
|
|
832
708
|
}
|
|
833
|
-
const spinner = new Spinner(`Creating sandbox with ${
|
|
709
|
+
const spinner = new Spinner(`Creating sandbox with ${providerName}...`).start();
|
|
834
710
|
const startTime = Date.now();
|
|
835
711
|
try {
|
|
836
712
|
const compute2 = await getComputeInstance(state);
|
|
@@ -930,122 +806,48 @@ function isStaleConnectionError(error) {
|
|
|
930
806
|
];
|
|
931
807
|
return stalePhrases.some((phrase) => message.includes(phrase));
|
|
932
808
|
}
|
|
933
|
-
async function switchProvider(state,
|
|
934
|
-
if (
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
return;
|
|
938
|
-
}
|
|
939
|
-
await connectToLocal(state, providerName);
|
|
809
|
+
async function switchProvider(state, name) {
|
|
810
|
+
if (!isValidProvider(name)) {
|
|
811
|
+
logError(`Unknown provider: ${name}`);
|
|
812
|
+
console.log(`Available providers: ${PROVIDER_NAMES.join(", ")}`);
|
|
940
813
|
return;
|
|
941
814
|
}
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
if (!providerName) {
|
|
946
|
-
logError("Usage: provider direct <name>");
|
|
947
|
-
console.log("Example: provider direct e2b");
|
|
948
|
-
return;
|
|
949
|
-
}
|
|
950
|
-
useDirect = true;
|
|
951
|
-
actualProvider = providerName;
|
|
952
|
-
} else if (mode === "gateway") {
|
|
953
|
-
if (!providerName) {
|
|
954
|
-
logError("Usage: provider gateway <name>");
|
|
955
|
-
console.log("Example: provider gateway e2b");
|
|
956
|
-
return;
|
|
957
|
-
}
|
|
958
|
-
useDirect = false;
|
|
959
|
-
actualProvider = providerName;
|
|
960
|
-
}
|
|
961
|
-
if (actualProvider === "gateway") {
|
|
962
|
-
actualProvider = autoDetectProvider(false) || "e2b";
|
|
963
|
-
}
|
|
964
|
-
if (!isValidProvider(actualProvider)) {
|
|
965
|
-
logError(`Unknown provider: ${actualProvider}`);
|
|
966
|
-
console.log(`Available providers: ${PROVIDER_NAMES.filter((p) => p !== "gateway").join(", ")}, local`);
|
|
967
|
-
return;
|
|
968
|
-
}
|
|
969
|
-
if (!useDirect && !isProviderReady("gateway")) {
|
|
970
|
-
logError("Gateway mode requires COMPUTESDK_API_KEY");
|
|
971
|
-
console.log(getProviderSetupHelp("gateway"));
|
|
972
|
-
return;
|
|
973
|
-
}
|
|
974
|
-
if (useDirect && !isProviderReady(actualProvider)) {
|
|
975
|
-
logError(`Provider ${actualProvider} is not fully configured for direct mode.`);
|
|
976
|
-
console.log(getProviderSetupHelp(actualProvider));
|
|
815
|
+
if (!isProviderReady(name)) {
|
|
816
|
+
logError(`Provider ${name} is not fully configured.`);
|
|
817
|
+
console.log(getProviderSetupHelp(name));
|
|
977
818
|
return;
|
|
978
819
|
}
|
|
979
820
|
if (hasSandbox(state)) {
|
|
980
821
|
const shouldDestroy = await confirm("Destroy current sandbox?", false, state);
|
|
981
822
|
if (shouldDestroy) {
|
|
982
823
|
await destroySandbox(state);
|
|
983
|
-
state.currentProvider =
|
|
984
|
-
state.useDirectMode = useDirect;
|
|
824
|
+
state.currentProvider = name;
|
|
985
825
|
state.compute = null;
|
|
986
|
-
|
|
987
|
-
logSuccess(`Switched to ${modeStr}`);
|
|
826
|
+
logSuccess(`Switched to ${name}`);
|
|
988
827
|
} else {
|
|
989
828
|
logWarning("Keeping current sandbox. Provider remains unchanged.");
|
|
990
829
|
}
|
|
991
830
|
} else {
|
|
992
|
-
state.currentProvider =
|
|
993
|
-
state.useDirectMode = useDirect;
|
|
831
|
+
state.currentProvider = name;
|
|
994
832
|
state.compute = null;
|
|
995
|
-
|
|
996
|
-
logSuccess(`Switched to ${modeStr}`);
|
|
833
|
+
logSuccess(`Switched to ${name}`);
|
|
997
834
|
}
|
|
998
835
|
}
|
|
999
836
|
function defineProviderCommand(state) {
|
|
1000
|
-
return async function provider(
|
|
1001
|
-
if (!
|
|
837
|
+
return async function provider(name) {
|
|
838
|
+
if (!name) {
|
|
1002
839
|
if (state.currentProvider) {
|
|
1003
|
-
const modeStr = state.useDirectMode ? "direct" : state.currentProvider === "local" ? "local daemon" : "via gateway";
|
|
1004
840
|
console.log(`
|
|
1005
|
-
Current provider: ${c.green(state.currentProvider)}
|
|
841
|
+
Current provider: ${c.green(state.currentProvider)}
|
|
1006
842
|
`);
|
|
1007
843
|
} else {
|
|
1008
844
|
console.log(c.yellow("\nNo provider selected\n"));
|
|
1009
845
|
}
|
|
1010
846
|
return;
|
|
1011
847
|
}
|
|
1012
|
-
await switchProvider(state,
|
|
848
|
+
await switchProvider(state, name);
|
|
1013
849
|
};
|
|
1014
850
|
}
|
|
1015
|
-
async function toggleMode(state, mode) {
|
|
1016
|
-
const newMode = mode || (state.useDirectMode ? "gateway" : "direct");
|
|
1017
|
-
if (newMode === "direct") {
|
|
1018
|
-
state.useDirectMode = true;
|
|
1019
|
-
logSuccess("Switched to direct mode \u{1F517}");
|
|
1020
|
-
console.log(c.dim("Next sandbox will use direct provider packages\n"));
|
|
1021
|
-
if (hasSandbox(state) && !state.useDirectMode) {
|
|
1022
|
-
console.log(c.yellow("Current sandbox is in gateway mode."));
|
|
1023
|
-
console.log(c.dim('Run "restart" to switch to direct mode\n'));
|
|
1024
|
-
}
|
|
1025
|
-
} else {
|
|
1026
|
-
state.useDirectMode = false;
|
|
1027
|
-
logSuccess("Switched to gateway mode \u{1F310}");
|
|
1028
|
-
console.log(c.dim("Next sandbox will use gateway (requires COMPUTESDK_API_KEY)\n"));
|
|
1029
|
-
if (hasSandbox(state) && state.useDirectMode) {
|
|
1030
|
-
console.log(c.yellow("Current sandbox is in direct mode."));
|
|
1031
|
-
console.log(c.dim('Run "restart" to switch to gateway mode\n'));
|
|
1032
|
-
}
|
|
1033
|
-
}
|
|
1034
|
-
}
|
|
1035
|
-
function showMode(state) {
|
|
1036
|
-
const mode = state.useDirectMode ? "direct" : "gateway";
|
|
1037
|
-
const icon = mode === "gateway" ? "\u{1F310}" : "\u{1F517}";
|
|
1038
|
-
console.log(`
|
|
1039
|
-
Current mode: ${c.green(mode)} ${icon}`);
|
|
1040
|
-
if (mode === "gateway") {
|
|
1041
|
-
console.log(c.dim("Routes through ComputeSDK API (requires COMPUTESDK_API_KEY)"));
|
|
1042
|
-
} else {
|
|
1043
|
-
console.log(c.dim("Direct connection to providers (requires provider packages)"));
|
|
1044
|
-
}
|
|
1045
|
-
console.log(`
|
|
1046
|
-
Switch with: ${c.cyan("provider e2b")} (gateway) or ${c.cyan("provider direct e2b")} (direct)
|
|
1047
|
-
`);
|
|
1048
|
-
}
|
|
1049
851
|
function toggleVerbose(state) {
|
|
1050
852
|
state.verbose = !state.verbose;
|
|
1051
853
|
if (state.verbose) {
|
|
@@ -1072,182 +874,6 @@ Verbose mode: ${status}`);
|
|
|
1072
874
|
Toggle with: ${c.cyan("verbose")}
|
|
1073
875
|
`);
|
|
1074
876
|
}
|
|
1075
|
-
async function connectToSandbox(state, sandboxUrl, token) {
|
|
1076
|
-
if (!sandboxUrl) {
|
|
1077
|
-
logError("Usage: connect <sandbox_url> [token]");
|
|
1078
|
-
console.log("Example: connect https://sandbox-123.localhost:8080");
|
|
1079
|
-
console.log("Example: connect https://sandbox-123.localhost:8080 your_access_token");
|
|
1080
|
-
return;
|
|
1081
|
-
}
|
|
1082
|
-
const cleanUrl = sandboxUrl.replace(/\/$/, "");
|
|
1083
|
-
if (hasSandbox(state)) {
|
|
1084
|
-
const shouldDisconnect = await confirm("Disconnect from current sandbox?", false, state);
|
|
1085
|
-
if (!shouldDisconnect) {
|
|
1086
|
-
logWarning("Keeping current sandbox. Connection cancelled.");
|
|
1087
|
-
return;
|
|
1088
|
-
}
|
|
1089
|
-
clearSandbox(state);
|
|
1090
|
-
}
|
|
1091
|
-
const spinner = new Spinner(`Connecting to ${cleanUrl}...`).start();
|
|
1092
|
-
const startTime = Date.now();
|
|
1093
|
-
try {
|
|
1094
|
-
const { Sandbox } = await import("computesdk");
|
|
1095
|
-
let WebSocket;
|
|
1096
|
-
try {
|
|
1097
|
-
const wsModule = await import("ws");
|
|
1098
|
-
WebSocket = wsModule.default;
|
|
1099
|
-
} catch {
|
|
1100
|
-
logError('Failed to import "ws" module. Please install it: pnpm add ws');
|
|
1101
|
-
throw new Error('Missing "ws" dependency');
|
|
1102
|
-
}
|
|
1103
|
-
const sandbox = new Sandbox({
|
|
1104
|
-
sandboxUrl: cleanUrl,
|
|
1105
|
-
sandboxId: "",
|
|
1106
|
-
// Will be populated when we get info
|
|
1107
|
-
provider: "connected",
|
|
1108
|
-
// Mark as directly connected
|
|
1109
|
-
token,
|
|
1110
|
-
// Optional access token
|
|
1111
|
-
WebSocket
|
|
1112
|
-
});
|
|
1113
|
-
const info = await sandbox.getInfo();
|
|
1114
|
-
const duration = Date.now() - startTime;
|
|
1115
|
-
setSandbox(state, sandbox, "connected");
|
|
1116
|
-
spinner.succeed(`Connected to sandbox ${c.dim(`(${formatDuration(duration)})`)}`);
|
|
1117
|
-
console.log(c.dim(`Provider: ${info.provider || "unknown"}`));
|
|
1118
|
-
console.log(c.dim(`Sandbox ID: ${info.id || "unknown"}`));
|
|
1119
|
-
} catch (error) {
|
|
1120
|
-
const duration = Date.now() - startTime;
|
|
1121
|
-
spinner.fail(`Failed to connect ${c.dim(`(${formatDuration(duration)})`)}`);
|
|
1122
|
-
if (error instanceof Error) {
|
|
1123
|
-
logError(`Error: ${error.message}`);
|
|
1124
|
-
if (error.stack) {
|
|
1125
|
-
console.log(c.dim(error.stack));
|
|
1126
|
-
}
|
|
1127
|
-
}
|
|
1128
|
-
throw error;
|
|
1129
|
-
}
|
|
1130
|
-
}
|
|
1131
|
-
async function readLocalConfig() {
|
|
1132
|
-
const os2 = await import("os");
|
|
1133
|
-
const fs = await import("fs/promises");
|
|
1134
|
-
const path3 = await import("path");
|
|
1135
|
-
const configPath = path3.join(os2.homedir(), ".compute", "config.json");
|
|
1136
|
-
try {
|
|
1137
|
-
const content = await fs.readFile(configPath, "utf-8");
|
|
1138
|
-
return JSON.parse(content);
|
|
1139
|
-
} catch {
|
|
1140
|
-
return null;
|
|
1141
|
-
}
|
|
1142
|
-
}
|
|
1143
|
-
async function isLocalDaemonRunning() {
|
|
1144
|
-
const os2 = await import("os");
|
|
1145
|
-
const fs = await import("fs/promises");
|
|
1146
|
-
const path3 = await import("path");
|
|
1147
|
-
const pidPath = path3.join(os2.homedir(), ".compute", "compute.pid");
|
|
1148
|
-
try {
|
|
1149
|
-
const pidContent = await fs.readFile(pidPath, "utf-8");
|
|
1150
|
-
const pid = parseInt(pidContent.trim(), 10);
|
|
1151
|
-
process.kill(pid, 0);
|
|
1152
|
-
return true;
|
|
1153
|
-
} catch {
|
|
1154
|
-
return false;
|
|
1155
|
-
}
|
|
1156
|
-
}
|
|
1157
|
-
async function listLocalSandboxes() {
|
|
1158
|
-
const config = await readLocalConfig();
|
|
1159
|
-
if (!config) {
|
|
1160
|
-
logError("No local daemon config found at ~/.compute/config.json");
|
|
1161
|
-
console.log(c.dim('Run "compute start" to start the local daemon'));
|
|
1162
|
-
return;
|
|
1163
|
-
}
|
|
1164
|
-
const isRunning = await isLocalDaemonRunning();
|
|
1165
|
-
console.log("");
|
|
1166
|
-
console.log(c.bold("Local Daemon Status:"), isRunning ? c.green("Running") : c.red("Stopped"));
|
|
1167
|
-
console.log("");
|
|
1168
|
-
if (!config.sandboxes || config.sandboxes.length === 0) {
|
|
1169
|
-
console.log(c.dim("No sandboxes found"));
|
|
1170
|
-
return;
|
|
1171
|
-
}
|
|
1172
|
-
console.log(c.bold("Sandboxes:"));
|
|
1173
|
-
for (const sandbox of config.sandboxes) {
|
|
1174
|
-
const isMain = sandbox.subdomain === config.main_subdomain;
|
|
1175
|
-
const mainLabel = isMain ? c.green(" (main)") : "";
|
|
1176
|
-
console.log(` ${c.cyan(sandbox.subdomain)}${mainLabel}`);
|
|
1177
|
-
console.log(c.dim(` https://${sandbox.subdomain}.sandbox.computesdk.com`));
|
|
1178
|
-
}
|
|
1179
|
-
console.log("");
|
|
1180
|
-
console.log(c.dim(`Connect with: local ${config.main_subdomain}`));
|
|
1181
|
-
console.log("");
|
|
1182
|
-
}
|
|
1183
|
-
async function connectToLocal(state, subdomain) {
|
|
1184
|
-
const config = await readLocalConfig();
|
|
1185
|
-
if (!config) {
|
|
1186
|
-
logError("No local daemon config found at ~/.compute/config.json");
|
|
1187
|
-
console.log(c.dim('Run "compute start" to start the local daemon'));
|
|
1188
|
-
return;
|
|
1189
|
-
}
|
|
1190
|
-
const isRunning = await isLocalDaemonRunning();
|
|
1191
|
-
if (!isRunning) {
|
|
1192
|
-
logError("Local daemon is not running");
|
|
1193
|
-
console.log(c.dim('Run "compute start" to start the local daemon'));
|
|
1194
|
-
return;
|
|
1195
|
-
}
|
|
1196
|
-
const targetSubdomain = subdomain || config.main_subdomain;
|
|
1197
|
-
const sandbox = config.sandboxes.find((s) => s.subdomain === targetSubdomain);
|
|
1198
|
-
if (!sandbox) {
|
|
1199
|
-
logError(`Sandbox "${targetSubdomain}" not found`);
|
|
1200
|
-
console.log(c.dim('Run "local list" to see available sandboxes'));
|
|
1201
|
-
return;
|
|
1202
|
-
}
|
|
1203
|
-
const sandboxUrl = `https://${targetSubdomain}.sandbox.computesdk.com`;
|
|
1204
|
-
const token = config.access_token;
|
|
1205
|
-
if (hasSandbox(state)) {
|
|
1206
|
-
const shouldDisconnect = await confirm("Disconnect from current sandbox?", false, state);
|
|
1207
|
-
if (!shouldDisconnect) {
|
|
1208
|
-
logWarning("Keeping current sandbox. Connection cancelled.");
|
|
1209
|
-
return;
|
|
1210
|
-
}
|
|
1211
|
-
clearSandbox(state);
|
|
1212
|
-
}
|
|
1213
|
-
const spinner = new Spinner(`Connecting to local sandbox ${targetSubdomain}...`).start();
|
|
1214
|
-
const startTime = Date.now();
|
|
1215
|
-
try {
|
|
1216
|
-
const { Sandbox } = await import("computesdk");
|
|
1217
|
-
let WebSocket;
|
|
1218
|
-
try {
|
|
1219
|
-
const wsModule = await import("ws");
|
|
1220
|
-
WebSocket = wsModule.default;
|
|
1221
|
-
} catch {
|
|
1222
|
-
spinner.fail('Failed to import "ws" module');
|
|
1223
|
-
logError("Please install ws: pnpm add ws");
|
|
1224
|
-
throw new Error('Missing "ws" dependency');
|
|
1225
|
-
}
|
|
1226
|
-
const sandboxInstance = new Sandbox({
|
|
1227
|
-
sandboxUrl,
|
|
1228
|
-
sandboxId: targetSubdomain,
|
|
1229
|
-
provider: "local",
|
|
1230
|
-
token,
|
|
1231
|
-
WebSocket
|
|
1232
|
-
});
|
|
1233
|
-
const info = await sandboxInstance.getInfo();
|
|
1234
|
-
const duration = Date.now() - startTime;
|
|
1235
|
-
setSandbox(state, sandboxInstance, "local");
|
|
1236
|
-
state.verbose = true;
|
|
1237
|
-
process.env.COMPUTESDK_DEBUG = "1";
|
|
1238
|
-
spinner.succeed(`Connected to local sandbox ${c.dim(`(${formatDuration(duration)})`)}`);
|
|
1239
|
-
console.log(c.dim(`Sandbox: ${targetSubdomain}`));
|
|
1240
|
-
console.log(c.dim(`URL: ${sandboxUrl}`));
|
|
1241
|
-
console.log(c.dim(`Verbose mode: enabled (for debugging)`));
|
|
1242
|
-
} catch (error) {
|
|
1243
|
-
const duration = Date.now() - startTime;
|
|
1244
|
-
spinner.fail(`Failed to connect ${c.dim(`(${formatDuration(duration)})`)}`);
|
|
1245
|
-
if (error instanceof Error) {
|
|
1246
|
-
logError(`Error: ${error.message}`);
|
|
1247
|
-
}
|
|
1248
|
-
throw error;
|
|
1249
|
-
}
|
|
1250
|
-
}
|
|
1251
877
|
async function cleanupOnExit(state, replServer) {
|
|
1252
878
|
if (!hasSandbox(state)) {
|
|
1253
879
|
return;
|
|
@@ -1256,10 +882,6 @@ async function cleanupOnExit(state, replServer) {
|
|
|
1256
882
|
replServer.pause();
|
|
1257
883
|
}
|
|
1258
884
|
console.log("");
|
|
1259
|
-
if (state.currentProvider === "connected") {
|
|
1260
|
-
logWarning("Disconnecting from external sandbox (not destroying).");
|
|
1261
|
-
return;
|
|
1262
|
-
}
|
|
1263
885
|
const shouldDestroy = await confirm("Destroy active sandbox?", false, state);
|
|
1264
886
|
if (shouldDestroy) {
|
|
1265
887
|
await destroySandbox(state);
|
|
@@ -1298,6 +920,18 @@ function isCommand(value) {
|
|
|
1298
920
|
// src/cli/repl.ts
|
|
1299
921
|
import * as path2 from "path";
|
|
1300
922
|
import * as os from "os";
|
|
923
|
+
function getSandboxMetadata(sandbox) {
|
|
924
|
+
const instance = sandbox.getInstance?.();
|
|
925
|
+
if (typeof instance !== "object" || instance === null || !("config" in instance)) {
|
|
926
|
+
return {};
|
|
927
|
+
}
|
|
928
|
+
const config = instance.config;
|
|
929
|
+
if (typeof config !== "object" || config === null || !("metadata" in config)) {
|
|
930
|
+
return {};
|
|
931
|
+
}
|
|
932
|
+
const metadata = config.metadata;
|
|
933
|
+
return typeof metadata === "object" && metadata !== null ? metadata : {};
|
|
934
|
+
}
|
|
1301
935
|
function createREPL(state) {
|
|
1302
936
|
const replServer = repl.start({
|
|
1303
937
|
prompt: "> ",
|
|
@@ -1395,22 +1029,12 @@ function injectCmdContext(replServer) {
|
|
|
1395
1029
|
function injectWorkbenchCommands(replServer, state) {
|
|
1396
1030
|
replServer.context.provider = defineProviderCommand(state);
|
|
1397
1031
|
replServer.context.providers = () => showProviders();
|
|
1398
|
-
replServer.context.mode = async (modeName) => {
|
|
1399
|
-
if (!modeName) {
|
|
1400
|
-
showMode(state);
|
|
1401
|
-
} else {
|
|
1402
|
-
await toggleMode(state, modeName);
|
|
1403
|
-
}
|
|
1404
|
-
};
|
|
1405
1032
|
replServer.context.restart = async () => {
|
|
1406
1033
|
await restartSandbox(state);
|
|
1407
1034
|
};
|
|
1408
1035
|
replServer.context.destroy = async () => {
|
|
1409
1036
|
await destroySandbox(state);
|
|
1410
1037
|
};
|
|
1411
|
-
replServer.context.connect = async (url, token) => {
|
|
1412
|
-
await connectToSandbox(state, url, token);
|
|
1413
|
-
};
|
|
1414
1038
|
replServer.context.info = () => showInfo(state);
|
|
1415
1039
|
replServer.context.verbose = () => {
|
|
1416
1040
|
toggleVerbose(state);
|
|
@@ -1447,13 +1071,6 @@ function injectWorkbenchCommands(replServer, state) {
|
|
|
1447
1071
|
}
|
|
1448
1072
|
return sandboxReady.ready();
|
|
1449
1073
|
};
|
|
1450
|
-
replServer.context.runCode = async (code, runtime) => {
|
|
1451
|
-
const sandbox = state.currentSandbox;
|
|
1452
|
-
if (!sandbox) {
|
|
1453
|
-
throw new Error("No active sandbox. Run a command to auto-create one.");
|
|
1454
|
-
}
|
|
1455
|
-
return sandbox.runCode(code, runtime);
|
|
1456
|
-
};
|
|
1457
1074
|
replServer.context.runCommand = async (command, options) => {
|
|
1458
1075
|
const sandbox = state.currentSandbox;
|
|
1459
1076
|
if (!sandbox) {
|
|
@@ -1472,7 +1089,7 @@ function injectWorkbenchCommands(replServer, state) {
|
|
|
1472
1089
|
return {
|
|
1473
1090
|
sandboxId: sandbox2.sandboxId,
|
|
1474
1091
|
provider: sandbox2.provider,
|
|
1475
|
-
metadata: sandbox2
|
|
1092
|
+
metadata: getSandboxMetadata(sandbox2)
|
|
1476
1093
|
};
|
|
1477
1094
|
}
|
|
1478
1095
|
const compute2 = await getComputeInstance2(state);
|
|
@@ -1482,70 +1099,7 @@ function injectWorkbenchCommands(replServer, state) {
|
|
|
1482
1099
|
return {
|
|
1483
1100
|
sandboxId: sandbox.sandboxId,
|
|
1484
1101
|
provider: sandbox.provider,
|
|
1485
|
-
metadata: sandbox
|
|
1486
|
-
};
|
|
1487
|
-
};
|
|
1488
|
-
replServer.context.findOrCreate = async (options) => {
|
|
1489
|
-
if (state.useDirectMode) {
|
|
1490
|
-
throw new Error('Named sandboxes (findOrCreate) are only available in gateway mode. Use "mode gateway" to switch.');
|
|
1491
|
-
}
|
|
1492
|
-
const { getComputeInstance: getComputeInstance2, confirmSandboxSwitch: confirmSandboxSwitch2 } = await Promise.resolve().then(() => (init_commands(), commands_exports));
|
|
1493
|
-
const { setSandbox: setSandbox2 } = await Promise.resolve().then(() => (init_state(), state_exports));
|
|
1494
|
-
const { logSuccess: logSuccess2 } = await Promise.resolve().then(() => (init_output(), output_exports));
|
|
1495
|
-
const shouldSwitch = await confirmSandboxSwitch2(state);
|
|
1496
|
-
if (!shouldSwitch) {
|
|
1497
|
-
const compute3 = await getComputeInstance2(state);
|
|
1498
|
-
const sandbox2 = await compute3.sandbox.findOrCreate(options);
|
|
1499
|
-
return {
|
|
1500
|
-
sandboxId: sandbox2.sandboxId,
|
|
1501
|
-
provider: sandbox2.provider,
|
|
1502
|
-
name: options.name,
|
|
1503
|
-
namespace: options.namespace || "default",
|
|
1504
|
-
metadata: sandbox2.getInstance().config.metadata || {}
|
|
1505
|
-
};
|
|
1506
|
-
}
|
|
1507
|
-
const compute2 = await getComputeInstance2(state);
|
|
1508
|
-
const sandbox = await compute2.sandbox.findOrCreate(options);
|
|
1509
|
-
setSandbox2(state, sandbox, sandbox.provider);
|
|
1510
|
-
logSuccess2(`Switched to sandbox ${sandbox.sandboxId}`);
|
|
1511
|
-
return {
|
|
1512
|
-
sandboxId: sandbox.sandboxId,
|
|
1513
|
-
provider: sandbox.provider,
|
|
1514
|
-
name: options.name,
|
|
1515
|
-
namespace: options.namespace || "default",
|
|
1516
|
-
metadata: sandbox.getInstance().config.metadata || {}
|
|
1517
|
-
};
|
|
1518
|
-
};
|
|
1519
|
-
replServer.context.find = async (options) => {
|
|
1520
|
-
if (state.useDirectMode) {
|
|
1521
|
-
throw new Error('Named sandboxes (find) are only available in gateway mode. Use "mode gateway" to switch.');
|
|
1522
|
-
}
|
|
1523
|
-
const { getComputeInstance: getComputeInstance2, confirmSandboxSwitch: confirmSandboxSwitch2 } = await Promise.resolve().then(() => (init_commands(), commands_exports));
|
|
1524
|
-
const { setSandbox: setSandbox2 } = await Promise.resolve().then(() => (init_state(), state_exports));
|
|
1525
|
-
const { logSuccess: logSuccess2 } = await Promise.resolve().then(() => (init_output(), output_exports));
|
|
1526
|
-
const compute2 = await getComputeInstance2(state);
|
|
1527
|
-
const sandbox = await compute2.sandbox.find(options);
|
|
1528
|
-
if (!sandbox) {
|
|
1529
|
-
return null;
|
|
1530
|
-
}
|
|
1531
|
-
const shouldSwitch = await confirmSandboxSwitch2(state);
|
|
1532
|
-
if (!shouldSwitch) {
|
|
1533
|
-
return {
|
|
1534
|
-
sandboxId: sandbox.sandboxId,
|
|
1535
|
-
provider: sandbox.provider,
|
|
1536
|
-
name: options.name,
|
|
1537
|
-
namespace: options.namespace || "default",
|
|
1538
|
-
metadata: sandbox.getInstance().config.metadata || {}
|
|
1539
|
-
};
|
|
1540
|
-
}
|
|
1541
|
-
setSandbox2(state, sandbox, sandbox.provider);
|
|
1542
|
-
logSuccess2(`Switched to sandbox ${sandbox.sandboxId}`);
|
|
1543
|
-
return {
|
|
1544
|
-
sandboxId: sandbox.sandboxId,
|
|
1545
|
-
provider: sandbox.provider,
|
|
1546
|
-
name: options.name,
|
|
1547
|
-
namespace: options.namespace || "default",
|
|
1548
|
-
metadata: sandbox.getInstance().config.metadata || {}
|
|
1102
|
+
metadata: getSandboxMetadata(sandbox)
|
|
1549
1103
|
};
|
|
1550
1104
|
};
|
|
1551
1105
|
replServer.context.filesystem = {
|
|
@@ -1604,160 +1158,6 @@ function injectWorkbenchCommands(replServer, state) {
|
|
|
1604
1158
|
};
|
|
1605
1159
|
}
|
|
1606
1160
|
};
|
|
1607
|
-
replServer.context.child = {
|
|
1608
|
-
get create() {
|
|
1609
|
-
return async () => {
|
|
1610
|
-
if (state.useDirectMode) {
|
|
1611
|
-
throw new Error('Child sandboxes are only available in gateway mode. Use "mode gateway" to switch.');
|
|
1612
|
-
}
|
|
1613
|
-
const sandbox = state.currentSandbox;
|
|
1614
|
-
if (!sandbox) {
|
|
1615
|
-
throw new Error("No active sandbox. Run a command to auto-create one.");
|
|
1616
|
-
}
|
|
1617
|
-
const instance = sandbox.getInstance();
|
|
1618
|
-
return instance.child.create();
|
|
1619
|
-
};
|
|
1620
|
-
},
|
|
1621
|
-
get list() {
|
|
1622
|
-
return async () => {
|
|
1623
|
-
if (state.useDirectMode) {
|
|
1624
|
-
throw new Error('Child sandboxes are only available in gateway mode. Use "mode gateway" to switch.');
|
|
1625
|
-
}
|
|
1626
|
-
const sandbox = state.currentSandbox;
|
|
1627
|
-
if (!sandbox) {
|
|
1628
|
-
throw new Error("No active sandbox. Run a command to auto-create one.");
|
|
1629
|
-
}
|
|
1630
|
-
const instance = sandbox.getInstance();
|
|
1631
|
-
return instance.child.list();
|
|
1632
|
-
};
|
|
1633
|
-
},
|
|
1634
|
-
get retrieve() {
|
|
1635
|
-
return async (subdomain) => {
|
|
1636
|
-
if (state.useDirectMode) {
|
|
1637
|
-
throw new Error('Child sandboxes are only available in gateway mode. Use "mode gateway" to switch.');
|
|
1638
|
-
}
|
|
1639
|
-
const sandbox = state.currentSandbox;
|
|
1640
|
-
if (!sandbox) {
|
|
1641
|
-
throw new Error("No active sandbox. Run a command to auto-create one.");
|
|
1642
|
-
}
|
|
1643
|
-
const instance = sandbox.getInstance();
|
|
1644
|
-
return instance.child.retrieve(subdomain);
|
|
1645
|
-
};
|
|
1646
|
-
},
|
|
1647
|
-
get destroy() {
|
|
1648
|
-
return async (subdomain, options) => {
|
|
1649
|
-
if (state.useDirectMode) {
|
|
1650
|
-
throw new Error('Child sandboxes are only available in gateway mode. Use "mode gateway" to switch.');
|
|
1651
|
-
}
|
|
1652
|
-
const sandbox = state.currentSandbox;
|
|
1653
|
-
if (!sandbox) {
|
|
1654
|
-
throw new Error("No active sandbox. Run a command to auto-create one.");
|
|
1655
|
-
}
|
|
1656
|
-
const instance = sandbox.getInstance();
|
|
1657
|
-
return instance.child.destroy(subdomain, options);
|
|
1658
|
-
};
|
|
1659
|
-
}
|
|
1660
|
-
};
|
|
1661
|
-
replServer.context.server = {
|
|
1662
|
-
get start() {
|
|
1663
|
-
return async (options) => {
|
|
1664
|
-
const sandbox = state.currentSandbox;
|
|
1665
|
-
if (!sandbox) {
|
|
1666
|
-
throw new Error("No active sandbox. Run a command to auto-create one.");
|
|
1667
|
-
}
|
|
1668
|
-
return sandbox.server.start(options);
|
|
1669
|
-
};
|
|
1670
|
-
},
|
|
1671
|
-
get list() {
|
|
1672
|
-
return async () => {
|
|
1673
|
-
const sandbox = state.currentSandbox;
|
|
1674
|
-
if (!sandbox) {
|
|
1675
|
-
throw new Error("No active sandbox. Run a command to auto-create one.");
|
|
1676
|
-
}
|
|
1677
|
-
return sandbox.server.list();
|
|
1678
|
-
};
|
|
1679
|
-
},
|
|
1680
|
-
get retrieve() {
|
|
1681
|
-
return async (slug) => {
|
|
1682
|
-
const sandbox = state.currentSandbox;
|
|
1683
|
-
if (!sandbox) {
|
|
1684
|
-
throw new Error("No active sandbox. Run a command to auto-create one.");
|
|
1685
|
-
}
|
|
1686
|
-
return sandbox.server.retrieve(slug);
|
|
1687
|
-
};
|
|
1688
|
-
},
|
|
1689
|
-
get stop() {
|
|
1690
|
-
return async (slug) => {
|
|
1691
|
-
const sandbox = state.currentSandbox;
|
|
1692
|
-
if (!sandbox) {
|
|
1693
|
-
throw new Error("No active sandbox. Run a command to auto-create one.");
|
|
1694
|
-
}
|
|
1695
|
-
return sandbox.server.stop(slug);
|
|
1696
|
-
};
|
|
1697
|
-
},
|
|
1698
|
-
get restart() {
|
|
1699
|
-
return async (slug) => {
|
|
1700
|
-
const sandbox = state.currentSandbox;
|
|
1701
|
-
if (!sandbox) {
|
|
1702
|
-
throw new Error("No active sandbox. Run a command to auto-create one.");
|
|
1703
|
-
}
|
|
1704
|
-
return sandbox.server.restart(slug);
|
|
1705
|
-
};
|
|
1706
|
-
}
|
|
1707
|
-
};
|
|
1708
|
-
replServer.context.terminal = {
|
|
1709
|
-
get create() {
|
|
1710
|
-
return async (options) => {
|
|
1711
|
-
const sandbox = state.currentSandbox;
|
|
1712
|
-
if (!sandbox) {
|
|
1713
|
-
throw new Error("No active sandbox. Run a command to auto-create one.");
|
|
1714
|
-
}
|
|
1715
|
-
const term = await sandbox.terminal.create(options);
|
|
1716
|
-
if (state.verbose) {
|
|
1717
|
-
if (term._ws) {
|
|
1718
|
-
term._ws.config.debug = true;
|
|
1719
|
-
}
|
|
1720
|
-
term.on("output", (data) => {
|
|
1721
|
-
console.log("[terminal:output]", JSON.stringify(data));
|
|
1722
|
-
});
|
|
1723
|
-
term.on("error", (error) => {
|
|
1724
|
-
console.log("[terminal:error]", error);
|
|
1725
|
-
});
|
|
1726
|
-
term.on("destroyed", () => {
|
|
1727
|
-
console.log("[terminal:destroyed]");
|
|
1728
|
-
});
|
|
1729
|
-
}
|
|
1730
|
-
return term;
|
|
1731
|
-
};
|
|
1732
|
-
},
|
|
1733
|
-
get list() {
|
|
1734
|
-
return async () => {
|
|
1735
|
-
const sandbox = state.currentSandbox;
|
|
1736
|
-
if (!sandbox) {
|
|
1737
|
-
throw new Error("No active sandbox. Run a command to auto-create one.");
|
|
1738
|
-
}
|
|
1739
|
-
return sandbox.terminal.list();
|
|
1740
|
-
};
|
|
1741
|
-
},
|
|
1742
|
-
get retrieve() {
|
|
1743
|
-
return async (id) => {
|
|
1744
|
-
const sandbox = state.currentSandbox;
|
|
1745
|
-
if (!sandbox) {
|
|
1746
|
-
throw new Error("No active sandbox. Run a command to auto-create one.");
|
|
1747
|
-
}
|
|
1748
|
-
return sandbox.terminal.retrieve(id);
|
|
1749
|
-
};
|
|
1750
|
-
},
|
|
1751
|
-
get destroy() {
|
|
1752
|
-
return async (id) => {
|
|
1753
|
-
const sandbox = state.currentSandbox;
|
|
1754
|
-
if (!sandbox) {
|
|
1755
|
-
throw new Error("No active sandbox. Run a command to auto-create one.");
|
|
1756
|
-
}
|
|
1757
|
-
return sandbox.terminal.destroy(id);
|
|
1758
|
-
};
|
|
1759
|
-
}
|
|
1760
|
-
};
|
|
1761
1161
|
replServer.context.getInstance = () => {
|
|
1762
1162
|
const sandbox = state.currentSandbox;
|
|
1763
1163
|
if (!sandbox) {
|
|
@@ -1768,36 +1168,13 @@ function injectWorkbenchCommands(replServer, state) {
|
|
|
1768
1168
|
}
|
|
1769
1169
|
function setupSmartEvaluator(replServer, state) {
|
|
1770
1170
|
const originalEval = replServer.eval;
|
|
1771
|
-
const workbenchCommands = /* @__PURE__ */ new Set(["help", "providers", "info", "env", "restart", "destroy", "
|
|
1171
|
+
const workbenchCommands = /* @__PURE__ */ new Set(["help", "providers", "info", "env", "restart", "destroy", "verbose", "sandboxInfo"]);
|
|
1772
1172
|
replServer.eval = function(cmd2, context, filename, callback) {
|
|
1773
1173
|
const trimmedCmd = cmd2.trim();
|
|
1774
|
-
const
|
|
1775
|
-
if (providerLocalMatch) {
|
|
1776
|
-
const arg = providerLocalMatch[1];
|
|
1777
|
-
const providerCmd = arg ? `await provider('local', '${arg}')` : `await provider('local')`;
|
|
1778
|
-
originalEval.call(this, providerCmd, context, filename, callback);
|
|
1779
|
-
return;
|
|
1780
|
-
}
|
|
1781
|
-
const providerMatch = trimmedCmd.match(/^provider(?:\s+(direct|gateway))?\s+([\w-]+)$/);
|
|
1174
|
+
const providerMatch = trimmedCmd.match(/^provider\s+([\w-]+)$/);
|
|
1782
1175
|
if (providerMatch) {
|
|
1783
|
-
const
|
|
1784
|
-
|
|
1785
|
-
const providerCmd = mode ? `await provider('${mode}', '${providerName}')` : `await provider('${providerName}')`;
|
|
1786
|
-
originalEval.call(this, providerCmd, context, filename, callback);
|
|
1787
|
-
return;
|
|
1788
|
-
}
|
|
1789
|
-
const providerOnlyMatch = trimmedCmd.match(/^provider\s+(direct|gateway)$/);
|
|
1790
|
-
if (providerOnlyMatch) {
|
|
1791
|
-
const mode = providerOnlyMatch[1];
|
|
1792
|
-
const providerCmd = `await provider('${mode}')`;
|
|
1793
|
-
originalEval.call(this, providerCmd, context, filename, callback);
|
|
1794
|
-
return;
|
|
1795
|
-
}
|
|
1796
|
-
const modeMatch = trimmedCmd.match(/^mode\s+(gateway|direct)$/);
|
|
1797
|
-
if (modeMatch) {
|
|
1798
|
-
const modeName = modeMatch[1];
|
|
1799
|
-
const modeCmd = `await mode('${modeName}')`;
|
|
1800
|
-
originalEval.call(this, modeCmd, context, filename, callback);
|
|
1176
|
+
const providerName = providerMatch[1];
|
|
1177
|
+
originalEval.call(this, `await provider('${providerName}')`, context, filename, callback);
|
|
1801
1178
|
return;
|
|
1802
1179
|
}
|
|
1803
1180
|
const dollarMatch = trimmedCmd.match(/^\$(.+)$/);
|
|
@@ -1850,14 +1227,10 @@ function setupSmartEvaluator(replServer, state) {
|
|
|
1850
1227
|
function setupAutocomplete(replServer, state) {
|
|
1851
1228
|
const originalCompleter = replServer.completer;
|
|
1852
1229
|
const workbenchCommands = {
|
|
1853
|
-
"provider": [...PROVIDER_NAMES
|
|
1854
|
-
// Include 'local' as a provider option
|
|
1855
|
-
"mode": ["gateway", "direct"],
|
|
1230
|
+
"provider": [...PROVIDER_NAMES],
|
|
1856
1231
|
"providers": [],
|
|
1857
1232
|
"restart": [],
|
|
1858
1233
|
"destroy": [],
|
|
1859
|
-
"connect": [],
|
|
1860
|
-
// Connect takes a URL argument
|
|
1861
1234
|
"info": [],
|
|
1862
1235
|
"env": [],
|
|
1863
1236
|
"help": [],
|
|
@@ -1866,7 +1239,6 @@ function setupAutocomplete(replServer, state) {
|
|
|
1866
1239
|
".exit": [],
|
|
1867
1240
|
// Sandbox methods
|
|
1868
1241
|
"getUrl": [],
|
|
1869
|
-
"runCode": [],
|
|
1870
1242
|
"sandboxInfo": [],
|
|
1871
1243
|
"getInstance": []
|
|
1872
1244
|
// Filesystem is an object, so it gets dot notation autocomplete automatically
|
|
@@ -1956,39 +1328,10 @@ init_providers();
|
|
|
1956
1328
|
init_commands();
|
|
1957
1329
|
async function startWorkbench() {
|
|
1958
1330
|
const state = createState();
|
|
1959
|
-
const localDaemonRunning = await isLocalDaemonRunning();
|
|
1960
1331
|
state.availableProviders = getAvailableProviders();
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
}
|
|
1964
|
-
const detectedProvider = autoDetectProvider();
|
|
1965
|
-
if (localDaemonRunning) {
|
|
1966
|
-
state.currentProvider = "local";
|
|
1967
|
-
state.useDirectMode = false;
|
|
1968
|
-
state.verbose = true;
|
|
1969
|
-
process.env.COMPUTESDK_DEBUG = "1";
|
|
1970
|
-
} else {
|
|
1971
|
-
const hasGateway = state.availableProviders.includes("gateway");
|
|
1972
|
-
const backendProviders = state.availableProviders.filter((p) => p !== "gateway" && p !== "local");
|
|
1973
|
-
if (hasGateway && backendProviders.length > 0) {
|
|
1974
|
-
state.currentProvider = backendProviders[0] || "e2b";
|
|
1975
|
-
state.useDirectMode = false;
|
|
1976
|
-
} else if (backendProviders.length > 0) {
|
|
1977
|
-
state.currentProvider = backendProviders[0];
|
|
1978
|
-
state.useDirectMode = true;
|
|
1979
|
-
} else {
|
|
1980
|
-
state.currentProvider = detectedProvider;
|
|
1981
|
-
state.useDirectMode = false;
|
|
1982
|
-
}
|
|
1983
|
-
}
|
|
1984
|
-
showWelcome(state.availableProviders, state.currentProvider, state.useDirectMode, localDaemonRunning);
|
|
1332
|
+
state.currentProvider = state.availableProviders[0] ?? null;
|
|
1333
|
+
showWelcome(state.availableProviders, state.currentProvider);
|
|
1985
1334
|
const replServer = createREPL(state);
|
|
1986
|
-
if (localDaemonRunning) {
|
|
1987
|
-
try {
|
|
1988
|
-
await connectToLocal(state);
|
|
1989
|
-
} catch {
|
|
1990
|
-
}
|
|
1991
|
-
}
|
|
1992
1335
|
replServer.on("exit", async () => {
|
|
1993
1336
|
await cleanupOnExit(state, replServer);
|
|
1994
1337
|
process.exit(0);
|