-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathinput-store.js
More file actions
257 lines (225 loc) · 7.37 KB
/
input-store.js
File metadata and controls
257 lines (225 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { createStore } from "/js/AlpineStore.js";
import * as shortcuts from "/js/shortcuts.js";
import { store as fileBrowserStore } from "/components/modals/file-browser/file-browser-store.js";
import { store as messageQueueStore } from "/components/chat/message-queue/message-queue-store.js";
import { store as attachmentsStore } from "/components/chat/attachments/attachmentsStore.js";
import { store as chatsStore } from "/components/sidebar/chats/chats-store.js";
const model = {
paused: false,
message: "",
/** Composer + menu (bottom actions moved into dropdown) */
chatMoreMenuOpen: false,
progressText: "",
progressActive: false,
toggleChatMoreMenu() {
this.chatMoreMenuOpen = !this.chatMoreMenuOpen;
},
closeChatMoreMenu() {
this.chatMoreMenuOpen = false;
},
_getSendState() {
const hasInput = this.message.trim() || attachmentsStore?.attachments?.length > 0;
const hasQueue = !!messageQueueStore?.hasQueue;
const running = !!chatsStore.selectedContext?.running;
if (hasQueue && !hasInput) return "all";
if ((running || hasQueue) && hasInput) return "queue";
return "normal";
},
get inputPlaceholder() {
const state = this._getSendState();
if (state === "all") return "Press Enter to send queued messages";
// Show progress as ghost text when agent is working and input is empty
if (this.progressText && !this.message) {
return "|> " + this.progressText;
}
return "Type your message here...";
},
// Computed: send button icon type
get sendButtonIcon() {
const state = this._getSendState();
if (state === "all") return "send_and_archive";
if (state === "queue") return "schedule_send";
return "send";
},
// Computed: send button CSS class
get sendButtonClass() {
const state = this._getSendState();
if (state === "all") return "send-queue send-all";
if (state === "queue") return "send-queue queue";
return "";
},
// Computed: send button title
get sendButtonTitle() {
const state = this._getSendState();
if (state === "all") return "Send all queued messages";
if (state === "queue") return "Add to queue";
return "Send message";
},
init() {
console.log("Input store initialized");
// Event listeners are now handled via Alpine directives in the component
},
async sendMessage() {
// Delegate to the global function
if (globalThis.sendMessage) {
await globalThis.sendMessage();
}
},
adjustTextareaHeight() {
const chatInput = document.getElementById("chat-input");
if (chatInput) {
if (!this.message) chatInput.value = "";
chatInput.style.height = "auto";
chatInput.style.height = chatInput.scrollHeight + "px";
// pick up any layout shift triggered by the height assignment
chatInput.style.height = Math.max(chatInput.scrollHeight, parseInt(chatInput.style.height)) + "px";
}
},
async pauseAgent(paused) {
const prev = this.paused;
this.paused = paused;
try {
const context = globalThis.getContext?.();
if (!globalThis.sendJsonData)
throw new Error("sendJsonData not available");
await globalThis.sendJsonData("/pause", { paused, context });
} catch (e) {
this.paused = prev;
if (globalThis.toastFetchError) {
globalThis.toastFetchError("Error pausing agent", e);
}
}
},
async nudge() {
try {
const context = globalThis.getContext();
await globalThis.sendJsonData("/nudge", { ctxid: context });
} catch (e) {
if (globalThis.toastFetchError) {
globalThis.toastFetchError("Error nudging agent", e);
}
}
},
async stopAgent() {
try {
const context = globalThis.getContext();
if (!globalThis.sendJsonData) throw new Error("sendJsonData not available");
await globalThis.sendJsonData("/stop", { context });
} catch (e) {
if (globalThis.toastFetchError) {
globalThis.toastFetchError("Error stopping agent", e);
}
}
},
async loadKnowledge() {
try {
const resp = await shortcuts.callJsonApi(
"/plugins/_memory/knowledge_path_get",
{ ctxid: shortcuts.getCurrentContextId() }
);
if (!resp.ok) throw new Error("Error getting knowledge path");
const path = resp.path;
// open file browser and wait for it to close
await fileBrowserStore.open(path);
// progress notification
shortcuts.frontendNotification({
type: shortcuts.NotificationType.PROGRESS,
message: "Loading knowledge...",
priority: shortcuts.NotificationPriority.NORMAL,
displayTime: 999,
group: "knowledge_load",
frontendOnly: true,
});
// then reindex knowledge
await globalThis.sendJsonData("/plugins/_memory/knowledge_reindex", {
ctxid: shortcuts.getCurrentContextId(),
});
// finished notification
shortcuts.frontendNotification({
type: shortcuts.NotificationType.SUCCESS,
message: "Knowledge loaded successfully",
priority: shortcuts.NotificationPriority.NORMAL,
displayTime: 2,
group: "knowledge_load",
frontendOnly: true,
});
} catch (e) {
// error notification
shortcuts.frontendNotification({
type: shortcuts.NotificationType.ERROR,
message: "Error loading knowledge",
priority: shortcuts.NotificationPriority.NORMAL,
displayTime: 5,
group: "knowledge_load",
frontendOnly: true,
});
}
},
// previous implementation without projects
async _loadKnowledge() {
const input = document.createElement("input");
input.type = "file";
input.accept = ".txt,.pdf,.csv,.html,.json,.md";
input.multiple = true;
input.onchange = async () => {
try {
const formData = new FormData();
for (let file of input.files) {
formData.append("files[]", file);
}
formData.append("ctxid", globalThis.getContext());
const response = await globalThis.fetchApi("/import_knowledge", {
method: "POST",
body: formData,
});
if (!response.ok) {
if (globalThis.toast)
globalThis.toast(await response.text(), "error");
} else {
const data = await response.json();
if (globalThis.toast) {
globalThis.toast(
"Knowledge files imported: " + data.filenames.join(", "),
"success"
);
}
}
} catch (e) {
if (globalThis.toastFetchError) {
globalThis.toastFetchError("Error loading knowledge", e);
}
}
};
input.click();
},
async browseFiles(path) {
if (!path) {
const ctxid = shortcuts.getCurrentContextId();
if (ctxid) {
try {
const resp = await shortcuts.callJsonApi("/chat_files_path_get", {
ctxid,
});
if (resp.ok) path = resp.path;
} catch (_e) {
console.error("Error getting chat files path", _e);
}
}
}
await fileBrowserStore.open(path);
},
focus() {
const chatInput = document.getElementById("chat-input");
if (chatInput) {
chatInput.focus();
}
},
reset() {
this.message = "";
attachmentsStore.clearAttachments();
this.chatMoreMenuOpen = false;
this.adjustTextareaHeight();
}
};
const store = createStore("chatInput", model);
export { store };