Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions bin/chat.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import readline from 'readline'
import { tools } from './tools/tools.js'

/** @type {'text' | 'tool'} */
Expand Down Expand Up @@ -241,26 +242,33 @@ function writeWithColor() {
export function chat() {
/** @type {Message[][]} */
const messages = []
process.stdin.setEncoding('utf-8')

write(colors.system, 'question: ', colors.normal)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true,
})

process.stdin.on('data', async (/** @type {string} */ input) => {
input = input.trim()
if (input === 'exit') {
process.exit()
} else if (input) {
try {
write(colors.user, 'answer: ', colors.normal)
outputMode = 'text' // switch to text output mode
messages.push([{ role: 'user', content: input.trim() }])
await sendMessages(messages)
} catch (error) {
console.error(colors.error, '\n' + error)
} finally {
write('\n\n')
function prompt() {
rl.question(`${colors.system}question: ${colors.normal}`, async (input) => {
input = input.trim()
if (input === 'exit') {
rl.close()
process.exit()
} else if (input) {
try {
write(colors.user, 'answer: ', colors.normal)
outputMode = 'text' // switch to text output mode
messages.push([{ role: 'user', content: input }])
await sendMessages(messages)
} catch (error) {
console.error(colors.error, '\n' + error)
} finally {
write('\n\n')
}
}
}
write(colors.system, 'question: ', colors.normal)
})
prompt()
})
}

prompt()
}