@pouchy_ai/admin-sdk 0.6.1 → 0.7.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/CHANGELOG.md CHANGED
@@ -2,6 +2,47 @@
2
2
 
3
3
  All notable changes to `@pouchy_ai/admin-sdk` are documented here.
4
4
 
5
+ ## 0.7.0 — 2026-07-27
6
+
7
+ Adds: a throttled request now tells you how long to wait.
8
+
9
+ - **`AdminApiError.retryAfter`** — seconds, present on a 429 and `undefined` on
10
+ every other failure. Every write this client makes (`POST` / `PATCH` /
11
+ `DELETE`) passes through one per-IP throttle on the server — a
12
+ `v1-admin-write` bucket of 120 non-GET requests per minute across all of
13
+ `/v1/*` — and the natural migration loop (`updateAgent` per agent,
14
+ `setUserSuspended` per user, `setSkillRate` per skill) reaches 120 in seconds.
15
+ The server has always answered with both a `Retry-After` header and a body
16
+ `retryAfter`; this package discarded both, so the one caller who provably
17
+ needs a backoff number had only the prose message to guess from — and a
18
+ guessed-too-short retry keeps the sliding window saturated, extending the
19
+ throttle it is trying to escape.
20
+
21
+ ```ts
22
+ try {
23
+ await admin.updateAgent(id, patch);
24
+ } catch (e) {
25
+ if (e instanceof AdminApiError && e.status === 429) {
26
+ await new Promise((r) => setTimeout(r, (e.retryAfter ?? 5) * 1000));
27
+ }
28
+ }
29
+ ```
30
+
31
+ Resolution order matches the companion JS SDK's `retryAfterFrom`: body
32
+ `retryAfterSec`, then body `retryAfter` (the key the `/v1` throttle actually
33
+ emits), then the `Retry-After` header as seconds, then its RFC 9110 HTTP-date
34
+ form converted to a non-negative delta. A response that names none leaves
35
+ `retryAfter` **undefined** — never `0`, which would tell backoff code to retry
36
+ immediately.
37
+
38
+ - **Back-compatible.** New optional third constructor argument and a new
39
+ optional readonly property; `status`, `message` and the `instanceof
40
+ AdminApiError` check are untouched. Minor bump because the public error type
41
+ gained a member.
42
+
43
+ Both sibling SDKs already surfaced this value (JS `CompanionError.retryAfter`,
44
+ python `CompanionError.retry_after`) — same name, same units, same precedence.
45
+
5
46
  ## 0.6.1 — 2026-07-27
6
47
 
7
48
  Fix: the four long-running requests are no longer cut at 30s while the server is
package/README.md CHANGED
@@ -95,6 +95,33 @@ try {
95
95
  }
96
96
  ```
97
97
 
98
+ ### Throttling (429)
99
+
100
+ Writes are rate-limited per IP — 120 non-`GET` requests per minute across the
101
+ whole `/v1` plane — so a migration loop over agents, users or skills will hit it.
102
+ On a 429, `AdminApiError.retryAfter` carries the server's backoff in **seconds**
103
+ (from the body's `retryAfter` / `retryAfterSec`, or the `Retry-After` header).
104
+ It is `undefined` on every other failure, and `undefined` — never `0` — when the
105
+ server named no delay, so `??` your own floor rather than retrying immediately:
106
+
107
+ ```ts
108
+ async function withBackoff<T>(call: () => Promise<T>, tries = 5): Promise<T> {
109
+ for (let i = 0; ; i++) {
110
+ try {
111
+ return await call();
112
+ } catch (e) {
113
+ if (!(e instanceof AdminApiError) || e.status !== 429 || i >= tries) throw e;
114
+ await new Promise((r) => setTimeout(r, (e.retryAfter ?? 5) * 1000));
115
+ }
116
+ }
117
+ }
118
+
119
+ await withBackoff(() => admin.updateAgent(id, { status: 'published' }));
120
+ ```
121
+
122
+ Reads (`GET`) are not covered by that bucket. `retryAfter` is available from
123
+ 0.7.0.
124
+
98
125
  ## Surface
99
126
 
100
127
  | Area | Methods |
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const ADMIN_SDK_VERSION = "0.6.1";
1
+ export declare const ADMIN_SDK_VERSION = "0.7.0";
2
2
  export declare const DEFAULT_BASE_URL = "https://pouchy.ai/v1/admin";
3
3
  /** Deadline for the routes whose server handler declares `maxDuration: 300` —
4
4
  * the server's own ceiling plus headroom, so a client abort can only ever mean
@@ -45,10 +45,25 @@ export interface AdminClientOptions {
45
45
  timeoutMs?: number;
46
46
  }
47
47
  /** Thrown on any non-2xx response. `status` is the HTTP status; `message` is the
48
- * server's `error` string when present. */
48
+ * server's `error` string when present; `retryAfter` is the throttle's own
49
+ * backoff in SECONDS on a 429 (undefined on every other failure).
50
+ *
51
+ * Why `retryAfter` exists: the entire write surface of this SDK sits behind one
52
+ * per-IP throttle — `hooks.server.ts` runs a `v1-admin-write` bucket (120
53
+ * non-GET requests / minute) over every `/v1/*` path, and `/v1/admin` is where
54
+ * this client lives. A migration loop (`updateAgent` per agent,
55
+ * `setUserSuspended` per user, `setSkillRate` per skill) reaches 120 in
56
+ * seconds. The server answers with BOTH a `Retry-After` header and a body
57
+ * `retryAfter`, and this package used to discard both — leaving the one caller
58
+ * who provably needs a backoff number with nothing but a prose message to
59
+ * guess from, and a guessed-too-short retry keeps the sliding window
60
+ * saturated. Both sibling SDKs already surface it (JS
61
+ * `CompanionError.retryAfter`, python `CompanionError.retry_after`); this is
62
+ * the same value under the same name. */
49
63
  export declare class AdminApiError extends Error {
50
64
  status: number;
51
- constructor(message: string, status: number);
65
+ readonly retryAfter?: number;
66
+ constructor(message: string, status: number, retryAfter?: number);
52
67
  }
