8000 fix(chat): render user messages as literal text by Aaravkhanal · Pull Request #569 · google/adk-web · GitHub
[go: up one dir, main page]

Skip to content
Open
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
10 changes: 5 additions & 5 deletions src/app/components/content-bubble/content-bubble.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
@if (item.thought) {
<div class="output-chip-header">thought</div>
}
<ng-container [ngComponentOutlet]="markdownComponent" [ngComponentOutletInputs]="{text: item.text, thought: item.thought}"></ng-container>
<ng-container [ngComponentOutlet]="markdownComponent" [ngComponentOutletInputs]="{text: item.text, thought: item.thought, literalText: role === 'user'}"></ng-container>
</div>
} @else {
<app-code-execution [executableCode]="item.executableCode" [codeExecutionResult]="item.codeExecutionResult"></app-code-execution>
Expand All @@ -48,11 +48,11 @@
@if (part.thought && type !== 'thought') {
<div class="output-chip-header">thought</div>
}
<ng-container [ngComponentOutlet]="markdownComponent" [ngComponentOutletInputs]="{text: part.text, thought: part.thought}"></ng-container>
<ng-container [ngComponentOutlet]="markdownComponent" [ngComponentOutletInputs]="{text: part.text, thought: part.thought, literalText: role === 'user'}"></ng-container>
</div>
}
} @else {
<ng-container [ngComponentOutlet]="markdownComponent" [ngComponentOutletInputs]="{text: uiEvent.text || rawMessageText, thought: uiEvent.thought}"></ng-container>
<ng-container [ngComponentOutlet]="markdownComponent" [ngComponentOutletInputs]="{text: uiEvent.text || rawMessageText, thought: uiEvent.thought, literalText: role === 'user'}"></ng-container>
}
</div>
} @else if (uiEvent.thought || uiEvent.text || uiEvent.renderedContent || uiEvent.a2uiData || uiEvent.event.inputTranscription || uiEvent.event.outputTranscription) {
Expand Down Expand Up @@ -94,11 +94,11 @@
@if (part.thought && type !== 'thought') {
<div class="output-chip-header">thought</div>
}
<ng-container [ngComponentOutlet]="markdownComponent" [ngComponentOutletInputs]="{text: part.text, thought: part.thought}"></ng-container>
<ng-container [ngComponentOutlet]="markdownComponent" [ngComponentOutletInputs]="{text: part.text, thought: part.thought, literalText: role === 'user'}"></ng-container>
</div>
}
} @else {
<ng-container [ngComponentOutlet]="markdownComponent" [ngComponentOutletInputs]="{text: uiEvent.text!, thought: uiEvent.thought}"></ng-container>
<ng-container [ngComponentOutlet]="markdownComponent" [ngComponentOutletInputs]="{text: uiEvent.text!, thought: uiEvent.thought, literalText: role === 'user'}"></ng-container>
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/app/components/markdown/markdown.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
limitations under the License.
-->

<markdown
[data]="text()"
(ready)="openExternalLinksInNewTab()"
[ngStyle]="{
color: thought() ? '#9aa0a6' : 'inherit'
}"
></markdown>
@if (literalText()) {
<span class="literal-text" [style.color]="thought() ? '#9aa0a6' : 'inherit'">{{ text() }}</span>
} @else {
<markdown
[data]="text()"
(ready)="openExternalLinksInNewTab()"
[ngStyle]="{
color: thought() ? '#9aa0a6' : 'inherit'
}"
></markdown>
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ export const MARKDOWN_COMPONENT = new InjectionToken<Type<MarkdownComponentInter
export interface MarkdownComponentInterface {
text: InputSignal<string>;
thought: InputSignal<boolean>;
literalText: InputSignal<boolean>;
isReadme?: InputSignal<boolean>;
}
12 changes: 12 additions & 0 deletions src/app/components/markdown/markdown.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ describe('MarkdownComponent', () => {
expect(element.querySelector('strong')?.textContent).toBe('bold');
}));

it('renders literal text without interpreting Markdown', () => {
const userText = '2*3*4*5';
fixture.componentRef.setInput('text', userText);
fixture.componentRef.setInput('literalText', true);
fixture.detectChanges();

const element: HTMLElement = fixture.nativeElement;
expect(element.querySelector('.literal-text')?.textContent).toBe(userText);
expect(element.querySelector('markdown')).toBeNull();
expect(element.querySelector('em')).toBeNull();
});

it('opens HTTP and HTTPS links in a new tab safely', fakeAsync(() => {
fixture.componentRef.setInput('text', [
'[HTTP](http://example.com)',
Expand Down
6 changes: 6 additions & 0 deletions src/app/components/markdown/markdown.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ import 'prismjs/components/prism-yaml';
provideMarkdown(),
],
styles: [`
.literal-text {
display: block;
white-space: pre-wrap;
overflow-wrap: anywhere;
}
.mermaid-container {
display: flex;
justify-content: center;
Expand Down Expand Up @@ -182,6 +187,7 @@ import 'prismjs/components/prism-yaml';
export class MarkdownComponent implements OnInit {
text = input('');
thought = input(false);
literalText = input(false);
isReadme = input(false);

constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ import {MarkdownComponentInterface} from '../markdown.component.interface';
export class MockMarkdownComponent implements MarkdownComponentInterface {
text = input<string>('');
thought = input<boolean>(false);
literalText = input<boolean>(false);
isReadme = input<boolean>(false);
}
0