@sisu-ai/mw-react-parser 3.0.2 → 3.0.3
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 +65 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,42 +1,92 @@
|
|
|
1
1
|
# @sisu-ai/mw-react-parser
|
|
2
|
+
|
|
3
|
+
Lightweight ReAct-style tool loop. The model proposes an action in plain text, you parse it, execute a tool, then the model reflects and answers.
|
|
4
|
+
|
|
2
5
|
[](https://github.com/finger-gun/sisu/actions/workflows/tests.yml)
|
|
3
6
|
[](https://github.com/finger-gun/sisu/blob/main/LICENSE)
|
|
4
7
|
[](https://www.npmjs.com/package/@sisu-ai/mw-react-parser)
|
|
5
8
|
[](https://github.com/finger-gun/sisu/blob/main/CONTRIBUTING.md)
|
|
6
9
|
|
|
7
|
-
Lightweight ReAct-style tool loop. Lets the model decide an action in natural language, you parse it, execute, and then the model reflects and answers.
|
|
8
|
-
|
|
9
10
|
## Setup
|
|
10
11
|
```bash
|
|
11
12
|
npm i @sisu-ai/mw-react-parser
|
|
12
13
|
```
|
|
13
14
|
|
|
14
|
-
##
|
|
15
|
-
|
|
15
|
+
## Exports
|
|
16
|
+
- `reactToolLoop()` — returns middleware that performs one ReAct cycle as described.
|
|
16
17
|
|
|
17
|
-
##
|
|
18
|
-
- Think
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
|
|
18
|
+
## What It Does
|
|
19
|
+
- Think → Act → Observe → Reflect loop without provider‑specific function calling.
|
|
20
|
+
- Parses `Action: <tool>` and `Action Input: <json or text>` from the assistant’s message.
|
|
21
|
+
- Invokes a registered tool, feeds the observation back, and asks the model for a final answer.
|
|
22
|
+
|
|
23
|
+
## How It Works
|
|
24
|
+
- Calls `model.generate(..., { toolChoice: 'none' })` to get an initial assistant message.
|
|
25
|
+
- Extracts `tool` and `args` via regex; attempts `JSON.parse` on the input, falls back to raw text.
|
|
26
|
+
- Executes the tool and appends a user message like `Observation (tool): <result>`.
|
|
27
|
+
- Calls `model.generate` again (tools still disabled) and pushes the final assistant message.
|
|
28
|
+
|
|
29
|
+
This keeps the loop adapter‑agnostic and easy to reason about at the cost of relying on formatting.
|
|
22
30
|
|
|
23
31
|
## Usage
|
|
24
32
|
```ts
|
|
33
|
+
import 'dotenv/config';
|
|
34
|
+
import { Agent, createConsoleLogger, InMemoryKV, NullStream, SimpleTools, type Ctx } from '@sisu-ai/core';
|
|
35
|
+
import { openAIAdapter } from '@sisu-ai/adapter-openai';
|
|
36
|
+
import { registerTools } from '@sisu-ai/mw-register-tools';
|
|
37
|
+
import { inputToMessage } from '@sisu-ai/mw-conversation-buffer';
|
|
25
38
|
import { reactToolLoop } from '@sisu-ai/mw-react-parser';
|
|
26
39
|
|
|
40
|
+
const model = openAIAdapter({ model: 'gpt-4o-mini' });
|
|
41
|
+
|
|
42
|
+
// Example tool
|
|
43
|
+
const webSearch = {
|
|
44
|
+
name: 'webSearch',
|
|
45
|
+
description: 'Search the web for a query',
|
|
46
|
+
schema: { type: 'object', properties: { q: { type: 'string' } }, required: ['q'] },
|
|
47
|
+
handler: async ({ q }: { q: string }) => ({ top: [`Result for ${q}`] })
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const ctx: Ctx = {
|
|
51
|
+
input: 'Find the npm page for @sisu-ai/core then summarize.',
|
|
52
|
+
messages: [{ role: 'system', content: 'When helpful, decide an action using:\nAction: <tool>\nAction Input: <JSON>. Then reflect with the observation.' }],
|
|
53
|
+
model,
|
|
54
|
+
tools: new SimpleTools(),
|
|
55
|
+
memory: new InMemoryKV(),
|
|
56
|
+
stream: new NullStream(),
|
|
57
|
+
state: {},
|
|
58
|
+
signal: new AbortController().signal,
|
|
59
|
+
log: createConsoleLogger({ level: 'info' })
|
|
60
|
+
};
|
|
61
|
+
|
|
27
62
|
const app = new Agent()
|
|
63
|
+
.use(registerTools([webSearch as any]))
|
|
64
|
+
.use(inputToMessage)
|
|
28
65
|
.use(reactToolLoop());
|
|
29
66
|
```
|
|
30
67
|
|
|
31
|
-
Prompting
|
|
32
|
-
- Seed system with the
|
|
33
|
-
- `Use tools when helpful. Reply with
|
|
68
|
+
### Prompting Tips
|
|
69
|
+
- Seed the system prompt with the required format, e.g.:
|
|
70
|
+
- `Use tools when helpful. Reply with:\nAction: <tool>\nAction Input: <JSON>`
|
|
71
|
+
- Keep tool schemas strict and arguments small to make parsing robust.
|
|
72
|
+
|
|
73
|
+
## When To Use
|
|
74
|
+
- You want a provider‑agnostic tool loop that works with any chat model.
|
|
75
|
+
- You need a simple ReAct cycle without native function calling.
|
|
34
76
|
|
|
35
|
-
|
|
36
|
-
-
|
|
37
|
-
-
|
|
77
|
+
## When Not To Use
|
|
78
|
+
- You rely on provider‑native tools/function‑calling — prefer `@sisu-ai/mw-tool-calling` instead.
|
|
79
|
+
- You need multi‑step or iterative planning — use `iterativeToolCalling` or a planner middleware.
|
|
80
|
+
|
|
81
|
+
## Notes & Gotchas
|
|
82
|
+
- Formatting sensitivity: parsing uses regex; poor formatting may fail to extract the action.
|
|
83
|
+
- Security: validate tool inputs (zod) and guard tools with allow‑lists/capabilities.
|
|
84
|
+
- Streaming: this middleware uses non‑streaming `generate` calls; pair with a streaming middleware if you need live tokens.
|
|
38
85
|
|
|
39
86
|
# Community & Support
|
|
87
|
+
|
|
88
|
+
Discover what you can do through examples or documentation. Check it out at https://github.com/finger-gun/sisu. Example projects live under [`examples/`](https://github.com/finger-gun/sisu/tree/main/examples) in the repo.
|
|
89
|
+
|
|
40
90
|
- [Code of Conduct](https://github.com/finger-gun/sisu/blob/main/CODE_OF_CONDUCT.md)
|
|
41
91
|
- [Contributing Guide](https://github.com/finger-gun/sisu/blob/main/CONTRIBUTING.md)
|
|
42
92
|
- [License](https://github.com/finger-gun/sisu/blob/main/LICENSE)
|