aboutsummaryrefslogtreecommitdiff
path: root/tools_todo.md
blob: 865394d73f862a0a69c7830a5e9cd06a83dfef6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
### What’s actually happening

1. **The loader _is_ registering your tool**
   You should see a console line like:

    ```
    ✓ Loaded dynamic tool 'inspirationalQuoteGeneratorTool' from 'dynamic/InspirationalQuoteGeneratorTool.ts'
    ```

    That proves the object is stored in `dynamicToolRegistry`.

2. **The validator rejects the LLM’s action string**
   The key in the registry is **`inspirationalQuoteGeneratorTool`**
   The LLM emitted **`inspirationalquotegenerator`**`allowedActions.includes("inspirationalquotegenerator")` is `false`_“Action … is not a valid tool”_.

So the bug is a **name-mismatch**, not a missing registration.

---

## 📏 Pick one naming convention and stick to it

Let’s convert every class name to an **all-lowercase string with the “Tool” suffix stripped**, e.g.:

```
InspirationalQuoteGeneratorTool → inspirationalquotegenerator
WordCountTool                  → wordcount
```

That way the key the loader stores **matches** what the LLM will read from the system prompt.

---

### 1. Add a helper

```ts
function classToActionKey(className: string): string {
    return className.replace(/Tool$/, '').toLowerCase();
}
```

---

### 2. Use it everywhere you register or expose a tool

#### a) Loader (`loadExistingDynamicTools`)

```ts
const actionName = classToActionKey(className);

if (!this.dynamicToolRegistry.has(actionName)) {
    const ToolClass = require(`../tools/${path}`)[className];
    if (ToolClass && ToolClass.prototype instanceof BaseTool) {
        const instance = new ToolClass();
        // Tell the prompt generator what name the model must use
        (instance as any).name = actionName;
        this.dynamicToolRegistry.set(actionName, instance);
        console.info(`✓ registered '${actionName}'`);
    }
}
```

#### b) Create-at-runtime flow (`CreateNewTool`)

```ts
const actionKey = classToActionKey(toolName); // toolName is the class name
agent.registerDynamicTool(actionKey, newToolInstance);
```

#### c) System-prompt generation

If your `BaseTool` already has a `name` field that `getReactPrompt` reads, you’ve set it above. Otherwise just update the code that builds `allTools`:

```ts
const allTools = this.getAllTools();
allTools.forEach(t => {
    if (!(t as any).name) (t as any).name = classToActionKey(t.constructor.name);
});
return getReactPrompt(allTools, docSummaries, chatHistory);
```

_(Or be cleaner and extend `BaseTool` with a proper `public name: string`.)_

---

### 3. (Optionally) normalise existing static tools

Set their `name` property the same way when you instantiate them:

```ts
this.tools.calculate = new CalculateTool();
(this.tools.calculate as any).name = 'calculate'; // already fine, but explicit
```

---

### 4. Test

1. Hard-reload / restart dev-server so the new code is bundled.
2. Ask: “Give me an inspirational quote” again.

Because the prompt now advertises **`inspirationalquotegenerator`** and the registry key matches, validation will pass and the tool will run.

---

## TL;DR

Your tools are loading; the _keys_ don’t match the action the model outputs.
Create a single `classToActionKey` helper, call it everywhere, and the mismatch disappears.