agentguardiam-node-sdk 0.2.0 → 0.2.1
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 +17 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +70 -28
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,3 +13,20 @@ const result = await gateway.task("Read staging customer", {
|
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
Pass both `userId` and the matching `userToken` to mint a task for a human principal. The dashboard token is sent only on the broker's mint call. A positive wait in seconds as the third argument to `task.call` waits for an existing human approval, subject to the task expiry, and retries the exact call once. `Held` exposes the approval ID and `Denied` exposes the decision code.
|
|
16
|
+
|
|
17
|
+
To pause for a human approval across requests, keep the grant (0.2.1+) and resume with the same arguments:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
const grant = await gateway.acquireTask("Refund order 5521", { tools: ["payments_transfer"], userId, userToken });
|
|
21
|
+
try {
|
|
22
|
+
await grant.session((task) => task.call("payments_transfer", args));
|
|
23
|
+
} catch (error) {
|
|
24
|
+
if (!(error instanceof Held)) throw error;
|
|
25
|
+
// ... later, when grant.approvalStatus(error.approvalId) is "approved":
|
|
26
|
+
await grant.session((task) => task.call("payments_transfer", args));
|
|
27
|
+
} finally {
|
|
28
|
+
await grant.revoke();
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
A private CA is added with `NODE_EXTRA_CA_CERTS=<ca.pem>`, which extends Node's trust store.
|
package/dist/index.d.ts
CHANGED
|
@@ -23,6 +23,20 @@ export declare class AgentGuard {
|
|
|
23
23
|
#private;
|
|
24
24
|
constructor(baseUrl: string, agentKey: string);
|
|
25
25
|
task<T>(purpose: string, options: TaskOptions, useTask: (task: Task) => Promise<T>): Promise<T>;
|
|
26
|
+
/**
|
|
27
|
+
* Mint a grant that outlives one callback: keep it across a pause for human approval, then call
|
|
28
|
+
* `grant.session(...)` again with the SAME arguments and finally `grant.revoke()` (parity with the Python SDK).
|
|
29
|
+
*/
|
|
30
|
+
acquireTask(purpose: string, options: TaskOptions): Promise<TaskGrant>;
|
|
31
|
+
}
|
|
32
|
+
/** A task credential whose sessions share one task identity. */
|
|
33
|
+
export declare class TaskGrant {
|
|
34
|
+
#private;
|
|
35
|
+
readonly expiresAt: number;
|
|
36
|
+
constructor(baseUrl: string, agentHeaders: Record<string, string>, jti: string, token: string, expiresAt: number);
|
|
37
|
+
session<T>(useTask: (task: Task) => Promise<T>): Promise<T>;
|
|
38
|
+
approvalStatus(approvalId: string): Promise<string | null>;
|
|
39
|
+
revoke(): Promise<void>;
|
|
26
40
|
}
|
|
27
41
|
export declare class Task {
|
|
28
42
|
#private;
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,7 @@ const REQUESTED_TTL_CAP = 2 ** 31 - 1;
|
|
|
7
7
|
const APPROVAL_MARGIN_MS = 5_000;
|
|
8
8
|
const POLL_INTERVAL_MS = 2_000;
|
|
9
9
|
const REQUEST_TIMEOUT_MS = 10_000;
|
|
10
|
+
const SDK_VERSION = "0.2.1";
|
|
10
11
|
export class Denied extends Error {
|
|
11
12
|
code;
|
|
12
13
|
guard;
|
|
@@ -39,6 +40,30 @@ export class AgentGuard {
|
|
|
39
40
|
this.#agentKey = agentKey;
|
|
40
41
|
}
|
|
41
42
|
async task(purpose, options, useTask) {
|
|
43
|
+
const grant = await this.acquireTask(purpose, options);
|
|
44
|
+
let taskFailed = false;
|
|
45
|
+
try {
|
|
46
|
+
return await grant.session(useTask);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
taskFailed = true;
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
try {
|
|
54
|
+
await grant.revoke();
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
if (!taskFailed)
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Mint a grant that outlives one callback: keep it across a pause for human approval, then call
|
|
64
|
+
* `grant.session(...)` again with the SAME arguments and finally `grant.revoke()` (parity with the Python SDK).
|
|
65
|
+
*/
|
|
66
|
+
async acquireTask(purpose, options) {
|
|
42
67
|
const agentHeaders = { "X-Agent-Key": this.#agentKey };
|
|
43
68
|
// The signed-in user's bearer is sent to the broker for human attribution only.
|
|
44
69
|
const mintHeaders = options.userToken === undefined
|
|
@@ -53,40 +78,57 @@ export class AgentGuard {
|
|
|
53
78
|
});
|
|
54
79
|
if (typeof grant.jti !== "string" || !grant.jti)
|
|
55
80
|
throw internalError("Invalid task grant");
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
81
|
+
const expiresAt = typeof grant.expires_at === "string" ? Date.parse(grant.expires_at) : NaN;
|
|
82
|
+
if (typeof grant.token !== "string" || !grant.token || !Number.isFinite(expiresAt)) {
|
|
83
|
+
try {
|
|
84
|
+
await brokerCall(this.#baseUrl, agentHeaders, "revoke_task_token", { jti: grant.jti });
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
// the grant is unusable either way
|
|
62
88
|
}
|
|
89
|
+
throw internalError("Invalid task grant");
|
|
90
|
+
}
|
|
91
|
+
return new TaskGrant(this.#baseUrl, agentHeaders, grant.jti, grant.token, expiresAt);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** A task credential whose sessions share one task identity. */
|
|
95
|
+
export class TaskGrant {
|
|
96
|
+
#baseUrl;
|
|
97
|
+
#agentHeaders;
|
|
98
|
+
#jti;
|
|
99
|
+
#token;
|
|
100
|
+
expiresAt;
|
|
101
|
+
constructor(baseUrl, agentHeaders, jti, token, expiresAt) {
|
|
102
|
+
this.#baseUrl = baseUrl;
|
|
103
|
+
this.#agentHeaders = agentHeaders;
|
|
104
|
+
this.#jti = jti;
|
|
105
|
+
this.#token = token;
|
|
106
|
+
this.expiresAt = expiresAt;
|
|
107
|
+
}
|
|
108
|
+
async session(useTask) {
|
|
109
|
+
if (!this.#token)
|
|
110
|
+
throw new Denied("token_revoked", "token_broker", "Task grant was revoked", "");
|
|
111
|
+
const client = new Client({ name: "agentguardiam-node", version: SDK_VERSION });
|
|
112
|
+
try {
|
|
63
113
|
await client.connect(new StreamableHTTPClientTransport(new URL(`${this.#baseUrl}/gw/mcp`), {
|
|
64
|
-
requestInit: { headers: { Authorization: `Bearer ${
|
|
114
|
+
requestInit: { headers: { Authorization: `Bearer ${this.#token}` }, redirect: "error" },
|
|
65
115
|
}));
|
|
66
|
-
return await useTask(new Task(client, this.#baseUrl, agentHeaders, expiresAt));
|
|
67
|
-
}
|
|
68
|
-
catch (error) {
|
|
69
|
-
taskFailed = true;
|
|
70
|
-
throw error;
|
|
116
|
+
return await useTask(new Task(client, this.#baseUrl, this.#agentHeaders, this.expiresAt));
|
|
71
117
|
}
|
|
72
118
|
finally {
|
|
73
|
-
|
|
74
|
-
try {
|
|
75
|
-
await client.close();
|
|
76
|
-
}
|
|
77
|
-
catch (error) {
|
|
78
|
-
teardownError = error;
|
|
79
|
-
}
|
|
80
|
-
try {
|
|
81
|
-
await brokerCall(this.#baseUrl, agentHeaders, "revoke_task_token", { jti: grant.jti });
|
|
82
|
-
}
|
|
83
|
-
catch (error) {
|
|
84
|
-
teardownError ??= error;
|
|
85
|
-
}
|
|
86
|
-
if (!taskFailed && teardownError !== undefined)
|
|
87
|
-
throw teardownError;
|
|
119
|
+
await client.close();
|
|
88
120
|
}
|
|
89
121
|
}
|
|
122
|
+
async approvalStatus(approvalId) {
|
|
123
|
+
const review = await brokerCall(this.#baseUrl, this.#agentHeaders, "get_approval_status", { approval_id: approvalId });
|
|
124
|
+
return typeof review.status === "string" ? review.status : null;
|
|
125
|
+
}
|
|
126
|
+
async revoke() {
|
|
127
|
+
if (!this.#token)
|
|
128
|
+
return;
|
|
129
|
+
this.#token = "";
|
|
130
|
+
await brokerCall(this.#baseUrl, this.#agentHeaders, "revoke_task_token", { jti: this.#jti });
|
|
131
|
+
}
|
|
90
132
|
}
|
|
91
133
|
export class Task {
|
|
92
134
|
#client;
|
|
@@ -170,7 +212,7 @@ export class Task {
|
|
|
170
212
|
}
|
|
171
213
|
/** Calls one task-token broker tool over MCP with the agent key (and, when minting, the user bearer). */
|
|
172
214
|
async function brokerCall(baseUrl, headers, tool, args, timeoutMs = REQUEST_TIMEOUT_MS) {
|
|
173
|
-
const client = new Client({ name: "agentguardiam-node", version:
|
|
215
|
+
const client = new Client({ name: "agentguardiam-node", version: SDK_VERSION });
|
|
174
216
|
await client.connect(new StreamableHTTPClientTransport(new URL(`${baseUrl}/gw/broker/mcp`), { requestInit: { headers, redirect: "error" } }), { timeout: timeoutMs });
|
|
175
217
|
try {
|
|
176
218
|
const result = await client.callTool({ name: tool, arguments: args }, undefined, { timeout: timeoutMs });
|