PMFlow/backend/handle_confirm2.py

66 lines
2.2 KiB
Python

"""Handle draft confirmation directly."""
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 latest pending draft work item
stmt = (
select(WorkItem, IngestEvent)
.join(IngestEvent, IngestEvent.id == WorkItem.ingest_event_id)
.where(
and_(
WorkItem.tenant_id == tenant_id,
WorkItem.state == "draft_pending_confirm",
)
)
.order_by(WorkItem.id.desc())
.limit(1)
)
result = await db.execute(stmt)
row = result.first()
if not row:
print("No pending draft found")
return
work_item, ingest_event = row
print(f"Found pending draft: work_item_id={work_item.id}, intent={work_item.intent}")
# 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_x100b530cb976dc80c2a9f8c5bc69309",
channel_ref="oc_c487d87563e48c2f9566ccd5a758212c",
source_sender_open_id="ou_b6d2d3aa8c02d18256def525e85d558f",
expected_version=None,
thread_context={
"reply_to_message_id": "om_x100b530c8cefdca4c3b0892f8e9a726"
}
)
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())