Skip to content

Commit 25cf09a

Browse files
nkanf-devhsluoyz
authored andcommitted
fix: recover interrupted chat status (#2314)
1 parent 4d0d58f commit 25cf09a

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

controllers/message_answer_job.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ func (m *messageAnswerJobManager) getOrStart(id string, host string, lang string
7575
m.mu.Unlock()
7676

7777
go func() {
78-
defer job.finish()
78+
defer func() {
79+
job.finish()
80+
cleanupMessageAnswerJobChatStatus(id)
81+
}()
7982
generateMessageAnswer(id, job.writer, host, lang, signedIn, nil)
8083
}()
8184

@@ -94,6 +97,19 @@ func (m *messageAnswerJobManager) cancel(id string) bool {
9497
return true
9598
}
9699

100+
func cleanupMessageAnswerJobChatStatus(id string) {
101+
message, err := object.GetMessage(id)
102+
if err != nil || message == nil || message.Author != "AI" || message.Chat == "" {
103+
return
104+
}
105+
if message.Text != "" || message.ErrorText != "" {
106+
return
107+
}
108+
if err = clearMessageChatGenerating(message); err != nil {
109+
fmt.Printf("failed to clear generating chat for message answer job %s: %s\n", id, err.Error())
110+
}
111+
}
112+
97113
func (m *messageAnswerJobManager) remove(id string, job *messageAnswerJob) {
98114
m.mu.Lock()
99115
defer m.mu.Unlock()

web/src/ChatPage.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,20 @@ class ChatPage extends BaseListPage {
108108

109109
const status = res.data;
110110
const isViewing = this.state.chat?.name === chat.name;
111+
const generationFinished = chat.isGenerating && !status.isGenerating;
111112

112-
if (chat.isGenerating && !status.isGenerating && isViewing && status.isUnread) {
113+
if (generationFinished && isViewing && this.state.messageLoading) {
114+
const lastMessage = this.state.messages?.[this.state.messages.length - 1];
115+
if (lastMessage?.author === "AI") {
116+
MessageBackend.closeMessageEventSource(lastMessage.owner, lastMessage.name, false);
117+
}
118+
this.setState({
119+
messageLoading: false,
120+
});
121+
this.getMessages({...chat, ...status}, {skipPendingAnswer: true});
122+
}
123+
124+
if (generationFinished && isViewing && status.isUnread) {
113125
this.markChatRead({...chat, ...status});
114126
return;
115127
}
@@ -417,7 +429,7 @@ class ChatPage extends BaseListPage {
417429
});
418430
}
419431

420-
getMessages(chat) {
432+
getMessages(chat, options = {}) {
421433
this.setState({
422434
messageError: false,
423435
});
@@ -435,6 +447,13 @@ class ChatPage extends BaseListPage {
435447
if (res.data.length > 0) {
436448
const lastMessage = res.data[res.data.length - 1];
437449
if (lastMessage.author === "AI" && lastMessage.replyTo !== "" && lastMessage.text === "") {
450+
if (options.skipPendingAnswer) {
451+
this.setState({
452+
messageLoading: false,
453+
messageError: lastMessage.errorText !== "",
454+
});
455+
return;
456+
}
438457
let text = "";
439458
let reasonText = "";
440459
this.setState({

0 commit comments

Comments
 (0)