MCPcopy Create free account
hub / github.com/echoVic/blade-code / startChatLoop

Function startChatLoop

src/commands/llm.ts:150–217  ·  view source on GitHub ↗

* 开始聊天循环

(llm: BaseLLM, useStream: boolean = false)

Source from the content-addressed store, hash-verified

148 * 开始聊天循环
149 */
150async function startChatLoop(llm: BaseLLM, useStream: boolean = false) {
151 console.log(chalk.cyan('\n🤖 LLM 聊天开始!输入 "quit" 或 "exit" 退出'));
152 console.log(chalk.gray('💡 直接在终端输入消息即可\n'));
153
154 const conversationHistory: Array<{ role: 'user' | 'assistant'; content: string }> = [];
155
156 while (true) {
157 try {
158 // 获取用户输入
159 const answers = await inquirer.prompt([
160 {
161 type: 'input',
162 name: 'message',
163 message: '你:',
164 },
165 ]);
166
167 const userMessage = answers.message.trim();
168
169 if (!userMessage) {
170 console.log(chalk.yellow('请输入有效的消息'));
171 continue;
172 }
173
174 if (userMessage.toLowerCase() === 'quit' || userMessage.toLowerCase() === 'exit') {
175 console.log(chalk.blue('👋 再见!'));
176 break;
177 }
178
179 // 添加用户消息到历史
180 conversationHistory.push({ role: 'user', content: userMessage });
181
182 // 生成回复
183 console.log(chalk.green('\nAI: '), { newline: false });
184
185 if (useStream && llm instanceof QwenLLM && llm.streamChat) {
186 // 流式输出
187 const response = await llm.streamChat(
188 {
189 messages: conversationHistory,
190 },
191 chunk => {
192 process.stdout.write(chunk);
193 }
194 );
195
196 console.log('\n');
197 conversationHistory.push({ role: 'assistant', content: response.content });
198 } else {
199 // 普通输出
200 const response = await llm.conversation(conversationHistory);
201 console.log(response);
202 console.log('');
203
204 conversationHistory.push({ role: 'assistant', content: response });
205 }
206
207 // 保持对话历史在合理长度

Callers 1

llmCommandFunction · 0.85

Calls 5

logMethod · 0.45
streamChatMethod · 0.45
conversationMethod · 0.45
errorMethod · 0.45
destroyMethod · 0.45

Tested by

no test coverage detected