Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/chat/history.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { List, Map } from "immutable";67import { Well } from "@cocalc/frontend/antd-bootstrap";8import { TimeAgo } from "@cocalc/frontend/components";9import StaticMarkdown from "@cocalc/frontend/editors/slate/static-markdown";10import { sanitize_html_safe } from "@cocalc/frontend/misc";11import {12isLanguageModelService,13service2model,14} from "@cocalc/util/db-schema/llm-utils";15import { isValidUUID, trunc_middle } from "@cocalc/util/misc";16import { LLMModelName } from "../components/llm-name";1718export function HistoryTitle() {19return (20<div21style={{22borderRadius: "10px 10px 0px 0px",23textAlign: "center",24padding: "0px",25}}26>27<span style={{ fontStyle: "italic", fontWeight: "bold" }}>28Message History29</span>30</div>31);32}3334export function HistoryFooter() {35return (36<div37style={{ borderRadius: "0px 0px 10px 10px", marginBottom: "3px" }}38></div>39);40}4142interface HistoryProps {43history?: List<any>;44user_map?: Map<string, any>;45}4647export function History({ history, user_map }: HistoryProps) {48if (history == null || user_map == null) {49return null;50}5152function renderAuthor(author_id: string): JSX.Element | null {53if (user_map == null) {54return null;55}56if (isValidUUID(author_id) && user_map.get(author_id) != null) {57const first_name = user_map.getIn([author_id, "first_name"]);58const last_name = user_map.getIn([author_id, "last_name"]);59return <>{trunc_middle(`${first_name} ${last_name}`, 20)}</>;60} else if (isLanguageModelService(author_id)) {61return <LLMModelName model={service2model(author_id)} size={14} />;62} else {63return <>Unknown author</>;64}65}6667// convert to javascript from immutable, and remove current version.68const historyList = history.toJS().slice(1);69const v: JSX.Element[] = [];70for (const index in historyList) {71const message = historyList[index];72const { content, author_id, date } = message;73const value = sanitize_html_safe(content);74const author = renderAuthor(author_id);75v.push(76<Well key={index} style={{ marginBottom: "0px" }}>77<div style={{ marginBottom: "-10px", wordWrap: "break-word" }}>78<StaticMarkdown value={value} />79</div>80<div className="small">81{value.trim() == "" ? "Message deleted " : "Last edit "}82<TimeAgo date={new Date(date)} />83{author ? <> by {author}</> : undefined}84</div>85</Well>,86);87}88return <div>{v}</div>;89}909192