(options: ChatCommandOptions, deps: ChatCommandDeps)
| 338 | } |
| 339 | |
| 340 | export async function runChatCommand(options: ChatCommandOptions, deps: ChatCommandDeps): Promise<void> { |
| 341 | const stdout = deps.stdout ?? process.stdout |
| 342 | const stderr = deps.stderr ?? process.stderr |
| 343 | |
| 344 | if (options.question?.trim()) { |
| 345 | const result = await runChatTurn({ ...options, question: options.question.trim() }, deps) |
| 346 | if (options.json) { |
| 347 | write(stdout, `${JSON.stringify(result, null, 2)}\n`) |
| 348 | } else if (options.stream === false) { |
| 349 | write(stdout, `${result.answer}\n`) |
| 350 | } |
| 351 | return |
| 352 | } |
| 353 | |
| 354 | const rl = createInterface({ |
| 355 | input: deps.stdin ?? process.stdin, |
| 356 | output: stdout as Writable, |
| 357 | }) |
| 358 | |
| 359 | try { |
| 360 | let aiChatId = options.aiChatId |
| 361 | while (true) { |
| 362 | const question = (await rl.question('chatlab> ')).trim() |
| 363 | if (!question || question === 'exit' || question === 'quit') break |
| 364 | try { |
| 365 | const result = await runChatTurn({ ...options, aiChatId, question, json: false, stream: true }, deps) |
| 366 | aiChatId = result.aiChatId |
| 367 | } catch (error) { |
| 368 | write(stderr, `${error instanceof Error ? error.message : String(error)}\n`) |
| 369 | } |
| 370 | } |
| 371 | } finally { |
| 372 | rl.close() |
| 373 | } |
| 374 | } |
no test coverage detected