@zenadmin-ai/sdk 0.0.0-stage → 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/README.md +14 -2
- package/dist/auth.d.ts +33 -0
- package/dist/client.d.ts +85 -0
- package/dist/errors.d.ts +53 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +501 -0
- package/dist/types.d.ts +173 -0
- package/package.json +41 -4
package/README.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zenadmin-ai/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TypeScript client for the ZenAdmin agent API, used by `@zenadmin-ai/cli` and `@zenadmin-ai/mcp-server`.
|
|
4
|
+
Runs on Node 22+ and Bun.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import { ZenAdmin } from "@zenadmin-ai/sdk";
|
|
8
|
+
|
|
9
|
+
const zen = await ZenAdmin.fromEnvironment({ client: "cli" }); // ZENADMIN_TOKEN or `zen login`
|
|
10
|
+
const found = await zen.read("employees.search", { query: "ada" });
|
|
11
|
+
const change = await zen.propose("webstore.request_quote", input, { requestId: "nightly-job-42" });
|
|
12
|
+
// change.approval_url: an admin approves it in ZenAdmin; follow it with zen.waitForOperation(change.id)
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
A token can read and prepare changes, never approve them. Guide: https://github.com/Usehybrid/mewtwo#typescript-sdk
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare const DEFAULT_API_URL = "https://prod-api.zenadmin.ai";
|
|
2
|
+
export declare const looksLikeToken: (value: string) => boolean;
|
|
3
|
+
export declare function credentialsPath(env?: NodeJS.ProcessEnv): string;
|
|
4
|
+
export interface ResolvedCredentials {
|
|
5
|
+
token: string | null;
|
|
6
|
+
apiUrl: string;
|
|
7
|
+
/** Where the token came from, so `zen whoami` can say. */
|
|
8
|
+
source: "option" | "environment" | "file" | null;
|
|
9
|
+
/** The credentials file is readable by other users. */
|
|
10
|
+
insecureFile: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** Only http(s) origins; the agent API path is added by the client. */
|
|
13
|
+
export declare function normaliseApiUrl(value: string): string;
|
|
14
|
+
export declare function resolveCredentials(options?: {
|
|
15
|
+
token?: string;
|
|
16
|
+
apiUrl?: string;
|
|
17
|
+
}, env?: NodeJS.ProcessEnv): Promise<ResolvedCredentials>;
|
|
18
|
+
/** Saves the token for this user only (directory 0700, file 0600). */
|
|
19
|
+
export declare function storeCredentials(token: string, apiUrl?: string, env?: NodeJS.ProcessEnv): Promise<string>;
|
|
20
|
+
/** Forgets the stored token. The token itself keeps working until it is revoked in ZenAdmin. */
|
|
21
|
+
export declare function clearCredentials(env?: NodeJS.ProcessEnv): Promise<boolean>;
|
|
22
|
+
/**
|
|
23
|
+
* Bun loads .env files from the working directory into process.env (expanding `$OTHER` references);
|
|
24
|
+
* Node does not. A .env in a project the admin happens to be in must not choose where the token goes
|
|
25
|
+
* (ZENADMIN_API_URL), which token or credentials file is used, or anything else, so every ZENADMIN_*
|
|
26
|
+
* key named in one of those files is dropped, whatever its value. A value set in the shell for the
|
|
27
|
+
* same key is dropped too, since the two cannot be told apart; `zen login`, `--api-url` and the
|
|
28
|
+
* MCP client's config still work.
|
|
29
|
+
*/
|
|
30
|
+
export declare function withoutDotenvSettings(env?: NodeJS.ProcessEnv, cwd?: string): Promise<{
|
|
31
|
+
env: NodeJS.ProcessEnv;
|
|
32
|
+
ignored: string[];
|
|
33
|
+
}>;
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { type Operation, type ReadResult, type Tool, type WhoAmI } from "./types.js";
|
|
2
|
+
export declare const SDK_VERSION = "0.3.0";
|
|
3
|
+
export interface ZenAdminOptions {
|
|
4
|
+
token: string;
|
|
5
|
+
apiUrl?: string;
|
|
6
|
+
/** Recorded on every operation, so the admin sees where a change came from. */
|
|
7
|
+
client?: "mcp" | "cli";
|
|
8
|
+
userAgent?: string;
|
|
9
|
+
timeoutMs?: number;
|
|
10
|
+
maxRetries?: number;
|
|
11
|
+
/** For tests. */
|
|
12
|
+
fetch?: typeof fetch;
|
|
13
|
+
/** Debug lines (method, path, status, request ID). Never the token or bodies. */
|
|
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;
|
|
22
|
+
/** For tests: how long to wait before a retry. */
|
|
23
|
+
sleep?: (ms: number) => Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
export declare class ZenAdmin {
|
|
26
|
+
readonly apiUrl: string;
|
|
27
|
+
readonly client: "mcp" | "cli";
|
|
28
|
+
private readonly token;
|
|
29
|
+
private readonly userAgent;
|
|
30
|
+
private readonly timeoutMs;
|
|
31
|
+
private readonly maxRetries;
|
|
32
|
+
private readonly fetchImpl;
|
|
33
|
+
private readonly log?;
|
|
34
|
+
private readonly sleep;
|
|
35
|
+
private readonly requestIdPrefix?;
|
|
36
|
+
private readonly maxRetryWaitMs;
|
|
37
|
+
private requestCount;
|
|
38
|
+
constructor(options: ZenAdminOptions);
|
|
39
|
+
/** Token and URL from options, then ZENADMIN_TOKEN / ZENADMIN_API_URL, then `zen login`. */
|
|
40
|
+
static fromEnvironment(options?: Partial<ZenAdminOptions>, env?: NodeJS.ProcessEnv): Promise<ZenAdmin>;
|
|
41
|
+
/** Who the token acts as, and what it may do. Clients call this first and stop if it fails. */
|
|
42
|
+
whoami(): Promise<WhoAmI>;
|
|
43
|
+
/** Every tool this token can use right now, with its input JSON Schema, kind and impact. */
|
|
44
|
+
tools(): Promise<Tool[]>;
|
|
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
|
+
*/
|
|
49
|
+
read(toolId: string, input: Record<string, unknown>): Promise<ReadResult>;
|
|
50
|
+
/**
|
|
51
|
+
* Prepares a change. Nothing happens until the admin approves it in ZenAdmin. Sending the same
|
|
52
|
+
* requestId again (after a timeout, or from another process) returns the same proposal.
|
|
53
|
+
*/
|
|
54
|
+
propose(actionId: string, input: Record<string, unknown>, options?: {
|
|
55
|
+
requestId?: string;
|
|
56
|
+
}): Promise<Operation & {
|
|
57
|
+
request_id: string;
|
|
58
|
+
}>;
|
|
59
|
+
operation(id: string, options?: {
|
|
60
|
+
signal?: AbortSignal;
|
|
61
|
+
}): Promise<Operation>;
|
|
62
|
+
/**
|
|
63
|
+
* The caller's proposal made with this request ID, or null if there is none. Lets a client that
|
|
64
|
+
* lost a response check before proposing again.
|
|
65
|
+
*/
|
|
66
|
+
operationByRequest(requestId: string): Promise<Operation | null>;
|
|
67
|
+
/** Cancels a change that is still waiting for approval. Returns its state either way. */
|
|
68
|
+
cancel(id: string): Promise<Operation>;
|
|
69
|
+
/**
|
|
70
|
+
* Polls until the operation is settled (done, failed, cancelled, expired or check_result) or the
|
|
71
|
+
* time runs out, and returns the latest state. A timeout is not a failure: the change may still
|
|
72
|
+
* be approved later. Aborting `signal` stops at once, also mid-request or mid-wait, and rejects
|
|
73
|
+
* with the signal's reason.
|
|
74
|
+
*/
|
|
75
|
+
waitForOperation(id: string, options?: {
|
|
76
|
+
timeoutMs?: number;
|
|
77
|
+
intervalMs?: number;
|
|
78
|
+
signal?: AbortSignal;
|
|
79
|
+
onUpdate?: (op: Operation) => void;
|
|
80
|
+
}): Promise<{
|
|
81
|
+
operation: Operation;
|
|
82
|
+
timedOut: boolean;
|
|
83
|
+
}>;
|
|
84
|
+
private request;
|
|
85
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Errors from the ZenAdmin agent API. `code` is the server's stable code (INVALID_TOKEN,
|
|
3
|
+
* INSUFFICIENT_SCOPE, APPROVAL_REQUIRED, IDEMPOTENCY_CONFLICT, ...); `requestId` is the X-Request-Id
|
|
4
|
+
* to quote to support. Messages never contain the token.
|
|
5
|
+
*/
|
|
6
|
+
export declare class ZenAdminError extends Error {
|
|
7
|
+
readonly code: string;
|
|
8
|
+
readonly status: number;
|
|
9
|
+
readonly requestId: string | null;
|
|
10
|
+
constructor(opts: {
|
|
11
|
+
code: string;
|
|
12
|
+
message: string;
|
|
13
|
+
status: number;
|
|
14
|
+
requestId?: string | null;
|
|
15
|
+
});
|
|
16
|
+
toJSON(): {
|
|
17
|
+
error: {
|
|
18
|
+
code: string;
|
|
19
|
+
message: string;
|
|
20
|
+
status: number;
|
|
21
|
+
request_id: string | null;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/** Missing, malformed, expired or revoked token (401). */
|
|
26
|
+
export declare class AuthenticationError extends ZenAdminError {
|
|
27
|
+
constructor(message: string, requestId?: string | null, code?: string);
|
|
28
|
+
}
|
|
29
|
+
/** The token or the admin may not do this (403): scope, permission, or a console-only step. */
|
|
30
|
+
export declare class ForbiddenError extends ZenAdminError {
|
|
31
|
+
constructor(code: string, message: string, requestId?: string | null);
|
|
32
|
+
}
|
|
33
|
+
/** Unknown record, operation or action (404). ACTION_NOT_FOUND means the action is not supported. */
|
|
34
|
+
export declare class NotFoundError extends ZenAdminError {
|
|
35
|
+
constructor(code: string, message: string, requestId?: string | null);
|
|
36
|
+
}
|
|
37
|
+
/** The request conflicts with an earlier one, e.g. a request ID reused for another change (409). */
|
|
38
|
+
export declare class ConflictError extends ZenAdminError {
|
|
39
|
+
constructor(code: string, message: string, requestId?: string | null);
|
|
40
|
+
}
|
|
41
|
+
export declare class RateLimitError extends ZenAdminError {
|
|
42
|
+
readonly retryAfterSeconds: number;
|
|
43
|
+
constructor(message: string, retryAfterSeconds: number, requestId?: string | null);
|
|
44
|
+
}
|
|
45
|
+
/** The API could not be reached, or did not answer in time. */
|
|
46
|
+
export declare class NetworkError extends ZenAdminError {
|
|
47
|
+
constructor(message: string);
|
|
48
|
+
}
|
|
49
|
+
/** No token was configured. */
|
|
50
|
+
export declare class NotSignedInError extends ZenAdminError {
|
|
51
|
+
constructor();
|
|
52
|
+
}
|
|
53
|
+
export declare function errorFromResponse(status: number, body: unknown, requestId: string | null, retryAfter: string | null): ZenAdminError;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
// packages/sdk/src/client.ts
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
|
|
4
|
+
// packages/sdk/src/auth.ts
|
|
5
|
+
import { chmod, mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
6
|
+
import { homedir } from "node:os";
|
|
7
|
+
import { dirname, join } from "node:path";
|
|
8
|
+
var DEFAULT_API_URL = "https://prod-api.zenadmin.ai";
|
|
9
|
+
var TOKEN = /^zat_[A-Za-z0-9_-]{43}$/;
|
|
10
|
+
var looksLikeToken = (value) => TOKEN.test(value);
|
|
11
|
+
function credentialsPath(env = process.env) {
|
|
12
|
+
return env.ZENADMIN_CREDENTIALS_FILE || join(homedir(), ".zenadmin", "credentials.json");
|
|
13
|
+
}
|
|
14
|
+
function normaliseApiUrl(value) {
|
|
15
|
+
const url = new URL(value);
|
|
16
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") {
|
|
17
|
+
throw new Error("The API URL must start with https://");
|
|
18
|
+
}
|
|
19
|
+
if (url.protocol === "http:" && !["localhost", "127.0.0.1"].includes(url.hostname)) {
|
|
20
|
+
throw new Error("Use https:// for the API URL (http:// is only for localhost)");
|
|
21
|
+
}
|
|
22
|
+
return url.origin + url.pathname.replace(/\/+$/, "");
|
|
23
|
+
}
|
|
24
|
+
async function readStored(path) {
|
|
25
|
+
try {
|
|
26
|
+
const [raw, info] = await Promise.all([readFile(path, "utf8"), stat(path)]);
|
|
27
|
+
const parsed = JSON.parse(raw);
|
|
28
|
+
const insecure = process.platform !== "win32" && (info.mode & 63) !== 0;
|
|
29
|
+
if (typeof parsed.token !== "string")
|
|
30
|
+
return { creds: null, insecure };
|
|
31
|
+
return {
|
|
32
|
+
creds: { token: parsed.token, api_url: typeof parsed.api_url === "string" ? parsed.api_url : undefined },
|
|
33
|
+
insecure
|
|
34
|
+
};
|
|
35
|
+
} catch {
|
|
36
|
+
return { creds: null, insecure: false };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async function resolveCredentials(options = {}, env = process.env) {
|
|
40
|
+
const stored = await readStored(credentialsPath(env));
|
|
41
|
+
const apiUrl = normaliseApiUrl(options.apiUrl || env.ZENADMIN_API_URL || stored.creds?.api_url || DEFAULT_API_URL);
|
|
42
|
+
if (options.token)
|
|
43
|
+
return { token: options.token, apiUrl, source: "option", insecureFile: false };
|
|
44
|
+
if (env.ZENADMIN_TOKEN) {
|
|
45
|
+
return { token: env.ZENADMIN_TOKEN, apiUrl, source: "environment", insecureFile: false };
|
|
46
|
+
}
|
|
47
|
+
if (stored.creds) {
|
|
48
|
+
return { token: stored.creds.token, apiUrl, source: "file", insecureFile: stored.insecure };
|
|
49
|
+
}
|
|
50
|
+
return { token: null, apiUrl, source: null, insecureFile: false };
|
|
51
|
+
}
|
|
52
|
+
async function storeCredentials(token, apiUrl, env = process.env) {
|
|
53
|
+
if (!looksLikeToken(token))
|
|
54
|
+
throw new Error("That does not look like a ZenAdmin access token.");
|
|
55
|
+
const path = credentialsPath(env);
|
|
56
|
+
await mkdir(dirname(path), { recursive: true, mode: 448 });
|
|
57
|
+
const body = { token, ...apiUrl ? { api_url: normaliseApiUrl(apiUrl) } : {} };
|
|
58
|
+
await writeFile(path, `${JSON.stringify(body, null, 2)}
|
|
59
|
+
`, { mode: 384 });
|
|
60
|
+
await chmod(path, 384);
|
|
61
|
+
return path;
|
|
62
|
+
}
|
|
63
|
+
async function clearCredentials(env = process.env) {
|
|
64
|
+
try {
|
|
65
|
+
await rm(credentialsPath(env));
|
|
66
|
+
return true;
|
|
67
|
+
} catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
var DOTENV_FILES = [".env", ".env.local", ".env.development", ".env.production", ".env.test"];
|
|
72
|
+
async function withoutDotenvSettings(env = process.env, cwd = process.cwd()) {
|
|
73
|
+
if (typeof globalThis.Bun === "undefined")
|
|
74
|
+
return { env, ignored: [] };
|
|
75
|
+
const named = new Set;
|
|
76
|
+
for (const file of DOTENV_FILES) {
|
|
77
|
+
let text;
|
|
78
|
+
try {
|
|
79
|
+
text = await readFile(join(cwd, file), "utf8");
|
|
80
|
+
} catch {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
for (const match of text.matchAll(/^[ \t]*(?:export[ \t]+)?(ZENADMIN_[A-Za-z0-9_]*)[ \t]*[=:]/gm)) {
|
|
84
|
+
named.add(match[1]);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const cleaned = { ...env };
|
|
88
|
+
const ignored = [...named].filter((key) => cleaned[key] !== undefined).sort();
|
|
89
|
+
for (const key of ignored)
|
|
90
|
+
delete cleaned[key];
|
|
91
|
+
return { env: cleaned, ignored };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// packages/sdk/src/errors.ts
|
|
95
|
+
class ZenAdminError extends Error {
|
|
96
|
+
code;
|
|
97
|
+
status;
|
|
98
|
+
requestId;
|
|
99
|
+
constructor(opts) {
|
|
100
|
+
super(opts.message);
|
|
101
|
+
this.name = "ZenAdminError";
|
|
102
|
+
this.code = opts.code;
|
|
103
|
+
this.status = opts.status;
|
|
104
|
+
this.requestId = opts.requestId ?? null;
|
|
105
|
+
}
|
|
106
|
+
toJSON() {
|
|
107
|
+
return {
|
|
108
|
+
error: {
|
|
109
|
+
code: this.code,
|
|
110
|
+
message: this.message,
|
|
111
|
+
status: this.status,
|
|
112
|
+
request_id: this.requestId
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
class AuthenticationError extends ZenAdminError {
|
|
119
|
+
constructor(message, requestId, code = "INVALID_TOKEN") {
|
|
120
|
+
super({ code, message, status: 401, requestId });
|
|
121
|
+
this.name = "AuthenticationError";
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
class ForbiddenError extends ZenAdminError {
|
|
126
|
+
constructor(code, message, requestId) {
|
|
127
|
+
super({ code, message, status: 403, requestId });
|
|
128
|
+
this.name = "ForbiddenError";
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
class NotFoundError extends ZenAdminError {
|
|
133
|
+
constructor(code, message, requestId) {
|
|
134
|
+
super({ code, message, status: 404, requestId });
|
|
135
|
+
this.name = "NotFoundError";
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
class ConflictError extends ZenAdminError {
|
|
140
|
+
constructor(code, message, requestId) {
|
|
141
|
+
super({ code, message, status: 409, requestId });
|
|
142
|
+
this.name = "ConflictError";
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
class RateLimitError extends ZenAdminError {
|
|
147
|
+
retryAfterSeconds;
|
|
148
|
+
constructor(message, retryAfterSeconds, requestId) {
|
|
149
|
+
super({ code: "RATE_LIMITED", message, status: 429, requestId });
|
|
150
|
+
this.name = "RateLimitError";
|
|
151
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
class NetworkError extends ZenAdminError {
|
|
156
|
+
constructor(message) {
|
|
157
|
+
super({ code: "NETWORK_ERROR", message, status: 0 });
|
|
158
|
+
this.name = "NetworkError";
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
class NotSignedInError extends ZenAdminError {
|
|
163
|
+
constructor() {
|
|
164
|
+
super({
|
|
165
|
+
code: "NOT_SIGNED_IN",
|
|
166
|
+
message: "No ZenAdmin token. Create one in ZenAdmin (Zen AI > Access tokens), then run `zen login` or set ZENADMIN_TOKEN.",
|
|
167
|
+
status: 0
|
|
168
|
+
});
|
|
169
|
+
this.name = "NotSignedInError";
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function errorFromResponse(status, body, requestId, retryAfter) {
|
|
173
|
+
const data = body && typeof body === "object" ? body : {};
|
|
174
|
+
const code = typeof data.code === "string" ? data.code : `HTTP_${status}`;
|
|
175
|
+
const message = typeof data.message === "string" && data.message ? data.message : `ZenAdmin answered with status ${status}.`;
|
|
176
|
+
switch (status) {
|
|
177
|
+
case 401:
|
|
178
|
+
return new AuthenticationError(message, requestId, code);
|
|
179
|
+
case 403:
|
|
180
|
+
return new ForbiddenError(code, message, requestId);
|
|
181
|
+
case 404:
|
|
182
|
+
return new NotFoundError(code, message, requestId);
|
|
183
|
+
case 409:
|
|
184
|
+
return new ConflictError(code, message, requestId);
|
|
185
|
+
case 429: {
|
|
186
|
+
const seconds = Number(retryAfter);
|
|
187
|
+
return new RateLimitError(message, Number.isFinite(seconds) && seconds > 0 ? seconds : 60, requestId);
|
|
188
|
+
}
|
|
189
|
+
default:
|
|
190
|
+
return new ZenAdminError({ code, message, status, requestId });
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// packages/sdk/src/types.ts
|
|
195
|
+
import { z } from "zod";
|
|
196
|
+
var ChannelSchema = z.enum(["console", "mcp", "cli"]);
|
|
197
|
+
var WhoAmISchema = z.object({
|
|
198
|
+
company_id: z.string(),
|
|
199
|
+
company_name: z.string().nullish(),
|
|
200
|
+
user_id: z.string(),
|
|
201
|
+
channel: ChannelSchema,
|
|
202
|
+
token: z.object({
|
|
203
|
+
id: z.string(),
|
|
204
|
+
kind: z.enum(["personal", "automation", "connection"]),
|
|
205
|
+
scopes: z.array(z.enum(["read", "propose"])),
|
|
206
|
+
expires_at: z.string()
|
|
207
|
+
}).nullable(),
|
|
208
|
+
can_propose: z.boolean()
|
|
209
|
+
});
|
|
210
|
+
var ImpactSchema = z.enum(["none", "low", "medium", "high"]);
|
|
211
|
+
var ToolSchema = z.object({
|
|
212
|
+
id: z.string().regex(/^[a-z_]+\.[a-z_]+$/),
|
|
213
|
+
label: z.string(),
|
|
214
|
+
description: z.string(),
|
|
215
|
+
kind: z.enum(["read", "write"]),
|
|
216
|
+
module: z.string(),
|
|
217
|
+
requires_approval: z.boolean(),
|
|
218
|
+
impact: ImpactSchema,
|
|
219
|
+
scope: z.enum(["read", "propose"]),
|
|
220
|
+
input_schema: z.record(z.string(), z.unknown())
|
|
221
|
+
});
|
|
222
|
+
var ToolListSchema = z.object({ tools: z.array(ToolSchema) });
|
|
223
|
+
var LinkSchema = z.object({ label: z.string(), href: z.string() });
|
|
224
|
+
var OperationStatusSchema = z.enum([
|
|
225
|
+
"pending_approval",
|
|
226
|
+
"running",
|
|
227
|
+
"succeeded",
|
|
228
|
+
"failed",
|
|
229
|
+
"cancelled",
|
|
230
|
+
"expired",
|
|
231
|
+
"unknown"
|
|
232
|
+
]);
|
|
233
|
+
var OperationSchema = z.object({
|
|
234
|
+
id: z.string(),
|
|
235
|
+
action: z.string(),
|
|
236
|
+
status: OperationStatusSchema,
|
|
237
|
+
payload_hash: z.string(),
|
|
238
|
+
title: z.string(),
|
|
239
|
+
fields: z.array(z.object({ label: z.string(), value: z.string(), from: z.string().nullish() })),
|
|
240
|
+
effects: z.array(z.string()),
|
|
241
|
+
links: z.array(LinkSchema),
|
|
242
|
+
impact: z.enum(["low", "medium", "high"]),
|
|
243
|
+
expires_at: z.string(),
|
|
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(),
|
|
249
|
+
error: z.object({ code: z.string(), message: z.string() }).nullable(),
|
|
250
|
+
channel: ChannelSchema.optional(),
|
|
251
|
+
approval_url: z.string().nullable().optional()
|
|
252
|
+
});
|
|
253
|
+
function outcomeOf(operation) {
|
|
254
|
+
switch (operation.status) {
|
|
255
|
+
case "pending_approval":
|
|
256
|
+
return "approval_required";
|
|
257
|
+
case "running":
|
|
258
|
+
return "in_progress";
|
|
259
|
+
case "succeeded":
|
|
260
|
+
return operation.result?.partial ? "partial" : "completed";
|
|
261
|
+
case "failed":
|
|
262
|
+
return "failed";
|
|
263
|
+
case "cancelled":
|
|
264
|
+
return "cancelled";
|
|
265
|
+
case "expired":
|
|
266
|
+
return "expired";
|
|
267
|
+
default:
|
|
268
|
+
return "check_result";
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
var isSettled = (operation) => !["pending_approval", "running"].includes(operation.status);
|
|
272
|
+
var isReusable = (operation) => !isSettled(operation) || operation.status === "unknown";
|
|
273
|
+
var ReadResultSchema = z.record(z.string(), z.unknown());
|
|
274
|
+
|
|
275
|
+
// packages/sdk/src/client.ts
|
|
276
|
+
var SDK_VERSION = "0.3.0";
|
|
277
|
+
var REQUEST_ID = /^[A-Za-z0-9_-]{8,64}$/;
|
|
278
|
+
var RETRY_STATUSES = new Set([429, 502, 503, 504]);
|
|
279
|
+
var defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
280
|
+
function abortable(wait, signal) {
|
|
281
|
+
if (!signal)
|
|
282
|
+
return wait;
|
|
283
|
+
signal.throwIfAborted();
|
|
284
|
+
return new Promise((resolve, reject) => {
|
|
285
|
+
const onAbort = () => reject(signal.reason);
|
|
286
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
287
|
+
wait.then(() => {
|
|
288
|
+
signal.removeEventListener("abort", onAbort);
|
|
289
|
+
resolve();
|
|
290
|
+
}, (error) => {
|
|
291
|
+
signal.removeEventListener("abort", onAbort);
|
|
292
|
+
reject(error);
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
class ZenAdmin {
|
|
298
|
+
apiUrl;
|
|
299
|
+
client;
|
|
300
|
+
token;
|
|
301
|
+
userAgent;
|
|
302
|
+
timeoutMs;
|
|
303
|
+
maxRetries;
|
|
304
|
+
fetchImpl;
|
|
305
|
+
log;
|
|
306
|
+
sleep;
|
|
307
|
+
requestIdPrefix;
|
|
308
|
+
maxRetryWaitMs;
|
|
309
|
+
requestCount = 0;
|
|
310
|
+
constructor(options) {
|
|
311
|
+
if (!options.token)
|
|
312
|
+
throw new NotSignedInError;
|
|
313
|
+
this.token = options.token;
|
|
314
|
+
this.apiUrl = normaliseApiUrl(options.apiUrl ?? DEFAULT_API_URL);
|
|
315
|
+
this.client = options.client ?? "cli";
|
|
316
|
+
this.userAgent = options.userAgent ?? `zenadmin-sdk/${SDK_VERSION} (node ${process.version})`;
|
|
317
|
+
this.timeoutMs = options.timeoutMs ?? 30000;
|
|
318
|
+
this.maxRetries = options.maxRetries ?? 3;
|
|
319
|
+
this.fetchImpl = options.fetch ?? fetch;
|
|
320
|
+
this.log = options.log;
|
|
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
|
+
}
|
|
329
|
+
}
|
|
330
|
+
static async fromEnvironment(options = {}, env = process.env) {
|
|
331
|
+
const creds = await resolveCredentials({ token: options.token, apiUrl: options.apiUrl }, env);
|
|
332
|
+
if (!creds.token)
|
|
333
|
+
throw new NotSignedInError;
|
|
334
|
+
return new ZenAdmin({ ...options, token: creds.token, apiUrl: creds.apiUrl });
|
|
335
|
+
}
|
|
336
|
+
whoami() {
|
|
337
|
+
return this.request("GET", "/whoami", WhoAmISchema);
|
|
338
|
+
}
|
|
339
|
+
async tools() {
|
|
340
|
+
return (await this.request("GET", "/tools", ToolListSchema)).tools;
|
|
341
|
+
}
|
|
342
|
+
read(toolId, input) {
|
|
343
|
+
return this.request("POST", `/actions/${encodeURIComponent(toolId)}`, ReadResultSchema, { input });
|
|
344
|
+
}
|
|
345
|
+
async propose(actionId, input, options = {}) {
|
|
346
|
+
const requestId = options.requestId ?? randomUUID();
|
|
347
|
+
if (!REQUEST_ID.test(requestId)) {
|
|
348
|
+
throw new ZenAdminError({
|
|
349
|
+
code: "INVALID_INPUT",
|
|
350
|
+
message: "A request ID is 8–64 letters, digits, - or _.",
|
|
351
|
+
status: 0
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
const operation = await this.request("POST", `/actions/${encodeURIComponent(actionId)}/propose`, OperationSchema, { input, client_request_id: requestId });
|
|
355
|
+
return { ...operation, request_id: requestId };
|
|
356
|
+
}
|
|
357
|
+
operation(id, options = {}) {
|
|
358
|
+
return this.request("GET", `/operations/${encodeURIComponent(id)}`, OperationSchema, undefined, options.signal);
|
|
359
|
+
}
|
|
360
|
+
async operationByRequest(requestId) {
|
|
361
|
+
if (!REQUEST_ID.test(requestId))
|
|
362
|
+
return null;
|
|
363
|
+
try {
|
|
364
|
+
return await this.request("GET", `/operations/by-request/${encodeURIComponent(requestId)}`, OperationSchema);
|
|
365
|
+
} catch (error) {
|
|
366
|
+
if (error instanceof NotFoundError)
|
|
367
|
+
return null;
|
|
368
|
+
throw error;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
cancel(id) {
|
|
372
|
+
return this.request("POST", `/operations/${encodeURIComponent(id)}/cancel`, OperationSchema, {});
|
|
373
|
+
}
|
|
374
|
+
async waitForOperation(id, options = {}) {
|
|
375
|
+
const deadline = Date.now() + (options.timeoutMs ?? 10 * 60000);
|
|
376
|
+
const interval = Math.max(1000, options.intervalMs ?? 3000);
|
|
377
|
+
let last = null;
|
|
378
|
+
const signal = options.signal;
|
|
379
|
+
for (;; ) {
|
|
380
|
+
signal?.throwIfAborted();
|
|
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
|
+
}
|
|
394
|
+
signal?.throwIfAborted();
|
|
395
|
+
if (!last || last.status !== operation.status)
|
|
396
|
+
options.onUpdate?.(operation);
|
|
397
|
+
last = operation;
|
|
398
|
+
if (isSettled(operation))
|
|
399
|
+
return { operation, timedOut: false };
|
|
400
|
+
if (Date.now() + interval > deadline)
|
|
401
|
+
return { operation, timedOut: true };
|
|
402
|
+
await abortable(this.sleep(interval), signal);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
async request(method, path, schema, body, signal) {
|
|
406
|
+
const url = `${this.apiUrl}/api/agent/v1${path}`;
|
|
407
|
+
let attempt = 0;
|
|
408
|
+
for (;; ) {
|
|
409
|
+
signal?.throwIfAborted();
|
|
410
|
+
const requestId = this.requestIdPrefix ? `${this.requestIdPrefix}.${++this.requestCount}` : randomUUID();
|
|
411
|
+
const requestSignal = signal ? AbortSignal.any([signal, AbortSignal.timeout(this.timeoutMs)]) : AbortSignal.timeout(this.timeoutMs);
|
|
412
|
+
let response;
|
|
413
|
+
try {
|
|
414
|
+
response = await this.fetchImpl(url, {
|
|
415
|
+
method,
|
|
416
|
+
headers: {
|
|
417
|
+
Authorization: `Bearer ${this.token}`,
|
|
418
|
+
Accept: "application/json",
|
|
419
|
+
"User-Agent": this.userAgent,
|
|
420
|
+
"X-ZenAdmin-Client": this.client,
|
|
421
|
+
"X-Request-Id": requestId,
|
|
422
|
+
...body === undefined ? {} : { "Content-Type": "application/json" }
|
|
423
|
+
},
|
|
424
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
425
|
+
signal: requestSignal
|
|
426
|
+
});
|
|
427
|
+
} catch (error) {
|
|
428
|
+
signal?.throwIfAborted();
|
|
429
|
+
const failure = new NetworkError(`Could not reach ZenAdmin at ${this.apiUrl}: ${error instanceof Error ? error.message : String(error)}`);
|
|
430
|
+
if (attempt < this.maxRetries) {
|
|
431
|
+
attempt++;
|
|
432
|
+
await abortable(this.sleep(250 * 2 ** attempt), signal);
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
throw failure;
|
|
436
|
+
}
|
|
437
|
+
const answeredId = response.headers.get("x-request-id") ?? requestId;
|
|
438
|
+
this.log?.(`${method} ${path} → ${response.status} (request ${answeredId})`);
|
|
439
|
+
let json = null;
|
|
440
|
+
try {
|
|
441
|
+
json = await response.json();
|
|
442
|
+
} catch {
|
|
443
|
+
json = null;
|
|
444
|
+
}
|
|
445
|
+
if (!response.ok) {
|
|
446
|
+
const error = errorFromResponse(response.status, json, answeredId, response.headers.get("retry-after"));
|
|
447
|
+
const tooLong = error instanceof RateLimitError && error.retryAfterSeconds * 1000 > this.maxRetryWaitMs;
|
|
448
|
+
if (RETRY_STATUSES.has(response.status) && attempt < this.maxRetries && !tooLong) {
|
|
449
|
+
attempt++;
|
|
450
|
+
const wait = error instanceof RateLimitError ? error.retryAfterSeconds * 1000 : 250 * 2 ** attempt;
|
|
451
|
+
await abortable(this.sleep(wait), signal);
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
throw error;
|
|
455
|
+
}
|
|
456
|
+
const envelope = json;
|
|
457
|
+
const parsed = envelope && envelope.error === false ? schema.safeParse(envelope.data) : null;
|
|
458
|
+
if (!parsed?.success) {
|
|
459
|
+
throw new ZenAdminError({
|
|
460
|
+
code: "INVALID_RESPONSE",
|
|
461
|
+
message: `ZenAdmin sent an unexpected response to ${method} ${path}. Update @zenadmin packages, or contact support with this request ID.`,
|
|
462
|
+
status: response.status,
|
|
463
|
+
requestId: answeredId
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
return parsed.data;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
export {
|
|
471
|
+
AuthenticationError,
|
|
472
|
+
ChannelSchema,
|
|
473
|
+
ConflictError,
|
|
474
|
+
DEFAULT_API_URL,
|
|
475
|
+
ForbiddenError,
|
|
476
|
+
ImpactSchema,
|
|
477
|
+
NetworkError,
|
|
478
|
+
NotFoundError,
|
|
479
|
+
NotSignedInError,
|
|
480
|
+
OperationSchema,
|
|
481
|
+
OperationStatusSchema,
|
|
482
|
+
RateLimitError,
|
|
483
|
+
ReadResultSchema,
|
|
484
|
+
SDK_VERSION,
|
|
485
|
+
ToolListSchema,
|
|
486
|
+
ToolSchema,
|
|
487
|
+
WhoAmISchema,
|
|
488
|
+
ZenAdmin,
|
|
489
|
+
ZenAdminError,
|
|
490
|
+
clearCredentials,
|
|
491
|
+
credentialsPath,
|
|
492
|
+
errorFromResponse,
|
|
493
|
+
isReusable,
|
|
494
|
+
isSettled,
|
|
495
|
+
looksLikeToken,
|
|
496
|
+
normaliseApiUrl,
|
|
497
|
+
outcomeOf,
|
|
498
|
+
resolveCredentials,
|
|
499
|
+
storeCredentials,
|
|
500
|
+
withoutDotenvSettings
|
|
501
|
+
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The agent API contract (hybr1d-backend docs/agent-layer.md, "MCP and CLI clients"). Responses are
|
|
3
|
+
* validated here so a server change surfaces as a clear error instead of a wrong answer.
|
|
4
|
+
*/
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
export declare const ChannelSchema: z.ZodEnum<{
|
|
7
|
+
console: "console";
|
|
8
|
+
mcp: "mcp";
|
|
9
|
+
cli: "cli";
|
|
10
|
+
}>;
|
|
11
|
+
export declare const WhoAmISchema: z.ZodObject<{
|
|
12
|
+
company_id: z.ZodString;
|
|
13
|
+
company_name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
14
|
+
user_id: z.ZodString;
|
|
15
|
+
channel: z.ZodEnum<{
|
|
16
|
+
console: "console";
|
|
17
|
+
mcp: "mcp";
|
|
18
|
+
cli: "cli";
|
|
19
|
+
}>;
|
|
20
|
+
token: z.ZodNullable<z.ZodObject<{
|
|
21
|
+
id: z.ZodString;
|
|
22
|
+
kind: z.ZodEnum<{
|
|
23
|
+
personal: "personal";
|
|
24
|
+
automation: "automation";
|
|
25
|
+
connection: "connection";
|
|
26
|
+
}>;
|
|
27
|
+
scopes: z.ZodArray<z.ZodEnum<{
|
|
28
|
+
read: "read";
|
|
29
|
+
propose: "propose";
|
|
30
|
+
}>>;
|
|
31
|
+
expires_at: z.ZodString;
|
|
32
|
+
}, z.core.$strip>>;
|
|
33
|
+
can_propose: z.ZodBoolean;
|
|
34
|
+
}, z.core.$strip>;
|
|
35
|
+
export type WhoAmI = z.infer<typeof WhoAmISchema>;
|
|
36
|
+
export declare const ImpactSchema: z.ZodEnum<{
|
|
37
|
+
none: "none";
|
|
38
|
+
low: "low";
|
|
39
|
+
medium: "medium";
|
|
40
|
+
high: "high";
|
|
41
|
+
}>;
|
|
42
|
+
/** One tool the caller can use now. Every tool is classified: read or write, impact, scope. */
|
|
43
|
+
export declare const ToolSchema: z.ZodObject<{
|
|
44
|
+
id: z.ZodString;
|
|
45
|
+
label: z.ZodString;
|
|
46
|
+
description: z.ZodString;
|
|
47
|
+
kind: z.ZodEnum<{
|
|
48
|
+
read: "read";
|
|
49
|
+
write: "write";
|
|
50
|
+
}>;
|
|
51
|
+
module: z.ZodString;
|
|
52
|
+
requires_approval: z.ZodBoolean;
|
|
53
|
+
impact: z.ZodEnum<{
|
|
54
|
+
none: "none";
|
|
55
|
+
low: "low";
|
|
56
|
+
medium: "medium";
|
|
57
|
+
high: "high";
|
|
58
|
+
}>;
|
|
59
|
+
scope: z.ZodEnum<{
|
|
60
|
+
read: "read";
|
|
61
|
+
propose: "propose";
|
|
62
|
+
}>;
|
|
63
|
+
input_schema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
64
|
+
}, z.core.$strip>;
|
|
65
|
+
export type Tool = z.infer<typeof ToolSchema>;
|
|
66
|
+
export declare const ToolListSchema: z.ZodObject<{
|
|
67
|
+
tools: z.ZodArray<z.ZodObject<{
|
|
68
|
+
id: z.ZodString;
|
|
69
|
+
label: z.ZodString;
|
|
70
|
+
description: z.ZodString;
|
|
71
|
+
kind: z.ZodEnum<{
|
|
72
|
+
read: "read";
|
|
73
|
+
write: "write";
|
|
74
|
+
}>;
|
|
75
|
+
module: z.ZodString;
|
|
76
|
+
requires_approval: z.ZodBoolean;
|
|
77
|
+
impact: z.ZodEnum<{
|
|
78
|
+
none: "none";
|
|
79
|
+
low: "low";
|
|
80
|
+
medium: "medium";
|
|
81
|
+
high: "high";
|
|
82
|
+
}>;
|
|
83
|
+
scope: z.ZodEnum<{
|
|
84
|
+
read: "read";
|
|
85
|
+
propose: "propose";
|
|
86
|
+
}>;
|
|
87
|
+
input_schema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
88
|
+
}, z.core.$strip>>;
|
|
89
|
+
}, z.core.$strip>;
|
|
90
|
+
export declare const OperationStatusSchema: z.ZodEnum<{
|
|
91
|
+
unknown: "unknown";
|
|
92
|
+
pending_approval: "pending_approval";
|
|
93
|
+
running: "running";
|
|
94
|
+
succeeded: "succeeded";
|
|
95
|
+
failed: "failed";
|
|
96
|
+
cancelled: "cancelled";
|
|
97
|
+
expired: "expired";
|
|
98
|
+
}>;
|
|
99
|
+
export type OperationStatus = z.infer<typeof OperationStatusSchema>;
|
|
100
|
+
export declare const OperationSchema: z.ZodObject<{
|
|
101
|
+
id: z.ZodString;
|
|
102
|
+
action: z.ZodString;
|
|
103
|
+
status: z.ZodEnum<{
|
|
104
|
+
unknown: "unknown";
|
|
105
|
+
pending_approval: "pending_approval";
|
|
106
|
+
running: "running";
|
|
107
|
+
succeeded: "succeeded";
|
|
108
|
+
failed: "failed";
|
|
109
|
+
cancelled: "cancelled";
|
|
110
|
+
expired: "expired";
|
|
111
|
+
}>;
|
|
112
|
+
payload_hash: z.ZodString;
|
|
113
|
+
title: z.ZodString;
|
|
114
|
+
fields: z.ZodArray<z.ZodObject<{
|
|
115
|
+
label: z.ZodString;
|
|
116
|
+
value: z.ZodString;
|
|
117
|
+
from: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
118
|
+
}, z.core.$strip>>;
|
|
119
|
+
effects: z.ZodArray<z.ZodString>;
|
|
120
|
+
links: z.ZodArray<z.ZodObject<{
|
|
121
|
+
label: z.ZodString;
|
|
122
|
+
href: z.ZodString;
|
|
123
|
+
}, z.core.$strip>>;
|
|
124
|
+
impact: z.ZodEnum<{
|
|
125
|
+
low: "low";
|
|
126
|
+
medium: "medium";
|
|
127
|
+
high: "high";
|
|
128
|
+
}>;
|
|
129
|
+
expires_at: z.ZodString;
|
|
130
|
+
result: z.ZodNullable<z.ZodObject<{
|
|
131
|
+
message: z.ZodString;
|
|
132
|
+
links: z.ZodArray<z.ZodObject<{
|
|
133
|
+
label: z.ZodString;
|
|
134
|
+
href: z.ZodString;
|
|
135
|
+
}, z.core.$strip>>;
|
|
136
|
+
partial: z.ZodOptional<z.ZodObject<{
|
|
137
|
+
done: z.ZodNumber;
|
|
138
|
+
total: z.ZodNumber;
|
|
139
|
+
}, z.core.$strip>>;
|
|
140
|
+
}, z.core.$strip>>;
|
|
141
|
+
error: z.ZodNullable<z.ZodObject<{
|
|
142
|
+
code: z.ZodString;
|
|
143
|
+
message: z.ZodString;
|
|
144
|
+
}, z.core.$strip>>;
|
|
145
|
+
channel: z.ZodOptional<z.ZodEnum<{
|
|
146
|
+
console: "console";
|
|
147
|
+
mcp: "mcp";
|
|
148
|
+
cli: "cli";
|
|
149
|
+
}>>;
|
|
150
|
+
approval_url: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
151
|
+
}, z.core.$strip>;
|
|
152
|
+
export type Operation = z.infer<typeof OperationSchema>;
|
|
153
|
+
/**
|
|
154
|
+
* What an operation's status means for the caller. A token can never approve, so a new proposal
|
|
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.
|
|
157
|
+
*/
|
|
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;
|
|
162
|
+
/** Final statuses: nothing more will happen to the operation. */
|
|
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;
|
|
171
|
+
/** Read tool results are passed through as the server returns them (bounded lists with links). */
|
|
172
|
+
export declare const ReadResultSchema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
173
|
+
export type ReadResult = z.infer<typeof ReadResultSchema>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenadmin-ai/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "TypeScript client for the ZenAdmin agent API: read tools and approval-bound changes.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"license": "UNLICENSED",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/Usehybrid/mewtwo.git"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/Usehybrid/mewtwo#readme",
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=22",
|
|
26
|
+
"bun": ">=1.4"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"typecheck": "tsc --noEmit",
|
|
33
|
+
"test": "bun test"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"zod": "^4.6.6"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "^25.4.0",
|
|
40
|
+
"bun-types": "^1.3.10",
|
|
41
|
+
"typescript": "^5.9.3"
|
|
42
|
+
}
|
|
43
|
+
}
|