jarvis-ai-assistant 0.2.7__py3-none-any.whl → 0.3.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 (38) hide show
  1. jarvis/__init__.py +1 -1
  2. jarvis/jarvis_agent/__init__.py +267 -240
  3. jarvis/jarvis_agent/agent_manager.py +85 -0
  4. jarvis/jarvis_agent/config_editor.py +53 -0
  5. jarvis/jarvis_agent/file_methodology_manager.py +105 -0
  6. jarvis/jarvis_agent/jarvis.py +37 -398
  7. jarvis/jarvis_agent/memory_manager.py +133 -0
  8. jarvis/jarvis_agent/methodology_share_manager.py +174 -0
  9. jarvis/jarvis_agent/prompts.py +18 -3
  10. jarvis/jarvis_agent/share_manager.py +176 -0
  11. jarvis/jarvis_agent/task_analyzer.py +126 -0
  12. jarvis/jarvis_agent/task_manager.py +111 -0
  13. jarvis/jarvis_agent/tool_share_manager.py +139 -0
  14. jarvis/jarvis_code_agent/code_agent.py +26 -20
  15. jarvis/jarvis_data/config_schema.json +37 -0
  16. jarvis/jarvis_platform/ai8.py +13 -1
  17. jarvis/jarvis_platform/base.py +20 -5
  18. jarvis/jarvis_platform/human.py +11 -1
  19. jarvis/jarvis_platform/kimi.py +10 -0
  20. jarvis/jarvis_platform/openai.py +20 -0
  21. jarvis/jarvis_platform/tongyi.py +14 -9
  22. jarvis/jarvis_platform/yuanbao.py +10 -0
  23. jarvis/jarvis_platform_manager/main.py +12 -12
  24. jarvis/jarvis_tools/registry.py +79 -20
  25. jarvis/jarvis_tools/retrieve_memory.py +36 -8
  26. jarvis/jarvis_utils/clipboard.py +90 -0
  27. jarvis/jarvis_utils/config.py +64 -0
  28. jarvis/jarvis_utils/git_utils.py +17 -7
  29. jarvis/jarvis_utils/globals.py +18 -12
  30. jarvis/jarvis_utils/input.py +118 -16
  31. jarvis/jarvis_utils/methodology.py +48 -5
  32. jarvis/jarvis_utils/utils.py +196 -106
  33. {jarvis_ai_assistant-0.2.7.dist-info → jarvis_ai_assistant-0.3.0.dist-info}/METADATA +1 -1
  34. {jarvis_ai_assistant-0.2.7.dist-info → jarvis_ai_assistant-0.3.0.dist-info}/RECORD +38 -28
  35. {jarvis_ai_assistant-0.2.7.dist-info → jarvis_ai_assistant-0.3.0.dist-info}/WHEEL +0 -0
  36. {jarvis_ai_assistant-0.2.7.dist-info → jarvis_ai_assistant-0.3.0.dist-info}/entry_points.txt +0 -0
  37. {jarvis_ai_assistant-0.2.7.dist-info → jarvis_ai_assistant-0.3.0.dist-info}/licenses/LICENSE +0 -0
  38. {jarvis_ai_assistant-0.2.7.dist-info → jarvis_ai_assistant-0.3.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,85 @@
1
+ # -*- coding: utf-8 -*-
2
+ """Agent管理器模块,负责Agent的初始化和任务执行"""
3
+ from typing import Optional
4
+
5
+ import typer
6
+
7
+ from jarvis.jarvis_agent import (
8
+ Agent,
9
+ OutputType,
10
+ PrettyOutput,
11
+ get_multiline_input,
12
+ origin_agent_system_prompt,
13
+ )
14
+ from jarvis.jarvis_agent.builtin_input_handler import builtin_input_handler
15
+ from jarvis.jarvis_agent.shell_input_handler import shell_input_handler
16
+ from jarvis.jarvis_agent.task_manager import TaskManager
17
+ from jarvis.jarvis_tools.registry import ToolRegistry
18
+
19
+
20
+ class AgentManager:
21
+ """Agent管理器,负责Agent的生命周期管理"""
22
+
23
+ def __init__(
24
+ self,
25
+ llm_type: str,
26
+ model_group: Optional[str] = None,
27
+ tool_group: Optional[str] = None,
28
+ restore_session: bool = False,
29
+ ):
30
+ self.llm_type = llm_type
31
+ self.model_group = model_group
32
+ self.tool_group = tool_group
33
+ self.restore_session = restore_session
34
+ self.agent: Optional[Agent] = None
35
+
36
+ def initialize(self) -> Agent:
37
+ """初始化Agent"""
38
+ # 如果提供了 tool_group 参数,设置到配置中
39
+ if self.tool_group:
40
+ from jarvis.jarvis_utils.config import set_config
41
+
42
+ set_config("JARVIS_TOOL_GROUP", self.tool_group)
43
+
44
+ self.agent = Agent(
45
+ system_prompt=origin_agent_system_prompt,
46
+ llm_type=self.llm_type,
47
+ model_group=self.model_group,
48
+ input_handler=[shell_input_handler, builtin_input_handler],
49
+ output_handler=[ToolRegistry()], # type: ignore
50
+ need_summary=False,
51
+ )
52
+
53
+ # 尝试恢复会话
54
+ if self.restore_session:
55
+ if self.agent.restore_session():
56
+ PrettyOutput.print("会话已成功恢复。", OutputType.SUCCESS)
57
+ else:
58
+ PrettyOutput.print("无法恢复会话。", OutputType.WARNING)
59
+
60
+ return self.agent
61
+
62
+ def run_task(self, task_content: Optional[str] = None) -> None:
63
+ """运行任务"""
64
+ if not self.agent:
65
+ raise RuntimeError("Agent not initialized")
66
+
67
+ # 优先处理命令行直接传入的任务
68
+ if task_content:
69
+ self.agent.run(task_content)
70
+ raise typer.Exit(code=0)
71
+
72
+ # 处理预定义任务
73
+ if self.agent.first:
74
+ task_manager = TaskManager()
75
+ tasks = task_manager.load_tasks()
76
+ if tasks and (selected_task := task_manager.select_task(tasks)):
77
+ PrettyOutput.print(f"开始执行任务: \n{selected_task}", OutputType.INFO)
78
+ self.agent.run(selected_task)
79
+ raise typer.Exit(code=0)
80
+
81
+ # 获取用户输入
82
+ user_input = get_multiline_input("请输入你的任务(输入空行退出):")
83
+ if user_input:
84
+ self.agent.run(user_input)
85
+ raise typer.Exit(code=0)
@@ -0,0 +1,53 @@
1
+ # -*- coding: utf-8 -*-
2
+ """配置编辑器模块,负责配置文件的编辑功能"""
3
+ import os
4
+ import platform
5
+ import shutil
6
+ import subprocess
7
+ from pathlib import Path
8
+ from typing import Optional
9
+
10
+ import typer
11
+
12
+ from jarvis.jarvis_agent import OutputType, PrettyOutput
13
+
14
+
15
+ class ConfigEditor:
16
+ """配置文件编辑器"""
17
+
18
+ @staticmethod
19
+ def get_default_editor() -> Optional[str]:
20
+ """根据操作系统获取默认编辑器"""
21
+ if platform.system() == "Windows":
22
+ # 优先级:终端工具 -> 代码编辑器 -> 通用文本编辑器
23
+ editors = ["nvim", "vim", "nano", "code", "notepad++", "notepad"]
24
+ else:
25
+ # 优先级:终端工具 -> 代码编辑器 -> 通用文本编辑器
26
+ editors = ["nvim", "vim", "vi", "nano", "emacs", "code", "gedit", "kate"]
27
+
28
+ return next((e for e in editors if shutil.which(e)), None)
29
+
30
+ @staticmethod
31
+ def edit_config(config_file: Optional[str] = None) -> None:
32
+ """编辑配置文件"""
33
+ config_file_path = (
34
+ Path(config_file)
35
+ if config_file
36
+ else Path(os.path.expanduser("~/.jarvis/config.yaml"))
37
+ )
38
+
39
+ editor = ConfigEditor.get_default_editor()
40
+
41
+ if editor:
42
+ try:
43
+ subprocess.run([editor, str(config_file_path)], check=True)
44
+ raise typer.Exit(code=0)
45
+ except (subprocess.CalledProcessError, FileNotFoundError) as e:
46
+ PrettyOutput.print(f"Failed to open editor: {e}", OutputType.ERROR)
47
+ raise typer.Exit(code=1)
48
+ else:
49
+ PrettyOutput.print(
50
+ f"No suitable editor found. Please install one of: vim, nano, emacs, code",
51
+ OutputType.ERROR,
52
+ )
53
+ raise typer.Exit(code=1)
@@ -0,0 +1,105 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ 文件和方法论管理器模块
4
+ 负责处理文件上传和方法论加载功能
5
+ """
6
+ import os
7
+ import tempfile
8
+ from typing import List, Optional
9
+
10
+ from jarvis.jarvis_utils.methodology import load_methodology, upload_methodology
11
+ from jarvis.jarvis_utils.output import OutputType, PrettyOutput
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
+ from jarvis.jarvis_agent.memory_manager import MemoryManager
50
+
51
+ memory_manager = MemoryManager(self.agent)
52
+ memory_tags_prompt = memory_manager.prepare_memory_tags_prompt()
53
+
54
+ if self.agent.files:
55
+ self.agent.session.prompt = f"{self.agent.session.prompt}\n\n上传的文件包含历史对话信息和方法论文件,可以从中获取一些经验信息。{memory_tags_prompt}"
56
+ else:
57
+ self.agent.session.prompt = f"{self.agent.session.prompt}\n\n上传的文件包含历史对话信息,可以从中获取一些经验信息。{memory_tags_prompt}"
58
+
59
+ def _handle_files_upload(self):
60
+ """处理普通文件上传"""
61
+ if not self.agent.model.upload_files(self.agent.files): # type: ignore
62
+ PrettyOutput.print("文件上传失败,将忽略文件列表", OutputType.WARNING)
63
+ else:
64
+ self.agent.session.prompt = f"{self.agent.session.prompt}\n\n上传的文件包含历史对话信息,可以从中获取一些经验信息。"
65
+
66
+ def _handle_local_mode(self):
67
+ """处理本地模式(不支持文件上传)"""
68
+ if self.agent.files:
69
+ PrettyOutput.print("不支持上传文件,将忽略文件列表", OutputType.WARNING)
70
+ if self.agent.use_methodology:
71
+ self._load_local_methodology()
72
+
73
+ def _load_local_methodology(self):
74
+ """加载本地方法论"""
75
+ msg = self.agent.session.prompt
76
+ for handler in self.agent.input_handler:
77
+ msg, _ = handler(msg, self.agent)
78
+
79
+ from jarvis.jarvis_agent.memory_manager import MemoryManager
80
+
81
+ memory_manager = MemoryManager(self.agent)
82
+ memory_tags_prompt = memory_manager.prepare_memory_tags_prompt()
83
+ methodology = load_methodology(msg, self.agent.get_tool_registry())
84
+ self.agent.session.prompt = f"{self.agent.session.prompt}\n\n以下是历史类似问题的执行经验,可参考:\n{methodology}{memory_tags_prompt}"
85
+
86
+ def handle_history_with_file_upload(self) -> str:
87
+ """使用文件上传方式处理历史"""
88
+ tmp_file_name = ""
89
+ try:
90
+ tmp_file = tempfile.NamedTemporaryFile(
91
+ delete=False, mode="w", encoding="utf-8"
92
+ )
93
+ tmp_file_name = tmp_file.name
94
+ tmp_file.write(self.agent.session.prompt)
95
+ tmp_file.close()
96
+
97
+ self.agent.clear_history()
98
+
99
+ if self.agent.model and self.agent.model.upload_files([tmp_file_name]):
100
+ return "上传的文件是历史对话信息,请基于历史对话信息继续完成任务。"
101
+ else:
102
+ return ""
103
+ finally:
104
+ if tmp_file_name and os.path.exists(tmp_file_name):
105
+ os.remove(tmp_file_name)