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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Add auto-scroll attribute to conversation.",
"packageName": "@ni/spright-angular",
"email": "163188334+Alexia-Claudia-Micu@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
Comment thread
Alexia-Claudia-Micu marked this conversation as resolved.
"type": "minor",
"comment": "Add autoscroll to chat conversation",
"packageName": "@ni/spright-blazor",
"email": "jattasNI@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
Comment thread
Alexia-Claudia-Micu marked this conversation as resolved.
"type": "minor",
"comment": "Add scrolling behavior to chat.",
"packageName": "@ni/spright-components",
"email": "163188334+Alexia-Claudia-Micu@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -1,61 +1,193 @@
import { Component } from '@angular/core';
import { Component, type OnDestroy } from '@angular/core';
import type { ChatInputSendEventDetail } from '@ni/spright-angular/chat/input';

interface ChatEntry {
Comment thread
Alexia-Claudia-Micu marked this conversation as resolved.
type: 'user' | 'advisor' | 'system';
text: string;
streaming: boolean;
}

const singleResponse = `To configure your Python version, select Adapters from the Configure menu.
Configure the Python adapter. Choose the desired version from the Version dropdown.
You can also specify a Python version for a specific module call in the Advanced Settings of the Python adapter.
Additionally, you can set environment variables in the adapter configuration to control runtime behavior.
This gives you fine-grained control over which interpreter is used per step in your test sequence.
If you have multiple virtual environments, make sure to point the adapter to the correct executable path.
The path must be absolute and should not contain spaces unless properly quoted.
For further reference, consult the NI TestStand help documentation under the Python Adapter section.`;

const cannedResponseWords = Array(5).fill(singleResponse).join('\n').split(/\s+/);

