@alva-ai/toolkit 0.23.0 → 0.24.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.cjs CHANGED
@@ -23,10 +23,12 @@ __export(src_exports, {
23
23
  AlertsResource: () => AlertsResource,
24
24
  AlvaClient: () => AlvaClient,
25
25
  AlvaError: () => AlvaError,
26
+ AutomationAlertDeliveryResource: () => AutomationAlertDeliveryResource,
26
27
  AutomationResource: () => AutomationResource,
27
28
  CliUsageError: () => CliUsageError,
28
29
  MarketsResource: () => MarketsResource,
29
30
  PBSV_UPDATE_MESSAGE: () => PBSV_UPDATE_MESSAGE,
31
+ SUBSCRIBE_MANAGE_MESSAGE: () => SUBSCRIBE_MANAGE_MESSAGE,
30
32
  SUBSCRIBE_PROPOSE_MESSAGE: () => SUBSCRIBE_PROPOSE_MESSAGE,
31
33
  SUBSCRIBE_RESULT_MESSAGE: () => SUBSCRIBE_RESULT_MESSAGE,
32
34
  UDF_AUTH_REQUIRED_MESSAGE: () => UDF_AUTH_REQUIRED_MESSAGE,
@@ -248,6 +250,134 @@ var FsResource = class {
248
250
  }
249
251
  };
250
252
 
253
+ // src/jsonPayload.ts
254
+ var MAX_SAFE_INTEGER_DIGITS = "9007199254740991";
255
+ function scaledIntegerGreater(left, leftTrailingZeros, right, rightTrailingZeros) {
256
+ const leftLength = left.length + leftTrailingZeros;
257
+ const rightLength = right.length + rightTrailingZeros;
258
+ if (leftLength !== rightLength) return leftLength > rightLength;
259
+ for (let index = 0; index < leftLength; index++) {
260
+ const leftDigit = index < left.length ? left[index] : "0";
261
+ const rightDigit = index < right.length ? right[index] : "0";
262
+ if (leftDigit !== rightDigit) return leftDigit > rightDigit;
263
+ }
264
+ return false;
265
+ }
266
+ function numberOutsideSafeRange(token) {
267
+ const match = token.match(
268
+ /^-?(?<whole>\d+)(?:\.(?<fraction>\d+))?(?:[eE](?<exponent>[+-]?\d+))?$/
269
+ );
270
+ if (!match?.groups) return true;
271
+ const whole = match.groups.whole;
272
+ const fraction = match.groups.fraction ?? "";
273
+ const exponent = Number(match.groups.exponent ?? "0");
274
+ if (!Number.isSafeInteger(exponent)) return true;
275
+ const digits = (whole + fraction).replace(/^0+/, "") || "0";
276
+ if (digits === "0") return false;
277
+ const scale = exponent - fraction.length;
278
+ return scale >= 0 ? scaledIntegerGreater(digits, scale, MAX_SAFE_INTEGER_DIGITS, 0) : scaledIntegerGreater(digits, 0, MAX_SAFE_INTEGER_DIGITS, -scale);
279
+ }
280
+ function validateJSONNumberTokens(raw) {
281
+ let index = 0;
282
+ while (index < raw.length) {
283
+ if (raw[index] === '"') {
284
+ index++;
285
+ while (index < raw.length) {
286
+ if (raw[index] === "\\") {
287
+ index += 2;
288
+ } else if (raw[index++] === '"') {
289
+ break;
290
+ }
291
+ }
292
+ continue;
293
+ }
294
+ if (raw[index] === "-" || /\d/.test(raw[index] ?? "")) {
295
+ const match = raw.slice(index).match(/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/);
296
+ if (match) {
297
+ if (numberOutsideSafeRange(match[0])) {
298
+ throw new TypeError(
299
+ "args contains a number outside JavaScript's safe range; use a string for int64 and identifier values"
300
+ );
301
+ }
302
+ index += match[0].length;
303
+ continue;
304
+ }
305
+ }
306
+ index++;
307
+ }
308
+ }
309
+ function assertPlainJSONData(value, path, seen) {
310
+ if (value === null || typeof value === "string" || typeof value === "boolean")
311
+ return;
312
+ if (typeof value === "number") {
313
+ if (!Number.isFinite(value)) {
314
+ throw new TypeError(`${path} must contain only finite numbers`);
315
+ }
316
+ if (Number.isInteger(value) && !Number.isSafeInteger(value)) {
317
+ throw new TypeError(
318
+ `${path} contains a number outside JavaScript's safe range; use a string for int64 and identifier values`
319
+ );
320
+ }
321
+ return;
322
+ }
323
+ if (typeof value !== "object") {
324
+ throw new TypeError(`${path} must contain only JSON data`);
325
+ }
326
+ if (seen.has(value)) {
327
+ throw new TypeError(`${path} must not contain circular references`);
328
+ }
329
+ seen.add(value);
330
+ if (Array.isArray(value)) {
331
+ value.forEach(
332
+ (item, index) => assertPlainJSONData(item, `${path}[${index}]`, seen)
333
+ );
334
+ } else {
335
+ const prototype = Object.getPrototypeOf(value);
336
+ if (prototype !== Object.prototype && prototype !== null) {
337
+ throw new TypeError(`${path} must contain only plain JSON objects`);
338
+ }
339
+ for (const [key, descriptor] of Object.entries(
340
+ Object.getOwnPropertyDescriptors(value)
341
+ )) {
342
+ if (!descriptor.enumerable) continue;
343
+ if (!("value" in descriptor)) {
344
+ throw new TypeError(`${path} must not contain accessors`);
345
+ }
346
+ assertPlainJSONData(descriptor.value, `${path}.${key}`, seen);
347
+ }
348
+ }
349
+ seen.delete(value);
350
+ }
351
+ function serializeStructuredArgs(value) {
352
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
353
+ throw new TypeError("args must be a JSON object");
354
+ }
355
+ assertPlainJSONData(value, "args", /* @__PURE__ */ new Set());
356
+ const serialized = JSON.stringify(value);
357
+ validateSerializedArgs(serialized);
358
+ return serialized;
359
+ }
360
+ function validateSerializedArgs(raw) {
361
+ const value = JSON.parse(raw);
362
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
363
+ throw new TypeError("args must be a JSON object");
364
+ }
365
+ validateJSONNumberTokens(raw);
366
+ }
367
+ function objectWithRawJSONField(fields, fieldName, rawValue) {
368
+ const entries = Object.entries(fields).filter(
369
+ ([key, value]) => value !== void 0 && (rawValue === void 0 || key !== fieldName)
370
+ );
371
+ if (rawValue !== void 0) {
372
+ validateSerializedArgs(rawValue);
373
+ entries.push([fieldName, rawValue]);
374
+ }
375
+ return `{${entries.map(([key, value]) => {
376
+ const encodedValue = key === fieldName && rawValue !== void 0 ? rawValue : JSON.stringify(value);
377
+ return `${JSON.stringify(key)}:${encodedValue}`;
378
+ }).join(",")}}`;
379
+ }
380
+
251
381
  // src/resources/run.ts
