"""Handle draft confirmation with better matching.""" import asyncio async def main(): from app.core.database import AsyncSessionLocal from app.services.ai_pmo.ai_pmo_kernel import AIPMOKernelService from app.services.ai_pmo.draft_confirm_handler import DraftConfirmHandler from sqlalchemy import select, and_ from app.models import WorkItem, IngestEvent tenant_id = 1 async with AsyncSessionLocal() as db: # Find the most recent project_create draft (not project_signal) stmt = ( select(WorkItem, IngestEvent) .join(IngestEvent, IngestEvent.id == WorkItem.ingest_event_id) .where( and_( WorkItem.tenant_id == tenant_id, WorkItem.state == "draft_pending_confirm", WorkItem.intent.like("project_create%"), ) ) .order_by(WorkItem.id.desc()) .limit(1) ) result = await db.execute(stmt) row = result.first() if not row: print("No pending project_create draft found") return work_item, ingest_event = row print(f"Found pending draft: work_item_id={work_item.id}, intent={work_item.intent}") print(f"Summary: {work_item.draft_content.get('summary', 'N/A')}") # Handle confirmation kernel = AIPMOKernelService(db, tenant_id=tenant_id) handler = DraftConfirmHandler(kernel) result = await handler.handle_text_reply( message_text="确认,按这版详细方案入库,并保留里程碑、风险和验收标准。", sender="林宇航", source_system="feishu", source_message_id="om_x100b530d1d6020acc14e0a90435704b", channel_ref="oc_c487d87563e48c2f9566ccd5a758212c", source_sender_open_id="ou_b6d2d3aa8c02d18256def525e85d558f", expected_version=None, thread_context={ "reply_to_message_id": "om_x100b530d7cc744a0c39a8052f539e52" } ) print(f"\nResult: {result}") await db.commit() if result.get("handled"): print(f"\n✅ Success: {result.get('followup_reply')}") print(f"Signal ID: {result.get('signal_id')}") print(f"Project ID: {result.get('project_id')}") else: print(f"\n❌ Failed: {result}") if __name__ == "__main__": asyncio.run(main())