@ai.ntellect/core 0.3.3 ā 0.4.0
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/.nvmrc +1 -0
- package/README.FR.md +201 -261
- package/README.md +208 -260
- package/agent/index.ts +199 -216
- package/agent/tools/get-rss.ts +64 -0
- package/bull.ts +5 -0
- package/dist/agent/index.d.ts +29 -26
- package/dist/agent/index.js +123 -112
- package/dist/agent/tools/get-rss.d.ts +16 -0
- package/dist/agent/tools/get-rss.js +62 -0
- package/dist/bull.d.ts +1 -0
- package/dist/bull.js +9 -0
- package/dist/examples/index.d.ts +2 -0
- package/dist/examples/index.js +89 -0
- package/dist/llm/interpreter/context.d.ts +5 -22
- package/dist/llm/interpreter/context.js +8 -9
- package/dist/llm/interpreter/index.d.ts +9 -5
- package/dist/llm/interpreter/index.js +55 -48
- package/dist/llm/memory-manager/context.d.ts +2 -0
- package/dist/llm/memory-manager/context.js +22 -0
- package/dist/llm/memory-manager/index.d.ts +17 -0
- package/dist/llm/memory-manager/index.js +107 -0
- package/dist/llm/orchestrator/context.d.ts +2 -10
- package/dist/llm/orchestrator/context.js +19 -14
- package/dist/llm/orchestrator/index.d.ts +36 -21
- package/dist/llm/orchestrator/index.js +122 -88
- package/dist/llm/orchestrator/types.d.ts +12 -0
- package/dist/llm/orchestrator/types.js +2 -0
- package/dist/memory/cache.d.ts +6 -5
- package/dist/memory/cache.js +31 -21
- package/dist/memory/persistent.d.ts +5 -3
- package/dist/memory/persistent.js +89 -73
- package/dist/services/redis-cache.d.ts +37 -0
- package/dist/services/redis-cache.js +93 -0
- package/dist/services/scheduler.d.ts +39 -16
- package/dist/services/scheduler.js +81 -103
- package/dist/services/telegram-monitor.d.ts +0 -15
- package/dist/services/telegram-monitor.js +117 -101
- package/dist/test.js +106 -172
- package/dist/types.d.ts +38 -7
- package/dist/utils/generate-object.d.ts +12 -0
- package/dist/utils/generate-object.js +90 -0
- package/dist/utils/header-builder.d.ts +11 -0
- package/dist/utils/header-builder.js +34 -0
- package/dist/utils/inject-actions.js +2 -2
- package/dist/utils/queue-item-transformer.d.ts +2 -2
- package/dist/utils/schema-generator.d.ts +16 -0
- package/dist/utils/schema-generator.js +46 -0
- package/examples/index.ts +103 -0
- package/llm/interpreter/context.ts +20 -8
- package/llm/interpreter/index.ts +81 -54
- package/llm/memory-manager/context.ts +21 -0
- package/llm/memory-manager/index.ts +163 -0
- package/llm/orchestrator/context.ts +20 -13
- package/llm/orchestrator/index.ts +210 -130
- package/llm/orchestrator/types.ts +14 -0
- package/memory/cache.ts +37 -31
- package/memory/persistent.ts +121 -99
- package/package.json +11 -2
- package/services/redis-cache.ts +128 -0
- package/services/scheduler.ts +102 -141
- package/services/telegram-monitor.ts +138 -138
- package/t.py +79 -0
- package/t.spec +38 -0
- package/types.ts +40 -7
- package/utils/generate-object.ts +105 -0
- package/utils/header-builder.ts +40 -0
- package/utils/inject-actions.ts +4 -6
- package/utils/queue-item-transformer.ts +2 -1
- package/utils/schema-generator.ts +73 -0
- package/agent/handlers/ActionHandler.ts +0 -48
- package/agent/handlers/ConfirmationHandler.ts +0 -37
- package/agent/handlers/EventHandler.ts +0 -35
- package/dist/agent/handlers/ActionHandler.d.ts +0 -8
- package/dist/agent/handlers/ActionHandler.js +0 -36
- package/dist/agent/handlers/ConfirmationHandler.d.ts +0 -7
- package/dist/agent/handlers/ConfirmationHandler.js +0 -31
- package/dist/agent/handlers/EventHandler.d.ts +0 -10
- package/dist/agent/handlers/EventHandler.js +0 -34
- package/dist/llm/evaluator/context.d.ts +0 -10
- package/dist/llm/evaluator/context.js +0 -24
- package/dist/llm/evaluator/index.d.ts +0 -16
- package/dist/llm/evaluator/index.js +0 -150
- package/llm/evaluator/context.ts +0 -21
- package/llm/evaluator/index.ts +0 -193
@@ -1,69 +1,75 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.Interpreter = void 0;
|
4
|
-
const openai_1 = require("@ai-sdk/openai");
|
5
4
|
const ai_1 = require("ai");
|
6
5
|
const zod_1 = require("zod");
|
6
|
+
const generate_object_1 = require("../../utils/generate-object");
|
7
|
+
const header_builder_1 = require("../../utils/header-builder");
|
8
|
+
const interpreterSchema = zod_1.z.object({
|
9
|
+
requestLanguage: zod_1.z
|
10
|
+
.string()
|
11
|
+
.describe("The language of the user's request (fr, en, es, etc.)"),
|
12
|
+
actionsCompleted: zod_1.z
|
13
|
+
.array(zod_1.z.object({
|
14
|
+
name: zod_1.z.string(),
|
15
|
+
reasoning: zod_1.z.string(),
|
16
|
+
}))
|
17
|
+
.describe("The actions done and why."),
|
18
|
+
response: zod_1.z.string().describe("The response to the user's request."),
|
19
|
+
});
|
7
20
|
class Interpreter {
|
8
|
-
constructor(name,
|
9
|
-
this.behavior = behavior;
|
10
|
-
this.model = (0, openai_1.openai)("gpt-4o");
|
21
|
+
constructor({ name, model, character, }) {
|
11
22
|
this.name = name;
|
12
|
-
this.
|
23
|
+
this.model = model;
|
24
|
+
this.character = character;
|
13
25
|
}
|
14
|
-
|
26
|
+
buildContext(state) {
|
15
27
|
const { userRequest, results } = state;
|
16
|
-
const { role, language, guidelines
|
28
|
+
const { role, language, guidelines } = this.character;
|
17
29
|
const { important, warnings, steps } = guidelines;
|
18
|
-
const context =
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
30
|
+
const context = header_builder_1.LLMHeaderBuilder.create();
|
31
|
+
if (role) {
|
32
|
+
context.addHeader("ROLE", role);
|
33
|
+
}
|
34
|
+
if (language) {
|
35
|
+
context.addHeader("LANGUAGE", language);
|
36
|
+
}
|
37
|
+
if (important.length > 0) {
|
38
|
+
context.addHeader("IMPORTANT", important);
|
39
|
+
}
|
40
|
+
if (warnings.length > 0) {
|
41
|
+
context.addHeader("NEVER", warnings);
|
42
|
+
}
|
43
|
+
context.addHeader("CURRENT_RESULTS", results);
|
28
44
|
return context;
|
29
45
|
}
|
30
46
|
async process(prompt, state, onFinish) {
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
response: zod_1.z.string(),
|
44
|
-
}),
|
45
|
-
prompt,
|
46
|
-
system: context,
|
47
|
-
});
|
48
|
-
console.log("\nā
Interpretation completed");
|
49
|
-
console.log("ā".repeat(50));
|
50
|
-
console.log("Generated response:", result.object);
|
51
|
-
if (result.object.actionsCompleted.length > 0) {
|
52
|
-
console.log("\nš Suggested actions:");
|
53
|
-
result.object.actionsCompleted.forEach((action, index) => {
|
54
|
-
console.log(`\n${index + 1}. Action Details:`);
|
55
|
-
console.log(` Name: ${action.name}`);
|
56
|
-
console.log(` Reasoning: ${action.reasoning}`);
|
47
|
+
try {
|
48
|
+
console.log("\nšØ Starting interpretation process");
|
49
|
+
console.log("Prompt:", prompt);
|
50
|
+
console.log("Results to interpret:", JSON.stringify(state, null, 2));
|
51
|
+
const context = this.buildContext(state);
|
52
|
+
console.log("Context:", context.toString());
|
53
|
+
const result = await (0, generate_object_1.generateObject)({
|
54
|
+
model: this.model,
|
55
|
+
prompt,
|
56
|
+
system: context.toString(),
|
57
|
+
temperature: 1.3,
|
58
|
+
schema: interpreterSchema,
|
57
59
|
});
|
60
|
+
if (onFinish)
|
61
|
+
onFinish(result.object);
|
62
|
+
return result.object;
|
63
|
+
}
|
64
|
+
catch (error) {
|
65
|
+
console.error("Error parsing schema:", error);
|
66
|
+
throw error;
|
58
67
|
}
|
59
|
-
if (onFinish)
|
60
|
-
onFinish(result.object);
|
61
|
-
return result.object;
|
62
68
|
}
|
63
69
|
async streamProcess(prompt, state, onFinish) {
|
64
70
|
console.log("\nšØ Starting streaming interpretation");
|
65
71
|
console.log("Prompt:", prompt);
|
66
|
-
const context = this.
|
72
|
+
const context = this.buildContext(state);
|
67
73
|
const result = await (0, ai_1.streamText)({
|
68
74
|
model: this.model,
|
69
75
|
onFinish: (event) => {
|
@@ -72,7 +78,8 @@ class Interpreter {
|
|
72
78
|
onFinish(event);
|
73
79
|
},
|
74
80
|
prompt,
|
75
|
-
system: context,
|
81
|
+
system: context.toString(),
|
82
|
+
temperature: 1.3,
|
76
83
|
});
|
77
84
|
return result;
|
78
85
|
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.memoryManagerInstructions = void 0;
|
4
|
+
exports.memoryManagerInstructions = {
|
5
|
+
role: "You are the memory curator. Your role is to extract and format memories from interactions.",
|
6
|
+
language: "user_request",
|
7
|
+
guidelines: {
|
8
|
+
important: [
|
9
|
+
"Generate memories based on the user request",
|
10
|
+
"Generate query for requested data as the user could ask for it later",
|
11
|
+
"Should be short-term memories only if it's ephemeral but relevant and reusable",
|
12
|
+
"Only store as long-term: User information, User preferences, Important facts that don't change often, Historical milestones",
|
13
|
+
"Make memory data concise and clear",
|
14
|
+
"Set appropriate TTL based on data volatility",
|
15
|
+
],
|
16
|
+
warnings: [
|
17
|
+
"Never store data that is not provided by the results",
|
18
|
+
"Never store data that is not relevant to the user request",
|
19
|
+
],
|
20
|
+
},
|
21
|
+
examplesMessages: [],
|
22
|
+
};
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { LanguageModelV1 } from "ai";
|
2
|
+
import { CacheMemory } from "../../memory/cache";
|
3
|
+
import { PersistentMemory } from "../../memory/persistent";
|
4
|
+
import { State } from "../orchestrator/types";
|
5
|
+
export declare class MemoryManager {
|
6
|
+
private readonly model;
|
7
|
+
private readonly memory?;
|
8
|
+
constructor(config: {
|
9
|
+
model: LanguageModelV1;
|
10
|
+
memory?: {
|
11
|
+
cache?: CacheMemory;
|
12
|
+
persistent?: PersistentMemory;
|
13
|
+
};
|
14
|
+
});
|
15
|
+
buildContext(state: State): string;
|
16
|
+
process(state: State, result: string): Promise<void>;
|
17
|
+
}
|
@@ -0,0 +1,107 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.MemoryManager = void 0;
|
4
|
+
const zod_1 = require("zod");
|
5
|
+
const types_1 = require("../../types");
|
6
|
+
const generate_object_1 = require("../../utils/generate-object");
|
7
|
+
const header_builder_1 = require("../../utils/header-builder");
|
8
|
+
const context_1 = require("./context");
|
9
|
+
class MemoryManager {
|
10
|
+
constructor(config) {
|
11
|
+
this.model = config.model;
|
12
|
+
this.memory = config.memory;
|
13
|
+
}
|
14
|
+
buildContext(state) {
|
15
|
+
const context = header_builder_1.LLMHeaderBuilder.create()
|
16
|
+
.addHeader("ROLE", context_1.memoryManagerInstructions.role)
|
17
|
+
.addHeader("LANGUAGE", context_1.memoryManagerInstructions.language)
|
18
|
+
.addHeader("IMPORTANT", context_1.memoryManagerInstructions.guidelines.important)
|
19
|
+
.addHeader("WARNINGS", context_1.memoryManagerInstructions.guidelines.warnings)
|
20
|
+
.addHeader("CURRENT_CONTEXT", state.currentContext)
|
21
|
+
.addHeader("RESULTS", JSON.stringify(state.results));
|
22
|
+
return context.toString();
|
23
|
+
}
|
24
|
+
async process(state, result) {
|
25
|
+
const context = this.buildContext(state);
|
26
|
+
const memories = await (0, generate_object_1.generateObject)({
|
27
|
+
model: this.model,
|
28
|
+
schema: zod_1.z.object({
|
29
|
+
memories: zod_1.z.array(zod_1.z.object({
|
30
|
+
data: zod_1.z.string(),
|
31
|
+
type: zod_1.z.enum(["short-term", "long-term"]),
|
32
|
+
category: zod_1.z.enum([
|
33
|
+
"user_information",
|
34
|
+
"user_preference",
|
35
|
+
"task",
|
36
|
+
"current_goal",
|
37
|
+
"news",
|
38
|
+
"fact",
|
39
|
+
"other",
|
40
|
+
]),
|
41
|
+
queryForData: zod_1.z.string(),
|
42
|
+
tags: zod_1.z.array(zod_1.z.string()),
|
43
|
+
ttl: zod_1.z.number(),
|
44
|
+
})),
|
45
|
+
}),
|
46
|
+
prompt: state.currentContext,
|
47
|
+
system: context.toString(),
|
48
|
+
temperature: 1,
|
49
|
+
});
|
50
|
+
console.log("Memories:", memories.object.memories);
|
51
|
+
if (!this.memory) {
|
52
|
+
return;
|
53
|
+
}
|
54
|
+
// Store memories after all processing is complete
|
55
|
+
await Promise.all([
|
56
|
+
// Store short-term memories in cache
|
57
|
+
...memories.object.memories
|
58
|
+
.filter((m) => m.type === "short-term")
|
59
|
+
.map(async (memoryItem) => {
|
60
|
+
if (!this.memory?.cache) {
|
61
|
+
return;
|
62
|
+
}
|
63
|
+
const existingCacheMemories = await this.memory.cache.findSimilarActions(memoryItem.data, {
|
64
|
+
similarityThreshold: 85,
|
65
|
+
maxResults: 3,
|
66
|
+
scope: types_1.MemoryScope.GLOBAL,
|
67
|
+
});
|
68
|
+
if (existingCacheMemories.length > 0) {
|
69
|
+
console.log("ā ļø Similar memory already exists in cache:", memoryItem.data);
|
70
|
+
return;
|
71
|
+
}
|
72
|
+
await this.memory.cache.createMemory({
|
73
|
+
query: memoryItem.queryForMemory,
|
74
|
+
data: memoryItem.data,
|
75
|
+
ttl: memoryItem.ttl, // Use TTL from LLM
|
76
|
+
});
|
77
|
+
console.log("ā
Memory stored in cache:", memoryItem.data);
|
78
|
+
}),
|
79
|
+
// Store long-term memories in persistent storage
|
80
|
+
...memories.object.memories
|
81
|
+
.filter((m) => m.type === "long-term")
|
82
|
+
.map(async (memoryItem) => {
|
83
|
+
if (!this.memory?.persistent) {
|
84
|
+
return;
|
85
|
+
}
|
86
|
+
const existingPersistentMemories = await this.memory.persistent.findRelevantDocuments(memoryItem.data, {
|
87
|
+
similarityThreshold: 85,
|
88
|
+
});
|
89
|
+
if (existingPersistentMemories.length > 0) {
|
90
|
+
console.log("ā ļø Similar memory already exists in persistent storage:", memoryItem.data);
|
91
|
+
return;
|
92
|
+
}
|
93
|
+
await this.memory.persistent.createMemory({
|
94
|
+
query: memoryItem.queryForMemory,
|
95
|
+
data: memoryItem.data,
|
96
|
+
category: memoryItem.category,
|
97
|
+
tags: memoryItem.tags,
|
98
|
+
roomId: "global",
|
99
|
+
createdAt: new Date(),
|
100
|
+
id: crypto.randomUUID(),
|
101
|
+
});
|
102
|
+
console.log("ā
Memory stored in persistent storage:", memoryItem);
|
103
|
+
}),
|
104
|
+
]);
|
105
|
+
}
|
106
|
+
}
|
107
|
+
exports.MemoryManager = MemoryManager;
|
@@ -1,10 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
language: string;
|
4
|
-
role: string;
|
5
|
-
guidelines: {
|
6
|
-
important: string[];
|
7
|
-
warnings: string[];
|
8
|
-
};
|
9
|
-
};
|
10
|
-
};
|
1
|
+
import { Character } from "../interpreter/context";
|
2
|
+
export declare const orchestratorInstructions: Character;
|
@@ -1,18 +1,23 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
4
|
-
exports.
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
3
|
+
exports.orchestratorInstructions = void 0;
|
4
|
+
exports.orchestratorInstructions = {
|
5
|
+
role: "You are the orchestrator. Your role is to evaluate the current state and determine next actions.",
|
6
|
+
language: "user_request",
|
7
|
+
guidelines: {
|
8
|
+
important: [
|
9
|
+
"Continue executing actions until ALL necessary goals are achieved",
|
10
|
+
"You can schedule actions in cron expression to be executed later (if needed)",
|
11
|
+
"Only stop when you have a complete picture of the goal",
|
12
|
+
"Social responses can be partial while gathering more data",
|
13
|
+
"Set shouldContinue to false if no more actions are needed",
|
14
|
+
"Once all actions are completed, choose the right interpreter to interpret the results",
|
15
|
+
],
|
16
|
+
warnings: [
|
17
|
+
"Never use a tool if it's not related to the user request",
|
18
|
+
"Never schedule actions that are not related to the user request",
|
19
|
+
"Never repeat the same action if it's not required to achieve the goal",
|
20
|
+
"Never repeat scheduled actions if not required to achieve the goal",
|
21
|
+
],
|
17
22
|
},
|
18
23
|
};
|
@@ -1,29 +1,44 @@
|
|
1
|
+
import { LanguageModelV1 } from "ai";
|
1
2
|
import { CacheMemory } from "../../memory/cache";
|
2
3
|
import { PersistentMemory } from "../../memory/persistent";
|
3
|
-
import {
|
4
|
-
|
4
|
+
import { CacheConfig } from "../../services/redis-cache";
|
5
|
+
import { ActionSchema, QueueCallbacks } from "../../types";
|
6
|
+
import { Interpreter } from "../interpreter";
|
7
|
+
import { State } from "./types";
|
8
|
+
export declare class AgentRuntime {
|
5
9
|
private readonly model;
|
6
|
-
tools
|
7
|
-
private
|
8
|
-
private
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
actions: {
|
10
|
+
private readonly tools;
|
11
|
+
private readonly interpreters;
|
12
|
+
private readonly queueManager;
|
13
|
+
private readonly scheduler;
|
14
|
+
private readonly cache;
|
15
|
+
private memory?;
|
16
|
+
constructor(model: LanguageModelV1, tools: ActionSchema[], interpreters: Interpreter[], redisConfig: CacheConfig, memory?: {
|
17
|
+
persistent?: PersistentMemory;
|
18
|
+
cache?: CacheMemory;
|
19
|
+
}, callbacks?: QueueCallbacks);
|
20
|
+
private buildContext;
|
21
|
+
process(state: State): Promise<{
|
22
|
+
shouldContinue: boolean;
|
23
|
+
actions: Array<{
|
20
24
|
name: string;
|
21
|
-
|
22
|
-
parameters: {
|
25
|
+
parameters: Array<{
|
23
26
|
name: string;
|
24
27
|
value: any;
|
25
|
-
}
|
26
|
-
|
27
|
-
|
28
|
+
}>;
|
29
|
+
scheduler?: {
|
30
|
+
isScheduled: boolean;
|
31
|
+
scheduledAtInC?: string;
|
32
|
+
interval?: string;
|
33
|
+
reason?: string;
|
34
|
+
};
|
35
|
+
}>;
|
36
|
+
socialResponse?: {
|
37
|
+
shouldRespond: boolean;
|
38
|
+
response?: string;
|
39
|
+
isPartialResponse?: boolean;
|
40
|
+
};
|
41
|
+
interpreter?: string;
|
42
|
+
results?: string;
|
28
43
|
}>;
|
29
44
|
}
|
@@ -1,105 +1,139 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
4
|
-
const openai_1 = require("@ai-sdk/openai");
|
5
|
-
const ai_1 = require("ai");
|
3
|
+
exports.AgentRuntime = void 0;
|
6
4
|
const zod_1 = require("zod");
|
5
|
+
const queue_1 = require("../../services/queue");
|
6
|
+
const redis_cache_1 = require("../../services/redis-cache");
|
7
|
+
const scheduler_1 = require("../../services/scheduler");
|
8
|
+
const types_1 = require("../../types");
|
9
|
+
const generate_object_1 = require("../../utils/generate-object");
|
10
|
+
const header_builder_1 = require("../../utils/header-builder");
|
7
11
|
const inject_actions_1 = require("../../utils/inject-actions");
|
8
12
|
const context_1 = require("./context");
|
9
|
-
class
|
10
|
-
constructor(
|
11
|
-
this.model =
|
12
|
-
this.
|
13
|
+
class AgentRuntime {
|
14
|
+
constructor(model, tools, interpreters, redisConfig, memory, callbacks) {
|
15
|
+
this.model = model;
|
16
|
+
this.tools = tools;
|
17
|
+
this.interpreters = interpreters;
|
18
|
+
this.queueManager = new queue_1.ActionQueueManager(tools, callbacks);
|
13
19
|
this.memory = memory;
|
14
|
-
this.
|
15
|
-
|
16
|
-
{
|
17
|
-
name: "search_internal_knowledge_base",
|
18
|
-
description: "Search for relevant information in the internal knowledge base",
|
19
|
-
parameters: zod_1.z.object({
|
20
|
-
query: zod_1.z.string(),
|
21
|
-
}),
|
22
|
-
execute: async ({ query }) => {
|
23
|
-
const persistentMemories = await this.memory.persistent.findRelevantDocuments(query, {
|
24
|
-
similarityThreshold: 70,
|
25
|
-
});
|
26
|
-
return `# LONG_TERM_MEMORY: ${JSON.stringify(persistentMemories)}`;
|
27
|
-
},
|
28
|
-
},
|
29
|
-
];
|
20
|
+
this.cache = new redis_cache_1.RedisCache(redisConfig);
|
21
|
+
this.scheduler = new scheduler_1.TaskScheduler(this, this.cache);
|
30
22
|
}
|
31
|
-
|
32
|
-
|
33
|
-
const
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
23
|
+
async buildContext(state) {
|
24
|
+
console.log("š§ Building context with RAG and CAG...");
|
25
|
+
const context = header_builder_1.LLMHeaderBuilder.create();
|
26
|
+
// Add orchestrator instructions
|
27
|
+
context.addHeader("ROLE", context_1.orchestratorInstructions.role);
|
28
|
+
context.addHeader("LANGUAGE", context_1.orchestratorInstructions.language);
|
29
|
+
context.addHeader("IMPORTANT", context_1.orchestratorInstructions.guidelines.important);
|
30
|
+
context.addHeader("WARNINGS", context_1.orchestratorInstructions.guidelines.warnings);
|
31
|
+
// Add tools to context
|
32
|
+
context.addHeader("TOOLS", (0, inject_actions_1.injectActions)(this.tools));
|
33
|
+
// Add previous actions if any
|
34
|
+
if (state.previousActions?.length) {
|
35
|
+
context.addHeader("PREVIOUS_ACTIONS", JSON.stringify(state.previousActions));
|
36
|
+
}
|
37
|
+
// Get recent similar actions (CAG)
|
38
|
+
if (this.memory?.cache) {
|
39
|
+
const cacheMemories = await this.memory.cache.findSimilarActions(state.currentContext, {
|
40
|
+
similarityThreshold: 80,
|
41
|
+
maxResults: 3,
|
42
|
+
scope: types_1.MemoryScope.GLOBAL,
|
43
|
+
});
|
44
|
+
if (cacheMemories.length > 0) {
|
45
|
+
context.addHeader("RECENT_ACTIONS", JSON.stringify(cacheMemories));
|
46
|
+
}
|
47
|
+
}
|
48
|
+
// Get relevant knowledge (RAG)
|
49
|
+
if (this.memory?.persistent) {
|
50
|
+
const persistentMemory = await this.memory.persistent.findRelevantDocuments(state.currentContext, {
|
51
|
+
similarityThreshold: 80,
|
52
|
+
});
|
53
|
+
if (persistentMemory.length > 0) {
|
54
|
+
context.addHeader("RELEVANT_KNOWLEDGE", JSON.stringify(persistentMemory));
|
55
|
+
}
|
56
|
+
}
|
57
|
+
// Add available interpreters
|
58
|
+
context.addHeader("AVAILABLE_INTERPRETERS", JSON.stringify(this.interpreters.map((i) => i.name)));
|
59
|
+
console.log("Context built with memories", context.toString());
|
60
|
+
return context.toString();
|
44
61
|
}
|
45
|
-
async process(
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
62
|
+
async process(state) {
|
63
|
+
console.log("š Processing state:");
|
64
|
+
console.dir(state, { depth: null });
|
65
|
+
if (state.previousActions?.length) {
|
66
|
+
console.log("š Previous actions:", state.previousActions
|
67
|
+
.map((a) => (typeof a === "string" ? a : a.name))
|
68
|
+
.join(", "));
|
69
|
+
}
|
70
|
+
const context = await this.buildContext(state);
|
71
|
+
console.log("\nš§ Generating response from LLM...");
|
72
|
+
const response = await (0, generate_object_1.generateObject)({
|
73
|
+
model: this.model,
|
74
|
+
schema: zod_1.z.object({
|
75
|
+
shouldContinue: zod_1.z.boolean(),
|
76
|
+
actions: zod_1.z.array(zod_1.z.object({
|
77
|
+
name: zod_1.z.string(),
|
78
|
+
parameters: zod_1.z.array(zod_1.z.object({
|
57
79
|
name: zod_1.z.string(),
|
58
|
-
|
59
|
-
parameters: zod_1.z.array(zod_1.z.object({
|
60
|
-
name: zod_1.z.string(),
|
61
|
-
value: zod_1.z.any(),
|
62
|
-
})),
|
80
|
+
value: zod_1.z.any(),
|
63
81
|
})),
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
...response.object,
|
72
|
-
actions: response.object.actions.map((action) => ({
|
73
|
-
...action,
|
74
|
-
parameters: Array.isArray(action.parameters)
|
75
|
-
? action.parameters.map((param) => ({
|
76
|
-
name: param.name,
|
77
|
-
value: param.value ?? null,
|
78
|
-
}))
|
79
|
-
: Object.entries(action.parameters || {}).map(([name, value]) => ({
|
80
|
-
name,
|
81
|
-
value: value ?? null,
|
82
|
-
})),
|
82
|
+
scheduler: zod_1.z
|
83
|
+
.object({
|
84
|
+
isScheduled: zod_1.z.boolean(),
|
85
|
+
cronExpression: zod_1.z.string().optional(),
|
86
|
+
reason: zod_1.z.string().optional(),
|
87
|
+
})
|
88
|
+
.optional(),
|
83
89
|
})),
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
90
|
+
socialResponse: zod_1.z
|
91
|
+
.object({
|
92
|
+
shouldRespond: zod_1.z.boolean(),
|
93
|
+
response: zod_1.z.string().optional(),
|
94
|
+
isPartialResponse: zod_1.z.boolean().optional(),
|
95
|
+
})
|
96
|
+
.optional(),
|
97
|
+
interpreter: zod_1.z.string().optional(),
|
98
|
+
}),
|
99
|
+
prompt: state.currentContext,
|
100
|
+
system: context.toString(),
|
101
|
+
temperature: 0,
|
102
|
+
});
|
103
|
+
console.log("š Orchestrator response:");
|
104
|
+
console.dir(response.object, { depth: null });
|
105
|
+
// Force shouldContinue to false if no actions are planned
|
106
|
+
if (response.object.actions.length === 0) {
|
107
|
+
response.object.shouldContinue = false;
|
108
|
+
console.log("ā ļø No actions planned, forcing shouldContinue to false");
|
109
|
+
}
|
110
|
+
// Handle social interactions and actions in a single block
|
111
|
+
if (response.object.socialResponse?.shouldRespond) {
|
112
|
+
console.log("\nš¬ Processing social response");
|
113
|
+
if (response.object.socialResponse.response) {
|
114
|
+
console.log("š¢ Response:", response.object.socialResponse.response);
|
115
|
+
// Ensure all parameters have a value property
|
92
116
|
}
|
93
|
-
return validatedResponse;
|
94
117
|
}
|
95
|
-
|
96
|
-
|
97
|
-
if (
|
98
|
-
|
99
|
-
|
118
|
+
// Handle scheduled actions
|
119
|
+
for (const action of response.object.actions) {
|
120
|
+
if (action.scheduler?.isScheduled) {
|
121
|
+
await this.scheduler.scheduleRequest({
|
122
|
+
originalRequest: state.currentContext,
|
123
|
+
cronExpression: action.scheduler.cronExpression,
|
124
|
+
});
|
100
125
|
}
|
101
|
-
throw error;
|
102
126
|
}
|
127
|
+
// Store actions in Redis cache
|
128
|
+
if (response.object.actions.length > 0) {
|
129
|
+
const requestId = crypto.randomUUID();
|
130
|
+
await this.cache.storePreviousActions(requestId, response.object.actions);
|
131
|
+
}
|
132
|
+
// Store message in recent messages
|
133
|
+
await this.cache.storeRecentMessage(state.currentContext, {
|
134
|
+
socialResponse: response.object.socialResponse,
|
135
|
+
});
|
136
|
+
return response.object;
|
103
137
|
}
|
104
138
|
}
|
105
|
-
exports.
|
139
|
+
exports.AgentRuntime = AgentRuntime;
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { QueueResult } from "../../types";
|
2
|
+
export interface State {
|
3
|
+
currentContext: string;
|
4
|
+
previousActions: (string | QueueResult)[];
|
5
|
+
reward?: number;
|
6
|
+
userRequest?: string;
|
7
|
+
results?: string;
|
8
|
+
}
|
9
|
+
export interface Action {
|
10
|
+
name: string;
|
11
|
+
parameters: Record<string, any>;
|
12
|
+
}
|