39 lines
1.4 KiB
TypeScript
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',
|
|
},
|
|
};
|
|
}
|