@deliverd/sdk 0.35.0 → 0.36.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 +18 -0
- package/dist/elements.d.ts +96 -0
- package/dist/elements.d.ts.map +1 -0
- package/dist/elements.js +94 -0
- package/dist/react.d.ts +40 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +73 -0
- package/dist/released.d.ts +1 -1
- package/dist/released.d.ts.map +1 -1
- package/dist/released.js +1504 -0
- package/package.json +19 -1
- package/src/elements.ts +160 -0
- package/src/react.ts +104 -0
- package/src/released.ts +1505 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deliverd/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.0",
|
|
4
4
|
"description": "Add human approval to any AI agent. Ask a person, wait for the answer, act on it. Start free at deliverd.dev.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -17,6 +17,14 @@
|
|
|
17
17
|
"./verify": {
|
|
18
18
|
"types": "./dist/verify.d.ts",
|
|
19
19
|
"default": "./dist/verify.js"
|
|
20
|
+
},
|
|
21
|
+
"./elements": {
|
|
22
|
+
"types": "./dist/elements.d.ts",
|
|
23
|
+
"default": "./dist/elements.js"
|
|
24
|
+
},
|
|
25
|
+
"./react": {
|
|
26
|
+
"types": "./dist/react.d.ts",
|
|
27
|
+
"default": "./dist/react.js"
|
|
20
28
|
}
|
|
21
29
|
},
|
|
22
30
|
"files": [
|
|
@@ -34,8 +42,18 @@
|
|
|
34
42
|
"typecheck": "tsc --noEmit",
|
|
35
43
|
"prepublishOnly": "npm run build"
|
|
36
44
|
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"react": ">=18"
|
|
47
|
+
},
|
|
48
|
+
"peerDependenciesMeta": {
|
|
49
|
+
"react": {
|
|
50
|
+
"optional": true
|
|
51
|
+
}
|
|
52
|
+
},
|
|
37
53
|
"devDependencies": {
|
|
38
54
|
"@types/node": "^20",
|
|
55
|
+
"@types/react": "^19",
|
|
56
|
+
"react": "^19",
|
|
39
57
|
"typescript": "^5",
|
|
40
58
|
"vitest": "^4.1.11"
|
|
41
59
|
},
|
package/src/elements.ts
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deliverd Elements from a bundler: load the script, mount a decision.
|
|
3
|
+
*
|
|
4
|
+
* `https://deliverd.dev/elements/v1.js` is the element. This module does not
|
|
5
|
+
* reimplement it; it loads it, once, and hands back the same `mountDecision`
|
|
6
|
+
* a `<script>` tag would. Keeping one copy is the point: the frame, the
|
|
7
|
+
* origin checks on its messages and the sizing all live in that script, so a
|
|
8
|
+
* fix there reaches every page without anybody upgrading a package.
|
|
9
|
+
*
|
|
10
|
+
* Browser only. On a server there is no page to put a frame in, so
|
|
11
|
+
* `loadElements()` rejects there rather than pretending.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { loadElements } from "@deliverd/sdk/elements";
|
|
15
|
+
*
|
|
16
|
+
* const elements = await loadElements();
|
|
17
|
+
* const decision = elements.mountDecision("#decision", {
|
|
18
|
+
* clientSecret, // from your server: approvals.createSession(id, { mode: "embedded", origin })
|
|
19
|
+
* onDecided(event) {
|
|
20
|
+
* // Tell your server, which reads decisionSessions.get(event.sessionId) before acting.
|
|
21
|
+
* },
|
|
22
|
+
* });
|
|
23
|
+
* // later: decision.unmount();
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** Where Elements is served from, unless you say otherwise. */
|
|
28
|
+
export const ELEMENTS_ORIGIN = "https://deliverd.dev";
|
|
29
|
+
|
|
30
|
+
/** The path of the script on that origin. Versioned: v1 keeps working. */
|
|
31
|
+
export const ELEMENTS_PATH = "/elements/v1.js";
|
|
32
|
+
|
|
33
|
+
/** What every event hands your handler. */
|
|
34
|
+
export interface DecisionEvent {
|
|
35
|
+
type: "ready" | "verified" | "decided" | "expired";
|
|
36
|
+
sessionId: string;
|
|
37
|
+
approvalId: string;
|
|
38
|
+
/** Set on `decided`: what the approver chose. */
|
|
39
|
+
decision: "approved" | "rejected" | "changes_requested" | null;
|
|
40
|
+
/** Set on `decided`: the request's own status, which other approvers may still be part of. */
|
|
41
|
+
approvalStatus: string | null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface MountDecisionOptions {
|
|
45
|
+
/**
|
|
46
|
+
* The session's `clientSecret`, from `approvals.createSession(id, { mode: "embedded", origin })`
|
|
47
|
+
* on your server. Required unless `demo` is set.
|
|
48
|
+
*/
|
|
49
|
+
clientSecret?: string;
|
|
50
|
+
/**
|
|
51
|
+
* Show the demo request instead of a real one: sample data, a code shown on
|
|
52
|
+
* screen, and nothing recorded. Needs no account and frames on any site, so
|
|
53
|
+
* you can see the element and its events before writing server code.
|
|
54
|
+
*/
|
|
55
|
+
demo?: boolean;
|
|
56
|
+
/** The frame's accessible title. "Decision" when omitted. */
|
|
57
|
+
title?: string;
|
|
58
|
+
/** The frame's height before it reports its own, in pixels. 320 when omitted. */
|
|
59
|
+
minHeight?: number;
|
|
60
|
+
/** Set `false` to size the frame yourself instead of letting it fit its content. */
|
|
61
|
+
autoResize?: boolean;
|
|
62
|
+
onReady?: (event: DecisionEvent) => void;
|
|
63
|
+
onVerified?: (event: DecisionEvent) => void;
|
|
64
|
+
/** The browser's word only: confirm with `decisionSessions.get(id)` from your server before acting. */
|
|
65
|
+
onDecided?: (event: DecisionEvent) => void;
|
|
66
|
+
onExpired?: (event: DecisionEvent) => void;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface MountedDecision {
|
|
70
|
+
frame: HTMLIFrameElement;
|
|
71
|
+
unmount(): void;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** What the script puts on `window.Deliverd`. */
|
|
75
|
+
export interface DeliverdElements {
|
|
76
|
+
mountDecision(target: string | Element, options: MountDecisionOptions): MountedDecision;
|
|
77
|
+
elementsVersion: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface LoadElementsOptions {
|
|
81
|
+
/**
|
|
82
|
+
* Another Deliverd origin, such as your own self-hosted deployment. Must be
|
|
83
|
+
* the origin the session was made on: the frame only talks to the script
|
|
84
|
+
* that came from its own origin.
|
|
85
|
+
*/
|
|
86
|
+
origin?: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
type Loaded = Window & { Deliverd?: Partial<DeliverdElements> };
|
|
90
|
+
|
|
91
|
+
const loading = new Map<string, Promise<DeliverdElements>>();
|
|
92
|
+
|
|
93
|
+
function ready(w: Loaded): DeliverdElements | null {
|
|
94
|
+
const api = w.Deliverd;
|
|
95
|
+
return api && typeof api.mountDecision === "function" ? (api as DeliverdElements) : null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Loads Elements into the page once, and resolves to its API.
|
|
100
|
+
*
|
|
101
|
+
* Safe to call as often as you like: a second call, from anywhere, waits on
|
|
102
|
+
* the same script rather than adding another. If the page already loaded the
|
|
103
|
+
* script with a `<script>` tag, that one is used.
|
|
104
|
+
*/
|
|
105
|
+
export function loadElements(options: LoadElementsOptions = {}): Promise<DeliverdElements> {
|
|
106
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
107
|
+
return Promise.reject(new Error("Deliverd Elements runs in a browser. Call loadElements() from client code."));
|
|
108
|
+
}
|
|
109
|
+
const origin = new URL(options.origin ?? ELEMENTS_ORIGIN).origin;
|
|
110
|
+
const w = window as Loaded;
|
|
111
|
+
const existing = ready(w);
|
|
112
|
+
if (existing) return Promise.resolve(existing);
|
|
113
|
+
|
|
114
|
+
const cached = loading.get(origin);
|
|
115
|
+
if (cached) return cached;
|
|
116
|
+
|
|
117
|
+
const src = origin + ELEMENTS_PATH;
|
|
118
|
+
const promise = new Promise<DeliverdElements>((resolve, reject) => {
|
|
119
|
+
const fail = (message: string) => {
|
|
120
|
+
loading.delete(origin);
|
|
121
|
+
reject(new Error(message));
|
|
122
|
+
};
|
|
123
|
+
const done = () => {
|
|
124
|
+
const api = ready(w);
|
|
125
|
+
if (api) resolve(api);
|
|
126
|
+
else fail(`Deliverd Elements loaded from ${src} but did not start.`);
|
|
127
|
+
};
|
|
128
|
+
let script = document.querySelector<HTMLScriptElement>(`script[src="${src}"]`);
|
|
129
|
+
if (!script) {
|
|
130
|
+
script = document.createElement("script");
|
|
131
|
+
script.src = src;
|
|
132
|
+
script.async = true;
|
|
133
|
+
document.head.appendChild(script);
|
|
134
|
+
}
|
|
135
|
+
script.addEventListener("load", done, { once: true });
|
|
136
|
+
script.addEventListener("error", () => fail(`Could not load Deliverd Elements from ${src}.`), { once: true });
|
|
137
|
+
});
|
|
138
|
+
loading.set(origin, promise);
|
|
139
|
+
return promise;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Loads Elements and mounts a decision into `host`, unless `cancelled()` says
|
|
144
|
+
* the caller went away first. Resolves to the mounted decision, or `null`
|
|
145
|
+
* when it was cancelled. What the React component does in an effect, kept
|
|
146
|
+
* here so it can be used, and tested, without React.
|
|
147
|
+
*/
|
|
148
|
+
export async function mountWhenLoaded(
|
|
149
|
+
host: Element,
|
|
150
|
+
options: MountDecisionOptions,
|
|
151
|
+
load: () => Promise<DeliverdElements>,
|
|
152
|
+
cancelled: () => boolean
|
|
153
|
+
): Promise<MountedDecision | null> {
|
|
154
|
+
if (!options.demo && !options.clientSecret) {
|
|
155
|
+
throw new Error("Deliverd: pass clientSecret (from an embedded session), or demo: true.");
|
|
156
|
+
}
|
|
157
|
+
const elements = await load();
|
|
158
|
+
if (cancelled()) return null;
|
|
159
|
+
return elements.mountDecision(host, options);
|
|
160
|
+
}
|
package/src/react.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `<DeliverdDecision>`: a decision session in a React page.
|
|
5
|
+
*
|
|
6
|
+
* A thin wrapper over Deliverd Elements (`@deliverd/sdk/elements`): it loads
|
|
7
|
+
* the script once, mounts the frame into a `<div>` it renders, passes your
|
|
8
|
+
* handlers through, and unmounts the frame when the component goes away or
|
|
9
|
+
* the secret changes. The frame is still served from Deliverd's origin, so
|
|
10
|
+
* your page cannot read it or press its buttons, and the approver still
|
|
11
|
+
* proves who they are before deciding.
|
|
12
|
+
*
|
|
13
|
+
* ```tsx
|
|
14
|
+
* import { DeliverdDecision } from "@deliverd/sdk/react";
|
|
15
|
+
*
|
|
16
|
+
* <DeliverdDecision
|
|
17
|
+
* clientSecret={clientSecret} // from your server
|
|
18
|
+
* onDecided={(event) => confirmOnServer(event.sessionId)}
|
|
19
|
+
* />
|
|
20
|
+
*
|
|
21
|
+
* // No account yet? See it with sample data, nothing recorded:
|
|
22
|
+
* <DeliverdDecision demo onDecided={(event) => console.log(event)} />
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* React 18 or later. Written without JSX so the package needs no build step
|
|
26
|
+
* of its own for it; `react` is an optional peer dependency, needed only if
|
|
27
|
+
* you import this entry.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { createElement, useEffect, useRef, type CSSProperties, type ReactElement } from "react";
|
|
31
|
+
import {
|
|
32
|
+
loadElements,
|
|
33
|
+
mountWhenLoaded,
|
|
34
|
+
type DecisionEvent,
|
|
35
|
+
type MountDecisionOptions,
|
|
36
|
+
type MountedDecision,
|
|
37
|
+
} from "./elements.js";
|
|
38
|
+
|
|
39
|
+
export type { DecisionEvent } from "./elements.js";
|
|
40
|
+
|
|
41
|
+
export interface DeliverdDecisionProps
|
|
42
|
+
extends Pick<
|
|
43
|
+
MountDecisionOptions,
|
|
44
|
+
"clientSecret" | "demo" | "title" | "minHeight" | "autoResize" | "onReady" | "onVerified" | "onDecided" | "onExpired"
|
|
45
|
+
> {
|
|
46
|
+
/** Another Deliverd origin, such as a self-hosted deployment. https://deliverd.dev when omitted. */
|
|
47
|
+
origin?: string;
|
|
48
|
+
/** Called when the script cannot load or the options are refused. Thrown to the console otherwise. */
|
|
49
|
+
onError?: (error: Error) => void;
|
|
50
|
+
className?: string;
|
|
51
|
+
style?: CSSProperties;
|
|
52
|
+
id?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function DeliverdDecision(props: DeliverdDecisionProps): ReactElement {
|
|
56
|
+
const host = useRef<HTMLDivElement>(null);
|
|
57
|
+
// Handlers read through a ref, so a new arrow function on every render does not remount the frame.
|
|
58
|
+
const latest = useRef(props);
|
|
59
|
+
latest.current = props;
|
|
60
|
+
|
|
61
|
+
const { clientSecret, demo, title, minHeight, autoResize, origin } = props;
|
|
62
|
+
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
const el = host.current;
|
|
65
|
+
if (!el) return;
|
|
66
|
+
let cancelled = false;
|
|
67
|
+
let mounted: MountedDecision | null = null;
|
|
68
|
+
const relay = (name: "onReady" | "onVerified" | "onDecided" | "onExpired") => (event: DecisionEvent) =>
|
|
69
|
+
latest.current[name]?.(event);
|
|
70
|
+
|
|
71
|
+
mountWhenLoaded(
|
|
72
|
+
el,
|
|
73
|
+
{
|
|
74
|
+
clientSecret,
|
|
75
|
+
demo,
|
|
76
|
+
title,
|
|
77
|
+
minHeight,
|
|
78
|
+
autoResize,
|
|
79
|
+
onReady: relay("onReady"),
|
|
80
|
+
onVerified: relay("onVerified"),
|
|
81
|
+
onDecided: relay("onDecided"),
|
|
82
|
+
onExpired: relay("onExpired"),
|
|
83
|
+
},
|
|
84
|
+
() => loadElements({ origin }),
|
|
85
|
+
() => cancelled
|
|
86
|
+
)
|
|
87
|
+
.then((result) => {
|
|
88
|
+
if (cancelled) result?.unmount();
|
|
89
|
+
else mounted = result;
|
|
90
|
+
})
|
|
91
|
+
.catch((err: unknown) => {
|
|
92
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
93
|
+
if (latest.current.onError) latest.current.onError(error);
|
|
94
|
+
else console.error(error);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
return () => {
|
|
98
|
+
cancelled = true;
|
|
99
|
+
mounted?.unmount();
|
|
100
|
+
};
|
|
101
|
+
}, [clientSecret, demo, title, minHeight, autoResize, origin]);
|
|
102
|
+
|
|
103
|
+
return createElement("div", { ref: host, className: props.className, style: props.style, id: props.id });
|
|
104
|
+
}
|