Async implementation of compact command.
(args: str, context: CommandContext)
| 425 | |
| 426 | |
| 427 | async def _compact_async(args: str, context: CommandContext) -> LocalCommandResult: |
| 428 | """ |
| 429 | Async implementation of compact command. |
| 430 | """ |
| 431 | if not hasattr(context.conversation, "messages"): |
| 432 | return LocalCommandResult( |
| 433 | type="text", |
| 434 | value="No conversation to compact.", |
| 435 | ) |
| 436 | |
| 437 | messages = context.conversation.messages |
| 438 | if len(messages) < 2: |
| 439 | return LocalCommandResult( |
| 440 | type="text", |
| 441 | value=f"Nothing to compact: only {len(messages)} messages.", |
| 442 | ) |
| 443 | |
| 444 | # Get provider from config |
| 445 | provider = context.config.get("provider") |
| 446 | if provider is None: |
| 447 | return LocalCommandResult( |
| 448 | type="text", |
| 449 | value="Compact requires an LLM provider (not available in this context).", |
| 450 | ) |
| 451 | |
| 452 | model = context.config.get("model", "claude-sonnet-4-6") |
| 453 | custom_instructions = args.strip() or None |
| 454 | |
| 455 | try: |
| 456 | # Import here to avoid circular imports |
| 457 | from ..compact_service.service import compact_conversation |
| 458 | |
| 459 | result = await compact_conversation( |
| 460 | conversation=context.conversation, |
| 461 | provider=provider, |
| 462 | model=model, |
| 463 | custom_instructions=custom_instructions, |
| 464 | trigger="manual", |
| 465 | ) |
| 466 | return LocalCommandResult( |
| 467 | type="compact", |
| 468 | value=result.user_display_message or "Conversation compacted.", |
| 469 | compaction_result=CompactionResult( |
| 470 | pre_compact_count=result.pre_compact_count, |
| 471 | post_compact_count=result.post_compact_count, |
| 472 | tokens_saved=result.tokens_saved, |
| 473 | trigger=result.trigger, |
| 474 | summary_preview=result.summary_text[:200] if len(result.summary_text) > 200 else result.summary_text, |
| 475 | ), |
| 476 | ) |
| 477 | except ValueError as e: |
| 478 | return LocalCommandResult(type="text", value=str(e)) |
| 479 | except Exception as e: |
| 480 | import traceback |
| 481 | traceback.print_exc() |
| 482 | return LocalCommandResult( |
| 483 | type="text", |
| 484 | value=f"Compact failed: {e}", |
no test coverage detected