Import chats store via ES module instead

keyboardstaff committed Feb 25, 2026 at 08:18 UTC 1779277187023124ecd44581211018fcf786fb54
1 file changed +14 -22
plugins/chat_branching/extensions/webui/set_messages_after_loop/inject-branch-buttons.js
+14 -22
@@ -1,11 +1,9 @@
1 // Chat Branching Plugin — injects a "branch" button into every message's action bar.
2 -// Runs as a set_messages_after_loop JS extension.
3 -//
4 -// Uses the unified handler output: context.results is an array of
5 -// { args: { no, id, type, … }, result: { element, … } } from each setMessage() call.
2 +// Uses the unified handler output: context.results[] = { args: { no, … }, result: { element, … } }
3
4 import { createActionButton } from "/components/messages/action-buttons/simple-action-buttons.js";
5 import { callJsonApi } from "/js/api.js";
6 +import { store as chatsStore } from "/components/sidebar/chats/chats-store.js";
7
8 export default async function injectBranchButtons(context) {
9 if (!context?.results?.length) return;
@@ -17,24 +15,18 @@ export default async function injectBranchButtons(context) {
15 if (!bar || bar.querySelector(".action-fork_right")) continue;
16
17 const logNo = args.no;
20 - const btn = createActionButton("fork_right", "Branch chat", async () => {
21 - const ctxid = globalThis.getContext?.();
22 - if (!ctxid) throw new Error("No active chat");
23 -
24 - const res = await callJsonApi("/plugins/chat_branching/branch_chat", {
25 - context: ctxid,
26 - log_no: logNo,
27 - });
28 -
29 - if (!res?.ok) throw new Error(res?.message || "Branch failed");
30 -
31 - // Select the newly created branch chat
32 - const chatsStore = Alpine.store("chats");
33 - if (chatsStore) {
18 + bar.appendChild(
19 + createActionButton("fork_right", "Branch chat", async () => {
20 + const ctxid = globalThis.getContext?.();
21 + if (!ctxid) throw new Error("No active chat");
22 +
23 + const res = await callJsonApi("/plugins/chat_branching/branch_chat", {
24 + context: ctxid,
25 + log_no: logNo,
26 + });
27 + if (!res?.ok) throw new Error(res?.message || "Branch failed");
28 chatsStore.selectChat(res.ctxid);
35 - }
36 - });
37 -
38 - if (btn) bar.appendChild(btn);
29 + }),
30 + );
31 }
32 }
\ No newline at end of file