252
382
  var RunResource = class {
253
383
  constructor(client) {
@@ -256,13 +386,24 @@ var RunResource = class {
256
386
  client;
257
387
  async execute(params) {
258
388
  this.client._requireAuth();
389
+ return params.args === void 0 ? this.request(params) : this.request(params, serializeStructuredArgs(params.args));
390
+ }
391
+ /** @internal Used by the CLI to preserve the exact --args JSON text. */
392
+ async _executeSerializedArgs(params, serializedArgs) {
393
+ this.client._requireAuth();
394
+ return this.request(params, serializedArgs);
395
+ }
396
+ request(params, serializedArgs) {
397
+ const body = {
398
+ code: params.code,
399
+ entry_path: params.entry_path,
400
+ working_dir: params.working_dir,
401
+ args: params.args,
402
+ max_heap_size_mb: params.max_heap_size_mb
403
+ };
259
404
  return this.client._request("POST", "/api/v1/run", {
260
- body: {
261
- code: params.code,
262
- entry_path: params.entry_path,
263
- working_dir: params.working_dir,
264
- args: params.args,
265
- max_heap_size_mb: params.max_heap_size_mb
405
+ ...serializedArgs === void 0 ? { body } : {
406
+ jsonBody: objectWithRawJSONField(body, "args", serializedArgs)
266
407
  },
267
408
  timeoutMs: params.timeout_ms
268
409
  });
@@ -277,19 +418,30 @@ var DeployResource = class {
277
418
  client;
278
419
  async create(params) {
279
420
  this.client._requireAuth();
421
+ return params.args === void 0 ? this.createRequest(params) : this.createRequest(params, serializeStructuredArgs(params.args));
422
+ }
423
+ /** @internal Used by the CLI to preserve the exact --args JSON text. */
424
+ async _createSerializedArgs(params, serializedArgs) {
425
+ this.client._requireAuth();
426
+ return this.createRequest(params, serializedArgs);
427
+ }
428
+ createRequest(params, serializedArgs) {
429
+ const body = {
430
+ name: params.name,
431
+ path: params.path,
432
+ cron_expression: params.cron_expression,
433
+ args: params.args,
434
+ push_notify: params.push_notify,
435
+ max_heap_size_mb: params.max_heap_size_mb,
436
+ execution_timeout_seconds: params.execution_timeout_seconds,
437
+ run_as_user_id: params.run_as_user_id,
438
+ start_at: params.start_at,
439
+ end_at: params.end_at,
440
+ max_runs: params.max_runs
441
+ };
280
442
  return this.client._request("POST", "/api/v1/deploy/cronjob", {
281
- body: {
282
- name: params.name,
283
- path: params.path,
284
- cron_expression: params.cron_expression,
285
- args: params.args,
286
- push_notify: params.push_notify,
287
- max_heap_size_mb: params.max_heap_size_mb,
288
- execution_timeout_seconds: params.execution_timeout_seconds,
289
- run_as_user_id: params.run_as_user_id,
290
- start_at: params.start_at,
291
- end_at: params.end_at,
292
- max_runs: params.max_runs
443
+ ...serializedArgs === void 0 ? { body } : {
444
+ jsonBody: objectWithRawJSONField(body, "args", serializedArgs)
293
445
  }
294
446
  });
295
447
  }
@@ -308,9 +460,19 @@ var DeployResource = class {
308
460
  }
309
461
  async update(params) {
310
462
  this.client._requireAuth();
463
+ return params.args === void 0 ? this.updateRequest(params) : this.updateRequest(params, serializeStructuredArgs(params.args));
464
+ }
465
+ /** @internal Used by the CLI to preserve the exact --args JSON text. */
466
+ async _updateSerializedArgs(params, serializedArgs) {
467
+ this.client._requireAuth();
468
+ return this.updateRequest(params, serializedArgs);
469
+ }
470
+ updateRequest(params, serializedArgs) {
311
471
  const { id, ...body } = params;
312
472
  return this.client._request("PATCH", `/api/v1/deploy/cronjob/${id}`, {
313
- body
473
+ ...serializedArgs === void 0 ? { body } : {
474
+ jsonBody: objectWithRawJSONField(body, "args", serializedArgs)
475
+ }
314
476
  });
315
477
  }
316
478
  async delete(params) {
@@ -566,11 +728,40 @@ function requireFeedID(id) {
566
728
  }
567
729
 
568
730
  // src/resources/automation.ts
731
+ var AUTOMATION_ALERT_DELIVERY_QUERY = `
732
+ query ToolkitAutomationAlertDelivery($automationId: ID!) {
733
+ automation(id: $automationId) {
734
+ alertDelivery {
735
+ isEnabled
736
+ alvaChannels { id }
737
+ email { isEnabled isAvailable address }
738
+ }
739
+ }
740
+ }
741
+ `.trim();
742
+ var UPDATE_AUTOMATION_ALERT_DELIVERY = `
743
+ mutation ToolkitUpdateAutomationAlertDelivery(
744
+ $input: UpdateAutomationAlertDeliveryInput!
745
+ $includeAlvaChannels: Boolean!
746
+ $includeEmail: Boolean!
747
+ ) {
748
+ updateAutomationAlertDelivery(input: $input) {
749
+ automation { id }
750
+ alertDelivery {
751
+ isEnabled
752
+ alvaChannels @include(if: $includeAlvaChannels) { id }
753
+ email @include(if: $includeEmail) { isEnabled isAvailable address }
754
+ }
755
+ }
756
+ }
757
+ `.trim();
569
758
  var AutomationResource = class {
570
759
  constructor(client) {
571
760
  this.client = client;
761
+ this.delivery = new AutomationAlertDeliveryResource(client);
572
762
  }
573
763
  client;
764
+ delivery;
574
765
  list(params = {}) {
575
766
  return this.client.feed.list(params);
576
767
  }
@@ -622,6 +813,90 @@ var AutomationResource = class {
622
813
  return this.client.release.feed(params);
623
814
  }
624
815
  };
816
+ var AutomationAlertDeliveryResource = class {
817
+ constructor(client) {
818
+ this.client = client;
819
+ }
820
+ client;
821
+ async get(params) {
822
+ this.client._requireAuth();
823
+ const id = requireAutomationIDString(params.id);
824
+ const data = await this.graphql(
825
+ AUTOMATION_ALERT_DELIVERY_QUERY,
826
+ { automationId: id }
827
+ );
828
+ const delivery = data.automation?.alertDelivery;
829
+ if (!delivery) {
830
+ throw new AlvaError(
831
+ "GRAPHQL_EMPTY_RESPONSE",
832
+ "GraphQL response did not include automation.alertDelivery",
833
+ 502
834
+ );
835
+ }
836
+ return delivery;
837
+ }
838
+ async update(params) {
839
+ this.client._requireAuth();
840
+ const id = requireAutomationIDString(params.id);
841
+ const includeAlvaChannels = params.alvaChannelIds !== void 0;
842
+ const includeEmail = params.emailEnabled !== void 0;
843
+ if (!includeAlvaChannels && !includeEmail) {
844
+ throw new Error(
845
+ "automation delivery update requires alvaChannelIds or emailEnabled"
846
+ );
847
+ }
848
+ const alvaChannelIds = includeAlvaChannels ? uniqueAlvaChannelIDs(params.alvaChannelIds ?? []) : void 0;
849
+ const data = await this.graphql(
850
+ UPDATE_AUTOMATION_ALERT_DELIVERY,
851
+ {
852
+ input: {
853
+ automationId: id,
854
+ ...includeAlvaChannels ? { alvaChannelIds } : {},
855
+ ...includeEmail ? { emailEnabled: params.emailEnabled } : {}
856
+ },
857
+ includeAlvaChannels,
858
+ includeEmail
859
+ }
860
+ );
861
+ const result = data.updateAutomationAlertDelivery;
862
+ if (!result) {
863
+ throw new AlvaError(
864
+ "GRAPHQL_EMPTY_RESPONSE",
865
+ "GraphQL response did not include updateAutomationAlertDelivery",
866
+ 502
867
+ );
868
+ }
869
+ return result;
870
+ }
871
+ async graphql(query, variables) {
872
+ const response = await this.client._request("POST", "/query", {
873
+ body: { query, variables }
874
+ });
875
+ if (!response) {
876
+ throw new AlvaError(
877
+ "GRAPHQL_EMPTY_RESPONSE",
878
+ "GraphQL response was empty",
879
+ 502
880
+ );
881
+ }
882
+ if (response.errors && response.errors.length > 0) {
883
+ throw new AlvaError(
884
+ "GRAPHQL_ERROR",
885
+ graphQLErrorMessage(response.errors),
886
+ 400,
887
+ { errors: response.errors }
888
+ );
889
+ }
890
+ if (!response.data) {
891
+ throw new AlvaError(
892
+ "GRAPHQL_EMPTY_RESPONSE",
893
+ "GraphQL response did not include data",
894
+ 502
895
+ );
896
+ }
897
+ return response.data;
898
+ }
899
+ };
625
900
  function requireAutomationID(id) {
626
901
  if (!Number.isInteger(id) || id <= 0) {
627
902
  throw new Error("automation id must be a positive integer");
@@ -637,6 +912,23 @@ function requireAutomationIDString(id) {
637
912
  function hasAutomationUpdate(params) {
638
913
  return params.version !== void 0 || params.cronjob_id !== void 0 || params.description !== void 0 || params.changelog !== void 0 || params.agent_type !== void 0 || params.trigger === true;
639
914
  }
915
+ function uniqueAlvaChannelIDs(values) {
916
+ const ids = [...new Set(values)];
917
+ for (const id of ids) {
918
+ if (!/^(0|[1-9]\d*)$/.test(id)) {
919
+ throw new Error(
920
+ "Alva channel ids must be positive integer strings or the agent sentinel 0"
921
+ );
922
+ }
923
+ }
924
+ return ids;
925
+ }
926
+ function graphQLErrorMessage(errors) {
927
+ const messages = errors.map(
928
+ (error) => error && typeof error.message === "string" && error.message ? error.message : void 0
929
+ ).filter((message) => Boolean(message));
930
+ return messages.length > 0 ? messages.join("; ") : "GraphQL request failed";
931
+ }
640
932
 
641
933
  // src/resources/playbooks.ts
642
934
  var PLAYBOOK_VISIBILITIES = [
@@ -2492,7 +2784,7 @@ query ToolkitCreditWalletItems($input: CreditWalletItemConnectionInput!) {
2492
2784
  }
2493
2785
  }
2494
2786
  `.trim();
2495
- function graphQLErrorMessage(errors) {
2787
+ function graphQLErrorMessage2(errors) {
2496
2788
  const messages = errors.map(
2497
2789
  (error) => typeof error.message === "string" && error.message ? error.message : void 0
2498
2790
  ).filter((message) => Boolean(message));
@@ -2547,7 +2839,7 @@ var CreditsResource = class {
2547
2839
  if (response.errors && response.errors.length > 0) {
2548
2840
  throw new AlvaError(
2549
2841
  "GRAPHQL_ERROR",
2550
- graphQLErrorMessage(response.errors),
2842
+ graphQLErrorMessage2(response.errors),
2551
2843
  400,
2552
2844
  { errors: response.errors }
2553
2845
  );
@@ -2704,7 +2996,7 @@ query ToolkitCompanyEarnings($ticker: String!, $input: CompanyEarningsInput) {
2704
2996
  }
2705
2997
  }
2706
2998
  `.trim();
2707
- function graphQLErrorMessage2(errors) {
2999
+ function graphQLErrorMessage3(errors) {
2708
3000
  const messages = errors.map(
2709
3001
  (error) => typeof error.message === "string" && error.message ? error.message : void 0
2710
3002
  ).filter((message) => Boolean(message));
@@ -2860,7 +3152,7 @@ var MarketsResource = class {
2860
3152
  if (response.errors && response.errors.length > 0) {
2861
3153
  throw new AlvaError(
2862
3154
  "GRAPHQL_ERROR",
2863
- graphQLErrorMessage2(response.errors),
3155
+ graphQLErrorMessage3(response.errors),
2864
3156
  400,
2865
3157
  { errors: response.errors }
2866
3158
  );
@@ -3313,6 +3605,7 @@ var UDF_CONSENT_RESPONSE_MESSAGE = "alva:udf:consent-response";
3313
3605
  var UDF_AUTH_REQUIRED_MESSAGE = "alva:udf:auth-required";
3314
3606
  var SUBSCRIBE_PROPOSE_MESSAGE = "alva:subscribe:propose";
3315
3607
  var SUBSCRIBE_RESULT_MESSAGE = "alva:subscribe:result";
3608
+ var SUBSCRIBE_MANAGE_MESSAGE = "alva:subscribe:manage";
3316
3609
  var DEFAULT_API_ORIGIN = "https://api-llm.prd.alva.ai";
3317
3610
  var CONSENT_TIMEOUT_MS = 5 * 60 * 1e3;
3318
3611
  var cachedToken = null;
@@ -3505,8 +3798,19 @@ var proposeSubscribe = async (input) => {
3505
3798
  );
3506
3799
  });
3507
3800
  };
3801
+ var manageSubscriptions = () => {
3802
+ if (!runtimeWindow || !expectedParentOrigin) return;
3803
+ runtimeWindow.parent.postMessage(
3804
+ {
3805
+ type: SUBSCRIBE_MANAGE_MESSAGE,
3806
+ playbook_id: playbookIdFromBoot
3807
+ },
3808
+ expectedParentOrigin
3809
+ );
3810
+ };
3508
3811
  var subscribe = {
3509
- propose: proposeSubscribe
3812
+ propose: proposeSubscribe,
3813
+ manage: manageSubscriptions
3510
3814
  };
3511
3815
  var requestAuthentication = (functionName) => {
3512
3816
  if (!runtimeWindow || !expectedParentOrigin) return;
@@ -3682,16 +3986,18 @@ var installPlaybookRuntime = (targetWindow) => {
3682
3986
  };
3683
3987
 
3684
3988
  // src/index.ts
3685
- var VERSION = true ? "0.23.0" : "dev";
3989
+ var VERSION = true ? "0.24.0" : "dev";
3686
3990
  // Annotate the CommonJS export names for ESM import in node:
3687
3991
  0 && (module.exports = {
3688
3992
  AlertsResource,
3689
3993
  AlvaClient,
3690
3994
  AlvaError,
3995
+ AutomationAlertDeliveryResource,
3691
3996
  AutomationResource,
3692
3997
  CliUsageError,
3693
3998
  MarketsResource,
3694
3999
  PBSV_UPDATE_MESSAGE,
4000
+ SUBSCRIBE_MANAGE_MESSAGE,
3695
4001
  SUBSCRIBE_PROPOSE_MESSAGE,
3696
4002
  SUBSCRIBE_RESULT_MESSAGE,
3697
4003
  UDF_AUTH_REQUIRED_MESSAGE,