This commit is contained in:
zyp
2026-11-25 17:25:46 +08:00
parent c27f84cf91
commit 066a5023b7
11 changed files with 767 additions and 53 deletions
+56 -12
View File
@@ -3,7 +3,7 @@ import logging
import math
from typing import Annotated
from fastapi import APIRouter, Depends, Header, HTTPException
from fastapi import APIRouter, Depends, Header, HTTPException, Response
from pydantic import BaseModel, ConfigDict, Field
from uuid import UUID
@@ -23,14 +23,15 @@ from app.modules.work_assistant.project_document_review import ProjectDocument,
from app.modules.work_assistant.project_document_review import RULE_VERSION as DOCUMENT_RULE
from app.modules.work_assistant.project_risk_review import ProjectRiskRequest, review_project_risk, validation_reason
from app.modules.work_assistant.project_risk_review import RULE_VERSION as PROJECT_RISK_RULE
from app.modules.work_assistant.review_jobs import review_jobs
from app.modules.work_assistant.review_jobs import JobDeferred, review_jobs
from app.modules.work_assistant.model_dispatch import background_review, dispatch
from app.modules.work_assistant.performance_review import PerformanceRequest, review_performance, validation_reason as performance_validation_reason, RULE_VERSION as PERFORMANCE_RULE
from app.modules.work_assistant.personnel_profile import ProfileRequest, review_profile, REVIEW_CACHE_RULE as PERSONNEL_RULE
from app.modules.work_assistant.personnel_profile import ProfileRequest, review_profile, verified_partial_profile, REVIEW_CACHE_RULE as PERSONNEL_RULE, REVIEW_POLICY as PERSONNEL_POLICY
from app.modules.work_assistant.sales_review import SalesRequest, review_sales, RULE_VERSION as SALES_RULE
from app.modules.work_assistant.bid_writing import BidStep, bid_step, RULE_VERSION as BID_RULE
from app.modules.work_assistant.bid_queue import bid_queue
from app.modules.work_assistant.submission_advice import AdviceRequest, review_advice
from app.modules.work_assistant.inspection_review import InspectionRequest, review_inspection
def require_pms_bridge(x_work_assistant_key: Annotated[str | None, Header()] = None) -> None:
@@ -43,6 +44,7 @@ def require_pms_bridge(x_work_assistant_key: Annotated[str | None, Header()] = N
router = APIRouter(dependencies=[Depends(require_pms_bridge)])
logger = logging.getLogger(__name__)
PERSONNEL_JOB_PROTOCOL = 1
@router.post('/submission-advice')
def submission_advice(payload: AdviceRequest) -> dict:
@@ -89,25 +91,52 @@ def message_push_preview(payload: PushPreviewRequest) -> dict:
@router.post("/personnel-profile-review")
def personnel_profile_review(payload: ProfileRequest) -> dict:
def personnel_profile_review(payload: ProfileRequest, response: Response = None) -> dict:
token = background_review.set(True)
from app.modules.work_assistant.personnel_workflow import progress
from app.modules.work_assistant.personnel_workflow import MAX_UNCHANGED_SLICES, progress
protocol_header = {"X-Personnel-Job-Protocol": str(PERSONNEL_JOB_PROTOCOL)}
if response is not None:
response.headers.update(protocol_header)
latest={}
def execute(checkpoint,save):
if checkpoint:latest.update(checkpoint)
def persist(value):
latest.clear();latest.update(value);save(value)
return review_profile(payload,checkpoint=checkpoint,save_checkpoint=persist,staged=True)
try:
return review_profile(payload,checkpoint=checkpoint,save_checkpoint=persist,staged=True)
except JobDeferred:
previous = checkpoint or {}
unchanged = progress(previous)['token'] == progress(latest)['token']
if unchanged:
slices = previous.get('unchanged_slices', 0) + 1
if slices >= MAX_UNCHANGED_SLICES:
result = verified_partial_profile(payload, latest)
logger.warning("personnel_profile_partial reason=unchanged_slices")
return result
persist({**latest, 'unchanged_slices': slices})
raise
except ValueError:
result = verified_partial_profile(payload, latest)
logger.warning("personnel_profile_partial reason=validation")
return result
except ModelUnavailable as exc:
if exc.reason not in {'profile_context_limit', 'direct_output_truncated', 'gateway_output_truncated'} or exc.retryable:
raise
result = verified_partial_profile(payload, latest)
logger.warning("personnel_profile_partial reason=%s", exc.reason)
return result
try:
return review_jobs.run(payload.model_dump(), PERSONNEL_RULE + ':' + routing_revision(),
execute, lease_seconds=180, rapid_recovery=True, progress_recovery=True)
execute, lease_seconds=180, rapid_recovery=True, progress_recovery=True, resume_context_failures=True)
except ModelUnavailable as exc:
# A saved time slice is waiting to continue, not a model outage. Keep
# Java on its waiting path without consuming its service-failure budget.
reason = exc.reason if exc.reason in {'review_continuation', 'review_suspended', 'review_in_progress', 'review_backoff',
'circuit_open', 'queue_full', 'queue_timeout', 'context_capacity'} else 'model_timeout' if 'timeout' in exc.reason else 'model_unavailable'
reason = exc.reason
if reason not in {'review_continuation', 'review_suspended', 'review_in_progress', 'review_backoff',
'circuit_open', 'queue_full', 'queue_timeout', 'context_capacity', 'profile_context_limit'}:
reason = ('model_timeout' if 'timeout' in reason else 'model_retrying') if exc.retryable else 'model_unavailable'
delay = max(1, min(900, math.ceil(exc.retry_after or 15)))
headers={"X-Review-State": reason, "Retry-After": str(delay)}
headers={**protocol_header, "X-Review-State": reason, "Retry-After": str(delay)}
if latest:
state=progress(latest)
headers.update({'X-Review-Progress':state['token'],'X-Review-Phase':state['phase'],
@@ -115,7 +144,8 @@ def personnel_profile_review(payload: ProfileRequest) -> dict:
raise HTTPException(503, "人员画像等待继续分析", headers=headers) from exc
except (ValueError, TypeError, AttributeError) as exc:
logger.warning("personnel_profile_failed type=%s", type(exc).__name__)
raise HTTPException(502, "画像结论未通过依据校验,未发布结果", headers={"X-Review-State": "validation"}) from exc
raise HTTPException(502, "画像结论未通过依据校验,未发布结果",
headers={**protocol_header, "X-Review-State": "validation"}) from exc
finally:
background_review.reset(token)
@@ -226,10 +256,24 @@ def project_document_review(payload: ProjectDocument) -> dict:
@router.get("/review-capabilities")
def review_capabilities() -> dict:
return {"bid_writing_rule": BID_RULE, "project_risk_rule": PROJECT_RISK_RULE, "document_rule": DOCUMENT_RULE, "performance_rule": PERFORMANCE_RULE, "sales_rule": SALES_RULE, "dispatch_version": 1,
"personnel_rule": PERSONNEL_RULE, "goal_review_rule": GOAL_RULE, "decomposition_rule": DECOMPOSITION_RULE,
"personnel_rule": PERSONNEL_POLICY, "personnel_job_protocol": PERSONNEL_JOB_PROTOCOL,
"goal_review_rule": GOAL_RULE, "decomposition_rule": DECOMPOSITION_RULE,
"task_review_schema": TASK_REVIEW_SCHEMA, "routing_revision": routing_revision(), "model_dispatch": dispatch.snapshot()}
@router.post("/inspection-review")
def inspection_review(payload: InspectionRequest) -> dict:
token = background_review.set(True)
try:
return review_inspection(payload)
except ModelUnavailable as error:
raise HTTPException(503, "工作检查分析暂不可用") from error
except ValueError as error:
raise HTTPException(422, "工作检查依据核对未通过") from error
finally:
background_review.reset(token)
@router.post("/meeting-work-review")
def meeting_work_review(payload: MeetingItem) -> dict:
try: