wip,chat: forking edits
This commit is contained in:
1 parent
5ae94ca513
commit
7bf1080d19
2 files changed
+113
-13
No files matched your search
@@ -285,7 +285,7 @@ def register(server, app, auth_manager):
|
|||||||
elif action == "fork-chat-thread":
|
elif action == "fork-chat-thread":
|
||||||
thread_id = await app.fork_chat_thread(
|
thread_id = await app.fork_chat_thread(
|
||||||
thread_id=body['threadId'],
|
thread_id=body['threadId'],
|
||||||
message_id=body['messageId'],
|
message_id=body.get('messageId'),
|
||||||
)
|
)
|
||||||
await send({'message': "chat thread created", 'threadId': thread_id, 'nonce': nonce})
|
await send({'message': "chat thread created", 'threadId': thread_id, 'nonce': nonce})
|
||||||
|
|
||||||
|
|||||||
+112
-12
@@ -251,20 +251,35 @@ export class ChatSession {
|
|||||||
// Handles a chat message broadcast from the backend
|
// Handles a chat message broadcast from the backend
|
||||||
receiveMessage (it) {
|
receiveMessage (it) {
|
||||||
if (it.update) {
|
if (it.update) {
|
||||||
// Find message in list. If not found, add it
|
// TODO: if threadId changed, the old message has to be searched
|
||||||
for (let thread of this.threads) {
|
|
||||||
|
let thread = this.getThread(it.threadId);
|
||||||
|
|
||||||
for (let entry of thread.messages) {
|
for (let entry of thread.messages) {
|
||||||
if (entry.id == it.id) {
|
if (entry.id == it.id) {
|
||||||
entry.type = it.kind;
|
entry.type = it.kind;
|
||||||
entry.subkind = it.subkind;
|
entry.subkind = it.subkind;
|
||||||
entry.format = it.format;
|
entry.format = it.format;
|
||||||
entry.content = it.content;
|
entry.content = it.content;
|
||||||
|
|
||||||
|
// If this changes, message has to get moved
|
||||||
entry.threadId = it.threadId;
|
entry.threadId = it.threadId;
|
||||||
|
|
||||||
this.computeChatEntries();
|
this.computeChatEntries();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Message not found in target thread
|
||||||
|
// Find the old message
|
||||||
|
ui.alert("TODO: find updated messages in different threads");
|
||||||
|
//let stack = Array.from(this.threads);
|
||||||
|
let oldMessage = null;
|
||||||
|
|
||||||
|
// if we didn't find it, add it like a new one
|
||||||
|
if (oldMessage) {
|
||||||
|
ui.alert("TODO: move a message with updated thread");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -351,16 +366,34 @@ export class ChatSession {
|
|||||||
if (kind == null && this.noteMode.value) kind = 'note';
|
if (kind == null && this.noteMode.value) kind = 'note';
|
||||||
|
|
||||||
const messageId = this.editingMessageId.value;
|
const messageId = this.editingMessageId.value;
|
||||||
const threadId = await this.resolveTargetThread();
|
|
||||||
|
|
||||||
if (messageId == null || this.creatingThread.value) {
|
if (messageId == null) {
|
||||||
// New message in target thread (maybe fork)
|
let threadId = this.currentThreadId.value;
|
||||||
|
|
||||||
|
// New message creating threads fork the current thread
|
||||||
|
if (this.creatingThread.value) {
|
||||||
|
let res;
|
||||||
|
if (threadId == null) {
|
||||||
|
res = await backend.sendAction("create-chat-thread", {
|
||||||
|
chatId: this.chatId,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res = await backend.sendAction("fork-chat-thread", {
|
||||||
|
chatId: this.chatId,
|
||||||
|
threadId: threadId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
threadId = res.threadId;
|
||||||
|
}
|
||||||
|
|
||||||
backend.sendAction("send-chat-message", {
|
backend.sendAction("send-chat-message", {
|
||||||
chatId: this.chatId, content, threadId, kind, subkind,
|
chatId: this.chatId, content, threadId, kind, subkind,
|
||||||
}).then(response => {
|
}).then(res => {
|
||||||
if (response && response.error) {
|
if (res.error) {
|
||||||
ui.alert("Error sending chat message: " + response.error);
|
ui.alert("Error sending chat message: " + res.error);
|
||||||
|
} else {
|
||||||
|
this.activateThread(threadId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -373,8 +406,73 @@ export class ChatSession {
|
|||||||
}, {unsent: true});
|
}, {unsent: true});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Editing existing message
|
// Editing or forking a message
|
||||||
|
|
||||||
|
// Find the threads of the message and it's predecessor
|
||||||
|
let messageThreadId;
|
||||||
|
let previousThreadId;
|
||||||
|
|
||||||
|
let thread = this.getThread(this.currentThreadId.value);
|
||||||
|
while (thread) {
|
||||||
|
let index = thread.messages.findIndex(it => it.id == messageId);
|
||||||
|
if (index >= 0) {
|
||||||
|
messageThreadId = thread.id;
|
||||||
|
if (index == 0) {
|
||||||
|
previousThreadId = thread.parentId;
|
||||||
|
} else {
|
||||||
|
previousThreadId = thread.id;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
} else if (thread.parentId) {
|
||||||
|
thread = this.getThread(thread.parentId);
|
||||||
|
} else {
|
||||||
|
ui.alert("Target message not found in active threads: " + messageId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.creatingThread.value) {
|
||||||
|
|
||||||
|
// Send the target message to a fork
|
||||||
|
let res = await backend.sendAction("fork-chat-thread", {
|
||||||
|
chatId: this.chatId,
|
||||||
|
threadId: previousThreadId,
|
||||||
|
messageId,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Then create a new fork again for the new message
|
||||||
|
res = await backend.sendAction("fork-chat-thread", {
|
||||||
|
chatId: this.chatId,
|
||||||
|
threadId: previousThreadId,
|
||||||
|
});
|
||||||
|
|
||||||
|
let newThreadId = res.threadId;
|
||||||
|
this.getThread(newThreadId).update({parentId: previousThreadId});
|
||||||
|
|
||||||
|
// Send in new thread
|
||||||
|
backend.sendAction("send-chat-message", {
|
||||||
|
chatId: this.chatId,
|
||||||
|
threadId: newThreadId,
|
||||||
|
content, kind, subkind,
|
||||||
|
}).then(res => {
|
||||||
|
if (res.error) {
|
||||||
|
ui.alert("Error sending chat message: " + res.error);
|
||||||
|
} else {
|
||||||
|
this.activateThread(newThreadId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.addMessage({
|
||||||
|
type: kind || 'message',
|
||||||
|
subkind,
|
||||||
|
sender: 'user',
|
||||||
|
content,
|
||||||
|
threadId: newThreadId,
|
||||||
|
}, {unsent: true});
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// Editing existing message
|
||||||
backend.sendAction("update-chat-message", {
|
backend.sendAction("update-chat-message", {
|
||||||
messageId, content, kind,
|
messageId, content, kind,
|
||||||
}).then(response => {
|
}).then(response => {
|
||||||
@@ -384,13 +482,14 @@ export class ChatSession {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// No update event received, local manual update
|
// No update event received, local manual update
|
||||||
let thread = await this.getThread(threadId);
|
let thread = await this.getThread(messageThreadId);
|
||||||
let existing = thread.messages.find(msg => msg.id == messageId);
|
let existing = thread.messages.find(msg => msg.id == messageId);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
existing.content = content;
|
existing.content = content;
|
||||||
existing.type = kind || 'message';
|
existing.type = kind || 'message';
|
||||||
} else {
|
} else {
|
||||||
console.error("Can't edit locally?", {messageId, threadId, t_m: thread.messages});
|
console.error("Can't edit locally?", {messageId, thread});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.editingMessageId.value = null;
|
this.editingMessageId.value = null;
|
||||||
@@ -419,8 +518,9 @@ export class ChatSession {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Begins or ends message editing; the view keeps its input field in sync
|
// Begins or ends message editing; the view keeps its input field in sync
|
||||||
setEditing (messageId) {
|
setEditing (messageId, fork) {
|
||||||
this.editingMessageId.value = messageId ?? null;
|
this.editingMessageId.value = messageId ?? null;
|
||||||
|
this.creatingThread.value = Boolean(fork);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds a bulk of messages to the message list, in their correct
|
// Adds a bulk of messages to the message list, in their correct
|
||||||
|
|||||||
Reference in new issue
Block a user