# @grafana/assistant

Type definitions and utilities for integrating with the Grafana Assistant.

## Installation

```bash
npm install @grafana/assistant
```

## Usage

### Checking Assistant Availability

```typescript
import { isAssistantAvailable } from '@grafana/assistant';

// Check if the assistant is available
isAssistantAvailable().subscribe((available) => {
  if (available) {
    console.log('Assistant is available');
  }
});
```

### Using the Assistant Hook (React)

```typescript
import { useAssistant } from '@grafana/assistant';

function MyComponent() {
  const { isLoading, isAvailable, openAssistant, closeAssistant } = useAssistant();

  if (isLoading) {
    return <div>Loading...</div>;
  }

  if (!isAvailable || !openAssistant) {
    return <div>Assistant not available</div>;
  }

  return (
    <button onClick={() => openAssistant({
      origin: 'grafana/panel-data-analyzer',
      prompt: 'Help me analyze data' })}>
      Open Assistant
    </button>
  );
}
```

### Opening the Assistant Sidebar

```typescript
import { openAssistant, closeAssistant } from '@grafana/assistant';

// Open the assistant with an initial prompt
openAssistant({ origin: 'grafana/some-feature', prompt: 'Show me CPU usage over the last hour' });

// Open the assistant with initial prompt and autoSend set to false
openAssistant({
  origin: 'grafana-datasources/prometheus/some-feature',
  prompt: 'Help me analyze data',
  autoSend: false,
});

// Open the assistant without an initial prompt
openAssistant({
  origin: 'grafana-slo-app/some-page',
});

// Open a new conversation with one Agent Skill already attached, named by the token a user
// types after `/`. Never a handle or the server-issued selection id.
//
// This is a request, not a grant: the composer resolves the name against the catalog that user
// may already attach from, so an unknown, switched-off or unsupported name attaches nothing and
// says so, without revealing which. Naming a skill never auto-sends — whoever opens the link
// sees what it attached before anything reaches the model — and pins Assistant mode unless a
// `mode` is given. Ignored alongside `chatId`; a skill attaches to a new conversation only.
openAssistant({
  origin: 'grafana-pathfinder-app/authoring-link',
  prompt: 'Turn these steps into an interactive guide',
  agentSkillInvocationName: 'pathfinder-authoring',
});

// Close the assistant
closeAssistant();
```

### Using Context with the Assistant

```typescript
import { openAssistant, createAssistantContextItem } from '@grafana/assistant';

// Create context items to provide additional information to the assistant
const datasourceContext = createAssistantContextItem('datasource', {
  datasourceUid: 'prometheus-uid',
});

const dashboardContext = createAssistantContextItem('dashboard', {
  dashboardUid: 'dashboard-uid',
  dashboardTitle: 'System Overview',
  folderUid: 'folder-uid',
  folderTitle: 'Production',
});

// Open the assistant with initial prompt and context
openAssistant({
  origin: 'grafana/dashboard-page',
  prompt: 'Analyze the CPU metrics from this dashboard',
  context: [datasourceContext, dashboardContext],
});

// Create hidden context for system instructions (won't show in UI pills)
const systemInstructions = createAssistantContextItem('structured', {
  hidden: true,
  title: 'System Instructions',
  data: {
    instructions: 'When analyzing metrics, always consider seasonal patterns and anomalies',
    preferences: 'Use clear, technical language and provide actionable insights',
  },
});

// Include hidden context with visible context
openAssistant({
  prompt: 'What are the trends in this data?',
  context: [datasourceContext, systemInstructions],
});
```

### Providing Page-Specific Context

You can register context items that are automatically included when the assistant is opened on pages matching specific URL patterns. The `providePageContext` function returns a setter function that allows you to update the context dynamically:

```typescript
import { providePageContext, createAssistantContextItem } from '@grafana/assistant';

// Create initial context for dashboard pages
const dashboardContext = createAssistantContextItem('structured', {
  data: {
    name: 'Dashboard Context',
    pageType: 'dashboard',
    capabilities: ['view', 'edit', 'share'],
    help: 'Use the assistant to analyze dashboard data, create queries, or troubleshoot issues.',
  },
});

// Register context for all dashboard pages and get a setter function
const setContext = providePageContext('/d/*', [dashboardContext]);

// Later, dynamically update the context based on user interactions or data changes
setContext([
  dashboardContext,
  createAssistantContextItem('structured', {
    data: {
      name: 'Panel Context',
      selectedPanel: 'cpu-usage-panel',
      panelType: 'graph',
    },
  }),
]);

// Clean up when no longer needed
setContext.unregister();

// Example: Provide hidden system instructions for explore pages
providePageContext('/explore', [
  createAssistantContextItem('structured', {
    hidden: true,
    title: 'Additional instructions',
    data: {
      'System Instructions':
        'When working with explore queries, always validate the time range and check for data availability. Suggest using recording rules for expensive queries.',
    },
  }),
]);
```

### Using Page Context in React Components

For React components, use the `useProvidePageContext` hook which automatically handles cleanup:

```typescript
import { useProvidePageContext, createAssistantContextItem } from '@grafana/assistant';

function DashboardComponent() {
  const dashboardContext = createAssistantContextItem('structured', {
    data: {
      name: 'Dashboard Context',
      pageType: 'dashboard'
    }
  });

  // Automatically cleans up on unmount
  const setContext = useProvidePageContext('/d/*', [dashboardContext]);

  // Update context when needed
  const handlePanelSelect = (panelId: string) => {
    setContext([
      dashboardContext,
      createAssistantContextItem('structured', {
        data: {
          name: 'Panel Context',
          selectedPanel: panelId
        }
      })
    ]);
  };

  return <div>Dashboard content</div>;
}
```

### Accessing Current Page Context

Use the `usePageContext` hook to access all active context for the current page:

```typescript
import { usePageContext } from '@grafana/assistant';

function MyComponent() {
  const pageContext = usePageContext();

  console.log('Active context items:', pageContext);

  return <div>Page has {pageContext.length} context items</div>;
}
```

### Creating Context Items

The `createAssistantContextItem` function allows you to create structured context items that provide the assistant with additional information about your Grafana resources.

```typescript
import { createAssistantContextItem } from '@grafana/assistant';

// Create a datasource context
const datasourceContext = createAssistantContextItem('datasource', {
  datasourceUid: 'prom-123',
  title: 'Main Datasource', // Optional custom title
  icon: 'database', // Optional custom icon
});

// Create a dashboard context
const dashboardContext = createAssistantContextItem('dashboard', {
  dashboardUid: 'dash-456',
  dashboardTitle: 'Application Overview',
  folderUid: 'folder-789',
  folderTitle: 'Production Dashboards',
});

// Create a label context
const labelContext = createAssistantContextItem('label_name', {
  datasourceUid: 'prom-123',
  datasourceType: 'prometheus',
  labelName: 'service',
});

// Create a label value context
const labelValueContext = createAssistantContextItem('label_value', {
  datasourceUid: 'prom-123',
  datasourceType: 'prometheus',
  labelName: 'service',
  labelValue: 'api-server',
});

// Create a folder context
const folderContext = createAssistantContextItem('dashboard_folder', {
  folderUid: 'folder-789',
  folderTitle: 'Production Dashboards',
});

// Create structured data context
const structuredContext = createAssistantContextItem('structured', {
  data: {
    name: 'Custom Data',
    metrics: ['cpu_usage', 'memory_usage'],
    threshold: 80,
  },
});

// Create generic context
const genericContext = createAssistantContextItem('unknown', {
  id: 'my-context',
  text: 'Some additional context information',
});

// Create component context for custom React components
const componentContext = createAssistantContextItem('component', {
  components: {
    MyCustomComponent: MyCustomComponentImplementation,
    AnotherComponent: AnotherComponentImplementation,
  },
  prompt: `
