@difizen/libro-ai-native
Version:
70 lines (59 loc) • 1.45 kB
text/typescript
import { AnswerState, ChatEvent } from '@difizen/magent-chat';
import type {
IChatMessageSender,
ErrorMessage,
IChatEvent,
ChatEventChunk,
ChatEventError,
ChatEventResult,
IChatMessageItem,
} from '@difizen/magent-chat';
import { autoFactory, AutoFactoryOption } from '@difizen/magent-core';
import { inject, prop } from '@difizen/mana-app';
export class LibroAIChatMessageItemModel {
id?: string;
sender: IChatMessageSender;
state: AnswerState;
protected _content = '';
get content(): string {
return this._content;
}
set content(v) {
this._content = v;
}
option: IChatMessageItem;
error?: ErrorMessage;
constructor( option: IChatMessageItem) {
if (option.content) {
this.state = AnswerState.SUCCESS;
} else {
this.state = AnswerState.WAITING;
}
}
handleEventData(e: IChatEvent) {
if (ChatEvent.isChunk(e)) {
this.appendChunk(e);
}
if (ChatEvent.isError(e)) {
this.handleError(e);
}
if (ChatEvent.isResult(e)) {
this.handleResult(e);
}
}
appendChunk(e: ChatEventChunk) {
this.content = `${this.content}${e.output}`;
}
handleResult(e: ChatEventResult) {
this.content = e.output;
this.state = AnswerState.SUCCESS;
}
handleError(e: ChatEventError) {
this.error = { message: e.message };
this.state = AnswerState.FAIL;
}
}