Enhance Planning Exercise Profiles and Context Handling
Deploy Development / deploy (push) Successful in 44s
Test Suite / pytest-backend (push) Successful in 43s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 12s
Test Suite / k6 /health Baseline (push) Successful in 33s
Test Suite / playwright-tests (push) Successful in 1m14s

- Introduced new functions to generate skill profiles from exercise IDs, improving the ability to summarize skills for both units and sections.
- Updated the planning target profile to incorporate section-specific exercise IDs, allowing for more granular skill tracking and context.
- Enhanced the ExercisePickerModal and related pages to support section context, including titles, guidance notes, and exercise counts.
- Implemented expectation mode handling in the planning target pipeline to differentiate between planning references and query-only scenarios.
- Incremented version to 0.8.174 and updated changelog to reflect these enhancements in planning AI capabilities.
This commit is contained in:
Lars committed 2026-05-22 23:00:31 +02:00
1 parent 8e68261bc1
commit 04cc77d501
8 files changed
+361 -18

No files matched your search

+155 -3
View File
@@ -12,6 +12,7 @@ from fastapi import HTTPException
from pydantic import BaseModel, Field
from tenant_context import TenantContext, library_content_visibility_sql
from planning_exercise_profiles import skill_profile_summary_from_exercise_ids
from planning_exercise_retrieval import run_multistage_planning_retrieval
from planning_exercise_llm_rank import try_llm_rerank_planning_hits
from planning_exercise_target_pipeline import (
@@ -56,6 +57,9 @@ class PlanningExerciseSuggestRequest(BaseModel):
query: Optional[str] = ""
intent_hint: Optional[str] = None
planned_exercise_ids: Optional[List[int]] = None
section_title: Optional[str] = None
section_guidance_notes: Optional[str] = None
section_planned_exercise_ids: Optional[List[int]] = None
include_llm_intent: bool = True
include_llm_rank: bool = False
limit: int = Field(default=20, ge=1, le=50)
@@ -241,6 +245,131 @@ def _load_group_recent_exercise_ids(
return out
def _section_for_context(
sections: Sequence[Dict[str, Any]],
section_order_index: Optional[int],
) -> Optional[Dict[str, Any]]:
if section_order_index is None:
return None
target = int(section_order_index)
for sec in sections:
if int(sec.get("order_index") or -1) == target:
return sec
if 0 <= target < len(sections):
return sections[target]
return None
def _collect_exercise_ids_from_section(sec: Optional[Dict[str, Any]]) -> List[int]:
if not sec:
return []
out: List[int] = []
seen: Set[int] = set()
for it in sorted(sec.get("items") or [], key=lambda x: int(x.get("order_index") or 0)):
if str(it.get("item_type") or "").strip().lower() == "note":
continue
raw = it.get("exercise_id")
if raw is None:
continue
try:
eid = int(raw)
except (TypeError, ValueError):
continue
if eid < 1 or eid in seen:
continue
seen.add(eid)
out.append(eid)
return out
def _resolve_last_exercise_in_section(sec: Optional[Dict[str, Any]]) -> Tuple[Optional[int], Optional[str]]:
if not sec:
return None, None
last_id: Optional[int] = None
last_title: Optional[str] = None
for it in sorted(sec.get("items") or [], key=lambda x: int(x.get("order_index") or 0)):
if str(it.get("item_type") or "").strip().lower() == "note":
continue
raw = it.get("exercise_id")
if raw is None:
continue
try:
eid = int(raw)
except (TypeError, ValueError):
continue
if eid < 1:
continue
last_id = eid
t = (it.get("exercise_title") or "").strip()
last_title = t or None
return last_id, last_title
def _attach_planning_context_details(
cur,
pack: Dict[str, Any],
*,
sections: Optional[Sequence[Dict[str, Any]]] = None,
body: Optional[PlanningExerciseSuggestRequest] = None,
) -> Dict[str, Any]:
"""Abschnitt, Fähigkeitenprofile und letzte Übung anreichern."""
sec: Optional[Dict[str, Any]] = None
section_idx = pack.get("section_order_index")
if sections is not None and section_idx is not None:
sec = _section_for_context(sections, section_idx)
section_ids = _collect_exercise_ids_from_section(sec)
if body and body.section_planned_exercise_ids:
section_ids = []
seen: Set[int] = set()
for raw in body.section_planned_exercise_ids:
try:
eid = int(raw)
except (TypeError, ValueError):
continue
if eid < 1 or eid in seen:
continue
seen.add(eid)
section_ids.append(eid)
elif pack.get("section_planned_exercise_ids"):
section_ids = list(pack.get("section_planned_exercise_ids") or [])
section_title = pack.get("section_title")
if body and (body.section_title or "").strip():
section_title = (body.section_title or "").strip()
elif sec and (sec.get("title") or "").strip():
section_title = (sec.get("title") or "").strip()
guidance = None
if body and (body.section_guidance_notes or "").strip():
guidance = (body.section_guidance_notes or "").strip()
elif sec and (sec.get("guidance_notes") or "").strip():
guidance = (sec.get("guidance_notes") or "").strip()
last_in_section_id, last_in_section_title = _resolve_last_exercise_in_section(sec)
if body and not last_in_section_id and pack.get("anchor_exercise_id"):
last_in_section_id = pack.get("anchor_exercise_id")
last_in_section_title = pack.get("anchor_title")
unit_ids = list(pack.get("planned_exercise_ids") or [])
pack["section_title"] = section_title
pack["section_guidance_notes"] = guidance
pack["section_planned_exercise_ids"] = section_ids
pack["section_exercise_count"] = len(section_ids)
pack["last_section_exercise_id"] = last_in_section_id
pack["last_section_exercise_title"] = last_in_section_title
pack["unit_skill_profile_summary"] = skill_profile_summary_from_exercise_ids(cur, unit_ids)
pack["section_skill_profile_summary"] = skill_profile_summary_from_exercise_ids(cur, section_ids)
pack["has_planning_reference"] = bool(
unit_ids
or section_ids
or pack.get("anchor_exercise_id")
or (pack.get("unit") or {}).get("framework_slot_id")
or (pack.get("unit") or {}).get("origin_framework_slot_id")
)
return pack
def _section_title_for_index(sections: Sequence[Dict[str, Any]], section_order_index: Optional[int]) -> Optional[str]:
if section_order_index is None:
return None
@@ -348,7 +477,7 @@ def build_planning_exercise_context_pack(
titles = _load_exercise_titles(cur, [x for x in [anchor_id] if x])
anchor_title = titles.get(anchor_id) if anchor_id else None
return {
pack = {
"unit_id": int(body.unit_id),
"unit": {
"id": int(body.unit_id),
@@ -369,6 +498,7 @@ def build_planning_exercise_context_pack(
"progression_edge_notes": progression_notes,
"group_recent_exercise_ids": sorted(group_recent),
}
return _attach_planning_context_details(cur, pack, sections=sections, body=body)
def build_client_planning_context_pack(
@@ -413,7 +543,7 @@ def build_client_planning_context_pack(
titles = _load_exercise_titles(cur, [x for x in [anchor_id] if x])
anchor_title = titles.get(anchor_id) if anchor_id else None
return {
pack = {
"unit_id": None,
"unit": {
"id": None,
@@ -424,7 +554,7 @@ def build_client_planning_context_pack(
"group_id": group_id,
"group_name": group_name,
"section_order_index": body.section_order_index,
"section_title": None,
"section_title": (body.section_title or "").strip() or None,
"planned_exercise_ids": planned_ids,
"anchor_exercise_id": anchor_id,
"anchor_title": anchor_title,
@@ -435,6 +565,7 @@ def build_client_planning_context_pack(
"group_recent_exercise_ids": sorted(group_recent),
"context_mode": "client_free",
}
return _attach_planning_context_details(cur, pack, sections=None, body=body)
def suggest_planning_exercises(
@@ -448,27 +579,40 @@ def suggest_planning_exercises(
else:
pack = build_client_planning_context_pack(cur, tenant=tenant, body=body)
pack = _apply_client_planned_override(cur, pack, body)
pack = _attach_planning_context_details(cur, pack, body=body)
query = _normalize_query(body.query)
heuristic_intent = resolve_planning_exercise_intent(query, body.intent_hint)
has_plan_ref = bool(pack.get("has_planning_reference"))
expectation_mode = "planning_hybrid" if has_plan_ref else "query_only"
pipeline_context = {
"unit_title": pack.get("unit_title"),
"group_name": pack.get("group_name"),
"section_title": pack.get("section_title"),
"section_guidance_notes": pack.get("section_guidance_notes"),
"section_exercise_count": pack.get("section_exercise_count"),
"planned_count": len(pack.get("planned_exercise_ids") or []),
"anchor_title": pack.get("anchor_title"),
"anchor_exercise_id": pack.get("anchor_exercise_id"),
"last_section_exercise_title": pack.get("last_section_exercise_title"),
"progression_graph_id": pack.get("progression_graph_id"),
"unit_skill_profile": pack.get("unit_skill_profile_summary"),
"section_skill_profile": pack.get("section_skill_profile_summary"),
"has_planning_reference": has_plan_ref,
"expectation_mode": expectation_mode,
}
target_profile, intent, scenario_kind, query_intent_summary = build_planning_target_with_query_pipeline(
cur,
unit=pack["unit"],
planned_exercise_ids=pack["planned_exercise_ids"],
section_planned_exercise_ids=pack.get("section_planned_exercise_ids") or [],
anchor_exercise_id=pack.get("anchor_exercise_id"),
query=query,
heuristic_intent=heuristic_intent,
include_llm_intent=body.include_llm_intent,
context_summary=pipeline_context,
has_planning_reference=has_plan_ref,
)
weights = _intent_weights(intent)
target_profile_summary = target_profile.to_summary_dict(cur)
@@ -549,11 +693,18 @@ def suggest_planning_exercises(
"unit_title": pack.get("unit_title"),
"group_name": pack.get("group_name"),
"section_title": pack.get("section_title"),
"section_guidance_notes": pack.get("section_guidance_notes"),
"section_exercise_count": pack.get("section_exercise_count"),
"planned_count": len(planned_set),
"anchor_title": pack.get("anchor_title"),
"anchor_exercise_id": pack.get("anchor_exercise_id"),
"last_section_exercise_title": pack.get("last_section_exercise_title"),
"progression_graph_id": pack.get("progression_graph_id"),
"context_mode": pack.get("context_mode") or ("unit" if pack.get("unit_id") else "client_free"),
"unit_skill_profile": pack.get("unit_skill_profile_summary"),
"section_skill_profile": pack.get("section_skill_profile_summary"),
"has_planning_reference": pack.get("has_planning_reference"),
"expectation_mode": expectation_mode,
}
return {
@@ -568,5 +719,6 @@ def suggest_planning_exercises(
"intent_resolved": intent,
"intent_heuristic": heuristic_intent,
"query_normalized": query or None,
"expectation_mode": expectation_mode,
"hits": hits,
}