AS AgentScopeAPI 手册 / API Reference
类class agentscope.middleware

ReMeMiddleware

class ReMeMiddleware(MiddlewareBase)
源码Source agentscope/middleware/_longterm_memory/_reme/_middleware.py:88–552

Re Me 中间件,在智能体执行流程中提供横切扩展。位于 agentscope.middleware 包,继承自 MiddlewareBase。核心成员包括 close、on_reply、on_reasoning、on_system_prompt、list_tools。

ReMe is embedded in-process (no separate service): the middleware instantiates a :class:reme.ReMe application whose LLM-backed jobs use the chat_model configured at construction. The app is built and owned by the middleware — pass workspace_dir and optionally a Parameters with chat_model / embedding_model; it is created lazily on first use. When chat_model is omitted, the AgentScope config supplies the LLM from ReMe's LLM_* environment variables. Providing an embedding_model enables the vector store; otherwise search stays keyword-only.

The model is fixed at construction (never taken from an agent), so the embedded app's single LLM is well-defined even when one middleware instance is shared across several agents. Per-conversation state — the ReMe session_id — is instead read live from each agent at hook time and never stored, keeping shared use isolated. Background retrieval tasks are likewise tracked per session_id so concurrent replies in different sessions never clobber each other's in-flight search.

AgentScope middleware has no framework-managed lifecycle, so the app is built once and started lazily on first use (idempotent). Call :meth:close for explicit teardown of the embedded app.

Example::

from agentscope.middleware import ReMeMiddleware
from agentscope.tool import Toolkit

mw = ReMeMiddleware(
    workspace_dir=".reme",
    parameters=ReMeMiddleware.Parameters(mode="both"),
)
agent = Agent(
    ...,
    toolkit=Toolkit(tools=await mw.list_tools()),
    middlewares=[mw],
)

嵌套类摘要Nested Class Summary

类Class类型Kind说明Description
Parameters类class
参数,驱动智能体的推理、工具调用与会话执行。
User-tunable ReMe memory parameters.

构造器摘要Constructor Summary

构造器Constructor说明Description
def __init__(*, workspace_dir: str = '.reme', parameters: Parameters | None = None) -> None
初始化 `ReMeMiddleware` 实例。
Initialize the ReMe middleware.

方法摘要Method Summary

方法Method说明Description
async def close() -> Noneasync
关闭连接并释放相关资源。
Close the embedded ReMe app.
async def on_reply(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGeneratorasync
拦截并扩展回复流程。
Handles the reply event.
async def on_reasoning(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGenerat…async
拦截并扩展推理阶段。
Inject background-retrieved memories before a reasoning step.
async def on_system_prompt(agent: 'Agent', current_prompt: str) -> strasync
拦截并修改系统提示词。
Append search-tool instructions to the system prompt.
async def list_tools() -> list['ToolBase']async
列出当前连接可用的工具。
List memory tools provided by this middleware.

构造器详情Constructor Detail

ReMeMiddleware

def __init__(*, workspace_dir: str = '.reme', parameters: Parameters | None = None) -> None

初始化 ReMeMiddleware 实例。

The ReMe session_id (which scopes write-back memory cards) is not taken here — it is read from agent.state.session_id at hook time (every agent has one; AgentState generates it), mirroring :class:TracingMiddleware.

参数Parameters
名称Name类型Type说明Description
workspace_dirkw默认default '.reme'str工作区目录(类型 `str`)。ReMe workspace (vault) directory for memory cards and indexes. Defaults to `".reme"`.
parameterskw默认default NoneParameters | None参数(类型 `Parameters | None`)。User-tunable parameters (`chat_model` / `embedding_model` / `mode` / `top_k`) whose schema the agent service renders as a configuration form. When `None`, defaults are used. Providing an `embedding_model` also enables ReMe's vector store (otherwise search is keyword-only).
源码Source agentscope/middleware/_longterm_memory/_reme/_middleware.py:184–222

方法详情Method Detail

close

async def close() -> None
async

关闭连接并释放相关资源。

AgentScope does not manage middleware lifecycle, so call this explicitly for clean teardown (e.g.

返回Returns None
无返回值。No value is returned.
源码Source agentscope/middleware/_longterm_memory/_reme/_middleware.py:286–294

on_reply

async def on_reply(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGenerator
async

拦截并扩展回复流程。

Handles the reply event.

参数Parameters
名称Name类型Type说明Description
agent'Agent'智能体(类型 `'Agent'`)。The agent.
input_kwargsdict输入关键字参数(类型 `dict`)。The input kwargs.
next_handlerCallable[..., AsyncGenerator]下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The next handler.
返回Returns AsyncGenerator
返回AsyncGenerator。Returns async generator.
源码Source agentscope/middleware/_longterm_memory/_reme/_middleware.py:312–380

on_reasoning

async def on_reasoning(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGenerator
async

拦截并扩展推理阶段。

The search started in :meth:on_reply runs concurrently; this hook polls it before each reasoning step and, once it is finished, appends the retrieved memories to the agent's context so the next model call sees them.

参数Parameters
名称Name类型Type说明Description
agent'Agent'智能体(类型 `'Agent'`)。The agent.
input_kwargsdict输入关键字参数(类型 `dict`)。The input kwargs.
next_handlerCallable[..., AsyncGenerator]下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The next handler.
返回Returns AsyncGenerator
返回AsyncGenerator。Returns async generator.
源码Source agentscope/middleware/_longterm_memory/_reme/_middleware.py:385–416

on_system_prompt

async def on_system_prompt(agent: 'Agent', current_prompt: str) -> str
async

拦截并修改系统提示词。

Append search-tool instructions to the system prompt.

参数Parameters
名称Name类型Type说明Description
agent'Agent'智能体(类型 `'Agent'`)。The agent whose system prompt is being transformed.
current_promptstr当前提示词(类型 `str`)。The system prompt produced by previous middleware.
返回Returns str
返回字符串结果。`str`:
源码Source agentscope/middleware/_longterm_memory/_reme/_middleware.py:421–441

list_tools

async def list_tools() -> list['ToolBase']
async

列出当前连接可用的工具。

List memory tools provided by this middleware.

返回Returns list['ToolBase']
返回'ToolBase'列表。`list[ToolBase]`:
源码Source agentscope/middleware/_longterm_memory/_reme/_middleware.py:443–454

嵌套类详情Nested Class Detail

Parameters

class Parameters

参数,驱动智能体的推理、工具调用与会话执行。位于 agentscope.middleware 包。

The agent service parses this schema to render a configuration form. Structural wiring (workspace_dir) stays on the constructor.

继承成员Inherited Members

基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.