3f372bcb39
The advisor endpoint now serves two shapes off one orchestration:
- new FE ({question}) -> v3 JSON contract (clarity/answer/evidence/citations/...).
- legacy consumer ({message}, e.g. breakpilot-workspace which reads a text stream and
persists raw bytes) -> plain-text stream of the L2 answer (clean prose, no [n] markup,
no clarify gate). isLegacyRequest() discriminates; answerSystem() gains withCitations.
Prevents the v3 contract from breaking breakpilot-workspace's chat (CLAUDE.md rule #4,
keep every consumer working). No deploy. tsc clean, 13 vitest (incl. isLegacyRequest),
check-loc 0.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
82 lines
2.8 KiB
TypeScript
82 lines
2.8 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import {
|
|
resolveMode,
|
|
mapClarity,
|
|
mapFootnotes,
|
|
buildCitations,
|
|
numberedEvidenceForPrompt,
|
|
isLegacyRequest,
|
|
} from '../advisor/retrieve-mapping'
|
|
import type { EvidenceUnit } from '../advisor/contract'
|
|
|
|
describe('resolveMode', () => {
|
|
it('a chosen context always forces answer', () => expect(resolveMode('clarify', true)).toBe('answer'))
|
|
it('clarify + no context -> clarify', () => expect(resolveMode('clarify', false)).toBe('clarify'))
|
|
it('answer -> answer', () => expect(resolveMode('answer', false)).toBe('answer'))
|
|
it('unknown/undefined -> answer', () => expect(resolveMode(undefined, false)).toBe('answer'))
|
|
})
|
|
|
|
describe('mapClarity', () => {
|
|
it('clarify maps candidate_contexts -> suggested_contexts', () => {
|
|
const c = mapClarity(
|
|
{ mode: 'clarify', concentration: 0.3, candidate_contexts: [{ id: 'ds', label: 'Datenschutz', hits: 5 }] },
|
|
'clarify',
|
|
)
|
|
expect(c.is_underspecified).toBe(true)
|
|
expect(c.suggested_contexts).toEqual([{ id: 'ds', label: 'Datenschutz' }])
|
|
})
|
|
it('answer keeps dominant_context, drops suggestions', () => {
|
|
const c = mapClarity({ mode: 'answer', concentration: 0.88, dominant_context: 'ds' }, 'answer')
|
|
expect(c.is_underspecified).toBe(false)
|
|
expect(c.dominant_context).toBe('ds')
|
|
expect(c.suggested_contexts).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
const ev: EvidenceUnit[] = [
|
|
{ evidence_id: 'e1', document: 'DSGVO', section: 'Art. 30', paragraph: 'Abs. 1', snippet: 'x' },
|
|
{ evidence_id: 'e2', document: 'BDSG', section: '§ 38' },
|
|
]
|
|
|
|
describe('buildCitations', () => {
|
|
it('numbers citations 1..n mapped to evidence', () => {
|
|
const cs = buildCitations(ev)
|
|
expect(cs).toHaveLength(2)
|
|
expect(cs[0]).toMatchObject({ citation_id: 'c1', number: 1, evidence_id: 'e1' })
|
|
expect(cs[1].number).toBe(2)
|
|
})
|
|
})
|
|
|
|
describe('numberedEvidenceForPrompt', () => {
|
|
it('prefixes each unit with [n] + its location', () => {
|
|
const s = numberedEvidenceForPrompt(ev)
|
|
expect(s).toContain('[1] DSGVO Art. 30 Abs. 1')
|
|
expect(s).toContain('[2] BDSG § 38')
|
|
})
|
|
})
|
|
|
|
describe('mapFootnotes', () => {
|
|
it('remaps a /retrieve footnote to the contract footnote', () => {
|
|
const fns = mapFootnotes([
|
|
{ id: 'f1', number: 17, regulation_short: 'EDPB WP248', section: 'Kap III', text: 't' },
|
|
])
|
|
expect(fns[0]).toMatchObject({
|
|
footnote_id: 'f1',
|
|
ref: 'Fußnote 17',
|
|
document: 'EDPB WP248',
|
|
section: 'Kap III',
|
|
text: 't',
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('isLegacyRequest', () => {
|
|
it('message-only (workspace) -> legacy stream', () => {
|
|
expect(isLegacyRequest({ message: 'Ist meine DSE ausreichend?' })).toBe(true)
|
|
})
|
|
it('question present -> contract (JSON)', () => {
|
|
expect(isLegacyRequest({ question: 'x', message: 'y' })).toBe(false)
|
|
expect(isLegacyRequest({ question: 'x' })).toBe(false)
|
|
})
|
|
})
|