@ornncompute/cli 1.0.221 → 1.0.222

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ornncompute/cli",
3
- "version": "1.0.221",
3
+ "version": "1.0.222",
4
4
  "description": "Command-line interface for Ornn compute access workflows.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -3483,10 +3483,34 @@ function listAccounts(request, input = {}, signal) {
3483
3483
  // ../../packages/capabilities/dist/capabilities/credits.js
3484
3484
  import { z as z17 } from "zod";
3485
3485
 
3486
+ // ../../packages/capabilities/dist/ledger-amount.js
3487
+ function parsePostingCents(value) {
3488
+ if (typeof value === "number" && !Number.isSafeInteger(value))
3489
+ return null;
3490
+ const match = /^(-?\d+)(?:\.(\d+))?$/.exec(String(value));
3491
+ if (!match)
3492
+ return null;
3493
+ const fraction = match[2]?.replace(/0+$/, "") ?? "";
3494
+ return { units: BigInt(match[1] + fraction), scale: fraction.length };
3495
+ }
3496
+
3486
3497
  // ../../packages/capabilities/dist/credit-balance.js
3498
+ function ledgerAccountBalanceCents(accounts, role, currency = "usd") {
3499
+ const amounts = accounts.filter((row) => row.account.account_role === role && row.account.currency === currency).map((row) => parsePostingCents(row.balance_cents));
3500
+ if (!amounts.every((amount) => amount !== null))
3501
+ return null;
3502
+ const scale = amounts.reduce((max, amount) => Math.max(max, amount.scale), 0);
3503
+ const sum = amounts.reduce((total, { units, scale: amountScale }) => total + units * BigInt(10) ** BigInt(scale - amountScale), BigInt(0));
3504
+ const digits = String(sum < 0 ? -sum : sum).padStart(scale + 1, "0");
3505
+ const whole = scale ? digits.slice(0, -scale) : digits;
3506
+ const fraction = scale ? digits.slice(-scale).replace(/0+$/, "") : "";
3507
+ return `${sum < 0 ? "-" : ""}${whole}${fraction ? `.${fraction}` : ""}`;
3508
+ }
3487
3509
  function availableCreditCents(accounts, currency = "usd") {
3488
- const sum = accounts.filter((row) => row.account.account_role === "ORNN_CREDIT" && row.account.currency === currency).reduce((total, row) => total + Number(row.balance_cents), 0);
3489
- return Math.max(0, -sum);
3510
+ const balance = ledgerAccountBalanceCents(accounts, "ORNN_CREDIT", currency);
3511
+ if (balance === null)
3512
+ return null;
3513
+ return balance.startsWith("-") ? balance.slice(1) : "0";
3490
3514
  }
3491
3515
 
3492
3516
  // ../../packages/capabilities/dist/capabilities/credits.js
@@ -3510,10 +3534,13 @@ var creditBalanceCapability = defineCapability({
3510
3534
  throw new Error("Admin role required.");
3511
3535
  const { organizationId } = z17.object({ organizationId: z17.uuid() }).parse(input);
3512
3536
  const { accounts } = await listAccounts(ctx.fetch, { organization_id: organizationId });
3537
+ const balance = availableCreditCents(accounts);
3538
+ if (balance === null)
3539
+ throw new Error("Could not read the available Ornn credit balance.");
3513
3540
  return {
3514
3541
  organizationId,
3515
3542
  currency: "usd",
3516
- availableCreditCents: availableCreditCents(accounts)
3543
+ availableCreditCents: balance
3517
3544
  };
3518
3545
  }
3519
3546
  });