Skip to content

Conversation

@ysolanky
Copy link
Contributor

Summary

Refactor and cleanup of Memory, Agent and Team classes

(If applicable, issue number: #____)

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Improvement
  • Model update
  • Other:

Checklist

  • Code complies with style guidelines
  • Ran format/validation scripts (./scripts/format.sh and ./scripts/validate.sh)
  • Self-review completed
  • Documentation updated (comments, docstrings)
  • Examples and guides: Relevant cookbook examples have been included or updated (if applicable)
  • Tested in clean environment
  • Tests added/updated (if applicable)

Additional Notes

Add any important context (deployment instructions, screenshots, security considerations, etc.)

Comment on lines +596 to +597
if self.memory is None:
self.memory = Memory()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would this still be needed? Before this was the only source of runs, so it was needed, but now I don't think it is needed by default?

self.db = db

# We are making memories
def __post_init__(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets rather only instantiate on runtime?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be instantiated on runtime, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought post-init runs after the init? When you create the object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry yes - though the post init is only valid for the cases where user sets a model on the Memory. So for most use cases, the logic that is inside the post init only occurs at runtime. I think this pattern works, wdyt?

Comment on lines +121 to +133
def read_from_db(self, user_id: Optional[str] = None):
if self.db:
# If no user_id is provided, read all memories
if user_id is None:
all_memories = self.db.get_user_memories()
else:
all_memories = self.db.get_user_memories(user_id=user_id)

# Reset the memories
self.memories = {}
memories = {}
for memory in all_memories:
if memory.user_id is not None and memory.id is not None:
self.memories.setdefault(memory.user_id, {})[memory.id] = UserMemory.from_dict(memory.memory)
if memory.user_id is not None and memory.memory_id is not None:
memories.setdefault(memory.user_id, {})[memory.memory_id] = memory.memory
return memories
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All uses of this method now pass user_id to filter by.

Do you think it's still useful to return a dictionary ({<user_id>: <memories>}) vs just a flat list? I think we will always have only one item in the dict

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There can be a provision to view all memories at once, right? I am thinking of a use case when the user wants to see all the Db level memories

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... the flat list could handle that case and still make reads now simpler. But I don't think this is too important, we can keep it as is if you're happy!


gen_session_name_prompt = "Conversation\n"
messages_for_generating_session_name = self.memory.get_messages_for_session(session_id=session_id)
messages_for_generating_session_name = self.agent_session.get_messages_for_session(session_id=session_id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not now but in a next iteration we should try and pass the session around as opposed to having it set on the agent. In an effort to make things stateless

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is interesting


class MemoryRow(BaseModel):
"""Memory Row that is stored in the database"""
class SummaryRow(BaseModel):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this can also get a better name now? Just SessionSummary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed SummaryRow as it was not needed, we just use the SessionSummary object

@ysolanky ysolanky marked this pull request as ready for review July 17, 2025 04:07
@ysolanky ysolanky requested a review from a team as a code owner July 17, 2025 04:07
# Else read from the db
if self.memory is not None and self.memory.db is not None:
return self.memory.read_chat_history(session_id=self.session_id, session_type="agent")
if self.agent_session is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the interest of removing state we might have to only do this on the DB object

@manuhortet manuhortet merged commit 83adc2d into v2.0 Jul 17, 2025
1 check passed
@manuhortet manuhortet deleted the refactor/memory_update_10 branch July 17, 2025 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants