Skip to content

Commit 9357f42

Browse files
committed
Merge remote-tracking branch 'origin/master' into sagews-soft-deprecation
2 parents c8d0047 + 4ad3190 commit 9357f42

File tree

26 files changed

+723
-583
lines changed

26 files changed

+723
-583
lines changed

src/packages/frontend/chat/README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
# Chat
22

33
WARNING: like all development docs, don't trust anything technical in
4-
this file; instead, only trust the code itself! Nobody ever looks at
4+
this file; instead, only trust the code itself! Nobody ever looks at
55
docs like this, except people very new to the codebase, hence they tend
66
to just maximize confusion.
77

8+
## Timestamps
9+
10+
Note: There are a couple of ways to represent a time in Javascript:
11+
12+
- iso string
13+
- ms since epoch as a number
14+
- string version of ms since epoch
15+
- Date object
16+
17+
The data structures for chat have somehow evolved since that
18+
crazy Sage Days by the Ocean in WA to use all of these at once, which is
19+
confusing and annoying. Be careful!
20+
821
## Overview
922

1023
CoCalc has two chat views.
@@ -59,4 +72,3 @@ date : Date Object
5972
history : immutable.List of immutable.Maps
6073
editing : immutable.Map
6174
```
62-

src/packages/frontend/chat/actions.ts

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {
3939
toOllamaModel,
4040
type LanguageModel,
4141
} from "@cocalc/util/db-schema/llm-utils";
42-
import { cmp, isValidUUID, parse_hashtags, uuid } from "@cocalc/util/misc";
42+
import { cmp, isValidUUID, uuid } from "@cocalc/util/misc";
4343
import { reuseInFlight } from "@cocalc/util/reuse-in-flight";
4444
import { getSortedDates, getUserName } from "./chat-log";
4545
import { message_to_markdown } from "./message";
@@ -51,7 +51,6 @@ import {
5151
Feedback,
5252
MessageHistory,
5353
} from "./types";
54-
import { getSelectedHashtagsSearch } from "./utils";
5554
import { history_path } from "@cocalc/util/misc";
5655

5756
const MAX_CHATSTREAM = 10;
@@ -193,7 +192,7 @@ export class ChatActions extends Actions<ChatState> {
193192
});
194193
}
195194

196-
public foldThread(reply_to: Date, msgIndex: number) {
195+
public foldThread(reply_to: Date, messageIndex?: number) {
197196
if (this.syncdb == null) return;
198197
const account_id = this.redux.getStore("account").get_account_id();
199198
const cur = this.syncdb.get_one({ event: "chat", date: reply_to });
@@ -210,8 +209,8 @@ export class ChatActions extends Actions<ChatState> {
210209

211210
this.syncdb.commit();
212211

213-
if (folded && msgIndex != null) {
214-
this.scrollToBottom(msgIndex);
212+
if (folded && messageIndex != null) {
213+
this.scrollToBottom(messageIndex);
215214
}
216215
}
217216

@@ -294,40 +293,44 @@ export class ChatActions extends Actions<ChatState> {
294293
// For replies search find full threads not individual messages.
295294
this.setState({
296295
input: "",
297-
search: "",
298296
});
297+
this.clearAllFilters();
299298
} else {
300-
// TODO: but until we improve search to be by thread (instead of by message), do this:
301-
this.setState({
302-
search: "",
303-
});
304-
}
305-
this.ensureDraftStartsWithHashtags(false);
306-
307-
if (this.store != null) {
308-
const project_id = this.store?.get("project_id");
309-
const path = this.store?.get("path");
310-
// set notification saying that we sent an actual chat
311-
let action;
299+
// when replying we make sure that the thread is expanded, since otherwise
300+
// our reply won't be visible
301+
const messages = this.store.get("messages");
312302
if (
313-
noNotification ||
314-
mentionsLanguageModel(input) ||
315-
this.isLanguageModelThread(reply_to)
303+
messages
304+
?.getIn([`${reply_to.valueOf()}`, "folding"])
305+
?.includes(sender_id)
316306
) {
317-
// Note: don't mark it is a chat if it is with chatgpt,
318-
// since no point in notifying all collabs of this.
319-
action = "edit";
320-
} else {
321-
action = "chat";
307+
this.foldThread(reply_to);
322308
}
323-
webapp_client.mark_file({
324-
project_id,
325-
path,
326-
action,
327-
ttl: 10000,
328-
});
329-
track("send_chat", { project_id, path });
330309
}
310+
311+
const project_id = this.store?.get("project_id");
312+
const path = this.store?.get("path");
313+
// set notification saying that we sent an actual chat
314+
let action;
315+
if (
316+
noNotification ||
317+
mentionsLanguageModel(input) ||
318+
this.isLanguageModelThread(reply_to)
319+
) {
320+
// Note: don't mark it is a chat if it is with chatgpt,
321+
// since no point in notifying all collabs of this.
322+
action = "edit";
323+
} else {
324+
action = "chat";
325+
}
326+
webapp_client.mark_file({
327+
project_id,
328+
path,
329+
action,
330+
ttl: 10000,
331+
});
332+
track("send_chat", { project_id, path });
333+
331334
this.save_to_disk();
332335
(async () => {
333336
await this.processLLM({
@@ -621,38 +624,6 @@ export class ChatActions extends Actions<ChatState> {
621624
? selectedHashtags.delete(tag)
622625
: selectedHashtags.set(tag, state);
623626
this.setState({ selectedHashtags });
624-
this.ensureDraftStartsWithHashtags(true);
625-
}
626-
627-
private ensureDraftStartsWithHashtags(commit: boolean = false): void {
628-
if (this.syncdb == null || this.store == null) return;
629-
// set draft input to match selected hashtags, if any.
630-
const hashtags = this.store.get("selectedHashtags");
631-
if (hashtags == null) return;
632-
const { selectedHashtagsSearch } = getSelectedHashtagsSearch(hashtags);
633-
let input = this.store.get("input");
634-
const prefix = selectedHashtagsSearch.trim() + " ";
635-
if (input.startsWith(prefix)) {
636-
return;
637-
}
638-
const v = parse_hashtags(input);
639-
if (v.length > 0) {
640-
input = input.slice(v[v.length - 1][1]);
641-
}
642-
643-
input = prefix + input;
644-
this.setState({ input });
645-
const sender_id = this.redux.getStore("account").get_account_id();
646-
this.syncdb.set({
647-
event: "draft",
648-
active: Date.now(),
649-
sender_id,
650-
input,
651-
date: 0,
652-
});
653-
if (commit) {
654-
this.syncdb.commit();
655-
}
656627
}
657628

658629
public help() {
@@ -1131,6 +1102,14 @@ export class ChatActions extends Actions<ChatState> {
11311102
foreground_project: true,
11321103
});
11331104
};
1105+
1106+
clearAllFilters = () => {
1107+
this.setState({
1108+
search: "",
1109+
selectedHashtags: immutableMap(),
1110+
filterRecentH: 0,
1111+
});
1112+
};
11341113
}
11351114

11361115
export function getRootMessage(

0 commit comments

Comments
 (0)