196 lines
5.3 KiB
JavaScript
196 lines
5.3 KiB
JavaScript
"use strict";
|
|
|
|
import {Event} from "../Application/Event.ecma.js";
|
|
import {Common} from "../Utils/Common.ecma.js";
|
|
|
|
/**
|
|
* @class AIChatMessageModel
|
|
* @constructor
|
|
* @param {!AIChatConversationModel} conversation
|
|
* @param {!(Object.<string, any|null>|Array.<any|null>)} inputs
|
|
* @return {void}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
export const AIChatMessageModel = (function(){
|
|
|
|
/**
|
|
* @constructs AIChatMessageModel
|
|
* @param {!AIChatConversationModel} conversation
|
|
* @param {!(Object.<string, any|null>|Array.<any|null>)} inputs
|
|
* @return {void}
|
|
* @access private
|
|
* @static
|
|
*/
|
|
const AIChatMessageModel = function(conversation, inputs){
|
|
/** @type {AIChatConversationModel} */
|
|
this.conversation = conversation;
|
|
/** @type {string} */
|
|
this.status = Common.get_value("status", inputs, "unloaded");
|
|
/** @type {string|null} */
|
|
this.id = Common.get_value("id", inputs);
|
|
/** @type {string} */
|
|
this.role = Common.get_value("role", inputs, "unknown");
|
|
/** @type {boolean} */
|
|
this.ok = Common.get_value("ok", inputs, false);
|
|
/** @type {boolean} */
|
|
this.done = Common.get_value("done", inputs, false);
|
|
/** @type {string} */
|
|
this.message = (Common.get_value("message", inputs, ""));
|
|
/** @type {number} */
|
|
this.tokens = Common.get_value("tokens", inputs, 0);
|
|
/** @type {number} */
|
|
this.date_from = Common.get_value("date_from", inputs, Date.now());
|
|
/** @type {number} */
|
|
this.date_to = Common.get_value("date_to", inputs, Date.now());
|
|
};
|
|
|
|
return AIChatMessageModel;
|
|
})();
|
|
|
|
/**
|
|
* @class AIChatConversationModel
|
|
* @constructor
|
|
* @param {!AIChatModel} chat
|
|
* @param {!(Object.<string, any|null>|Array.<any|null>)} inputs
|
|
* @return {void}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
export const AIChatConversationModel = (function(){
|
|
|
|
/**
|
|
* @constructs AIChatConversationModel
|
|
* @param {!AIChatModel} chat
|
|
* @param {!(Object.<string, any|null>|Array.<any|null>)} inputs
|
|
* @return {void}
|
|
* @access private
|
|
* @static
|
|
*/
|
|
const AIChatConversationModel = function(chat, inputs){
|
|
/** @type {AIChatModel} */
|
|
this.chat = chat;
|
|
/** @type {string} */
|
|
this.id = Common.get_value("id", inputs);
|
|
/** @type {Array.<AIChatMessageModel>} */
|
|
this.messages = Common.get_value("messages", inputs, []).map(message => new AIChatMessageModel(this, message));
|
|
/** @type {boolean} */
|
|
this.selected = Common.get_value("selected", inputs, true);
|
|
};
|
|
|
|
return AIChatConversationModel;
|
|
})();
|
|
|
|
/**
|
|
* @class AIChatModel
|
|
* @constructor
|
|
* @return {void}
|
|
* @access private
|
|
* @static
|
|
*/
|
|
export const AIChatModel = (function(){
|
|
|
|
/**
|
|
* @constructs AIChatModel
|
|
* @return {void}
|
|
* @access private
|
|
* @static
|
|
*/
|
|
const AIChatModel = function(){
|
|
|
|
/** @type {AIChatModel} */
|
|
const self = this;
|
|
|
|
/** @type {Array.<AIChatConversationModel>} */
|
|
this.conversations = [];
|
|
/** @type {AIChatConversationModel|null} */
|
|
this.selected_conversation = null;
|
|
|
|
/** @type {Event} */
|
|
this.on_change = new Event();
|
|
|
|
/**
|
|
* @returns {void}
|
|
* @access private
|
|
*/
|
|
const constructor = () => {
|
|
|
|
self.on_change.add(update_changes);
|
|
|
|
};
|
|
|
|
/**
|
|
* @param {!AIChatMessageModel} model
|
|
* @returns {void}
|
|
* @access private
|
|
*/
|
|
const update_changes = model => {
|
|
|
|
/** @type {HTMLElement|null} */
|
|
const message = document.querySelector(".aichat [data-conversation='" + model.conversation.id + "'] .aichat-message[data-id='" + model.id + "']");
|
|
|
|
};
|
|
|
|
/**
|
|
* @param {!string} conversation
|
|
* @param {!string} id
|
|
* @returns {AIChatMessageModel|null}
|
|
* @access private
|
|
*/
|
|
const get_message = (conversation, id) => {
|
|
|
|
/** @type {AIChatConversationModel|null} */
|
|
const conversation_model = self.conversations.find(model => model.id === conversation);
|
|
|
|
if(conversation_model)
|
|
return conversation_model.messages.find(model => model.id === id) || null;
|
|
return null;
|
|
};
|
|
|
|
/**
|
|
* @param {!string} message
|
|
* @param {!boolean} [done = false]
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.update = (conversation, id, message, done = false) => {
|
|
|
|
/** @type {AIChatMessageModel|null} */
|
|
let model = get_message(conversation, id);
|
|
|
|
if(!model)
|
|
return false;
|
|
|
|
model.message += message;
|
|
model.tokens ++;
|
|
model.done = done;
|
|
conversation_model.chat.on_change.execute(model);
|
|
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* @param {!string} status
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.set_status = status => {
|
|
|
|
/** @type {AIChatMessageModel|null} */
|
|
let model = get_message(conversation, id);
|
|
|
|
if(!model)
|
|
return false;
|
|
|
|
model.status = status;
|
|
conversation_model.chat.on_change.execute(model);
|
|
|
|
return true;
|
|
};
|
|
|
|
constructor();
|
|
|
|
};
|
|
|
|
return AIChatModel;
|
|
})(); |