feat: enhance LocalAgent configuration and UI components

- Updated .env.example to provide clearer configuration instructions and API key setup.
- Removed debug_env.py as it was no longer needed.
- Refactored main.py to streamline application initialization and workspace setup.
- Introduced a new HistoryManager for managing task execution history.
- Enhanced UI components in chat_view.py and task_guide_view.py to improve user interaction and code preview functionality.
- Added loading indicators and improved task history display in the UI.
- Implemented unit tests for history management and intent classification.
This commit is contained in:
Mimikko-zeus
2026-01-07 10:29:13 +08:00
parent 1ba5f0f7d6
commit 0a92355bfb
18 changed files with 2144 additions and 557 deletions

View File

@@ -243,6 +243,9 @@ class TaskGuideView:
# 执行计划区域Markdown
self._create_plan_section()
# 代码预览区域(可折叠)
self._create_code_section()
# 风险提示区域
self._create_risk_section()
@@ -306,6 +309,148 @@ class TaskGuideView:
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.plan_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
def _create_code_section(self):
"""创建代码预览区域(可折叠)"""
# 折叠状态
self._code_expanded = False
# 外层框架
self.code_section = tk.LabelFrame(
self.frame,
text=" 💻 生成的代码 ",
font=('Microsoft YaHei UI', 10, 'bold'),
fg='#64b5f6',
bg='#1e1e1e',
relief=tk.GROOVE
)
self.code_section.pack(fill=tk.X, padx=10, pady=3)
# 展开/折叠按钮
self.toggle_code_btn = tk.Button(
self.code_section,
text="▶ 点击展开代码预览",
font=('Microsoft YaHei UI', 9),
bg='#2d2d2d',
fg='#64b5f6',
activebackground='#3d3d3d',
activeforeground='#64b5f6',
relief=tk.FLAT,
cursor='hand2',
command=self._toggle_code_view
)
self.toggle_code_btn.pack(fill=tk.X, padx=5, pady=5)
# 代码显示区域(初始隐藏)
self.code_frame = tk.Frame(self.code_section, bg='#1e1e1e')
# 代码文本框
self.code_text = tk.Text(
self.code_frame,
wrap=tk.NONE,
font=('Consolas', 10),
bg='#1e1e1e',
fg='#d4d4d4',
insertbackground='white',
relief=tk.FLAT,
height=12,
padx=8,
pady=5
)
# 配置代码高亮标签
self.code_text.tag_configure('keyword', foreground='#569cd6')
self.code_text.tag_configure('string', foreground='#ce9178')
self.code_text.tag_configure('comment', foreground='#6a9955')
self.code_text.tag_configure('function', foreground='#dcdcaa')
self.code_text.tag_configure('number', foreground='#b5cea8')
# 滚动条
code_scrollbar_y = ttk.Scrollbar(self.code_frame, orient=tk.VERTICAL, command=self.code_text.yview)
code_scrollbar_x = ttk.Scrollbar(self.code_frame, orient=tk.HORIZONTAL, command=self.code_text.xview)
self.code_text.configure(yscrollcommand=code_scrollbar_y.set, xscrollcommand=code_scrollbar_x.set)
code_scrollbar_y.pack(side=tk.RIGHT, fill=tk.Y)
code_scrollbar_x.pack(side=tk.BOTTOM, fill=tk.X)
self.code_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# 复制按钮
self.copy_code_btn = tk.Button(
self.code_frame,
text="📋 复制代码",
font=('Microsoft YaHei UI', 9),
bg='#424242',
fg='white',
activebackground='#616161',
activeforeground='white',
relief=tk.FLAT,
cursor='hand2',
command=self._copy_code
)
def _toggle_code_view(self):
"""切换代码预览的展开/折叠状态"""
self._code_expanded = not self._code_expanded
if self._code_expanded:
self.toggle_code_btn.config(text="▼ 点击折叠代码预览")
self.code_frame.pack(fill=tk.BOTH, expand=True, padx=3, pady=(0, 5))
self.copy_code_btn.pack(pady=5)
else:
self.toggle_code_btn.config(text="▶ 点击展开代码预览")
self.copy_code_btn.pack_forget()
self.code_frame.pack_forget()
def _copy_code(self):
"""复制代码到剪贴板"""
code = self.code_text.get(1.0, tk.END).strip()
self.frame.clipboard_clear()
self.frame.clipboard_append(code)
# 显示复制成功提示
original_text = self.copy_code_btn.cget('text')
self.copy_code_btn.config(text="✓ 已复制!")
self.frame.after(1500, lambda: self.copy_code_btn.config(text=original_text))
def _apply_syntax_highlight(self, code: str):
"""应用简单的语法高亮"""
import re
# 关键字
keywords = r'\b(import|from|def|class|if|else|elif|for|while|try|except|finally|with|as|return|yield|raise|pass|break|continue|and|or|not|in|is|None|True|False|lambda|global|nonlocal)\b'
# 字符串
strings = r'(\"\"\"[\s\S]*?\"\"\"|\'\'\'[\s\S]*?\'\'\'|\"[^\"]*\"|\'[^\']*\')'
# 注释
comments = r'(#.*$)'
# 函数调用
functions = r'\b([a-zA-Z_][a-zA-Z0-9_]*)\s*\('
# 数字
numbers = r'\b(\d+\.?\d*)\b'
# 先插入纯文本
self.code_text.delete(1.0, tk.END)
self.code_text.insert(1.0, code)
# 应用高亮
for match in re.finditer(keywords, code, re.MULTILINE):
start = f"1.0+{match.start()}c"
end = f"1.0+{match.end()}c"
self.code_text.tag_add('keyword', start, end)
for match in re.finditer(strings, code, re.MULTILINE):
start = f"1.0+{match.start()}c"
end = f"1.0+{match.end()}c"
self.code_text.tag_add('string', start, end)
for match in re.finditer(comments, code, re.MULTILINE):
start = f"1.0+{match.start()}c"
end = f"1.0+{match.end()}c"
self.code_text.tag_add('comment', start, end)
for match in re.finditer(numbers, code, re.MULTILINE):
start = f"1.0+{match.start(1)}c"
end = f"1.0+{match.end(1)}c"
self.code_text.tag_add('number', start, end)
def _create_risk_section(self):
"""创建风险提示区域"""
section = tk.LabelFrame(
@@ -423,6 +568,12 @@ class TaskGuideView:
"""设置执行计划Markdown 格式)"""
self.plan_text.set_markdown(plan)
def set_code(self, code: str):
"""设置生成的代码"""
self.code_text.config(state=tk.NORMAL)
self._apply_syntax_highlight(code)
self.code_text.config(state=tk.DISABLED)
def set_risk_info(self, info: str):
"""设置风险提示"""
self.risk_label.config(text=info)