Repository navigation
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Implements a functional PowerMemMiddleware for LangChain agents, enabling long-term memory retrieval before model calls and optional interaction write-back after agent runs, aligning the package with its contract tests and example usage.
Changes:
- Added PowerMem search + context formatting and injected retrieved memories into the model request.
- Implemented sync/async middleware hooks (
before_agent/abefore_agent,wrap_model_call/awrap_model_call,after_agent/aafter_agent) with fail-open behavior. - Added helper utilities for extracting latest user/assistant text and safely calling sync/async memory backends.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| point. Students are expected to replace this placeholder with a LangChain | ||
| middleware implementation that satisfies the package contract tests. | ||
| """ | ||
| """为LangChain agent提供PowerMem长期记忆能力的中间件""" |
Comment on lines
+146
to
+157
| try: | ||
| result = self.memory.search( | ||
| query, | ||
| user_id=self.user_id, | ||
| limit=self.search_limit, | ||
| ) | ||
| if inspect.isawaitable(result): | ||
| result = asyncio.run(result) | ||
| return {"powermem_context": _format_search_result(result)} | ||
| except Exception: | ||
| logger.warning("PowerMem search failed; continuing without memory", exc_info=True) | ||
| return {"powermem_context": ""} |
Comment on lines
+234
to
+244
| try: | ||
| result = self.memory.add( | ||
| interaction, | ||
| user_id=self.user_id, | ||
| infer=False, | ||
| ) | ||
| if inspect.isawaitable(result): | ||
| asyncio.run(result) | ||
| except Exception: | ||
| logger.warning("PowerMem write-back failed; agent result is unchanged", exc_info=True) | ||
| return None |
Comment on lines
+45
to
+47
| if isinstance(block, str): # 普通字符串 | ||
| parts.append(block) | ||
| elif isinstance(block, Mapping): # 列表 |
Member
LangChain Contract ScoreLangChain Contract Score ======================== score: 100.00 / 100 suites: 2 rule: A weighted capability receives credit only when it passes on every configured Python suite and all declared prerequisite capabilities also receive credit. Gate tests are reported but do not add points. Public import contract: PASS (gate) py311: passed py312: passed Retrieve memory before model call: PASS (35/35) py311: passed py312: passed Persist interaction after agent run: PASS (25/25) py311: passed py312: passed Disable interaction persistence: PASS (10/10) py311: passed py312: passed Fail-open on search error: PASS (15/15) py311: passed py312: passed Async memory injection: PASS (15/15) py311: passed py312: passed LangChain Contract ReportLangChain Contract Report ========================= score: 100.00 / 100 rule: A weighted capability receives credit only when it passes on every configured Python suite and all declared prerequisite capabilities also receive credit. Gate tests are reported but do not add points. Suite Summary ------------- py311: passed=6/6, failures=0, errors=0, skipped=0 py312: passed=6/6, failures=0, errors=0, skipped=0 Gate Checks ----------- Public import contract [gate] PASS - py311: passed - py312: passed Weighted Breakdown ------------------ Retrieve memory before model call [35/35] PASS - py311: passed - py312: passed Persist interaction after agent run [25/25] PASS - py311: passed - py312: passed Disable interaction persistence [10/10] PASS - py311: passed - py312: passed Fail-open on search error [15/15] PASS - py311: passed - py312: passed Async memory injection [15/15] PASS - py311: passed - py312: passed |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Solution Description