Files
legal-ai/web-ui/src/lib/api/learning.ts
Chaim 959cb093b4
All checks were successful
G12 Leak-Guard / leak-guard (pull_request) Successful in 5s
feat(learning): אינדיקציית-תיק למצב למידת-קול + חילוץ-הלכות אחרי החלטה סופית
אחרי העלאת החלטה סופית והרצת שני הפייפליינים האוטומטיים (למידת-קול,
חילוץ/אימות-הלכות), התיק לא הציג אם כל תהליך בוצע/הצליח/למה-נכשל. במיוחד
תקלת chair_name ריק (2026-06-12) שמפילה בשקט את העתק-ה-case_law → חילוץ-הלכות
לא מתחיל בכלל, בלי שזה גלוי. כעת מוצגות שתי אינדיקציות ליד כפתורי-ההרצה.

Backend (גזירה ממקור-יחיד, ללא מסלול-מעקב מקביל):
- SCHEMA_V36: draft_final_pairs.learning_run (JSONB) — שדה-תיעוד על פנקס-ההתאמה
  (INV-LRN4), חותם את תוצאת-הריצה של פייפליין-הלמידה (succeeded/failed+סיבה+at).
- set_learning_run_outcome() — חיתום הצלחה/כישלון על ה-pair האחרון.
- case_learning_status() — גזירה read-only מ-draft_final_pairs/style_corpus/
  decision_lessons/case_law/halachot: בוצע? הצליח? למה-לא? כמה הלכות חולצו.
- final_learning_pipeline.py — חותם outcome בהצלחה וב-except (surfaced, לא בלוע).
- חשיפה: case_get מוסיף learning_status (→MCP + /api/cases/{case}/details) +
  endpoint ייעודי GET /api/cases/{case}/learning-status (אותה פונקציה — בלי כפילות).

UI (אושר דרך שער-העיצוב Claude Design — כרטיס 21-final-learning-status):
- useCaseLearningStatus (api/learning.ts) — hook + polling עדין בזמן in-flight.
- LearningStatusBadges — 2 שורות (למידת-קול / חילוץ-הלכות) עם badge + תת-שורה
  (מס' לקחים · רישום-קורפוס / מס' הלכות + פירוק אושרו/ממתינות/נדחו / סיבת-כישלון).
- שילוב ב-drafts-panel תחת "החלטה סופית של היו״ר" + אינוולידציה בכפתורי-ההרצה.

אומת מול ה-DB החי: הצליח+5 הלכות (8174-12-24) · נכנס-אך-pending (1200-12-25) ·
לא-נכנס-לקורפוס (8125-09-24) · round-trip חיתום-כישלון. tsc/eslint נקיים.

Invariants: G1 (נרמול-במקור — גזירה, לא טלאי), G2 (אין מסלול מקביל — שדה על
הפנקס הקיים + exposer יחיד), INV-LRN4 (פנקס-ההתאמה), INV-IA1 (מקור-אמת יחיד).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 10:50:12 +00:00

193 lines
6.1 KiB
TypeScript

/**
* Style-acquisition learning surface (T6/T13) — reconciliation ledger + style-distance.
* Backs the /training "למידה" tab. Endpoints under /api/learning.
*/
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { apiRequest } from "./client";
import { trainingKeys } from "./training";
import { methodologyKeys } from "./methodology";
export type DraftFinalPair = {
id: string;
case_id: string | null;
case_number: string;
title: string;
status: "final_received" | "analyzed" | "lessons_folded" | string;
change_percent: number | null;
created_at: string | null;
updated_at: string | null;
};
export type StyleDistance = {
case_number: string;
outcome: string;
golden_ratio_adherence: {
outcome: string;
total_words: number;
sections: Record<string, { actual_pct: number; target: [number, number]; deviation_pp: number }>;
max_deviation: number | null;
};
anti_pattern_hits: { total: number; by_pattern: Record<string, { count: number; note: string }> };
draft_to_final_diff: { change_percent?: number } | null;
pair_status: string | null;
summary: {
ratio_max_deviation_pp: number | null;
anti_pattern_total: number;
change_percent: number | null;
};
error?: string;
};
export const learningKeys = {
all: ["learning"] as const,
pairs: (status: string) => [...learningKeys.all, "pairs", status] as const,
distance: (caseNumber: string) => [...learningKeys.all, "distance", caseNumber] as const,
caseStatus: (caseNumber: string) => [...learningKeys.all, "case-status", caseNumber] as const,
};
export function useReconciliationLedger(status = "") {
return useQuery({
queryKey: learningKeys.pairs(status),
queryFn: ({ signal }) =>
apiRequest<{ items: DraftFinalPair[]; count: number }>(
`/api/learning/pairs${status ? `?status=${status}` : ""}`,
{ signal },
),
staleTime: 15_000,
});
}
export function useStyleDistance(caseNumber: string | null) {
return useQuery({
queryKey: learningKeys.distance(caseNumber ?? ""),
queryFn: ({ signal }) =>
apiRequest<StyleDistance>(
`/api/learning/style-distance/${encodeURIComponent(caseNumber!)}`,
{ signal },
),
enabled: Boolean(caseNumber),
staleTime: 15_000,
});
}
// ── T14: curator distillation proposal + chair approval gate ──────
export type DistillationChange = {
type?: string;
domain?: string;
block?: string;
description?: string;
draft_text?: string;
final_text?: string;
lesson?: string;
};
export type PairDetail = {
id: string;
case_number: string;
title: string;
status: string;
diff_stats: { change_percent?: number } | null;
overall_assessment: string;
changes: DistillationChange[]; // style_method only (server-filtered)
new_expressions: string[];
};
export function usePairDetail(pairId: string | null) {
return useQuery({
queryKey: [...learningKeys.all, "pair", pairId ?? ""] as const,
queryFn: ({ signal }) =>
apiRequest<PairDetail>(`/api/learning/pairs/${pairId}`, { signal }),
enabled: Boolean(pairId),
staleTime: 15_000,
});
}
export function usePromoteLearning(pairId: string) {
const qc = useQueryClient();
return useMutation({
mutationFn: (body: { lessons: string[]; phrases: string[] }) =>
apiRequest<{ id: string; status: string; folded_lessons: number; folded_phrases: number }>(
`/api/learning/pairs/${pairId}/promote`,
{ method: "POST", body },
),
onSuccess: () => {
qc.invalidateQueries({ queryKey: learningKeys.all });
// LRN-10 (INV-IA2): promote flips folded lessons' review_status, so the
// per-corpus LessonsTab must refetch (lessons key is corpus-scoped —
// invalidate the whole "lessons" prefix since promote is pair-scoped).
qc.invalidateQueries({ queryKey: [...trainingKeys.all, "lessons"] });
// MET-1 (INV-IA2): promote also writes discussion_rules + transition_
// phrases['universal'] — /methodology (staleTime 30s) would stay stale.
qc.invalidateQueries({ queryKey: methodologyKeys.all });
},
});
}
// ── Post-final pipeline status (case indicator) ──────────────────────
// Derived status of the two post-final pipelines for one case: whether voice
// learning + halacha extraction ran, succeeded, why not, and how many halachot
// were extracted. Backs the indicator in the drafts panel's final-decision section.
export type VoiceLearningStatus = {
ran: boolean;
outcome: "succeeded" | "failed" | "not_run";
error: string | null;
pair_status: string; // final_received | analyzed | lessons_folded | ''
lessons_count: number;
style_corpus_enrolled: boolean;
lessons_proposed: number;
analyzed_at: string | null;
};
export type HalachaExtractionStatus = {
enrolled_in_corpus: boolean;
not_enrolled_reason: string | null;
status:
| "pending"
| "processing"
| "completed"
| "failed"
| "partial"
| "extraction_failed"
| "no_chunks"
| "busy"
| null;
failed?: boolean;
halachot_count: number;
approved: number;
pending: number;
rejected: number;
};
export type CaseLearningStatus = {
final_uploaded: boolean;
voice_learning: VoiceLearningStatus;
halacha_extraction: HalachaExtractionStatus;
};
/** Whether the indicator should keep polling (a pipeline is mid-flight). */
function isLearningInFlight(s: CaseLearningStatus | undefined): boolean {
const h = s?.halacha_extraction.status;
return h === "processing" || h === "pending" || h === "busy";
}
export function useCaseLearningStatus(caseNumber: string, enabled = true) {
return useQuery({
queryKey: learningKeys.caseStatus(caseNumber),
queryFn: ({ signal }) =>
apiRequest<CaseLearningStatus>(
`/api/cases/${caseNumber}/learning-status`,
{ signal },
),
enabled,
staleTime: 15_000,
// Background pipelines: refetch gently while something is still running.
refetchInterval: (query) =>
isLearningInFlight(query.state.data as CaseLearningStatus | undefined)
? 15_000
: false,
});
}