Skip to content

Commit d4c170a

Browse files
committedNov 3, 2024
fix memory save
1 parent 2070f05 commit d4c170a

File tree

20 files changed

+104
-793
lines changed

20 files changed

+104
-793
lines changed
 

‎core/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@types/fluent-ffmpeg": "2.1.27",
4949
"@types/jest": "29.5.14",
5050
"@types/node": "22.8.4",
51+
"@types/pdfjs-dist": "^2.10.378",
5152
"@types/sql.js": "1.4.9",
5253
"@types/tar": "6.1.13",
5354
"@types/wav-encoder": "1.3.3",
@@ -67,8 +68,7 @@
6768
"ts-node": "10.9.2",
6869
"tslib": "2.8.0",
6970
"typescript": "5.6.3",
70-
"wrangler": "3.84.0",
71-
"@types/pdfjs-dist": "^2.10.378"
71+
"wrangler": "3.84.0"
7272
},
7373
"pnpm": {
7474
"overrides": {

‎core/src/clients/direct/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,12 @@ class DirectClient {
162162
inReplyTo: undefined,
163163
};
164164

165-
const userMessage = { content, userId, roomId };
165+
const userMessage = { content, userId, roomId, agentId: runtime.agentId };
166166

167167
const memory: Memory = {
168168
id: messageId,
169169
...userMessage,
170+
agentId: runtime.agentId,
170171
userId,
171172
roomId,
172173
content,

‎core/src/clients/discord/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ export class DiscordClient extends EventEmitter {
196196
await this.runtime.messageManager.createMemory({
197197
id: reactionUUID, // This is the ID of the reaction message
198198
userId: userIdUUID,
199+
agentId: this.runtime.agentId,
199200
content: {
200201
text: reactionMessage,
201202
source: "discord",
@@ -261,6 +262,7 @@ export class DiscordClient extends EventEmitter {
261262
await this.runtime.messageManager.createMemory({
262263
id: reactionUUID, // This is the ID of the reaction removal message
263264
userId: userIdUUID,
265+
agentId: this.runtime.agentId,
264266
content: {
265267
text: reactionMessage,
266268
source: "discord",

‎core/src/clients/discord/messages.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,12 @@ export class MessageManager {
171171
: undefined,
172172
};
173173

174-
const userMessage = { content, userId: userIdUUID, roomId };
174+
const userMessage = {
175+
content,
176+
userId: userIdUUID,
177+
agentId: this.runtime.agentId,
178+
roomId,
179+
};
175180

176181
let state = (await this.runtime.composeState(userMessage, {
177182
discordClient: this.client,
@@ -185,6 +190,7 @@ export class MessageManager {
185190
id: stringToUuid(message.id),
186191
...userMessage,
187192
userId: userIdUUID,
193+
agentId: this.runtime.agentId,
188194
roomId,
189195
content,
190196
createdAt: message.createdTimestamp,
@@ -277,6 +283,7 @@ export class MessageManager {
277283
const memory: Memory = {
278284
id: stringToUuid(message.id),
279285
userId: this.runtime.agentId,
286+
agentId: this.runtime.agentId,
280287
content,
281288
roomId,
282289
embedding: embeddingZeroVector,
@@ -306,6 +313,7 @@ export class MessageManager {
306313
const memory: Memory = {
307314
id: stringToUuid(m.id),
308315
userId: this.runtime.agentId,
316+
agentId: this.runtime.agentId,
309317
content: {
310318
...content,
311319
action,

‎core/src/clients/discord/voice.ts

+3
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ export class VoiceManager extends EventEmitter {
314314

315315
let state = await this.runtime.composeState(
316316
{
317+
agentId: this.runtime.agentId,
317318
content: { text: text, source: "Discord" },
318319
userId: userIdUUID,
319320
roomId,
@@ -334,6 +335,7 @@ export class VoiceManager extends EventEmitter {
334335
id: stringToUuid(
335336
channelId + "-voice-message-" + Date.now()
336337
),
338+
agentId: this.runtime.agentId,
337339
content: {
338340
text: text,
339341
source: "discord",
@@ -383,6 +385,7 @@ export class VoiceManager extends EventEmitter {
383385
id: stringToUuid(
384386
memory.id + "-voice-response-" + Date.now()
385387
),
388+
agentId: this.runtime.agentId,
386389
userId: this.runtime.agentId,
387390
content: {
388391
...content,

‎core/src/clients/telegram/src/messageManager.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ export class MessageManager {
285285
// Create memory for the message
286286
const memory: Memory = {
287287
id: messageId,
288+
agentId,
288289
userId,
289290
roomId,
290291
content,
@@ -333,7 +334,8 @@ export class MessageManager {
333334

334335
const memory: Memory = {
335336
id: stringToUuid(sentMessage.message_id.toString()),
336-
userId: agentId,
337+
agentId,
338+
userId,
337339
roomId,
338340
content: {
339341
...content,

‎core/src/clients/twitter/base.ts

+2
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ export class ClientBase extends EventEmitter {
448448
id: stringToUuid(tweet.id),
449449
userId: tweetuserId,
450450
content: content,
451+
agentId: this.runtime.agentId,
451452
roomId,
452453
embedding: embeddingZeroVector,
453454
createdAt: tweet.timestamp * 1000,
@@ -539,6 +540,7 @@ export class ClientBase extends EventEmitter {
539540
id: stringToUuid(tweet.id),
540541
userId: tweetuserId,
541542
content: content,
543+
agentId: this.runtime.agentId,
542544
roomId,
543545
embedding: embeddingZeroVector,
544546
createdAt: tweet.timestamp * 1000,

‎core/src/clients/twitter/generate.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class TwitterGenerationClient extends ClientBase {
3131
this.generateNewTweet();
3232
setTimeout(
3333
generateNewTweetLoop,
34-
(Math.floor(Math.random() * (45 - 15 + 1)) + 15) * 60 * 1000
34+
(Math.floor(Math.random() * (20 - 2 + 1)) + 2) * 60 * 1000
3535
); // Random interval between 4-8 hours
3636
};
3737
// setTimeout(() => {
@@ -84,6 +84,7 @@ export class TwitterGenerationClient extends ClientBase {
8484
{
8585
userId: this.runtime.agentId,
8686
roomId: stringToUuid("twitter_generate_room"),
87+
agentId: this.runtime.agentId,
8788
content: { text: "", action: "" },
8889
},
8990
{
@@ -174,6 +175,7 @@ export class TwitterGenerationClient extends ClientBase {
174175
await this.runtime.messageManager.createMemory({
175176
id: stringToUuid(postId),
176177
userId: this.runtime.agentId,
178+
agentId: this.runtime.agentId,
177179
content: {
178180
text: newTweetContent.trim(),
179181
url: tweet.permanentUrl,

‎core/src/clients/twitter/interactions.ts

+2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export class TwitterInteractionClient extends ClientBase {
135135

136136
const message = {
137137
content: { text: tweet.text },
138+
agentId: this.runtime.agentId,
138139
userId: userIdUUID,
139140
roomId,
140141
};
@@ -248,6 +249,7 @@ export class TwitterInteractionClient extends ClientBase {
248249

249250
const message = {
250251
id: tweetId,
252+
agentId: this.runtime.agentId,
251253
content: {
252254
text: tweet.text,
253255
url: tweet.permanentUrl,

‎core/src/clients/twitter/search.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { SearchMode } from "agent-twitter-client";
22
import fs from "fs";
3-
import { addHeader, composeContext } from "../../core/context.ts";
3+
import { composeContext } from "../../core/context.ts";
4+
import {
5+
generateMessageResponse,
6+
generateText,
7+
} from "../../core/generation.ts";
48
import { log_to_file } from "../../core/logger.ts";
59
import { messageCompletionFooter } from "../../core/parsing.ts";
610
import {
@@ -13,10 +17,6 @@ import {
1317
import { stringToUuid } from "../../core/uuid.ts";
1418
import { ClientBase } from "./base.ts";
1519
import { buildConversationThread, sendTweetChunks, wait } from "./utils.ts";
16-
import {
17-
generateText,
18-
generateMessageResponse,
19-
} from "../../core/generation.ts";
2020

2121
const messageHandlerTemplate =
2222
`{{relevantFacts}}
@@ -41,6 +41,9 @@ About {{agentName}} (@{{twitterUserName}}):
4141
# Task: Respond to the following post in the style and perspective of {{agentName}} (aka @{{twitterUserName}}). Write a {{adjective}} response for {{agentName}} to say directly in response to the post. don't generalize.
4242
{{currentPost}}
4343
44+
IMPORTANT: Your response CANNOT be longer than 20 words.
45+
Aim for 1-2 short sentences maximum. Be concise and direct.
46+
4447
Your response should not contain any questions. Brief, concise statements only. No emojis. Use \\n\\n (double spaces) between statements.
4548
4649
` + messageCompletionFooter;
@@ -196,6 +199,7 @@ export class TwitterSearchClient extends ClientBase {
196199

197200
const message = {
198201
id: stringToUuid(selectedTweet.id),
202+
agentId: this.runtime.agentId,
199203
content: {
200204
text: selectedTweet.text,
201205
url: selectedTweet.permanentUrl,

‎core/src/clients/twitter/utils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { stringToUuid } from "../../core/uuid.ts";
55
import { ClientBase } from "./base.ts";
66
import { prettyConsole } from "../../index.ts";
77

8-
const MAX_TWEET_LENGTH = 280;
8+
const MAX_TWEET_LENGTH = 240;
99

1010
export const wait = (minTime: number = 1000, maxTime: number = 3000) => {
1111
const waitTime =
@@ -59,6 +59,7 @@ export async function buildConversationThread(
5959

6060
client.runtime.messageManager.createMemory({
6161
id: stringToUuid(currentTweet.id),
62+
agentId: client.runtime.agentId,
6263
content: {
6364
text: currentTweet.text,
6465
source: "twitter",

‎core/src/core/runtime.ts

+43-42
Original file line numberDiff line numberDiff line change
@@ -328,48 +328,49 @@ export class AgentRuntime implements IAgentRuntime {
328328
for (const knowledgeItem of knowledge) {
329329
// TODO: Fix the knowledge???
330330
continue;
331-
const knowledgeId = stringToUuid(knowledgeItem);
332-
const existingDocument =
333-
await this.documentsManager.getMemoryById(knowledgeId);
334-
if (!existingDocument) {
335-
console.log(
336-
"Processing knowledge for ",
337-
this.character.name,
338-
" - ",
339-
knowledgeItem.slice(0, 100)
340-
);
341-
await this.documentsManager.createMemory({
342-
embedding: embeddingZeroVector,
343-
id: knowledgeId,
344-
roomId: this.agentId,
345-
userId: this.agentId,
346-
createdAt: Date.now(),
347-
content: {
348-
text: knowledgeItem,
349-
},
350-
});
351-
const fragments = await splitChunks(
352-
this,
353-
knowledgeItem,
354-
1200,
355-
200,
356-
"fast"
357-
);
358-
for (const fragment of fragments) {
359-
const embedding = await embed(this, fragment);
360-
await this.fragmentsManager.createMemory({
361-
id: stringToUuid(fragment),
362-
roomId: this.agentId,
363-
userId: this.agentId,
364-
createdAt: Date.now(),
365-
content: {
366-
source: knowledgeId,
367-
text: fragment,
368-
},
369-
embedding,
370-
});
371-
}
372-
}
331+
// const knowledgeId = stringToUuid(knowledgeItem);
332+
// const existingDocument =
333+
// await this.documentsManager.getMemoryById(knowledgeId);
334+
// if (!existingDocument) {
335+
// console.log(
336+
// "Processing knowledge for ",
337+
// this.character.name,
338+
// " - ",
339+
// knowledgeItem.slice(0, 100)
340+
// );
341+
// await this.documentsManager.createMemory({
342+
// embedding: embeddingZeroVector,
343+
// id: knowledgeId,
344+
// agentId: this.agentId,
345+
// roomId: this.agentId,
346+
// userId: this.agentId,
347+
// createdAt: Date.now(),
348+
// content: {
349+
// text: knowledgeItem,
350+
// },
351+
// });
352+
// const fragments = await splitChunks(
353+
// this,
354+
// knowledgeItem,
355+
// 1200,
356+
// 200,
357+
// "fast"
358+
// );
359+
// for (const fragment of fragments) {
360+
// const embedding = await embed(this, fragment);
361+
// await this.fragmentsManager.createMemory({
362+
// id: stringToUuid(fragment),
363+
// roomId: this.agentId,
364+
// userId: this.agentId,
365+
// createdAt: Date.now(),
366+
// content: {
367+
// source: knowledgeId,
368+
// text: fragment,
369+
// },
370+
// embedding,
371+
// });
372+
// }
373+
// }
373374
}
374375
}
375376

‎core/src/core/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export interface State {
148148
export interface Memory {
149149
id?: UUID; // An optional unique identifier for the memory.
150150
userId: UUID; // The user ID associated with the memory.
151-
agentId?: UUID; // The agent ID associated with the memory.
151+
agentId: UUID; // The agent ID associated with the memory.
152152
createdAt?: number; // An optional timestamp indicating when the memory was created.
153153
content: Content; // The content of the memory, which can be a structured object or a plain string.
154154
embedding?: number[]; // An optional embedding vector representing the semantic content of the memory.

‎core/src/evaluators/fact.ts

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ async function handler(runtime: IAgentRuntime, message: Memory) {
9090
for (const fact of filteredFacts) {
9191
const factMemory = await runtime.factManager.addEmbeddingToMemory({
9292
userId: agentId!,
93+
agentId,
9394
content: { text: fact },
9495
roomId,
9596
createdAt: Date.now(),

‎core/src/test_resources/populateMemories.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export async function populateMemories(
1616
const bakedMemory =
1717
await runtime.messageManager.addEmbeddingToMemory({
1818
userId: c.userId as UUID,
19+
agentId: runtime.agentId,
1920
content: c.content,
2021
roomId,
2122
embedding: existingEmbedding,

‎core/tests/actions.test.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ async function handleMessage(
3232
const _senderContent = (senderContent as Content).text?.trim();
3333
if (_senderContent) {
3434
await runtime.messageManager.createMemory({
35-
id: stringToUuid(message.id),
35+
id: stringToUuid(
36+
message.id ?? userId + runtime.agentId + Date.now()
37+
),
3638
userId: userId!,
39+
agentId: runtime.agentId,
3740
content: {
3841
text: _senderContent,
3942
action: (message.content as Content)?.action ?? "null",
@@ -84,6 +87,7 @@ async function handleMessage(
8487
if (responseContent.text) {
8588
const response = {
8689
userId: runtime.agentId,
90+
agentId: runtime.agentId,
8791
content: responseContent,
8892
roomId,
8993
embedding: embeddingZeroVector,
@@ -115,7 +119,7 @@ describe("Actions", () => {
115119
});
116120

117121
user = session.user!;
118-
runtime = _runtime;
122+
runtime = _runtime as IAgentRuntime;
119123

120124
// check if the user id exists in the 'accounts' table
121125
// if it doesn't, create it with the name 'Test User'
@@ -184,6 +188,7 @@ describe("Actions", () => {
184188
if (testAction && testAction.validate) {
185189
const isValid = await testAction.validate(runtime, {
186190
userId: user.id as UUID,
191+
agentId: runtime.agentId,
187192
content: { text: "Test message" },
188193
roomId: roomId,
189194
});
@@ -197,6 +202,7 @@ describe("Actions", () => {
197202

198203
test("Test that actions are properly validated in state", async () => {
199204
const message: Memory = {
205+
agentId: runtime.agentId,
200206
userId: user.id as UUID,
201207
content: {
202208
text: "Please respond with the message 'ok' and the action TEST_ACTION",
@@ -214,6 +220,7 @@ describe("Actions", () => {
214220
test("Validate that TEST_ACTION is in the state", async () => {
215221
await runAiTest("Validate TEST_ACTION is in the state", async () => {
216222
const message: Memory = {
223+
agentId: runtime.agentId,
217224
userId: user.id as UUID,
218225
content: {
219226
text: "Please respond with the message 'ok' and the action TEST_ACTION",
@@ -241,6 +248,7 @@ describe("Actions", () => {
241248

242249
const mockMessage: Memory = {
243250
userId: user.id as UUID,
251+
agentId: runtime.agentId,
244252
content: {
245253
text: "Test message for TEST action",
246254
},

‎pnpm-lock.yaml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/clients/twitter/generate.ts

-205
This file was deleted.

‎src/clients/twitter/search.ts

-358
This file was deleted.

‎src/clients/twitter/utils.ts

-173
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.