jan/web/models/ChatMessage.ts
Louis 96dba2690d feat: class-based plugin manager
chore: add facades

refactor: core module export

refactor: inference plugin - deprecate function registering (#537)

* refactor: revamp inference plugin as class - deprecate function registering

* refactor: monitoring plugin - deprecate service registering (#538)

refactor: revamp inference plugin as class - deprecate function registering

chore: update import

refactor: plugin revamp - model management

chore: update build steps and remove experimental plugins

refactor: remove pluggable electron

chore: add sorting for conversations

chore: build plugins for testing

chore: consistent plugin directory name

chore: docs

chore: fix CI

chore: update conversation prefix
2023-11-06 13:46:01 +07:00

86 lines
1.9 KiB
TypeScript

import { NewMessageResponse } from '@janhq/core'
import { Message } from '@janhq/core/lib/types'
export enum MessageType {
Text = 'Text',
Image = 'Image',
ImageWithText = 'ImageWithText',
Error = 'Error',
}
export enum MessageSenderType {
Ai = 'assistant',
User = 'user',
}
export enum MessageStatus {
Ready = 'ready',
Pending = 'pending',
}
export interface ChatMessage {
id: string
conversationId: string
messageType: MessageType
messageSenderType: MessageSenderType
senderUid: string
senderName: string
senderAvatarUrl: string
text: string | undefined
imageUrls?: string[] | undefined
createdAt: number
status: MessageStatus
}
export interface RawMessage {
_id?: string
conversationId?: string
user?: string
avatar?: string
message?: string
createdAt?: string
updatedAt?: string
}
export const toChatMessage = (
m: RawMessage | Message | NewMessageResponse,
bot?: Bot,
conversationId?: string
): ChatMessage => {
const createdAt = new Date(m.createdAt ?? '').getTime()
const imageUrls: string[] = []
const imageUrl = undefined
if (imageUrl) {
imageUrls.push(imageUrl)
}
const messageType = MessageType.Text
const messageSenderType =
m.user === 'user' ? MessageSenderType.User : MessageSenderType.Ai
const content = m.message ?? ''
let senderName = m.user === 'user' ? 'You' : 'Assistant'
if (senderName === 'Assistant' && bot) {
senderName = bot.name
}
return {
id: (m._id ?? 0).toString(),
conversationId: (
(m as RawMessage | NewMessageResponse)?.conversationId ??
conversationId ??
0
).toString(),
messageType: messageType,
messageSenderType: messageSenderType,
senderUid: m.user?.toString() || '0',
senderName: senderName,
senderAvatarUrl:
m.user === 'user' ? 'icons/avatar.svg' : 'icons/app_icon.svg',
text: content,
imageUrls: imageUrls,
createdAt: createdAt,
status: MessageStatus.Ready,
}
}