@sisu-ai/mw-react-parser 1.0.0

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 ADDED
@@ -0,0 +1,17 @@
1
+ # @sisu-ai/mw-react-parser
2
+
3
+ Lightweight ReAct-style tool loop.
4
+
5
+ ## Behavior
6
+ - Asks the model for a reply with tools disabled.
7
+ - Parses `Action: <tool>` and `Action Input: <json or text>` from the assistant message.
8
+ - Invokes the tool and appends a `role:'tool'` message.
9
+ - Asks the model again for a final assistant message.
10
+
11
+ ## Usage
12
+ ```ts
13
+ import { reactToolLoop } from '@sisu-ai/mw-react-parser';
14
+
15
+ const app = new Agent()
16
+ .use(reactToolLoop());
17
+ ```
@@ -0,0 +1,2 @@
1
+ import type { Middleware } from '@sisu-ai/core';
2
+ export declare const reactToolLoop: () => Middleware;
package/dist/index.js ADDED
@@ -0,0 +1,35 @@
1
+ export const reactToolLoop = () => {
2
+ return async (ctx, next) => {
3
+ await next(); // upstream prep
4
+ const res = await ctx.model.generate(ctx.messages, { toolChoice: 'none', signal: ctx.signal });
5
+ const msg = res.message;
6
+ const content = msg?.content ?? '';
7
+ const toolMatch = content.match(/Action:\s*(\w+)/i);
8
+ const inputMatch = content.match(/Action Input:\s*([\s\S]*)/i);
9
+ if (toolMatch && inputMatch) {
10
+ const toolName = toolMatch[1];
11
+ ctx.log.info?.('[react] parsed tool action', { toolName });
12
+ const tool = ctx.tools.get(toolName);
13
+ if (!tool)
14
+ throw new Error('Unknown tool: ' + toolName);
15
+ let args = inputMatch[1].trim();
16
+ try {
17
+ args = JSON.parse(args);
18
+ }
19
+ catch { }
20
+ ctx.log.debug?.('[react] invoking tool', { toolName, args });
21
+ const result = await tool.handler(args, ctx);
22
+ // ReAct pattern: feed tool output back as a user message (not provider-specific tool role)
23
+ const rendered = typeof result === 'string' ? result : JSON.stringify(result);
24
+ ctx.messages.push({ role: 'user', content: `Observation (${toolName}): ${rendered}` });
25
+ const res2 = await ctx.model.generate(ctx.messages, { toolChoice: 'none', signal: ctx.signal });
26
+ ctx.log.debug?.('[react] got assistant follow-up');
27
+ ctx.messages.push(res2.message);
28
+ }
29
+ else {
30
+ ctx.log.debug?.('[react] no tool action parsed; appending assistant message');
31
+ if (msg)
32
+ ctx.messages.push(msg);
33
+ }
34
+ };
35
+ };
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@sisu-ai/mw-react-parser",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "scripts": {
14
+ "build": "tsc -b"
15
+ },
16
+ "peerDependencies": {
17
+ "@sisu-ai/core": "0.2.0"
18
+ }
19
+ }