jarvis-ai-assistant 0.1.222__py3-none-any.whl → 0.7.0__py3-none-any.whl

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 (162) hide show
  1. jarvis/__init__.py +1 -1
  2. jarvis/jarvis_agent/__init__.py +1143 -245
  3. jarvis/jarvis_agent/agent_manager.py +97 -0
  4. jarvis/jarvis_agent/builtin_input_handler.py +12 -10
  5. jarvis/jarvis_agent/config_editor.py +57 -0
  6. jarvis/jarvis_agent/edit_file_handler.py +392 -99
  7. jarvis/jarvis_agent/event_bus.py +48 -0
  8. jarvis/jarvis_agent/events.py +157 -0
  9. jarvis/jarvis_agent/file_context_handler.py +79 -0
  10. jarvis/jarvis_agent/file_methodology_manager.py +117 -0
  11. jarvis/jarvis_agent/jarvis.py +1117 -147
  12. jarvis/jarvis_agent/main.py +78 -34
  13. jarvis/jarvis_agent/memory_manager.py +195 -0
  14. jarvis/jarvis_agent/methodology_share_manager.py +174 -0
  15. jarvis/jarvis_agent/prompt_manager.py +82 -0
  16. jarvis/jarvis_agent/prompts.py +46 -9
  17. jarvis/jarvis_agent/protocols.py +4 -1
  18. jarvis/jarvis_agent/rewrite_file_handler.py +141 -0
  19. jarvis/jarvis_agent/run_loop.py +146 -0
  20. jarvis/jarvis_agent/session_manager.py +9 -9
  21. jarvis/jarvis_agent/share_manager.py +228 -0
  22. jarvis/jarvis_agent/shell_input_handler.py +23 -3
  23. jarvis/jarvis_agent/stdio_redirect.py +295 -0
  24. jarvis/jarvis_agent/task_analyzer.py +212 -0
  25. jarvis/jarvis_agent/task_manager.py +154 -0
  26. jarvis/jarvis_agent/task_planner.py +496 -0
  27. jarvis/jarvis_agent/tool_executor.py +8 -4
  28. jarvis/jarvis_agent/tool_share_manager.py +139 -0
  29. jarvis/jarvis_agent/user_interaction.py +42 -0
  30. jarvis/jarvis_agent/utils.py +54 -0
  31. jarvis/jarvis_agent/web_bridge.py +189 -0
  32. jarvis/jarvis_agent/web_output_sink.py +53 -0
  33. jarvis/jarvis_agent/web_server.py +751 -0
  34. jarvis/jarvis_c2rust/__init__.py +26 -0
  35. jarvis/jarvis_c2rust/cli.py +613 -0
  36. jarvis/jarvis_c2rust/collector.py +258 -0
  37. jarvis/jarvis_c2rust/library_replacer.py +1122 -0
  38. jarvis/jarvis_c2rust/llm_module_agent.py +1300 -0
  39. jarvis/jarvis_c2rust/optimizer.py +960 -0
  40. jarvis/jarvis_c2rust/scanner.py +1681 -0
  41. jarvis/jarvis_c2rust/transpiler.py +2325 -0
  42. jarvis/jarvis_code_agent/build_validation_config.py +133 -0
  43. jarvis/jarvis_code_agent/code_agent.py +1605 -178
  44. jarvis/jarvis_code_agent/code_analyzer/__init__.py +62 -0
  45. jarvis/jarvis_code_agent/code_analyzer/base_language.py +74 -0
  46. jarvis/jarvis_code_agent/code_analyzer/build_validator/__init__.py +44 -0
  47. jarvis/jarvis_code_agent/code_analyzer/build_validator/base.py +102 -0
  48. jarvis/jarvis_code_agent/code_analyzer/build_validator/cmake.py +59 -0
  49. jarvis/jarvis_code_agent/code_analyzer/build_validator/detector.py +125 -0
  50. jarvis/jarvis_code_agent/code_analyzer/build_validator/fallback.py +69 -0
  51. jarvis/jarvis_code_agent/code_analyzer/build_validator/go.py +38 -0
  52. jarvis/jarvis_code_agent/code_analyzer/build_validator/java_gradle.py +44 -0
  53. jarvis/jarvis_code_agent/code_analyzer/build_validator/java_maven.py +38 -0
  54. jarvis/jarvis_code_agent/code_analyzer/build_validator/makefile.py +50 -0
  55. jarvis/jarvis_code_agent/code_analyzer/build_validator/nodejs.py +93 -0
  56. jarvis/jarvis_code_agent/code_analyzer/build_validator/python.py +129 -0
  57. jarvis/jarvis_code_agent/code_analyzer/build_validator/rust.py +54 -0
  58. jarvis/jarvis_code_agent/code_analyzer/build_validator/validator.py +154 -0
  59. jarvis/jarvis_code_agent/code_analyzer/build_validator.py +43 -0
  60. jarvis/jarvis_code_agent/code_analyzer/context_manager.py +363 -0
  61. jarvis/jarvis_code_agent/code_analyzer/context_recommender.py +18 -0
  62. jarvis/jarvis_code_agent/code_analyzer/dependency_analyzer.py +132 -0
  63. jarvis/jarvis_code_agent/code_analyzer/file_ignore.py +330 -0
  64. jarvis/jarvis_code_agent/code_analyzer/impact_analyzer.py +781 -0
  65. jarvis/jarvis_code_agent/code_analyzer/language_registry.py +185 -0
  66. jarvis/jarvis_code_agent/code_analyzer/language_support.py +89 -0
  67. jarvis/jarvis_code_agent/code_analyzer/languages/__init__.py +31 -0
  68. jarvis/jarvis_code_agent/code_analyzer/languages/c_cpp_language.py +231 -0
  69. jarvis/jarvis_code_agent/code_analyzer/languages/go_language.py +183 -0
  70. jarvis/jarvis_code_agent/code_analyzer/languages/python_language.py +219 -0
  71. jarvis/jarvis_code_agent/code_analyzer/languages/rust_language.py +209 -0
  72. jarvis/jarvis_code_agent/code_analyzer/llm_context_recommender.py +451 -0
  73. jarvis/jarvis_code_agent/code_analyzer/symbol_extractor.py +77 -0
  74. jarvis/jarvis_code_agent/code_analyzer/tree_sitter_extractor.py +48 -0
  75. jarvis/jarvis_code_agent/lint.py +275 -13
  76. jarvis/jarvis_code_agent/utils.py +142 -0
  77. jarvis/jarvis_code_analysis/checklists/loader.py +20 -6
  78. jarvis/jarvis_code_analysis/code_review.py +583 -548
  79. jarvis/jarvis_data/config_schema.json +339 -28
  80. jarvis/jarvis_git_squash/main.py +22 -13
  81. jarvis/jarvis_git_utils/git_commiter.py +171 -55
  82. jarvis/jarvis_mcp/sse_mcp_client.py +22 -15
  83. jarvis/jarvis_mcp/stdio_mcp_client.py +4 -4
  84. jarvis/jarvis_mcp/streamable_mcp_client.py +36 -16
  85. jarvis/jarvis_memory_organizer/memory_organizer.py +753 -0
  86. jarvis/jarvis_methodology/main.py +48 -63
  87. jarvis/jarvis_multi_agent/__init__.py +302 -43
  88. jarvis/jarvis_multi_agent/main.py +70 -24
  89. jarvis/jarvis_platform/ai8.py +40 -23
  90. jarvis/jarvis_platform/base.py +210 -49
  91. jarvis/jarvis_platform/human.py +11 -1
  92. jarvis/jarvis_platform/kimi.py +82 -76
  93. jarvis/jarvis_platform/openai.py +73 -1
  94. jarvis/jarvis_platform/registry.py +8 -15
  95. jarvis/jarvis_platform/tongyi.py +115 -101
  96. jarvis/jarvis_platform/yuanbao.py +89 -63
  97. jarvis/jarvis_platform_manager/main.py +194 -132
  98. jarvis/jarvis_platform_manager/service.py +122 -86
  99. jarvis/jarvis_rag/cli.py +156 -53
  100. jarvis/jarvis_rag/embedding_manager.py +155 -12
  101. jarvis/jarvis_rag/llm_interface.py +10 -13
  102. jarvis/jarvis_rag/query_rewriter.py +63 -12
  103. jarvis/jarvis_rag/rag_pipeline.py +222 -40
  104. jarvis/jarvis_rag/reranker.py +26 -3
  105. jarvis/jarvis_rag/retriever.py +270 -14
  106. jarvis/jarvis_sec/__init__.py +3605 -0
  107. jarvis/jarvis_sec/checkers/__init__.py +32 -0
  108. jarvis/jarvis_sec/checkers/c_checker.py +2680 -0
  109. jarvis/jarvis_sec/checkers/rust_checker.py +1108 -0
  110. jarvis/jarvis_sec/cli.py +116 -0
  111. jarvis/jarvis_sec/report.py +257 -0
  112. jarvis/jarvis_sec/status.py +264 -0
  113. jarvis/jarvis_sec/types.py +20 -0
  114. jarvis/jarvis_sec/workflow.py +219 -0
  115. jarvis/jarvis_smart_shell/main.py +405 -137
  116. jarvis/jarvis_stats/__init__.py +13 -0
  117. jarvis/jarvis_stats/cli.py +387 -0
  118. jarvis/jarvis_stats/stats.py +711 -0
  119. jarvis/jarvis_stats/storage.py +612 -0
  120. jarvis/jarvis_stats/visualizer.py +282 -0
  121. jarvis/jarvis_tools/ask_user.py +1 -0
  122. jarvis/jarvis_tools/base.py +18 -2
  123. jarvis/jarvis_tools/clear_memory.py +239 -0
  124. jarvis/jarvis_tools/cli/main.py +220 -144
  125. jarvis/jarvis_tools/execute_script.py +52 -12
  126. jarvis/jarvis_tools/file_analyzer.py +17 -12
  127. jarvis/jarvis_tools/generate_new_tool.py +46 -24
  128. jarvis/jarvis_tools/read_code.py +277 -18
  129. jarvis/jarvis_tools/read_symbols.py +141 -0
  130. jarvis/jarvis_tools/read_webpage.py +86 -13
  131. jarvis/jarvis_tools/registry.py +294 -90
  132. jarvis/jarvis_tools/retrieve_memory.py +227 -0
  133. jarvis/jarvis_tools/save_memory.py +194 -0
  134. jarvis/jarvis_tools/search_web.py +62 -28
  135. jarvis/jarvis_tools/sub_agent.py +205 -0
  136. jarvis/jarvis_tools/sub_code_agent.py +217 -0
  137. jarvis/jarvis_tools/virtual_tty.py +330 -62
  138. jarvis/jarvis_utils/builtin_replace_map.py +4 -5
  139. jarvis/jarvis_utils/clipboard.py +90 -0
  140. jarvis/jarvis_utils/config.py +607 -50
  141. jarvis/jarvis_utils/embedding.py +3 -0
  142. jarvis/jarvis_utils/fzf.py +57 -0
  143. jarvis/jarvis_utils/git_utils.py +251 -29
  144. jarvis/jarvis_utils/globals.py +174 -17
  145. jarvis/jarvis_utils/http.py +58 -79
  146. jarvis/jarvis_utils/input.py +899 -153
  147. jarvis/jarvis_utils/methodology.py +210 -83
  148. jarvis/jarvis_utils/output.py +220 -137
  149. jarvis/jarvis_utils/utils.py +1906 -135
  150. jarvis_ai_assistant-0.7.0.dist-info/METADATA +465 -0
  151. jarvis_ai_assistant-0.7.0.dist-info/RECORD +192 -0
  152. {jarvis_ai_assistant-0.1.222.dist-info → jarvis_ai_assistant-0.7.0.dist-info}/entry_points.txt +8 -2
  153. jarvis/jarvis_git_details/main.py +0 -265
  154. jarvis/jarvis_platform/oyi.py +0 -357
  155. jarvis/jarvis_tools/edit_file.py +0 -255
  156. jarvis/jarvis_tools/rewrite_file.py +0 -195
  157. jarvis_ai_assistant-0.1.222.dist-info/METADATA +0 -767
  158. jarvis_ai_assistant-0.1.222.dist-info/RECORD +0 -110
  159. /jarvis/{jarvis_git_details → jarvis_memory_organizer}/__init__.py +0 -0
  160. {jarvis_ai_assistant-0.1.222.dist-info → jarvis_ai_assistant-0.7.0.dist-info}/WHEEL +0 -0
  161. {jarvis_ai_assistant-0.1.222.dist-info → jarvis_ai_assistant-0.7.0.dist-info}/licenses/LICENSE +0 -0
  162. {jarvis_ai_assistant-0.1.222.dist-info → jarvis_ai_assistant-0.7.0.dist-info}/top_level.txt +0 -0
