Skip to content

Commit 8378e0f

Browse files
authored
perf: init tool (#319)
* add gpt5.2 * perf: init tool * perf: init tool * perf: init tool * rename gpt 5.2 * rename gpt 5.2 * rename gpt 5.2
1 parent 801e6f6 commit 8378e0f

File tree

5 files changed

+40
-40
lines changed

5 files changed

+40
-40
lines changed

modules/model/provider/OpenAI/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import { ModelTypeEnum, type ProviderConfigType } from '../../type';
33
const models: ProviderConfigType = {
44
provider: 'OpenAI',
55
list: [
6+
{
7+
type: ModelTypeEnum.llm,
8+
model: 'gpt-5.2',
9+
maxContext: 400000,
10+
maxTokens: 128000,
11+
quoteMaxToken: 400000,
12+
maxTemperature: null,
13+
responseFormatList: ['text', 'json_schema'],
14+
vision: true,
15+
reasoning: false,
16+
toolChoice: true
17+
},
618
{
719
type: ModelTypeEnum.llm,
820
model: 'gpt-5.1',

modules/tool/api/upload/delete.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { UploadToolsS3Path } from '@tool/constants';
99
import { addLog } from '@/utils/log';
1010

1111
export default s.route(contract.tool.upload.delete, async ({ query: { toolId } }) => {
12-
addLog.debug(`Deleting tool: ${toolId}`);
1312
const res = await mongoSessionRun(async (session) => {
1413
const result = await MongoSystemPlugin.findOneAndDelete({ toolId }).session(session);
1514
if (!result || !result.toolId) {

modules/tool/init.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ export async function initTools() {
3030
}
3131
global.isIniting = true;
3232
try {
33+
const start = Date.now();
34+
addLog.info(`Load tools start`);
35+
3336
await refreshDir(toolsDir);
3437
// 1. download pkgs into pkg dir
3538
// 1.1 get tools from mongo
@@ -38,29 +41,23 @@ export async function initTools() {
3841
}).lean();
3942

4043
addLog.debug(`Tools in mongo: ${toolsInMongo.length}`);
41-
// 1.2 download it to temp dir
44+
const toolMap: ToolMapType = new Map();
45+
46+
// 2 download it to temp dir, and parse it
4247
await batch(
4348
10,
44-
toolsInMongo.map(
45-
(tool) => () =>
46-
privateS3Server.downloadFile({
47-
downloadPath: toolsDir,
48-
objectName: `${UploadToolsS3Path}/${tool.toolId}.js`
49-
})
50-
)
49+
toolsInMongo.map((tool) => async () => {
50+
const filepath = await privateS3Server.downloadFile({
51+
downloadPath: toolsDir,
52+
objectName: `${UploadToolsS3Path}/${tool.toolId}.js`
53+
});
54+
if (!filepath) return;
55+
const filename = filepath.replace(`${toolsDir}/`, '');
56+
const loadedTools = await LoadToolsByFilename(filename);
57+
loadedTools.forEach((tool) => toolMap.set(tool.toolId, tool));
58+
})
5159
);
5260

53-
// 2. get all tool dirs
54-
addLog.debug(`Load tool in local: ${toolsInMongo.length}`);
55-
const toolFiles = await readdir(toolsDir);
56-
const toolMap: ToolMapType = new Map();
57-
58-
const promises = toolFiles.map(async (filename) => {
59-
const loadedTools = await LoadToolsByFilename(filename);
60-
loadedTools.forEach((tool) => toolMap.set(tool.toolId, tool));
61-
});
62-
await Promise.all(promises);
63-
6461
// 3. read dev tools, if in dev mode
6562
if (!isProd && process.env.DISABLE_DEV_TOOLS !== 'true') {
6663
const dir = join(basePath, 'modules', 'tool', 'packages');
@@ -81,11 +78,11 @@ export async function initTools() {
8178
}
8279
}
8380

84-
addLog.info(`Load Tools: ${toolMap.size}`);
81+
addLog.info(`Load tools finish: ${toolMap.size}, time: ${Date.now() - start}ms`);
8582
global.isIniting = false;
8683
return toolMap;
8784
} catch (e) {
88-
addLog.error(`Init Tools Error:`, e);
85+
addLog.error(`Load tools Error:`, e);
8986
global.isIniting = false;
9087
return getCachedData(SystemCacheKeyEnum.systemTool);
9188
}

modules/tool/loadToolProd.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,18 @@ import type { ToolSetType, ToolType } from './type';
33
import { addLog } from '@/utils/log';
44
import { join } from 'path';
55
import { parseMod } from './parseMod';
6-
import { createHash } from 'crypto';
7-
import { readFile } from 'fs/promises';
6+
import { stat } from 'fs/promises';
87

98
// Load tool or toolset and its children
109
export const LoadToolsByFilename = async (filename: string): Promise<ToolType[]> => {
10+
const start = Date.now();
11+
1112
const filePath = join(toolsDir, filename);
1213

1314
// Calculate file content hash for cache key
14-
// Same content = same hash = reuse cache, reducing memory usage
15-
const fileContent = await readFile(filePath);
16-
const contentHash = createHash('md5').update(fileContent).digest('hex').slice(0, 8);
17-
18-
// Clear module cache in Node.js to prevent memory leaks
19-
// @ts-ignore - require.cache only exists in Node.js, not in Bun
20-
if (typeof require !== 'undefined' && require.cache) {
21-
// Try to delete cache entries (works for CJS modules)
22-
delete require.cache[filePath];
23-
delete require.cache[require.resolve?.(filePath) || filePath];
24-
}
25-
26-
// Use content hash as cache buster (works for ESM in both Node.js and Bun)
15+
const fileSize = await stat(filePath).then((res) => res.size);
2716
// This ensures same content reuses the same cached module
28-
const modulePath = `${filePath}?v=${contentHash}`;
17+
const modulePath = `${filePath}?v=${fileSize}`;
2918

3019
const rootMod = (await import(modulePath)).default as ToolType | ToolSetType;
3120

@@ -34,5 +23,7 @@ export const LoadToolsByFilename = async (filename: string): Promise<ToolType[]>
3423
return [];
3524
}
3625

26+
addLog.debug(`Load tool ${filename} finish, time: ${Date.now() - start}ms`);
27+
3728
return parseMod({ rootMod, filename });
3829
};

runtime/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { refreshVersionKey } from '@/cache';
1+
import { getCachedData, refreshVersionKey } from '@/cache';
22
import { SystemCacheKeyEnum } from '@/cache/type';
33
import { isProd } from '@/constants';
44
import { initOpenAPI } from '@/contract/openapi';
@@ -50,8 +50,9 @@ async function main(reboot: boolean = false) {
5050
await refreshDir(tempDir); // upload pkg files, unpkg, temp dir
5151
await ensureDir(tempToolsDir); // ensure the unpkged tools temp dir
5252

53+
await refreshVersionKey(SystemCacheKeyEnum.systemTool); // init system tool
5354
await Promise.all([
54-
refreshVersionKey(SystemCacheKeyEnum.systemTool), // init system tool
55+
getCachedData(SystemCacheKeyEnum.systemTool), // init system tool
5556
initModels(reboot),
5657
initWorkflowTemplates()
5758
]);

0 commit comments

Comments
 (0)