Skip to content

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

:art: Raise the agent session timeout default to 30 minutes

Danielbc7743046490
diff --git a/kernel/api/agent.go b/kernel/api/agent.go
index 1e0a9807df..e91795def8 100644
--- a/kernel/api/agent.go
+++ b/kernel/api/agent.go
@@ -195,14 +195,23 @@ func agentChat(c *gin.Context) {
}
}
-func newAgentSessionDeadline(timeoutSeconds int) (*time.Timer, <-chan time.Time) {
+// sessionDeadlineTimeoutSeconds 解析会话总超时秒数:小于等于 0 表示不限制,超过上限时按上限截断。
+func sessionDeadlineTimeoutSeconds(timeoutSeconds int) (seconds int, unlimited bool) {
if timeoutSeconds <= 0 {
- return nil, nil
+ return 0, true
}
- if timeoutSeconds > 3600 {
- timeoutSeconds = 3600
+ if timeoutSeconds > conf.MaxAgentSessionTimeout {
+ timeoutSeconds = conf.MaxAgentSessionTimeout
+ }
+ return timeoutSeconds, false
+}
+
+func newAgentSessionDeadline(timeoutSeconds int) (*time.Timer, <-chan time.Time) {
+ seconds, unlimited := sessionDeadlineTimeoutSeconds(timeoutSeconds)
+ if unlimited {
+ return nil, nil
}
- timer := time.NewTimer(time.Duration(timeoutSeconds) * time.Second)
+ timer := time.NewTimer(time.Duration(seconds) * time.Second)
return timer, timer.C
}
diff --git a/kernel/api/agent_timeout_test.go b/kernel/api/agent_timeout_test.go
index 2cc50e028f..4d67380085 100644
--- a/kernel/api/agent_timeout_test.go
+++ b/kernel/api/agent_timeout_test.go
@@ -25,6 +25,22 @@ func TestNewAgentSessionDeadlineZeroHasNoLimit(t *testing.T) {
}
}
+func TestSessionDeadlineTimeoutSecondsSkippedWhenUnlimited(t *testing.T) {
+ if _, skipped := sessionDeadlineTimeoutSeconds(0); !skipped {
+ t.Fatal("zero session timeout was not treated as unlimited")
+ }
+}
+
+func TestSessionDeadlineTimeoutSecondsCapsAtMax(t *testing.T) {
+ seconds, skipped := sessionDeadlineTimeoutSeconds(conf.MaxAgentSessionTimeout + 1)
+ if skipped || seconds != conf.MaxAgentSessionTimeout {
+ t.Fatalf("session timeout above the max = %d, skipped=%v", seconds, skipped)
+ }
+ if seconds, skipped = sessionDeadlineTimeoutSeconds(30); skipped || seconds != 30 {
+ t.Fatalf("session timeout below the max = %d, skipped=%v", seconds, skipped)
+ }
+}
+
func TestResolveAgentConfirmTimeout(t *testing.T) {
if conf.DefaultAgentConfirmTimeout != 600 {
t.Fatalf("default confirmation timeout changed: %d", conf.DefaultAgentConfirmTimeout)
diff --git a/kernel/conf/ai.go b/kernel/conf/ai.go
index 1d2c360fba..f8ce12bea0 100644
--- a/kernel/conf/ai.go
+++ b/kernel/conf/ai.go
@@ -42,6 +42,12 @@ type AI struct {
// DefaultAgentConfirmTimeout 是智能体确认超时时间的默认值(秒),也用于智能体提问的等待时长。
const DefaultAgentConfirmTimeout = 600
+// DefaultAgentSessionTimeout 是智能体会话总超时时间的默认值(秒)。
+const DefaultAgentSessionTimeout = 1800
+
+// MaxAgentSessionTimeout 是智能体会话总超时时间的上限(秒)。
+const MaxAgentSessionTimeout = 3600
+
type Agent struct {
ModelID string `json:"modelId"`
SessionTimeout int `json:"sessionTimeout"`
@@ -182,7 +188,7 @@ func defaultRerank() *Rerank {
func defaultAgent() *Agent {
return &Agent{
- SessionTimeout: 600,
+ SessionTimeout: DefaultAgentSessionTimeout,
StreamIdleTimeout: 120,
ConfirmTimeout: DefaultAgentConfirmTimeout,
MaxRetries: 3,
@@ -538,8 +544,8 @@ func (ai *AI) Normalize() {
}
if ai.Agent.SessionTimeout < 0 {
ai.Agent.SessionTimeout = 0
- } else if ai.Agent.SessionTimeout > 3600 {
- ai.Agent.SessionTimeout = 3600
+ } else if ai.Agent.SessionTimeout > MaxAgentSessionTimeout {
+ ai.Agent.SessionTimeout = MaxAgentSessionTimeout
}
if ai.Agent.StreamIdleTimeout < 1 {
ai.Agent.StreamIdleTimeout = 120
diff --git a/kernel/conf/ai_timeout_test.go b/kernel/conf/ai_timeout_test.go
index aec8842cc8..e43afc16f9 100644
--- a/kernel/conf/ai_timeout_test.go
+++ b/kernel/conf/ai_timeout_test.go
@@ -10,6 +10,21 @@ package conf
import "testing"
+func TestAgentTimeoutDefaults(t *testing.T) {
+ if DefaultAgentSessionTimeout != 1800 {
+ t.Fatalf("default session timeout changed: %d", DefaultAgentSessionTimeout)
+ }
+ if MaxAgentSessionTimeout != 3600 {
+ t.Fatalf("max session timeout changed: %d", MaxAgentSessionTimeout)
+ }
+ if DefaultAgentConfirmTimeout != 600 {
+ t.Fatalf("default confirmation timeout changed: %d", DefaultAgentConfirmTimeout)
+ }
+ if DefaultAgentSessionTimeout > MaxAgentSessionTimeout {
+ t.Fatalf("default session timeout %d exceeds the max %d", DefaultAgentSessionTimeout, MaxAgentSessionTimeout)
+ }
+}
+
func TestAINormalizeAgentTimeoutsAndRetries(t *testing.T) {
tests := []struct {
name string
@@ -18,9 +33,9 @@ func TestAINormalizeAgentTimeoutsAndRetries(t *testing.T) {
wantStreamIdle int
wantMaxRetries int
}{
- {name: "defaults", agent: nil, wantSession: 600, wantStreamIdle: 120, wantMaxRetries: 3},
+ {name: "defaults", agent: nil, wantSession: DefaultAgentSessionTimeout, wantStreamIdle: 120, wantMaxRetries: 3},
{name: "zero means unlimited session and no retries", agent: &Agent{}, wantSession: 0, wantStreamIdle: 120, wantMaxRetries: 0},
- {name: "clamps upper bounds", agent: &Agent{SessionTimeout: 7200, StreamIdleTimeout: 900, MaxRetries: 20}, wantSession: 3600, wantStreamIdle: 600, wantMaxRetries: 10},
+ {name: "clamps upper bounds", agent: &Agent{SessionTimeout: 7200, StreamIdleTimeout: 900, MaxRetries: 20}, wantSession: MaxAgentSessionTimeout, wantStreamIdle: 600, wantMaxRetries: 10},
{name: "normalizes negative values", agent: &Agent{SessionTimeout: -1, StreamIdleTimeout: -1, MaxRetries: -1}, wantSession: 0, wantStreamIdle: 120, wantMaxRetries: 0},
}