Skip to content

Commit daa5b55

Browse files
authored
plugins: add wiki search (labring#2886)
* plugins: add wiki search * 扁平化代码 * fix: url error
1 parent fe6c889 commit daa5b55

File tree

7 files changed

+632
-70
lines changed

7 files changed

+632
-70
lines changed

packages/plugins/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
"version": "1.0.0",
44
"type": "module",
55
"dependencies": {
6-
"duck-duck-scrape": "^2.2.5",
7-
"lodash": "^4.17.21",
86
"axios": "^1.5.1",
7+
"duck-duck-scrape": "^2.2.5",
8+
"echarts": "5.4.1",
99
"expr-eval": "^2.0.2",
10-
"echarts": "5.4.1"
10+
"lodash": "^4.17.21",
11+
"wikijs": "^6.4.1"
1112
},
1213
"devDependencies": {
1314
"@fastgpt/global": "workspace:*",

packages/plugins/register.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const packagePluginList = [
2424
'duckduckgo/searchNews',
2525
'duckduckgo/searchVideo',
2626
'drawing',
27-
'drawing/baseChart'
27+
'drawing/baseChart',
28+
'wiki'
2829
];
2930

3031
export const list = [...staticPluginList, ...packagePluginList];

packages/plugins/src/wiki/index.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { getErrText } from '@fastgpt/global/common/error/utils';
2+
import { addLog } from '@fastgpt/service/common/system/log';
3+
import { delay } from '@fastgpt/global/common/system/utils';
4+
import wiki from 'wikijs';
5+
6+
type Props = {
7+
query: string;
8+
};
9+
10+
// Response type same as HTTP outputs
11+
type Response = Promise<{
12+
result: string;
13+
}>;
14+
15+
const main = async (props: Props, retry = 3): Response => {
16+
const { query } = props;
17+
18+
try {
19+
const searchResults = await wiki({ apiUrl: 'https://zh.wikipedia.org/w/api.php' })
20+
.page(query)
21+
.then((page) => {
22+
return page.summary();
23+
});
24+
25+
return {
26+
result: searchResults
27+
};
28+
} catch (error) {
29+
console.log(error);
30+
31+
if (retry <= 0) {
32+
addLog.warn('search wiki error', { error });
33+
return {
34+
result: getErrText(error, 'Failed to fetch data from wiki')
35+
};
36+
}
37+
38+
await delay(Math.random() * 5000);
39+
return main(props, retry - 1);
40+
}
41+
};
42+
43+
export default main;

0 commit comments

Comments
 (0)