@tpmjs/tools-sprites-exec 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.ts +24 -0
- package/dist/index.js +108 -0
- package/package.json +52 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as ai from 'ai';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Sprites Exec Tool for TPMJS
|
|
5
|
+
* Executes a command inside a sprite and returns the output.
|
|
6
|
+
* Supports stdin input for interactive commands.
|
|
7
|
+
*
|
|
8
|
+
* @requires SPRITES_TOKEN environment variable
|
|
9
|
+
*/
|
|
10
|
+
interface ExecResult {
|
|
11
|
+
exitCode: number;
|
|
12
|
+
stdout: string;
|
|
13
|
+
stderr: string;
|
|
14
|
+
duration: number;
|
|
15
|
+
}
|
|
16
|
+
type SpritesExecInput = {
|
|
17
|
+
name: string;
|
|
18
|
+
cmd: string;
|
|
19
|
+
stdin?: string;
|
|
20
|
+
timeoutMs?: number;
|
|
21
|
+
};
|
|
22
|
+
declare const spritesExecTool: ai.Tool<SpritesExecInput, ExecResult>;
|
|
23
|
+
|
|
24
|
+
export { type ExecResult, spritesExecTool as default, spritesExecTool };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { tool, jsonSchema } from 'ai';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
var SPRITES_API_BASE = "https://api.sprites.dev/v1";
|
|
5
|
+
function getSpritesToken() {
|
|
6
|
+
const token = process.env.SPRITES_TOKEN;
|
|
7
|
+
if (!token) {
|
|
8
|
+
throw new Error(
|
|
9
|
+
"SPRITES_TOKEN environment variable is required. Get your token from https://sprites.dev"
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
return token;
|
|
13
|
+
}
|
|
14
|
+
var spritesExecTool = tool({
|
|
15
|
+
description: "Execute a command inside a sprite and return the output. Supports stdin input for interactive commands. Returns exit code, stdout, stderr, and execution duration.",
|
|
16
|
+
inputSchema: jsonSchema({
|
|
17
|
+
type: "object",
|
|
18
|
+
properties: {
|
|
19
|
+
name: {
|
|
20
|
+
type: "string",
|
|
21
|
+
description: "Name of the sprite to execute command in"
|
|
22
|
+
},
|
|
23
|
+
cmd: {
|
|
24
|
+
type: "string",
|
|
25
|
+
description: "Command to execute (e.g., 'ls -la', 'python script.py')"
|
|
26
|
+
},
|
|
27
|
+
stdin: {
|
|
28
|
+
type: "string",
|
|
29
|
+
description: "Optional stdin input to pass to the command"
|
|
30
|
+
},
|
|
31
|
+
timeoutMs: {
|
|
32
|
+
type: "number",
|
|
33
|
+
description: "Execution timeout in milliseconds (default: 60000)"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
required: ["name", "cmd"],
|
|
37
|
+
additionalProperties: false
|
|
38
|
+
}),
|
|
39
|
+
async execute({ name, cmd, stdin, timeoutMs }) {
|
|
40
|
+
if (!name || typeof name !== "string") {
|
|
41
|
+
throw new Error("Sprite name is required and must be a string");
|
|
42
|
+
}
|
|
43
|
+
if (!cmd || typeof cmd !== "string") {
|
|
44
|
+
throw new Error("Command is required and must be a string");
|
|
45
|
+
}
|
|
46
|
+
const token = getSpritesToken();
|
|
47
|
+
const timeout = timeoutMs || 6e4;
|
|
48
|
+
const startTime = Date.now();
|
|
49
|
+
let response;
|
|
50
|
+
try {
|
|
51
|
+
const controller = new AbortController();
|
|
52
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
53
|
+
const body = { cmd };
|
|
54
|
+
if (stdin) {
|
|
55
|
+
body.stdin = stdin;
|
|
56
|
+
}
|
|
57
|
+
response = await fetch(`${SPRITES_API_BASE}/sprites/${encodeURIComponent(name)}/exec`, {
|
|
58
|
+
method: "POST",
|
|
59
|
+
headers: {
|
|
60
|
+
Authorization: `Bearer ${token}`,
|
|
61
|
+
"Content-Type": "application/json",
|
|
62
|
+
"User-Agent": "TPMJS/1.0"
|
|
63
|
+
},
|
|
64
|
+
body: JSON.stringify(body),
|
|
65
|
+
signal: controller.signal
|
|
66
|
+
});
|
|
67
|
+
clearTimeout(timeoutId);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if (error instanceof Error) {
|
|
70
|
+
if (error.name === "AbortError") {
|
|
71
|
+
throw new Error(
|
|
72
|
+
`Command execution in sprite "${name}" timed out after ${timeout}ms`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
throw new Error(`Failed to execute command in sprite "${name}": ${error.message}`);
|
|
76
|
+
}
|
|
77
|
+
throw new Error(`Failed to execute command in sprite "${name}": Unknown network error`);
|
|
78
|
+
}
|
|
79
|
+
const duration = Date.now() - startTime;
|
|
80
|
+
if (!response.ok) {
|
|
81
|
+
if (response.status === 404) {
|
|
82
|
+
throw new Error(`Sprite "${name}" not found`);
|
|
83
|
+
}
|
|
84
|
+
if (response.status === 401) {
|
|
85
|
+
throw new Error("Invalid SPRITES_TOKEN. Check your API token at https://sprites.dev");
|
|
86
|
+
}
|
|
87
|
+
const errorText = await response.text().catch(() => "Unknown error");
|
|
88
|
+
throw new Error(
|
|
89
|
+
`Failed to execute command in sprite "${name}": HTTP ${response.status} - ${errorText}`
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
let data;
|
|
93
|
+
try {
|
|
94
|
+
data = await response.json();
|
|
95
|
+
} catch {
|
|
96
|
+
throw new Error("Failed to parse response from Sprites API");
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
exitCode: data.exitCode ?? data.exit_code ?? 0,
|
|
100
|
+
stdout: data.stdout || "",
|
|
101
|
+
stderr: data.stderr || "",
|
|
102
|
+
duration: data.duration || duration
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
var index_default = spritesExecTool;
|
|
107
|
+
|
|
108
|
+
export { index_default as default, spritesExecTool };
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tpmjs/tools-sprites-exec",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Execute a command inside a sprite and return the output with exit code",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": ["tpmjs", "sprites", "sandbox", "code-execution", "ai"],
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": ["dist"],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsup",
|
|
16
|
+
"dev": "tsup --watch",
|
|
17
|
+
"type-check": "tsc --noEmit",
|
|
18
|
+
"clean": "rm -rf dist .turbo"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@tpmjs/tsconfig": "workspace:*",
|
|
22
|
+
"tsup": "^8.5.1",
|
|
23
|
+
"typescript": "^5.9.3"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": { "access": "public" },
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/anthropics/tpmjs.git",
|
|
29
|
+
"directory": "packages/tools/official/sprites-exec"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://tpmjs.com",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"tpmjs": {
|
|
34
|
+
"category": "sandbox",
|
|
35
|
+
"frameworks": ["vercel-ai"],
|
|
36
|
+
"tools": [{
|
|
37
|
+
"name": "spritesExecTool",
|
|
38
|
+
"description": "Execute a command inside a sprite and return the output with exit code",
|
|
39
|
+
"parameters": [
|
|
40
|
+
{ "name": "name", "type": "string", "description": "Name of the sprite to execute command in", "required": true },
|
|
41
|
+
{ "name": "cmd", "type": "string", "description": "Command to execute", "required": true },
|
|
42
|
+
{ "name": "stdin", "type": "string", "description": "Optional stdin input", "required": false },
|
|
43
|
+
{ "name": "timeoutMs", "type": "number", "description": "Execution timeout in milliseconds", "required": false }
|
|
44
|
+
],
|
|
45
|
+
"returns": {
|
|
46
|
+
"type": "ExecResult",
|
|
47
|
+
"description": "Command output with exitCode, stdout, stderr, and duration"
|
|
48
|
+
}
|
|
49
|
+
}]
|
|
50
|
+
},
|
|
51
|
+
"dependencies": { "ai": "6.0.23" }
|
|
52
|
+
}
|