mem-deep-research 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. mem_deep_research-0.1.0/.gitignore +147 -0
  2. mem_deep_research-0.1.0/CHANGELOG.md +16 -0
  3. mem_deep_research-0.1.0/CLAUDE.md +274 -0
  4. mem_deep_research-0.1.0/CONTRIBUTING.md +46 -0
  5. mem_deep_research-0.1.0/LICENSE +190 -0
  6. mem_deep_research-0.1.0/PKG-INFO +333 -0
  7. mem_deep_research-0.1.0/README.md +290 -0
  8. mem_deep_research-0.1.0/docs/00-architecture.md +111 -0
  9. mem_deep_research-0.1.0/docs/01-quick-start.md +170 -0
  10. mem_deep_research-0.1.0/docs/02-configuration.md +169 -0
  11. mem_deep_research-0.1.0/docs/03-core-modules.md +235 -0
  12. mem_deep_research-0.1.0/docs/04-context-management.md +177 -0
  13. mem_deep_research-0.1.0/docs/05-monitoring.md +147 -0
  14. mem_deep_research-0.1.0/docs/06-llm-providers.md +183 -0
  15. mem_deep_research-0.1.0/docs/07-tool-system.md +227 -0
  16. mem_deep_research-0.1.0/docs/08-hook-and-secure-context.md +229 -0
  17. mem_deep_research-0.1.0/docs/09-prompt-system.md +209 -0
  18. mem_deep_research-0.1.0/docs/10-skill-system.md +202 -0
  19. mem_deep_research-0.1.0/docs/11-development.md +160 -0
  20. mem_deep_research-0.1.0/mem_deep_research/__init__.py +39 -0
  21. mem_deep_research-0.1.0/mem_deep_research_core/__init__.py +75 -0
  22. mem_deep_research-0.1.0/mem_deep_research_core/cli/__init__.py +5 -0
  23. mem_deep_research-0.1.0/mem_deep_research_core/cli/main.py +164 -0
  24. mem_deep_research-0.1.0/mem_deep_research_core/cli/templates.py +606 -0
  25. mem_deep_research-0.1.0/mem_deep_research_core/config/__init__.py +40 -0
  26. mem_deep_research-0.1.0/mem_deep_research_core/config/agent_example.yaml +131 -0
  27. mem_deep_research-0.1.0/mem_deep_research_core/config/skills/definitions/search_strategy.md +88 -0
  28. mem_deep_research-0.1.0/mem_deep_research_core/config/tool/tool-calculator.yaml +6 -0
  29. mem_deep_research-0.1.0/mem_deep_research_core/config/tool/tool-searching-serper.yaml +8 -0
  30. mem_deep_research-0.1.0/mem_deep_research_core/config_schema.py +336 -0
  31. mem_deep_research-0.1.0/mem_deep_research_core/core/__init__.py +83 -0
  32. mem_deep_research-0.1.0/mem_deep_research_core/core/agent_factory.py +416 -0
  33. mem_deep_research-0.1.0/mem_deep_research_core/core/answer_handler.py +153 -0
  34. mem_deep_research-0.1.0/mem_deep_research_core/core/context_manager.py +646 -0
  35. mem_deep_research-0.1.0/mem_deep_research_core/core/hooks.py +319 -0
  36. mem_deep_research-0.1.0/mem_deep_research_core/core/interceptor_config.py +195 -0
  37. mem_deep_research-0.1.0/mem_deep_research_core/core/llm_call_handler.py +547 -0
  38. mem_deep_research-0.1.0/mem_deep_research_core/core/main_loop.py +511 -0
  39. mem_deep_research-0.1.0/mem_deep_research_core/core/message_interceptor.py +313 -0
  40. mem_deep_research-0.1.0/mem_deep_research_core/core/monitoring.py +471 -0
  41. mem_deep_research-0.1.0/mem_deep_research_core/core/orchestrator.py +991 -0
  42. mem_deep_research-0.1.0/mem_deep_research_core/core/pipeline.py +225 -0
  43. mem_deep_research-0.1.0/mem_deep_research_core/core/secure_context.py +197 -0
  44. mem_deep_research-0.1.0/mem_deep_research_core/core/stream_handler.py +210 -0
  45. mem_deep_research-0.1.0/mem_deep_research_core/core/sub_agent_runner.py +456 -0
  46. mem_deep_research-0.1.0/mem_deep_research_core/core/task_planner.py +189 -0
  47. mem_deep_research-0.1.0/mem_deep_research_core/core/tool_executor.py +356 -0
  48. mem_deep_research-0.1.0/mem_deep_research_core/core/tool_result_formatter.py +204 -0
  49. mem_deep_research-0.1.0/mem_deep_research_core/core/user_context.py +237 -0
  50. mem_deep_research-0.1.0/mem_deep_research_core/core/window_strategy.py +792 -0
  51. mem_deep_research-0.1.0/mem_deep_research_core/deep_research.py +467 -0
  52. mem_deep_research-0.1.0/mem_deep_research_core/exceptions.py +255 -0
  53. mem_deep_research-0.1.0/mem_deep_research_core/llm/__init__.py +13 -0
  54. mem_deep_research-0.1.0/mem_deep_research_core/llm/client.py +73 -0
  55. mem_deep_research-0.1.0/mem_deep_research_core/llm/provider_client_base.py +405 -0
  56. mem_deep_research-0.1.0/mem_deep_research_core/llm/providers/__init__.py +22 -0
  57. mem_deep_research-0.1.0/mem_deep_research_core/llm/providers/claude_anthropic_client.py +236 -0
  58. mem_deep_research-0.1.0/mem_deep_research_core/llm/providers/claude_openrouter_client.py +28 -0
  59. mem_deep_research-0.1.0/mem_deep_research_core/llm/providers/deepseek_openrouter_client.py +105 -0
  60. mem_deep_research-0.1.0/mem_deep_research_core/llm/providers/gpt5_openai_client.py +11 -0
  61. mem_deep_research-0.1.0/mem_deep_research_core/llm/providers/gpt5_openrouter_client.py +40 -0
  62. mem_deep_research-0.1.0/mem_deep_research_core/llm/providers/gpt_openai_client.py +272 -0
  63. mem_deep_research-0.1.0/mem_deep_research_core/llm/providers/openai_compatible_client.py +560 -0
  64. mem_deep_research-0.1.0/mem_deep_research_core/llm/util.py +291 -0
  65. mem_deep_research-0.1.0/mem_deep_research_core/mem_deep_research_logging/__init__.py +0 -0
  66. mem_deep_research-0.1.0/mem_deep_research_core/mem_deep_research_logging/logger.py +409 -0
  67. mem_deep_research-0.1.0/mem_deep_research_core/mem_deep_research_logging/task_tracer.py +131 -0
  68. mem_deep_research-0.1.0/mem_deep_research_core/prompts/__init__.py +33 -0
  69. mem_deep_research-0.1.0/mem_deep_research_core/prompts/agent_prompt.py +286 -0
  70. mem_deep_research-0.1.0/mem_deep_research_core/prompts/template_loader.py +173 -0
  71. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/base/chinese_context.md +9 -0
  72. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/base/chinese_worker.md +10 -0
  73. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/base/objective_main.md +3 -0
  74. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/base/objective_worker.md +27 -0
  75. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/base/sub_agent_tool_description.md +5 -0
  76. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/base/summarize.md +31 -0
  77. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/base/system_intro.md +5 -0
  78. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/base/tool_format_native.md +10 -0
  79. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/base/tool_format_xml.md +31 -0
  80. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/extraction/browsecomp_zh.md +88 -0
  81. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/extraction/gaia_answer_type.md +12 -0
  82. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/extraction/gaia_chinese_supplement.md +28 -0
  83. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/extraction/gaia_confidence.md +36 -0
  84. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/extraction/gaia_extract_number.md +75 -0
  85. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/extraction/gaia_extract_string.md +84 -0
  86. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/extraction/gaia_extract_time.md +55 -0
  87. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/guidance/task_guidance_chinese.md +13 -0
  88. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/hints/hint_chinese_supplement.md +12 -0
  89. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/hints/hint_instruction.md +18 -0
  90. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/hints/hint_prefix.md +5 -0
  91. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/language/detect_language.md +5 -0
  92. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/presets/research.md +40 -0
  93. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/presets/research_planning.md +33 -0
  94. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/presets/time_sensitive.md +21 -0
  95. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/reflection/reflection.md +13 -0
  96. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/reflection/reflection_chinese.md +13 -0
  97. mem_deep_research-0.1.0/mem_deep_research_core/prompts/templates/skills/select_skills.md +55 -0
  98. mem_deep_research-0.1.0/mem_deep_research_core/py.typed +0 -0
  99. mem_deep_research-0.1.0/mem_deep_research_core/skills/__init__.py +23 -0
  100. mem_deep_research-0.1.0/mem_deep_research_core/skills/inline_selector.py +200 -0
  101. mem_deep_research-0.1.0/mem_deep_research_core/skills/llm_selector.py +193 -0
  102. mem_deep_research-0.1.0/mem_deep_research_core/skills/matcher.py +521 -0
  103. mem_deep_research-0.1.0/mem_deep_research_core/tool/__init__.py +13 -0
  104. mem_deep_research-0.1.0/mem_deep_research_core/tool/manager.py +796 -0
  105. mem_deep_research-0.1.0/mem_deep_research_core/tool/mcp_servers/__init__.py +0 -0
  106. mem_deep_research-0.1.0/mem_deep_research_core/tool/mcp_servers/browser_session.py +95 -0
  107. mem_deep_research-0.1.0/mem_deep_research_core/tool/mcp_servers/calculator_server.py +116 -0
  108. mem_deep_research-0.1.0/mem_deep_research_core/utils/__init__.py +0 -0
  109. mem_deep_research-0.1.0/mem_deep_research_core/utils/external_loader.py +418 -0
  110. mem_deep_research-0.1.0/mem_deep_research_core/utils/io_utils.py +177 -0
  111. mem_deep_research-0.1.0/mem_deep_research_core/utils/parsing_utils.py +763 -0
  112. mem_deep_research-0.1.0/mem_deep_research_core/utils/stream_parsing_utils.py +293 -0
  113. mem_deep_research-0.1.0/mem_deep_research_core/utils/summary_utils.py +296 -0
  114. mem_deep_research-0.1.0/mem_deep_research_core/utils/tool_utils.py +171 -0
  115. mem_deep_research-0.1.0/pyproject.toml +146 -0
