@capsule-run/sdk 0.5.2 → 0.6.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 +49 -1
- package/dist/app.d.ts +1 -0
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js.map +1 -1
- package/dist/run.d.ts +34 -0
- package/dist/run.d.ts.map +1 -0
- package/dist/run.js +60 -0
- package/dist/run.js.map +1 -0
- package/dist/task.d.ts +2 -0
- package/dist/task.d.ts.map +1 -1
- package/dist/task.js +2 -0
- package/dist/task.js.map +1 -1
- package/package.json +6 -1
- package/src/app.ts +1 -0
- package/src/index.ts +1 -0
- package/src/run.ts +89 -0
- package/src/task.ts +4 -0
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ npm install -g @capsule-run/cli
|
|
|
20
20
|
npm install @capsule-run/sdk
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
##
|
|
23
|
+
## Getting started
|
|
24
24
|
|
|
25
25
|
Create `hello.ts`:
|
|
26
26
|
|
|
@@ -44,6 +44,35 @@ capsule run hello.ts
|
|
|
44
44
|
|
|
45
45
|
> Use `--verbose` to display real-time task execution details.
|
|
46
46
|
|
|
47
|
+
## Integrate Into an Existing Project
|
|
48
|
+
|
|
49
|
+
The `run()` function lets you execute tasks programmatically from your application code, no CLI needed.
|
|
50
|
+
|
|
51
|
+
> You need `@capsule-run/cli` in your dependencies to use the `runner` functions in TypeScript.
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { run } from '@capsule-run/sdk/runner';
|
|
55
|
+
|
|
56
|
+
const result = await run({
|
|
57
|
+
file: './capsule.ts',
|
|
58
|
+
args: ['code to execute']
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Create `capsule.ts`:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { task } from "@capsule-run/sdk";
|
|
66
|
+
|
|
67
|
+
export const main = task({
|
|
68
|
+
name: "main",
|
|
69
|
+
compute: "LOW",
|
|
70
|
+
ram: "64MB"
|
|
71
|
+
}, (code: string): string => {
|
|
72
|
+
return eval(code);
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
47
76
|
## How It Works
|
|
48
77
|
|
|
49
78
|
Simply use a wrapper function to define your tasks:
|
|
@@ -113,6 +142,7 @@ Every task returns a structured JSON envelope containing both the result and exe
|
|
|
113
142
|
| `timeout` | Maximum execution time | `string` or `number` | unlimited | `"30s"`, `"5m"` |
|
|
114
143
|
| `maxRetries` | Retry attempts on failure | `number` | `0` | `3` |
|
|
115
144
|
| `allowedFiles` | Folders accessible in the sandbox | `string[]` | `[]` | `["./data", "./output"]` |
|
|
145
|
+
| `allowedHosts` | Domains accessible in the sandbox | `string[]` | `["*"]` | `["api.openai.com", "*.anthropic.com"]` |
|
|
116
146
|
| `envVariables` | Environment variables accessible in the sandbox | `string[]` | `[]` | `["API_KEY"]` |
|
|
117
147
|
|
|
118
148
|
### Compute Levels
|
|
@@ -163,6 +193,24 @@ export const main = task({ name: "main", allowedFiles: ["./data"] }, async () =>
|
|
|
163
193
|
});
|
|
164
194
|
```
|
|
165
195
|
|
|
196
|
+
### Network Access
|
|
197
|
+
|
|
198
|
+
Tasks can make HTTP requests to domains specified in `allowedHosts`. By default, all outbound requests are allowed (`[\"*\"]`). Restrict access by providing a whitelist of domains.
|
|
199
|
+
|
|
200
|
+
> Wildcards are supported: `*.example.com` matches all subdomains of `example.com`.
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
import { task } from "@capsule-run/sdk";
|
|
204
|
+
|
|
205
|
+
export const main = task({
|
|
206
|
+
name: "main",
|
|
207
|
+
allowedHosts: ["api.openai.com", "*.anthropic.com"]
|
|
208
|
+
}, async () => {
|
|
209
|
+
const response = await fetch("https://api.openai.com/v1/models");
|
|
210
|
+
return response.json();
|
|
211
|
+
});
|
|
212
|
+
```
|
|
213
|
+
|
|
166
214
|
### Environment Variables
|
|
167
215
|
|
|
168
216
|
Tasks can access environment variables to read configuration, API keys, or other runtime settings. Use the standard `process.env`:
|
package/dist/app.d.ts
CHANGED
package/dist/app.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,QAAQ,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG;IACzD,IAAI,EAAE,CAAC,CAAC;IACR,MAAM,EAAE,UAAU,CAAC;CACpB;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;CACzB;AAID;;GAEG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,EAC5D,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,CAAC,EACP,MAAM,EAAE,UAAU,GACjB,IAAI,CAEN;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC,GAAG,SAAS,CAE3E;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,CAElE;AAED;;GAEG;AACH,wBAAgB,YAAY,IAAI,MAAM,EAAE,CAEvC;AAQD;;;;;GAKG;AACH,qBAAa,UAAU;IACrB;;;OAGG;IACG,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,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,OAAO,YAAmB,CAAC"}
|
package/dist/app.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.js","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAAA;;GAEG;
|
|
1
|
+
{"version":3,"file":"app.js","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAAA;;GAEG;AAkBH,MAAM,KAAK,GAA+B,IAAI,GAAG,EAAE,CAAC;AAEpD;;GAEG;AACH,MAAM,UAAU,YAAY,CAC1B,IAAY,EACZ,IAAO,EACP,MAAkB;IAElB,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;AACpC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,OAAO,CAAC,IAAY;IAClC,OAAO,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC;AAC/B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,IAAY;IACxC,OAAO,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;AACjC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,YAAY;IAC1B,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;AAClC,CAAC;AAQD;;;;;GAKG;AACH,MAAM,OAAO,UAAU;IACrB;;;OAGG;IACH,KAAK,CAAC,GAAG,CAAC,QAAgB;QACxB,IAAI,CAAC;YACH,MAAM,IAAI,GAAa,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;YAC5C,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,IAAI,MAAM,CAAC;YAC1C,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC;YAC7B,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,EAAE,CAAC;YAEjC,IAAI,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAA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
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capsule SDK - SDK Runner
|
|
3
|
+
*
|
|
4
|
+
* Provides the `run` function for running Capsule tasks
|
|
5
|
+
* from third party applications.
|
|
6
|
+
*/
|
|
7
|
+
export interface RunnerOptions {
|
|
8
|
+
file: string;
|
|
9
|
+
args?: string[];
|
|
10
|
+
cwd?: string;
|
|
11
|
+
capsulePath?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface RunnerResult {
|
|
14
|
+
success: boolean;
|
|
15
|
+
result: string | number | boolean | object | null;
|
|
16
|
+
error: {
|
|
17
|
+
error_type: string;
|
|
18
|
+
message: string;
|
|
19
|
+
} | null;
|
|
20
|
+
execution: {
|
|
21
|
+
task_name: string;
|
|
22
|
+
duration_ms: number;
|
|
23
|
+
retries: number;
|
|
24
|
+
fuel_consumed: number;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Run a Capsule task from a third-party application
|
|
29
|
+
*
|
|
30
|
+
* @param options - Runner options
|
|
31
|
+
* @returns Promise with the runner result
|
|
32
|
+
*/
|
|
33
|
+
export declare function run(options: RunnerOptions): Promise<RunnerResult>;
|
|
34
|
+
//# sourceMappingURL=run.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,IAAI,CAAC;IAClD,KAAK,EAAE;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IACtD,SAAS,EAAE;QACT,SAAS,EAAE,MAAM,CAAC;QAClB,WAAW,EAAE,MAAM,CAAC;QACpB,OAAO,EAAE,MAAM,CAAC;QAChB,aAAa,EAAE,MAAM,CAAC;KACvB,CAAC;CACH;AAYD;;;;;GAKG;AACH,wBAAgB,GAAG,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC,CA0CjE"}
|
package/dist/run.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capsule SDK - SDK Runner
|
|
3
|
+
*
|
|
4
|
+
* Provides the `run` function for running Capsule tasks
|
|
5
|
+
* from third party applications.
|
|
6
|
+
*/
|
|
7
|
+
import { execFile } from 'child_process';
|
|
8
|
+
import { resolve } from 'path';
|
|
9
|
+
import { existsSync } from 'fs';
|
|
10
|
+
/**
|
|
11
|
+
* Get the appropriate capsule command for the current platform
|
|
12
|
+
*/
|
|
13
|
+
function getCapsuleCommand(capsulePath) {
|
|
14
|
+
if (process.platform === 'win32' && !capsulePath.endsWith('.cmd')) {
|
|
15
|
+
return `${capsulePath}.cmd`;
|
|
16
|
+
}
|
|
17
|
+
return capsulePath;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Run a Capsule task from a third-party application
|
|
21
|
+
*
|
|
22
|
+
* @param options - Runner options
|
|
23
|
+
* @returns Promise with the runner result
|
|
24
|
+
*/
|
|
25
|
+
export function run(options) {
|
|
26
|
+
const { file, args = [], cwd, capsulePath = 'capsule' } = options;
|
|
27
|
+
const command = getCapsuleCommand(capsulePath);
|
|
28
|
+
const resolvedFile = resolve(cwd || process.cwd(), file);
|
|
29
|
+
if (!existsSync(resolvedFile)) {
|
|
30
|
+
return Promise.reject(new Error(`File not found: ${resolvedFile}`));
|
|
31
|
+
}
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
const cmdArgs = ['run', resolvedFile, '--json', ...args];
|
|
34
|
+
let executable = command;
|
|
35
|
+
let executionArgs = cmdArgs;
|
|
36
|
+
if (process.platform === 'win32') {
|
|
37
|
+
executable = process.env.comspec || 'cmd.exe';
|
|
38
|
+
executionArgs = ['/d', '/s', '/c', command, ...cmdArgs];
|
|
39
|
+
}
|
|
40
|
+
execFile(executable, executionArgs, { cwd, encoding: 'utf-8' }, (error, stdout, stderr) => {
|
|
41
|
+
if (error && !stdout) {
|
|
42
|
+
if (error.code === 'ENOENT') {
|
|
43
|
+
reject(new Error(`Capsule CLI not found. Use 'npm install -g @capsule-run/cli' to install it.`));
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
reject(new Error(stderr || error.message));
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const lines = stdout.trim().split('\n');
|
|
51
|
+
const jsonLine = lines[lines.length - 1];
|
|
52
|
+
resolve(JSON.parse(jsonLine));
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
reject(new Error(`Failed to parse Capsule output: ${stdout}`));
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=run.js.map
|
package/dist/run.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.js","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAqBhC;;GAEG;AACH,SAAS,iBAAiB,CAAC,WAAmB;IAC5C,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;QAClE,OAAO,GAAG,WAAW,M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AAE,CAAC;oBACvD,MAAM,CAAC,IAAI,KAAK,CACd,6EAA6E,CAC9E,CAAC,CAAC;oBACH,OAAO;gBACT,CAAC;gBACD,MAAM,CAAC,IAAI,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;gBAC3C,OAAO;YACT,CAAC;YAED,IAAI,CAAC;gBACH,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;gBACxC,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;gBACzC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC;YAChC,CAAC;YAAC,MAAM,CAAC;gBACP,MAAM,CAAC,IAAI,KAAK,CAAC,mCAAmC,MAAM,EAAE,CAAC,CAAC,CAAC;YACjE,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/dist/task.d.ts
CHANGED
|
@@ -21,6 +21,8 @@ export interface TaskOptions {
|
|
|
21
21
|
maxRetries?: number;
|
|
22
22
|
/** Files/folders accessible in the sandbox, e.g., ["./data"] */
|
|
23
23
|
allowedFiles?: string[];
|
|
24
|
+
/** Allowed hosts for HTTP requests */
|
|
25
|
+
allowedHosts?: string[];
|
|
24
26
|
/** Environment variables available from your .env file for the task */
|
|
25
27
|
envVariables?: string[];
|
|
26
28
|
}
|
package/dist/task.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"task.d.ts","sourceRoot":"","sources":["../src/task.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,MAAM,WAAW,WAAW;IAC1B,2BAA2B;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,gDAAgD;IAChD,OAAO,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;IAC7C,sCAAsC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AG,CAAC,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC1B,gCAAgC;IAChC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,gEAAgE;IAChE,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,sCAAsC;IACtC,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,uEAAuE;IACvE,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;CACzB;AAED,UAAU,UAAU,CAAC,CAAC;IACpB,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,CAAC,CAAC;IACV,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,SAAS,EAAE,aAAa,CAAC;CAC1B;AAED,UAAU,aAAa;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC;CACzB;AAID,KAAK,cAAc,CAAC,CAAC,IAAI,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,CAAC,GAC/C,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,GACtB,UAAU,CAAC,CAAC,CAAC,CAAC;AA+ClB,wBAAgB,IAAI,CAAC,KAAK,SAAS,GAAG,EAAE,EAAE,OAAO,EAC/C,OAAO,EAAE,WAAW,EACpB,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,GAC9B,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,cAAc,CAAC,OAAO,CAAC,CA+D7C"}
|
package/dist/task.js
CHANGED
|
@@ -53,6 +53,7 @@ function normalizeTimeout(timeout) {
|
|
|
53
53
|
export function task(options, fn) {
|
|
54
54
|
const taskName = options.name;
|
|
55
55
|
let compute = options.compute?.toString().toUpperCase() ?? "MEDIUM";
|
|
56
|
+
let allowedHosts = options.allowedHosts ?? ["*"];
|
|
56
57
|
const taskConfig = {
|
|
57
58
|
name: taskName,
|
|
58
59
|
compute,
|
|
@@ -60,6 +61,7 @@ export function task(options, fn) {
|
|
|
60
61
|
timeout: normalizeTimeout(options.timeout),
|
|
61
62
|
maxRetries: options.maxRetries,
|
|
62
63
|
allowedFiles: options.allowedFiles,
|
|
64
|
+
allowedHosts,
|
|
63
65
|
envVariables: options.envVariables,
|
|
64
66
|
};
|
|
65
67
|
const wrapper = (...args) => {
|
package/dist/task.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"task.js","sourceRoot":"","sources":["../src/task.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,YAAY,EAAmB,MAAM,UAAU,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"task.js","sourceRoot":"","sources":["../src/task.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,YAAY,EAAmB,MAAM,UAAU,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AA6CpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,OAAyB;IACjD,IAAI,CAAC,OAAO;QAAE,OAAO,SAAS,CAAC;IAC/B,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAChC,OAAO,GAAG,OAAO,IAAI,CAAC;IACxB,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,MAAM,UAAU,IAAI,CAClB,OAAoB,EACpB,EAA+B;IAE/B,MAAM,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;IAC9B,IAAI,OAAO,GAAG,OAAO,CAAC,OAAO,EAAE,QAAQ,EAAE,CAAC,WAAW,EAAE,IAAI,QAAQ,CAAC;IACpE,IAAI,YAAY,GAAG,OAAO,CAAC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O,MAAiC,CAAC;QAC3C,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,IAAI,CAAC,YAAY,WAAW,EAAE,CAAC;gBAC7B,OAAO,UAAgD,CAAC;YAC1D,CAAC;YACD,MAAM,CAAC,CAAC;QACV,CAAC;IACH,CAAC,CAAC;IAEF,YAAY,CAAC,QAAQ,EAAE,EAAE,EAAE,UAAU,CAAC,CAAC;IAEvC,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capsule-run/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Capsule JavaScript SDK - run AI agent tasks in secure WASM sandboxes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -15,6 +15,10 @@
|
|
|
15
15
|
"types": "./dist/index.d.ts",
|
|
16
16
|
"import": "./dist/index.js"
|
|
17
17
|
},
|
|
18
|
+
"./runner": {
|
|
19
|
+
"types": "./dist/run.d.ts",
|
|
20
|
+
"import": "./dist/run.js"
|
|
21
|
+
},
|
|
18
22
|
"./capsule/app.js": {
|
|
19
23
|
"types": "./dist/app.d.ts",
|
|
20
24
|
"import": "./dist/app.js"
|
|
@@ -48,6 +52,7 @@
|
|
|
48
52
|
},
|
|
49
53
|
"devDependencies": {
|
|
50
54
|
"@types/events": "^3.0.3",
|
|
55
|
+
"@types/node": "^25.2.3",
|
|
51
56
|
"@types/path-browserify": "^1.0.3",
|
|
52
57
|
"@types/readable-stream": "^4.0.18"
|
|
53
58
|
},
|
package/src/app.ts
CHANGED
package/src/index.ts
CHANGED
package/src/run.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capsule SDK - SDK Runner
|
|
3
|
+
*
|
|
4
|
+
* Provides the `run` function for running Capsule tasks
|
|
5
|
+
* from third party applications.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { execFile } from 'child_process';
|
|
9
|
+
import { resolve } from 'path';
|
|
10
|
+
import { existsSync } from 'fs';
|
|
11
|
+
|
|
12
|
+
export interface RunnerOptions {
|
|
13
|
+
file: string;
|
|
14
|
+
args?: string[];
|
|
15
|
+
cwd?: string;
|
|
16
|
+
capsulePath?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface RunnerResult {
|
|
20
|
+
success: boolean;
|
|
21
|
+
result: string | number | boolean | object | null;
|
|
22
|
+
error: { error_type: string; message: string } | null;
|
|
23
|
+
execution: {
|
|
24
|
+
task_name: string;
|
|
25
|
+
duration_ms: number;
|
|
26
|
+
retries: number;
|
|
27
|
+
fuel_consumed: number;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Get the appropriate capsule command for the current platform
|
|
33
|
+
*/
|
|
34
|
+
function getCapsuleCommand(capsulePath: string): string {
|
|
35
|
+
if (process.platform === 'win32' && !capsulePath.endsWith('.cmd')) {
|
|
36
|
+
return `${capsulePath}.cmd`;
|
|
37
|
+
}
|
|
38
|
+
return capsulePath;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Run a Capsule task from a third-party application
|
|
43
|
+
*
|
|
44
|
+
* @param options - Runner options
|
|
45
|
+
* @returns Promise with the runner result
|
|
46
|
+
*/
|
|
47
|
+
export function run(options: RunnerOptions): Promise<RunnerResult> {
|
|
48
|
+
const { file, args = [], cwd, capsulePath = 'capsule' } = options;
|
|
49
|
+
const command = getCapsuleCommand(capsulePath);
|
|
50
|
+
|
|
51
|
+
const resolvedFile = resolve(cwd || process.cwd(), file);
|
|
52
|
+
|
|
53
|
+
if (!existsSync(resolvedFile)) {
|
|
54
|
+
return Promise.reject(new Error(`File not found: ${resolvedFile}`));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return new Promise((resolve, reject) => {
|
|
58
|
+
const cmdArgs = ['run', resolvedFile, '--json', ...args];
|
|
59
|
+
|
|
60
|
+
let executable = command;
|
|
61
|
+
let executionArgs = cmdArgs;
|
|
62
|
+
|
|
63
|
+
if (process.platform === 'win32') {
|
|
64
|
+
executable = process.env.comspec || 'cmd.exe';
|
|
65
|
+
executionArgs = ['/d', '/s', '/c', command, ...cmdArgs];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
execFile(executable, executionArgs, { cwd, encoding: 'utf-8' }, (error, stdout, stderr) => {
|
|
69
|
+
if (error && !stdout) {
|
|
70
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
|
|
71
|
+
reject(new Error(
|
|
72
|
+
`Capsule CLI not found. Use 'npm install -g @capsule-run/cli' to install it.`
|
|
73
|
+
));
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
reject(new Error(stderr || error.message));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
const lines = stdout.trim().split('\n');
|
|
82
|
+
const jsonLine = lines[lines.length - 1];
|
|
83
|
+
resolve(JSON.parse(jsonLine));
|
|
84
|
+
} catch {
|
|
85
|
+
reject(new Error(`Failed to parse Capsule output: ${stdout}`));
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}
|
package/src/task.ts
CHANGED
|
@@ -25,6 +25,8 @@ export interface TaskOptions {
|
|
|
25
25
|
maxRetries?: number;
|
|
26
26
|
/** Files/folders accessible in the sandbox, e.g., ["./data"] */
|
|
27
27
|
allowedFiles?: string[];
|
|
28
|
+
/** Allowed hosts for HTTP requests */
|
|
29
|
+
allowedHosts?: string[];
|
|
28
30
|
/** Environment variables available from your .env file for the task */
|
|
29
31
|
envVariables?: string[];
|
|
30
32
|
}
|
|
@@ -100,6 +102,7 @@ export function task<TArgs extends any[], TReturn>(
|
|
|
100
102
|
): (...args: TArgs) => TaskReturnType<TReturn> {
|
|
101
103
|
const taskName = options.name;
|
|
102
104
|
let compute = options.compute?.toString().toUpperCase() ?? "MEDIUM";
|
|
105
|
+
let allowedHosts = options.allowedHosts ?? ["*"];
|
|
103
106
|
|
|
104
107
|
const taskConfig: TaskConfig = {
|
|
105
108
|
name: taskName,
|
|
@@ -108,6 +111,7 @@ export function task<TArgs extends any[], TReturn>(
|
|
|
108
111
|
timeout: normalizeTimeout(options.timeout),
|
|
109
112
|
maxRetries: options.maxRetries,
|
|
110
113
|
allowedFiles: options.allowedFiles,
|
|
114
|
+
allowedHosts,
|
|
111
115
|
envVariables: options.envVariables,
|
|
112
116
|
};
|
|
113
117
|
|