Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

fix: send the displayed citation numbering back with conversation history so bare-number follow-ups resolve to what the user saw; link text must be the recording title

Murtaza Nasird5bb28d2cb15
diff --git a/src/services/inquire_agent.py b/src/services/inquire_agent.py
index ff8c23d5..2926340c 100644
--- a/src/services/inquire_agent.py
+++ b/src/services/inquire_agent.py
@@ -575,9 +575,13 @@ seek_seconds, link directly to that moment using the result's `time` value verba
Without a timestamp, link the recording: \
[Title](/recordings/<recording_id>). Cite the recording each substantive point came from, at most \
one citation per bullet or paragraph. These links open the recording and start playback at that \
-moment. The interface renders citations as compact numbered markers with a source list at the end, \
-so do NOT also write the recording's title in the prose around a citation; let the citation itself \
-identify the recording.
+moment. The link text must ALWAYS be the recording's exact title (plus " @ time" when known) — \
+never a date, a number, or other text — because the interface derives a numbered source list from \
+it: citations render as compact numbered markers and the titles appear once at the end. For the \
+same reason, avoid repeating a recording's title in the prose immediately around its citation. \
+When the user refers to a bare number ("what about 5", "open 3"), they mean that numbered citation \
+from your previous answer; the displayed numbering is included with each earlier answer in this \
+conversation — use it, never guess.
- Order information from the most recent recordings first unless the question implies otherwise."""
@@ -601,7 +605,12 @@ KEEP_VERBATIM_MESSAGES = 8 # last 4 turns
def sanitize_history(message_history):
- """Client history -> [{role, content}] with activity provenance folded in."""
+ """Client history -> [{role, content}] with activity provenance folded in.
+
+ Assistant messages also carry the citation numbering the UI actually
+ displayed (the client assigns numbers at render time), so a follow-up
+ like "tell me more about 5" resolves to what the user saw on screen.
+ """
out = []
for msg in message_history or []:
if not isinstance(msg, dict):
@@ -610,8 +619,25 @@ def sanitize_history(message_history):
content = msg.get('content')
if role not in ('user', 'assistant') or not isinstance(content, str) or not content.strip():
continue
- if role == 'assistant' and msg.get('activity'):
- content = f"{content}\n\n[research performed for this answer: {str(msg['activity'])[:300]}]"
+ if role == 'assistant':
+ if msg.get('activity'):
+ content = f"{content}\n\n[research performed for this answer: {str(msg['activity'])[:300]}]"
+ sources = msg.get('sources')
+ if isinstance(sources, list) and sources:
+ lines = []
+ for s in sources[:20]:
+ if not isinstance(s, dict):
+ continue
+ try:
+ n = int(s.get('n'))
+ rid = int(s.get('recording_id'))
+ except (TypeError, ValueError):
+ continue
+ title = str(s.get('title') or '')[:120]
+ lines.append(f"{n} = {title} (recording {rid})")
+ if lines:
+ content = (f"{content}\n\n[citations in this answer were displayed to the "
+ f"user numbered: {'; '.join(lines)}]")
out.append({'role': role, 'content': content})
return out
diff --git a/templates/inquire.html b/templates/inquire.html
index 0d325df5..97a0f0fa 100644
--- a/templates/inquire.html
+++ b/templates/inquire.html
@@ -957,7 +957,7 @@
// appearance per unique recording, with one Sources entry
// per recording at the end. Deterministic here rather than
// trusting the model to number consistently.
- const decorateCitations = (html) => {
+ const decorateCitations = (html, message) => {
try {
const tpl = document.createElement('template');
tpl.innerHTML = html;
@@ -979,6 +979,12 @@
a.textContent = tm ? (entry.n + ' @ ' + tm[1]) : String(entry.n);
a.setAttribute('title', entry.title);
}
+ if (message) {
+ // Persist the displayed numbering so follow-up
+ // turns can resolve "tell me more about 5":
+ // this mapping is sent back with the history.
+ message.sources = [...refs.entries()].map(([id, e]) => ({ n: e.n, recording_id: Number(id), title: e.title }));
+ }
if (refs.size) {
const wrap = document.createElement('div');
wrap.className = 'citation-refs';
@@ -1035,7 +1041,7 @@
const sentHistory = inquireChatMessages.value
.slice(memoryState.fromIndex, -1)
.filter(m => m.role === 'user' || m.role === 'assistant')
- .map(m => ({ role: m.role, content: m.content, activity: m.activitySummary || undefined }));
+ .map(m => ({ role: m.role, content: m.content, activity: m.activitySummary || undefined, sources: (m.sources && m.sources.length) ? m.sources : undefined }));
const sentFromIndex = memoryState.fromIndex;
const controller = new AbortController();
inquireAbort.value = controller;
@@ -1127,7 +1133,7 @@
assistantMessage.steps = agentSteps.value;
}
assistantMessage.content += data.delta;
- assistantMessage.html = decorateCitations(window.renderMarkdownSafe(assistantMessage.content));
+ assistantMessage.html = decorateCitations(window.renderMarkdownSafe(assistantMessage.content), assistantMessage);
// Smart scroll during streaming
await nextTick();
scrollToBottom();
@@ -1136,7 +1142,7 @@
// presenting the partial answer as complete (issue #349).
if (data.truncated && messageAdded && assistantMessage.content) {
assistantMessage.content += `\n\n_⚠️ ${t('chat.responseTruncated')}_`;
- assistantMessage.html = decorateCitations(window.renderMarkdownSafe(assistantMessage.content));
+ assistantMessage.html = decorateCitations(window.renderMarkdownSafe(assistantMessage.content), assistantMessage);
}
return;
} else if (data.error) {
diff --git a/tests/test_inquire_agent.py b/tests/test_inquire_agent.py
index 0711842a..d5903ec1 100644
--- a/tests/test_inquire_agent.py
+++ b/tests/test_inquire_agent.py
@@ -494,6 +494,25 @@ def test_sanitize_history_folds_activity_and_drops_junk():
assert all(set(m) == {'role', 'content'} for m in out)
+def test_sanitize_history_folds_displayed_citation_numbering():
+ """The UI numbers citations at render time; the mapping must reach the
+ model so 'tell me more about 5' resolves to what the user saw."""
+ history = [
+ {'role': 'assistant', 'content': 'answer text',
+ 'sources': [{'n': 1, 'recording_id': 58, 'title': 'Integrating AI'},
+ {'n': 2, 'recording_id': 73, 'title': 'Impact of AI'},
+ {'n': 'junk', 'recording_id': None, 'title': 'dropped'}]},
+ {'role': 'user', 'content': 'tell me more about 2'},
+ ]
+ out = ia.sanitize_history(history)
+ folded = out[0]['content']
+ assert 'displayed to the user numbered' in folded
+ assert '1 = Integrating AI (recording 58)' in folded
+ assert '2 = Impact of AI (recording 73)' in folded
+ assert 'dropped' not in folded
+ assert set(out[0]) == {'role', 'content'}
+
+
def test_compact_history_uses_llm_and_keeps_tail(data, monkeypatch):
with app.app_context():
history = [{'role': 'user' if i % 2 == 0 else 'assistant', 'content': f'm{i}'}