@@ -0,0 +1,147 @@
1
+ # =============================================================================
2
+ # mem_deep_research Framework .gitignore
3
+ # =============================================================================
4
+
5
+ # -----------------------------------------------------------------------------
6
+ # Logs and Output
7
+ # -----------------------------------------------------------------------------
8
+ logs/
9
+ *.log
10
+ *.json.log
11
+ outputs/
12
+ task_logs/
13
+
14
+ # -----------------------------------------------------------------------------
15
+ # Environment and Secrets
16
+ # -----------------------------------------------------------------------------
17
+ .env
18
+ .env.local
19
+ .env.*.local
20
+ *.pem
21
+ *.key
22
+ secrets/
23
+ credentials.json
24
+
25
+ # -----------------------------------------------------------------------------
26
+ # Python
27
+ # -----------------------------------------------------------------------------
28
+ __pycache__/
29
+ *.py[cod]
30
+ *$py.class
31
+ *.so
32
+ .Python
33
+ build/
34
+ develop-eggs/
35
+ dist/
36
+ downloads/
37
+ eggs/
38
+ .eggs/
39
+ lib/
40
+ lib64/
41
+ parts/
42
+ sdist/
43
+ var/
44
+ wheels/
45
+ *.egg-info/
46
+ .installed.cfg
47
+ *.egg
48
+ MANIFEST
49
+
50
+ # Virtual environments
51
+ .venv/
52
+ venv/
53
+ ENV/
54
+ env/
55
+ .conda/
56
+
57
+ # -----------------------------------------------------------------------------
58
+ # IDE and Editor
59
+ # -----------------------------------------------------------------------------
60
+ .idea/
61
+ .vscode/
62
+ *.swp
63
+ *.swo
64
+ *~
65
+ .project
66
+ .pydevproject
67
+ .settings/
68
+ *.sublime-workspace
69
+ *.sublime-project
70
+
71
+ # -----------------------------------------------------------------------------
72
+ # OS Generated
73
+ # -----------------------------------------------------------------------------
74
+ .DS_Store
75
+ .DS_Store?
76
+ ._*
77
+ .Spotlight-V100
78
+ .Trashes
79
+ ehthumbs.db
80
+ Thumbs.db
81
+
82
+ # -----------------------------------------------------------------------------
83
+ # Testing and Coverage
84
+ # -----------------------------------------------------------------------------
85
+ .coverage
86
+ .pytest_cache/
87
+ htmlcov/
88
+ .tox/
89
+ .nox/
90
+ coverage.xml
91
+ *.cover
92
+ *.coverage
93
+ .hypothesis/
94
+ pytest_cache/
95
+
96
+ # -----------------------------------------------------------------------------
97
+ # Jupyter Notebook
98
+ # -----------------------------------------------------------------------------
99
+ .ipynb_checkpoints/
100
+ *.ipynb_checkpoints
101
+
102
+ # -----------------------------------------------------------------------------
103
+ # Type Checking
104
+ # -----------------------------------------------------------------------------
105
+ .mypy_cache/
106
+ .dmypy.json
107
+ dmypy.json
108
+ .pyre/
109
+ .pytype/
110
+
111
+ # -----------------------------------------------------------------------------
112
+ # Documentation
113
+ # -----------------------------------------------------------------------------
114
+ docs/_build/
115
+ site/
116
+
117
+ # -----------------------------------------------------------------------------
118
+ # Package Management
119
+ # -----------------------------------------------------------------------------
120
+ *.lock
121
+ !poetry.lock
122
+ !uv.lock
123
+ pip-log.txt
124
+ pip-delete-this-directory.txt
125
+
126
+ # -----------------------------------------------------------------------------
127
+ # Hydra
128
+ # -----------------------------------------------------------------------------
129
+ outputs/
130
+ multirun/
131
+ .hydra/
132
+
133
+ # -----------------------------------------------------------------------------
134
+ # Temporary files
135
+ # -----------------------------------------------------------------------------
136
+ *.tmp
137
+ *.temp
138
+ *.bak
139
+ tmp/
140
+ temp/
141
+ *.cache
142
+
143
+ # -----------------------------------------------------------------------------
144
+ # MCP Server artifacts
145
+ # -----------------------------------------------------------------------------
146
+ .mcp/
147
+ mcp_cache/
@@ -0,0 +1,16 @@
1
+ # Changelog
2
+
3
+ ## [0.1.0] - 2026-03-11
4
+
5
+ ### Added
6
+ - MCP-native tool integration with stdio, SSE, and streamable-http transports
7
+ - Three-tier context management: Observation Masking, LLM summarization, emergency pruning
8
+ - Tool call deduplication with hit-count tracking and progressive escalation
9
+ - Execution monitoring with stall/loop detection and three-level escalation
10
+ - Skill system with rules, LLM, and inline selection modes
11
+ - Hook system for lifecycle events (agent, turn, tool)
12
+ - SecureContext for automatic sensitive data isolation
13
+ - Streaming output with structured tag extraction
14
+ - Deep research mode with reflection checkpoints and auto task planning
15
+ - CLI for project initialization, execution, and testing
16
+ - Pydantic-based configuration validation
@@ -0,0 +1,274 @@
1
+ # Mem Deep Research Framework
2
+
3
+ 可扩展的 AI Agent 框架,专注于深度研究任务。基于 MCP 工具协议,支持多 LLM 提供商。
4
+
5
+ ## 项目结构
6
+
7
+ ```
8
+ mem_deep_research_core/ # 框架核心代码
9
+ ├── deep_research.py # 主入口 (DeepResearch 类)
10
+ ├── config_schema.py # Pydantic 配置验证
11
+ ├── exceptions.py # 异常定义
12
+ ├── core/ # 核心模块
13
+ │ ├── orchestrator.py # Agent 编排器(主循环 _run_main_loop)
14
+ │ ├── context_manager.py # 上下文管理(masking + dedup + source registry)
15
+ │ ├── window_strategy.py # 窗口压缩策略(ObservationMasking/LLMSummarize/BinaryReduction)
16
+ │ ├── hooks.py # 钩子系统(HookRegistry, HookContext)
17
+ │ ├── secure_context.py # 隐私数据保护(_secure 字段 → 占位符)
18
+ │ ├── tool_executor.py # 工具执行器
19
+ │ ├── llm_call_handler.py # LLM 调用 + 重试
20
+ │ ├── sub_agent_runner.py # 子 Agent 生命周期
21
+ │ ├── stream_handler.py # SSE 流式输出
22
+ │ ├── monitoring.py # 执行监控 + 循环检测
23
+ │ ├── user_context.py # 用户上下文构建
24
+ │ ├── task_planner.py # LLM 任务分解
25
+ │ ├── message_interceptor.py # 消息拦截
26
+ │ └── answer_handler.py # 最终答案提取
27
+ ├── llm/ # LLM 客户端
28
+ │ ├── provider_client_base.py # Provider 基类
29
+ │ └── providers/ # 8 个 Provider 实现
30
+ ├── prompts/ # Prompt 系统
31
+ │ ├── agent_prompt.py # AgentPrompt 统一类
32
+ │ ├── template_loader.py # 模板加载器
33
+ │ └── templates/ # Markdown 模板(base/presets/extraction/...)
34
+ ├── tool/ # 工具模块
35
+ │ ├── manager.py # ToolManager(MCP 工具管理)
36
+ │ └── mcp_servers/ # 内置 MCP 服务器
37
+ ├── skills/ # Skill 系统
38
+ │ ├── matcher.py # 规则匹配 + 注入
39
+ │ ├── llm_selector.py # LLM Skill 选择
40
+ │ └── inline_selector.py # Inline Skill 选择(零额外开销)
41
+ └── utils/
42
+ ├── external_loader.py # 配置加载器(全局 config_loader/external_loader)
43
+ ├── tool_utils.py # 工具辅助
44
+ └── stream_parsing_utils.py # 流式解析(StructuredTagExtractor, TextInterceptor)
45
+ config/ # 框架默认配置
46
+ ├── agent_example.yaml # Agent 配置示例
47
+ ├── tool/ # 内置工具配置 YAML
48
+ └── skills/definitions/ # Skill 定义
49
+ tests/ # 单元测试
50
+ ```
51
+
52
+ ## 核心概念
53
+
54
+ ### 执行流程
55
+
56
+ ```
57
+ DeepResearch.run(query)
58
+ → Pipeline.run()
59
+ → AgentFactory 创建 Orchestrator + LLM Client + ToolManager
60
+ → Orchestrator._run_main_loop():
61
+ while turn < max_turns:
62
+ 1. Monitor.pre_turn_check()
63
+ 2. LLM 调用
64
+ 3. Monitor.post_turn_check() (循环检测)
65
+ 4. Inline Skill: 解析 <next_skills>
66
+ 5. 解析工具调用 + 跨轮次去重
67
+ 6. 执行工具(SecureContext 自动解占位符)
68
+ 7. Context 管理(ObservationMasking → LLMSummarize → BinaryReduction)
69
+ 8. Hook: on_turn_end
70
+ 9. 反思检查点
71
+ → 生成最终摘要 → ResearchResult
72
+ ```
73
+
74
+ ### 使用方式
75
+
76
+ ```python
77
+ from mem_deep_research import DeepResearch
78
+
79
+ # 方式 1: 从项目目录加载
80
+ dr = DeepResearch.from_project("./my_project")
81
+ result = await dr.run("研究任务")
82
+
83
+ # 方式 2: 代码配置
84
+ dr = DeepResearch(
85
+ llm_provider="anthropic",
86
+ model="claude-sonnet-4-20250514",
87
+ api_key="your-key",
88
+ )
89
+ result = await dr.run("任务")
90
+
91
+ # 同步
92
+ result = dr.run_sync("任务")
93
+ ```
94
+
95
+ ### 项目目录结构
96
+
97
+ 用户项目通过 `DeepResearch.from_project()` 加载:
98
+
99
+ ```
100
+ my_project/
101
+ ├── config/
102
+ │ ├── agent.yaml # Agent 配置(LLM、工具、参数)
103
+ │ ├── tool/ # 项目级工具配置(覆盖框架默认)
104
+ │ ├── skills/definitions/ # 项目级 Skill 定义
105
+ │ └── prompts/ # 自定义 Prompt 模板
106
+ ├── hooks.py # 项目钩子(自动加载)
107
+ ├── .env # API 密钥
108
+ └── run.py # 入口脚本
109
+ ```
110
+
111
+ ## 关键系统详解
112
+
113
+ ### 1. 钩子系统 (`core/hooks.py`)
114
+
115
+ 全局注册表 `hooks = HookRegistry()`,支持的钩子:
116
+
117
+ | 钩子 | 时机 | 可修改 |
118
+ |------|------|--------|
119
+ | `on_agent_start` | Agent 开始 | — |
120
+ | `on_agent_end` | Agent 结束 | — |
121
+ | `on_turn_start` | 每轮开始 | — |
122
+ | `on_turn_end` | 每轮结束 | — |
123
+ | `on_tool_start` | 工具调用前 | arguments |
124
+ | `on_tool_end` | 工具调用后 | tool_result |
125
+ | `on_tool_result_format` | 结果格式化 | 返回值 |
126
+ | `on_thinking_generate` | thinking 描述 | 返回值 |
127
+ | `on_env_inject` | MCP 环境变量 | server_params |
128
+
129
+ 用法:
130
+ ```python
131
+ from mem_deep_research_core.core.hooks import hooks, HookContext
132
+
133
+ @hooks.register("on_tool_end", priority=10)
134
+ def my_hook(ctx: HookContext, original_fn):
135
+ result = original_fn(ctx) # 调用原逻辑
136
+ return modified_result # 或完全替换
137
+ ```
138
+
139
+ ### 2. SecureContext (`core/secure_context.py`)
140
+
141
+ context dict 中的 `_secure` 字段自动在 system prompt 中显示为 `[SECURE:xxx]` 占位符,工具调用前自动替换回真实值。
142
+
143
+ ```python
144
+ context = {
145
+ "user_name": "张三", # 正常显示
146
+ "_secure": {
147
+ "user_id": "real-123", # system prompt 中显示 [SECURE:user_id]
148
+ "org_id": "org-456", # 工具调用时自动替换回 "org-456"
149
+ }
150
+ }
151
+ ```
152
+
153
+ 关键函数:
154
+ - `get_display_value(ctx, field)` — system prompt 用,_secure 字段返回占位符
155
+ - `get_real_value(ctx, field)` — 工具执行用,始终返回真实值
156
+ - `resolve_placeholders_in_args(args, ctx)` — 递归替换工具参数中的占位符
157
+
158
+ ### 3. 上下文管理 (`core/context_manager.py` + `core/window_strategy.py`)
159
+
160
+ 三级窗口压缩策略,通过 `WindowStrategyPipeline` 组合:
161
+
162
+ | 级别 | 策略 | 触发条件 | LLM 成本 |
163
+ |------|------|---------|---------|
164
+ | L1 | ObservationMasking | token 占比 > 60% | 零 |
165
+ | L2 | LLMSummarize | token 占比 > 80% | 一次 LLM 调用 |
166
+ | L3 | BinaryReduction | token 占比 > 95% | 零 |
167
+
168
+ 支持自定义策略:继承 `WindowStrategy` ABC,实现 `should_trigger()` + `apply()`。
169
+
170
+ ### 4. Skill 系统 (`skills/`)
171
+
172
+ 三种选择方式(配置 `skill_selection.method`):
173
+
174
+ | method | 说明 | 开销 |
175
+ |--------|------|------|
176
+ | `rules` | 基于 keywords/tools/context 打分 | 零 |
177
+ | `llm` | 额外 LLM 调用选择 | 一次轻量 LLM |
178
+ | `inline` | LLM 在回复中声明 `<next_skills>` | 零 |
179
+
180
+ Skill 定义格式(`config/skills/definitions/*.md`):
181
+ ```markdown
182
+ ---
183
+ name: search_strategy
184
+ type: knowledge
185
+ description: "搜索策略指南"
186
+ triggers:
187
+ keywords: ["搜索", "查找"]
188
+ tools_mentioned: ["semantic_search"]
189
+ metadata:
190
+ priority: 10
191
+ ---
192
+ # Skill 内容(注入 system prompt)
193
+ ```
194
+
195
+ ### 5. 工具系统 (`tool/manager.py`)
196
+
197
+ 基于 MCP 协议,支持三种传输:
198
+ - **stdio**: 本地进程(`npx`, `python` 脚本)
199
+ - **streamable-http**: HTTP 远程服务
200
+ - **sse**: Server-Sent Events
201
+
202
+ 工具配置 YAML 示例:
203
+ ```yaml
204
+ name: tool-searching
205
+ tool_command: npx
206
+ args: ["-y", "@anthropic/tool-searching"]
207
+ env:
208
+ SERPER_API_KEY: "${oc.env:SERPER_API_KEY,}"
209
+ ```
210
+
211
+ ### 6. Prompt 系统 (`prompts/`)
212
+
213
+ `AgentPrompt` 统一类,通过配置组合模板:
214
+
215
+ ```yaml
216
+ prompt:
217
+ agent_type: main # main | worker
218
+ tool_format: xml # xml | native
219
+ presets: [research, time_sensitive] # 可选预设
220
+ custom_system_template: my_prompt # 自定义模板
221
+ ```
222
+
223
+ 模板位于 `prompts/templates/`,用 `{{variable}}` 占位符,`PromptTemplateLoader` 加载渲染。
224
+
225
+ ### 7. LLM 客户端 (`llm/`)
226
+
227
+ 所有 provider 继承 `LLMProviderClientBase`,关键方法:
228
+ - `call_llm(system_prompt, messages, tool_definitions)` — 调用 LLM
229
+ - `update_message_history(history, tool_results, exceeded)` — 更新消息历史
230
+ - `_estimate_tokens(text)` — token 估算
231
+
232
+ ## 配置说明 (`config_schema.py`)
233
+
234
+ ```yaml
235
+ main_agent:
236
+ prompt:
237
+ agent_type: main
238
+ tool_format: xml
239
+ presets: []
240
+ llm:
241
+ provider_class: "ClaudeOpenRouterClient"
242
+ model_name: "anthropic/claude-sonnet-4"
243
+ temperature: 0.3
244
+ max_tokens: 32000
245
+ max_context_length: 128000 # -1 = 不限制
246
+ keep_tool_result: 5 # -1 全部保留, N 保留最近 N 个
247
+ tool_config: [tool-reading, tool-searching]
248
+ max_turns: 20
249
+ max_tool_calls_per_turn: 10
250
+ chinese_context: false
251
+ skill_selection:
252
+ enabled: true
253
+ method: inline # rules | llm | inline
254
+ max_skills: 3
255
+ context_manager:
256
+ enable_dedup: true
257
+ enable_compact: true
258
+ compact_at_ratio: 0.6
259
+ summarize_at_ratio: 0.8
260
+ compact_keep_recent: 3
261
+ deep_research:
262
+ enabled: false
263
+ reflection_interval: 5
264
+ auto_planning: false
265
+ ```
266
+
267
+ ## 开发规范
268
+
269
+ - Python 3.12+,全异步设计
270
+ - 测试运行:`python -m pytest tests/ -v`
271
+ - 配置验证用 Pydantic,运行时配置用 OmegaConf (Hydra)
272
+ - 工具遵循 MCP (Model Context Protocol) 规范
273
+ - 框架无状态,所有定制通过项目级 config + hooks.py 注入
274
+ - 新增核心功能需在 `config_schema.py` 添加配置项并设合理默认值
@@ -0,0 +1,46 @@
1
+ # Contributing to Mem Deep Research
2
+
3
+ Thank you for your interest in contributing!
4
+
5
+ ## Development Setup
6
+
7
+ ```bash
8
+ git clone https://github.com/cjhyy/mem-deep-research.git
9
+ cd mem-deep-research
10
+ python -m venv .venv
11
+ source .venv/bin/activate
12
+ pip install -e ".[dev]"
13
+ ```
14
+
15
+ ## Running Tests
16
+
17
+ ```bash
18
+ python -m pytest tests/ -v
19
+ ```
20
+
21
+ ## Code Style
22
+
23
+ This project uses [Ruff](https://docs.astral.sh/ruff/) for linting and formatting:
24
+
25
+ ```bash
26
+ ruff check .
27
+ ruff format .
28
+ ```
29
+
30
+ ## Pull Request Process
31
+
32
+ 1. Fork the repository and create your branch from `main`
33
+ 2. Add tests for any new functionality
34
+ 3. Ensure all tests pass
35
+ 4. Update documentation if needed
36
+ 5. Submit a pull request
37
+
38
+ ## Reporting Issues
39
+
40
+ - Use GitHub Issues for bug reports and feature requests
41
+ - Include reproduction steps, expected behavior, and actual behavior
42
+ - For security vulnerabilities, please email chenjunhong54321@163.com directly
43
+
44
+ ## License
45
+
46
+ By contributing, you agree that your contributions will be licensed under the Apache License 2.0.
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to the Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by the Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding any notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2026 Mem Deep Research Contributors
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.