PMFlow/backend/handle_confirm.py

67 lines
2.2 KiB
Python

"""Handle draft confirmation directly."""
import asyncio
import os
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 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_x100b53013d259114c4d68abc6d1af6a",
channel_ref="oc_c487d87563e48c2f9566ccd5a758212c",
source_sender_open_id="ou_b6d2d3aa8c02d18256def525e85d558f",
expected_version=None,
thread_context={
"reply_to_message_id": "om_x100b53013e7a4cb0c34d331fdf31ca9"
}
)
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())