-
Notifications
You must be signed in to change notification settings - Fork 15
feat: memory Driver interface and "local" implementor #86
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
Draft
jpmcb
wants to merge
1
commit into
main
Choose a base branch
from
memory
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.
+553
−0
Draft
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
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
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,70 @@ | ||
| package mcp | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "github.com/modelcontextprotocol/go-sdk/mcp" | ||
|
|
||
| "github.com/papercomputeco/tapes/pkg/memory" | ||
| ) | ||
|
|
||
| var ( | ||
| memoryRecallToolName = "memory_recall" | ||
| memoryRecallDescription = "Recall facts from the tapes memory layer. Given a node hash (a position in the conversation DAG), returns extracted facts that are relevant to that position. Use this to retrieve persistent knowledge from past conversations." | ||
| ) | ||
|
|
||
| // MemoryRecallInput represents the input arguments for the MCP memory_recall tool. | ||
| type MemoryRecallInput struct { | ||
| Hash string `json:"hash" jsonschema:"the node hash identifying a position in the conversation DAG to recall facts for"` | ||
| } | ||
|
|
||
| // MemoryRecallOutput represents the structured output of a memory recall. | ||
| type MemoryRecallOutput struct { | ||
| Facts []memory.Fact `json:"facts"` | ||
| } | ||
|
|
||
| // handleMemoryRecall processes a memory recall request via MCP. | ||
| func (s *Server) handleMemoryRecall(ctx context.Context, _ *mcp.CallToolRequest, input MemoryRecallInput) (*mcp.CallToolResult, MemoryRecallOutput, error) { | ||
| if input.Hash == "" { | ||
| return &mcp.CallToolResult{ | ||
| IsError: true, | ||
| Content: []mcp.Content{ | ||
| &mcp.TextContent{Text: "hash is required"}, | ||
| }, | ||
| }, MemoryRecallOutput{}, nil | ||
| } | ||
|
|
||
| facts, err := s.config.MemoryDriver.Recall(ctx, input.Hash) | ||
| if err != nil { | ||
| return &mcp.CallToolResult{ | ||
| IsError: true, | ||
| Content: []mcp.Content{ | ||
| &mcp.TextContent{Text: fmt.Sprintf("Memory recall failed: %v", err)}, | ||
| }, | ||
| }, MemoryRecallOutput{}, nil | ||
| } | ||
|
|
||
| if facts == nil { | ||
| facts = []memory.Fact{} | ||
| } | ||
|
|
||
| output := MemoryRecallOutput{Facts: facts} | ||
|
|
||
| jsonBytes, err := json.Marshal(output) | ||
| if err != nil { | ||
| return &mcp.CallToolResult{ | ||
| IsError: true, | ||
| Content: []mcp.Content{ | ||
| &mcp.TextContent{Text: fmt.Sprintf("Failed to serialize results: %v", err)}, | ||
| }, | ||
| }, MemoryRecallOutput{}, nil | ||
| } | ||
|
|
||
| return &mcp.CallToolResult{ | ||
| Content: []mcp.Content{ | ||
| &mcp.TextContent{Text: string(jsonBytes)}, | ||
| }, | ||
| }, output, nil | ||
| } | ||
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
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,53 @@ | ||
| // Package memory provides a pluggable memory layer for the tapes system. | ||
| // | ||
| // Memory drivers extract durable facts from conversation nodes and recall them | ||
| // on demand. Facts are distilled, persistent knowledge derived from | ||
| // conversations — not raw messages. | ||
| // | ||
| // The [Driver] interface is intentionally minimal: Store extracts facts from | ||
| // nodes, Recall retrieves facts relevant to a DAG position, and Close releases | ||
| // resources. Memory is one-way; backend systems manage their own lifecycle and | ||
| // eviction policies. | ||
| // | ||
| // Short-term context (e.g., sliding windows over recent nodes) is a | ||
| // proxy-level concern handled via the storage driver's Ancestry method and is | ||
| // not part of the memory interface. | ||
| // | ||
| // Drivers are pluggable via configuration: | ||
| // | ||
| // [memory] | ||
| // provider = "local" # or "cognee", "graph" | ||
| package memory | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/papercomputeco/tapes/pkg/merkle" | ||
| ) | ||
|
|
||
| // Driver handles storage and recall of conversation memory. | ||
| // Implementers extract durable facts from conversation nodes and recall them | ||
| // given a position in the DAG. | ||
| type Driver interface { | ||
| // Store persists one or more nodes into memory. This is the forcing | ||
| // function for driver implementors to extract facts from conversation | ||
| // nodes. Called asynchronously by the proxy worker pool after a | ||
| // conversation turn is stored in the DAG. | ||
| Store(ctx context.Context, nodes []*merkle.Node) error | ||
|
|
||
| // Recall retrieves facts relevant to a position in the DAG tree. | ||
| // The hash identifies a node (typically the current leaf), and the | ||
| // driver returns facts relevant to that branch/position. | ||
| Recall(ctx context.Context, hash string) ([]Fact, error) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could pass a context string here as well so it is compatible with Cognee |
||
|
|
||
| // Close releases driver resources. | ||
| Close() error | ||
| } | ||
|
|
||
| // Fact represents a distilled, durable piece of knowledge extracted from | ||
| // conversations. Facts are the output of the memory layer — not raw messages, | ||
| // but persistent knowledge that may be relevant across branches and sessions. | ||
| type Fact struct { | ||
| // Content is the extracted fact text. | ||
| Content string `json:"content"` | ||
| } | ||
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,7 @@ | ||
| package memory | ||
|
|
||
| import "errors" | ||
|
|
||
| // ErrNotConfigured is returned when memory operations are attempted | ||
| // but no memory driver has been configured. | ||
| var ErrNotConfigured = errors.New("memory not configured") |
Oops, something went wrong.
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.
For query-based backends (e.g., Cognee), there is a possible mismatch here since they expect queries, not hashes. Consider resolving the hash via
DagLoaderto derive a query and pass that to the driver.