@deliverd/sdk 0.35.0 → 0.37.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 CHANGED
@@ -808,6 +808,24 @@ done.status; // "complete"
808
808
  done.decision; // "approved"
809
809
  ```
810
810
 
811
+ In React, mount it with one component from `@deliverd/sdk/react` (React 18 or
812
+ later; `react` is an optional peer dependency):
813
+
814
+ ```tsx
815
+ import { DeliverdDecision } from "@deliverd/sdk/react";
816
+
817
+ <DeliverdDecision
818
+ clientSecret={clientSecret} // embedded.clientSecret, handed down from your server
819
+ onDecided={(event) => confirmOnServer(event.sessionId)}
820
+ />;
821
+
822
+ // No account yet? The demo request: sample data, nothing recorded.
823
+ <DeliverdDecision demo onDecided={(event) => console.log(event.decision)} />;
824
+ ```
825
+
826
+ Elsewhere, `loadElements()` from `@deliverd/sdk/elements` loads the script once
827
+ and resolves to the same `mountDecision`.
828
+
811
829
  `deliverd.approvals.list({ approver: "sarah@acme.com", status: "pending" })` is
812
830
  her inbox, to show in your own UI. `deliverd.decisionSessions.expire(id)` ends a
813
831
  session early.
@@ -903,6 +921,20 @@ files"). A no, a wait that runs out, or a route that fails all leave the
903
921
  control untouched. The route waits four minutes by default; raise its
904
922
  `timeout`, the bridge's `timeout` and your function's limit together.
905
923
 
924
+ If the browser's own agent can use the page too, through WebMCP, give
925
+ `registerTools` the WebMCP version. The approver is told the browser's agent
926
+ asked, and a step the bridge already put to a person is not asked twice:
927
+
928
+ ```ts
929
+ import { deliverdWebMcpAllow } from "@deliverd/sdk/adapters";
930
+
931
+ registerTools(agent, { allow: deliverdWebMcpAllow({ endpoint: "/api/deliverd/ui-approval" }) });
932
+ ```
933
+
934
+ Don't pass `allow: () => true` there unless the bridge itself has an `allow`
935
+ that asks a person: without one, UI SDK lets the browser's agent through a
936
+ `data-agent-confirm` mark on that `true` alone.
937
+
906
938
  **LangGraph** — pause the graph rather than a process, and resume it from the callback:
907
939
 
908
940
  ```ts
