SimpleChatTC:ChatMessageEx: Recent chat users upd

Users of recent_chat updated to work with ChatMessageEx

As part of same recent_chat_ns also added, for the case where the
array of chat messages can be passed as is ie in the chat mode,
provided it has only the network handshake representation of the
messages.
This commit is contained in:
hanishkvc 2025-10-14 21:36:11 +05:30
parent 4d9e3d1566
commit 963b9f4661
1 changed files with 22 additions and 7 deletions

View File

@ -273,6 +273,21 @@ class SimpleChat {
return rchat; return rchat;
} }
/**
* Return recent chat messages in the format,
* which can be directly sent to the ai server.
* @param {number} iRecentUserMsgCnt - look at recent_chat for semantic
*/
recent_chat_ns(iRecentUserMsgCnt) {
let xchat = this.recent_chat(iRecentUserMsgCnt);
let chat = []
for (const msg of xchat) {
chat.push(msg.ns)
}
return chat
}
/** /**
* Add an entry into xchat. * Add an entry into xchat.
* NOTE: A new copy is created and added into xchat. * NOTE: A new copy is created and added into xchat.
@ -299,8 +314,8 @@ class SimpleChat {
} }
let last = undefined; let last = undefined;
for(const x of this.recent_chat(gMe.iRecentUserMsgCnt)) { for(const x of this.recent_chat(gMe.iRecentUserMsgCnt)) {
let entry = ui.el_create_append_p(`${x.role}: ${x.content}`, div); let entry = ui.el_create_append_p(`${x.ns.role}: ${x.content_equiv()}`, div);
entry.className = `role-${x.role}`; entry.className = `role-${x.ns.role}`;
last = entry; last = entry;
} }
if (last !== undefined) { if (last !== undefined) {
@ -338,7 +353,7 @@ class SimpleChat {
* The needed fields/options are picked from a global object. * The needed fields/options are picked from a global object.
* Add optional stream flag, if required. * Add optional stream flag, if required.
* Convert the json into string. * Convert the json into string.
* @param {Object} obj * @param {Object<string, any>} obj
*/ */
request_jsonstr_extend(obj) { request_jsonstr_extend(obj) {
for(let k in gMe.apiRequestOptions) { for(let k in gMe.apiRequestOptions) {
@ -358,7 +373,7 @@ class SimpleChat {
*/ */
request_messages_jsonstr() { request_messages_jsonstr() {
let req = { let req = {
messages: this.recent_chat(gMe.iRecentUserMsgCnt), messages: this.recent_chat_ns(gMe.iRecentUserMsgCnt),
} }
return this.request_jsonstr_extend(req); return this.request_jsonstr_extend(req);
} }
@ -370,15 +385,15 @@ class SimpleChat {
request_prompt_jsonstr(bInsertStandardRolePrefix) { request_prompt_jsonstr(bInsertStandardRolePrefix) {
let prompt = ""; let prompt = "";
let iCnt = 0; let iCnt = 0;
for(const chat of this.recent_chat(gMe.iRecentUserMsgCnt)) { for(const msg of this.recent_chat(gMe.iRecentUserMsgCnt)) {
iCnt += 1; iCnt += 1;
if (iCnt > 1) { if (iCnt > 1) {
prompt += "\n"; prompt += "\n";
} }
if (bInsertStandardRolePrefix) { if (bInsertStandardRolePrefix) {
prompt += `${chat.role}: `; prompt += `${msg.ns.role}: `;
} }
prompt += `${chat.content}`; prompt += `${msg.ns.content}`;
} }
let req = { let req = {
prompt: prompt, prompt: prompt,