sidebar: ai: add suggestions for save/load

This commit is contained in:
end-4
2025-07-07 17:19:50 +02:00
parent ae69a4f11a
commit 04e0266a4e
2 changed files with 64 additions and 6 deletions
@@ -382,11 +382,11 @@ Inline w/ backslash and round brackets \\(e^{i\\pi} + 1 = 0\\)
background: null background: null
onTextChanged: { // Handle suggestions onTextChanged: { // Handle suggestions
if(messageInputField.text.length === 0) { if (messageInputField.text.length === 0) {
root.suggestionQuery = "" root.suggestionQuery = ""
root.suggestionList = [] root.suggestionList = []
return return
} else if(messageInputField.text.startsWith(`${root.commandPrefix}model`)) { } else if (messageInputField.text.startsWith(`${root.commandPrefix}model`)) {
root.suggestionQuery = messageInputField.text.split(" ")[1] ?? "" root.suggestionQuery = messageInputField.text.split(" ")[1] ?? ""
const modelResults = Fuzzy.go(root.suggestionQuery, Ai.modelList.map(model => { const modelResults = Fuzzy.go(root.suggestionQuery, Ai.modelList.map(model => {
return { return {
@@ -404,7 +404,7 @@ Inline w/ backslash and round brackets \\(e^{i\\pi} + 1 = 0\\)
description: `${Ai.models[model.target].description}`, description: `${Ai.models[model.target].description}`,
} }
}) })
} else if(messageInputField.text.startsWith(`${root.commandPrefix}prompt`)) { } else if (messageInputField.text.startsWith(`${root.commandPrefix}prompt`)) {
root.suggestionQuery = messageInputField.text.split(" ")[1] ?? "" root.suggestionQuery = messageInputField.text.split(" ")[1] ?? ""
const promptFileResults = Fuzzy.go(root.suggestionQuery, Ai.promptFiles.map(file => { const promptFileResults = Fuzzy.go(root.suggestionQuery, Ai.promptFiles.map(file => {
return { return {
@@ -422,6 +422,44 @@ Inline w/ backslash and round brackets \\(e^{i\\pi} + 1 = 0\\)
description: `Load prompt from ${file.target}`, description: `Load prompt from ${file.target}`,
} }
}) })
} else if (messageInputField.text.startsWith(`${root.commandPrefix}save`)) {
root.suggestionQuery = messageInputField.text.split(" ")[1] ?? ""
const promptFileResults = Fuzzy.go(root.suggestionQuery, Ai.savedChats.map(file => {
return {
name: Fuzzy.prepare(file),
obj: file,
}
}), {
all: true,
key: "name"
})
root.suggestionList = promptFileResults.map(file => {
const chatName = FileUtils.trimFileExt(FileUtils.fileNameForPath(file.target)).trim()
return {
name: `${messageInputField.text.trim().split(" ").length == 1 ? (root.commandPrefix + "save ") : ""}${chatName}`,
displayName: `${chatName}`,
description: `Save chat from ${chatName}`,
}
})
} else if (messageInputField.text.startsWith(`${root.commandPrefix}load`)) {
root.suggestionQuery = messageInputField.text.split(" ")[1] ?? ""
const promptFileResults = Fuzzy.go(root.suggestionQuery, Ai.savedChats.map(file => {
return {
name: Fuzzy.prepare(file),
obj: file,
}
}), {
all: true,
key: "name"
})
root.suggestionList = promptFileResults.map(file => {
const chatName = FileUtils.trimFileExt(FileUtils.fileNameForPath(file.target)).trim()
return {
name: `${messageInputField.text.trim().split(" ").length == 1 ? (root.commandPrefix + "load ") : ""}${chatName}`,
displayName: `${chatName}`,
description: `Load chat from ${file.target}`,
}
})
} else if(messageInputField.text.startsWith(root.commandPrefix)) { } else if(messageInputField.text.startsWith(root.commandPrefix)) {
root.suggestionQuery = messageInputField.text root.suggestionQuery = messageInputField.text
root.suggestionList = root.allCommands.filter(cmd => cmd.name.startsWith(messageInputField.text.substring(1))).map(cmd => { root.suggestionList = root.allCommands.filter(cmd => cmd.name.startsWith(messageInputField.text.substring(1))).map(cmd => {
+23 -3
View File
@@ -39,6 +39,7 @@ Singleton {
property list<var> defaultPrompts: [] property list<var> defaultPrompts: []
property list<var> userPrompts: [] property list<var> userPrompts: []
property list<var> promptFiles: [...defaultPrompts, ...userPrompts] property list<var> promptFiles: [...defaultPrompts, ...userPrompts]
property list<var> savedChats: []
// Model properties: // Model properties:
// - name: Name of the model // - name: Name of the model
@@ -292,6 +293,20 @@ Singleton {
} }
} }
Process {
id: getSavedChats
running: true
command: ["ls", "-1", Directories.aiChats]
stdout: StdioCollector {
onStreamFinished: {
if (text.length === 0) return;
root.savedChats = text.split("\n")
.filter(fileName => fileName.endsWith(".json"))
.map(fileName => `${Directories.aiChats}/${fileName}`)
}
}
}
FileView { FileView {
id: promptLoader id: promptLoader
watchChanges: false; watchChanges: false;
@@ -797,6 +812,7 @@ Singleton {
id: chatSaveFile id: chatSaveFile
property string chatName: "chat" property string chatName: "chat"
path: `${Directories.aiChats}/${chatName}.json` path: `${Directories.aiChats}/${chatName}.json`
blockLoading: true
} }
/** /**
@@ -804,9 +820,10 @@ Singleton {
* @param chatName name of the chat * @param chatName name of the chat
*/ */
function saveChat(chatName) { function saveChat(chatName) {
chatSaveFile.chatName = chatName chatSaveFile.chatName = chatName.trim()
const saveContent = JSON.stringify(root.chatToJson()) const saveContent = JSON.stringify(root.chatToJson())
chatSaveFile.setText(saveContent) chatSaveFile.setText(saveContent)
getSavedChats.running = true;
} }
/** /**
@@ -815,9 +832,10 @@ Singleton {
*/ */
function loadChat(chatName) { function loadChat(chatName) {
try { try {
chatSaveFile.chatName = chatName chatSaveFile.chatName = chatName.trim()
chatSaveFile.reload()
const saveContent = chatSaveFile.text() const saveContent = chatSaveFile.text()
console.log(saveContent) // console.log(saveContent)
const saveData = JSON.parse(saveContent) const saveData = JSON.parse(saveContent)
root.clearMessages() root.clearMessages()
root.messageIDs = saveData.map((_, i) => { root.messageIDs = saveData.map((_, i) => {
@@ -843,6 +861,8 @@ Singleton {
} }
} catch (e) { } catch (e) {
console.log("[AI] Could not load chat: ", e); console.log("[AI] Could not load chat: ", e);
} finally {
getSavedChats.running = true;
} }
} }
} }