cloudflare workers support #3543
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This section describes the implementation of Cloudflare Workers support for mem0ai.
Problem Statement
The original mem0ai TypeScript SDK had compatibility issues with Cloudflare Workers due to:
Solution Overview
Created a Workers-compatible client that:
fetch,crypto.subtle) instead of Node.js APIsImplementation Details
New Files Created
1. Core Client (
mem0-ts/src/workers/index.ts)CloudflareWorkerMemoryClientclassfetchcrypto.subtle2. Example Implementation (
mem0-ts/examples/cloudflare-workers/)worker.ts)wrangler.toml)package.json)3. Tests (
mem0-ts/src/workers/index.test.ts)4. Documentation (
docs/cloudflare-workers.md)Configuration Changes
Updated
mem0-ts/package.json{ "exports": { "./workers": { "types": "./dist/workers/index.d.ts", "require": "./dist/workers/index.js", "import": "./dist/workers/index.mjs" } }, "typesVersions": { "*": { "workers": ["./dist/workers/index.d.ts"] } } }Updated
mem0-ts/tsup.config.tsKey Features
1. Web-First Architecture
Instead of:
Uses:
2. Same API Interface
3. Error Handling
4. TypeScript Support
Full type definitions exported:
Usage Examples
Basic Usage
Complete Worker
Performance Benefits
Limitations
Current Limitations
Future Enhancements
Developer Experience
Import Path
Development Workflow
Impact
This implementation:
Next Steps
This implementation successfully enables mem0ai to run in Cloudflare Workers, providing developers with ultra-low-latency AI applications with persistent memory at the edge.
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
npm test -- src/workers/index.test.tsThe new Cloudflare Workers implementation passes all tests:
Constructor Tests
• Should instantiate with valid API key
• Should throw error with missing API key
• Should use custom host when provided
API Methods Tests
• Should have all expected methods
• Should handle ping request
• Should handle add request
• Should handle search request
• Should handle API errors gracefully
Error Handling Tests
• Should validate update parameters
Platform Compatibility Tests
• Should use Web APIs only
The passing tests confirm that the
CloudflareWorkerMemoryClient:Checklist:
Maintainer Checklist