-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentToolsVisibilityEditModeTests.cs
More file actions
189 lines (161 loc) · 6.87 KB
/
AgentToolsVisibilityEditModeTests.cs
File metadata and controls
189 lines (161 loc) · 6.87 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CoreAI.AgentMemory;
using CoreAI.Ai;
using CoreAI.Config;
using CoreAI.Infrastructure.Llm;
using CoreAI.Infrastructure.Logging;
using NUnit.Framework;
using UnityEngine;
namespace CoreAI.Tests.EditMode
{
/// <summary>
/// EditMode coverage that verifies an agent exposes every registered tool.
/// </summary>
public sealed class AgentToolsVisibilityEditModeTests
{
[Test]
public void AgentBuilder_WithMultipleTools_AllToolsVisible()
{
AgentConfig config = new AgentBuilder("TestAgent")
.WithSystemPrompt("Test")
.WithTool(new MemoryLlmTool())
.WithTool(new LuaLlmTool(new TestLuaExecutor(), ScriptableObject.CreateInstance<CoreAISettingsAsset>(),
Logging.NullLog.Instance))
.Build();
Assert.AreEqual(2, config.Tools.Count);
Assert.AreEqual("memory", config.Tools[0].Name);
Assert.AreEqual("execute_lua", config.Tools[1].Name);
}
[Test]
public void AgentMemoryPolicy_SetToolsForRole_ToolsAreRetrievable()
{
AgentMemoryPolicy policy = new();
List<ILlmTool> tools = new() { new MemoryLlmTool(), new InventoryLlmTool(new TestInventoryProvider()) };
policy.SetToolsForRole("TestRole", tools);
IReadOnlyList<ILlmTool> retrieved = policy.GetToolsForRole("TestRole");
// Кастомный список уже содержит memory — синглтон из политики не дублируется.
Assert.AreEqual(2, retrieved.Count);
Assert.AreEqual("memory", retrieved[0].Name);
}
[Test]
public void AgentMemoryPolicy_GetToolsForRole_DedupesMemory_AfterAgentBuilderApplyToPolicy()
{
AgentMemoryPolicy policy = new();
new AgentBuilder(BuiltInAgentRoleIds.Creator)
.WithMode(AgentMode.ToolsAndChat)
.WithMemory(MemoryToolAction.Append)
.Build()
.ApplyToPolicy(policy);
IReadOnlyList<ILlmTool> tools = policy.GetToolsForRole(BuiltInAgentRoleIds.Creator);
int memoryCount = 0;
foreach (ILlmTool t in tools)
{
if (string.Equals(t.Name, "memory", StringComparison.OrdinalIgnoreCase))
{
memoryCount++;
}
}
Assert.AreEqual(1, memoryCount);
}
[Test]
public void MeaiLlmClient_BuildAIFunctions_CreatesAllFunctions()
{
#if COREAI_NO_LLM
Assert.Ignore("COREAI_NO_LLM: MeaiLlmClient (HTTP/MEAI pipeline) is excluded from build.");
#else
CoreAISettingsAsset settings = ScriptableObject.CreateInstance<CoreAISettingsAsset>();
settings.ConfigureHttpApi("http://localhost:1234/v1", "", "test");
IGameLogger logger = GameLoggerUnscopedFallback.Instance;
TestMemoryStore memoryStore = new();
MeaiLlmClient client = MeaiLlmClient.CreateHttp(settings, logger, memoryStore);
// Проверяем что BuildAIFunctions создаёт функции для каждого инструмента
List<ILlmTool> tools = new()
{
new MemoryLlmTool(),
new LuaLlmTool(new TestLuaExecutor(), ScriptableObject.CreateInstance<CoreAISettingsAsset>(),
Logging.NullLog.Instance)
};
// Вызываем CompleteAsync чтобы проверить что инструменты обрабатываются
// (в EditMode без реальной LLM — проверяем что код не падает)
Task<LlmCompletionResult> task = client.CompleteAsync(new LlmCompletionRequest
{
AgentRoleId = "TestRole",
SystemPrompt = "test",
UserPayload = "test",
Tools = tools
});
// В EditMode без LLM — ожидаем ошибку подключения, но не ArgumentNullException
Assert.IsTrue(task.IsCompleted || task.Status == TaskStatus.WaitingForActivation);
UnityEngine.Object.DestroyImmediate(settings);
#endif
}
[Test]
public void AgentBuilder_WithMemory_AddsMemoryTool()
{
AgentConfig config = new AgentBuilder("TestAgent")
.WithSystemPrompt("Test")
.WithMemory()
.Build();
Assert.AreEqual(1, config.Tools.Count);
Assert.AreEqual("memory", config.Tools[0].Name);
}
[Test]
public void AgentBuilder_WithToolsAndMemory_AllToolsVisible()
{
AgentConfig config = new AgentBuilder("TestAgent")
.WithSystemPrompt("Test")
.WithTool(new InventoryLlmTool(new TestInventoryProvider()))
.WithMemory()
.Build();
Assert.AreEqual(2, config.Tools.Count);
Assert.AreEqual("get_inventory", config.Tools[0].Name);
Assert.AreEqual("memory", config.Tools[1].Name);
}
#region Test Helpers
private sealed class TestLuaExecutor : LuaTool.ILuaExecutor
{
public Task<LuaTool.LuaResult> ExecuteAsync(string code,
System.Threading.CancellationToken ct)
{
return Task.FromResult(new LuaTool.LuaResult { Success = true, Output = "" });
}
}
private sealed class TestInventoryProvider : InventoryTool.IInventoryProvider
{
public Task<List<InventoryTool.InventoryItem>> GetInventoryAsync(
System.Threading.CancellationToken ct)
{
return Task.FromResult(new List<InventoryTool.InventoryItem>());
}
}
private sealed class TestMemoryStore : IAgentMemoryStore
{
public readonly Dictionary<string, AgentMemoryState> States = new();
public bool TryLoad(string roleId, out AgentMemoryState state)
{
return States.TryGetValue(roleId, out state);
}
public void Save(string roleId, AgentMemoryState state)
{
States[roleId] = state;
}
public void Clear(string roleId)
{
States.Remove(roleId);
}
public void ClearChatHistory(string roleId)
{
}
public void AppendChatMessage(string roleId, string role, string content, bool persistToDisk = true)
{
}
public ChatMessage[] GetChatHistory(string roleId, int maxMessages = 0)
{
return Array.Empty<ChatMessage>();
}
}
#endregion
}
}