Skip to content

migrated to cloud hosted mem0 - #234

Closed
cmd-err wants to merge 1 commit into
juspay:releasefrom
cmd-err:BZ-45257-cloud-hosted-mem-0-support
Closed

cmd-err wants to merge 1 commit into
juspay:releasefrom
cmd-err:BZ-45257-cloud-hosted-mem-0-support

Conversation

@cmd-err

@cmd-err cmd-err commented Nov 11, 2025 •

Copy link
Copy Markdown
Contributor

Pull Request

Description

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🧹 Code refactoring (no functional changes)
  • ⚡ Performance improvement
  • 🧪 Test coverage improvement
  • 🔧 Build/CI configuration change

Related Issues

  • Fixes #
  • Related to #

Changes Made

AI Provider Impact

  • OpenAI
  • Anthropic
  • Google AI/Vertex
  • AWS Bedrock
  • Azure OpenAI
  • Hugging Face
  • Ollama
  • Mistral
  • All providers
  • No provider-specific changes

Component Impact

  • CLI
  • SDK
  • MCP Integration
  • Streaming
  • Tool Calling
  • Configuration
  • Documentation
  • Tests

Testing

  • Unit tests added/updated
  • Integration tests added/updated
  • E2E tests added/updated
  • Manual testing performed
  • All existing tests pass

Test Environment

  • OS:
  • Node.js version:
  • Package manager:

Performance Impact

  • No performance impact
  • Performance improvement
  • Minor performance impact (acceptable)
  • Significant performance impact (needs discussion)

Breaking Changes

Screenshots/Demo

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

Additional Notes

Summary by CodeRabbit

Release Notes

  • New Features

    • Mem0 cloud API integration for enhanced memory management
    • New MCP server management capabilities for in-memory server handling
    • Expanded memory retrieval with enriched metadata, timestamps, and relevance scoring
  • Improvements

    • Simplified memory configuration using API key-based setup
    • Enhanced memory search with additional filtering and context options
    • Improved resource cleanup and lifecycle management for memory operations

Copilot AI review requested due to automatic review settings November 11, 2025 10:23
@coderabbitai

coderabbitai Bot commented Nov 11, 2025 •

Copy link
Copy Markdown

Walkthrough

This PR migrates Mem0 integration from local open-source setup to cloud-based API. It introduces Mem0Config (apiKey, optional orgId, projectId) and updates Mem0Memory interface methods. NeuroLink dependencies shift to cloud MemoryClient, memory operations adjust field naming (userId to user_id) and payload shapes, and the test script validates cloud-based flows.

Changes

Cohort / File(s) Summary
Mem0 Cloud API Initialization
src/lib/memory/mem0Initializer.ts
Replaces OSS Memory with MemoryClient cloud API; introduces Mem0Config interface (apiKey, orgId, projectId); updates initializeMem0 signature to accept Mem0Config and return Mem0Memory | null; adjusts fallback memory implementation with new method shapes and getAll support.
Dependency Management
package.json
Pins mem0ai dependency from ^2.1.38 to 2.1.38 (removes caret version constraint).
Type System Updates
src/lib/types/conversation.ts
Changes ConversationMemoryConfig.mem0Config type from MemoryConfig (mem0ai/oss) to Mem0Config (from mem0Initializer); updates comment to reflect cloud API integration.
Mem0Memory Interface Refactor
src/lib/types/utilities.ts
Comprehensive Mem0Memory API redesign: search/add/get/update signatures updated with cloud-compatible parameter/return shapes; removes history/reset methods; adds getAll method; introduces extended metadata fields and options (user_id, agent_id, app_id, run_id, infer, async_mode).
NeuroLink Memory Integration
src/lib/neurolink.ts
Adds static import of initializeMem0 and Mem0Config; updates mem0Config field from MemoryConfig to Mem0Config; adjusts memory search/add calls to use user_id field and cloud API payload shapes; enhances conversation turn storage format and adds MCP server management methods (addInMemoryMCPServer, getInMemoryServers, getInMemoryServerInfos, getAutoDiscoveredServerInfos).
Cloud Integration Test Suite
scripts/examples/real-memory-test.js
Rewrites test script to exercise Mem0 cloud API flows; simplifies config to apiKey-only setup; updates user context identifiers (deepak_123→deepak_1234, bob_456→bob_4561); replaces local service checks with cloud API verification; adds cleanupResources function for proper shutdown.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application
    participant NL as NeuroLink
    participant Init as initializeMem0
    participant MC as MemoryClient (Cloud)
    participant FB as Fallback Memory

    App->>NL: initialize(config with mem0Config)
    NL->>Init: initializeMem0(mem0Config)
    Init->>MC: new MemoryClient(apiKey, org_id, project_id)
    alt Cloud API Available
        MC-->>Init: client ready
        Init-->>NL: Mem0Memory instance
    else Cloud API Unavailable
        Init->>FB: createFallbackMemory()
        FB-->>Init: Mem0Memory fallback
        Init-->>NL: Mem0Memory instance (fallback)
    end
    NL-->>App: NeuroLink initialized
