feat: 重构为 Monorepo 架构并实现 HTTP Server

架构变更:
- 采用 pnpm workspaces 实现 Monorepo 结构
- 将现有代码迁移到 packages/core
- 新增 packages/server HTTP 服务层

Server 功能:
- REST API: 会话管理、工具管理、配置管理
- WebSocket: 实时双向通信支持
- SSE: 服务端事件推送
- Hono + Bun 作为运行时

API 端点:
- GET/POST /api/sessions - 会话 CRUD
- GET/POST /api/sessions/:id/messages - 消息管理
- GET /api/sessions/:id/events - SSE 事件流
- WS /api/ws/:sessionId - WebSocket 连接
- GET/POST /api/tools - 工具管理
- GET/PUT /api/config - 配置管理
This commit is contained in:
2025-12-12 10:42:20 +08:00
parent 59dbed926e
commit 5e32375f0e
301 changed files with 3281 additions and 43 deletions
@@ -0,0 +1,160 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { agentOutputTool } from '../../../../src/tools/task/agent_output.js';
import { getAgentManager, resetAgentManager } from '../../../../src/agent/manager.js';
// Mock AgentExecutor - 使用延迟执行让测试更可控
vi.mock('../../../../src/agent/executor.js', () => ({
AgentExecutor: vi.fn().mockImplementation(() => ({
execute: vi.fn().mockImplementation(async () => {
await new Promise((r) => setTimeout(r, 50));
return {
success: true,
text: '任务完成结果',
steps: 3,
sessionId: 'test-session',
};
}),
})),
}));
describe('agentOutputTool - Agent 输出工具', () => {
beforeEach(() => {
vi.clearAllMocks();
resetAgentManager();
});
describe('工具定义', () => {
it('有正确的名称', () => {
expect(agentOutputTool.name).toBe('agent_output');
});
it('有正确的元数据', () => {
expect(agentOutputTool.metadata.category).toBe('agent');
expect(agentOutputTool.metadata.keywords).toContain('agent');
expect(agentOutputTool.metadata.keywords).toContain('output');
expect(agentOutputTool.metadata.keywords).toContain('background');
});
it('agent_id 参数是必须的', () => {
expect(agentOutputTool.parameters.agent_id.required).toBe(true);
});
it('block 和 timeout 参数是可选的', () => {
expect(agentOutputTool.parameters.block.required).toBe(false);
expect(agentOutputTool.parameters.timeout.required).toBe(false);
});
});
describe('execute - 执行', () => {
it('Agent 不存在时返回错误', async () => {
const result = await agentOutputTool.execute({
agent_id: 'non-existent-id',
});
expect(result.success).toBe(false);
expect(result.error).toContain('不存在');
});
it('返回 Agent 的状态', async () => {
const manager = getAgentManager();
const agentId = await manager.runInBackground(
{
name: 'test-agent',
description: '测试',
mode: 'subagent',
},
'测试任务',
'执行测试',
{
provider: 'anthropic',
apiKey: 'test',
model: 'test',
maxTokens: 1000,
systemPrompt: 'test',
},
{} as any,
{ parentSessionId: 'parent', workdir: '/test' }
);
// 立即查询(不阻塞)
const result = await agentOutputTool.execute({
agent_id: agentId,
block: false,
});
// 应该成功返回状态(可能是 running 或 completed
expect(result.output).toBeDefined();
expect(result.metadata?.agentId).toBe(agentId);
});
it('阻塞等待后返回结果', async () => {
const manager = getAgentManager();
const agentId = await manager.runInBackground(
{
name: 'test-agent',
description: '测试',
mode: 'subagent',
},
'测试任务',
'执行测试',
{
provider: 'anthropic',
apiKey: 'test',
model: 'test',
maxTokens: 1000,
systemPrompt: 'test',
},
{} as any,
{ parentSessionId: 'parent', workdir: '/test' }
);
// 使用阻塞模式等待
const result = await agentOutputTool.execute({
agent_id: agentId,
block: true,
timeout: 5,
});
// 等待后应该有确定的状态
expect(result.metadata?.status).toBeDefined();
expect(['running', 'completed', 'failed']).toContain(result.metadata?.status);
});
it('返回包含正确字段的输出', async () => {
const manager = getAgentManager();
const agentId = await manager.runInBackground(
{
name: 'test-agent',
description: '测试',
mode: 'subagent',
},
'测试任务',
'执行测试',
{
provider: 'anthropic',
apiKey: 'test',
model: 'test',
maxTokens: 1000,
systemPrompt: 'test',
},
{} as any,
{ parentSessionId: 'parent', workdir: '/test' }
);
// 等待完成
await new Promise((r) => setTimeout(r, 200));
const result = await agentOutputTool.execute({
agent_id: agentId,
block: false,
});
// 检查返回了有效结果
expect(result.output).toBeDefined();
expect(result.metadata?.agentId).toBe(agentId);
expect(result.metadata?.agentName).toBe('test-agent');
// 状态应该是完成或失败(由于 mock,可能会失败)
expect(['completed', 'failed']).toContain(result.metadata?.status);
});
});
});
@@ -0,0 +1,380 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
// 可变状态
let mockExecuteResult = {
success: true,
text: '任务完成',
steps: 3,
};
let mockVisionConfig: { provider: string; apiKey: string; model: string; baseUrl?: string } | null = null;
let mockRunInBackgroundResult = 'agent-123';
// Mock loadVisionConfig
vi.mock('../../../../src/utils/config.js', () => ({
loadVisionConfig: () => mockVisionConfig,
}));
// Mock agent manager
vi.mock('../../../../src/agent/manager.js', () => ({
getAgentManager: () => ({
runInBackground: vi.fn(async () => mockRunInBackgroundResult),
}),
}));
// Mock agent registry 和 AgentExecutor
vi.mock('../../../../src/agent/index.js', () => ({
agentRegistry: {
listSubagents: vi.fn(() => [
{ name: 'explore', description: '代码探索', mode: 'subagent' },
{ name: 'vision', description: '图片分析', mode: 'subagent' },
]),
get: vi.fn(),
},
AgentExecutor: class {
execute() {
return Promise.resolve(mockExecuteResult);
}
},
}));
// Mock tool registry
vi.mock('../../../../src/tools/registry.js', () => ({
toolRegistry: {},
}));
// Mock session manager
vi.mock('../../../../src/session/index.js', () => ({
SessionManager: vi.fn(),
}));
import { taskTool, initTaskContext, getTaskContext } from '../../../../src/tools/task/task.js';
import { agentRegistry } from '../../../../src/agent/index.js';
describe('taskTool - 扩展测试', () => {
const createMockSession = () => ({
getSessionId: vi.fn(() => 'parent-session'),
createChildSession: vi.fn(() => ({
id: 'child-session',
messages: [],
})),
saveChildSession: vi.fn(),
});
beforeEach(() => {
vi.clearAllMocks();
initTaskContext(null as any, null as any);
mockExecuteResult = {
success: true,
text: '任务完成',
steps: 3,
};
mockVisionConfig = null;
mockRunInBackgroundResult = 'agent-123';
});
describe('getTaskContext', () => {
it('返回上下文', () => {
initTaskContext({ model: 'test' } as any, { sessionManager: true } as any);
expect(getTaskContext()).not.toBeNull();
});
it('初始化 null 时返回包含 null 的对象', () => {
initTaskContext(null as any, null as any);
const ctx = getTaskContext();
expect(ctx?.baseConfig).toBeNull();
expect(ctx?.sessionManager).toBeNull();
});
});
describe('Vision Agent 处理', () => {
it('Vision Agent 无配置时返回错误', async () => {
mockVisionConfig = null;
const mockSession = createMockSession();
initTaskContext({ model: 'test' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'vision',
description: '图片分析 Agent',
mode: 'subagent',
prompt: '你是视觉助手',
});
const result = await taskTool.execute({
description: 'analyze image',
prompt: '分析图片',
subagent_type: 'vision',
});
expect(result.success).toBe(false);
expect(result.error).toContain('Vision Agent 需要配置');
});
it('Vision Agent 有配置时成功执行', async () => {
mockVisionConfig = {
provider: 'openai',
apiKey: 'vision-key',
model: 'gpt-4o',
};
const mockSession = createMockSession();
initTaskContext({ model: 'test' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'vision',
description: '图片分析 Agent',
mode: 'subagent',
prompt: '你是视觉助手',
});
const result = await taskTool.execute({
description: 'analyze image',
prompt: '分析图片',
subagent_type: 'vision',
});
expect(result.success).toBe(true);
});
});
describe('模型选择', () => {
it('无效模型选择返回错误', async () => {
const mockSession = createMockSession();
initTaskContext({ model: 'test' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'explore',
description: '探索 Agent',
mode: 'subagent',
prompt: '你是探索助手',
});
const result = await taskTool.execute({
description: 'test',
prompt: 'test',
subagent_type: 'explore',
model: 'invalid-model',
});
expect(result.success).toBe(false);
expect(result.error).toContain('无效的模型选择');
expect(result.error).toContain('sonnet, opus, haiku');
});
it('sonnet 模型选择有效', async () => {
const mockSession = createMockSession();
initTaskContext({ model: 'original-model' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'explore',
description: '探索 Agent',
mode: 'subagent',
prompt: '你是探索助手',
});
const result = await taskTool.execute({
description: 'test',
prompt: 'test',
subagent_type: 'explore',
model: 'sonnet',
});
expect(result.success).toBe(true);
});
it('opus 模型选择有效', async () => {
const mockSession = createMockSession();
initTaskContext({ model: 'original-model' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'explore',
description: '探索 Agent',
mode: 'subagent',
prompt: '你是探索助手',
});
const result = await taskTool.execute({
description: 'test',
prompt: 'test',
subagent_type: 'explore',
model: 'opus',
});
expect(result.success).toBe(true);
});
it('haiku 模型选择有效', async () => {
const mockSession = createMockSession();
initTaskContext({ model: 'original-model' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'explore',
description: '探索 Agent',
mode: 'subagent',
prompt: '你是探索助手',
});
const result = await taskTool.execute({
description: 'test',
prompt: 'test',
subagent_type: 'explore',
model: 'haiku',
});
expect(result.success).toBe(true);
});
});
describe('后台运行模式', () => {
it('后台运行返回 agentId', async () => {
const mockSession = createMockSession();
initTaskContext({ model: 'test' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'explore',
description: '探索 Agent',
mode: 'subagent',
prompt: '你是探索助手',
});
const result = await taskTool.execute({
description: 'background task',
prompt: 'do something',
subagent_type: 'explore',
run_in_background: true,
});
expect(result.success).toBe(true);
expect(result.output).toContain('agent-123');
expect(result.output).toContain('后台启动');
expect(result.metadata?.mode).toBe('background');
});
it('sessionId 不存在时使用 standalone', async () => {
const mockSession = {
getSessionId: vi.fn(() => null),
createChildSession: vi.fn(() => ({
id: 'child-session',
messages: [],
})),
saveChildSession: vi.fn(),
};
initTaskContext({ model: 'test' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'explore',
description: '探索 Agent',
mode: 'subagent',
prompt: '你是探索助手',
});
const result = await taskTool.execute({
description: 'background task',
prompt: 'do something',
subagent_type: 'explore',
run_in_background: true,
});
expect(result.success).toBe(true);
});
});
describe('同步执行模式元数据', () => {
it('同步模式返回正确 metadata', async () => {
const mockSession = createMockSession();
initTaskContext({ model: 'test' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'explore',
description: '探索 Agent',
mode: 'subagent',
prompt: '你是探索助手',
});
const result = await taskTool.execute({
description: 'test',
prompt: 'test',
subagent_type: 'explore',
});
expect(result.metadata?.mode).toBe('sync');
});
it('失败时也返回 metadata', async () => {
const mockSession = createMockSession();
initTaskContext({ model: 'test' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'explore',
description: '探索 Agent',
mode: 'subagent',
prompt: '你是探索助手',
});
mockExecuteResult = {
success: false,
text: '',
steps: 1,
};
const result = await taskTool.execute({
description: 'test',
prompt: 'test',
subagent_type: 'explore',
});
expect(result.success).toBe(false);
expect(result.metadata).toBeDefined();
expect(result.metadata?.mode).toBe('sync');
});
it('失败时没有 error 消息时使用默认消息', async () => {
const mockSession = createMockSession();
initTaskContext({ model: 'test' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'explore',
description: '探索 Agent',
mode: 'subagent',
prompt: '你是探索助手',
});
mockExecuteResult = {
success: false,
text: '',
steps: 1,
// 没有 error 字段
} as any;
const result = await taskTool.execute({
description: 'test',
prompt: 'test',
subagent_type: 'explore',
});
expect(result.success).toBe(false);
expect(result.error).toBe('子任务执行失败');
});
});
describe('图片传递', () => {
it('同步模式传递图片', async () => {
const mockSession = createMockSession();
initTaskContext({ model: 'test' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'explore',
description: '探索 Agent',
mode: 'subagent',
prompt: '你是探索助手',
});
const result = await taskTool.execute({
description: 'analyze images',
prompt: 'describe these',
subagent_type: 'explore',
images: [{ data: 'base64data', mimeType: 'image/png' }],
});
expect(result.success).toBe(true);
});
});
});
@@ -0,0 +1,307 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
// 使用可变的引用对象来绕过 hoisting 问题
const mockState = {
execute: vi.fn(),
};
// Mock agent registry 和 AgentExecutor
vi.mock('../../../../src/agent/index.js', () => {
// 在 mock 工厂内部定义类
return {
agentRegistry: {
listSubagents: vi.fn(() => [
{ name: 'explore', description: '代码探索', mode: 'subagent' },
{ name: 'code-reviewer', description: '代码审查', mode: 'subagent' },
]),
get: vi.fn(),
},
AgentExecutor: class {
execute(...args: any[]) {
return mockState.execute(...args);
}
},
};
});
// Mock tool registry
vi.mock('../../../../src/tools/registry.js', () => ({
toolRegistry: {},
}));
// Mock session manager
vi.mock('../../../../src/session/index.js', () => ({
SessionManager: vi.fn(),
}));
import { taskTool, initTaskContext, updateTaskDescription } from '../../../../src/tools/task/task.js';
import { agentRegistry } from '../../../../src/agent/index.js';
describe('taskTool - Task 工具', () => {
beforeEach(() => {
vi.clearAllMocks();
// 重置上下文为 null
initTaskContext(null as any, null as any);
// 重置 mock
mockState.execute.mockResolvedValue({
success: true,
text: '任务完成',
steps: 3,
});
});
describe('工具定义', () => {
it('有正确的名称', () => {
expect(taskTool.name).toBe('task');
});
it('有正确的元数据', () => {
expect(taskTool.metadata.category).toBe('agent');
expect(taskTool.metadata.keywords).toContain('task');
expect(taskTool.metadata.keywords).toContain('subagent');
});
it('定义了必需的参数', () => {
expect(taskTool.parameters.description.required).toBe(true);
expect(taskTool.parameters.prompt.required).toBe(true);
expect(taskTool.parameters.subagent_type.required).toBe(true);
});
});
describe('initTaskContext - 初始化上下文', () => {
it('设置上下文不报错', () => {
const mockConfig = { model: 'test' };
const mockSession = {
getSessionId: vi.fn(() => 'session-id'),
createChildSession: vi.fn(() => ({ id: 'child', messages: [] })),
saveChildSession: vi.fn(),
};
expect(() => initTaskContext(mockConfig as any, mockSession as any)).not.toThrow();
});
});
describe('updateTaskDescription - 更新描述', () => {
it('更新工具描述', () => {
vi.mocked(agentRegistry.listSubagents).mockReturnValue([
{ name: 'explore', description: '代码探索', mode: 'subagent' },
{ name: 'code-reviewer', description: '代码审查', mode: 'subagent' },
]);
updateTaskDescription();
expect(taskTool.description).toContain('explore');
expect(taskTool.description).toContain('code-reviewer');
});
it('无子 Agent 时显示提示', () => {
vi.mocked(agentRegistry.listSubagents).mockReturnValue([]);
updateTaskDescription();
expect(taskTool.description).toContain('没有可用');
});
});
describe('execute - 执行', () => {
it('未初始化上下文时返回错误', async () => {
// 确保上下文为 null
initTaskContext(null as any, null as any);
vi.mocked(agentRegistry.get).mockReturnValue(undefined);
const result = await taskTool.execute({
description: 'test task',
prompt: 'do something',
subagent_type: 'explore',
});
expect(result.success).toBe(false);
// 可能是未初始化或未找到 Agent
expect(result.error).toBeDefined();
});
it('成功执行子任务', async () => {
const mockSession = {
getSessionId: vi.fn(() => 'parent-session'),
createChildSession: vi.fn(() => ({
id: 'child-session',
messages: [],
})),
saveChildSession: vi.fn(),
};
initTaskContext({ model: 'test' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'explore',
description: '探索 Agent',
mode: 'subagent',
prompt: '你是探索助手',
});
const result = await taskTool.execute({
description: 'search code',
prompt: 'find all API routes',
subagent_type: 'explore',
});
expect(result.success).toBe(true);
expect(result.output).toContain('任务完成');
});
it('未找到 Agent 时返回错误', async () => {
const mockSession = {
getSessionId: vi.fn(() => 'parent-session'),
createChildSession: vi.fn(() => ({ id: 'child', messages: [] })),
saveChildSession: vi.fn(),
};
initTaskContext({ model: 'test' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue(undefined);
vi.mocked(agentRegistry.listSubagents).mockReturnValue([
{ name: 'explore', description: '探索', mode: 'subagent' },
]);
const result = await taskTool.execute({
description: 'test',
prompt: 'test',
subagent_type: 'nonexistent',
});
expect(result.success).toBe(false);
expect(result.error).toContain('未找到 Agent');
});
it('primary 模式 Agent 不能作为子任务', async () => {
const mockSession = {
getSessionId: vi.fn(() => 'parent-session'),
createChildSession: vi.fn(() => ({ id: 'child', messages: [] })),
saveChildSession: vi.fn(),
};
initTaskContext({ model: 'test' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'primary-agent',
description: '主 Agent',
mode: 'primary',
prompt: '你是主助手',
});
const result = await taskTool.execute({
description: 'test',
prompt: 'test',
subagent_type: 'primary-agent',
});
expect(result.success).toBe(false);
expect(result.error).toContain('primary 模式');
});
it('子任务失败时返回错误', async () => {
const mockSession = {
getSessionId: vi.fn(() => 'parent-session'),
createChildSession: vi.fn(() => ({ id: 'child', messages: [] })),
saveChildSession: vi.fn(),
};
initTaskContext({ model: 'test' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'explore',
description: '探索 Agent',
mode: 'subagent',
prompt: '你是探索助手',
});
mockState.execute.mockResolvedValue({
success: false,
text: '',
error: '执行失败',
steps: 1,
});
const result = await taskTool.execute({
description: 'test',
prompt: 'test',
subagent_type: 'explore',
});
expect(result.success).toBe(false);
expect(result.error).toContain('执行失败');
});
it('返回元数据', async () => {
const mockSession = {
getSessionId: vi.fn(() => 'parent-session'),
createChildSession: vi.fn(() => ({
id: 'child-session-123',
messages: [],
})),
saveChildSession: vi.fn(),
};
initTaskContext({ model: 'test' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'explore',
description: '探索 Agent',
mode: 'subagent',
prompt: '你是探索助手',
});
mockState.execute.mockResolvedValue({
success: true,
text: '完成',
steps: 5,
});
const result = await taskTool.execute({
description: 'test',
prompt: 'test',
subagent_type: 'explore',
});
expect(result.metadata).toBeDefined();
expect(result.metadata?.agent).toBe('explore');
expect(result.metadata?.sessionId).toBe('child-session-123');
expect(result.metadata?.steps).toBe(5);
});
it('保存子会话', async () => {
const saveChildSession = vi.fn();
const mockSession = {
getSessionId: vi.fn(() => 'parent-session'),
createChildSession: vi.fn(() => ({
id: 'child-session',
messages: [],
})),
saveChildSession,
};
initTaskContext({ model: 'test' } as any, mockSession as any);
vi.mocked(agentRegistry.get).mockReturnValue({
name: 'explore',
description: '探索 Agent',
mode: 'subagent',
prompt: '你是探索助手',
});
mockState.execute.mockResolvedValue({
success: true,
text: '任务结果',
steps: 2,
});
await taskTool.execute({
description: 'test',
prompt: 'test prompt',
subagent_type: 'explore',
});
expect(saveChildSession).toHaveBeenCalled();
});
});
});