@@ -20,8 +20,8 @@ export { generateWithDeliverdApprovals, pendingMastraApproval, resolveMastraAppr
20
20
  export type { MastraApprovingAgent, MastraOutput } from "./mastra.js";
21
21
  export { runMuseFunctionCalls, runMuseToolCalls } from "./muse-spark.js";
22
22
  export type { MuseFunctionCall, MuseFunctionCallOutput, MuseToolApprovalOptions, MuseToolCall, MuseToolMessage, MuseToolRunner, } from "./muse-spark.js";
23
- export { deliverdAllow, describeStep, uiSdkApprovalRoute, UI_SDK_DEFAULT_TIMEOUT } from "./ui-sdk.js";
24
- export type { DeliverdAllowOptions, UiSdkApprovalAnswer, UiSdkApprovalRouteOptions, UiSdkStep } from "./ui-sdk.js";
23
+ export { deliverdAllow, deliverdWebMcpAllow, describeStep, uiSdkApprovalRoute, UI_SDK_DEFAULT_TIMEOUT } from "./ui-sdk.js";
24
+ export type { DeliverdAllowOptions, UiSdkApprovalAnswer, UiSdkApprovalRouteOptions, UiSdkAskContext, UiSdkStep } from "./ui-sdk.js";
25
25
  export { defaultDescription, DEFAULT_TOOL_EXPIRY } from "./shared.js";
26
26
  export type { Approver, CallDescription, CallOutcome, ToolApprovalOptions } from "./shared.js";
27
27
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/adapters/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,YAAY,EACV,gBAAgB,EAChB,uBAAuB,EACvB,sBAAsB,EACtB,yBAAyB,GAC1B,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,+BAA+B,EAC/B,yBAAyB,EACzB,yBAAyB,GAC1B,MAAM,4BAA4B,CAAC;AACpC,YAAY,EACV,4BAA4B,EAC5B,mBAAmB,EACnB,kBAAkB,EAClB,uBAAuB,EACvB,wBAAwB,EACxB,6BAA6B,GAC9B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,oBAA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AAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AACtE,YAAY,EAAE,QAAQ,EAAE,eAAe,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC"}
@@ -13,5 +13,5 @@ export { resolveInterruptions, runWithDeliverdApprovals } from "./openai-agents.
13
13
  export { approveInGraph, resumeFromCallback } from "./langgraph.js";
14
14
  export { generateWithDeliverdApprovals, pendingMastraApproval, resolveMastraApproval } from "./mastra.js";
15
15
  export { runMuseFunctionCalls, runMuseToolCalls } from "./muse-spark.js";
16
- export { deliverdAllow, describeStep, uiSdkApprovalRoute, UI_SDK_DEFAULT_TIMEOUT } from "./ui-sdk.js";
16
+ export { deliverdAllow, deliverdWebMcpAllow, describeStep, uiSdkApprovalRoute, UI_SDK_DEFAULT_TIMEOUT } from "./ui-sdk.js";
17
17
  export { defaultDescription, DEFAULT_TOOL_EXPIRY } from "./shared.js";
@@ -38,8 +38,13 @@ import { type CallDescription, type ToolApprovalOptions } from "./shared.js";
38
38
  * out is a no, and the request expires with it, so nobody approves a step the
39
39
  * agent has stopped waiting for.
40
40
  *
41
- * Shapes follow `AgentAction` and `createAgent`'s `allow` option in
42
- * `@ui-sdk/core`'s agent docs (0.1.0-beta.1).
41
+ * When the browser's own agent can use the page too (WebMCP, through UI SDK's
42
+ * `registerTools`), pass `deliverdWebMcpAllow` as that `allow`. UI SDK runs it
43
+ * after the bridge's for each step the browser's agent asks for, inside the
44
+ * same `timeout`.
45
+ *
46
+ * Shapes follow `AgentAction`, `createAgent`'s `allow` option and
47
+ * `registerTools` in `@ui-sdk/core`'s agent docs (0.1.0-beta.1 and beta.2).
43
48
  */
44
49
  /** The step UI SDK hands to `allow`: its `AgentAction`. */
