@keln.ai/sdk 0.0.0-stage → 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/CHANGELOG.md +14 -0
- package/LICENSE +202 -0
- package/NOTICE +6 -0
- package/README.md +598 -2
- package/dist/api-promise.d.ts +30 -0
- package/dist/api-promise.js +38 -0
- package/dist/api-promise.js.map +1 -0
- package/dist/api.d.ts +130 -0
- package/dist/api.js +321 -0
- package/dist/api.js.map +1 -0
- package/dist/client.d.ts +27 -0
- package/dist/client.js +42 -0
- package/dist/client.js.map +1 -0
- package/dist/commands.d.ts +78 -0
- package/dist/commands.js +269 -0
- package/dist/commands.js.map +1 -0
- package/dist/errors.d.ts +93 -0
- package/dist/errors.js +173 -0
- package/dist/errors.js.map +1 -0
- package/dist/files.d.ts +33 -0
- package/dist/files.js +130 -0
- package/dist/files.js.map +1 -0
- package/dist/images.d.ts +79 -0
- package/dist/images.js +150 -0
- package/dist/images.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/inference.d.ts +672 -0
- package/dist/inference.js +244 -0
- package/dist/inference.js.map +1 -0
- package/dist/retry.d.ts +10 -0
- package/dist/retry.js +87 -0
- package/dist/retry.js.map +1 -0
- package/dist/sandbox.d.ts +132 -0
- package/dist/sandbox.js +216 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/sse.d.ts +20 -0
- package/dist/sse.js +119 -0
- package/dist/sse.js.map +1 -0
- package/dist/streaming.d.ts +20 -0
- package/dist/streaming.js +77 -0
- package/dist/streaming.js.map +1 -0
- package/dist/terminals.d.ts +75 -0
- package/dist/terminals.js +239 -0
- package/dist/terminals.js.map +1 -0
- package/dist/types.d.ts +226 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/dist/uploads.d.ts +35 -0
- package/dist/uploads.js +154 -0
- package/dist/uploads.js.map +1 -0
- package/dist/util.d.ts +19 -0
- package/dist/util.js +69 -0
- package/dist/util.js.map +1 -0
- package/dist/web.d.ts +66 -0
- package/dist/web.js +44 -0
- package/dist/web.js.map +1 -0
- package/package.json +57 -4
- package/src/api-promise.ts +50 -0
- package/src/api.ts +415 -0
- package/src/client.ts +43 -0
- package/src/commands.ts +325 -0
- package/src/errors.ts +185 -0
- package/src/files.ts +149 -0
- package/src/images.ts +174 -0
- package/src/index.ts +114 -0
- package/src/inference.ts +737 -0
- package/src/retry.ts +77 -0
- package/src/sandbox.ts +281 -0
- package/src/sse.ts +124 -0
- package/src/streaming.ts +79 -0
- package/src/terminals.ts +269 -0
- package/src/types.ts +241 -0
- package/src/uploads.ts +177 -0
- package/src/util.ts +78 -0
- package/src/web.ts +94 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { apiError } from './errors.js';
|
|
2
|
+
import { sandboxPath } from './util.js';
|
|
3
|
+
/** A terminal socket's frames: the first byte of each says what it is. */
|
|
4
|
+
const OP_DATA = 0x00;
|
|
5
|
+
const OP_RESIZE = 0x01;
|
|
6
|
+
const OP_EXIT = 0x03;
|
|
7
|
+
/** Unread output frames before the socket stops reading: a slow reader slows the program, not fills memory. */
|
|
8
|
+
const PAUSE_AT_FRAMES = 1024;
|
|
9
|
+
/**
|
|
10
|
+
* An open socket to a terminal: `send` input, `resize` it, iterate its output bytes. Iterating ends when the program
|
|
11
|
+
* exits, with its code in `exitCode`, or after `close()`. A socket dropped before that throws `KelnConnectionError`;
|
|
12
|
+
* the program may still run, and `terminal.attach()` opens a new socket. An aborted signal ends it with its reason.
|
|
13
|
+
*/
|
|
14
|
+
export class TerminalSocket {
|
|
15
|
+
/** The program's exit code, once the socket has ended, if the API sent one. */
|
|
16
|
+
exitCode;
|
|
17
|
+
#api;
|
|
18
|
+
#socket;
|
|
19
|
+
#output = [];
|
|
20
|
+
#wake;
|
|
21
|
+
#ended = false;
|
|
22
|
+
#closing = false;
|
|
23
|
+
#failure;
|
|
24
|
+
#opened = Promise.withResolvers();
|
|
25
|
+
#closed = Promise.withResolvers();
|
|
26
|
+
/** Every listener goes on before the socket connects: a first frame can arrive with the upgrade's answer. */
|
|
27
|
+
constructor(api, socket, signal) {
|
|
28
|
+
this.#api = api;
|
|
29
|
+
this.#socket = socket;
|
|
30
|
+
this.#opened.promise.catch(() => { });
|
|
31
|
+
socket.binaryType = 'nodebuffer';
|
|
32
|
+
const onAbort = () => {
|
|
33
|
+
this.#failure = signal?.reason;
|
|
34
|
+
this.#output.length = 0;
|
|
35
|
+
this.#opened.reject(signal?.reason);
|
|
36
|
+
socket.terminate();
|
|
37
|
+
};
|
|
38
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
39
|
+
socket.on('open', () => this.#opened.resolve());
|
|
40
|
+
socket.on('unexpected-response', (_request, response) => this.#refused(response));
|
|
41
|
+
socket.on('error', (error) => this.#fail(error));
|
|
42
|
+
socket.on('message', (data, isBinary) => this.#frame(data, isBinary));
|
|
43
|
+
socket.on('close', (code, reason) => {
|
|
44
|
+
signal?.removeEventListener('abort', onAbort);
|
|
45
|
+
this.#ended = true;
|
|
46
|
+
// The service closes with `exit:<code>` after the exit frame, and the close alone can carry it.
|
|
47
|
+
const exit = /^exit:(-?\d+)$/.exec(reason.toString('utf8'));
|
|
48
|
+
if (exit !== null)
|
|
49
|
+
this.exitCode ??= Number(exit[1]);
|
|
50
|
+
if (code !== 1000 && this.exitCode === undefined && !this.#closing)
|
|
51
|
+
this.#fail(`the socket closed with code ${code}`);
|
|
52
|
+
this.#opened.reject(this.#failure);
|
|
53
|
+
this.#closed.resolve();
|
|
54
|
+
this.#wake?.();
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/** Attaches, with the retries a read gets. Used by `terminal.attach()`. */
|
|
58
|
+
static async open(api, path, signal) {
|
|
59
|
+
return await api.retrying(true, signal, async () => {
|
|
60
|
+
signal?.throwIfAborted();
|
|
61
|
+
const socket = new TerminalSocket(api, api.webSocket(path), signal);
|
|
62
|
+
await socket.#opened.promise;
|
|
63
|
+
return socket;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/** A refused upgrade: the API's answer, read whole, with its headers. */
|
|
67
|
+
#refused(response) {
|
|
68
|
+
const chunks = [];
|
|
69
|
+
response.on('data', (chunk) => chunks.push(chunk));
|
|
70
|
+
response.on('end', () => {
|
|
71
|
+
const text = this.#api.redact(Buffer.concat(chunks).toString('utf8'));
|
|
72
|
+
const headers = new Headers();
|
|
73
|
+
for (const [name, value] of Object.entries(response.headers)) {
|
|
74
|
+
for (const one of Array.isArray(value) ? value : value === undefined ? [] : [value])
|
|
75
|
+
headers.append(name, one);
|
|
76
|
+
}
|
|
77
|
+
const retryAfter = response.headers['retry-after'];
|
|
78
|
+
this.#failure = apiError(response.statusCode, text, Array.isArray(retryAfter) ? retryAfter[0] : retryAfter, headers);
|
|
79
|
+
this.#opened.reject(this.#failure);
|
|
80
|
+
this.#socket.terminate();
|
|
81
|
+
});
|
|
82
|
+
response.on('error', (error) => {
|
|
83
|
+
this.#fail(error);
|
|
84
|
+
this.#socket.terminate();
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
#fail(cause) {
|
|
88
|
+
this.#failure ??= this.#api.connectionError(cause);
|
|
89
|
+
this.#opened.reject(this.#failure);
|
|
90
|
+
}
|
|
91
|
+
#frame(data, isBinary) {
|
|
92
|
+
if (!isBinary)
|
|
93
|
+
return; // text frames and unknown opcodes are not part of the protocol
|
|
94
|
+
if (data[0] === OP_DATA && data.length > 1) {
|
|
95
|
+
this.#output.push(new Uint8Array(data.subarray(1)));
|
|
96
|
+
if (this.#output.length >= PAUSE_AT_FRAMES)
|
|
97
|
+
this.#socket.pause();
|
|
98
|
+
this.#wake?.();
|
|
99
|
+
}
|
|
100
|
+
else if (data[0] === OP_EXIT && data.length >= 5) {
|
|
101
|
+
this.exitCode = data.readInt32BE(1);
|
|
102
|
+
void this.close(); // the last frame. The server closes next; a late frame is still read until it does
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
#write(frame) {
|
|
106
|
+
return new Promise((resolve, reject) => {
|
|
107
|
+
this.#socket.send(frame, { binary: true }, (error) => {
|
|
108
|
+
if (error === undefined || error === null)
|
|
109
|
+
resolve();
|
|
110
|
+
else
|
|
111
|
+
reject(this.#api.connectionError(error));
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/** Sends input to the program. */
|
|
116
|
+
async send(data) {
|
|
117
|
+
const bytes = typeof data === 'string' ? new TextEncoder().encode(data) : data;
|
|
118
|
+
const frame = new Uint8Array(bytes.length + 1);
|
|
119
|
+
frame[0] = OP_DATA;
|
|
120
|
+
frame.set(bytes, 1);
|
|
121
|
+
await this.#write(frame);
|
|
122
|
+
}
|
|
123
|
+
/** Resizes the terminal. */
|
|
124
|
+
async resize(cols, rows) {
|
|
125
|
+
const frame = Buffer.alloc(5);
|
|
126
|
+
frame[0] = OP_RESIZE;
|
|
127
|
+
frame.writeUInt16BE(cols, 1);
|
|
128
|
+
frame.writeUInt16BE(rows, 3);
|
|
129
|
+
await this.#write(frame);
|
|
130
|
+
}
|
|
131
|
+
/** Closes the socket. The program keeps running; `terminal.kill()` ends it. */
|
|
132
|
+
async close() {
|
|
133
|
+
this.#closing = true;
|
|
134
|
+
this.#socket.resume(); // a paused socket would never read the close it is waiting for
|
|
135
|
+
this.#socket.close();
|
|
136
|
+
await this.#closed.promise;
|
|
137
|
+
}
|
|
138
|
+
async *[Symbol.asyncIterator]() {
|
|
139
|
+
while (true) {
|
|
140
|
+
const chunk = this.#output.shift();
|
|
141
|
+
if (chunk !== undefined) {
|
|
142
|
+
if (this.#output.length === 0)
|
|
143
|
+
this.#socket.resume();
|
|
144
|
+
yield chunk;
|
|
145
|
+
}
|
|
146
|
+
else if (this.#ended) {
|
|
147
|
+
if (this.#failure !== undefined)
|
|
148
|
+
throw this.#failure;
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
await new Promise((resolve) => {
|
|
153
|
+
this.#wake = resolve;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/** One terminal. Its fields are the API's, as of the last answer. */
|
|
160
|
+
export class Terminal {
|
|
161
|
+
id;
|
|
162
|
+
object;
|
|
163
|
+
sandboxId;
|
|
164
|
+
state;
|
|
165
|
+
cols;
|
|
166
|
+
rows;
|
|
167
|
+
exitCode;
|
|
168
|
+
createdAt;
|
|
169
|
+
endedAt;
|
|
170
|
+
/** The object as the API sent it. */
|
|
171
|
+
raw;
|
|
172
|
+
#api;
|
|
173
|
+
/** Made by `sandbox.terminals`. */
|
|
174
|
+
constructor(api, data) {
|
|
175
|
+
this.#api = api;
|
|
176
|
+
this.#apply(data);
|
|
177
|
+
}
|
|
178
|
+
#apply(data) {
|
|
179
|
+
this.raw = data;
|
|
180
|
+
this.id = data.id;
|
|
181
|
+
this.object = data.object;
|
|
182
|
+
this.sandboxId = data.sandbox_id;
|
|
183
|
+
this.state = data.state;
|
|
184
|
+
this.cols = data.cols;
|
|
185
|
+
this.rows = data.rows;
|
|
186
|
+
this.exitCode = data.exit_code ?? null;
|
|
187
|
+
this.createdAt = data.created_at;
|
|
188
|
+
this.endedAt = data.ended_at ?? null;
|
|
189
|
+
return this;
|
|
190
|
+
}
|
|
191
|
+
get #path() {
|
|
192
|
+
return `${sandboxPath(this.sandboxId)}/terminals/${encodeURIComponent(this.id)}`;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Opens a socket to the terminal. A terminal can be attached again after a disconnect; a socket receives the
|
|
196
|
+
* output written while it is attached. The signal stays with the socket: aborting it later ends the socket.
|
|
197
|
+
*/
|
|
198
|
+
async attach(options = {}) {
|
|
199
|
+
return await TerminalSocket.open(this.#api, `${this.#path}/ws`, options.signal);
|
|
200
|
+
}
|
|
201
|
+
/** Reads the terminal again and updates this object. */
|
|
202
|
+
async refresh(options = {}) {
|
|
203
|
+
return this.#apply(await this.#api.json('GET', this.#path, options));
|
|
204
|
+
}
|
|
205
|
+
/** Resizes the terminal, the same as a resize frame on a socket. */
|
|
206
|
+
async resize(cols, rows, options = {}) {
|
|
207
|
+
const path = `${this.#path}/resize`;
|
|
208
|
+
return this.#apply(await this.#api.json('POST', path, { ...options, json: { cols, rows } }));
|
|
209
|
+
}
|
|
210
|
+
/** Ends the terminal's program. Ending an exited terminal changes nothing. */
|
|
211
|
+
async kill(options = {}) {
|
|
212
|
+
return this.#apply(await this.#api.json('DELETE', this.#path, options));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/** The terminals of one sandbox: `sandbox.terminals`. */
|
|
216
|
+
export class Terminals {
|
|
217
|
+
#api;
|
|
218
|
+
#path;
|
|
219
|
+
/** Made by `Sandbox`. */
|
|
220
|
+
constructor(api, sandboxId) {
|
|
221
|
+
this.#api = api;
|
|
222
|
+
this.#path = `${sandboxPath(sandboxId)}/terminals`;
|
|
223
|
+
}
|
|
224
|
+
/** Starts a program on a pseudo-terminal. Attach to it with `terminal.attach()`. */
|
|
225
|
+
async create(options = {}) {
|
|
226
|
+
const { signal, ...json } = options;
|
|
227
|
+
return new Terminal(this.#api, await this.#api.json('POST', this.#path, { json, signal }));
|
|
228
|
+
}
|
|
229
|
+
/** The sandbox's terminals, oldest first. */
|
|
230
|
+
async list(options = {}) {
|
|
231
|
+
const page = await this.#api.json('GET', this.#path, options);
|
|
232
|
+
return page.data.map((data) => new Terminal(this.#api, data));
|
|
233
|
+
}
|
|
234
|
+
async get(terminalId, options = {}) {
|
|
235
|
+
const path = `${this.#path}/${encodeURIComponent(terminalId)}`;
|
|
236
|
+
return new Terminal(this.#api, await this.#api.json('GET', path, options));
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
//# sourceMappingURL=terminals.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"terminals.js","sourceRoot":"","sources":["../src/terminals.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ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AAK,CAAC,MAAM,CAAC,UAAiC,EAAE;QAC9C,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;QACpC,OAAO,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAe,MAAM,EAAE,IAAI,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;IAC3G,CAAC;IAED,6CAA6C;IAC7C,KAAK,CAAC,IAAI,CAAC,UAAuB,EAAE;QAClC,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAyB,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACtF,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;IAChE,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,UAAkB,EAAE,UAAuB,EAAE;QACrD,MAAM,IAAI,GAAG,GAAG,IAAI,CAAC,KAAK,IAAI,kBAAkB,CAAC,UAAU,CAAC,EAAE,CAAC;QAC/D,OAAO,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAe,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;IAC3F,CAAC;CACF"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Keln Sandbox API's shapes. The `*Data` types are what the API sends, snake_case, as `raw` holds them; a field
|
|
3
|
+
* added later is reachable through the index signature.
|
|
4
|
+
*/
|
|
5
|
+
/** Every call takes one. Aborting it ends the call with the signal's reason. */
|
|
6
|
+
export type CallOptions = {
|
|
7
|
+
signal?: AbortSignal;
|
|
8
|
+
};
|
|
9
|
+
/** `starting`, `running`, `paused`, `stopped`, or a state added later, passed through as a string. */
|
|
10
|
+
export type SandboxState = 'starting' | 'running' | 'paused' | 'stopped' | (string & {});
|
|
11
|
+
/** `running`, `exited`, or a state added later, passed through as a string. */
|
|
12
|
+
export type CommandState = 'running' | 'exited' | (string & {});
|
|
13
|
+
export type TerminalState = 'running' | 'exited' | (string & {});
|
|
14
|
+
export type Network = {
|
|
15
|
+
/** Outbound internet access, DNS included. `false` blocks everything not in `allow`. Default `true`. */
|
|
16
|
+
internet?: boolean;
|
|
17
|
+
/** Domains, IPv4 addresses or IPv4 CIDRs reachable even with `internet` false. */
|
|
18
|
+
allow?: string[];
|
|
19
|
+
/** Domains, IPv4 addresses or IPv4 CIDRs always blocked. Wins over `allow`. */
|
|
20
|
+
deny?: string[];
|
|
21
|
+
};
|
|
22
|
+
export type Port = {
|
|
23
|
+
port: number;
|
|
24
|
+
public: boolean;
|
|
25
|
+
/** `https://<port>-<id without sbx_>.<preview domain>`. */
|
|
26
|
+
url: string;
|
|
27
|
+
};
|
|
28
|
+
export type SandboxData = {
|
|
29
|
+
id: string;
|
|
30
|
+
object: string;
|
|
31
|
+
state: SandboxState;
|
|
32
|
+
template: string;
|
|
33
|
+
image?: string | null;
|
|
34
|
+
cpu: number;
|
|
35
|
+
memory_mb: number;
|
|
36
|
+
disk_mb: number;
|
|
37
|
+
created_at: number;
|
|
38
|
+
started_at?: number | null;
|
|
39
|
+
expires_at: number;
|
|
40
|
+
ended_at?: number | null;
|
|
41
|
+
paused_at?: number | null;
|
|
42
|
+
end_reason?: string | null;
|
|
43
|
+
metadata: Record<string, string>;
|
|
44
|
+
network: Network;
|
|
45
|
+
ports?: Port[];
|
|
46
|
+
port_token?: string | null;
|
|
47
|
+
[field: string]: unknown;
|
|
48
|
+
};
|
|
49
|
+
export type SandboxListData = {
|
|
50
|
+
object: string;
|
|
51
|
+
data: SandboxData[];
|
|
52
|
+
has_more: boolean;
|
|
53
|
+
next_cursor?: string | null;
|
|
54
|
+
[field: string]: unknown;
|
|
55
|
+
};
|
|
56
|
+
export type SandboxTemplateData = {
|
|
57
|
+
object: string;
|
|
58
|
+
name: string;
|
|
59
|
+
cpu: number;
|
|
60
|
+
memory_mb: number;
|
|
61
|
+
disk_mb: number;
|
|
62
|
+
[field: string]: unknown;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* `creating`, `ready`, `failed`, or a state added later, passed through as a string. `deleted` comes only in the answer
|
|
66
|
+
* to a delete.
|
|
67
|
+
*/
|
|
68
|
+
export type SandboxImageState = 'creating' | 'ready' | 'failed' | 'deleted' | (string & {});
|
|
69
|
+
/** Why a `failed` image failed. A code added later passes through as a string. */
|
|
70
|
+
export type SandboxImageError = {
|
|
71
|
+
code: 'snapshot_failed' | 'sandbox_stopped' | (string & {});
|
|
72
|
+
message: string;
|
|
73
|
+
[field: string]: unknown;
|
|
74
|
+
};
|
|
75
|
+
export type SandboxImageData = {
|
|
76
|
+
id: string;
|
|
77
|
+
object: string;
|
|
78
|
+
state: SandboxImageState;
|
|
79
|
+
sandbox_id: string;
|
|
80
|
+
template: string;
|
|
81
|
+
cpu: number;
|
|
82
|
+
memory_mb: number;
|
|
83
|
+
disk_mb: number;
|
|
84
|
+
size_bytes: number | null;
|
|
85
|
+
created_at: number;
|
|
86
|
+
ready_at: number | null;
|
|
87
|
+
error: SandboxImageError | null;
|
|
88
|
+
metadata: Record<string, string>;
|
|
89
|
+
[field: string]: unknown;
|
|
90
|
+
};
|
|
91
|
+
export type SandboxImageListData = {
|
|
92
|
+
object: string;
|
|
93
|
+
data: SandboxImageData[];
|
|
94
|
+
has_more: boolean;
|
|
95
|
+
next_cursor?: string | null;
|
|
96
|
+
[field: string]: unknown;
|
|
97
|
+
};
|
|
98
|
+
export type CommandData = {
|
|
99
|
+
id: string;
|
|
100
|
+
object: string;
|
|
101
|
+
sandbox_id: string;
|
|
102
|
+
state: CommandState;
|
|
103
|
+
argv: string[];
|
|
104
|
+
exit_code?: number | null;
|
|
105
|
+
signal?: number | null;
|
|
106
|
+
oom_killed?: boolean;
|
|
107
|
+
stdin: boolean;
|
|
108
|
+
started_at: number;
|
|
109
|
+
ended_at?: number | null;
|
|
110
|
+
[field: string]: unknown;
|
|
111
|
+
};
|
|
112
|
+
export type TerminalData = {
|
|
113
|
+
id: string;
|
|
114
|
+
object: string;
|
|
115
|
+
sandbox_id: string;
|
|
116
|
+
state: TerminalState;
|
|
117
|
+
cols: number;
|
|
118
|
+
rows: number;
|
|
119
|
+
exit_code?: number | null;
|
|
120
|
+
created_at: number;
|
|
121
|
+
ended_at?: number | null;
|
|
122
|
+
[field: string]: unknown;
|
|
123
|
+
};
|
|
124
|
+
export type FileWrittenData = {
|
|
125
|
+
object: string;
|
|
126
|
+
path: string;
|
|
127
|
+
size: number;
|
|
128
|
+
[field: string]: unknown;
|
|
129
|
+
};
|
|
130
|
+
export type FileEntryData = {
|
|
131
|
+
name: string;
|
|
132
|
+
path: string;
|
|
133
|
+
type: string;
|
|
134
|
+
size?: number | null;
|
|
135
|
+
modified_at?: number | null;
|
|
136
|
+
[field: string]: unknown;
|
|
137
|
+
};
|
|
138
|
+
export type FileStatData = {
|
|
139
|
+
object: string;
|
|
140
|
+
path: string;
|
|
141
|
+
type: string;
|
|
142
|
+
size?: number | null;
|
|
143
|
+
modified_at?: number | null;
|
|
144
|
+
[field: string]: unknown;
|
|
145
|
+
};
|
|
146
|
+
export type ListData<T> = {
|
|
147
|
+
object: string;
|
|
148
|
+
data: T[];
|
|
149
|
+
[field: string]: unknown;
|
|
150
|
+
};
|
|
151
|
+
export type SandboxTemplate = {
|
|
152
|
+
object: string;
|
|
153
|
+
name: string;
|
|
154
|
+
/** The default vCPUs. */
|
|
155
|
+
cpu: number;
|
|
156
|
+
/** The default memory in MiB. */
|
|
157
|
+
memoryMb: number;
|
|
158
|
+
diskMb: number;
|
|
159
|
+
raw: SandboxTemplateData;
|
|
160
|
+
};
|
|
161
|
+
export type FileWritten = {
|
|
162
|
+
object: string;
|
|
163
|
+
path: string;
|
|
164
|
+
/** Bytes written. */
|
|
165
|
+
size: number;
|
|
166
|
+
raw: FileWrittenData;
|
|
167
|
+
};
|
|
168
|
+
export type FileEntry = {
|
|
169
|
+
name: string;
|
|
170
|
+
path: string;
|
|
171
|
+
/** `file` or `dir`. A symbolic link lists as `file`. */
|
|
172
|
+
type: string;
|
|
173
|
+
size: number | null;
|
|
174
|
+
modifiedAt: number | null;
|
|
175
|
+
raw: FileEntryData;
|
|
176
|
+
};
|
|
177
|
+
export type FileStat = {
|
|
178
|
+
object: string;
|
|
179
|
+
path: string;
|
|
180
|
+
/** `file`, `dir` or `other` (a device, socket or pipe). */
|
|
181
|
+
type: string;
|
|
182
|
+
size: number | null;
|
|
183
|
+
modifiedAt: number | null;
|
|
184
|
+
raw: FileStatData;
|
|
185
|
+
};
|
|
186
|
+
/** What a command ended with. A non-zero exit is a result, not an exception. */
|
|
187
|
+
export type CommandResult = {
|
|
188
|
+
/** The command's id, `cmd_…`. */
|
|
189
|
+
id: string;
|
|
190
|
+
exitCode: number | null;
|
|
191
|
+
stdout: string;
|
|
192
|
+
stderr: string;
|
|
193
|
+
/** The signal that ended it, if one did. */
|
|
194
|
+
exitSignal: number | null;
|
|
195
|
+
oomKilled: boolean;
|
|
196
|
+
};
|
|
197
|
+
export type StartedEvent = {
|
|
198
|
+
type: 'started';
|
|
199
|
+
/** The event's position in the command's stream, the value of `lastEventId`. */
|
|
200
|
+
id: string;
|
|
201
|
+
commandId: string;
|
|
202
|
+
pid: number;
|
|
203
|
+
raw: Record<string, unknown>;
|
|
204
|
+
};
|
|
205
|
+
export type OutputEvent = {
|
|
206
|
+
type: 'stdout' | 'stderr';
|
|
207
|
+
id: string;
|
|
208
|
+
/** The output bytes. */
|
|
209
|
+
data: Uint8Array;
|
|
210
|
+
/** When the output was captured, Unix milliseconds. */
|
|
211
|
+
ts: number | undefined;
|
|
212
|
+
raw: Record<string, unknown>;
|
|
213
|
+
};
|
|
214
|
+
export type ExitEvent = {
|
|
215
|
+
type: 'exit';
|
|
216
|
+
id: string;
|
|
217
|
+
exitCode: number | null;
|
|
218
|
+
signal: number | null;
|
|
219
|
+
oomKilled: boolean;
|
|
220
|
+
raw: Record<string, unknown>;
|
|
221
|
+
};
|
|
222
|
+
/**
|
|
223
|
+
* One event of a command's stream. A type added to the API later is passed through too, with its name in `type`, its
|
|
224
|
+
* id in `id` and its data in `raw`. It is not in this union, which keeps `event.type === 'stdout'` narrowing.
|
|
225
|
+
*/
|
|
226
|
+
export type CommandEvent = StartedEvent | OutputEvent | ExitEvent;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** Text, binary content, or a Blob inside a file's parts. */
|
|
2
|
+
type BlobLikePart = string | ArrayBuffer | ArrayBufferView | Blob;
|
|
3
|
+
/** The parts a `File` is made of. The SDK builds without the DOM types, so it names them itself. */
|
|
4
|
+
type BlobPart = BlobLikePart;
|
|
5
|
+
/** A `File`'s type and modification time. */
|
|
6
|
+
type FilePropertyBag = {
|
|
7
|
+
type?: string;
|
|
8
|
+
lastModified?: number;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* What a call's `file` may be: a `File` or `Blob`, a fetch `Response`, or any async iterable of bytes, a Node
|
|
12
|
+
* `fs.createReadStream(path)` among them. `toFile` makes a `File` from a `Buffer` or `Uint8Array`.
|
|
13
|
+
*/
|
|
14
|
+
export type Uploadable = File | Blob | Response | AsyncIterable<BlobLikePart>;
|
|
15
|
+
/** What `toFile` takes. A top-level string is left out, so a path is never mistaken for the file's contents. */
|
|
16
|
+
export type ToFileInput = File | Blob | Response | Exclude<BlobLikePart, string> | AsyncIterable<BlobLikePart>;
|
|
17
|
+
/** A `File`; a missing name becomes `unknown_file`. */
|
|
18
|
+
export declare function makeFile(fileBits: BlobPart[], fileName: string | undefined, options?: FilePropertyBag): File;
|
|
19
|
+
/** A filename from an object's `name`, `filename`, `url` or `path`, without its directories. */
|
|
20
|
+
export declare function getName(value: unknown): string | undefined;
|
|
21
|
+
/** Whether `value` is a file to upload: a `File` or `Blob`, a fetch `Response`, or an async iterable of bytes. */
|
|
22
|
+
export declare function isUploadable(value: unknown): value is Uploadable;
|
|
23
|
+
/**
|
|
24
|
+
* Content as a `File`, buffered: a `File` comes back as it is unless renamed or retyped, a `Response` or `Blob` keeps
|
|
25
|
+
* its type, and bytes or an async iterable of them become a file named `name`, else after the input's own name.
|
|
26
|
+
*/
|
|
27
|
+
export declare function toFile(value: ToFileInput | PromiseLike<ToFileInput>, name?: string | null, options?: FilePropertyBag): Promise<File>;
|
|
28
|
+
/** A request body as `FormData`, its fields in the body's order. An abort of `signal` stops reading its files. */
|
|
29
|
+
export declare function createForm(body: Record<string, unknown>, signal?: AbortSignal): Promise<FormData>;
|
|
30
|
+
/**
|
|
31
|
+
* A file as a base64 data URL, read whole and typed as its multipart part would be: the file's own type, else
|
|
32
|
+
* `application/octet-stream`. The API reads an image's real type from its bytes. An abort of `signal` stops the read.
|
|
33
|
+
*/
|
|
34
|
+
export declare function toDataUrl(value: Uploadable, signal?: AbortSignal): Promise<string>;
|
|
35
|
+
export {};
|
package/dist/uploads.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// Portions adapted from openai-node (Apache-2.0, Copyright 2026 OpenAI) and changed by Keln. See NOTICE.
|
|
2
|
+
/**
|
|
3
|
+
* Uploads: what a call's `file` may be, and the multipart form it travels in (`keln.audio.transcriptions`,
|
|
4
|
+
* `keln.images.edit`). Node 22 and later only, so the global `File`, `Blob` and `FormData` are used as they are. Every
|
|
5
|
+
* upload is read into memory before it is sent, so a retry can send it again, and a call's signal stops that read.
|
|
6
|
+
* Filenames are the last part of a path. A file can also go as a base64 data URL, for the JSON form of an image edit.
|
|
7
|
+
*/
|
|
8
|
+
import { untilAborted } from './util.js';
|
|
9
|
+
/** A `File`; a missing name becomes `unknown_file`. */
|
|
10
|
+
export function makeFile(fileBits, fileName, options) {
|
|
11
|
+
// The runtime File takes every BlobPart; Node's declared constructor leaves ArrayBuffer out.
|
|
12
|
+
return new File(fileBits, fileName ?? 'unknown_file', options);
|
|
13
|
+
}
|
|
14
|
+
function basename(value) {
|
|
15
|
+
return value.split(/[\\/]/).pop() || undefined;
|
|
16
|
+
}
|
|
17
|
+
/** A filename from an object's `name`, `filename`, `url` or `path`, without its directories. */
|
|
18
|
+
export function getName(value) {
|
|
19
|
+
if (typeof value !== 'object' || value === null)
|
|
20
|
+
return undefined;
|
|
21
|
+
const v = value;
|
|
22
|
+
const text = (x) => (x ? String(x) : undefined);
|
|
23
|
+
const explicit = text(v.name) ?? text(v.filename);
|
|
24
|
+
if (explicit !== undefined)
|
|
25
|
+
return basename(explicit);
|
|
26
|
+
const url = text(v.url);
|
|
27
|
+
if (url !== undefined) {
|
|
28
|
+
try {
|
|
29
|
+
return basename(decodeURIComponent(new URL(url).pathname));
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return basename(url);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const path = text(v.path);
|
|
36
|
+
return path !== undefined ? basename(path) : undefined;
|
|
37
|
+
}
|
|
38
|
+
const isAsyncIterable = (value) => value !== null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
|
|
39
|
+
/** Whether `value` is a file to upload: a `File` or `Blob`, a fetch `Response`, or an async iterable of bytes. */
|
|
40
|
+
export function isUploadable(value) {
|
|
41
|
+
return value instanceof Blob || value instanceof Response || isAsyncIterable(value);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The parts of `value`, an async iterable read to its end. An abort of `signal` throws its reason at once and ends
|
|
45
|
+
* the iteration, so a stream that is read stops.
|
|
46
|
+
*/
|
|
47
|
+
async function getBytes(value, signal) {
|
|
48
|
+
if (typeof value === 'string' || ArrayBuffer.isView(value) || value instanceof ArrayBuffer || value instanceof Blob) {
|
|
49
|
+
return [value];
|
|
50
|
+
}
|
|
51
|
+
if (isAsyncIterable(value)) {
|
|
52
|
+
const iterator = value[Symbol.asyncIterator]();
|
|
53
|
+
const parts = [];
|
|
54
|
+
try {
|
|
55
|
+
while (true) {
|
|
56
|
+
const next = await untilAborted(iterator.next(), signal, () => { });
|
|
57
|
+
if (next.done === true)
|
|
58
|
+
return parts;
|
|
59
|
+
parts.push(...(await getBytes(next.value, signal)));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
void Promise.resolve()
|
|
64
|
+
.then(() => iterator.return?.())
|
|
65
|
+
.catch(() => { });
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const constructor = value?.constructor?.name;
|
|
70
|
+
throw new TypeError(`A file's content must be a string, bytes, a Blob or an async iterable of them, not ${typeof value}${constructor ? ` (${constructor})` : ''}.`);
|
|
71
|
+
}
|
|
72
|
+
/** A `Response`'s body as a `Blob`, typed by its `Content-Type`. An abort of `signal` throws its reason at once. */
|
|
73
|
+
async function blobOf(response, signal) {
|
|
74
|
+
return await untilAborted(response.blob(), signal, () => { });
|
|
75
|
+
}
|
|
76
|
+
/** `toFile`, its reading stopped by an abort of `signal`. */
|
|
77
|
+
async function toFileUntil(value, name, options, signal) {
|
|
78
|
+
const input = await value;
|
|
79
|
+
if (input instanceof File) {
|
|
80
|
+
if ((name == null || name === input.name) && (options?.type == null || options.type === input.type))
|
|
81
|
+
return input;
|
|
82
|
+
return makeFile([input], name ?? input.name, { ...options, type: options?.type ?? input.type, lastModified: input.lastModified });
|
|
83
|
+
}
|
|
84
|
+
if (input instanceof Response) {
|
|
85
|
+
const blob = await blobOf(input, signal);
|
|
86
|
+
return makeFile([blob], name ?? getName(input), options?.type === undefined && blob.type ? { ...options, type: blob.type } : options);
|
|
87
|
+
}
|
|
88
|
+
const parts = await getBytes(input, signal);
|
|
89
|
+
const typed = parts.find((p) => p instanceof Blob && p.type !== '');
|
|
90
|
+
return makeFile(parts, name ?? getName(input), options?.type === undefined && typed ? { ...options, type: typed.type } : options);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Content as a `File`, buffered: a `File` comes back as it is unless renamed or retyped, a `Response` or `Blob` keeps
|
|
94
|
+
* its type, and bytes or an async iterable of them become a file named `name`, else after the input's own name.
|
|
95
|
+
*/
|
|
96
|
+
export async function toFile(value, name, options) {
|
|
97
|
+
return await toFileUntil(value, name, options, undefined);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* One value into the form: a string, number or boolean as text, a file-like value as a file part, an array as
|
|
101
|
+
* repeated `key[]` fields, an object as `key[name]` fields; `undefined` is left out, `null` refused.
|
|
102
|
+
*/
|
|
103
|
+
async function addFormValue(form, key, value, signal) {
|
|
104
|
+
if (value === undefined)
|
|
105
|
+
return;
|
|
106
|
+
if (value === null) {
|
|
107
|
+
throw new TypeError(`"${key}" is null, and a form field can't be null: leave it out, or send the string 'null'.`);
|
|
108
|
+
}
|
|
109
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
110
|
+
form.append(key, String(value));
|
|
111
|
+
}
|
|
112
|
+
else if (value instanceof Response) {
|
|
113
|
+
const blob = await blobOf(value, signal);
|
|
114
|
+
form.append(key, makeFile([blob], getName(value), { type: blob.type }));
|
|
115
|
+
}
|
|
116
|
+
else if (value instanceof Blob) {
|
|
117
|
+
const filename = getName(value);
|
|
118
|
+
if (filename === undefined)
|
|
119
|
+
form.append(key, value);
|
|
120
|
+
else
|
|
121
|
+
form.append(key, value, filename);
|
|
122
|
+
}
|
|
123
|
+
else if (isAsyncIterable(value)) {
|
|
124
|
+
form.append(key, makeFile(await getBytes(value, signal), getName(value)));
|
|
125
|
+
}
|
|
126
|
+
else if (Array.isArray(value)) {
|
|
127
|
+
for (const entry of value)
|
|
128
|
+
await addFormValue(form, `${key}[]`, entry, signal);
|
|
129
|
+
}
|
|
130
|
+
else if (typeof value === 'object') {
|
|
131
|
+
for (const [name, prop] of Object.entries(value))
|
|
132
|
+
await addFormValue(form, `${key}[${name}]`, prop, signal);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
throw new TypeError(`"${key}" is ${typeof value}: a form field takes a string, number, boolean, object, array, File or Blob.`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/** A request body as `FormData`, its fields in the body's order. An abort of `signal` stops reading its files. */
|
|
139
|
+
export async function createForm(body, signal) {
|
|
140
|
+
const form = new FormData();
|
|
141
|
+
for (const [key, value] of Object.entries(body))
|
|
142
|
+
await addFormValue(form, key, value, signal);
|
|
143
|
+
return form;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* A file as a base64 data URL, read whole and typed as its multipart part would be: the file's own type, else
|
|
147
|
+
* `application/octet-stream`. The API reads an image's real type from its bytes. An abort of `signal` stops the read.
|
|
148
|
+
*/
|
|
149
|
+
export async function toDataUrl(value, signal) {
|
|
150
|
+
const file = await toFileUntil(value, undefined, undefined, signal);
|
|
151
|
+
const base64 = Buffer.from(await untilAborted(file.arrayBuffer(), signal, () => { })).toString('base64');
|
|
152
|
+
return `data:${file.type || 'application/octet-stream'};base64,${base64}`;
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=uploads.js.map
|