Files
gongxue-base/apps/admin/src/components/AiChat/message-mappers.ts
wangziqi 8656394b9b
All checks were successful
CI / check (pull_request) Successful in 3m25s
Refactor AI chat: streaming, tool calls, UI polish
2026-07-24 16:27:50 +08:00

39 lines
1.4 KiB
TypeScript

import type { MessageInfo } from '@ant-design/x-sdk';
import type { AiChatMessage, AiChatMessageStatus, AiMessageRecord, AiToolRun } from './types';
function mapStatus(record: AiMessageRecord): AiChatMessageStatus {
if (record.status === 'pending') return 'loading';
if (record.status === 'failed') return 'error';
if (record.status === 'cancelled') return 'abort';
return record.role === 'user' ? 'local' : 'success';
}
function normalizeToolRun(tool: AiToolRun): AiToolRun {
return {
...tool,
status: tool.status === 'error' ? 'failed' : tool.status,
summary: tool.resultSummary ?? tool.argumentsSummary ?? tool.summary,
};
}
export function mapHistoryMessage(record: AiMessageRecord): MessageInfo<AiChatMessage> {
return {
id: record.id,
status: mapStatus(record),
message: {
id: record.id,
role: record.role,
content: record.content || '',
reasoningContent: record.reasoningContent || '',
toolRuns: (record.toolRuns || []).map(normalizeToolRun),
attachments: record.attachments ?? [],
replyToMessageId: record.replyToMessageId,
feedback: record.feedback,
feedbackReason: record.feedbackReason,
metadata: record.metadata,
error: record.status === 'failed' ? record.errorCode || 'AI 回答生成失败' : undefined,
cancelled: record.status === 'cancelled',
},
};
}