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

@@ -0,0 +1,235 @@
"""
历史记录管理器单元测试
"""
import unittest
import sys
import tempfile
import shutil
from pathlib import Path
# 添加项目根目录到路径
sys.path.insert(0, str(Path(__file__).parent.parent))
from history.manager import HistoryManager, TaskRecord
class TestHistoryManager(unittest.TestCase):
"""历史记录管理器测试"""
def setUp(self):
"""创建临时目录用于测试"""
self.temp_dir = Path(tempfile.mkdtemp())
self.manager = HistoryManager(self.temp_dir)
def tearDown(self):
"""清理临时目录"""
shutil.rmtree(self.temp_dir, ignore_errors=True)
def test_add_record(self):
"""测试添加记录"""
record = self.manager.add_record(
task_id="test_001",
user_input="复制文件",
intent_label="execution",
intent_confidence=0.95,
execution_plan="复制所有文件",
code="shutil.copy(...)",
success=True,
duration_ms=100
)
self.assertEqual(record.task_id, "test_001")
self.assertEqual(record.user_input, "复制文件")
self.assertTrue(record.success)
def test_get_all(self):
"""测试获取所有记录"""
# 添加多条记录
for i in range(3):
self.manager.add_record(
task_id=f"test_{i:03d}",
user_input=f"任务 {i}",
intent_label="execution",
intent_confidence=0.9,
execution_plan="计划",
code="代码",
success=True,
duration_ms=100
)
records = self.manager.get_all()
self.assertEqual(len(records), 3)
def test_get_recent(self):
"""测试获取最近记录"""
# 添加 5 条记录
for i in range(5):
self.manager.add_record(
task_id=f"test_{i:03d}",
user_input=f"任务 {i}",
intent_label="execution",
intent_confidence=0.9,
execution_plan="计划",
code="代码",
success=True,
duration_ms=100
)
# 获取最近 3 条
recent = self.manager.get_recent(3)
self.assertEqual(len(recent), 3)
# 最新的在前
self.assertEqual(recent[0].task_id, "test_004")
def test_get_by_id(self):
"""测试根据 ID 获取记录"""
self.manager.add_record(
task_id="unique_id",
user_input="测试",
intent_label="execution",
intent_confidence=0.9,
execution_plan="计划",
code="代码",
success=True,
duration_ms=100
)
record = self.manager.get_by_id("unique_id")
self.assertIsNotNone(record)
self.assertEqual(record.task_id, "unique_id")
# 不存在的 ID
not_found = self.manager.get_by_id("not_exist")
self.assertIsNone(not_found)
def test_clear(self):
"""测试清空记录"""
# 添加记录
self.manager.add_record(
task_id="test",
user_input="测试",
intent_label="execution",
intent_confidence=0.9,
execution_plan="计划",
code="代码",
success=True,
duration_ms=100
)
self.assertEqual(len(self.manager.get_all()), 1)
# 清空
self.manager.clear()
self.assertEqual(len(self.manager.get_all()), 0)
def test_get_stats(self):
"""测试统计信息"""
# 添加成功和失败的记录
self.manager.add_record(
task_id="success_1",
user_input="成功任务",
intent_label="execution",
intent_confidence=0.9,
execution_plan="计划",
code="代码",
success=True,
duration_ms=100
)
self.manager.add_record(
task_id="success_2",
user_input="成功任务2",
intent_label="execution",
intent_confidence=0.9,
execution_plan="计划",
code="代码",
success=True,
duration_ms=200
)
self.manager.add_record(
task_id="failed_1",
user_input="失败任务",
intent_label="execution",
intent_confidence=0.9,
execution_plan="计划",
code="代码",
success=False,
duration_ms=50
)
stats = self.manager.get_stats()
self.assertEqual(stats['total'], 3)
self.assertEqual(stats['success'], 2)
self.assertEqual(stats['failed'], 1)
self.assertAlmostEqual(stats['success_rate'], 2/3)
def test_persistence(self):
"""测试持久化"""
# 添加记录
self.manager.add_record(
task_id="persist_test",
user_input="持久化测试",
intent_label="execution",
intent_confidence=0.9,
execution_plan="计划",
code="代码",
success=True,
duration_ms=100
)
# 创建新的管理器实例(模拟重启)
new_manager = HistoryManager(self.temp_dir)
# 应该能读取到之前的记录
records = new_manager.get_all()
self.assertEqual(len(records), 1)
self.assertEqual(records[0].task_id, "persist_test")
def test_max_history_size(self):
"""测试历史记录数量限制"""
# 添加超过限制的记录
for i in range(HistoryManager.MAX_HISTORY_SIZE + 10):
self.manager.add_record(
task_id=f"test_{i:03d}",
user_input=f"任务 {i}",
intent_label="execution",
intent_confidence=0.9,
execution_plan="计划",
code="代码",
success=True,
duration_ms=100
)
# 应该只保留最大数量
records = self.manager.get_all()
self.assertEqual(len(records), HistoryManager.MAX_HISTORY_SIZE)
class TestTaskRecord(unittest.TestCase):
"""任务记录数据类测试"""
def test_create_record(self):
"""测试创建记录"""
record = TaskRecord(
task_id="test",
timestamp="2024-01-01 12:00:00",
user_input="测试",
intent_label="execution",
intent_confidence=0.9,
execution_plan="计划",
code="代码",
success=True,
duration_ms=100,
stdout="输出",
stderr="",
log_path="/path/to/log"
)
self.assertEqual(record.task_id, "test")
self.assertTrue(record.success)
self.assertEqual(record.duration_ms, 100)
if __name__ == '__main__':
unittest.main()