sbox-sdk 0.0.2
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/LICENSE +21 -0
- package/README.md +137 -0
- package/dist/adapter/index.d.ts +22 -0
- package/dist/adapter/index.d.ts.map +1 -0
- package/dist/adapter/index.js +16 -0
- package/dist/agent-tools/index.d.ts +13 -0
- package/dist/agent-tools/index.d.ts.map +1 -0
- package/dist/agent-tools/index.js +9 -0
- package/dist/agent-tools/policy.d.ts +48 -0
- package/dist/agent-tools/policy.d.ts.map +1 -0
- package/dist/agent-tools/policy.js +51 -0
- package/dist/agent-tools/registry.d.ts +9 -0
- package/dist/agent-tools/registry.d.ts.map +1 -0
- package/dist/agent-tools/registry.js +412 -0
- package/dist/agent-tools/result.d.ts +32 -0
- package/dist/agent-tools/result.d.ts.map +1 -0
- package/dist/agent-tools/result.js +14 -0
- package/dist/agent-tools/types.d.ts +76 -0
- package/dist/agent-tools/types.d.ts.map +1 -0
- package/dist/agent-tools/types.js +1 -0
- package/dist/ai/index.d.ts +36 -0
- package/dist/ai/index.d.ts.map +1 -0
- package/dist/ai/index.js +40 -0
- package/dist/ai-sdk/index.d.ts +31 -0
- package/dist/ai-sdk/index.d.ts.map +1 -0
- package/dist/ai-sdk/index.js +80 -0
- package/dist/anthropic/index.d.ts +42 -0
- package/dist/anthropic/index.d.ts.map +1 -0
- package/dist/anthropic/index.js +64 -0
- package/dist/aws-lambda/index.d.ts +87 -0
- package/dist/aws-lambda/index.d.ts.map +1 -0
- package/dist/aws-lambda/index.js +290 -0
- package/dist/beam/index.d.ts +92 -0
- package/dist/beam/index.d.ts.map +1 -0
- package/dist/beam/index.js +222 -0
- package/dist/blaxel/index.d.ts +125 -0
- package/dist/blaxel/index.d.ts.map +1 -0
- package/dist/blaxel/index.js +220 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +249 -0
- package/dist/cloudflare/index.d.ts +64 -0
- package/dist/cloudflare/index.d.ts.map +1 -0
- package/dist/cloudflare/index.js +259 -0
- package/dist/codesandbox/index.d.ts +100 -0
- package/dist/codesandbox/index.d.ts.map +1 -0
- package/dist/codesandbox/index.js +227 -0
- package/dist/conformance/index.d.ts +20 -0
- package/dist/conformance/index.d.ts.map +1 -0
- package/dist/conformance/index.js +189 -0
- package/dist/daytona/index.d.ts +64 -0
- package/dist/daytona/index.d.ts.map +1 -0
- package/dist/daytona/index.js +258 -0
- package/dist/e2b/index.d.ts +63 -0
- package/dist/e2b/index.d.ts.map +1 -0
- package/dist/e2b/index.js +411 -0
- package/dist/fly/index.d.ts +75 -0
- package/dist/fly/index.d.ts.map +1 -0
- package/dist/fly/index.js +222 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/internal/capabilities.d.ts +57 -0
- package/dist/internal/capabilities.d.ts.map +1 -0
- package/dist/internal/capabilities.js +68 -0
- package/dist/internal/client.d.ts +9 -0
- package/dist/internal/client.d.ts.map +1 -0
- package/dist/internal/client.js +126 -0
- package/dist/internal/encoding.d.ts +8 -0
- package/dist/internal/encoding.d.ts.map +1 -0
- package/dist/internal/encoding.js +20 -0
- package/dist/internal/errors.d.ts +45 -0
- package/dist/internal/errors.d.ts.map +1 -0
- package/dist/internal/errors.js +79 -0
- package/dist/internal/exec.d.ts +19 -0
- package/dist/internal/exec.d.ts.map +1 -0
- package/dist/internal/exec.js +208 -0
- package/dist/internal/plugin.d.ts +38 -0
- package/dist/internal/plugin.d.ts.map +1 -0
- package/dist/internal/plugin.js +1 -0
- package/dist/internal/runtime.d.ts +8 -0
- package/dist/internal/runtime.d.ts.map +1 -0
- package/dist/internal/runtime.js +21 -0
- package/dist/internal/sandbox.d.ts +12 -0
- package/dist/internal/sandbox.d.ts.map +1 -0
- package/dist/internal/sandbox.js +438 -0
- package/dist/internal/shell.d.ts +36 -0
- package/dist/internal/shell.d.ts.map +1 -0
- package/dist/internal/shell.js +88 -0
- package/dist/internal/stream.d.ts +15 -0
- package/dist/internal/stream.d.ts.map +1 -0
- package/dist/internal/stream.js +58 -0
- package/dist/internal/types.d.ts +381 -0
- package/dist/internal/types.d.ts.map +1 -0
- package/dist/internal/types.js +1 -0
- package/dist/langchain/index.d.ts +25 -0
- package/dist/langchain/index.d.ts.map +1 -0
- package/dist/langchain/index.js +61 -0
- package/dist/mastra/index.d.ts +43 -0
- package/dist/mastra/index.d.ts.map +1 -0
- package/dist/mastra/index.js +69 -0
- package/dist/memory/index.d.ts +57 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +573 -0
- package/dist/modal/index.d.ts +67 -0
- package/dist/modal/index.d.ts.map +1 -0
- package/dist/modal/index.js +223 -0
- package/dist/morph/index.d.ts +91 -0
- package/dist/morph/index.d.ts.map +1 -0
- package/dist/morph/index.js +221 -0
- package/dist/northflank/index.d.ts +74 -0
- package/dist/northflank/index.d.ts.map +1 -0
- package/dist/northflank/index.js +265 -0
- package/dist/openai/index.d.ts +25 -0
- package/dist/openai/index.d.ts.map +1 -0
- package/dist/openai/index.js +71 -0
- package/dist/railway/index.d.ts +109 -0
- package/dist/railway/index.d.ts.map +1 -0
- package/dist/railway/index.js +219 -0
- package/dist/runloop/index.d.ts +69 -0
- package/dist/runloop/index.d.ts.map +1 -0
- package/dist/runloop/index.js +226 -0
- package/dist/testing/index.d.ts +44 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +61 -0
- package/dist/vercel/index.d.ts +63 -0
- package/dist/vercel/index.d.ts.map +1 -0
- package/dist/vercel/index.js +241 -0
- package/package.json +252 -0
- package/src/aws-lambda/runner/Dockerfile +15 -0
- package/src/aws-lambda/runner/README.md +59 -0
- package/src/aws-lambda/runner/server.mjs +91 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One normalized error taxonomy for every provider. Providers must never leak
|
|
3
|
+
* their native SDK errors past the adapter boundary — they route through
|
|
4
|
+
* `mapError` or `SandboxError.wrap()`.
|
|
5
|
+
*/
|
|
6
|
+
const RETRYABLE_CODES = new Set([
|
|
7
|
+
"Timeout",
|
|
8
|
+
"QuotaExceeded",
|
|
9
|
+
"Provider",
|
|
10
|
+
]);
|
|
11
|
+
export class SandboxError extends Error {
|
|
12
|
+
code;
|
|
13
|
+
provider;
|
|
14
|
+
status;
|
|
15
|
+
retryable;
|
|
16
|
+
timedOut;
|
|
17
|
+
aborted;
|
|
18
|
+
constructor(code, message, init = {}) {
|
|
19
|
+
super(`[sbox]${init.provider ? ` [${init.provider}]` : ""} ${message}`, {
|
|
20
|
+
cause: init.cause,
|
|
21
|
+
});
|
|
22
|
+
this.name = "SandboxError";
|
|
23
|
+
this.code = code;
|
|
24
|
+
this.provider = init.provider;
|
|
25
|
+
this.status = init.status;
|
|
26
|
+
this.retryable = init.retryable ?? RETRYABLE_CODES.has(code);
|
|
27
|
+
this.timedOut = init.timedOut ?? code === "Timeout";
|
|
28
|
+
this.aborted = init.aborted ?? false;
|
|
29
|
+
Error.captureStackTrace?.(this, SandboxError);
|
|
30
|
+
}
|
|
31
|
+
/** Coerce any thrown value into a SandboxError (pass-through if already one). */
|
|
32
|
+
static wrap(err, provider, fallback = "Provider") {
|
|
33
|
+
if (err instanceof SandboxError) {
|
|
34
|
+
return err;
|
|
35
|
+
}
|
|
36
|
+
if (err instanceof Error && err.name === "AbortError") {
|
|
37
|
+
return new SandboxError("Timeout", err.message, {
|
|
38
|
+
aborted: true,
|
|
39
|
+
cause: err,
|
|
40
|
+
provider,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return new SandboxError(fallback, err instanceof Error ? err.message : String(err), { cause: err, provider });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** Thrown synchronously by a namespace facade before any network call. */
|
|
47
|
+
export class NotSupportedError extends SandboxError {
|
|
48
|
+
feature;
|
|
49
|
+
constructor(provider, feature) {
|
|
50
|
+
super("NotSupported", `'${feature}' is not supported by provider '${provider}'`, { provider, retryable: false });
|
|
51
|
+
this.name = "NotSupportedError";
|
|
52
|
+
this.feature = feature;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export class ProviderNotFoundError extends SandboxError {
|
|
56
|
+
constructor(name, known = []) {
|
|
57
|
+
super("ProviderNotFound", `Unknown provider "${name}". Registered: ${known.join(", ") || "(none)"}`);
|
|
58
|
+
this.name = "ProviderNotFoundError";
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export class AllProvidersFailedError extends SandboxError {
|
|
62
|
+
attempts;
|
|
63
|
+
constructor(attempts) {
|
|
64
|
+
super("AllProvidersFailed", `all providers failed: ${attempts.map((a) => a.provider).join(", ")}`, { cause: attempts });
|
|
65
|
+
this.name = "AllProvidersFailedError";
|
|
66
|
+
this.attempts = attempts;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Centralized HTTP-status retryability classifier. */
|
|
70
|
+
export function isRetryableStatus(status) {
|
|
71
|
+
return (status === 408 ||
|
|
72
|
+
status === 409 ||
|
|
73
|
+
status === 425 ||
|
|
74
|
+
status === 429 ||
|
|
75
|
+
(status !== undefined && status >= 500));
|
|
76
|
+
}
|
|
77
|
+
export function isRetryableError(err) {
|
|
78
|
+
return err instanceof SandboxError && err.retryable;
|
|
79
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapts a raw adapter `DriverExec` (an AsyncIterable of output events) into the
|
|
3
|
+
* public `ExecHandle` — which is simultaneously awaitable (buffered ExecResult)
|
|
4
|
+
* and async-iterable (live OutputEvent stream), and also exposes byte streams.
|
|
5
|
+
* A single internal pump drives all consumers, so awaiting and streaming the
|
|
6
|
+
* same handle never double-consumes the source.
|
|
7
|
+
*/
|
|
8
|
+
import { SandboxError } from "./errors.js";
|
|
9
|
+
import type { DriverExec, ExecHandle } from "./types.js";
|
|
10
|
+
export interface ExecHandleOptions {
|
|
11
|
+
onStdout?: (chunk: string) => void;
|
|
12
|
+
onStderr?: (chunk: string) => void;
|
|
13
|
+
/** Parse `__sbox_rc=N` out of stdout to synthesize the exit code. */
|
|
14
|
+
parseExitMarker?: boolean;
|
|
15
|
+
/** Normalize errors thrown while iterating the source. */
|
|
16
|
+
mapError?: (err: unknown) => SandboxError;
|
|
17
|
+
}
|
|
18
|
+
export declare function createExecHandle(source: DriverExec, opts?: ExecHandleOptions): ExecHandle;
|
|
19
|
+
//# sourceMappingURL=exec.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exec.d.ts","sourceRoot":"","sources":["../../src/internal/exec.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,KAAK,EACV,UAAU,EACV,UAAU,EAGX,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,qEAAqE;IACrE,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,0DAA0D;IAC1D,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,YAAY,CAAC;CAC3C;AA0OD,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,UAAU,EAClB,IAAI,CAAC,EAAE,iBAAiB,GACvB,UAAU,CAEZ"}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapts a raw adapter `DriverExec` (an AsyncIterable of output events) into the
|
|
3
|
+
* public `ExecHandle` — which is simultaneously awaitable (buffered ExecResult)
|
|
4
|
+
* and async-iterable (live OutputEvent stream), and also exposes byte streams.
|
|
5
|
+
* A single internal pump drives all consumers, so awaiting and streaming the
|
|
6
|
+
* same handle never double-consumes the source.
|
|
7
|
+
*/
|
|
8
|
+
import { SandboxError } from "./errors.js";
|
|
9
|
+
const MARKER_RE = /__sbox_rc=(-?\d+)/;
|
|
10
|
+
const MARKER_STRIP_RE = /\n?__sbox_rc=-?\d+\s*/g;
|
|
11
|
+
class ExecHandleImpl {
|
|
12
|
+
#source;
|
|
13
|
+
#opts;
|
|
14
|
+
#events = [];
|
|
15
|
+
#wakers = [];
|
|
16
|
+
#startedPump = false;
|
|
17
|
+
#done = false;
|
|
18
|
+
#error = null;
|
|
19
|
+
#exitCode = 0;
|
|
20
|
+
#synthExit = 0;
|
|
21
|
+
#synthesized = false;
|
|
22
|
+
#resultPromise;
|
|
23
|
+
#resolveResult;
|
|
24
|
+
#rejectResult;
|
|
25
|
+
constructor(source, opts = {}) {
|
|
26
|
+
this.#source = source;
|
|
27
|
+
this.#opts = opts;
|
|
28
|
+
this.#resultPromise = new Promise((res, rej) => {
|
|
29
|
+
this.#resolveResult = res;
|
|
30
|
+
this.#rejectResult = rej;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
#notify() {
|
|
34
|
+
const wakers = this.#wakers;
|
|
35
|
+
this.#wakers = [];
|
|
36
|
+
for (const w of wakers) {
|
|
37
|
+
w();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
#nextTick() {
|
|
41
|
+
return new Promise((resolve) => this.#wakers.push(resolve));
|
|
42
|
+
}
|
|
43
|
+
#ensureStarted() {
|
|
44
|
+
if (this.#startedPump) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
this.#startedPump = true;
|
|
48
|
+
void this.#pump();
|
|
49
|
+
}
|
|
50
|
+
#start() {
|
|
51
|
+
this.#ensureStarted();
|
|
52
|
+
return this.#resultPromise;
|
|
53
|
+
}
|
|
54
|
+
async #pump() {
|
|
55
|
+
try {
|
|
56
|
+
for await (const ev of this.#source) {
|
|
57
|
+
this.#handle(ev);
|
|
58
|
+
}
|
|
59
|
+
this.#finish(null);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
this.#finish(error);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
#handle(ev) {
|
|
66
|
+
if (ev.type === "exit") {
|
|
67
|
+
this.#exitCode = ev.exitCode;
|
|
68
|
+
this.#events.push(ev);
|
|
69
|
+
this.#notify();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
let { data } = ev;
|
|
73
|
+
if (ev.type === "stdout" && this.#opts.parseExitMarker) {
|
|
74
|
+
const m = MARKER_RE.exec(data);
|
|
75
|
+
if (m && m[1] !== undefined) {
|
|
76
|
+
this.#synthExit = Number(m[1]);
|
|
77
|
+
this.#synthesized = true;
|
|
78
|
+
data = data.replace(MARKER_STRIP_RE, "");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (data.length === 0) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
this.#events.push({ data, type: ev.type });
|
|
85
|
+
if (ev.type === "stdout") {
|
|
86
|
+
this.#opts.onStdout?.(data);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
this.#opts.onStderr?.(data);
|
|
90
|
+
}
|
|
91
|
+
this.#notify();
|
|
92
|
+
}
|
|
93
|
+
#finish(err) {
|
|
94
|
+
this.#done = true;
|
|
95
|
+
if (err !== null && err !== undefined) {
|
|
96
|
+
this.#error = this.#opts.mapError
|
|
97
|
+
? this.#opts.mapError(err)
|
|
98
|
+
: SandboxError.wrap(err);
|
|
99
|
+
this.#rejectResult(this.#error);
|
|
100
|
+
this.#notify();
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
let stdout = "";
|
|
104
|
+
let stderr = "";
|
|
105
|
+
for (const ev of this.#events) {
|
|
106
|
+
if (ev.type === "stdout") {
|
|
107
|
+
stdout += ev.data;
|
|
108
|
+
}
|
|
109
|
+
else if (ev.type === "stderr") {
|
|
110
|
+
stderr += ev.data;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const result = {
|
|
114
|
+
exitCode: this.#synthesized ? this.#synthExit : this.#exitCode,
|
|
115
|
+
stderr,
|
|
116
|
+
stdout,
|
|
117
|
+
...(this.#synthesized ? { exitCodeSynthesized: true } : {}),
|
|
118
|
+
};
|
|
119
|
+
this.#resolveResult(result);
|
|
120
|
+
this.#notify();
|
|
121
|
+
}
|
|
122
|
+
// ---- Promise<ExecResult> ----
|
|
123
|
+
then(onfulfilled, onrejected) {
|
|
124
|
+
return this.#start().then(onfulfilled, onrejected);
|
|
125
|
+
}
|
|
126
|
+
catch(onrejected) {
|
|
127
|
+
return this.#start().catch(onrejected);
|
|
128
|
+
}
|
|
129
|
+
finally(onfinally) {
|
|
130
|
+
return this.#start().finally(onfinally);
|
|
131
|
+
}
|
|
132
|
+
[Symbol.toStringTag] = "ExecHandle";
|
|
133
|
+
// ---- AsyncIterable<OutputEvent> ----
|
|
134
|
+
async *[Symbol.asyncIterator]() {
|
|
135
|
+
this.#ensureStarted();
|
|
136
|
+
let i = 0;
|
|
137
|
+
for (;;) {
|
|
138
|
+
while (i < this.#events.length) {
|
|
139
|
+
const ev = this.#events[i];
|
|
140
|
+
i++;
|
|
141
|
+
if (ev) {
|
|
142
|
+
yield ev;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (this.#done) {
|
|
146
|
+
if (this.#error) {
|
|
147
|
+
throw this.#error;
|
|
148
|
+
}
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
await this.#nextTick();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// ---- ExecHandle extras ----
|
|
155
|
+
get pid() {
|
|
156
|
+
this.#ensureStarted();
|
|
157
|
+
return this.#source.pid;
|
|
158
|
+
}
|
|
159
|
+
#makeStream(kind) {
|
|
160
|
+
const enc = new TextEncoder();
|
|
161
|
+
let i = 0;
|
|
162
|
+
const self = this;
|
|
163
|
+
return new ReadableStream({
|
|
164
|
+
async pull(controller) {
|
|
165
|
+
for (;;) {
|
|
166
|
+
while (i < self.#events.length) {
|
|
167
|
+
const ev = self.#events[i];
|
|
168
|
+
i++;
|
|
169
|
+
if (ev && ev.type === kind) {
|
|
170
|
+
controller.enqueue(enc.encode(ev.data));
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (self.#done) {
|
|
175
|
+
if (self.#error) {
|
|
176
|
+
controller.error(self.#error);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
controller.close();
|
|
180
|
+
}
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
await self.#nextTick();
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
start: () => self.#ensureStarted(),
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
get stdout() {
|
|
190
|
+
return this.#makeStream("stdout");
|
|
191
|
+
}
|
|
192
|
+
get stderr() {
|
|
193
|
+
return this.#makeStream("stderr");
|
|
194
|
+
}
|
|
195
|
+
async kill(signal) {
|
|
196
|
+
await this.#source.kill(signal);
|
|
197
|
+
}
|
|
198
|
+
async writeStdin(data) {
|
|
199
|
+
if (!this.#source.writeStdin) {
|
|
200
|
+
throw new SandboxError("NotSupported", "stdin is not supported");
|
|
201
|
+
}
|
|
202
|
+
const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
203
|
+
await this.#source.writeStdin(bytes);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
export function createExecHandle(source, opts) {
|
|
207
|
+
return new ExecHandleImpl(source, opts);
|
|
208
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The sandbox plugin system. A plugin augments every sandbox the client builds.
|
|
3
|
+
* The headline kind is an *AI-provider* plugin (`kind: "ai-provider"`), which
|
|
4
|
+
* contributes a framework-shaped `tools` property to `sandbox.tools` via
|
|
5
|
+
* `extend`. The shape of `sandbox.tools` follows whichever AI-provider plugin
|
|
6
|
+
* is installed — the app never imports a projection function.
|
|
7
|
+
*
|
|
8
|
+
* This module is intentionally free of any agent-tools dependency: the per-create
|
|
9
|
+
* overrides reach a plugin as an opaque `createOptions` bag it interprets itself.
|
|
10
|
+
*/
|
|
11
|
+
import type { Sandbox } from "./types.js";
|
|
12
|
+
/** Standard "merge a union of object types into one intersection" helper. */
|
|
13
|
+
export type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
14
|
+
/** Passed to `extend`/`onCreate`. Carries the 2nd arg of `client.create`. */
|
|
15
|
+
export interface PluginSetupContext {
|
|
16
|
+
/** Per-sandbox overrides from `client.create(spec, options)`; plugin-defined. */
|
|
17
|
+
readonly createOptions?: Readonly<Record<string, unknown>>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A sandbox plugin. `Ext` is the typed set of properties it grafts onto the
|
|
21
|
+
* sandbox object (e.g. `{ tools: Record<string, AISDKTool> }`).
|
|
22
|
+
*/
|
|
23
|
+
export interface SandboxPlugin<Ext extends object = object> {
|
|
24
|
+
readonly name: string;
|
|
25
|
+
/** Discriminant. The client enforces AT MOST ONE `"ai-provider"` plugin. */
|
|
26
|
+
readonly kind?: "ai-provider" | "middleware" | "lifecycle" | "mcp";
|
|
27
|
+
/** Pure, synchronous projection merged onto the sandbox (keeps build sync). */
|
|
28
|
+
extend?(sandbox: Sandbox, ctx: PluginSetupContext): Ext;
|
|
29
|
+
/** Async side-effects, awaited by the client after the sandbox is built. */
|
|
30
|
+
onCreate?(sandbox: Sandbox & Ext, ctx: PluginSetupContext): void | Promise<void>;
|
|
31
|
+
/** Runs before the underlying destroy(); flush audit logs, stop servers, … */
|
|
32
|
+
onDestroy?(sandbox: Sandbox): void | Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
/** The contribution type of a single plugin. */
|
|
35
|
+
export type PluginExt<P> = P extends SandboxPlugin<infer E> ? E : object;
|
|
36
|
+
/** The merged contribution of a tuple of plugins (empty tuple -> `{}`). */
|
|
37
|
+
export type MergePlugins<Ps extends readonly SandboxPlugin[]> = Ps extends readonly [] ? object : UnionToIntersection<PluginExt<Ps[number]>>;
|
|
38
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/internal/plugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAE1C,6EAA6E;AAC7E,MAAM,MAAM,mBAAmB,CAAC,CAAC,IAAI,CACnC,CAAC,SAAS,OAAO,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,IAAI,GAAG,KAAK,CAC3C,SAAS,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,IAAI,GAC1B,CAAC,GACD,KAAK,CAAC;AAEV,6EAA6E;AAC7E,MAAM,WAAW,kBAAkB;IACjC,iFAAiF;IACjF,QAAQ,CAAC,aAAa,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CAC5D;AAED;;;GAGG;AACH,MAAM,WAAW,aAAa,CAAC,GAAG,SAAS,MAAM,GAAG,MAAM;IACxD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,4EAA4E;IAC5E,QAAQ,CAAC,IAAI,CAAC,EAAE,aAAa,GAAG,YAAY,GAAG,WAAW,GAAG,KAAK,CAAC;IACnE,+EAA+E;IAC/E,MAAM,CAAC,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,kBAAkB,GAAG,GAAG,CAAC;IACxD,4EAA4E;IAC5E,QAAQ,CAAC,CACP,OAAO,EAAE,OAAO,GAAG,GAAG,EACtB,GAAG,EAAE,kBAAkB,GACtB,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACxB,8EAA8E;IAC9E,SAAS,CAAC,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACpD;AAED,gDAAgD;AAChD,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,SAAS,aAAa,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC;AAEzE,2EAA2E;AAC3E,MAAM,MAAM,YAAY,CAAC,EAAE,SAAS,SAAS,aAAa,EAAE,IAC1D,EAAE,SAAS,SAAS,EAAE,GAAG,MAAM,GAAG,mBAAmB,CAAC,SAAS,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tiny runtime probe so the (fetch-only) core can adapt without importing any
|
|
3
|
+
* `node:` module. Providers that genuinely need Node APIs do their own checks.
|
|
4
|
+
*/
|
|
5
|
+
export type Runtime = "node" | "bun" | "deno" | "workerd" | "unknown";
|
|
6
|
+
export declare function detectRuntime(): Runtime;
|
|
7
|
+
export declare function hasFetch(): boolean;
|
|
8
|
+
//# sourceMappingURL=runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/internal/runtime.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,OAAO,GAAG,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,SAAS,GAAG,SAAS,CAAC;AAEtE,wBAAgB,aAAa,IAAI,OAAO,CAmBvC;AAED,wBAAgB,QAAQ,IAAI,OAAO,CAElC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function detectRuntime() {
|
|
2
|
+
const g = globalThis;
|
|
3
|
+
if (g.Deno) {
|
|
4
|
+
return "deno";
|
|
5
|
+
}
|
|
6
|
+
if (g.Bun) {
|
|
7
|
+
return "bun";
|
|
8
|
+
}
|
|
9
|
+
if (typeof navigator !== "undefined" &&
|
|
10
|
+
navigator.userAgent === "Cloudflare-Workers") {
|
|
11
|
+
return "workerd";
|
|
12
|
+
}
|
|
13
|
+
const proc = g.process;
|
|
14
|
+
if (proc?.versions?.node) {
|
|
15
|
+
return "node";
|
|
16
|
+
}
|
|
17
|
+
return "unknown";
|
|
18
|
+
}
|
|
19
|
+
export function hasFetch() {
|
|
20
|
+
return typeof globalThis.fetch === "function";
|
|
21
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { CapabilityMap } from "./capabilities.js";
|
|
2
|
+
import type { PluginSetupContext, SandboxPlugin } from "./plugin.js";
|
|
3
|
+
import type { Sandbox, SandboxProvider } from "./types.js";
|
|
4
|
+
export interface BuildSandboxBase {
|
|
5
|
+
fetch: typeof fetch;
|
|
6
|
+
emulate?: (keyof CapabilityMap)[];
|
|
7
|
+
defaultMetadata?: Record<string, string>;
|
|
8
|
+
/** Plugins to graft onto every sandbox (applied here so forks inherit them). */
|
|
9
|
+
plugins?: readonly SandboxPlugin[];
|
|
10
|
+
}
|
|
11
|
+
export declare function buildSandbox<Caps extends CapabilityMap, Raw>(provider: SandboxProvider<Caps, Raw>, handle: import("./types.js").DriverHandle<Raw>, base: BuildSandboxBase, setup?: PluginSetupContext): Sandbox<Caps, Raw>;
|
|
12
|
+
//# sourceMappingURL=sandbox.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sandbox.d.ts","sourceRoot":"","sources":["../../src/internal/sandbox.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAGvD,OAAO,KAAK,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAQrE,OAAO,KAAK,EAaV,OAAO,EACP,eAAe,EAIhB,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,OAAO,KAAK,CAAC;IACpB,OAAO,CAAC,EAAE,CAAC,MAAM,aAAa,CAAC,EAAE,CAAC;IAClC,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzC,gFAAgF;IAChF,OAAO,CAAC,EAAE,SAAS,aAAa,EAAE,CAAC;CACpC;AAED,wBAAgB,YAAY,CAAC,IAAI,SAAS,aAAa,EAAE,GAAG,EAC1D,QAAQ,EAAE,eAAe,CAAC,IAAI,EAAE,GAAG,CAAC,EACpC,MAAM,EAAE,OAAO,YAAY,EAAE,YAAY,CAAC,GAAG,CAAC,EAC9C,IAAI,EAAE,gBAAgB,EACtB,KAAK,GAAE,kBAAuB,GAC7B,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAoYpB"}
|