@ai.ntellect/core 0.1.97 ā 0.1.98
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/dist/agent/index.d.ts +20 -4
- package/dist/agent/index.js +28 -46
- package/dist/llm/evaluator/context.d.ts +8 -6
- package/dist/llm/evaluator/context.js +25 -32
- package/dist/llm/evaluator/index.d.ts +4 -2
- package/dist/llm/evaluator/index.js +58 -27
- package/dist/llm/orchestrator/context.d.ts +7 -6
- package/dist/llm/orchestrator/context.js +14 -17
- package/dist/llm/orchestrator/index.d.ts +21 -4
- package/dist/llm/orchestrator/index.js +79 -20
- package/dist/llm/synthesizer/context.d.ts +12 -7
- package/dist/llm/synthesizer/context.js +51 -34
- package/dist/llm/synthesizer/index.d.ts +7 -5
- package/dist/llm/synthesizer/index.js +63 -16
- package/dist/memory/cache.d.ts +2 -2
- package/dist/memory/cache.js +35 -21
- package/dist/memory/persistent.js +20 -18
- package/dist/services/queue.js +19 -10
- package/dist/test.d.ts +45 -0
- package/dist/test.js +106 -5
- package/dist/types.d.ts +8 -1
- package/dist/utils/inject-actions.js +7 -1
- package/llm/orchestrator/context.ts +1 -1
- package/llm/synthesizer/index.ts +1 -1
- package/package.json +1 -1
package/dist/agent/index.d.ts
CHANGED
@@ -1,10 +1,26 @@
|
|
1
1
|
import { Orchestrator } from "../llm/orchestrator";
|
2
2
|
import { CacheMemory } from "../memory/cache";
|
3
3
|
import { PersistentMemory } from "../memory/persistent";
|
4
|
-
import { AgentEvent, User } from "../types";
|
4
|
+
import { ActionSchema, AgentEvent, QueueResult, User } from "../types";
|
5
|
+
export type State = {
|
6
|
+
behavior: {
|
7
|
+
role: string;
|
8
|
+
language: string;
|
9
|
+
guidelines: {
|
10
|
+
important: string[];
|
11
|
+
warnings: string[];
|
12
|
+
steps?: string[];
|
13
|
+
};
|
14
|
+
};
|
15
|
+
userRequest: string;
|
16
|
+
actions: ActionSchema[];
|
17
|
+
results: QueueResult[];
|
18
|
+
examplesMessages?: {
|
19
|
+
role: string;
|
20
|
+
content: string;
|
21
|
+
}[];
|
22
|
+
};
|
5
23
|
export declare class Agent {
|
6
|
-
private readonly SIMILARITY_THRESHOLD;
|
7
|
-
private readonly MAX_RESULTS;
|
8
24
|
private readonly actionHandler;
|
9
25
|
private readonly user;
|
10
26
|
private readonly orchestrator;
|
@@ -22,7 +38,7 @@ export declare class Agent {
|
|
22
38
|
stream: boolean;
|
23
39
|
maxEvaluatorIteration: number;
|
24
40
|
});
|
25
|
-
process(prompt: string,
|
41
|
+
process(prompt: string, events: AgentEvent): Promise<any>;
|
26
42
|
private handleActions;
|
27
43
|
private handleActionResults;
|
28
44
|
private transformActions;
|
package/dist/agent/index.js
CHANGED
@@ -9,8 +9,6 @@ const sanitize_results_1 = require("../utils/sanitize-results");
|
|
9
9
|
const ActionHandler_1 = require("./handlers/ActionHandler");
|
10
10
|
class Agent {
|
11
11
|
constructor({ user, orchestrator, persistentMemory, cacheMemory, stream, maxEvaluatorIteration = 1, }) {
|
12
|
-
this.SIMILARITY_THRESHOLD = 95;
|
13
|
-
this.MAX_RESULTS = 1;
|
14
12
|
this.evaluatorIteration = 0;
|
15
13
|
this.accumulatedResults = [];
|
16
14
|
this.user = user;
|
@@ -22,37 +20,25 @@ class Agent {
|
|
22
20
|
this.actionHandler = new ActionHandler_1.ActionHandler();
|
23
21
|
this.accumulatedResults = [];
|
24
22
|
}
|
25
|
-
async process(prompt,
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
}
|
35
|
-
if (similarActions.length > 0) {
|
36
|
-
actions = queue_item_transformer_1.QueueItemTransformer.transformActionsToQueueItems(similarActions[0].data);
|
37
|
-
isSimilar = true;
|
38
|
-
}
|
39
|
-
}
|
40
|
-
if (!actions?.length && !isSimilar) {
|
41
|
-
console.log("No similar actions found in cache for query: ", prompt);
|
42
|
-
console.log("Requesting orchestrator for actions..");
|
43
|
-
const request = await this.orchestrator.process(contextualizedPrompt);
|
44
|
-
events.onMessage?.(request);
|
45
|
-
actions = request.actions;
|
23
|
+
async process(prompt, events) {
|
24
|
+
console.log("Requesting orchestrator for actions..");
|
25
|
+
const request = await this.orchestrator.process(prompt, this.accumulatedResults);
|
26
|
+
events.onMessage?.(request);
|
27
|
+
const isOnChainAction = request.actions.some((action) => action.type === "on-chain");
|
28
|
+
if (isOnChainAction) {
|
29
|
+
return {
|
30
|
+
data: this.accumulatedResults,
|
31
|
+
initialPrompt: prompt,
|
32
|
+
};
|
46
33
|
}
|
47
|
-
return actions
|
34
|
+
return request.actions.length > 0
|
48
35
|
? this.handleActions({
|
49
36
|
initialPrompt: prompt,
|
50
|
-
|
51
|
-
actions: actions,
|
37
|
+
actions: request.actions,
|
52
38
|
}, events)
|
53
39
|
: undefined;
|
54
40
|
}
|
55
|
-
async handleActions({ initialPrompt,
|
41
|
+
async handleActions({ initialPrompt, actions, }, events) {
|
56
42
|
const queueItems = this.transformActions(actions);
|
57
43
|
const actionsResult = await this.actionHandler.executeActions(queueItems, this.orchestrator.tools, {
|
58
44
|
onQueueStart: events.onQueueStart,
|
@@ -74,23 +60,16 @@ class Agent {
|
|
74
60
|
const evaluator = new evaluator_1.Evaluator(this.orchestrator.tools, this.persistentMemory);
|
75
61
|
console.log("Accumulated results:");
|
76
62
|
console.dir(this.accumulatedResults, { depth: null });
|
77
|
-
const sanitizedResults =
|
78
|
-
const evaluation = await evaluator.process(initialPrompt,
|
63
|
+
// const sanitizedResults = ResultSanitizer.sanitize(this.accumulatedResults);
|
64
|
+
const evaluation = await evaluator.process(initialPrompt, this.accumulatedResults);
|
79
65
|
events.onMessage?.(evaluation);
|
80
66
|
if (evaluation.isNextActionNeeded) {
|
81
67
|
this.evaluatorIteration++;
|
82
68
|
return this.handleActions({
|
83
|
-
initialPrompt:
|
84
|
-
contextualizedPrompt: initialPrompt,
|
69
|
+
initialPrompt: initialPrompt,
|
85
70
|
actions: evaluation.nextActionsNeeded,
|
86
71
|
}, events);
|
87
72
|
}
|
88
|
-
if (!this.actionHandler.hasNonPrepareActions(this.accumulatedResults)) {
|
89
|
-
return {
|
90
|
-
data: this.accumulatedResults,
|
91
|
-
initialPrompt,
|
92
|
-
};
|
93
|
-
}
|
94
73
|
return this.handleActionResults({
|
95
74
|
data: this.accumulatedResults,
|
96
75
|
initialPrompt,
|
@@ -101,19 +80,22 @@ class Agent {
|
|
101
80
|
const sanitizedResults = sanitize_results_1.ResultSanitizer.sanitize(this.accumulatedResults);
|
102
81
|
const summaryData = JSON.stringify({
|
103
82
|
result: sanitizedResults,
|
104
|
-
initialPrompt: actionsResult.initialPrompt,
|
105
83
|
});
|
106
84
|
this.accumulatedResults = [];
|
107
85
|
this.evaluatorIteration = 0;
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
86
|
+
for (const action of actionsResult.data) {
|
87
|
+
if (!action.error) {
|
88
|
+
await this.cacheMemory?.createMemory({
|
89
|
+
content: actionsResult.initialPrompt,
|
90
|
+
data: action.result,
|
91
|
+
scope: types_1.MemoryScope.GLOBAL,
|
92
|
+
type: types_1.MemoryType.ACTION,
|
93
|
+
});
|
94
|
+
}
|
95
|
+
}
|
114
96
|
return this.stream
|
115
|
-
? (await synthesizer.streamProcess(summaryData)).toDataStreamResponse()
|
116
|
-
: await synthesizer.process(
|
97
|
+
? (await synthesizer.streamProcess(actionsResult.initialPrompt, summaryData)).toDataStreamResponse()
|
98
|
+
: await synthesizer.process(actionsResult.initialPrompt, this.accumulatedResults);
|
117
99
|
}
|
118
100
|
transformActions(actions) {
|
119
101
|
let predefinedActions = queue_item_transformer_1.QueueItemTransformer.transformActionsToQueueItems(actions) || [];
|
@@ -1,9 +1,11 @@
|
|
1
|
-
import { ActionSchema } from "../../types";
|
2
1
|
export declare const evaluatorContext: {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
2
|
+
behavior: {
|
3
|
+
language: string;
|
4
|
+
role: string;
|
5
|
+
guidelines: {
|
6
|
+
important: string[];
|
7
|
+
warnings: string[];
|
8
|
+
steps: string[];
|
9
|
+
};
|
7
10
|
};
|
8
|
-
compose: (goal: string, results: string, tools: ActionSchema[]) => string;
|
9
11
|
};
|
@@ -1,38 +1,31 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.evaluatorContext = void 0;
|
4
|
-
const inject_actions_1 = require("../../utils/inject-actions");
|
5
4
|
exports.evaluatorContext = {
|
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
|
-
1. Success status with explanation (no action needed)
|
32
|
-
2. Next actions needed (if any)
|
33
|
-
3. Why you are doing the next actions or why you are not doing them
|
34
|
-
4. Extract relevant information to remember
|
35
|
-
5. For each facts, generate a memoryType (3 memory types: episodic, semantic, procedural)
|
36
|
-
`;
|
5
|
+
behavior: {
|
6
|
+
language: "user_language",
|
7
|
+
role: "Your role is to verify if the goal has been achieved and make a response or suggest next actions.",
|
8
|
+
guidelines: {
|
9
|
+
important: [
|
10
|
+
"Verify if all required actions were executed successfully.",
|
11
|
+
"Check if the results align with the initial goal.",
|
12
|
+
"Identify and extract additional relevant information naturally during the process. Examples:",
|
13
|
+
" - Link a token symbol (e.g., 'USDC') to its address (e.g., '0xA0b8...6EB48').",
|
14
|
+
" - Associate a wallet address (e.g., '0x1234...abcd') to a user-friendly name (e.g., 'Work Wallet').",
|
15
|
+
" - Map a token address (e.g., '0x6B17...71d0F') back to its symbol or name (e.g., 'DAI').",
|
16
|
+
"Store these facts in memory with their type (episodic, semantic, or procedural).",
|
17
|
+
],
|
18
|
+
warnings: [
|
19
|
+
"NEVER modify the results directly.",
|
20
|
+
"NEVER make assumptions about missing data.",
|
21
|
+
"NEVER repeat actions already completed unless explicitly required.",
|
22
|
+
],
|
23
|
+
steps: [
|
24
|
+
"Verify success: Confirm if the goal has been fully or partially achieved. If partially, describe what's missing.",
|
25
|
+
"Recommend next actions: Clearly state what needs to be done next (if applicable) and why.",
|
26
|
+
"Store key facts: Store any relevant information in memory with their type (episodic, semantic, or procedural).",
|
27
|
+
"Be clear, concise, and prioritize storing key facts that may help improve future interactions.",
|
28
|
+
],
|
29
|
+
},
|
37
30
|
},
|
38
31
|
};
|
@@ -1,9 +1,11 @@
|
|
1
|
+
import { State } from "../../agent";
|
1
2
|
import { PersistentMemory } from "../../memory/persistent";
|
2
|
-
import { ActionSchema } from "../../types";
|
3
|
+
import { ActionSchema, QueueResult } from "../../types";
|
3
4
|
export declare class Evaluator {
|
4
5
|
private readonly model;
|
5
6
|
tools: ActionSchema[];
|
6
7
|
private memory;
|
7
8
|
constructor(tools: ActionSchema[], memory: PersistentMemory);
|
8
|
-
|
9
|
+
composeContext(state: State): string;
|
10
|
+
process(prompt: string, results: QueueResult[]): Promise<any>;
|
9
11
|
}
|
@@ -5,6 +5,7 @@ const openai_1 = require("@ai-sdk/openai");
|
|
5
5
|
const ai_1 = require("ai");
|
6
6
|
const zod_1 = require("zod");
|
7
7
|
const types_1 = require("../../types");
|
8
|
+
const inject_actions_1 = require("../../utils/inject-actions");
|
8
9
|
const context_1 = require("./context");
|
9
10
|
class Evaluator {
|
10
11
|
constructor(tools, memory) {
|
@@ -12,24 +13,56 @@ class Evaluator {
|
|
12
13
|
this.tools = tools;
|
13
14
|
this.memory = memory;
|
14
15
|
}
|
15
|
-
|
16
|
+
composeContext(state) {
|
17
|
+
const { behavior, userRequest, actions, results } = state;
|
18
|
+
const { role, language, guidelines } = behavior;
|
19
|
+
const { important, warnings, steps } = guidelines;
|
20
|
+
const context = `
|
21
|
+
# ROLE: ${role}
|
22
|
+
# LANGUAGE: ${language}
|
23
|
+
# IMPORTANT: ${important.join("\n")}
|
24
|
+
# NEVER: ${warnings.join("\n")}
|
25
|
+
# USER_REQUEST: ${userRequest}
|
26
|
+
# ACTIONS AVAILABLE: ${(0, inject_actions_1.injectActions)(actions)}
|
27
|
+
# CURRENT_RESULTS: ${results.map((r) => r.result).join(", ")}
|
28
|
+
# STEPS: ${steps?.join("\n") || ""}
|
29
|
+
`;
|
30
|
+
return context;
|
31
|
+
}
|
32
|
+
async process(prompt, results) {
|
16
33
|
try {
|
34
|
+
const context = this.composeContext({
|
35
|
+
behavior: context_1.evaluatorContext.behavior,
|
36
|
+
userRequest: prompt,
|
37
|
+
actions: this.tools,
|
38
|
+
results: results,
|
39
|
+
});
|
40
|
+
console.log("\nš Evaluator processing");
|
41
|
+
console.log("Goal:", prompt);
|
42
|
+
console.log("Results to evaluate:", JSON.stringify(results, null, 2));
|
17
43
|
const response = await (0, ai_1.generateObject)({
|
18
44
|
model: this.model,
|
19
45
|
schema: zod_1.z.object({
|
20
46
|
isRemindNeeded: zod_1.z.boolean(),
|
21
|
-
|
47
|
+
importantToRemembers: zod_1.z.array(zod_1.z.object({
|
22
48
|
memoryType: zod_1.z.string(),
|
23
49
|
content: zod_1.z.string(),
|
24
50
|
data: zod_1.z.string(),
|
25
51
|
})),
|
26
52
|
response: zod_1.z.string(),
|
27
53
|
isNextActionNeeded: zod_1.z.boolean(),
|
28
|
-
nextActionsNeeded:
|
54
|
+
nextActionsNeeded: zod_1.z.array(zod_1.z.object({
|
55
|
+
name: zod_1.z.string(),
|
56
|
+
parameters: zod_1.z.array(zod_1.z.object({
|
57
|
+
name: zod_1.z.string(),
|
58
|
+
value: zod_1.z.any(),
|
59
|
+
})),
|
60
|
+
})),
|
29
61
|
why: zod_1.z.string(),
|
30
62
|
}),
|
31
63
|
prompt: prompt,
|
32
|
-
system:
|
64
|
+
system: context,
|
65
|
+
temperature: 0,
|
33
66
|
});
|
34
67
|
const validatedResponse = {
|
35
68
|
...response.object,
|
@@ -39,44 +72,42 @@ class Evaluator {
|
|
39
72
|
})),
|
40
73
|
};
|
41
74
|
if (validatedResponse.isRemindNeeded) {
|
42
|
-
|
43
|
-
|
75
|
+
console.log("\nš Processing important memories to store", validatedResponse);
|
76
|
+
for (const item of validatedResponse.importantToRemembers) {
|
77
|
+
console.log("\nš Processing memory item:");
|
78
|
+
console.log("Type:", item.memoryType);
|
79
|
+
console.log("Content:", item.content);
|
44
80
|
const memories = await this.memory.searchSimilarQueries(item.content, {
|
45
81
|
similarityThreshold: 95,
|
46
82
|
});
|
47
83
|
if (memories.length > 0) {
|
48
|
-
console.log("Similar
|
49
|
-
memories,
|
50
|
-
});
|
84
|
+
console.log("š Similar memory already exists - skipping");
|
51
85
|
continue;
|
52
86
|
}
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
data: item.data,
|
63
|
-
scope: types_1.MemoryScope.GLOBAL,
|
64
|
-
createdAt: new Date(),
|
65
|
-
});
|
66
|
-
}
|
87
|
+
console.log("⨠Storing new memory");
|
88
|
+
await this.memory.createMemory({
|
89
|
+
id: crypto.randomUUID(),
|
90
|
+
purpose: item.memoryType,
|
91
|
+
query: item.content,
|
92
|
+
data: item.data,
|
93
|
+
scope: types_1.MemoryScope.GLOBAL,
|
94
|
+
createdAt: new Date(),
|
95
|
+
});
|
67
96
|
}
|
68
97
|
}
|
69
|
-
console.log("
|
70
|
-
console.
|
98
|
+
console.log("\nā
Evaluation completed");
|
99
|
+
console.log("ā".repeat(50));
|
100
|
+
console.log("Results:", JSON.stringify(validatedResponse, null, 2));
|
71
101
|
return validatedResponse;
|
72
102
|
}
|
73
103
|
catch (error) {
|
104
|
+
console.error("\nā Evaluator error:", error.message);
|
74
105
|
if (error) {
|
75
106
|
console.log("Evaluator error");
|
76
107
|
console.dir(error.value, { depth: null });
|
77
108
|
console.error(error.message);
|
78
|
-
if (error.value.
|
79
|
-
for (const item of error.value.
|
109
|
+
if (error.value.importantToRemembers.length > 0) {
|
110
|
+
for (const item of error.value.importantToRemembers) {
|
80
111
|
// Check if the item is already in the memory
|
81
112
|
const memories = await this.memory.searchSimilarQueries(item.content);
|
82
113
|
if (memories.length === 0) {
|
@@ -1,9 +1,10 @@
|
|
1
|
-
import { ActionSchema } from "../../types";
|
2
1
|
export declare const orchestratorContext: {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
2
|
+
behavior: {
|
3
|
+
language: string;
|
4
|
+
role: string;
|
5
|
+
guidelines: {
|
6
|
+
important: string[];
|
7
|
+
warnings: never[];
|
8
|
+
};
|
7
9
|
};
|
8
|
-
compose: (tools: ActionSchema[]) => string;
|
9
10
|
};
|
@@ -1,23 +1,20 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.orchestratorContext = void 0;
|
4
|
-
const inject_actions_1 = require("../../utils/inject-actions");
|
5
4
|
exports.orchestratorContext = {
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
The actions are: ${(0, inject_actions_1.injectActions)(tools)}
|
21
|
-
`;
|
5
|
+
behavior: {
|
6
|
+
language: "user_language",
|
7
|
+
role: "You are the orchestrator agent. Your role is to determine what actions are needed to achieve the user goal.",
|
8
|
+
guidelines: {
|
9
|
+
important: [
|
10
|
+
"If there is no action to do, you must answer in the 'answer' field.",
|
11
|
+
"If some parameters are not clear or missing, don't add the action, YOU MUST ask the user for them.",
|
12
|
+
"ALWAYS use the same language as user request. (If it's English, use English, if it's French, use French, etc.)",
|
13
|
+
"For ON-CHAIN actions, just use the useful actions.",
|
14
|
+
"For QUESTIONS or ANALYSIS, you CAN search in memory and internal knowledge base.",
|
15
|
+
"NEVER repeat same actions if the user doesn't ask for it.",
|
16
|
+
],
|
17
|
+
warnings: [],
|
18
|
+
},
|
22
19
|
},
|
23
20
|
};
|
@@ -1,9 +1,26 @@
|
|
1
|
+
import { State } from "../../agent";
|
2
|
+
import { CacheMemory } from "../../memory/cache";
|
1
3
|
import { PersistentMemory } from "../../memory/persistent";
|
2
|
-
import { ActionSchema,
|
3
|
-
export declare class Orchestrator
|
4
|
+
import { ActionSchema, QueueResult } from "../../types";
|
5
|
+
export declare class Orchestrator {
|
4
6
|
private readonly model;
|
5
7
|
tools: ActionSchema[];
|
6
8
|
private memory;
|
7
|
-
|
8
|
-
|
9
|
+
private id;
|
10
|
+
constructor(id: string, tools: ActionSchema[], memory: {
|
11
|
+
persistent: PersistentMemory;
|
12
|
+
cache: CacheMemory;
|
13
|
+
});
|
14
|
+
composeContext(state: State): string;
|
15
|
+
process(prompt: string, results: QueueResult[]): Promise<{
|
16
|
+
actions: {
|
17
|
+
name: string;
|
18
|
+
type: string;
|
19
|
+
parameters: {
|
20
|
+
name: string;
|
21
|
+
value: any;
|
22
|
+
}[];
|
23
|
+
}[];
|
24
|
+
answer: string;
|
25
|
+
}>;
|
9
26
|
}
|
@@ -5,24 +5,42 @@ const openai_1 = require("@ai-sdk/openai");
|
|
5
5
|
const ai_1 = require("ai");
|
6
6
|
const zod_1 = require("zod");
|
7
7
|
const types_1 = require("../../types");
|
8
|
+
const inject_actions_1 = require("../../utils/inject-actions");
|
8
9
|
const context_1 = require("./context");
|
9
10
|
class Orchestrator {
|
10
|
-
constructor(tools, memory) {
|
11
|
+
constructor(id, tools, memory) {
|
11
12
|
this.model = (0, openai_1.openai)("gpt-4o");
|
13
|
+
this.id = id;
|
12
14
|
this.memory = memory;
|
13
15
|
this.tools = [
|
14
16
|
...tools,
|
15
17
|
{
|
16
|
-
name: "
|
18
|
+
name: "search_internal_knowledge_base",
|
17
19
|
description: "Search for relevant information in the internal knowledge base",
|
18
20
|
parameters: zod_1.z.object({
|
19
21
|
query: zod_1.z.string(),
|
20
22
|
}),
|
21
23
|
execute: async ({ query }) => {
|
22
|
-
const
|
23
|
-
similarityThreshold:
|
24
|
+
const persistentMemories = await this.memory.persistent.searchSimilarQueries(query, {
|
25
|
+
similarityThreshold: 70,
|
24
26
|
});
|
25
|
-
return
|
27
|
+
return persistentMemories;
|
28
|
+
},
|
29
|
+
},
|
30
|
+
{
|
31
|
+
name: "search_cache_memory",
|
32
|
+
description: "Search for relevant information in the cache",
|
33
|
+
parameters: zod_1.z.object({
|
34
|
+
query: zod_1.z.string(),
|
35
|
+
}),
|
36
|
+
execute: async ({ query }) => {
|
37
|
+
const cacheMemories = await this.memory.cache.findSimilarQueries(query, {
|
38
|
+
similarityThreshold: 70,
|
39
|
+
maxResults: 1,
|
40
|
+
userId: this.id,
|
41
|
+
scope: types_1.MemoryScope.GLOBAL,
|
42
|
+
});
|
43
|
+
return cacheMemories;
|
26
44
|
},
|
27
45
|
},
|
28
46
|
{
|
@@ -37,7 +55,7 @@ class Orchestrator {
|
|
37
55
|
whyStored: zod_1.z.string(),
|
38
56
|
}),
|
39
57
|
execute: async ({ query, purpose, data, scope, userId, }) => {
|
40
|
-
const memories = await this.memory.createMemory({
|
58
|
+
const memories = await this.memory.persistent.createMemory({
|
41
59
|
query,
|
42
60
|
purpose,
|
43
61
|
data,
|
@@ -51,38 +69,79 @@ class Orchestrator {
|
|
51
69
|
},
|
52
70
|
];
|
53
71
|
}
|
54
|
-
|
72
|
+
composeContext(state) {
|
73
|
+
const { behavior, userRequest, actions, results } = state;
|
74
|
+
const { role, language, guidelines } = behavior;
|
75
|
+
const { important, warnings } = guidelines;
|
76
|
+
const context = `
|
77
|
+
# ROLE: ${role}
|
78
|
+
# LANGUAGE: ${language}
|
79
|
+
# IMPORTANT: ${important.join("\n")}
|
80
|
+
# USER_REQUEST: ${userRequest}
|
81
|
+
# ACTIONS_AVAILABLES: ${(0, inject_actions_1.injectActions)(actions)} (NEVER REPEAT ACTIONS)
|
82
|
+
# CURRENT_RESULTS: ${results.map((r) => r.result).join(", ")}
|
83
|
+
`.trim();
|
84
|
+
return context;
|
85
|
+
}
|
86
|
+
async process(prompt, results) {
|
87
|
+
const state = this.composeContext({
|
88
|
+
behavior: context_1.orchestratorContext.behavior,
|
89
|
+
userRequest: prompt,
|
90
|
+
actions: this.tools,
|
91
|
+
results: results,
|
92
|
+
});
|
55
93
|
try {
|
94
|
+
console.log("\nš Orchestrator processing");
|
95
|
+
console.log("Prompt:", prompt);
|
56
96
|
const response = await (0, ai_1.generateObject)({
|
57
97
|
model: this.model,
|
58
98
|
schema: zod_1.z.object({
|
59
|
-
actions:
|
99
|
+
actions: zod_1.z.array(zod_1.z.object({
|
100
|
+
name: zod_1.z.string(),
|
101
|
+
type: zod_1.z.enum(["on-chain", "off-chain", "question", "analysis"]),
|
102
|
+
parameters: zod_1.z.array(zod_1.z.object({
|
103
|
+
name: zod_1.z.string(),
|
104
|
+
value: zod_1.z.any(),
|
105
|
+
})),
|
106
|
+
})),
|
60
107
|
answer: zod_1.z.string(),
|
61
108
|
}),
|
62
109
|
prompt: prompt,
|
63
|
-
system:
|
110
|
+
system: state,
|
111
|
+
temperature: 0,
|
64
112
|
});
|
65
113
|
const validatedResponse = {
|
66
114
|
...response.object,
|
67
115
|
actions: response.object.actions.map((action) => ({
|
68
116
|
...action,
|
69
|
-
parameters: action.parameters
|
117
|
+
parameters: Array.isArray(action.parameters)
|
118
|
+
? action.parameters.map((param) => ({
|
119
|
+
name: param.name,
|
120
|
+
value: param.value ?? null,
|
121
|
+
}))
|
122
|
+
: Object.entries(action.parameters || {}).map(([name, value]) => ({
|
123
|
+
name,
|
124
|
+
value: value ?? null,
|
125
|
+
})),
|
70
126
|
})),
|
71
127
|
};
|
72
|
-
console.log("
|
73
|
-
console.
|
128
|
+
console.log("\nā
Orchestration completed");
|
129
|
+
console.log("ā".repeat(50));
|
130
|
+
console.log("Actions determined:", validatedResponse.actions.map((a) => {
|
131
|
+
return `${a.name} (${a.type})`;
|
132
|
+
}));
|
133
|
+
if (validatedResponse.answer) {
|
134
|
+
console.log("Response:", validatedResponse.answer);
|
135
|
+
}
|
74
136
|
return validatedResponse;
|
75
137
|
}
|
76
138
|
catch (error) {
|
77
|
-
|
78
|
-
|
79
|
-
console.
|
80
|
-
|
81
|
-
return {
|
82
|
-
...error.value,
|
83
|
-
};
|
139
|
+
console.error("\nā Orchestrator error:", error.message);
|
140
|
+
if (error?.value) {
|
141
|
+
console.log("Partial response:", JSON.stringify(error.value, null, 2));
|
142
|
+
return { ...error.value };
|
84
143
|
}
|
85
|
-
|
144
|
+
throw error;
|
86
145
|
}
|
87
146
|
}
|
88
147
|
}
|