@botmanor/fe-sdk 2026.1203.1 → 2026.1204.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 +16 -2
- package/dist/index.d.ts +22 -1
- package/dist/index.js +47 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,6 +9,7 @@ import { StewardsSdkProvider, StewardConsole, useStewardsEvent } from '@botmanor
|
|
|
9
9
|
gatewayUrl={gatewayUrl}
|
|
10
10
|
workspaceId={workspaceId}
|
|
11
11
|
getAccessToken={() => auth.currentToken()}
|
|
12
|
+
getWorkspaceToken={() => auth.currentWorkspaceToken()} // app shells only
|
|
12
13
|
onTokenExpired={() => auth.refresh()}
|
|
13
14
|
onEvent={(e) => analytics.track(e.type)}
|
|
14
15
|
theme={{ accent: 'var(--my-brand)' }}
|
|
@@ -26,8 +27,12 @@ Components read and write Stewards through the host's GraphQL gateway
|
|
|
26
27
|
(`gatewayUrl`, an https URL with no credentials or query string; http only for
|
|
27
28
|
loopback). The access token from `getAccessToken` (the embed session token when
|
|
28
29
|
embedded on a third-party page) goes in the `Authorization` header only; no
|
|
29
|
-
cookies, no referrer.
|
|
30
|
-
`
|
|
30
|
+
cookies, no referrer. Inside a Burdenoff app shell, also pass
|
|
31
|
+
`getWorkspaceToken`: the workspace gateway needs the shell's workspace token
|
|
32
|
+
(sent as `X-Workspace-Authorization`) beside the access token for
|
|
33
|
+
workspace-scoped fields. An embed on a third-party page leaves it out. A 401 or
|
|
34
|
+
`UNAUTHENTICATED` (or a `getWorkspaceToken` that throws) raises `onTokenExpired`
|
|
35
|
+
and the `auth.expired` event. Operations match `@burdenoff/microfe-stewards` and use the
|
|
31
36
|
`Botmanor*` schema; the SDK does not depend on that package because it pulls in
|
|
32
37
|
Apollo and the fe-libs shell.
|
|
33
38
|
|
|
@@ -35,3 +40,12 @@ Apollo and the fe-libs shell.
|
|
|
35
40
|
- `StewardBuilder`: create from an agent and charter, or edit a charter (`stewardId`).
|
|
36
41
|
- `StewardRunView`: steps, approvals, trust level and budget; polls while active.
|
|
37
42
|
- `ApprovalInbox`: pending steward approvals with the approval scope choice.
|
|
43
|
+
|
|
44
|
+
## Events
|
|
45
|
+
|
|
46
|
+
`useStewardsEvent(type, handler)` or the provider's `onEvent` receive
|
|
47
|
+
`run.started`, `run.waiting`, `run.completed`, `run.failed`,
|
|
48
|
+
`approval.requested`, `approval.decided`, `auth.expired` and `request.failed`.
|
|
49
|
+
`request.failed` carries the error `code`; a used-up plan allowance arrives as
|
|
50
|
+
`QUOTA_EXHAUSTED` with its `quotaName` (for example `botmanor.stewards.runs`),
|
|
51
|
+
so a host can open its upgrade dialog.
|
package/dist/index.d.ts
CHANGED
|
@@ -14,7 +14,9 @@ import { CSSProperties, ReactNode } from "react";
|
|
|
14
14
|
export declare class StewardsRequestError extends Error {
|
|
15
15
|
readonly code: string;
|
|
16
16
|
readonly status?: number;
|
|
17
|
-
|
|
17
|
+
/** The plan quota a `QUOTA_EXHAUSTED` refusal names, e.g. `botmanor.stewards.runs`. */
|
|
18
|
+
readonly quotaName?: string;
|
|
19
|
+
constructor(message: string, code: string, status?: number, quotaName?: string);
|
|
18
20
|
}
|
|
19
21
|
interface StewardsClient {
|
|
20
22
|
request<T>(document: string, variables?: Record<string, unknown>, signal?: AbortSignal): Promise<T>;
|
|
@@ -55,6 +57,17 @@ interface StewardsEventMap {
|
|
|
55
57
|
"auth.expired": {
|
|
56
58
|
reason: string;
|
|
57
59
|
};
|
|
60
|
+
/**
|
|
61
|
+
* A request was refused or failed. `code` is the GraphQL error code
|
|
62
|
+
* (`QUOTA_EXHAUSTED` for a used-up plan allowance, with its `quotaName`) or
|
|
63
|
+
* a transport code (`NETWORK`, `UNAUTHENTICATED`, `BAD_RESPONSE`).
|
|
64
|
+
*/
|
|
65
|
+
"request.failed": {
|
|
66
|
+
code: string;
|
|
67
|
+
message: string;
|
|
68
|
+
status?: number;
|
|
69
|
+
quotaName?: string;
|
|
70
|
+
};
|
|
58
71
|
}
|
|
59
72
|
type StewardsEventType = keyof StewardsEventMap;
|
|
60
73
|
type StewardsEvent = { [K in StewardsEventType]: {
|
|
@@ -108,6 +121,14 @@ interface StewardsSdkConfig {
|
|
|
108
121
|
* by the SDK beyond one request, so the host stays the owner of refresh.
|
|
109
122
|
*/
|
|
110
123
|
getAccessToken: () => string | Promise<string>;
|
|
124
|
+
/**
|
|
125
|
+
* Returns the workspace token for `workspaceId`, when the host has one. A
|
|
126
|
+
* Burdenoff app shell signs in with two tokens and the workspace gateway
|
|
127
|
+
* needs both for workspace-scoped fields; it goes in the
|
|
128
|
+
* `X-Workspace-Authorization` header. An embed on a third-party page omits
|
|
129
|
+
* it: its single embed session token already names the workspace.
|
|
130
|
+
*/
|
|
131
|
+
getWorkspaceToken?: () => string | null | undefined | Promise<string | null | undefined>;
|
|
111
132
|
/** Called when the token getter throws or the gateway answers 401. */
|
|
112
133
|
onTokenExpired?: (reason: string) => void;
|
|
113
134
|
/** Every SDK event, for hosts that prefer one callback to `useStewardsEvent`. */
|
package/dist/index.js
CHANGED
|
@@ -7,11 +7,14 @@ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
|
7
7
|
var StewardsRequestError = class extends Error {
|
|
8
8
|
code;
|
|
9
9
|
status;
|
|
10
|
-
|
|
10
|
+
/** The plan quota a `QUOTA_EXHAUSTED` refusal names, e.g. `botmanor.stewards.runs`. */
|
|
11
|
+
quotaName;
|
|
12
|
+
constructor(message, code, status, quotaName) {
|
|
11
13
|
super(message);
|
|
12
14
|
this.name = "StewardsRequestError";
|
|
13
15
|
this.code = code;
|
|
14
16
|
this.status = status;
|
|
17
|
+
if (quotaName) this.quotaName = quotaName;
|
|
15
18
|
}
|
|
16
19
|
};
|
|
17
20
|
const LOOPBACK_HOSTS = /* @__PURE__ */ new Set([
|
|
@@ -35,15 +38,31 @@ function assertSafeGatewayUrl(raw) {
|
|
|
35
38
|
return url;
|
|
36
39
|
}
|
|
37
40
|
/**
|
|
41
|
+
* The host's workspace token, when it gives one. A getter that throws is an
|
|
42
|
+
* auth failure like a failing access-token getter: it is reported through
|
|
43
|
+
* `reportAuthExpired` and becomes a typed `UNAUTHENTICATED` error.
|
|
44
|
+
*/
|
|
45
|
+
async function resolveWorkspaceToken(config, options) {
|
|
46
|
+
if (!config.getWorkspaceToken) return void 0;
|
|
47
|
+
try {
|
|
48
|
+
return await config.getWorkspaceToken();
|
|
49
|
+
} catch (err) {
|
|
50
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
51
|
+
options.reportAuthExpired(`workspace token unavailable: ${reason}`);
|
|
52
|
+
throw new StewardsRequestError("Your workspace session could not be read", "UNAUTHENTICATED");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
38
56
|
* GraphQL transport over the public workspace gateway. The access token goes
|
|
39
|
-
* in the `Authorization` header only (never in the URL or body),
|
|
57
|
+
* in the `Authorization` header only (never in the URL or body), the host's
|
|
58
|
+
* workspace token, when it gives one, in `X-Workspace-Authorization`; cookies are
|
|
40
59
|
* not sent (`credentials: "omit"`), and no referrer leaks the embedding page.
|
|
41
60
|
*/
|
|
42
61
|
function createStewardsClient(options) {
|
|
43
|
-
|
|
62
|
+
const send = async (document, variables, signal) => {
|
|
44
63
|
const config = options.getConfig();
|
|
45
64
|
const url = assertSafeGatewayUrl(config.gatewayUrl);
|
|
46
|
-
const token = await options.resolveToken();
|
|
65
|
+
const [token, workspaceToken] = await Promise.all([options.resolveToken(), resolveWorkspaceToken(config, options)]);
|
|
47
66
|
const doFetch = options.fetchImpl ?? fetch;
|
|
48
67
|
let response;
|
|
49
68
|
try {
|
|
@@ -53,7 +72,8 @@ function createStewardsClient(options) {
|
|
|
53
72
|
"content-type": "application/json",
|
|
54
73
|
accept: "application/json",
|
|
55
74
|
authorization: `Bearer ${token}`,
|
|
56
|
-
"x-workspace-id": config.workspaceId
|
|
75
|
+
"x-workspace-id": config.workspaceId,
|
|
76
|
+
...workspaceToken ? { "x-workspace-authorization": `Bearer ${workspaceToken}` } : {}
|
|
57
77
|
},
|
|
58
78
|
body: JSON.stringify({
|
|
59
79
|
query: document,
|
|
@@ -81,10 +101,19 @@ function createStewardsClient(options) {
|
|
|
81
101
|
if (first) {
|
|
82
102
|
const code = typeof first.extensions?.code === "string" ? first.extensions.code : "GRAPHQL_ERROR";
|
|
83
103
|
if (code === "UNAUTHENTICATED") options.reportAuthExpired("gateway rejected the token");
|
|
84
|
-
|
|
104
|
+
const quotaName = typeof first.extensions?.quotaName === "string" ? first.extensions.quotaName : void 0;
|
|
105
|
+
throw new StewardsRequestError(first.message ?? "Request failed", code, response.status, quotaName);
|
|
85
106
|
}
|
|
86
107
|
if (!response.ok || body.data == null) throw new StewardsRequestError(`Unexpected response from the Stewards service (${response.status})`, "BAD_RESPONSE", response.status);
|
|
87
108
|
return body.data;
|
|
109
|
+
};
|
|
110
|
+
return { async request(document, variables = {}, signal) {
|
|
111
|
+
try {
|
|
112
|
+
return await send(document, variables, signal);
|
|
113
|
+
} catch (err) {
|
|
114
|
+
if (err instanceof StewardsRequestError && !signal?.aborted) options.reportFailure?.(err);
|
|
115
|
+
throw err;
|
|
116
|
+
}
|
|
88
117
|
} };
|
|
89
118
|
}
|
|
90
119
|
|
|
@@ -166,16 +195,25 @@ function StewardsSdkProvider({ children, ...config }) {
|
|
|
166
195
|
client: createStewardsClient({
|
|
167
196
|
getConfig: () => configRef.current,
|
|
168
197
|
resolveToken,
|
|
169
|
-
reportAuthExpired
|
|
198
|
+
reportAuthExpired,
|
|
199
|
+
reportFailure: (error) => {
|
|
200
|
+
events.emit("request.failed", {
|
|
201
|
+
code: error.code,
|
|
202
|
+
message: error.message,
|
|
203
|
+
...error.status !== void 0 ? { status: error.status } : {},
|
|
204
|
+
...error.quotaName ? { quotaName: error.quotaName } : {}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
170
207
|
})
|
|
171
208
|
};
|
|
172
209
|
});
|
|
173
|
-
const { gatewayUrl, workspaceId, getAccessToken, onTokenExpired, theme } = config;
|
|
210
|
+
const { gatewayUrl, workspaceId, getAccessToken, getWorkspaceToken, onTokenExpired, theme } = config;
|
|
174
211
|
const value = useMemo(() => ({
|
|
175
212
|
config: {
|
|
176
213
|
gatewayUrl,
|
|
177
214
|
workspaceId,
|
|
178
215
|
getAccessToken,
|
|
216
|
+
...getWorkspaceToken ? { getWorkspaceToken } : {},
|
|
179
217
|
...onTokenExpired ? { onTokenExpired } : {},
|
|
180
218
|
...onEvent ? { onEvent } : {},
|
|
181
219
|
...theme ? { theme } : {}
|
|
@@ -186,6 +224,7 @@ function StewardsSdkProvider({ children, ...config }) {
|
|
|
186
224
|
gatewayUrl,
|
|
187
225
|
workspaceId,
|
|
188
226
|
getAccessToken,
|
|
227
|
+
getWorkspaceToken,
|
|
189
228
|
onTokenExpired,
|
|
190
229
|
onEvent,
|
|
191
230
|
theme,
|