|
|
@@ -135,7 +135,10 @@ def _load_context_in_session(session, grade: DemandGrade) -> V2DemandContext | N
|
|
|
DemandVideoExpansion.biz_dt == grade.biz_dt,
|
|
|
DemandVideoExpansion.source_demand_grade_id == grade.id,
|
|
|
DemandVideoExpansion.is_delete == 0,
|
|
|
- ).order_by(DemandVideoExpansion.id)
|
|
|
+ ).order_by(
|
|
|
+ DemandVideoExpansion.create_time.desc(),
|
|
|
+ DemandVideoExpansion.id.desc(),
|
|
|
+ )
|
|
|
))
|
|
|
video_order, points_by_video = _points_from_expansions(expansions)
|
|
|
if not points_by_video:
|
|
|
@@ -143,7 +146,11 @@ def _load_context_in_session(session, grade: DemandGrade) -> V2DemandContext | N
|
|
|
source_points = list(session.scalars(
|
|
|
select(MultiDemandVideoPoint).where(
|
|
|
MultiDemandVideoPoint.video_id.in_(source_ids)
|
|
|
- ).order_by(MultiDemandVideoPoint.video_id, MultiDemandVideoPoint.id)
|
|
|
+ ).order_by(
|
|
|
+ MultiDemandVideoPoint.video_id,
|
|
|
+ MultiDemandVideoPoint.create_time.desc(),
|
|
|
+ MultiDemandVideoPoint.id.desc(),
|
|
|
+ )
|
|
|
)) if source_ids else []
|
|
|
video_order, points_by_video = _points_from_source_rows(source_ids, source_points)
|
|
|
if not points_by_video:
|
|
|
@@ -187,6 +194,37 @@ def load_v2_demand_context(demand_grade_id: int) -> V2DemandContext:
|
|
|
return context
|
|
|
|
|
|
|
|
|
+def _latest_demand_grade_query(demand_word: str):
|
|
|
+ return (
|
|
|
+ select(DemandGrade)
|
|
|
+ .where(DemandGrade.demand_name == demand_word)
|
|
|
+ .order_by(
|
|
|
+ DemandGrade.biz_dt.desc(),
|
|
|
+ DemandGrade.create_time.desc(),
|
|
|
+ DemandGrade.id.desc(),
|
|
|
+ )
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+def load_latest_v2_demand_context_by_name(demand_word: str) -> V2DemandContext:
|
|
|
+ """Load the newest exact ``demand_name`` match from demand_grade."""
|
|
|
+ normalized = str(demand_word or "").strip()
|
|
|
+ if not normalized:
|
|
|
+ raise ValueError("需求词不能为空")
|
|
|
+ with get_session() as session:
|
|
|
+ grades = list(session.scalars(_latest_demand_grade_query(normalized)))
|
|
|
+ if not grades:
|
|
|
+ raise LookupError(f"demand_grade 未找到需求词: {normalized}")
|
|
|
+ newest = grades[0]
|
|
|
+ context = _load_context_in_session(session, newest)
|
|
|
+ if context is not None:
|
|
|
+ return context
|
|
|
+ raise ValueError(
|
|
|
+ "需求的最新记录没有可用参考视频和点位: "
|
|
|
+ f"demand_name={normalized}, demand_grade_id={newest.id}, biz_dt={newest.biz_dt}"
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
def pick_latest_v2_demand_context(*, index: int = 0) -> V2DemandContext:
|
|
|
with get_session() as session:
|
|
|
latest = session.scalar(select(DemandGrade.biz_dt).where(
|
|
|
@@ -254,3 +292,27 @@ def prepare_v2_demand_run(
|
|
|
reference_video_count=len(context.videos),
|
|
|
point_count=context.point_count,
|
|
|
)
|
|
|
+
|
|
|
+
|
|
|
+def prepare_latest_v2_demand_run_by_name(demand_word: str) -> PreparedV2DemandRun:
|
|
|
+ """Resolve the newest demand record, build its full context, and create a V2 run."""
|
|
|
+ context = load_latest_v2_demand_context_by_name(demand_word)
|
|
|
+ run_key = f"demand-test-{uuid.uuid4().hex}"[:64]
|
|
|
+ rules = build_rule_snapshot()
|
|
|
+ user_input = build_v2_user_input(context, run_id=run_key, rules=rules)
|
|
|
+ get_find_agent_v2_service().create_run(
|
|
|
+ run_id=run_key,
|
|
|
+ user_input=user_input,
|
|
|
+ demand_word=context.demand_name,
|
|
|
+ demand_grade_id=context.demand_grade_id,
|
|
|
+ rule_config=rules,
|
|
|
+ )
|
|
|
+ return PreparedV2DemandRun(
|
|
|
+ run_id=run_key,
|
|
|
+ demand_grade_id=context.demand_grade_id,
|
|
|
+ demand_name=context.demand_name,
|
|
|
+ biz_dt=context.biz_dt,
|
|
|
+ user_input=user_input,
|
|
|
+ reference_video_count=len(context.videos),
|
|
|
+ point_count=context.point_count,
|
|
|
+ )
|