Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina committed May 10, 2024
1 parent 89f3be5 commit c17f20d
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions static/scripts/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,7 @@ async function drawStreamedMessageContents (parent, message) {
const lines = tokenString.split('\n')

if (newLine) {
lines[0] = lines[0].replace(/^ +/g, (spaces) => {
return spaces.split('').map(() => '&nbsp;').join('')
})
lines[0] = addNonBreakingSpaces(lines[0])
newLine = false
}

Expand All @@ -264,13 +262,10 @@ async function drawStreamedMessageContents (parent, message) {
current.scrollIntoView(false)

for (let i = 1; i < lines.length; i++) {
// console.log(`line (newline=${newLine})`, JSON.stringify(lines[i]))
current = document.createElement('p')
parent.appendChild(current)
current.scrollIntoView(false)
lines[i] = lines[i].replace(/^ +/g, (spaces) => {
return spaces.split('').map(() => '&nbsp;').join('')
})
lines[i] = addNonBreakingSpaces(lines[i])
current.innerHTML += lines[i]
newLine = true
}
Expand All @@ -285,6 +280,16 @@ async function drawStreamedMessageContents (parent, message) {
promptLock = false
}

/**
* @param {string} message
* @returns {string}
*/
function addNonBreakingSpaces (str) {
return str.replace(/^ +/g, (spaces) => {
return spaces.split('').map(() => '&nbsp;').join('')
})
}

/**
* @param {{ prompt: string, response: string, errored: boolean | undefined }} message
* @returns {HTMLParagraphElement}
Expand Down

0 comments on commit c17f20d

Please sign in to comment.