-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
160 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './terminal.ts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import {input} from '@inquirer/prompts' | ||
import chalk from 'chalk' | ||
|
||
import {AIbitatPlugin} from '..' | ||
|
||
/** | ||
* Print a message on the terminal | ||
* | ||
* @param message | ||
* @param simulateStream | ||
*/ | ||
export async function print( | ||
message: {from: string; to: string; content?: string} & { | ||
state: 'loading' | 'error' | 'success' | 'interrupt' | ||
}, | ||
simulateStream: boolean = true, | ||
) { | ||
const replying = chalk.dim(`(to ${message.to})`) | ||
const reference = `${chalk.magenta('✎')} ${chalk.bold( | ||
message.from, | ||
)} ${replying}:` | ||
|
||
if (!simulateStream) { | ||
console.log(reference) | ||
console.log(message.content) | ||
// Add an extra line after the message | ||
console.log() | ||
return | ||
} | ||
|
||
process.stdout.write(`${reference}\n`) | ||
|
||
// Emulate streaming by breaking the cached response into chunks | ||
const chunks = message.content?.split(' ') || [] | ||
const stream = new ReadableStream({ | ||
async start(controller) { | ||
for (const chunk of chunks) { | ||
const bytes = new TextEncoder().encode(chunk + ' ') | ||
controller.enqueue(bytes) | ||
await new Promise(r => | ||
setTimeout( | ||
r, | ||
// get a random number between 10ms and 50ms to simulate a random delay | ||
Math.floor(Math.random() * 40) + 10, | ||
), | ||
) | ||
} | ||
controller.close() | ||
}, | ||
}) | ||
|
||
// Stream the response to the chat | ||
for await (const chunk of stream) { | ||
process.stdout.write(new TextDecoder().decode(chunk)) | ||
} | ||
|
||
// Add an extra line after the message | ||
console.log() | ||
console.log() | ||
} | ||
|
||
/** | ||
* Ask for feedback to the user using the terminal | ||
* | ||
* @param node | ||
* @returns | ||
*/ | ||
export function askForFeedback(node: {from: string; to: string}) { | ||
return input({ | ||
message: `Provide feedback to ${chalk.yellow(node.to)} as ${chalk.yellow( | ||
node.from, | ||
)}. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: `, | ||
}) | ||
} | ||
|
||
/** | ||
* Terminal plugin. It prints the messages on the terminal and asks for feedback | ||
* while the conversation is running in the background. | ||
*/ | ||
export function terminal({ | ||
simulateStream = true, | ||
}: { | ||
/** | ||
* Simulate streaming by breaking the cached response into chunks. | ||
* Helpful to make the conversation more realistic and faster. | ||
* @default true | ||
*/ | ||
simulateStream?: boolean | ||
} = {}) { | ||
return { | ||
name: 'terminal', | ||
setup(aibitat) { | ||
let printing: Promise<void> | null = null | ||
|
||
aibitat.onStart(() => { | ||
console.log() | ||
console.log('🚀 starting chat ...\n') | ||
console.time('🚀 chat finished!') | ||
printing = Promise.resolve() | ||
}) | ||
|
||
aibitat.onMessage(async message => { | ||
await printing | ||
printing = print(message, simulateStream) | ||
}) | ||
|
||
aibitat.onTerminate(() => console.timeEnd('🚀 chat finished')) | ||
|
||
aibitat.onInterrupt(async (node, current) => { | ||
await printing | ||
const feedback = await askForFeedback(node) | ||
// Add an extra line after the message | ||
console.log() | ||
|
||
if (feedback === 'exit') { | ||
console.timeEnd('🚀 chat finished') | ||
return process.exit(0) | ||
} | ||
|
||
await current.continue(feedback) | ||
}) | ||
}, | ||
} as AIbitatPlugin | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.