Skip to content

Commit

Permalink
feat: make doc preview as composable
Browse files Browse the repository at this point in the history
  • Loading branch information
Shibani Basava authored and Shibani Basava committed Feb 12, 2024
1 parent 761b56a commit 89ce27c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
13 changes: 8 additions & 5 deletions packages/chat-component/src/components/chat-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import { ChatController } from './chat-controller.js';
import { ChatHistoryController } from './chat-history-controller.js';
import {
lazyMultiInject,
type ChatInputComponent,
ComponentType,
type ChatInputComponent,
type ChatInputFooterComponent,
type CitationActionComponent,
} from './composable.js';

/**
Expand Down Expand Up @@ -74,7 +75,6 @@ export class ChatComponent extends LitElement {
@query('#question-input')
questionInput!: HTMLInputElement;

// Default prompts to display in the chat
@state()
isDisabled = false;

Expand Down Expand Up @@ -110,6 +110,9 @@ export class ChatComponent extends LitElement {
@lazyMultiInject(ComponentType.ChatInputFooterComponent)
chatInputFooterComponets: ChatInputFooterComponent[] | undefined;

@lazyMultiInject(ComponentType.CitationActionComponent)
citationActionComponents: CitationActionComponent[] | undefined;

override updated(changedProperties: Map<string | number | symbol, unknown>) {
super.updated(changedProperties);
// The following block is only necessary when you want to override the component from settings in the outside.
Expand Down Expand Up @@ -323,9 +326,9 @@ export class ChatComponent extends LitElement {
@on-citation-click="${this.handleCitationClick}"
></citation-list>
${this.selectedCitation
? html`<document-previewer
url="${this.apiUrl}/content/${this.selectedCitation.text}"
></document-previewer>`
? this.citationActionComponents?.map((component) =>
component.render(this.selectedCitation, `${this.apiUrl}/content/${this.selectedCitation.text}`),
)
: ''}
</div>
`
Expand Down
12 changes: 12 additions & 0 deletions packages/chat-component/src/components/citation-previewer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { injectable } from 'inversify';
import { container, type CitationActionComponent, ComponentType } from './composable.js';
import { html } from 'lit';

@injectable()
export class CitationPreviewer implements CitationActionComponent {
render(citation: Citation, url: string) {
return html`<document-previewer url="${url}"></document-previewer>`;
}
}

container.bind<CitationActionComponent>(ComponentType.CitationActionComponent).to(CitationPreviewer);
11 changes: 10 additions & 1 deletion packages/chat-component/src/components/composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const ComponentType = {
ChatInputComponent: Symbol.for('ChatInputComponent'),
ChatInputFooterComponent: Symbol.for('ChatInputFooterComponent'),
ChatEntryActionButtonComponent: Symbol.for('ChatEntryActionButtonComponent'),
CitationActionComponent: Symbol.for('CitationActionComponent'),
};

export interface ComposableComponent {
Expand All @@ -33,10 +34,16 @@ export interface ChatEntryActionButtonComponent extends ComposableComponent {
render: (entry: ChatThreadEntry, isDisabled: boolean, handleClick: (event: Event) => void) => TemplateResult;
}

export interface CitationActionComponent {
render: (citation: Citation, url: string) => TemplateResult;
}

// Add a default component since inversify currently doesn't seem to support optional bindings
// and bindings fail if no component is provided
@injectable()
export class DefaultEmptyComponent {
export class DefaultEmptyComponent implements ComposableComponent {
attach() {}

render() {
return html``;
}
Expand All @@ -52,3 +59,5 @@ export class DefaultFooterComponent extends DefaultEmptyComponent implements Cha

container.bind<ChatInputComponent>(ComponentType.ChatInputComponent).to(DefaultInputComponent);
container.bind<ChatInputFooterComponent>(ComponentType.ChatInputFooterComponent).to(DefaultFooterComponent);
container.bind<ChatEntryActionButtonComponent>(ComponentType.ChatEntryActionButtonComponent).to(DefaultEmptyComponent);
container.bind<CitationActionComponent>(ComponentType.CitationActionComponent).to(DefaultEmptyComponent);
4 changes: 3 additions & 1 deletion packages/chat-component/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import './link-icon.js';
import './chat-stage.js';
import './loading-indicator.js';
import './document-previewer.js';
import './tab-component.js';
import './citation-list.js';
import './chat-thread-component.js';
Expand All @@ -16,6 +15,9 @@ import './voice-input-button.js';

import './default-questions.js';
import './teaser-list-component.js';

import './document-previewer.js';
import './citation-previewer.js';
// [COMPOSE COMPONENTS END]

export * from './chat-component.js';

0 comments on commit 89ce27c

Please sign in to comment.