-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* plugins: add wiki search * 扁平化代码 * fix: url error
- Loading branch information
1 parent
fe6c889
commit daa5b55
Showing
7 changed files
with
632 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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; |
Oops, something went wrong.