Loading
sequenceDiagram
    participant App as Application
    participant NL as NeuroLink
    participant Mem0 as Mem0Memory
    participant MC as MemoryClient

    App->>NL: generate(prompt, messages)
    NL->>Mem0: search(query, {user_id, limit, ...})
    Mem0->>MC: API call
    MC-->>Mem0: Memory[] with metadata
    Mem0-->>NL: formatted memory context
    NL->>NL: assemble prompt with context
    
    NL->>Mem0: add(messages[], {user_id, infer, async_mode})
    Note over Mem0,MC: Store conversation in cloud
    Mem0->>MC: API call (async)
    MC-->>Mem0: {id, memory, event, data}
    
    NL-->>App: response
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

  • mem0Initializer.ts: Cloud API initialization logic and cloud/fallback branching paths require careful validation
  • neurolink.ts: Multiple memory operation call sites with field naming changes (userId→user_id) and payload shape adjustments; new MCP server management methods need review
  • utilities.ts: Substantial Mem0Memory interface changes across 5 methods with new parameter/return type signatures; validate compatibility with implementation
  • Type system consistency: Verify Mem0Config propagates correctly through conversation and initialization flows
  • Test script changes: Cloud API flow validation and cleanup routine execution

Possibly related PRs

Suggested reviewers

  • murdore

Poem

🐰 From local stores to clouds so high,
Mem0 takes flight through the sky,
New keys and configs lead the way,
Cloud-backed memories here to stay,
Search and store with user_id grace,
A fluffy refactor wins the race! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'migrated to cloud hosted mem0' directly and clearly summarizes the main objective of the changeset, which involves replacing OSS Mem0 integration with cloud-based Mem0 API integration across the codebase.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR migrates the mem0 integration from the self-hosted OSS version to the cloud-hosted mem0 API, simplifying the configuration and eliminating the need for local vector database infrastructure.

  • Replaced mem0ai/oss with mem0ai cloud API client
  • Updated all API calls to use cloud-specific parameter names (e.g., userId → user_id)
  • Simplified configuration from complex embedder/vectorStore/LLM setup to simple API key

Reviewed Changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/lib/types/utilities.ts Updated Mem0Memory interface to match cloud API signatures with new parameter names and return types
src/lib/types/conversation.ts Changed import from mem0ai/oss MemoryConfig to local Mem0Config type
src/lib/neurolink.ts Updated mem0 integration to use cloud API with static imports, changed parameter names (userId→user_id), and fixed message role types
src/lib/memory/mem0Initializer.ts Replaced Memory class with MemoryClient, created Mem0Config interface, and updated initialization logic for cloud API
scripts/examples/real-memory-test.js Simplified test configuration from complex local setup to cloud API with updated user IDs
pnpm-lock.yaml Locked mem0ai to exact version 2.1.38
package.json Removed caret from mem0ai version to pin to 2.1.38
oldImplementation.txt Documentation of previous implementation for reference
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread package.json
"json-schema-to-zod": "^2.6.1",
"mathjs": "^14.7.0",
"mem0ai": "^2.1.38",
"mem0ai": "2.1.38",

Copilot AI Nov 11, 2025

Copy link

Choose a reason for hiding this comment

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

[nitpick] Pinning to an exact version (removing the ^ prefix) may prevent automatic patch and minor updates. Consider whether this strict version pinning is intentional. If the goal is to prevent breaking changes from mem0ai updates during the migration, document this decision or plan to restore semver ranges after validation.

Suggested change
"mem0ai": "2.1.38",
"mem0ai": "^2.1.38",

Copilot uses AI. Check for mistakes.
user_id?: string;
created_at?: Date;
updated_at?: Date;
}>;

Copilot AI Nov 11, 2025

Copy link

Choose a reason for hiding this comment

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

The get() method return type changed from nullable (| null) to always returning an object. This is a breaking change that could cause runtime errors if calling code expects and handles null returns. Consider adding null-safety checks or updating the type definition to include | null if the API can actually return null.

Suggested change
}>;
} | null>;

Copilot uses AI. Check for mistakes.
Comment on lines +58 to +65
get: async () =>
({
id: "",
memory: "",
user_id: "",
created_at: new Date(),
updated_at: new Date(),
}) as unknown as Memory,

Copilot AI Nov 11, 2025

Copy link

