@ai.ntellect/core 0.3.1 → 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 +204 -185
- package/agent/tools/get-rss.ts +64 -0
- package/bull.ts +5 -0
- package/dist/agent/index.d.ts +29 -22
- package/dist/agent/index.js +124 -97
- 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 -16
- 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 -6
- package/dist/memory/cache.js +35 -42
- package/dist/memory/persistent.d.ts +9 -13
- package/dist/memory/persistent.js +94 -114
- package/dist/services/redis-cache.d.ts +37 -0
- package/dist/services/redis-cache.js +93 -0
- package/dist/services/scheduler.d.ts +40 -0
- package/dist/services/scheduler.js +99 -0
- package/dist/services/telegram-monitor.d.ts +0 -0
- package/dist/services/telegram-monitor.js +118 -0
- package/dist/test.d.ts +0 -167
- package/dist/test.js +437 -372
- package/dist/types.d.ts +60 -9
- 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 +41 -55
- package/memory/persistent.ts +121 -149
- package/package.json +11 -2
- package/services/redis-cache.ts +128 -0
- package/services/scheduler.ts +128 -0
- package/services/telegram-monitor.ts +138 -0
- package/t.py +79 -0
- package/t.spec +38 -0
- package/types.ts +64 -9
- 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 -22
- package/dist/llm/evaluator/index.d.ts +0 -16
- package/dist/llm/evaluator/index.js +0 -151
- package/llm/evaluator/context.ts +0 -21
- package/llm/evaluator/index.ts +0 -194
@@ -0,0 +1,21 @@
|
|
1
|
+
import { Character } from "../interpreter/context";
|
2
|
+
|
3
|
+
export const memoryManagerInstructions: Character = {
|
4
|
+
role: "You are the memory curator. Your role is to extract and format memories from interactions.",
|
5
|
+
language: "user_request",
|
6
|
+
guidelines: {
|
7
|
+
important: [
|
8
|
+
"Generate memories based on the user request",
|
9
|
+
"Generate query for requested data as the user could ask for it later",
|
10
|
+
"Should be short-term memories only if it's ephemeral but relevant and reusable",
|
11
|
+
"Only store as long-term: User information, User preferences, Important facts that don't change often, Historical milestones",
|
12
|
+
"Make memory data concise and clear",
|
13
|
+
"Set appropriate TTL based on data volatility",
|
14
|
+
],
|
15
|
+
warnings: [
|
16
|
+
"Never store data that is not provided by the results",
|
17
|
+
"Never store data that is not relevant to the user request",
|
18
|
+
],
|
19
|
+
},
|
20
|
+
examplesMessages: [],
|
21
|
+
};
|
@@ -0,0 +1,163 @@
|
|
1
|
+
import { LanguageModelV1 } from "ai";
|
2
|
+
import { z } from "zod";
|
3
|
+
import { CacheMemory } from "../../memory/cache";
|
4
|
+
import { PersistentMemory } from "../../memory/persistent";
|
5
|
+
import { MemoryScope } from "../../types";
|
6
|
+
import { generateObject } from "../../utils/generate-object";
|
7
|
+
import { LLMHeaderBuilder } from "../../utils/header-builder";
|
8
|
+
import { State } from "../orchestrator/types";
|
9
|
+
import { memoryManagerInstructions } from "./context";
|
10
|
+
|
11
|
+
interface MemoryResponse {
|
12
|
+
memories: Array<{
|
13
|
+
data: string;
|
14
|
+
type: "short-term" | "long-term";
|
15
|
+
category:
|
16
|
+
| "user_information"
|
17
|
+
| "user_preference"
|
18
|
+
| "task"
|
19
|
+
| "current_goal"
|
20
|
+
| "news"
|
21
|
+
| "fact"
|
22
|
+
| "other";
|
23
|
+
queryForMemory: string;
|
24
|
+
tags: string[];
|
25
|
+
ttl: number;
|
26
|
+
}>;
|
27
|
+
}
|
28
|
+
export class MemoryManager {
|
29
|
+
private readonly model: LanguageModelV1;
|
30
|
+
private readonly memory?: {
|
31
|
+
cache?: CacheMemory;
|
32
|
+
persistent?: PersistentMemory;
|
33
|
+
};
|
34
|
+
|
35
|
+
constructor(config: {
|
36
|
+
model: LanguageModelV1;
|
37
|
+
memory?: {
|
38
|
+
cache?: CacheMemory;
|
39
|
+
persistent?: PersistentMemory;
|
40
|
+
};
|
41
|
+
}) {
|
42
|
+
this.model = config.model;
|
43
|
+
this.memory = config.memory;
|
44
|
+
}
|
45
|
+
|
46
|
+
buildContext(state: State) {
|
47
|
+
const context = LLMHeaderBuilder.create()
|
48
|
+
.addHeader("ROLE", memoryManagerInstructions.role)
|
49
|
+
.addHeader("LANGUAGE", memoryManagerInstructions.language)
|
50
|
+
.addHeader("IMPORTANT", memoryManagerInstructions.guidelines.important)
|
51
|
+
.addHeader("WARNINGS", memoryManagerInstructions.guidelines.warnings)
|
52
|
+
.addHeader("CURRENT_CONTEXT", state.currentContext)
|
53
|
+
.addHeader("RESULTS", JSON.stringify(state.results));
|
54
|
+
return context.toString();
|
55
|
+
}
|
56
|
+
|
57
|
+
async process(state: State, result: string) {
|
58
|
+
const context = this.buildContext(state);
|
59
|
+
|
60
|
+
const memories = await generateObject<MemoryResponse>({
|
61
|
+
model: this.model,
|
62
|
+
schema: z.object({
|
63
|
+
memories: z.array(
|
64
|
+
z.object({
|
65
|
+
data: z.string(),
|
66
|
+
type: z.enum(["short-term", "long-term"]),
|
67
|
+
category: z.enum([
|
68
|
+
"user_information",
|
69
|
+
"user_preference",
|
70
|
+
"task",
|
71
|
+
"current_goal",
|
72
|
+
"news",
|
73
|
+
"fact",
|
74
|
+
"other",
|
75
|
+
]),
|
76
|
+
queryForData: z.string(),
|
77
|
+
tags: z.array(z.string()),
|
78
|
+
ttl: z.number(),
|
79
|
+
})
|
80
|
+
),
|
81
|
+
}),
|
82
|
+
prompt: state.currentContext,
|
83
|
+
system: context.toString(),
|
84
|
+
temperature: 1,
|
85
|
+
});
|
86
|
+
|
87
|
+
console.log("Memories:", memories.object.memories);
|
88
|
+
|
89
|
+
if (!this.memory) {
|
90
|
+
return;
|
91
|
+
}
|
92
|
+
|
93
|
+
// Store memories after all processing is complete
|
94
|
+
await Promise.all([
|
95
|
+
// Store short-term memories in cache
|
96
|
+
...memories.object.memories
|
97
|
+
.filter((m: any) => m.type === "short-term")
|
98
|
+
.map(async (memoryItem: any) => {
|
99
|
+
if (!this.memory?.cache) {
|
100
|
+
return;
|
101
|
+
}
|
102
|
+
|
103
|
+
const existingCacheMemories =
|
104
|
+
await this.memory.cache.findSimilarActions(memoryItem.data, {
|
105
|
+
similarityThreshold: 85,
|
106
|
+
maxResults: 3,
|
107
|
+
scope: MemoryScope.GLOBAL,
|
108
|
+
});
|
109
|
+
|
110
|
+
if (existingCacheMemories.length > 0) {
|
111
|
+
console.log(
|
112
|
+
"⚠️ Similar memory already exists in cache:",
|
113
|
+
memoryItem.data
|
114
|
+
);
|
115
|
+
return;
|
116
|
+
}
|
117
|
+
|
118
|
+
await this.memory.cache.createMemory({
|
119
|
+
query: memoryItem.queryForMemory,
|
120
|
+
data: memoryItem.data,
|
121
|
+
ttl: memoryItem.ttl, // Use TTL from LLM
|
122
|
+
});
|
123
|
+
console.log("✅ Memory stored in cache:", memoryItem.data);
|
124
|
+
}),
|
125
|
+
|
126
|
+
// Store long-term memories in persistent storage
|
127
|
+
...memories.object.memories
|
128
|
+
.filter((m) => m.type === "long-term")
|
129
|
+
.map(async (memoryItem) => {
|
130
|
+
if (!this.memory?.persistent) {
|
131
|
+
return;
|
132
|
+
}
|
133
|
+
|
134
|
+
const existingPersistentMemories =
|
135
|
+
await this.memory.persistent.findRelevantDocuments(
|
136
|
+
memoryItem.data,
|
137
|
+
{
|
138
|
+
similarityThreshold: 85,
|
139
|
+
}
|
140
|
+
);
|
141
|
+
|
142
|
+
if (existingPersistentMemories.length > 0) {
|
143
|
+
console.log(
|
144
|
+
"⚠️ Similar memory already exists in persistent storage:",
|
145
|
+
memoryItem.data
|
146
|
+
);
|
147
|
+
return;
|
148
|
+
}
|
149
|
+
|
150
|
+
await this.memory.persistent.createMemory({
|
151
|
+
query: memoryItem.queryForMemory,
|
152
|
+
data: memoryItem.data,
|
153
|
+
category: memoryItem.category,
|
154
|
+
tags: memoryItem.tags,
|
155
|
+
roomId: "global",
|
156
|
+
createdAt: new Date(),
|
157
|
+
id: crypto.randomUUID(),
|
158
|
+
});
|
159
|
+
console.log("✅ Memory stored in persistent storage:", memoryItem);
|
160
|
+
}),
|
161
|
+
]);
|
162
|
+
}
|
163
|
+
}
|
@@ -1,15 +1,22 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
import { Character } from "../interpreter/context";
|
2
|
+
|
3
|
+
export const orchestratorInstructions: Character = {
|
4
|
+
role: "You are the orchestrator. Your role is to evaluate the current state and determine next actions.",
|
5
|
+
language: "user_request",
|
6
|
+
guidelines: {
|
7
|
+
important: [
|
8
|
+
"Continue executing actions until ALL necessary goals are achieved",
|
9
|
+
"You can schedule actions in cron expression to be executed later (if needed)",
|
10
|
+
"Only stop when you have a complete picture of the goal",
|
11
|
+
"Social responses can be partial while gathering more data",
|
12
|
+
"Set shouldContinue to false if no more actions are needed",
|
13
|
+
"Once all actions are completed, choose the right interpreter to interpret the results",
|
14
|
+
],
|
15
|
+
warnings: [
|
16
|
+
"Never use a tool if it's not related to the user request",
|
17
|
+
"Never schedule actions that are not related to the user request",
|
18
|
+
"Never repeat the same action if it's not required to achieve the goal",
|
19
|
+
"Never repeat scheduled actions if not required to achieve the goal",
|
20
|
+
],
|
14
21
|
},
|
15
22
|
};
|
@@ -1,152 +1,232 @@
|
|
1
|
-
import {
|
2
|
-
import { generateObject } from "ai";
|
1
|
+
import { LanguageModelV1 } from "ai";
|
3
2
|
import { z } from "zod";
|
4
3
|
import { CacheMemory } from "../../memory/cache";
|
5
4
|
import { PersistentMemory } from "../../memory/persistent";
|
6
|
-
import {
|
5
|
+
import { ActionQueueManager } from "../../services/queue";
|
6
|
+
import { CacheConfig, RedisCache } from "../../services/redis-cache";
|
7
|
+
import { TaskScheduler } from "../../services/scheduler";
|
8
|
+
import {
|
9
|
+
ActionSchema,
|
10
|
+
GenerateObjectResponse,
|
11
|
+
MemoryScope,
|
12
|
+
QueueCallbacks,
|
13
|
+
} from "../../types";
|
14
|
+
import { generateObject } from "../../utils/generate-object";
|
15
|
+
import { LLMHeaderBuilder } from "../../utils/header-builder";
|
7
16
|
import { injectActions } from "../../utils/inject-actions";
|
8
|
-
import {
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
private
|
14
|
-
|
15
|
-
|
17
|
+
import { Interpreter } from "../interpreter";
|
18
|
+
import { orchestratorInstructions } from "./context";
|
19
|
+
import { State } from "./types";
|
20
|
+
|
21
|
+
export class AgentRuntime {
|
22
|
+
private readonly model: LanguageModelV1;
|
23
|
+
private readonly tools: ActionSchema[];
|
24
|
+
private readonly interpreters: Interpreter[];
|
25
|
+
private readonly queueManager: ActionQueueManager;
|
26
|
+
private readonly scheduler: TaskScheduler;
|
27
|
+
private readonly cache: RedisCache;
|
28
|
+
private memory?: {
|
29
|
+
persistent?: PersistentMemory;
|
30
|
+
cache?: CacheMemory;
|
16
31
|
};
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
this.
|
32
|
+
|
33
|
+
constructor(
|
34
|
+
model: LanguageModelV1,
|
35
|
+
tools: ActionSchema[],
|
36
|
+
interpreters: Interpreter[],
|
37
|
+
redisConfig: CacheConfig,
|
38
|
+
memory?: {
|
39
|
+
persistent?: PersistentMemory;
|
40
|
+
cache?: CacheMemory;
|
41
|
+
},
|
42
|
+
callbacks?: QueueCallbacks
|
43
|
+
) {
|
44
|
+
this.model = model;
|
45
|
+
this.tools = tools;
|
46
|
+
this.interpreters = interpreters;
|
47
|
+
this.queueManager = new ActionQueueManager(tools, callbacks);
|
32
48
|
this.memory = memory;
|
33
|
-
this.
|
34
|
-
|
35
|
-
{
|
36
|
-
name: "search_internal_knowledge_base",
|
37
|
-
description:
|
38
|
-
"Search for relevant information in the internal knowledge base",
|
39
|
-
parameters: z.object({
|
40
|
-
query: z.string(),
|
41
|
-
}),
|
42
|
-
execute: async ({ query }: { query: string }) => {
|
43
|
-
const persistentMemories =
|
44
|
-
await this.memory.persistent.findRelevantDocuments(query, {
|
45
|
-
similarityThreshold: 70,
|
46
|
-
});
|
47
|
-
return `# LONG_TERM_MEMORY: ${JSON.stringify(persistentMemories)}`;
|
48
|
-
},
|
49
|
-
},
|
50
|
-
];
|
49
|
+
this.cache = new RedisCache(redisConfig);
|
50
|
+
this.scheduler = new TaskScheduler(this, this.cache);
|
51
51
|
}
|
52
52
|
|
53
|
-
|
54
|
-
|
55
|
-
const
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
53
|
+
private async buildContext(state: State): Promise<string> {
|
54
|
+
console.log("🧠 Building context with RAG and CAG...");
|
55
|
+
const context = LLMHeaderBuilder.create();
|
56
|
+
|
57
|
+
// Add orchestrator instructions
|
58
|
+
context.addHeader("ROLE", orchestratorInstructions.role);
|
59
|
+
context.addHeader("LANGUAGE", orchestratorInstructions.language);
|
60
|
+
context.addHeader(
|
61
|
+
"IMPORTANT",
|
62
|
+
orchestratorInstructions.guidelines.important
|
63
|
+
);
|
64
|
+
context.addHeader("WARNINGS", orchestratorInstructions.guidelines.warnings);
|
65
|
+
// Add tools to context
|
66
|
+
context.addHeader("TOOLS", injectActions(this.tools));
|
67
|
+
|
68
|
+
// Add previous actions if any
|
69
|
+
if (state.previousActions?.length) {
|
70
|
+
context.addHeader(
|
71
|
+
"PREVIOUS_ACTIONS",
|
72
|
+
JSON.stringify(state.previousActions)
|
73
|
+
);
|
74
|
+
}
|
75
|
+
|
76
|
+
// Get recent similar actions (CAG)
|
77
|
+
if (this.memory?.cache) {
|
78
|
+
const cacheMemories = await this.memory.cache.findSimilarActions(
|
79
|
+
state.currentContext,
|
80
|
+
{
|
81
|
+
similarityThreshold: 80,
|
82
|
+
maxResults: 3,
|
83
|
+
scope: MemoryScope.GLOBAL,
|
84
|
+
}
|
85
|
+
);
|
86
|
+
|
87
|
+
if (cacheMemories.length > 0) {
|
88
|
+
context.addHeader("RECENT_ACTIONS", JSON.stringify(cacheMemories));
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
// Get relevant knowledge (RAG)
|
93
|
+
if (this.memory?.persistent) {
|
94
|
+
const persistentMemory =
|
95
|
+
await this.memory.persistent.findRelevantDocuments(
|
96
|
+
state.currentContext,
|
97
|
+
{
|
98
|
+
similarityThreshold: 80,
|
99
|
+
}
|
100
|
+
);
|
101
|
+
|
102
|
+
if (persistentMemory.length > 0) {
|
103
|
+
context.addHeader(
|
104
|
+
"RELEVANT_KNOWLEDGE",
|
105
|
+
JSON.stringify(persistentMemory)
|
106
|
+
);
|
107
|
+
}
|
108
|
+
}
|
109
|
+
|
110
|
+
// Add available interpreters
|
111
|
+
context.addHeader(
|
112
|
+
"AVAILABLE_INTERPRETERS",
|
113
|
+
JSON.stringify(this.interpreters.map((i) => i.name))
|
114
|
+
);
|
115
|
+
console.log("Context built with memories", context.toString());
|
116
|
+
return context.toString();
|
68
117
|
}
|
69
118
|
|
70
|
-
async process(
|
71
|
-
|
72
|
-
|
73
|
-
): Promise<{
|
74
|
-
actions: {
|
119
|
+
async process(state: State): Promise<{
|
120
|
+
shouldContinue: boolean;
|
121
|
+
actions: Array<{
|
75
122
|
name: string;
|
76
|
-
|
77
|
-
parameters: {
|
123
|
+
parameters: Array<{
|
78
124
|
name: string;
|
79
125
|
value: any;
|
80
|
-
}
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
results: results,
|
87
|
-
});
|
88
|
-
try {
|
89
|
-
console.log("\n🎭 Orchestrator processing");
|
90
|
-
console.log("Prompt:", prompt);
|
91
|
-
|
92
|
-
const response = await generateObject({
|
93
|
-
model: this.model,
|
94
|
-
schema: z.object({
|
95
|
-
actions: z.array(
|
96
|
-
z.object({
|
97
|
-
name: z.string(),
|
98
|
-
type: z.enum(["on-chain", "off-chain", "question", "analysis"]),
|
99
|
-
parameters: z.array(
|
100
|
-
z.object({
|
101
|
-
name: z.string(),
|
102
|
-
value: z.any(),
|
103
|
-
})
|
104
|
-
),
|
105
|
-
})
|
106
|
-
),
|
107
|
-
answer: z.string(),
|
108
|
-
}),
|
109
|
-
prompt: prompt,
|
110
|
-
system: state,
|
111
|
-
temperature: 0,
|
112
|
-
});
|
113
|
-
|
114
|
-
const validatedResponse = {
|
115
|
-
...response.object,
|
116
|
-
actions: response.object.actions.map((action) => ({
|
117
|
-
...action,
|
118
|
-
parameters: Array.isArray(action.parameters)
|
119
|
-
? action.parameters.map((param) => ({
|
120
|
-
name: param.name,
|
121
|
-
value: param.value ?? null,
|
122
|
-
}))
|
123
|
-
: Object.entries(action.parameters || {}).map(([name, value]) => ({
|
124
|
-
name,
|
125
|
-
value: value ?? null,
|
126
|
-
})),
|
127
|
-
})),
|
126
|
+
}>;
|
127
|
+
scheduler?: {
|
128
|
+
isScheduled: boolean;
|
129
|
+
scheduledAtInC?: string;
|
130
|
+
interval?: string;
|
131
|
+
reason?: string;
|
128
132
|
};
|
129
|
-
|
130
|
-
|
131
|
-
|
133
|
+
}>;
|
134
|
+
socialResponse?: {
|
135
|
+
shouldRespond: boolean;
|
136
|
+
response?: string;
|
137
|
+
isPartialResponse?: boolean;
|
138
|
+
};
|
139
|
+
interpreter?: string;
|
140
|
+
results?: string;
|
141
|
+
}> {
|
142
|
+
console.log("🔄 Processing state:");
|
143
|
+
console.dir(state, { depth: null });
|
144
|
+
if (state.previousActions?.length) {
|
132
145
|
console.log(
|
133
|
-
"
|
134
|
-
|
135
|
-
|
136
|
-
|
146
|
+
"📊 Previous actions:",
|
147
|
+
state.previousActions
|
148
|
+
.map((a) => (typeof a === "string" ? a : a.name))
|
149
|
+
.join(", ")
|
137
150
|
);
|
138
|
-
|
139
|
-
|
151
|
+
}
|
152
|
+
|
153
|
+
const context = await this.buildContext(state);
|
154
|
+
|
155
|
+
console.log("\n🧠 Generating response from LLM...");
|
156
|
+
const response = await generateObject<GenerateObjectResponse>({
|
157
|
+
model: this.model,
|
158
|
+
schema: z.object({
|
159
|
+
shouldContinue: z.boolean(),
|
160
|
+
actions: z.array(
|
161
|
+
z.object({
|
162
|
+
name: z.string(),
|
163
|
+
parameters: z.array(
|
164
|
+
z.object({
|
165
|
+
name: z.string(),
|
166
|
+
value: z.any(),
|
167
|
+
})
|
168
|
+
),
|
169
|
+
scheduler: z
|
170
|
+
.object({
|
171
|
+
isScheduled: z.boolean(),
|
172
|
+
cronExpression: z.string().optional(),
|
173
|
+
reason: z.string().optional(),
|
174
|
+
})
|
175
|
+
.optional(),
|
176
|
+
})
|
177
|
+
),
|
178
|
+
socialResponse: z
|
179
|
+
.object({
|
180
|
+
shouldRespond: z.boolean(),
|
181
|
+
response: z.string().optional(),
|
182
|
+
isPartialResponse: z.boolean().optional(),
|
183
|
+
})
|
184
|
+
.optional(),
|
185
|
+
interpreter: z.string().optional(),
|
186
|
+
}),
|
187
|
+
prompt: state.currentContext,
|
188
|
+
system: context.toString(),
|
189
|
+
temperature: 0,
|
190
|
+
});
|
191
|
+
console.log("🔄 Orchestrator response:");
|
192
|
+
console.dir(response.object, { depth: null });
|
193
|
+
|
194
|
+
// Force shouldContinue to false if no actions are planned
|
195
|
+
if (response.object.actions.length === 0) {
|
196
|
+
response.object.shouldContinue = false;
|
197
|
+
console.log("⚠️ No actions planned, forcing shouldContinue to false");
|
198
|
+
}
|
199
|
+
|
200
|
+
// Handle social interactions and actions in a single block
|
201
|
+
if (response.object.socialResponse?.shouldRespond) {
|
202
|
+
console.log("\n💬 Processing social response");
|
203
|
+
if (response.object.socialResponse.response) {
|
204
|
+
console.log("📢 Response:", response.object.socialResponse.response);
|
205
|
+
// Ensure all parameters have a value property
|
140
206
|
}
|
207
|
+
}
|
141
208
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
209
|
+
// Handle scheduled actions
|
210
|
+
for (const action of response.object.actions) {
|
211
|
+
if (action.scheduler?.isScheduled) {
|
212
|
+
await this.scheduler.scheduleRequest({
|
213
|
+
originalRequest: state.currentContext,
|
214
|
+
cronExpression: action.scheduler.cronExpression,
|
215
|
+
});
|
148
216
|
}
|
149
|
-
throw error;
|
150
217
|
}
|
218
|
+
|
219
|
+
// Store actions in Redis cache
|
220
|
+
if (response.object.actions.length > 0) {
|
221
|
+
const requestId = crypto.randomUUID();
|
222
|
+
await this.cache.storePreviousActions(requestId, response.object.actions);
|
223
|
+
}
|
224
|
+
|
225
|
+
// Store message in recent messages
|
226
|
+
await this.cache.storeRecentMessage(state.currentContext, {
|
227
|
+
socialResponse: response.object.socialResponse,
|
228
|
+
});
|
229
|
+
|
230
|
+
return response.object;
|
151
231
|
}
|
152
232
|
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { QueueResult } from "../../types";
|
2
|
+
|
3
|
+
export interface State {
|
4
|
+
currentContext: string;
|
5
|
+
previousActions: (string | QueueResult)[];
|
6
|
+
reward?: number;
|
7
|
+
userRequest?: string;
|
8
|
+
results?: string;
|
9
|
+
}
|
10
|
+
|
11
|
+
export interface Action {
|
12
|
+
name: string;
|
13
|
+
parameters: Record<string, any>;
|
14
|
+
}
|