@nabeh/chat-widget
v0.1.8
Published
Embeddable AI chat widget for Angular and other web applications.
Readme
@nabeh/chat-widget
Embeddable AI chat widget for Angular and other web applications.
Installation
npm install @nabeh/chat-widgetAngular Usage
Use the widget after the user session is available so you can pass user context and, if needed, an access token.
import { AfterViewInit, Component, OnDestroy } from '@angular/core';
import { createChatWidget } from '@nabeh/chat-widget';
@Component({
selector: 'app-root',
template: ''
})
export class AppComponent implements AfterViewInit, OnDestroy {
private widget?: ReturnType<typeof createChatWidget>;
ngAfterViewInit(): void {
this.widget = createChatWidget({
apiBaseUrl: 'http://your-api-url',
endpoints: {
ask: '/my-chats/:chatId/messages',
history: '/my-chats/:chatId/messages',
listChats: '/my-chats',
createChat: '/my-chats',
updateChat: '/my-chats/:chatId',
deleteChat: '/my-chats/:chatId'
},
rag: {
knowledgeNames: ['sample-kb'],
loadHistoryOnOpen: true
},
getUserContext: async () => ({
userId: 'your-user-id',
// email: '[email protected]'
})
});
}
ngOnDestroy(): void {
this.widget?.destroy();
}
}Angular Embedded Page Usage
Use displayMode: 'embedded' when the host application already owns the page shell and you only want the chat experience rendered inside a content area.
import { AfterViewInit, Component, ElementRef, OnDestroy, ViewChild } from '@angular/core';
import { createChatWidget } from '@nabeh/chat-widget';
@Component({
selector: 'app-knowledge-assistant-page',
template: `<div #chatRoot class="knowledge-assistant-root"></div>`
})
export class KnowledgeAssistantPageComponent implements AfterViewInit, OnDestroy {
@ViewChild('chatRoot', { static: true })
private chatRoot?: ElementRef<HTMLElement>;
private widget?: ReturnType<typeof createChatWidget>;
ngAfterViewInit(): void {
if (!this.chatRoot?.nativeElement) {
return;
}
this.widget = createChatWidget({
apiBaseUrl: 'http://your-api-url',
displayMode: 'embedded',
mount: this.chatRoot.nativeElement,
embedded: {
showHeader: false
},
endpoints: {
ask: '/my-chats/:chatId/messages',
history: '/my-chats/:chatId/messages',
listChats: '/my-chats',
createChat: '/my-chats',
updateChat: '/my-chats/:chatId',
deleteChat: '/my-chats/:chatId'
},
rag: {
knowledgeNames: ['sample-kb'],
loadHistoryOnOpen: true
},
getUserContext: async () => ({
userId: 'your-user-id'
})
});
}
ngOnDestroy(): void {
this.widget?.destroy();
}
}Configuration
apiBaseUrl: Backend base URL. Example:https://api.example.comdisplayMode:widgetfor the floating launcher,embeddedfor a full chat page rendered insidemountendpoints.ask: Send-message endpointendpoints.history: Fetch chat history endpointendpoints.listChats: List chats endpointendpoints.createChat: Create chat endpointendpoints.updateChat: Update chat metadata endpointendpoints.deleteChat: Delete chat endpointrag.knowledgeNames: Knowledge bases to queryrag.loadHistoryOnOpen: Load history automatically when the widget opensgetUserContext: Function that returns the current user contextgetAccessToken: Optional function that returns a bearer tokenembedded.showHeader: In embedded mode, show or hide the library-owned page header. Default:false
Example With Token
import { createChatWidget } from '@nabeh/chat-widget';
const widget = createChatWidget({
apiBaseUrl: 'http://your-api-url',
endpoints: {
ask: '/my-chats/:chatId/messages',
history: '/my-chats/:chatId/messages',
listChats: '/my-chats',
createChat: '/my-chats',
updateChat: '/my-chats/:chatId',
deleteChat: '/my-chats/:chatId'
},
rag: {
knowledgeNames: ['sample-kb'],
loadHistoryOnOpen: true
},
getAccessToken: async () => localStorage.getItem('access_token'),
getUserContext: async () => ({
userId: 'your-user-id',
email: '[email protected]'
})
});Browser Global
If you are loading the browser bundle manually, the package also exposes:
window.ChatWidget.init(config)window.ChatWidget.createChatWidget(config)
Widget Instance API
createChatWidget(...) returns an instance with:
open()close()toggle()destroy()sendMessage(message)setAccessTokenProvider(provider)getChatId()loadChats()loadHistory()
Notes
apiBaseUrlshould point to your backend, not your Angular frontend- initialize the widget after authentication if your backend requires user identity or tokens
- call
destroy()when the hosting Angular component is destroyed - for embedded mode, give the mount container a real height such as
min-height: calc(100vh - navbar - footer)