Choose a reason for hiding this comment

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

The fallback implementation's get() method uses a type assertion as unknown as Memory to bypass TypeScript's type checking. This is a code smell that could hide type incompatibilities. Consider creating a proper fallback Memory object that matches the expected type structure without assertions.

Suggested change
get: async () =>
({
id: "",
memory: "",
user_id: "",
created_at: new Date(),
updated_at: new Date(),
}) as unknown as Memory,
get: async (): Promise<Memory> => ({
id: "",
memory: "",
user_id: "",
created_at: new Date(),
updated_at: new Date(),
}),

Copilot uses AI. Check for mistakes.
Comment thread src/lib/neurolink.ts
} from "./services/server/ai/observability/instrumentation.js";
import type { ObservabilityConfig } from "./types/observability.js";
import type { NeurolinkConstructorConfig } from "./types/configTypes.js";
// Static import for mem0 initialization (cloud API - no bundling issues!)

Copilot AI Nov 11, 2025

Copy link

Choose a reason for hiding this comment

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

[nitpick] The comment states "cloud API - no bundling issues!" but doesn't explain what bundling issues existed before or how the cloud API solves them. Consider adding more context to help future developers understand the rationale for this migration.

Suggested change
// Static import for mem0 initialization (cloud API - no bundling issues!)
// Previously, dynamic imports or platform-specific code for mem0 initialization caused bundling issues
// (e.g., with Webpack or Vite, which struggled with dynamic code paths or optional dependencies).
// Migrating to a static import of the cloud API ensures all dependencies are resolved at build time,
// eliminating bundling errors and improving compatibility across environments.

Copilot uses AI. Check for mistakes.
},
}
};
apiKey: process.env.MEM0_API_KEY || "",

Copilot AI Nov 11, 2025

Copy link

Choose a reason for hiding this comment

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

API key is hardcoded in the test file. Replace with environment variable to prevent accidental exposure:

apiKey: process.env.MEM0_API_KEY || "",

Copilot uses AI. Check for mistakes.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (3)
oldImplementation.txt (3)

9-10: Consider the implications of exact version pinning.

Removing the caret (^) from mem0ai means you won't receive patch updates automatically. While this improves reproducibility, it may prevent automatic security patches.

If this exact version is required for cloud API compatibility, consider documenting the reason. Otherwise, you might want to allow patch updates:

-    "mem0ai": "2.1.38",
+    "mem0ai": "~2.1.38",

The tilde (~) allows patch-level updates (2.1.x) while preventing minor version changes.


881-888: Simplify fallback type casting.

The double type cast as unknown as Memory suggests a type mismatch. This pattern is fragile and can hide type safety issues.

Consider a cleaner approach:

-    get: async () =>
-      ({
-        id: "",
-        memory: "",
-        user_id: "",
-        created_at: new Date(),
-        updated_at: new Date(),
-      }) as unknown as Memory,
+    get: async (memoryId: string) => {
+      logger.warn("[mem0Initializer] Fallback memory - get() called but not functional");
+      return {
+        id: memoryId,
+        memory: "",
+        user_id: "",
+        created_at: new Date(),
+        updated_at: new Date(),
+      } satisfies Partial<Memory> as Memory;
+    },

This approach:

  • Uses satisfies for better type checking
  • Logs a warning so developers know fallback is active
  • Uses the provided memoryId parameter

224-224: Replace 30-second fixed delays with adaptive polling.

Mem0 Platform targets sub-50ms retrieval times, and graph construction completes in under a minute even in worst-case scenarios. The 30-second delay appears conservative. Mem0 achieves 0.20s median and 0.15s p95 search latency, suggesting immediate retrieval is often possible after indexing completes.

Consider polling for memory availability instead of a fixed delay—this adapts to actual indexing speed rather than overestimating latency.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 60c35e0 and 4553239.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (7)
  • oldImplementation.txt (1 hunks)
  • package.json (1 hunks)
  • scripts/examples/real-memory-test.js (9 hunks)
  • src/lib/memory/mem0Initializer.ts (2 hunks)
  • src/lib/neurolink.ts (7 hunks)
  • src/lib/types/conversation.ts (2 hunks)
  • src/lib/types/utilities.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2025-09-17T17:55:15.261Z
Learnt from: RajuSudhar
Repo: juspay/neurolink PR: 173
File: src/lib/index.ts:16-16
Timestamp: 2025-09-17T17:55:15.261Z
Learning: In src/lib/types/providers.ts, ProviderConfig was renamed to AIModelProviderConfig to deduplicate type names, as there was an existing ProviderConfig type that better suited the "ProviderConfig" name. This was an intentional breaking change for better type organization.

