-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Security GenAI] Use stable IDs for Security AI Prompt saved object filenames #265301
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
KDKHD
merged 5 commits into
elastic:main
from
KDKHD:feature/237178-stable-prompt-filenames
Apr 27, 2026
+135
−3
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c06c4c1
[Security GenAI] Use stable IDs for Security AI Prompt saved object f…
KDKHD 4759852
Convert camelCase prompt IDs to kebab-case for readable filenames
KDKHD 8a9b28f
Fix ID collision when model is set without provider
KDKHD 00ea790
Changes from node scripts/eslint_all_files --no-cache --fix
kibanamachine 798ab20
Merge branch 'main' into feature/237178-stable-prompt-filenames
KDKHD 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
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
119 changes: 119 additions & 0 deletions
119
...ns/security/plugins/elastic_assistant/scripts/generate_security_ai_prompts_script.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,119 @@ | ||
| /* | ||
| * 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import type { Prompt } from '@kbn/security-ai-prompts'; | ||
| import { | ||
| SAVED_OBJECT_ID_PREFIX, | ||
| generateSavedObject, | ||
| generateSavedObjects, | ||
| generateStableId, | ||
| } from './generate_security_ai_prompts_script'; | ||
|
|
||
| const basePrompt: Prompt = { | ||
| promptGroupId: 'aiAssistant', | ||
| promptId: 'systemPrompt', | ||
| prompt: { default: 'You are a helpful assistant.' }, | ||
| }; | ||
|
|
||
| describe('generateStableId', () => { | ||
| it('converts camelCase to kebab-case', () => { | ||
| const id = generateStableId(basePrompt); | ||
| expect(id).toBe(`${SAVED_OBJECT_ID_PREFIX}ai-assistant-system-prompt`); | ||
| }); | ||
|
|
||
| it('handles acronyms correctly (e.g. ESQL stays grouped)', () => { | ||
| const id = generateStableId({ ...basePrompt, promptId: 'NaturalLanguageESQLTool' }); | ||
| expect(id).toBe(`${SAVED_OBJECT_ID_PREFIX}ai-assistant-natural-language-esql-tool`); | ||
| }); | ||
|
|
||
| it('appends provider when present', () => { | ||
| const id = generateStableId({ ...basePrompt, provider: 'openai' }); | ||
| expect(id).toBe(`${SAVED_OBJECT_ID_PREFIX}ai-assistant-system-prompt-openai`); | ||
| }); | ||
|
|
||
| it('appends model when present', () => { | ||
| const id = generateStableId({ ...basePrompt, provider: 'openai', model: 'gpt-4o' }); | ||
| expect(id).toBe(`${SAVED_OBJECT_ID_PREFIX}ai-assistant-system-prompt-openai-gpt-4o`); | ||
| }); | ||
|
|
||
| it('omits model segment when provider is absent', () => { | ||
| const id = generateStableId({ ...basePrompt, model: 'gpt-4o' }); | ||
| expect(id).toBe(`${SAVED_OBJECT_ID_PREFIX}ai-assistant-system-prompt`); | ||
| }); | ||
|
|
||
| it('does not collide when provider-only vs model-only have the same value', () => { | ||
| const providerOnly = generateStableId({ ...basePrompt, provider: 'openai' }); | ||
| const modelOnly = generateStableId({ ...basePrompt, model: 'openai' }); | ||
| expect(providerOnly).not.toBe(modelOnly); | ||
| }); | ||
|
|
||
| it('is fully lowercase', () => { | ||
| const id = generateStableId({ | ||
| ...basePrompt, | ||
| promptGroupId: 'AttackDiscovery', | ||
| promptId: 'SystemPrompt', | ||
| provider: 'OpenAI', | ||
| }); | ||
| expect(id).toBe(id.toLowerCase()); | ||
| }); | ||
|
|
||
| it('returns the same id on repeated calls (stable)', () => { | ||
| expect(generateStableId(basePrompt)).toBe(generateStableId(basePrompt)); | ||
| }); | ||
|
|
||
| it('produces distinct ids for different providers', () => { | ||
| const openai = generateStableId({ ...basePrompt, provider: 'openai' }); | ||
| const bedrock = generateStableId({ ...basePrompt, provider: 'bedrock' }); | ||
| expect(openai).not.toBe(bedrock); | ||
| }); | ||
|
|
||
| it('produces distinct ids for different promptGroupIds', () => { | ||
| const a = generateStableId({ ...basePrompt, promptGroupId: 'aiAssistant' }); | ||
| const b = generateStableId({ ...basePrompt, promptGroupId: 'attackDiscovery' }); | ||
| expect(a).not.toBe(b); | ||
| }); | ||
|
|
||
| it('produces distinct ids for different promptIds', () => { | ||
| const a = generateStableId({ ...basePrompt, promptId: 'systemPrompt' }); | ||
| const b = generateStableId({ ...basePrompt, promptId: 'userPrompt' }); | ||
| expect(a).not.toBe(b); | ||
| }); | ||
| }); | ||
|
|
||
| describe('generateSavedObject', () => { | ||
| it('sets a stable id', () => { | ||
| const result = generateSavedObject(basePrompt); | ||
| expect(result.id).toBe(generateStableId(basePrompt)); | ||
| }); | ||
|
|
||
| it('sets type to security-ai-prompt', () => { | ||
| expect(generateSavedObject(basePrompt).type).toBe('security-ai-prompt'); | ||
| }); | ||
|
|
||
| it('preserves prompt attributes', () => { | ||
| const prompt: Prompt = { ...basePrompt, provider: 'bedrock', description: 'My prompt' }; | ||
| const { attributes } = generateSavedObject(prompt); | ||
| expect(attributes.promptId).toBe(prompt.promptId); | ||
| expect(attributes.promptGroupId).toBe(prompt.promptGroupId); | ||
| expect(attributes.provider).toBe('bedrock'); | ||
| expect(attributes.description).toBe('My prompt'); | ||
| expect(attributes.prompt.default).toBe(prompt.prompt.default); | ||
| }); | ||
| }); | ||
|
|
||
| describe('generateSavedObjects', () => { | ||
| it('maps each prompt to a saved object with a unique stable id', () => { | ||
| const prompts: Prompt[] = [ | ||
| { ...basePrompt, promptId: 'systemPrompt' }, | ||
| { ...basePrompt, promptId: 'userPrompt' }, | ||
| { ...basePrompt, promptId: 'systemPrompt', provider: 'openai' }, | ||
| ]; | ||
| const results = generateSavedObjects(prompts); | ||
| const ids = results.map((r) => r.id); | ||
| expect(new Set(ids).size).toBe(ids.length); | ||
| }); | ||
| }); |
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.
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.