- When mentioning specific entities, use the your_app_MyCustomComponent with the syntax: <your_app_MyCustomComponent prop1={'value1'} prop2={'value2'} />
- Custom components must never be wrapped in code blocks (backticks \` or \`\`\`).
- Always provide all required props when using custom components.
`,
  namespace: 'your_app', // Optional: Namespace for your components (defaults to 'components')
});

// Create hidden context (useful for system instructions)
const hiddenInstructions = createAssistantContextItem('structured', {
  hidden: true, // Won't be shown in UI pills but will be sent to the assistant
  title: 'Page-specific instructions',
  data: {
    instructions: 'Always provide step-by-step explanations',
    context: 'This is a complex system with multiple dependencies',
  },
});
```

### Registering Custom Components for Assistant Usage

The `component` context type allows you to register custom React components that the assistant can use in its responses. This is particularly useful for rendering domain-specific UI elements that enhance the user experience.

> **⚠️ Important:** Components must be approved by #grafana-assistant before usage. Please reach out to the Grafana Assistant team for review and approval of your custom components before implementing them in production.

#### Method 1: Using provideComponents (Recommended)

```typescript
import { provideComponents } from '@grafana/assistant';
import MyEntityMention from 'components/MyEntityMentionComponent';
import MyCustomChart from 'components/MyCustomChartComponent';

/**
 * Configure the assistant context for your app plugin.
 * This sets up custom components that will be available to the Grafana Assistant
 * when users are on any page matching the provided pattern.
 */
export function configureAssistantContext() {
  provideComponents(
    `
    - When mentioning an entity in text, always use the your_app_MyEntityMention component: <your_app_MyEntityMention name={'entity name here'} type={'entity type here'} />
    - For displaying charts, use the your_app_MyCustomChart component: <your_app_MyCustomChart data={chartData} title={'Chart Title'} />
    - Custom components must never be wrapped in code blocks (backticks \` or \`\`\`).
    `,
    'your_app', // namespace
    {
      MyEntityMention,
      MyCustomChart,
    },
    /.*/ // matches all pages
  );
}
```

#### Method 2: Using createAssistantContextItem with providePageContext

```typescript
import { createAssistantContextItem, providePageContext } from '@grafana/assistant';
import MyEntityMention from 'components/MyEntityMentionComponent';
import MyCustomChart from 'components/MyCustomChartComponent';

export function configureAssistantContext() {
  providePageContext(/.*/, [
    createAssistantContextItem('component', {
      components: {
        MyEntityMention,
        MyCustomChart,
      },
      prompt: `
        - When mentioning an entity in text, always use the your_app_MyEntityMention component: <your_app_MyEntityMention name={'entity name here'} type={'entity type here'} />
        - For displaying charts, use the your_app_MyCustomChart component: <your_app_MyCustomChart data={chartData} title={'Chart Title'} />
        - Custom components must never be wrapped in code blocks (backticks \` or \`\`\`).
      `,
      namespace: 'your_app', // Optional: defaults to 'components' if not provided
    }),
  ]);
}
```

**Key Points for Component Context:**

- **Approval Required**: ⚠️ Components must be approved by #grafana-assistant before usage
- **Components**: A record of component names to their React component implementations
- **Prompt**: Instructions for the assistant on how and when to use these components
- **Namespace**: A unique identifier for your app's components (e.g., 'your_app', 'datasource'). Optional - defaults to 'components' if not provided
- **Component Usage**: Components are referenced in assistant responses as `<namespace_ComponentName prop={'value'} />` (note the underscore format)
- **No Code Blocks**: Custom components should never be wrapped in markdown code blocks

**Example Component Implementation:**

```typescript
interface MyEntityMentionProps {
  name: string;
  type: string;
  env?: string;
  site?: string;
  namespace?: string;
  properties?: string;
}

const MyEntityMention: React.FC<MyEntityMentionProps> = ({ name, type, env, site, namespace, properties }) => {
  return (
    <span
      className="entity-mention"
      data-entity-type={type}
      title={`${type}: ${name}`}
    >
      {name}
    </span>
  );
};

export default MyEntityMention;
```

### Using Hidden Context for System Instructions

The `hidden` parameter allows you to provide context to the assistant without showing it in the UI pills. This is particularly useful for:

- **System instructions**: Provide behavioral guidelines specific to certain pages
- **Metadata**: Include technical details that might confuse users if shown
- **Contextual rules**: Add page-specific rules without cluttering the interface

Example use cases:

```typescript
// Provide exploration-specific guidelines
providePageContext('/explore', [
  createAssistantContextItem('structured', {
    hidden: true,
    title: 'Explore Page Guidelines',
    data: {
      instructions: [
        'Always validate time ranges before running queries',
        'Suggest using recording rules for expensive queries',
        'Warn about queries that might impact performance',
      ],
      queryLimits: {
        maxTimeRange: '24h',
        suggestedSampleInterval: '1m',
      },
    },
  }),
]);

// Add dashboard-specific context
providePageContext('/d/*', [
  createAssistantContextItem('structured', {
    hidden: true,
    title: 'Dashboard Instructions',
    data: {
      bestPractices: 'Encourage users to use template variables for flexibility',
      performance: 'Suggest panel caching for slow queries',
    },
  }),
]);
```

### Exposing Functions to the Assistant

```typescript
import {
  getExposeAssistantFunctionsConfig,
  newFunctionNamespace,
  FunctionImplementation,
  FunctionNamespace,
  NamedFunctions,
} from '@grafana/assistant';

// Define your functions
const myFunctions: NamedFunctions = {
  getMetrics: (datasource: string) => {
    return ['cpu_usage', 'memory_usage', 'disk_io'];
  },

  calculateAverage: (values: number[]) => {
    return values.reduce((a, b) => a + b, 0) / values.length;
  },
};

// Create a namespace for your functions
const namespace = newFunctionNamespace('myPlugin', myFunctions);

// Export the configuration for your plugin
export const getExtensionConfigs = () => [getExposeAssistantFunctionsConfig([namespace])];
```

### Contributing Assistant extensions (experimental)

An app plugin can add conversation starters, expanded tool details, inline reply components, and custom artifact renderers to Grafana Assistant. Register one `AssistantExtensionManifest` with `getAssistantExtensionConfig`:

```typescript
import { AppPlugin } from '@grafana/data';
import { SuggestionContext, getAssistantExtensionConfig, type AssistantExtensionManifest } from '@grafana/assistant';

const manifest: AssistantExtensionManifest = {
  schemaVersion: 1,
  starters: [{
    label: 'Review service errors',
    message: 'Help me review recent service errors in this stack.',
    contexts: [SuggestionContext.Alerting, SuggestionContext.Logs, SuggestionContext.Workspace],
  }],
};

