|
| 1 | +import * as readline from 'node:readline/promises'; |
| 2 | +import chalk from 'chalk'; |
| 3 | +import type {DatabaseManager} from '../../../../core/database/manager.js'; |
| 4 | +import {DEFAULT_PROFILE} from '../../../../core/shared/constants.js'; |
| 5 | +import { |
| 6 | + processActTemplate, |
| 7 | + validateCredentials, |
| 8 | + validateProfile, |
| 9 | +} from '../../ask/actions/index.js'; |
| 10 | +import type {ChatOptions} from '../types.js'; |
| 11 | +import {streamChatResponse} from './response-handler.js'; |
| 12 | + |
| 13 | +export interface ChatMessage { |
| 14 | + role: 'user' | 'assistant'; |
| 15 | + content: string; |
| 16 | +} |
| 17 | + |
| 18 | +export const startChatSession = async ( |
| 19 | + _dbManager: DatabaseManager, |
| 20 | + options: ChatOptions, |
| 21 | +): Promise<void> => { |
| 22 | + const profile = options.profile || DEFAULT_PROFILE; |
| 23 | + validateProfile(profile, options); |
| 24 | + |
| 25 | + const credentials = validateCredentials(profile, options); |
| 26 | + |
| 27 | + if (options.verbose) { |
| 28 | + console.log( |
| 29 | + chalk.dim( |
| 30 | + `🔗 Connected to ${credentials.provider} (${credentials.model})`, |
| 31 | + ), |
| 32 | + ); |
| 33 | + console.log(chalk.dim(`📁 Profile: ${profile}`)); |
| 34 | + if (options.act) { |
| 35 | + console.log(chalk.dim(`🎭 Act template: ${options.act}`)); |
| 36 | + } |
| 37 | + console.log(chalk.dim('💬 Type "/quit" or "/exit" to end the chat\n')); |
| 38 | + } |
| 39 | + |
| 40 | + const messages: ChatMessage[] = []; |
| 41 | + |
| 42 | + if (options.act) { |
| 43 | + try { |
| 44 | + const systemPrompt = processActTemplate(options.act, '', options); |
| 45 | + if (systemPrompt?.trim()) { |
| 46 | + messages.push({ |
| 47 | + role: 'assistant', |
| 48 | + content: `I'm now acting as: ${options.act}. How can I help you?`, |
| 49 | + }); |
| 50 | + |
| 51 | + if (options.verbose) { |
| 52 | + console.log(chalk.dim(`🎭 Applied act template: ${options.act}`)); |
| 53 | + } |
| 54 | + } |
| 55 | + } catch (error) { |
| 56 | + console.error(chalk.red(`❌ Error applying act template: ${error}`)); |
| 57 | + return; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + console.log(chalk.bold.blue('🤖 Welcome to Rawi Chat!')); |
| 62 | + console.log( |
| 63 | + chalk.gray( |
| 64 | + 'Start typing your message. Use /quit or /exit to end the chat.\n', |
| 65 | + ), |
| 66 | + ); |
| 67 | + |
| 68 | + const terminal = readline.createInterface({ |
| 69 | + input: process.stdin, |
| 70 | + output: process.stdout, |
| 71 | + }); |
| 72 | + |
| 73 | + try { |
| 74 | + while (true) { |
| 75 | + let userInput: string; |
| 76 | + |
| 77 | + try { |
| 78 | + userInput = await terminal.question(chalk.blue('You: ')); |
| 79 | + } catch (error) { |
| 80 | + if ( |
| 81 | + error instanceof Error && |
| 82 | + 'code' in error && |
| 83 | + error.code === 'ERR_USE_AFTER_CLOSE' |
| 84 | + ) { |
| 85 | + console.log(chalk.yellow('\n👋 Chat session ended (stdin closed)')); |
| 86 | + break; |
| 87 | + } |
| 88 | + throw error; |
| 89 | + } |
| 90 | + |
| 91 | + if (userInput.trim() === '/quit' || userInput.trim() === '/exit') { |
| 92 | + console.log(chalk.yellow('\n👋 Goodbye!')); |
| 93 | + break; |
| 94 | + } |
| 95 | + |
| 96 | + if (userInput.trim() === '/help') { |
| 97 | + console.log(chalk.cyan('\n📚 Chat Commands:')); |
| 98 | + console.log(chalk.gray(' /help - Show this help message')); |
| 99 | + console.log(chalk.gray(' /quit - Exit the chat')); |
| 100 | + console.log(chalk.gray(' /exit - Exit the chat')); |
| 101 | + console.log(chalk.gray(' /clear - Clear message history')); |
| 102 | + console.log(''); |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + if (userInput.trim() === '/clear') { |
| 107 | + messages.length = 0; |
| 108 | + console.log(chalk.dim('🧹 Message history cleared\n')); |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + if (userInput.trim() === '') { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + messages.push({ |
| 117 | + role: 'user', |
| 118 | + content: userInput.trim(), |
| 119 | + }); |
| 120 | + |
| 121 | + try { |
| 122 | + const assistantResponse = await streamChatResponse( |
| 123 | + credentials, |
| 124 | + messages, |
| 125 | + options, |
| 126 | + terminal, |
| 127 | + ); |
| 128 | + |
| 129 | + messages.push({ |
| 130 | + role: 'assistant', |
| 131 | + content: assistantResponse, |
| 132 | + }); |
| 133 | + } catch (error) { |
| 134 | + console.error(chalk.red(`❌ Error: ${error}`)); |
| 135 | + } |
| 136 | + } |
| 137 | + } catch (error) { |
| 138 | + console.error(chalk.red(`❌ Error in chat session: ${error}`)); |
| 139 | + } finally { |
| 140 | + terminal.close(); |
| 141 | + } |
| 142 | +}; |
0 commit comments