refactor(backend): update service layers

- services/agent_conversations.py: update agent conversations service
- services/agent_foundation.py: update agent foundation service
- services/orchestrator.py: update orchestrator service
- services/user_agent.py: update user agent service
This commit is contained in:
caoxiaozhu
2026-05-13 15:31:04 +00:00
parent de51ed2e9f
commit 44b2838a12
4 changed files with 462 additions and 80 deletions

View File

@@ -35,6 +35,38 @@ from app.models.financial_record import (
logger = get_logger("app.services.agent_foundation")
DEMO_EXPENSE_CLAIM_SIGNATURES = {
(
"EXP-202605-001",
"张三",
"华南客户拜访差旅报销",
"3280.00",
"submitted",
),
(
"EXP-202605-002",
"李四",
"客户路演餐费",
"860.00",
"approved",
),
(
"EXP-202605-003",
"王五",
"市场活动会务差旅",
"3280.00",
"review",
),
}
DEMO_RECEIVABLE_SIGNATURES = {
("AR-202605-001", "客户A", "50000.00", "partial"),
("AR-202605-002", "客户B", "78000.00", "overdue"),
}
DEMO_PAYABLE_SIGNATURES = {
("AP-202605-001", "供应商A", "33000.00", "scheduled"),
("AP-202605-002", "供应商B", "96000.00", "overdue"),
}
def prepare_agent_foundation() -> None:
settings = get_settings()
@@ -55,7 +87,7 @@ class AgentFoundationService:
try:
Base.metadata.create_all(bind=self.db.get_bind())
self._seed_agent_assets()
self._seed_financial_records()
self._sync_demo_financial_records()
self._seed_runs_and_logs()
self.db.commit()
except Exception:
@@ -63,6 +95,12 @@ class AgentFoundationService:
logger.exception("Failed to prepare agent foundation")
raise
def _sync_demo_financial_records(self) -> None:
if get_settings().seed_demo_financial_records:
self._seed_financial_records()
return
self._purge_demo_financial_records()
def _seed_agent_assets(self) -> None:
existing_codes = set(self.db.scalars(select(AgentAsset.code)).all())
if existing_codes:
@@ -568,6 +606,41 @@ class AgentFoundationService:
self.db.add_all([claim_1, claim_2, claim_3, *ar_records, *ap_records])
def _purge_demo_financial_records(self) -> None:
demo_claims = list(self.db.scalars(select(ExpenseClaim)).all())
for claim in demo_claims:
signature = (
str(claim.claim_no or "").strip(),
str(claim.employee_name or "").strip(),
str(claim.reason or "").strip(),
f"{Decimal(claim.amount or 0):.2f}",
str(claim.status or "").strip(),
)
if signature in DEMO_EXPENSE_CLAIM_SIGNATURES:
self.db.delete(claim)
demo_receivables = list(self.db.scalars(select(AccountsReceivableRecord)).all())
for record in demo_receivables:
signature = (
str(record.receivable_no or "").strip(),
str(record.customer_name or "").strip(),
f"{Decimal(record.amount_outstanding or 0):.2f}",
str(record.status or "").strip(),
)
if signature in DEMO_RECEIVABLE_SIGNATURES:
self.db.delete(record)
demo_payables = list(self.db.scalars(select(AccountsPayableRecord)).all())
for record in demo_payables:
signature = (
str(record.payable_no or "").strip(),
str(record.vendor_name or "").strip(),
f"{Decimal(record.amount_outstanding or 0):.2f}",
str(record.status or "").strip(),
)
if signature in DEMO_PAYABLE_SIGNATURES:
self.db.delete(record)
def _seed_runs_and_logs(self) -> None:
if self.db.scalar(select(AgentRun.id).limit(1)) is not None:
return