chat: proper fork behavior in backend, wip frontend

This commit is contained in:
Shivering Ocelot committed 2026-09-25 18:41:16 -05:00
1 parent 7bf1080d19
commit c588a12160
5 files changed
+108 -48

No files matched your search

+50 -46
View File
@@ -255,15 +255,12 @@ export class ChatSession {
let thread = this.getThread(it.threadId);
for (let entry of thread.messages) {
if (entry.id == it.id) {
entry.type = it.kind;
entry.subkind = it.subkind;
entry.format = it.format;
entry.content = it.content;
// If this changes, message has to get moved
entry.threadId = it.threadId;
for (let msg of thread.messages) {
if (msg.id == it.id) {
msg.type = it.kind;
msg.subkind = it.subkind;
msg.format = it.format;
msg.content = it.content;
this.computeChatEntries();
return;
@@ -271,15 +268,20 @@ export class ChatSession {
}
// 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;
// Remove from wherever it is, we will add it as new
let stack = Array.from(this.threads);
while (stack.length) {
let current = stack.pop();
// if we didn't find it, add it like a new one
if (oldMessage) {
ui.alert("TODO: move a message with updated thread");
return;
let index = current.messages.findIndex(msg => msg.id == it.id);
if (index >= 0) {
current.messages.splice(index, 1);
break;
}
for (let child of current.children) {
stack.push(child);
}
}
}
@@ -299,8 +301,8 @@ export class ChatSession {
}
}
// addMessageBulk I think is more correct
this.addMessage({
// addMessageBulk inserts in correct position, unlike addMessage
this.addMessageBulk([{
id: it.id,
type: it.kind,
subkind: it.subkind,
@@ -309,7 +311,7 @@ export class ChatSession {
content: it.content,
threadId: it.threadId,
time: it.time,
});
}]);
this.computeChatEntries();
this.rescroll();
@@ -594,30 +596,32 @@ export class ChatSession {
// Continuing a thread: it becomes the one open thread
this.creatingThread.value = false;
this.currentThreadId.value = threadId;
this.activeThreadIds = [threadId];
this.activeThreadIds = [];
// Load thread messages asynchronously
backend.sendAction("get-thread-messages", {
chatId: this.chatId, threadId
}).then(res => {
// TODO: reassigning all fields?
this.addMessageBulk(res.messages.map(it => ({
id: it.id,
type: it.kind,
format: it.format,
subkind: it.subkind,
sender: it.sender,
content: it.content,
threadId: it.threadId,
time: it.time,
})));
});
// Activate threads in the ancestry
// Activate and load all threads in the ancestry
let thread = this.getThread(threadId);
while (thread.parentId) {
thread = this.getThread(thread.parentId);
while (true) {
this.activeThreadIds.push(thread.id);
// Load messages asynchronously
backend.sendAction("get-thread-messages", {
chatId: this.chatId, threadId: thread.id
}).then(res => {
// TODO: reassigning all fields?
this.addMessageBulk(res.messages.map(it => ({
id: it.id,
type: it.kind,
format: it.format,
subkind: it.subkind,
sender: it.sender,
content: it.content,
threadId: it.threadId,
time: it.time,
})));
});
if (!thread.parentId) break;
thread = this.getThread(thread.parentId);
}
this.computeChatEntries();
@@ -634,13 +638,13 @@ export class ChatSession {
this.rescroll();
}
createFork (threadId, messageId) {
backend.sendAction("fork-chat-thread", {
async createFork (threadId, messageId) {
let res = await backend.sendAction("fork-chat-thread", {
chatId: this.chatId, threadId, messageId
}).then(res => {
// Wait for received message updates???
ui.alert(`Forked thread ${threadId} into ${res.threadId}`);
});
let thread = this.getThread(res.threadId);
thread.update({parentId: threadId});
return thread;
}
+3 -1
View File
@@ -142,7 +142,9 @@
break;
}
case 'fork': {
session.createFork(message.threadId, message.id);
let newThread = await session.createFork(message.threadId, message.id);
ui.alert(`Forked thread ${newThread.id} from ${message.threadId}`);
session.activateThread(newThread.id);
break;
}
case 'toggle-log': {