@clocknext/sdk 0.12.1 → 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/dist/index.d.ts CHANGED
@@ -123,7 +123,14 @@ interface WalletSignal extends SignalBase {
123
123
  /**
124
124
  * Records one step of a multi-call outcome run. The `agentKey` identifies the
125
125
  * step (and the outcome); `runId` groups every signal of a single run, so a step
126
- * may repeat freely. The run bills once, when the caller marks it `complete`.
126
+ * may repeat freely. The run bills once, when the caller closes it with
127
+ * `signals.completeOutcome()` — a SEPARATE, dedicated call, never a flag on a
128
+ * step signal.
129
+ *
130
+ * There is no `complete` field here. `POST /api/v1/signal/outcome` (what this
131
+ * type posts to) now accepts ONLY the step shape and rejects a body that also
132
+ * carries `complete` — see {@link OutcomeCompletionInput} /
133
+ * `signals.completeOutcome()`, its own dedicated route.
127
134
  */
128
135
  interface OutcomeSignal extends SignalBase {
129
136
  type: "outcome";
@@ -135,17 +142,6 @@ interface OutcomeSignal extends SignalBase {
135
142
  * outcome). Opaque to ClockNext — any string you can correlate; unique per org.
136
143
  */
137
144
  runId: string;
138
- /**
139
- * Set `true` on the LAST step signal to declare the run finished — completion
140
- * is never inferred from step coverage. Sending it twice is a safe no-op and
141
- * can't bill again.
142
- *
143
- * This closes the RUN only. If the signal also carries a `composite`, that
144
- * occurrence stays open and unbilled until you call
145
- * `signals.completeComposite()` — the two completions are separate, and
146
- * neither implies the other.
147
- */
148
- complete?: boolean;
149
145
  /** Optional composite this signal belongs to. See {@link CompositeTag}. */
150
146
  composite?: CompositeTag;
151
147
  }
@@ -154,6 +150,56 @@ type Signal = CreditSignal | WalletSignal | OutcomeSignal;
154
150
  type CreditInput = Omit<CreditSignal, "type">;
155
151
  type WalletInput = Omit<WalletSignal, "type">;
156
152
  type OutcomeInput = Omit<OutcomeSignal, "type">;
153
+ /**
154
+ * Input for `signals.completeOutcome()` — closes ONE outcome run, the outcome
155
+ * mirror of {@link CompositeCompletionInput}. No `agentKey`, no `usage`: this
156
+ * closes an EXISTING run that an earlier `signals.outcome()` step already
157
+ * opened, so there is no step behind this call and nothing here to price.
158
+ *
159
+ * This closes the RUN only. If a step of this run also carried a `composite`,
160
+ * that occurrence stays open and unbilled until you separately call
161
+ * `signals.completeComposite()` — the two completions are independent, and
162
+ * neither implies the other.
163
+ */
164
+ interface OutcomeCompletionInput {
165
+ customerId: string;
166
+ /** The same `runId` the run's step signals were sent with. */
167
+ runId: string;
168
+ /** Optional member (email) this completion is attributed to. */
169
+ member?: string;
170
+ /** Idempotency key, auto-generated if omitted. Sending `complete` twice for
171
+ * the same run is already a safe no-op server-side, so this only collapses
172
+ * a retry into one attempt rather than protecting against a double bill. */
173
+ idempotencyKey?: string;
174
+ }
175
+ /**
176
+ * The result of `signals.completeOutcome()`.
177
+ *
178
+ * Unlike {@link CompositeCompletionResult}, `ALREADY_COMPLETED` is NOT what a
179
+ * normal second ping for the same run gets back — that is rejected outright
180
+ * (a permanent error: the run has already been billed, and completing it again
181
+ * would bill it twice). `ALREADY_COMPLETED` is reserved for the much rarer case
182
+ * of two concurrent deliveries of the exact same request (e.g. a client-side
183
+ * retry racing the original), which really is a harmless echo.
184
+ */
185
+ interface OutcomeCompletionResult {
186
+ /** How far the ping got. Never `"buffered"` — completions are always sent
187
+ * immediately. See {@link TrackResult.status}. */
188
+ delivery: Extract<SignalDelivery, "accepted" | "recorded">;
189
+ /** True when the ping was accepted but not yet applied (`202`). Prefer
190
+ * {@link OutcomeCompletionResult.delivery}. */
191
+ queued: boolean;
192
+ /** `COMPLETED` on the first close, `ALREADY_COMPLETED` only on the rare
193
+ * concurrent-replay case described above. Absent when the ping was queued. */
194
+ status?: "COMPLETED" | "ALREADY_COMPLETED";
195
+ /** The priced usage log, when the server closed the run inline. `null` on a
196
+ * `202` hand-off. */
197
+ usageLog?: SerializedUsageLog | null;
198
+ /** The durable inbox id — see {@link TrackResult.rawId}. */
199
+ rawId?: string;
200
+ /** The queue message id, present only on a `202` hand-off. */
201
+ messageId?: string;
202
+ }
157
203
  interface SerializedRuleApplication {
158
204
  ruleId: string | null;
159
205
  ruleName: string;
@@ -310,6 +356,93 @@ interface UsageList {
310
356
  };
311
357
  logs: SerializedUsageLog[];
312
358
  }
359
+ /** A preset time window, or `"custom"` for an explicit `from`/`to` range. */
360
+ type UsageLogPeriod = "all" | "today" | "7d" | "30d" | "90d" | "custom";
361
+ interface UsageLogListParams {
362
+ customerId: string;
363
+ /** Default 50, max 200. */
364
+ limit?: number;
365
+ /** Pass back a previous page's `nextCursor` to fetch the next page. Omit for
366
+ * the first page. */
367
+ cursor?: string;
368
+ /** Default `"all"`. Pair `"custom"` with `from`/`to`. */
369
+ period?: UsageLogPeriod;
370
+ /** `YYYY-MM-DD`, only used when `period` is `"custom"`. */
371
+ from?: string;
372
+ /** `YYYY-MM-DD`, only used when `period` is `"custom"`. */
373
+ to?: string;
374
+ }
375
+ interface UsageLogAppliedRule {
376
+ ruleId: string | null;
377
+ ruleName: string;
378
+ mode: "MARGIN" | "CREDIT" | null;
379
+ marginPercentApplied: number | null;
380
+ /** Formatted, same as {@link UsageLogEntry.credit}'s `amount`. */
381
+ creditsApplied: string;
382
+ }
383
+ interface UsageLogOutcomeAttribution {
384
+ /** "2 / 4" — the step this call performed, and the outcome's total step
385
+ * count. Not a progress target: a step may repeat within a run. */
386
+ step: string | null;
387
+ stepName: string | null;
388
+ runId: string | null;
389
+ /** How many signals the run holds. */
390
+ runSignals: number;
391
+ /** True once the run closed (`complete: true` was sent for it). */
392
+ closedRun: boolean;
393
+ }
394
+ /**
395
+ * One row of a customer's usage history, merged from every meter kind
396
+ * (`UsageLog`, `UnitUsageLog`, completed composite occurrences) into one
397
+ * feed and sorted by time — the same rows the customer portal's own "Usage
398
+ * logs" table shows. `model`, `credit`, `outcome`, `unit` and `composite` are
399
+ * each `null` unless `kind` matches that group.
400
+ */
401
+ interface UsageLogEntry {
402
+ id: string;
403
+ createdAt: string;
404
+ kind: "credit" | "outcome" | "unit" | "wallet" | "composite";
405
+ status: SignalStatus;
406
+ typeName: string | null;
407
+ member: string | null;
408
+ model: {
409
+ name: string | null;
410
+ provider: string | null;
411
+ } | null;
412
+ usage: {
413
+ inputTokens: number;
414
+ outputTokens: number;
415
+ cacheTokens: number;
416
+ };
417
+ /** Formatted USD strings (e.g. `"$0.0064"`). */
418
+ cost: {
419
+ providedCost: string;
420
+ customerCost: string;
421
+ };
422
+ /** Populated only when `kind === "credit"`. `amount` is a formatted credit
423
+ * value, honouring the org's credit-decimals display setting. */
424
+ credit: {
425
+ amount: string;
426
+ appliedRules: UsageLogAppliedRule[];
427
+ } | null;
428
+ /** Populated only when `kind === "outcome"`. */
429
+ outcome: UsageLogOutcomeAttribution | null;
430
+ /** Populated only when `kind === "unit"`. `quantity` is formatted. */
431
+ unit: {
432
+ quantity: string;
433
+ } | null;
434
+ /** Populated only when `kind === "composite"`. `amount` is a formatted USD
435
+ * price — the composite's flat price, read live, not stored per row. */
436
+ composite: {
437
+ amount: string;
438
+ } | null;
439
+ }
440
+ interface UsageLogPage {
441
+ usageLogs: UsageLogEntry[];
442
+ /** Pass this back as `cursor` to fetch the next page; `null` on the last
443
+ * page. */
444
+ nextCursor: string | null;
445
+ }
313
446
  interface ApiCustomer {
314
447
  id: string;
315
448
  slug: string;
@@ -902,17 +1035,21 @@ interface WalletEntryInput {
902
1035
  notes?: string;
903
1036
  }
904
1037
  /** Grant (positive) or claw back (negative) a credit balance. `delta` must be a
905
- * non-zero whole number. The credit is addressed by its stable `agentKey`. */
1038
+ * non-zero whole number. The credit is addressed by its stable `agentKey`. A
1039
+ * reduce is never rejected or floored for going below 0 — the resulting
1040
+ * balance may read negative, same as a real-usage overdraft already can. */
906
1041
  interface CreditAdjustInput {
907
1042
  /** The credit's stable agent key (the same key used to record usage). */
908
1043
  agentKey: string;
909
1044
  delta: number;
910
1045
  notes?: string;
911
1046
  }
912
- /** Grant/claw an outcome balance. `delta` is a non-zero whole number; floored at
913
- * 0. The outcome is addressed here by a STEP's `agentKey`, which resolves to its
1047
+ /** Grant/claw an outcome balance. `delta` is a non-zero whole number. The
1048
+ * outcome is addressed here by a STEP's `agentKey`, which resolves to its
914
1049
  * parent outcome — NOT by the outcome's own `agentKey` ({@link Outcome.agentKey}),
915
- * which this endpoint does not accept. */
1050
+ * which this endpoint does not accept. A reduce is never rejected or floored
1051
+ * for going below 0 — the resulting balance may read negative, same as a
1052
+ * real-usage overdraft already can. */
916
1053
  interface OutcomeAdjustInput {
917
1054
  /** An outcome STEP's stable agent key — identifies the parent outcome. Passing
918
1055
  * the outcome's own `agentKey` here fails with `422`. */
@@ -920,14 +1057,35 @@ interface OutcomeAdjustInput {
920
1057
  delta: number;
921
1058
  notes?: string;
922
1059
  }
923
- /** Grant/claw a unit balance. `delta` is non-zero (fractional allowed); floored
924
- * at 0. The unit is addressed by its stable `agentKey`. */
1060
+ /** Grant/claw a unit balance. `delta` is non-zero (fractional allowed). The
1061
+ * unit is addressed by its stable `agentKey`. A reduce is never rejected or
1062
+ * floored for going below 0 — the resulting balance may read negative, same
1063
+ * as a real-usage overdraft already can. */
925
1064
  interface UnitAdjustInput {
926
1065
  /** The unit's stable agent key (the same key used to record usage). */
927
1066
  agentKey: string;
928
1067
  delta: number;
929
1068
  notes?: string;
930
1069
  }
1070
+ /**
1071
+ * Grant (positive) or claw back (negative) slots on a customer's ADVANCE
1072
+ * composite (pricing metric) pool for the CURRENT billing cycle. `delta` is a
1073
+ * non-zero whole number. A reduce is never rejected or floored for going below
1074
+ * 0 — same as credit/outcome/unit adjustments; the resulting pool may read
1075
+ * negative. Fails if the customer has no active ADVANCE purchase component
1076
+ * selling this composite.
1077
+ */
1078
+ interface CompositeAdjustInput {
1079
+ /**
1080
+ * The composite's stable identifier — its `refId` (see {@link Composite.refId}),
1081
+ * the same value signals tag it with, NOT `agentKey` (a composite has no
1082
+ * `agentKey` column of its own). Named `agentKey` on the wire only so the
1083
+ * four adjust endpoints share one request shape.
1084
+ */
1085
+ agentKey: string;
1086
+ delta: number;
1087
+ notes?: string;
1088
+ }
931
1089
  /** A per-model token bundle attached to a credit or outcome step. */
932
1090
  interface ModelBundleEntry {
933
1091
  orgModelId: string;
@@ -1660,8 +1818,11 @@ declare class Customers {
1660
1818
  transaction: WalletTransaction;
1661
1819
  balanceUsd: number;
1662
1820
  }>;
1663
- /** Grant or claw back a credit balance. `POST …/:id/credits/adjust`. */
1664
- adjustCredit(id: string, input: CreditAdjustInput): Promise<void>;
1821
+ /** Grant or claw back a credit balance. Returns the remaining balance.
1822
+ * `POST …/:id/credits/adjust`. */
1823
+ adjustCredit(id: string, input: CreditAdjustInput): Promise<{
1824
+ remaining: number;
1825
+ }>;
1665
1826
  /** Grant or claw back an outcome balance. Returns the remaining balance. */
1666
1827
  adjustOutcome(id: string, input: OutcomeAdjustInput): Promise<{
1667
1828
  remaining: number;
@@ -1670,6 +1831,14 @@ declare class Customers {
1670
1831
  adjustUnit(id: string, input: UnitAdjustInput): Promise<{
1671
1832
  remaining: number;
1672
1833
  }>;
1834
+ /**
1835
+ * Grant or claw back slots on a customer's ADVANCE composite pool for the
1836
+ * current billing cycle. Returns the resulting `remaining` slot count.
1837
+ * `POST …/:id/composites/adjust`.
1838
+ */
1839
+ adjustComposite(id: string, input: CompositeAdjustInput): Promise<{
1840
+ remaining: number;
1841
+ }>;
1673
1842
  }
1674
1843
 
1675
1844
  /**
@@ -1956,6 +2125,25 @@ declare class Signals {
1956
2125
  outcome(input: OutcomeInput, opts?: {
1957
2126
  wait?: boolean;
1958
2127
  }): Promise<TrackResult>;
2128
+ /**
2129
+ * Close ONE outcome run — declares the workflow finished and bills it. The
2130
+ * outcome mirror of {@link Signals.completeComposite}, on its own dedicated
2131
+ * route: `POST /api/v1/signal/outcome/complete`.
2132
+ *
2133
+ * An outcome bills per COMPLETION, never per step, and `signals.outcome()`
2134
+ * never closes one — it accepts only the step shape. Declaring the workflow
2135
+ * finished is always this separate, explicit, contentless call.
2136
+ *
2137
+ * This closes the RUN only. If a step of this run also carried a
2138
+ * `composite`, that occurrence stays open and unbilled until you separately
2139
+ * call {@link Signals.completeComposite} — the two completions are
2140
+ * independent, and neither implies the other.
2141
+ *
2142
+ * Sent immediately, never buffered. A genuinely second, independent call for
2143
+ * an already-closed run is a PERMANENT error (it would bill the run twice),
2144
+ * not a safe no-op — see {@link OutcomeCompletionResult}.
2145
+ */
2146
+ completeOutcome(input: OutcomeCompletionInput): Promise<OutcomeCompletionResult>;
1959
2147
  /**
1960
2148
  * Recent usage logs + totals for a customer. `GET /api/v1/usage`.
1961
2149
  *
@@ -1993,13 +2181,13 @@ declare class Signals {
1993
2181
  * arrive in any order and the occurrence stays open and unbilled until you
1994
2182
  * say otherwise. Put it on the line before your own `return`.
1995
2183
  *
1996
- * EVERY composite needs this, outcome-backed ones included. An outcome's own
1997
- * `complete: true` closes its RUN and nothing else — it does not close the
1998
- * composite occurrence the run was tagged into, whatever mix of credits,
1999
- * outcomes and units that composite wraps. (Earlier versions of this SDK
2000
- * documented the opposite. Believing it means the occurrence stays ONGOING
2001
- * forever and is never billed, so if you tag outcome signals with a
2002
- * `composite`, you must still call this.)
2184
+ * EVERY composite needs this, outcome-backed ones included. `completeOutcome()`
2185
+ * closes its RUN and nothing else — it does not close the composite
2186
+ * occurrence the run was tagged into, whatever mix of credits, outcomes and
2187
+ * units that composite wraps. (Earlier versions of this SDK documented the
2188
+ * opposite. Believing it means the occurrence stays ONGOING forever and is
2189
+ * never billed, so if you tag outcome signals with a `composite`, you must
2190
+ * still call this.)
2003
2191
  *
2004
2192
  * Closing twice is safe: the second call answers `ALREADY_COMPLETED` and
2005
2193
  * cannot bill again. Calling it EARLY is also safe — before every entitlement
@@ -2018,6 +2206,24 @@ declare class Signals {
2018
2206
  customerId: string;
2019
2207
  limit?: number;
2020
2208
  }): Promise<UnitUsageList>;
2209
+ /**
2210
+ * One customer's usage history, merged from every meter kind — credit,
2211
+ * outcome, unit, wallet, and completed composite occurrences — into one
2212
+ * feed sorted by time. `GET /api/v1/signal/usage-log`. Exactly the same
2213
+ * rows the customer portal's own "Usage logs" table shows, so a caller can
2214
+ * build against this instead of the portal UI.
2215
+ *
2216
+ * Cursor-paginated, NOT page-numbered: `limit` + `cursor` in, `nextCursor`
2217
+ * out. Omit `cursor` for the first page; pass the previous page's
2218
+ * `nextCursor` back to fetch the next one, and stop once it comes back
2219
+ * `null`. Unlike offset pagination, the cost of a page never grows with how
2220
+ * deep you've paged — each page only asks for rows strictly before the
2221
+ * cursor's position.
2222
+ *
2223
+ * Read-only; unlike {@link track}/{@link unit}/etc., there is nothing to
2224
+ * buffer or retry here.
2225
+ */
2226
+ usageLog(params: UsageLogListParams): Promise<UsageLogPage>;
2021
2227
  }
2022
2228
 
2023
2229
  /**
@@ -2211,4 +2417,4 @@ declare class NetworkError extends ClockNextError {
2211
2417
  });
2212
2418
  }
2213
2419
 
2214
- export { type AddMemberInput, AllowanceError, type ApiCustomer, AuthError, type BillingCycle, type BillingMode, ClockNext, type ClockNextConfig, ClockNextError, type ClockNextErrorCode, type ComponentType, type Composite, type CompositeCatalogueRef, type CompositeCompletionInput, type CompositeCompletionResult, type CompositeComponentInput, type CompositeTag, ConflictError, type CostPoint, type CostStats, type CreateCompositeInput, type CreateCreditInput, type CreateCustomerInput, type CreateOutcomeInput, type CreatePlanInput, type CreatePurchaseInput, type CreateUnitInput, type Credit, type CreditAdjustInput, type CreditAnalytics, type CreditBalance, type CreditComponentInput, type CreditDetail, type CreditInput, type CreditSignal, type CustomerBalances, type CustomerList, type CustomerPlan, type CustomerPlanDetails, type CustomerProfileInput, type DropReason, type FetchLike, type FlatComponentInput, type FlatUnitInput, ForbiddenError, type Invoice, type InvoiceDetail, type InvoiceLineItem, type InvoiceListRow, type InvoiceParty, type InvoicePlanSnapshot, type InvoiceStatus, type Logger, type Member, type MemberProfile, type Model, type ModelBundleEntry, NetworkError, NotFoundError, type Outcome, type OutcomeAdjustInput, type OutcomeAnalytics, type OutcomeBalance, type OutcomeComponentInput, type OutcomeDetail, type OutcomeInput, type OutcomeSignal, type OutcomeStepInput, type PayLink, type PaymentDetail, type PaymentRow, type Plan, type PlanComponent, type PlanComponentInput, type PlanDetail, PlanError, type PlanKind, type PlanRef, type PlanType, type PortalToken, type PortalTokenInput, type Purchase, type PurchaseDetail, type PurchaseStatus, RateLimitError, type RemovedMember, type SerializedOutcomeAttribution, type SerializedRuleApplication, type SerializedUnitUsage, type SerializedUsageLog, ServerError, type Signal, type SignalDelivery, type SignalStatus, type SignalType, type ThresholdKind, type TieredUnitInput, type Tokens, type TrackResult, type Unit, type UnitAdjustInput, type UnitBalance, type UnitComponentInput, type UnitCustomerRow, type UnitDetail, type UnitInput, type UnitPricingType, type UnitTier, type UnitTierInput, type UnitTrackResult, type UnitUsageList, type UnitUsageRow, type UpdateCreditInput, type UpdateMemberInput, type UpdateOutcomeInput, type UpdatePlanInput, type UpdateUnitInput, type UpsertWebhookInput, type UsageBalance, type UsageList, ValidationError, type WalletComponentInput, type WalletEntitlementKind, type WalletEntryInput, type WalletInput, type WalletList, type WalletSignal, type WalletTransaction, type WalletTransactionSource, type WalletTransactionType, type Webhook, type WorkspaceIdentity, signalToWire };
2420
+ export { type AddMemberInput, AllowanceError, type ApiCustomer, AuthError, type BillingCycle, type BillingMode, ClockNext, type ClockNextConfig, ClockNextError, type ClockNextErrorCode, type ComponentType, type Composite, type CompositeAdjustInput, type CompositeCatalogueRef, type CompositeCompletionInput, type CompositeCompletionResult, type CompositeComponentInput, type CompositeTag, ConflictError, type CostPoint, type CostStats, type CreateCompositeInput, type CreateCreditInput, type CreateCustomerInput, type CreateOutcomeInput, type CreatePlanInput, type CreatePurchaseInput, type CreateUnitInput, type Credit, type CreditAdjustInput, type CreditAnalytics, type CreditBalance, type CreditComponentInput, type CreditDetail, type CreditInput, type CreditSignal, type CustomerBalances, type CustomerList, type CustomerPlan, type CustomerPlanDetails, type CustomerProfileInput, type DropReason, type FetchLike, type FlatComponentInput, type FlatUnitInput, ForbiddenError, type Invoice, type InvoiceDetail, type InvoiceLineItem, type InvoiceListRow, type InvoiceParty, type InvoicePlanSnapshot, type InvoiceStatus, type Logger, type Member, type MemberProfile, type Model, type ModelBundleEntry, NetworkError, NotFoundError, type Outcome, type OutcomeAdjustInput, type OutcomeAnalytics, type OutcomeBalance, type OutcomeCompletionInput, type OutcomeCompletionResult, type OutcomeComponentInput, type OutcomeDetail, type OutcomeInput, type OutcomeSignal, type OutcomeStepInput, type PayLink, type PaymentDetail, type PaymentRow, type Plan, type PlanComponent, type PlanComponentInput, type PlanDetail, PlanError, type PlanKind, type PlanRef, type PlanType, type PortalToken, type PortalTokenInput, type Purchase, type PurchaseDetail, type PurchaseStatus, RateLimitError, type RemovedMember, type SerializedOutcomeAttribution, type SerializedRuleApplication, type SerializedUnitUsage, type SerializedUsageLog, ServerError, type Signal, type SignalDelivery, type SignalStatus, type SignalType, type ThresholdKind, type TieredUnitInput, type Tokens, type TrackResult, type Unit, type UnitAdjustInput, type UnitBalance, type UnitComponentInput, type UnitCustomerRow, type UnitDetail, type UnitInput, type UnitPricingType, type UnitTier, type UnitTierInput, type UnitTrackResult, type UnitUsageList, type UnitUsageRow, type UpdateCreditInput, type UpdateMemberInput, type UpdateOutcomeInput, type UpdatePlanInput, type UpdateUnitInput, type UpsertWebhookInput, type UsageBalance, type UsageList, type UsageLogAppliedRule, type UsageLogEntry, type UsageLogListParams, type UsageLogOutcomeAttribution, type UsageLogPage, type UsageLogPeriod, ValidationError, type WalletComponentInput, type WalletEntitlementKind, type WalletEntryInput, type WalletInput, type WalletList, type WalletSignal, type WalletTransaction, type WalletTransactionSource, type WalletTransactionType, type Webhook, type WorkspaceIdentity, signalToWire };
package/dist/index.js CHANGED
@@ -326,7 +326,7 @@ function sleep(ms) {
326
326
  }
327
327
 
328
328
  // src/http.ts
329
- var SDK_VERSION = "0.12.1" ;
329
+ var SDK_VERSION = "0.14.0" ;
330
330
  var Transport = class {
331
331
  constructor(cfg) {
332
332
  this.cfg = cfg;
@@ -702,13 +702,15 @@ var Customers = class {
702
702
  // (PATCH …/:id) — there is no separate currency endpoint. Credit-type
703
703
  // assignment has no public API.
704
704
  // --- Balance adjustments -------------------------------------------------
705
- /** Grant or claw back a credit balance. `POST …/:id/credits/adjust`. */
705
+ /** Grant or claw back a credit balance. Returns the remaining balance.
706
+ * `POST …/:id/credits/adjust`. */
706
707
  async adjustCredit(id, input) {
707
- await this.transport.request({
708
+ const res = await this.transport.request({
708
709
  method: "POST",
709
710
  path: `/api/v1/customers/${encodeURIComponent(id)}/credits/adjust`,
710
711
  body: input
711
712
  });
713
+ return { remaining: res.remaining };
712
714
  }
713
715
  /** Grant or claw back an outcome balance. Returns the remaining balance. */
714
716
  async adjustOutcome(id, input) {
@@ -728,6 +730,19 @@ var Customers = class {
728
730
  });
729
731
  return { remaining: res.remaining };
730
732
  }
733
+ /**
734
+ * Grant or claw back slots on a customer's ADVANCE composite pool for the
735
+ * current billing cycle. Returns the resulting `remaining` slot count.
736
+ * `POST …/:id/composites/adjust`.
737
+ */
738
+ async adjustComposite(id, input) {
739
+ const res = await this.transport.request({
740
+ method: "POST",
741
+ path: `/api/v1/customers/${encodeURIComponent(id)}/composites/adjust`,
742
+ body: input
743
+ });
744
+ return { remaining: res.remaining };
745
+ }
731
746
  };
732
747
 
733
748
  // src/resources/invoices.ts
@@ -1013,6 +1028,8 @@ var SIGNAL_PATHS = {
1013
1028
  };
1014
1029
  var UNIT_PATH = "/api/v1/signal/unit";
1015
1030
  var COMPOSITE_COMPLETE_PATH = "/api/v1/signal/composite/complete";
1031
+ var OUTCOME_COMPLETE_PATH = "/api/v1/signal/outcome/complete";
1032
+ var USAGE_LOG_PATH = "/api/v1/signal/usage-log";
1016
1033
  function signalToWire(signal) {
1017
1034
  const t = signal.tokens;
1018
1035
  const body = {
@@ -1033,7 +1050,6 @@ function signalToWire(signal) {
1033
1050
  }
1034
1051
  if (signal.type === "outcome") {
1035
1052
  body.runId = signal.runId;
1036
- if (signal.complete != null) body.complete = signal.complete;
1037
1053
  }
1038
1054
  if (signal.type !== "wallet" && signal.composite) {
1039
1055
  body.composite = { [signal.composite.ref]: signal.composite.value };
@@ -1103,6 +1119,48 @@ var Signals = class {
1103
1119
  outcome(input, opts) {
1104
1120
  return this.track({ ...input, type: "outcome" }, opts);
1105
1121
  }
1122
+ /**
1123
+ * Close ONE outcome run — declares the workflow finished and bills it. The
1124
+ * outcome mirror of {@link Signals.completeComposite}, on its own dedicated
1125
+ * route: `POST /api/v1/signal/outcome/complete`.
1126
+ *
1127
+ * An outcome bills per COMPLETION, never per step, and `signals.outcome()`
1128
+ * never closes one — it accepts only the step shape. Declaring the workflow
1129
+ * finished is always this separate, explicit, contentless call.
1130
+ *
1131
+ * This closes the RUN only. If a step of this run also carried a
1132
+ * `composite`, that occurrence stays open and unbilled until you separately
1133
+ * call {@link Signals.completeComposite} — the two completions are
1134
+ * independent, and neither implies the other.
1135
+ *
1136
+ * Sent immediately, never buffered. A genuinely second, independent call for
1137
+ * an already-closed run is a PERMANENT error (it would bill the run twice),
1138
+ * not a safe no-op — see {@link OutcomeCompletionResult}.
1139
+ */
1140
+ async completeOutcome(input) {
1141
+ const idempotencyKey = input.idempotencyKey ?? newKey();
1142
+ const res = await this.transport.request({
1143
+ method: "POST",
1144
+ path: OUTCOME_COMPLETE_PATH,
1145
+ idempotencyKey,
1146
+ body: {
1147
+ customerId: input.customerId,
1148
+ runId: input.runId,
1149
+ complete: true,
1150
+ ...input.member ? { member: input.member } : {},
1151
+ idempotencyKey
1152
+ },
1153
+ retry: true
1154
+ });
1155
+ const queued = res.queued === true;
1156
+ return {
1157
+ delivery: queued ? "accepted" : "recorded",
1158
+ queued,
1159
+ ...res.status != null ? { status: res.status } : {},
1160
+ usageLog: res.usageLog ?? null,
1161
+ ...handles(res)
1162
+ };
1163
+ }
1106
1164
  /**
1107
1165
  * Recent usage logs + totals for a customer. `GET /api/v1/usage`.
1108
1166
  *
@@ -1175,13 +1233,13 @@ var Signals = class {
1175
1233
  * arrive in any order and the occurrence stays open and unbilled until you
1176
1234
  * say otherwise. Put it on the line before your own `return`.
1177
1235
  *
1178
- * EVERY composite needs this, outcome-backed ones included. An outcome's own
1179
- * `complete: true` closes its RUN and nothing else — it does not close the
1180
- * composite occurrence the run was tagged into, whatever mix of credits,
1181
- * outcomes and units that composite wraps. (Earlier versions of this SDK
1182
- * documented the opposite. Believing it means the occurrence stays ONGOING
1183
- * forever and is never billed, so if you tag outcome signals with a
1184
- * `composite`, you must still call this.)
1236
+ * EVERY composite needs this, outcome-backed ones included. `completeOutcome()`
1237
+ * closes its RUN and nothing else — it does not close the composite
1238
+ * occurrence the run was tagged into, whatever mix of credits, outcomes and
1239
+ * units that composite wraps. (Earlier versions of this SDK documented the
1240
+ * opposite. Believing it means the occurrence stays ONGOING forever and is
1241
+ * never billed, so if you tag outcome signals with a `composite`, you must
1242
+ * still call this.)
1185
1243
  *
1186
1244
  * Closing twice is safe: the second call answers `ALREADY_COMPLETED` and
1187
1245
  * cannot bill again. Calling it EARLY is also safe — before every entitlement
@@ -1230,6 +1288,37 @@ var Signals = class {
1230
1288
  query: { customerId: params.customerId, limit: params.limit }
1231
1289
  });
1232
1290
  }
1291
+ /**
1292
+ * One customer's usage history, merged from every meter kind — credit,
1293
+ * outcome, unit, wallet, and completed composite occurrences — into one
1294
+ * feed sorted by time. `GET /api/v1/signal/usage-log`. Exactly the same
1295
+ * rows the customer portal's own "Usage logs" table shows, so a caller can
1296
+ * build against this instead of the portal UI.
1297
+ *
1298
+ * Cursor-paginated, NOT page-numbered: `limit` + `cursor` in, `nextCursor`
1299
+ * out. Omit `cursor` for the first page; pass the previous page's
1300
+ * `nextCursor` back to fetch the next one, and stop once it comes back
1301
+ * `null`. Unlike offset pagination, the cost of a page never grows with how
1302
+ * deep you've paged — each page only asks for rows strictly before the
1303
+ * cursor's position.
1304
+ *
1305
+ * Read-only; unlike {@link track}/{@link unit}/etc., there is nothing to
1306
+ * buffer or retry here.
1307
+ */
1308
+ usageLog(params) {
1309
+ return this.transport.request({
1310
+ method: "GET",
1311
+ path: USAGE_LOG_PATH,
1312
+ query: {
1313
+ customerId: params.customerId,
1314
+ limit: params.limit,
1315
+ cursor: params.cursor,
1316
+ period: params.period,
1317
+ from: params.from,
1318
+ to: params.to
1319
+ }
1320
+ });
1321
+ }
1233
1322
  };
1234
1323
 
1235
1324
  // src/resources/units.ts