Skip to content

Commit

Permalink
fix(agents): remove user messages in bee runner memory (#183)
Browse files Browse the repository at this point in the history
Signed-off-by: Radek Ježek <[email protected]>
  • Loading branch information
jezekra1 authored Nov 19, 2024
1 parent d82be6e commit a1a3d0e
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/agents/bee/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { JSONParserField, ZodParserField } from "@/agents/parsers/field.js";
import { z } from "zod";
import { Serializable } from "@/internals/serializable.js";
import { shallowCopy } from "@/serializer/utils.js";
import { isEmpty } from "remeda";
import { isEmpty, last } from "remeda";

export class BeeAgentRunnerFatalError extends BeeAgentError {
isFatal = true;
Expand Down Expand Up @@ -93,6 +93,22 @@ export class BeeAgentRunner extends Serializable {
return message;
};

const inputMemoryTransformed = [
...input.memory.messages,
...(prompt !== null || input.memory.isEmpty()
? [
BaseMessage.of({
role: Role.USER,
text: prompt ?? "",
meta: {
// TODO: createdAt
createdAt: new Date(),
},
}),
]
: []),
].map(transformMessage);

const memory = new TokenMemory({
llm: input.llm,
capacityThreshold: 0.85,
Expand All @@ -101,9 +117,9 @@ export class BeeAgentRunner extends Serializable {
removalSelector(curMessages) {
// First we remove messages from the past conversations
const prevConversationMessage = curMessages.find((msg) =>
input.memory.messages.includes(msg),
inputMemoryTransformed.includes(msg),
);
if (prevConversationMessage) {
if (prevConversationMessage && prevConversationMessage !== last(inputMemoryTransformed)) {
return prevConversationMessage;
}

Expand Down Expand Up @@ -151,24 +167,9 @@ export class BeeAgentRunner extends Serializable {
createdAt: new Date(),
},
}),
...input.memory.messages.map(transformMessage),
...inputMemoryTransformed,
]);

if (prompt !== null || input.memory.isEmpty()) {
await memory.add(
transformMessage(
BaseMessage.of({
role: Role.USER,
text: prompt ?? "",
meta: {
// TODO: createdAt
createdAt: new Date(),
},
}),
),
);
}

return new this(input, options, memory);
}

Expand Down

0 comments on commit a1a3d0e

Please sign in to comment.