@grafana/assistant 0.1.9 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +202 -126
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/investigations.d.ts +41 -0
- package/dist/investigations.d.ts.map +1 -0
- package/dist/limits/index.d.ts +73 -0
- package/dist/limits/index.d.ts.map +1 -0
- package/dist/sidebar.d.ts +3 -2
- package/dist/sidebar.d.ts.map +1 -1
- package/dist/terms/index.d.ts +1 -15
- package/dist/terms/index.d.ts.map +1 -1
- package/dist/terms/settings.gen.d.ts +138 -0
- package/dist/terms/settings.gen.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -36,7 +36,7 @@ function MyComponent() {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
return (
|
|
39
|
-
<button onClick={() => openAssistant({
|
|
39
|
+
<button onClick={() => openAssistant({
|
|
40
40
|
origin: 'grafana/panel-data-analyzer',
|
|
41
41
|
prompt: 'Help me analyze data' })}>
|
|
42
42
|
Open Assistant
|
|
@@ -54,7 +54,11 @@ import { openAssistant, closeAssistant } from '@grafana/assistant';
|
|
|
54
54
|
openAssistant({ origin: 'grafana/some-feature', prompt: 'Show me CPU usage over the last hour' });
|
|
55
55
|
|
|
56
56
|
// Open the assistant with initial prompt and autoSend set to false
|
|
57
|
-
openAssistant({
|
|
57
|
+
openAssistant({
|
|
58
|
+
origin: 'grafana-datasources/prometheus/some-feature',
|
|
59
|
+
prompt: 'Help me analyze data',
|
|
60
|
+
autoSend: false,
|
|
61
|
+
});
|
|
58
62
|
|
|
59
63
|
// Open the assistant without an initial prompt
|
|
60
64
|
openAssistant({
|
|
@@ -79,14 +83,14 @@ const dashboardContext = createAssistantContextItem('dashboard', {
|
|
|
79
83
|
dashboardUid: 'dashboard-uid',
|
|
80
84
|
dashboardTitle: 'System Overview',
|
|
81
85
|
folderUid: 'folder-uid',
|
|
82
|
-
folderTitle: 'Production'
|
|
86
|
+
folderTitle: 'Production',
|
|
83
87
|
});
|
|
84
88
|
|
|
85
89
|
// Open the assistant with initial prompt and context
|
|
86
|
-
openAssistant({
|
|
90
|
+
openAssistant({
|
|
87
91
|
origin: 'grafana/dashboard-page',
|
|
88
92
|
prompt: 'Analyze the CPU metrics from this dashboard',
|
|
89
|
-
context: [datasourceContext, dashboardContext]
|
|
93
|
+
context: [datasourceContext, dashboardContext],
|
|
90
94
|
});
|
|
91
95
|
|
|
92
96
|
// Create hidden context for system instructions (won't show in UI pills)
|
|
@@ -95,14 +99,14 @@ const systemInstructions = createAssistantContextItem('structured', {
|
|
|
95
99
|
title: 'System Instructions',
|
|
96
100
|
data: {
|
|
97
101
|
instructions: 'When analyzing metrics, always consider seasonal patterns and anomalies',
|
|
98
|
-
preferences: 'Use clear, technical language and provide actionable insights'
|
|
99
|
-
}
|
|
102
|
+
preferences: 'Use clear, technical language and provide actionable insights',
|
|
103
|
+
},
|
|
100
104
|
});
|
|
101
105
|
|
|
102
106
|
// Include hidden context with visible context
|
|
103
|
-
openAssistant({
|
|
107
|
+
openAssistant({
|
|
104
108
|
prompt: 'What are the trends in this data?',
|
|
105
|
-
context: [datasourceContext, systemInstructions]
|
|
109
|
+
context: [datasourceContext, systemInstructions],
|
|
106
110
|
});
|
|
107
111
|
```
|
|
108
112
|
|
|
@@ -119,8 +123,8 @@ const dashboardContext = createAssistantContextItem('structured', {
|
|
|
119
123
|
name: 'Dashboard Context',
|
|
120
124
|
pageType: 'dashboard',
|
|
121
125
|
capabilities: ['view', 'edit', 'share'],
|
|
122
|
-
help: 'Use the assistant to analyze dashboard data, create queries, or troubleshoot issues.'
|
|
123
|
-
}
|
|
126
|
+
help: 'Use the assistant to analyze dashboard data, create queries, or troubleshoot issues.',
|
|
127
|
+
},
|
|
124
128
|
});
|
|
125
129
|
|
|
126
130
|
// Register context for all dashboard pages and get a setter function
|
|
@@ -133,9 +137,9 @@ setContext([
|
|
|
133
137
|
data: {
|
|
134
138
|
name: 'Panel Context',
|
|
135
139
|
selectedPanel: 'cpu-usage-panel',
|
|
136
|
-
panelType: 'graph'
|
|
137
|
-
}
|
|
138
|
-
})
|
|
140
|
+
panelType: 'graph',
|
|
141
|
+
},
|
|
142
|
+
}),
|
|
139
143
|
]);
|
|
140
144
|
|
|
141
145
|
// Clean up when no longer needed
|
|
@@ -147,7 +151,8 @@ providePageContext('/explore', [
|
|
|
147
151
|
hidden: true,
|
|
148
152
|
title: 'Additional instructions',
|
|
149
153
|
data: {
|
|
150
|
-
'System Instructions':
|
|
154
|
+
'System Instructions':
|
|
155
|
+
'When working with explore queries, always validate the time range and check for data availability. Suggest using recording rules for expensive queries.',
|
|
151
156
|
},
|
|
152
157
|
}),
|
|
153
158
|
]);
|
|
@@ -197,9 +202,9 @@ import { usePageContext } from '@grafana/assistant';
|
|
|
197
202
|
|
|
198
203
|
function MyComponent() {
|
|
199
204
|
const pageContext = usePageContext();
|
|
200
|
-
|
|
205
|
+
|
|
201
206
|
console.log('Active context items:', pageContext);
|
|
202
|
-
|
|
207
|
+
|
|
203
208
|
return <div>Page has {pageContext.length} context items</div>;
|
|
204
209
|
}
|
|
205
210
|
```
|
|
@@ -215,7 +220,7 @@ import { createAssistantContextItem } from '@grafana/assistant';
|
|
|
215
220
|
const datasourceContext = createAssistantContextItem('datasource', {
|
|
216
221
|
datasourceUid: 'prom-123',
|
|
217
222
|
title: 'Main Datasource', // Optional custom title
|
|
218
|
-
icon: 'database' // Optional custom icon
|
|
223
|
+
icon: 'database', // Optional custom icon
|
|
219
224
|
});
|
|
220
225
|
|
|
221
226
|
// Create a dashboard context
|
|
@@ -223,14 +228,14 @@ const dashboardContext = createAssistantContextItem('dashboard', {
|
|
|
223
228
|
dashboardUid: 'dash-456',
|
|
224
229
|
dashboardTitle: 'Application Overview',
|
|
225
230
|
folderUid: 'folder-789',
|
|
226
|
-
folderTitle: 'Production Dashboards'
|
|
231
|
+
folderTitle: 'Production Dashboards',
|
|
227
232
|
});
|
|
228
233
|
|
|
229
234
|
// Create a label context
|
|
230
235
|
const labelContext = createAssistantContextItem('label_name', {
|
|
231
236
|
datasourceUid: 'prom-123',
|
|
232
237
|
datasourceType: 'prometheus',
|
|
233
|
-
labelName: 'service'
|
|
238
|
+
labelName: 'service',
|
|
234
239
|
});
|
|
235
240
|
|
|
236
241
|
// Create a label value context
|
|
@@ -238,13 +243,13 @@ const labelValueContext = createAssistantContextItem('label_value', {
|
|
|
238
243
|
datasourceUid: 'prom-123',
|
|
239
244
|
datasourceType: 'prometheus',
|
|
240
245
|
labelName: 'service',
|
|
241
|
-
labelValue: 'api-server'
|
|
246
|
+
labelValue: 'api-server',
|
|
242
247
|
});
|
|
243
248
|
|
|
244
249
|
// Create a folder context
|
|
245
250
|
const folderContext = createAssistantContextItem('dashboard_folder', {
|
|
246
251
|
folderUid: 'folder-789',
|
|
247
|
-
folderTitle: 'Production Dashboards'
|
|
252
|
+
folderTitle: 'Production Dashboards',
|
|
248
253
|
});
|
|
249
254
|
|
|
250
255
|
// Create structured data context
|
|
@@ -252,14 +257,14 @@ const structuredContext = createAssistantContextItem('structured', {
|
|
|
252
257
|
data: {
|
|
253
258
|
name: 'Custom Data',
|
|
254
259
|
metrics: ['cpu_usage', 'memory_usage'],
|
|
255
|
-
threshold: 80
|
|
256
|
-
}
|
|
260
|
+
threshold: 80,
|
|
261
|
+
},
|
|
257
262
|
});
|
|
258
263
|
|
|
259
264
|
// Create generic context
|
|
260
265
|
const genericContext = createAssistantContextItem('unknown', {
|
|
261
266
|
id: 'my-context',
|
|
262
|
-
text: 'Some additional context information'
|
|
267
|
+
text: 'Some additional context information',
|
|
263
268
|
});
|
|
264
269
|
|
|
265
270
|
// Create component context for custom React components
|
|
@@ -278,12 +283,12 @@ const componentContext = createAssistantContextItem('component', {
|
|
|
278
283
|
|
|
279
284
|
// Create hidden context (useful for system instructions)
|
|
280
285
|
const hiddenInstructions = createAssistantContextItem('structured', {
|
|
281
|
-
hidden: true,
|
|
286
|
+
hidden: true, // Won't be shown in UI pills but will be sent to the assistant
|
|
282
287
|
title: 'Page-specific instructions',
|
|
283
288
|
data: {
|
|
284
289
|
instructions: 'Always provide step-by-step explanations',
|
|
285
|
-
context: 'This is a complex system with multiple dependencies'
|
|
286
|
-
}
|
|
290
|
+
context: 'This is a complex system with multiple dependencies',
|
|
291
|
+
},
|
|
287
292
|
});
|
|
288
293
|
```
|
|
289
294
|
|
|
@@ -357,6 +362,7 @@ export function configureAssistantContext() {
|
|
|
357
362
|
- **No Code Blocks**: Custom components should never be wrapped in markdown code blocks
|
|
358
363
|
|
|
359
364
|
**Example Component Implementation:**
|
|
365
|
+
|
|
360
366
|
```typescript
|
|
361
367
|
interface MyEntityMentionProps {
|
|
362
368
|
name: string;
|
|
@@ -369,8 +375,8 @@ interface MyEntityMentionProps {
|
|
|
369
375
|
|
|
370
376
|
const MyEntityMention: React.FC<MyEntityMentionProps> = ({ name, type, env, site, namespace, properties }) => {
|
|
371
377
|
return (
|
|
372
|
-
<span
|
|
373
|
-
className="entity-mention"
|
|
378
|
+
<span
|
|
379
|
+
className="entity-mention"
|
|
374
380
|
data-entity-type={type}
|
|
375
381
|
title={`${type}: ${name}`}
|
|
376
382
|
>
|
|
@@ -402,14 +408,14 @@ providePageContext('/explore', [
|
|
|
402
408
|
instructions: [
|
|
403
409
|
'Always validate time ranges before running queries',
|
|
404
410
|
'Suggest using recording rules for expensive queries',
|
|
405
|
-
'Warn about queries that might impact performance'
|
|
411
|
+
'Warn about queries that might impact performance',
|
|
406
412
|
],
|
|
407
413
|
queryLimits: {
|
|
408
414
|
maxTimeRange: '24h',
|
|
409
|
-
suggestedSampleInterval: '1m'
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
})
|
|
415
|
+
suggestedSampleInterval: '1m',
|
|
416
|
+
},
|
|
417
|
+
},
|
|
418
|
+
}),
|
|
413
419
|
]);
|
|
414
420
|
|
|
415
421
|
// Add dashboard-specific context
|
|
@@ -419,21 +425,21 @@ providePageContext('/d/*', [
|
|
|
419
425
|
title: 'Dashboard Instructions',
|
|
420
426
|
data: {
|
|
421
427
|
bestPractices: 'Encourage users to use template variables for flexibility',
|
|
422
|
-
performance: 'Suggest panel caching for slow queries'
|
|
423
|
-
}
|
|
424
|
-
})
|
|
428
|
+
performance: 'Suggest panel caching for slow queries',
|
|
429
|
+
},
|
|
430
|
+
}),
|
|
425
431
|
]);
|
|
426
432
|
```
|
|
427
433
|
|
|
428
434
|
### Exposing Functions to the Assistant
|
|
429
435
|
|
|
430
436
|
```typescript
|
|
431
|
-
import {
|
|
432
|
-
getExposeAssistantFunctionsConfig,
|
|
437
|
+
import {
|
|
438
|
+
getExposeAssistantFunctionsConfig,
|
|
433
439
|
newFunctionNamespace,
|
|
434
440
|
FunctionImplementation,
|
|
435
441
|
FunctionNamespace,
|
|
436
|
-
NamedFunctions
|
|
442
|
+
NamedFunctions,
|
|
437
443
|
} from '@grafana/assistant';
|
|
438
444
|
|
|
439
445
|
// Define your functions
|
|
@@ -441,19 +447,17 @@ const myFunctions: NamedFunctions = {
|
|
|
441
447
|
getMetrics: (datasource: string) => {
|
|
442
448
|
return ['cpu_usage', 'memory_usage', 'disk_io'];
|
|
443
449
|
},
|
|
444
|
-
|
|
450
|
+
|
|
445
451
|
calculateAverage: (values: number[]) => {
|
|
446
452
|
return values.reduce((a, b) => a + b, 0) / values.length;
|
|
447
|
-
}
|
|
453
|
+
},
|
|
448
454
|
};
|
|
449
455
|
|
|
450
456
|
// Create a namespace for your functions
|
|
451
457
|
const namespace = newFunctionNamespace('myPlugin', myFunctions);
|
|
452
458
|
|
|
453
459
|
// Export the configuration for your plugin
|
|
454
|
-
export const getExtensionConfigs = () => [
|
|
455
|
-
getExposeAssistantFunctionsConfig([namespace])
|
|
456
|
-
];
|
|
460
|
+
export const getExtensionConfigs = () => [getExposeAssistantFunctionsConfig([namespace])];
|
|
457
461
|
```
|
|
458
462
|
|
|
459
463
|
### Using the OpenAssistantButton Component
|
|
@@ -472,14 +476,14 @@ function MyDashboard() {
|
|
|
472
476
|
return (
|
|
473
477
|
<div>
|
|
474
478
|
<h1>Dashboard</h1>
|
|
475
|
-
|
|
479
|
+
|
|
476
480
|
{/* Basic usage */}
|
|
477
481
|
<OpenAssistantButton
|
|
478
482
|
prompt="Help me analyze the CPU usage trends in this dashboard"
|
|
479
483
|
context={[dashboardContext]}
|
|
480
484
|
origin="grafana/dashboard-menu"
|
|
481
485
|
/>
|
|
482
|
-
|
|
486
|
+
|
|
483
487
|
{/* Icon-only button */}
|
|
484
488
|
<OpenAssistantButton
|
|
485
489
|
prompt="Show me optimization suggestions"
|
|
@@ -488,7 +492,7 @@ function MyDashboard() {
|
|
|
488
492
|
size="md"
|
|
489
493
|
origin="grafana-datasources/loki/query-editor"
|
|
490
494
|
/>
|
|
491
|
-
|
|
495
|
+
|
|
492
496
|
{/* Button without auto-send */}
|
|
493
497
|
<OpenAssistantButton
|
|
494
498
|
prompt="What patterns do you see in the data?"
|
|
@@ -511,20 +515,80 @@ function MyDashboard() {
|
|
|
511
515
|
A React hook that provides assistant availability status and control functions.
|
|
512
516
|
|
|
513
517
|
**Returns:** An object with the following properties:
|
|
518
|
+
|
|
514
519
|
- `isAvailable: boolean` - Whether the Assistant is available
|
|
515
520
|
- `openAssistant?: (props: OpenAssistantProps) => void` - Function to open the assistant (undefined if not available)
|
|
516
521
|
- `closeAssistant?: () => void` - Function to close the assistant (undefined if not available)
|
|
517
522
|
- `toggleAssistant?: (props: OpenAssistantProps) => void` - Function to toggle the assistant (undefined if not available)
|
|
518
523
|
|
|
519
|
-
#### `useTerms(): { accepted: boolean; loading: boolean; error: string | null
|
|
524
|
+
#### `useTerms(): { accepted: boolean; loading: boolean; error: string | null }`
|
|
520
525
|
|
|
521
526
|
A React hook to check terms and conditions acceptance status. Automatically fetches terms data on mount and provides loading/error states.
|
|
522
527
|
|
|
523
528
|
**Returns:** An object with the following properties:
|
|
529
|
+
|
|
524
530
|
- `accepted: boolean` - Whether terms are accepted and up-to-date
|
|
525
531
|
- `loading: boolean` - Whether the check is in progress
|
|
526
532
|
- `error: string | null` - Error message if the check failed, null otherwise
|
|
527
|
-
|
|
533
|
+
|
|
534
|
+
#### `useLimits(): LimitsHookResult`
|
|
535
|
+
|
|
536
|
+
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.
|
|
537
|
+
|
|
538
|
+
**Returns:** An object with the following properties:
|
|
539
|
+
|
|
540
|
+
- `count: number` - Current prompt count for the month
|
|
541
|
+
- `limit: number` - Monthly prompt limit (0 = unlimited)
|
|
542
|
+
- `month: string` - Current month in YYYY-MM format
|
|
543
|
+
- `isLimitReached: boolean` - Whether the limit has been reached
|
|
544
|
+
- `loading: boolean` - Whether the initial fetch is in progress
|
|
545
|
+
- `error: string | null` - Error message if the fetch failed, null otherwise
|
|
546
|
+
- `refetch: () => void` - Function to manually refetch limits
|
|
547
|
+
|
|
548
|
+
**Example:**
|
|
549
|
+
|
|
550
|
+
```typescript
|
|
551
|
+
import { useLimits } from '@grafana/assistant';
|
|
552
|
+
|
|
553
|
+
function MyComponent() {
|
|
554
|
+
const { count, limit, isLimitReached, loading, error } = useLimits();
|
|
555
|
+
|
|
556
|
+
if (loading) return <div>Loading...</div>;
|
|
557
|
+
if (error) return <div>Error: {error}</div>;
|
|
558
|
+
if (isLimitReached) {
|
|
559
|
+
return <div>Monthly limit reached ({count}/{limit})</div>;
|
|
560
|
+
}
|
|
561
|
+
return <div>Prompts used: {count}/{limit || 'unlimited'}</div>;
|
|
562
|
+
}
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
#### `checkLimits(): Promise<LimitsData>`
|
|
566
|
+
|
|
567
|
+
An async function to check user prompt limits. Returns a promise with the limits data.
|
|
568
|
+
|
|
569
|
+
**Returns:** A promise that resolves to an object with:
|
|
570
|
+
|
|
571
|
+
- `count: number` - Current prompt count for the month
|
|
572
|
+
- `limit: number` - Monthly prompt limit (0 = unlimited)
|
|
573
|
+
- `month: string` - Current month in YYYY-MM format
|
|
574
|
+
- `isLimitReached: boolean` - Whether the limit has been reached
|
|
575
|
+
|
|
576
|
+
**Example:**
|
|
577
|
+
|
|
578
|
+
```typescript
|
|
579
|
+
import { checkLimits } from '@grafana/assistant';
|
|
580
|
+
|
|
581
|
+
async function checkUserLimits() {
|
|
582
|
+
try {
|
|
583
|
+
const { isLimitReached, count, limit } = await checkLimits();
|
|
584
|
+
if (isLimitReached) {
|
|
585
|
+
console.log(`Limit reached: ${count}/${limit}`);
|
|
586
|
+
}
|
|
587
|
+
} catch (error) {
|
|
588
|
+
console.error('Failed to check limits:', error);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
```
|
|
528
592
|
|
|
529
593
|
#### `usePageContext(): ChatContextItem[]`
|
|
530
594
|
|
|
@@ -537,6 +601,7 @@ A React hook that returns all active context items for the current page based on
|
|
|
537
601
|
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.
|
|
538
602
|
|
|
539
603
|
**Parameters:**
|
|
604
|
+
|
|
540
605
|
- `urlPattern` - URL pattern (string or RegExp) to match against page URLs
|
|
541
606
|
- `initialContext` - Initial array of ChatContextItem to provide when the pattern matches (defaults to empty array)
|
|
542
607
|
|
|
@@ -549,15 +614,16 @@ A React hook that provides all components available for the current page. This h
|
|
|
549
614
|
**Returns:** Object containing all components available for the current page, keyed by `namespace_componentName` format.
|
|
550
615
|
|
|
551
616
|
**Example:**
|
|
617
|
+
|
|
552
618
|
```typescript
|
|
553
619
|
import { usePageComponents } from '@grafana/assistant';
|
|
554
620
|
|
|
555
621
|
function MyAssistantComponent() {
|
|
556
622
|
const components = usePageComponents();
|
|
557
|
-
|
|
623
|
+
|
|
558
624
|
// Access components registered with namespace 'myapp' and component name 'EntityMention'
|
|
559
625
|
const EntityMentionComponent = components['myapp_EntityMention'];
|
|
560
|
-
|
|
626
|
+
|
|
561
627
|
return (
|
|
562
628
|
<div>
|
|
563
629
|
Available components: {Object.keys(components).join(', ')}
|
|
@@ -573,8 +639,9 @@ function MyAssistantComponent() {
|
|
|
573
639
|
A pre-built React component that renders a button to open the Grafana Assistant with configurable prompt and context.
|
|
574
640
|
|
|
575
641
|
**Parameters:**
|
|
642
|
+
|
|
576
643
|
- `prompt: string` - **Required** The initial prompt to display in the assistant
|
|
577
|
-
- `origin: string` - **Required**
|
|
644
|
+
- `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'
|
|
578
645
|
- `context?: ChatContextItem[]` - Optional context items created with `createAssistantContextItem`
|
|
579
646
|
- `autoSend?: boolean` - Whether to automatically send the initial prompt (defaults to `true`)
|
|
580
647
|
- `title?: string` - Text to display on the button (defaults to `'Analyze with Assistant'`)
|
|
@@ -706,6 +773,7 @@ export function PanelEditor() {
|
|
|
706
773
|
```
|
|
707
774
|
|
|
708
775
|
**Key Features:**
|
|
776
|
+
|
|
709
777
|
- `isGenerating` - Boolean indicating generation is in progress
|
|
710
778
|
- `content` - Current streaming content (updates in real-time)
|
|
711
779
|
- `generate(options)` - Start a new generation
|
|
@@ -738,7 +806,7 @@ export const createWeatherTool = (onToolComplete: (data: WeatherData) => void) =
|
|
|
738
806
|
return createTool(
|
|
739
807
|
async (input: z.infer<typeof weatherSchema>): Promise<ToolOutput> => {
|
|
740
808
|
const { city } = input;
|
|
741
|
-
|
|
809
|
+
|
|
742
810
|
// Fetch weather data (simplified example)
|
|
743
811
|
const weatherData: WeatherData = {
|
|
744
812
|
city,
|
|
@@ -746,10 +814,10 @@ export const createWeatherTool = (onToolComplete: (data: WeatherData) => void) =
|
|
|
746
814
|
conditions: 'Sunny',
|
|
747
815
|
humidity: 65,
|
|
748
816
|
};
|
|
749
|
-
|
|
817
|
+
|
|
750
818
|
// Call the callback to pass artifact data
|
|
751
819
|
onToolComplete(weatherData);
|
|
752
|
-
|
|
820
|
+
|
|
753
821
|
// Return tuple: [text response, artifact]
|
|
754
822
|
return [
|
|
755
823
|
`Current weather in ${city}: ${weatherData.temperature.celsius}°C, ${weatherData.conditions}`,
|
|
@@ -794,6 +862,43 @@ export function WeatherComponent() {
|
|
|
794
862
|
}
|
|
795
863
|
```
|
|
796
864
|
|
|
865
|
+
### Investigation Events
|
|
866
|
+
|
|
867
|
+
Subscribe to investigation lifecycle events to react when investigations are created.
|
|
868
|
+
|
|
869
|
+
#### `onInvestigationCreated(callback: (payload: InvestigationCreatedPayload) => void): () => void`
|
|
870
|
+
|
|
871
|
+
Subscribe to investigation created events.
|
|
872
|
+
|
|
873
|
+
**Parameters:**
|
|
874
|
+
|
|
875
|
+
- `callback` - Function called when an investigation is created, receives `{ investigationId: string }`
|
|
876
|
+
|
|
877
|
+
**Returns:** An unsubscribe function to stop listening for events
|
|
878
|
+
|
|
879
|
+
**Example:**
|
|
880
|
+
|
|
881
|
+
```typescript
|
|
882
|
+
import { onInvestigationCreated } from '@grafana/assistant';
|
|
883
|
+
|
|
884
|
+
// Subscribe to investigation creation events
|
|
885
|
+
const unsubscribe = onInvestigationCreated((payload) => {
|
|
886
|
+
console.log('Investigation created:', payload.investigationId);
|
|
887
|
+
// Navigate to the investigation, update UI, etc.
|
|
888
|
+
});
|
|
889
|
+
|
|
890
|
+
// Later, when you want to stop listening
|
|
891
|
+
unsubscribe();
|
|
892
|
+
```
|
|
893
|
+
|
|
894
|
+
#### `emitInvestigationCreated(investigationId: string): void`
|
|
895
|
+
|
|
896
|
+
Emit an investigation created event. This is typically used internally by the Assistant but can be called by external integrations if needed.
|
|
897
|
+
|
|
898
|
+
**Parameters:**
|
|
899
|
+
|
|
900
|
+
- `investigationId` - The ID of the newly created investigation
|
|
901
|
+
|
|
797
902
|
### Availability Functions
|
|
798
903
|
|
|
799
904
|
#### `isAssistantAvailable(): Observable<boolean>`
|
|
@@ -804,9 +909,12 @@ Checks if the assistant plugin is available.
|
|
|
804
909
|
|
|
805
910
|
### Terms and Conditions Functions
|
|
806
911
|
|
|
912
|
+
> **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.
|
|
913
|
+
|
|
807
914
|
#### `checkTerms(): Promise<boolean>`
|
|
808
915
|
|
|
809
916
|
Checks whether terms and conditions are accepted and up-to-date. This function verifies that:
|
|
917
|
+
|
|
810
918
|
1. Terms have been accepted
|
|
811
919
|
2. The accepted version matches or exceeds the current version
|
|
812
920
|
|
|
@@ -815,6 +923,7 @@ Checks whether terms and conditions are accepted and up-to-date. This function v
|
|
|
815
923
|
**Throws:** Error if the API call fails.
|
|
816
924
|
|
|
817
925
|
**Example:**
|
|
926
|
+
|
|
818
927
|
```typescript
|
|
819
928
|
import { checkTerms } from '@grafana/assistant';
|
|
820
929
|
|
|
@@ -829,52 +938,6 @@ try {
|
|
|
829
938
|
}
|
|
830
939
|
```
|
|
831
940
|
|
|
832
|
-
#### `acceptTerms(): Promise<void>`
|
|
833
|
-
|
|
834
|
-
Accepts terms and conditions for the current tenant. This function sends a PUT request to mark the current terms version as accepted.
|
|
835
|
-
|
|
836
|
-
**Returns:** A promise that resolves when terms are successfully accepted.
|
|
837
|
-
|
|
838
|
-
**Throws:** Error if the API call fails.
|
|
839
|
-
|
|
840
|
-
**Example:**
|
|
841
|
-
|
|
842
|
-
```typescript
|
|
843
|
-
import { acceptTerms } from '@grafana/assistant';
|
|
844
|
-
|
|
845
|
-
try {
|
|
846
|
-
await acceptTerms();
|
|
847
|
-
console.log('Terms accepted successfully');
|
|
848
|
-
} catch (error) {
|
|
849
|
-
// Handle error
|
|
850
|
-
console.error('Failed to accept terms:', error);
|
|
851
|
-
}
|
|
852
|
-
```
|
|
853
|
-
|
|
854
|
-
**Example with `useTerms` hook:**
|
|
855
|
-
|
|
856
|
-
```typescript
|
|
857
|
-
import { useTerms } from '@grafana/assistant';
|
|
858
|
-
|
|
859
|
-
function TermsComponent() {
|
|
860
|
-
const { accepted, loading, error, acceptTerms } = useTerms();
|
|
861
|
-
|
|
862
|
-
if (loading) return <div>Loading...</div>;
|
|
863
|
-
if (error) return <div>Error: {error}</div>;
|
|
864
|
-
|
|
865
|
-
if (!accepted) {
|
|
866
|
-
return (
|
|
867
|
-
<div>
|
|
868
|
-
<p>Please accept the terms and conditions</p>
|
|
869
|
-
<button onClick={() => acceptTerms()}>Accept Terms</button>
|
|
870
|
-
</div>
|
|
871
|
-
);
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
return <div>Terms accepted</div>;
|
|
875
|
-
}
|
|
876
|
-
```
|
|
877
|
-
|
|
878
941
|
### Sidebar Functions
|
|
879
942
|
|
|
880
943
|
#### `openAssistant(props: OpenAssistantProps): void`
|
|
@@ -882,6 +945,7 @@ function TermsComponent() {
|
|
|
882
945
|
Opens the Grafana Assistant sidebar.
|
|
883
946
|
|
|
884
947
|
**Parameters:**
|
|
948
|
+
|
|
885
949
|
- `props: OpenAssistantProps` - Configuration object
|
|
886
950
|
- `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'
|
|
887
951
|
- `prompt?: string` - Optional initial prompt to display in the assistant
|
|
@@ -899,9 +963,10 @@ Closes the Grafana Assistant sidebar.
|
|
|
899
963
|
Creates a context item that can be passed to the assistant to provide additional information.
|
|
900
964
|
|
|
901
965
|
**Parameters:**
|
|
966
|
+
|
|
902
967
|
- `type` - The type of context to create. Supported types:
|
|
903
968
|
- `'datasource'` - Datasource context
|
|
904
|
-
- `'dashboard'` - Dashboard context
|
|
969
|
+
- `'dashboard'` - Dashboard context
|
|
905
970
|
- `'dashboard_folder'` - Folder context
|
|
906
971
|
- `'label_name'` - Label name context
|
|
907
972
|
- `'label_value'` - Label value context
|
|
@@ -918,6 +983,7 @@ Creates a context item that can be passed to the assistant to provide additional
|
|
|
918
983
|
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.
|
|
919
984
|
|
|
920
985
|
**Parameters:**
|
|
986
|
+
|
|
921
987
|
- `urlPattern` - URL pattern (string with wildcards or RegExp) to match against page URLs
|
|
922
988
|
- String patterns support `*` (match any characters) and `**` (match any path segments)
|
|
923
989
|
- RegExp patterns provide full regex matching capabilities
|
|
@@ -926,12 +992,13 @@ Registers context items for specific pages based on URL patterns. Returns a sett
|
|
|
926
992
|
**Returns:** A setter function to update the context, with an `unregister` method attached for cleanup
|
|
927
993
|
|
|
928
994
|
**Examples:**
|
|
995
|
+
|
|
929
996
|
```typescript
|
|
930
997
|
// String patterns - returns setter function
|
|
931
998
|
const setDashboardContext = providePageContext('/d/*', initialContext); // Match any dashboard
|
|
932
999
|
const setExploreContext = providePageContext('/explore**', initialContext); // Match explore and subpaths
|
|
933
1000
|
|
|
934
|
-
// RegExp patterns
|
|
1001
|
+
// RegExp patterns
|
|
935
1002
|
const setSpecificContext = providePageContext(/^\/d\/[a-zA-Z0-9]+$/, initialContext); // Match specific dashboard format
|
|
936
1003
|
|
|
937
1004
|
// Update context dynamically
|
|
@@ -946,6 +1013,7 @@ setDashboardContext.unregister();
|
|
|
946
1013
|
A simplified function for providing components to the assistant. This is a convenience wrapper around `providePageContext` specifically for registering components.
|
|
947
1014
|
|
|
948
1015
|
**Parameters:**
|
|
1016
|
+
|
|
949
1017
|
- `prompt` - Instructions for the assistant on how and when to use these components
|
|
950
1018
|
- `namespace` - A unique identifier for your app's components (e.g., 'myapp', 'datasource')
|
|
951
1019
|
- `components` - A record of component names to their React component implementations
|
|
@@ -954,6 +1022,7 @@ A simplified function for providing components to the assistant. This is a conve
|
|
|
954
1022
|
**Returns:** A setter function to update the context, with an `unregister` method attached for cleanup
|
|
955
1023
|
|
|
956
1024
|
**Example:**
|
|
1025
|
+
|
|
957
1026
|
```typescript
|
|
958
1027
|
import { provideComponents } from '@grafana/assistant';
|
|
959
1028
|
import MyEntityMention from './components/MyEntityMention';
|
|
@@ -994,27 +1063,29 @@ The questions system allows external parties to provide sample prompts with opti
|
|
|
994
1063
|
Registers questions for specific pages based on URL patterns. Returns a setter function to update the questions dynamically.
|
|
995
1064
|
|
|
996
1065
|
**Parameters:**
|
|
1066
|
+
|
|
997
1067
|
- `urlPattern` - URL pattern (string with wildcards or RegExp) to match against page URLs
|
|
998
1068
|
- `initialQuestions` - Initial array of `Question` to provide when the pattern matches
|
|
999
1069
|
|
|
1000
1070
|
**Returns:** A setter function to update the questions, with an `unregister` method attached for cleanup
|
|
1001
1071
|
|
|
1002
1072
|
**Examples:**
|
|
1073
|
+
|
|
1003
1074
|
```typescript
|
|
1004
1075
|
// Register questions for dashboard pages
|
|
1005
|
-
const setQuestions = provideQuestions(
|
|
1076
|
+
const setQuestions = provideQuestions('/d/*', [
|
|
1006
1077
|
{
|
|
1007
|
-
prompt:
|
|
1078
|
+
prompt: 'What metrics are available in this dashboard?',
|
|
1008
1079
|
context: [], // Optional context items created with `createAssistantContextItem`
|
|
1009
1080
|
},
|
|
1010
1081
|
{
|
|
1011
|
-
prompt:
|
|
1082
|
+
prompt: 'How can I optimize the queries in this dashboard?',
|
|
1012
1083
|
context: [
|
|
1013
|
-
createAssistantContextItem(
|
|
1084
|
+
createAssistantContextItem('dashboard', {
|
|
1014
1085
|
dashboardUid: 'dashboard-uid',
|
|
1015
1086
|
dashboardTitle: 'System Overview',
|
|
1016
1087
|
folderUid: 'folder-uid',
|
|
1017
|
-
folderTitle: 'Production'
|
|
1088
|
+
folderTitle: 'Production',
|
|
1018
1089
|
}),
|
|
1019
1090
|
],
|
|
1020
1091
|
},
|
|
@@ -1023,9 +1094,9 @@ const setQuestions = provideQuestions("/d/*", [
|
|
|
1023
1094
|
// Update questions dynamically
|
|
1024
1095
|
setQuestions([
|
|
1025
1096
|
{
|
|
1026
|
-
prompt:
|
|
1027
|
-
context: []
|
|
1028
|
-
}
|
|
1097
|
+
prompt: 'What are the key insights from this dashboard?',
|
|
1098
|
+
context: [],
|
|
1099
|
+
},
|
|
1029
1100
|
]);
|
|
1030
1101
|
|
|
1031
1102
|
// Cleanup when done
|
|
@@ -1037,12 +1108,14 @@ setQuestions.unregister();
|
|
|
1037
1108
|
React hook for providing questions that automatically cleans up on unmount. This is the recommended way to use questions in React components.
|
|
1038
1109
|
|
|
1039
1110
|
**Parameters:**
|
|
1111
|
+
|
|
1040
1112
|
- `urlPattern` - URL pattern (string or RegExp) to match against page URLs
|
|
1041
1113
|
- `initialQuestions` - Initial array of questions to provide when the pattern matches (defaults to empty array)
|
|
1042
1114
|
|
|
1043
1115
|
**Returns:** A setter function to update the questions
|
|
1044
1116
|
|
|
1045
1117
|
**Example:**
|
|
1118
|
+
|
|
1046
1119
|
```typescript
|
|
1047
1120
|
function DashboardComponent() {
|
|
1048
1121
|
const setQuestions = useProvideQuestions('/d/*', [
|
|
@@ -1065,10 +1138,11 @@ React hook to get all questions that match the current URL. This filters the pag
|
|
|
1065
1138
|
**Returns:** Array of questions from all matching registrations
|
|
1066
1139
|
|
|
1067
1140
|
**Example:**
|
|
1141
|
+
|
|
1068
1142
|
```typescript
|
|
1069
1143
|
function AssistantComponent() {
|
|
1070
1144
|
const questions = useQuestions();
|
|
1071
|
-
|
|
1145
|
+
|
|
1072
1146
|
return (
|
|
1073
1147
|
<div>
|
|
1074
1148
|
<h3>Suggested Questions:</h3>
|
|
@@ -1134,7 +1208,7 @@ Represents a registered page context mapping.
|
|
|
1134
1208
|
|
|
1135
1209
|
```typescript
|
|
1136
1210
|
interface Question {
|
|
1137
|
-
prompt: string;
|
|
1211
|
+
prompt: string; // The sample prompt/question
|
|
1138
1212
|
context?: ChatContextItem[]; // Optional context items
|
|
1139
1213
|
}
|
|
1140
1214
|
```
|
|
@@ -1162,7 +1236,7 @@ interface CreateDatasourceContextParams {
|
|
|
1162
1236
|
datasourceUid: string;
|
|
1163
1237
|
title?: string;
|
|
1164
1238
|
icon?: IconName;
|
|
1165
|
-
hidden?: boolean;
|
|
1239
|
+
hidden?: boolean; // If true, the context item will not be shown in the context pills
|
|
1166
1240
|
}
|
|
1167
1241
|
```
|
|
1168
1242
|
|
|
@@ -1176,7 +1250,7 @@ interface CreateDashboardContextParams {
|
|
|
1176
1250
|
folderTitle?: string;
|
|
1177
1251
|
title?: string;
|
|
1178
1252
|
icon?: IconName;
|
|
1179
|
-
hidden?: boolean;
|
|
1253
|
+
hidden?: boolean; // If true, the context item will not be shown in the context pills
|
|
1180
1254
|
}
|
|
1181
1255
|
```
|
|
1182
1256
|
|
|
@@ -1188,7 +1262,7 @@ interface CreateFolderContextParams {
|
|
|
1188
1262
|
folderTitle: string;
|
|
1189
1263
|
title?: string;
|
|
1190
1264
|
icon?: IconName;
|
|
1191
|
-
hidden?: boolean;
|
|
1265
|
+
hidden?: boolean; // If true, the context item will not be shown in the context pills
|
|
1192
1266
|
}
|
|
1193
1267
|
```
|
|
1194
1268
|
|
|
@@ -1201,7 +1275,7 @@ interface CreateLabelNameContextParams {
|
|
|
1201
1275
|
labelName: string;
|
|
1202
1276
|
title?: string;
|
|
1203
1277
|
icon?: IconName;
|
|
1204
|
-
hidden?: boolean;
|
|
1278
|
+
hidden?: boolean; // If true, the context item will not be shown in the context pills
|
|
1205
1279
|
}
|
|
1206
1280
|
```
|
|
1207
1281
|
|
|
@@ -1215,7 +1289,7 @@ interface CreateLabelValueContextParams {
|
|
|
1215
1289
|
labelValue: string;
|
|
1216
1290
|
title?: string;
|
|
1217
1291
|
icon?: IconName;
|
|
1218
|
-
hidden?: boolean;
|
|
1292
|
+
hidden?: boolean; // If true, the context item will not be shown in the context pills
|
|
1219
1293
|
}
|
|
1220
1294
|
```
|
|
1221
1295
|
|
|
@@ -1226,7 +1300,7 @@ interface StructuredNodeDataParams {
|
|
|
1226
1300
|
data: Record<string, any>;
|
|
1227
1301
|
title?: string;
|
|
1228
1302
|
icon?: IconName;
|
|
1229
|
-
hidden?: boolean;
|
|
1303
|
+
hidden?: boolean; // If true, the context item will not be shown in the context pills
|
|
1230
1304
|
}
|
|
1231
1305
|
```
|
|
1232
1306
|
|
|
@@ -1236,10 +1310,10 @@ interface StructuredNodeDataParams {
|
|
|
1236
1310
|
interface CreateComponentContextParams {
|
|
1237
1311
|
components: Record<string, React.ComponentType<any>>;
|
|
1238
1312
|
prompt: string;
|
|
1239
|
-
namespace?: string;
|
|
1313
|
+
namespace?: string; // Optional: defaults to 'components' if not provided
|
|
1240
1314
|
title?: string;
|
|
1241
1315
|
icon?: IconName;
|
|
1242
|
-
hidden?: boolean;
|
|
1316
|
+
hidden?: boolean; // If true, the context item will not be shown in the context pills
|
|
1243
1317
|
}
|
|
1244
1318
|
```
|
|
1245
1319
|
|
|
@@ -1251,7 +1325,7 @@ interface NodeDataParams {
|
|
|
1251
1325
|
text?: string;
|
|
1252
1326
|
title?: string;
|
|
1253
1327
|
icon?: IconName;
|
|
1254
|
-
hidden?: boolean;
|
|
1328
|
+
hidden?: boolean; // If true, the context item will not be shown in the context pills
|
|
1255
1329
|
}
|
|
1256
1330
|
```
|
|
1257
1331
|
|
|
@@ -1299,6 +1373,7 @@ Groups functions under a namespace.
|
|
|
1299
1373
|
Creates a new function namespace.
|
|
1300
1374
|
|
|
1301
1375
|
**Parameters:**
|
|
1376
|
+
|
|
1302
1377
|
- `namespace: string` - The namespace identifier
|
|
1303
1378
|
- `functions: NamedFunctions` - The functions to include in the namespace
|
|
1304
1379
|
|
|
@@ -1307,6 +1382,7 @@ Creates a new function namespace.
|
|
|
1307
1382
|
Creates a plugin extension configuration for exposing functions to the assistant.
|
|
1308
1383
|
|
|
1309
1384
|
**Parameters:**
|
|
1385
|
+
|
|
1310
1386
|
- `namespaces: FunctionNamespace[]` - Array of function namespaces to expose
|
|
1311
1387
|
|
|
1312
1388
|
### Constants
|
|
@@ -1326,7 +1402,7 @@ The package also exports the following types for advanced use cases:
|
|
|
1326
1402
|
```typescript
|
|
1327
1403
|
// Data types for context items
|
|
1328
1404
|
export type DashboardNodeData;
|
|
1329
|
-
export type DatasourceNodeData;
|
|
1405
|
+
export type DatasourceNodeData;
|
|
1330
1406
|
export type FolderNodeData;
|
|
1331
1407
|
export type LabelNameNodeData;
|
|
1332
1408
|
export type LabelValueNodeData;
|
|
@@ -1341,4 +1417,4 @@ export type NamedComponents;
|
|
|
1341
1417
|
|
|
1342
1418
|
## License
|
|
1343
1419
|
|
|
1344
|
-
Apache-2.0
|
|
1420
|
+
Apache-2.0
|