@@ -14,9 +14,15 @@ from typing import Any, Dict, List, Optional
14
14
 
15
15
  from jarvis.jarvis_platform.base import BasePlatform
16
16
  from jarvis.jarvis_platform.registry import PlatformRegistry
17
- from jarvis.jarvis_utils.config import get_data_dir
17
+ from jarvis.jarvis_utils.config import (
18
+ get_data_dir,
19
+ get_methodology_dirs,
20
+ get_central_methodology_repo,
21
+ get_max_input_token_count,
22
+ )
18
23
  from jarvis.jarvis_utils.output import OutputType, PrettyOutput
19
- from jarvis.jarvis_utils.utils import is_context_overflow
24
+ from jarvis.jarvis_utils.utils import daily_check_git_updates
25
+ from jarvis.jarvis_utils.embedding import get_context_token_count
20
26
 
21
27
 
22
28
  def _get_methodology_directory() -> str:
@@ -37,33 +43,76 @@ def _get_methodology_directory() -> str:
37
43
 
38
44
  def _load_all_methodologies() -> Dict[str, str]:
39
45
  """
40
- 加载所有方法论文件
46
+ 从默认目录和配置的外部目录加载所有方法论文件。
41
47
 
42
48
  返回:
43
- Dict[str, str]: 方法论字典,键为问题类型,值为方法论内容
49
+ Dict[str, str]: 方法论字典,键为问题类型,值为方法论内容。
44
50
  """
45
- methodology_dir = _get_methodology_directory()
46
51
  all_methodologies = {}
52
+ methodology_dirs = [_get_methodology_directory()] + get_methodology_dirs()
53
+
54
+ # 如果配置了中心方法论仓库,将其添加到加载路径
55
+ central_repo = get_central_methodology_repo()
56
+ if central_repo:
57
+ # 支持本地目录路径或Git仓库URL
58
+ expanded = os.path.expanduser(os.path.expandvars(central_repo))
59
+ if os.path.isdir(expanded):
60
+ # 直接使用本地目录(支持Git仓库的子目录)
61
+ methodology_dirs.append(expanded)
62
+ else:
63
+ # 中心方法论仓库存储在数据目录下的特定位置
64
+ central_repo_path = os.path.join(get_data_dir(), "central_methodology_repo")
65
+ methodology_dirs.append(central_repo_path)
66
+
67
+ # 确保中心方法论仓库被克隆/更新
68
+ if not os.path.exists(central_repo_path):
69
+ try:
70
+ import subprocess
47
71
 
48
- if not os.path.exists(methodology_dir):
49
- return all_methodologies
72
+ PrettyOutput.print(
73
+ f"正在克隆中心方法论仓库: {central_repo}", OutputType.INFO
74
+ )
75
+ subprocess.run(
76
+ ["git", "clone", central_repo, central_repo_path], check=True
77
+ )
78
+ except Exception as e:
79
+ PrettyOutput.print(
80
+ f"克隆中心方法论仓库失败: {str(e)}", OutputType.ERROR
81
+ )
82
+
83
+ # --- 全局每日更新检查 ---
84
+ daily_check_git_updates(methodology_dirs, "methodologies")
50
85
 
