@difizen/magent-chat
Version:
79 lines (64 loc) • 1.77 kB
text/typescript
import { autoFactory, AutoFactoryOption } from '@difizen/magent-core';
import { inject, prop } from '@difizen/mana-app';
import type { Dayjs } from 'dayjs';
import dayjs from 'dayjs';
import { QuestionState } from './protocol.js';
import type {
IChatMessageItem,
IChatMessageSender,
BaseChatMessageModel,
AnswerState,
ErrorMessage,
} from './protocol.js';
export interface ChatMessageItemOption extends IChatMessageItem {
parent: BaseChatMessageModel;
}
export class DefaultChatMessageItemModel {
static isOption = (option: IChatMessageItem): option is ChatMessageItemOption => {
return !!option && 'parent' in option;
};
id?: string;
msgId?: string;
sender: IChatMessageSender;
option: ChatMessageItemOption;
protected parent: BaseChatMessageModel;
protected _content: string;
get content(): string {
return this._content;
}
set content(v) {
this._content = v;
}
created?: Dayjs;
modified?: Dayjs;
error?: ErrorMessage;
state: QuestionState | AnswerState;
constructor( option: ChatMessageItemOption) {
this.option = option;
this.id = option.id;
this.parent = option.parent;
this.created = dayjs(option.created);
this.modified = dayjs(option.modified);
this.sender = option.sender;
this.content = option.content;
}
}
export class HumanChatMessageItemModel extends DefaultChatMessageItemModel {
declare state: QuestionState;
constructor( option: ChatMessageItemOption) {
super(option);
if (option.content) {
this.state = QuestionState.SUCCESS;
} else {
this.state = QuestionState.SENDING;
}
}
}