mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-05-31 00:55:42 +08:00
refactor(ChatMessagesPane): extract message key generation logic to a utility function
This commit is contained in:
38
src/components/chat/utils/messageKeys.ts
Normal file
38
src/components/chat/utils/messageKeys.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import type { ChatMessage } from '../types/types';
|
||||
|
||||
const toMessageKeyPart = (value: unknown): string | null => {
|
||||
if (typeof value !== 'string' && typeof value !== 'number') {
|
||||
return null;
|
||||
}
|
||||
|
||||
const normalized = String(value).trim();
|
||||
return normalized.length > 0 ? normalized : null;
|
||||
};
|
||||
|
||||
export const getIntrinsicMessageKey = (message: ChatMessage): string | null => {
|
||||
const candidates = [
|
||||
message.id,
|
||||
message.messageId,
|
||||
message.toolId,
|
||||
message.toolCallId,
|
||||
message.blobId,
|
||||
message.rowid,
|
||||
message.sequence,
|
||||
];
|
||||
|
||||
for (const candidate of candidates) {
|
||||
const keyPart = toMessageKeyPart(candidate);
|
||||
if (keyPart) {
|
||||
return `message-${message.type}-${keyPart}`;
|
||||
}
|
||||
}
|
||||
|
||||
const timestamp = new Date(message.timestamp).getTime();
|
||||
if (!Number.isFinite(timestamp)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const contentPreview = typeof message.content === 'string' ? message.content.slice(0, 48) : '';
|
||||
const toolName = typeof message.toolName === 'string' ? message.toolName : '';
|
||||
return `message-${message.type}-${timestamp}-${toolName}-${contentPreview}`;
|
||||
};
|
||||
Reference in New Issue
Block a user