-
Notifications
You must be signed in to change notification settings - Fork 21
internal(assistant): Add assistant backend #1154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1081779
internel(assistant): Add assistant backend
e-fisher 2eda406
fix: show verification code, fix auth hanging on cancel
e-fisher e38ad47
fix: Send StreamChatEnd after stream chat errors
cursoragent 5288d59
fix: test errors
e-fisher e19a654
refactor: wrap a2a session logic in a class
e-fisher 8de7e2a
fix: Abort active A2A SSE sessions on assistant and Grafana sign out
cursoragent 8b4ae5d
fix: refresh failure discards still-valid access token
e-fisher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import log from 'electron-log/main' | ||
|
|
||
| import type { A2ASessionConfig } from './config' | ||
| import { LOG_PREFIX } from './constants' | ||
| import { buildA2AHeaders, safeResponseText } from './helpers' | ||
|
|
||
| export async function sendTaskCancel( | ||
| config: A2ASessionConfig, | ||
| taskId: string | ||
| ): Promise<void> { | ||
| const body = { | ||
| jsonrpc: '2.0', | ||
| id: crypto.randomUUID(), | ||
| method: 'tasks/cancel', | ||
| params: { id: taskId }, | ||
| } | ||
|
|
||
| const response = await fetch(`${config.baseUrl}/agents/${config.agentId}`, { | ||
| method: 'POST', | ||
| headers: buildA2AHeaders(config), | ||
| body: JSON.stringify(body), | ||
| }) | ||
|
|
||
| if (!response.ok) { | ||
| const text = await safeResponseText(response) | ||
| log.error( | ||
| LOG_PREFIX, | ||
| `Failed to cancel task ${taskId} (${response.status}):`, | ||
| text | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { getProfileData } from '@/handlers/auth/fs' | ||
|
|
||
| import { getValidAssistantTokens } from './tokenRefresh' | ||
|
|
||
| export interface A2AConfig { | ||
| baseUrl: string | ||
| agentId: string | ||
| extensions: string | ||
| bearerToken: string | ||
| } | ||
|
|
||
| export type A2ASessionConfig = Omit<A2AConfig, 'extensions'> | ||
|
|
||
| const AGENT_ID = 'grafana_assistant_k6_studio' | ||
| const REMOTE_TOOL_EXTENSION = | ||
| 'https://grafana.com/extensions/remote-tool-execution/v1' | ||
| const TOKEN_STREAMING_EXTENSION = | ||
| 'https://grafana.com/extensions/token-streaming/v1' | ||
|
|
||
| export async function getA2AConfig(): Promise<A2AConfig> { | ||
| const profile = await getProfileData() | ||
| const stackId = profile.profiles.currentStack | ||
|
|
||
| if (!stackId) { | ||
| throw new Error( | ||
| 'No Grafana Cloud stack selected. Please sign in to Grafana Cloud first.' | ||
| ) | ||
| } | ||
|
|
||
| const tokens = await getValidAssistantTokens(stackId) | ||
|
|
||
| if (!tokens) { | ||
| throw new Error( | ||
| 'Not authenticated with Grafana Assistant. Please connect to Grafana Assistant first.' | ||
| ) | ||
| } | ||
|
|
||
| return { | ||
| baseUrl: `${tokens.apiEndpoint}/api/cli/v1/a2a`, | ||
| agentId: AGENT_ID, | ||
| extensions: [REMOTE_TOOL_EXTENSION, TOKEN_STREAMING_EXTENSION].join(', '), | ||
| bearerToken: tokens.accessToken, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| export const LOG_PREFIX = '[GrafanaAssistant]' | ||
|
|
||
| export const NO_USAGE = { | ||
| inputTokens: undefined, | ||
| outputTokens: undefined, | ||
| totalTokens: undefined, | ||
| } as const | ||
|
|
||
| /** A2A artifact name constants used in SSE events */ | ||
| export const ARTIFACT_NAME = { | ||
| STEP_TOOL_CALL: 'step.toolCall', | ||
| STEP_COMPLETE: 'step.complete', | ||
| STEP_MESSAGE: 'step.message', | ||
| STEP_TOOL_RESULT: 'step.toolResult', | ||
| MESSAGE_STREAM_START: 'message.stream.start', | ||
| MESSAGE_CONTENT_DELTA: 'message.content.delta', | ||
| MESSAGE_STREAM_COMPLETE: 'message.stream.complete', | ||
| } as const |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.