@alva-ai/toolkit 0.23.1 → 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,6 +23,7 @@ __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,
@@ -249,6 +250,134 @@ var FsResource = class {
249
250
  }
250
251
  };
251
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
+
252
381
  // src/resources/run.ts
253
382
  var RunResource = class {
254
383
  constructor(client) {
@@ -257,13 +386,24 @@ var RunResource = class {
257
386
  client;
258
387
  async execute(params) {
259
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
+ };
260
404
  return this.client._request("POST", "/api/v1/run", {
261
- body: {
262
- code: params.code,
263
- entry_path: params.entry_path,
264
- working_dir: params.working_dir,
265
- args: params.args,
266
- max_heap_size_mb: params.max_heap_size_mb
405
+ ...serializedArgs === void 0 ? { body } : {
406
+ jsonBody: objectWithRawJSONField(body, "args", serializedArgs)
267
407
  },
268
408
  timeoutMs: params.timeout_ms
269
409
  });
@@ -278,19 +418,30 @@ var DeployResource = class {
278
418
  client;
279
419
  async create(params) {
280
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
+ };
281
442
  return this.client._request("POST", "/api/v1/deploy/cronjob", {
282
- body: {
283
- name: params.name,
284
- path: params.path,
285
- cron_expression: params.cron_expression,
286
- args: params.args,
287
- push_notify: params.push_notify,
288
- max_heap_size_mb: params.max_heap_size_mb,
289
- execution_timeout_seconds: params.execution_timeout_seconds,
290
- run_as_user_id: params.run_as_user_id,
291
- start_at: params.start_at,
292
- end_at: params.end_at,
293
- max_runs: params.max_runs
443
+ ...serializedArgs === void 0 ? { body } : {
444
+ jsonBody: objectWithRawJSONField(body, "args", serializedArgs)
294
445
  }
295
446
  });
296
447
  }
@@ -309,9 +460,19 @@ var DeployResource = class {
309
460
  }
310
461
  async update(params) {
311
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) {
312
471
  const { id, ...body } = params;
313
472
  return this.client._request("PATCH", `/api/v1/deploy/cronjob/${id}`, {
314
- body
473
+ ...serializedArgs === void 0 ? { body } : {
474
+ jsonBody: objectWithRawJSONField(body, "args", serializedArgs)
475
+ }
315
476
  });
316
477
  }
317
478
  async delete(params) {
@@ -567,11 +728,40 @@ function requireFeedID(id) {
567
728
  }
568
729
 
569
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();
570
758
  var AutomationResource = class {
571
759
  constructor(client) {
572
760
  this.client = client;
761
+ this.delivery = new AutomationAlertDeliveryResource(client);
573
762
  }
574
763
  client;
764
+ delivery;
575
765
  list(params = {}) {
576
766
  return this.client.feed.list(params);
577
767
  }
@@ -623,6 +813,90 @@ var AutomationResource = class {
623
813
  return this.client.release.feed(params);
624
814
  }
625
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
+ };
626
900
  function requireAutomationID(id) {
627
901
  if (!Number.isInteger(id) || id <= 0) {
628
902
  throw new Error("automation id must be a positive integer");
@@ -638,6 +912,23 @@ function requireAutomationIDString(id) {
638
912
  function hasAutomationUpdate(params) {
639
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;
640
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
+ }
641
932
 
642
933
  // src/resources/playbooks.ts
643
934
  var PLAYBOOK_VISIBILITIES = [
@@ -2493,7 +2784,7 @@ query ToolkitCreditWalletItems($input: CreditWalletItemConnectionInput!) {
2493
2784
  }
2494
2785
  }
2495
2786
  `.trim();
2496
- function graphQLErrorMessage(errors) {
2787
+ function graphQLErrorMessage2(errors) {
2497
2788
  const messages = errors.map(
2498
2789
  (error) => typeof error.message === "string" && error.message ? error.message : void 0
2499
2790
  ).filter((message) => Boolean(message));
@@ -2548,7 +2839,7 @@ var CreditsResource = class {
2548
2839
  if (response.errors && response.errors.length > 0) {
2549
2840
  throw new AlvaError(
2550
2841
  "GRAPHQL_ERROR",
2551
- graphQLErrorMessage(response.errors),
2842
+ graphQLErrorMessage2(response.errors),
2552
2843
  400,
2553
2844
  { errors: response.errors }
2554
2845
  );
@@ -2705,7 +2996,7 @@ query ToolkitCompanyEarnings($ticker: String!, $input: CompanyEarningsInput) {
2705
2996
  }
2706
2997
  }
2707
2998
  `.trim();
2708
- function graphQLErrorMessage2(errors) {
2999
+ function graphQLErrorMessage3(errors) {
2709
3000
  const messages = errors.map(
2710
3001
  (error) => typeof error.message === "string" && error.message ? error.message : void 0
2711
3002
  ).filter((message) => Boolean(message));
@@ -2861,7 +3152,7 @@ var MarketsResource = class {
2861
3152
  if (response.errors && response.errors.length > 0) {
2862
3153
  throw new AlvaError(
2863
3154
  "GRAPHQL_ERROR",
2864
- graphQLErrorMessage2(response.errors),
3155
+ graphQLErrorMessage3(response.errors),
2865
3156
  400,
2866
3157
  { errors: response.errors }
2867
3158
  );
@@ -3695,12 +3986,13 @@ var installPlaybookRuntime = (targetWindow) => {
3695
3986
  };
3696
3987
 
3697
3988
  // src/index.ts
3698
- var VERSION = true ? "0.23.1" : "dev";
3989
+ var VERSION = true ? "0.24.0" : "dev";
3699
3990
  // Annotate the CommonJS export names for ESM import in node:
3700
3991
  0 && (module.exports = {
3701
3992
  AlertsResource,
3702
3993
  AlvaClient,
3703
3994
  AlvaError,
3995
+ AutomationAlertDeliveryResource,
3704
3996
  AutomationResource,
3705
3997
  CliUsageError,
3706
3998
  MarketsResource,