export const plugin = new AppPlugin().addFunction(getAssistantExtensionConfig(manifest));
```

Starter `contexts` is an optional list of `SuggestionContext` values exported by `@grafana/assistant`. A starter is eligible when any listed context matches the current route (including nested routes). Eligible plugin starters share randomized selection with built-in route suggestions in the sidebar and Workspace; registration does not guarantee a visible slot. Omit `contexts` to limit the starter to the contributing plugin’s `/a/<pluginId>` routes. An empty list disables the starter. Invalid contexts cause the host to drop that starter rather than broaden its scope. Generic suggestions fill remaining slots.

Also add these fields to your existing `plugin.json`:

```json
{
  "preload": true,
  "extensions": {
    "addedFunctions": [{
      "title": "assistant-extension",
      "targets": ["grafana-assistant-app/extension/v1"]
    }]
  }
}
```

Use Grafana 12.1 or later and an Assistant environment with `assistant.frontend.plugin-extensions` enabled. `preload: true` supports Grafana 12.1, whose extension loader doesn't discover function-only declarations. Add the function to your existing `AppPlugin` if you already have one. Keep the manifest and its component/function references stable after registration.

Use `starters` for attributed first messages on empty chats; use `provideQuestions` for page-specific suggestions. The supported `chits` kinds are `tool-details` and `inline`. Artifact writes use `artifact_files`; renderers receive a read-only conversation filesystem.

The manifest also accepts `skills` and `promptContributions`. Skills currently appear in settings but aren't loaded by the model. Modes require `assistant.frontend.plugin-extensions-prompt` and your plugin ID in `assistant.frontend.plugin-extensions-prompt-allowlist`, in addition to the master flag. An operator can disable all contributions with `assistant.frontend.plugin-extensions-enabled.<pluginId>`.

Browser registration does not enable skill activation, even when the builtin Agent Skills runtime is enabled. The proposed plugin activation contract requires trusted server integration and a supported runtime surface. Automatic discovery additionally requires a short author hint, `whenToUse`, which is not yet part of this SDK. Existing manifests remain valid for inspection and would be manual-only without a hint once manual plugin activation ships.

Inline components can supply `prompt` (up to 4096 UTF-8 bytes) to explain when to emit their tag in normal chat, without selecting a mode. This is attributed chat context, not a system-prompt override. Set `selfClosing: true` for a childless marker such as `<grafana_myplugin_app_result name="checkout" />`; the host recovers saved bare/backticked tag names and suppresses identical duplicates outside code.

Use `InlineChitProps` to receive `turn.isStreaming` and `turn.tools` (message-local tool names, IDs, and unknown inputs). Validate inputs in your plugin. A stopped turn is also non-streaming; this is not a success signal. Older hosts omit `turn` and ignore the new fields. The [inline integration example](../../../docs/sources/reference/plugin-extensions.md#use-tool-inputs-and-wait-for-the-turn) shows how to keep a scene stable after its first settled render.

For a larger view, add `display: 'block'` with `selfClosing: true`. It uses the same tag but renders below the message text, outside Markdown paragraphs, at the available message width with keyboard-accessible horizontal scrolling. Omitted `display` keeps inline placement. `InlineChitDisplay` exports the two display values.

This API is experimental and may change between releases. For contribution examples, ownership rules, limits, and troubleshooting, refer to the [plugin extensions guide](../../../docs/sources/reference/plugin-extensions.md).

### Using the OpenAssistantButton Component

The `OpenAssistantButton` is a pre-built Button component that provides an easy way to add assistant functionality to your UI with a single button click. It automatically checks availability and hides itself if the Assistant is not available.

```typescript
import { OpenAssistantButton, createAssistantContextItem } from '@grafana/assistant';

function MyDashboard() {
  const dashboardContext = createAssistantContextItem('dashboard', {
    dashboardUid: 'dashboard-123',
    dashboardTitle: 'CPU Metrics Dashboard'
  });

  return (
    <div>
      <h1>Dashboard</h1>

      {/* Basic usage */}
      <OpenAssistantButton
        prompt="Help me analyze the CPU usage trends in this dashboard"
        context={[dashboardContext]}
        origin="grafana/dashboard-menu"
      />

      {/* Icon-only button */}
      <OpenAssistantButton
        prompt="Show me optimization suggestions"
        iconOnlyButton={true}
        name="Get AI suggestions"
        size="md"
        origin="grafana-datasources/loki/query-editor"
      />

      {/* Button without auto-send */}
      <OpenAssistantButton
        prompt="What patterns do you see in the data?"
        autoSend={false}
        name="Ask Assistant"
        size="lg"
        origin="grafana/panel-viewer"
      />
    </div>
  );
}
```

## API Reference

### React Hooks

#### `useAssistant(): AssistantHook`

A React hook that provides assistant availability status and control functions.

**Returns:** An object with the following properties:

- `isLoading: boolean` - Whether the availability check is still in progress
- `isAvailable: boolean` - Whether the Assistant is available
- `openAssistant?: (props: OpenAssistantProps) => void` - Function to open the assistant (undefined if not available)
- `closeAssistant?: () => void` - Function to close the assistant (undefined if not available)
- `toggleAssistant?: (props: OpenAssistantProps) => void` - Function to toggle the assistant (undefined if not available)

#### `useTerms(): { accepted: boolean; loading: boolean; error: string | null }`

A React hook to check terms and conditions acceptance status. Automatically fetches terms data on mount and provides loading/error states.

**Returns:** An object with the following properties:

- `accepted: boolean` - Whether terms are accepted and up-to-date
- `loading: boolean` - Whether the check is in progress
- `error: string | null` - Error message if the check failed, null otherwise

#### `useLimits(): LimitsHookResult`

A React hook to check user prompt limits. Automatically fetches limits data on mount and listens to real-time updates via custom events when the Assistant plugin updates limits.

**Returns:** An object with the following properties:

- `count: number` - Current prompt count for the month
- `limit: number` - Monthly prompt limit (0 = unlimited)
- `month: string` - Current month in YYYY-MM format
- `isLimitReached: boolean` - Whether the limit has been reached
- `loading: boolean` - Whether the initial fetch is in progress
- `error: string | null` - Error message if the fetch failed, null otherwise
- `refetch: () => void` - Function to manually refetch limits

**Example:**

```typescript
import { useLimits } from '@grafana/assistant';

function MyComponent() {
  const { count, limit, isLimitReached, loading, error } = useLimits();

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error}</div>;
  if (isLimitReached) {
    return <div>Monthly limit reached ({count}/{limit})</div>;
  }
  return <div>Prompts used: {count}/{limit || 'unlimited'}</div>;
}
```

#### `checkLimits(): Promise<LimitsData>`

An async function to check user prompt limits. Returns a promise with the limits data.

**Returns:** A promise that resolves to an object with:

- `count: number` - Current prompt count for the month
- `limit: number` - Monthly prompt limit (0 = unlimited)
- `month: string` - Current month in YYYY-MM format
- `isLimitReached: boolean` - Whether the limit has been reached

**Example:**

```typescript
import { checkLimits } from '@grafana/assistant';

async function checkUserLimits() {
  try {
    const { isLimitReached, count, limit } = await checkLimits();
    if (isLimitReached) {
      console.log(`Limit reached: ${count}/${limit}`);
    }
  } catch (error) {
    console.error('Failed to check limits:', error);
  }
}
```

#### `usePageContext(): ChatContextItem[]`

A React hook that returns all active context items for the current page based on registered URL patterns.

**Returns:** Array of `ChatContextItem` objects that match the current page URL.

#### `useProvidePageContext(urlPattern: string | RegExp, initialContext?: ChatContextItem[]): (context: ChatContextItem[]) => void`

A React hook for providing page context that automatically cleans up on unmount. This is the recommended way to use page context in React components.

**Parameters:**

- `urlPattern` - URL pattern (string or RegExp) to match against page URLs
- `initialContext` - Initial array of ChatContextItem to provide when the pattern matches (defaults to empty array)

**Returns:** A setter function to update the context

#### `usePageComponents(): Record<string, ComponentImplementation>`

A React hook that provides all components available for the current page. This hook automatically updates when the URL changes or when component registrations change.

**Returns:** Object containing all components available for the current page, keyed by `namespace_componentName` format.

**Example:**

```typescript
import { usePageComponents } from '@grafana/assistant';