51
86
  import glob
52
87
 
53
- for filepath in glob.glob(os.path.join(methodology_dir, "*.json")):
54
- try:
55
- with open(filepath, "r", encoding="utf-8", errors="ignore") as f:
56
- methodology = json.load(f)
57
- problem_type = methodology.get("problem_type", "")
58
- content = methodology.get("content", "")
59
- if problem_type and content:
60
- all_methodologies[problem_type] = content
61
- except Exception as e:
62
- filename = os.path.basename(filepath)
63
- PrettyOutput.print(
64
- f"加载方法论文件 {filename} 失败: {str(e)}", OutputType.WARNING
65
- )
88
+ # 收集循环中的提示,统一打印,避免逐条加框
89
+ warn_dirs: List[str] = []
90
+ error_lines: List[str] = []
91
+
92
+ for directory in set(methodology_dirs): # Use set to avoid duplicates
93
+ if not os.path.isdir(directory):
94
+ warn_dirs.append(f"警告: 方法论目录不存在或不是一个目录: {directory}")
95
+ continue
66
96
 
97
+ for filepath in glob.glob(os.path.join(directory, "*.json")):
98
+ try:
99
+ with open(filepath, "r", encoding="utf-8", errors="ignore") as f:
100
+ methodology = json.load(f)
101
+ problem_type = methodology.get("problem_type", "")
102
+ content = methodology.get("content", "")
103
+ if problem_type and content:
104
+ if problem_type in all_methodologies:
105
+ pass
106
+ all_methodologies[problem_type] = content
107
+ except Exception as e:
108
+ filename = os.path.basename(filepath)
109
+ error_lines.append(f"加载方法论文件 {filename} 失败: {str(e)}")
110
+
111
+ # 统一打印目录警告与文件加载失败信息
112
+ if warn_dirs:
113
+ PrettyOutput.print("\n".join(warn_dirs), OutputType.WARNING)
114
+ if error_lines:
115
+ PrettyOutput.print("\n".join(error_lines), OutputType.WARNING)
67
116
  return all_methodologies
