@sisu-ai/mw-react-parser 3.0.1 → 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.
Files changed (2) hide show
  1. package/README.md +75 -14
  2. package/package.json +3 -2
package/README.md CHANGED
@@ -1,33 +1,94 @@
1
1
  # @sisu-ai/mw-react-parser
2
2
 
3
- 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.
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
+
5
+ [![Tests](https://github.com/finger-gun/sisu/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/finger-gun/sisu/actions/workflows/tests.yml)
6
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](https://github.com/finger-gun/sisu/blob/main/LICENSE)
7
+ [![Downloads](https://img.shields.io/npm/dm/%40sisu-ai%2Fmw-react-parser)](https://www.npmjs.com/package/@sisu-ai/mw-react-parser)
8
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/finger-gun/sisu/blob/main/CONTRIBUTING.md)
4
9
 
5
10
  ## Setup
6
11
  ```bash
7
12
  npm i @sisu-ai/mw-react-parser
8
13
  ```
9
14
 
10
- ## Documentation
11
- Discover what you can do through examples or documentation. Check it out at https://github.com/finger-gun/sisu
15
+ ## Exports
16
+ - `reactToolLoop()` returns middleware that performs one ReAct cycle as described.
17
+
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.
12
28
 
13
- ## Behavior (ReAct pattern)
14
- - Think: Ask the model for an initial response with tools disabled (`toolChoice: 'none'`).
15
- - Act: Parse `Action: <tool>` and `Action Input: <json or text>` from its message.
16
- - Observe: Execute the named tool with parsed input, append a `role:'tool'` message.
17
- - Reflect: Ask the model again to produce the final answer that incorporates the observation.
29
+ This keeps the loop adapter‑agnostic and easy to reason about at the cost of relying on formatting.
18
30
 
19
31
  ## Usage
20
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';
21
38
  import { reactToolLoop } from '@sisu-ai/mw-react-parser';
22
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
+
23
62
  const app = new Agent()
63
+ .use(registerTools([webSearch as any]))
64
+ .use(inputToMessage)
24
65
  .use(reactToolLoop());
25
66
  ```
26
67
 
27
- Prompting tip
28
- - Seed system with the expected format, e.g.:
29
- - `Use tools when helpful. Reply with\nAction: <tool>\nAction Input: <JSON>`
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.
76
+
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.
85
+
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.
30
89
 
31
- Customizing
32
- - Provide your own tools via `@sisu-ai/mw-register-tools` and define strict zod schemas for reliable parsing.
33
- - If you need a different action syntax, build a small middleware before/after to transform the assistant message.
90
+ - [Code of Conduct](https://github.com/finger-gun/sisu/blob/main/CODE_OF_CONDUCT.md)
91
+ - [Contributing Guide](https://github.com/finger-gun/sisu/blob/main/CONTRIBUTING.md)
92
+ - [License](https://github.com/finger-gun/sisu/blob/main/LICENSE)
93
+ - [Report a Bug](https://github.com/finger-gun/sisu/issues/new?template=bug_report.md)
94
+ - [Request a Feature](https://github.com/finger-gun/sisu/issues/new?template=feature_request.md)
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@sisu-ai/mw-react-parser",
3
- "version": "3.0.1",
3
+ "version": "3.0.3",
4
+ "license": "Apache-2.0",
4
5
  "type": "module",
5
6
  "main": "dist/index.js",
6
7
  "types": "dist/index.d.ts",
@@ -14,7 +15,7 @@
14
15
  "build": "tsc -b"
15
16
  },
16
17
  "peerDependencies": {
17
- "@sisu-ai/core": "1.0.1"
18
+ "@sisu-ai/core": "1.0.2"
18
19
  },
19
20
  "repository": {
20
21
  "type": "git",