-
-
Notifications
You must be signed in to change notification settings - Fork 24.8k
fix: add timeout to prompt list fetch #6644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import axios from 'axios' | ||
| import promptsListsService from '.' | ||
|
|
||
| jest.mock('axios') | ||
|
|
||
| const mockedAxios = axios as jest.Mocked<typeof axios> | ||
|
|
||
| describe('promptsListsService.createPromptsList', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| it('fetches LangChain Hub prompts with a bounded timeout', async () => { | ||
| const repos = [{ id: 'owner/prompt', num_likes: 42 }] | ||
| mockedAxios.get.mockResolvedValue({ data: { repos } }) | ||
|
|
||
| const result = await promptsListsService.createPromptsList({}) | ||
|
|
||
| expect(mockedAxios.get).toHaveBeenCalledWith( | ||
| 'https://api.hub.langchain.com/repos/?limit=100&has_commits=true&sort_field=num_likes&sort_direction=desc&is_archived=false', | ||
| { timeout: 10000 } | ||
| ) | ||
| expect(result).toEqual({ | ||
| status: 'OK', | ||
| repos | ||
| }) | ||
| }) | ||
|
|
||
| it('returns the existing fallback when LangChain Hub request fails', async () => { | ||
| mockedAxios.get.mockRejectedValue(new Error('timeout')) | ||
|
|
||
| await expect(promptsListsService.createPromptsList({})).resolves.toEqual({ | ||
| status: 'ERROR', | ||
| repos: [] | ||
| }) | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| import axios from 'axios' | ||
|
|
||
| const LANGCHAIN_HUB_PROMPTS_TIMEOUT_MS = 10000 | ||
|
|
||
| const createPromptsList = async (requestBody: any) => { | ||
| try { | ||
| const tags = requestBody.tags ? `tags=${requestBody.tags}` : '' | ||
| // Default to 100, TODO: add pagination and use offset & limit | ||
| const url = `https://api.hub.langchain.com/repos/?limit=100&${tags}has_commits=true&sort_field=num_likes&sort_direction=desc&is_archived=false` | ||
| const resp = await axios.get(url) | ||
| const resp = await axios.get(url, { timeout: LANGCHAIN_HUB_PROMPTS_TIMEOUT_MS }) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a bug in the URL construction on line 9 when Additionally, if To fix both issues, consider refactoring the URL construction using const createPromptsList = async (requestBody: any) => {
try {
const params = new URLSearchParams({
limit: '100',
has_commits: 'true',
sort_field: 'num_likes',
sort_direction: 'desc',
is_archived: 'false'
})
if (requestBody.tags) {
params.append('tags', requestBody.tags)
}
const url = `https://api.hub.langchain.com/repos/?${params.toString()}`
const resp = await axios.get(url, { timeout: LANGCHAIN_HUB_PROMPTS_TIMEOUT_MS })
if (resp.data?.repos) {
return {
status: 'OK',
repos: resp.data.repos
}
}
return { status: 'ERROR', repos: [] }
} catch (error) {
return { status: 'ERROR', repos: [] }
}
} |
||
| if (resp.data.repos) { | ||
| return { | ||
| status: 'OK', | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test case to verify that the
tagsparameter is correctly appended to the URL query parameters. This will help prevent regressions and verify the fix for the URL query parameter formatting bug.