动态加载工具配置

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-06-17 13:13:06 +08:00
co-authored by Claude
parent 249227ef0a
commit 4e4c6b32fc
13 changed files with 704 additions and 188 deletions
+26 -3
View File
@@ -10,6 +10,7 @@ import (
"strings"
"time"
searchagent "aichat/agents/search"
"aichat/contextwindow"
"aichat/conversation"
"aichat/llm"
@@ -61,17 +62,39 @@ func (s *Server) switchOpenAIHandler(c *gin.Context) {
})
}
func (s *Server) searchState() (*searchagent.State, bool) {
if s == nil || s.toolManager == nil {
return nil, false
}
raw, ok := s.toolManager.RawState(searchagent.ToolName)
if !ok {
return nil, false
}
state, ok := raw.(*searchagent.State)
return state, ok && state != nil
}
func (s *Server) listSearchHandler(c *gin.Context) {
c.JSON(http.StatusOK, s.searchState.ListProfiles())
state, ok := s.searchState()
if !ok {
c.JSON(http.StatusOK, searchagent.ListResponse{})
return
}
c.JSON(http.StatusOK, state.ListProfiles())
}
func (s *Server) switchSearchHandler(c *gin.Context) {
state, ok := s.searchState()
if !ok {
c.JSON(http.StatusBadRequest, gin.H{"error": "搜索工具未加载"})
return
}
var req activeProfileRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "请求格式错误: " + err.Error()})
return
}
profile, err := s.searchState.SwitchActive(req.Name)
profile, err := state.SwitchActive(req.Name)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
@@ -219,7 +242,7 @@ func (s *Server) chatHandler(c *gin.Context) {
contextMessages = chatWindow.ChatMessages
// 用 Function Calling 工具循环替代旧的路由+隐藏上下文机制
messages, err := toolrouter.RunAgentToolLoop(ctx, s.toolRouterState, profile, contextMessages, s.searchState, s.sqlState, emit)
messages, err := toolrouter.RunAgentToolLoop(ctx, s.toolRouterState, profile, contextMessages, s.toolManager, emit)
if err != nil {
fmt.Fprintln(os.Stderr, "Agent 工具循环失败:", err)
messages, err = message.BuildArkMessages(contextMessages)