Skip to content

Commit 8452bb9

Browse files
committed
refactor
1 parent f9a92a0 commit 8452bb9

File tree

3 files changed

+20
-55
lines changed

3 files changed

+20
-55
lines changed

src/handlers/ai/a2a/assistantAuth.ts

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { browserWindowFromEvent } from '@/utils/electron'
1515
import { abortAllActiveAssistantSessions } from '../grafanaAssistantProvider'
1616
import { AssistantAuthHandler } from '../types'
1717

18+
import { getCurrentStackUrl } from './config'
1819
import { LOG_PREFIX } from './constants'
1920
import {
2021
checkStackHealth,
@@ -222,39 +223,13 @@ export function initialize() {
222223
})
223224

224225
ipcMain.handle(AssistantAuthHandler.WakeStack, async (): Promise<void> => {
225-
const profile = await getProfileData()
226-
const stackId = profile.profiles.currentStack
227-
228-
if (!stackId) {
229-
throw new Error('No stack selected')
230-
}
231-
232-
const stack = profile.profiles.stacks[stackId]
233-
234-
if (!stack) {
235-
throw new Error(`Stack ${stackId} not found in profile`)
236-
}
237-
238-
return wakeStack(stack.url)
226+
return wakeStack(await getCurrentStackUrl())
239227
})
240228

241229
ipcMain.handle(
242230
AssistantAuthHandler.CheckStackHealth,
243231
async (): Promise<StackHealthStatus> => {
244-
const profile = await getProfileData()
245-
const stackId = profile.profiles.currentStack
246-
247-
if (!stackId) {
248-
throw new Error('No stack selected')
249-
}
250-
251-
const stack = profile.profiles.stacks[stackId]
252-
253-
if (!stack) {
254-
throw new Error(`Stack ${stackId} not found in profile`)
255-
}
256-
257-
return checkStackHealth(stack.url)
232+
return checkStackHealth(await getCurrentStackUrl())
258233
}
259234
)
260235

src/handlers/ai/a2a/config.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ const REMOTE_TOOL_EXTENSION =
1717
const TOKEN_STREAMING_EXTENSION =
1818
'https://grafana.com/extensions/token-streaming/v1'
1919

20+
export async function getCurrentStackUrl(): Promise<string> {
21+
const profile = await getProfileData()
22+
const stackId = profile.profiles.currentStack
23+
24+
if (!stackId) {
25+
throw new Error('No stack selected')
26+
}
27+
28+
const stack = profile.profiles.stacks[stackId]
29+
30+
if (!stack) {
31+
throw new Error(`Stack ${stackId} not found in profile`)
32+
}
33+
34+
return stack.url
35+
}
36+
2037
export async function getA2AConfig(): Promise<A2AConfig> {
2138
const profile = await getProfileData()
2239
const stackId = profile.profiles.currentStack

src/handlers/ai/a2a/stackHealth.test.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -100,31 +100,4 @@ describe('checkStackHealth', () => {
100100

101101
expect(result).toBe<StackHealthStatus>('loading')
102102
})
103-
104-
it('uses a timeout to avoid hanging on unresponsive stacks', async () => {
105-
mockFetch.mockResolvedValueOnce({
106-
ok: true,
107-
json: () => Promise.resolve({ database: 'ok' }),
108-
})
109-
110-
await checkStackHealth(stackUrl)
111-
112-
const healthInit = mockFetch.mock.calls[0]![1] as RequestInit
113-
expect(healthInit.signal).toBeInstanceOf(AbortSignal)
114-
})
115-
116-
it('does not call wakeStack', async () => {
117-
mockFetch.mockResolvedValueOnce({
118-
ok: true,
119-
json: () => Promise.resolve({ database: 'ok' }),
120-
})
121-
122-
await checkStackHealth(stackUrl)
123-
124-
expect(mockFetch).toHaveBeenCalledTimes(1)
125-
expect(mockFetch).toHaveBeenCalledWith(
126-
expect.stringContaining('/api/health'),
127-
expect.any(Object)
128-
)
129-
})
130103
})

0 commit comments

Comments
 (0)