This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
feat: update fetch configuration logic #65
Closed
Closed
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fb8cf57
Fix mistral client when using recent node
HenriJ be90bbe
Update package.json version to match user-agent version num
alexleventer 7f0ec34
add usage property to completion chunk type
dardanbujupaj f57a868
add tool_calls in the signature of chat response
bmichotte 4d64188
Merge remote-tracking branch 'HenriJ/patch-1' into combine-0.0.4
sublimator 41c7093
Merge remote-tracking branch 'alexleventer/main' into combine-0.0.4
sublimator 7b70038
Merge remote-tracking branch 'dardanbujupaj/stream-usage' into combin…
sublimator 76d447b
Merge remote-tracking branch 'bmichotte/main' into combine-0.0.4
sublimator 52a8a4b
feat: update fetch configuration logic
sublimator cf56320
feat: apply @fuegoio's simplification
sublimator 6e2dd95
fix: typo
sublimator ded00ec
fix: future proof for commonjs
sublimator 6d8cbaf
Revert "add tool_calls in the signature of chat response"
sublimator 848d539
Revert "add usage property to completion chunk type"
sublimator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,14 @@ | ||
let isNode = false; | ||
|
||
const VERSION = '0.0.3'; | ||
const RETRY_STATUS_CODES = [429, 500, 502, 503, 504]; | ||
const ENDPOINT = 'https://api.mistral.ai'; | ||
|
||
/** | ||
* Initialize fetch | ||
* @return {Promise<void>} | ||
*/ | ||
async function initializeFetch() { | ||
if (typeof window === 'undefined' || | ||
typeof globalThis.fetch === 'undefined') { | ||
const nodeFetch = await import('node-fetch'); | ||
fetch = nodeFetch.default; | ||
isNode = true; | ||
} else { | ||
fetch = globalThis.fetch; | ||
} | ||
} | ||
const isNode = typeof process !== 'undefined' && | ||
process.versions != null && | ||
process.versions.node != null; | ||
const haveNativeFetch = typeof globalThis.fetch !== 'undefined'; | ||
|
||
initializeFetch(); | ||
const configuredFetch = isNode && !haveNativeFetch ? | ||
(await import('node-fetch')).default : globalThis.fetch; | ||
|
||
/** | ||
* MistralAPIError | ||
|
@@ -67,6 +56,16 @@ class MistralClient { | |
} | ||
} | ||
|
||
/** | ||
* @return {Promise} | ||
* @private | ||
* @param {...*} args - fetch args | ||
* hook point for non-global fetch override | ||
*/ | ||
async _fetch(...args) { | ||
return configuredFetch(...args); | ||
} | ||
|
||
/** | ||
* | ||
* @param {*} method | ||
|
@@ -90,11 +89,13 @@ class MistralClient { | |
|
||
for (let attempts = 0; attempts < this.maxRetries; attempts++) { | ||
try { | ||
const response = await fetch(url, options); | ||
const response = await this._fetch(url, options); | ||
|
||
if (response.ok) { | ||
if (request?.stream) { | ||
if (isNode) { | ||
if (isNode && !haveNativeFetch || | ||
// The test mocks do not return a body with getReader | ||
typeof response.body.getReader === 'undefined') { | ||
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. I think here we can remove the condition on |
||
return response.body; | ||
} else { | ||
const reader = response.body.getReader(); | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think here we should only use the
haveNativeFetch
condition no? Is there a case whereisNode
would befalse
andhaveNativeFetch
befalse
as well?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.
Back on laptop :) Thanks for the prodding
Went with this and killed off haveNativeFetch/isNode entirely by just using feature detection, and adding a comment