Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sdream-ai-fe
Overview
Overview
Details
Activity
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
侯明涛
sdream-ai-fe
Commits
e17c6656
Commit
e17c6656
authored
Feb 11, 2026
by
Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:流式请求增加超时时间30分钟
parent
d77ff208
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
17 deletions
+25
-17
src/api/chat.ts
+25
-17
No files found.
src/api/chat.ts
View file @
e17c6656
...
...
@@ -7,18 +7,29 @@ import { http } from '@/utils/request'
export
function
fetchCheckTokenApi
()
{
return
http
.
post
(
'/user/api/user_center/mobile/v1/check_token'
,
{})
}
const
STREAM_TIMEOUT_MS
=
30
*
60
*
1000
// 流式请求超时:半小时
export
function
fetchStreamResponse
(
url
:
string
,
body
:
Record
<
string
,
any
>
,
onMessage
:
(
msg
:
any
)
=>
void
,
signal
?:
AbortSignal
):
Promise
<
void
>
{
body
.
stream
=
true
const
decoder
=
new
TextDecoder
(
'utf-8'
)
let
buffer
=
''
let
dataMsgBuffer
=
''
const
controller
=
new
AbortController
()
const
timeoutId
=
setTimeout
(()
=>
controller
.
abort
(),
STREAM_TIMEOUT_MS
)
if
(
signal
)
{
signal
.
addEventListener
(
'abort'
,
()
=>
controller
.
abort
())
}
const
effectiveSignal
=
controller
.
signal
const
cleanup
=
()
=>
clearTimeout
(
timeoutId
)
return
new
Promise
<
void
>
((
resolve
,
reject
)
=>
{
function
processMessage
(
reader
:
any
)
{
reader
.
read
().
then
((
content
:
any
)
=>
{
// 检查是否已被中止
if
(
signal
?.
aborted
)
{
resolve
()
// 中止时也 resolve,表示流已结束
if
(
effectiveSignal
.
aborted
)
{
cleanup
()
resolve
()
return
}
buffer
+=
decoder
.
decode
(
content
.
value
,
{
stream
:
!
content
.
done
})
...
...
@@ -61,20 +72,18 @@ export function fetchStreamResponse(url: string, body: Record<string, any>, onMe
processMessage
(
reader
)
}
else
{
// 流结束,resolve Promise(不再发送 END 消息)
cleanup
()
resolve
()
}
}).
catch
((
error
:
unknown
)
=>
{
// 如果是 AbortError,resolve 而不是 reject(因为这是主动取消)
if
(
error
instanceof
Error
&&
error
.
name
===
'AbortError'
)
{
cleanup
()
resolve
()
return
}
onMessage
({
type
:
'ERROR'
,
content
:
error
,
})
reject
(
error
)
// 其他错误 reject Promise
onMessage
({
type
:
'ERROR'
,
content
:
error
})
cleanup
()
reject
(
error
)
})
}
const
tokenStr
=
window
.
localStorage
.
getItem
(
'__TOKEN__'
)
||
'""'
...
...
@@ -94,30 +103,29 @@ export function fetchStreamResponse(url: string, body: Record<string, any>, onMe
},
method
:
'POST'
,
body
:
JSON
.
stringify
(
body
),
signal
,
signal
:
effectiveSignal
,
})
.
then
((
response
)
=>
{
return
response
.
body
?.
getReader
()
})
.
then
((
reader
)
=>
{
if
(
!
reader
)
{
cleanup
()
reject
(
new
Error
(
'Failed to get reader from response'
))
return
}
return
processMessage
(
reader
)
})
.
catch
((
error
:
unknown
)
=>
{
// 如果是 AbortError,resolve 而不是 reject(因为这是主动取消)
if
(
error
instanceof
Error
&&
error
.
name
===
'AbortError'
)
{
cleanup
()
resolve
()
return
}
onMessage
({
type
:
'ERROR'
,
content
:
error
,
})
reject
(
error
)
})
onMessage
({
type
:
'ERROR'
,
content
:
error
})
cleanup
()
reject
(
error
)
})
})
}
...
...
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