( options: ProcessMessageOptions, )
| 340 | } |
| 341 | |
| 342 | async function processMessage( |
| 343 | options: ProcessMessageOptions, |
| 344 | ): Promise<{ compactionIndex?: number | null } | void> { |
| 345 | const { |
| 346 | userInput, |
| 347 | chatHistory, |
| 348 | model, |
| 349 | llmApi, |
| 350 | isHeadless, |
| 351 | format, |
| 352 | silent, |
| 353 | compactionIndex: initialCompactionIndex, |
| 354 | firstAssistantResponse = false, |
| 355 | } = options; |
| 356 | let compactionIndex = initialCompactionIndex; |
| 357 | // Check for slash commands in headless mode |
| 358 | if (userInput.trim() === "/compact") { |
| 359 | return handleManualCompaction(chatHistory, model, llmApi, isHeadless); |
| 360 | } |
| 361 | |
| 362 | // Track user prompt |
| 363 | telemetryService.logUserPrompt(userInput.length, userInput); |
| 364 | |
| 365 | // Check if auto-compacting is needed BEFORE adding user message |
| 366 | // The handleAutoCompaction function decides whether compaction is actually needed |
| 367 | const autoCompactionResult = await handleAutoCompaction( |
| 368 | chatHistory, |
| 369 | model, |
| 370 | llmApi, |
| 371 | isHeadless, |
| 372 | format, |
| 373 | ); |
| 374 | if (autoCompactionResult !== null) { |
| 375 | compactionIndex = autoCompactionResult; |
| 376 | // Service already updated in handleAutoCompaction via setHistory |
| 377 | } |
| 378 | |
| 379 | // Add user message to history AFTER potential compaction |
| 380 | services.chatHistory.addUserMessage(userInput); |
| 381 | |
| 382 | // Get AI response with potential tool usage |
| 383 | if (!isHeadless) { |
| 384 | console.info(`\n${chalk.bold.blue("Assistant:")}`); |
| 385 | } |
| 386 | |
| 387 | try { |
| 388 | // Get AI response with potential tool usage |
| 389 | const finalResponse = await getStreamingResponse( |
| 390 | compactionIndex, |
| 391 | model, |
| 392 | llmApi, |
| 393 | ); |
| 394 | |
| 395 | // Generate session title after first assistant response |
| 396 | if (firstAssistantResponse && finalResponse && finalResponse.trim()) { |
| 397 | await handleTitleGeneration(finalResponse, llmApi, model); |
| 398 | } |
| 399 |
no test coverage detected