-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[One workflow] 🐞 Include registered triggers in agent builder experience #270221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yngrdyn
merged 5 commits into
elastic:main
from
yngrdyn:17218-one-workflow-extend-get_trigger_definitions-tool-to-include-custom-trigger-types
May 22, 2026
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
243692e
Include plugin-registered triggers in get_trigger_definitions agent tool
yngrdyn fc49a3e
fixing build
yngrdyn a5bdab4
Merge branch 'main' into 17218-one-workflow-extend-get_trigger_defini…
yngrdyn ee2b445
Moving more information to commonTriggerSchema so it can be reused in…
yngrdyn 5985439
Merge branch 'main' into 17218-one-workflow-extend-get_trigger_defini…
yngrdyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
...rm/plugins/shared/workflows_management/common/build_trigger_definitions_for_agent.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import { builtInTriggerDefinitions } from '@kbn/workflows'; | ||
| import { z } from '@kbn/zod/v4'; | ||
| import { | ||
| isTriggerDefinitionsLookupError, | ||
| lookupTriggerDefinitionsForAgent, | ||
| } from './build_trigger_definitions_for_agent'; | ||
|
|
||
| const mockCasesTrigger = { | ||
| id: 'cases.caseUpdated', | ||
| eventSchema: z.object({ | ||
| caseId: z.string(), | ||
| owner: z.string(), | ||
| updatedFields: z.array(z.string()).optional(), | ||
| }), | ||
| }; | ||
|
|
||
| describe('lookupTriggerDefinitionsForAgent', () => { | ||
| it('returns built-in and registered trigger types without filter', () => { | ||
| const result = lookupTriggerDefinitionsForAgent({ | ||
| registeredTriggers: [mockCasesTrigger], | ||
| }); | ||
|
|
||
| expect(isTriggerDefinitionsLookupError(result)).toBe(false); | ||
| if (isTriggerDefinitionsLookupError(result)) { | ||
| return; | ||
| } | ||
|
|
||
| expect(result.count).toBe(builtInTriggerDefinitions.length + 1); | ||
| expect(result.triggerTypes.map((t) => t.id)).toContain('cases.caseUpdated'); | ||
| expect(result.triggerTypes.map((t) => t.id)).toContain('manual'); | ||
| }); | ||
|
|
||
| it('returns jsonSchema and examples for built-in triggers', () => { | ||
| const result = lookupTriggerDefinitionsForAgent({ | ||
| registeredTriggers: [mockCasesTrigger], | ||
| }); | ||
|
|
||
| expect(isTriggerDefinitionsLookupError(result)).toBe(false); | ||
| if (isTriggerDefinitionsLookupError(result)) { | ||
| return; | ||
| } | ||
|
|
||
| const builtInTriggers = result.triggerTypes.filter((trigger) => | ||
| builtInTriggerDefinitions.some((def) => def.id === trigger.id) | ||
| ); | ||
| for (const trigger of builtInTriggers) { | ||
| expect(trigger.jsonSchema).toBeDefined(); | ||
| expect(trigger.examples?.length).toBeGreaterThan(0); | ||
| } | ||
| }); | ||
|
|
||
| it('returns custom trigger with trigger-specific eventContextSchema when filtered', () => { | ||
| const result = lookupTriggerDefinitionsForAgent({ | ||
| registeredTriggers: [mockCasesTrigger], | ||
| triggerType: 'cases.caseUpdated', | ||
| }); | ||
|
|
||
| expect(isTriggerDefinitionsLookupError(result)).toBe(false); | ||
| if (isTriggerDefinitionsLookupError(result)) { | ||
| return; | ||
| } | ||
|
|
||
| expect(result.count).toBe(1); | ||
| expect(result.triggerTypes[0].id).toBe('cases.caseUpdated'); | ||
| const schemaStr = JSON.stringify(result.triggerTypes[0].eventContextSchema); | ||
| expect(schemaStr).toContain('caseId'); | ||
| expect(schemaStr).toContain('owner'); | ||
| expect(schemaStr).toContain('spaceId'); | ||
| expect(schemaStr).toContain('timestamp'); | ||
| }); | ||
|
|
||
| it('filters by built-in triggerType', () => { | ||
| const result = lookupTriggerDefinitionsForAgent({ | ||
| registeredTriggers: [mockCasesTrigger], | ||
| triggerType: 'scheduled', | ||
| }); | ||
|
|
||
| expect(isTriggerDefinitionsLookupError(result)).toBe(false); | ||
| if (isTriggerDefinitionsLookupError(result)) { | ||
| return; | ||
| } | ||
|
|
||
| expect(result.count).toBe(1); | ||
| expect(result.triggerTypes[0].id).toBe('scheduled'); | ||
| }); | ||
|
|
||
| it('returns error for unknown trigger type with built-in and registered availableTypes', () => { | ||
| const result = lookupTriggerDefinitionsForAgent({ | ||
| registeredTriggers: [mockCasesTrigger], | ||
| triggerType: 'nonexistent', | ||
| }); | ||
|
|
||
| expect(isTriggerDefinitionsLookupError(result)).toBe(true); | ||
| if (!isTriggerDefinitionsLookupError(result)) { | ||
| return; | ||
| } | ||
|
|
||
| expect(result.error).toContain('not found'); | ||
| expect(result.availableTypes).toContain('manual'); | ||
| expect(result.availableTypes).toContain('cases.caseUpdated'); | ||
| }); | ||
|
|
||
| it('compacts large enums in scheduled trigger jsonSchema', () => { | ||
| const result = lookupTriggerDefinitionsForAgent({ | ||
| registeredTriggers: [mockCasesTrigger], | ||
| triggerType: 'scheduled', | ||
| }); | ||
|
|
||
| expect(isTriggerDefinitionsLookupError(result)).toBe(false); | ||
| if (isTriggerDefinitionsLookupError(result)) { | ||
| return; | ||
| } | ||
|
|
||
| const schemaStr = JSON.stringify(result.triggerTypes[0].jsonSchema); | ||
| expect(schemaStr).not.toContain('Pacific/Honolulu'); | ||
| expect(schemaStr).toContain('allowed values'); | ||
| }); | ||
| }); |
226 changes: 226 additions & 0 deletions
226
...latform/plugins/shared/workflows_management/common/build_trigger_definitions_for_agent.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import type { BaseTriggerDefinition } from '@kbn/workflows'; | ||
| import { | ||
| builtInTriggerDefinitions, | ||
| EventTimestampSchema, | ||
| WorkflowEventsSchema, | ||
| } from '@kbn/workflows'; | ||
| import { BaseEventSchema } from '@kbn/workflows/spec/schema/common/base_event'; | ||
| import { AlertEventSchema } from '@kbn/workflows/spec/schema/triggers/alert_trigger_schema'; | ||
| import type { ServerTriggerDefinition } from '@kbn/workflows-extensions/server'; | ||
| import { z } from '@kbn/zod/v4'; | ||
|
|
||
| const LARGE_ENUM_THRESHOLD = 20; | ||
|
|
||
| export interface TriggerDefinitionForAgent { | ||
| id: string; | ||
| label: string; | ||
| description: string; | ||
| jsonSchema: unknown; | ||
| eventContextSchema: unknown; | ||
| eventContextNote: string; | ||
| examples?: string[]; | ||
| } | ||
|
|
||
| export const EVENT_CONTEXT_NOTE = | ||
| 'The event context is available via {{ event.* }} in Liquid templates. ' + | ||
| 'NEVER use {{ triggers.event }} or {{ trigger.event }} — the correct variable is {{ event }}.'; | ||
|
|
||
| function zodToJsonSchemaSafe(schema: z.ZodType): unknown { | ||
| try { | ||
| const jsonSchema = z.toJSONSchema(schema, { target: 'draft-7', unrepresentable: 'any' }); | ||
| return compactLargeEnums(jsonSchema as Record<string, unknown>); | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| function compactLargeEnums(node: unknown): unknown { | ||
| if (node === null || typeof node !== 'object') return node; | ||
| if (Array.isArray(node)) return node.map(compactLargeEnums); | ||
|
|
||
| const obj = node as Record<string, unknown>; | ||
| const result: Record<string, unknown> = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| if (key === 'enum' && Array.isArray(value) && value.length > LARGE_ENUM_THRESHOLD) { | ||
| const examples = value.slice(0, 5) as string[]; | ||
| result.type = 'string'; | ||
| result.description = [ | ||
| obj.description ?? '', | ||
| `One of ${value.length} allowed values, e.g.: ${examples.join(', ')}`, | ||
| ] | ||
| .filter(Boolean) | ||
| .join('. '); | ||
| } else { | ||
| result[key] = compactLargeEnums(value); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| function isZodObject(schema: z.ZodType): schema is z.ZodObject<z.ZodRawShape> { | ||
| return schema instanceof z.ZodObject; | ||
| } | ||
|
|
||
| function getCustomTriggerYamlSchema(triggerId: string): z.ZodType { | ||
| return z.object({ | ||
| type: z.literal(triggerId), | ||
| on: z | ||
| .object({ | ||
| condition: z.string().optional(), | ||
| workflowEvents: WorkflowEventsSchema.optional(), | ||
| }) | ||
| .optional(), | ||
| }); | ||
| } | ||
|
|
||
| function humanizeTriggerId(triggerId: string): string { | ||
| const [namespace, event] = triggerId.split('.'); | ||
| const formatSegment = (segment: string) => | ||
| segment | ||
| .replace(/([a-z])([A-Z])/g, '$1 $2') | ||
| .replace(/[_-]+/g, ' ') | ||
| .replace(/\b\w/g, (char) => char.toUpperCase()); | ||
| if (!event) { | ||
| return formatSegment(triggerId); | ||
| } | ||
| return `${formatSegment(namespace)} - ${formatSegment(event)}`; | ||
| } | ||
|
|
||
| function getEventContextSchema( | ||
| triggerId: string, | ||
| customDefsById: Map<string, ServerTriggerDefinition> | ||
| ): unknown { | ||
| if (triggerId === 'alert') { | ||
| return zodToJsonSchemaSafe(AlertEventSchema); | ||
| } | ||
|
|
||
| const custom = customDefsById.get(triggerId); | ||
| if (custom && isZodObject(custom.eventSchema)) { | ||
| return zodToJsonSchemaSafe( | ||
| z.object({ | ||
| ...BaseEventSchema.shape, | ||
| ...EventTimestampSchema.shape, | ||
| ...custom.eventSchema.shape, | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| return zodToJsonSchemaSafe(BaseEventSchema); | ||
| } | ||
|
|
||
| function createRegisteredTriggersMap( | ||
| registeredTriggers: ServerTriggerDefinition[] | ||
| ): Map<string, ServerTriggerDefinition> { | ||
| return new Map(registeredTriggers.map((def) => [def.id, def])); | ||
| } | ||
|
|
||
| function formatBuiltInTrigger( | ||
| def: BaseTriggerDefinition, | ||
| registeredTriggersById: Map<string, ServerTriggerDefinition> | ||
| ): TriggerDefinitionForAgent { | ||
| return { | ||
| id: def.id, | ||
| label: def.label, | ||
| description: def.description, | ||
| jsonSchema: zodToJsonSchemaSafe(def.schema), | ||
| eventContextSchema: getEventContextSchema(def.id, registeredTriggersById), | ||
| eventContextNote: EVENT_CONTEXT_NOTE, | ||
| examples: def.documentation.examples, | ||
| }; | ||
| } | ||
|
|
||
| function formatRegisteredTrigger( | ||
| def: ServerTriggerDefinition, | ||
| registeredTriggersById: Map<string, ServerTriggerDefinition> | ||
| ): TriggerDefinitionForAgent { | ||
| return { | ||
| id: def.id, | ||
| label: humanizeTriggerId(def.id), | ||
| description: `Event-driven trigger (${def.id}). Use on.condition with KQL on event.* fields to filter when the workflow runs.`, | ||
| jsonSchema: zodToJsonSchemaSafe(getCustomTriggerYamlSchema(def.id)), | ||
| eventContextSchema: getEventContextSchema(def.id, registeredTriggersById), | ||
| eventContextNote: EVENT_CONTEXT_NOTE, | ||
| examples: [ | ||
|
Kiryous marked this conversation as resolved.
Outdated
|
||
| `triggers: | ||
| - type: ${def.id} | ||
| on: | ||
| condition: 'event.owner: "securitySolution"'`, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Full trigger catalog for workflow authoring agents. | ||
| * | ||
| * - Built-ins (`manual`, `scheduled`, `alert`) come from `@kbn/workflows`. | ||
| * - Everything else comes from `api.getRegisteredTriggers()` (workflows_extensions registry). | ||
| */ | ||
| export function getAllTriggerDefinitionsForAgent( | ||
| registeredTriggers: ServerTriggerDefinition[] | ||
| ): TriggerDefinitionForAgent[] { | ||
| const registeredById = createRegisteredTriggersMap(registeredTriggers); | ||
| const builtInIds = new Set(builtInTriggerDefinitions.map((def) => def.id)); | ||
|
|
||
| const builtIn = builtInTriggerDefinitions.map((def) => formatBuiltInTrigger(def, registeredById)); | ||
| const pluginRegistered = registeredTriggers | ||
| .filter((def) => !builtInIds.has(def.id)) | ||
| .map((def) => formatRegisteredTrigger(def, registeredById)); | ||
|
|
||
| return [...builtIn, ...pluginRegistered]; | ||
| } | ||
|
|
||
| export interface TriggerDefinitionsLookupSuccess { | ||
| count: number; | ||
| triggerTypes: TriggerDefinitionForAgent[]; | ||
| } | ||
|
|
||
| export interface TriggerDefinitionsLookupError { | ||
| error: string; | ||
| availableTypes: string[]; | ||
| } | ||
|
|
||
| export type TriggerDefinitionsLookupResult = | ||
| | TriggerDefinitionsLookupSuccess | ||
| | TriggerDefinitionsLookupError; | ||
|
|
||
| export const isTriggerDefinitionsLookupError = ( | ||
| result: TriggerDefinitionsLookupResult | ||
| ): result is TriggerDefinitionsLookupError => 'error' in result; | ||
|
|
||
| /** | ||
| * Resolves the agent trigger catalog, optionally filtered to a single trigger id. | ||
| */ | ||
| export function lookupTriggerDefinitionsForAgent({ | ||
| registeredTriggers, | ||
| triggerType, | ||
| }: { | ||
| registeredTriggers: ServerTriggerDefinition[]; | ||
| triggerType?: string; | ||
| }): TriggerDefinitionsLookupResult { | ||
| const allTriggerDefinitions = getAllTriggerDefinitionsForAgent(registeredTriggers); | ||
|
|
||
| if (!triggerType) { | ||
| return { count: allTriggerDefinitions.length, triggerTypes: allTriggerDefinitions }; | ||
| } | ||
|
|
||
| const matching = allTriggerDefinitions.filter((def) => def.id === triggerType); | ||
| if (matching.length === 0) { | ||
| return { | ||
| error: `Trigger type "${triggerType}" not found`, | ||
| availableTypes: allTriggerDefinitions.map((def) => def.id), | ||
| }; | ||
| } | ||
|
|
||
| return { count: matching.length, triggerTypes: matching }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.