Applied to files:

  • src/lib/types/conversation.ts
  • src/lib/memory/mem0Initializer.ts
  • src/lib/types/utilities.ts
  • oldImplementation.txt
  • src/lib/neurolink.ts
📚 Learning: 2025-09-24T06:42:06.088Z
Learnt from: amreetkhuntia
Repo: juspay/neurolink PR: 185
File: src/lib/evaluation/contextBuilder.ts:79-85
Timestamp: 2025-09-24T06:42:06.088Z
Learning: In the NeuroLink codebase, using `(options.prompt || [])` pattern for handling potentially undefined prompt arrays is the preferred approach over extracting to a normalized variable when building conversation history in the ContextBuilder class.

Applied to files:

  • src/lib/neurolink.ts
🧬 Code graph analysis (3)
src/lib/types/conversation.ts (1)
src/lib/memory/mem0Initializer.ts (1)
  • Mem0Config (14-18)
src/lib/memory/mem0Initializer.ts (2)
scripts/examples/real-memory-test.js (1)
  • mem0Config (43-48)
src/lib/types/utilities.ts (1)
  • Mem0Memory (214-271)
src/lib/neurolink.ts (1)
src/lib/memory/mem0Initializer.ts (1)
  • Mem0Config (14-18)
🪛 Gitleaks (8.29.0)
oldImplementation.txt

[high] 123-123: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

🪛 LanguageTool
oldImplementation.txt

[style] ~792-~792: This phrase is redundant (‘I’ stands for ‘Interface’). Use simply “APIInterface”.
Context: ...ance methods based on actual mem0ai/oss API + * Interface for mem0 Memory instance methods based ...

(ACRONYM_TAUTOLOGY)

🔇 Additional comments (10)
package.json (1)

188-188: Thanks for pinning mem0ai.

Locking to 2.1.38 avoids surprise API/client changes while we’re migrating to the cloud workflow. 👍

src/lib/types/conversation.ts (1)

6-40: Type alignment looks good.

Importing the new Mem0Config keeps ConversationMemoryConfig in sync with the cloud initializer contract.

scripts/examples/real-memory-test.js (1)

42-54: Nice clarity on the cloud config.

Explicitly surfacing MEM0_API_KEY (and documenting optional org/project IDs) makes the cloud test script much easier to wire up. 👍

src/lib/neurolink.ts (1)

145-264: Integration matches the new initializer.

NeuroLink now pulls the typed Mem0Config and lazily calls initializeMem0, lining up with the cloud client interface once the initializer fix lands.

src/lib/types/utilities.ts (1)

212-270: LGTM! Cloud API type definitions are well-structured.

The Mem0Memory interface has been properly updated to reflect the cloud API:

  • Consistent snake_case naming (user_id, agent_id, etc.)
  • Proper optional parameter typing
  • New getAll() method added
  • Removed OSS-specific methods (history(), reset())

The breaking changes are significant but align with the PR's migration objective.

oldImplementation.txt (5)

914-919: Good: Static import eliminates bundling issues.

Switching from dynamic import to static import for mem0 initialization is a solid improvement:

  • Eliminates runtime import overhead
  • Improves bundling and tree-shaking
  • Provides better IDE support and type checking
  • Comment correctly notes "no bundling issues" with cloud API

961-968: Improved memory context prompt formatting.

The updated prompt is clearer and more natural:

  • "potentially relevant context" sets appropriate expectations
  • Better whitespace/structure makes it more readable
  • More conversational tone ("User's current question")

975-989: Correctly updated for cloud API structure.

The memory search implementation properly reflects the cloud API changes:

  • Snake_case user_id parameter
  • Simplified response structure (no nested results)
  • Added filter(Boolean) to handle empty memories

Optional refactor for slightly cleaner code:

           if (memories && memories.length > 0) {
             // Enhance the input with memory context
             const memoryContext = memories
+              .filter(m => m.memory)
               .map((m) => m.memory || "")
-              .filter(Boolean)
               .join("\n");

This filters earlier and avoids the redundant || "" with filter(Boolean).


995-1013: Correct role change: "system" → "assistant".

The change from "system" role to "assistant" role for AI responses is correct. The "assistant" role is the standard in chat APIs (OpenAI, Anthropic, etc.) for representing AI-generated messages.

The use of as const type assertions ensures type safety with the role literals.


1072-1083: LGTM! Type import correctly updated.

The type import change properly reflects the migration from OSS to cloud API:

  • Imports from internal module mem0Initializer.ts
  • Uses the new Mem0Config interface
  • Comment accurately describes the cloud API integration

Comment thread oldImplementation.txt
Comment thread oldImplementation.txt
Comment thread src/lib/memory/mem0Initializer.ts
@cmd-err cmd-err closed this Nov 12, 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