refactor(server): 消除与 Core 的重复类型定义
- 删除 Server 中 60+ 个与 Core 重复的类型定义 - 将动态导入 (await import) 改为静态类型导入 (import type) - 保留必要的运行时静态导入 - 修复测试文件中的 mock 初始化问题 - 净删除约 960 行重复代码 重构文件: - routes/checkpoints.ts: 删除 155 行重复类型 - routes/agents.ts: 删除 93 行重复类型 - routes/commands.ts: 删除 83 行重复类型 - routes/mcp.ts: 修复类型窄化 - routes/hooks.ts: 已使用静态导入 - routes/providers.ts: 删除 63 行重复类型 - session/manager.ts: 删除 41 行重复类型 - routes/sessions.ts: 添加类型导入 - permission/handler.ts: 添加类型导入
This commit is contained in:
@@ -12,6 +12,8 @@ import {
|
||||
type MergedMessage,
|
||||
type MessagePart,
|
||||
} from '../types.js';
|
||||
import type { MessageInfo, Part, ToolPart } from '@ai-assistant/core';
|
||||
import { MessageStorage, PartStorage } from '@ai-assistant/core';
|
||||
|
||||
export const sessionsRouter = new Hono();
|
||||
|
||||
@@ -125,41 +127,6 @@ sessionsRouter.get('/:id/messages', async (c) => {
|
||||
}
|
||||
|
||||
try {
|
||||
// 动态导入 Core 存储 API
|
||||
const corePath = '@ai-assistant/core';
|
||||
type MessageInfo = {
|
||||
id: string;
|
||||
sessionId: string;
|
||||
role: 'user' | 'assistant';
|
||||
parentId?: string;
|
||||
createdAt: number;
|
||||
partIds: string[];
|
||||
};
|
||||
type Part = {
|
||||
id: string;
|
||||
createdAt: number;
|
||||
type: string;
|
||||
text?: string;
|
||||
toolCallId?: string;
|
||||
toolName?: string;
|
||||
state?: {
|
||||
status: 'pending' | 'running' | 'completed' | 'error';
|
||||
input?: Record<string, unknown>;
|
||||
output?: unknown;
|
||||
error?: string;
|
||||
time?: { start: number; end?: number };
|
||||
};
|
||||
};
|
||||
|
||||
const { MessageStorage, PartStorage } = (await import(/* webpackIgnore: true */ corePath)) as {
|
||||
MessageStorage: {
|
||||
listBySession(sessionId: string): Promise<MessageInfo[]>;
|
||||
};
|
||||
PartStorage: {
|
||||
getByIds(messageId: string, partIds: string[]): Promise<Part[]>;
|
||||
};
|
||||
};
|
||||
|
||||
// 获取消息列表(按创建时间排序)
|
||||
const messageInfos = await MessageStorage.listBySession(id);
|
||||
|
||||
@@ -178,17 +145,18 @@ sessionsRouter.get('/:id/messages', async (c) => {
|
||||
if (p.type === 'reasoning') {
|
||||
return { type: 'reasoning', id: p.id, text: p.text ?? '' };
|
||||
}
|
||||
// tool
|
||||
const state = p.state!;
|
||||
const startTime = state.time?.start;
|
||||
const endTime = state.time?.end;
|
||||
// tool - 使用类型断言
|
||||
const toolPart = p as ToolPart;
|
||||
const state = toolPart.state;
|
||||
const startTime = state.status !== 'pending' ? state.time?.start : undefined;
|
||||
const endTime = state.status === 'completed' || state.status === 'error' ? state.time?.end : undefined;
|
||||
return {
|
||||
type: 'tool',
|
||||
id: p.id,
|
||||
toolCallId: p.toolCallId ?? '',
|
||||
toolName: p.toolName ?? '',
|
||||
toolCallId: toolPart.toolCallId ?? '',
|
||||
toolName: toolPart.toolName ?? '',
|
||||
status: state.status,
|
||||
arguments: state.input ?? {},
|
||||
arguments: state.status !== 'pending' ? (state.input as Record<string, unknown>) : {},
|
||||
result: state.status === 'completed' ? state.output : undefined,
|
||||
error: state.status === 'error' ? state.error : undefined,
|
||||
duration: startTime && endTime ? endTime - startTime : undefined,
|
||||
@@ -203,15 +171,15 @@ sessionsRouter.get('/:id/messages', async (c) => {
|
||||
|
||||
// 兼容字段:提取工具调用
|
||||
const toolCalls: ToolCallInfo[] = parts
|
||||
.filter((p) => p.type === 'tool' && p.state)
|
||||
.filter((p): p is ToolPart => p.type === 'tool')
|
||||
.map((p) => {
|
||||
const state = p.state!;
|
||||
const startTime = state.time?.start;
|
||||
const endTime = state.time?.end;
|
||||
const state = p.state;
|
||||
const startTime = state.status !== 'pending' ? state.time?.start : undefined;
|
||||
const endTime = state.status === 'completed' || state.status === 'error' ? state.time?.end : undefined;
|
||||
return {
|
||||
id: p.toolCallId ?? '',
|
||||
name: p.toolName ?? '',
|
||||
arguments: state.input ?? {},
|
||||
arguments: state.status !== 'pending' ? (state.input as Record<string, unknown>) : {},
|
||||
status: state.status,
|
||||
result: state.status === 'completed' ? state.output : undefined,
|
||||
error: state.status === 'error' ? state.error : undefined,
|
||||
|
||||
Reference in New Issue
Block a user