mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-19 22:43:38 +00:00
🧫 chore: Keep Jest Specs Out of Tool Discovery (#15766)
* fix: Skip test modules during structured tool discovery * fix: Keep tool discovery validation in TypeScript
This commit is contained in:
parent
ec15075dc8
commit
0d85af0a77
6 changed files with 59 additions and 4 deletions
|
|
@ -23,7 +23,7 @@ jest.mock(
|
|||
{ virtual: true },
|
||||
);
|
||||
|
||||
const AzureAISearch = require('./AzureAISearch');
|
||||
const AzureAISearch = require('../AzureAISearch');
|
||||
|
||||
describe('AzureAISearch', () => {
|
||||
const requiredFields = {
|
||||
|
|
@ -29,7 +29,7 @@ jest.mock('@librechat/api', () => ({
|
|||
}));
|
||||
jest.mock('~/config/paths', () => ({}), { virtual: true });
|
||||
|
||||
const StableDiffusionAPI = require('./StableDiffusion');
|
||||
const StableDiffusionAPI = require('../StableDiffusion');
|
||||
|
||||
describe('StableDiffusionAPI', () => {
|
||||
beforeEach(() => {
|
||||
50
api/server/services/start/__tests__/tools.spec.js
Normal file
50
api/server/services/start/__tests__/tools.spec.js
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const { loadAndFormatTools } = require('../tools');
|
||||
|
||||
describe('loadAndFormatTools', () => {
|
||||
let directory;
|
||||
|
||||
beforeEach(() => {
|
||||
directory = fs.mkdtempSync(path.join(os.tmpdir(), 'librechat-tools-'));
|
||||
const toolPath = require.resolve('@librechat/agents/langchain/tools');
|
||||
fs.writeFileSync(
|
||||
path.join(directory, 'Example.js'),
|
||||
`const { Tool } = require(${JSON.stringify(toolPath)});
|
||||
module.exports = class extends Tool {
|
||||
constructor() {
|
||||
super();
|
||||
this.name = 'example';
|
||||
this.description = 'Example tool';
|
||||
}
|
||||
async _call() { return 'example'; }
|
||||
};`,
|
||||
);
|
||||
for (const file of ['Example.spec.js', 'Example.test.js']) {
|
||||
fs.writeFileSync(
|
||||
path.join(directory, file),
|
||||
`require('fs').writeFileSync(${JSON.stringify(path.join(directory, `${file}.loaded`))}, 'loaded');
|
||||
throw new Error('Test module must not execute during tool discovery');`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
fs.rmSync(directory, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ adminIncluded: [] },
|
||||
{ adminIncluded: ['Example.js', 'Example.spec.js', 'Example.test.js'] },
|
||||
])(
|
||||
'skips test modules before execution with includedTools=$adminIncluded',
|
||||
({ adminIncluded }) => {
|
||||
const tools = loadAndFormatTools({ directory, adminIncluded });
|
||||
|
||||
expect(tools.example.function.name).toBe('example');
|
||||
expect(fs.existsSync(path.join(directory, 'Example.spec.js.loaded'))).toBe(false);
|
||||
expect(fs.existsSync(path.join(directory, 'Example.test.js.loaded'))).toBe(false);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
|
@ -7,6 +7,7 @@ const { Tool } = require('@librechat/agents/langchain/tools');
|
|||
const { Tools, ImageVisionTool } = require('librechat-data-provider');
|
||||
const {
|
||||
getToolkitKey,
|
||||
isToolModuleFile,
|
||||
oaiToolkit,
|
||||
geminiToolkit,
|
||||
createAskUserQuestionTool,
|
||||
|
|
@ -16,7 +17,7 @@ const { toolkits } = require('~/app/clients/tools/manifest');
|
|||
/**
|
||||
* Loads and formats tools from the specified tool directory.
|
||||
*
|
||||
* The directory is scanned for JavaScript files, excluding any files in the filter set.
|
||||
* The directory is scanned for JavaScript files, excluding test files and any files in the filter set.
|
||||
* For each file, it attempts to load the file as a module and instantiate a class, if it's a subclass of `StructuredTool`.
|
||||
* Each tool instance is then formatted to be compatible with the OpenAI Assistant.
|
||||
* Additionally, instances of LangChain Tools are included in the result.
|
||||
|
|
@ -42,7 +43,7 @@ function loadAndFormatTools({ directory, adminFilter = [], adminIncluded = [] })
|
|||
|
||||
for (const file of files) {
|
||||
const filePath = path.join(directory, file);
|
||||
if (!file.endsWith('.js') || (filter.has(file) && included.size === 0)) {
|
||||
if (!isToolModuleFile(file) || (filter.has(file) && included.size === 0)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
|||
3
packages/api/src/tools/discovery.ts
Normal file
3
packages/api/src/tools/discovery.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export function isToolModuleFile(filename: string): boolean {
|
||||
return filename.endsWith('.js') && !/\.(spec|test)\.js$/.test(filename);
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
export * from './format';
|
||||
export * from './discovery';
|
||||
export * from './protection';
|
||||
export * from './registry';
|
||||
export * from './toolkits';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue