@ai.ntellect/core 0.1.81 → 0.1.84
Sign up to get free protection for your applications and to get access to all the features.
- package/README.FR.md +31 -60
- package/README.md +74 -96
- package/agent/handlers/ActionHandler.ts +1 -1
- package/agent/index.ts +85 -43
- package/dist/agent/index.d.ts +1 -1
- package/dist/agent/index.js +61 -33
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/llm/evaluator/context.js +3 -5
- package/dist/llm/evaluator/index.js +25 -29
- package/dist/llm/orchestrator/context.js +1 -1
- package/dist/llm/orchestrator/index.js +30 -9
- package/dist/llm/synthesizer/index.d.ts +1 -5
- package/dist/llm/synthesizer/index.js +1 -5
- package/dist/memory/cache.d.ts +5 -5
- package/dist/memory/cache.js +19 -49
- package/dist/memory/persistent.d.ts +1 -1
- package/dist/memory/persistent.js +5 -6
- package/dist/services/queue.js +21 -8
- package/dist/t.d.ts +46 -0
- package/dist/t.js +102 -0
- package/dist/test.d.ts +68 -0
- package/dist/test.js +167 -0
- package/dist/types.d.ts +27 -3
- package/dist/types.js +9 -1
- package/dist/utils/inject-actions.js +1 -1
- package/dist/utils/queue-item-transformer.d.ts +2 -2
- package/dist/utils/queue-item-transformer.js +5 -6
- package/dist/utils/sanitize-results.d.ts +17 -0
- package/dist/utils/sanitize-results.js +60 -0
- package/index.ts +0 -1
- package/llm/evaluator/context.ts +3 -5
- package/llm/evaluator/index.ts +25 -31
- package/llm/orchestrator/context.ts +1 -1
- package/llm/orchestrator/index.ts +42 -13
- package/llm/synthesizer/index.ts +2 -10
- package/memory/cache.ts +23 -61
- package/memory/persistent.ts +7 -6
- package/package.json +3 -1
- package/services/queue.ts +21 -12
- package/t.ts +133 -0
- package/types.ts +14 -3
- package/utils/inject-actions.ts +1 -1
- package/utils/queue-item-transformer.ts +25 -11
- package/utils/sanitize-results.ts +66 -0
package/README.FR.md
CHANGED
@@ -230,79 +230,50 @@ export const prepareTransaction = {
|
|
230
230
|
|
231
231
|
## 3. Exécution du workflow
|
232
232
|
|
233
|
-
|
234
|
-
|
235
|
-
### Exemple de création d'un workflow :
|
233
|
+
L'agent gère l'ensemble du processus de compréhension des requêtes utilisateur et de coordination des réponses. Voici un exemple d'utilisation de l'agent :
|
236
234
|
|
237
235
|
```typescript
|
238
|
-
const
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
const orchestrator = new Orchestrator(tools);
|
236
|
+
const memory = new PersistentMemory({
|
237
|
+
host: "http://localhost:7700",
|
238
|
+
apiKey: "VOTRE_CLE_API",
|
239
|
+
});
|
244
240
|
|
245
|
-
const
|
246
|
-
|
247
|
-
|
241
|
+
const orchestrator = new Orchestrator(
|
242
|
+
[
|
243
|
+
getChainsTVL,
|
244
|
+
getRssNews,
|
245
|
+
// autres outils...
|
246
|
+
],
|
247
|
+
memory
|
248
248
|
);
|
249
|
-
```
|
250
|
-
|
251
|
-
- **Orchestrator** : Gestion de l'ordre des actions.
|
252
|
-
- **MemoryCache** : Réutilisation des résultats précédents.
|
253
|
-
- **EventEmitter** : Suivi et notification de l'état du workflow.
|
254
|
-
|
255
|
-
### Processus du workflow :
|
256
|
-
|
257
|
-
1. Le prompt utilisateur est analysé.
|
258
|
-
2. L'orchestrateur décide des actions nécessaires et leur ordre.
|
259
|
-
3. Les actions sont exécutées.
|
260
|
-
4. Les résultats sont synthétisés et renvoyés à l'utilisateur.
|
261
249
|
|
262
|
-
|
263
|
-
|
264
|
-
|
250
|
+
const agent = new Agent({
|
251
|
+
user: { id: "user_id" },
|
252
|
+
orchestrator,
|
253
|
+
persistentMemory: memory,
|
254
|
+
stream: false,
|
255
|
+
maxEvaluatorIteration: 1,
|
256
|
+
});
|
265
257
|
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
const { messages, from } = request.body;
|
271
|
-
const latestMessage = messages[messages.length - 1];
|
272
|
-
|
273
|
-
const workflow = new Workflow(
|
274
|
-
{ id: from },
|
275
|
-
{ orchestrator, memoryCache, eventEmitter }
|
276
|
-
);
|
277
|
-
return workflow.start(latestMessage.content, messages);
|
258
|
+
// Traitement d'une requête utilisateur
|
259
|
+
const result = await agent.process(prompt, context, {
|
260
|
+
onMessage: (message) => {
|
261
|
+
console.log({ message });
|
278
262
|
},
|
279
263
|
});
|
280
264
|
```
|
281
265
|
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
return (
|
290
|
-
<div>
|
291
|
-
<div>{messages}</div>
|
292
|
-
<input
|
293
|
-
type="text"
|
294
|
-
value={input}
|
295
|
-
onChange={(e) => setInput(e.target.value)}
|
296
|
-
/>
|
297
|
-
<button onClick={handleSubmit}>Envoyer</button>
|
298
|
-
</div>
|
299
|
-
);
|
300
|
-
}
|
301
|
-
```
|
266
|
+
### Flux de traitement de l'agent :
|
267
|
+
|
268
|
+
1. L'utilisateur envoie un prompt
|
269
|
+
2. L'agent analyse le prompt et le contexte
|
270
|
+
3. L'orchestrateur exécute les outils/actions nécessaires
|
271
|
+
4. L'évaluateur évalue les résultats
|
272
|
+
5. L'agent génère la réponse finale
|
302
273
|
|
303
274
|
---
|
304
275
|
|
305
|
-
##
|
276
|
+
## 4. WIP (Work in Progress)
|
306
277
|
|
307
278
|
Voici les éléments actuellement en développement ou à améliorer :
|
308
279
|
|
package/README.md
CHANGED
@@ -1,50 +1,51 @@
|
|
1
1
|
# AI.ntellect Core Framework
|
2
2
|
|
3
|
-
## Table of
|
3
|
+
## Table of contents
|
4
4
|
|
5
|
-
1. [Main
|
5
|
+
1. [Main components](#main-components)
|
6
|
+
- [Agent](#agent)
|
6
7
|
- [Orchestrator](#orchestrator)
|
7
|
-
- [Queue Manager](#queue-manager)
|
8
8
|
- [Synthesizer](#synthesizer)
|
9
9
|
- [Evaluator](#evaluator)
|
10
10
|
- [Memory](#memory-architecture)
|
11
|
-
2. [Action
|
12
|
-
3. [
|
13
|
-
4. [
|
14
|
-
5. [WIP (Work in Progress)](#wip-work-in-progress)
|
11
|
+
2. [Action creation and management](#action-creation-and-management)
|
12
|
+
3. [Agent processing](#agent-processing)
|
13
|
+
4. [WIP (Work in Progress)](#wip-work-in-progress)
|
15
14
|
|
16
15
|
---
|
17
16
|
|
18
17
|
## 1. Main components
|
19
18
|
|
20
|
-
The system relies on several key components that ensure smooth and efficient
|
19
|
+
The system relies on several key components that ensure smooth and efficient processing of user requests through an AI agent architecture.
|
21
20
|
|
22
|
-
###
|
21
|
+
### Agent
|
23
22
|
|
24
|
-
The
|
23
|
+
The agent is the core component that processes user requests and manages the entire interaction flow. It coordinates with other components to understand user needs, execute appropriate actions, and generate relevant responses.
|
25
24
|
|
26
|
-
- **Main
|
27
|
-
- **
|
28
|
-
-
|
29
|
-
-
|
30
|
-
-
|
31
|
-
-
|
25
|
+
- **Main role**: Process user requests and coordinate system components
|
26
|
+
- **Key features**:
|
27
|
+
- Processes user prompts
|
28
|
+
- Manages conversation context
|
29
|
+
- Coordinates with orchestrator for action execution
|
30
|
+
- Handles response generation
|
31
|
+
- Maintains user state and memory
|
32
32
|
|
33
|
-
###
|
33
|
+
### Orchestrator
|
34
34
|
|
35
|
-
The
|
35
|
+
The orchestrator works under the agent's direction to manage the execution of actions. It analyzes requirements based on the agent's interpretation of user needs and coordinates the execution of appropriate tools.
|
36
36
|
|
37
|
-
- **Main
|
38
|
-
- **
|
39
|
-
-
|
40
|
-
-
|
41
|
-
-
|
37
|
+
- **Main role**: Organize and direct the execution of actions
|
38
|
+
- **Interactions**:
|
39
|
+
- Manages available tools/actions
|
40
|
+
- Executes actions based on agent requests
|
41
|
+
- Uses memory for context and caching
|
42
|
+
- Coordinates with evaluator for result assessment
|
42
43
|
|
43
44
|
### Synthesizer
|
44
45
|
|
45
46
|
The synthesizer is responsible for generating responses and analyzing actions based on the results obtained in the workflow. It can create summaries or more complex responses from the raw results obtained during the execution of actions.
|
46
47
|
|
47
|
-
- **Main
|
48
|
+
- **Main role**: Transform the results of actions into a comprehensible and structured output
|
48
49
|
- **Interactions**:
|
49
50
|
- Takes the results of executed actions
|
50
51
|
- Creates summaries or tailored responses
|
@@ -55,8 +56,8 @@ The synthesizer is responsible for generating responses and analyzing actions ba
|
|
55
56
|
|
56
57
|
The evaluator is responsible for assessing the results of executed actions and determining if additional actions are needed. It works in conjunction with the orchestrator to ensure all user requirements are met.
|
57
58
|
|
58
|
-
- **Main
|
59
|
-
- **Main
|
59
|
+
- **Main role**: Evaluate action results and determine next steps
|
60
|
+
- **Main functions**:
|
60
61
|
- Analyzes results from executed actions
|
61
62
|
- Determines if additional actions are needed
|
62
63
|
- Suggests next actions to the orchestrator
|
@@ -139,7 +140,7 @@ sudo apt-get install redis-server
|
|
139
140
|
|
140
141
|
CAG optimizes workflow execution through Redis-based caching:
|
141
142
|
|
142
|
-
- **Main
|
143
|
+
- **Main role**: Cache frequently used procedural patterns
|
143
144
|
- **Implementation**:
|
144
145
|
|
145
146
|
- Uses Redis for high-performance storage
|
@@ -229,81 +230,50 @@ export const prepareTransaction = {
|
|
229
230
|
|
230
231
|
---
|
231
232
|
|
232
|
-
## 3.
|
233
|
-
|
234
|
-
The workflow represents the entire process of executing a number of defined actions. When a user sends a prompt, the orchestrator determines which actions to perform based on the needs.
|
233
|
+
## 3. Agent processing
|
235
234
|
|
236
|
-
|
235
|
+
The agent handles the entire process of understanding user requests and coordinating responses. Here's an example of how to use the agent:
|
237
236
|
|
238
237
|
```typescript
|
239
|
-
const
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
const orchestrator = new Orchestrator(tools);
|
238
|
+
const memory = new PersistentMemory({
|
239
|
+
host: "http://localhost:7700",
|
240
|
+
apiKey: "YOUR_API_KEY",
|
241
|
+
});
|
245
242
|
|
246
|
-
const
|
247
|
-
|
248
|
-
|
243
|
+
const orchestrator = new Orchestrator(
|
244
|
+
[
|
245
|
+
getChainsTVL,
|
246
|
+
getRssNews,
|
247
|
+
// other tools...
|
248
|
+
],
|
249
|
+
memory
|
249
250
|
);
|
250
|
-
```
|
251
|
-
|
252
|
-
- **Orchestrator**: Manages the order of actions.
|
253
|
-
- **MemoryCache**: Reuses previous results.
|
254
|
-
- **EventEmitter**: Tracks and notifies the state of the workflow.
|
255
|
-
|
256
|
-
### Workflow process:
|
257
251
|
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
## 4. API calls and client side
|
252
|
+
const agent = new Agent({
|
253
|
+
user: { id: "user_id" },
|
254
|
+
orchestrator,
|
255
|
+
persistentMemory: memory,
|
256
|
+
stream: false,
|
257
|
+
maxEvaluatorIteration: 1,
|
258
|
+
});
|
266
259
|
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
const { messages, from } = request.body;
|
272
|
-
const latestMessage = messages[messages.length - 1];
|
273
|
-
|
274
|
-
const workflow = new Workflow(
|
275
|
-
{ id: from },
|
276
|
-
{ orchestrator, memoryCache, eventEmitter }
|
277
|
-
);
|
278
|
-
return workflow.start(latestMessage.content, messages);
|
260
|
+
// Process a user request
|
261
|
+
const result = await agent.process(prompt, context, {
|
262
|
+
onMessage: (message) => {
|
263
|
+
console.log({ message });
|
279
264
|
},
|
280
265
|
});
|
281
266
|
```
|
282
267
|
|
283
|
-
|
284
|
-
export function Chat({ id, initialMessages }) {
|
285
|
-
const { messages, setMessages, handleSubmit, input, setInput } = useChat({
|
286
|
-
api: "/api/chat",
|
287
|
-
body: { id, from: activeAccount?.address },
|
288
|
-
});
|
289
|
-
|
290
|
-
return (
|
291
|
-
<div>
|
292
|
-
<div>{messages}</div>
|
293
|
-
<input
|
294
|
-
type="text"
|
295
|
-
value={input}
|
296
|
-
onChange={(e) => setInput(e.target.value)}
|
297
|
-
/>
|
298
|
-
<button onClick={handleSubmit}>Send</button>
|
299
|
-
</div>
|
300
|
-
);
|
301
|
-
}
|
302
|
-
```
|
268
|
+
### Agent process flow:
|
303
269
|
|
304
|
-
|
270
|
+
1. User sends a prompt
|
271
|
+
2. Agent analyzes the prompt and context
|
272
|
+
3. Orchestrator executes required tools/actions
|
273
|
+
4. Evaluator assesses results
|
274
|
+
5. Agent generates final response
|
305
275
|
|
306
|
-
##
|
276
|
+
## 4. WIP (Work in Progress)
|
307
277
|
|
308
278
|
Here are the elements currently in development or improvement:
|
309
279
|
|
@@ -313,9 +283,10 @@ Here are the elements currently in development or improvement:
|
|
313
283
|
|
314
284
|
**Objective**: Enable multiple agents to collaborate on complex tasks with specialization and coordination.
|
315
285
|
|
316
|
-
**Interest**: Collaboration between agents allows breaking down complex tasks into specialized subtasks, enhancing the
|
286
|
+
**Interest**: Collaboration between agents allows breaking down complex tasks into specialized subtasks, enhancing the
|
287
|
+
efficiency and quality of results. It also enables better resource management and faster adaptation to changes.
|
317
288
|
|
318
|
-
**Steps to
|
289
|
+
**Steps to implement**:
|
319
290
|
|
320
291
|
- [ ] Task delegation framework.
|
321
292
|
- [ ] Shared context management.
|
@@ -329,9 +300,11 @@ Here are the elements currently in development or improvement:
|
|
329
300
|
|
330
301
|
**Objective**: Create a model for recognizing on-chain interactions and creating workflows for complex interactions.
|
331
302
|
|
332
|
-
**Interest**: This feature allows the agent to understand and interact with smart contracts more intuitively,
|
303
|
+
**Interest**: This feature allows the agent to understand and interact with smart contracts more intuitively,
|
304
|
+
facilitating the execution of complex actions on the blockchain. It improves accessibility and efficiency in
|
305
|
+
interacting with smart contracts.
|
333
306
|
|
334
|
-
**Steps to
|
307
|
+
**Steps to implement**:
|
335
308
|
|
336
309
|
- [ ] Extraction and processing of relevant contract ABIs.
|
337
310
|
- [ ] Filtering of relevant functions.
|
@@ -347,9 +320,13 @@ Here are the elements currently in development or improvement:
|
|
347
320
|
|
348
321
|
## Lit Protocol implementation
|
349
322
|
|
350
|
-
**Objective**: Add the ability to execute Lit actions, enabling decentralized and secure calculations on the Lit
|
323
|
+
**Objective**: Add the ability to execute Lit actions, enabling decentralized and secure calculations on the Lit
|
324
|
+
network.
|
351
325
|
|
352
|
-
**Interest**: Integrating the Lit Protocol allows executing Lit actions in a decentralized manner, using cryptographic
|
326
|
+
**Interest**: Integrating the Lit Protocol allows executing Lit actions in a decentralized manner, using cryptographic
|
327
|
+
keys to validate operations. These actions can be used to run JavaScript scripts in a decentralized environment,
|
328
|
+
offering transparency as all interactions are recorded on the blockchain. The main benefit lies in automation and
|
329
|
+
security while preserving user privacy, which enhances trust in on-chain interactions.
|
353
330
|
|
354
331
|
**Steps to Implement**:
|
355
332
|
|
@@ -358,4 +335,5 @@ Here are the elements currently in development or improvement:
|
|
358
335
|
- [ ] Develop modules for executing Lit actions, including signature management and secure script execution.
|
359
336
|
- [ ] Test the integration, security, and transparency of Lit actions to ensure they function properly.
|
360
337
|
|
361
|
-
**Status**: Under study to determine feasibility and technical implications, particularly regarding integrating
|
338
|
+
**Status**: Under study to determine feasibility and technical implications, particularly regarding integrating
|
339
|
+
decentralization into the existing system.
|
@@ -17,7 +17,7 @@ export class ActionHandler {
|
|
17
17
|
onActionStart: callbacks?.onActionStart,
|
18
18
|
onActionComplete: callbacks?.onActionComplete,
|
19
19
|
onQueueComplete: callbacks?.onQueueComplete,
|
20
|
-
onConfirmationRequired: async (message) => {
|
20
|
+
onConfirmationRequired: async (message: any) => {
|
21
21
|
if (callbacks?.onConfirmationRequired) {
|
22
22
|
return await callbacks.onConfirmationRequired(message);
|
23
23
|
}
|
package/agent/index.ts
CHANGED
@@ -3,8 +3,17 @@ import { Orchestrator } from "../llm/orchestrator";
|
|
3
3
|
import { Synthesizer } from "../llm/synthesizer";
|
4
4
|
import { CacheMemory } from "../memory/cache";
|
5
5
|
import { PersistentMemory } from "../memory/persistent";
|
6
|
-
import {
|
6
|
+
import {
|
7
|
+
ActionSchema,
|
8
|
+
AgentEvent,
|
9
|
+
MemoryScope,
|
10
|
+
MemoryType,
|
11
|
+
QueueResult,
|
12
|
+
TransformedQueueItem,
|
13
|
+
User,
|
14
|
+
} from "../types";
|
7
15
|
import { QueueItemTransformer } from "../utils/queue-item-transformer";
|
16
|
+
import { ResultSanitizer } from "../utils/sanitize-results";
|
8
17
|
import { ActionHandler } from "./handlers/ActionHandler";
|
9
18
|
|
10
19
|
export class Agent {
|
@@ -18,6 +27,7 @@ export class Agent {
|
|
18
27
|
private readonly stream: boolean;
|
19
28
|
private readonly maxEvaluatorIteration: number;
|
20
29
|
private evaluatorIteration = 0;
|
30
|
+
private accumulatedResults: QueueResult[] = [];
|
21
31
|
|
22
32
|
constructor({
|
23
33
|
user,
|
@@ -41,6 +51,7 @@ export class Agent {
|
|
41
51
|
this.stream = stream;
|
42
52
|
this.maxEvaluatorIteration = maxEvaluatorIteration;
|
43
53
|
this.actionHandler = new ActionHandler();
|
54
|
+
this.accumulatedResults = [];
|
44
55
|
}
|
45
56
|
|
46
57
|
async process(
|
@@ -48,20 +59,44 @@ export class Agent {
|
|
48
59
|
contextualizedPrompt: string,
|
49
60
|
events: AgentEvent
|
50
61
|
): Promise<any> {
|
51
|
-
|
52
|
-
|
53
|
-
|
62
|
+
let actions: ActionSchema[] | TransformedQueueItem[] | undefined =
|
63
|
+
undefined;
|
64
|
+
let isSimilar: boolean = false;
|
65
|
+
|
66
|
+
if (this.cacheMemory) {
|
67
|
+
const similarActions = await this.cacheMemory.findSimilarQueries(prompt, {
|
68
|
+
similarityThreshold: this.SIMILARITY_THRESHOLD,
|
69
|
+
maxResults: this.MAX_RESULTS,
|
70
|
+
userId: this.user.id,
|
71
|
+
scope: MemoryScope.GLOBAL,
|
72
|
+
});
|
73
|
+
|
74
|
+
if (similarActions.length > 0) {
|
75
|
+
actions = QueueItemTransformer.transformActionsToQueueItems(
|
76
|
+
similarActions[0].data
|
77
|
+
);
|
78
|
+
isSimilar = true;
|
79
|
+
}
|
80
|
+
}
|
54
81
|
|
55
|
-
if (
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
},
|
62
|
-
events
|
63
|
-
);
|
82
|
+
if (!actions?.length && !isSimilar) {
|
83
|
+
console.log("No similar actions found in cache for query: ", prompt);
|
84
|
+
console.log("Requesting orchestrator for actions..");
|
85
|
+
const request = await this.orchestrator.process(contextualizedPrompt);
|
86
|
+
events.onMessage?.(request);
|
87
|
+
actions = request.actions;
|
64
88
|
}
|
89
|
+
|
90
|
+
return actions && actions.length > 0
|
91
|
+
? this.handleActions(
|
92
|
+
{
|
93
|
+
initialPrompt: prompt,
|
94
|
+
contextualizedPrompt: contextualizedPrompt,
|
95
|
+
actions: actions as ActionSchema[],
|
96
|
+
},
|
97
|
+
events
|
98
|
+
)
|
99
|
+
: undefined;
|
65
100
|
}
|
66
101
|
|
67
102
|
private async handleActions(
|
@@ -76,8 +111,7 @@ export class Agent {
|
|
76
111
|
},
|
77
112
|
events: AgentEvent
|
78
113
|
): Promise<any> {
|
79
|
-
const
|
80
|
-
const queueItems = this.transformActions(actions, similarActions);
|
114
|
+
const queueItems = this.transformActions(actions);
|
81
115
|
|
82
116
|
const actionsResult = await this.actionHandler.executeActions(
|
83
117
|
queueItems,
|
@@ -91,81 +125,89 @@ export class Agent {
|
|
91
125
|
}
|
92
126
|
);
|
93
127
|
|
128
|
+
this.accumulatedResults = [
|
129
|
+
...this.accumulatedResults,
|
130
|
+
...actionsResult.data,
|
131
|
+
];
|
132
|
+
|
94
133
|
if (this.evaluatorIteration >= this.maxEvaluatorIteration) {
|
95
|
-
return this.handleActionResults({
|
134
|
+
return this.handleActionResults({
|
135
|
+
data: this.accumulatedResults,
|
136
|
+
initialPrompt,
|
137
|
+
});
|
96
138
|
}
|
97
139
|
|
98
140
|
const evaluator = new Evaluator(
|
99
141
|
this.orchestrator.tools,
|
100
142
|
this.persistentMemory
|
101
143
|
);
|
144
|
+
console.log("Accumulated results:");
|
145
|
+
console.dir(this.accumulatedResults, { depth: null });
|
146
|
+
|
147
|
+
const sanitizedResults = ResultSanitizer.sanitize(this.accumulatedResults);
|
102
148
|
const evaluation = await evaluator.process(
|
103
149
|
initialPrompt,
|
104
150
|
contextualizedPrompt,
|
105
|
-
|
151
|
+
sanitizedResults
|
106
152
|
);
|
107
153
|
|
108
154
|
events.onMessage?.(evaluation);
|
109
155
|
|
110
|
-
if (evaluation.
|
156
|
+
if (evaluation.isNextActionNeeded) {
|
111
157
|
this.evaluatorIteration++;
|
112
158
|
return this.handleActions(
|
113
159
|
{
|
114
160
|
initialPrompt: contextualizedPrompt,
|
115
161
|
contextualizedPrompt: initialPrompt,
|
116
|
-
actions: evaluation.
|
162
|
+
actions: evaluation.nextActionsNeeded,
|
117
163
|
},
|
118
164
|
events
|
119
165
|
);
|
120
166
|
}
|
121
167
|
|
122
|
-
if (!this.actionHandler.hasNonPrepareActions(
|
168
|
+
if (!this.actionHandler.hasNonPrepareActions(this.accumulatedResults)) {
|
123
169
|
return {
|
124
|
-
data:
|
170
|
+
data: this.accumulatedResults,
|
125
171
|
initialPrompt,
|
126
172
|
};
|
127
173
|
}
|
128
174
|
|
129
|
-
return this.handleActionResults({
|
175
|
+
return this.handleActionResults({
|
176
|
+
data: this.accumulatedResults,
|
177
|
+
initialPrompt,
|
178
|
+
});
|
130
179
|
}
|
131
180
|
|
132
181
|
private async handleActionResults(actionsResult: {
|
133
|
-
data:
|
182
|
+
data: QueueResult[];
|
134
183
|
initialPrompt: string;
|
135
184
|
}) {
|
136
185
|
const synthesizer = new Synthesizer();
|
186
|
+
const sanitizedResults = ResultSanitizer.sanitize(this.accumulatedResults);
|
137
187
|
const summaryData = JSON.stringify({
|
138
|
-
result:
|
188
|
+
result: sanitizedResults,
|
139
189
|
initialPrompt: actionsResult.initialPrompt,
|
140
190
|
});
|
141
191
|
|
192
|
+
this.accumulatedResults = [];
|
193
|
+
this.evaluatorIteration = 0;
|
194
|
+
|
195
|
+
await this.cacheMemory?.createMemory({
|
196
|
+
content: actionsResult.initialPrompt,
|
197
|
+
data: actionsResult.data,
|
198
|
+
scope: MemoryScope.GLOBAL,
|
199
|
+
type: MemoryType.ACTION,
|
200
|
+
});
|
201
|
+
|
142
202
|
return this.stream
|
143
203
|
? (await synthesizer.streamProcess(summaryData)).toDataStreamResponse()
|
144
204
|
: await synthesizer.process(summaryData);
|
145
205
|
}
|
146
206
|
|
147
|
-
private
|
148
|
-
if (!this.cacheMemory) {
|
149
|
-
return [];
|
150
|
-
}
|
151
|
-
|
152
|
-
return this.cacheMemory.findBestMatches(prompt, {
|
153
|
-
similarityThreshold: this.SIMILARITY_THRESHOLD,
|
154
|
-
maxResults: this.MAX_RESULTS,
|
155
|
-
userId: this.user.id,
|
156
|
-
scope: MemoryScope.USER,
|
157
|
-
});
|
158
|
-
}
|
159
|
-
|
160
|
-
private transformActions(actions: ActionSchema[], similarActions: any[]) {
|
207
|
+
private transformActions(actions: ActionSchema[]) {
|
161
208
|
let predefinedActions =
|
162
209
|
QueueItemTransformer.transformActionsToQueueItems(actions) || [];
|
163
210
|
|
164
|
-
if (similarActions?.length > 0) {
|
165
|
-
predefinedActions =
|
166
|
-
QueueItemTransformer.transformFromSimilarActions(similarActions) || [];
|
167
|
-
}
|
168
|
-
|
169
211
|
return predefinedActions;
|
170
212
|
}
|
171
213
|
}
|
package/dist/agent/index.d.ts
CHANGED
@@ -13,6 +13,7 @@ export declare class Agent {
|
|
13
13
|
private readonly stream;
|
14
14
|
private readonly maxEvaluatorIteration;
|
15
15
|
private evaluatorIteration;
|
16
|
+
private accumulatedResults;
|
16
17
|
constructor({ user, orchestrator, persistentMemory, cacheMemory, stream, maxEvaluatorIteration, }: {
|
17
18
|
user: User;
|
18
19
|
orchestrator: Orchestrator;
|
@@ -24,6 +25,5 @@ export declare class Agent {
|
|
24
25
|
process(prompt: string, contextualizedPrompt: string, events: AgentEvent): Promise<any>;
|
25
26
|
private handleActions;
|
26
27
|
private handleActionResults;
|
27
|
-
private findSimilarActions;
|
28
28
|
private transformActions;
|
29
29
|
}
|