68
117
 
69
118
 
@@ -135,12 +184,20 @@ def upload_methodology(platform: BasePlatform, other_files: List[str] = []) -> b
135
184
  pass
136
185
 
137
186
 
138
- def load_methodology(user_input: str, tool_registery: Optional[Any] = None) -> str:
187
+ def load_methodology(
188
+ user_input: str,
189
+ tool_registery: Optional[Any] = None,
190
+ platform_name: Optional[str] = None,
191
+ model_name: Optional[str] = None,
192
+ ) -> str:
139
193
  """
140
194
  加载方法论并上传到大模型。
141
195
 
142
196
  参数:
143
197
  user_input: 用户输入文本,用于提示大模型
198
+ tool_registery: 工具注册表,用于获取工具列表
199
+ platform_name (str, optional): 指定的平台名称. Defaults to None.
200
+ model_name (str, optional): 指定的模型名称. Defaults to None.
144
201
 
145
202
  返回:
146
203
  str: 相关的方法论提示,如果未找到方法论则返回空字符串
@@ -155,71 +212,113 @@ def load_methodology(user_input: str, tool_registery: Optional[Any] = None) -> s
155
212
 
156
213
  try:
157
214
  # 加载所有方法论
158
- print(f"📁 加载方法论文件...")
215
+ PrettyOutput.print("📁 加载方法论文件...", OutputType.INFO)
159
216
  methodologies = _load_all_methodologies()
160
217
  if not methodologies:
161
- print(f"没有找到方法论文件")
218
+ PrettyOutput.print("没有找到方法论文件", OutputType.WARNING)
162
219
  return ""
163
- print(f"✅ 加载方法论文件完成 (共 {len(methodologies)} 个)")
220
+ PrettyOutput.print(
221
+ f"加载方法论文件完成 (共 {len(methodologies)} 个)", OutputType.SUCCESS
222
+ )
223
+
224
+ if platform_name:
225
+ platform = PlatformRegistry().create_platform(platform_name)
226
+ if platform and model_name:
227
+ platform.set_model_name(model_name)
228
+ else:
229
+ platform = PlatformRegistry().get_normal_platform()
164
230
 
165
- # 获取当前平台
166
- platform = PlatformRegistry().get_normal_platform()
167
- platform.set_suppress_output(False)
168
231
  if not platform:
232
+ PrettyOutput.print("无法创建平台实例", OutputType.ERROR)
169
233
  return ""
170
234
 
171
- # 构建基础提示信息
172
- base_prompt = f"""以下是所有可用的方法论内容:
235
+ platform.set_suppress_output(True)
236
+
237
+ # 步骤1:获取所有方法论的标题
238
+ methodology_titles = list(methodologies.keys())
239
+
240
+ # 步骤2:让大模型选择相关性高的方法论
241
+ selection_prompt = """以下是所有可用的方法论标题:
173
242
 
174
243
  """
