create-tangle-sandbox 0.1.0
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/dist/index.d.mts +1 -0
- package/dist/index.mjs +84 -0
- package/package.json +28 -0
- package/templates/agent/.env.example +1 -0
- package/templates/agent/package.json +17 -0
- package/templates/agent/src/index.ts +53 -0
- package/templates/agent/tsconfig.json +14 -0
- package/templates/basic/.env.example +1 -0
- package/templates/basic/package.json +17 -0
- package/templates/basic/src/index.ts +40 -0
- package/templates/basic/tsconfig.json +14 -0
- package/templates/web-scraper/.env.example +1 -0
- package/templates/web-scraper/package.json +17 -0
- package/templates/web-scraper/src/index.ts +70 -0
- package/templates/web-scraper/tsconfig.json +14 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { createInterface } from "node:readline";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { program } from "commander";
|
|
7
|
+
//#region src/index.ts
|
|
8
|
+
const TEMPLATES_DIR = join(dirname(fileURLToPath(import.meta.url)), "..", "templates");
|
|
9
|
+
const TEMPLATES = [
|
|
10
|
+
"basic",
|
|
11
|
+
"agent",
|
|
12
|
+
"web-scraper"
|
|
13
|
+
];
|
|
14
|
+
const TEMPLATE_DESCRIPTIONS = {
|
|
15
|
+
basic: "Minimal: create sandbox, execute commands, cleanup",
|
|
16
|
+
agent: "Agent task execution with streaming output",
|
|
17
|
+
"web-scraper": "Headless browser sandbox for web scraping"
|
|
18
|
+
};
|
|
19
|
+
function prompt(question) {
|
|
20
|
+
const rl = createInterface({
|
|
21
|
+
input: process.stdin,
|
|
22
|
+
output: process.stdout
|
|
23
|
+
});
|
|
24
|
+
return new Promise((resolve) => {
|
|
25
|
+
rl.question(question, (answer) => {
|
|
26
|
+
rl.close();
|
|
27
|
+
resolve(answer.trim());
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
async function selectTemplate() {
|
|
32
|
+
console.log("\nAvailable templates:\n");
|
|
33
|
+
TEMPLATES.forEach((t, i) => {
|
|
34
|
+
console.log(` ${i + 1}. ${t} — ${TEMPLATE_DESCRIPTIONS[t]}`);
|
|
35
|
+
});
|
|
36
|
+
const answer = await prompt(`\nSelect a template (1-${TEMPLATES.length}): `);
|
|
37
|
+
const index = Number.parseInt(answer, 10) - 1;
|
|
38
|
+
if (index >= 0 && index < TEMPLATES.length) return TEMPLATES[index];
|
|
39
|
+
console.log("Invalid selection, using 'basic'");
|
|
40
|
+
return "basic";
|
|
41
|
+
}
|
|
42
|
+
function scaffoldProject(projectDir, template, projectName) {
|
|
43
|
+
const templateDir = join(TEMPLATES_DIR, template);
|
|
44
|
+
if (!existsSync(templateDir)) {
|
|
45
|
+
console.error(`Template '${template}' not found at ${templateDir}`);
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
mkdirSync(projectDir, { recursive: true });
|
|
49
|
+
cpSync(templateDir, projectDir, { recursive: true });
|
|
50
|
+
const pkgPath = join(projectDir, "package.json");
|
|
51
|
+
if (existsSync(pkgPath)) {
|
|
52
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
|
|
53
|
+
pkg.name = projectName;
|
|
54
|
+
writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`);
|
|
55
|
+
}
|
|
56
|
+
console.log(`\nProject created at ${projectDir}\n`);
|
|
57
|
+
console.log("Next steps:\n");
|
|
58
|
+
console.log(` cd ${projectName}`);
|
|
59
|
+
console.log(" npm install");
|
|
60
|
+
console.log(" cp .env.example .env # add your TANGLE_API_KEY");
|
|
61
|
+
console.log(" npm start\n");
|
|
62
|
+
}
|
|
63
|
+
program.name("create-tangle-sandbox").description("Scaffold a new Tangle Sandbox project").argument("[project-name]", "Project directory name").option("-t, --template <template>", "Template to use (basic, agent, web-scraper)").action(async (inputProjectName, opts) => {
|
|
64
|
+
console.log("\n create-tangle-sandbox\n");
|
|
65
|
+
let projectName = inputProjectName;
|
|
66
|
+
if (!projectName) {
|
|
67
|
+
projectName = await prompt("Project name: ");
|
|
68
|
+
if (!projectName) {
|
|
69
|
+
console.error("Project name is required");
|
|
70
|
+
process.exit(1);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
let template = opts?.template;
|
|
74
|
+
if (!template || !TEMPLATES.includes(template)) template = await selectTemplate();
|
|
75
|
+
const projectDir = resolve(process.cwd(), projectName);
|
|
76
|
+
if (existsSync(projectDir) && readdirSync(projectDir).length > 0) {
|
|
77
|
+
console.error(`Directory '${projectName}' already exists and is not empty`);
|
|
78
|
+
process.exit(1);
|
|
79
|
+
}
|
|
80
|
+
scaffoldProject(projectDir, template, projectName);
|
|
81
|
+
});
|
|
82
|
+
program.parse();
|
|
83
|
+
//#endregion
|
|
84
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "create-tangle-sandbox",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Scaffold a new Tangle Sandbox project",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"create-tangle-sandbox": "./dist/index.mjs"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"templates"
|
|
12
|
+
],
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"commander": "12.1.0"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/node": "25.6.0",
|
|
18
|
+
"tsdown": "0.21.10",
|
|
19
|
+
"tsx": "^4.19.0",
|
|
20
|
+
"typescript": "^6.0.3"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsdown src/index.ts --format esm --dts",
|
|
24
|
+
"check-types": "tsc --noEmit",
|
|
25
|
+
"dev": "tsx src/index.ts",
|
|
26
|
+
"test": "node --test"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
TANGLE_API_KEY=sk-tan-YOUR_KEY_HERE
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "my-sandbox-agent",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"start": "tsx src/index.ts",
|
|
7
|
+
"build": "tsc"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@tangle-network/sandbox": "latest"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@types/node": "^22.0.0",
|
|
14
|
+
"tsx": "^4.19.0",
|
|
15
|
+
"typescript": "^6.0.2"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Sandbox } from "@tangle-network/sandbox";
|
|
2
|
+
|
|
3
|
+
const apiKey = process.env.TANGLE_API_KEY;
|
|
4
|
+
const baseUrl = process.env.TANGLE_BASE_URL;
|
|
5
|
+
|
|
6
|
+
if (!apiKey) {
|
|
7
|
+
throw new Error("TANGLE_API_KEY environment variable is required");
|
|
8
|
+
}
|
|
9
|
+
if (!baseUrl) {
|
|
10
|
+
throw new Error("TANGLE_BASE_URL environment variable is required");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const client = new Sandbox({ apiKey, baseUrl });
|
|
14
|
+
|
|
15
|
+
async function runTask(task: string) {
|
|
16
|
+
console.log(`Creating sandbox for task: ${task}`);
|
|
17
|
+
|
|
18
|
+
const sandbox = await client.create({
|
|
19
|
+
environment: "universal",
|
|
20
|
+
env: { TASK: task },
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
await sandbox.waitFor("running");
|
|
24
|
+
|
|
25
|
+
console.log(`Sandbox ready: ${sandbox.id}`);
|
|
26
|
+
|
|
27
|
+
// Send a prompt to the agent and stream the response.
|
|
28
|
+
for await (const event of sandbox.streamPrompt(task, {
|
|
29
|
+
model: "anthropic/claude-sonnet-4-20250514",
|
|
30
|
+
})) {
|
|
31
|
+
if (event.type === "message.part.updated") {
|
|
32
|
+
const part = event.data.part as { type?: string };
|
|
33
|
+
if (part?.type === "text") {
|
|
34
|
+
process.stdout.write((event.data.delta as string | undefined) ?? "");
|
|
35
|
+
}
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (event.type === "error") {
|
|
40
|
+
console.error(
|
|
41
|
+
`\n[Error: ${String((event.data as { message?: unknown }).message ?? "unknown")}]`,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
console.log("\n\nTask completed. Cleaning up...");
|
|
47
|
+
await sandbox.delete();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const task =
|
|
51
|
+
process.argv[2] ??
|
|
52
|
+
"Create a simple Express.js server with a /health endpoint";
|
|
53
|
+
runTask(task).catch(console.error);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"outDir": "dist",
|
|
9
|
+
"rootDir": "src",
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"types": ["node"]
|
|
12
|
+
},
|
|
13
|
+
"include": ["src"]
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
TANGLE_API_KEY=sk-tan-YOUR_KEY_HERE
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "my-sandbox-project",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"start": "tsx src/index.ts",
|
|
7
|
+
"build": "tsc"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@tangle-network/sandbox": "latest"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@types/node": "^22.0.0",
|
|
14
|
+
"tsx": "^4.19.0",
|
|
15
|
+
"typescript": "^6.0.2"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Sandbox } from "@tangle-network/sandbox";
|
|
2
|
+
|
|
3
|
+
const apiKey = process.env.TANGLE_API_KEY;
|
|
4
|
+
const baseUrl = process.env.TANGLE_BASE_URL;
|
|
5
|
+
|
|
6
|
+
if (!apiKey) {
|
|
7
|
+
throw new Error("TANGLE_API_KEY environment variable is required");
|
|
8
|
+
}
|
|
9
|
+
if (!baseUrl) {
|
|
10
|
+
throw new Error("TANGLE_BASE_URL environment variable is required");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const client = new Sandbox({ apiKey, baseUrl });
|
|
14
|
+
|
|
15
|
+
async function main() {
|
|
16
|
+
console.log("Creating sandbox...");
|
|
17
|
+
|
|
18
|
+
const sandbox = await client.create({
|
|
19
|
+
environment: "ubuntu:22.04",
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
await sandbox.waitFor("running");
|
|
23
|
+
|
|
24
|
+
console.log(`Sandbox created: ${sandbox.id}`);
|
|
25
|
+
console.log(`Status: ${sandbox.status}`);
|
|
26
|
+
|
|
27
|
+
if (sandbox.url) {
|
|
28
|
+
console.log(`Web terminal: ${sandbox.url}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Execute a command
|
|
32
|
+
const result = await sandbox.exec("echo 'Hello from Tangle Sandbox!'");
|
|
33
|
+
console.log(`Output: ${result.stdout}`);
|
|
34
|
+
|
|
35
|
+
// Cleanup
|
|
36
|
+
await sandbox.delete();
|
|
37
|
+
console.log("Sandbox deleted");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"outDir": "dist",
|
|
9
|
+
"rootDir": "src",
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"types": ["node"]
|
|
12
|
+
},
|
|
13
|
+
"include": ["src"]
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
TANGLE_API_KEY=sk-tan-YOUR_KEY_HERE
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "my-sandbox-scraper",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"start": "tsx src/index.ts",
|
|
7
|
+
"build": "tsc"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@tangle-network/sandbox": "latest"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@types/node": "^22.0.0",
|
|
14
|
+
"tsx": "^4.19.0",
|
|
15
|
+
"typescript": "^6.0.2"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Sandbox } from "@tangle-network/sandbox";
|
|
2
|
+
|
|
3
|
+
const apiKey = process.env.TANGLE_API_KEY;
|
|
4
|
+
const baseUrl = process.env.TANGLE_BASE_URL;
|
|
5
|
+
|
|
6
|
+
if (!apiKey) {
|
|
7
|
+
throw new Error("TANGLE_API_KEY environment variable is required");
|
|
8
|
+
}
|
|
9
|
+
if (!baseUrl) {
|
|
10
|
+
throw new Error("TANGLE_BASE_URL environment variable is required");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const client = new Sandbox({ apiKey, baseUrl });
|
|
14
|
+
|
|
15
|
+
async function scrape(url: string) {
|
|
16
|
+
console.log("Setting up headless browser sandbox...");
|
|
17
|
+
|
|
18
|
+
const sandbox = await client.create({
|
|
19
|
+
environment: "universal",
|
|
20
|
+
resources: { cpuCores: 2, memoryMB: 2048 },
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
await sandbox.waitFor("running");
|
|
24
|
+
|
|
25
|
+
console.log(`Sandbox ready: ${sandbox.id}`);
|
|
26
|
+
|
|
27
|
+
// Install Playwright inside the sandbox
|
|
28
|
+
await sandbox.exec("npm install playwright");
|
|
29
|
+
await sandbox.exec("npx playwright install chromium --with-deps");
|
|
30
|
+
|
|
31
|
+
// Write the scraper script to the workspace root (relative path).
|
|
32
|
+
await sandbox.write(
|
|
33
|
+
"scraper.js",
|
|
34
|
+
`
|
|
35
|
+
const { chromium } = require('playwright');
|
|
36
|
+
|
|
37
|
+
(async () => {
|
|
38
|
+
const browser = await chromium.launch();
|
|
39
|
+
const page = await browser.newPage();
|
|
40
|
+
await page.goto(${JSON.stringify(url)});
|
|
41
|
+
|
|
42
|
+
const title = await page.title();
|
|
43
|
+
const text = await page.innerText('body');
|
|
44
|
+
|
|
45
|
+
console.log(JSON.stringify({
|
|
46
|
+
title,
|
|
47
|
+
textLength: text.length,
|
|
48
|
+
preview: text.slice(0, 500),
|
|
49
|
+
}, null, 2));
|
|
50
|
+
|
|
51
|
+
await browser.close();
|
|
52
|
+
})();
|
|
53
|
+
`,
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
// Run the scraper
|
|
57
|
+
const result = await sandbox.exec("node scraper.js");
|
|
58
|
+
console.log("Scrape result:");
|
|
59
|
+
console.log(result.stdout);
|
|
60
|
+
|
|
61
|
+
if (result.stderr) {
|
|
62
|
+
console.error("Errors:", result.stderr);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
await sandbox.delete();
|
|
66
|
+
console.log("Done!");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const url = process.argv[2] ?? "https://example.com";
|
|
70
|
+
scrape(url).catch(console.error);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"outDir": "dist",
|
|
9
|
+
"rootDir": "src",
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"types": ["node"]
|
|
12
|
+
},
|
|
13
|
+
"include": ["src"]
|
|
14
|
+
}
|