@tsailab/xai
Version:
The loto-xai is an openai nodejs sdk compatible extension library.
118 lines • 3.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultChatid = void 0;
exports.createDefaultChatbotAgent = createDefaultChatbotAgent;
exports.newChatbotAgent = newChatbotAgent;
exports.createNewUserMessage = createNewUserMessage;
exports.createInitAssistantMessage = createInitAssistantMessage;
exports.updateSomeChatbotMessage = updateSomeChatbotMessage;
exports.mergeMessageAttach = mergeMessageAttach;
const utils_1 = require("../../utils");
exports.defaultChatid = 'xaichat_1000';
/**
* create first Default chatAgent
*/
function createDefaultChatbotAgent(xaiModel, pet) {
const { modelId, model, provider, modelName } = xaiModel;
const agent = {
chatid: exports.defaultChatid,
uuid: pet?.uuid ?? 1000,
title: pet?.title?.length ? pet.title : modelName,
created: Date.now(),
model,
modelId,
provider,
system: pet?.system,
data: [],
};
return agent;
}
/**
*
* @param xaiModel
* @param pet
* @returns agent
*/
function newChatbotAgent(xaiModel, pet) {
const { provider, model, modelId, modelName } = xaiModel;
const { title, uuid, system } = pet;
const chatid = utils_1.RandomHelper.createChatid();
const agent = {
chatid,
title: title?.length ? title : modelName,
created: Date.now(),
uuid,
model,
modelId,
provider,
system,
data: [],
};
return agent;
}
/**
*
* @param chatid string required
* @param text string
* @returns ChatbotMessage
*/
function createNewUserMessage(chatid, text) {
return {
msgid: utils_1.RandomHelper.createMsgid(),
chatid,
created: Date.now(),
text,
error: false,
loading: false,
inversion: true,
role: 'user',
};
}
function createInitAssistantMessage(chatid, reqid = utils_1.RandomHelper.createReqid()) {
return {
msgid: utils_1.RandomHelper.createMsgid(),
chatid,
reqid,
created: Date.now(),
text: '',
error: false,
loading: true,
inversion: false,
role: 'assistant',
};
}
/**
* when data not contains some.chatid return null
* @param data
* @param some
* @returns data or null
*
*/
function updateSomeChatbotMessage(data, some) {
if (!data?.length)
return null;
const { msgid, ...others } = some;
const pos = data.findIndex((m) => m.msgid === msgid);
if (pos < 0) {
return null;
}
data.splice(pos, 1, { ...data[pos], ...others, msgid });
return data;
}
/**
*
* @param attaches
* @param attach
* @returns ChatbotAttach[]
*/
function mergeMessageAttach(attaches, attach) {
const idx = attaches.findIndex((a) => a.id === attach.id);
if (idx < 0) {
attaches.push(attach);
}
else {
attaches.splice(idx, 1, { ...attaches[idx], ...attach });
}
return attaches;
}
//# sourceMappingURL=bot.helper.js.map