ai service: rename currentModel and model in setModel for clarity

This commit is contained in:
end-4
2025-05-15 23:10:21 +02:00
parent 9fdf7bd6a4
commit b2c1ad628e
2 changed files with 23 additions and 19 deletions
@@ -533,7 +533,7 @@ int main(int argc, char* argv[]) {
font.weight: Font.DemiBold font.weight: Font.DemiBold
color: Appearance.m3colors.m3onSurface color: Appearance.m3colors.m3onSurface
elide: Text.ElideRight elide: Text.ElideRight
text: Ai.models[Ai.currentModel].name text: Ai.getModel().name
} }
} }
StyledToolTip { StyledToolTip {
@@ -541,7 +541,7 @@ int main(int argc, char* argv[]) {
extraVisibleCondition: false extraVisibleCondition: false
alternativeVisibleCondition: mouseArea.containsMouse // Show tooltip when hovered alternativeVisibleCondition: mouseArea.containsMouse // Show tooltip when hovered
content: StringUtils.format(qsTr("Current model: {0}\nSet it with {1}model MODEL"), content: StringUtils.format(qsTr("Current model: {0}\nSet it with {1}model MODEL"),
Ai.models[Ai.currentModel].name, root.commandPrefix) Ai.getModel().name, root.commandPrefix)
} }
MouseArea { MouseArea {
+21 -17
View File
@@ -77,10 +77,10 @@ Singleton {
}, },
} }
property var modelList: Object.keys(root.models) property var modelList: Object.keys(root.models)
property var currentModel: PersistentStates.ai.model property var currentModelId: PersistentStates.ai.model
Component.onCompleted: { Component.onCompleted: {
setModel(currentModel, false); // Do necessary setup for model setModel(currentModelId, false); // Do necessary setup for model
getOllamaModels.running = true getOllamaModels.running = true
} }
@@ -159,34 +159,38 @@ Singleton {
); );
} }
function setModel(model, feedback = true) { function getModel() {
if (!model) model = "" return models[currentModelId];
model = model.toLowerCase() }
if (modelList.indexOf(model) !== -1) {
PersistentStateManager.setState("ai.model", model); function setModel(modelId, feedback = true) {
if (feedback) root.addMessage("Model set to " + models[model].name, Ai.interfaceRole) if (!modelId) modelId = ""
if (models[model].requires_key) { modelId = modelId.toLowerCase()
if (modelList.indexOf(modelId) !== -1) {
PersistentStateManager.setState("ai.model", modelId);
if (feedback) root.addMessage("Model set to " + models[modelId].name, Ai.interfaceRole)
if (models[modelId].requires_key) {
// If key not there show advice // If key not there show advice
if (root.apiKeysLoaded && (!root.apiKeys[models[model].key_id] || root.apiKeys[models[model].key_id].length === 0)) { if (root.apiKeysLoaded && (!root.apiKeys[models[modelId].key_id] || root.apiKeys[models[modelId].key_id].length === 0)) {
root.addApiKeyAdvice(models[model]) root.addApiKeyAdvice(models[modelId])
} }
} }
} else { } else {
if (feedback) root.addMessage(qsTr("Invalid model. Supported: \n```\n") + modelList.join("\n```\n```\n"), Ai.interfaceRole) + "\n```" if (feedback) root.addMessage(qsTr("Invalid model. Supported: \n```\n") + modelList.join("\n```\n```\n"), Ai.interfaceRole) + "\n```"
} }
if (models[model]?.requires_key) { if (models[modelId]?.requires_key) {
KeyringStorage.fetchKeyringData(); KeyringStorage.fetchKeyringData();
} }
} }
function setApiKey(key) { function setApiKey(key) {
const model = models[currentModel]; const model = models[currentModelId];
if (!model.requires_key) { if (!model.requires_key) {
root.addMessage(`${model.name} does not require an API key`, Ai.interfaceRole); root.addMessage(`${model.name} does not require an API key`, Ai.interfaceRole);
return; return;
} }
if (!key || key.length === 0) { if (!key || key.length === 0) {
const model = models[currentModel]; const model = models[currentModelId];
root.addApiKeyAdvice(model) root.addApiKeyAdvice(model)
return; return;
} }
@@ -195,7 +199,7 @@ Singleton {
} }
function printApiKey() { function printApiKey() {
const model = models[currentModel]; const model = models[currentModelId];
if (model.requires_key) { if (model.requires_key) {
const key = root.apiKeys[model.key_id]; const key = root.apiKeys[model.key_id];
if (key) { if (key) {
@@ -263,7 +267,7 @@ Singleton {
} }
function makeRequest() { function makeRequest() {
const model = models[currentModel]; const model = models[currentModelId];
requester.apiFormat = model.api_format ?? "openai"; requester.apiFormat = model.api_format ?? "openai";
/* Put API key in environment variable */ /* Put API key in environment variable */
@@ -280,7 +284,7 @@ Singleton {
/* Create local message object */ /* Create local message object */
requester.message = root.aiMessageComponent.createObject(root, { requester.message = root.aiMessageComponent.createObject(root, {
"role": "assistant", "role": "assistant",
"model": currentModel, "model": currentModelId,
"content": "", "content": "",
"thinking": true, "thinking": true,
"done": false, "done": false,