Files
FastGPT/packages/plugins/src/wiki/index.ts
papapatrick 701643f2ab plugins: add wiki search (#2886)
* plugins: add wiki search

* 扁平化代码

* fix: url error
2024-10-12 15:23:01 +08:00

44 lines
974 B
TypeScript

import { getErrText } from '@fastgpt/global/common/error/utils';
import { addLog } from '@fastgpt/service/common/system/log';
import { delay } from '@fastgpt/global/common/system/utils';
import wiki from 'wikijs';
type Props = {
query: string;
};
// Response type same as HTTP outputs
type Response = Promise<{
result: string;
}>;
const main = async (props: Props, retry = 3): Response => {
const { query } = props;
try {
const searchResults = await wiki({ apiUrl: 'https://zh.wikipedia.org/w/api.php' })
.page(query)
.then((page) => {
return page.summary();
});
return {
result: searchResults
};
} catch (error) {
console.log(error);
if (retry <= 0) {
addLog.warn('search wiki error', { error });
return {
result: getErrText(error, 'Failed to fetch data from wiki')
};
}
await delay(Math.random() * 5000);
return main(props, retry - 1);
}
};
export default main;