jan/web/app/_models/ChatMessage.ts
Louis 27258433d1
#357 plugin & app can subscribe and emit events (#358)
* feature: event based plugin

* chore: update README.md

* Update yarn script for build plugins (#363)

* Update yarn script for build plugins

* Plugin-core install from npmjs instead of from local

---------

Co-authored-by: Hien To <>

* #360 plugin preferences (#361)

* feature: #360 plugin preferences

* chore: update core-plugin README.md

* chore: create collections on start

* chore: bumb core version

* chore: update README

* chore: notify preferences update

* fix: preference update

---------

Co-authored-by: hiento09 <136591877+hiento09@users.noreply.github.com>
2023-10-16 10:23:38 +00:00

70 lines
1.8 KiB
TypeScript

import { NewMessageResponse } from "@janhq/plugin-core";
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 | NewMessageResponse): 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 ?? "";
return {
id: (m._id ?? 0).toString(),
conversationId: (m.conversationId ?? 0).toString(),
messageType: messageType,
messageSenderType: messageSenderType,
senderUid: m.user?.toString() || "0",
senderName: m.user === "user" ? "You" : m.user && m.user !== "ai" && m.user !== "assistant" ? m.user : "Assistant",
senderAvatarUrl: m.avatar ? m.avatar : m.user === "user" ? "icons/avatar.svg" : "icons/app_icon.svg",
text: content,
imageUrls: imageUrls,
createdAt: createdAt,
status: MessageStatus.Ready,
};
};