CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

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