From c347fecf2ea717b825ae7ec58cf9dda044ee5dd5 Mon Sep 17 00:00:00 2001 From: Lukasz Papierkowski Date: Thu, 29 Aug 2024 17:18:30 +0200 Subject: [PATCH] AI Assistant: Download & install aiassistantserver for supported OSes Change-Id: I87e0ec1613c30252fc5db608af42319d844add0b Reviewed-by: Marcus Tillmanns --- .../lua-plugins/ai_asistant/init.lua | 108 +++++++----------- 1 file changed, 43 insertions(+), 65 deletions(-) diff --git a/share/qtcreator/lua-plugins/ai_asistant/init.lua b/share/qtcreator/lua-plugins/ai_asistant/init.lua index 31737bd4894..2a6200e8e2e 100644 --- a/share/qtcreator/lua-plugins/ai_asistant/init.lua +++ b/share/qtcreator/lua-plugins/ai_asistant/init.lua @@ -14,7 +14,6 @@ local Action = require("Action") Hooks = {} AutoSuggestionDelay = 2000 ServerName = "qtaiassistantserver" -ServerReleasesURL = "https://qtaiassistant.gitlab-pages.qt.io/qtails/releases/" .. ServerName .. ".json" InlineChatActive = false local function collectSuggestions(responseTable) @@ -78,89 +77,68 @@ local function createInitOptions() } end -local function filter(tbl, callback) - for i = #tbl, 1, -1 do - if not callback(tbl[i]) then - table.remove(tbl, i) - end - end -end - - local function installOrUpdateServer() - -- TODO: Support Windows and Mac OS - local data = a.wait(fetch({ - url = ServerReleasesURL, + url = "https://qtccache.qt.io/QtAIAssistant/LatestRelease", convertToTable = true })) - if data == nil or data["assets"] == nil then + if not data then print("Failed to fetch release data.") return end - local links = data["assets"]["links"] - if type(links) == "table" and #links > 0 then - local tag_name = data["tag_name"] - print("Found tag name:", tag_name) + local id = string.format("%.0f", data["id"]) + print("Found version:", id) - local lspPkgInfo = Install.packageInfo(ServerName) - if not lspPkgInfo or lspPkgInfo.version ~= tag_name then - local osTr = { mac = "darwin", windows = "win32", linux = "linux" } - local archTr = { unknown = "", x86 = "ia32", x86_64 = "x64", itanium = "", arm = "", arm64 = "arm64" } - local extTr = { mac = "gz", windows = "zip", linux = "tar.gz" } - local os = osTr[Utils.HostOsInfo.os] + local lspPkgInfo = Install.packageInfo(ServerName) + if not lspPkgInfo or lspPkgInfo.version ~= id then + local osTr = { mac = "macos", windows = "windows", linux = "ubuntu" } + local os = osTr[Utils.HostOsInfo.os] - if os ~= "linux" then - print("Currently unsupported OS:", Utils.HostOsInfo.os) - return + print("Attempt to download server for: ", Utils.HostOsInfo.os) + + local expectedFileName = "project-build-" .. os + local asset = nil + for _, a in ipairs(data["assets"]) do + if string.find(a.name, expectedFileName, 1, true) then + asset = a + break end - - local arch = archTr[Utils.HostOsInfo.architecture] - local ext = extTr[Utils.HostOsInfo.os] - local expectedFileName = ServerName .. "-" .. tag_name .. "-" .. os .. "-" .. arch .. "." .. ext - - filter(links, function(asset) - return string.find(asset.name, expectedFileName, 1, true) == 1 - end) - - if #links == 0 then - print("No assets found for this platform. Expected file base name:", expectedFileName) - return - end - - print("Using link:", links[1].url) - - local res, err = a.wait(Install.install( - "Do you want to install the qtaiassistantserver?", { - name = "qtaiassistantserver", - url = links[1].url, - version = tag_name - })) - - if not res then - mm.writeFlashing("Failed to install qtaiassistantserver: " .. err) - return - end - - lspPkgInfo = Install.packageInfo("qtaiassistantserver") - print("Installed:", lspPkgInfo.name, " version:", lspPkgInfo.version, " at:", lspPkgInfo.path) end - local base_name = "qtaiassistantserver-" .. tag_name - local binary = base_name .. "-linux-x64" - if Utils.HostOsInfo.isWindowsHost() then - binary = base_name .. "-windows-x64.exe" + if not asset then + print("No assets found for this platform. Expected file base name:", expectedFileName) + return end - Settings.binary:setValue(lspPkgInfo.path:resolvePath(binary)) - Settings:apply() + local assetId = string.format("%.0f", asset["id"]) + local downloadUrl = "https://qtccache.qt.io/QtAIAssistant/Asset?assetId=" .. assetId + print("Using download URL:", downloadUrl) - else - print("No links found in the release data.") + local res, err = a.wait(Install.install( + "Do you want to install the " .. ServerName .. "?", { + name = ServerName, + url = downloadUrl, + version = id + })) + + if not res then + mm.writeFlashing("Failed to install " .. ServerName .. ": " .. err .. ". Please make sure you have 7z installed for package extraction.") + return + end + + lspPkgInfo = Install.packageInfo(ServerName) + print("Installed:", lspPkgInfo.name, " version:", lspPkgInfo.version, " at:", lspPkgInfo.path) end + local binary = ServerName + if Utils.HostOsInfo.isWindowsHost() then + binary = binary .. ".exe" + end + + Settings.binary:setValue(lspPkgInfo.path:resolvePath(binary)) + Settings:apply() end IsTryingToInstall = false