fix: 延迟创建 session,只在用户真正对话时才创建

This commit is contained in:
2026-04-27 08:00:20 +08:00
parent 3b8ffad2d5
commit b95518e787
2 changed files with 9 additions and 9 deletions

View File

@@ -12,6 +12,8 @@ import (
var lastContext string
var ErrNeedNewSession = fmt.Errorf("需要创建新会话")
func GetContextPrompt(userInput string) string {
db := GetDB()
if db == nil {
@@ -231,15 +233,8 @@ func SaveChat(userInput, aiReply string, useSessionSummary bool) (int, error) {
// 获取或创建 Session
session := currentSession
if session == nil {
var err error
session, err = db.GetLatestSession()
if err != nil || session == nil {
session, err = createSession()
if err != nil {
return 0, fmt.Errorf("创建会话失败: %v", err)
}
}
currentSession = session
// 如果没有 session返回错误让用户创建
return 0, ErrNeedNewSession
}
// 保存原始 session summary用于恢复

View File

@@ -279,6 +279,11 @@ func runWithStreaming(agentLoop *agent.AgentLoop, input, sessionKey string, temp
memoryCfg = internal.GetProjectConfig().Memory
if memoryCfg.Enabled {
chatCount, saveErr = memory.SaveChat(originalInput, resp, !memory.ShouldSkipSummaryUpdate(originalInput))
// 如果需要新 session提示用户
if saveErr == memory.ErrNeedNewSession {
fmt.Println("请使用 /new 创建新会话后再对话")
saveErr = nil
}
}
elapsed := time.Since(startTime)