@Component({
selector: 'example-chat-conversation-section',
template: `
<example-sub-container label="Chat Conversation and Messages (Spright)">
<spright-chat-conversation>
<nimble-toolbar slot="toolbar">
<nimble-icon-messages-sparkle slot="start"></nimble-icon-messages-sparkle>
<span class="toolbar-title">AI Assistant</span>
<nimble-button appearance="ghost" content-hidden title="Create new chat" slot="end">
Create new chat
<nimble-icon-pencil-to-rectangle slot="start"></nimble-icon-pencil-to-rectangle>
</nimble-button>
</nimble-toolbar>
<nimble-banner slot="start" open severity="information">
<span slot="title">Title of the banner</span>
<div class="conversations">
<spright-chat-conversation>
<nimble-toolbar slot="toolbar">
<nimble-icon-messages-sparkle slot="start"></nimble-icon-messages-sparkle>
<span class="toolbar-title">AI Assistant</span>
<nimble-button appearance="ghost" content-hidden title="Create new chat" slot="end">
Create new chat
<nimble-icon-pencil-to-rectangle slot="start"></nimble-icon-pencil-to-rectangle>
</nimble-button>
</nimble-toolbar>
<nimble-banner slot="start" open severity="information">
<span slot="title">Title of the banner</span>
This is the message text of this banner. It tells you something interesting.
</nimble-banner>
<spright-chat-message-system>To start, press any key.</spright-chat-message-system>
<spright-chat-message-outbound>Where is the Any key?</spright-chat-message-outbound>
<spright-chat-message-system>
<nimble-spinner appearance="accent"></nimble-spinner>
</spright-chat-message-system>
<spright-chat-message-inbound>
<nimble-button slot="footer-actions" appearance='ghost' content-hidden>
<nimble-icon-copy-text slot="start"></nimble-icon-copy-text>
Copy
</nimble-button>
<nimble-icon-webvi-custom style="height: 100px; width: 100px;"></nimble-icon-webvi-custom>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<nimble-button slot="end" appearance="block">Order a tab</nimble-button>
<nimble-button slot="end" appearance="block">Check core temperature</nimble-button>
</spright-chat-message-inbound>
@for (message of chatUserMessages; track message) {
<spright-chat-message-outbound><span>{{message}}</span></spright-chat-message-outbound>
}
<spright-chat-input slot="input" placeholder="Type here" (send)="onChatInputSend($event)"></spright-chat-input>
<span slot="end">
AI-generated content may be incorrect.
<nimble-anchor href="https://www.ni.com" target="_blank">View Terms and Conditions</nimble-anchor>
</span>
</spright-chat-conversation>
</nimble-banner>
<spright-chat-message-system>To start, press any key.</spright-chat-message-system>
<spright-chat-message-outbound>Where is the Any key?</spright-chat-message-outbound>
<spright-chat-message-system>
<nimble-spinner appearance="accent"></nimble-spinner>
</spright-chat-message-system>
<spright-chat-message-inbound>
<nimble-button slot="footer-actions" appearance='ghost' content-hidden>
<nimble-icon-copy-text slot="start"></nimble-icon-copy-text>
Copy
</nimble-button>
<nimble-icon-webvi-custom style="height: 100px; width: 100px;"></nimble-icon-webvi-custom>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<nimble-button slot="end" appearance="block">Order a tab</nimble-button>
<nimble-button slot="end" appearance="block">Check core temperature</nimble-button>
</spright-chat-message-inbound>
@for (message of staticUserMessages; track message) {
<spright-chat-message-outbound><span>{{message}}</span></spright-chat-message-outbound>
}
<spright-chat-input slot="input" placeholder="Type here" (send)="onStaticChatInputSend($event)"></spright-chat-input>
<span slot="end">
AI-generated content may be incorrect.
<nimble-anchor href="https://www.ni.com" target="_blank">View Terms and Conditions</nimble-anchor>
</span>
</spright-chat-conversation>

<div class="streaming-section">
<label class="response-label">Custom advisor response (leave empty for canned):</label>
<textarea class="response-input" rows="3" (input)="onCustomAdvisorTextInput($event)"></textarea>
<spright-chat-conversation auto-scroll>
<nimble-toolbar slot="toolbar">
<nimble-icon-messages-sparkle slot="start"></nimble-icon-messages-sparkle>
<span class="toolbar-title">AI Assistant (Streaming)</span>
</nimble-toolbar>
@for (entry of messages; track entry) {
@if (entry.type === 'user') {
<spright-chat-message-outbound>
<span>{{entry.text}}</span>
</spright-chat-message-outbound>
}
@if (entry.type === 'system') {
<spright-chat-message-system>
<nimble-spinner appearance="accent"></nimble-spinner>
</spright-chat-message-system>
}
@if (entry.type === 'advisor') {
<spright-chat-message-inbound>
<span>{{entry.text}}</span>
@if (!entry.streaming) {
<nimble-button slot="footer-actions" appearance="ghost" content-hidden title="Copy">
<nimble-icon-copy-text slot="start"></nimble-icon-copy-text>
Copy
</nimble-button>
}
</spright-chat-message-inbound>
}
}
<spright-chat-input slot="input" placeholder="Send a message…" (send)="onChatInputSend($event)" [send-disabled]="isStreaming"></spright-chat-input>
<span slot="end">AI-generated content may be incorrect.</span>
</spright-chat-conversation>
</div>
</div>
</example-sub-container>
`,
styles: [`
spright-chat-conversation { max-width: 700px; }
spright-chat-message span { white-space: pre-wrap; }
.conversations {
display: flex;
gap: 16px;
}
spright-chat-conversation {
width: 700px;
height: 650px;
}
.streaming-section {
display: flex;
flex-direction: column;
gap: 8px;
}
.response-label {
font: var(--ni-nimble-body-font);
color: var(--ni-nimble-body-font-color);
}
.response-input {
font: var(--ni-nimble-body-font);
color: var(--ni-nimble-body-font-color);
background: var(--ni-nimble-fill-secondary-color);
border: 1px solid var(--ni-nimble-border-color);
padding: 4px;
width: 700px;
resize: vertical;
}
spright-chat-message-outbound span,
spright-chat-message-inbound span { white-space: pre-wrap; }
`],
standalone: false
})
export class ChatConversationSectionComponent {
public chatUserMessages: string[] = [];
export class ChatConversationSectionComponent implements OnDestroy {
public staticUserMessages: string[] = [];
public messages: ChatEntry[] = [];
public isStreaming = false;

private customAdvisorText = '';
private streamInterval: ReturnType<typeof setInterval> | null = null;

public onCustomAdvisorTextInput(e: Event): void {
this.customAdvisorText = (e.target as HTMLTextAreaElement).value;
}

public onStaticChatInputSend(e: Event): void {
this.staticUserMessages.push((e as CustomEvent<ChatInputSendEventDetail>).detail.text);
}

public onChatInputSend(e: Event): void {
const chatInputSendEvent = (e as CustomEvent<ChatInputSendEventDetail>);
this.chatUserMessages.push(chatInputSendEvent.detail.text);
const text = (e as CustomEvent<ChatInputSendEventDetail>).detail.text;
this.messages.push({ type: 'user', text, streaming: false });
this.startStreaming();
}

public ngOnDestroy(): void {
if (this.streamInterval !== null) {
clearInterval(this.streamInterval);
}
}

private startStreaming(): void {
this.isStreaming = true;
const spinnerEntry: ChatEntry = { type: 'system', text: '', streaming: true };
this.messages.push(spinnerEntry);

const responseWords = this.customAdvisorText.trim().length > 0
? this.customAdvisorText.trim().split(/\s+/)
: cannedResponseWords;
let wordIndex = 0;
setTimeout(() => {
const idx = this.messages.indexOf(spinnerEntry);
if (idx !== -1) {
this.messages.splice(idx, 1);
}
const advisorEntry: ChatEntry = { type: 'advisor', text: '', streaming: true };
this.messages.push(advisorEntry);

this.streamInterval = setInterval(() => {
if (wordIndex < responseWords.length) {
advisorEntry.text += (wordIndex === 0 ? '' : ' ') + responseWords[wordIndex];
wordIndex += 1;
} else {
clearInterval(this.streamInterval!);
this.streamInterval = null;
advisorEntry.streaming = false;
this.isStreaming = false;
}
}, 30);
}, 300);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Directive } from '@angular/core';
import { Directive, ElementRef, Input, Renderer2 } from '@angular/core';
import { toBooleanProperty, type BooleanValueOrAttribute } from '@ni/nimble-angular/internal-utilities';
import { type ChatConversation, chatConversationTag } from '@ni/spright-components/dist/esm/chat/conversation';

export type { ChatConversation };
Expand All @@ -11,4 +12,14 @@ export { chatConversationTag };
selector: 'spright-chat-conversation',
standalone: false
})
export class SprightChatConversationDirective { }
export class SprightChatConversationDirective {
public get autoScroll(): boolean {
return this.elementRef.nativeElement.autoScroll;
}

@Input('auto-scroll') public set autoScroll(value: BooleanValueOrAttribute) {
this.renderer.setProperty(this.elementRef.nativeElement, 'autoScroll', toBooleanProperty(value));
}

public constructor(private readonly renderer: Renderer2, private readonly elementRef: ElementRef<ChatConversation>) {}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { TestBed } from '@angular/core/testing';
import { Component, ElementRef, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { SprightChatConversationModule } from '../spright-chat-conversation.module';
import { SprightChatConversationDirective, type ChatConversation } from '../spright-chat-conversation.directive';

describe('Spright chat conversation', () => {
describe('module', () => {
Expand All @@ -13,4 +15,77 @@ describe('Spright chat conversation', () => {
expect(customElements.get('spright-chat-conversation')).not.toBeUndefined();
});
});

describe('with no values in template', () => {
@Component({
template: `
<spright-chat-conversation #chatConversation></spright-chat-conversation>
`,
standalone: false
})
class TestHostComponent {
@ViewChild('chatConversation', { read: SprightChatConversationDirective }) public directive: SprightChatConversationDirective;
@ViewChild('chatConversation', { read: ElementRef }) public elementRef: ElementRef<ChatConversation>;
}

let fixture: ComponentFixture<TestHostComponent>;
let directive: SprightChatConversationDirective;
let nativeElement: ChatConversation;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestHostComponent],
imports: [SprightChatConversationModule]
});
fixture = TestBed.createComponent(TestHostComponent);
fixture.detectChanges();
directive = fixture.componentInstance.directive;
nativeElement = fixture.componentInstance.elementRef.nativeElement;
});

it('has expected defaults for autoScroll', () => {
expect(directive.autoScroll).toBeFalse();
expect(nativeElement.autoScroll).toBeFalse();
});

it('can use the directive to set autoScroll', () => {
directive.autoScroll = true;
expect(nativeElement.autoScroll).toBeTrue();
});
});

describe('with template string values', () => {
@Component({
template: `
<spright-chat-conversation #chatConversation
auto-scroll
>
</spright-chat-conversation>`,
standalone: false
})
class TestHostComponent {
@ViewChild('chatConversation', { read: SprightChatConversationDirective }) public directive: SprightChatConversationDirective;
@ViewChild('chatConversation', { read: ElementRef }) public elementRef: ElementRef<ChatConversation>;
}

let fixture: ComponentFixture<TestHostComponent>;
let directive: SprightChatConversationDirective;
let nativeElement: ChatConversation;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestHostComponent],
imports: [SprightChatConversationModule]
});
fixture = TestBed.createComponent(TestHostComponent);
fixture.detectChanges();
directive = fixture.componentInstance.directive;
nativeElement = fixture.componentInstance.elementRef.nativeElement;
});

it('will use template string values for autoScroll', () => {
expect(directive.autoScroll).toBeTrue();
expect(nativeElement.autoScroll).toBeTrue();
});
});
});
Loading