@@ -2,7 +2,11 @@ import { LitElement, html } from "lit";
22import { unsafeHTML } from "lit-html/directives/unsafe-html.js" ;
33import { property } from "lit/decorators.js" ;
44
5- import { LightElement , createElement } from "../utils/_utils" ;
5+ import {
6+ LightElement ,
7+ createElement ,
8+ showShinyClientMessage ,
9+ } from "../utils/_utils" ;
610
711type ContentType = "markdown" | "html" | "text" ;
812
@@ -55,7 +59,7 @@ class ChatMessage extends LightElement {
5559 @property ( ) content_type : ContentType = "markdown" ;
5660 @property ( { type : Boolean , reflect : true } ) streaming = false ;
5761
58- render ( ) : ReturnType < LitElement [ "render" ] > {
62+ render ( ) {
5963 const noContent = this . content . trim ( ) . length === 0 ;
6064 const icon = noContent ? ICONS . dots_fade : ICONS . robot ;
6165
@@ -73,7 +77,7 @@ class ChatMessage extends LightElement {
7377class ChatUserMessage extends LightElement {
7478 @property ( ) content = "..." ;
7579
76- render ( ) : ReturnType < LitElement [ "render" ] > {
80+ render ( ) {
7781 return html `
7882 < shiny-markdown-stream
7983 content =${ this . content }
@@ -84,7 +88,7 @@ class ChatUserMessage extends LightElement {
8488}
8589
8690class ChatMessages extends LightElement {
87- render ( ) : ReturnType < LitElement [ "render" ] > {
91+ render ( ) {
8892 return html `` ;
8993 }
9094}
@@ -109,7 +113,7 @@ class ChatInput extends LightElement {
109113 return this . querySelector ( "button" ) as HTMLButtonElement ;
110114 }
111115
112- render ( ) : ReturnType < LitElement [ "render" ] > {
116+ render ( ) {
113117 const icon =
114118 '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-arrow-up-circle-fill" viewBox="0 0 16 16"><path d="M16 8A8 8 0 1 0 0 8a8 8 0 0 0 16 0m-7.5 3.5a.5.5 0 0 1-1 0V5.707L5.354 7.854a.5.5 0 1 1-.708-.708l3-3a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1-.708.708L8.5 5.707z"/></svg>' ;
115119
@@ -199,7 +203,7 @@ class ChatContainer extends LightElement {
199203 return last ? ( last as ChatMessage ) : null ;
200204 }
201205
202- render ( ) : ReturnType < LitElement [ "render" ] > {
206+ render ( ) {
203207 return html `` ;
204208 }
205209
@@ -352,9 +356,21 @@ $(function () {
352356 const evt = new CustomEvent ( message . handler , {
353357 detail : message . obj ,
354358 } ) ;
359+
355360 const el = document . getElementById ( message . id ) ;
356- // TODO: throw an error if the element is not found?
357- el ?. dispatchEvent ( evt ) ;
361+
362+ if ( ! el ) {
363+ const errMsg = `
364+ Unable to handle Chat() message since element with id ${ message . id } wasn't
365+ found. Do you need to call .ui() (Express) or need a
366+ chat_ui('${ message . id } ') in the UI (Core)?
367+ ` ;
368+ console . error ( errMsg ) ;
369+ showShinyClientMessage ( { message : errMsg , status : "error" } ) ;
370+ return ;
371+ }
372+
373+ el . dispatchEvent ( evt ) ;
358374 }
359375 ) ;
360376} ) ;
0 commit comments