Skip to content

Commit

Permalink
fix memory save
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Nov 3, 2024
1 parent 2070f05 commit d4c170a
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 793 deletions.
4 changes: 2 additions & 2 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@types/fluent-ffmpeg": "2.1.27",
"@types/jest": "29.5.14",
"@types/node": "22.8.4",
"@types/pdfjs-dist": "^2.10.378",
"@types/sql.js": "1.4.9",
"@types/tar": "6.1.13",
"@types/wav-encoder": "1.3.3",
Expand All @@ -67,8 +68,7 @@
"ts-node": "10.9.2",
"tslib": "2.8.0",
"typescript": "5.6.3",
"wrangler": "3.84.0",
"@types/pdfjs-dist": "^2.10.378"
"wrangler": "3.84.0"
},
"pnpm": {
"overrides": {
Expand Down
3 changes: 2 additions & 1 deletion core/src/clients/direct/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,12 @@ class DirectClient {
inReplyTo: undefined,
};

const userMessage = { content, userId, roomId };
const userMessage = { content, userId, roomId, agentId: runtime.agentId };

const memory: Memory = {
id: messageId,
...userMessage,
agentId: runtime.agentId,
userId,
roomId,
content,
Expand Down
2 changes: 2 additions & 0 deletions core/src/clients/discord/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export class DiscordClient extends EventEmitter {
await this.runtime.messageManager.createMemory({
id: reactionUUID, // This is the ID of the reaction message
userId: userIdUUID,
agentId: this.runtime.agentId,
content: {
text: reactionMessage,
source: "discord",
Expand Down Expand Up @@ -261,6 +262,7 @@ export class DiscordClient extends EventEmitter {
await this.runtime.messageManager.createMemory({
id: reactionUUID, // This is the ID of the reaction removal message
userId: userIdUUID,
agentId: this.runtime.agentId,
content: {
text: reactionMessage,
source: "discord",
Expand Down
10 changes: 9 additions & 1 deletion core/src/clients/discord/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,12 @@ export class MessageManager {
: undefined,
};

const userMessage = { content, userId: userIdUUID, roomId };
const userMessage = {
content,
userId: userIdUUID,
agentId: this.runtime.agentId,
roomId,
};

let state = (await this.runtime.composeState(userMessage, {
discordClient: this.client,
Expand All @@ -185,6 +190,7 @@ export class MessageManager {
id: stringToUuid(message.id),
...userMessage,
userId: userIdUUID,
agentId: this.runtime.agentId,
roomId,
content,
createdAt: message.createdTimestamp,
Expand Down Expand Up @@ -277,6 +283,7 @@ export class MessageManager {
const memory: Memory = {
id: stringToUuid(message.id),
userId: this.runtime.agentId,
agentId: this.runtime.agentId,
content,
roomId,
embedding: embeddingZeroVector,
Expand Down Expand Up @@ -306,6 +313,7 @@ export class MessageManager {
const memory: Memory = {
id: stringToUuid(m.id),
userId: this.runtime.agentId,
agentId: this.runtime.agentId,
content: {
...content,
action,
Expand Down
3 changes: 3 additions & 0 deletions core/src/clients/discord/voice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ export class VoiceManager extends EventEmitter {

let state = await this.runtime.composeState(
{
agentId: this.runtime.agentId,
content: { text: text, source: "Discord" },
userId: userIdUUID,
roomId,
Expand All @@ -334,6 +335,7 @@ export class VoiceManager extends EventEmitter {
id: stringToUuid(
channelId + "-voice-message-" + Date.now()
),
agentId: this.runtime.agentId,
content: {
text: text,
source: "discord",
Expand Down Expand Up @@ -383,6 +385,7 @@ export class VoiceManager extends EventEmitter {
id: stringToUuid(
memory.id + "-voice-response-" + Date.now()
),
agentId: this.runtime.agentId,
userId: this.runtime.agentId,
content: {
...content,
Expand Down
4 changes: 3 additions & 1 deletion core/src/clients/telegram/src/messageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ export class MessageManager {
// Create memory for the message
const memory: Memory = {
id: messageId,
agentId,
userId,
roomId,
content,
Expand Down Expand Up @@ -333,7 +334,8 @@ export class MessageManager {

const memory: Memory = {
id: stringToUuid(sentMessage.message_id.toString()),
userId: agentId,
agentId,
userId,
roomId,
content: {
...content,
Expand Down
2 changes: 2 additions & 0 deletions core/src/clients/twitter/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ export class ClientBase extends EventEmitter {
id: stringToUuid(tweet.id),
userId: tweetuserId,
content: content,
agentId: this.runtime.agentId,
roomId,
embedding: embeddingZeroVector,
createdAt: tweet.timestamp * 1000,
Expand Down Expand Up @@ -539,6 +540,7 @@ export class ClientBase extends EventEmitter {
id: stringToUuid(tweet.id),
userId: tweetuserId,
content: content,
agentId: this.runtime.agentId,
roomId,
embedding: embeddingZeroVector,
createdAt: tweet.timestamp * 1000,
Expand Down
4 changes: 3 additions & 1 deletion core/src/clients/twitter/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class TwitterGenerationClient extends ClientBase {
this.generateNewTweet();
setTimeout(
generateNewTweetLoop,
(Math.floor(Math.random() * (45 - 15 + 1)) + 15) * 60 * 1000
(Math.floor(Math.random() * (20 - 2 + 1)) + 2) * 60 * 1000
); // Random interval between 4-8 hours
};
// setTimeout(() => {
Expand Down Expand Up @@ -84,6 +84,7 @@ export class TwitterGenerationClient extends ClientBase {
{
userId: this.runtime.agentId,
roomId: stringToUuid("twitter_generate_room"),
agentId: this.runtime.agentId,
content: { text: "", action: "" },
},
{
Expand Down Expand Up @@ -174,6 +175,7 @@ export class TwitterGenerationClient extends ClientBase {
await this.runtime.messageManager.createMemory({
id: stringToUuid(postId),
userId: this.runtime.agentId,
agentId: this.runtime.agentId,
content: {
text: newTweetContent.trim(),
url: tweet.permanentUrl,
Expand Down
2 changes: 2 additions & 0 deletions core/src/clients/twitter/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export class TwitterInteractionClient extends ClientBase {

const message = {
content: { text: tweet.text },
agentId: this.runtime.agentId,
userId: userIdUUID,
roomId,
};
Expand Down Expand Up @@ -248,6 +249,7 @@ export class TwitterInteractionClient extends ClientBase {

const message = {
id: tweetId,
agentId: this.runtime.agentId,
content: {
text: tweet.text,
url: tweet.permanentUrl,
Expand Down
14 changes: 9 additions & 5 deletions core/src/clients/twitter/search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { SearchMode } from "agent-twitter-client";
import fs from "fs";
import { addHeader, composeContext } from "../../core/context.ts";
import { composeContext } from "../../core/context.ts";
import {
generateMessageResponse,
generateText,
} from "../../core/generation.ts";
import { log_to_file } from "../../core/logger.ts";
import { messageCompletionFooter } from "../../core/parsing.ts";
import {
Expand All @@ -13,10 +17,6 @@ import {
import { stringToUuid } from "../../core/uuid.ts";
import { ClientBase } from "./base.ts";
import { buildConversationThread, sendTweetChunks, wait } from "./utils.ts";
import {
generateText,
generateMessageResponse,
} from "../../core/generation.ts";

const messageHandlerTemplate =
`{{relevantFacts}}
Expand All @@ -41,6 +41,9 @@ About {{agentName}} (@{{twitterUserName}}):
# 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.
{{currentPost}}
IMPORTANT: Your response CANNOT be longer than 20 words.
Aim for 1-2 short sentences maximum. Be concise and direct.
Your response should not contain any questions. Brief, concise statements only. No emojis. Use \\n\\n (double spaces) between statements.
` + messageCompletionFooter;
Expand Down Expand Up @@ -196,6 +199,7 @@ export class TwitterSearchClient extends ClientBase {

const message = {
id: stringToUuid(selectedTweet.id),
agentId: this.runtime.agentId,
content: {
text: selectedTweet.text,
url: selectedTweet.permanentUrl,
Expand Down
3 changes: 2 additions & 1 deletion core/src/clients/twitter/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { stringToUuid } from "../../core/uuid.ts";
import { ClientBase } from "./base.ts";
import { prettyConsole } from "../../index.ts";

const MAX_TWEET_LENGTH = 280;
const MAX_TWEET_LENGTH = 240;

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

client.runtime.messageManager.createMemory({
id: stringToUuid(currentTweet.id),
agentId: client.runtime.agentId,
content: {
text: currentTweet.text,
source: "twitter",
Expand Down
85 changes: 43 additions & 42 deletions core/src/core/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,48 +328,49 @@ export class AgentRuntime implements IAgentRuntime {
for (const knowledgeItem of knowledge) {
// TODO: Fix the knowledge???
continue;
const knowledgeId = stringToUuid(knowledgeItem);
const existingDocument =
await this.documentsManager.getMemoryById(knowledgeId);
if (!existingDocument) {
console.log(
"Processing knowledge for ",
this.character.name,
" - ",
knowledgeItem.slice(0, 100)
);
await this.documentsManager.createMemory({
embedding: embeddingZeroVector,
id: knowledgeId,
roomId: this.agentId,
userId: this.agentId,
createdAt: Date.now(),
content: {
text: knowledgeItem,
},
});
const fragments = await splitChunks(
this,
knowledgeItem,
1200,
200,
"fast"
);
for (const fragment of fragments) {
const embedding = await embed(this, fragment);
await this.fragmentsManager.createMemory({
id: stringToUuid(fragment),
roomId: this.agentId,
userId: this.agentId,
createdAt: Date.now(),
content: {
source: knowledgeId,
text: fragment,
},
embedding,
});
}
}
// const knowledgeId = stringToUuid(knowledgeItem);
// const existingDocument =
// await this.documentsManager.getMemoryById(knowledgeId);
// if (!existingDocument) {
// console.log(
// "Processing knowledge for ",
// this.character.name,
// " - ",
// knowledgeItem.slice(0, 100)
// );
// await this.documentsManager.createMemory({
// embedding: embeddingZeroVector,
// id: knowledgeId,
// agentId: this.agentId,
// roomId: this.agentId,
// userId: this.agentId,
// createdAt: Date.now(),
// content: {
// text: knowledgeItem,
// },
// });
// const fragments = await splitChunks(
// this,
// knowledgeItem,
// 1200,
// 200,
// "fast"
// );
// for (const fragment of fragments) {
// const embedding = await embed(this, fragment);
// await this.fragmentsManager.createMemory({
// id: stringToUuid(fragment),
// roomId: this.agentId,
// userId: this.agentId,
// createdAt: Date.now(),
// content: {
// source: knowledgeId,
// text: fragment,
// },
// embedding,
// });
// }
// }
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export interface State {
export interface Memory {
id?: UUID; // An optional unique identifier for the memory.
userId: UUID; // The user ID associated with the memory.
agentId?: UUID; // The agent ID associated with the memory.
agentId: UUID; // The agent ID associated with the memory.
createdAt?: number; // An optional timestamp indicating when the memory was created.
content: Content; // The content of the memory, which can be a structured object or a plain string.
embedding?: number[]; // An optional embedding vector representing the semantic content of the memory.
Expand Down
1 change: 1 addition & 0 deletions core/src/evaluators/fact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ async function handler(runtime: IAgentRuntime, message: Memory) {
for (const fact of filteredFacts) {
const factMemory = await runtime.factManager.addEmbeddingToMemory({
userId: agentId!,
agentId,
content: { text: fact },
roomId,
createdAt: Date.now(),
Expand Down
1 change: 1 addition & 0 deletions core/src/test_resources/populateMemories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function populateMemories(
const bakedMemory =
await runtime.messageManager.addEmbeddingToMemory({
userId: c.userId as UUID,
agentId: runtime.agentId,
content: c.content,
roomId,
embedding: existingEmbedding,
Expand Down
Loading

0 comments on commit d4c170a

Please sign in to comment.