Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getMemoryByIds to database adapters #2293

Merged
merged 2 commits into from
Jan 15, 2025

Conversation

bbopar
Copy link

@bbopar bbopar commented Jan 14, 2025

Relates to

#1186 - Add getMemoriesByIds

Risks

Low - Added new feature and tested (non-breaking change which adds functionality)

Background

What does this PR do?

Adds getMemoriesByIds method to all database adapters (pglite, postgres, sqlite, sqljs and supabase) to support batch memory retrieval. This enables fetching multiple memories by their IDs in a single query, with optional filtering by table name.

What kind of change is this?

Features (non-breaking change which adds functionality)

Documentation changes needed?

Documentation update is needed but there's a current issue with typedoc generation:

  • Error: "Hook useDocsSidebar is called outside the "
  • Affects API documentation build
  • Using @elizaos/core v0.1.8+build.1
  • Node v23.3.0

This will need to be addressed in a separate PR to fix the documentation build process.

Testing

Where should a reviewer start?

  1. Review the implementation of getMemoriesByIds in each database adapter:
    • pglite implementation
    • postgres implementation
    • sqlite implementation
    • sqljs implementation
    • supabase implementation
  2. For testing, I've created separated branch: 1186-add-get-memories-by-ids-test where you can test the impl for all adapters.

Detailed testing steps

  1. Clone and checkout the test branch:
    git clone https://github.com/elizaOS/eliza.git
    cd eliza
    git checkout 1186-add-get-memories-by-ids-test
    

branch for testing: https://github.com/bbopar/eliza/tree/1186-add-get-memories-by-ids-test

  1. Choose and set up the database adapter you want to test (pglite/postgres/sqlite/sqljs/supabase)

  2. Test cases (using temporary API endpoint for validation):

Test Case 1: Basic memory retrieval with table filter

curl -X POST http://localhost:3000/agents/bed6bfd7-be02-074a-9474-52354571ee7c/memories/batch \
  -H "Content-Type: application/json" \
  -d '{
    "memoryIds": [
      "dddabf84-30ff-00f9-b7e5-e3dd3db5f4d5",
      "4517881e-058e-0f3d-afb3-92e7e3461cca",
      "2942ba68-1513-024c-baae-eac424a16b0f"
    ],
    "tableName": "documents"
  }'

Expected output:

image

Test Case 2: Invalid table name handling

curl -X POST http://localhost:3000/agents/bed6bfd7-be02-074a-9474-52354571ee7c/memories/batch \
-H "Content-Type: application/json" \
-d '{
  "memoryIds": [
    "dddabf84-30ff-00f9-b7e5-e3dd3db5f4d5",
    "4517881e-058e-0f3d-afb3-92e7e3461cca"
  ],
  "tableName": "invalid_table"
}'

Expected output:

image

Test Case 3: Empty memory IDs array

curl -X POST http://localhost:3000/agents/bed6bfd7-be02-074a-9474-52354571ee7c/memories/batch \
-H "Content-Type: application/json" \
-d '{
  "memoryIds": [],
  "tableName": "documents"
}'

Expected output:

image

Test Case 4: Missing table name (no type filter)

curl -X POST http://localhost:3000/agents/bed6bfd7-be02-074a-9474-52354571ee7c/memories/batch \
-H "Content-Type: application/json" \
-d '{
  "memoryIds": [
    "dddabf84-30ff-00f9-b7e5-e3dd3db5f4d5",
    "4517881e-058e-0f3d-afb3-92e7e3461cca"
  ]
}'

Expected output:

image

Test Case 5: Large batch retrieval

curl -X POST http://localhost:3000/agents/bed6bfd7-be02-074a-9474-52354571ee7c/memories/batch \
-H "Content-Type: application/json" \
-d '{
  "memoryIds": [
    "dddabf84-30ff-00f9-b7e5-e3dd3db5f4d5",
    "4517881e-058e-0f3d-afb3-92e7e3461cca",
    "2942ba68-1513-024c-baae-eac424a16b0f",
    "0d3ad1ac-f322-03ff-88f5-14784ac098ea",
    "134c90e2-92e4-05e6-80c1-cea061663586",
    "640c3d8e-cbf6-0787-8c32-4ae86404ce75"
  ],
  "tableName": "documents"
}'

