@clocknext/sdk 0.13.0 → 0.14.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clocknext/sdk",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "Official ClockNext SDK — record usage signals and manage plans, purchases, invoices, customers, and billing from your backend.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/index.ts CHANGED
@@ -59,6 +59,12 @@ export type {
59
59
  UnitUsageRow,
60
60
  UnitUsageList,
61
61
  SerializedUnitUsage,
62
+ UsageLogPeriod,
63
+ UsageLogListParams,
64
+ UsageLogAppliedRule,
65
+ UsageLogOutcomeAttribution,
66
+ UsageLogEntry,
67
+ UsageLogPage,
62
68
  // workspace
63
69
  WorkspaceIdentity,
64
70
  Model,
@@ -17,6 +17,8 @@ import type {
17
17
  UnitTrackResult,
18
18
  UnitUsageList,
19
19
  UsageList,
20
+ UsageLogListParams,
21
+ UsageLogPage,
20
22
  WalletInput,
21
23
  } from "../types.js";
22
24
 
@@ -44,6 +46,7 @@ const SIGNAL_PATHS: Record<SignalType, string> = {
44
46
  const UNIT_PATH = "/api/v1/signal/unit";
45
47
  const COMPOSITE_COMPLETE_PATH = "/api/v1/signal/composite/complete";
46
48
  const OUTCOME_COMPLETE_PATH = "/api/v1/signal/outcome/complete";
49
+ const USAGE_LOG_PATH = "/api/v1/signal/usage-log";
47
50
 
48
51
  /**
49
52
  * Builds the request body for a typed Signal.
@@ -445,4 +448,36 @@ export class Signals {
445
448
  query: { customerId: params.customerId, limit: params.limit },
446
449
  });
447
450
  }
451
+
452
+ /**
453
+ * One customer's usage history, merged from every meter kind — credit,
454
+ * outcome, unit, wallet, and completed composite occurrences — into one
455
+ * feed sorted by time. `GET /api/v1/signal/usage-log`. Exactly the same
456
+ * rows the customer portal's own "Usage logs" table shows, so a caller can
457
+ * build against this instead of the portal UI.
458
+ *
459
+ * Cursor-paginated, NOT page-numbered: `limit` + `cursor` in, `nextCursor`
460
+ * out. Omit `cursor` for the first page; pass the previous page's
461
+ * `nextCursor` back to fetch the next one, and stop once it comes back
462
+ * `null`. Unlike offset pagination, the cost of a page never grows with how
463
+ * deep you've paged — each page only asks for rows strictly before the
464
+ * cursor's position.
465
+ *
466
+ * Read-only; unlike {@link track}/{@link unit}/etc., there is nothing to
467
+ * buffer or retry here.
468
+ */
469
+ usageLog(params: UsageLogListParams): Promise<UsageLogPage> {
470
+ return this.transport.request<UsageLogPage>({
471
+ method: "GET",
472
+ path: USAGE_LOG_PATH,
473
+ query: {
474
+ customerId: params.customerId,
475
+ limit: params.limit,
476
+ cursor: params.cursor,
477
+ period: params.period,
478
+ from: params.from,
479
+ to: params.to,
480
+ },
481
+ });
482
+ }
448
483
  }
package/src/types.ts CHANGED
@@ -385,6 +385,86 @@ export interface UsageList {
385
385
  logs: SerializedUsageLog[];
386
386
  }
387
387
 
388
+ // ---------------------------------------------------------------------------
389
+ // Usage log (mirrors GET /api/v1/signal/usage-log)
390
+ // ---------------------------------------------------------------------------
391
+
392
+ /** A preset time window, or `"custom"` for an explicit `from`/`to` range. */
393
+ export type UsageLogPeriod = "all" | "today" | "7d" | "30d" | "90d" | "custom";
394
+
395
+ export interface UsageLogListParams {
396
+ customerId: string;
397
+ /** Default 50, max 200. */
398
+ limit?: number;
399
+ /** Pass back a previous page's `nextCursor` to fetch the next page. Omit for
400
+ * the first page. */
401
+ cursor?: string;
402
+ /** Default `"all"`. Pair `"custom"` with `from`/`to`. */
403
+ period?: UsageLogPeriod;
404
+ /** `YYYY-MM-DD`, only used when `period` is `"custom"`. */
405
+ from?: string;
406
+ /** `YYYY-MM-DD`, only used when `period` is `"custom"`. */
407
+ to?: string;
408
+ }
409
+
410
+ export interface UsageLogAppliedRule {
411
+ ruleId: string | null;
412
+ ruleName: string;
413
+ mode: "MARGIN" | "CREDIT" | null;
414
+ marginPercentApplied: number | null;
415
+ /** Formatted, same as {@link UsageLogEntry.credit}'s `amount`. */
416
+ creditsApplied: string;
417
+ }
418
+
419
+ export interface UsageLogOutcomeAttribution {
420
+ /** "2 / 4" — the step this call performed, and the outcome's total step
421
+ * count. Not a progress target: a step may repeat within a run. */
422
+ step: string | null;
423
+ stepName: string | null;
424
+ runId: string | null;
425
+ /** How many signals the run holds. */
426
+ runSignals: number;
427
+ /** True once the run closed (`complete: true` was sent for it). */
428
+ closedRun: boolean;
429
+ }
430
+
431
+ /**
432
+ * One row of a customer's usage history, merged from every meter kind
433
+ * (`UsageLog`, `UnitUsageLog`, completed composite occurrences) into one
434
+ * feed and sorted by time — the same rows the customer portal's own "Usage
435
+ * logs" table shows. `model`, `credit`, `outcome`, `unit` and `composite` are
436
+ * each `null` unless `kind` matches that group.
437
+ */
438
+ export interface UsageLogEntry {
439
+ id: string;
440
+ createdAt: string;
441
+ kind: "credit" | "outcome" | "unit" | "wallet" | "composite";
442
+ status: SignalStatus;
443
+ typeName: string | null;
444
+ member: string | null;
445
+ model: { name: string | null; provider: string | null } | null;
446
+ usage: { inputTokens: number; outputTokens: number; cacheTokens: number };
447
+ /** Formatted USD strings (e.g. `"$0.0064"`). */
448
+ cost: { providedCost: string; customerCost: string };
449
+ /** Populated only when `kind === "credit"`. `amount` is a formatted credit
450
+ * value, honouring the org's credit-decimals display setting. */
451
+ credit: { amount: string; appliedRules: UsageLogAppliedRule[] } | null;
452
+ /** Populated only when `kind === "outcome"`. */
453
+ outcome: UsageLogOutcomeAttribution | null;
454
+ /** Populated only when `kind === "unit"`. `quantity` is formatted. */
455
+ unit: { quantity: string } | null;
456
+ /** Populated only when `kind === "composite"`. `amount` is a formatted USD
457
+ * price — the composite's flat price, read live, not stored per row. */
458
+ composite: { amount: string } | null;
459
+ }
460
+
461
+ export interface UsageLogPage {
462
+ usageLogs: UsageLogEntry[];
463
+ /** Pass this back as `cursor` to fetch the next page; `null` on the last
464
+ * page. */
465
+ nextCursor: string | null;
466
+ }
467
+
388
468
  // ---------------------------------------------------------------------------
389
469
  // Customers (mirrors src/lib/customers/api.ts)
390
470
  // ---------------------------------------------------------------------------