@applica-software-guru/persona-sdk 0.1.70 → 0.1.72
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 +114 -8
- package/dist/bundle.cjs.js +12 -12
- package/dist/bundle.cjs.js.map +1 -1
- package/dist/bundle.es.js +1368 -1359
- package/dist/bundle.es.js.map +1 -1
- package/dist/bundle.iife.js +12 -12
- package/dist/bundle.iife.js.map +1 -1
- package/dist/bundle.umd.js +12 -12
- package/dist/bundle.umd.js.map +1 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/types.d.ts +5 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/runtime.tsx +20 -11
- package/src/types.ts +7 -0
package/README.md
CHANGED
|
@@ -14,6 +14,7 @@ Persona SDK is the official SDK for the Persona API. It provides a simple and ef
|
|
|
14
14
|
- [Installation](#installation)
|
|
15
15
|
- [Usage](#usage)
|
|
16
16
|
- [Accessing Messages](#accessing-messages)
|
|
17
|
+
- [Message Filtering](#message-filtering)
|
|
17
18
|
- [Protocols Overview](#protocols-overview)
|
|
18
19
|
- [API Reference](#api-reference)
|
|
19
20
|
- [Customization](#customization)
|
|
@@ -131,6 +132,110 @@ Both approaches are useful for building custom UI components, analytics, or debu
|
|
|
131
132
|
|
|
132
133
|
---
|
|
133
134
|
|
|
135
|
+
## Message Filtering
|
|
136
|
+
|
|
137
|
+
The `messageFilter` prop allows you to process and filter messages before they are displayed in the chat interface. This provides powerful customization capabilities for controlling what messages users see and how they are presented.
|
|
138
|
+
|
|
139
|
+
### Basic Usage
|
|
140
|
+
|
|
141
|
+
Add a `messageFilter` function to your `PersonaRuntimeProvider`:
|
|
142
|
+
|
|
143
|
+
```tsx
|
|
144
|
+
import { PersonaRuntimeProvider, MessageFilter, PersonaMessage } from '@applica-software-guru/persona-sdk';
|
|
145
|
+
|
|
146
|
+
const myMessageFilter: MessageFilter = (messages: PersonaMessage[]) => {
|
|
147
|
+
// Process and return the filtered messages
|
|
148
|
+
return messages.filter((message) => message.type !== 'reasoning');
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
function App() {
|
|
152
|
+
return (
|
|
153
|
+
<PersonaRuntimeProvider apiKey="your-api-key" agentId="your-agent-id" protocols={{ rest: true }} messageFilter={myMessageFilter}>
|
|
154
|
+
{/* Your app content */}
|
|
155
|
+
</PersonaRuntimeProvider>
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### MessageFilter Type
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
type MessageFilter = (messages: PersonaMessage[]) => PersonaMessage[];
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
The function receives an array of `PersonaMessage` objects and must return an array of `PersonaMessage` objects.
|
|
167
|
+
|
|
168
|
+
### Common Use Cases
|
|
169
|
+
|
|
170
|
+
#### 1. Hide Reasoning Messages
|
|
171
|
+
|
|
172
|
+
```tsx
|
|
173
|
+
const hideReasoningFilter: MessageFilter = (messages) => {
|
|
174
|
+
return messages.filter((message) => message.type !== 'reasoning');
|
|
175
|
+
};
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
#### 2. Content Filtering
|
|
179
|
+
|
|
180
|
+
```tsx
|
|
181
|
+
const profanityFilter: MessageFilter = (messages) => {
|
|
182
|
+
const bannedWords = ['spam', 'inappropriate'];
|
|
183
|
+
return messages.filter((message) => {
|
|
184
|
+
const text = message.text.toLowerCase();
|
|
185
|
+
return !bannedWords.some((word) => text.includes(word));
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
#### 3. Message Limit
|
|
191
|
+
|
|
192
|
+
```tsx
|
|
193
|
+
const messageLimitFilter: MessageFilter = (messages) => {
|
|
194
|
+
const maxMessages = 50;
|
|
195
|
+
return messages.length > maxMessages ? messages.slice(-maxMessages) : messages;
|
|
196
|
+
};
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
#### 4. Text Transformation
|
|
200
|
+
|
|
201
|
+
```tsx
|
|
202
|
+
const transformTextFilter: MessageFilter = (messages) => {
|
|
203
|
+
return messages.map((message) => ({
|
|
204
|
+
...message,
|
|
205
|
+
text: message.text.replace(/\b(TODO|FIXME)\b/g, '⚠️ $1'),
|
|
206
|
+
}));
|
|
207
|
+
};
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
#### 5. Combining Multiple Filters
|
|
211
|
+
|
|
212
|
+
```tsx
|
|
213
|
+
const combinedFilter: MessageFilter = (messages) => {
|
|
214
|
+
let filtered = hideReasoningFilter(messages);
|
|
215
|
+
filtered = profanityFilter(filtered);
|
|
216
|
+
filtered = messageLimitFilter(filtered);
|
|
217
|
+
return filtered;
|
|
218
|
+
};
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### Important Notes
|
|
222
|
+
|
|
223
|
+
- The filter is applied to **all messages**, including both incoming messages from the assistant and outgoing messages from the user
|
|
224
|
+
- The filter function should be pure (no side effects) and return a new array
|
|
225
|
+
- Filters are applied every time the message list is updated
|
|
226
|
+
- If no filter is provided, all messages are displayed as-is
|
|
227
|
+
- Be careful with message ordering when filtering, as it may affect conversation context
|
|
228
|
+
|
|
229
|
+
### Performance Considerations
|
|
230
|
+
|
|
231
|
+
- Keep filter functions lightweight as they run on every message update
|
|
232
|
+
- For expensive operations, consider debouncing or memoization
|
|
233
|
+
- Avoid creating new filter functions on every render to prevent unnecessary re-processing
|
|
234
|
+
|
|
235
|
+
For complete working examples, see the `examples/message-filter-example.tsx` file in the repository.
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
134
239
|
## Protocols Overview
|
|
135
240
|
|
|
136
241
|
The Persona SDK supports multiple communication protocols to provide flexibility and performance. These protocols can work together, sharing the same session and data, to ensure the best possible experience.
|
|
@@ -175,14 +280,15 @@ This design allows the Persona SDK to adapt dynamically to the available protoco
|
|
|
175
280
|
|
|
176
281
|
### Props for `PersonaRuntimeProvider`
|
|
177
282
|
|
|
178
|
-
| Prop
|
|
179
|
-
|
|
|
180
|
-
| `dev`
|
|
181
|
-
| `logger`
|
|
182
|
-
| `protocols`
|
|
183
|
-
| `session`
|
|
184
|
-
| `apiKey`
|
|
185
|
-
| `agentId`
|
|
283
|
+
| Prop | Type | Default Value | Description |
|
|
284
|
+
| --------------- | ------------- | ----------------------------------------------- | --------------------------------------------------------------------------------------------------- |
|
|
285
|
+
| `dev` | boolean | `false` | Enable development mode for local Persona API usage. |
|
|
286
|
+
| `logger` | function | `console.log` | Custom logger for debugging and logging messages. |
|
|
287
|
+
| `protocols` | object | `{ rest: true, webrtc: true, websocket: true }` | Protocols to use for the chat. Can be enabled/disabled using boolean values. |
|
|
288
|
+
| `session` | string | `undefined` | Session ID for the chat. Required for the chat to function. |
|
|
289
|
+
| `apiKey` | string | `toolkit-test` | API key for authentication. Required for the chat to function. |
|
|
290
|
+
| `agentId` | string | `default` | Agent ID for the chat. Required for the chat to function. |
|
|
291
|
+
| `messageFilter` | MessageFilter | `undefined` | Optional function to filter and transform messages before they are displayed in the chat interface. |
|
|
186
292
|
|
|
187
293
|
---
|
|
188
294
|
|