[8.x] [Security assistant] Update custom tool name regex (#206487) (#206514)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Security assistant] Update custom tool name regex
(#206487)](https://github.com/elastic/kibana/pull/206487)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Steph
Milovic","email":"stephanie.milovic@elastic.co"},"sourceCommit":{"committedDate":"2025-01-13T20:57:34Z","message":"[Security
assistant] Update custom tool name regex
(#206487)","sha":"ef5977e653a65421b53b31455be90ef713b4abdc","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:Security
Generative
AI","backport:version","v8.18.0","v8.16.3","v8.17.1"],"title":"[Security
assistant] Update custom tool name
regex","number":206487,"url":"https://github.com/elastic/kibana/pull/206487","mergeCommit":{"message":"[Security
assistant] Update custom tool name regex
(#206487)","sha":"ef5977e653a65421b53b31455be90ef713b4abdc"}},"sourceBranch":"main","suggestedTargetBranches":["8.x","8.16","8.17"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/206487","number":206487,"mergeCommit":{"message":"[Security
assistant] Update custom tool name regex
(#206487)","sha":"ef5977e653a65421b53b31455be90ef713b4abdc"}},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.16","label":"v8.16.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.17","label":"v8.17.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

---------

Co-authored-by: Steph Milovic <stephanie.milovic@elastic.co>
This commit is contained in:
Kibana Machine 2025-01-14 10:07:44 +11:00 committed by GitHub
parent d7b26dc3aa
commit 74f06117d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions

View file

@ -231,4 +231,33 @@ describe('getStructuredToolForIndexEntry', () => {
);
expect(result).toContain(`I'm sorry, but I was unable to find any information`);
});
it('should match the name regex correctly', () => {
const tool = getStructuredToolForIndexEntry({
indexEntry: getCreateKnowledgeBaseEntrySchemaMock({
type: 'index',
name: `1bad-name?`,
}) as IndexEntry,
esClient: mockEsClient,
logger: mockLogger,
elserId: 'hi',
});
const nameRegex = /^[a-zA-Z0-9_-]+$/;
expect(tool.lc_kwargs.name).toMatch(nameRegex);
});
it('dashes get removed before `a` is prepended', () => {
const tool = getStructuredToolForIndexEntry({
indexEntry: getCreateKnowledgeBaseEntrySchemaMock({
type: 'index',
name: `-testing`,
}) as IndexEntry,
esClient: mockEsClient,
logger: mockLogger,
elserId: 'hi',
});
expect(tool.lc_kwargs.name).toMatch('testing');
});
});

View file

@ -160,7 +160,11 @@ export const getStructuredToolForIndexEntry = ({
}, {});
return new DynamicStructuredTool({
name: indexEntry.name.replace(/[^a-zA-Z0-9-]/g, ''), // // Tool names expects a string that matches the pattern '^[a-zA-Z0-9-]+$'
name: indexEntry.name
// Replace invalid characters with an empty string
.replace(/[^a-zA-Z0-9_]/g, '')
// Ensure it starts with a letter. If not, prepend 'a'
.replace(/^[^a-zA-Z]/, 'a'),
description: indexEntry.description,
schema: z.object({
query: z.string().describe(indexEntry.queryDescription),