Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions src/routes/litigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ litigationRoutes.post('/synthesize', async (c) => {

if (result) {
if (!result.aiEnabled) {
return c.json({ synthesis: rawNotes, passthrough: true });
return c.json({ synthesis: rawNotes, passthrough: true, source: 'passthrough' });
}
return c.json({ synthesis: result.result });
return c.json({ synthesis: result.result, source: 'chittyconnect' });
}
console.warn('[litigation/synthesize] ChittyConnect execute failed, falling back to direct AI');
}
Expand All @@ -59,7 +59,7 @@ litigationRoutes.post('/synthesize', async (c) => {
FALLBACK_SYNTHESIZE_PROMPT,
`Raw notes:\n${rawNotes}${property ? `\nProperty: ${property}` : ''}${caseNumber ? `\nCase: ${caseNumber}` : ''}`,
);
return c.json({ synthesis: result });
return c.json({ synthesis: result, source: 'ai-gateway-fallback' });
} catch (err) {
console.error('[litigation/synthesize]', err instanceof Error ? err.message : err);
return c.json({ error: 'AI synthesis failed. Please try again.' }, 502);
Expand Down Expand Up @@ -182,9 +182,9 @@ litigationRoutes.post('/draft', async (c) => {

if (result) {
if (!result.aiEnabled) {
return c.json({ draft: synthesizedFacts, passthrough: true });
return c.json({ draft: synthesizedFacts, passthrough: true, source: 'passthrough' });
}
return c.json({ draft: result.result });
return c.json({ draft: result.result, source: 'chittyconnect' });
}
console.warn('[litigation/draft] ChittyConnect execute failed, falling back to direct AI');
}
Expand All @@ -194,7 +194,7 @@ litigationRoutes.post('/draft', async (c) => {
FALLBACK_DRAFT_PROMPT.replace('{{recipient}}', recipient).replace('{{focus}}', focus),
`Synthesized facts:\n${synthesizedFacts}`,
);
return c.json({ draft: result });
return c.json({ draft: result, source: 'ai-gateway-fallback' });
} catch (err) {
console.error('[litigation/draft]', err instanceof Error ? err.message : err);
return c.json({ error: 'AI drafting failed. Please try again.' }, 502);
Expand Down Expand Up @@ -223,13 +223,19 @@ litigationRoutes.post('/qc', async (c) => {

if (result) {
if (!result.aiEnabled) {
return c.json({ flags: [], passthrough: true });
return c.json({ flags: [], passthrough: true, warning: 'AI not enabled — QC scan skipped' });
}
try {
const cleaned = result.result.replace(/```json\s*/g, '').replace(/```\s*/g, '').trim();
return c.json({ flags: JSON.parse(cleaned) });
} catch {
return c.json({ flags: [], warning: 'QC analysis returned non-parseable results' });
const flags = JSON.parse(cleaned);
if (!Array.isArray(flags)) {
console.warn('[litigation/qc] Parsed result is not an array:', typeof flags);
return c.json({ flags: [], warning: 'QC analysis returned unexpected format — scan incomplete' });
}
return c.json({ flags });
} catch (parseErr) {
console.error('[litigation/qc] Parse failed:', parseErr instanceof Error ? parseErr.message : parseErr);
return c.json({ flags: [], warning: 'QC analysis returned non-parseable results — scan incomplete' });
}
}
console.warn('[litigation/qc] ChittyConnect execute failed, falling back to direct AI');
Expand All @@ -242,11 +248,15 @@ litigationRoutes.post('/qc', async (c) => {
);
const cleaned = result.replace(/```json\s*/g, '').replace(/```\s*/g, '').trim();
const flags = JSON.parse(cleaned);
if (!Array.isArray(flags)) {
console.warn('[litigation/qc] Fallback parsed result is not an array:', typeof flags);
return c.json({ flags: [], warning: 'QC analysis returned unexpected format — scan incomplete' });
}
return c.json({ flags });
} catch (err) {
console.error('[litigation/qc]', err instanceof Error ? err.message : err);
if (err instanceof SyntaxError) {
return c.json({ flags: [], warning: 'QC analysis returned non-parseable results' });
return c.json({ flags: [], warning: 'QC analysis returned non-parseable results — scan incomplete' });
}
return c.json({ error: 'AI QC scan failed. Please try again.' }, 502);
}
Expand Down Expand Up @@ -294,7 +304,11 @@ async function callAIGatewayFallback(
choices?: { message?: { content?: string } }[];
};

return result.choices?.[0]?.message?.content || '';
const content = result.choices?.[0]?.message?.content || '';
if (!content.trim()) {
throw new Error('AI gateway returned empty response');
}
return content;
}

// ── Fallback prompts (used until ChittyConnect prompt registry is seeded) ──
Expand Down
18 changes: 17 additions & 1 deletion ui/src/pages/LitigationAssistant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function LitigationAssistant() {
const [synthesis, setSynthesis] = useState('');
const [draft, setDraft] = useState('');
const [flags, setFlags] = useState<QCFlag[]>([]);
const [qcWarning, setQcWarning] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const [copied, setCopied] = useState(false);

Expand Down Expand Up @@ -95,6 +96,7 @@ export function LitigationAssistant() {
setSynthesis('');
setDraft('');
setFlags([]);
setQcWarning(null);

try {
const res = await api.litigationSynthesize({ rawNotes, property, caseNumber });
Expand Down Expand Up @@ -131,6 +133,7 @@ export function LitigationAssistant() {
try {
const res = await api.litigationQC({ rawNotes, draftEmail: draft });
setFlags(res.flags || []);
setQcWarning(res.warning || null);
setStep('scanned');
} catch (err) {
setError(err instanceof Error ? err.message : 'QC scan failed');
Expand Down Expand Up @@ -384,7 +387,20 @@ export function LitigationAssistant() {
<span className="text-[9px] uppercase tracking-wider text-card-muted font-mono">Step 4</span>
</div>
{step === 'scanned' ? (
flags.length === 0 ? (
flags.length === 0 && qcWarning ? (
<div className="text-center py-6">
<AlertTriangle size={32} className="mx-auto text-amber-500 mb-2" />
<p className="font-semibold text-amber-700">Scan Incomplete</p>
<p className="text-sm text-card-muted mt-1">{qcWarning}</p>
<button
onClick={handleQC}
disabled={isLoading}
className="mt-3 text-xs px-3 py-1.5 rounded-lg bg-amber-100 hover:bg-amber-200 text-amber-700 transition-colors"
>
Retry Scan
</button>
</div>
) : flags.length === 0 ? (
<div className="text-center py-6">
<CheckCircle size={32} className="mx-auto text-emerald-500 mb-2" />
<p className="font-semibold text-emerald-700">Clear</p>
Expand Down
Loading