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.
- jarvis/__init__.py +1 -1
- jarvis/jarvis_agent/__init__.py +1143 -245
- jarvis/jarvis_agent/agent_manager.py +97 -0
- jarvis/jarvis_agent/builtin_input_handler.py +12 -10
- jarvis/jarvis_agent/config_editor.py +57 -0
- jarvis/jarvis_agent/edit_file_handler.py +392 -99
- jarvis/jarvis_agent/event_bus.py +48 -0
- jarvis/jarvis_agent/events.py +157 -0
- jarvis/jarvis_agent/file_context_handler.py +79 -0
- jarvis/jarvis_agent/file_methodology_manager.py +117 -0
- jarvis/jarvis_agent/jarvis.py +1117 -147
- jarvis/jarvis_agent/main.py +78 -34
- jarvis/jarvis_agent/memory_manager.py +195 -0
- jarvis/jarvis_agent/methodology_share_manager.py +174 -0
- jarvis/jarvis_agent/prompt_manager.py +82 -0
- jarvis/jarvis_agent/prompts.py +46 -9
- jarvis/jarvis_agent/protocols.py +4 -1
- jarvis/jarvis_agent/rewrite_file_handler.py +141 -0
- jarvis/jarvis_agent/run_loop.py +146 -0
- jarvis/jarvis_agent/session_manager.py +9 -9
- jarvis/jarvis_agent/share_manager.py +228 -0
- jarvis/jarvis_agent/shell_input_handler.py +23 -3
- jarvis/jarvis_agent/stdio_redirect.py +295 -0
- jarvis/jarvis_agent/task_analyzer.py +212 -0
- jarvis/jarvis_agent/task_manager.py +154 -0
- jarvis/jarvis_agent/task_planner.py +496 -0
- jarvis/jarvis_agent/tool_executor.py +8 -4
- jarvis/jarvis_agent/tool_share_manager.py +139 -0
- jarvis/jarvis_agent/user_interaction.py +42 -0
- jarvis/jarvis_agent/utils.py +54 -0
- jarvis/jarvis_agent/web_bridge.py +189 -0
- jarvis/jarvis_agent/web_output_sink.py +53 -0
- jarvis/jarvis_agent/web_server.py +751 -0
- jarvis/jarvis_c2rust/__init__.py +26 -0
- jarvis/jarvis_c2rust/cli.py +613 -0
- jarvis/jarvis_c2rust/collector.py +258 -0
- jarvis/jarvis_c2rust/library_replacer.py +1122 -0
- jarvis/jarvis_c2rust/llm_module_agent.py +1300 -0
- jarvis/jarvis_c2rust/optimizer.py +960 -0
- jarvis/jarvis_c2rust/scanner.py +1681 -0
- jarvis/jarvis_c2rust/transpiler.py +2325 -0
- jarvis/jarvis_code_agent/build_validation_config.py +133 -0
- jarvis/jarvis_code_agent/code_agent.py +1605 -178
- jarvis/jarvis_code_agent/code_analyzer/__init__.py +62 -0
- jarvis/jarvis_code_agent/code_analyzer/base_language.py +74 -0
- jarvis/jarvis_code_agent/code_analyzer/build_validator/__init__.py +44 -0
- jarvis/jarvis_code_agent/code_analyzer/build_validator/base.py +102 -0
- jarvis/jarvis_code_agent/code_analyzer/build_validator/cmake.py +59 -0
- jarvis/jarvis_code_agent/code_analyzer/build_validator/detector.py +125 -0
- jarvis/jarvis_code_agent/code_analyzer/build_validator/fallback.py +69 -0
- jarvis/jarvis_code_agent/code_analyzer/build_validator/go.py +38 -0
- jarvis/jarvis_code_agent/code_analyzer/build_validator/java_gradle.py +44 -0
- jarvis/jarvis_code_agent/code_analyzer/build_validator/java_maven.py +38 -0
- jarvis/jarvis_code_agent/code_analyzer/build_validator/makefile.py +50 -0
- jarvis/jarvis_code_agent/code_analyzer/build_validator/nodejs.py +93 -0
- jarvis/jarvis_code_agent/code_analyzer/build_validator/python.py +129 -0
- jarvis/jarvis_code_agent/code_analyzer/build_validator/rust.py +54 -0
- jarvis/jarvis_code_agent/code_analyzer/build_validator/validator.py +154 -0
- jarvis/jarvis_code_agent/code_analyzer/build_validator.py +43 -0
- jarvis/jarvis_code_agent/code_analyzer/context_manager.py +363 -0
- jarvis/jarvis_code_agent/code_analyzer/context_recommender.py +18 -0
- jarvis/jarvis_code_agent/code_analyzer/dependency_analyzer.py +132 -0
- jarvis/jarvis_code_agent/code_analyzer/file_ignore.py +330 -0
- jarvis/jarvis_code_agent/code_analyzer/impact_analyzer.py +781 -0
- jarvis/jarvis_code_agent/code_analyzer/language_registry.py +185 -0
- jarvis/jarvis_code_agent/code_analyzer/language_support.py +89 -0
- jarvis/jarvis_code_agent/code_analyzer/languages/__init__.py +31 -0
- jarvis/jarvis_code_agent/code_analyzer/languages/c_cpp_language.py +231 -0
- jarvis/jarvis_code_agent/code_analyzer/languages/go_language.py +183 -0
- jarvis/jarvis_code_agent/code_analyzer/languages/python_language.py +219 -0
- jarvis/jarvis_code_agent/code_analyzer/languages/rust_language.py +209 -0
- jarvis/jarvis_code_agent/code_analyzer/llm_context_recommender.py +451 -0
- jarvis/jarvis_code_agent/code_analyzer/symbol_extractor.py +77 -0
- jarvis/jarvis_code_agent/code_analyzer/tree_sitter_extractor.py +48 -0
- jarvis/jarvis_code_agent/lint.py +275 -13
- jarvis/jarvis_code_agent/utils.py +142 -0
- jarvis/jarvis_code_analysis/checklists/loader.py +20 -6
- jarvis/jarvis_code_analysis/code_review.py +583 -548
- jarvis/jarvis_data/config_schema.json +339 -28
- jarvis/jarvis_git_squash/main.py +22 -13
- jarvis/jarvis_git_utils/git_commiter.py +171 -55
- jarvis/jarvis_mcp/sse_mcp_client.py +22 -15
- jarvis/jarvis_mcp/stdio_mcp_client.py +4 -4
- jarvis/jarvis_mcp/streamable_mcp_client.py +36 -16
- jarvis/jarvis_memory_organizer/memory_organizer.py +753 -0
- jarvis/jarvis_methodology/main.py +48 -63
- jarvis/jarvis_multi_agent/__init__.py +302 -43
- jarvis/jarvis_multi_agent/main.py +70 -24
- jarvis/jarvis_platform/ai8.py +40 -23
- jarvis/jarvis_platform/base.py +210 -49
- jarvis/jarvis_platform/human.py +11 -1
- jarvis/jarvis_platform/kimi.py +82 -76
- jarvis/jarvis_platform/openai.py +73 -1
- jarvis/jarvis_platform/registry.py +8 -15
- jarvis/jarvis_platform/tongyi.py +115 -101
- jarvis/jarvis_platform/yuanbao.py +89 -63
- jarvis/jarvis_platform_manager/main.py +194 -132
- jarvis/jarvis_platform_manager/service.py +122 -86
- jarvis/jarvis_rag/cli.py +156 -53
- jarvis/jarvis_rag/embedding_manager.py +155 -12
- jarvis/jarvis_rag/llm_interface.py +10 -13
- jarvis/jarvis_rag/query_rewriter.py +63 -12
- jarvis/jarvis_rag/rag_pipeline.py +222 -40
- jarvis/jarvis_rag/reranker.py +26 -3
- jarvis/jarvis_rag/retriever.py +270 -14
- jarvis/jarvis_sec/__init__.py +3605 -0
- jarvis/jarvis_sec/checkers/__init__.py +32 -0
- jarvis/jarvis_sec/checkers/c_checker.py +2680 -0
- jarvis/jarvis_sec/checkers/rust_checker.py +1108 -0
- jarvis/jarvis_sec/cli.py +116 -0
- jarvis/jarvis_sec/report.py +257 -0
- jarvis/jarvis_sec/status.py +264 -0
- jarvis/jarvis_sec/types.py +20 -0
- jarvis/jarvis_sec/workflow.py +219 -0
- jarvis/jarvis_smart_shell/main.py +405 -137
- jarvis/jarvis_stats/__init__.py +13 -0
- jarvis/jarvis_stats/cli.py +387 -0
- jarvis/jarvis_stats/stats.py +711 -0
- jarvis/jarvis_stats/storage.py +612 -0
- jarvis/jarvis_stats/visualizer.py +282 -0
- jarvis/jarvis_tools/ask_user.py +1 -0
- jarvis/jarvis_tools/base.py +18 -2
- jarvis/jarvis_tools/clear_memory.py +239 -0
- jarvis/jarvis_tools/cli/main.py +220 -144
- jarvis/jarvis_tools/execute_script.py +52 -12
- jarvis/jarvis_tools/file_analyzer.py +17 -12
- jarvis/jarvis_tools/generate_new_tool.py +46 -24
- jarvis/jarvis_tools/read_code.py +277 -18
- jarvis/jarvis_tools/read_symbols.py +141 -0
- jarvis/jarvis_tools/read_webpage.py +86 -13
- jarvis/jarvis_tools/registry.py +294 -90
- jarvis/jarvis_tools/retrieve_memory.py +227 -0
- jarvis/jarvis_tools/save_memory.py +194 -0
- jarvis/jarvis_tools/search_web.py +62 -28
- jarvis/jarvis_tools/sub_agent.py +205 -0
- jarvis/jarvis_tools/sub_code_agent.py +217 -0
- jarvis/jarvis_tools/virtual_tty.py +330 -62
- jarvis/jarvis_utils/builtin_replace_map.py +4 -5
- jarvis/jarvis_utils/clipboard.py +90 -0
- jarvis/jarvis_utils/config.py +607 -50
- jarvis/jarvis_utils/embedding.py +3 -0
- jarvis/jarvis_utils/fzf.py +57 -0
- jarvis/jarvis_utils/git_utils.py +251 -29
- jarvis/jarvis_utils/globals.py +174 -17
- jarvis/jarvis_utils/http.py +58 -79
- jarvis/jarvis_utils/input.py +899 -153
- jarvis/jarvis_utils/methodology.py +210 -83
- jarvis/jarvis_utils/output.py +220 -137
- jarvis/jarvis_utils/utils.py +1906 -135
- jarvis_ai_assistant-0.7.0.dist-info/METADATA +465 -0
- jarvis_ai_assistant-0.7.0.dist-info/RECORD +192 -0
- {jarvis_ai_assistant-0.1.222.dist-info → jarvis_ai_assistant-0.7.0.dist-info}/entry_points.txt +8 -2
- jarvis/jarvis_git_details/main.py +0 -265
- jarvis/jarvis_platform/oyi.py +0 -357
- jarvis/jarvis_tools/edit_file.py +0 -255
- jarvis/jarvis_tools/rewrite_file.py +0 -195
- jarvis_ai_assistant-0.1.222.dist-info/METADATA +0 -767
- jarvis_ai_assistant-0.1.222.dist-info/RECORD +0 -110
- /jarvis/{jarvis_git_details → jarvis_memory_organizer}/__init__.py +0 -0
- {jarvis_ai_assistant-0.1.222.dist-info → jarvis_ai_assistant-0.7.0.dist-info}/WHEEL +0 -0
- {jarvis_ai_assistant-0.1.222.dist-info → jarvis_ai_assistant-0.7.0.dist-info}/licenses/LICENSE +0 -0
- {jarvis_ai_assistant-0.1.222.dist-info → jarvis_ai_assistant-0.7.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
事件主题与负载类型定义(jarvis_agent.events)
|
|
4
|
+
|
|
5
|
+
目的:
|
|
6
|
+
- 统一事件名称,避免在代码各处硬编码字符串导致的漂移
|
|
7
|
+
- 提供事件负载的类型提示,便于静态检查与后续文档化
|
|
8
|
+
- 本文件仅提供常量与类型定义,不改变现有行为
|
|
9
|
+
"""
|
|
10
|
+
from typing import Any, TypedDict, List
|
|
11
|
+
|
|
12
|
+
# 事件主题常量
|
|
13
|
+
BEFORE_TOOL_CALL = "before_tool_call"
|
|
14
|
+
AFTER_TOOL_CALL = "after_tool_call"
|
|
15
|
+
|
|
16
|
+
# 会话与任务生命周期相关
|
|
17
|
+
TASK_STARTED = "task_started"
|
|
18
|
+
TASK_COMPLETED = "task_completed"
|
|
19
|
+
|
|
20
|
+
# 总结阶段
|
|
21
|
+
BEFORE_SUMMARY = "before_summary"
|
|
22
|
+
AFTER_SUMMARY = "after_summary"
|
|
23
|
+
|
|
24
|
+
# 附加提示
|
|
25
|
+
BEFORE_ADDON_PROMPT = "before_addon_prompt"
|
|
26
|
+
AFTER_ADDON_PROMPT = "after_addon_prompt"
|
|
27
|
+
|
|
28
|
+
# 历史清理
|
|
29
|
+
BEFORE_HISTORY_CLEAR = "before_history_clear"
|
|
30
|
+
AFTER_HISTORY_CLEAR = "after_history_clear"
|
|
31
|
+
|
|
32
|
+
# 模型调用
|
|
33
|
+
BEFORE_MODEL_CALL = "before_model_call"
|
|
34
|
+
AFTER_MODEL_CALL = "after_model_call"
|
|
35
|
+
|
|
36
|
+
# 其他
|
|
37
|
+
INTERRUPT_TRIGGERED = "interrupt_triggered"
|
|
38
|
+
BEFORE_TOOL_FILTER = "before_tool_filter"
|
|
39
|
+
TOOL_FILTERED = "tool_filtered"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# 事件负载类型(仅用于类型提示)
|
|
43
|
+
class BeforeToolCallEvent(TypedDict, total=False):
|
|
44
|
+
agent: Any
|
|
45
|
+
current_response: str
|
|
46
|
+
|
|
47
|
+
class AfterToolCallEvent(TypedDict, total=False):
|
|
48
|
+
agent: Any
|
|
49
|
+
current_response: str
|
|
50
|
+
need_return: bool
|
|
51
|
+
tool_prompt: str
|
|
52
|
+
|
|
53
|
+
# 任务生命周期
|
|
54
|
+
class TaskStartedEvent(TypedDict, total=False):
|
|
55
|
+
agent: Any
|
|
56
|
+
name: str
|
|
57
|
+
description: str
|
|
58
|
+
user_input: str
|
|
59
|
+
|
|
60
|
+
class TaskCompletedEvent(TypedDict, total=False):
|
|
61
|
+
agent: Any
|
|
62
|
+
auto_completed: bool
|
|
63
|
+
need_summary: bool
|
|
64
|
+
|
|
65
|
+
# 总结阶段
|
|
66
|
+
class BeforeSummaryEvent(TypedDict, total=False):
|
|
67
|
+
agent: Any
|
|
68
|
+
prompt: str
|
|
69
|
+
auto_completed: bool
|
|
70
|
+
need_summary: bool
|
|
71
|
+
|
|
72
|
+
class AfterSummaryEvent(TypedDict, total=False):
|
|
73
|
+
agent: Any
|
|
74
|
+
summary: str
|
|
75
|
+
|
|
76
|
+
# 附加提示
|
|
77
|
+
class BeforeAddonPromptEvent(TypedDict, total=False):
|
|
78
|
+
agent: Any
|
|
79
|
+
need_complete: bool
|
|
80
|
+
current_message: str
|
|
81
|
+
has_session_addon: bool
|
|
82
|
+
|
|
83
|
+
class AfterAddonPromptEvent(TypedDict, total=False):
|
|
84
|
+
agent: Any
|
|
85
|
+
need_complete: bool
|
|
86
|
+
addon_text: str
|
|
87
|
+
final_message: str
|
|
88
|
+
|
|
89
|
+
# 历史清理
|
|
90
|
+
class BeforeHistoryClearEvent(TypedDict, total=False):
|
|
91
|
+
agent: Any
|
|
92
|
+
|
|
93
|
+
class AfterHistoryClearEvent(TypedDict, total=False):
|
|
94
|
+
agent: Any
|
|
95
|
+
|
|
96
|
+
# 模型调用
|
|
97
|
+
class BeforeModelCallEvent(TypedDict, total=False):
|
|
98
|
+
agent: Any
|
|
99
|
+
message: str
|
|
100
|
+
|
|
101
|
+
class AfterModelCallEvent(TypedDict, total=False):
|
|
102
|
+
agent: Any
|
|
103
|
+
message: str
|
|
104
|
+
response: str
|
|
105
|
+
|
|
106
|
+
# 中断
|
|
107
|
+
class InterruptTriggeredEvent(TypedDict, total=False):
|
|
108
|
+
agent: Any
|
|
109
|
+
current_response: str
|
|
110
|
+
user_input: str
|
|
111
|
+
|
|
112
|
+
# 工具筛选
|
|
113
|
+
class BeforeToolFilterEvent(TypedDict, total=False):
|
|
114
|
+
agent: Any
|
|
115
|
+
task: str
|
|
116
|
+
total_tools: int
|
|
117
|
+
threshold: int
|
|
118
|
+
|
|
119
|
+
class ToolFilteredEvent(TypedDict, total=False):
|
|
120
|
+
agent: Any
|
|
121
|
+
task: str
|
|
122
|
+
selected_tools: List[str]
|
|
123
|
+
total_tools: int
|
|
124
|
+
threshold: int
|
|
125
|
+
|
|
126
|
+
__all__ = [
|
|
127
|
+
"BEFORE_TOOL_CALL",
|
|
128
|
+
"AFTER_TOOL_CALL",
|
|
129
|
+
"TASK_STARTED",
|
|
130
|
+
"TASK_COMPLETED",
|
|
131
|
+
"BEFORE_SUMMARY",
|
|
132
|
+
"AFTER_SUMMARY",
|
|
133
|
+
"BEFORE_ADDON_PROMPT",
|
|
134
|
+
"AFTER_ADDON_PROMPT",
|
|
135
|
+
"BEFORE_HISTORY_CLEAR",
|
|
136
|
+
"AFTER_HISTORY_CLEAR",
|
|
137
|
+
"BEFORE_MODEL_CALL",
|
|
138
|
+
"AFTER_MODEL_CALL",
|
|
139
|
+
"INTERRUPT_TRIGGERED",
|
|
140
|
+
"BEFORE_TOOL_FILTER",
|
|
141
|
+
"TOOL_FILTERED",
|
|
142
|
+
"BeforeToolCallEvent",
|
|
143
|
+
"AfterToolCallEvent",
|
|
144
|
+
"TaskStartedEvent",
|
|
145
|
+
"TaskCompletedEvent",
|
|
146
|
+
"BeforeSummaryEvent",
|
|
147
|
+
"AfterSummaryEvent",
|
|
148
|
+
"BeforeAddonPromptEvent",
|
|
149
|
+
"AfterAddonPromptEvent",
|
|
150
|
+
"BeforeHistoryClearEvent",
|
|
151
|
+
"AfterHistoryClearEvent",
|
|
152
|
+
"BeforeModelCallEvent",
|
|
153
|
+
"AfterModelCallEvent",
|
|
154
|
+
"InterruptTriggeredEvent",
|
|
155
|
+
"BeforeToolFilterEvent",
|
|
156
|
+
"ToolFilteredEvent",
|
|
157
|
+
]
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import re
|
|
3
|
+
import os
|
|
4
|
+
from typing import Any, Tuple
|
|
5
|
+
|
|
6
|
+
from jarvis.jarvis_tools.read_code import ReadCodeTool
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def is_text_file(filepath: str) -> bool:
|
|
10
|
+
"""
|
|
11
|
+
Check if a file is a text file.
|
|
12
|
+
"""
|
|
13
|
+
try:
|
|
14
|
+
with open(filepath, "r", encoding="utf-8") as f:
|
|
15
|
+
f.read(1024) # Try to read a small chunk
|
|
16
|
+
return True
|
|
17
|
+
except (UnicodeDecodeError, IOError):
|
|
18
|
+
return False
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def count_lines(filepath: str) -> int:
|
|
22
|
+
"""
|
|
23
|
+
Count the number of lines in a file.
|
|
24
|
+
"""
|
|
25
|
+
try:
|
|
26
|
+
with open(filepath, "r", encoding="utf-8", errors="ignore") as f:
|
|
27
|
+
return sum(1 for _ in f)
|
|
28
|
+
except IOError:
|
|
29
|
+
return 0
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def file_context_handler(user_input: str, agent_: Any) -> Tuple[str, bool]:
|
|
33
|
+
"""
|
|
34
|
+
Extracts file paths from the input, reads their content if they are valid text files
|
|
35
|
+
and appends the content to the input.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
user_input: The user's input string.
|
|
39
|
+
agent_: The agent instance.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
A tuple containing the modified user input and a boolean indicating if
|
|
43
|
+
further processing should be skipped.
|
|
44
|
+
"""
|
|
45
|
+
# Regex to find paths in single quotes
|
|
46
|
+
raw_paths = re.findall(r"'([^']+)'", user_input)
|
|
47
|
+
# Convert to absolute paths and de-duplicate by absolute path while preserving order
|
|
48
|
+
abs_to_raws: dict[str, list[str]] = {}
|
|
49
|
+
file_paths = []
|
|
50
|
+
for _raw in raw_paths:
|
|
51
|
+
abs_path = os.path.abspath(_raw)
|
|
52
|
+
if abs_path not in abs_to_raws:
|
|
53
|
+
abs_to_raws[abs_path] = []
|
|
54
|
+
file_paths.append(abs_path)
|
|
55
|
+
abs_to_raws[abs_path].append(_raw)
|
|
56
|
+
|
|
57
|
+
if not file_paths:
|
|
58
|
+
return user_input, False
|
|
59
|
+
|
|
60
|
+
added_context = ""
|
|
61
|
+
read_code_tool = ReadCodeTool()
|
|
62
|
+
|
|
63
|
+
for abs_path in file_paths:
|
|
64
|
+
if os.path.isfile(abs_path) and is_text_file(abs_path):
|
|
65
|
+
line_count = count_lines(abs_path)
|
|
66
|
+
if line_count > 0:
|
|
67
|
+
# Use ReadCodeTool to get formatted content
|
|
68
|
+
result = read_code_tool._handle_single_file(abs_path)
|
|
69
|
+
if result["success"]:
|
|
70
|
+
# Remove all original path tokens that map to this absolute path to avoid redundancy
|
|
71
|
+
for _raw in abs_to_raws.get(abs_path, []):
|
|
72
|
+
user_input = user_input.replace(f"'{_raw}'", "")
|
|
73
|
+
# Append the full, formatted output from the tool, which includes headers and line numbers
|
|
74
|
+
added_context += "\n" + result["stdout"]
|
|
75
|
+
|
|
76
|
+
if added_context:
|
|
77
|
+
user_input = user_input.strip() + added_context
|
|
78
|
+
|
|
79
|
+
return user_input, False
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
文件和方法论管理器模块
|
|
4
|
+
负责处理文件上传和方法论加载功能
|
|
5
|
+
"""
|
|
6
|
+
import os
|
|
7
|
+
import tempfile
|
|
8
|
+
|
|
9
|
+
from jarvis.jarvis_utils.methodology import load_methodology, upload_methodology
|
|
10
|
+
from jarvis.jarvis_utils.output import OutputType, PrettyOutput
|
|
11
|
+
from jarvis.jarvis_agent.utils import join_prompts
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class FileMethodologyManager:
|
|
15
|
+
"""文件和方法论管理器,负责处理文件上传和方法论相关功能"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, agent):
|
|
18
|
+
"""
|
|
19
|
+
初始化文件和方法论管理器
|
|
20
|
+
|
|
21
|
+
参数:
|
|
22
|
+
agent: Agent实例
|
|
23
|
+
"""
|
|
24
|
+
self.agent = agent
|
|
25
|
+
|
|
26
|
+
def handle_files_and_methodology(self):
|
|
27
|
+
"""处理文件上传和方法论加载"""
|
|
28
|
+
if self.agent.model and self.agent.model.support_upload_files():
|
|
29
|
+
self._handle_file_upload_mode()
|
|
30
|
+
else:
|
|
31
|
+
self._handle_local_mode()
|
|
32
|
+
|
|
33
|
+
def _handle_file_upload_mode(self):
|
|
34
|
+
"""处理支持文件上传的模式"""
|
|
35
|
+
if self.agent.use_methodology:
|
|
36
|
+
self._handle_methodology_upload()
|
|
37
|
+
elif self.agent.files:
|
|
38
|
+
self._handle_files_upload()
|
|
39
|
+
|
|
40
|
+
def _handle_methodology_upload(self):
|
|
41
|
+
"""处理方法论上传"""
|
|
42
|
+
if not upload_methodology(self.agent.model, other_files=self.agent.files): # type: ignore
|
|
43
|
+
if self.agent.files:
|
|
44
|
+
PrettyOutput.print("文件上传失败,将忽略文件列表", OutputType.WARNING)
|
|
45
|
+
# 上传失败则回退到本地加载
|
|
46
|
+
self._load_local_methodology()
|
|
47
|
+
else:
|
|
48
|
+
# 上传成功
|
|
49
|
+
|
|
50
|
+
if self.agent.files:
|
|
51
|
+
self.agent.session.prompt = join_prompts([
|
|
52
|
+
self.agent.session.prompt,
|
|
53
|
+
"上传的文件包含历史对话信息和方法论文件,可以从中获取一些经验信息。"
|
|
54
|
+
])
|
|
55
|
+
else:
|
|
56
|
+
self.agent.session.prompt = join_prompts([
|
|
57
|
+
self.agent.session.prompt,
|
|
58
|
+
"上传的文件包含历史对话信息,可以从中获取一些经验信息。"
|
|
59
|
+
])
|
|
60
|
+
|
|
61
|
+
def _handle_files_upload(self):
|
|
62
|
+
"""处理普通文件上传"""
|
|
63
|
+
if not self.agent.model.upload_files(self.agent.files): # type: ignore
|
|
64
|
+
PrettyOutput.print("文件上传失败,将忽略文件列表", OutputType.WARNING)
|
|
65
|
+
else:
|
|
66
|
+
self.agent.session.prompt = join_prompts([
|
|
67
|
+
self.agent.session.prompt,
|
|
68
|
+
"上传的文件包含历史对话信息,可以从中获取一些经验信息。"
|
|
69
|
+
])
|
|
70
|
+
|
|
71
|
+
def _handle_local_mode(self):
|
|
72
|
+
"""处理本地模式(不支持文件上传)"""
|
|
73
|
+
if self.agent.files:
|
|
74
|
+
PrettyOutput.print("不支持上传文件,将忽略文件列表", OutputType.WARNING)
|
|
75
|
+
if self.agent.use_methodology:
|
|
76
|
+
self._load_local_methodology()
|
|
77
|
+
|
|
78
|
+
def _load_local_methodology(self):
|
|
79
|
+
"""加载本地方法论"""
|
|
80
|
+
msg = self.agent.session.prompt
|
|
81
|
+
for handler in self.agent.input_handler:
|
|
82
|
+
msg, _ = handler(msg, self.agent)
|
|
83
|
+
|
|
84
|
+
from jarvis.jarvis_agent.memory_manager import MemoryManager
|
|
85
|
+
|
|
86
|
+
MemoryManager(self.agent)
|
|
87
|
+
methodology = load_methodology(
|
|
88
|
+
msg,
|
|
89
|
+
self.agent.get_tool_registry(),
|
|
90
|
+
platform_name=self.agent.model.platform_name(),
|
|
91
|
+
model_name=self.agent.model.name(),
|
|
92
|
+
)
|
|
93
|
+
self.agent.session.prompt = join_prompts([
|
|
94
|
+
self.agent.session.prompt,
|
|
95
|
+
f"以下是历史类似问题的执行经验,可参考:\n{methodology}"
|
|
96
|
+
])
|
|
97
|
+
|
|
98
|
+
def handle_history_with_file_upload(self) -> str:
|
|
99
|
+
"""使用文件上传方式处理历史"""
|
|
100
|
+
tmp_file_name = ""
|
|
101
|
+
try:
|
|
102
|
+
tmp_file = tempfile.NamedTemporaryFile(
|
|
103
|
+
delete=False, mode="w", encoding="utf-8"
|
|
104
|
+
)
|
|
105
|
+
tmp_file_name = tmp_file.name
|
|
106
|
+
tmp_file.write(self.agent.session.prompt)
|
|
107
|
+
tmp_file.close()
|
|
108
|
+
|
|
109
|
+
self.agent.clear_history()
|
|
110
|
+
|
|
111
|
+
if self.agent.model and self.agent.model.upload_files([tmp_file_name]):
|
|
112
|
+
return "上传的文件是历史对话信息,请基于历史对话信息继续完成任务。"
|
|
113
|
+
else:
|
|
114
|
+
return ""
|
|
115
|
+
finally:
|
|
116
|
+
if tmp_file_name and os.path.exists(tmp_file_name):
|
|
117
|
+
os.remove(tmp_file_name)
|