Skip to content

Commit 11e90d6

Browse files
committed
Merge remote-tracking branch 'origin/master' into next-tweak-contact-links
2 parents 7fd6b95 + 8bb2848 commit 11e90d6

File tree

23 files changed

+488
-352
lines changed

23 files changed

+488
-352
lines changed

src/packages/frontend/chat/actions.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ export class ChatActions extends Actions<ChatState> {
9090
};
9191

9292
toggleFoldThread = (reply_to: Date, messageIndex?: number) => {
93-
if (this.syncdb == null) {
94-
return;
95-
}
93+
if (this.syncdb == null) return;
9694
const account_id = this.redux.getStore("account").get_account_id();
9795
const cur = this.syncdb.get_one({ event: "chat", date: reply_to });
9896
const folding = cur?.get("folding") ?? List([]);
@@ -113,6 +111,29 @@ export class ChatActions extends Actions<ChatState> {
113111
}
114112
};
115113

114+
foldAllThreads = (onlyLLM = true) => {
115+
if (this.syncdb == null || this.store == null) return;
116+
const messages = this.store.get("messages");
117+
if (messages == null) return;
118+
const account_id = this.redux.getStore("account").get_account_id();
119+
for (const [_timestamp, message] of messages) {
120+
// ignore replies
121+
if (message.get("reply_to") != null) continue;
122+
const date = message.get("date");
123+
if (!(date instanceof Date)) continue;
124+
const isLLMThread = this.isLanguageModelThread(date) !== false;
125+
if (onlyLLM && !isLLMThread) continue;
126+
const folding = message?.get("folding") ?? List([]);
127+
const folded = folding.includes(account_id);
128+
if (!folded) {
129+
this.syncdb.set({
130+
folding: folding.push(account_id),
131+
date,
132+
});
133+
}
134+
}
135+
};
136+
116137
feedback = (message: ChatMessageTyped, feedback: Feedback | null) => {
117138
if (this.syncdb == null) return;
118139
const date = message.get("date");
@@ -479,6 +500,7 @@ export class ChatActions extends Actions<ChatState> {
479500
scrollToDate: null,
480501
});
481502
};
503+
482504
scrollToIndex = (index: number = -1) => {
483505
if (this.syncdb == null) return;
484506
// we first clear, then set it, since scroll to needs to

src/packages/frontend/chat/chat-log.tsx

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77
Render all the messages in the chat.
88
*/
99

10+
// cSpell:ignore: timespan
11+
1012
import { Alert, Button } from "antd";
1113
import { Set as immutableSet } from "immutable";
1214
import { MutableRefObject, useEffect, useMemo, useRef } from "react";
1315
import { Virtuoso, VirtuosoHandle } from "react-virtuoso";
16+
1417
import { chatBotName, isChatBot } from "@cocalc/frontend/account/chatbot";
1518
import { useRedux, useTypedRedux } from "@cocalc/frontend/app-framework";
1619
import { Icon } from "@cocalc/frontend/components";
1720
import useVirtuosoScrollHook from "@cocalc/frontend/components/virtuoso-scroll-hook";
1821
import { HashtagBar } from "@cocalc/frontend/editors/task-editor/hashtag-bar";
22+
import { DivTempHeight } from "@cocalc/frontend/jupyter/cell-list";
1923
import {
2024
cmp,
2125
hoursToTimeIntervalHuman,
@@ -24,12 +28,21 @@ import {
2428
} from "@cocalc/util/misc";
2529
import type { ChatActions } from "./actions";
2630
import Composing from "./composing";
27-
import Message from "./message";
28-
import type { ChatMessageTyped, ChatMessages, Mode } from "./types";
29-
import { getSelectedHashtagsSearch, newest_content } from "./utils";
30-
import { getRootMessage, getThreadRootDate } from "./utils";
31-
import { DivTempHeight } from "@cocalc/frontend/jupyter/cell-list";
3231
import { filterMessages } from "./filter-messages";
32+
import Message from "./message";
33+
import type {
34+
ChatMessageTyped,
35+
ChatMessages,
36+
CostEstimate,
37+
Mode,
38+
NumChildren,
39+
} from "./types";
40+
import {
41+
getRootMessage,
42+
getSelectedHashtagsSearch,
43+
getThreadRootDate,
44+
newest_content,
45+
} from "./utils";
3346

3447
interface Props {
3548
project_id: string; // used to render links more effectively
@@ -83,7 +96,7 @@ export function ChatLog({
8396
} = useMemo<{
8497
dates: string[];
8598
numFolded: number;
86-
numChildren;
99+
numChildren: NumChildren;
87100
}>(() => {
88101
const { dates, numFolded, numChildren } = getSortedDates(
89102
messages,
@@ -288,10 +301,7 @@ function isPrevMessageSender(
288301
);
289302
}
290303

291-
function isThread(
292-
message: ChatMessageTyped,
293-
numChildren: { [date: number]: number },
294-
) {
304+
function isThread(message: ChatMessageTyped, numChildren: NumChildren) {
295305
if (message.get("reply_to") != null) {
296306
return true;
297307
}
@@ -324,7 +334,7 @@ export function getSortedDates(
324334
): {
325335
dates: string[];
326336
numFolded: number;
327-
numChildren: { [date: number]: number };
337+
numChildren: NumChildren;
328338
} {
329339
let numFolded = 0;
330340
let m = messages;
@@ -342,7 +352,7 @@ export function getSortedDates(
342352

343353
// Do a linear pass through all messages to divide into threads, so that
344354
// getSortedDates is O(n) instead of O(n^2) !
345-
const numChildren: { [date: number]: number } = {};
355+
const numChildren: NumChildren = {};
346356
for (const [_, message] of m) {
347357
const parent = message.get("reply_to");
348358
if (parent != null) {
@@ -486,22 +496,21 @@ export function MessageList({
486496
selectedDate,
487497
numChildren,
488498
}: {
489-
messages;
490-
account_id;
499+
messages: ChatMessages;
500+
account_id: string;
491501
user_map;
492502
mode;
493503
sortedDates;
494504
virtuosoRef?;
495-
search?;
496-
project_id?;
497-
path?;
498-
fontSize?;
505+
project_id?: string;
506+
path?: string;
507+
fontSize?: number;
499508
selectedHashtags?;
500509
actions?;
501-
costEstimate?;
510+
costEstimate?: CostEstimate;
502511
manualScrollRef?;
503512
selectedDate?: string;
504-
numChildren?;
513+
numChildren?: NumChildren;
505514
}) {
506515
const virtuosoHeightsRef = useRef<{ [index: number]: number }>({});
507516
const virtuosoScroll = useVirtuosoScrollHook({

src/packages/frontend/chat/chatroom.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import { Button, Divider, Input, Select, Space, Tooltip } from "antd";
77
import { debounce } from "lodash";
8+
import { FormattedMessage } from "react-intl";
9+
810
import { ButtonGroup, Col, Row, Well } from "@cocalc/frontend/antd-bootstrap";
911
import {
1012
React,
@@ -18,16 +20,16 @@ import { Icon, Loading } from "@cocalc/frontend/components";
1820
import StaticMarkdown from "@cocalc/frontend/editors/slate/static-markdown";
1921
import { FrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
2022
import { hoursToTimeIntervalHuman } from "@cocalc/util/misc";
21-
import { FormattedMessage } from "react-intl";
2223
import type { ChatActions } from "./actions";
23-
import type { ChatState } from "./store";
2424
import { ChatLog } from "./chat-log";
25+
import Filter from "./filter";
26+
import { FoldAllThreads } from "./fold-threads";
2527
import ChatInput from "./input";
2628
import { LLMCostEstimationChat } from "./llm-cost-estimation";
29+
import type { ChatState } from "./store";
2730
import { SubmitMentionsFn } from "./types";
2831
import { INPUT_HEIGHT, markChatAsReadIfUnseen } from "./utils";
2932
import VideoChatButton from "./video/launch-button";
30-
import Filter from "./filter";
3133

3234
const FILTER_RECENT_NONE = {
3335
value: 0,
@@ -68,7 +70,7 @@ interface Props {
6870
project_id: string;
6971
path: string;
7072
is_visible?: boolean;
71-
font_size: number;
73+
fontSize: number;
7274
desc?;
7375
}
7476

@@ -77,7 +79,7 @@ export function ChatRoom({
7779
project_id,
7880
path,
7981
is_visible,
80-
font_size,
82+
fontSize,
8183
desc,
8284
}: Props) {
8385
const useEditor = useEditorRedux<ChatState>({ project_id, path });
@@ -261,6 +263,7 @@ export function ChatRoom({
261263
<ButtonGroup style={{ marginLeft: "5px" }}>
262264
{render_video_chat_button()}
263265
</ButtonGroup>
266+
<FoldAllThreads actions={actions} shortLabel={false} />
264267
</Space>
265268
);
266269
}
@@ -285,7 +288,7 @@ export function ChatRoom({
285288
path={path}
286289
scrollToBottomRef={scrollToBottomRef}
287290
mode={"standalone"}
288-
fontSize={font_size}
291+
fontSize={fontSize}
289292
search={search}
290293
filterRecentH={filterRecentH}
291294
selectedHashtags={selectedHashtags}
@@ -304,7 +307,7 @@ export function ChatRoom({
304307
}}
305308
>
306309
<ChatInput
307-
fontSize={font_size}
310+
fontSize={fontSize}
308311
autoFocus
309312
cacheId={`${path}${project_id}-new`}
310313
input={input}

src/packages/frontend/chat/filter-messages.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ NOTE: chat uses every imaginable way to store a timestamp at once,
66
which is the may source of weirdness in the code below... Beware.
77
*/
88

9-
import type { ChatMessages, ChatMessageTyped, MessageHistory } from "./types";
10-
import { search_match, search_split } from "@cocalc/util/misc";
119
import { List } from "immutable";
10+
import LRU from "lru-cache";
11+
1212
import type { TypedMap } from "@cocalc/frontend/app-framework";
1313
import { redux } from "@cocalc/frontend/app-framework";
1414
import { webapp_client } from "@cocalc/frontend/webapp-client";
15-
import LRU from "lru-cache";
15+
import { search_match, search_split } from "@cocalc/util/misc";
16+
import type { ChatMessages, ChatMessageTyped, MessageHistory } from "./types";
1617

1718
export function filterMessages({
1819
messages,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Button } from "antd";
2+
3+
import { ChatActions } from "@cocalc/frontend/chat/actions";
4+
import { Icon, Tip } from "@cocalc/frontend/components";
5+
6+
export function FoldAllThreads({
7+
actions,
8+
shortLabel,
9+
}: {
10+
actions: ChatActions;
11+
shortLabel: boolean;
12+
}) {
13+
return (
14+
<Tip placement="top" title="Fold all language model threads">
15+
<Button
16+
onClick={() => {
17+
actions.foldAllThreads(true);
18+
}}
19+
>
20+
<Icon name="to-top-outlined" />{" "}
21+
{shortLabel ? "LLM" : "Fold LLM threads"}
22+
</Button>
23+
</Tip>
24+
);
25+
}

src/packages/frontend/chat/llm-cost-estimation.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import {
77
ESTIMATION_HELP_TEXT,
88
MODEL_FREE_TO_USE,
99
} from "@cocalc/frontend/misc/llm-cost-estimation";
10+
import type { CostEstimate } from "./types";
1011

1112
export function LLMCostEstimationChat({
1213
costEstimate,
1314
compact,
1415
style,
1516
}: {
16-
costEstimate?: { min: number; max: number } | null;
17+
costEstimate?: CostEstimate;
1718
compact: boolean; // only mean is shown
1819
style?: CSS;
1920
}) {

src/packages/frontend/chat/llm-msg-summarize.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Button, Collapse, Switch } from "antd";
77

88
import { useLanguageModelSetting } from "@cocalc/frontend/account/useLanguageModelSetting";
99
import { useAsyncEffect, useState } from "@cocalc/frontend/app-framework";
10-
import { Icon, Paragraph, RawPrompt } from "@cocalc/frontend/components";
10+
import { Icon, Paragraph, RawPrompt, Tip } from "@cocalc/frontend/components";
1111
import AIAvatar from "@cocalc/frontend/components/ai-avatar";
1212
import PopconfirmKeyboard from "@cocalc/frontend/components/popconfirm-keyboard";
1313
import LLMSelector, {
@@ -111,9 +111,14 @@ export function SummarizeThread({
111111
onConfirm={() => actions?.summarizeThread({ model, reply_to, short })}
112112
okText="Summarize"
113113
>
114-
<Button type="text" style={{ color: COLORS.GRAY_M }}>
115-
<Icon name="vertical-align-middle" /> Summarize…
116-
</Button>
114+
<Tip
115+
placement={"bottom"}
116+
title={"Summarize this thread using a language model."}
117+
>
118+
<Button type="text" style={{ color: COLORS.GRAY_M }}>
119+
<Icon name="vertical-align-middle" /> Summarize…
120+
</Button>
121+
</Tip>
117122
</PopconfirmKeyboard>
118123
);
119124
}

0 commit comments

Comments
 (0)