-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add memvid as provider #18
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
Open
PhantomInTheWire
wants to merge
1
commit into
supermemoryai:main
Choose a base branch
from
PhantomInTheWire:feat/memvid
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import type { | ||
| Provider, | ||
| ProviderConfig, | ||
| IngestOptions, | ||
| IngestResult, | ||
| SearchOptions, | ||
| IndexingProgressCallback, | ||
| } from "../../types/provider" | ||
| import type { UnifiedSession } from "../../types/unified" | ||
| import { logger } from "../../utils/logger" | ||
| import { open, create } from "@memvid/sdk" | ||
| import type { Memvid } from "@memvid/sdk" | ||
| import { MEMVID_PROMPTS } from "./prompts" | ||
| import path from "path" | ||
| import fs from "fs" | ||
|
|
||
| export class MemvidProvider implements Provider { | ||
| name = "memvid" | ||
| prompts = MEMVID_PROMPTS | ||
| private client: Memvid | null = null | ||
| private filePath: string = "memorybench.mv2" | ||
|
|
||
| async initialize(config: ProviderConfig): Promise<void> { | ||
| if (config.filePath) { | ||
| this.filePath = config.filePath as string | ||
| } else if (process.env.MEMVID_FILE_PATH) { | ||
| this.filePath = process.env.MEMVID_FILE_PATH | ||
| } | ||
|
|
||
| const dir = path.dirname(this.filePath) | ||
| if (!fs.existsSync(dir)) { | ||
| fs.mkdirSync(dir, { recursive: true }) | ||
| } | ||
|
|
||
| try { | ||
| if (fs.existsSync(this.filePath)) { | ||
| this.client = await open(this.filePath) | ||
| logger.info(`Initialized Memvid provider (opened) at ${this.filePath}`) | ||
| } else { | ||
| this.client = await create(this.filePath) | ||
| logger.info(`Initialized Memvid provider (created) at ${this.filePath}`) | ||
| } | ||
| } catch (e) { | ||
| logger.error(`Failed to initialize Memvid: ${e}`) | ||
| throw e | ||
| } | ||
| } | ||
|
|
||
| async ingest(sessions: UnifiedSession[], options: IngestOptions): Promise<IngestResult> { | ||
| if (!this.client) throw new Error("Provider not initialized") | ||
|
|
||
| const documentIds: string[] = [] | ||
|
|
||
| for (const session of sessions) { | ||
| const content = session.messages | ||
| .map((m) => `${m.role.toUpperCase()}: ${m.content}`) | ||
| .join("\n\n") | ||
|
|
||
| const uri = `mv2://session/${session.sessionId}` | ||
|
|
||
| try { | ||
| const frameId = await this.client.put({ | ||
| title: `Session ${session.sessionId}`, | ||
| text: content, | ||
| tags: [options.containerTag, ...(options.metadata?.tags as string[] || [])], | ||
| metadata: { | ||
| ...options.metadata, | ||
| containerTag: options.containerTag, | ||
| sessionId: session.sessionId, | ||
| uri: uri | ||
| } | ||
| }) | ||
|
|
||
| documentIds.push(String(frameId)) | ||
| } catch (e) { | ||
| logger.error(`Failed to ingest session ${session.sessionId}: ${e}`) | ||
| } | ||
| } | ||
|
|
||
| return { documentIds } | ||
| } | ||
|
|
||
| async awaitIndexing( | ||
| result: IngestResult, | ||
| _containerTag: string, | ||
| onProgress?: IndexingProgressCallback | ||
| ): Promise<void> { | ||
| onProgress?.({ | ||
| completedIds: result.documentIds, | ||
| failedIds: [], | ||
| total: result.documentIds.length | ||
| }) | ||
| } | ||
|
|
||
| async search(query: string, options: SearchOptions): Promise<unknown[]> { | ||
| if (!this.client) throw new Error("Provider not initialized") | ||
|
|
||
| try { | ||
| const result = await this.client.find(query, { | ||
| k: options.limit || 10, | ||
| // mode: "auto" // default | ||
| // scope? | ||
| }) | ||
| return result.hits || [] | ||
| } catch (e) { | ||
| logger.error(`Search failed: ${e}`) | ||
| return [] | ||
| } | ||
| } | ||
|
|
||
| async clear(containerTag: string): Promise<void> { | ||
| if (!this.client) return | ||
|
|
||
| try { | ||
| if (this.client.seal) { | ||
| await this.client.seal() | ||
| } | ||
| this.client = null | ||
|
|
||
| if (fs.existsSync(this.filePath)) { | ||
| fs.unlinkSync(this.filePath) | ||
| logger.info(`Deleted Memvid file at ${this.filePath}`) | ||
| } | ||
|
|
||
| this.client = await create(this.filePath) | ||
| logger.info(`Re-initialized Memvid provider (created) at ${this.filePath}`) | ||
| } catch (e) { | ||
| logger.error(`Failed to clear Memvid: ${e}`) | ||
| if (!this.client && fs.existsSync(this.filePath)) { | ||
| this.client = await open(this.filePath) | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import type { ProviderPrompts } from "../../types/prompts" | ||
|
|
||
| interface MemvidResult { | ||
| content?: string | ||
| text?: string | ||
| snippet?: string | ||
| title?: string | ||
| score?: number | ||
| created_at?: string | ||
| [key: string]: unknown | ||
| } | ||
|
|
||
| function buildMemvidContext(context: unknown[]): string { | ||
| return context.map((item) => { | ||
| const r = item as MemvidResult | ||
| const title = r.title || "Untitled" | ||
| const content = r.snippet || r.text || r.content || JSON.stringify(r) | ||
| return `Title: ${title}\nSnippet: ${content}` | ||
| }).join("\n\n") | ||
| } | ||
|
|
||
| export function buildMemvidAnswerPrompt(question: string, context: unknown[], questionDate?: string): string { | ||
| const contextStr = buildMemvidContext(context) | ||
|
|
||
| return `Based on the following context from the knowledge base, answer the question. | ||
|
|
||
| Context: | ||
| ${contextStr} | ||
|
|
||
| Question: ${question} | ||
| Question Date: ${questionDate || "Not provided"} | ||
|
|
||
| Answer:` | ||
| } | ||
|
|
||
| export const MEMVID_PROMPTS: ProviderPrompts = { | ||
| answerPrompt: buildMemvidAnswerPrompt | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The
putmethod from the@memvid/sdkreturnsPromise<void>, but the code incorrectly expects it to return a document ID, resulting inframeIdbeingundefined.Severity: CRITICAL | Confidence: High
🔍 Detailed Analysis
The
putmethod from the@memvid/sdkis documented to return aPromise<void>, meaning it does not resolve with any value. However, the code atsrc/providers/memvid/index.ts:63attempts to assign the result of this promise to aframeIdvariable. This variable will consequently beundefined. The code then proceeds to callString(frameId), which evaluates to the literal string "undefined". As a result, every document ingested through this provider will be assigned the same invalid document ID of "undefined", breaking any downstream functionality that relies on unique document tracking.💡 Suggested Fix
Since the
putmethod from the@memvid/sdkdoes not return a document ID, the logic for tracking ingested documents needs to be revised. Remove the assignment of theawait this.client.put(...)result toframeIdand adjust thedocumentIdsarray accordingly, as it's not possible to retrieve IDs from this specific call.🤖 Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID:
8252104