Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
657334a5
Unverified
Commit
657334a5
authored
Nov 20, 2023
by
Ricky
Committed by
GitHub
Nov 20, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: fetch stream compatibility enhance (#1551)
parent
31195975
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
33 additions
and
2 deletions
+33
-2
global.d.ts
web/global.d.ts
+4
-1
package.json
web/package.json
+1
-0
base.ts
web/service/base.ts
+7
-1
stream.ts
web/utils/stream.ts
+21
-0
No files found.
web/global.d.ts
View file @
657334a5
declare
module
'lamejs'
;
declare
module
'react-18-input-autosize'
;
\ No newline at end of file
declare
module
'react-18-input-autosize'
;
declare
module
'fetch-readablestream'
{
export
default
function
fetchReadableStream
(
url
:
string
,
options
?:
RequestInit
):
Promise
<
Response
>
}
web/package.json
View file @
657334a5
...
...
@@ -36,6 +36,7 @@
"echarts"
:
"^5.4.1"
,
"echarts-for-react"
:
"^3.0.2"
,
"emoji-mart"
:
"^5.5.2"
,
"fetch-readablestream"
:
"^0.2.0"
,
"i18next"
:
"^22.4.13"
,
"i18next-resources-to-backend"
:
"^1.1.3"
,
"immer"
:
"^9.0.19"
,
...
...
web/service/base.ts
View file @
657334a5
import
fetchStream
from
'fetch-readablestream'
import
{
API_PREFIX
,
IS_CE_EDITION
,
PUBLIC_API_PREFIX
}
from
'@/config'
import
Toast
from
'@/app/components/base/toast'
import
type
{
MessageEnd
,
MessageReplace
,
ThoughtItem
}
from
'@/app/components/app/chat/type'
import
{
isSupportNativeFetchStream
}
from
'@/utils/stream'
const
TIME_OUT
=
100000
const
supportNativeFetchStream
=
isSupportNativeFetchStream
()
const
ContentType
=
{
json
:
'application/json'
,
...
...
@@ -220,6 +223,9 @@ const baseFetch = <T>(
if
(
body
&&
bodyStringify
)
options
.
body
=
JSON
.
stringify
(
body
)
// for those do not support native fetch stream, we use fetch-readablestream as polyfill
const
doFetch
=
supportNativeFetchStream
?
globalThis
.
fetch
:
fetchStream
// Handle timeout
return
Promise
.
race
([
new
Promise
((
resolve
,
reject
)
=>
{
...
...
@@ -228,7 +234,7 @@ const baseFetch = <T>(
},
TIME_OUT
)
}),
new
Promise
((
resolve
,
reject
)
=>
{
globalThis
.
f
etch
(
urlWithPrefix
,
options
as
RequestInit
)
doF
etch
(
urlWithPrefix
,
options
as
RequestInit
)
.
then
((
res
)
=>
{
const
resClone
=
res
.
clone
()
// Error handler
...
...
web/utils/stream.ts
0 → 100644
View file @
657334a5
// https://developer.chrome.com/articles/fetch-streaming-requests/#feature-detection
export
const
isSupportNativeFetchStream
=
()
=>
{
const
supportsRequestStreams
=
(()
=>
{
let
duplexAccessed
=
false
const
params
=
{
body
:
new
ReadableStream
(),
method
:
'POST'
,
get
duplex
()
{
duplexAccessed
=
true
return
'half'
},
}
const
hasContentType
=
new
Request
(
''
,
params
).
headers
.
has
(
'Content-Type'
)
return
duplexAccessed
&&
!
hasContentType
})()
return
supportsRequestStreams
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment