agentbox-sdk 0.0.0 → 0.1.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.
@@ -0,0 +1,18 @@
1
+ // src/shared/shell.ts
2
+ function shellQuote(value) {
3
+ if (value.length === 0) {
4
+ return "''";
5
+ }
6
+ return `'${value.replace(/'/g, `'"'"'`)}'`;
7
+ }
8
+ function toShellCommand(command) {
9
+ if (typeof command === "string") {
10
+ return command;
11
+ }
12
+ return command.map(shellQuote).join(" ");
13
+ }
14
+
15
+ export {
16
+ shellQuote,
17
+ toShellCommand
18
+ };
@@ -0,0 +1,231 @@
1
+ import {
2
+ AgentProvider
3
+ } from "./chunk-2NKMDGYH.js";
4
+
5
+ // src/agents/ports.ts
6
+ var AGENT_RESERVED_PORTS = {
7
+ [AgentProvider.ClaudeCode]: [43180],
8
+ [AgentProvider.Codex]: [43181],
9
+ [AgentProvider.OpenCode]: [4096]
10
+ };
11
+ function collectAllAgentReservedPorts() {
12
+ const seen = /* @__PURE__ */ new Set();
13
+ for (const ports of Object.values(AGENT_RESERVED_PORTS)) {
14
+ for (const port of ports) {
15
+ seen.add(port);
16
+ }
17
+ }
18
+ return Array.from(seen);
19
+ }
20
+
21
+ // src/shared/errors.ts
22
+ var AgentBoxError = class extends Error {
23
+ code;
24
+ details;
25
+ constructor(message, options) {
26
+ super(message, { cause: options?.cause });
27
+ this.name = "AgentBoxError";
28
+ this.code = options?.code;
29
+ this.details = options?.details;
30
+ }
31
+ };
32
+ var UnsupportedProviderError = class extends AgentBoxError {
33
+ constructor(kind, provider) {
34
+ super(`Unsupported ${kind} provider "${provider}".`, {
35
+ code: "UNSUPPORTED_PROVIDER",
36
+ details: { kind, provider }
37
+ });
38
+ this.name = "UnsupportedProviderError";
39
+ }
40
+ };
41
+ function asError(error) {
42
+ if (error instanceof Error) {
43
+ return error;
44
+ }
45
+ return new Error(typeof error === "string" ? error : JSON.stringify(error));
46
+ }
47
+ function suppressUnhandledRejection(promise) {
48
+ promise.catch(() => void 0);
49
+ return promise;
50
+ }
51
+
52
+ // src/shared/async-queue.ts
53
+ var AsyncQueue = class {
54
+ items = [];
55
+ resolvers = [];
56
+ closed = false;
57
+ failure = null;
58
+ push(item) {
59
+ if (this.closed || this.failure) {
60
+ return;
61
+ }
62
+ const resolver = this.resolvers.shift();
63
+ if (resolver) {
64
+ resolver.resolve({ done: false, value: item });
65
+ return;
66
+ }
67
+ this.items.push(item);
68
+ }
69
+ finish() {
70
+ if (this.closed || this.failure) {
71
+ return;
72
+ }
73
+ this.closed = true;
74
+ while (this.resolvers.length > 0) {
75
+ this.resolvers.shift()?.resolve({ done: true, value: void 0 });
76
+ }
77
+ }
78
+ fail(error) {
79
+ if (this.failure) {
80
+ return;
81
+ }
82
+ this.failure = asError(error);
83
+ while (this.resolvers.length > 0) {
84
+ this.resolvers.shift()?.reject(this.failure);
85
+ }
86
+ }
87
+ async next() {
88
+ if (this.failure) {
89
+ throw this.failure;
90
+ }
91
+ if (this.items.length > 0) {
92
+ const value = this.items.shift();
93
+ return { done: false, value };
94
+ }
95
+ if (this.closed) {
96
+ return { done: true, value: void 0 };
97
+ }
98
+ return new Promise((resolve, reject) => {
99
+ this.resolvers.push({ resolve, reject });
100
+ });
101
+ }
102
+ [Symbol.asyncIterator]() {
103
+ return this;
104
+ }
105
+ };
106
+
107
+ // src/shared/network.ts
108
+ import net from "net";
109
+ import { setTimeout as sleepTimeout } from "timers/promises";
110
+ async function sleep(ms) {
111
+ await sleepTimeout(ms);
112
+ }
113
+ async function getAvailablePort(host = "127.0.0.1") {
114
+ return new Promise((resolve, reject) => {
115
+ const server = net.createServer();
116
+ server.once("error", reject);
117
+ server.listen(0, host, () => {
118
+ const address = server.address();
119
+ if (!address || typeof address === "string") {
120
+ reject(new Error("Could not determine an available port."));
121
+ return;
122
+ }
123
+ const { port } = address;
124
+ server.close((error) => {
125
+ if (error) {
126
+ reject(error);
127
+ return;
128
+ }
129
+ resolve(port);
130
+ });
131
+ });
132
+ });
133
+ }
134
+ async function waitFor(predicate, options) {
135
+ const timeoutMs = options?.timeoutMs ?? 15e3;
136
+ const intervalMs = options?.intervalMs ?? 250;
137
+ const startedAt = Date.now();
138
+ while (Date.now() - startedAt < timeoutMs) {
139
+ if (await predicate()) {
140
+ return;
141
+ }
142
+ await sleep(intervalMs);
143
+ }
144
+ throw new Error(`Timed out after ${timeoutMs}ms.`);
145
+ }
146
+
147
+ // src/shared/streams.ts
148
+ async function readStreamAsText(stream) {
149
+ const reader = stream.getReader();
150
+ const decoder = new TextDecoder();
151
+ let result = "";
152
+ try {
153
+ while (true) {
154
+ const { done, value } = await reader.read();
155
+ if (done) {
156
+ break;
157
+ }
158
+ result += typeof value === "string" ? value : decoder.decode(value, { stream: true });
159
+ }
160
+ } finally {
161
+ reader.releaseLock();
162
+ }
163
+ result += decoder.decode();
164
+ return result;
165
+ }
166
+ async function pipeReadableStream(stream, onChunk) {
167
+ const reader = stream.getReader();
168
+ const decoder = new TextDecoder();
169
+ try {
170
+ while (true) {
171
+ const { done, value } = await reader.read();
172
+ if (done) {
173
+ break;
174
+ }
175
+ onChunk(
176
+ typeof value === "string" ? value : decoder.decode(value, { stream: true })
177
+ );
178
+ }
179
+ } finally {
180
+ reader.releaseLock();
181
+ }
182
+ const finalChunk = decoder.decode();
183
+ if (finalChunk) {
184
+ onChunk(finalChunk);
185
+ }
186
+ }
187
+ async function readNodeStream(stream) {
188
+ const chunks = [];
189
+ for await (const chunk of stream) {
190
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
191
+ }
192
+ return Buffer.concat(chunks);
193
+ }
194
+ async function* linesFromTextChunks(chunks) {
195
+ let buffer = "";
196
+ for await (const chunk of chunks) {
197
+ buffer += chunk;
198
+ while (true) {
199
+ const newlineIndex = buffer.indexOf("\n");
200
+ if (newlineIndex === -1) {
201
+ break;
202
+ }
203
+ const line = buffer.slice(0, newlineIndex).trimEnd();
204
+ buffer = buffer.slice(newlineIndex + 1);
205
+ if (line) {
206
+ yield line;
207
+ }
208
+ }
209
+ }
210
+ const trailing = buffer.trim();
211
+ if (trailing) {
212
+ yield trailing;
213
+ }
214
+ }
215
+
216
+ export {
217
+ AgentBoxError,
218
+ UnsupportedProviderError,
219
+ asError,
220
+ suppressUnhandledRejection,
221
+ AsyncQueue,
222
+ sleep,
223
+ getAvailablePort,
224
+ waitFor,
225
+ readStreamAsText,
226
+ pipeReadableStream,
227
+ readNodeStream,
228
+ linesFromTextChunks,
229
+ AGENT_RESERVED_PORTS,
230
+ collectAllAgentReservedPorts
231
+ };