45
50
  export interface UiSdkStep {
@@ -71,6 +76,12 @@ export interface DeliverdAllowOptions {
71
76
  headers?: Record<string, string> | (() => Record<string, string>);
72
77
  /** Told each answer, so the page can show a person's reason. */
73
78
  onAnswer?: (answer: UiSdkApprovalAnswer, step: UiSdkStep) => void;
79
+ /**
80
+ * Which agent asked, sent to your route so the approver can tell. `"webmcp"`
81
+ * means the browser's own agent, through `registerTools`. Letters, digits
82
+ * and dashes, up to 40.
83
+ */
84
+ via?: string;
74
85
  fetch?: typeof globalThis.fetch;
75
86
  }
76
87
  /**
@@ -83,6 +94,28 @@ export interface DeliverdAllowOptions {
83
94
  export declare function deliverdAllow(options: DeliverdAllowOptions): (step: UiSdkStep, context?: {
84
95
  signal?: AbortSignal;
85
96
  }) => Promise<boolean>;
97
+ /**
98
+ * The `allow` that UI SDK's `registerTools` asks for, answered by a person
99
+ * through Deliverd. Use it when the browser's own agent (ChatGPT's site tools,
100
+ * or any browser with WebMCP) can use your page:
101
+ *
102
+ * ```ts
103
+ * registerTools(agent, {
104
+ * allow: deliverdWebMcpAllow({ endpoint: "/api/deliverd/ui-approval" }),
105
+ * });
106
+ * ```
107
+ *
108
+ * It asks about marked steps by default, as `deliverdAllow` does, and tells
109
+ * your route the browser's agent asked (`via: "webmcp"`). A step the bridge's
110
+ * own `deliverdAllow` already put to a person is not asked about again.
111
+ *
112
+ * Do not pass `allow: () => true` to `registerTools` unless the bridge has an
113
+ * `allow` that asks a person: with no bridge `allow`, UI SDK lets the browser's
114
+ * agent through a `data-agent-confirm` mark on that `true` alone.
115
+ */
116
+ export declare function deliverdWebMcpAllow(options: DeliverdAllowOptions): (step: UiSdkStep, context?: {
117
+ signal?: AbortSignal;
118
+ }) => Promise<boolean>;
86
119
  export interface UiSdkApprovalRouteOptions extends Omit<ToolApprovalOptions, "when" | "describe"> {
87
120
  /**
88
121
  * Which steps go to a person, decided on the server. Default: those on a
@@ -90,9 +123,9 @@ export interface UiSdkApprovalRouteOptions extends Omit<ToolApprovalOptions, "wh
90
123
  * the browser only sends steps it wants a person for, so anything else
91
124
  * arriving here was not sent by `deliverdAllow`.
92
125
  */
93
- when?: (step: UiSdkStep, request: Request) => boolean | Promise<boolean>;
126
+ when?: (step: UiSdkStep, request: Request, context: UiSdkAskContext) => boolean | Promise<boolean>;
94
127
  /** What the approver reads. Built from the mark's sentence, the control and its values when omitted. */
95
- describe?: (step: UiSdkStep) => CallDescription;
128
+ describe?: (step: UiSdkStep, context: UiSdkAskContext) => CallDescription;
96
129
  /**
97
130
  * How long to wait for the person. Default four minutes, inside UI SDK's
98
131
  * own five-minute wait and a typical function limit. Raise all three together.
@@ -101,6 +134,11 @@ export interface UiSdkApprovalRouteOptions extends Omit<ToolApprovalOptions, "wh
101
134
  /** How long the request stays open. Default: the same as `timeout`. */
102
135
  expiresIn?: Duration;
103
136
  }
137
+ /** What the page said about who asked. The page says it; nothing checks it. */
138
+ export interface UiSdkAskContext {
139
+ /** `"webmcp"` when the browser's own agent asked, through `deliverdWebMcpAllow`. */
140
+ via?: string;
141
+ }
104
142
  export declare const UI_SDK_DEFAULT_TIMEOUT: Duration;
105
143
  /**
106
144
  * The server half: a fetch-style `POST` handler (Next.js route handlers, Hono,
@@ -112,5 +150,5 @@ export declare const UI_SDK_DEFAULT_TIMEOUT: Duration;
112
150
  */
113
151
  export declare function uiSdkApprovalRoute(options: UiSdkApprovalRouteOptions): (request: Request) => Promise<Response>;
114
152
  /** What an approver reads about a step, when the app did not write it. */
115
- export declare function describeStep(step: UiSdkStep): CallDescription;
153
+ export declare function describeStep(step: UiSdkStep, context?: UiSdkAskContext): CallDescription;
116
154
  //# sourceMappingURL=ui-sdk.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ui-sdk.d.ts","sourceRoot":"","sources":["../../src/adapters/ui-sdk.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAgB,KAAK,eAAe,EAAE,KAAK,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAE3F;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAEH,2DAA2D;AAC3D,MAAM,WAAW,SAAS;IACxB,GAAG,EAAE,MAAM,CAAC;IACZ,SA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AE,SAAS,EAAE,OAAO,EAAE,eAAe,KAAK,eAAe,CAAC;IAC1E;;;OAGG;IACH,OAAO,CAAC,EAAE,QAAQ,CAAC;IACnB,uEAAuE;IACvE,SAAS,CAAC,EAAE,QAAQ,CAAC;CACtB;AAED,+EAA+E;AAC/E,MAAM,WAAW,eAAe;IAC9B,oFAAoF;IACpF,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,eAAO,MAAM,sBAAsB,EAAE,QAAe,CAAC;AAErD;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,yBAAyB,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAyC9G;AAED,0EAA0E;AAC1E,wBAAgB,YAAY,CAAC,IAAI,EAAE,SAAS,EAAE,OAAO,GAAE,eAAoB,GAAG,eAAe,CAgB5F"}
@@ -1,4 +1,11 @@
1
1
  import { askAboutCall } from "./shared.js";
2
+ /**
3
+ * Steps a person has already approved through Deliverd, by identity. UI SDK
4
+ * hands the same step object to the bridge's `allow` and then to the one
5
+ * `registerTools` was given, so the second can tell that the person was
6
+ * already asked about this very step and not ask them twice.
7
+ */
8
+ const approvedSteps = new WeakSet();
2
9
  /**
3
10
  * UI SDK's `allow` hook, answered by a person through Deliverd.
4
11
  *
@@ -8,6 +15,7 @@ import { askAboutCall } from "./shared.js";
8
15
  */
9
16
  export function deliverdAllow(options) {
10
17
  const needsPerson = options.when ?? ((step) => Boolean(step.confirm));
18
+ const via = options.via !== undefined && VIA.test(options.via) ? options.via : undefined;
11
19
  return async (step, context) => {
12
20
  if (!needsPerson(step))
13
21
  return true;
@@ -18,7 +26,7 @@ export function deliverdAllow(options) {
18
26
  const response = await doFetch(options.endpoint, {
19
27
  method: "POST",
20
28
  headers: { "content-type": "application/json", ...headers },
21
- body: JSON.stringify({ step: pickStep(step), nonce: nonce() }),
29
+ body: JSON.stringify({ step: pickStep(step), nonce: nonce(), ...(via ? { via } : {}) }),
22
30
  signal: context?.signal,
23
31
  });
24
32
  const body = (await response.json().catch(() => null));
@@ -38,9 +46,40 @@ export function deliverdAllow(options) {
38
46
  answer = { allowed: false, reason: "The approval route could not be reached, so the step was not run." };
39
47
  }
40
48
  options.onAnswer?.(answer, step);
49
+ if (answer.allowed && typeof step === "object" && step !== null)
50
+ approvedSteps.add(step);
41
51
  return answer.allowed;
42
52
  };
43
53
  }
54
+ /**
55
+ * The `allow` that UI SDK's `registerTools` asks for, answered by a person
56
+ * through Deliverd. Use it when the browser's own agent (ChatGPT's site tools,
57
+ * or any browser with WebMCP) can use your page:
58
+ *
59
+ * ```ts
60
+ * registerTools(agent, {
61
+ * allow: deliverdWebMcpAllow({ endpoint: "/api/deliverd/ui-approval" }),
62
+ * });
63
+ * ```
64
+ *
65
+ * It asks about marked steps by default, as `deliverdAllow` does, and tells
66
+ * your route the browser's agent asked (`via: "webmcp"`). A step the bridge's
67
+ * own `deliverdAllow` already put to a person is not asked about again.
68
+ *
69
+ * Do not pass `allow: () => true` to `registerTools` unless the bridge has an
70
+ * `allow` that asks a person: with no bridge `allow`, UI SDK lets the browser's
71
+ * agent through a `data-agent-confirm` mark on that `true` alone.
72
+ */
73
+ export function deliverdWebMcpAllow(options) {
74
+ const ask = deliverdAllow({ ...options, via: options.via ?? "webmcp" });
75
+ return async (step, context) => {
76
+ // The bridge's own deliverdAllow said yes to this very step a moment ago.
77
+ // Used once: the same object never counts for a later step.
78
+ if (typeof step === "object" && step !== null && approvedSteps.delete(step))
79
+ return true;
80
+ return ask(step, context);
81
+ };
82
+ }
44
83
  export const UI_SDK_DEFAULT_TIMEOUT = "4m";
45
84
  /**
46
85
  * The server half: a fetch-style `POST` handler (Next.js route handlers, Hono,
@@ -70,30 +109,34 @@ export function uiSdkApprovalRoute(options) {
70
109
  const parsed = readBody(raw);
71
110
  if (!parsed)
72
111
  return json({ allowed: false, reason: "The step could not be read." }, 400);
73
- const { step, nonce: id } = parsed;
74
- if (!(await needsPerson(step, request))) {
112
+ const { step, nonce: id, via } = parsed;
113
+ const context = via ? { via } : {};
114
+ if (!(await needsPerson(step, request, context))) {
75
115
  return json({ allowed: false, reason: "This step is not one this route asks a person about." }, 403);
76
116
  }
77
117
  const name = stepName(step);
78
118
  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
119
  ...options,
80
120
  when: undefined,
81
- describe: () => describe(step),
121
+ describe: () => describe(step, context),
82
122
  timeout,
83
123
  expiresIn: options.expiresIn ?? timeout,
84
- metadata: { ...options.metadata, source: "ui-sdk" },
124
+ metadata: { ...options.metadata, source: "ui-sdk", ...(via ? { via } : {}) },
85
125
  }, request.signal);
86
126
  return json(outcome.allowed ? { allowed: true, decidedBy: outcome.decidedBy } : { allowed: false, reason: outcome.reason }, 200);
87
127
  };
88
128
  }
89
129
  /** What an approver reads about a step, when the app did not write it. */
90
- export function describeStep(step) {
130
+ export function describeStep(step, context = {}) {
91
131
  const target = step.label ? `"${step.label}"` : `the ${step.component}`;
92
132
  const verb = step.action === "press" ? "press" : step.action === "setValue" ? "set" : step.action;
93
133
  const values = (step.args ?? []).map((arg) => (typeof arg === "string" ? arg : JSON.stringify(arg))).join(", ");
94
134
  const lines = [
95
135
  step.confirm ? `The app says this: ${step.confirm}` : undefined,
96
136
  `An agent wants to ${verb} ${target}${values ? ` (${truncate(values, 500)})` : ""} in the app.`,
137
+ context.via === "webmcp"
138
+ ? "It is the browser's own agent, using the page through WebMCP, not an agent the app runs."
139
+ : undefined,
97
140
  ].filter(Boolean);
98
141
  return {
99
142
  title: truncate(step.confirm ?? `Let the agent ${verb} ${target}`, 200),
@@ -102,6 +145,7 @@ export function describeStep(step) {
102
145
  };
103
146
  }
104
147
  const BODY_MAX = 16_000;
148
+ const VIA = /^[a-z0-9-]{1,40}$/;
105
149
  function stepName(step) {
106
150
  return step.label ? `${step.action} "${step.label}"` : `${step.action} ${step.component}`;
107
151
  }
@@ -119,7 +163,9 @@ function pickStep(step) {
119
163
  function readBody(raw) {
120
164
  if (typeof raw !== "object" || raw === null)
121
165
  return null;
122
- const { step, nonce: id } = raw;
166
+ const { step, nonce: id, via } = raw;
167
+ if (via !== undefined && (typeof via !== "string" || !VIA.test(via)))
168
+ return null;
123
169
  if (typeof id !== "string" || id.length === 0 || id.length > 100)
124
170
  return null;
125
171
  if (typeof step !== "object" || step === null)
@@ -134,7 +180,7 @@ function readBody(raw) {
134
180
  return null;
135
181
  if (s.args !== undefined && !Array.isArray(s.args))
136
182
  return null;
137
- return { step: pickStep(s), nonce: id };
183
+ return { step: pickStep(s), nonce: id, ...(typeof via === "string" ? { via } : {}) };
138
184
  }
139
185
  function json(body, status) {
140
186
  return new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
@@ -0,0 +1,96 @@
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
+ /** Where Elements is served from, unless you say otherwise. */
27
+ export declare const ELEMENTS_ORIGIN = "https://deliverd.dev";
28
+ /** The path of the script on that origin. Versioned: v1 keeps working. */
29
+ export declare const ELEMENTS_PATH = "/elements/v1.js";
30
+ /** What every event hands your handler. */
31
+ export interface DecisionEvent {
32
+ type: "ready" | "verified" | "decided" | "expired";
33
+ sessionId: string;
34
+ approvalId: string;
35
+ /** Set on `decided`: what the approver chose. */
36
+ decision: "approved" | "rejected" | "changes_requested" | null;
37
+ /** Set on `decided`: the request's own status, which other approvers may still be part of. */
38
+ approvalStatus: string | null;
39
+ }
40
+ export interface MountDecisionOptions {
41
+ /**
42
+ * The session's `clientSecret`, from `approvals.createSession(id, { mode: "embedded", origin })`
43
+ * on your server. Required unless `demo` is set.
44
+ */
45
+ clientSecret?: string;
46
+ /**
47
+ * Show the demo request instead of a real one: sample data, a code shown on
48
+ * screen, and nothing recorded. Needs no account and frames on any site, so
49
+ * you can see the element and its events before writing server code.
50
+ */
51
+ demo?: boolean;
52
+ /** The frame's accessible title. "Decision" when omitted. */
53
+ title?: string;
54
+ /** The frame's height before it reports its own, in pixels. 320 when omitted. */
55
+ minHeight?: number;
56
+ /** Set `false` to size the frame yourself instead of letting it fit its content. */
57
+ autoResize?: boolean;
58
+ onReady?: (event: DecisionEvent) => void;
59
+ onVerified?: (event: DecisionEvent) => void;
60
+ /** The browser's word only: confirm with `decisionSessions.get(id)` from your server before acting. */
61
+ onDecided?: (event: DecisionEvent) => void;
62
+ onExpired?: (event: DecisionEvent) => void;
63
+ }
64
+ export interface MountedDecision {
65
+ frame: HTMLIFrameElement;
66
+ unmount(): void;
67
+ }
68
+ /** What the script puts on `window.Deliverd`. */
69
+ export interface DeliverdElements {
70
+ mountDecision(target: string | Element, options: MountDecisionOptions): MountedDecision;
71
+ elementsVersion: number;
72
+ }
73
+ export interface LoadElementsOptions {
74
+ /**
75
+ * Another Deliverd origin, such as your own self-hosted deployment. Must be
76
+ * the origin the session was made on: the frame only talks to the script
77
+ * that came from its own origin.
78
+ */
79
+ origin?: string;
80
+ }
81
+ /**
82
+ * Loads Elements into the page once, and resolves to its API.
83
+ *
84
+ * Safe to call as often as you like: a second call, from anywhere, waits on
85
+ * the same script rather than adding another. If the page already loaded the
86
+ * script with a `<script>` tag, that one is used.
87
+ */
88
+ export declare function loadElements(options?: LoadElementsOptions): Promise<DeliverdElements>;
89
+ /**
90
+ * Loads Elements and mounts a decision into `host`, unless `cancelled()` says
91
+ * the caller went away first. Resolves to the mounted decision, or `null`
92
+ * when it was cancelled. What the React component does in an effect, kept
93
+ * here so it can be used, and tested, without React.
94
+ */
95
+ export declare function mountWhenLoaded(host: Element, options: MountDecisionOptions, load: () => Promise<DeliverdElements>, cancelled: () => boolean): Promise<MountedDecision | null>;
96
+ //# sourceMappingURL=elements.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"elements.d.ts","sourceRoot":"","sources":["../src/elements.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,+DAA+D;AAC/D,eAAO,MAAM,eAAe,yBAAyB,CAAC;AAEtD,0EAA0E;AAC1E,eAAO,MAAM,aAAa,oBAAoB,CAAC;AAE/C,2CAA2C;AAC3C,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,GAAG,SAAS,CAAC;IACnD,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,iDAAiD;IACjD,QAAQ,EAAE,UAAU,GAAG,UAAU,GAAG,mBAAmB,GAAG,IAAI,CAAC;IAC/D,8FAA8F;IAC9F,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B;AAED,MAAM,WAAW,oBAAoB;IACnC;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;;OAIG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,6DAA6D;IAC7D,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,iFAAiF;IACjF,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,oFAAoF;IACpF,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAC;IACzC,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAC;IAC5C,uGAAuG;IACvG,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAC;IAC3C,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAC;CAC5C;AAED,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,iBAAiB,CAAC;IACzB,OAAO,IAAI,IAAI,CAAC;CACjB;AAED,iDAAiD;AACjD,MAAM,WAAW,gBAAgB;IAC/B,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,EAAE,OAAO,EAAE,oBAAoB,GAAG,eAAe,CAAC;IACxF,eAAe,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,mBAAmB;IAClC;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAWD;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,mBAAwB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAmCzF;AAED;;;;;GAKG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,oBAAoB,EAC7B,IAAI,EAAE,MAAM,OAAO,CAAC,gBAAgB,CAAC,EACrC,SAAS,EAAE,MAAM,OAAO,GACvB,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CAOjC"}
@@ -0,0 +1,94 @@
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
+ /** Where Elements is served from, unless you say otherwise. */
27
+ export const ELEMENTS_ORIGIN = "https://deliverd.dev";
28
+ /** The path of the script on that origin. Versioned: v1 keeps working. */
29
+ export const ELEMENTS_PATH = "/elements/v1.js";
30
+ const loading = new Map();
31
+ function ready(w) {
32
+ const api = w.Deliverd;
33
+ return api && typeof api.mountDecision === "function" ? api : null;
34
+ }
35
+ /**
36
+ * Loads Elements into the page once, and resolves to its API.
37
+ *
38
+ * Safe to call as often as you like: a second call, from anywhere, waits on
39
+ * the same script rather than adding another. If the page already loaded the
40
+ * script with a `<script>` tag, that one is used.
41
+ */
42
+ export function loadElements(options = {}) {
43
+ if (typeof window === "undefined" || typeof document === "undefined") {
44
+ return Promise.reject(new Error("Deliverd Elements runs in a browser. Call loadElements() from client code."));
45
+ }
46
+ const origin = new URL(options.origin ?? ELEMENTS_ORIGIN).origin;
47
+ const w = window;
48
+ const existing = ready(w);
49
+ if (existing)
50
+ return Promise.resolve(existing);
51
+ const cached = loading.get(origin);
52
+ if (cached)
53
+ return cached;
54
+ const src = origin + ELEMENTS_PATH;
55
+ const promise = new Promise((resolve, reject) => {
56
+ const fail = (message) => {
57
+ loading.delete(origin);
58
+ reject(new Error(message));
59
+ };
60
+ const done = () => {
61
+ const api = ready(w);
62
+ if (api)
63
+ resolve(api);
64
+ else
65
+ fail(`Deliverd Elements loaded from ${src} but did not start.`);
66
+ };
67
+ let script = document.querySelector(`script[src="${src}"]`);
68
+ if (!script) {
69
+ script = document.createElement("script");
70
+ script.src = src;
71
+ script.async = true;
72
+ document.head.appendChild(script);
73
+ }
74
+ script.addEventListener("load", done, { once: true });
75
+ script.addEventListener("error", () => fail(`Could not load Deliverd Elements from ${src}.`), { once: true });
76
+ });
77
+ loading.set(origin, promise);
78
+ return promise;
79
+ }
80
+ /**
81
+ * Loads Elements and mounts a decision into `host`, unless `cancelled()` says
82
+ * the caller went away first. Resolves to the mounted decision, or `null`
83
+ * when it was cancelled. What the React component does in an effect, kept
84
+ * here so it can be used, and tested, without React.
85
+ */
86
+ export async function mountWhenLoaded(host, options, load, cancelled) {
87
+ if (!options.demo && !options.clientSecret) {
88
+ throw new Error("Deliverd: pass clientSecret (from an embedded session), or demo: true.");
89
+ }
90
+ const elements = await load();
91
+ if (cancelled())
92
+ return null;
93
+ return elements.mountDecision(host, options);
94
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * `<DeliverdDecision>`: a decision session in a React page.
3
+ *
4
+ * A thin wrapper over Deliverd Elements (`@deliverd/sdk/elements`): it loads
5
+ * the script once, mounts the frame into a `<div>` it renders, passes your
6
+ * handlers through, and unmounts the frame when the component goes away or
7
+ * the secret changes. The frame is still served from Deliverd's origin, so
8
+ * your page cannot read it or press its buttons, and the approver still
9
+ * proves who they are before deciding.
10
+ *
11
+ * ```tsx
12
+ * import { DeliverdDecision } from "@deliverd/sdk/react";
13
+ *
14
+ * <DeliverdDecision
15
+ * clientSecret={clientSecret} // from your server
16
+ * onDecided={(event) => confirmOnServer(event.sessionId)}
17
+ * />
18
+ *
19
+ * // No account yet? See it with sample data, nothing recorded:
20
+ * <DeliverdDecision demo onDecided={(event) => console.log(event)} />
21
+ * ```
22
+ *
23
+ * React 18 or later. Written without JSX so the package needs no build step
24
+ * of its own for it; `react` is an optional peer dependency, needed only if
25
+ * you import this entry.
26
+ */
27
+ import { type CSSProperties, type ReactElement } from "react";
28
+ import { type MountDecisionOptions } from "./elements.js";
29
+ export type { DecisionEvent } from "./elements.js";
30
+ export interface DeliverdDecisionProps extends Pick<MountDecisionOptions, "clientSecret" | "demo" | "title" | "minHeight" | "autoResize" | "onReady" | "onVerified" | "onDecided" | "onExpired"> {
31
+ /** Another Deliverd origin, such as a self-hosted deployment. https://deliverd.dev when omitted. */
32
+ origin?: string;
33
+ /** Called when the script cannot load or the options are refused. Thrown to the console otherwise. */
34
+ onError?: (error: Error) => void;
35
+ className?: string;
36
+ style?: CSSProperties;
37
+ id?: string;
38
+ }
39
+ export declare function DeliverdDecision(props: DeliverdDecisionProps): ReactElement;
40
+ //# sourceMappingURL=react.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,EAAoC,KAAK,aAAa,EAAE,KAAK,YAAY,EAAE,MAAM,OAAO,CAAC;AAChG,OAAO,EAIL,KAAK,oBAAoB,EAE1B,MAAM,eAAe,CAAC;AAEvB,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,MAAM,WAAW,qBACf,SAAQ,IAAI,CACV,oBAAoB,EACpB,cAAc,GAAG,MAAM,GAAG,OAAO,GAAG,WAAW,GAAG,YAAY,GAAG,SAAS,GAAG,YAAY,GAAG,WAAW,GAAG,WAAW,CACtH;IACD,oGAAoG;IACpG,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,sGAAsG;IACtG,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IACjC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,aAAa,CAAC;IACtB,EAAE,CAAC,EAAE,MAAM,CAAC;CACb;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,GAAG,YAAY,CAiD3E"}
package/dist/react.js ADDED
@@ -0,0 +1,73 @@
1
+ "use client";
2
+ /**
3
+ * `<DeliverdDecision>`: a decision session in a React page.
4
+ *
5
+ * A thin wrapper over Deliverd Elements (`@deliverd/sdk/elements`): it loads
6
+ * the script once, mounts the frame into a `<div>` it renders, passes your
7
+ * handlers through, and unmounts the frame when the component goes away or
8
+ * the secret changes. The frame is still served from Deliverd's origin, so
9
+ * your page cannot read it or press its buttons, and the approver still
10
+ * proves who they are before deciding.
11
+ *
12
+ * ```tsx
13
+ * import { DeliverdDecision } from "@deliverd/sdk/react";
14
+ *
15
+ * <DeliverdDecision
16
+ * clientSecret={clientSecret} // from your server
17
+ * onDecided={(event) => confirmOnServer(event.sessionId)}
18
+ * />
19
+ *
20
+ * // No account yet? See it with sample data, nothing recorded:
21
+ * <DeliverdDecision demo onDecided={(event) => console.log(event)} />
22
+ * ```
23
+ *
24
+ * React 18 or later. Written without JSX so the package needs no build step
25
+ * of its own for it; `react` is an optional peer dependency, needed only if
26
+ * you import this entry.
27
+ */
28
+ import { createElement, useEffect, useRef } from "react";
29
+ import { loadElements, mountWhenLoaded, } from "./elements.js";
30
+ export function DeliverdDecision(props) {
31
+ const host = useRef(null);
32
+ // Handlers read through a ref, so a new arrow function on every render does not remount the frame.
33
+ const latest = useRef(props);
34
+ latest.current = props;
35
+ const { clientSecret, demo, title, minHeight, autoResize, origin } = props;
36
+ useEffect(() => {
37
+ const el = host.current;
38
+ if (!el)
39
+ return;
40
+ let cancelled = false;
41
+ let mounted = null;
42
+ const relay = (name) => (event) => latest.current[name]?.(event);
43
+ mountWhenLoaded(el, {
44
+ clientSecret,
45
+ demo,
46
+ title,
47
+ minHeight,
48
+ autoResize,
49
+ onReady: relay("onReady"),
50
+ onVerified: relay("onVerified"),
51
+ onDecided: relay("onDecided"),
52
+ onExpired: relay("onExpired"),
53
+ }, () => loadElements({ origin }), () => cancelled)
54
+ .then((result) => {
55
+ if (cancelled)
56
+ result?.unmount();
57
+ else
58
+ mounted = result;
59
+ })
60
+ .catch((err) => {
61
+ const error = err instanceof Error ? err : new Error(String(err));
62
+ if (latest.current.onError)
63
+ latest.current.onError(error);
64
+ else
65
+ console.error(error);
66
+ });
67
+ return () => {
68
+ cancelled = true;
69
+ mounted?.unmount();
70
+ };
71
+ }, [clientSecret, demo, title, minHeight, autoResize, origin]);
72
+ return createElement("div", { ref: host, className: props.className, style: props.style, id: props.id });
73
+ }
@@ -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", "0.33.0", "0.34.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", "0.35.0", "0.36.0"];
47
47
  /**
48
48
  * A fingerprint of what each *published* version recorded.
49
49
  *
@@ -1 +1 @@
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"}
1
+ {"version":3,"file":"released.d.ts","sourceRoot":"","sources":["../src/released.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,gBAAgB,qbA6CnB,CAAC;AAEX;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAuHzC,CAAC;AAEF,eAAO,MAAM,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAu7mCtD,CAAC"}