53
68
  export type Env = 'live' | 'test';
54
69
  export type AgentStatus = 'draft' | 'published';
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@
8
8
  // import { createAdminClient } from '@pouchy_ai/admin-sdk';
9
9
  // const admin = createAdminClient({ adminKey: process.env.POUCHY_ADMIN_KEY! });
10
10
  // const { agents } = await admin.listAgents();
11
- export const ADMIN_SDK_VERSION = '0.6.1';
11
+ export const ADMIN_SDK_VERSION = '0.7.0';
12
12
  export const DEFAULT_BASE_URL = 'https://pouchy.ai/v1/admin';
13
13
  /** Default per-request timeout (ms). A hung upstream otherwise never rejects. */
14
14
  const DEFAULT_TIMEOUT_MS = 30_000;
@@ -56,14 +56,65 @@ export function requestDeadlineMs(method, path) {
56
56
  : DEFAULT_TIMEOUT_MS;
57
57
  }
58
58
  /** Thrown on any non-2xx response. `status` is the HTTP status; `message` is the
59
- * server's `error` string when present. */
59
+ * server's `error` string when present; `retryAfter` is the throttle's own
60
+ * backoff in SECONDS on a 429 (undefined on every other failure).
61
+ *
62
+ * Why `retryAfter` exists: the entire write surface of this SDK sits behind one
63
+ * per-IP throttle — `hooks.server.ts` runs a `v1-admin-write` bucket (120
64
+ * non-GET requests / minute) over every `/v1/*` path, and `/v1/admin` is where
65
+ * this client lives. A migration loop (`updateAgent` per agent,
66
+ * `setUserSuspended` per user, `setSkillRate` per skill) reaches 120 in
67
+ * seconds. The server answers with BOTH a `Retry-After` header and a body
68
+ * `retryAfter`, and this package used to discard both — leaving the one caller
69
+ * who provably needs a backoff number with nothing but a prose message to
70
+ * guess from, and a guessed-too-short retry keeps the sliding window
71
+ * saturated. Both sibling SDKs already surface it (JS
72
+ * `CompanionError.retryAfter`, python `CompanionError.retry_after`); this is
73
+ * the same value under the same name. */
60
74
  export class AdminApiError extends Error {
61
75
  status;
62
- constructor(message, status) {
76
+ retryAfter;
77
+ constructor(message, status, retryAfter) {
63
78
  super(message);
64
79
  this.name = 'AdminApiError';
65
80
  this.status = status;
81
+ if (retryAfter !== undefined)
82
+ this.retryAfter = retryAfter;
83
+ }
84
+ }
85
+ /** Seconds to wait before retrying a throttled response, or undefined when the
86
+ * server named neither a body field nor a header.
87
+ *
88
+ * Precedence mirrors the JS companion SDK's `retryAfterFrom`, plus the one key
89
+ * this plane actually emits: the `/v1` admin-write 429 body says `retryAfter`,
90
+ * while the `/api/companion` 429 body says `retryAfterSec`. Accept both so the
91
+ * same helper is correct if a route is ever aligned on the other name.
92
+ *
93
+ * A missing/blank header must stay undefined — `Number(null)` is 0, which
94
+ * would stamp `retryAfter: 0` on EVERY error without the header (a 404, a
95
+ * 400 …) and tell backoff code "retry immediately". */
96
+ function retryAfterFrom(res, body) {
97
+ for (const k of ['retryAfterSec', 'retryAfter']) {
98
+ const v = body?.[k];
99
+ if (typeof v === 'number' && Number.isFinite(v) && v >= 0)
100
+ return v;
66
101
  }
102
+ // Optional-chained because `opts.fetch` is a documented injection point and
103
+ // hand-rolled test doubles routinely return a bare `{ ok, status, json }`
104
+ // with no `headers` — reading a header must never turn their 4xx fixture
105
+ // into a TypeError.
106
+ const raw = res.headers?.get?.('Retry-After');
107
+ if (raw === null || raw === undefined || raw.trim() === '')
108
+ return undefined;
109
+ const header = Number(raw);
110
+ if (Number.isFinite(header) && header >= 0)
111
+ return header;
112
+ // RFC 9110 also allows an HTTP-date (proxies/CDNs emit it) — convert to a
113
+ // non-negative seconds delta. Unparseable stays undefined.
114
+ const at = Date.parse(raw);
115
+ if (!Number.isNaN(at))
116
+ return Math.max(0, Math.ceil((at - Date.now()) / 1000));
117
+ return undefined;
67
118
  }
68
119
  function qs(params) {
69
120
  const u = new URLSearchParams();
@@ -107,7 +158,7 @@ export function createAdminClient(opts) {
107
158
  }
108
159
  const data = (await res.json().catch(() => ({})));
109
160
  if (!res.ok)
110
- throw new AdminApiError(data?.error ?? `HTTP ${res.status}`, res.status);
161
+ throw new AdminApiError(data?.error ?? `HTTP ${res.status}`, res.status, retryAfterFrom(res, data));
111
162
  return data;
112
163
  }
113
164
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pouchy_ai/admin-sdk",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
4
4
  "description": "Typed TypeScript client for the Pouchy Admin API \u2014 manage agents, keys, end users, knowledge, skills, channels, schedules, webhooks and credentials headlessly, with a project Admin key.",
5
5
  "type": "module",
6
6
  "license": "SEE LICENSE IN LICENSE",