Skip to content

Commit d8f35f1

Browse files
authored
[Agent Builder] create_visualization tool (#237117)
## Summary create_visualization tool resolves elastic/search-team#11446
1 parent 1a8b460 commit d8f35f1

File tree

37 files changed

+1083
-112
lines changed

37 files changed

+1083
-112
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ target
2222
.claude
2323
.cursor
2424
.windsurf
25+
claude.md
2526
*.iml
2627
*.log
2728
types.eslint.config.js

src/platform/packages/shared/kbn-lens-embeddable-utils/config_builder/schema/charts/metric.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export const metricStateSchemaNoESQL = schema.object({
206206
),
207207
});
208208

209-
const esqlMetricState = schema.object({
209+
export const esqlMetricState = schema.object({
210210
type: schema.literal('metric'),
211211
...sharedPanelInfoSchema,
212212
...layerSettingsSchema,

src/platform/packages/shared/kbn-management/settings/setting_ids/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export const ACCESSIBILITY_DISABLE_ANIMATIONS_ID = 'accessibility:disableAnimati
6161

6262
// Agent builder settings
6363
export const AGENT_BUILDER_ENABLED_SETTING_ID = 'agentBuilder:enabled';
64+
export const AGENT_BUILDER_DASHBOARD_TOOLS_SETTING_ID = 'agentBuilder:dashboardTools';
6465

6566
// Autocomplete settings
6667
export const AUTOCOMPLETE_USE_TIME_RANGE_ID = 'autocomplete:useTimeRange';

src/platform/plugins/private/kibana_usage_collection/server/collectors/management/schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,10 @@ export const stackManagementSchema: MakeSchemaFrom<UsageStats> = {
507507
type: 'boolean',
508508
_meta: { description: 'Non-default value of setting.' },
509509
},
510+
'agentBuilder:dashboardTools': {
511+
type: 'boolean',
512+
_meta: { description: 'Non-default value of setting.' },
513+
},
510514
'workflows:ui:enabled': {
511515
type: 'boolean',
512516
_meta: { description: 'Non-default value of setting.' },

src/platform/plugins/private/kibana_usage_collection/server/collectors/management/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export interface UsageStats {
5050
'observability:aiAssistantSearchConnectorIndexPattern': string;
5151
'ai:anonymizationSettings': string;
5252
'agentBuilder:enabled': boolean;
53+
'agentBuilder:dashboardTools': boolean;
5354
'workflows:ui:enabled': boolean;
5455
'visualization:heatmap:maxBuckets': number;
5556
'visualization:regionmap:showWarnings': boolean;

src/platform/plugins/shared/telemetry/schema/oss_platform.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10680,6 +10680,12 @@
1068010680
"description": "Non-default value of setting."
1068110681
}
1068210682
},
10683+
"agentBuilder:dashboardTools": {
10684+
"type": "boolean",
10685+
"_meta": {
10686+
"description": "Non-default value of setting."
10687+
}
10688+
},
1068310689
"workflows:ui:enabled": {
1068410690
"type": "boolean",
1068510691
"_meta": {

x-pack/platform/packages/shared/onechat/onechat-common/tools/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const platformCoreTools = {
2323
getDocumentById: platformCoreTool('get_document_by_id'),
2424
generateEsql: platformCoreTool('generate_esql'),
2525
executeEsql: platformCoreTool('execute_esql'),
26+
createVisualization: platformCoreTool('create_visualization'),
2627
} as const;
2728

2829
/**

x-pack/platform/packages/shared/onechat/onechat-common/tools/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export {
5858
type QueryResult,
5959
type ResourceResult,
6060
type TabularDataResult,
61+
type VisualizationResult,
6162
type OtherResult,
6263
isErrorResult,
6364
isOtherResult,

x-pack/platform/packages/shared/onechat/onechat-common/tools/tool_result.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ export enum ToolResultType {
1212
resource = 'resource',
1313
tabularData = 'tabular_data',
1414
query = 'query',
15+
visualization = 'visualization',
1516
other = 'other',
1617
error = 'error',
1718
}
1819

20+
export enum SupportedChartType {
21+
Metric = 'metric',
22+
Map = 'map',
23+
}
24+
1925
interface ToolResultMixin<TType extends ToolResultType, TData extends Object> {
2026
tool_result_id: string;
2127
type: TType;
@@ -47,6 +53,16 @@ export type TabularDataResult = ToolResultMixin<
4753

4854
export type QueryResult = ToolResultMixin<ToolResultType.query, { esql: string }>;
4955

56+
export interface VisualizationResult {
57+
tool_result_id: string;
58+
type: ToolResultType.visualization;
59+
data: {
60+
visualization: Record<string, unknown>;
61+
chart_type: SupportedChartType;
62+
esql: string;
63+
};
64+
}
65+
5066
export type OtherResult = ToolResultMixin<ToolResultType.other, Record<string, unknown>>;
5167

5268
export type ErrorResult = ToolResultMixin<
@@ -62,6 +78,7 @@ export type ToolResult =
6278
| ResourceResult
6379
| TabularDataResult
6480
| QueryResult
81+
| VisualizationResult
6582
| OtherResult
6683
| ErrorResult;
6784

x-pack/platform/packages/shared/onechat/onechat-genai-utils/tools/generate_esql/nl_to_esql.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { EsqlDocumentBase } from '@kbn/inference-plugin/server/tasks/nl_to_esql/
1313
import type { ToolEventEmitter } from '@kbn/onechat-server';
1414
import type { EsqlResponse } from '../utils/esql';
1515
import { createNlToEsqlGraph } from './graph';
16+
import { indexExplorer } from '../index_explorer';
1617

1718
export interface GenerateEsqlResponse {
1819
/**
@@ -49,7 +50,7 @@ export interface GenerateEsqlOptions {
4950
/**
5051
* The resource (index/datastream/alias) to target
5152
*/
52-
index: string;
53+
index?: string;
5354
/**
5455
* Additional context to provide to the model (user prompt)
5556
*/
@@ -97,10 +98,32 @@ export const generateEsql = async ({
9798
},
9899
async () => {
99100
try {
101+
// Discover index if not provided
102+
let selectedTarget = index;
103+
if (!selectedTarget) {
104+
logger?.debug('No index provided, discovering target index using indexExplorer');
105+
const {
106+
resources: [selectedResource],
107+
} = await indexExplorer({
108+
nlQuery,
109+
esClient,
110+
limit: 1,
111+
model,
112+
logger,
113+
});
114+
if (!selectedResource) {
115+
throw new Error(
116+
'Could not discover a suitable index for the query. Please specify an index explicitly.'
117+
);
118+
}
119+
selectedTarget = selectedResource.name;
120+
logger?.debug(`Discovered target index: ${selectedTarget}`);
121+
}
122+
100123
const outState = await graph.invoke(
101124
{
102125
nlQuery,
103-
target: index,
126+
target: selectedTarget,
104127
executeQuery,
105128
maxRetries,
106129
additionalInstructions,

0 commit comments

Comments
 (0)