@runsynapse/sdk 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/README.md +76 -0
- package/dist/index.d.ts +121 -0
- package/dist/index.js +176 -0
- package/package.json +42 -0
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# @synapserun/sdk
|
|
2
|
+
|
|
3
|
+
**281x faster code execution for AI agents.** Sub-millisecond Wasm execution on bare metal.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i @synapserun/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Synapse } from '@synapserun/sdk';
|
|
15
|
+
|
|
16
|
+
const client = new Synapse({ apiKey: 'sk_live_...' });
|
|
17
|
+
|
|
18
|
+
// Execute .syn code — returns in <1ms (p99)
|
|
19
|
+
const result = await client.execute('@f 0 main [ + 21 21 ]');
|
|
20
|
+
console.log(result.result); // 42
|
|
21
|
+
console.log(result.latencyMs); // 0.52
|
|
22
|
+
|
|
23
|
+
// Batch execute — up to 1000 jobs
|
|
24
|
+
const batch = await client.executeBatch([
|
|
25
|
+
{ code: '@f 0 main [ + 1 2 ]' },
|
|
26
|
+
{ code: '@f 0 main [ * 3 4 ]' },
|
|
27
|
+
{ code: '@f 0 main [ - 100 58 ]' },
|
|
28
|
+
]);
|
|
29
|
+
batch.results.forEach(r => console.log(r.result)); // 3, 12, 42
|
|
30
|
+
|
|
31
|
+
// Validate without executing
|
|
32
|
+
const meta = await client.validate('@f 0 main [ + 1 1 ]');
|
|
33
|
+
console.log(meta); // { valid: true, wasm_size: 89, ... }
|
|
34
|
+
|
|
35
|
+
// Natural language computation
|
|
36
|
+
const answer = await client.compute('calculate fibonacci of 10');
|
|
37
|
+
console.log(answer);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Performance
|
|
41
|
+
|
|
42
|
+
Proven with 100,000 sequential executions on Hetzner AX102:
|
|
43
|
+
|
|
44
|
+
| Metric | Value |
|
|
45
|
+
|--------|-------|
|
|
46
|
+
| **p50 latency** | 0.53ms |
|
|
47
|
+
| **p99 latency** | 0.72ms |
|
|
48
|
+
| **vs E2B** | 281x faster |
|
|
49
|
+
| **vs Blaxel** | 47x faster |
|
|
50
|
+
| **Correctness** | 100,000/100,000 |
|
|
51
|
+
|
|
52
|
+
## API Reference
|
|
53
|
+
|
|
54
|
+
### `new Synapse(config)`
|
|
55
|
+
|
|
56
|
+
| Parameter | Type | Default | Description |
|
|
57
|
+
|-----------|------|---------|-------------|
|
|
58
|
+
| `apiKey` | `string` | — | Your API key |
|
|
59
|
+
| `baseUrl` | `string` | `https://api.synapse.dev` | Gateway URL |
|
|
60
|
+
| `timeout` | `number` | `15000` | Timeout in ms |
|
|
61
|
+
| `maxRetries` | `number` | `3` | Retry count |
|
|
62
|
+
|
|
63
|
+
### Methods
|
|
64
|
+
|
|
65
|
+
| Method | Description |
|
|
66
|
+
|--------|-------------|
|
|
67
|
+
| `execute(code)` | Execute .syn source code |
|
|
68
|
+
| `executeWasm(bytes)` | Execute pre-compiled .wasm binary |
|
|
69
|
+
| `executeBatch(jobs)` | Execute up to 1000 jobs in parallel |
|
|
70
|
+
| `validate(code)` | Validate .syn syntax |
|
|
71
|
+
| `compute(intent)` | Natural language → computation |
|
|
72
|
+
| `health()` | Check gateway health |
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synapse SDK — 281x faster code execution for AI agents.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { Synapse } from '@synapserun/sdk';
|
|
7
|
+
*
|
|
8
|
+
* const client = new Synapse({ apiKey: 'sk_live_...' });
|
|
9
|
+
* const result = await client.execute('@f 0 main [ + 21 21 ]');
|
|
10
|
+
* console.log(result.result); // 42
|
|
11
|
+
* console.log(result.latencyMs); // 0.52
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export interface SynapseConfig {
|
|
15
|
+
/** API key starting with sk_live_ */
|
|
16
|
+
apiKey: string;
|
|
17
|
+
/** Gateway URL (default: https://api.synapse.dev) */
|
|
18
|
+
baseUrl?: string;
|
|
19
|
+
/** Request timeout in ms (default: 15000) */
|
|
20
|
+
timeout?: number;
|
|
21
|
+
/** Max retries for transient errors (default: 3) */
|
|
22
|
+
maxRetries?: number;
|
|
23
|
+
}
|
|
24
|
+
export interface ExecutionResult {
|
|
25
|
+
/** Integer return value from the Wasm module */
|
|
26
|
+
result: number;
|
|
27
|
+
/** Captured stdout output */
|
|
28
|
+
stdout: string;
|
|
29
|
+
/** Arena memory position */
|
|
30
|
+
arenaPos: number;
|
|
31
|
+
/** End-to-end latency in milliseconds */
|
|
32
|
+
latencyMs: number;
|
|
33
|
+
/** Unique execution ID */
|
|
34
|
+
executionId: string;
|
|
35
|
+
/** Compile time in milliseconds */
|
|
36
|
+
compileTimeMs: number;
|
|
37
|
+
/** Wasm binary size in bytes */
|
|
38
|
+
wasmSize: number;
|
|
39
|
+
/** SHA-256 hash of the Wasm binary */
|
|
40
|
+
deterministicHash: string;
|
|
41
|
+
/** Cost in USD */
|
|
42
|
+
costUsd: number;
|
|
43
|
+
}
|
|
44
|
+
export interface BatchResult {
|
|
45
|
+
/** Total jobs in batch */
|
|
46
|
+
total: number;
|
|
47
|
+
/** Successfully executed */
|
|
48
|
+
succeeded: number;
|
|
49
|
+
/** Failed executions */
|
|
50
|
+
failed: number;
|
|
51
|
+
/** Total batch latency in ms */
|
|
52
|
+
batchLatencyMs: number;
|
|
53
|
+
/** Per-job results */
|
|
54
|
+
results: Array<{
|
|
55
|
+
result: number;
|
|
56
|
+
stdout: string;
|
|
57
|
+
latencyMs: number;
|
|
58
|
+
status: string;
|
|
59
|
+
}>;
|
|
60
|
+
}
|
|
61
|
+
export interface HealthStatus {
|
|
62
|
+
status: string;
|
|
63
|
+
version: string;
|
|
64
|
+
kernelConnected: boolean;
|
|
65
|
+
endpoints: string[];
|
|
66
|
+
}
|
|
67
|
+
export declare class SynapseError extends Error {
|
|
68
|
+
statusCode: number;
|
|
69
|
+
errorType: string;
|
|
70
|
+
constructor(statusCode: number, message: string, errorType?: string);
|
|
71
|
+
}
|
|
72
|
+
export declare class Synapse {
|
|
73
|
+
private apiKey;
|
|
74
|
+
private baseUrl;
|
|
75
|
+
private timeout;
|
|
76
|
+
private maxRetries;
|
|
77
|
+
constructor(config: SynapseConfig);
|
|
78
|
+
private request;
|
|
79
|
+
/**
|
|
80
|
+
* Execute .syn source code on bare-metal Wasm engine.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const result = await client.execute('@f 0 main [ + 21 21 ]');
|
|
85
|
+
* console.log(result.result); // 42
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
execute(code: string): Promise<ExecutionResult>;
|
|
89
|
+
/**
|
|
90
|
+
* Execute a pre-compiled .wasm binary.
|
|
91
|
+
*/
|
|
92
|
+
executeWasm(wasmBytes: Uint8Array): Promise<ExecutionResult>;
|
|
93
|
+
/**
|
|
94
|
+
* Execute up to 1000 jobs in a single batch.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* const batch = await client.executeBatch([
|
|
99
|
+
* { code: '@f 0 main [ + 1 2 ]' },
|
|
100
|
+
* { code: '@f 0 main [ * 3 4 ]' },
|
|
101
|
+
* ]);
|
|
102
|
+
* batch.results.forEach(r => console.log(r.result)); // 3, 12
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
executeBatch(jobs: Array<{
|
|
106
|
+
code: string;
|
|
107
|
+
}>): Promise<BatchResult>;
|
|
108
|
+
/**
|
|
109
|
+
* Validate .syn syntax without executing.
|
|
110
|
+
*/
|
|
111
|
+
validate(code: string): Promise<Record<string, unknown>>;
|
|
112
|
+
/**
|
|
113
|
+
* Natural language computation — describe what you want and get results.
|
|
114
|
+
*/
|
|
115
|
+
compute(intent: string): Promise<Record<string, unknown>>;
|
|
116
|
+
/**
|
|
117
|
+
* Check gateway health and kernel connectivity.
|
|
118
|
+
*/
|
|
119
|
+
health(): Promise<HealthStatus>;
|
|
120
|
+
}
|
|
121
|
+
export default Synapse;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Synapse SDK — 281x faster code execution for AI agents.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { Synapse } from '@synapserun/sdk';
|
|
8
|
+
*
|
|
9
|
+
* const client = new Synapse({ apiKey: 'sk_live_...' });
|
|
10
|
+
* const result = await client.execute('@f 0 main [ + 21 21 ]');
|
|
11
|
+
* console.log(result.result); // 42
|
|
12
|
+
* console.log(result.latencyMs); // 0.52
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.Synapse = exports.SynapseError = void 0;
|
|
17
|
+
class SynapseError extends Error {
|
|
18
|
+
constructor(statusCode, message, errorType = '') {
|
|
19
|
+
super(`Synapse API error ${statusCode}: ${message}`);
|
|
20
|
+
this.name = 'SynapseError';
|
|
21
|
+
this.statusCode = statusCode;
|
|
22
|
+
this.errorType = errorType;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.SynapseError = SynapseError;
|
|
26
|
+
class Synapse {
|
|
27
|
+
constructor(config) {
|
|
28
|
+
this.apiKey = config.apiKey;
|
|
29
|
+
this.baseUrl = (config.baseUrl || 'https://api.synapse.dev').replace(/\/$/, '');
|
|
30
|
+
this.timeout = config.timeout || 15000;
|
|
31
|
+
this.maxRetries = config.maxRetries || 3;
|
|
32
|
+
}
|
|
33
|
+
async request(endpoint, payload) {
|
|
34
|
+
const url = `${this.baseUrl}${endpoint}`;
|
|
35
|
+
let lastError = null;
|
|
36
|
+
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
|
|
37
|
+
try {
|
|
38
|
+
const controller = new AbortController();
|
|
39
|
+
const timer = setTimeout(() => controller.abort(), this.timeout);
|
|
40
|
+
const resp = await fetch(url, {
|
|
41
|
+
method: payload ? 'POST' : 'GET',
|
|
42
|
+
headers: {
|
|
43
|
+
'Content-Type': 'application/json',
|
|
44
|
+
'Authorization': `Bearer ${this.apiKey}`,
|
|
45
|
+
},
|
|
46
|
+
body: payload ? JSON.stringify(payload) : undefined,
|
|
47
|
+
signal: controller.signal,
|
|
48
|
+
});
|
|
49
|
+
clearTimeout(timer);
|
|
50
|
+
const body = await resp.json();
|
|
51
|
+
if (!resp.ok) {
|
|
52
|
+
const errMsg = body.error || `HTTP ${resp.status}`;
|
|
53
|
+
if (resp.status >= 400 && resp.status < 500) {
|
|
54
|
+
throw new SynapseError(resp.status, errMsg, body.error_type || '');
|
|
55
|
+
}
|
|
56
|
+
throw new SynapseError(resp.status, errMsg);
|
|
57
|
+
}
|
|
58
|
+
return body;
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
if (e instanceof SynapseError && e.statusCode >= 400 && e.statusCode < 500) {
|
|
62
|
+
throw e; // Don't retry client errors
|
|
63
|
+
}
|
|
64
|
+
lastError = e;
|
|
65
|
+
if (attempt < this.maxRetries) {
|
|
66
|
+
await new Promise(r => setTimeout(r, Math.min(2 ** attempt * 100, 5000)));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
throw lastError || new SynapseError(0, 'Unknown error');
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Execute .syn source code on bare-metal Wasm engine.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const result = await client.execute('@f 0 main [ + 21 21 ]');
|
|
78
|
+
* console.log(result.result); // 42
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
async execute(code) {
|
|
82
|
+
const body = await this.request('/v1/execute', { code });
|
|
83
|
+
if (body.status === 'error') {
|
|
84
|
+
throw new SynapseError(400, body.error || 'execution_failed');
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
result: body.result || 0,
|
|
88
|
+
stdout: body.stdout || '',
|
|
89
|
+
arenaPos: body.arena_pos || 0,
|
|
90
|
+
latencyMs: body.latency_ms || 0,
|
|
91
|
+
executionId: body.execution_id || '',
|
|
92
|
+
compileTimeMs: body.compile_time_ms || 0,
|
|
93
|
+
wasmSize: body.wasm_size || 0,
|
|
94
|
+
deterministicHash: body.deterministic_hash || '',
|
|
95
|
+
costUsd: body.cost_usd || 0,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Execute a pre-compiled .wasm binary.
|
|
100
|
+
*/
|
|
101
|
+
async executeWasm(wasmBytes) {
|
|
102
|
+
const base64 = Buffer.from(wasmBytes).toString('base64');
|
|
103
|
+
const body = await this.request('/v1/execute', { wasm: base64 });
|
|
104
|
+
if (body.status === 'error') {
|
|
105
|
+
throw new SynapseError(400, body.error || 'execution_failed');
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
result: body.result || 0,
|
|
109
|
+
stdout: body.stdout || '',
|
|
110
|
+
arenaPos: body.arena_pos || 0,
|
|
111
|
+
latencyMs: body.latency_ms || 0,
|
|
112
|
+
executionId: body.execution_id || '',
|
|
113
|
+
compileTimeMs: body.compile_time_ms || 0,
|
|
114
|
+
wasmSize: body.wasm_size || 0,
|
|
115
|
+
deterministicHash: body.deterministic_hash || '',
|
|
116
|
+
costUsd: body.cost_usd || 0,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Execute up to 1000 jobs in a single batch.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* const batch = await client.executeBatch([
|
|
125
|
+
* { code: '@f 0 main [ + 1 2 ]' },
|
|
126
|
+
* { code: '@f 0 main [ * 3 4 ]' },
|
|
127
|
+
* ]);
|
|
128
|
+
* batch.results.forEach(r => console.log(r.result)); // 3, 12
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
async executeBatch(jobs) {
|
|
132
|
+
const body = await this.request('/v1/execute/batch', { jobs });
|
|
133
|
+
if (body.status === 'error') {
|
|
134
|
+
throw new SynapseError(400, body.error || 'batch_failed');
|
|
135
|
+
}
|
|
136
|
+
const results = body.results || [];
|
|
137
|
+
return {
|
|
138
|
+
total: body.total || 0,
|
|
139
|
+
succeeded: body.succeeded || 0,
|
|
140
|
+
failed: body.failed || 0,
|
|
141
|
+
batchLatencyMs: body.batch_latency_ms || 0,
|
|
142
|
+
results: results.map(r => ({
|
|
143
|
+
result: r.result || 0,
|
|
144
|
+
stdout: r.stdout || '',
|
|
145
|
+
latencyMs: r.latency_ms || 0,
|
|
146
|
+
status: r.status || 'unknown',
|
|
147
|
+
})),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Validate .syn syntax without executing.
|
|
152
|
+
*/
|
|
153
|
+
async validate(code) {
|
|
154
|
+
return this.request('/v1/validate', { code });
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Natural language computation — describe what you want and get results.
|
|
158
|
+
*/
|
|
159
|
+
async compute(intent) {
|
|
160
|
+
return this.request('/v1/compute', { intent });
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Check gateway health and kernel connectivity.
|
|
164
|
+
*/
|
|
165
|
+
async health() {
|
|
166
|
+
const body = await this.request('/');
|
|
167
|
+
return {
|
|
168
|
+
status: body.status || 'unknown',
|
|
169
|
+
version: body.version || '',
|
|
170
|
+
kernelConnected: body.kernel_connected || false,
|
|
171
|
+
endpoints: body.endpoints || [],
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
exports.Synapse = Synapse;
|
|
176
|
+
exports.default = Synapse;
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@runsynapse/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Synapse SDK — 281x faster code execution for AI agents. Sub-millisecond Wasm execution on bare metal.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"keywords": [
|
|
12
|
+
"synapse",
|
|
13
|
+
"wasm",
|
|
14
|
+
"ai",
|
|
15
|
+
"agents",
|
|
16
|
+
"sandbox",
|
|
17
|
+
"mcp",
|
|
18
|
+
"execution",
|
|
19
|
+
"bare-metal"
|
|
20
|
+
],
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": "Synapse",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/synapse-dev/synapse-sdk"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://synapserun.dev",
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=18"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc",
|
|
36
|
+
"prepublishOnly": "npm run build"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "^25.5.0",
|
|
40
|
+
"typescript": "^5.0.0"
|
|
41
|
+
}
|
|
42
|
+
}
|