Skip to content

Commit 799e26c

Browse files
authored
Fixed handling of multi-round prompt discussions and minor UI fixes
2 parents cb27c8e + f9e5d09 commit 799e26c

8 files changed

Lines changed: 69 additions & 37 deletions

File tree

chat_client/static/css/klatchatNano.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,4 +437,4 @@ background-color: darkblue;
437437

438438
.modal, .modal-backdrop {
439439
position: absolute !important;
440-
}
440+
}

chat_client/static/js/builder_utils.js

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,12 @@ async function buildSubmindHTML(promptID, submindID, submindUserData, submindRes
177177
}
178178

179179
const phaseDataObjectMapping = {
180-
'response': submindResponse,
181-
'vote': submindVote
180+
'response': submindResponse || emptyAnswer,
181+
'vote': submindVote || emptyAnswer
182182
}
183183
let promptParticipantTemplate;
184184
// Fallback to the single-discussion rounds
185-
if (!Array.isArray(submindOpinions)) {
185+
if (!discussionRounds || discussionRounds === 1) {
186186
phaseDataObjectMapping['opinion'] = submindOpinions;
187187
promptParticipantTemplate = 'prompt_participant'
188188
}else{
@@ -213,13 +213,16 @@ async function buildSubmindHTML(promptID, submindID, submindUserData, submindRes
213213
*/
214214
function buildSubmindDiscussionHTML(promptID, userNickname, submindOpinions, discussionRounds) {
215215
let html = '';
216-
for (let i = 0; i < discussionRounds; i++){
216+
if (!submindOpinions){
217+
submindOpinions = [];
218+
}
219+
for (let i = 1; i <= discussionRounds; i++){
217220
let opinion;
218221
// means that discussion phases were skipped
219-
if (i > submindOpinions.length - 1){
222+
if (i > submindOpinions.length){
220223
opinion = {}
221224
} else {
222-
opinion = submindOpinions[i];
225+
opinion = submindOpinions[i - 1];
223226
}
224227

225228
const createdOnTS = opinion?.created_on
@@ -243,15 +246,25 @@ function buildSubmindDiscussionHTML(promptID, userNickname, submindOpinions, dis
243246
* @param winner_response - shout of the winner
244247
*/
245248
async function buildPromptWinnerHTML(nickname, winner_response) {
246-
return `
247-
<div class="d-flex flex-column align-items-center justify-content-center">
248-
<span class="mt-2 mb-3 font-weight-bold">Selected winner</span>
249-
${await buildPromptParticipantIcon(nickname)}
250-
<div style="max-width: 400px; margin-top: 20px;">
251-
${winner_response}
252-
</div>
253-
</div>
254-
`
249+
let html;
250+
if (nickname){
251+
html = `
252+
<div class="d-flex flex-column align-items-center justify-content-center">
253+
<span class="mt-2 mb-3 font-weight-bold">Selected winner</span>
254+
${await buildPromptParticipantIcon(nickname)}
255+
<div style="max-width: 400px; margin-top: 20px;">
256+
${winner_response}
257+
</div>
258+
</div>
259+
`
260+
}else{
261+
html = `
262+
<div class="d-flex flex-column align-items-center justify-content-center" style="font-weight: normal">
263+
Consensus not reached.
264+
</div>
265+
`
266+
}
267+
return html;
255268
}
256269

257270
/**
@@ -369,6 +382,12 @@ async function buildPromptHTML(prompt) {
369382
promptData['winner'] = 'Consensus not reached.'
370383
}
371384

385+
let tableRowLength = 4;
386+
387+
if (discussionRounds){
388+
tableRowLength += discussionRounds - 1;
389+
}
390+
372391
const discussionsHeader = !discussionRounds
373392
? `<th data-rtc-resizable="discussion">Discussion</th>`
374393
: Array.from({ length: discussionRounds }, (_, i) =>
@@ -381,7 +400,8 @@ async function buildPromptHTML(prompt) {
381400
'prompt_id': prompt['_id'],
382401
'cid': prompt['cid'],
383402
'discussions_header': discussionsHeader,
384-
'message_time': prompt['created_on']
403+
'message_time': prompt['created_on'],
404+
'tr_length': tableRowLength
385405
});
386406
}
387407

chat_client/static/js/chat_utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ async function buildConversation(conversationData, skin, remember=true,conversat
387387
* @param maxResults - max number of messages to fetch
388388
* @returns {Promise<{}>} promise resolving conversation data returned
389389
*/
390-
async function getConversationDataByInput(input, skin, oldestMessageTS=null, maxResults=10){
390+
async function getConversationDataByInput(input, skin, oldestMessageTS=null, maxResults=30){
391391
let conversationData = {};
392392
if(input){
393393
let query_url = `chat_api/search/${input.toString()}?limit_chat_history=${maxResults}&skin=${skin}`;
@@ -696,7 +696,7 @@ async function displayConversation(searchStr, skin=CONVERSATION_SKINS.PROMPTS, a
696696
}
697697
else if (searchStr !== "") {
698698
const alertParent = document.getElementById(alertParentID || conversationParentID);
699-
await getConversationDataByInput(searchStr, skin, null, 10).then(async conversationData => {
699+
await getConversationDataByInput(searchStr, skin, null).then(async conversationData => {
700700
let responseOk = false;
701701
if (!conversationData || Object.keys(conversationData).length === 0){
702702
displayAlert(

chat_client/static/js/klatchatNano.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4071,4 +4071,4 @@ const initKlatChat = (options) => {
40714071
document.addEventListener('DOMContentLoaded', (e) => {
40724072
return new NanoBuilder(options);
40734073
})
4074-
};
4074+
};

chat_client/static/js/message_utils.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,29 @@ const getUserPromptTR = (promptID, userID) => {
106106

107107
/**
108108
* Adds prompt message of specified user id
109-
* @param cid: target conversation id
110-
* @param userID: target submind user id
111-
* @param messageText: message of submind
112-
* @param promptId: target prompt id
113-
* @param promptState: prompt state to consider
109+
* @param cid - target conversation id
110+
* @param userID - target submind user id
111+
* @param messageText - message of submind
112+
* @param promptId - target prompt id
113+
* @param promptState - prompt state to consider
114+
* @param promptContext - associated prompt's context
114115
*/
115-
async function addPromptMessage(cid, userID, messageText, promptId, promptState){
116+
async function addPromptMessage(cid, userID, messageText, promptId, promptState, promptContext){
116117
const tableBody = document.getElementById(`${promptId}_tbody`);
117118
if (await getCurrentSkin(cid) === CONVERSATION_SKINS.PROMPTS){
118119
try {
119120
const userData = await getUserData(userID);
120121
promptState = PROMPT_STATES[promptState].toLowerCase();
121122
if (!getUserPromptTR(promptId, userID)) {
122-
const newUserRow = await buildSubmindHTML(promptId, userID, userData, '', '', '');
123+
const newUserRow = await buildSubmindHTML(promptId, userID, userData, '', '', '', promptContext?.discussion_rounds);
123124
tableBody.insertAdjacentHTML('beforeend', newUserRow);
124125
}
125126
try {
126-
console.log("userData:", userData)
127-
const messageElem = document.getElementById(`${promptId}_${userData['nickname']}_${promptState}`);
127+
let messageElemId = `${promptId}_${userData['nickname']}_${promptState}`
128+
if (promptState === "disc" && promptContext?.discussion_rounds > 1 && promptContext?.discussion_counter){
129+
messageElemId += `_${promptContext?.discussion_counter}`
130+
}
131+
const messageElem = document.getElementById(messageElemId);
128132
messageElem.innerText = messageText;
129133
} catch (e) {
130134
console.warn(`Failed to add prompt message (${cid},${userID}, ${messageText}, ${promptId}, ${promptState}) - ${e}`)
@@ -182,7 +186,7 @@ async function addOldMessages(cid, skin=CONVERSATION_SKINS.BASE) {
182186
const firstMessageItem = messageContainer.children[i];
183187
const oldestMessageTS = await DBGateway.getInstance(DB_TABLES.CHAT_MESSAGES_PAGINATION).getItem(cid).then(res=> res?.oldest_created_on || null);
184188
if (oldestMessageTS) {
185-
const numMessages = await getCurrentSkin(cid) === CONVERSATION_SKINS.PROMPTS? 30: 10;
189+
const numMessages = await getCurrentSkin(cid) === CONVERSATION_SKINS.PROMPTS? 50: 10;
186190
await getConversationDataByInput( cid, skin, oldestMessageTS, numMessages ).then( async conversationData => {
187191
if (messageContainer) {
188192
const userMessageList = getUserMessages( conversationData, null );

chat_client/static/js/sio.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function initSIO(){
7272
});
7373

7474
socket.on('new_prompt_message', async (message) => {
75-
await addPromptMessage(message['cid'], message['userID'], message['messageText'], message['promptID'], message['promptState'])
75+
await addPromptMessage(message['cid'], message['userID'], message['messageText'], message['promptID'], message['promptState'], message?.context)
7676
.catch(err => console.error('Error occurred while adding new prompt data: ', err));
7777
});
7878

@@ -82,9 +82,17 @@ function initSIO(){
8282
console.info(`setting prompt_id=${promptID} as completed`);
8383
if (promptElem){
8484
const promptWinner = document.getElementById(`${promptID}_winner`);
85-
const winner_response = document.getElementById(`${promptID}_${data['winner']}_resp`).innerText;
86-
console.log("data:", data)
87-
promptWinner.innerHTML = await buildPromptWinnerHTML(data['winner'], winner_response);
85+
let winnerResponse;
86+
if (data?.winner){
87+
const winnerRespHTML = document.getElementById(`${promptID}_${data.winner}_resp`);
88+
if (winnerRespHTML) {
89+
winnerResponse = winnerRespHTML.innerText;
90+
}
91+
}
92+
if (!winnerResponse){
93+
winnerResponse = "Consensus not reached."
94+
}
95+
promptWinner.innerHTML = await buildPromptWinnerHTML(data['winner'], winnerResponse);
8896
}else {
8997
console.warn(`Failed to get HTML element from prompt_id=${promptID}`);
9098
}

chat_client/templates/components/prompt_table.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<table class="data table table-bordered prompt-table" data-rtc-resizable-table="table.one" id="{prompt_id}">
33
<thead>
44
<tr>
5-
<th colspan="4">Prompt: "{prompt_text}"</th>
5+
<th colspan="{tr_length}">Prompt: "{prompt_text}"</th>
66
</tr>
77
<tr>
88
<th data-rtc-resizable="subminds">Submind</th>
@@ -15,7 +15,7 @@
1515
{prompt_participants_data}
1616
</tbody>
1717
<tr>
18-
<th colspan="4" id="{prompt_id}_winner" style="font-weight: normal">{selected_winner}</th>
18+
<th colspan="{tr_length}" id="{prompt_id}_winner" style="font-weight: normal">{selected_winner}</th>
1919
</tr>
2020
</table>
2121
</li>

requirements/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cachetools==5.5.0
22
fastapi==0.115.6
33
httpx==0.28.1 # required by FastAPI
44
Jinja2==3.1.4
5-
klatchat-utils~=0.0,>=0.0.1a1
5+
klatchat-utils~=0.0,>=0.0.1a2
66
neon_utils[sentry]~=1.12
77
python-multipart==0.0.9
88
requests==2.32.3

0 commit comments

Comments
 (0)