175
- # 构建完整内容
176
- full_content = base_prompt
177
- for problem_type, content in methodologies.items():
178
- full_content += f"## {problem_type}\n\n{content}\n\n---\n\n"
244
+ for i, title in enumerate(methodology_titles, 1):
245
+ selection_prompt += f"{i}. {title}\n"
179
246
 
180
- full_content += f"以下是所有可用的工具内容:\n\n"
181
- full_content += prompt
247
+ selection_prompt += f"""
248
+ 以下是可用的工具列表:
249
+ {prompt}
182
250
 
183
- # 添加用户输入和输出要求
184
- full_content += f"""
185
- 请根据以上方法论和可调用的工具内容,规划/总结出以下用户需求的执行步骤: {user_input}
251
+ 用户需求:{user_input}
186
252
 
187
- 请按以下格式回复:
188
- ### 与该任务/需求相关的方法论
189
- 1. [方法论名字]
190
- 2. [方法论名字]
191
- ### 根据以上方法论,规划/总结出执行步骤
192
- 1. [步骤1]
193
- 2. [步骤2]
194
- 3. [步骤3]
253
+ 请分析用户需求,从上述方法论中选择出与需求相关性较高的方法论(可以选择多个)。
195
254
 
196
- 如果没有匹配的方法论,请输出:没有历史方法论可参考
197
- 除以上要求外,不要输出任何内容
255
+ 请严格按照以下格式返回序号:
256
+ <NUM>序号1,序号2,序号3</NUM>
257
+
258
+ 例如:<NUM>1,3,5</NUM>
259
+
260
+ 如果没有相关的方法论,请返回:<NUM>none</NUM>
261
+
262
+ 注意:只返回<NUM>标签内的内容,不要有其他任何输出。
198
263
  """
199
264
 
200
- # 检查内容是否过大
201
- is_large_content = is_context_overflow(full_content)
202
- temp_file_path = None
265
+ # 获取大模型选择的方法论序号
266
+ response = platform.chat_until_success(selection_prompt).strip()
267
+
268
+ # 重置平台,恢复输出
269
+ platform.reset()
270
+ platform.set_suppress_output(False)
271
+
272
+ # 从响应中提取<NUM>标签内的内容
273
+ import re
274
+
275
+ num_match = re.search(r"<NUM>(.*?)</NUM>", response, re.DOTALL)
203
276
 
277
+ if not num_match:
278
+ # 如果没有找到<NUM>标签,尝试直接解析响应
279
+ selected_indices_str = response
280
+ else:
281
+ selected_indices_str = num_match.group(1).strip()
282
+
283
+ if selected_indices_str.lower() == "none":
284
+ return "没有历史方法论可参考"
285
+
286
+ # 解析选择的序号
287
+ selected_methodologies = {}
204
288
  try:
205
- if is_large_content:
206
- # 创建临时文件
207
- print(f"📝 创建方法论临时文件...")
208
- temp_file_path = _create_methodology_temp_file(methodologies)
209
- if not temp_file_path:
210
- print(f"❌ 创建方法论临时文件失败")
211
- return ""
212
- print(f"✅ 创建方法论临时文件完成")
213
-
214
- # 尝试上传文件
215
- upload_success = platform.upload_files([temp_file_path])
216
-
217
- if upload_success:
218
- # 使用上传的文件生成摘要
219
- return platform.chat_until_success(
220
- base_prompt
221
- + f"""
222
- 请根据已上传的方法论和可调用的工具文件内容,规划/总结出以下用户需求的执行步骤: {user_input}
289
+ if selected_indices_str:
290
+ indices = [
291
+ int(idx.strip())
292
+ for idx in selected_indices_str.split(",")
293
+ if idx.strip().isdigit()
294
+ ]
295
+ for idx in indices:
296
+ if 1 <= idx <= len(methodology_titles):
297
+ title = methodology_titles[idx - 1]
298
+ selected_methodologies[title] = methodologies[title]
299
+ except Exception:
300
+ # 如果解析失败,返回空结果
301
+ return "没有历史方法论可参考"
302
+
303
+ if not selected_methodologies:
304
+ return "没有历史方法论可参考"
305
+
306
+ # 获取最大输入token数的2/3作为方法论的token限制
307
+ max_input_tokens = get_max_input_token_count()
308
+ methodology_token_limit = int(max_input_tokens * 2 / 3)
309
+
310
+ # 步骤3:将选择出来的方法论内容提供给大模型生成步骤
311
+ # 首先构建基础提示词部分
312
+ base_prompt = """以下是与用户需求相关的方法论内容:
313
+
314
+ """
315
+ suffix_prompt = f"""以下是所有可用的工具内容:
316
+
317
+ {prompt}
318
+
319
+ 用户需求:{user_input}
320
+
321
+ 请根据以上方法论和可调用的工具内容,规划/总结出执行步骤。
223
322
 
224
323
  请按以下格式回复:
225
324
  ### 与该任务/需求相关的方法论
@@ -230,22 +329,50 @@ def load_methodology(user_input: str, tool_registery: Optional[Any] = None) -> s
230
329
  2. [步骤2]
231
330
  3. [步骤3]
232
331
 
233
- 如果没有匹配的方法论,请输出:没有历史方法论可参考
234
332
  除以上要求外,不要输出任何内容
235
333
  """
