wontopos 2.2.35 → 2.2.37

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.
@@ -44,6 +44,9 @@
44
44
  export declare function _resetStoreIdWarnings(): void;
45
45
  export declare const SEARCH_LIMIT_MIN = 5;
46
46
  export declare const SEARCH_LIMIT_MAX = 20;
47
+ /** `recall`'s surrounding-context count. 0 to 20 inclusive; 0 attaches none. */
48
+ export declare const CONTEXT_LIMIT_MIN = 0;
49
+ export declare const CONTEXT_LIMIT_MAX = 20;
47
50
  /** Quota from a response's `X-RateLimit-*` headers, or `null` if absent. */
48
51
  export interface RateLimit {
49
52
  limit: number | null;
@@ -356,6 +359,36 @@ export interface HistoryTurn {
356
359
  timestamp?: string;
357
360
  [key: string]: unknown;
358
361
  }
362
+ /** `usage` → what this key has spent, and what is left to spend. */
363
+ export interface UsageResult {
364
+ window_days?: number;
365
+ /** This API key's LIFETIME spend. Never another key's. */
366
+ key?: {
367
+ requests?: number;
368
+ cost_cents?: number;
369
+ input_tokens?: number;
370
+ output_tokens?: number;
371
+ since?: string;
372
+ };
373
+ /** The workspace this key belongs to, over the window. */
374
+ workspace?: {
375
+ workspace_id?: string | null;
376
+ requests?: number;
377
+ cost_cents?: number;
378
+ };
379
+ /** Per-store spend over the window, busiest first, and at most 50 rows — a longer
380
+ * list is cut, so these need not sum to `workspace`. `other`, when present, is an
381
+ * overflow bucket rather than a store. */
382
+ stores?: {
383
+ store?: string;
384
+ requests?: number;
385
+ cost_cents?: number;
386
+ }[];
387
+ /** Prepaid balance left on the ACCOUNT. Negative means overdrawn. */
388
+ balance_cents?: number;
389
+ expiring_soon_cents?: number;
390
+ [key: string]: unknown;
391
+ }
359
392
  export interface StatsResult {
360
393
  total_memories?: number;
361
394
  short_term_turns?: number;
@@ -547,6 +580,22 @@ export interface ClientOptions {
547
580
  * key on an options object is not an error in TS at runtime). Both names work;
548
581
  * `maxRetries` wins if somehow both are given. */
549
582
  retries?: number;
583
+ /** A total budget for one call, in ms, across every attempt.
584
+ *
585
+ * `timeoutMs` bounds ONE attempt. At the defaults — 30s, two retries — a single
586
+ * call can hold a connection for 30s + backoff + 30s + backoff + 30s, over a
587
+ * minute, and a server handler awaiting it has no way to say "I only have five
588
+ * seconds". This is that way. Unset means no overall budget. */
589
+ deadlineMs?: number;
590
+ /** The caller's `AbortSignal` — cancel work already in flight.
591
+ *
592
+ * The SDK aborts on its own timeout; this is the seam for the caller's reason.
593
+ * When someone closes the chat window, the recall that window asked for should
594
+ * end, and the backoff sleep waiting to retry it should end too, instead of
595
+ * running to completion and billing for an answer nobody will read.
596
+ *
597
+ * Per call, clone: `mem.withSignal(ctrl.signal).recall(...)`. */
598
+ signal?: AbortSignal;
550
599
  /** The `fetch` used for every request (default: the global `fetch`).
551
600
  *
552
601
  * This is the seam for anything the runtime cannot express through options.
@@ -576,6 +625,8 @@ export declare class Client {
576
625
  private readonly apiKey;
577
626
  private readonly model;
578
627
  private readonly maxRetries;
628
+ private readonly deadlineMs?;
629
+ private readonly signal?;
579
630
  /** Transport. Held as a field (not read off `globalThis` per call) so a caller
580
631
  * who passes one gets it for every request, including from cloned clients. */
581
632
  private readonly fetchImpl;
@@ -603,6 +654,18 @@ export declare class Client {
603
654
  withModel(model: string): Client;
604
655
  /** A client bound to `userId` as its default store (everything else kept). */
605
656
  withUser(userId: string): Client;
657
+ /** A client bound to a caller's `AbortSignal` (everything else kept).
658
+ *
659
+ * const ctrl = new AbortController();
660
+ * req.on("close", () => ctrl.abort());
661
+ * const r = await mem.withSignal(ctrl.signal).recall(q, user);
662
+ *
663
+ * Aborting ends the request in flight AND any backoff sleep waiting to retry it. */
664
+ withSignal(signal: AbortSignal): Client;
665
+ /** A client with a total budget per call, across every retry (everything else
666
+ * kept). `mem.withDeadline(5_000).search(q)` inside a handler that has five
667
+ * seconds. */
668
+ withDeadline(deadlineMs: number): Client;
606
669
  /** A client with a different per-request timeout in ms (everything else kept).
607
670
  * `mem.withTimeout(120_000).addBulk(bigBlob)` — this slow call only. */
608
671
  withTimeout(timeoutMs: number): Client;
@@ -687,6 +750,20 @@ export declare class Client {
687
750
  history(userId?: string): Promise<HistoryTurn[]>;
688
751
  /** Memory counts for a store: { total_memories, short_term_turns }. */
689
752
  stats(userId?: string): Promise<StatsResult>;
753
+ /**
754
+ * What this key has spent, and what is left — the numbers behind "can I keep going?".
755
+ *
756
+ * Free: it carries no charge and skips the balance gate, because an account at zero
757
+ * still has to be able to find out why. Rate-limited instead.
758
+ *
759
+ * Scoped to THIS key: its own lifetime spend, its workspace and stores over the
760
+ * window. It never returns another key's spend. `balance_cents` is account-wide,
761
+ * because that is what gates the next call whichever key makes it.
762
+ *
763
+ * const u = await mem.usage(7);
764
+ * if (u.balance_cents! < 100) stop();
765
+ */
766
+ usage(days?: number): Promise<UsageResult>;
690
767
  /** Fetch ONE memory by id — the text you stored, and its metadata.
691
768
  * The id is what `add`/`store` or `listMemories` returned. Same visibility as
692
769
  * `listMemories`: an id from another store, an internal record id, or an
@@ -907,6 +984,31 @@ export declare class Client {
907
984
  * a non-2xx we re-read the body as text and reuse `errorFor`, which keeps 404
908
985
  * (`NotFoundError`) and 401 behaving the same as everywhere else.
909
986
  */
987
+ /** Where this call's budget runs out, or `undefined` when it has none. Computed
988
+ * once per call, not per attempt — a budget recomputed each attempt is not a
989
+ * budget. */
990
+ private deadlineAt;
991
+ /** The abort wiring for ONE attempt: this SDK's per-attempt timeout, the caller's
992
+ * signal, and whatever is left of the overall deadline — whichever fires first.
993
+ *
994
+ * One function, two callers (`request` and `requestBytes`), because a guard living
995
+ * in one of two request paths is exactly how `getImage` ended up with no retry loop
996
+ * while the module doc promised every call had one. */
997
+ private beginAttempt;
998
+ /** What to say when an attempt was cut short. The three reasons need different
999
+ * words: a timeout may have landed server-side, a cancellation certainly did not
1000
+ * matter to the caller, and an exhausted budget is the caller's own setting. */
1001
+ private abortMessage;
1002
+ /** Sleep between attempts — but wake the moment the caller aborts, and give up
1003
+ * rather than wait out a backoff the budget cannot cover. A backoff that runs to
1004
+ * completion after the caller gave up wastes exactly as long as the request it was
1005
+ * waiting to repeat.
1006
+ *
1007
+ * Clamping the wait to what is left and retrying anyway is the same answer as
1008
+ * having no deadline at all: the call still spends the whole allowance, and the one
1009
+ * attempt it buys has nothing left to finish in. If the wait does not fit, the
1010
+ * budget is already decided. */
1011
+ private backoffSleep;
910
1012
  private requestBytes;
911
1013
  /** Validate an idempotency key BEFORE the network. The API answers a malformed
912
1014
  * key with a 400, which on a retry path reads as "my write failed" when in fact