diff --git a/package.json b/package.json index 5b9bbad..08eca64 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ }, "dependencies": { "@types/uuid": "^8.3.3", + "semver": "^7.5.4", "uuid": "^8.3.2" } } diff --git a/src/llms/constants.ts b/src/llms/constants.ts index f88f0be..024b729 100644 --- a/src/llms/constants.ts +++ b/src/llms/constants.ts @@ -1,2 +1,19 @@ +import { logWarning } from "@grafana/runtime"; + +import { SemVer } from "semver"; + export const LLM_PLUGIN_ID = 'grafana-llm-app'; export const LLM_PLUGIN_ROUTE = `/api/plugins/${LLM_PLUGIN_ID}`; + +// The LLM app was at version 0.2.0 before we added the health check. +// If the health check fails, or the details don't exist on the response, +// we should assume it's this older version. +export let LLM_PLUGIN_VERSION = new SemVer('0.2.0'); + +export function setLLMPluginVersion(version: string) { + try { + LLM_PLUGIN_VERSION = new SemVer(version); + } catch (e) { + logWarning('Failed to parse version of grafana-llm-app; assuming old version is present.') + } +} diff --git a/src/llms/openai.ts b/src/llms/openai.ts index 9d661c2..8245f54 100644 --- a/src/llms/openai.ts +++ b/src/llms/openai.ts @@ -14,7 +14,7 @@ import { getBackendSrv, getGrafanaLiveSrv, logDebug } from "@grafana/runtime"; import { pipe, Observable, UnaryFunction } from "rxjs"; import { filter, map, scan, takeWhile } from "rxjs/operators"; -import { LLM_PLUGIN_ID, LLM_PLUGIN_ROUTE } from "./constants"; +import { LLM_PLUGIN_ID, LLM_PLUGIN_ROUTE, setLLMPluginVersion } from "./constants"; import { LLMAppHealthCheck } from "./types"; const OPENAI_CHAT_COMPLETIONS_PATH = 'openai/v1/chat/completions'; @@ -344,7 +344,12 @@ export const enabled = async () => { } return false; } - // If the plugin is installed then check if it is configured. + const { details } = response; + // Update the version if it's present on the response. + if (details.version !== undefined) { + setLLMPluginVersion(details.version); + } + // If the plugin is installed then check if it is configured. return details?.openAIEnabled ?? false; } diff --git a/src/llms/types.ts b/src/llms/types.ts index 28663fd..5d86345 100644 --- a/src/llms/types.ts +++ b/src/llms/types.ts @@ -2,5 +2,6 @@ export type LLMAppHealthCheck = { details: { openAIEnabled?: boolean; vectorEnabled?: boolean; + version?: string; }; }; diff --git a/src/llms/vector.ts b/src/llms/vector.ts index 31af5db..e3d3d3b 100644 --- a/src/llms/vector.ts +++ b/src/llms/vector.ts @@ -9,7 +9,7 @@ */ import { getBackendSrv, logDebug } from "@grafana/runtime"; -import { LLM_PLUGIN_ROUTE } from "./constants"; +import { LLM_PLUGIN_ROUTE, setLLMPluginVersion } from "./constants"; import { LLMAppHealthCheck } from "./types"; interface SearchResultPayload extends Record { } @@ -89,7 +89,11 @@ export const enabled = async () => { } return false; } - // If the plugin is installed then check if it is configured. const { details } = response; + // Update the version if it's present on the response. + if (details.version !== undefined) { + setLLMPluginVersion(details.version); + } + // If the plugin is installed then check if it is configured. return details.vectorEnabled ?? false; }; diff --git a/yarn.lock b/yarn.lock index f4e2fc3..d54cb03 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6551,7 +6551,7 @@ semver@^7.3.5, semver@^7.3.7, semver@^7.5.0: dependencies: lru-cache "^6.0.0" -semver@^7.5.1: +semver@^7.5.1, semver@^7.5.4: version "7.5.4" resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.4.tgz#483986ec4ed38e1c6c48c34894a9182dbff68a6e" integrity sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==