function MyAssistantComponent() {
  const components = usePageComponents();

  // Access components registered with namespace 'myapp' and component name 'EntityMention'
  const EntityMentionComponent = components['myapp_EntityMention'];

  return (
    <div>
      Available components: {Object.keys(components).join(', ')}
    </div>
  );
}
```

### React Components

#### `<OpenAssistantButton />`

A pre-built React component that renders a button to open the Grafana Assistant with configurable prompt and context.

**Parameters:**

- `prompt: string` - **Required** The initial prompt to display in the assistant
- `origin: string` - **Required** Origin of the request that opened the assistant. Should be structured using forward slashes with the first part as a namespace. Examples: 'grafana-datasources/prometheus/query-builder', 'grafana-slo-app/slo-editor-overview'
- `context?: ChatContextItem[]` - Optional context items created with `createAssistantContextItem`
- `autoSend?: boolean` - Whether to automatically send the initial prompt (defaults to `true`)
- `title?: string` - Text to display on the button (defaults to `'Analyze with Assistant'`)
- `size?: 'xs' | 'sm' | 'md' | 'lg'` - Button size (defaults to `'sm'`)
- `iconOnlyButton?: boolean` - If `true`, renders as an icon-only button with tooltip (defaults to `false`)

#### `<QueryWithAssistantButton />`

A button component designed for query editors that opens the Assistant sidebar pre-populated with query context, datasource information, and an actionable prompt. It automatically detects whether the user is creating a new query or updating an existing one and adjusts the prompt accordingly. Renders nothing if the Assistant is not available.

**Parameters:**

- `size?: ComponentSize` - Button size (`'xs'`, `'sm'`, `'md'`, `'lg'`), defaults to `'sm'`
- `currentQuery: DataQuery` - **Required** The current query being edited
- `queries: DataQuery[]` - **Required** All queries in the editor (used to provide context about sibling queries)
- `dataSourceInstanceSettings: DataSourceInstanceSettings` - **Required** The datasource instance settings
- `app?: CoreApp` - The app context (e.g., `CoreApp.Explore`, `CoreApp.Dashboard`)
- `datasourceApi: DataSourceApi | null` - **Required** The datasource API instance (used to get query display text)
- `title?: string` - Button text (defaults to `'Query with Assistant'`)

**Example:**

```typescript
import { QueryWithAssistantButton } from '@grafana/assistant';
import { CoreApp } from '@grafana/data';

function QueryEditor({ query, queries, datasource, datasourceSettings }) {
  return (
    <div>
      {/* Your query editor UI */}
      <QueryWithAssistantButton
        size="sm"
        currentQuery={query}
        queries={queries}
        dataSourceInstanceSettings={datasourceSettings}
        app={CoreApp.Explore}
        datasourceApi={datasource}
      />

      {/* With custom title */}
      <QueryWithAssistantButton
        size="sm"
        currentQuery={query}
        queries={queries}
        dataSourceInstanceSettings={datasourceSettings}
        app={CoreApp.Explore}
        datasourceApi={datasource}
        title="Ask AI"
      />
    </div>
  );
}
```

#### `<AITextInput />`

An AI-powered text input component that uses the inline assistant to generate content. Features a gradient border animation while generating and an integrated send button. Perfect for generating short-form content like titles, labels, or names.

**Parameters:**

- `value: string` - **Required** The current value of the input (controlled component)
- `onChange: (value: string) => void` - **Required** Called when the input value changes
- `origin: string` - **Required** Origin for analytics tracking (e.g., 'grafana/panel-editor/title')
- `placeholder?: string` - Placeholder text (defaults to 'Ask AI to generate...')
- `systemPrompt?: string` - Optional system prompt to guide the assistant's behavior
- `onComplete?: (text: string) => void` - Callback when generation completes successfully
- `onError?: (error: Error) => void` - Callback if an error occurs during generation
- `onDelta?: (delta: string) => void` - Callback for streaming tokens as they are received during generation
- `disabled?: boolean` - Whether the input is disabled
- `autoGenerate?: boolean` - When true, automatically generates content on mount if value is empty (uses `systemPrompt` as the generation prompt)
- `streaming?: boolean` - When true, streams generated text into the input in real-time. When false (default), shows a placeholder until generation completes
- `className?: string` - Additional CSS class name
- `data-testid?: string` - Test ID for testing

**Example:**

```typescript
import { AITextInput } from '@grafana/assistant';
import { useState } from 'react';

function PanelEditor() {
  const [title, setTitle] = useState('');

  return (
    <AITextInput
      value={title}
      onChange={setTitle}
      origin="grafana/panel-editor/title"
      systemPrompt="Generate a concise, descriptive panel title. Return only the title text."
      placeholder="Ask AI to generate a title..."
      autoGenerate
      streaming
      onComplete={(text) => {
        setTitle(text);
        console.log('Generated:', text);
      }}
    />
  );
}
```

#### `<AITextArea />`

An AI-powered textarea component that uses the inline assistant to generate longer content. Features a gradient border animation while generating and an integrated send button. Use Cmd/Ctrl+Enter to submit. Ideal for descriptions, documentation, or multi-line content generation.

**Parameters:**

- `value: string` - **Required** The current value of the textarea (controlled component)
- `onChange: (value: string) => void` - **Required** Called when the textarea value changes
- `origin: string` - **Required** Origin for analytics tracking (e.g., 'grafana/panel-editor/description')
- `placeholder?: string` - Placeholder text (defaults to 'Ask AI to generate... (Cmd/Ctrl+Enter to send)')
- `systemPrompt?: string` - Optional system prompt to guide the assistant's behavior
- `onComplete?: (text: string) => void` - Callback when generation completes successfully
- `onError?: (error: Error) => void` - Callback if an error occurs during generation
- `onDelta?: (delta: string) => void` - Callback for streaming tokens as they are received during generation
- `disabled?: boolean` - Whether the textarea is disabled
- `autoGenerate?: boolean` - When true, automatically generates content on mount if value is empty (uses `systemPrompt` as the generation prompt)
- `streaming?: boolean` - When true, streams generated text into the textarea in real-time. When false (default), shows a placeholder until generation completes
- `rows?: number` - Number of rows for the textarea (defaults to 4)
- `className?: string` - Additional CSS class name
- `data-testid?: string` - Test ID for testing

**Example:**

```typescript
import { AITextArea } from '@grafana/assistant';
import { useState } from 'react';

function DashboardEditor() {
  const [description, setDescription] = useState('');

  return (
    <AITextArea
      value={description}
      onChange={setDescription}
      origin="grafana/dashboard-editor/description"
      systemPrompt="Generate a clear dashboard description in 2-3 sentences."
      placeholder="Ask AI to generate a description... (Cmd/Ctrl+Enter to send)"
      rows={6}
      autoGenerate
      streaming
      onComplete={(text) => {
        setDescription(text);
        console.log('Generated:', text);
      }}
    />
  );
}
```

#### `useAIGeneration(options): UseAIGenerationResult`

The hook behind `AITextInput` and `AITextArea`. Use it when you want the same inline-assistant rewrite behavior but with your own trigger and text control — for example, an "Improve with AI" button next to a plain textarea.

**Options (`UseAIGenerationOptions`):**

- `value: string` - **Required** The current text (controlled)
- `onChange: (value: string) => void` - **Required** Called with the generated text (and blanked while generating)
- `origin: string` - **Required** Origin for analytics tracking
- `systemPrompt?: string` - Optional system prompt to guide the assistant
- `getUserPrompt?: (textInput: string) => string` - Wrap the current text into the prompt sent to the assistant
- `onComplete?: (text: string) => void` / `onError?: (error: Error) => void` / `onDelta?: (delta: string) => void` - Generation lifecycle callbacks
- `disabled?: boolean`, `autoGenerate?: boolean`, `streaming?: boolean` - Same semantics as `AITextArea`

**Returns:** `{ displayValue, isSendDisabled, handleGenerate, isGenerating, error }`

**Example:**

```typescript
import { useAIGeneration } from '@grafana/assistant';
import { useState } from 'react';

