* 单次问答
( agent: Agent, question: string, scenario: string, useStream: boolean = false, useContext: boolean = false )
| 202 | * 单次问答 |
| 203 | */ |
| 204 | async function answerSingleQuestion( |
| 205 | agent: Agent, |
| 206 | question: string, |
| 207 | scenario: string, |
| 208 | useStream: boolean = false, |
| 209 | useContext: boolean = false |
| 210 | ) { |
| 211 | try { |
| 212 | let response: string; |
| 213 | |
| 214 | if (useStream) { |
| 215 | // 流式输出模式 |
| 216 | console.log(chalk.green('\n💬 AI: '), { newline: false }); |
| 217 | |
| 218 | switch (scenario) { |
| 219 | case 'customer': |
| 220 | if (useContext) { |
| 221 | response = await agent.chatWithContext( |
| 222 | question, |
| 223 | '你是专业的客服代表,友好耐心地解答问题' |
| 224 | ); |
| 225 | console.log(response); |
| 226 | } else { |
| 227 | const messages: LLMMessage[] = [ |
| 228 | { role: 'system', content: '你是专业的客服代表,友好耐心地解答问题' }, |
| 229 | { role: 'user', content: question }, |
| 230 | ]; |
| 231 | response = await agent.streamChat(messages, chunk => { |
| 232 | process.stdout.write(chunk); |
| 233 | }); |
| 234 | } |
| 235 | break; |
| 236 | case 'code': |
| 237 | // 代码场景直接使用非流式,因为需要工具调用 |
| 238 | response = await agent.reviewCode(question, 'auto-detect'); |
| 239 | console.log(response); |
| 240 | break; |
| 241 | case 'assistant': |
| 242 | default: |
| 243 | // 智能助手模式的流式输出 |
| 244 | if (useContext) { |
| 245 | const smartResponse = await agent.smartChatWithContext(question); |
| 246 | |
| 247 | if (smartResponse.toolCalls && smartResponse.toolCalls.length > 0) { |
| 248 | const toolNames = smartResponse.toolCalls.map(t => t.toolName).join(', '); |
| 249 | console.log(chalk.gray(`🔧 使用的工具: ${toolNames}`)); |
| 250 | if (smartResponse.reasoning) { |
| 251 | console.log(chalk.gray(`💭 推理过程: ${smartResponse.reasoning}`)); |
| 252 | } |
| 253 | } |
| 254 | console.log(smartResponse.content); |
| 255 | } else { |
| 256 | const smartResponse = await agent.smartChat(question); |
| 257 | |
| 258 | if (smartResponse.toolCalls && smartResponse.toolCalls.length > 0) { |
| 259 | const toolNames = smartResponse.toolCalls.map(t => t.toolName).join(', '); |
| 260 | console.log(chalk.gray(`🔧 使用的工具: ${toolNames}`)); |
| 261 | if (smartResponse.reasoning) { |
no test coverage detected