Skip to content

Commit 1e12d63

Browse files
Merge pull request #13 from wisedev-code/feat/add_tool_support
feat: include tool support
2 parents b8dbe5d + c9fa1fa commit 1e12d63

5 files changed

Lines changed: 515 additions & 0 deletions

File tree

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
# ⚙️ StepBuilder Overview
2+
3+
The **`StepBuilder`** class provides a fluent interface for defining the **behavioral flow of an AI Agent**.
4+
It allows developers to specify **how an agent should process input, use memory, access knowledge, or perform role-based actions** — all through a clear, chainable syntax.
5+
6+
This builder is primarily used when creating or configuring an Agent (e.g., via `AIHub.Agent()`), to define the ordered sequence of reasoning or operational steps the Agent should follow.
7+
8+
---
9+
10+
### 🧩 Namespace
11+
12+
```csharp
13+
using MaIN.Domain.Entities.Agents.Knowledge;
14+
using MaIN.Services.Services.Models.Commands;
15+
```
16+
17+
---
18+
19+
### 🏗️ Class Definition
20+
21+
```csharp
22+
public class StepBuilder
23+
```
24+
25+
A utility class for constructing a list of predefined **agent execution steps**.
26+
Each step defines a mode of operation that influences how the Agent interprets, searches, or responds to input.
27+
28+
---
29+
30+
### 🔹 Core Methods
31+
32+
#### **`Instance`**
33+
34+
```csharp
35+
public static StepBuilder Instance => new();
36+
```
37+
38+
Provides a new instance of `StepBuilder` for fluent chaining.
39+
Used as an entry point for defining a step sequence:
40+
41+
```csharp
42+
StepBuilder.Instance.Answer().Build();
43+
```
44+
45+
---
46+
47+
#### **`Answer()`**
48+
49+
Marks a basic answering step — the Agent simply generates a direct response to the input.
50+
51+
---
52+
53+
#### **`AnswerUseMemory()`**
54+
55+
Enables the Agent to **access and use stored memory** when forming a response.
56+
57+
---
58+
59+
#### **`AnswerUseKnowledge(bool alwaysSearchMemory = false)`**
60+
61+
Allows the Agent to **search its knowledge base** to form an answer.
62+
The optional `alwaysSearchMemory` flag ensures memory is always consulted alongside knowledge retrieval.
63+
64+
---
65+
66+
#### **`AnswerUseKnowledgeWithTags(params string[] tags)`**
67+
68+
Filters knowledge retrieval by **specific tags**, letting the Agent focus on a targeted domain of stored information.
69+
70+
Example:
71+
72+
```csharp
73+
StepBuilder.Instance.AnswerUseKnowledgeWithTags("finance", "market");
74+
```
75+
76+
---
77+
78+
#### **`AnswerUseKnowledgeWithType(KnowledgeItemType type)`**
79+
80+
Restricts knowledge usage to a particular **type** (e.g., documents, facts, summaries).
81+
82+
---
83+
84+
#### **`AnswerUseKnowledgeAndMemory()`**
85+
86+
Combines both **knowledge-based reasoning** and **memory recall** for more contextually rich responses.
87+
88+
---
89+
90+
#### **`AnswerUseKnowledgeAndMemoryWithTags(params string[] tags)`**
91+
92+
A hybrid mode using both memory and tagged knowledge retrieval.
93+
94+
---
95+
96+
#### **`Become(string role)`**
97+
98+
Instructs the Agent to **adopt a specific role or persona** before continuing execution.
99+
100+
Example:
101+
102+
```csharp
103+
.Become("assistant_expert")
104+
```
105+
106+
---
107+
108+
#### **`FetchData(FetchResponseType fetchResponseType = FetchResponseType.AS_Answer)`**
109+
110+
Defines a step where the Agent **fetches external data**, optionally as a system-level operation.
111+
112+
* `AS_Answer` → Fetches data to produce a normal response
113+
* `AS_System` → Fetches data for internal processing or state updates
114+
115+
---
116+
117+
#### **`Mcp()`**
118+
119+
Adds an **MCP (Multi-Component Process)** step — typically used for complex, multi-agent or multi-step coordination workflows.
120+
121+
---
122+
123+
#### **`Redirect(string agentId, string output = "AS_Output", string mode = "REPLACE")`**
124+
125+
Redirects execution flow to another Agent.
126+
Useful for **delegation or agent chaining**.
127+
128+
Example:
129+
130+
```csharp
131+
.Redirect("knowledge_agent", "AS_Output", "MERGE");
132+
```
133+
134+
---
135+
136+
#### **`Build()`**
137+
138+
Finalizes and returns the constructed list of steps.
139+
This list is then passed to the Agent configuration pipeline.
140+
141+
Example:
142+
143+
```csharp
144+
.WithSteps(StepBuilder.Instance
145+
.AnswerUseKnowledgeAndMemory()
146+
.Build())
147+
```
148+
149+
---
150+
151+
### 🧠 Example Usage
152+
153+
```csharp
154+
var steps = StepBuilder.Instance
155+
.AnswerUseKnowledgeAndMemoryWithTags("research", "analysis")
156+
.Become("data_researcher")
157+
.Build();
158+
159+
await AIHub.Agent()
160+
.WithModel("claude-sonnet-4-5-20250929")
161+
.WithSteps(steps)
162+
.CreateAsync(interactiveResponse: true);
163+
```
164+
165+
---
166+
167+
### 🔍 Summary
168+
169+
| Category | Purpose |
170+
| ---------------------- | --------------------------------------------- |
171+
| **Answer** | Simple or knowledge/memory-enhanced responses |
172+
| **Knowledge & Memory** | Access stored or learned context |
173+
| **Role / Behavior** | Define how the agent behaves or identifies |
174+
| **Data Fetching** | Integrate external or internal data sources |
175+
| **Redirection** | Delegate tasks between agents |
176+
| **MCP** | MCP server call |
177+
178+
---
179+
180+
The `StepBuilder` provides a **modular, declarative way to control agent logic**, making agent configurations expressive, reusable, and easy to reason about.
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
# 🧰 ToolsConfigurationBuilder Overview
2+
3+
The **`ToolsConfigurationBuilder`** class provides a structured and extensible way to define and register **tools** that can be used by AI agents during execution.
4+
Tools act as **callable functions** — external operations the model can invoke dynamically to retrieve data, perform computations, or trigger actions.
5+
6+
This builder simplifies tool creation by offering multiple overloads for different execution patterns (sync/async, typed/untyped, parameterized/no-parameter tools).
7+
8+
---
9+
10+
### 🧩 Namespace
11+
12+
```csharp
13+
using System.Text.Json;
14+
using MaIN.Domain.Entities.Tools;
15+
```
16+
17+
---
18+
19+
### 🏗️ Class Definition
20+
21+
```csharp
22+
public class ToolsConfigurationBuilder
23+
```
24+
25+
A fluent builder for constructing a **`ToolsConfiguration`** object, which contains a list of **`ToolDefinition`** entries and optional configuration settings (such as tool selection behavior).
26+
27+
Each added tool defines a function name, description, expected parameters, and an execution delegate.
28+
29+
---
30+
31+
### 🔹 Core Usage
32+
33+
Used during agent configuration to specify tools the model can access:
34+
35+
```csharp
36+
.WithTools(new ToolsConfigurationBuilder()
37+
.AddTool(
38+
name: "get_weather",
39+
description: "Retrieve current weather information",
40+
parameters: new { type = "object", properties = new { city = new { type = "string" } } },
41+
execute: WeatherTools.GetWeather)
42+
.WithToolChoice("auto")
43+
.Build())
44+
```
45+
46+
---
47+
48+
### 🔧 Methods Overview
49+
50+
#### **`AddDefaultTool(string type)`**
51+
52+
Registers a basic tool definition with a specific type.
53+
Used when predefined tool behavior is already encapsulated elsewhere.
54+
55+
```csharp
56+
.AddDefaultTool("system_logger")
57+
```
58+
59+
---
60+
61+
#### **`AddTool(string name, string description, object parameters, Func<string, Task<string>> execute)`**
62+
63+
Registers an asynchronous tool that receives raw JSON input and returns a serialized string result.
64+
This version is suitable when working directly with serialized arguments.
65+
66+
---
67+
68+
#### **`AddTool(string name, string description, object parameters, Func<string, string> execute)`**
69+
70+
Registers a synchronous version of the above method.
71+
The provided function executes immediately and returns a plain string result.
72+
73+
---
74+
75+
#### **`AddTool<TArgs>(string name, string description, object parameters, Func<TArgs, Task<object>> execute)`**
76+
77+
Registers a **typed asynchronous tool**.
78+
The input JSON is automatically deserialized into a strongly-typed object (`TArgs`), and the result is serialized back to JSON.
79+
80+
Example:
81+
82+
```csharp
83+
.AddTool<CreateNoteArgs>(
84+
"create_note",
85+
"Create a new note with a title and content",
86+
new
87+
{
88+
type = "object",
89+
properties = new
90+
{
91+
title = new { type = "string", description = "Title of the note" },
92+
content = new { type = "string", description = "Content of the note" }
93+
},
94+
required = new[] { "title", "content" }
95+
},
96+
NoteTools.CreateNoteAsync)
97+
```
98+
99+
---
100+
101+
#### **`AddTool<TArgs>(string name, string description, object parameters, Func<TArgs, object> execute)`**
102+
103+
Registers a **typed synchronous tool**, similar to the previous method but without asynchronous execution.
104+
105+
The input is deserialized into the specified argument type (`TArgs`), the tool executes synchronously, and the result is serialized to JSON.
106+
107+
---
108+
109+
#### **`AddTool(string name, string description, Func<Task<object>> execute)`**
110+
111+
Registers an **asynchronous parameterless tool**, ideal for actions that dont require inputs (e.g., fetching current time or status).
112+
113+
Example:
114+
115+
```csharp
116+
.AddTool("get_current_time", "Get the current date and time", Tools.GetCurrentTimeAsync)
117+
```
118+
119+
---
120+
121+
#### **`AddTool(string name, string description, Func<object> execute)`**
122+
123+
Registers a **synchronous parameterless tool**.
124+
This overload is ideal for lightweight, immediate-return tools.
125+
126+
---
127+
128+
#### **`WithToolChoice(string choice)`**
129+
130+
Specifies the **tool selection strategy** for the agent.
131+
Common values:
132+
133+
* `"auto"` → The model decides when to use tools automatically.
134+
* `"none"` → Disables tool invocation.
135+
* `"required"` → Forces tool usage if applicable.
136+
137+
---
138+
139+
#### **`Build()`**
140+
141+
Finalizes the configuration and returns a fully constructed **`ToolsConfiguration`** object.
142+
143+
---
144+
145+
### 🧠 Example Usage
146+
147+
```csharp
148+
var toolsConfig = new ToolsConfigurationBuilder()
149+
.AddTool<GetWeatherArgs>(
150+
name: "get_weather",
151+
description: "Get current weather conditions for a city",
152+
parameters: new
153+
{
154+
type = "object",
155+
properties = new
156+
{
157+
city = new { type = "string", description = "City name" }
158+
},
159+
required = new[] { "city" }
160+
},
161+
execute: WeatherTools.GetWeatherAsync)
162+
.AddTool(
163+
name: "get_current_time",
164+
description: "Return the current local time",
165+
execute: Tools.GetCurrentTime)
166+
.WithToolChoice("auto")
167+
.Build();
168+
```
169+
170+
This configuration can then be attached to a chat or agent:
171+
172+
```csharp
173+
await AIHub.Chat()
174+
.WithModel("gpt-5-nano")
175+
.WithTools(toolsConfig)
176+
.CompleteAsync(interactive: true);
177+
```
178+
179+
---
180+
181+
### 📘 Summary
182+
183+
| Category | Description |
184+
| ----------------- | -------------------------------------------------------------------------------------- |
185+
| **Purpose** | Defines callable tools the model can execute during interaction |
186+
| **Supports** | Synchronous & asynchronous tools, typed & untyped execution |
187+
| **Serialization** | Automatically handles JSON deserialization for input and serialization for output |
188+
| **Integration** | Used directly with agents or chat sessions via `.WithTools()` |
189+
| **Flexibility** | Allows parameterized tools, parameterless tools, and dynamic tool selection strategies |
190+
191+
---
192+
193+
The `ToolsConfigurationBuilder` provides a **clean, declarative, and extensible interface** for integrating external logic into AI-driven workflows, enabling agents to **go beyond text generation** and interact with real-world data and actions seamlessly.

0 commit comments

Comments
 (0)