function InstructionsField() {
  const [text, setText] = useState('');
  const { handleGenerate, isGenerating } = useAIGeneration({
    value: text,
    onChange: setText,
    origin: 'my-app/instructions',
    systemPrompt: 'Refine the user input into a clear, actionable prompt. Output only the prompt.',
    getUserPrompt: (input) => `Improve this prompt, keeping its intent: ${input}`,
  });

  return (
    <div>
      <button type="button" onClick={handleGenerate} disabled={!text.trim() || isGenerating}>
        {isGenerating ? 'Improving…' : 'Improve with AI'}
      </button>
      <textarea value={text} onChange={(e) => setText(e.target.value)} />
    </div>
  );
}
```

### Using the Inline Assistant

The `useInlineAssistant` hook generates content inline without opening the sidebar — perfect for filling form fields with AI-generated text.

```typescript
import { useInlineAssistant } from '@grafana/assistant';
import { useState } from 'react';

export function PanelEditor() {
  const [title, setTitle] = useState('');
  const titleGen = useInlineAssistant();

  return (
    <div>
      <Input
        value={titleGen.isGenerating ? titleGen.content : title}
        onChange={(e) => setTitle(e.target.value)}
      />
      <Button
        onClick={() => {
          titleGen.generate({
            prompt: 'Generate a panel title for CPU metrics',
            origin: 'my-plugin/panel-editor',
            agentName: 'panel-title-generator',
            agentId: 'v1',
            onComplete: (text) => setTitle(text),
            onError: (err) => console.error(err)
          });
        }}
        disabled={titleGen.isGenerating}
      >
        {titleGen.isGenerating ? 'Generating...' : 'Generate'}
      </Button>
    </div>
  );
}
```

**Key Features:**

- `isGenerating` - Boolean indicating generation is in progress
- `content` - Current streaming content (updates in real-time)
- `generate(options)` - Start a new generation
- Each instance is independent—use multiple for concurrent fields

For instrumentation, keep `origin` focused on where the request came from and use `agentName` for the stable logical inline agent within that integration. The SDK namespaces `agentName` by the `origin` namespace, so different integrations can safely reuse names like `title-generator` without colliding in Sigil. Use `agentId` when you also want to tag a version or implementation identifier.

#### Using Tools with Inline Assistant

Tools extend the assistant's capabilities with custom actions. Tools can return both text and structured data (artifacts). To access artifact data, include an `onToolComplete` callback in your tool implementation.

```typescript
import { createTool } from '@grafana/assistant';
import { z } from 'zod';
import type { ToolInvokeOptions, ToolOutput } from '@grafana/assistant';

// Define artifact type
interface WeatherData {
  city: string;
  temperature: { celsius: number; fahrenheit: number };
  conditions: string;
  humidity: number;
}

// Create tool schema
const weatherSchema = z.object({
  city: z.string().describe('The city name to get weather for'),
});

// Create tool that accepts onToolComplete callback
export const createWeatherTool = (onToolComplete: (data: WeatherData) => void) => {
  return createTool(
    async (input: z.infer<typeof weatherSchema>): Promise<ToolOutput> => {
      const { city } = input;

      // Fetch weather data (simplified example)
      const weatherData: WeatherData = {
        city,
        temperature: { celsius: 22, fahrenheit: 72 },
        conditions: 'Sunny',
        humidity: 65,
      };

      // Call the callback to pass artifact data
      onToolComplete(weatherData);

      // Return tuple: [text response, artifact]
      return [
        `Current weather in ${city}: ${weatherData.temperature.celsius}°C, ${weatherData.conditions}`,
        weatherData,
      ];
    },
    {
      name: 'get_weather',
      description: 'Fetches weather information and returns structured data.',
      inputSchema: z.toJSONSchema(weatherSchema),
      validate: (input) => weatherSchema.parse(input),
      responseFormat: 'content_and_artifact',
    }
  );
};

// Use the tool with inline assistant
export function WeatherComponent() {
  const [result, setResult] = useState('');
  const [weatherData, setWeatherData] = useState<WeatherData | null>(null);
  const gen = useInlineAssistant();

  const handleGetWeather = () => {
    gen.generate({
      prompt: 'What is the weather in San Francisco?',
      origin: 'my-plugin/weather',
      tools: [createWeatherTool((data) => setWeatherData(data))], // Pass callback
      onComplete: (text) => setResult(text),
      onError: (err) => console.error(err),
    });
  };

  return (
    <div>
      <Button onClick={handleGetWeather} disabled={gen.isGenerating}>
        Get Weather
      </Button>
      {result && <div>{result}</div>}
      {weatherData && <pre>{JSON.stringify(weatherData, null, 2)}</pre>}
    </div>
  );
}
```

#### Configuring the agent and calling it without React

By default the inline assistant suits short content generation: at most 5 model calls per prompt, the smaller model, no reasoning, and 4096 output tokens per call.
For agentic work, such as discovery with your own tools before answering, pass an `InlineAssistantConfig` to `useInlineAssistant(config)` or, without React, to the inline assistant factory.
The config is fixed for the life of the instance and applies to every prompt sent to it.

| Field | Allowed values | Default |
| --- | --- | --- |
| `maxSteps` | integer from 1 to 76; the agent stops after the last model call | `5` |
| `weak` | `true` for the smaller, faster model, `false` for the standard one | `true` |
| `reasoningLevel` | `'none'`, `'low'`, `'medium'`, `'high'`, `'xhigh'`, `'max'` | `'none'` |
| `maxTokens` | integer from 1 to 16000 output tokens per model call | `4096` |

`ensureInlineAssistantInitialized` loads the implementation on first use.
The factory returns an `InlineAssistant` that owns one conversation: each `sendPrompt` continues it, so you can answer a question the model asked in the next prompt.
Call `dispose()` when you are done.

```typescript
import { ensureInlineAssistantInitialized, getInlineAssistantFactory } from '@grafana/assistant';

await ensureInlineAssistantInitialized();
const assistant = await getInlineAssistantFactory()('my-plugin/planner', {
  maxSteps: 30,
  weak: false,
  reasoningLevel: 'medium',
  maxTokens: 8000,
});
try {
  await assistant.sendPrompt({
    prompt: 'Plan the checks for this requirement: ...',
    systemPrompt: 'Explore with the tools, then call submit_plan once.',
    tools: [listDatasourcesTool, queryTool, submitPlanTool],
    onComplete: (text) => console.log(text),
    onError: (err) => console.error(err),
  });
} finally {
  assistant.dispose();
}
```

The implementation comes from the installed Grafana Assistant, which can be older than your copy of the SDK.
Releases that predate `InlineAssistantConfig` ignore it and run with the defaults.
An invalid value makes the factory reject before any conversation is created; the hook reports it through `onError` and `error`.

### Creating and monitoring investigations

Use `createInvestigation`, `getInvestigationStatus`, and `getInvestigationReport` from a Grafana plugin. These methods call the public-preview v2 API through Grafana's plugin proxy as the signed-in user. They require a backend with the v2 investigation routes enabled. Do not embed service account tokens in browser code; use the [HTTP API](../../../docs/sources/reference/http-apis/_index.md#manage-investigations) for server-side integrations.

```typescript
import { createInvestigation, getInvestigationStatus, getInvestigationReport } from '@grafana/assistant';

// Call from an explicit user action. The caller awaits and handles failures.
async function startInvestigation(signal: AbortSignal) {
  return createInvestigation({ instruction: 'Investigate elevated API latency' }, { signal });
}

