@liveport/agent-sdk 0.1.1 → 0.1.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/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,16 +17,26 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
21
31
  var index_exports = {};
22
32
  __export(index_exports, {
23
33
  ApiError: () => ApiError,
34
+ ConnectionError: () => ConnectionError,
24
35
  LivePortAgent: () => LivePortAgent,
25
36
  TunnelTimeoutError: () => TunnelTimeoutError
26
37
  });
27
38
  module.exports = __toCommonJS(index_exports);
39
+ var import_ws = __toESM(require("ws"));
28
40
  var TunnelTimeoutError = class extends Error {
29
41
  constructor(timeout) {
30
42
  super(`Tunnel not available within ${timeout}ms timeout`);
@@ -41,23 +53,150 @@ var ApiError = class extends Error {
41
53
  this.code = code;
42
54
  }
43
55
  };
56
+ var ConnectionError = class extends Error {
57
+ constructor(message) {
58
+ super(message);
59
+ this.name = "ConnectionError";
60
+ }
61
+ };
44
62
  var LivePortAgent = class {
45
63
  config;
46
64
  abortController = null;
47
- waitInProgress = false;
65
+ wsConnection = null;
48
66
  constructor(config) {
49
67
  if (!config.key) {
50
68
  throw new Error("Bridge key is required");
51
69
  }
52
- if (config.timeout !== void 0 && config.timeout <= 0) {
53
- throw new Error("Timeout must be greater than 0");
54
- }
55
70
  this.config = {
56
71
  key: config.key,
57
- apiUrl: config.apiUrl || "https://app.liveport.dev",
72
+ apiUrl: config.apiUrl || "https://liveport.dev",
73
+ tunnelUrl: config.tunnelUrl || "https://tunnel.liveport.online",
58
74
  timeout: config.timeout || 3e4
59
75
  };
60
76
  }
77
+ /**
78
+ * Connect to the tunnel server and create a tunnel for the given local port.
79
+ *
80
+ * Opens a WebSocket to the tunnel server, authenticates with the bridge key,
81
+ * and waits for a tunnel assignment. Incoming HTTP requests from the tunnel
82
+ * are forwarded to localhost:<port>.
83
+ *
84
+ * @param port - The local port to tunnel
85
+ * @param options - Connection options
86
+ * @returns The tunnel info once connected
87
+ * @throws ConnectionError if the connection fails or times out
88
+ */
89
+ async connect(port, options) {
90
+ if (this.wsConnection) {
91
+ throw new ConnectionError("Already connected \u2014 call disconnect() first");
92
+ }
93
+ const serverUrl = options?.serverUrl || this.config.tunnelUrl;
94
+ const timeout = options?.timeout ?? this.config.timeout;
95
+ const WS = options?._WebSocketClass ?? import_ws.default;
96
+ return new Promise((resolve, reject) => {
97
+ const wsUrl = this.buildWebSocketUrl(serverUrl);
98
+ const headers = {
99
+ "X-Bridge-Key": this.config.key,
100
+ "X-Local-Port": String(port)
101
+ };
102
+ const socket = new WS(wsUrl, {
103
+ headers,
104
+ perMessageDeflate: false
105
+ });
106
+ this.wsConnection = socket;
107
+ let settled = false;
108
+ const connectTimeout = setTimeout(() => {
109
+ if (!settled) {
110
+ settled = true;
111
+ socket.close();
112
+ reject(new ConnectionError("Connection timeout"));
113
+ }
114
+ }, timeout);
115
+ socket.on("open", () => {
116
+ });
117
+ socket.on("message", (data) => {
118
+ try {
119
+ const message = JSON.parse(data.toString());
120
+ this.handleWsMessage(message, port, socket);
121
+ if (message.type === "connected" && !settled) {
122
+ clearTimeout(connectTimeout);
123
+ settled = true;
124
+ const payload = message.payload;
125
+ const tunnel = {
126
+ tunnelId: payload.tunnelId,
127
+ subdomain: payload.subdomain,
128
+ url: payload.url,
129
+ localPort: port,
130
+ // Use server-provided createdAt if the server sends it; fall back to client time
131
+ // (ConnectedPayload doesn't include createdAt today, but may in a future server version)
132
+ createdAt: payload.createdAt ? new Date(payload.createdAt) : /* @__PURE__ */ new Date(),
133
+ expiresAt: new Date(payload.expiresAt)
134
+ };
135
+ resolve(tunnel);
136
+ } else if (message.type === "error" && message.payload?.fatal && !settled) {
137
+ clearTimeout(connectTimeout);
138
+ settled = true;
139
+ reject(new ConnectionError(message.payload.message));
140
+ }
141
+ } catch {
142
+ }
143
+ });
144
+ socket.on("close", () => {
145
+ clearTimeout(connectTimeout);
146
+ if (!settled) {
147
+ settled = true;
148
+ reject(new ConnectionError("Connection closed before tunnel was established"));
149
+ }
150
+ if (this.wsConnection === socket) {
151
+ this.wsConnection = null;
152
+ }
153
+ });
154
+ socket.on("error", (err) => {
155
+ clearTimeout(connectTimeout);
156
+ if (!settled) {
157
+ settled = true;
158
+ if (this.wsConnection === socket) {
159
+ this.wsConnection = null;
160
+ }
161
+ reject(new ConnectionError(err.message));
162
+ }
163
+ });
164
+ });
165
+ }
166
+ /**
167
+ * Wait for the tunnel's public URL to become reachable.
168
+ *
169
+ * Polls the tunnel's public URL (not localhost) with HTTP GET requests
170
+ * until a 2xx response is received, or the timeout is exceeded. This
171
+ * validates the full tunnel path end-to-end.
172
+ *
173
+ * @param tunnel - The tunnel to check
174
+ * @param options - Wait options
175
+ * @throws TunnelTimeoutError if the tunnel is not ready within timeout
176
+ */
177
+ async waitForReady(tunnel, options) {
178
+ const timeout = options?.timeout ?? this.config.timeout;
179
+ const pollInterval = options?.pollInterval ?? 1e3;
180
+ const healthPath = options?.healthPath ?? "/";
181
+ const url = tunnel.url + healthPath;
182
+ const startTime = Date.now();
183
+ while (Date.now() - startTime < timeout) {
184
+ try {
185
+ const response = await fetch(url, {
186
+ method: "GET",
187
+ signal: AbortSignal.timeout(Math.max(1, Math.min(5e3, timeout - (Date.now() - startTime))))
188
+ });
189
+ if (response.ok) {
190
+ return;
191
+ }
192
+ } catch {
193
+ }
194
+ const remaining = timeout - (Date.now() - startTime);
195
+ if (remaining <= 0) break;
196
+ await this.sleep(Math.min(pollInterval, remaining));
197
+ }
198
+ throw new TunnelTimeoutError(timeout);
199
+ }
61
200
  /**
62
201
  * Wait for a tunnel to become available
63
202
  *
@@ -69,54 +208,35 @@ var LivePortAgent = class {
69
208
  * @throws ApiError if the API request fails
70
209
  */
71
210
  async waitForTunnel(options) {
72
- if (this.waitInProgress) {
73
- throw new Error(
74
- "Wait already in progress. Call disconnect() first or wait for the current operation to complete."
75
- );
76
- }
77
211
  const timeout = options?.timeout ?? this.config.timeout;
78
212
  const pollInterval = options?.pollInterval ?? 1e3;
79
- if (timeout <= 0) {
80
- throw new Error("Timeout must be greater than 0");
81
- }
82
- if (pollInterval <= 0) {
83
- throw new Error("Poll interval must be greater than 0");
84
- }
85
- if (pollInterval > timeout) {
86
- throw new Error("Poll interval cannot exceed timeout");
87
- }
88
- this.waitInProgress = true;
89
213
  this.abortController = new AbortController();
90
214
  const startTime = Date.now();
91
- try {
92
- while (Date.now() - startTime < timeout) {
93
- try {
94
- const response = await this.makeRequest(
95
- `/api/agent/tunnels/wait?timeout=${Math.min(pollInterval * 5, timeout - (Date.now() - startTime))}`,
96
- { signal: this.abortController.signal }
97
- );
98
- if (response.ok) {
99
- const data = await response.json();
100
- if (data.tunnel) {
101
- return this.parseTunnel(data.tunnel);
102
- }
103
- } else if (response.status === 408) {
104
- } else {
105
- const error = await response.json().catch(() => ({ code: "UNKNOWN", message: "Request failed" }));
106
- throw new ApiError(response.status, error.code, error.message);
107
- }
108
- } catch (err) {
109
- if (err instanceof ApiError) throw err;
110
- if (err.name === "AbortError") {
111
- throw new Error("Wait cancelled");
215
+ while (Date.now() - startTime < timeout) {
216
+ try {
217
+ const response = await this.makeRequest(
218
+ `/api/agent/tunnels/wait?timeout=${Math.min(pollInterval * 5, timeout - (Date.now() - startTime))}`,
219
+ { signal: this.abortController.signal }
220
+ );
221
+ if (response.ok) {
222
+ const data = await response.json();
223
+ if (data.tunnel) {
224
+ return this.parseTunnel(data.tunnel);
112
225
  }
226
+ } else if (response.status === 408) {
227
+ } else {
228
+ const error = await response.json().catch(() => ({ code: "UNKNOWN", message: "Request failed" }));
229
+ throw new ApiError(response.status, error.code, error.message);
230
+ }
231
+ } catch (err) {
232
+ if (err instanceof ApiError) throw err;
233
+ if (err.name === "AbortError") {
234
+ throw new Error("Wait cancelled");
113
235
  }
114
- await this.sleep(pollInterval);
115
236
  }
116
- throw new TunnelTimeoutError(timeout);
117
- } finally {
118
- this.waitInProgress = false;
237
+ await this.sleep(pollInterval);
119
238
  }
239
+ throw new TunnelTimeoutError(timeout);
120
240
  }
121
241
  /**
122
242
  * List all active tunnels for this bridge key
@@ -136,13 +256,115 @@ var LivePortAgent = class {
136
256
  /**
137
257
  * Disconnect and clean up
138
258
  *
139
- * Cancels any pending waitForTunnel calls.
259
+ * Cancels any pending waitForTunnel calls and closes any WebSocket
260
+ * connection created by connect().
140
261
  */
141
262
  async disconnect() {
142
263
  if (this.abortController) {
143
264
  this.abortController.abort();
144
265
  this.abortController = null;
145
266
  }
267
+ if (this.wsConnection) {
268
+ const ws = this.wsConnection;
269
+ this.wsConnection = null;
270
+ if (ws.readyState === 1 || ws.readyState === 0) {
271
+ ws.close(1e3, "Agent disconnect");
272
+ }
273
+ }
274
+ }
275
+ /**
276
+ * Build WebSocket URL from server URL
277
+ */
278
+ buildWebSocketUrl(serverUrl) {
279
+ const url = new URL(serverUrl);
280
+ url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
281
+ url.pathname = "/connect";
282
+ return url.toString();
283
+ }
284
+ /**
285
+ * Handle incoming WebSocket messages from the tunnel server
286
+ */
287
+ handleWsMessage(message, localPort, socket) {
288
+ switch (message.type) {
289
+ case "http_request":
290
+ void this.handleHttpRequest(message, localPort, socket).catch(() => {
291
+ });
292
+ break;
293
+ case "heartbeat":
294
+ if (socket.readyState === 1) {
295
+ socket.send(JSON.stringify({
296
+ type: "heartbeat_ack",
297
+ timestamp: Date.now()
298
+ }));
299
+ }
300
+ break;
301
+ }
302
+ }
303
+ /**
304
+ * Forward an HTTP request from the tunnel server to localhost
305
+ */
306
+ async handleHttpRequest(message, localPort, socket) {
307
+ const payload = message.payload;
308
+ try {
309
+ const safePath = payload.path?.startsWith("/") ? payload.path : `/${payload.path || ""}`;
310
+ const url = `http://localhost:${localPort}${safePath}`;
311
+ const HOP_BY_HOP = /* @__PURE__ */ new Set([
312
+ "host",
313
+ "connection",
314
+ "transfer-encoding",
315
+ "te",
316
+ "trailer",
317
+ "upgrade",
318
+ "keep-alive",
319
+ "proxy-authenticate",
320
+ "proxy-authorization"
321
+ ]);
322
+ const safeHeaders = Object.fromEntries(
323
+ Object.entries(payload.headers || {}).filter(([k]) => !HOP_BY_HOP.has(k.toLowerCase()))
324
+ );
325
+ const requestInit = {
326
+ method: payload.method,
327
+ headers: safeHeaders
328
+ };
329
+ if (payload.body) {
330
+ requestInit.body = Buffer.from(payload.body, "base64");
331
+ }
332
+ const response = await fetch(url, requestInit);
333
+ const responseBody = await response.arrayBuffer();
334
+ const responseHeaders = {};
335
+ response.headers.forEach((value, key) => {
336
+ responseHeaders[key] = value;
337
+ });
338
+ const responseMessage = {
339
+ type: "http_response",
340
+ id: message.id,
341
+ timestamp: Date.now(),
342
+ payload: {
343
+ status: response.status,
344
+ headers: responseHeaders,
345
+ body: responseBody.byteLength > 0 ? Buffer.from(responseBody).toString("base64") : void 0
346
+ }
347
+ };
348
+ if (socket.readyState === 1) {
349
+ socket.send(JSON.stringify(responseMessage));
350
+ }
351
+ } catch (err) {
352
+ const errorResponse = {
353
+ type: "http_response",
354
+ id: message.id,
355
+ timestamp: Date.now(),
356
+ payload: {
357
+ status: 502,
358
+ headers: { "Content-Type": "text/plain" },
359
+ body: Buffer.from(
360
+ `Error connecting to local server: ${err.message}`
361
+ ).toString("base64")
362
+ }
363
+ };
364
+ if (socket.readyState === 1) {
365
+ socket.send(JSON.stringify(errorResponse));
366
+ }
367
+ }
146
368
  }
147
369
  /**
148
370
  * Make an authenticated API request
@@ -162,45 +384,25 @@ var LivePortAgent = class {
162
384
  * Parse tunnel response into AgentTunnel
163
385
  */
164
386
  parseTunnel(data) {
165
- const tunnelId = data.tunnelId || data.id;
387
+ const tunnelId = data.tunnelId ?? data.id;
166
388
  const subdomain = data.subdomain;
167
389
  const url = data.url;
168
390
  const localPort = data.localPort;
169
- const createdAt = data.createdAt;
170
- const expiresAt = data.expiresAt;
171
- if (!tunnelId || typeof tunnelId !== "string") {
172
- throw new ApiError(500, "INVALID_RESPONSE", "Missing or invalid tunnelId in response");
173
- }
174
- if (!subdomain || typeof subdomain !== "string") {
175
- throw new ApiError(500, "INVALID_RESPONSE", "Missing or invalid subdomain in response");
176
- }
177
- if (!url || typeof url !== "string") {
178
- throw new ApiError(500, "INVALID_RESPONSE", "Missing or invalid url in response");
179
- }
180
- if (typeof localPort !== "number" || localPort <= 0 || localPort > 65535) {
181
- throw new ApiError(500, "INVALID_RESPONSE", "Missing or invalid localPort in response");
182
- }
183
- if (!createdAt || typeof createdAt !== "string") {
184
- throw new ApiError(500, "INVALID_RESPONSE", "Missing or invalid createdAt in response");
185
- }
186
- if (!expiresAt || typeof expiresAt !== "string") {
187
- throw new ApiError(500, "INVALID_RESPONSE", "Missing or invalid expiresAt in response");
188
- }
189
- const createdAtDate = new Date(createdAt);
190
- const expiresAtDate = new Date(expiresAt);
191
- if (isNaN(createdAtDate.getTime())) {
192
- throw new ApiError(500, "INVALID_RESPONSE", "Invalid createdAt date format");
391
+ if (!tunnelId || !subdomain || !url || localPort == null || typeof localPort !== "number") {
392
+ throw new Error(
393
+ `parseTunnel: missing required fields in API response (tunnelId=${tunnelId}, subdomain=${subdomain}, url=${url}, localPort=${localPort})`
394
+ );
193
395
  }
194
- if (isNaN(expiresAtDate.getTime())) {
195
- throw new ApiError(500, "INVALID_RESPONSE", "Invalid expiresAt date format");
396
+ if (!data.expiresAt) {
397
+ throw new Error("parseTunnel: missing expiresAt in API response");
196
398
  }
197
399
  return {
198
400
  tunnelId,
199
401
  subdomain,
200
402
  url,
201
403
  localPort,
202
- createdAt: createdAtDate,
203
- expiresAt: expiresAtDate
404
+ createdAt: data.createdAt ? new Date(data.createdAt) : /* @__PURE__ */ new Date(),
405
+ expiresAt: new Date(data.expiresAt)
204
406
  };
205
407
  }
206
408
  /**
@@ -213,6 +415,7 @@ var LivePortAgent = class {
213
415
  // Annotate the CommonJS export names for ESM import in node:
214
416
  0 && (module.exports = {
215
417
  ApiError,
418
+ ConnectionError,
216
419
  LivePortAgent,
217
420
  TunnelTimeoutError
218
421
  });
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * LivePort Agent SDK\n *\n * TypeScript SDK for AI agents to wait for and access localhost tunnels.\n */\n\nimport type { Tunnel } from \"@liveport/shared\";\n\nexport interface LivePortAgentConfig {\n /** Bridge key for authentication */\n key: string;\n /** API base URL (default: https://app.liveport.dev) */\n apiUrl?: string;\n /** Default timeout in milliseconds (default: 30000) */\n timeout?: number;\n}\n\nexport interface WaitForTunnelOptions {\n /** Timeout in milliseconds (default: 30000) */\n timeout?: number;\n /** Poll interval in milliseconds (default: 1000) */\n pollInterval?: number;\n}\n\nexport interface AgentTunnel {\n tunnelId: string;\n subdomain: string;\n url: string;\n localPort: number;\n createdAt: Date;\n expiresAt: Date;\n}\n\n/** Error thrown when tunnel wait times out */\nexport class TunnelTimeoutError extends Error {\n constructor(timeout: number) {\n super(`Tunnel not available within ${timeout}ms timeout`);\n this.name = \"TunnelTimeoutError\";\n }\n}\n\n/** Error thrown when API request fails */\nexport class ApiError extends Error {\n public readonly statusCode: number;\n public readonly code: string;\n\n constructor(statusCode: number, code: string, message: string) {\n super(message);\n this.name = \"ApiError\";\n this.statusCode = statusCode;\n this.code = code;\n }\n}\n\n/**\n * LivePort Agent SDK\n *\n * Allows AI agents to wait for and access localhost tunnels.\n *\n * @example\n * ```typescript\n * import { LivePortAgent } from '@liveport/agent-sdk';\n *\n * const agent = new LivePortAgent({\n * key: process.env.LIVEPORT_BRIDGE_KEY!\n * });\n *\n * // Wait for a tunnel to become available\n * const tunnel = await agent.waitForTunnel({ timeout: 30000 });\n * console.log(`Testing at: ${tunnel.url}`);\n *\n * // Run your tests against tunnel.url\n *\n * // Clean up when done\n * await agent.disconnect();\n * ```\n */\nexport class LivePortAgent {\n private config: Required<LivePortAgentConfig>;\n private abortController: AbortController | null = null;\n private waitInProgress = false;\n\n constructor(config: LivePortAgentConfig) {\n if (!config.key) {\n throw new Error(\"Bridge key is required\");\n }\n\n // Validate timeout before applying default\n if (config.timeout !== undefined && config.timeout <= 0) {\n throw new Error(\"Timeout must be greater than 0\");\n }\n\n this.config = {\n key: config.key,\n apiUrl: config.apiUrl || \"https://app.liveport.dev\",\n timeout: config.timeout || 30000,\n };\n }\n\n /**\n * Wait for a tunnel to become available\n *\n * Long-polls the API until a tunnel is ready or timeout is reached.\n *\n * @param options - Wait options\n * @returns The tunnel info once available\n * @throws TunnelTimeoutError if no tunnel becomes available within timeout\n * @throws ApiError if the API request fails\n */\n async waitForTunnel(options?: WaitForTunnelOptions): Promise<AgentTunnel> {\n // Prevent concurrent calls\n if (this.waitInProgress) {\n throw new Error(\n \"Wait already in progress. Call disconnect() first or wait for the current operation to complete.\"\n );\n }\n\n const timeout = options?.timeout ?? this.config.timeout;\n const pollInterval = options?.pollInterval ?? 1000;\n\n // Validate options\n if (timeout <= 0) {\n throw new Error(\"Timeout must be greater than 0\");\n }\n if (pollInterval <= 0) {\n throw new Error(\"Poll interval must be greater than 0\");\n }\n if (pollInterval > timeout) {\n throw new Error(\"Poll interval cannot exceed timeout\");\n }\n\n this.waitInProgress = true;\n this.abortController = new AbortController();\n const startTime = Date.now();\n\n try {\n while (Date.now() - startTime < timeout) {\n try {\n const response = await this.makeRequest(\n `/api/agent/tunnels/wait?timeout=${Math.min(pollInterval * 5, timeout - (Date.now() - startTime))}`,\n { signal: this.abortController.signal }\n );\n\n if (response.ok) {\n const data = await response.json() as { tunnel?: Record<string, unknown> };\n if (data.tunnel) {\n return this.parseTunnel(data.tunnel);\n }\n } else if (response.status === 408) {\n // Timeout from server, continue polling\n } else {\n const error = await response.json().catch(() => ({ code: \"UNKNOWN\", message: \"Request failed\" })) as { code: string; message: string };\n throw new ApiError(response.status, error.code, error.message);\n }\n } catch (err) {\n if (err instanceof ApiError) throw err;\n if ((err as Error).name === \"AbortError\") {\n throw new Error(\"Wait cancelled\");\n }\n // Network error, wait and retry\n }\n\n // Wait before next poll\n await this.sleep(pollInterval);\n }\n\n throw new TunnelTimeoutError(timeout);\n } finally {\n this.waitInProgress = false;\n }\n }\n\n /**\n * List all active tunnels for this bridge key\n *\n * @returns Array of active tunnels\n * @throws ApiError if the API request fails\n */\n async listTunnels(): Promise<AgentTunnel[]> {\n const response = await this.makeRequest(\"/api/agent/tunnels\");\n\n if (!response.ok) {\n const error = await response.json().catch(() => ({ code: \"UNKNOWN\", message: \"Request failed\" })) as { code: string; message: string };\n throw new ApiError(response.status, error.code, error.message);\n }\n\n const data = await response.json() as { tunnels?: Record<string, unknown>[] };\n return (data.tunnels || []).map((t) => this.parseTunnel(t));\n }\n\n /**\n * Disconnect and clean up\n *\n * Cancels any pending waitForTunnel calls.\n */\n async disconnect(): Promise<void> {\n if (this.abortController) {\n this.abortController.abort();\n this.abortController = null;\n }\n }\n\n /**\n * Make an authenticated API request\n */\n private async makeRequest(\n path: string,\n options: RequestInit = {}\n ): Promise<Response> {\n const url = `${this.config.apiUrl}${path}`;\n\n return fetch(url, {\n ...options,\n headers: {\n \"Authorization\": `Bearer ${this.config.key}`,\n \"Content-Type\": \"application/json\",\n ...options.headers,\n },\n });\n }\n\n /**\n * Parse tunnel response into AgentTunnel\n */\n private parseTunnel(data: Record<string, unknown>): AgentTunnel {\n const tunnelId = (data.tunnelId || data.id) as string;\n const subdomain = data.subdomain as string;\n const url = data.url as string;\n const localPort = data.localPort as number;\n const createdAt = data.createdAt as string;\n const expiresAt = data.expiresAt as string;\n\n // Validate required fields\n if (!tunnelId || typeof tunnelId !== \"string\") {\n throw new ApiError(500, \"INVALID_RESPONSE\", \"Missing or invalid tunnelId in response\");\n }\n if (!subdomain || typeof subdomain !== \"string\") {\n throw new ApiError(500, \"INVALID_RESPONSE\", \"Missing or invalid subdomain in response\");\n }\n if (!url || typeof url !== \"string\") {\n throw new ApiError(500, \"INVALID_RESPONSE\", \"Missing or invalid url in response\");\n }\n if (typeof localPort !== \"number\" || localPort <= 0 || localPort > 65535) {\n throw new ApiError(500, \"INVALID_RESPONSE\", \"Missing or invalid localPort in response\");\n }\n if (!createdAt || typeof createdAt !== \"string\") {\n throw new ApiError(500, \"INVALID_RESPONSE\", \"Missing or invalid createdAt in response\");\n }\n if (!expiresAt || typeof expiresAt !== \"string\") {\n throw new ApiError(500, \"INVALID_RESPONSE\", \"Missing or invalid expiresAt in response\");\n }\n\n const createdAtDate = new Date(createdAt);\n const expiresAtDate = new Date(expiresAt);\n\n // Validate dates are valid\n if (isNaN(createdAtDate.getTime())) {\n throw new ApiError(500, \"INVALID_RESPONSE\", \"Invalid createdAt date format\");\n }\n if (isNaN(expiresAtDate.getTime())) {\n throw new ApiError(500, \"INVALID_RESPONSE\", \"Invalid expiresAt date format\");\n }\n\n return {\n tunnelId,\n subdomain,\n url,\n localPort,\n createdAt: createdAtDate,\n expiresAt: expiresAtDate,\n };\n }\n\n /**\n * Sleep helper\n */\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n}\n\n// Re-export types\nexport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AAS,KAAK,oBAAoB,0CAA0C;AAAA,IACxF;AACA,QAAI,CAAC,aAAa,OAAO,cAAc,UAAU;AAC/C,YAAM,IAAI,SAAS,KAAK,oBAAoB,0CAA0C;AAAA,IACxF;AACA,QAAI,CAAC,aAAa,OAAO,cAAc,UAAU;AAC/C,YAAM,IAAI,SAAS,KAAK,oBAAoB,0CAA0C;AAAA,IACxF;AAEA,UAAM,gBAAgB,IAAI,KAAK,SAAS;AACxC,UAAM,gBAAgB,IAAI,KAAK,SAAS;AAGxC,QAAI,MAAM,cAAc,QAAQ,CAAC,GAAG;AAClC,YAAM,IAAI,SAAS,KAAK,oBAAoB,+BAA+B;AAAA,IAC7E;AACA,QAAI,MAAM,cAAc,QAAQ,CAAC,GAAG;AAClC,YAAM,IAAI,SAAS,KAAK,oBAAoB,+BAA+B;AAAA,IAC7E;AAEA,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,WAAW;AAAA,MACX,WAAW;AAAA,IACb;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKQ,MAAM,IAA2B;AACvC,WAAO,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,EAAE,CAAC;AAAA,EACzD;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * LivePort Agent SDK\n *\n * TypeScript SDK for AI agents to wait for and access localhost tunnels.\n */\n\nimport WebSocket from \"ws\";\n\n/**\n * Tunnel record as stored in the database.\n *\n * TODO: Remove this duplicate once @liveport/shared is published to npm\n * and agent-sdk can depend on it directly. Until then, keep in sync with\n * the Tunnel type in packages/shared/src/types/index.ts.\n */\nexport interface Tunnel {\n id: string;\n userId: string;\n bridgeKeyId?: string;\n subdomain: string;\n name?: string;\n localPort: number;\n publicUrl: string;\n region: string;\n connectedAt: Date;\n disconnectedAt?: Date;\n requestCount: number;\n bytesTransferred: number;\n}\n\nexport interface LivePortAgentConfig {\n /** Bridge key for authentication */\n key: string;\n /** API base URL (default: https://liveport.dev) */\n apiUrl?: string;\n /** Tunnel server URL for connect() (default: https://tunnel.liveport.online) */\n tunnelUrl?: string;\n /** Default timeout in milliseconds (default: 30000) */\n timeout?: number;\n}\n\nexport interface WaitForTunnelOptions {\n /** Timeout in milliseconds (default: 30000) */\n timeout?: number;\n /** Poll interval in milliseconds (default: 1000) */\n pollInterval?: number;\n}\n\nexport interface ConnectOptions {\n /** Tunnel server URL (overrides tunnelUrl from config) */\n serverUrl?: string;\n /** Connection timeout in milliseconds (default: 30000) */\n timeout?: number;\n}\n\n/** @internal Extended connect options with injectable WebSocket class for testing */\ninterface InternalConnectOptions extends ConnectOptions {\n _WebSocketClass?: typeof WebSocket;\n}\n\nexport interface WaitForReadyOptions {\n /** Timeout in milliseconds (default: 30000) */\n timeout?: number;\n /** Poll interval in milliseconds (default: 1000) */\n pollInterval?: number;\n /** Health check path (default: \"/\") */\n healthPath?: string;\n}\n\nexport interface AgentTunnel {\n tunnelId: string;\n subdomain: string;\n url: string;\n localPort: number;\n createdAt: Date;\n expiresAt: Date;\n}\n\n/** Error thrown when tunnel wait times out */\nexport class TunnelTimeoutError extends Error {\n constructor(timeout: number) {\n super(`Tunnel not available within ${timeout}ms timeout`);\n this.name = \"TunnelTimeoutError\";\n }\n}\n\n/** Error thrown when API request fails */\nexport class ApiError extends Error {\n public readonly statusCode: number;\n public readonly code: string;\n\n constructor(statusCode: number, code: string, message: string) {\n super(message);\n this.name = \"ApiError\";\n this.statusCode = statusCode;\n this.code = code;\n }\n}\n\n/** Error thrown when WebSocket connection fails */\nexport class ConnectionError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"ConnectionError\";\n }\n}\n\n/**\n * LivePort Agent SDK\n *\n * Allows AI agents to wait for, connect to, and access localhost tunnels.\n *\n * @example\n * ```typescript\n * import { LivePortAgent } from '@liveport/agent-sdk';\n *\n * const agent = new LivePortAgent({\n * key: process.env.LIVEPORT_BRIDGE_KEY!\n * });\n *\n * // Create a tunnel to local port 3000\n * const tunnel = await agent.connect(3000);\n * console.log(`Tunnel URL: ${tunnel.url}`);\n *\n * // Wait for the local server to be reachable through the tunnel\n * await agent.waitForReady(tunnel);\n *\n * // Run your tests against tunnel.url\n *\n * // Clean up when done\n * await agent.disconnect();\n * ```\n */\nexport class LivePortAgent {\n private config: Required<LivePortAgentConfig>;\n private abortController: AbortController | null = null;\n private wsConnection: WebSocket | null = null;\n\n constructor(config: LivePortAgentConfig) {\n if (!config.key) {\n throw new Error(\"Bridge key is required\");\n }\n\n this.config = {\n key: config.key,\n apiUrl: config.apiUrl || \"https://liveport.dev\",\n tunnelUrl: config.tunnelUrl || \"https://tunnel.liveport.online\",\n timeout: config.timeout || 30000,\n };\n }\n\n /**\n * Connect to the tunnel server and create a tunnel for the given local port.\n *\n * Opens a WebSocket to the tunnel server, authenticates with the bridge key,\n * and waits for a tunnel assignment. Incoming HTTP requests from the tunnel\n * are forwarded to localhost:<port>.\n *\n * @param port - The local port to tunnel\n * @param options - Connection options\n * @returns The tunnel info once connected\n * @throws ConnectionError if the connection fails or times out\n */\n async connect(port: number, options?: ConnectOptions): Promise<AgentTunnel> {\n if (this.wsConnection) {\n throw new ConnectionError(\"Already connected — call disconnect() first\");\n }\n\n const serverUrl = options?.serverUrl || this.config.tunnelUrl;\n const timeout = options?.timeout ?? this.config.timeout;\n const WS = (options as InternalConnectOptions)?._WebSocketClass ?? WebSocket;\n\n return new Promise<AgentTunnel>((resolve, reject) => {\n const wsUrl = this.buildWebSocketUrl(serverUrl);\n\n const headers: Record<string, string> = {\n \"X-Bridge-Key\": this.config.key,\n \"X-Local-Port\": String(port),\n };\n\n const socket = new WS(wsUrl, {\n headers,\n perMessageDeflate: false,\n });\n\n this.wsConnection = socket;\n\n let settled = false;\n\n const connectTimeout = setTimeout(() => {\n if (!settled) {\n settled = true;\n socket.close();\n reject(new ConnectionError(\"Connection timeout\"));\n }\n }, timeout);\n\n socket.on(\"open\", () => {\n // Waiting for \"connected\" message from server\n });\n\n socket.on(\"message\", (data: Buffer | string) => {\n try {\n const message = JSON.parse(data.toString());\n this.handleWsMessage(message, port, socket);\n\n if (message.type === \"connected\" && !settled) {\n clearTimeout(connectTimeout);\n settled = true;\n const payload = message.payload;\n const tunnel: AgentTunnel = {\n tunnelId: payload.tunnelId,\n subdomain: payload.subdomain,\n url: payload.url,\n localPort: port,\n // Use server-provided createdAt if the server sends it; fall back to client time\n // (ConnectedPayload doesn't include createdAt today, but may in a future server version)\n createdAt: payload.createdAt ? new Date(payload.createdAt as string | number) : new Date(),\n expiresAt: new Date(payload.expiresAt),\n };\n resolve(tunnel);\n } else if (message.type === \"error\" && message.payload?.fatal && !settled) {\n clearTimeout(connectTimeout);\n settled = true;\n reject(new ConnectionError(message.payload.message));\n }\n } catch {\n // Non-JSON messages (e.g. binary frames, malformed data) are\n // silently dropped. The connect timeout will fire if the\n // \"connected\" message never arrives.\n }\n });\n\n socket.on(\"close\", () => {\n clearTimeout(connectTimeout);\n if (!settled) {\n settled = true;\n reject(new ConnectionError(\"Connection closed before tunnel was established\"));\n }\n // Only null wsConnection if it's still this socket (avoid clobbering\n // a new connection created after disconnect() + connect())\n if (this.wsConnection === socket) {\n this.wsConnection = null;\n }\n });\n\n socket.on(\"error\", (err: Error) => {\n clearTimeout(connectTimeout);\n if (!settled) {\n settled = true;\n // Clear wsConnection on error so a new connect() call can proceed\n if (this.wsConnection === socket) {\n this.wsConnection = null;\n }\n reject(new ConnectionError(err.message));\n }\n });\n });\n }\n\n /**\n * Wait for the tunnel's public URL to become reachable.\n *\n * Polls the tunnel's public URL (not localhost) with HTTP GET requests\n * until a 2xx response is received, or the timeout is exceeded. This\n * validates the full tunnel path end-to-end.\n *\n * @param tunnel - The tunnel to check\n * @param options - Wait options\n * @throws TunnelTimeoutError if the tunnel is not ready within timeout\n */\n async waitForReady(tunnel: AgentTunnel, options?: WaitForReadyOptions): Promise<void> {\n const timeout = options?.timeout ?? this.config.timeout;\n const pollInterval = options?.pollInterval ?? 1000;\n const healthPath = options?.healthPath ?? \"/\";\n\n const url = tunnel.url + healthPath;\n const startTime = Date.now();\n\n while (Date.now() - startTime < timeout) {\n try {\n const response = await fetch(url, {\n method: \"GET\",\n signal: AbortSignal.timeout(Math.max(1, Math.min(5000, timeout - (Date.now() - startTime)))),\n });\n if (response.ok) {\n return;\n }\n } catch {\n // Network error or timeout, continue polling\n }\n\n const remaining = timeout - (Date.now() - startTime);\n if (remaining <= 0) break;\n await this.sleep(Math.min(pollInterval, remaining));\n }\n\n throw new TunnelTimeoutError(timeout);\n }\n\n /**\n * Wait for a tunnel to become available\n *\n * Long-polls the API until a tunnel is ready or timeout is reached.\n *\n * @param options - Wait options\n * @returns The tunnel info once available\n * @throws TunnelTimeoutError if no tunnel becomes available within timeout\n * @throws ApiError if the API request fails\n */\n async waitForTunnel(options?: WaitForTunnelOptions): Promise<AgentTunnel> {\n const timeout = options?.timeout ?? this.config.timeout;\n const pollInterval = options?.pollInterval ?? 1000;\n\n this.abortController = new AbortController();\n const startTime = Date.now();\n\n while (Date.now() - startTime < timeout) {\n try {\n const response = await this.makeRequest(\n `/api/agent/tunnels/wait?timeout=${Math.min(pollInterval * 5, timeout - (Date.now() - startTime))}`,\n { signal: this.abortController.signal }\n );\n\n if (response.ok) {\n const data = await response.json() as { tunnel?: Record<string, unknown> };\n if (data.tunnel) {\n return this.parseTunnel(data.tunnel);\n }\n } else if (response.status === 408) {\n // Timeout from server, continue polling\n } else {\n const error = await response.json().catch(() => ({ code: \"UNKNOWN\", message: \"Request failed\" })) as { code: string; message: string };\n throw new ApiError(response.status, error.code, error.message);\n }\n } catch (err) {\n if (err instanceof ApiError) throw err;\n if ((err as Error).name === \"AbortError\") {\n throw new Error(\"Wait cancelled\");\n }\n // Network error, wait and retry\n }\n\n // Wait before next poll\n await this.sleep(pollInterval);\n }\n\n throw new TunnelTimeoutError(timeout);\n }\n\n /**\n * List all active tunnels for this bridge key\n *\n * @returns Array of active tunnels\n * @throws ApiError if the API request fails\n */\n async listTunnels(): Promise<AgentTunnel[]> {\n const response = await this.makeRequest(\"/api/agent/tunnels\");\n\n if (!response.ok) {\n const error = await response.json().catch(() => ({ code: \"UNKNOWN\", message: \"Request failed\" })) as { code: string; message: string };\n throw new ApiError(response.status, error.code, error.message);\n }\n\n const data = await response.json() as { tunnels?: Record<string, unknown>[] };\n return (data.tunnels || []).map((t) => this.parseTunnel(t));\n }\n\n /**\n * Disconnect and clean up\n *\n * Cancels any pending waitForTunnel calls and closes any WebSocket\n * connection created by connect().\n */\n async disconnect(): Promise<void> {\n if (this.abortController) {\n this.abortController.abort();\n this.abortController = null;\n }\n\n if (this.wsConnection) {\n const ws = this.wsConnection;\n this.wsConnection = null; // Clear reference first so connect() can be called after\n\n // Use numeric constants (OPEN=1, CONNECTING=0) to avoid coupling to\n // the imported WebSocket class, which may differ from the injected one\n if (ws.readyState === 1 || ws.readyState === 0) {\n ws.close(1000, \"Agent disconnect\");\n }\n }\n }\n\n /**\n * Build WebSocket URL from server URL\n */\n private buildWebSocketUrl(serverUrl: string): string {\n const url = new URL(serverUrl);\n url.protocol = url.protocol === \"https:\" ? \"wss:\" : \"ws:\";\n url.pathname = \"/connect\";\n return url.toString();\n }\n\n /**\n * Handle incoming WebSocket messages from the tunnel server\n */\n private handleWsMessage(\n message: { type: string; id?: string; payload?: Record<string, unknown> },\n localPort: number,\n socket: WebSocket\n ): void {\n switch (message.type) {\n case \"http_request\":\n void this.handleHttpRequest(message, localPort, socket).catch(() => {\n // Errors are handled inside handleHttpRequest (sends 502 response)\n });\n break;\n\n case \"heartbeat\":\n // Respond with heartbeat_ack\n if (socket.readyState === 1 /* OPEN */) {\n socket.send(JSON.stringify({\n type: \"heartbeat_ack\",\n timestamp: Date.now(),\n }));\n }\n break;\n\n // connected and error are handled in the connect() promise\n // Other message types can be extended in the future\n }\n }\n\n /**\n * Forward an HTTP request from the tunnel server to localhost\n */\n private async handleHttpRequest(\n message: { type: string; id?: string; payload?: Record<string, unknown> },\n localPort: number,\n socket: WebSocket\n ): Promise<void> {\n const payload = message.payload as {\n method: string;\n path: string;\n headers: Record<string, string>;\n body?: string;\n };\n\n try {\n // Validate path to prevent injection (e.g. path traversal or host override)\n const safePath = payload.path?.startsWith(\"/\") ? payload.path : `/${payload.path || \"\"}`;\n const url = `http://localhost:${localPort}${safePath}`;\n\n // Strip hop-by-hop headers that could cause request smuggling or\n // confuse the local server when forwarded from the tunnel\n const HOP_BY_HOP = new Set([\n \"host\", \"connection\", \"transfer-encoding\", \"te\", \"trailer\",\n \"upgrade\", \"keep-alive\", \"proxy-authenticate\", \"proxy-authorization\",\n ]);\n const safeHeaders = Object.fromEntries(\n Object.entries(payload.headers || {}).filter(([k]) => !HOP_BY_HOP.has(k.toLowerCase()))\n );\n\n const requestInit: RequestInit = {\n method: payload.method,\n headers: safeHeaders,\n };\n\n if (payload.body) {\n requestInit.body = Buffer.from(payload.body, \"base64\");\n }\n\n const response = await fetch(url, requestInit);\n const responseBody = await response.arrayBuffer();\n\n const responseHeaders: Record<string, string> = {};\n response.headers.forEach((value, key) => {\n responseHeaders[key] = value;\n });\n\n const responseMessage = {\n type: \"http_response\",\n id: message.id,\n timestamp: Date.now(),\n payload: {\n status: response.status,\n headers: responseHeaders,\n body: responseBody.byteLength > 0\n ? Buffer.from(responseBody).toString(\"base64\")\n : undefined,\n },\n };\n\n if (socket.readyState === 1 /* OPEN */) {\n socket.send(JSON.stringify(responseMessage));\n }\n } catch (err) {\n const errorResponse = {\n type: \"http_response\",\n id: message.id,\n timestamp: Date.now(),\n payload: {\n status: 502,\n headers: { \"Content-Type\": \"text/plain\" },\n body: Buffer.from(\n `Error connecting to local server: ${(err as Error).message}`\n ).toString(\"base64\"),\n },\n };\n\n if (socket.readyState === 1 /* OPEN */) {\n socket.send(JSON.stringify(errorResponse));\n }\n }\n }\n\n /**\n * Make an authenticated API request\n */\n private async makeRequest(\n path: string,\n options: RequestInit = {}\n ): Promise<Response> {\n const url = `${this.config.apiUrl}${path}`;\n\n return fetch(url, {\n ...options,\n headers: {\n \"Authorization\": `Bearer ${this.config.key}`,\n \"Content-Type\": \"application/json\",\n ...options.headers,\n },\n });\n }\n\n /**\n * Parse tunnel response into AgentTunnel\n */\n private parseTunnel(data: Record<string, unknown>): AgentTunnel {\n // Accept both tunnelId (new API shape) and id (legacy shape) for backward compatibility\n const tunnelId = (data.tunnelId ?? data.id) as string;\n const subdomain = data.subdomain as string;\n const url = data.url as string;\n const localPort = data.localPort as number;\n\n if (!tunnelId || !subdomain || !url || localPort == null || typeof localPort !== \"number\") {\n throw new Error(\n `parseTunnel: missing required fields in API response (tunnelId=${tunnelId}, subdomain=${subdomain}, url=${url}, localPort=${localPort})`\n );\n }\n\n if (!data.expiresAt) {\n throw new Error(\"parseTunnel: missing expiresAt in API response\");\n }\n\n return {\n tunnelId,\n subdomain,\n url,\n localPort,\n createdAt: data.createdAt ? new Date(data.createdAt as string | number) : new Date(),\n expiresAt: new Date(data.expiresAt as string | number),\n };\n }\n\n /**\n * Sleep helper\n */\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAMA,gBAAsB;AAyEf,IAAM,qBAAN,cAAiC,MAAM;AAAA,EAC5C,YAAY,SAAiB;AAC3B,UAAM,+BAA+B,OAAO,YAAY;AACxD,SAAK,OAAO;AAAA,EACd;AACF;AAGO,IAAM,WAAN,cAAuB,MAAM;AAAA,EAClB;AAAA,EACA;AAAA,EAEhB,YAAY,YAAoB,MAAc,SAAiB;AAC7D,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,OAAO;AAAA,EACd;AACF;AAGO,IAAM,kBAAN,cAA8B,MAAM;AAAA,EACzC,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AA4BO,IAAM,gBAAN,MAAoB;AA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