@alva-ai/toolkit 0.23.1 → 0.25.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.js CHANGED
@@ -190,6 +190,134 @@ var FsResource = class {
190
190
  }
191
191
  };
192
192
 
193
+ // src/jsonPayload.ts
194
+ var MAX_SAFE_INTEGER_DIGITS = "9007199254740991";
195
+ function scaledIntegerGreater(left, leftTrailingZeros, right, rightTrailingZeros) {
196
+ const leftLength = left.length + leftTrailingZeros;
197
+ const rightLength = right.length + rightTrailingZeros;
198
+ if (leftLength !== rightLength) return leftLength > rightLength;
199
+ for (let index = 0; index < leftLength; index++) {
200
+ const leftDigit = index < left.length ? left[index] : "0";
201
+ const rightDigit = index < right.length ? right[index] : "0";
202
+ if (leftDigit !== rightDigit) return leftDigit > rightDigit;
203
+ }
204
+ return false;
205
+ }
206
+ function numberOutsideSafeRange(token) {
207
+ const match = token.match(
208
+ /^-?(?<whole>\d+)(?:\.(?<fraction>\d+))?(?:[eE](?<exponent>[+-]?\d+))?$/
209
+ );
210
+ if (!match?.groups) return true;
211
+ const whole = match.groups.whole;
212
+ const fraction = match.groups.fraction ?? "";
213
+ const exponent = Number(match.groups.exponent ?? "0");
214
+ if (!Number.isSafeInteger(exponent)) return true;
215
+ const digits = (whole + fraction).replace(/^0+/, "") || "0";
216
+ if (digits === "0") return false;
217
+ const scale = exponent - fraction.length;
218
+ return scale >= 0 ? scaledIntegerGreater(digits, scale, MAX_SAFE_INTEGER_DIGITS, 0) : scaledIntegerGreater(digits, 0, MAX_SAFE_INTEGER_DIGITS, -scale);
219
+ }
220
+ function validateJSONNumberTokens(raw) {
221
+ let index = 0;
222
+ while (index < raw.length) {
223
+ if (raw[index] === '"') {
224
+ index++;
225
+ while (index < raw.length) {
226
+ if (raw[index] === "\\") {
227
+ index += 2;
228
+ } else if (raw[index++] === '"') {
229
+ break;
230
+ }
231
+ }
232
+ continue;
233
+ }
234
+ if (raw[index] === "-" || /\d/.test(raw[index] ?? "")) {
235
+ const match = raw.slice(index).match(/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/);
236
+ if (match) {
237
+ if (numberOutsideSafeRange(match[0])) {
238
+ throw new TypeError(
239
+ "args contains a number outside JavaScript's safe range; use a string for int64 and identifier values"
240
+ );
241
+ }
242
+ index += match[0].length;
243
+ continue;
244
+ }
245
+ }
246
+ index++;
247
+ }
248
+ }
249
+ function assertPlainJSONData(value, path, seen) {
250
+ if (value === null || typeof value === "string" || typeof value === "boolean")
251
+ return;
252
+ if (typeof value === "number") {
253
+ if (!Number.isFinite(value)) {
254
+ throw new TypeError(`${path} must contain only finite numbers`);
255
+ }
256
+ if (Number.isInteger(value) && !Number.isSafeInteger(value)) {
257
+ throw new TypeError(
258
+ `${path} contains a number outside JavaScript's safe range; use a string for int64 and identifier values`
259
+ );
260
+ }
261
+ return;
262
+ }
263
+ if (typeof value !== "object") {
264
+ throw new TypeError(`${path} must contain only JSON data`);
265
+ }
266
+ if (seen.has(value)) {
267
+ throw new TypeError(`${path} must not contain circular references`);
268
+ }
269
+ seen.add(value);
270
+ if (Array.isArray(value)) {
271
+ value.forEach(
272
+ (item, index) => assertPlainJSONData(item, `${path}[${index}]`, seen)
273
+ );
274
+ } else {
275
+ const prototype = Object.getPrototypeOf(value);
276
+ if (prototype !== Object.prototype && prototype !== null) {
277
+ throw new TypeError(`${path} must contain only plain JSON objects`);
278
+ }
279
+ for (const [key, descriptor] of Object.entries(
280
+ Object.getOwnPropertyDescriptors(value)
281
+ )) {
282
+ if (!descriptor.enumerable) continue;
283
+ if (!("value" in descriptor)) {
284
+ throw new TypeError(`${path} must not contain accessors`);
285
+ }
286
+ assertPlainJSONData(descriptor.value, `${path}.${key}`, seen);
287
+ }
288
+ }
289
+ seen.delete(value);
290
+ }
291
+ function serializeStructuredArgs(value) {
292
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
293
+ throw new TypeError("args must be a JSON object");
294
+ }
295
+ assertPlainJSONData(value, "args", /* @__PURE__ */ new Set());
296
+ const serialized = JSON.stringify(value);
297
+ validateSerializedArgs(serialized);
298
+ return serialized;
299
+ }
300
+ function validateSerializedArgs(raw) {
301
+ const value = JSON.parse(raw);
302
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
303
+ throw new TypeError("args must be a JSON object");
304
+ }
305
+ validateJSONNumberTokens(raw);
306
+ }
307
+ function objectWithRawJSONField(fields, fieldName, rawValue) {
308
+ const entries = Object.entries(fields).filter(
309
+ ([key, value]) => value !== void 0 && (rawValue === void 0 || key !== fieldName)
310
+ );
311
+ if (rawValue !== void 0) {
312
+ validateSerializedArgs(rawValue);
313
+ entries.push([fieldName, rawValue]);
314
+ }
315
+ return `{${entries.map(([key, value]) => {
316
+ const encodedValue = key === fieldName && rawValue !== void 0 ? rawValue : JSON.stringify(value);
317
+ return `${JSON.stringify(key)}:${encodedValue}`;
318
+ }).join(",")}}`;
319
+ }
320
+
193
321
  // src/resources/run.ts
