@multiplayer-app/ai-agent-node 0.0.1
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/.env.example +45 -0
- package/README.md +611 -0
- package/config.example.json +73 -0
- package/dist/config.d.ts +35 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +44 -0
- package/dist/config.js.map +1 -0
- package/dist/helpers/AIHelper.d.ts +23 -0
- package/dist/helpers/AIHelper.d.ts.map +1 -0
- package/dist/helpers/AIHelper.js +326 -0
- package/dist/helpers/AIHelper.js.map +1 -0
- package/dist/helpers/AIHelper.test.d.ts +2 -0
- package/dist/helpers/AIHelper.test.d.ts.map +1 -0
- package/dist/helpers/AIHelper.test.js +332 -0
- package/dist/helpers/AIHelper.test.js.map +1 -0
- package/dist/helpers/ConfigHelper.d.ts +20 -0
- package/dist/helpers/ConfigHelper.d.ts.map +1 -0
- package/dist/helpers/ConfigHelper.js +118 -0
- package/dist/helpers/ConfigHelper.js.map +1 -0
- package/dist/helpers/ContextLimiter.d.ts +82 -0
- package/dist/helpers/ContextLimiter.d.ts.map +1 -0
- package/dist/helpers/ContextLimiter.js +165 -0
- package/dist/helpers/ContextLimiter.js.map +1 -0
- package/dist/helpers/FileHelper.d.ts +31 -0
- package/dist/helpers/FileHelper.d.ts.map +1 -0
- package/dist/helpers/FileHelper.js +175 -0
- package/dist/helpers/FileHelper.js.map +1 -0
- package/dist/helpers/SetupHelper.d.ts +5 -0
- package/dist/helpers/SetupHelper.d.ts.map +1 -0
- package/dist/helpers/SetupHelper.js +32 -0
- package/dist/helpers/SetupHelper.js.map +1 -0
- package/dist/helpers/index.d.ts +6 -0
- package/dist/helpers/index.d.ts.map +1 -0
- package/dist/helpers/index.js +6 -0
- package/dist/helpers/index.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/libs/index.d.ts +4 -0
- package/dist/libs/index.d.ts.map +1 -0
- package/dist/libs/index.js +4 -0
- package/dist/libs/index.js.map +1 -0
- package/dist/libs/kafka/config.d.ts +5 -0
- package/dist/libs/kafka/config.d.ts.map +1 -0
- package/dist/libs/kafka/config.js +5 -0
- package/dist/libs/kafka/config.js.map +1 -0
- package/dist/libs/kafka/consumer.d.ts +16 -0
- package/dist/libs/kafka/consumer.d.ts.map +1 -0
- package/dist/libs/kafka/consumer.js +126 -0
- package/dist/libs/kafka/consumer.js.map +1 -0
- package/dist/libs/kafka/index.d.ts +3 -0
- package/dist/libs/kafka/index.d.ts.map +1 -0
- package/dist/libs/kafka/index.js +3 -0
- package/dist/libs/kafka/index.js.map +1 -0
- package/dist/libs/kafka/kafka.d.ts +3 -0
- package/dist/libs/kafka/kafka.d.ts.map +1 -0
- package/dist/libs/kafka/kafka.js +24 -0
- package/dist/libs/kafka/kafka.js.map +1 -0
- package/dist/libs/kafka/producer.d.ts +11 -0
- package/dist/libs/kafka/producer.d.ts.map +1 -0
- package/dist/libs/kafka/producer.js +44 -0
- package/dist/libs/kafka/producer.js.map +1 -0
- package/dist/libs/logger/config.d.ts +5 -0
- package/dist/libs/logger/config.d.ts.map +1 -0
- package/dist/libs/logger/config.js +6 -0
- package/dist/libs/logger/config.js.map +1 -0
- package/dist/libs/logger/index.d.ts +10 -0
- package/dist/libs/logger/index.d.ts.map +1 -0
- package/dist/libs/logger/index.js +20 -0
- package/dist/libs/logger/index.js.map +1 -0
- package/dist/libs/logger/kafkajs-logger-creator.d.ts +12 -0
- package/dist/libs/logger/kafkajs-logger-creator.d.ts.map +1 -0
- package/dist/libs/logger/kafkajs-logger-creator.js +29 -0
- package/dist/libs/logger/kafkajs-logger-creator.js.map +1 -0
- package/dist/libs/logger/logger.d.ts +42 -0
- package/dist/libs/logger/logger.d.ts.map +1 -0
- package/dist/libs/logger/logger.js +44 -0
- package/dist/libs/logger/logger.js.map +1 -0
- package/dist/libs/s3/config.d.ts +7 -0
- package/dist/libs/s3/config.d.ts.map +1 -0
- package/dist/libs/s3/config.js +7 -0
- package/dist/libs/s3/config.js.map +1 -0
- package/dist/libs/s3/index.d.ts +4 -0
- package/dist/libs/s3/index.d.ts.map +1 -0
- package/dist/libs/s3/index.js +4 -0
- package/dist/libs/s3/index.js.map +1 -0
- package/dist/libs/s3/s3.lib.d.ts +25 -0
- package/dist/libs/s3/s3.lib.d.ts.map +1 -0
- package/dist/libs/s3/s3.lib.js +202 -0
- package/dist/libs/s3/s3.lib.js.map +1 -0
- package/dist/processors/ChatProcessor.d.ts +66 -0
- package/dist/processors/ChatProcessor.d.ts.map +1 -0
- package/dist/processors/ChatProcessor.js +610 -0
- package/dist/processors/ChatProcessor.js.map +1 -0
- package/dist/processors/ModelsProcessor.d.ts +11 -0
- package/dist/processors/ModelsProcessor.d.ts.map +1 -0
- package/dist/processors/ModelsProcessor.js +30 -0
- package/dist/processors/ModelsProcessor.js.map +1 -0
- package/dist/processors/index.d.ts +3 -0
- package/dist/processors/index.d.ts.map +1 -0
- package/dist/processors/index.js +3 -0
- package/dist/processors/index.js.map +1 -0
- package/dist/services/AIService.d.ts +48 -0
- package/dist/services/AIService.d.ts.map +1 -0
- package/dist/services/AIService.js +196 -0
- package/dist/services/AIService.js.map +1 -0
- package/dist/services/InternalEventsHandler.d.ts +21 -0
- package/dist/services/InternalEventsHandler.d.ts.map +1 -0
- package/dist/services/InternalEventsHandler.js +56 -0
- package/dist/services/InternalEventsHandler.js.map +1 -0
- package/dist/services/KafkaService.d.ts +35 -0
- package/dist/services/KafkaService.d.ts.map +1 -0
- package/dist/services/KafkaService.js +120 -0
- package/dist/services/KafkaService.js.map +1 -0
- package/dist/services/ModelFetcher.d.ts +54 -0
- package/dist/services/ModelFetcher.d.ts.map +1 -0
- package/dist/services/ModelFetcher.js +247 -0
- package/dist/services/ModelFetcher.js.map +1 -0
- package/dist/services/RedisService.d.ts +90 -0
- package/dist/services/RedisService.d.ts.map +1 -0
- package/dist/services/RedisService.js +236 -0
- package/dist/services/RedisService.js.map +1 -0
- package/dist/services/SocketService.d.ts +39 -0
- package/dist/services/SocketService.d.ts.map +1 -0
- package/dist/services/SocketService.js +128 -0
- package/dist/services/SocketService.js.map +1 -0
- package/dist/services/index.d.ts +7 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +7 -0
- package/dist/services/index.js.map +1 -0
- package/dist/store/AgentStore.d.ts +48 -0
- package/dist/store/AgentStore.d.ts.map +1 -0
- package/dist/store/AgentStore.js +98 -0
- package/dist/store/AgentStore.js.map +1 -0
- package/dist/store/ArtifactStore.d.ts +13 -0
- package/dist/store/ArtifactStore.d.ts.map +1 -0
- package/dist/store/ArtifactStore.js +27 -0
- package/dist/store/ArtifactStore.js.map +1 -0
- package/dist/store/ConfigStore.d.ts +89 -0
- package/dist/store/ConfigStore.d.ts.map +1 -0
- package/dist/store/ConfigStore.js +214 -0
- package/dist/store/ConfigStore.js.map +1 -0
- package/dist/store/ConfigStore.test.d.ts +2 -0
- package/dist/store/ConfigStore.test.d.ts.map +1 -0
- package/dist/store/ConfigStore.test.js +259 -0
- package/dist/store/ConfigStore.test.js.map +1 -0
- package/dist/store/ModelStore.d.ts +44 -0
- package/dist/store/ModelStore.d.ts.map +1 -0
- package/dist/store/ModelStore.js +81 -0
- package/dist/store/ModelStore.js.map +1 -0
- package/dist/store/ModelStore.test.d.ts +2 -0
- package/dist/store/ModelStore.test.d.ts.map +1 -0
- package/dist/store/ModelStore.test.js +390 -0
- package/dist/store/ModelStore.test.js.map +1 -0
- package/dist/store/index.d.ts +5 -0
- package/dist/store/index.d.ts.map +1 -0
- package/dist/store/index.js +5 -0
- package/dist/store/index.js.map +1 -0
- package/dist/tools/generateChartTool.d.ts +24 -0
- package/dist/tools/generateChartTool.d.ts.map +1 -0
- package/dist/tools/generateChartTool.js +124 -0
- package/dist/tools/generateChartTool.js.map +1 -0
- package/dist/tools/proposeFormValuesTool.d.ts +35 -0
- package/dist/tools/proposeFormValuesTool.d.ts.map +1 -0
- package/dist/tools/proposeFormValuesTool.js +56 -0
- package/dist/tools/proposeFormValuesTool.js.map +1 -0
- package/package.json +71 -0
- package/src/config.ts +46 -0
- package/src/helpers/AIHelper.test.ts +375 -0
- package/src/helpers/AIHelper.ts +353 -0
- package/src/helpers/ConfigHelper.ts +130 -0
- package/src/helpers/ContextLimiter.ts +228 -0
- package/src/helpers/FileHelper.ts +197 -0
- package/src/helpers/SetupHelper.ts +35 -0
- package/src/helpers/index.ts +5 -0
- package/src/index.ts +18 -0
- package/src/libs/index.ts +3 -0
- package/src/libs/kafka/config.ts +4 -0
- package/src/libs/kafka/consumer.ts +161 -0
- package/src/libs/kafka/index.ts +2 -0
- package/src/libs/kafka/kafka.ts +27 -0
- package/src/libs/kafka/producer.ts +48 -0
- package/src/libs/logger/config.ts +4 -0
- package/src/libs/logger/index.ts +21 -0
- package/src/libs/logger/kafkajs-logger-creator.ts +28 -0
- package/src/libs/logger/logger.ts +60 -0
- package/src/libs/s3/config.ts +7 -0
- package/src/libs/s3/index.ts +3 -0
- package/src/libs/s3/s3.lib.ts +284 -0
- package/src/processors/ChatProcessor.ts +713 -0
- package/src/processors/ModelsProcessor.ts +34 -0
- package/src/processors/index.ts +2 -0
- package/src/services/AIService.ts +241 -0
- package/src/services/InternalEventsHandler.ts +61 -0
- package/src/services/KafkaService.ts +142 -0
- package/src/services/ModelFetcher.ts +286 -0
- package/src/services/RedisService.ts +285 -0
- package/src/services/SocketService.ts +153 -0
- package/src/services/index.ts +6 -0
- package/src/store/AgentStore.ts +138 -0
- package/src/store/ArtifactStore.ts +29 -0
- package/src/store/ConfigStore.test.ts +314 -0
- package/src/store/ConfigStore.ts +239 -0
- package/src/store/ModelStore.test.ts +473 -0
- package/src/store/ModelStore.ts +93 -0
- package/src/store/index.ts +4 -0
- package/src/tools/generateChartTool.ts +131 -0
- package/src/tools/proposeFormValuesTool.ts +67 -0
- package/tsconfig.json +24 -0
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll } from 'vitest';
|
|
2
|
+
import { AIHelper } from './AIHelper';
|
|
3
|
+
import { ModelStore } from '../store/ModelStore';
|
|
4
|
+
import { MessageRole, AgentToolCallStatus } from '@multiplayer-app/ai-agent-types';
|
|
5
|
+
describe('AIHelper.getAssistantResponse', () => {
|
|
6
|
+
let modelStore;
|
|
7
|
+
let configStore;
|
|
8
|
+
beforeAll(() => {
|
|
9
|
+
// Setup real ModelStore instance
|
|
10
|
+
modelStore = ModelStore.getInstance();
|
|
11
|
+
modelStore.setModels([
|
|
12
|
+
{ id: 'openai/gpt-4o', label: 'GPT-4o Mini', provider: 'openrouter' }
|
|
13
|
+
]);
|
|
14
|
+
});
|
|
15
|
+
describe('with tool approval responses', () => {
|
|
16
|
+
it('should handle approved tool with successful execution', async () => {
|
|
17
|
+
// Note: getAssistantResponse accepts Pick<AgentMessage, 'role' | 'content'>[]
|
|
18
|
+
// but convertMessages internally handles toolCalls, so we cast for testing
|
|
19
|
+
const messages = [
|
|
20
|
+
{
|
|
21
|
+
role: MessageRole.User,
|
|
22
|
+
content: 'Please execute the tool'
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
role: MessageRole.Assistant,
|
|
26
|
+
content: 'I will execute the tool for you.',
|
|
27
|
+
toolCalls: [
|
|
28
|
+
{
|
|
29
|
+
id: 'tool-call-1',
|
|
30
|
+
name: 'test-tool',
|
|
31
|
+
input: { param: 'value' },
|
|
32
|
+
status: AgentToolCallStatus.Succeeded,
|
|
33
|
+
approved: true,
|
|
34
|
+
output: { result: 'success', data: { executed: true } }
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
];
|
|
39
|
+
const response = await AIHelper.getAssistantResponse(messages);
|
|
40
|
+
// Verify we get a response (actual model call)
|
|
41
|
+
expect(response).toBeDefined();
|
|
42
|
+
expect(typeof response).toBe('string');
|
|
43
|
+
expect(response.length).toBeGreaterThan(0);
|
|
44
|
+
// The response should acknowledge the tool execution
|
|
45
|
+
// (exact text depends on model, so we just verify it's a valid response)
|
|
46
|
+
}, 30000);
|
|
47
|
+
it('should handle denied tool with execution-denied output', async () => {
|
|
48
|
+
const messages = [
|
|
49
|
+
{
|
|
50
|
+
role: MessageRole.User,
|
|
51
|
+
content: 'Please execute the tool'
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
role: MessageRole.Assistant,
|
|
55
|
+
content: 'I will execute the tool for you.',
|
|
56
|
+
toolCalls: [
|
|
57
|
+
{
|
|
58
|
+
id: 'tool-call-1',
|
|
59
|
+
name: 'test-tool',
|
|
60
|
+
input: { param: 'value' },
|
|
61
|
+
status: AgentToolCallStatus.Succeeded,
|
|
62
|
+
approved: false,
|
|
63
|
+
reason: 'User denied execution',
|
|
64
|
+
output: {
|
|
65
|
+
type: 'execution-denied',
|
|
66
|
+
reason: 'User denied execution'
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
]
|
|
70
|
+
}
|
|
71
|
+
];
|
|
72
|
+
const response = await AIHelper.getAssistantResponse(messages);
|
|
73
|
+
// Verify we get a response acknowledging the denial
|
|
74
|
+
expect(response).toBeDefined();
|
|
75
|
+
expect(typeof response).toBe('string');
|
|
76
|
+
expect(response.length).toBeGreaterThan(0);
|
|
77
|
+
// The model should respond to the execution-denied output
|
|
78
|
+
}, 30000);
|
|
79
|
+
it('should handle multiple tools with mixed approvals', async () => {
|
|
80
|
+
const messages = [
|
|
81
|
+
{
|
|
82
|
+
role: MessageRole.User,
|
|
83
|
+
content: 'Execute multiple tools'
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
role: MessageRole.Assistant,
|
|
87
|
+
content: 'I will execute the tools.',
|
|
88
|
+
toolCalls: [
|
|
89
|
+
{
|
|
90
|
+
id: 'tool-call-1',
|
|
91
|
+
name: 'approved-tool',
|
|
92
|
+
input: { param: 'value1' },
|
|
93
|
+
status: AgentToolCallStatus.Succeeded,
|
|
94
|
+
approved: true,
|
|
95
|
+
output: { result: 'approved-result' }
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
id: 'tool-call-2',
|
|
99
|
+
name: 'denied-tool',
|
|
100
|
+
input: { param: 'value2' },
|
|
101
|
+
status: AgentToolCallStatus.Succeeded,
|
|
102
|
+
approved: false,
|
|
103
|
+
reason: 'Not allowed',
|
|
104
|
+
output: {
|
|
105
|
+
type: 'execution-denied',
|
|
106
|
+
reason: 'Not allowed'
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
]
|
|
110
|
+
}
|
|
111
|
+
];
|
|
112
|
+
const response = await AIHelper.getAssistantResponse(messages);
|
|
113
|
+
// Verify we get a response handling both tool results
|
|
114
|
+
expect(response).toBeDefined();
|
|
115
|
+
expect(typeof response).toBe('string');
|
|
116
|
+
expect(response.length).toBeGreaterThan(0);
|
|
117
|
+
// The model should process both approved and denied tool results
|
|
118
|
+
}, 30000);
|
|
119
|
+
it('should handle tool execution failure', async () => {
|
|
120
|
+
const messages = [
|
|
121
|
+
{
|
|
122
|
+
role: MessageRole.User,
|
|
123
|
+
content: 'Execute the tool'
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
role: MessageRole.Assistant,
|
|
127
|
+
content: 'I will execute the tool.',
|
|
128
|
+
toolCalls: [
|
|
129
|
+
{
|
|
130
|
+
id: 'tool-call-1',
|
|
131
|
+
name: 'failing-tool',
|
|
132
|
+
input: { param: 'value' },
|
|
133
|
+
status: AgentToolCallStatus.Failed,
|
|
134
|
+
approved: true,
|
|
135
|
+
error: 'Tool execution failed: Network error'
|
|
136
|
+
}
|
|
137
|
+
]
|
|
138
|
+
}
|
|
139
|
+
];
|
|
140
|
+
const response = await AIHelper.getAssistantResponse(messages);
|
|
141
|
+
// Verify we get a response even when tool execution failed
|
|
142
|
+
expect(response).toBeDefined();
|
|
143
|
+
expect(typeof response).toBe('string');
|
|
144
|
+
expect(response.length).toBeGreaterThan(0);
|
|
145
|
+
}, 30000);
|
|
146
|
+
it('should handle tool with execution-denied due to tool not found', async () => {
|
|
147
|
+
const messages = [
|
|
148
|
+
{
|
|
149
|
+
role: MessageRole.User,
|
|
150
|
+
content: 'Execute the tool'
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
role: MessageRole.Assistant,
|
|
154
|
+
content: 'I will execute the tool.',
|
|
155
|
+
toolCalls: [
|
|
156
|
+
{
|
|
157
|
+
id: 'tool-call-1',
|
|
158
|
+
name: 'missing-tool',
|
|
159
|
+
input: { param: 'value' },
|
|
160
|
+
status: AgentToolCallStatus.Succeeded,
|
|
161
|
+
approved: true,
|
|
162
|
+
output: {
|
|
163
|
+
type: 'execution-denied',
|
|
164
|
+
reason: 'Tool was not found'
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
]
|
|
168
|
+
}
|
|
169
|
+
];
|
|
170
|
+
const response = await AIHelper.getAssistantResponse(messages);
|
|
171
|
+
// Verify we get a response when tool was not found
|
|
172
|
+
expect(response).toBeDefined();
|
|
173
|
+
expect(typeof response).toBe('string');
|
|
174
|
+
expect(response.length).toBeGreaterThan(0);
|
|
175
|
+
}, 30000);
|
|
176
|
+
it('should handle tool with execution-denied due to tool execution error', async () => {
|
|
177
|
+
const messages = [
|
|
178
|
+
{
|
|
179
|
+
role: MessageRole.User,
|
|
180
|
+
content: 'Execute the tool'
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
role: MessageRole.Assistant,
|
|
184
|
+
content: 'I will execute the tool.',
|
|
185
|
+
toolCalls: [
|
|
186
|
+
{
|
|
187
|
+
id: 'tool-call-1',
|
|
188
|
+
name: 'error-tool',
|
|
189
|
+
input: { param: 'value' },
|
|
190
|
+
status: AgentToolCallStatus.Succeeded,
|
|
191
|
+
approved: true,
|
|
192
|
+
output: {
|
|
193
|
+
type: 'execution-denied',
|
|
194
|
+
reason: 'Tool failed with error'
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
]
|
|
198
|
+
}
|
|
199
|
+
];
|
|
200
|
+
const response = await AIHelper.getAssistantResponse(messages);
|
|
201
|
+
// Verify we get a response when tool execution had an error
|
|
202
|
+
expect(response).toBeDefined();
|
|
203
|
+
expect(typeof response).toBe('string');
|
|
204
|
+
expect(response.length).toBeGreaterThan(0);
|
|
205
|
+
}, 30000);
|
|
206
|
+
it('should handle messages without tool calls', async () => {
|
|
207
|
+
const messages = [
|
|
208
|
+
{
|
|
209
|
+
role: MessageRole.User,
|
|
210
|
+
content: 'Hello, how are you?'
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
role: MessageRole.Assistant,
|
|
214
|
+
content: 'I am doing well, thank you!'
|
|
215
|
+
}
|
|
216
|
+
];
|
|
217
|
+
const response = await AIHelper.getAssistantResponse(messages);
|
|
218
|
+
// Verify we get a response for normal conversation
|
|
219
|
+
expect(response).toBeDefined();
|
|
220
|
+
expect(typeof response).toBe('string');
|
|
221
|
+
expect(response.length).toBeGreaterThan(0);
|
|
222
|
+
}, 30000);
|
|
223
|
+
it('should handle tool calls without output (pending approval)', async () => {
|
|
224
|
+
const messages = [
|
|
225
|
+
{
|
|
226
|
+
role: MessageRole.User,
|
|
227
|
+
content: 'Execute the tool'
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
role: MessageRole.Assistant,
|
|
231
|
+
content: 'I need to execute a tool.',
|
|
232
|
+
toolCalls: [
|
|
233
|
+
{
|
|
234
|
+
id: 'tool-call-1',
|
|
235
|
+
name: 'pending-tool',
|
|
236
|
+
input: { param: 'value' },
|
|
237
|
+
status: AgentToolCallStatus.Pending,
|
|
238
|
+
requiresConfirmation: true,
|
|
239
|
+
approvalId: 'approval-123'
|
|
240
|
+
}
|
|
241
|
+
]
|
|
242
|
+
}
|
|
243
|
+
];
|
|
244
|
+
const response = await AIHelper.getAssistantResponse(messages);
|
|
245
|
+
// Verify we get a response even when tool is pending approval
|
|
246
|
+
// (tool calls without output should not include tool-result in messages)
|
|
247
|
+
expect(response).toBeDefined();
|
|
248
|
+
expect(typeof response).toBe('string');
|
|
249
|
+
expect(response.length).toBeGreaterThan(0);
|
|
250
|
+
}, 30000);
|
|
251
|
+
it('should handle sequential tool approvals', async () => {
|
|
252
|
+
const messages = [
|
|
253
|
+
{
|
|
254
|
+
role: MessageRole.User,
|
|
255
|
+
content: 'First request'
|
|
256
|
+
},
|
|
257
|
+
{
|
|
258
|
+
role: MessageRole.Assistant,
|
|
259
|
+
content: 'Executing first tool.',
|
|
260
|
+
toolCalls: [
|
|
261
|
+
{
|
|
262
|
+
id: 'tool-call-1',
|
|
263
|
+
name: 'first-tool',
|
|
264
|
+
input: { step: 1 },
|
|
265
|
+
status: AgentToolCallStatus.Succeeded,
|
|
266
|
+
approved: true,
|
|
267
|
+
output: { result: 'first-result' }
|
|
268
|
+
}
|
|
269
|
+
]
|
|
270
|
+
},
|
|
271
|
+
{
|
|
272
|
+
role: MessageRole.Assistant,
|
|
273
|
+
content: 'Now executing second tool.',
|
|
274
|
+
toolCalls: [
|
|
275
|
+
{
|
|
276
|
+
id: 'tool-call-2',
|
|
277
|
+
name: 'second-tool',
|
|
278
|
+
input: { step: 2 },
|
|
279
|
+
status: AgentToolCallStatus.Succeeded,
|
|
280
|
+
approved: false,
|
|
281
|
+
reason: 'User denied',
|
|
282
|
+
output: {
|
|
283
|
+
type: 'execution-denied',
|
|
284
|
+
reason: 'User denied'
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
]
|
|
288
|
+
}
|
|
289
|
+
];
|
|
290
|
+
const response = await AIHelper.getAssistantResponse(messages);
|
|
291
|
+
// Verify we get a response for sequential tool calls
|
|
292
|
+
expect(response).toBeDefined();
|
|
293
|
+
expect(typeof response).toBe('string');
|
|
294
|
+
expect(response.length).toBeGreaterThan(0);
|
|
295
|
+
// The model should process both sequential tool results
|
|
296
|
+
}, 30000);
|
|
297
|
+
it('should pass through options correctly', async () => {
|
|
298
|
+
const messages = [
|
|
299
|
+
{
|
|
300
|
+
role: MessageRole.User,
|
|
301
|
+
content: 'Test message'
|
|
302
|
+
}
|
|
303
|
+
];
|
|
304
|
+
const response = await AIHelper.getAssistantResponse(messages, undefined, {
|
|
305
|
+
temperature: 0.9,
|
|
306
|
+
maxOutputTokens: 1000
|
|
307
|
+
});
|
|
308
|
+
// Verify we get a response with custom options applied
|
|
309
|
+
expect(response).toBeDefined();
|
|
310
|
+
expect(typeof response).toBe('string');
|
|
311
|
+
expect(response.length).toBeGreaterThan(0);
|
|
312
|
+
// Options are passed through to the model
|
|
313
|
+
}, 30000);
|
|
314
|
+
it('should handle abort signal', async () => {
|
|
315
|
+
const messages = [
|
|
316
|
+
{
|
|
317
|
+
role: MessageRole.User,
|
|
318
|
+
content: 'Test message'
|
|
319
|
+
}
|
|
320
|
+
];
|
|
321
|
+
const abortController = new AbortController();
|
|
322
|
+
const signal = abortController.signal;
|
|
323
|
+
const response = await AIHelper.getAssistantResponse(messages, signal);
|
|
324
|
+
// Verify we get a response with abort signal passed through
|
|
325
|
+
expect(response).toBeDefined();
|
|
326
|
+
expect(typeof response).toBe('string');
|
|
327
|
+
expect(response.length).toBeGreaterThan(0);
|
|
328
|
+
// Signal is passed through to the model for cancellation support
|
|
329
|
+
}, 30000);
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
//# sourceMappingURL=AIHelper.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AIHelper.test.js","sourceRoot":"","sources":["../../src/helpers/AIHelper.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACzD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEjD,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AAGnF,QAAQ,CAAC,+BAA+B,EAAE,GAAG,EAAE;IAC7C,IAAI,UAAsB,CAAC;IAC3B,IAAI,WAAwB,CAAC;IAE7B,S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|
+
export declare class ConfigHelper {
|
|
3
|
+
static loadConfig(rawConfig: Record<string, any>): AgentAppConfig;
|
|
4
|
+
private static jsonSchemaToZod;
|
|
5
|
+
/**
|
|
6
|
+
* Converts raw JSON config to AgentAppConfig format
|
|
7
|
+
* Validates structure and converts JSON schemas to Zod schemas
|
|
8
|
+
*/
|
|
9
|
+
private static convertToAgentAppConfig;
|
|
10
|
+
/**
|
|
11
|
+
* Converts a tool from JSON format (with JSON schemas) to AgentTool format (with Zod schemas)
|
|
12
|
+
*/
|
|
13
|
+
private static convertTool;
|
|
14
|
+
/**
|
|
15
|
+
* Validates the AgentAppConfig structure
|
|
16
|
+
* @throws Error if validation fails
|
|
17
|
+
*/
|
|
18
|
+
private static validateConfig;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=ConfigHelper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConfigHelper.d.ts","sourceRoot":"","sources":["../../src/helpers/ConfigHelper.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAa,MAAM,iCAAiC,CAAC;AAIjF,qBAAa,YAAY;IAEvB,MAAM,CAAC,UAAU,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,cAAc;IAcjE,OAAO,CAAC,MAAM,CAAC,eAAe;IAK9B;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,uBAAuB;IAwBtC;;OAEG;IACH,OAAO,CAAC,MAAM,CAAC,WAAW;IA8B1B;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,cAAc;CAsC9B"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { AgentToolType } from '@multiplayer-app/ai-agent-types';
|
|
3
|
+
import { logger } from '../libs/logger';
|
|
4
|
+
export class ConfigHelper {
|
|
5
|
+
static loadConfig(rawConfig) {
|
|
6
|
+
try {
|
|
7
|
+
// Convert to AgentAppConfig format
|
|
8
|
+
return ConfigHelper.convertToAgentAppConfig(rawConfig);
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
if (error instanceof SyntaxError) {
|
|
12
|
+
logger.error(`Failed to parse config.json: ${error.message}`);
|
|
13
|
+
throw new Error(`Invalid JSON in config file: ${error.message}`);
|
|
14
|
+
}
|
|
15
|
+
logger.error(`Failed to load config: ${error}`);
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
static jsonSchemaToZod(schema) {
|
|
20
|
+
if (!schema)
|
|
21
|
+
return null;
|
|
22
|
+
return z.fromJSONSchema(schema);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Converts raw JSON config to AgentAppConfig format
|
|
26
|
+
* Validates structure and converts JSON schemas to Zod schemas
|
|
27
|
+
*/
|
|
28
|
+
static convertToAgentAppConfig(rawConfig) {
|
|
29
|
+
// Convert agents, transforming JSON schemas to Zod schemas
|
|
30
|
+
const config = rawConfig.agents.reduce((config, agent) => {
|
|
31
|
+
agent.contextKeys.forEach((contextKey) => {
|
|
32
|
+
if (!config[contextKey]) {
|
|
33
|
+
config[contextKey] = [];
|
|
34
|
+
}
|
|
35
|
+
config[contextKey].push({
|
|
36
|
+
name: agent.name,
|
|
37
|
+
description: agent.description,
|
|
38
|
+
defaultModel: agent.defaultModel,
|
|
39
|
+
systemPrompt: agent.systemPrompt,
|
|
40
|
+
temperature: agent.temperature || 0.1,
|
|
41
|
+
maxTokens: agent.maxTokens || 2000,
|
|
42
|
+
tools: agent.tools.map((tool) => ConfigHelper.convertTool(tool)),
|
|
43
|
+
outputSchema: agent.outputSchema ? ConfigHelper.jsonSchemaToZod(agent.outputSchema) : undefined
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
return config;
|
|
47
|
+
}, {});
|
|
48
|
+
return config;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Converts a tool from JSON format (with JSON schemas) to AgentTool format (with Zod schemas)
|
|
52
|
+
*/
|
|
53
|
+
static convertTool(tool) {
|
|
54
|
+
var _a;
|
|
55
|
+
if (tool.type === AgentToolType.WEB_SEARCH) {
|
|
56
|
+
return {
|
|
57
|
+
type: AgentToolType.WEB_SEARCH,
|
|
58
|
+
data: {
|
|
59
|
+
title: AgentToolType.WEB_SEARCH,
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (tool.type === AgentToolType.API_TOOL) {
|
|
64
|
+
return {
|
|
65
|
+
type: AgentToolType.API_TOOL,
|
|
66
|
+
data: {
|
|
67
|
+
title: tool.data.title,
|
|
68
|
+
description: tool.data.description,
|
|
69
|
+
headersToPass: tool.data.headersToPass,
|
|
70
|
+
method: tool.data.method,
|
|
71
|
+
url: tool.data.url,
|
|
72
|
+
body: tool.data.body ? ConfigHelper.jsonSchemaToZod(tool.data.body) : undefined,
|
|
73
|
+
queryParams: tool.data.queryParams ? ConfigHelper.jsonSchemaToZod(tool.data.queryParams) : undefined,
|
|
74
|
+
needsApproval: (_a = tool.data.needsApproval) !== null && _a !== void 0 ? _a : false,
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
throw new Error(`Unknown tool type: ${tool.type}`);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Validates the AgentAppConfig structure
|
|
82
|
+
* @throws Error if validation fails
|
|
83
|
+
*/
|
|
84
|
+
static validateConfig(config) {
|
|
85
|
+
if (!config.agents || !Array.isArray(config.agents)) {
|
|
86
|
+
throw new Error('Config must have an "agents" array');
|
|
87
|
+
}
|
|
88
|
+
if (!config.contexts || typeof config.contexts !== 'object') {
|
|
89
|
+
throw new Error('Config must have a "contexts" object');
|
|
90
|
+
}
|
|
91
|
+
// Validate agents
|
|
92
|
+
const agentNames = new Set();
|
|
93
|
+
for (const agent of config.agents) {
|
|
94
|
+
if (!agent.name) {
|
|
95
|
+
throw new Error('All agents must have a "name" field');
|
|
96
|
+
}
|
|
97
|
+
if (agentNames.has(agent.name)) {
|
|
98
|
+
throw new Error(`Duplicate agent name found: ${agent.name}`);
|
|
99
|
+
}
|
|
100
|
+
agentNames.add(agent.name);
|
|
101
|
+
if (!agent.systemPrompt) {
|
|
102
|
+
throw new Error(`Agent "${agent.name}" must have a "systemPrompt" field`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// Validate contexts reference valid agents
|
|
106
|
+
for (const [contextKey, contextAgentNames] of Object.entries(config.contexts)) {
|
|
107
|
+
if (!Array.isArray(contextAgentNames)) {
|
|
108
|
+
throw new Error(`Context "${contextKey}" must have an array of agent names`);
|
|
109
|
+
}
|
|
110
|
+
for (const agentName of contextAgentNames) {
|
|
111
|
+
if (!agentNames.has(agentName)) {
|
|
112
|
+
throw new Error(`Context "${contextKey}" references unknown agent: ${agentName}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=ConfigHelper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConfigHelper.js","sourceRoot":"","sources":["../../src/helpers/ConfigHelper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAExC,MAAM,OAAO,YAAY;IAEvB,MAAM,CAAC,UAAU,CAAC,SAA8B;QAC9C,IAAI,CAAC;YACH,mCAAmC;YACnC,OAAO,YAAY,CAAC,uBAAuB,CAAC,SAAS,CAAC,CAAC;QACzD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;gBACjC,MAAM,CAAC,KAAK,CAAC,gCAAgC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;gBAC9D,MAAM,IAAI,KAAK,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AK,CAAC,YAAY,UAAU,qCAAqC,CAAC,CAAC;YAC/E,CAAC;YAED,KAAK,MAAM,SAAS,IAAI,iBAAiB,EAAE,CAAC;gBAC1C,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;oBAC/B,MAAM,IAAI,KAAK,CAAC,YAAY,UAAU,+BAA+B,SAAS,EAAE,CAAC,CAAC;gBACpF,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { AgentMessage } from '@multiplayer-app/ai-agent-types';
|
|
2
|
+
/**
|
|
3
|
+
* Options for limiting chat context
|
|
4
|
+
*/
|
|
5
|
+
export interface ContextLimitOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Maximum number of messages to include in context.
|
|
8
|
+
* Default: 50 (common practice: 20-100 messages depending on use case)
|
|
9
|
+
*/
|
|
10
|
+
maxMessages?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Maximum token count (if token counting is implemented).
|
|
13
|
+
* This is a future enhancement - currently not used.
|
|
14
|
+
*/
|
|
15
|
+
maxTokens?: number;
|
|
16
|
+
/**
|
|
17
|
+
* Whether to always keep the first user message (often contains important context).
|
|
18
|
+
* Default: true
|
|
19
|
+
*/
|
|
20
|
+
keepFirstUserMessage?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Whether to keep all system messages (if any exist).
|
|
23
|
+
* Default: true
|
|
24
|
+
*/
|
|
25
|
+
keepSystemMessages?: boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Common practices for limiting AI context:
|
|
29
|
+
*
|
|
30
|
+
* 1. **Message Count Limiting** (implemented here)
|
|
31
|
+
* - Keep only the last N messages
|
|
32
|
+
* - Most common and simplest approach
|
|
33
|
+
* - Typical values: 20-100 messages depending on use case
|
|
34
|
+
*
|
|
35
|
+
* 2. **Token-Based Limiting** (future enhancement)
|
|
36
|
+
* - Count tokens and keep messages until hitting token budget
|
|
37
|
+
* - More accurate for cost control
|
|
38
|
+
* - Requires token counting library (e.g., tiktoken, @anthropic-ai/tokenizer)
|
|
39
|
+
*
|
|
40
|
+
* 3. **Hybrid Approach** (future enhancement)
|
|
41
|
+
* - Combine message count with token budget as safety check
|
|
42
|
+
* - Best of both worlds
|
|
43
|
+
*
|
|
44
|
+
* 4. **Sliding Window with Summary** (advanced)
|
|
45
|
+
* - Keep recent messages + summarize older ones
|
|
46
|
+
* - Requires summarization step
|
|
47
|
+
*
|
|
48
|
+
* 5. **Priority-Based** (implemented via keepFirstUserMessage)
|
|
49
|
+
* - Always keep system messages, first user message, and recent messages
|
|
50
|
+
* - Preserves important context while limiting size
|
|
51
|
+
*/
|
|
52
|
+
export declare class ContextLimiter {
|
|
53
|
+
/**
|
|
54
|
+
* Limits the context by keeping only the most recent messages
|
|
55
|
+
* while preserving important messages (first user message, system messages).
|
|
56
|
+
*
|
|
57
|
+
* @param messages - All messages from the chat (assumed to be in chronological order)
|
|
58
|
+
* @param options - Limiting options
|
|
59
|
+
* @returns Limited array of messages in original order
|
|
60
|
+
*/
|
|
61
|
+
static limitContext(messages: AgentMessage[], options?: ContextLimitOptions): AgentMessage[];
|
|
62
|
+
/**
|
|
63
|
+
* Estimates token count for a message (rough approximation).
|
|
64
|
+
* For accurate token counting, use a proper tokenizer library.
|
|
65
|
+
*
|
|
66
|
+
* This is a simple approximation: ~4 characters per token for English text.
|
|
67
|
+
*
|
|
68
|
+
* @param message - Message to estimate tokens for
|
|
69
|
+
* @returns Estimated token count
|
|
70
|
+
*/
|
|
71
|
+
static estimateTokens(message: AgentMessage): number;
|
|
72
|
+
/**
|
|
73
|
+
* Limits context by token count (future enhancement).
|
|
74
|
+
* Currently not implemented - requires proper tokenizer.
|
|
75
|
+
*
|
|
76
|
+
* @param messages - All messages from the chat
|
|
77
|
+
* @param maxTokens - Maximum token count
|
|
78
|
+
* @returns Limited array of messages
|
|
79
|
+
*/
|
|
80
|
+
static limitByTokens(messages: AgentMessage[], maxTokens: number): AgentMessage[];
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=ContextLimiter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ContextLimiter.d.ts","sourceRoot":"","sources":["../../src/helpers/ContextLimiter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,iCAAiC,CAAC;AAE5E;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAE/B;;;OAGG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,cAAc;IACzB;;;;;;;OAOG;IACH,MAAM,CAAC,YAAY,CACjB,QAAQ,EAAE,YAAY,EAAE,EACxB,OAAO,GAAE,mBAAwB,GAChC,YAAY,EAAE;IA0EjB;;;;;;;;OAQG;IACH,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,GAAG,MAAM;IAwBpD;;;;;;;OAOG;IACH,MAAM,CAAC,aAAa,CAClB,QAAQ,EAAE,YAAY,EAAE,EACxB,SAAS,EAAE,MAAM,GAChB,YAAY,EAAE;CAwClB"}
|