Skip to content

Commit f788195

Browse files
authored
Merge pull request #29 from grafana/svennergr/navigate-tools
add navigate to other and fix other navigate tools
2 parents e03f5a1 + 1c6eea2 commit f788195

File tree

5 files changed

+50
-11
lines changed

5 files changed

+50
-11
lines changed

public/app/features/dash/agent/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { lokiLabelValuesTool } from './lokiLabelValues';
66
import { navigateToDashboardTool } from './navigateToDashboard';
77
import { navigateToDrilldownLogs } from './navigateToDrilldownLogs';
88
import { navigateToExploreTool } from './navigateToExplore';
9+
import { navigateToOtherTool } from './navigateToOther';
910
import { prometheusInstantQueryTool } from './prometheusInstantQuery';
1011
import { prometheusLabelNamesTool } from './prometheusLabelNames';
1112
import { prometheusLabelValuesTool } from './prometheusLabelValues';
@@ -24,6 +25,7 @@ export const tools = [
2425
navigateToDrilldownLogs,
2526
getCurrentTimeTool,
2627
navigateToDashboardTool,
28+
navigateToOtherTool,
2729
];
2830

2931
export const toolsByName = tools.reduce(

public/app/features/dash/agent/tools/navigateToDashboard.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ const navigateToDashboardToolSchema = z.object({
99
dashboard_uid: z.string().optional().describe('UID of the dashboard'),
1010
dashboard_title: z.string().optional().describe('Title of the dashboard'),
1111
dashboard_tag: z.string().optional().describe('String tag that labels the dashboard'),
12+
navigate: z.boolean().describe('Whether to navigate to the dashboard. Only ever set this to true if the user has confirmed to navigate to the dashboard.'),
1213
});
1314

1415
export const navigateToDashboardTool = tool(
1516
async (input) => {
16-
const { dashboard_uid, dashboard_title, dashboard_tag } = navigateToDashboardToolSchema.parse(input);
17+
const { dashboard_uid, dashboard_title, dashboard_tag, navigate } = navigateToDashboardToolSchema.parse(input);
1718

1819
if (!dashboard_uid && !dashboard_title && !dashboard_tag) {
1920
return 'Failure. Ask the user which dashboard they want to see.';
@@ -36,9 +37,12 @@ export const navigateToDashboardTool = tool(
3637
return 'Failure. Tell the user that the dashboard does not exist';
3738
}
3839

39-
locationService.push(dashboard.url);
40+
const url = dashboard.url;
41+
if (navigate) {
42+
locationService.push(url);
43+
}
4044

41-
return 'success';
45+
return url;
4246
},
4347
{
4448
name: 'navigate_to_dashboard',

public/app/features/dash/agent/tools/navigateToDrilldownLogs.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@ const navigateToDrilldownLogsSchema = z.object({
1616
.array(z.enum(['critical', 'error', 'debug', 'info', 'warning', 'fatal']))
1717
.optional()
1818
.describe('Array of error levels to include in the filters.'),
19+
navigate: z.boolean().describe('Whether to navigate to the Drilldown Logs page. Only ever set this to true if the user has confirmed to navigate to Drilldown Logs.'),
1920
});
2021

2122
export const navigateToDrilldownLogs = tool(
2223
async (input) => {
23-
const { datasource_uid, label_filters, levels = [] } = navigateToDrilldownLogsSchema.parse(input);
24+
const { datasource_uid, label_filters, levels = [], navigate } = navigateToDrilldownLogsSchema.parse(input);
2425

2526
const varFilters = label_filters.map((filter) => `var-filters=${encodeURIComponent(filter)}`);
2627
const varLevels = levels.map((level) => `var-levels=${encodeURIComponent(`detected_level|=|${level}`)}`);
2728

28-
locationService.push(
29-
`/a/grafana-lokiexplore-app/explore/service_name/grafana/logs?from=now-15m&to=now&var-ds=${datasource_uid}&${varFilters.join('&')}&${varLevels.join('&')}`
30-
);
29+
const url = `/a/grafana-lokiexplore-app/explore/service_name/grafana/logs?from=now-15m&to=now&var-ds=${datasource_uid}&${varFilters.join('&')}&${varLevels.join('&')}`;
30+
if (navigate) {
31+
locationService.push(url);
32+
}
3133

32-
return 'success';
34+
return url;
3335
},
3436
{
3537
name: 'navigate_to_drilldown_logs',

public/app/features/dash/agent/tools/navigateToExplore.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import { lokiOrPrometheusTypeRefiner } from './refiners';
1010
const navigateToExploreSchema = z.object({
1111
datasource_uid: z.string().describe('Datasource UID that will execute the query').refine(lokiOrPrometheusTypeRefiner.func, lokiOrPrometheusTypeRefiner.message),
1212
query: z.string().describe('Query to be executed'),
13+
navigate: z.boolean().describe('Whether to navigate to the Explore page. Only ever set this to true if the user has confirmed to navigate to Explore.'),
1314
});
1415

1516
export const navigateToExploreTool = tool(
1617
async (input) => {
17-
const { datasource_uid, query } = navigateToExploreSchema.parse(input);
18+
const { datasource_uid, query, navigate } = navigateToExploreSchema.parse(input);
1819
const type = getDatasourceSrv().getAll().find((ds) => ds.uid === datasource_uid)?.type;
1920

2021
const panes = {
@@ -37,9 +38,12 @@ export const navigateToExploreTool = tool(
3738
},
3839
};
3940

40-
locationService.push(`/explore?schemaVersion=1&panes=${JSON.stringify(panes)}`);
41+
const url = `/explore?schemaVersion=1&panes=${JSON.stringify(panes)}`;
42+
if (navigate) {
43+
locationService.push(url);
44+
}
4145

42-
return 'success';
46+
return url;
4347
},
4448
{
4549
name: 'navigate_to_explore',
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { tool } from '@langchain/core/tools';
2+
import { z } from 'zod';
3+
4+
import { locationService } from '@grafana/runtime';
5+
6+
const navigateToOtherSchema = z.object({
7+
url: z.string().describe('URL to navigate to'),
8+
navigate: z.boolean().describe('Whether to navigate to the URL. Only ever set this to true if the user has confirmed to navigate to the URL.'),
9+
});
10+
11+
export const navigateToOtherTool = tool(
12+
async (input) => {
13+
const { url, navigate } = navigateToOtherSchema.parse(input);
14+
15+
if (navigate) {
16+
locationService.push(url);
17+
}
18+
19+
return url;
20+
},
21+
{
22+
name: 'navigate_to_other',
23+
description:
24+
'Use this tool when the user wants to navigate to a URL. NEVER use it without asking the user for confirmation.',
25+
schema: navigateToOtherSchema,
26+
}
27+
);

0 commit comments

Comments
 (0)