@zenadmin-ai/sdk 0.2.0 → 0.3.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/client.d.ts +15 -2
- package/dist/index.js +38 -8
- package/dist/types.d.ts +18 -3
- package/package.json +1 -1
package/dist/client.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Operation, type ReadResult, type Tool, type WhoAmI } from "./types.js";
|
|
2
|
-
export declare const SDK_VERSION = "0.
|
|
2
|
+
export declare const SDK_VERSION = "0.3.0";
|
|
3
3
|
export interface ZenAdminOptions {
|
|
4
4
|
token: string;
|
|
5
5
|
apiUrl?: string;
|
|
@@ -12,6 +12,13 @@ export interface ZenAdminOptions {
|
|
|
12
12
|
fetch?: typeof fetch;
|
|
13
13
|
/** Debug lines (method, path, status, request ID). Never the token or bodies. */
|
|
14
14
|
log?: (line: string) => void;
|
|
15
|
+
/**
|
|
16
|
+
* Correlation: every request's X-Request-Id becomes `<prefix>.<n>`, so a caller's own log line
|
|
17
|
+
* (the hosted MCP server's, for example) finds the backend's lines. 8–48 of A-Z a-z 0-9 _ -.
|
|
18
|
+
*/
|
|
19
|
+
requestIdPrefix?: string;
|
|
20
|
+
/** The longest Retry-After a 429 is retried after; a longer one is reported at once. Default 60 s. */
|
|
21
|
+
maxRetryWaitMs?: number;
|
|
15
22
|
/** For tests: how long to wait before a retry. */
|
|
16
23
|
sleep?: (ms: number) => Promise<void>;
|
|
17
24
|
}
|
|
@@ -25,6 +32,9 @@ export declare class ZenAdmin {
|
|
|
25
32
|
private readonly fetchImpl;
|
|
26
33
|
private readonly log?;
|
|
27
34
|
private readonly sleep;
|
|
35
|
+
private readonly requestIdPrefix?;
|
|
36
|
+
private readonly maxRetryWaitMs;
|
|
37
|
+
private requestCount;
|
|
28
38
|
constructor(options: ZenAdminOptions);
|
|
29
39
|
/** Token and URL from options, then ZENADMIN_TOKEN / ZENADMIN_API_URL, then `zen login`. */
|
|
30
40
|
static fromEnvironment(options?: Partial<ZenAdminOptions>, env?: NodeJS.ProcessEnv): Promise<ZenAdmin>;
|
|
@@ -32,7 +42,10 @@ export declare class ZenAdmin {
|
|
|
32
42
|
whoami(): Promise<WhoAmI>;
|
|
33
43
|
/** Every tool this token can use right now, with its input JSON Schema, kind and impact. */
|
|
34
44
|
tools(): Promise<Tool[]>;
|
|
35
|
-
/**
|
|
45
|
+
/**
|
|
46
|
+
* Runs a read tool: inventory.search, inventory.details, employees.search, catalog.search,
|
|
47
|
+
* orders.search or quotes.search (see tools() for what this token can use).
|
|
48
|
+
*/
|
|
36
49
|
read(toolId: string, input: Record<string, unknown>): Promise<ReadResult>;
|
|
37
50
|
/**
|
|
38
51
|
* Prepares a change. Nothing happens until the admin approves it in ZenAdmin. Sending the same
|
package/dist/index.js
CHANGED
|
@@ -163,7 +163,7 @@ class NotSignedInError extends ZenAdminError {
|
|
|
163
163
|
constructor() {
|
|
164
164
|
super({
|
|
165
165
|
code: "NOT_SIGNED_IN",
|
|
166
|
-
message: "No ZenAdmin token. Create one in ZenAdmin (
|
|
166
|
+
message: "No ZenAdmin token. Create one in ZenAdmin (Zen AI > Access tokens), then run `zen login` or set ZENADMIN_TOKEN.",
|
|
167
167
|
status: 0
|
|
168
168
|
});
|
|
169
169
|
this.name = "NotSignedInError";
|
|
@@ -196,6 +196,7 @@ import { z } from "zod";
|
|
|
196
196
|
var ChannelSchema = z.enum(["console", "mcp", "cli"]);
|
|
197
197
|
var WhoAmISchema = z.object({
|
|
198
198
|
company_id: z.string(),
|
|
199
|
+
company_name: z.string().nullish(),
|
|
199
200
|
user_id: z.string(),
|
|
200
201
|
channel: ChannelSchema,
|
|
201
202
|
token: z.object({
|
|
@@ -240,7 +241,11 @@ var OperationSchema = z.object({
|
|
|
240
241
|
links: z.array(LinkSchema),
|
|
241
242
|
impact: z.enum(["low", "medium", "high"]),
|
|
242
243
|
expires_at: z.string(),
|
|
243
|
-
result: z.object({
|
|
244
|
+
result: z.object({
|
|
245
|
+
message: z.string(),
|
|
246
|
+
links: z.array(LinkSchema),
|
|
247
|
+
partial: z.object({ done: z.number(), total: z.number() }).optional()
|
|
248
|
+
}).nullable(),
|
|
244
249
|
error: z.object({ code: z.string(), message: z.string() }).nullable(),
|
|
245
250
|
channel: ChannelSchema.optional(),
|
|
246
251
|
approval_url: z.string().nullable().optional()
|
|
@@ -252,7 +257,7 @@ function outcomeOf(operation) {
|
|
|
252
257
|
case "running":
|
|
253
258
|
return "in_progress";
|
|
254
259
|
case "succeeded":
|
|
255
|
-
return "completed";
|
|
260
|
+
return operation.result?.partial ? "partial" : "completed";
|
|
256
261
|
case "failed":
|
|
257
262
|
return "failed";
|
|
258
263
|
case "cancelled":
|
|
@@ -264,10 +269,11 @@ function outcomeOf(operation) {
|
|
|
264
269
|
}
|
|
265
270
|
}
|
|
266
271
|
var isSettled = (operation) => !["pending_approval", "running"].includes(operation.status);
|
|
272
|
+
var isReusable = (operation) => !isSettled(operation) || operation.status === "unknown";
|
|
267
273
|
var ReadResultSchema = z.record(z.string(), z.unknown());
|
|
268
274
|
|
|
269
275
|
// packages/sdk/src/client.ts
|
|
270
|
-
var SDK_VERSION = "0.
|
|
276
|
+
var SDK_VERSION = "0.3.0";
|
|
271
277
|
var REQUEST_ID = /^[A-Za-z0-9_-]{8,64}$/;
|
|
272
278
|
var RETRY_STATUSES = new Set([429, 502, 503, 504]);
|
|
273
279
|
var defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
@@ -298,6 +304,9 @@ class ZenAdmin {
|
|
|
298
304
|
fetchImpl;
|
|
299
305
|
log;
|
|
300
306
|
sleep;
|
|
307
|
+
requestIdPrefix;
|
|
308
|
+
maxRetryWaitMs;
|
|
309
|
+
requestCount = 0;
|
|
301
310
|
constructor(options) {
|
|
302
311
|
if (!options.token)
|
|
303
312
|
throw new NotSignedInError;
|
|
@@ -310,6 +319,13 @@ class ZenAdmin {
|
|
|
310
319
|
this.fetchImpl = options.fetch ?? fetch;
|
|
311
320
|
this.log = options.log;
|
|
312
321
|
this.sleep = options.sleep ?? defaultSleep;
|
|
322
|
+
this.maxRetryWaitMs = options.maxRetryWaitMs ?? 60000;
|
|
323
|
+
if (options.requestIdPrefix !== undefined) {
|
|
324
|
+
if (!/^[A-Za-z0-9_-]{8,48}$/.test(options.requestIdPrefix)) {
|
|
325
|
+
throw new Error("requestIdPrefix is 8–48 letters, digits, _ or -.");
|
|
326
|
+
}
|
|
327
|
+
this.requestIdPrefix = options.requestIdPrefix;
|
|
328
|
+
}
|
|
313
329
|
}
|
|
314
330
|
static async fromEnvironment(options = {}, env = process.env) {
|
|
315
331
|
const creds = await resolveCredentials({ token: options.token, apiUrl: options.apiUrl }, env);
|
|
@@ -362,7 +378,19 @@ class ZenAdmin {
|
|
|
362
378
|
const signal = options.signal;
|
|
363
379
|
for (;; ) {
|
|
364
380
|
signal?.throwIfAborted();
|
|
365
|
-
|
|
381
|
+
let operation;
|
|
382
|
+
try {
|
|
383
|
+
operation = await this.operation(id, { signal });
|
|
384
|
+
} catch (error) {
|
|
385
|
+
const wait = error instanceof RateLimitError ? error.retryAfterSeconds * 1000 : null;
|
|
386
|
+
if (wait === null || Date.now() + wait > deadline) {
|
|
387
|
+
if (last && wait !== null)
|
|
388
|
+
return { operation: last, timedOut: true };
|
|
389
|
+
throw error;
|
|
390
|
+
}
|
|
391
|
+
await abortable(this.sleep(wait), signal);
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
366
394
|
signal?.throwIfAborted();
|
|
367
395
|
if (!last || last.status !== operation.status)
|
|
368
396
|
options.onUpdate?.(operation);
|
|
@@ -379,7 +407,7 @@ class ZenAdmin {
|
|
|
379
407
|
let attempt = 0;
|
|
380
408
|
for (;; ) {
|
|
381
409
|
signal?.throwIfAborted();
|
|
382
|
-
const requestId = randomUUID();
|
|
410
|
+
const requestId = this.requestIdPrefix ? `${this.requestIdPrefix}.${++this.requestCount}` : randomUUID();
|
|
383
411
|
const requestSignal = signal ? AbortSignal.any([signal, AbortSignal.timeout(this.timeoutMs)]) : AbortSignal.timeout(this.timeoutMs);
|
|
384
412
|
let response;
|
|
385
413
|
try {
|
|
@@ -416,9 +444,10 @@ class ZenAdmin {
|
|
|
416
444
|
}
|
|
417
445
|
if (!response.ok) {
|
|
418
446
|
const error = errorFromResponse(response.status, json, answeredId, response.headers.get("retry-after"));
|
|
419
|
-
|
|
447
|
+
const tooLong = error instanceof RateLimitError && error.retryAfterSeconds * 1000 > this.maxRetryWaitMs;
|
|
448
|
+
if (RETRY_STATUSES.has(response.status) && attempt < this.maxRetries && !tooLong) {
|
|
420
449
|
attempt++;
|
|
421
|
-
const wait = error instanceof RateLimitError ?
|
|
450
|
+
const wait = error instanceof RateLimitError ? error.retryAfterSeconds * 1000 : 250 * 2 ** attempt;
|
|
422
451
|
await abortable(this.sleep(wait), signal);
|
|
423
452
|
continue;
|
|
424
453
|
}
|
|
@@ -461,6 +490,7 @@ export {
|
|
|
461
490
|
clearCredentials,
|
|
462
491
|
credentialsPath,
|
|
463
492
|
errorFromResponse,
|
|
493
|
+
isReusable,
|
|
464
494
|
isSettled,
|
|
465
495
|
looksLikeToken,
|
|
466
496
|
normaliseApiUrl,
|
package/dist/types.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export declare const ChannelSchema: z.ZodEnum<{
|
|
|
10
10
|
}>;
|
|
11
11
|
export declare const WhoAmISchema: z.ZodObject<{
|
|
12
12
|
company_id: z.ZodString;
|
|
13
|
+
company_name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
13
14
|
user_id: z.ZodString;
|
|
14
15
|
channel: z.ZodEnum<{
|
|
15
16
|
console: "console";
|
|
@@ -132,6 +133,10 @@ export declare const OperationSchema: z.ZodObject<{
|
|
|
132
133
|
label: z.ZodString;
|
|
133
134
|
href: z.ZodString;
|
|
134
135
|
}, z.core.$strip>>;
|
|
136
|
+
partial: z.ZodOptional<z.ZodObject<{
|
|
137
|
+
done: z.ZodNumber;
|
|
138
|
+
total: z.ZodNumber;
|
|
139
|
+
}, z.core.$strip>>;
|
|
135
140
|
}, z.core.$strip>>;
|
|
136
141
|
error: z.ZodNullable<z.ZodObject<{
|
|
137
142
|
code: z.ZodString;
|
|
@@ -147,12 +152,22 @@ export declare const OperationSchema: z.ZodObject<{
|
|
|
147
152
|
export type Operation = z.infer<typeof OperationSchema>;
|
|
148
153
|
/**
|
|
149
154
|
* What an operation's status means for the caller. A token can never approve, so a new proposal
|
|
150
|
-
* is always `approval_required` until the admin acts in ZenAdmin.
|
|
155
|
+
* is always `approval_required` until the admin acts in ZenAdmin. `in_progress` is approved and
|
|
156
|
+
* submitted; `partial` is a bulk change where some targets were not done.
|
|
151
157
|
*/
|
|
152
|
-
export type Outcome = "approval_required" | "in_progress" | "completed" | "failed" | "cancelled" | "expired" | "check_result";
|
|
153
|
-
export declare function outcomeOf(operation: Pick<Operation, "status">
|
|
158
|
+
export type Outcome = "approval_required" | "in_progress" | "completed" | "partial" | "failed" | "cancelled" | "expired" | "check_result";
|
|
159
|
+
export declare function outcomeOf(operation: Pick<Operation, "status"> & {
|
|
160
|
+
result?: Operation["result"];
|
|
161
|
+
}): Outcome;
|
|
154
162
|
/** Final statuses: nothing more will happen to the operation. */
|
|
155
163
|
export declare const isSettled: (operation: Pick<Operation, "status">) => boolean;
|
|
164
|
+
/**
|
|
165
|
+
* Whether asking for the same change again should get this operation back rather than a new one:
|
|
166
|
+
* while it still waits for approval or runs, and when its run did not report back (`unknown`: it
|
|
167
|
+
* may still be running, so a second proposal could repeat it). Done, failed, cancelled and expired
|
|
168
|
+
* changes are proposed afresh, for the admin to approve again.
|
|
169
|
+
*/
|
|
170
|
+
export declare const isReusable: (operation: Pick<Operation, "status">) => boolean;
|
|
156
171
|
/** Read tool results are passed through as the server returns them (bounded lists with links). */
|
|
157
172
|
export declare const ReadResultSchema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
158
173
|
export type ReadResult = z.infer<typeof ReadResultSchema>;
|