Expected output:

image

Discord username: .0xbbjoker

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Hi @bbopar! Welcome to the elizaOS community. Thanks for submitting your first pull request; your efforts are helping us accelerate towards AGI. We'll review it shortly. You are now an elizaOS contributor!

@wtfsayo
Copy link
Member

wtfsayo commented Jan 15, 2025

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Jan 15, 2025

📝 Walkthrough

Walkthrough

A new method getMemoriesByIds has been consistently added across multiple database adapter implementations (PGLite, Postgres, SQLite, SQLjs, Supabase) and the core database types. This method allows retrieving multiple memories by their IDs, with an optional table name parameter for type filtering. The implementation follows a similar pattern across different database adapters, enhancing the system's flexibility in memory retrieval.

Changes

File Change Summary
packages/core/src/types.ts Added getMemoriesByIds method to IDatabaseAdapter interface; updated method signatures for IMemoryManager and IIrysService interfaces
packages/core/src/database.ts Added abstract getMemoriesByIds method to DatabaseAdapter class
packages/core/__tests__/database.test.ts Added getMemoriesByIds method to MockDatabaseAdapter
packages/adapter-*/src/index.ts Added getMemoriesByIds method to database-specific adapters (PGLite, Postgres, SQLite, SQLjs, Supabase)

Finishing Touches

  • 📝 Generate Docstrings (Beta)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
packages/core/__tests__/database.test.ts (1)

18-29: Enhance mock implementation to utilize tableName parameter.

The mock implementation currently ignores the tableName parameter. Consider filtering memories based on the provided tableName to better simulate real database behavior.

 async getMemoriesByIds(
     memoryIds: UUID[],
-    _tableName?: string
+    tableName?: string
 ): Promise<Memory[]> {
-    return memoryIds.map((id) => ({
+    let memories = memoryIds.map((id) => ({
         id: id,
         content: { text: "Test Memory" },
         roomId: "room-id" as UUID,
         userId: "user-id" as UUID,
         agentId: "agent-id" as UUID,
+        type: "default-type"
     })) as Memory[];
+    return tableName ? memories.filter(m => m.type === tableName) : memories;
 }
packages/adapter-sqlite/src/index.ts (1)

207-232: Strong implementation with minor type improvement needed.

The implementation is robust with proper SQL injection prevention and data type handling. Consider using a more specific type for queryParams.

-const queryParams: any[] = [];
+const queryParams: (UUID | string)[] = [];
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 89b6a19 and e5403ec.

📒 Files selected for processing (8)
  • packages/adapter-pglite/src/index.ts (1 hunks)
  • packages/adapter-postgres/src/index.ts (1 hunks)
  • packages/adapter-sqlite/src/index.ts (1 hunks)
  • packages/adapter-sqljs/src/index.ts (1 hunks)
  • packages/adapter-supabase/src/index.ts (1 hunks)
  • packages/core/__tests__/database.test.ts (1 hunks)
  • packages/core/src/database.ts (2 hunks)
  • packages/core/src/types.ts (4 hunks)
👮 Files not reviewed due to content moderation or server errors (4)
  • packages/adapter-sqljs/src/index.ts
  • packages/core/src/types.ts
  • packages/adapter-pglite/src/index.ts
  • packages/adapter-postgres/src/index.ts
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: smoke-tests
  • GitHub Check: integration-tests
🔇 Additional comments (1)
packages/core/src/database.ts (1)

103-112: LGTM! Well-documented abstract method.

The method signature and documentation clearly define the contract for retrieving multiple memories by their IDs.

packages/adapter-supabase/src/index.ts Show resolved Hide resolved
@wtfsayo wtfsayo enabled auto-merge January 15, 2025 05:32
@wtfsayo wtfsayo self-requested a review January 15, 2025 05:32
@wtfsayo wtfsayo merged commit 4d42de6 into elizaOS:develop Jan 15, 2025
5 checks passed
This was referenced Jan 19, 2025
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.

2 participants