Skip to content

Commit ef5977e

Browse files
authored
[Security assistant] Update custom tool name regex (elastic#206487)
1 parent 95b76dc commit ef5977e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

x-pack/solutions/security/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/helpers.test.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,31 @@ describe('getStructuredToolForIndexEntry', () => {
221221
);
222222
expect(result).toContain(`I'm sorry, but I was unable to find any information`);
223223
});
224+
225+
it('should match the name regex correctly', () => {
226+
const tool = getStructuredToolForIndexEntry({
227+
indexEntry: getCreateKnowledgeBaseEntrySchemaMock({
228+
type: 'index',
229+
name: `1bad-name?`,
230+
}) as IndexEntry,
231+
esClient: mockEsClient,
232+
logger: mockLogger,
233+
});
234+
235+
const nameRegex = /^[a-zA-Z0-9_-]+$/;
236+
expect(tool.lc_kwargs.name).toMatch(nameRegex);
237+
});
238+
239+
it('dashes get removed before `a` is prepended', () => {
240+
const tool = getStructuredToolForIndexEntry({
241+
indexEntry: getCreateKnowledgeBaseEntrySchemaMock({
242+
type: 'index',
243+
name: `-testing`,
244+
}) as IndexEntry,
245+
esClient: mockEsClient,
246+
logger: mockLogger,
247+
});
248+
249+
expect(tool.lc_kwargs.name).toMatch('testing');
250+
});
224251
});

x-pack/solutions/security/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/helpers.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,11 @@ export const getStructuredToolForIndexEntry = ({
157157
}, {});
158158

159159
return new DynamicStructuredTool({
160-
name: indexEntry.name.replace(/[^a-zA-Z0-9-]/g, ''), // // Tool names expects a string that matches the pattern '^[a-zA-Z0-9-]+$'
160+
name: indexEntry.name
161+
// Replace invalid characters with an empty string
162+
.replace(/[^a-zA-Z0-9_]/g, '')
163+
// Ensure it starts with a letter. If not, prepend 'a'
164+
.replace(/^[^a-zA-Z]/, 'a'),
161165
description: indexEntry.description,
162166
schema: z.object({
163167
query: z.string().describe(indexEntry.queryDescription),

0 commit comments

Comments
 (0)