236
- )
237
- else:
238
- return "没有历史方法论可参考"
239
- # 如果内容不大或上传失败,直接使用chat_until_success
240
- return platform.chat_until_success(full_content)
241
-
242
- finally:
243
- # 清理临时文件
244
- if temp_file_path and os.path.exists(temp_file_path):
245
- try:
246
- os.remove(temp_file_path)
247
- except Exception:
248
- pass
334
+
335
+ # 计算基础部分的token数
336
+ base_tokens = get_context_token_count(base_prompt + suffix_prompt)
337
+ available_tokens = methodology_token_limit - base_tokens
338
+
339
+ # 基于token限制筛选方法论内容
340
+ final_prompt = base_prompt
341
+ selected_count = 0
342
+ total_methodology_tokens = 0
343
+
344
+ for problem_type, content in selected_methodologies.items():
345
+ methodology_text = f"## {problem_type}\n\n{content}\n\n---\n\n"
346
+ methodology_tokens = get_context_token_count(methodology_text)
347
+
348
+ # 检查是否会超过token限制
349
+ if total_methodology_tokens + methodology_tokens > available_tokens:
350
+ PrettyOutput.print(
351
+ f"达到方法论token限制 ({total_methodology_tokens}/{available_tokens}),停止加载更多方法论",
352
+ OutputType.INFO,
353
+ )
354
+ break
355
+
356
+ final_prompt += methodology_text
357
+ total_methodology_tokens += methodology_tokens
358
+ selected_count += 1
359
+
360
+ # 如果一个方法论都没有加载成功
361
+ if selected_count == 0:
362
+ PrettyOutput.print(
363
+ "警告:由于token限制,无法加载任何方法论内容", OutputType.WARNING
364
+ )
365
+ return "没有历史方法论可参考"
366
+
367
+ final_prompt += suffix_prompt
368
+
369
+ PrettyOutput.print(
370
+ f"成功加载 {selected_count} 个方法论,总token数: {total_methodology_tokens}",
371
+ OutputType.INFO,
372
+ )
373
+
374
+ # 如果内容不大,直接使用chat_until_success
375
+ return platform.chat_until_success(final_prompt)
249
376
 
250
377
  except Exception as e:
251
378
  PrettyOutput.print(f"加载方法论失败: {str(e)}", OutputType.ERROR)