mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[Security assistant] Update custom tool name regex (#206487)
This commit is contained in:
parent
95b76dc12b
commit
ef5977e653
2 changed files with 32 additions and 1 deletions
|
@ -221,4 +221,31 @@ 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,
|
||||
});
|
||||
|
||||
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,
|
||||
});
|
||||
|
||||
expect(tool.lc_kwargs.name).toMatch('testing');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -157,7 +157,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),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue