@ronza.ai/react 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +102 -2
- package/dist/index.cjs +179 -0
- package/dist/index.d.cts +43 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.js +177 -0
- package/package.json +69 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ronza
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,103 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ronza.ai/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Your Ronza agent in a React app (React 18 or 19): `<RonzaWidget />` and
|
|
4
|
+
`useRonza()`. Built on [`@ronza.ai/web`](https://www.npmjs.com/package/@ronza.ai/web),
|
|
5
|
+
which loads the chat from cdn.ronza.ai, so every fix to the chat reaches your
|
|
6
|
+
app without a new release of yours.
|
|
7
|
+
|
|
8
|
+
Guides for each framework: https://ronza.ai/docs/install
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install @ronza.ai/react
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Use
|
|
17
|
+
|
|
18
|
+
```tsx
|
|
19
|
+
import { RonzaWidget, useRonza } from '@ronza.ai/react';
|
|
20
|
+
|
|
21
|
+
export function App() {
|
|
22
|
+
return (
|
|
23
|
+
<>
|
|
24
|
+
<RonzaWidget
|
|
25
|
+
agentKey="pk_YOUR_PUBLIC_KEY"
|
|
26
|
+
getToken={async () => fetchTokenFromYourServer()}
|
|
27
|
+
context={{ plan: 'Pro' }}
|
|
28
|
+
/>
|
|
29
|
+
<HelpButton />
|
|
30
|
+
</>
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function HelpButton() {
|
|
35
|
+
const ronza = useRonza(); // null until the chat is ready
|
|
36
|
+
return <button disabled={!ronza} onClick={() => ronza?.open()}>Ask us</button>;
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### `<RonzaWidget />`
|
|
41
|
+
|
|
42
|
+
Renders nothing itself. Put it once in your app, for example in the layout.
|
|
43
|
+
|
|
44
|
+
| Prop | |
|
|
45
|
+
|---|---|
|
|
46
|
+
| `agentKey` | Required. The public key, `pk_...`, from the agent's install page in Ronza Chat. |
|
|
47
|
+
| `getToken` | Returns an identity token from your server for the signed-in user, or `null` when nobody is signed in (an empty or blank string counts as `null`). Called once at start, and again when the chat reports the error `identity_refused` (the signed-in session can no longer be renewed); the fresh token is handed over with `identify`. A `boot_failed` error is not retried with a new token: a token refused at start is a setup error to fix on your server. When it returns no token after a token (your user signed out), the chat is reset to an anonymous visitor. |
|
|
48
|
+
| `context` | What the page tells the agent, for example `{ plan: 'Pro' }`. Sent when the widget starts and again whenever it changes by value; removing it clears it, and a widget with no `context` (another route's, say) clears what an earlier one set. |
|
|
49
|
+
| `channel` | `'center'` for Center stage. |
|
|
50
|
+
| `hideLauncher` | Keep the launcher hidden and open the chat from your own button. |
|
|
51
|
+
| `theme`, `display`, `scriptOrigin` | As in `@ronza.ai/web`. |
|
|
52
|
+
|
|
53
|
+
`agentKey`, `channel`, `theme` and `display` are read when the widget first
|
|
54
|
+
mounts. Change the theme later with `useRonza()?.update({ theme })`.
|
|
55
|
+
|
|
56
|
+
Unmounting the widget hides the launcher and keeps the conversation, so a
|
|
57
|
+
route change does not end the chat. It is safe under Strict Mode: the double
|
|
58
|
+
mount loads the script once, starts the chat once and calls `getToken` once.
|
|
59
|
+
|
|
60
|
+
### `useRonza()`
|
|
61
|
+
|
|
62
|
+
The typed client from `@ronza.ai/web` (`open`, `close`, `on`, `context`,
|
|
63
|
+
`identify`, `reset`, `debug` and the rest) once the chat is ready, and `null` before
|
|
64
|
+
that and on the server. It works in any component, inside or outside the
|
|
65
|
+
widget's part of the tree.
|
|
66
|
+
|
|
67
|
+
### When the chat does not appear
|
|
68
|
+
|
|
69
|
+
`useRonza()` stays `null` for a chat that never starts, so reach it with
|
|
70
|
+
`getRonza()`, the same client as soon as `<RonzaWidget />` has started
|
|
71
|
+
loading the chat:
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
import { getRonza } from '@ronza.ai/react';
|
|
75
|
+
|
|
76
|
+
getRonza()?.debug();
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
`debug()` prints a checklist of the install to the browser's console: the
|
|
80
|
+
script's address, the agent's key, whether the chat's frame answered, and
|
|
81
|
+
the last error with a link to its section of the troubleshooting page,
|
|
82
|
+
https://ronza.ai/docs/install/troubleshooting. Adding `?ronza_debug=1` to
|
|
83
|
+
the page's address does the same without a code change. The output holds no
|
|
84
|
+
token, no message text and no page-context values, so you can paste it into
|
|
85
|
+
a message to Ronza's support.
|
|
86
|
+
|
|
87
|
+
When the chat could not start, the `error` event's `code` is `boot_failed`
|
|
88
|
+
and its `reason` is one of the install codes (`agent_not_found`,
|
|
89
|
+
`website_not_allowed`, `identity_token_refused`, `too_many_requests`,
|
|
90
|
+
`frame_blocked` or `start_failed`), each explained at
|
|
91
|
+
`https://ronza.ai/docs/install/troubleshooting#<reason>`. `getToken` is not
|
|
92
|
+
called again for any of them.
|
|
93
|
+
|
|
94
|
+
### Server rendering
|
|
95
|
+
|
|
96
|
+
Safe in server-rendered apps (Next.js and others): nothing runs on the server
|
|
97
|
+
and `useRonza()` is `null` there. The package is marked `'use client'`, so in
|
|
98
|
+
the Next.js App Router a server layout can render `<RonzaWidget />` directly;
|
|
99
|
+
`useRonza()` belongs in client components, like any hook.
|
|
100
|
+
|
|
101
|
+
## License
|
|
102
|
+
|
|
103
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
4
|
+
const react = require("react");
|
|
5
|
+
const web = require("@ronza.ai/web");
|
|
6
|
+
const FRESH_TOKEN_ERROR_CODE = "identity_refused";
|
|
7
|
+
const MIN_MILLISECONDS_BETWEEN_TOKEN_REFRESHES = 1e4;
|
|
8
|
+
let readyClient = null;
|
|
9
|
+
const readyClientSubscribers = /* @__PURE__ */ new Set();
|
|
10
|
+
function setReadyClient(client) {
|
|
11
|
+
if (readyClient === client) return;
|
|
12
|
+
readyClient = client;
|
|
13
|
+
for (const notify of readyClientSubscribers) notify();
|
|
14
|
+
}
|
|
15
|
+
function subscribeToReadyClient(notify) {
|
|
16
|
+
readyClientSubscribers.add(notify);
|
|
17
|
+
return () => {
|
|
18
|
+
readyClientSubscribers.delete(notify);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function readReadyClient() {
|
|
22
|
+
return readyClient;
|
|
23
|
+
}
|
|
24
|
+
function readServerClient() {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
function useRonza() {
|
|
28
|
+
return react.useSyncExternalStore(subscribeToReadyClient, readReadyClient, readServerClient);
|
|
29
|
+
}
|
|
30
|
+
function isPlainObject(value) {
|
|
31
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
32
|
+
const prototype = Object.getPrototypeOf(value);
|
|
33
|
+
return prototype === Object.prototype || prototype === null;
|
|
34
|
+
}
|
|
35
|
+
function isSameValue(left, right) {
|
|
36
|
+
if (Object.is(left, right)) return true;
|
|
37
|
+
if (Array.isArray(left) && Array.isArray(right)) {
|
|
38
|
+
return left.length === right.length && left.every((item, index) => isSameValue(item, right[index]));
|
|
39
|
+
}
|
|
40
|
+
if (isPlainObject(left) && isPlainObject(right)) {
|
|
41
|
+
const leftNames = Object.keys(left);
|
|
42
|
+
if (leftNames.length !== Object.keys(right).length) return false;
|
|
43
|
+
return leftNames.every((name) => Object.prototype.hasOwnProperty.call(right, name) && isSameValue(left[name], right[name]));
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
function copyPlain(value) {
|
|
48
|
+
if (Array.isArray(value)) return value.map(copyPlain);
|
|
49
|
+
if (isPlainObject(value)) {
|
|
50
|
+
const copy = {};
|
|
51
|
+
for (const name of Object.keys(value)) copy[name] = copyPlain(value[name]);
|
|
52
|
+
return copy;
|
|
53
|
+
}
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
function normaliseToken(answer) {
|
|
57
|
+
if (typeof answer !== "string") return null;
|
|
58
|
+
const token = answer.trim();
|
|
59
|
+
return token === "" ? null : token;
|
|
60
|
+
}
|
|
61
|
+
function reportError(message, error) {
|
|
62
|
+
console.error(`[ronza] ${message}`, error);
|
|
63
|
+
}
|
|
64
|
+
function RonzaWidget(props) {
|
|
65
|
+
const latestProps = react.useRef(props);
|
|
66
|
+
const [startedClient, setStartedClient] = react.useState(null);
|
|
67
|
+
const startedClientRef = react.useRef(null);
|
|
68
|
+
const startRequestedRef = react.useRef(false);
|
|
69
|
+
const mountedRef = react.useRef(false);
|
|
70
|
+
const lastTokenRef = react.useRef(null);
|
|
71
|
+
const refreshInFlightRef = react.useRef(false);
|
|
72
|
+
const lastRefreshAtRef = react.useRef(Number.NEGATIVE_INFINITY);
|
|
73
|
+
const lastSentContextRef = react.useRef(void 0);
|
|
74
|
+
react.useEffect(() => {
|
|
75
|
+
latestProps.current = props;
|
|
76
|
+
});
|
|
77
|
+
react.useEffect(() => {
|
|
78
|
+
mountedRef.current = true;
|
|
79
|
+
function adoptClient(client, chatWasAlreadyLoaded) {
|
|
80
|
+
startedClientRef.current = client;
|
|
81
|
+
setStartedClient(client);
|
|
82
|
+
if (chatWasAlreadyLoaded && !latestProps.current.hideLauncher) client.show();
|
|
83
|
+
}
|
|
84
|
+
async function start() {
|
|
85
|
+
const chatWasAlreadyLoaded = web.getRonza() !== null;
|
|
86
|
+
let token = null;
|
|
87
|
+
const getToken = latestProps.current.getToken;
|
|
88
|
+
if (getToken) {
|
|
89
|
+
try {
|
|
90
|
+
token = normaliseToken(await getToken());
|
|
91
|
+
} catch (error) {
|
|
92
|
+
reportError("getToken failed, so the chat starts without a signed-in visitor:", error);
|
|
93
|
+
}
|
|
94
|
+
if (!mountedRef.current) {
|
|
95
|
+
startRequestedRef.current = false;
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
lastTokenRef.current = token;
|
|
100
|
+
const options = latestProps.current;
|
|
101
|
+
const loading = web.loadRonza({
|
|
102
|
+
agentKey: options.agentKey,
|
|
103
|
+
channel: options.channel,
|
|
104
|
+
identityToken: token ?? void 0,
|
|
105
|
+
hideLauncher: options.hideLauncher,
|
|
106
|
+
theme: options.theme,
|
|
107
|
+
display: options.display,
|
|
108
|
+
scriptOrigin: options.scriptOrigin
|
|
109
|
+
});
|
|
110
|
+
loading.then(setReadyClient, (error) => {
|
|
111
|
+
if (error instanceof Error && error.message.startsWith("[ronza] ")) console.error(error.message);
|
|
112
|
+
else reportError("the chat could not be loaded:", error);
|
|
113
|
+
});
|
|
114
|
+
const client = web.getRonza();
|
|
115
|
+
if (client && client.agentKey === options.agentKey) adoptClient(client, chatWasAlreadyLoaded);
|
|
116
|
+
}
|
|
117
|
+
if (!startRequestedRef.current) {
|
|
118
|
+
startRequestedRef.current = true;
|
|
119
|
+
void start();
|
|
120
|
+
} else if (startedClientRef.current && !latestProps.current.hideLauncher) {
|
|
121
|
+
startedClientRef.current.show();
|
|
122
|
+
}
|
|
123
|
+
return () => {
|
|
124
|
+
mountedRef.current = false;
|
|
125
|
+
startedClientRef.current?.hide();
|
|
126
|
+
};
|
|
127
|
+
}, []);
|
|
128
|
+
react.useEffect(() => {
|
|
129
|
+
if (!startedClient) return void 0;
|
|
130
|
+
const client = startedClient;
|
|
131
|
+
async function refreshToken() {
|
|
132
|
+
const getToken = latestProps.current.getToken;
|
|
133
|
+
if (!getToken || refreshInFlightRef.current) return;
|
|
134
|
+
const now = Date.now();
|
|
135
|
+
if (now - lastRefreshAtRef.current < MIN_MILLISECONDS_BETWEEN_TOKEN_REFRESHES) return;
|
|
136
|
+
lastRefreshAtRef.current = now;
|
|
137
|
+
refreshInFlightRef.current = true;
|
|
138
|
+
let token;
|
|
139
|
+
try {
|
|
140
|
+
token = normaliseToken(await getToken());
|
|
141
|
+
} catch (error) {
|
|
142
|
+
reportError("getToken failed, so the refused sign-in was not renewed:", error);
|
|
143
|
+
return;
|
|
144
|
+
} finally {
|
|
145
|
+
refreshInFlightRef.current = false;
|
|
146
|
+
}
|
|
147
|
+
if (token === null) {
|
|
148
|
+
if (lastTokenRef.current !== null) {
|
|
149
|
+
lastTokenRef.current = null;
|
|
150
|
+
client.reset();
|
|
151
|
+
}
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (token === lastTokenRef.current) return;
|
|
155
|
+
lastTokenRef.current = token;
|
|
156
|
+
client.identify({ token });
|
|
157
|
+
}
|
|
158
|
+
function onError(detail) {
|
|
159
|
+
if (detail?.code === FRESH_TOKEN_ERROR_CODE) void refreshToken();
|
|
160
|
+
}
|
|
161
|
+
return client.on("error", onError);
|
|
162
|
+
}, [startedClient]);
|
|
163
|
+
react.useEffect(() => {
|
|
164
|
+
if (!startedClient) return;
|
|
165
|
+
const next = props.context ?? null;
|
|
166
|
+
const previous = lastSentContextRef.current;
|
|
167
|
+
if (previous !== void 0 && isSameValue(previous, next)) return;
|
|
168
|
+
const sent = copyPlain(next);
|
|
169
|
+
lastSentContextRef.current = sent;
|
|
170
|
+
startedClient.context(sent);
|
|
171
|
+
});
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
Object.defineProperty(exports, "getRonza", {
|
|
175
|
+
enumerable: true,
|
|
176
|
+
get: () => web.getRonza
|
|
177
|
+
});
|
|
178
|
+
exports.RonzaWidget = RonzaWidget;
|
|
179
|
+
exports.useRonza = useRonza;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { getRonza } from '@ronza.ai/web';
|
|
2
|
+
/**
|
|
3
|
+
* The page's client as soon as `<RonzaWidget />` has started loading the chat, before it is ready; `null` before
|
|
4
|
+
* that and on a server. `useRonza()` stays `null` for a chat that never starts, so this is how to reach one:
|
|
5
|
+
* `getRonza()?.debug()` prints the install checklist, and `getRonza()?.on('error', ...)` hears why it failed.
|
|
6
|
+
*/
|
|
7
|
+
export { getRonza };
|
|
8
|
+
import type { LoadRonzaOptions, PageContextFields, RonzaClient } from '@ronza.ai/web';
|
|
9
|
+
export type { LoadRonzaOptions, PageContextFields, PageContextValue, RonzaChannel, RonzaClient, RonzaDisplay, RonzaEventDetails, RonzaEventListener, RonzaEventName, RonzaSettingsUpdate, RonzaTheme, } from '@ronza.ai/web';
|
|
10
|
+
export interface RonzaWidgetProps {
|
|
11
|
+
/** The agent's public key, `pk_...`. Read when the widget first mounts. */
|
|
12
|
+
agentKey: string;
|
|
13
|
+
/**
|
|
14
|
+
* Returns an identity token from your server for the signed-in user, or `null` (or an empty string) when nobody
|
|
15
|
+
* is signed in. Called once at start, and again when the chat reports `identity_refused` (the signed-in session
|
|
16
|
+
* can no longer be renewed). Resolving to no token after a token signs the visitor out (`reset`).
|
|
17
|
+
*/
|
|
18
|
+
getToken?: () => Promise<string | null> | string | null;
|
|
19
|
+
/**
|
|
20
|
+
* What the page tells the agent (`{ plan: 'Pro' }`). Sent when the widget starts (left out, it clears what an
|
|
21
|
+
* earlier widget on the page set), then again whenever it changes by value.
|
|
22
|
+
*/
|
|
23
|
+
context?: PageContextFields | null;
|
|
24
|
+
/** `'center'` for Center stage. Read when the widget first mounts. */
|
|
25
|
+
channel?: LoadRonzaOptions['channel'];
|
|
26
|
+
/** Keep the launcher hidden; open the chat from your own button with `useRonza()?.open()`. */
|
|
27
|
+
hideLauncher?: boolean;
|
|
28
|
+
/** Read when the widget first mounts; change it later with `useRonza()?.update({ theme })`. */
|
|
29
|
+
theme?: LoadRonzaOptions['theme'];
|
|
30
|
+
/** Read when the widget first mounts. */
|
|
31
|
+
display?: LoadRonzaOptions['display'];
|
|
32
|
+
/** Where the script comes from. Default `https://cdn.ronza.ai`. */
|
|
33
|
+
scriptOrigin?: string;
|
|
34
|
+
}
|
|
35
|
+
/** The typed client once the chat is ready; `null` before, and on a server. */
|
|
36
|
+
export declare function useRonza(): RonzaClient | null;
|
|
37
|
+
/**
|
|
38
|
+
* Loads the agent and keeps it in step with the app: the signed-in user (`getToken`), the page context
|
|
39
|
+
* (`context`), and the launcher, hidden while the component is unmounted. Renders nothing.
|
|
40
|
+
*
|
|
41
|
+
* Strict Mode safe: its double mount loads the script once, boots once and calls `getToken` once.
|
|
42
|
+
*/
|
|
43
|
+
export declare function RonzaWidget(props: RonzaWidgetProps): null;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { getRonza } from '@ronza.ai/web';
|
|
2
|
+
/**
|
|
3
|
+
* The page's client as soon as `<RonzaWidget />` has started loading the chat, before it is ready; `null` before
|
|
4
|
+
* that and on a server. `useRonza()` stays `null` for a chat that never starts, so this is how to reach one:
|
|
5
|
+
* `getRonza()?.debug()` prints the install checklist, and `getRonza()?.on('error', ...)` hears why it failed.
|
|
6
|
+
*/
|
|
7
|
+
export { getRonza };
|
|
8
|
+
import type { LoadRonzaOptions, PageContextFields, RonzaClient } from '@ronza.ai/web';
|
|
9
|
+
export type { LoadRonzaOptions, PageContextFields, PageContextValue, RonzaChannel, RonzaClient, RonzaDisplay, RonzaEventDetails, RonzaEventListener, RonzaEventName, RonzaSettingsUpdate, RonzaTheme, } from '@ronza.ai/web';
|
|
10
|
+
export interface RonzaWidgetProps {
|
|
11
|
+
/** The agent's public key, `pk_...`. Read when the widget first mounts. */
|
|
12
|
+
agentKey: string;
|
|
13
|
+
/**
|
|
14
|
+
* Returns an identity token from your server for the signed-in user, or `null` (or an empty string) when nobody
|
|
15
|
+
* is signed in. Called once at start, and again when the chat reports `identity_refused` (the signed-in session
|
|
16
|
+
* can no longer be renewed). Resolving to no token after a token signs the visitor out (`reset`).
|
|
17
|
+
*/
|
|
18
|
+
getToken?: () => Promise<string | null> | string | null;
|
|
19
|
+
/**
|
|
20
|
+
* What the page tells the agent (`{ plan: 'Pro' }`). Sent when the widget starts (left out, it clears what an
|
|
21
|
+
* earlier widget on the page set), then again whenever it changes by value.
|
|
22
|
+
*/
|
|
23
|
+
context?: PageContextFields | null;
|
|
24
|
+
/** `'center'` for Center stage. Read when the widget first mounts. */
|
|
25
|
+
channel?: LoadRonzaOptions['channel'];
|
|
26
|
+
/** Keep the launcher hidden; open the chat from your own button with `useRonza()?.open()`. */
|
|
27
|
+
hideLauncher?: boolean;
|
|
28
|
+
/** Read when the widget first mounts; change it later with `useRonza()?.update({ theme })`. */
|
|
29
|
+
theme?: LoadRonzaOptions['theme'];
|
|
30
|
+
/** Read when the widget first mounts. */
|
|
31
|
+
display?: LoadRonzaOptions['display'];
|
|
32
|
+
/** Where the script comes from. Default `https://cdn.ronza.ai`. */
|
|
33
|
+
scriptOrigin?: string;
|
|
34
|
+
}
|
|
35
|
+
/** The typed client once the chat is ready; `null` before, and on a server. */
|
|
36
|
+
export declare function useRonza(): RonzaClient | null;
|
|
37
|
+
/**
|
|
38
|
+
* Loads the agent and keeps it in step with the app: the signed-in user (`getToken`), the page context
|
|
39
|
+
* (`context`), and the launcher, hidden while the component is unmounted. Renders nothing.
|
|
40
|
+
*
|
|
41
|
+
* Strict Mode safe: its double mount loads the script once, boots once and calls `getToken` once.
|
|
42
|
+
*/
|
|
43
|
+
export declare function RonzaWidget(props: RonzaWidgetProps): null;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useSyncExternalStore, useRef, useState, useEffect } from "react";
|
|
3
|
+
import { getRonza, loadRonza } from "@ronza.ai/web";
|
|
4
|
+
import { getRonza as getRonza2 } from "@ronza.ai/web";
|
|
5
|
+
const FRESH_TOKEN_ERROR_CODE = "identity_refused";
|
|
6
|
+
const MIN_MILLISECONDS_BETWEEN_TOKEN_REFRESHES = 1e4;
|
|
7
|
+
let readyClient = null;
|
|
8
|
+
const readyClientSubscribers = /* @__PURE__ */ new Set();
|
|
9
|
+
function setReadyClient(client) {
|
|
10
|
+
if (readyClient === client) return;
|
|
11
|
+
readyClient = client;
|
|
12
|
+
for (const notify of readyClientSubscribers) notify();
|
|
13
|
+
}
|
|
14
|
+
function subscribeToReadyClient(notify) {
|
|
15
|
+
readyClientSubscribers.add(notify);
|
|
16
|
+
return () => {
|
|
17
|
+
readyClientSubscribers.delete(notify);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function readReadyClient() {
|
|
21
|
+
return readyClient;
|
|
22
|
+
}
|
|
23
|
+
function readServerClient() {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
function useRonza() {
|
|
27
|
+
return useSyncExternalStore(subscribeToReadyClient, readReadyClient, readServerClient);
|
|
28
|
+
}
|
|
29
|
+
function isPlainObject(value) {
|
|
30
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
31
|
+
const prototype = Object.getPrototypeOf(value);
|
|
32
|
+
return prototype === Object.prototype || prototype === null;
|
|
33
|
+
}
|
|
34
|
+
function isSameValue(left, right) {
|
|
35
|
+
if (Object.is(left, right)) return true;
|
|
36
|
+
if (Array.isArray(left) && Array.isArray(right)) {
|
|
37
|
+
return left.length === right.length && left.every((item, index) => isSameValue(item, right[index]));
|
|
38
|
+
}
|
|
39
|
+
if (isPlainObject(left) && isPlainObject(right)) {
|
|
40
|
+
const leftNames = Object.keys(left);
|
|
41
|
+
if (leftNames.length !== Object.keys(right).length) return false;
|
|
42
|
+
return leftNames.every((name) => Object.prototype.hasOwnProperty.call(right, name) && isSameValue(left[name], right[name]));
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
function copyPlain(value) {
|
|
47
|
+
if (Array.isArray(value)) return value.map(copyPlain);
|
|
48
|
+
if (isPlainObject(value)) {
|
|
49
|
+
const copy = {};
|
|
50
|
+
for (const name of Object.keys(value)) copy[name] = copyPlain(value[name]);
|
|
51
|
+
return copy;
|
|
52
|
+
}
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
function normaliseToken(answer) {
|
|
56
|
+
if (typeof answer !== "string") return null;
|
|
57
|
+
const token = answer.trim();
|
|
58
|
+
return token === "" ? null : token;
|
|
59
|
+
}
|
|
60
|
+
function reportError(message, error) {
|
|
61
|
+
console.error(`[ronza] ${message}`, error);
|
|
62
|
+
}
|
|
63
|
+
function RonzaWidget(props) {
|
|
64
|
+
const latestProps = useRef(props);
|
|
65
|
+
const [startedClient, setStartedClient] = useState(null);
|
|
66
|
+
const startedClientRef = useRef(null);
|
|
67
|
+
const startRequestedRef = useRef(false);
|
|
68
|
+
const mountedRef = useRef(false);
|
|
69
|
+
const lastTokenRef = useRef(null);
|
|
70
|
+
const refreshInFlightRef = useRef(false);
|
|
71
|
+
const lastRefreshAtRef = useRef(Number.NEGATIVE_INFINITY);
|
|
72
|
+
const lastSentContextRef = useRef(void 0);
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
latestProps.current = props;
|
|
75
|
+
});
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
mountedRef.current = true;
|
|
78
|
+
function adoptClient(client, chatWasAlreadyLoaded) {
|
|
79
|
+
startedClientRef.current = client;
|
|
80
|
+
setStartedClient(client);
|
|
81
|
+
if (chatWasAlreadyLoaded && !latestProps.current.hideLauncher) client.show();
|
|
82
|
+
}
|
|
83
|
+
async function start() {
|
|
84
|
+
const chatWasAlreadyLoaded = getRonza() !== null;
|
|
85
|
+
let token = null;
|
|
86
|
+
const getToken = latestProps.current.getToken;
|
|
87
|
+
if (getToken) {
|
|
88
|
+
try {
|
|
89
|
+
token = normaliseToken(await getToken());
|
|
90
|
+
} catch (error) {
|
|
91
|
+
reportError("getToken failed, so the chat starts without a signed-in visitor:", error);
|
|
92
|
+
}
|
|
93
|
+
if (!mountedRef.current) {
|
|
94
|
+
startRequestedRef.current = false;
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
lastTokenRef.current = token;
|
|
99
|
+
const options = latestProps.current;
|
|
100
|
+
const loading = loadRonza({
|
|
101
|
+
agentKey: options.agentKey,
|
|
102
|
+
channel: options.channel,
|
|
103
|
+
identityToken: token ?? void 0,
|
|
104
|
+
hideLauncher: options.hideLauncher,
|
|
105
|
+
theme: options.theme,
|
|
106
|
+
display: options.display,
|
|
107
|
+
scriptOrigin: options.scriptOrigin
|
|
108
|
+
});
|
|
109
|
+
loading.then(setReadyClient, (error) => {
|
|
110
|
+
if (error instanceof Error && error.message.startsWith("[ronza] ")) console.error(error.message);
|
|
111
|
+
else reportError("the chat could not be loaded:", error);
|
|
112
|
+
});
|
|
113
|
+
const client = getRonza();
|
|
114
|
+
if (client && client.agentKey === options.agentKey) adoptClient(client, chatWasAlreadyLoaded);
|
|
115
|
+
}
|
|
116
|
+
if (!startRequestedRef.current) {
|
|
117
|
+
startRequestedRef.current = true;
|
|
118
|
+
void start();
|
|
119
|
+
} else if (startedClientRef.current && !latestProps.current.hideLauncher) {
|
|
120
|
+
startedClientRef.current.show();
|
|
121
|
+
}
|
|
122
|
+
return () => {
|
|
123
|
+
mountedRef.current = false;
|
|
124
|
+
startedClientRef.current?.hide();
|
|
125
|
+
};
|
|
126
|
+
}, []);
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
if (!startedClient) return void 0;
|
|
129
|
+
const client = startedClient;
|
|
130
|
+
async function refreshToken() {
|
|
131
|
+
const getToken = latestProps.current.getToken;
|
|
132
|
+
if (!getToken || refreshInFlightRef.current) return;
|
|
133
|
+
const now = Date.now();
|
|
134
|
+
if (now - lastRefreshAtRef.current < MIN_MILLISECONDS_BETWEEN_TOKEN_REFRESHES) return;
|
|
135
|
+
lastRefreshAtRef.current = now;
|
|
136
|
+
refreshInFlightRef.current = true;
|
|
137
|
+
let token;
|
|
138
|
+
try {
|
|
139
|
+
token = normaliseToken(await getToken());
|
|
140
|
+
} catch (error) {
|
|
141
|
+
reportError("getToken failed, so the refused sign-in was not renewed:", error);
|
|
142
|
+
return;
|
|
143
|
+
} finally {
|
|
144
|
+
refreshInFlightRef.current = false;
|
|
145
|
+
}
|
|
146
|
+
if (token === null) {
|
|
147
|
+
if (lastTokenRef.current !== null) {
|
|
148
|
+
lastTokenRef.current = null;
|
|
149
|
+
client.reset();
|
|
150
|
+
}
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (token === lastTokenRef.current) return;
|
|
154
|
+
lastTokenRef.current = token;
|
|
155
|
+
client.identify({ token });
|
|
156
|
+
}
|
|
157
|
+
function onError(detail) {
|
|
158
|
+
if (detail?.code === FRESH_TOKEN_ERROR_CODE) void refreshToken();
|
|
159
|
+
}
|
|
160
|
+
return client.on("error", onError);
|
|
161
|
+
}, [startedClient]);
|
|
162
|
+
useEffect(() => {
|
|
163
|
+
if (!startedClient) return;
|
|
164
|
+
const next = props.context ?? null;
|
|
165
|
+
const previous = lastSentContextRef.current;
|
|
166
|
+
if (previous !== void 0 && isSameValue(previous, next)) return;
|
|
167
|
+
const sent = copyPlain(next);
|
|
168
|
+
lastSentContextRef.current = sent;
|
|
169
|
+
startedClient.context(sent);
|
|
170
|
+
});
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
export {
|
|
174
|
+
RonzaWidget,
|
|
175
|
+
getRonza2 as getRonza,
|
|
176
|
+
useRonza
|
|
177
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ronza.ai/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "The Ronza chat agent in a React app: <RonzaWidget /> and useRonza(). Built on @ronza.ai/web.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ronza",
|
|
7
|
+
"chat",
|
|
8
|
+
"widget",
|
|
9
|
+
"agent",
|
|
10
|
+
"react"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"//": "Publish with pnpm (pnpm pack, then npm publish of that tarball, or pnpm publish): only pnpm turns workspace:^ into a caret range of the web package's version (^0.2.0); prepublishOnly (scripts/refuse-npm-publish.cjs) refuses a plain npm publish of this folder.",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/ronzahq/ronza-js.git",
|
|
17
|
+
"directory": "react"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://ronza.ai/docs/install",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"main": "./dist/index.cjs",
|
|
22
|
+
"module": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"import": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"require": {
|
|
31
|
+
"types": "./dist/index.d.cts",
|
|
32
|
+
"default": "./dist/index.cjs"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"README.md",
|
|
40
|
+
"LICENSE"
|
|
41
|
+
],
|
|
42
|
+
"sideEffects": false,
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=18"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@ronza.ai/web": "^0.2.0"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@testing-library/react": "^16.1.0",
|
|
57
|
+
"@types/node": "^22.0.0",
|
|
58
|
+
"@types/react": "^18.3.18",
|
|
59
|
+
"@types/react-dom": "^18.3.5",
|
|
60
|
+
"jsdom": "^25.0.1",
|
|
61
|
+
"react": "^18.3.1",
|
|
62
|
+
"react-dom": "^18.3.1",
|
|
63
|
+
"typescript": "^5.7.3",
|
|
64
|
+
"vite": "^6.0.11",
|
|
65
|
+
"vitest": "^2.0.0"
|
|
66
|
+
},
|
|
67
|
+
"scripts": {
|
|
68
|
+
"build": "pnpm --dir ../web run build && vite build && tsc -p tsconfig.build.json && node -e \"require('fs').copyFileSync('dist/index.d.ts','dist/index.d.cts')\"",
|
|
69
|
+
"test": "vitest run",
|
|
70
|
+
"typecheck": "tsc --noEmit -p tsconfig.json"
|
|
71
|
+
}
|
|
6
72
|
}
|