Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: https://liuyun.io/article/1783085681823

Daniel13a021f02d02
diff --git a/kernel/agent/agent.go b/kernel/agent/agent.go
index f916d3a4c2..52e502b394 100644
--- a/kernel/agent/agent.go
+++ b/kernel/agent/agent.go
@@ -356,7 +356,7 @@ type agentCheckpoint struct {
ContextLimit int `json:"contextLimit,omitempty"`
}
-func AgentChat(ctx context.Context, client *openai.Client, model string, sessionID string, userMessage string, language string, references []Reference, editorCtx EditorContext, pluginActions []PluginAction, regenerate bool, confirmTimeout time.Duration, maxRetries int, reasoningEffort string) <-chan AgentEvent {
+func AgentChat(ctx context.Context, client *openai.Client, model string, sessionID string, userMessage string, language string, references []Reference, editorCtx EditorContext, pluginActions []PluginAction, regenerate bool, confirmTimeout time.Duration, maxRetries int, reasoningEffort string, requestTimeout time.Duration) <-chan AgentEvent {
ch := make(chan AgentEvent, 256)
go func() {
@@ -465,8 +465,16 @@ func AgentChat(ctx context.Context, client *openai.Client, model string, session
ReasoningEffort: reasoningEffort,
}
- stream, streamErr := createStreamWithRetry(ctx, client, req, maxRetries, ch)
+ // 为每轮单次上游调用派生独立子 context,便于把单轮卡死转为可重试错误,
+ // 而不是直接取消整会话。requestTimeout<=0 时退化为可取消但无超时的 context。
+ roundCtx, roundCancel := context.WithCancel(ctx)
+ if requestTimeout > 0 {
+ roundCtx, roundCancel = context.WithTimeout(ctx, requestTimeout)
+ }
+
+ stream, streamErr := createStreamWithRetry(roundCtx, client, req, maxRetries, ch)
if streamErr != nil {
+ roundCancel()
if compactCount < 3 && isContextOverflow(streamErr) {
keepTurns := 3 - compactCount
if keepTurns < 1 {
@@ -502,12 +510,14 @@ func AgentChat(ctx context.Context, client *openai.Client, model string, session
}
finalCheckpoint()
stream.Close()
+ roundCancel()
return
}
select {
case <-ctx.Done():
stream.Close()
+ roundCancel()
return
default:
}
@@ -556,6 +566,7 @@ func AgentChat(ctx context.Context, client *openai.Client, model string, session
}
stream.Close()
+ roundCancel()
if len(aggregatedToolCalls) > 0 {
filtered := make([]openai.ToolCall, 0, len(aggregatedToolCalls))
@@ -1462,9 +1473,15 @@ func classifyRetry(err error) string {
if strings.Contains(msg, "Bad Request") {
return "fatal"
}
- if strings.Contains(msg, "context canceled") || strings.Contains(msg, "context deadline exceeded") {
+ // 父 context 被取消(用户停止 / 会话结束)属于不可重试的致命错误。
+ if errors.Is(err, context.Canceled) {
return "fatal"
}
+ // per-request 超时(本会话主动加的 roundCtx deadline)通常意味着对端只是慢,
+ // 归入 timeout 类走退避重试,避免一次卡顿直接终结整个会话。
+ if errors.Is(err, context.DeadlineExceeded) {
+ return "timeout"
+ }
return "network"
}
diff --git a/kernel/api/agent.go b/kernel/api/agent.go
index 00700b1070..0d243ea761 100644
--- a/kernel/api/agent.go
+++ b/kernel/api/agent.go
@@ -95,11 +95,17 @@ func agentChat(c *gin.Context) {
if maxRetries <= 0 {
maxRetries = 3
}
+ // per-request 超时:作用到 agent 每一轮上游 chat-completions 调用上,
+ // 让单次卡死的请求转为可重试错误,而不是干等到整会话超时。
+ requestTimeout := time.Duration(selectedProvider.RequestTimeout) * time.Second
+ if requestTimeout <= 0 {
+ requestTimeout = 30 * time.Second
+ }
app := c.GetHeader("X-SiYuan-App-ID")
ctx, cancel := context.WithCancel(c.Request.Context())
- eventCh := agent.AgentChat(ctx, client, selectedModel.Name, req.SessionID, req.Message, req.Language, req.References, req.EditorContext, req.PluginActions, req.Regenerate, confirmTimeout, maxRetries, req.ReasoningEffort)
+ eventCh := agent.AgentChat(ctx, client, selectedModel.Name, req.SessionID, req.Message, req.Language, req.References, req.EditorContext, req.PluginActions, req.Regenerate, confirmTimeout, maxRetries, req.ReasoningEffort, requestTimeout)
// 实例级互斥:同一 session 同时只允许一个活跃流。
// 检查+占用在同一把锁内(compare-and-set),失败时 cancel 释放刚启动的 goroutine 防泄漏。
@@ -131,13 +137,9 @@ func agentChat(c *gin.Context) {
return
}
- timeout := selectedProvider.RequestTimeout
- if timeout <= 0 {
- timeout = 30
- }
totalTimeout := time.Duration(model.Conf.AI.Agent.SessionTimeout) * time.Second
if totalTimeout <= 0 {
- totalTimeout = time.Duration(timeout) * time.Second * 10
+ totalTimeout = 600 * time.Second
}
if totalTimeout > 3600*time.Second {
totalTimeout = 3600 * time.Second