194
322
  var RunResource = class {
195
323
  constructor(client) {
@@ -198,13 +326,24 @@ var RunResource = class {
198
326
  client;
199
327
  async execute(params) {
200
328
  this.client._requireAuth();
329
+ return params.args === void 0 ? this.request(params) : this.request(params, serializeStructuredArgs(params.args));
330
+ }
331
+ /** @internal Used by the CLI to preserve the exact --args JSON text. */
332
+ async _executeSerializedArgs(params, serializedArgs) {
333
+ this.client._requireAuth();
334
+ return this.request(params, serializedArgs);
335
+ }
336
+ request(params, serializedArgs) {
337
+ const body = {
338
+ code: params.code,
339
+ entry_path: params.entry_path,
340
+ working_dir: params.working_dir,
341
+ args: params.args,
342
+ max_heap_size_mb: params.max_heap_size_mb
343
+ };
201
344
  return this.client._request("POST", "/api/v1/run", {
202
- body: {
203
- code: params.code,
204
- entry_path: params.entry_path,
205
- working_dir: params.working_dir,
206
- args: params.args,
207
- max_heap_size_mb: params.max_heap_size_mb
345
+ ...serializedArgs === void 0 ? { body } : {
346
+ jsonBody: objectWithRawJSONField(body, "args", serializedArgs)
208
347
  },
209
348
  timeoutMs: params.timeout_ms
210
349
  });
@@ -219,19 +358,30 @@ var DeployResource = class {
219
358
  client;
220
359
  async create(params) {
221
360
  this.client._requireAuth();
361
+ return params.args === void 0 ? this.createRequest(params) : this.createRequest(params, serializeStructuredArgs(params.args));
362
+ }
363
+ /** @internal Used by the CLI to preserve the exact --args JSON text. */
364
+ async _createSerializedArgs(params, serializedArgs) {
365
+ this.client._requireAuth();
366
+ return this.createRequest(params, serializedArgs);
367
+ }
368
+ createRequest(params, serializedArgs) {
369
+ const body = {
370
+ name: params.name,
371
+ path: params.path,
372
+ cron_expression: params.cron_expression,
373
+ args: params.args,
374
+ push_notify: params.push_notify,
375
+ max_heap_size_mb: params.max_heap_size_mb,
376
+ execution_timeout_seconds: params.execution_timeout_seconds,
377
+ run_as_user_id: params.run_as_user_id,
378
+ start_at: params.start_at,
379
+ end_at: params.end_at,
380
+ max_runs: params.max_runs
381
+ };
222
382
  return this.client._request("POST", "/api/v1/deploy/cronjob", {
223
- body: {
224
- name: params.name,
225
- path: params.path,
226
- cron_expression: params.cron_expression,
227
- args: params.args,
228
- push_notify: params.push_notify,
229
- max_heap_size_mb: params.max_heap_size_mb,
230
- execution_timeout_seconds: params.execution_timeout_seconds,
231
- run_as_user_id: params.run_as_user_id,
232
- start_at: params.start_at,
233
- end_at: params.end_at,
234
- max_runs: params.max_runs
383
+ ...serializedArgs === void 0 ? { body } : {
384
+ jsonBody: objectWithRawJSONField(body, "args", serializedArgs)
235
385
  }
236
386
  });
237
387
  }
@@ -250,9 +400,19 @@ var DeployResource = class {
250
400
  }
251
401
  async update(params) {
252
402
  this.client._requireAuth();
403
+ return params.args === void 0 ? this.updateRequest(params) : this.updateRequest(params, serializeStructuredArgs(params.args));
404
+ }
405
+ /** @internal Used by the CLI to preserve the exact --args JSON text. */
406
+ async _updateSerializedArgs(params, serializedArgs) {
407
+ this.client._requireAuth();
408
+ return this.updateRequest(params, serializedArgs);
409
+ }
410
+ updateRequest(params, serializedArgs) {
253
411
  const { id, ...body } = params;
254
412
  return this.client._request("PATCH", `/api/v1/deploy/cronjob/${id}`, {
255
- body
413
+ ...serializedArgs === void 0 ? { body } : {
414
+ jsonBody: objectWithRawJSONField(body, "args", serializedArgs)
415
+ }
256
416
  });
257
417
  }
258
418
  async delete(params) {
@@ -501,18 +661,53 @@ var FeedResource = class {
501
661
  }
502
662
  };
503
663
  function requireFeedID(id) {
504
- if (!Number.isInteger(id) || id <= 0) {
664
+ if (typeof id === "string") {
665
+ if (!/^[1-9]\d*$/.test(id)) {
666
+ throw new Error("feed id must be a positive integer");
667
+ }
668
+ return id;
669
+ }
670
+ if (!Number.isSafeInteger(id) || id <= 0) {
505
671
  throw new Error("feed id must be a positive integer");
506
672
  }
507
673
  return id;
508
674
  }
509
675
 
510
676
  // src/resources/automation.ts
677
+ var AUTOMATION_ALERT_DELIVERY_QUERY = `
678
+ query ToolkitAutomationAlertDelivery($automationId: ID!) {
679
+ automation(id: $automationId) {
680
+ alertDelivery {
681
+ isEnabled
682
+ alvaChannels { id }
683
+ email { isEnabled isAvailable address }
684
+ }
685
+ }
686
+ }
687
+ `.trim();
688
+ var UPDATE_AUTOMATION_ALERT_DELIVERY = `
689
+ mutation ToolkitUpdateAutomationAlertDelivery(
690
+ $input: UpdateAutomationAlertDeliveryInput!
691
+ $includeAlvaChannels: Boolean!
692
+ $includeEmail: Boolean!
693
+ ) {
694
+ updateAutomationAlertDelivery(input: $input) {
695
+ automation { id }
696
+ alertDelivery {
697
+ isEnabled
698
+ alvaChannels @include(if: $includeAlvaChannels) { id }
699
+ email @include(if: $includeEmail) { isEnabled isAvailable address }
700
+ }
701
+ }
702
+ }
703
+ `.trim();
511
704
  var AutomationResource = class {
512
705
  constructor(client) {
513
706
  this.client = client;
707
+ this.delivery = new AutomationAlertDeliveryResource(client);
514
708
  }
515
709
  client;
710
+ delivery;
516
711
  list(params = {}) {
517
712
  return this.client.feed.list(params);
518
713
  }
@@ -564,8 +759,93 @@ var AutomationResource = class {
564
759
  return this.client.release.feed(params);
565
760
  }
566
761
  };
762
+ var AutomationAlertDeliveryResource = class {
763
+ constructor(client) {
764
+ this.client = client;
765
+ }
766
+ client;
767
+ async get(params) {
768
+ this.client._requireAuth();
769
+ const id = requireAutomationIDString(params.id);
770
+ const data = await this.graphql(
771
+ AUTOMATION_ALERT_DELIVERY_QUERY,
772
+ { automationId: id }
773
+ );
774
+ const delivery = data.automation?.alertDelivery;
775
+ if (!delivery) {
776
+ throw new AlvaError(
777
+ "GRAPHQL_EMPTY_RESPONSE",
778
+ "GraphQL response did not include automation.alertDelivery",
779
+ 502
780
+ );
781
+ }
782
+ return delivery;
783
+ }
784
+ async update(params) {
785
+ this.client._requireAuth();
786
+ const id = requireAutomationIDString(params.id);
787
+ const includeAlvaChannels = params.alvaChannelIds !== void 0;
788
+ const includeEmail = params.emailEnabled !== void 0;
789
+ if (!includeAlvaChannels && !includeEmail) {
790
+ throw new Error(
791
+ "automation delivery update requires alvaChannelIds or emailEnabled"
792
+ );
793
+ }
794
+ const alvaChannelIds = includeAlvaChannels ? uniqueAlvaChannelIDs(params.alvaChannelIds ?? []) : void 0;
795
+ const data = await this.graphql(
796
+ UPDATE_AUTOMATION_ALERT_DELIVERY,
797
+ {
798
+ input: {
799
+ automationId: id,
800
+ ...includeAlvaChannels ? { alvaChannelIds } : {},
801
+ ...includeEmail ? { emailEnabled: params.emailEnabled } : {}
802
+ },
803
+ includeAlvaChannels,
804
+ includeEmail
805
+ }
806
+ );
807
+ const result = data.updateAutomationAlertDelivery;
808
+ if (!result) {
809
+ throw new AlvaError(
810
+ "GRAPHQL_EMPTY_RESPONSE",
811
+ "GraphQL response did not include updateAutomationAlertDelivery",
812
+ 502
813
+ );
814
+ }
815
+ return result;
816
+ }
817
+ async graphql(query, variables) {
818
+ const response = await this.client._request("POST", "/query", {
819
+ body: { query, variables }
820
+ });
821
+ if (!response) {
822
+ throw new AlvaError(
823
+ "GRAPHQL_EMPTY_RESPONSE",
824
+ "GraphQL response was empty",
825
+ 502
826
+ );
827
+ }
828
+ if (response.errors && response.errors.length > 0) {
829
+ throw new AlvaError(
830
+ "GRAPHQL_ERROR",
831
+ graphQLErrorMessage(response.errors),
832
+ 400,
833
+ { errors: response.errors }
834
+ );
835
+ }
836
+ if (!response.data) {
837
+ throw new AlvaError(
838
+ "GRAPHQL_EMPTY_RESPONSE",
839
+ "GraphQL response did not include data",
840
+ 502
841
+ );
842
+ }
843
+ return response.data;
844
+ }
845
+ };
567
846
  function requireAutomationID(id) {
568
- if (!Number.isInteger(id) || id <= 0) {
847
+ if (typeof id === "string") return requireAutomationIDString(id);
848
+ if (!Number.isSafeInteger(id) || id <= 0) {
569
849
  throw new Error("automation id must be a positive integer");
570
850
  }
571
851
  return id;
@@ -579,6 +859,23 @@ function requireAutomationIDString(id) {
579
859
  function hasAutomationUpdate(params) {
580
860
  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;
581
861
  }
862
+ function uniqueAlvaChannelIDs(values) {
863
+ const ids = [...new Set(values)];
864
+ for (const id of ids) {
865
+ if (!/^(0|[1-9]\d*)$/.test(id)) {
866
+ throw new Error(
867
+ "Alva channel ids must be positive integer strings or the agent sentinel 0"
868
+ );
869
+ }
870
+ }
871
+ return ids;
872
+ }
873
+ function graphQLErrorMessage(errors) {
874
+ const messages = errors.map(
875
+ (error) => error && typeof error.message === "string" && error.message ? error.message : void 0
876
+ ).filter((message) => Boolean(message));
877
+ return messages.length > 0 ? messages.join("; ") : "GraphQL request failed";
878
+ }
582
879
 
583
880
  // src/resources/playbooks.ts
584
881
  var PLAYBOOK_VISIBILITIES = [
@@ -2434,7 +2731,7 @@ query ToolkitCreditWalletItems($input: CreditWalletItemConnectionInput!) {
2434
2731
  }
2435
2732
  }
2436
2733
  `.trim();
2437
- function graphQLErrorMessage(errors) {
2734
+ function graphQLErrorMessage2(errors) {
2438
2735
  const messages = errors.map(
2439
2736
  (error) => typeof error.message === "string" && error.message ? error.message : void 0
2440
2737
  ).filter((message) => Boolean(message));
@@ -2489,7 +2786,7 @@ var CreditsResource = class {
2489
2786
  if (response.errors && response.errors.length > 0) {
2490
2787
  throw new AlvaError(
2491
2788
  "GRAPHQL_ERROR",
2492
- graphQLErrorMessage(response.errors),
2789
+ graphQLErrorMessage2(response.errors),
2493
2790
  400,
2494
2791
  { errors: response.errors }
2495
2792
  );
@@ -2646,7 +2943,7 @@ query ToolkitCompanyEarnings($ticker: String!, $input: CompanyEarningsInput) {
2646
2943
  }
2647
2944
  }
2648
2945
  `.trim();
2649
- function graphQLErrorMessage2(errors) {
2946
+ function graphQLErrorMessage3(errors) {
2650
2947
  const messages = errors.map(
2651
2948
  (error) => typeof error.message === "string" && error.message ? error.message : void 0
2652
2949
  ).filter((message) => Boolean(message));
@@ -2802,7 +3099,7 @@ var MarketsResource = class {
2802
3099
  if (response.errors && response.errors.length > 0) {
2803
3100
  throw new AlvaError(
2804
3101
  "GRAPHQL_ERROR",
2805
- graphQLErrorMessage2(response.errors),
3102
+ graphQLErrorMessage3(response.errors),
2806
3103
  400,
2807
3104
  { errors: response.errors }
2808
3105
  );
@@ -3636,11 +3933,12 @@ var installPlaybookRuntime = (targetWindow) => {
3636
3933
  };
3637
3934
 
3638
3935
  // src/index.ts
3639
- var VERSION = true ? "0.23.1" : "dev";
3936
+ var VERSION = true ? "0.25.0" : "dev";
3640
3937
  export {
3641
3938
  AlertsResource,
3642
3939
  AlvaClient,
3643
3940
  AlvaError,
3941
+ AutomationAlertDeliveryResource,
3644
3942
  AutomationResource,
3645
3943
  CliUsageError,
3646
3944
  MarketsResource,