@alva-ai/toolkit 0.12.0 → 0.13.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/dist/index.d.cts CHANGED
@@ -7,6 +7,10 @@ interface AlvaClientConfig {
7
7
  gaClientId?: string;
8
8
  gaSessionId?: string;
9
9
  utmParams?: string;
10
+ /** Optional default AbortSignal applied to every request from this client. */
11
+ signal?: AbortSignal;
12
+ /** Alva chat session owning produced artifacts; sent as X-Alva-Origin-Session-Id. */
13
+ originSessionId?: string;
10
14
  }
11
15
  interface UserProfile {
12
16
  id: string;
@@ -115,6 +119,11 @@ interface RunRequest {
115
119
  * 1–2048. Omitted means the server uses its default heap (256 MB).
116
120
  */
117
121
  max_heap_size_mb?: number;
122
+ /**
123
+ * Client-side HTTP timeout for waiting on POST /api/v1/run, in
124
+ * milliseconds. This is not sent to the server.
125
+ */
126
+ timeout_ms?: number;
118
127
  }
119
128
  interface RunResponse {
120
129
  result: string;
@@ -167,7 +176,7 @@ interface CronjobUpdateRequest {
167
176
  interface CronjobRunsListParams {
168
177
  cronjob_id: number;
169
178
  first?: number;
170
- cursor?: number;
179
+ cursor?: string;
171
180
  }
172
181
  interface CronjobRun {
173
182
  id: number;
@@ -1475,6 +1484,10 @@ interface RequestOptions {
1475
1484
  baseUrl?: string;
1476
1485
  /** If true, skip attaching any Alva auth header (X-Alva-Api-Key / x-Playbook-Viewer). */
1477
1486
  noAuth?: boolean;
1487
+ /** Client-side HTTP timeout in milliseconds. */
1488
+ timeoutMs?: number;
1489
+ /** Optional AbortSignal for this request. Defaults to the client-level signal. */
1490
+ signal?: AbortSignal;
1478
1491
  }
1479
1492
  declare class AlvaClient {
1480
1493
  readonly baseUrl: string;
@@ -1485,6 +1498,16 @@ declare class AlvaClient {
1485
1498
  readonly gaClientId?: string;
1486
1499
  readonly gaSessionId?: string;
1487
1500
  readonly utmParams?: string;
1501
+ readonly signal?: AbortSignal;
1502
+ /**
1503
+ * Alva chat session that owns work produced through this client (playbooks,
1504
+ * feeds, uploaded files). Forwarded as `X-Alva-Origin-Session-Id` so the
1505
+ * backend can attribute created artifacts to the originating session. This
1506
+ * is a request-context hint, NOT auth — the backend still verifies the
1507
+ * session belongs to the authenticated user before honoring it. Distinct
1508
+ * from `gaSessionId` (browser analytics).
1509
+ */
1510
+ readonly originSessionId?: string;
1488
1511
  private _fs?;
1489
1512
  private _run?;
1490
1513
  private _deploy?;
@@ -1539,7 +1562,8 @@ declare class AlvaClient {
1539
1562
  declare class AlvaError extends Error {
1540
1563
  readonly code: string;
1541
1564
  readonly status: number;
1542
- constructor(code: string, message: string, status: number);
1565
+ readonly details?: unknown;
1566
+ constructor(code: string, message: string, status: number, details?: unknown);
1543
1567
  }
1544
1568
 
1545
1569
  type UdfErrorCode = 'PBSV_EXPIRED' | 'PBSV_INVALID' | 'PBSV_PID_MISMATCH' | 'CONSENT_REQUIRED' | 'INSUFFICIENT_CREDITS' | 'FUNCTION_NOT_FOUND' | 'FUNCTION_DISABLED' | 'RESOURCE_EXHAUSTED' | 'EXECUTION_FAILED' | 'UNAUTHENTICATED' | 'PERMISSION_DENIED' | 'INTERNAL';
package/dist/index.d.ts CHANGED
@@ -7,6 +7,10 @@ interface AlvaClientConfig {
7
7
  gaClientId?: string;
8
8
  gaSessionId?: string;
9
9
  utmParams?: string;
10
+ /** Optional default AbortSignal applied to every request from this client. */
11
+ signal?: AbortSignal;
12
+ /** Alva chat session owning produced artifacts; sent as X-Alva-Origin-Session-Id. */
13
+ originSessionId?: string;
10
14
  }
11
15
  interface UserProfile {
12
16
  id: string;
@@ -115,6 +119,11 @@ interface RunRequest {
115
119
  * 1–2048. Omitted means the server uses its default heap (256 MB).
116
120
  */
117
121
  max_heap_size_mb?: number;
122
+ /**
123
+ * Client-side HTTP timeout for waiting on POST /api/v1/run, in
124
+ * milliseconds. This is not sent to the server.
125
+ */
126
+ timeout_ms?: number;
118
127
  }
119
128
  interface RunResponse {
120
129
  result: string;
@@ -167,7 +176,7 @@ interface CronjobUpdateRequest {
167
176
  interface CronjobRunsListParams {
168
177
  cronjob_id: number;
169
178
  first?: number;
170
- cursor?: number;
179
+ cursor?: string;
171
180
  }
172
181
  interface CronjobRun {
173
182
  id: number;
@@ -1475,6 +1484,10 @@ interface RequestOptions {
1475
1484
  baseUrl?: string;
1476
1485
  /** If true, skip attaching any Alva auth header (X-Alva-Api-Key / x-Playbook-Viewer). */
1477
1486
  noAuth?: boolean;
1487
+ /** Client-side HTTP timeout in milliseconds. */
1488
+ timeoutMs?: number;
1489
+ /** Optional AbortSignal for this request. Defaults to the client-level signal. */
1490
+ signal?: AbortSignal;
1478
1491
  }
1479
1492
  declare class AlvaClient {
1480
1493
  readonly baseUrl: string;
@@ -1485,6 +1498,16 @@ declare class AlvaClient {
1485
1498
  readonly gaClientId?: string;
1486
1499
  readonly gaSessionId?: string;
1487
1500
  readonly utmParams?: string;
1501
+ readonly signal?: AbortSignal;
1502
+ /**
1503
+ * Alva chat session that owns work produced through this client (playbooks,
1504
+ * feeds, uploaded files). Forwarded as `X-Alva-Origin-Session-Id` so the
1505
+ * backend can attribute created artifacts to the originating session. This
1506
+ * is a request-context hint, NOT auth — the backend still verifies the
1507
+ * session belongs to the authenticated user before honoring it. Distinct
1508
+ * from `gaSessionId` (browser analytics).
1509
+ */
1510
+ readonly originSessionId?: string;
1488
1511
  private _fs?;
1489
1512
  private _run?;
1490
1513
  private _deploy?;
@@ -1539,7 +1562,8 @@ declare class AlvaClient {
1539
1562
  declare class AlvaError extends Error {
1540
1563
  readonly code: string;
1541
1564
  readonly status: number;
1542
- constructor(code: string, message: string, status: number);
1565
+ readonly details?: unknown;
1566
+ constructor(code: string, message: string, status: number, details?: unknown);
1543
1567
  }
1544
1568
 
1545
1569
  type UdfErrorCode = 'PBSV_EXPIRED' | 'PBSV_INVALID' | 'PBSV_PID_MISMATCH' | 'CONSENT_REQUIRED' | 'INSUFFICIENT_CREDITS' | 'FUNCTION_NOT_FOUND' | 'FUNCTION_DISABLED' | 'RESOURCE_EXHAUSTED' | 'EXECUTION_FAILED' | 'UNAUTHENTICATED' | 'PERMISSION_DENIED' | 'INTERNAL';
package/dist/index.js CHANGED
@@ -2,15 +2,56 @@
2
2
  var AlvaError = class extends Error {
3
3
  code;
4
4
  status;
5
- constructor(code, message, status) {
5
+ details;
6
+ constructor(code, message, status, details) {
6
7
  super(message);
7
8
  this.name = "AlvaError";
8
9
  this.code = code;
9
10
  this.status = status;
11
+ this.details = details;
10
12
  }
11
13
  };
12
14
 
13
15
  // src/resources/fs.ts
16
+ function isValidUtf8(bytes) {
17
+ let i = 0;
18
+ while (i < bytes.length) {
19
+ const byte = bytes[i];
20
+ if (byte <= 127) {
21
+ i += 1;
22
+ continue;
23
+ }
24
+ let needed = 0;
25
+ let minCodePoint = 0;
26
+ let codePoint = 0;
27
+ if (byte >= 194 && byte <= 223) {
28
+ needed = 1;
29
+ minCodePoint = 128;
30
+ codePoint = byte & 31;
31
+ } else if (byte >= 224 && byte <= 239) {
32
+ needed = 2;
33
+ minCodePoint = 2048;
34
+ codePoint = byte & 15;
35
+ } else if (byte >= 240 && byte <= 244) {
36
+ needed = 3;
37
+ minCodePoint = 65536;
38
+ codePoint = byte & 7;
39
+ } else {
40
+ return false;
41
+ }
42
+ if (i + needed >= bytes.length) return false;
43
+ for (let j = 1; j <= needed; j += 1) {
44
+ const next = bytes[i + j];
45
+ if ((next & 192) !== 128) return false;
46
+ codePoint = codePoint << 6 | next & 63;
47
+ }
48
+ if (codePoint < minCodePoint || codePoint > 1114111 || codePoint >= 55296 && codePoint <= 57343) {
49
+ return false;
50
+ }
51
+ i += needed + 1;
52
+ }
53
+ return true;
54
+ }
14
55
  var FsResource = class {
15
56
  constructor(client) {
16
57
  this.client = client;
@@ -22,15 +63,14 @@ var FsResource = class {
22
63
  query: { path: params.path, offset: params.offset, size: params.size }
23
64
  });
24
65
  if (!(result instanceof ArrayBuffer)) return result;
66
+ if (!isValidUtf8(new Uint8Array(result))) {
67
+ return result;
68
+ }
69
+ const text = new TextDecoder("utf-8").decode(result);
25
70
  try {
26
- const text = new TextDecoder("utf-8", { fatal: true }).decode(result);
27
- try {
28
- return JSON.parse(text);
29
- } catch {
30
- return text;
31
- }
71
+ return JSON.parse(text);
32
72
  } catch {
33
- return result;
73
+ return text;
34
74
  }
35
75
  }
36
76
  /** Write file using JSON body (Mode 2). For text content. */
@@ -149,7 +189,8 @@ var RunResource = class {
149
189
  working_dir: params.working_dir,
150
190
  args: params.args,
151
191
  max_heap_size_mb: params.max_heap_size_mb
152
- }
192
+ },
193
+ timeoutMs: params.timeout_ms
153
194
  });
154
195
  }
155
196
  };
@@ -2484,6 +2525,62 @@ function parseResultPayload(result) {
2484
2525
  // src/client.ts
2485
2526
  var DEFAULT_BASE_URL = "https://api-llm.prd.alva.ai";
2486
2527
  var DEFAULT_ARRAYS_BASE_URL = "https://data-tools.prd.space.id";
2528
+ function isRecord(value) {
2529
+ return typeof value === "object" && value !== null;
2530
+ }
2531
+ function stringField(value, field) {
2532
+ if (!isRecord(value)) return void 0;
2533
+ const fieldValue = value[field];
2534
+ return typeof fieldValue === "string" && fieldValue ? fieldValue : void 0;
2535
+ }
2536
+ function describeCause(cause) {
2537
+ if (cause === void 0 || cause === null) {
2538
+ return {};
2539
+ }
2540
+ const details = {};
2541
+ if (cause instanceof Error) {
2542
+ if (cause.name) details.name = cause.name;
2543
+ if (cause.message) details.message = cause.message;
2544
+ const code = stringField(cause, "code");
2545
+ if (code) details.code = code;
2546
+ } else if (isRecord(cause)) {
2547
+ const code = stringField(cause, "code");
2548
+ const name = stringField(cause, "name");
2549
+ const message = stringField(cause, "message");
2550
+ if (code) details.code = code;
2551
+ if (name) details.name = name;
2552
+ if (message) details.message = message;
2553
+ } else {
2554
+ details.message = String(cause);
2555
+ }
2556
+ const parts = [details.code, details.name, details.message].filter(
2557
+ (part) => Boolean(part)
2558
+ );
2559
+ return {
2560
+ message: parts.length > 0 ? parts.join(": ") : void 0,
2561
+ details: Object.keys(details).length > 0 ? details : void 0
2562
+ };
2563
+ }
2564
+ function describeFetchFailure(err) {
2565
+ const message = err instanceof Error && err.message ? err.message : "Network request failed";
2566
+ const details = {};
2567
+ if (err instanceof Error) {
2568
+ details.name = err.name;
2569
+ details.message = err.message;
2570
+ } else {
2571
+ details.message = String(err);
2572
+ }
2573
+ const { message: causeMessage, details: causeDetails } = describeCause(
2574
+ isRecord(err) ? err.cause : void 0
2575
+ );
2576
+ if (causeDetails) {
2577
+ details.cause = causeDetails;
2578
+ }
2579
+ return {
2580
+ message: causeMessage ? `${message}; cause: ${causeMessage}` : message,
2581
+ details
2582
+ };
2583
+ }
2487
2584
  var AlvaClient = class {
2488
2585
  baseUrl;
2489
2586
  arraysBaseUrl;
@@ -2493,6 +2590,16 @@ var AlvaClient = class {
2493
2590
  gaClientId;
2494
2591
  gaSessionId;
2495
2592
  utmParams;
2593
+ signal;
2594
+ /**
2595
+ * Alva chat session that owns work produced through this client (playbooks,
2596
+ * feeds, uploaded files). Forwarded as `X-Alva-Origin-Session-Id` so the
2597
+ * backend can attribute created artifacts to the originating session. This
2598
+ * is a request-context hint, NOT auth — the backend still verifies the
2599
+ * session belongs to the authenticated user before honoring it. Distinct
2600
+ * from `gaSessionId` (browser analytics).
2601
+ */
2602
+ originSessionId;
2496
2603
  _fs;
2497
2604
  _run;
2498
2605
  _deploy;
@@ -2525,6 +2632,8 @@ var AlvaClient = class {
2525
2632
  this.gaClientId = config.gaClientId;
2526
2633
  this.gaSessionId = config.gaSessionId;
2527
2634
  this.utmParams = config.utmParams;
2635
+ this.signal = config.signal;
2636
+ this.originSessionId = config.originSessionId;
2528
2637
  }
2529
2638
  get fs() {
2530
2639
  return this._fs ??= new FsResource(this);
@@ -2639,6 +2748,9 @@ var AlvaClient = class {
2639
2748
  if (this.utmParams) {
2640
2749
  headers["X-Alva-UTM-Params"] = this.utmParams;
2641
2750
  }
2751
+ if (this.originSessionId && !options?.noAuth) {
2752
+ headers["X-Alva-Origin-Session-Id"] = this.originSessionId;
2753
+ }
2642
2754
  let fetchBody;
2643
2755
  if (options?.rawBody !== void 0) {
2644
2756
  headers["Content-Type"] = "application/octet-stream";
@@ -2651,21 +2763,89 @@ var AlvaClient = class {
2651
2763
  fetchBody = JSON.stringify(options.body);
2652
2764
  }
2653
2765
  let response;
2766
+ const timeoutMs = options?.timeoutMs;
2767
+ const externalSignal = options?.signal ?? this.signal;
2768
+ if (externalSignal?.aborted) {
2769
+ throw new AlvaError(
2770
+ "NETWORK_ABORTED",
2771
+ `Request aborted while calling ${method} ${path}`,
2772
+ 0,
2773
+ {
2774
+ method,
2775
+ path
2776
+ }
2777
+ );
2778
+ }
2779
+ const controller = timeoutMs !== void 0 ? new AbortController() : void 0;
2780
+ let didTimeout = false;
2781
+ let timeoutId;
2782
+ let abortListener;
2783
+ if (controller && timeoutMs !== void 0) {
2784
+ if (externalSignal) {
2785
+ abortListener = () => controller.abort(externalSignal.reason);
2786
+ externalSignal.addEventListener("abort", abortListener, { once: true });
2787
+ }
2788
+ timeoutId = setTimeout(() => {
2789
+ didTimeout = true;
2790
+ controller.abort(
2791
+ new Error(
2792
+ `Request timed out after ${timeoutMs}ms while calling ${method} ${path}`
2793
+ )
2794
+ );
2795
+ }, timeoutMs);
2796
+ }
2797
+ const fetchInit = {
2798
+ method,
2799
+ headers,
2800
+ body: fetchBody,
2801
+ signal: controller?.signal ?? externalSignal
2802
+ };
2803
+ if (timeoutMs !== void 0) {
2804
+ fetchInit.timeout = timeoutMs;
2805
+ }
2654
2806
  try {
2655
- response = await fetch(url, {
2807
+ response = await fetch(url, fetchInit);
2808
+ } catch (err) {
2809
+ if (externalSignal?.aborted && !didTimeout) {
2810
+ throw new AlvaError(
2811
+ "NETWORK_ABORTED",
2812
+ `Request aborted while calling ${method} ${path}`,
2813
+ 0,
2814
+ {
2815
+ method,
2816
+ path
2817
+ }
2818
+ );
2819
+ }
2820
+ if (didTimeout) {
2821
+ throw new AlvaError(
2822
+ "NETWORK_TIMEOUT",
2823
+ `Request timed out after ${timeoutMs}ms while calling ${method} ${path}`,
2824
+ 0,
2825
+ { method, path, timeout_ms: timeoutMs }
2826
+ );
2827
+ }
2828
+ const failure = describeFetchFailure(err);
2829
+ throw new AlvaError("NETWORK_ERROR", failure.message, 0, {
2656
2830
  method,
2657
- headers,
2658
- body: fetchBody
2831
+ path,
2832
+ ...failure.details
2659
2833
  });
2660
- } catch (err) {
2661
- throw new AlvaError(
2662
- "NETWORK_ERROR",
2663
- err instanceof Error ? err.message : "Network request failed",
2664
- 0
2665
- );
2834
+ } finally {
2835
+ if (abortListener && externalSignal) {
2836
+ externalSignal.removeEventListener("abort", abortListener);
2837
+ }
2838
+ if (timeoutId !== void 0) {
2839
+ clearTimeout(timeoutId);
2840
+ }
2666
2841
  }
2667
2842
  if (!response.ok) {
2668
- const bodyText = await response.text().catch(() => "");
2843
+ let bodyText = "";
2844
+ try {
2845
+ bodyText = await Promise.resolve(response.text());
2846
+ } catch {
2847
+ bodyText = "";
2848
+ }
2669
2849
  const contentType2 = response.headers.get("content-type") ?? "";
2670
2850
  if (contentType2.includes("application/json") && bodyText) {
2671
2851
  try {
@@ -3125,7 +3305,7 @@ var installPlaybookRuntime = (targetWindow) => {
3125
3305
  };
3126
3306
 
3127
3307
  // src/index.ts
3128
- var VERSION = true ? "0.12.0" : "dev";
3308
+ var VERSION = true ? "0.13.0" : "dev";
3129
3309
  export {
3130
3310
  AlvaClient,
3131
3311
  AlvaError,