| 1 | import { createStore } from "/js/AlpineStore.js"; |
| 2 | import { callJsonApi } from "/js/api.js"; |
| 3 | import { |
| 4 | toastFrontendSuccess, |
| 5 | toastFrontendError, |
| 6 | } from "/components/notifications/notification-store.js"; |
| 7 | |
| 8 | export const store = createStore("compactStore", { |
| 9 | compacting: false, |
| 10 | stats: null, |
| 11 | showModal: false, |
| 12 | presets: [], |
| 13 | selectedPresetName: "", |
| 14 | useChatModel: true, |
| 15 | |
| 16 | minTokens: 1000, |
| 17 | |
| 18 | get canCompact() { |
| 19 | return this.stats && this.stats.token_count >= this.minTokens; |
| 20 | }, |
| 21 | |
| 22 | get selectedModelDisplay() { |
| 23 | if (this.selectedPresetName) { |
| 24 | const preset = this.presets.find( |
| 25 | (p) => p.name === this.selectedPresetName |
| 26 | ); |
| 27 | if (preset) { |
| 28 | const cfg = this.useChatModel ? preset.chat : preset.utility; |
| 29 | if (cfg?.name) return cfg.name; |
| 30 | } |
| 31 | } |
| 32 | return this.useChatModel |
| 33 | ? this.stats?.chat_model_name || "Chat Model" |
| 34 | : this.stats?.utility_model_name || "Utility Model"; |
| 35 | }, |
| 36 | |
| 37 | async fetchStats() { |
| 38 | try { |
| 39 | const ctxid = globalThis.getContext?.(); |
| 40 | if (!ctxid) { |
| 41 | toastFrontendError("No active chat", "Compaction"); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | const [statsRes, presetsRes] = await Promise.all([ |
| 46 | callJsonApi("/plugins/_chat_compaction/compact_chat", { |
| 47 | context: ctxid, |
| 48 | action: "stats", |
| 49 | }), |
| 50 | callJsonApi("/plugins/_model_config/model_presets", { |
| 51 | action: "get", |
| 52 | }), |
| 53 | ]); |
| 54 | |
| 55 | if (!statsRes?.ok) { |
| 56 | throw new Error(statsRes?.message || "Failed to fetch stats"); |
| 57 | } |
| 58 | |
| 59 | this.stats = statsRes.stats; |
| 60 | this.presets = presetsRes?.ok ? presetsRes.presets || [] : []; |
| 61 | this.showModal = true; |
| 62 | } catch (e) { |
| 63 | toastFrontendError(e.message, "Compaction"); |
| 64 | } |
| 65 | }, |
| 66 | |
| 67 | async compact() { |
| 68 | this.compacting = true; |
| 69 | try { |
| 70 | const ctxid = globalThis.getContext?.(); |
| 71 | if (!ctxid) { |
| 72 | throw new Error("No active chat"); |
| 73 | } |
| 74 | |
| 75 | const res = await callJsonApi("/plugins/_chat_compaction/compact_chat", { |
| 76 | context: ctxid, |
| 77 | action: "compact", |
| 78 | use_chat_model: this.useChatModel, |
| 79 | preset_name: this.selectedPresetName || null, |
| 80 | }); |
| 81 | |
| 82 | if (!res?.ok) { |
| 83 | throw new Error(res?.message || "Compaction failed"); |
| 84 | } |
| 85 | |
| 86 | toastFrontendSuccess("Compaction started", "Compaction"); |
| 87 | this.showModal = false; |
| 88 | } catch (e) { |
| 89 | toastFrontendError(e.message, "Compaction"); |
| 90 | } finally { |
| 91 | this.compacting = false; |
| 92 | } |
| 93 | }, |
| 94 | |
| 95 | closeModal() { |
| 96 | this.showModal = false; |
| 97 | this.stats = null; |
| 98 | this.presets = []; |
| 99 | this.selectedPresetName = ""; |
| 100 | this.useChatModel = true; |
| 101 | }, |
| 102 | }); |