v1.2 优化保存
This commit is contained in:
+80
-1
@@ -44,6 +44,84 @@ import type { AppConfig, OpenAIMessage } from '../types'
|
||||
*/
|
||||
export const REQUEST_TIMEOUT = 300000
|
||||
|
||||
/**
|
||||
* 流式**空闲**超时(idle,不是总时长)。
|
||||
*
|
||||
* 语义:**连续 60 秒一个字节都没到** = 连接已死(对端挂了 / 中间设备静默断开)。
|
||||
* 必须远高于实测的分片节奏(20~45ms 一批,见 AGENTS.md §7),否则正常流会被误杀;
|
||||
* 也**绝不能**当成总时长预算 —— 长工具调用链(模型思考 + 多轮 tool_calls)
|
||||
* 合法地超过任何总时长,只有「完全无数据」才该判死。
|
||||
*
|
||||
* `REQUEST_TIMEOUT` 只覆盖到响应头到达为止(`sendChatRequest` 的 fetch 阶段),
|
||||
* 头到了之后定时器即清除,body 阶段由这个空闲看门狗接管。
|
||||
*/
|
||||
export const STREAM_IDLE_TIMEOUT = 60000
|
||||
|
||||
/** 流式空闲看门狗。 */
|
||||
export interface StreamWatchdog {
|
||||
/** 空闲超时或外部 signal 中止时 abort。 */
|
||||
signal: AbortSignal
|
||||
/** 喂狗:收到数据后调用,重置空闲计时。 */
|
||||
touch(): void
|
||||
/** 清掉计时器与外部监听,防止泄漏。dispose 后不要再 touch。 */
|
||||
dispose(): void
|
||||
/** 本次 abort 是否由空闲超时触发(区别于外部 signal 的用户中止)。 */
|
||||
didTimeOut(): boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* 流式读取的空闲看门狗。
|
||||
*
|
||||
* 自己持有 AbortController;`idleMs` 内没有任何 `touch()` 就 abort 并标记超时。
|
||||
* 外部 `signal`(用户按停止)也会让它 abort,但**不**标记超时 —— 用 `didTimeOut()`
|
||||
* 区分「连接死了」与「用户主动停」。
|
||||
*
|
||||
* 刻意用 `window.setTimeout` + `AbortController`,不用 `AbortSignal.timeout()`
|
||||
* (旧 TS lib 目标没有这个类型)。
|
||||
*/
|
||||
export function createStreamWatchdog(externalSignal: AbortSignal | undefined, idleMs: number): StreamWatchdog {
|
||||
const controller = new AbortController()
|
||||
let timedOut = false
|
||||
|
||||
const arm = (): number => {
|
||||
return window.setTimeout(() => {
|
||||
timedOut = true
|
||||
controller.abort()
|
||||
}, idleMs)
|
||||
}
|
||||
|
||||
let timerId = arm()
|
||||
|
||||
const onExternalAbort = (): void => {
|
||||
timedOut = false
|
||||
controller.abort()
|
||||
}
|
||||
|
||||
if (externalSignal) {
|
||||
if (externalSignal.aborted) {
|
||||
window.clearTimeout(timerId)
|
||||
onExternalAbort()
|
||||
} else {
|
||||
externalSignal.addEventListener('abort', onExternalAbort, { once: true })
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
signal: controller.signal,
|
||||
touch() {
|
||||
window.clearTimeout(timerId)
|
||||
timerId = arm()
|
||||
},
|
||||
dispose() {
|
||||
window.clearTimeout(timerId)
|
||||
externalSignal?.removeEventListener('abort', onExternalAbort)
|
||||
},
|
||||
didTimeOut() {
|
||||
return timedOut
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 工具定义。**形状与 OpenAI 兼容接口一致**,但这里自己定义而不 import
|
||||
* —— 传输层不得依赖 `bridge/`(`AGENTS.md §4.2`)。
|
||||
@@ -77,11 +155,12 @@ export interface ToolOptions {
|
||||
*
|
||||
* `kind` 是提示策略的唯一依据:
|
||||
* - `abort` 用户主动中断 —— 静默,由调用方走「已停止」分支
|
||||
* - `timeout` 流式空闲超时(连接死了但没报错)—— 必须提示,与用户中止区分开
|
||||
* - `network` 跨域 / 连不上 —— 必须提示,否则用户只看到「没反应」
|
||||
* - `http` 服务端返回了非 2xx —— 必须提示,message 已译成人话
|
||||
* - `unknown` 兜底
|
||||
*/
|
||||
export type TransportErrorKind = 'abort' | 'network' | 'http' | 'unknown'
|
||||
export type TransportErrorKind = 'abort' | 'timeout' | 'network' | 'http' | 'unknown'
|
||||
|
||||
export class ChatTransportError extends Error {
|
||||
readonly kind: TransportErrorKind
|
||||
|
||||
Reference in New Issue
Block a user