feat: implement answer-limited state handling in voice stream service
- Introduced handling for 'answer_limited' events from the server, allowing the UI to reflect when an assistant's answer has reached the output-token limit. - Updated the VoiceStreamService to manage limited and interrupted turns with bounded memory. - Enhanced the transcript component to display appropriate messages for limited answers, including a fallback for unavailable text. - Added styles to visually indicate limited answers in the UI. - Created unit tests for the new functionality, ensuring correct behavior for limited answers and their presentation.
This commit is contained in:
@@ -48,12 +48,43 @@
|
||||
opacity: 0.8;
|
||||
}
|
||||
|
||||
.bubble.is-limited {
|
||||
/* informational answer state: a quiet accent, never an error treatment */
|
||||
border-inline-start: 2px solid var(--color-primary);
|
||||
padding-inline-start: 0.5rem;
|
||||
}
|
||||
|
||||
.bubble-tag {
|
||||
margin-inline-start: 0.375rem;
|
||||
font-size: 0.75rem;
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
.bubble-tag.is-limited {
|
||||
color: var(--color-primary);
|
||||
font-weight: 600;
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.bubble-footer {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 0.125rem;
|
||||
margin-top: 0.25rem;
|
||||
}
|
||||
|
||||
.bubble-help {
|
||||
font-size: 0.75rem;
|
||||
line-height: 1.6;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.bubble-text.is-missing {
|
||||
color: var(--muted);
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
@keyframes bubbleIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
@@ -77,7 +108,7 @@
|
||||
|
||||
.source-chip {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
align-items: flex-start;
|
||||
gap: 0.375rem;
|
||||
padding: 0.25rem 0.5rem;
|
||||
border: 1px solid var(--surface-border);
|
||||
@@ -85,7 +116,7 @@
|
||||
background: var(--surface-bg);
|
||||
color: var(--text-secondary);
|
||||
font-size: 0.75rem;
|
||||
line-height: 1.5;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.source-chip.is-unavailable {
|
||||
@@ -103,6 +134,15 @@
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
/* The excerpt is the chip's content, shown complete with the provider's own line
|
||||
breaks — it is the retrieved answer text the user asked to see as the source. */
|
||||
.source-label {
|
||||
flex: 1 1 auto;
|
||||
min-width: 0;
|
||||
white-space: pre-wrap;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.transcript-empty {
|
||||
margin: auto;
|
||||
margin-bottom:10px;
|
||||
|
||||
@@ -5,13 +5,23 @@
|
||||
[class.is-user]="message.role === 'user'"
|
||||
[class.is-partial]="!message.final"
|
||||
[class.is-interrupted]="message.interrupted"
|
||||
[class.is-limited]="message.limited && !message.interrupted"
|
||||
dir="rtl"
|
||||
>
|
||||
@if (message.text) {
|
||||
<span class="bubble-text">{{ message.text }}</span>
|
||||
} @else if (message.limited && !message.interrupted) {
|
||||
<!-- A limited answer with no transcript text: never manufacture spoken text. -->
|
||||
<span class="bubble-text is-missing">متن این پاسخ در دسترس نیست</span>
|
||||
}
|
||||
@if (message.interrupted) {
|
||||
<span class="bubble-tag">(قطع شد)</span>
|
||||
} @else if (message.limited) {
|
||||
<!-- Informational answer state: the answer is limited, not a failure and not complete. -->
|
||||
<span class="bubble-footer">
|
||||
<span class="bubble-tag is-limited">پاسخ محدود شده</span>
|
||||
<span class="bubble-help">برای توضیح بیشتر بگویید «ادامه بده».</span>
|
||||
</span>
|
||||
}
|
||||
@if (message.sources?.length) {
|
||||
<ul class="source-list" aria-label="منابع پاسخ">
|
||||
@@ -19,7 +29,7 @@
|
||||
<li
|
||||
class="source-chip"
|
||||
[class.is-unavailable]="source.available === false"
|
||||
[attr.title]="source.excerpt ?? null"
|
||||
[attr.title]="sourceTooltip(source)"
|
||||
>
|
||||
<span class="source-ref">{{ source.source_ref }}</span>
|
||||
<span class="source-label">{{ sourceLabel(source) }}</span>
|
||||
|
||||
@@ -0,0 +1,234 @@
|
||||
import { provideZonelessChangeDetection } from '@angular/core';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { VoiceTranscriptComponent } from './voice-transcript.component';
|
||||
import { VoiceMessage, VoiceSource } from '../../../services/voice-stream/voice-stream.types';
|
||||
|
||||
const LIMITED_LABEL = 'پاسخ محدود شده';
|
||||
const LIMITED_HELP = 'برای توضیح بیشتر بگویید «ادامه بده».';
|
||||
const UNAVAILABLE_TEXT = 'متن این پاسخ در دسترس نیست';
|
||||
|
||||
function message(overrides: Partial<VoiceMessage>): VoiceMessage {
|
||||
return {
|
||||
id: 'turn-1:assistant',
|
||||
turnRef: 'turn-1',
|
||||
role: 'assistant',
|
||||
text: 'پاسخ',
|
||||
final: true,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
async function render(messages: VoiceMessage[]): Promise<HTMLElement> {
|
||||
const fixture = TestBed.createComponent(VoiceTranscriptComponent);
|
||||
fixture.componentRef.setInput('messages', messages);
|
||||
fixture.detectChanges();
|
||||
await fixture.whenStable();
|
||||
return fixture.nativeElement as HTMLElement;
|
||||
}
|
||||
|
||||
describe('VoiceTranscriptComponent', () => {
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [VoiceTranscriptComponent],
|
||||
providers: [provideZonelessChangeDetection()],
|
||||
}).compileComponents();
|
||||
});
|
||||
|
||||
it('shows the limited indication with the continuation help next to the transcript text', async () => {
|
||||
const element = await render([message({ limited: true })]);
|
||||
|
||||
expect(element.textContent).toContain('پاسخ'); // the available transcript stays visible
|
||||
expect(element.textContent).toContain(LIMITED_LABEL);
|
||||
expect(element.textContent).toContain(LIMITED_HELP);
|
||||
// The help text is a separate indication, never appended to the provider transcript.
|
||||
const text = element.querySelector('.bubble-text')?.textContent ?? '';
|
||||
expect(text).toBe('پاسخ');
|
||||
expect(text).not.toContain(LIMITED_HELP);
|
||||
});
|
||||
|
||||
it('shows the unavailable-text presentation when a limited answer has no transcript text', async () => {
|
||||
const element = await render([message({ text: '', limited: true })]);
|
||||
|
||||
expect(element.textContent).toContain(UNAVAILABLE_TEXT);
|
||||
expect(element.textContent).toContain(LIMITED_LABEL);
|
||||
expect(element.textContent).toContain(LIMITED_HELP);
|
||||
});
|
||||
|
||||
it('keeps the sources of a limited answer visible', async () => {
|
||||
const element = await render([
|
||||
message({
|
||||
limited: true,
|
||||
sources: [
|
||||
{
|
||||
source_ref: 'S1',
|
||||
document_id: 'doc-1',
|
||||
chunk_id: 'chunk-1',
|
||||
title: 'سند آزمون',
|
||||
excerpt: 'بانک سؤالات و پاسخهای سامانه هوش مصنوعی تلفنی',
|
||||
location: { page: null, section: 'docx/paragraph/1 | docx/table/1/row/1/cell/1' },
|
||||
},
|
||||
],
|
||||
}),
|
||||
]);
|
||||
|
||||
expect(element.querySelectorAll('.source-chip')).toHaveLength(1);
|
||||
expect(element.textContent).toContain(LIMITED_LABEL);
|
||||
});
|
||||
|
||||
it('shows the excerpt instead of the document section path', async () => {
|
||||
const source: VoiceSource = {
|
||||
source_ref: 'S1',
|
||||
document_id: 'doc-1',
|
||||
chunk_id: 'chunk-1',
|
||||
title: 'document.docx',
|
||||
excerpt: 'ساعت کاری شرکت از شنبه تا چهارشنبه است',
|
||||
location: { page: null, section: 'docx/paragraph/1 | docx/table/1/row/1/cell/1' },
|
||||
};
|
||||
const element = await render([message({ sources: [source] })]);
|
||||
|
||||
const label = element.querySelector('.source-label')?.textContent ?? '';
|
||||
expect(label).toContain('ساعت کاری شرکت از شنبه تا چهارشنبه است');
|
||||
expect(label).not.toContain('docx/table/1/row/1/cell/1');
|
||||
expect(label).not.toContain('document.docx'); // the filename is not the chip label
|
||||
});
|
||||
|
||||
it('keeps the full excerpt and the document details in the hover tooltip', async () => {
|
||||
const source: VoiceSource = {
|
||||
source_ref: 'S1',
|
||||
document_id: 'doc-1',
|
||||
chunk_id: 'chunk-1',
|
||||
title: 'document.docx',
|
||||
excerpt: 'سؤال: ساعت کاری؟\nپاسخ: ۸ تا ۱۳',
|
||||
location: { page: 3, section: 'docx/paragraph/1' },
|
||||
};
|
||||
const element = await render([message({ sources: [source] })]);
|
||||
|
||||
const tooltip = element.querySelector('.source-chip')?.getAttribute('title') ?? '';
|
||||
expect(tooltip).toContain('document.docx');
|
||||
expect(tooltip).toContain('docx/paragraph/1');
|
||||
expect(tooltip).toContain('صفحه 3');
|
||||
expect(tooltip).toContain('سؤال: ساعت کاری؟\nپاسخ: ۸ تا ۱۳');
|
||||
});
|
||||
|
||||
it('shows a single-paragraph excerpt completely with its line breaks', async () => {
|
||||
const excerpt =
|
||||
'بانک سؤالات و پاسخهای سامانه هوش مصنوعی تلفنی: ساعت کاری شرکت از شنبه تا چهارشنبه است';
|
||||
const source: VoiceSource = {
|
||||
source_ref: 'S1',
|
||||
document_id: 'doc-1',
|
||||
chunk_id: 'chunk-1',
|
||||
title: 'document.docx',
|
||||
excerpt,
|
||||
};
|
||||
const element = await render([message({ sources: [source] })]);
|
||||
|
||||
const label = element.querySelector<HTMLElement>('.source-label');
|
||||
// a single paragraph is not summarized: the full text is rendered…
|
||||
expect(label?.textContent).toBe(excerpt);
|
||||
// …and wraps instead of being clipped to one line
|
||||
const style = getComputedStyle(label!);
|
||||
expect(style.whiteSpace).toBe('pre-wrap');
|
||||
expect(style.textOverflow).not.toBe('ellipsis');
|
||||
});
|
||||
|
||||
it('summarizes a multi-paragraph excerpt to its first paragraph with an ellipsis', async () => {
|
||||
const source: VoiceSource = {
|
||||
source_ref: 'S1',
|
||||
document_id: 'doc-1',
|
||||
chunk_id: 'chunk-1',
|
||||
title: 'document.docx',
|
||||
excerpt:
|
||||
'بانک سؤالات و پاسخهای سامانه هوش مصنوعی تلفنی\nسؤال: ساعت کاری شرکت چنده؟\nپاسخ: ۸ تا ۱۳',
|
||||
};
|
||||
const element = await render([message({ sources: [source] })]);
|
||||
|
||||
const label = element.querySelector('.source-label')?.textContent ?? '';
|
||||
expect(label).toBe('بانک سؤالات و پاسخهای سامانه هوش مصنوعی تلفنی …');
|
||||
expect(label).not.toContain('سؤال: ساعت کاری');
|
||||
expect(label).not.toContain('پاسخ: ۸ تا ۱۳');
|
||||
// the complete excerpt stays available in the hover tooltip
|
||||
expect(element.querySelector('.source-chip')?.getAttribute('title')).toContain('پاسخ: ۸ تا ۱۳');
|
||||
});
|
||||
|
||||
it('treats blank-line-separated paragraphs as multiple paragraphs', async () => {
|
||||
const source: VoiceSource = {
|
||||
source_ref: 'S1',
|
||||
document_id: 'doc-1',
|
||||
chunk_id: 'chunk-1',
|
||||
title: 'document.docx',
|
||||
excerpt: 'پاراگراف اول\n\nپاراگراف دوم',
|
||||
};
|
||||
const element = await render([message({ sources: [source] })]);
|
||||
|
||||
const label = element.querySelector('.source-label')?.textContent ?? '';
|
||||
expect(label).toBe('پاراگراف اول …');
|
||||
expect(label).not.toContain('پاراگراف دوم');
|
||||
});
|
||||
|
||||
it('trims the excerpt so trailing wire newlines do not add empty lines', async () => {
|
||||
const source: VoiceSource = {
|
||||
source_ref: 'S1',
|
||||
document_id: 'doc-1',
|
||||
chunk_id: 'chunk-1',
|
||||
title: 'document.docx',
|
||||
excerpt: 'پاسخ: ۸ تا ۱۳\n',
|
||||
};
|
||||
const element = await render([message({ sources: [source] })]);
|
||||
|
||||
expect(element.querySelector('.source-label')?.textContent).toBe('پاسخ: ۸ تا ۱۳');
|
||||
});
|
||||
|
||||
it('falls back to the document title for sources the server marks unavailable', async () => {
|
||||
const element = await render([
|
||||
message({
|
||||
sources: [
|
||||
{
|
||||
source_ref: 'S1',
|
||||
document_id: 'doc-1',
|
||||
chunk_id: 'chunk-1',
|
||||
title: 'document.docx',
|
||||
excerpt: 'متن منقضیشده',
|
||||
available: false,
|
||||
},
|
||||
],
|
||||
}),
|
||||
]);
|
||||
|
||||
const chip = element.querySelector('.source-chip');
|
||||
expect(chip?.classList.contains('is-unavailable')).toBe(true);
|
||||
expect(chip?.querySelector('.source-label')?.textContent).toBe('document.docx');
|
||||
expect(chip?.querySelector('.source-label')?.textContent).not.toContain('متن منقضیشده');
|
||||
// The revoked excerpt stays hidden in the tooltip too.
|
||||
expect(chip?.getAttribute('title')).not.toContain('متن منقضیشده');
|
||||
expect(chip?.getAttribute('title')).toContain('document.docx');
|
||||
});
|
||||
|
||||
it('presents an interrupted answer as interrupted, not as limited', async () => {
|
||||
const element = await render([
|
||||
message({ limited: true, interrupted: true, text: 'پاسخ قطعشده' }),
|
||||
]);
|
||||
|
||||
expect(element.textContent).toContain('(قطع شد)');
|
||||
expect(element.textContent).not.toContain(LIMITED_LABEL);
|
||||
expect(element.textContent).not.toContain(LIMITED_HELP);
|
||||
});
|
||||
|
||||
it('never applies the limited indication to user rows or ordinary answers', async () => {
|
||||
const element = await render([
|
||||
message({ id: 'turn-1:user', turnRef: 'turn-1', role: 'user', text: 'ادامه بده' }),
|
||||
message({ id: 'turn-2:assistant', turnRef: 'turn-2', text: 'پاسخ عادی' }),
|
||||
]);
|
||||
|
||||
expect(element.textContent).not.toContain(LIMITED_LABEL);
|
||||
expect(element.textContent).toContain('ادامه بده');
|
||||
expect(element.textContent).toContain('پاسخ عادی');
|
||||
});
|
||||
|
||||
it('renders the limited indication in an RTL bubble', async () => {
|
||||
const element = await render([message({ limited: true })]);
|
||||
const bubble = element.querySelector('.bubble');
|
||||
|
||||
expect(bubble?.getAttribute('dir')).toBe('rtl');
|
||||
expect(bubble?.classList.contains('is-limited')).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -12,11 +12,42 @@ import { VoiceMessage, VoiceSource } from '../../../services/voice-stream/voice-
|
||||
export class VoiceTranscriptComponent {
|
||||
readonly messages = input.required<VoiceMessage[]>();
|
||||
|
||||
/** "عنوان — صفحه ۱، بند ۲" */
|
||||
/**
|
||||
* Compact chip label: the excerpt's first paragraph, followed by an ellipsis when the
|
||||
* chunk holds more paragraphs. The document's internal `location.section` path (e.g.
|
||||
* "docx/table/1/row/1/cell/1") is document structure, not content, so it is not shown
|
||||
* in the chip (it stays in the hover tooltip, along with the complete excerpt). Sources
|
||||
* the server marks unavailable keep their excerpt hidden and fall back to the title.
|
||||
*/
|
||||
sourceLabel(source: VoiceSource): string {
|
||||
if (source.available !== false) {
|
||||
const excerpt = source.excerpt?.trim();
|
||||
if (excerpt) {
|
||||
// Paragraph breaks (single or repeated newlines) separate the excerpt into
|
||||
// paragraphs; only the first one is summarized into the chip.
|
||||
const paragraphs = excerpt
|
||||
.split(/\r?\n+/)
|
||||
.map((paragraph) => paragraph.trim())
|
||||
.filter(Boolean);
|
||||
const first = paragraphs[0];
|
||||
if (first) return paragraphs.length > 1 ? `${first} …` : first;
|
||||
}
|
||||
}
|
||||
return source.title || source.document_id;
|
||||
}
|
||||
|
||||
/** Hover tooltip: document, location and the complete, untruncated excerpt. */
|
||||
sourceTooltip(source: VoiceSource): string {
|
||||
const parts: string[] = [];
|
||||
if (source.title) parts.push(source.title);
|
||||
if (source.location?.page != null) parts.push(`صفحه ${source.location.page}`);
|
||||
if (source.location?.section) parts.push(source.location.section);
|
||||
return parts.length ? `${source.title} — ${parts.join('، ')}` : source.title;
|
||||
// Same hidden-excerpt rule as the chip label: a source the server marks unavailable
|
||||
// has revoked text, so it is not surfaced here either.
|
||||
if (source.available !== false) {
|
||||
const excerpt = source.excerpt?.trim();
|
||||
if (excerpt) parts.push(excerpt);
|
||||
}
|
||||
return parts.join('\n');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user