bs-agent 0.0.26 → 0.0.27
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 +50 -0
- package/dist/react/index.cjs +15 -2
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +0 -7
- package/dist/react/index.d.ts +0 -7
- package/dist/react/index.js +15 -2
- package/dist/react/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -370,6 +370,56 @@ handleSend(input: AgentInput, options?: {
|
|
|
370
370
|
`AgentInput` is `string | ContentPart[]` — see
|
|
371
371
|
[Multimodal Input](#multimodal-input).
|
|
372
372
|
|
|
373
|
+
### Text Modifiers
|
|
374
|
+
|
|
375
|
+
`useAgent` accepts optional modifier functions that transform streamed text
|
|
376
|
+
before it is stored in messages. This is useful for stripping thinking tags,
|
|
377
|
+
converting markup, or applying any real-time post-processing.
|
|
378
|
+
|
|
379
|
+
#### `textDeltaModifier`
|
|
380
|
+
|
|
381
|
+
Runs on **each incoming text chunk** before it is appended. Receives the raw
|
|
382
|
+
delta, the full text accumulated so far, and event metadata.
|
|
383
|
+
|
|
384
|
+
```tsx
|
|
385
|
+
const agent = useAgent(myAgent, {
|
|
386
|
+
textDeltaModifier: (delta, fullText, meta) => {
|
|
387
|
+
// Example: strip <think>…</think> tags from each chunk
|
|
388
|
+
return delta.replace(/<\/?think>/g, "");
|
|
389
|
+
},
|
|
390
|
+
});
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
#### `fullTextModifier`
|
|
394
|
+
|
|
395
|
+
Runs on the **accumulated full text** after every delta is appended. The return
|
|
396
|
+
value becomes the displayed text, while the unmodified accumulation is preserved
|
|
397
|
+
internally as `_rawText`.
|
|
398
|
+
|
|
399
|
+
```tsx
|
|
400
|
+
const agent = useAgent(myAgent, {
|
|
401
|
+
fullTextModifier: (fullText, meta) => {
|
|
402
|
+
// Example: render LaTeX-style math as Unicode
|
|
403
|
+
return convertLatex(fullText);
|
|
404
|
+
},
|
|
405
|
+
});
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
#### Using both together
|
|
409
|
+
|
|
410
|
+
When both modifiers are provided they **chain** in order:
|
|
411
|
+
|
|
412
|
+
```
|
|
413
|
+
raw delta → textDeltaModifier(delta) → accumulated text → fullTextModifier(accumulated) → display
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
`textDeltaModifier` acts as a per-chunk preprocessor; `fullTextModifier` acts as
|
|
417
|
+
a post-accumulation formatter on top of the already-modified text.
|
|
418
|
+
|
|
419
|
+
> **Note:** `_rawText` stores the `textDeltaModifier`-processed accumulation,
|
|
420
|
+
> not the original unmodified stream. If `textDeltaModifier` strips content,
|
|
421
|
+
> `fullTextModifier` will never see it.
|
|
422
|
+
|
|
373
423
|
## `useAgentContext` Hook
|
|
374
424
|
|
|
375
425
|
An alternative to `useAgent` for multi-agent setups. Initializes agents
|
package/dist/react/index.cjs
CHANGED
|
@@ -51,6 +51,18 @@ var TEMPORARY_SESSION_ID = "sess_temp";
|
|
|
51
51
|
|
|
52
52
|
// src/react/session-utils.ts
|
|
53
53
|
var import_react = require("react");
|
|
54
|
+
function sanitizeMessagesForStorage(messages) {
|
|
55
|
+
return messages.map((msg) => {
|
|
56
|
+
if (!msg.attachments || msg.attachments.length === 0) return msg;
|
|
57
|
+
return {
|
|
58
|
+
...msg,
|
|
59
|
+
attachments: msg.attachments.map((att) => {
|
|
60
|
+
const { data, ...metadata } = att;
|
|
61
|
+
return metadata;
|
|
62
|
+
})
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
}
|
|
54
66
|
var useSessionUtils = (agentId, allSessions, setAllSessions, currentSessionId, setCurrentSessionId, messagesRef) => {
|
|
55
67
|
const agentSessions = (0, import_react.useMemo)(() => allSessions[agentId] || {}, [agentId, allSessions]);
|
|
56
68
|
const syncSessionRef = (0, import_react.useRef)();
|
|
@@ -58,13 +70,14 @@ var useSessionUtils = (agentId, allSessions, setAllSessions, currentSessionId, s
|
|
|
58
70
|
if (!currentSessionId || currentSessionId === TEMPORARY_SESSION_ID) {
|
|
59
71
|
return;
|
|
60
72
|
}
|
|
73
|
+
const messagesToPersist = sanitizeMessagesForStorage(updatedMessages ?? messagesRef.current);
|
|
61
74
|
setAllSessions((prev) => ({
|
|
62
75
|
...prev,
|
|
63
76
|
[agentId]: {
|
|
64
77
|
...prev[agentId],
|
|
65
78
|
[currentSessionId]: {
|
|
66
79
|
...prev[agentId]?.[currentSessionId],
|
|
67
|
-
messages:
|
|
80
|
+
messages: messagesToPersist,
|
|
68
81
|
updatedAt: Date.now()
|
|
69
82
|
}
|
|
70
83
|
}
|
|
@@ -121,7 +134,7 @@ var useSessionUtils = (agentId, allSessions, setAllSessions, currentSessionId, s
|
|
|
121
134
|
id: sessionId,
|
|
122
135
|
createdAt: Date.now(),
|
|
123
136
|
updatedAt: Date.now(),
|
|
124
|
-
messages: currentMessages,
|
|
137
|
+
messages: sanitizeMessagesForStorage(currentMessages),
|
|
125
138
|
name: sessionName
|
|
126
139
|
}
|
|
127
140
|
}
|