Skip to content

Commit 2b6badc

Browse files
committed
feat: add APIGetTemplate function and refactor template fetching logic
1 parent c67e645 commit 2b6badc

File tree

18 files changed

+15
-10199
lines changed

18 files changed

+15
-10199
lines changed

packages/service/core/app/tool/api.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ export async function APIGetSystemToolList() {
2121

2222
return Promise.reject(res.body);
2323
}
24+
export async function APIGetTemplate() {
25+
const res = await pluginClient.tool.getTemplateList();
26+
if (res.status === 200) {
27+
return res.body.map((item) => {
28+
return {
29+
...item,
30+
templateId: `community-${item.templateId}`
31+
};
32+
});
33+
}
34+
35+
return Promise.reject(res.body);
36+
}
2437

2538
const runToolInstance = new RunToolWithStream({
2639
baseUrl: BASE_URL,

packages/templates/register.ts

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,11 @@
1-
import fs from 'fs';
2-
import path from 'path';
31
import { isProduction } from '@fastgpt/global/common/system/constants';
42
import { PluginSourceEnum } from '@fastgpt/global/core/app/plugin/constants';
53
import { MongoAppTemplate } from '@fastgpt/service/core/app/templates/templateSchema';
64
import { type AppTemplateSchemaType } from '@fastgpt/global/core/app/type';
7-
8-
const getTemplateNameList = () => {
9-
const currentFileUrl = new URL(import.meta.url);
10-
const filePath = decodeURIComponent(
11-
process.platform === 'win32'
12-
? currentFileUrl.pathname.substring(1) // Remove leading slash on Windows
13-
: currentFileUrl.pathname
14-
);
15-
const templatesPath = path.join(path.dirname(filePath), 'src');
16-
17-
return fs.promises.readdir(templatesPath);
18-
};
5+
import { APIGetTemplate } from '@fastgpt/service/core/app/tool/api';
196

207
const getFileTemplates = async (): Promise<AppTemplateSchemaType[]> => {
21-
const templateNames = await getTemplateNameList();
22-
23-
return Promise.all(
24-
templateNames.map<Promise<AppTemplateSchemaType>>(async (name) => {
25-
const fileContent = (await import(`./src/${name}/template.json`))?.default;
26-
27-
return {
28-
...fileContent,
29-
templateId: `${PluginSourceEnum.community}-${name}`,
30-
isActive: true
31-
};
32-
})
33-
);
8+
return (await APIGetTemplate()) as AppTemplateSchemaType[];
349
};
3510

3611
const getAppTemplates = async () => {

0 commit comments

Comments
 (0)