// Call on refresh or from a polling loop with an interval and a deadline.
async function readInvestigation(investigationId: string, signal: AbortSignal) {
  const status = await getInvestigationStatus(investigationId, { signal });
  const report = status.state === 'completed'
    ? await getInvestigationReport(investigationId, { signal })
    : undefined;
  return { status, report };
}
```

| Method | Result |
| --- | --- |
| `createInvestigation(input, options?)` | `CreatedInvestigation`: `investigationId` and `chatId`. Input includes required `instruction` and optional `title`, `labels`, and `teamNames`. |
| `getInvestigationStatus(investigationId, options?)` | `InvestigationStatus`: identifiers, optional `state`, title, summary, and failure or completion details. |
| `getInvestigationReport(investigationId, options?)` | `InvestigationReport`: `investigationId`, `reportPending`, and optional `reportSpec` and `reportContent`. |

Results are unwrapped from the HTTP success envelope and validated at runtime. The exported types derive from the backend OpenAPI schema. Each call makes one request; the SDK doesn't poll, retry, or display error notifications. Every method accepts `{ signal?: AbortSignal }`. Aborting stops waiting for the request; it doesn't cancel an investigation already created on the server. A failed or aborted create request can still have started an investigation, so don't retry it automatically.

Use the returned `investigationId` for reads. The separate `chatId` identifies the backing conversation. Poll `state`: `pending` and `in_progress` indicate ongoing work; `completed`, `failed`, and `cancelled` stop ordinary polling; `paused` needs intervention. State is an optional, open string: bound polling with a deadline and handle unfamiliar or absent states explicitly.

A report can be absent even when `reportPending` is false. A snapshot can return 404 before its session starts; status reads remain the lifecycle source. Report retrieval also works during an investigation when a snapshot exists. Render `reportContent` as text or sanitized Markdown, never as trusted HTML.

Handle rejected promises in your UI. HTTP failures preserve Grafana's `FetchError` contract (use `isFetchError` from `@grafana/runtime`); malformed responses reject with a validation error. Reads respect the signed-in user's permissions and team visibility. Refer to the HTTP reference for exact permissions and response fields. For live updates, use `observeInvestigation` below.

For a structured view derived from an existing report, use the lens hook below.

### Observing investigation progress

`observeInvestigation(investigationId): Observable<InvestigationEvent>` watches an existing investigation as the signed-in Grafana user. It requires the v2 investigation APIs and the authenticated chat event stream on the backend. Each subscription starts its own connection and snapshot reads; calling the function alone does no work.

```typescript
import { observeInvestigation, type InvestigationEvent } from '@grafana/assistant';

// Pass your UI's update, error, and completion handlers.
function watchInvestigation(
  investigationId: string,
  onEvent: (event: InvestigationEvent) => void,
  onError: (error: unknown) => void,
  onComplete: () => void,
) {
  const subscription = observeInvestigation(investigationId).subscribe({
    next: onEvent,
    error: onError,
    complete: onComplete,
  });

  // Call when leaving the view. The investigation keeps running.
  return () => subscription.unsubscribe();
}
```

| Event | Payload | Meaning |
| --- | --- | --- |
| `status` | `event.status: InvestigationStatus` | Current persisted lifecycle state, emitted initially and when its fields change. |
| `report` | `event.report: InvestigationReport` | Current persisted report snapshot, when available, emitted initially and when its fields change. |
| `progress` | `investigationId`, `messageId`, `contentIndex`, `delta` | Live user-visible text. Append deltas separately for each `(messageId, contentIndex)` pair. |

The first event is a status snapshot. A report snapshot can be absent before the session starts. Live text can arrive while a snapshot is loading. Progress excludes tool inputs, reasoning, and private sub-agent audiences; it is best-effort text, not a complete conversation history or the report itself. Render text safely. Report events contain persisted snapshots rather than every in-flight report edit.

The observer refreshes persisted state after the stream connects and on relevant notifications, and reconciles about every 5 seconds while idle. Reads are serialized and identical snapshots are suppressed. Stream notifications don't override persisted lifecycle state.

`completed`, `failed`, and `cancelled` are terminal states. After observing a terminal state and checking the report, the Observable completes when no report is pending. A missing report does not prevent completion. `paused`, unknown, and absent states keep the subscription open. Use the status payload to distinguish success from investigation failure; `complete` means observation finished successfully, not that the investigation succeeded.

HTTP failures, invalid response/event data, a connection that doesn't open within 30 seconds, or a disconnected stream terminate the subscription through `error`. HTTP errors retain Grafana's `FetchError` shape. Browser event-stream errors don't expose an HTTP status. There is no automatic reconnect: subscribe again to recover current snapshots. Missed text deltas are not replayed, including text produced before the initial connection. Keep partial progress distinct from a complete transcript.

Unsubscribing closes the event connection, aborts pending reads, and clears timers. It never pauses or cancels server-side work. Multiple subscriptions are independent; share one subscription in your UI when multiple consumers need the same updates. The API observes investigations only and doesn't send prompts or observe regular Assistant conversations.

### Deriving Structured Views of Investigations (Lens)

The `useInvestigationThroughLens` hook derives a structured, LLM-generated view of an investigation's report, shaped by a JSON Schema you provide. Use it when a host plugin (for example, IRM) holds an `investigationId` and wants a compact, typed digest of the investigation — an executive headline, contributing factors, follow-up actions — without rendering the full report.

Two things make the schema effective:

1. **Field `description`s are per-field instructions to the model** — that's the steering mechanism. Audience, tone, length limits, fallback wording, and allowed values all belong in the descriptions (and in `enum`s, which the model must respect).
2. **Reserve LLM fields for synthesis.** Deterministic metadata — investigation status, timestamps, workspace link — is provided to the model verbatim and copied through rather than generated, so a schema of metadata-style fields (e.g. `{ investigationLink, createdAt }`) works but wastes the lens. Ask for what only the report can answer.

Declare the schema with `as const` and the hook infers the `data` type from it (via `json-schema-to-ts`):

```typescript
import { useInvestigationThroughLens } from '@grafana/assistant';

const incidentReviewSchema = {
  type: 'object',
  properties: {
    headline: {
      type: 'string',
      description: 'One sentence describing the incident for an executive audience, max 80 characters.',
    },
    customerImpact: {
      type: 'string',
      description:
        "Plain-language description of the impact on end users, or 'none observed' if the report shows no user-facing impact.",
    },
    contributingFactors: {
      type: 'array',
      items: { type: 'string' },
      description: 'Distinct technical or process factors that contributed to the incident.',
    },
    followUps: {
      type: 'array',
      description: 'Concrete follow-up actions to prevent recurrence.',
      items: {
        type: 'object',
        properties: {
          action: { type: 'string' },
          urgency: { type: 'string', enum: ['now', 'this-week', 'backlog'] },
          rationale: {
            type: 'string',
            description: 'Why this action prevents recurrence, grounded in the report.',
          },
        },
        required: ['action', 'urgency', 'rationale'],
        additionalProperties: false,
      },
    },
  },
  required: ['headline', 'customerImpact', 'contributingFactors', 'followUps'],
  additionalProperties: false,
} as const;

export function IncidentReview({ investigationId }: { investigationId: string }) {
  const lens = useInvestigationThroughLens(incidentReviewSchema, investigationId, {
    origin: 'grafana-irm/incident-review',
  });

  if (lens.status === 'loading') {
    return <Spinner />;
  }
  if (lens.status === 'error') {
    return <span>{lens.error.message}</span>;
  }

  // lens.data is fully typed from the schema:
  // { headline: string; customerImpact: string; contributingFactors: string[];
  //   followUps: Array<{ action: string; urgency: 'now' | 'this-week' | 'backlog'; rationale: string }> }
  return (
    <section>
      <h3>{lens.data.headline}</h3>
      <p>{lens.data.customerImpact}</p>
      <ul>
        {lens.data.followUps.map((f) => (
          <li key={f.action}>
            [{f.urgency}] {f.action} — {f.rationale}
          </li>
        ))}
      </ul>
    </section>
  );
}
```

**Behavior:**

- Declarative: generation runs on mount and re-runs when the schema or `investigationId` changes. `refresh()` regenerates, bypassing the cache.
- Cached: results are cached per (investigation, schema, report content), so remounts don't regenerate for an unchanged report.
- Grounded: deterministic facts (ids, titles, statuses, timestamps, links) are copied verbatim from investigation metadata — the model only derives analytical fields (summaries, root causes) from the report.
- Validated: the model's output is validated against your schema, with one corrective retry before failing.
- Pass `enabled: false` in the options to defer generation until a precondition is met. Re-enabling serves cached results by default; if the report may have changed while disabled (for example, it was still streaming), also set `refreshOnEnable: true` so the fetch triggered by re-enabling bypasses the caches once.

**Result** is a discriminated union — `data` and `error` never coexist:

```typescript
type UseInvestigationThroughLensResult<T> =
  | { status: 'loading'; refresh: () => void }
  | { status: 'success'; data: T; refresh: () => void }
  | { status: 'error'; error: InvestigationLensError; refresh: () => void };
```

**Error codes** (`lens.error.code`):

- `unavailable` — the Grafana Assistant plugin is not installed, disabled, or too old to provide the lens. The hook never throws into your render; treat this as "hide the surface".
- `no_report` — the investigation doesn't exist or hasn't produced a report yet.
- `permission_denied` — the current user cannot read the investigation.
- `validation_failed` — the model output didn't conform to the schema after one corrective retry.
- `llm_error` — the generation request itself failed (network, usage limits, provider errors).

For attribution, `origin` follows the same convention as the inline assistant: the first path segment identifies your integration and namespaces the generation in usage tracking.

### Availability Functions

#### `isAssistantAvailable(): Observable<boolean>`

Checks if the assistant plugin is available.

**Returns:** An RxJS Observable that emits `true` if the assistant is available, `false` otherwise.

### Terms and Conditions Functions

> **Note:** In most cases, plugins do not need to check terms directly. The Grafana Assistant automatically handles cases when terms are not accepted by prompting the user to accept them before proceeding.

#### `checkTerms(): Promise<boolean>`

Checks whether terms and conditions are accepted and up-to-date. This function verifies that:

1. Terms have been accepted
2. The accepted version matches or exceeds the current version

**Returns:** A promise that resolves to `true` if terms are accepted and up-to-date, `false` otherwise.

**Throws:** Error if the API call fails.

**Example:**

```typescript
import { checkTerms } from '@grafana/assistant';

try {
  const accepted = await checkTerms();
  if (!accepted) {
    // Show terms acceptance UI
  }
} catch (error) {
  // Handle error
  console.error('Failed to check terms:', error);
}
```

### Sidebar Functions

#### `openAssistant(props: OpenAssistantProps): void`

Opens the Grafana Assistant sidebar. Repeated calls with the same prompt and context are processed even while the sidebar remains open. Calls without `chatId` start a new conversation; calls with `chatId` target that conversation.

**Parameters:**

- `props: OpenAssistantProps` - Configuration object
  - `origin: string` - Origin of the request that opened the assistant. Should be structured using forward slashes with the first part as a namespace. Examples: 'grafana-datasources/prometheus/query-builder', 'grafana-slo-app/slo-editor-overview'
  - `prompt?: string` - Optional initial prompt to display in the assistant
  - `context?: ChatContextItem[]` - Optional context items to provide additional information to the assistant
  - `autoSend?: boolean` - Optional flag to automatically send the initial prompt

#### `closeAssistant(): void`

Closes the Grafana Assistant sidebar.

### Context Functions

#### `createAssistantContextItem<T>(type: T, params: ContextTypeParams<T>): ChatContextItem`

Creates a context item that can be passed to the assistant to provide additional information.

**Parameters:**

- `type` - The type of context to create. Supported types:
  - `'datasource'` - Datasource context
  - `'dashboard'` - Dashboard context
  - `'dashboard_folder'` - Folder context
  - `'label_name'` - Label name context
  - `'label_value'` - Label value context
  - `'structured'` - Custom structured data context
  - `'component'` - Custom React components context
  - `'unknown'` - Generic text context
- `params` - Parameters specific to the context type (see Context Types section). All context types support:
  - `hidden?: boolean` - If true, the context item will not be shown in the UI pills but will still be sent to the assistant. This is useful for providing system instructions, behavioral guidelines, or other metadata that shouldn't clutter the user interface.

**Returns:** A `ChatContextItem` that can be passed to `openAssistant`

#### `providePageContext(urlPattern: string | RegExp, initialContext: ChatContextItem[]): ((context: ChatContextItem[]) => void) & { unregister: () => void }`

Registers context items for specific pages based on URL patterns. Returns a setter function to update the context dynamically. The context will be automatically included when the assistant is opened on pages matching the pattern.

**Parameters:**

- `urlPattern` - URL pattern (string with wildcards or RegExp) to match against page URLs
  - String patterns support `*` (match any characters) and `**` (match any path segments)
  - RegExp patterns provide full regex matching capabilities
- `initialContext` - Initial array of `ChatContextItem` to provide when the pattern matches

**Returns:** A setter function to update the context, with an `unregister` method attached for cleanup

**Examples:**

```typescript
// String patterns - returns setter function
const setDashboardContext = providePageContext('/d/*', initialContext); // Match any dashboard
const setExploreContext = providePageContext('/explore**', initialContext); // Match explore and subpaths

// RegExp patterns
const setSpecificContext = providePageContext(/^\/d\/[a-zA-Z0-9]+$/, initialContext); // Match specific dashboard format

// Update context dynamically
setDashboardContext([...newContext]);

// Cleanup when done
setDashboardContext.unregister();
```

#### `provideComponents(prompt: string, namespace: string, components: Record<string, ComponentImplementation>, urlPattern?: string | RegExp): ((context: ChatContextItem[]) => void) & { unregister: () => void }`

A simplified function for providing components to the assistant. This is a convenience wrapper around `providePageContext` specifically for registering components.

**Parameters:**

- `prompt` - Instructions for the assistant on how and when to use these components
- `namespace` - A unique identifier for your app's components (e.g., 'myapp', 'datasource')
- `components` - A record of component names to their React component implementations
- `urlPattern` - Optional URL pattern to match against page URLs (defaults to `/.*/` - matches all pages)

**Returns:** A setter function to update the context, with an `unregister` method attached for cleanup

**Example:**

```typescript
import { provideComponents } from '@grafana/assistant';
import MyEntityMention from './components/MyEntityMention';
import MyCustomChart from './components/MyCustomChart';

// Register components for all pages
const setComponents = provideComponents(
  `
  - When mentioning entities, use the myapp_MyEntityMention component: <myapp_MyEntityMention name={'entity-name'} type={'entity-type'} />
  - For charts, use the myapp_MyCustomChart component: <myapp_MyCustomChart data={chartData} title={'Chart Title'} />
  - Custom components must never be wrapped in code blocks.
  `,
  'myapp',
  {
    MyEntityMention,
    MyCustomChart,
  }
);

// Register components only for dashboard pages
const setDashboardComponents = provideComponents(
  'Use components to enhance dashboard analysis.',
  'dashboard',
  { MyEntityMention },
  '/d/*'
);

// Cleanup when done
setComponents.unregister();
```

### Questions System

The questions system allows external parties to provide sample prompts with optional context for specific pages. It reuses the existing page context infrastructure but presents a simpler interface focused on questions.

#### `provideQuestions(urlPattern: string | RegExp, initialQuestions: Question[]): ((questions: Question[]) => void) & { unregister: () => void }`

Registers questions for specific pages based on URL patterns. Returns a setter function to update the questions dynamically.

**Parameters:**

- `urlPattern` - URL pattern (string with wildcards or RegExp) to match against page URLs
- `initialQuestions` - Initial array of `Question` to provide when the pattern matches

**Returns:** A setter function to update the questions, with an `unregister` method attached for cleanup

**Examples:**

```typescript
// Register questions for dashboard pages
const setQuestions = provideQuestions('/d/*', [
  {
    prompt: 'What metrics are available in this dashboard?',
    context: [], // Optional context items created with `createAssistantContextItem`
  },
  {
    prompt: 'How can I optimize the queries in this dashboard?',
    context: [
      createAssistantContextItem('dashboard', {
        dashboardUid: 'dashboard-uid',
        dashboardTitle: 'System Overview',
        folderUid: 'folder-uid',
        folderTitle: 'Production',
      }),
    ],
  },
]);

// Update questions dynamically
setQuestions([
  {
    prompt: 'What are the key insights from this dashboard?',
    context: [],
  },
]);

// Cleanup when done
setQuestions.unregister();
```

#### `useProvideQuestions(urlPattern: string | RegExp, initialQuestions?: Question[]): (questions: Question[]) => void`

React hook for providing questions that automatically cleans up on unmount. This is the recommended way to use questions in React components.

**Parameters:**

- `urlPattern` - URL pattern (string or RegExp) to match against page URLs
- `initialQuestions` - Initial array of questions to provide when the pattern matches (defaults to empty array)

**Returns:** A setter function to update the questions

**Example:**

```typescript
function DashboardComponent() {
  const setQuestions = useProvideQuestions('/d/*', [
    {
      prompt: "What does this dashboard show?",
    },
    {
      prompt: "How can I improve this dashboard?",
    }
  ]);

  return <div>Dashboard</div>;
}
```

#### `useQuestions(): Question[]`

React hook to get all questions that match the current URL. This filters the page context to only return question-type items.

**Returns:** Array of questions from all matching registrations

**Example:**

```typescript
function AssistantComponent() {
  const questions = useQuestions();

  return (
    <div>
      <h3>Suggested Questions:</h3>
      {questions.map((question, index) => (
        <div key={index}>
          <p>{question.prompt}</p>
          {question.context && question.context.length > 0 && (
            <p>Has additional context</p>
          )}
        </div>
      ))}
    </div>
  );
}
```

### Sidebar Types

#### `OpenAssistantProps`

```typescript
type OpenAssistantProps = {
  prompt?: string;
  context?: ChatContextItem[];
  autoSend?: boolean;
};
```

Configuration object for opening the assistant.

- `prompt` - Optional initial prompt to display
- `context` - Optional context items to provide
- `autoSend` - Optional flag to automatically send the initial prompt

#### `ChatContextItem`

```typescript
type ChatContextItem = {
  node: TreeNode;
  occurrences: string[];
};
```

A context item that provides structured information to the assistant.

### Page Context Types

#### `PageContextRegistration`

```typescript
interface PageContextRegistration {
  id: string;
  urlPattern: string | RegExp;
  context: ChatContextItem[];
}
```

Represents a registered page context mapping.

### Questions Types

#### `Question`

```typescript
interface Question {
  prompt: string; // The sample prompt/question
  context?: ChatContextItem[]; // Optional context items
}
```

Represents a question with an optional prompt and context.

#### `QuestionRegistration`

```typescript
interface QuestionRegistration {
  id: string;
  urlPattern: string | RegExp;
  questions: Question[];
}
```

Represents a registered questions mapping.

### Context Types

#### Datasource Context Parameters

```typescript
interface CreateDatasourceContextParams {
  datasourceUid: string;
  title?: string;
  icon?: IconName;
  hidden?: boolean; // If true, the context item will not be shown in the context pills
}
```

#### Dashboard Context Parameters

```typescript
interface CreateDashboardContextParams {
  dashboardUid: string;
  dashboardTitle: string;
  folderUid?: string;
  folderTitle?: string;
  title?: string;
  icon?: IconName;
  hidden?: boolean; // If true, the context item will not be shown in the context pills
}
```

#### Folder Context Parameters

```typescript
interface CreateFolderContextParams {
  folderUid: string;
  folderTitle: string;
  title?: string;
  icon?: IconName;
  hidden?: boolean; // If true, the context item will not be shown in the context pills
}
```

#### Label Name Context Parameters

```typescript
interface CreateLabelNameContextParams {
  datasourceUid: string;
  datasourceType: string;
  labelName: string;
  title?: string;
  icon?: IconName;
  hidden?: boolean; // If true, the context item will not be shown in the context pills
}
```

#### Label Value Context Parameters

```typescript
interface CreateLabelValueContextParams {
  datasourceUid: string;
  datasourceType: string;
  labelName: string;
  labelValue: string;
  title?: string;
  icon?: IconName;
  hidden?: boolean; // If true, the context item will not be shown in the context pills
}
```

#### Structured Data Context Parameters

```typescript
interface StructuredNodeDataParams {
  data: Record<string, any>;
  title?: string;
  icon?: IconName;
  hidden?: boolean; // If true, the context item will not be shown in the context pills
}
```

#### Component Context Parameters

```typescript
interface CreateComponentContextParams {
  components: Record<string, React.ComponentType<any>>;
  prompt: string;
  namespace?: string; // Optional: defaults to 'components' if not provided
  title?: string;
  icon?: IconName;
  hidden?: boolean; // If true, the context item will not be shown in the context pills
}
```

#### Generic Context Parameters

```typescript
interface NodeDataParams {
  id: string;
  text?: string;
  title?: string;
  icon?: IconName;
  hidden?: boolean; // If true, the context item will not be shown in the context pills
}
```

### Function Extension Types

#### `CallbackFunction`

```typescript
type CallbackFunction = () => FunctionNamespace[];
```

A function that returns an array of function namespaces.

#### `FunctionImplementation`

```typescript
type FunctionImplementation = (...args: any[]) => any;
```

A type for function implementations that can be exposed to the assistant.

#### `NamedFunctions`

```typescript
type NamedFunctions = Record<string, FunctionImplementation>;
```

A record of function names to their implementations.

#### `FunctionNamespace`

```typescript
type FunctionNamespace = {
  namespace: string;
  functions: NamedFunctions;
};
```

Groups functions under a namespace.

### Utility Functions

#### `newFunctionNamespace(namespace: string, functions: NamedFunctions): FunctionNamespace`

Creates a new function namespace.

**Parameters:**

- `namespace: string` - The namespace identifier
- `functions: NamedFunctions` - The functions to include in the namespace

#### `getExposeAssistantFunctionsConfig(namespaces: FunctionNamespace[]): PluginExtensionAddedFunctionConfig`

Creates a plugin extension configuration for exposing functions to the assistant.

**Parameters:**

- `namespaces: FunctionNamespace[]` - Array of function namespaces to expose

### Constants

#### `CALLBACK_EXTENSION_POINT`

```typescript
const CALLBACK_EXTENSION_POINT = 'grafana-assistant-app/callback/v0-alpha';
```

The extension point ID for registering assistant functions.

### Additional Exported Types

The package also exports the following types for advanced use cases:

```typescript
// Data types for context items
export type DashboardNodeData;
export type DatasourceNodeData;
export type FolderNodeData;
export type LabelNameNodeData;
export type LabelValueNodeData;
export type StructuredNodeData;
export type TreeNode;
export type ItemDataType;

// Component types
export type ComponentImplementation;
export type NamedComponents;
```


### Plugin filesystems (experimental)

Extension manifests can provide `filesystem: AssistantFilesystemContribution` to
mount live text/JSON files at `/plugins/<registered-plugin-id>/`. Required callbacks
are `read`, `list`, and `metadata`; optional `create` and conditional `write` enable
mutations. Each receives the current Grafana namespace; the owning backend must
authorize every operation. An optional `agentGuide` is demand-loaded as the mount's
read-only `AGENT.md`.

Registered artifact kinds can render mounted files in the canvas. Ordinary files
remain tool-accessible. Reads and listings never attach artifacts. Refer to the
[filesystem contract and example](../../../docs/sources/reference/plugin-extensions.md#expose-a-filesystem).

## License

Apache-2.0
