@deliverd/sdk 0.33.0 → 0.35.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 +37 -0
- package/dist/adapters/claude-managed-agents.d.ts +152 -0
- package/dist/adapters/claude-managed-agents.d.ts.map +1 -0
- package/dist/adapters/claude-managed-agents.js +165 -0
- package/dist/adapters/index.d.ts +4 -0
- package/dist/adapters/index.d.ts.map +1 -1
- package/dist/adapters/index.js +2 -0
- package/dist/adapters/ui-sdk.d.ts +116 -0
- package/dist/adapters/ui-sdk.d.ts.map +1 -0
- package/dist/adapters/ui-sdk.js +164 -0
- package/dist/released.d.ts +1 -1
- package/dist/released.d.ts.map +1 -1
- package/dist/released.js +2975 -0
- package/package.json +1 -1
- package/src/adapters/claude-managed-agents.ts +293 -0
- package/src/adapters/index.ts +15 -0
- package/src/adapters/ui-sdk.ts +263 -0
- package/src/released.ts +2977 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { askAboutCall } from "./shared.js";
|
|
2
|
+
/**
|
|
3
|
+
* UI SDK's `allow` hook, answered by a person through Deliverd.
|
|
4
|
+
*
|
|
5
|
+
* Anything other than a clear yes from your route is a no: a network failure,
|
|
6
|
+
* an error status, an answer it cannot read. UI SDK then tells the agent the
|
|
7
|
+
* person said no, and the control is not touched.
|
|
8
|
+
*/
|
|
9
|
+
export function deliverdAllow(options) {
|
|
10
|
+
const needsPerson = options.when ?? ((step) => Boolean(step.confirm));
|
|
11
|
+
return async (step, context) => {
|
|
12
|
+
if (!needsPerson(step))
|
|
13
|
+
return true;
|
|
14
|
+
const doFetch = options.fetch ?? globalThis.fetch;
|
|
15
|
+
const headers = typeof options.headers === "function" ? options.headers() : options.headers;
|
|
16
|
+
let answer;
|
|
17
|
+
try {
|
|
18
|
+
const response = await doFetch(options.endpoint, {
|
|
19
|
+
method: "POST",
|
|
20
|
+
headers: { "content-type": "application/json", ...headers },
|
|
21
|
+
body: JSON.stringify({ step: pickStep(step), nonce: nonce() }),
|
|
22
|
+
signal: context?.signal,
|
|
23
|
+
});
|
|
24
|
+
const body = (await response.json().catch(() => null));
|
|
25
|
+
answer =
|
|
26
|
+
response.ok && body?.allowed === true
|
|
27
|
+
? { allowed: true, decidedBy: typeof body.decidedBy === "string" ? body.decidedBy : null }
|
|
28
|
+
: {
|
|
29
|
+
allowed: false,
|
|
30
|
+
reason: body && body.allowed === false && typeof body.reason === "string"
|
|
31
|
+
? body.reason
|
|
32
|
+
: `The approval route answered ${response.status}, so the step was not run.`,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
if (context?.signal?.aborted)
|
|
37
|
+
return false;
|
|
38
|
+
answer = { allowed: false, reason: "The approval route could not be reached, so the step was not run." };
|
|
39
|
+
}
|
|
40
|
+
options.onAnswer?.(answer, step);
|
|
41
|
+
return answer.allowed;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export const UI_SDK_DEFAULT_TIMEOUT = "4m";
|
|
45
|
+
/**
|
|
46
|
+
* The server half: a fetch-style `POST` handler (Next.js route handlers, Hono,
|
|
47
|
+
* Remix, Workers) that asks Deliverd about one UI SDK step and waits.
|
|
48
|
+
*
|
|
49
|
+
* Set `timeout` below both your platform's function limit and the bridge's
|
|
50
|
+
* own `timeout`, or the function is cut off before it can answer. The
|
|
51
|
+
* request's abort signal cancels the wait when the page stops asking.
|
|
52
|
+
*/
|
|
53
|
+
export function uiSdkApprovalRoute(options) {
|
|
54
|
+
const needsPerson = options.when ?? ((step) => Boolean(step.confirm));
|
|
55
|
+
const describe = options.describe ?? describeStep;
|
|
56
|
+
const timeout = options.timeout ?? UI_SDK_DEFAULT_TIMEOUT;
|
|
57
|
+
return async (request) => {
|
|
58
|
+
if (request.method !== "POST")
|
|
59
|
+
return json({ allowed: false, reason: "Use POST." }, 405);
|
|
60
|
+
let raw;
|
|
61
|
+
try {
|
|
62
|
+
const text = await request.text();
|
|
63
|
+
if (text.length > BODY_MAX)
|
|
64
|
+
return json({ allowed: false, reason: "The step is too large to ask about." }, 413);
|
|
65
|
+
raw = JSON.parse(text);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return json({ allowed: false, reason: "The step could not be read." }, 400);
|
|
69
|
+
}
|
|
70
|
+
const parsed = readBody(raw);
|
|
71
|
+
if (!parsed)
|
|
72
|
+
return json({ allowed: false, reason: "The step could not be read." }, 400);
|
|
73
|
+
const { step, nonce: id } = parsed;
|
|
74
|
+
if (!(await needsPerson(step, request))) {
|
|
75
|
+
return json({ allowed: false, reason: "This step is not one this route asks a person about." }, 403);
|
|
76
|
+
}
|
|
77
|
+
const name = stepName(step);
|
|
78
|
+
const outcome = await askAboutCall(name, { component: step.component, ref: step.ref, action: step.action, args: step.args ?? [] }, `ui-sdk:${await sha256(canonical({ step, id }))}`, {
|
|
79
|
+
...options,
|
|
80
|
+
when: undefined,
|
|
81
|
+
describe: () => describe(step),
|
|
82
|
+
timeout,
|
|
83
|
+
expiresIn: options.expiresIn ?? timeout,
|
|
84
|
+
metadata: { ...options.metadata, source: "ui-sdk" },
|
|
85
|
+
}, request.signal);
|
|
86
|
+
return json(outcome.allowed ? { allowed: true, decidedBy: outcome.decidedBy } : { allowed: false, reason: outcome.reason }, 200);
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/** What an approver reads about a step, when the app did not write it. */
|
|
90
|
+
export function describeStep(step) {
|
|
91
|
+
const target = step.label ? `"${step.label}"` : `the ${step.component}`;
|
|
92
|
+
const verb = step.action === "press" ? "press" : step.action === "setValue" ? "set" : step.action;
|
|
93
|
+
const values = (step.args ?? []).map((arg) => (typeof arg === "string" ? arg : JSON.stringify(arg))).join(", ");
|
|
94
|
+
const lines = [
|
|
95
|
+
step.confirm ? `The app says this: ${step.confirm}` : undefined,
|
|
96
|
+
`An agent wants to ${verb} ${target}${values ? ` (${truncate(values, 500)})` : ""} in the app.`,
|
|
97
|
+
].filter(Boolean);
|
|
98
|
+
return {
|
|
99
|
+
title: truncate(step.confirm ?? `Let the agent ${verb} ${target}`, 200),
|
|
100
|
+
description: lines.join("\n\n"),
|
|
101
|
+
risk: step.confirm ? "high" : "medium",
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const BODY_MAX = 16_000;
|
|
105
|
+
function stepName(step) {
|
|
106
|
+
return step.label ? `${step.action} "${step.label}"` : `${step.action} ${step.component}`;
|
|
107
|
+
}
|
|
108
|
+
/** Only the fields `AgentAction` documents, so nothing else on the object leaves the page. */
|
|
109
|
+
function pickStep(step) {
|
|
110
|
+
return {
|
|
111
|
+
ref: step.ref,
|
|
112
|
+
component: step.component,
|
|
113
|
+
...(step.label !== undefined ? { label: step.label } : {}),
|
|
114
|
+
action: step.action,
|
|
115
|
+
args: step.args ?? [],
|
|
116
|
+
...(step.confirm !== undefined ? { confirm: step.confirm } : {}),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function readBody(raw) {
|
|
120
|
+
if (typeof raw !== "object" || raw === null)
|
|
121
|
+
return null;
|
|
122
|
+
const { step, nonce: id } = raw;
|
|
123
|
+
if (typeof id !== "string" || id.length === 0 || id.length > 100)
|
|
124
|
+
return null;
|
|
125
|
+
if (typeof step !== "object" || step === null)
|
|
126
|
+
return null;
|
|
127
|
+
const s = step;
|
|
128
|
+
const str = (v) => typeof v === "string" && v.length <= 1000;
|
|
129
|
+
if (!str(s.ref) || !str(s.component) || !str(s.action))
|
|
130
|
+
return null;
|
|
131
|
+
if (s.label !== undefined && !str(s.label))
|
|
132
|
+
return null;
|
|
133
|
+
if (s.confirm !== undefined && !str(s.confirm))
|
|
134
|
+
return null;
|
|
135
|
+
if (s.args !== undefined && !Array.isArray(s.args))
|
|
136
|
+
return null;
|
|
137
|
+
return { step: pickStep(s), nonce: id };
|
|
138
|
+
}
|
|
139
|
+
function json(body, status) {
|
|
140
|
+
return new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
|
|
141
|
+
}
|
|
142
|
+
function truncate(text, max) {
|
|
143
|
+
return text.length > max ? `${text.slice(0, max - 1)}…` : text;
|
|
144
|
+
}
|
|
145
|
+
function nonce() {
|
|
146
|
+
const c = globalThis.crypto;
|
|
147
|
+
if (c?.randomUUID)
|
|
148
|
+
return c.randomUUID();
|
|
149
|
+
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
150
|
+
}
|
|
151
|
+
function canonical(value) {
|
|
152
|
+
if (value === null || typeof value !== "object")
|
|
153
|
+
return JSON.stringify(value ?? null);
|
|
154
|
+
if (Array.isArray(value))
|
|
155
|
+
return `[${value.map(canonical).join(",")}]`;
|
|
156
|
+
const entries = Object.entries(value)
|
|
157
|
+
.filter(([, v]) => v !== undefined)
|
|
158
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
159
|
+
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`).join(",")}}`;
|
|
160
|
+
}
|
|
161
|
+
async function sha256(text) {
|
|
162
|
+
const digest = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
|
|
163
|
+
return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
164
|
+
}
|
package/dist/released.d.ts
CHANGED
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
* **Add the version here when you publish it, not before.** An entry is a
|
|
44
44
|
* claim that `npm install @deliverd/sdk@x` works today.
|
|
45
45
|
*/
|
|
46
|
-
export declare const PUBLISHED_TO_NPM: readonly ["0.1.0", "0.2.0", "0.3.0", "0.4.0", "0.5.0", "0.6.0", "0.6.1", "0.6.2", "0.7.0", "0.7.1", "0.8.0", "0.8.1", "0.8.2", "0.9.0", "0.10.0", "0.11.0", "0.11.1", "0.12.0", "0.13.0", "0.14.0", "0.15.0", "0.16.0", "0.17.0", "0.18.0", "0.19.0", "0.20.0", "0.21.0", "0.22.0", "0.23.0", "0.24.0", "0.24.1", "0.25.0", "0.25.1", "0.26.0", "0.27.0", "0.28.0", "0.29.0", "0.30.0", "0.31.0", "0.32.0"];
|
|
46
|
+
export declare const PUBLISHED_TO_NPM: readonly ["0.1.0", "0.2.0", "0.3.0", "0.4.0", "0.5.0", "0.6.0", "0.6.1", "0.6.2", "0.7.0", "0.7.1", "0.8.0", "0.8.1", "0.8.2", "0.9.0", "0.10.0", "0.11.0", "0.11.1", "0.12.0", "0.13.0", "0.14.0", "0.15.0", "0.16.0", "0.17.0", "0.18.0", "0.19.0", "0.20.0", "0.21.0", "0.22.0", "0.23.0", "0.24.0", "0.24.1", "0.25.0", "0.25.1", "0.26.0", "0.27.0", "0.28.0", "0.29.0", "0.30.0", "0.31.0", "0.32.0", "0.33.0", "0.34.0"];
|
|
47
47
|
/**
|
|
48
48
|
* A fingerprint of what each *published* version recorded.
|
|
49
49
|
*
|
package/dist/released.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"released.d.ts","sourceRoot":"","sources":["../src/released.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,gBAAgB,
|
|
1
|
+
{"version":3,"file":"released.d.ts","sourceRoot":"","sources":["../src/released.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,gBAAgB,iaA2CnB,CAAC;AAEX;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAmHzC,CAAC;AAEF,eAAO,MAAM,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAq/gCtD,CAAC"}
|