@sisu-ai/mw-react-parser 9.0.1 → 9.0.2

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/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import type { Middleware } from '@sisu-ai/core';
1
+ import type { Middleware } from "@sisu-ai/core";
2
2
  export declare const reactToolLoop: () => Middleware;
package/dist/index.js CHANGED
@@ -1,33 +1,44 @@
1
1
  export const reactToolLoop = () => {
2
2
  return async (ctx, next) => {
3
3
  await next(); // upstream prep
4
- const res = await ctx.model.generate(ctx.messages, { toolChoice: 'none', signal: ctx.signal });
4
+ const res = (await ctx.model.generate(ctx.messages, {
5
+ toolChoice: "none",
6
+ signal: ctx.signal,
7
+ }));
5
8
  const msg = res.message;
6
- const content = msg?.content ?? '';
9
+ const content = msg?.content ?? "";
7
10
  const toolMatch = content.match(/Action:\s*(\w+)/i);
8
11
  const inputMatch = content.match(/Action Input:\s*([\s\S]*)/i);
9
12
  if (toolMatch && inputMatch) {
10
13
  const toolName = toolMatch[1];
11
- ctx.log.info?.('[react] parsed tool action', { toolName });
14
+ ctx.log.info?.("[react] parsed tool action", { toolName });
12
15
  const tool = ctx.tools.get(toolName);
13
16
  if (!tool)
14
- throw new Error('Unknown tool: ' + toolName);
17
+ throw new Error("Unknown tool: " + toolName);
15
18
  let args = inputMatch[1].trim();
16
19
  try {
17
- args = JSON.parse(args);
20
+ args = JSON.parse(String(args));
18
21
  }
19
- catch { }
20
- ctx.log.debug?.('[react] invoking tool', { toolName, args });
22
+ catch {
23
+ args = inputMatch[1].trim();
24
+ }
25
+ ctx.log.debug?.("[react] invoking tool", { toolName, args });
21
26
  const result = await tool.handler(args, ctx);
22
27
  // 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');
28
+ const rendered = typeof result === "string" ? result : JSON.stringify(result);
29
+ ctx.messages.push({
30
+ role: "user",
31
+ content: `Observation (${toolName}): ${rendered}`,
32
+ });
33
+ const res2 = (await ctx.model.generate(ctx.messages, {
34
+ toolChoice: "none",
35
+ signal: ctx.signal,
36
+ }));
37
+ ctx.log.debug?.("[react] got assistant follow-up");
27
38
  ctx.messages.push(res2.message);
28
39
  }
29
40
  else {
30
- ctx.log.debug?.('[react] no tool action parsed; appending assistant message');
41
+ ctx.log.debug?.("[react] no tool action parsed; appending assistant message");
31
42
  if (msg)
32
43
  ctx.messages.push(msg);
33
44
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sisu-ai/mw-react-parser",
3
- "version": "9.0.1",
3
+ "version": "9.0.2",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",