fix: 延迟创建 session,只在用户真正对话时才创建
This commit is contained in:
@@ -12,6 +12,8 @@ import (
|
|||||||
|
|
||||||
var lastContext string
|
var lastContext string
|
||||||
|
|
||||||
|
var ErrNeedNewSession = fmt.Errorf("需要创建新会话")
|
||||||
|
|
||||||
func GetContextPrompt(userInput string) string {
|
func GetContextPrompt(userInput string) string {
|
||||||
db := GetDB()
|
db := GetDB()
|
||||||
if db == nil {
|
if db == nil {
|
||||||
@@ -231,15 +233,8 @@ func SaveChat(userInput, aiReply string, useSessionSummary bool) (int, error) {
|
|||||||
// 获取或创建 Session
|
// 获取或创建 Session
|
||||||
session := currentSession
|
session := currentSession
|
||||||
if session == nil {
|
if session == nil {
|
||||||
var err error
|
// 如果没有 session,返回错误让用户创建
|
||||||
session, err = db.GetLatestSession()
|
return 0, ErrNeedNewSession
|
||||||
if err != nil || session == nil {
|
|
||||||
session, err = createSession()
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("创建会话失败: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
currentSession = session
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 保存原始 session summary(用于恢复)
|
// 保存原始 session summary(用于恢复)
|
||||||
|
|||||||
@@ -279,6 +279,11 @@ func runWithStreaming(agentLoop *agent.AgentLoop, input, sessionKey string, temp
|
|||||||
memoryCfg = internal.GetProjectConfig().Memory
|
memoryCfg = internal.GetProjectConfig().Memory
|
||||||
if memoryCfg.Enabled {
|
if memoryCfg.Enabled {
|
||||||
chatCount, saveErr = memory.SaveChat(originalInput, resp, !memory.ShouldSkipSummaryUpdate(originalInput))
|
chatCount, saveErr = memory.SaveChat(originalInput, resp, !memory.ShouldSkipSummaryUpdate(originalInput))
|
||||||
|
// 如果需要新 session,提示用户
|
||||||
|
if saveErr == memory.ErrNeedNewSession {
|
||||||
|
fmt.Println("请使用 /new 创建新会话后再对话")
|
||||||
|
saveErr = nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
elapsed := time.Since(startTime)
|
elapsed := time.Since(startTime)
|
||||||
|
|||||||
Reference in New Issue
Block a user