diff --git a/public/app/plugins/datasource/prometheus/querybuilder/components/promQail/state/helpers.test.ts b/public/app/plugins/datasource/prometheus/querybuilder/components/promQail/state/helpers.test.ts index 06723eed074..7df017988b4 100644 --- a/public/app/plugins/datasource/prometheus/querybuilder/components/promQail/state/helpers.test.ts +++ b/public/app/plugins/datasource/prometheus/querybuilder/components/promQail/state/helpers.test.ts @@ -1,4 +1,47 @@ -import { guessMetricType } from './helpers'; +import { llms } from '@grafana/experimental'; + +import { guessMetricType, isLLMPluginEnabled } from './helpers'; + +// Mock the grafana-experimental llms module +jest.mock('@grafana/experimental', () => ({ + llms: { + openai: { + enabled: jest.fn(), + }, + vector: { + enabled: jest.fn(), + }, + }, +})); + +describe('isLLMPluginEnabled', () => { + it('should return true if LLM plugin is enabled', async () => { + jest.mocked(llms.openai.enabled).mockResolvedValue({ ok: true, configured: true }); + jest.mocked(llms.vector.enabled).mockResolvedValue({ ok: true, enabled: true }); + + const enabled = await isLLMPluginEnabled(); + + expect(enabled).toBe(true); + }); + + it('should return false if LLM plugin is not enabled', async () => { + jest.mocked(llms.openai.enabled).mockResolvedValue({ ok: false, configured: false }); + jest.mocked(llms.vector.enabled).mockResolvedValue({ ok: false, enabled: false }); + + const enabled = await isLLMPluginEnabled(); + + expect(enabled).toBe(false); + }); + + it('should return false if LLM plugin is enabled but health check fails', async () => { + jest.mocked(llms.openai.enabled).mockResolvedValue({ ok: false, configured: true }); + jest.mocked(llms.vector.enabled).mockResolvedValue({ ok: false, enabled: true }); + + const enabled = await isLLMPluginEnabled(); + + expect(enabled).toBe(false); + }); +}); const metricListWithType = [ // below is summary metric family diff --git a/public/app/plugins/datasource/prometheus/querybuilder/components/promQail/state/helpers.ts b/public/app/plugins/datasource/prometheus/querybuilder/components/promQail/state/helpers.ts index 86901267052..1f03c336ded 100644 --- a/public/app/plugins/datasource/prometheus/querybuilder/components/promQail/state/helpers.ts +++ b/public/app/plugins/datasource/prometheus/querybuilder/components/promQail/state/helpers.ts @@ -261,6 +261,21 @@ function guessMetricFamily(metric: string): string { return metric; } +/** + * Check if the LLM plugin is enabled. + * @returns true if the LLM plugin is enabled. + */ +export async function isLLMPluginEnabled(): Promise { + // Check if the LLM plugin is enabled. + // If not, we won't be able to make requests, so return early. + const openaiEnabled = llms.openai.enabled().then((response) => response.ok); + const vectorEnabled = llms.vector.enabled().then((response) => response.ok); + // combine 2 promises + return Promise.all([openaiEnabled, vectorEnabled]).then((results) => { + return results.every((result) => result); + }); +} + /** * Calls the API and adds suggestions to the interaction * @@ -279,7 +294,7 @@ export async function promQailSuggest( ) { // when you're not running promqail // @ts-ignore llms types issue - const check = (await llms.openai.enabled()) && (await llms.vector.enabled()); + const check = await isLLMPluginEnabled(); const interactionToUpdate = interaction ? interaction : createInteraction(SuggestionType.Historical);