|
| 1 | +import { type ReactNode, useRef, useEffect } from "react" |
| 2 | +import { debounce } from "lodash-es" |
| 3 | +import type { |
| 4 | + RealtimeConversationItem, |
| 5 | + RealtimeConversationItemContent, |
| 6 | +} from "@tsorta/browser/openai" |
| 7 | +import { |
| 8 | + DefinedRole, |
| 9 | + RealtimeConversationItemSimple, |
| 10 | + simplifyItem, |
| 11 | +} from "./simpleConversation" |
| 12 | + |
| 13 | +const log = console |
| 14 | + |
| 15 | +export interface ConversationProps { |
| 16 | + conversation: RealtimeConversationItemSimple[] |
| 17 | +} |
| 18 | + |
| 19 | +export const ConversationView = ({ |
| 20 | + conversation, |
| 21 | +}: ConversationProps): ReactNode => { |
| 22 | + return ( |
| 23 | + <div className="conversation d-flex flex-column overflow-y-scroll flex-grow-1"> |
| 24 | + {conversation |
| 25 | + .filter((item) => item.role !== "system") |
| 26 | + .map((item, index, arr) => ( |
| 27 | + <ConversationItem |
| 28 | + key={item.id} |
| 29 | + item={item} |
| 30 | + doScrollIntoView={index === arr.length - 1} |
| 31 | + /> |
| 32 | + ))} |
| 33 | + </div> |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +const RoleBgColorMap: Record<DefinedRole, string> = { |
| 38 | + user: "bg-primary-subtle", |
| 39 | + assistant: "bg-secondary-subtle", |
| 40 | + system: "bg-secondary", |
| 41 | +} |
| 42 | +const RoleTextColorMap: Record<DefinedRole, string> = { |
| 43 | + user: "text-primary", |
| 44 | + assistant: "text-secondary", |
| 45 | + system: "text-secondary", |
| 46 | +} |
| 47 | +const RoleLabelMap: Record<DefinedRole, string> = { |
| 48 | + user: "You", |
| 49 | + assistant: "Assistant", |
| 50 | + system: "System", |
| 51 | +} |
| 52 | + |
| 53 | +interface ConversationItemProps { |
| 54 | + item: RealtimeConversationItem |
| 55 | + doScrollIntoView: boolean |
| 56 | +} |
| 57 | + |
| 58 | +const ConversationItem = ({ |
| 59 | + item, |
| 60 | + doScrollIntoView, |
| 61 | +}: ConversationItemProps): ReactNode => { |
| 62 | + const simpleItem = simplifyItem(item) |
| 63 | + const { id, role, content } = simpleItem |
| 64 | + const alignClass = role === "user" ? "align-self-end" : "align-self-start" |
| 65 | + |
| 66 | + const divRef = useRef<HTMLDivElement>(null) |
| 67 | + |
| 68 | + useEffect(() => { |
| 69 | + const executeScroll = () => { |
| 70 | + // behavior: smooth works fine on FF+macOS, but not on Chrome+macOS, so we detect and force "instant" on chrome |
| 71 | + const behavior = /Chrome/.test(navigator.userAgent) ? "instant" : "smooth" |
| 72 | + divRef.current?.scrollIntoView({ |
| 73 | + behavior, |
| 74 | + block: "end", |
| 75 | + inline: "nearest", |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + if (doScrollIntoView) { |
| 80 | + debounce(executeScroll, 500)() |
| 81 | + } |
| 82 | + }, [item, doScrollIntoView, item.content, simpleItem.content.length]) |
| 83 | + |
| 84 | + return ( |
| 85 | + <div |
| 86 | + className={`conversation-item ${alignClass}`} |
| 87 | + style={{ maxWidth: "60%" }} |
| 88 | + ref={divRef} |
| 89 | + > |
| 90 | + <div className="attribution mx-8 mb-1 mt-6 text-xs text-gray-500 text-muted"> |
| 91 | + <small>{RoleLabelMap[role]}</small> |
| 92 | + </div> |
| 93 | + |
| 94 | + <div |
| 95 | + key={id} |
| 96 | + className={`mb-5 p-3 rounded-pill ${RoleBgColorMap[role]} ${RoleTextColorMap[role]}`} |
| 97 | + > |
| 98 | + <div className="my-content"> |
| 99 | + {content.map((contentItem, index) => ( |
| 100 | + <ConversationItemContent |
| 101 | + key={index} |
| 102 | + content={contentItem} |
| 103 | + doScrollIntoView={doScrollIntoView} |
| 104 | + /> |
| 105 | + ))} |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + ) |
| 110 | +} |
| 111 | + |
| 112 | +interface ConversationItemContentProps { |
| 113 | + content: RealtimeConversationItemContent |
| 114 | + doScrollIntoView: boolean |
| 115 | +} |
| 116 | + |
| 117 | +const ConversationItemContent = ({ |
| 118 | + content, |
| 119 | +}: ConversationItemContentProps): ReactNode => { |
| 120 | + if (!["text", "input_text", "input_audio"].includes(content.type)) { |
| 121 | + // NOTE: we find content.type="audio" coming in here in logging though it is not in the types! |
| 122 | + if ((content.type as string) !== "audio") { |
| 123 | + log.warn( |
| 124 | + `Unexpected type for RealtimeConversationItemContent '${content.type}'. Will not be rendered: %o`, |
| 125 | + content |
| 126 | + ) |
| 127 | + } |
| 128 | + return null |
| 129 | + } |
| 130 | + |
| 131 | + return ( |
| 132 | + <div className={`item-content item-type-${content.type}`}> |
| 133 | + {(content.type == "text" || content.type == "input_text") && ( |
| 134 | + <span>{content.text}</span> |
| 135 | + )} |
| 136 | + {content.type == "input_audio" && ( |
| 137 | + <span className="input_audio transcript"> |
| 138 | + {content.transcript ? content.transcript : "..."} |
| 139 | + </span> |
| 140 | + )} |
| 141 | + </div> |
| 142 | + ) |
| 143 | +} |
0 commit comments