@runtypelabs/persona 3.1.0 → 3.1.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 +31 -3
- package/dist/index.cjs +35 -35
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +34 -2
- package/dist/index.d.ts +34 -2
- package/dist/index.global.js +49 -49
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +35 -35
- package/dist/index.js.map +1 -1
- package/dist/widget.css +7 -0
- package/package.json +1 -1
- package/src/styles/widget.css +7 -0
- package/src/types.ts +32 -2
- package/src/ui.ts +18 -2
package/README.md
CHANGED
|
@@ -624,13 +624,15 @@ initAgentWidget({
|
|
|
624
624
|
|
|
625
625
|
The `onStateLoaded` hook is called after state is loaded from the storage adapter, but before the widget initializes. Use this to transform or inject messages based on external state (e.g., navigation flags, checkout returns).
|
|
626
626
|
|
|
627
|
+
Returning `{ state, open: true }` also tells the widget to open the panel after initialization — useful when injecting a post-navigation message that the user should immediately see.
|
|
628
|
+
|
|
627
629
|
```ts
|
|
630
|
+
// Plain state transform
|
|
628
631
|
initAgentWidget({
|
|
629
632
|
target: 'body',
|
|
630
633
|
config: {
|
|
631
634
|
storageAdapter: createLocalStorageAdapter('my-chat'),
|
|
632
635
|
onStateLoaded: (state) => {
|
|
633
|
-
// Check for pending navigation message
|
|
634
636
|
const navMessage = consumeNavigationFlag();
|
|
635
637
|
if (navMessage) {
|
|
636
638
|
return {
|
|
@@ -647,10 +649,36 @@ initAgentWidget({
|
|
|
647
649
|
}
|
|
648
650
|
}
|
|
649
651
|
});
|
|
652
|
+
|
|
653
|
+
// Return { state, open: true } to also open the panel
|
|
654
|
+
initAgentWidget({
|
|
655
|
+
target: 'body',
|
|
656
|
+
config: {
|
|
657
|
+
storageAdapter: createLocalStorageAdapter('my-chat'),
|
|
658
|
+
onStateLoaded: (state) => {
|
|
659
|
+
const navMessage = consumeNavigationFlag();
|
|
660
|
+
if (navMessage) {
|
|
661
|
+
return {
|
|
662
|
+
state: {
|
|
663
|
+
...state,
|
|
664
|
+
messages: [...(state.messages || []), {
|
|
665
|
+
id: `nav-${Date.now()}`,
|
|
666
|
+
role: 'assistant',
|
|
667
|
+
content: navMessage,
|
|
668
|
+
createdAt: new Date().toISOString()
|
|
669
|
+
}]
|
|
670
|
+
},
|
|
671
|
+
open: true
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
return state;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
});
|
|
650
678
|
```
|
|
651
679
|
|
|
652
680
|
**Use cases:**
|
|
653
|
-
- Inject messages after page navigation (e.g., "Here are our products!")
|
|
681
|
+
- Inject messages after page navigation (e.g., "Here are our products!") and open the panel
|
|
654
682
|
- Add confirmation messages after checkout/payment returns
|
|
655
683
|
- Transform or filter loaded messages
|
|
656
684
|
- Inject system messages based on external state
|
|
@@ -2019,7 +2047,7 @@ config: {
|
|
|
2019
2047
|
| `initialMessages` | `AgentWidgetMessage[]` | Seed the conversation transcript with initial messages. |
|
|
2020
2048
|
| `persistState` | `boolean \| AgentWidgetPersistStateConfig` | Persist widget state across page navigations. `true` uses defaults (sessionStorage). |
|
|
2021
2049
|
| `storageAdapter` | `AgentWidgetStorageAdapter` | Custom storage adapter with `load()`, `save(state)`, and `clear()` methods. |
|
|
2022
|
-
| `onStateLoaded` | `(state: AgentWidgetStoredState) => AgentWidgetStoredState` | Transform state after loading from storage but before widget initialization. |
|
|
2050
|
+
| `onStateLoaded` | `(state: AgentWidgetStoredState) => AgentWidgetStoredState \| { state: AgentWidgetStoredState; open?: boolean }` | Transform state after loading from storage but before widget initialization. Return `{ state, open: true }` to also open the panel. |
|
|
2023
2051
|
| `clearChatHistoryStorageKey` | `string` | Additional localStorage key to clear on chat reset. The widget clears `"persona-chat-history"` by default. |
|
|
2024
2052
|
|
|
2025
2053
|
**`persistState`** — `AgentWidgetPersistStateConfig`
|