UNPKG

@tsailab/xai

Version:

The loto-xai is an openai nodejs sdk compatible extension library.

109 lines 2.57 kB
import { RandomHelper } from '../../utils'; export const defaultChatid = 'xaichat_1000'; /** * create first Default chatAgent */ export function createDefaultChatbotAgent(xaiModel, pet) { const { modelId, model, provider, modelName } = xaiModel; const agent = { chatid: 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 */ export function newChatbotAgent(xaiModel, pet) { const { provider, model, modelId, modelName } = xaiModel; const { title, uuid, system } = pet; const chatid = 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 */ export function createNewUserMessage(chatid, text) { return { msgid: RandomHelper.createMsgid(), chatid, created: Date.now(), text, error: false, loading: false, inversion: true, role: 'user', }; } export function createInitAssistantMessage(chatid, reqid = RandomHelper.createReqid()) { return { msgid: 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 * */ export 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[] */ export 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