feat(llm): 大模型双协议(OpenAI+Anthropic)接入 + SSH凭证修复 + 容量预测AI解读

后端双协议: 新增 anthropic.go(Complete/Stream/工具转换) + client.go 工厂按 Protocol 路由; llm.Client 加 Stream; llm_chat 三方法收敛复用 llm.Client; resolveProtocol 按 provider 路由; Agent CLI 注入协议。编码套餐(GLM-5.2/Kimi/MiMo/Anthropic)走 Anthropic /v1/messages。

SSH凭证修复: applyCredential 正确解析凭证 JSON 的 private_key(旧实现把整个 JSON 当私钥 → ssh.ParsePrivateKey 失败 → ssh-key 登录不通); AssignCredential/批量导入/单台 CRUD 三处统一; 单台补齐 credential_id 字段(原被忽略)。

容量预测: 新增 AI 深度解读(buildCapacityInsightPrompt + PredictiveCapacityTab 按钮, 复用 aiApi.chat)。

其他: DeepSeek 模型名更新为 deepseek-v4-flash/pro; 智能助手去掉重复模型配置按钮; LLMConfig 精简生成参数(Claude Code 式)。

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
LoganandClaude committed 2026-06-28 22:37:46 +08:00
1 parent c0ee8e4ddd
commit 0b2e43ea9e
16 files changed
+689 -296

No files matched your search

+1
View File
@@ -166,6 +166,7 @@ func (ac *agentConn) startTask(sshCfg remoteexec.SSHConfig, llmCfg model.LLMConf
// 构建引擎 // 构建引擎
engine, err := agentcli.NewEngine(agentcli.EngineConfig{ engine, err := agentcli.NewEngine(agentcli.EngineConfig{
LLM: llm.Config{ LLM: llm.Config{
Protocol: resolveProtocol(llmCfg.Provider),
Endpoint: resolveEndpoint(llmCfg.Provider, llmCfg.Endpoint), Endpoint: resolveEndpoint(llmCfg.Provider, llmCfg.Endpoint),
APIKey: llmCfg.APIKey, Model: llmCfg.Model, APIKey: llmCfg.APIKey, Model: llmCfg.Model,
Temperature: llmCfg.Temperature, MaxTokens: llmCfg.MaxTokens, Temperature: llmCfg.Temperature, MaxTokens: llmCfg.MaxTokens,
+29 -9
View File
@@ -1,6 +1,7 @@
package controller package controller
import ( import (
"encoding/json"
"net/http" "net/http"
"strconv" "strconv"
@@ -43,15 +44,7 @@ func (h *CredentialHandler) AssignCredential(c *gin.Context) {
return return
} }
if cred.SecretType == "ssh" { applyCredential(cred, plain, &server)
server.SSHKey = plain
server.AuthType = "key"
server.Password = ""
} else {
server.Password = plain
server.AuthType = "password"
server.SSHKey = ""
}
if err := h.db.Save(&server).Error; err != nil { if err := h.db.Save(&server).Error; err != nil {
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "分配失败: " + err.Error()}) c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "分配失败: " + err.Error()})
@@ -60,3 +53,30 @@ func (h *CredentialHandler) AssignCredential(c *gin.Context) {
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "凭据已关联"}) c.JSON(http.StatusOK, model.Response{Code: 200, Message: "凭据已关联"})
} }
// applyCredential 将解密后的明文 plain 填充到 server 认证字段(统一入口,避免 plain JSON 误用) // Logan.Li
// ssh 凭证 plain 是 JSON {"private_key":...,"ssh_user":...,"ssh_port":...},须解析出 private_key(旧实现直接把
// 整个 JSON 当私钥,导致 ssh.ParsePrivateKey 失败 → ssh-key 登录不通);其余类型 plain 即密码。
// 供 AssignCredential / 单台服务器 CRUD / 批量导入 三处复用。
func applyCredential(cred model.Credential, plain string, server *model.Server) {
if cred.SecretType == "ssh" {
privateKey := plain
var sshc model.SSHSecretContent
if json.Unmarshal([]byte(plain), &sshc) == nil && sshc.PrivateKey != "" {
privateKey = sshc.PrivateKey
if sshc.SSHUser != "" {
server.Username = sshc.SSHUser
}
if sshc.SSHPort > 0 {
server.Port = sshc.SSHPort
}
}
server.SSHKey = privateKey
server.AuthType = "key"
server.Password = ""
} else {
server.Password = plain
server.AuthType = "password"
server.SSHKey = ""
}
}
+19 -17
View File
@@ -122,23 +122,6 @@ func (h *LLMHandler) SaveLLMConfig(c *gin.Context) {
}}) }})
} }
// OpenAI 兼容请求/响应结构(仅取必要字段) // Logan.Li
type openaiChatRequest struct {
Model string `json:"model"`
Messages []model.ChatMessage `json:"messages"`
Temperature float64 `json:"temperature"`
MaxTokens int `json:"max_tokens,omitempty"`
}
type openaiChoice struct {
Message model.ChatMessage `json:"message"`
}
type openaiChatResponse struct {
Choices []openaiChoice `json:"choices"`
Usage struct {
TotalTokens int `json:"total_tokens"`
} `json:"usage"`
}
// resolveEndpoint 根据提供商补全默认 endpoint // Logan.Li // resolveEndpoint 根据提供商补全默认 endpoint // Logan.Li
func resolveEndpoint(provider, endpoint string) string { func resolveEndpoint(provider, endpoint string) string {
if endpoint != "" { if endpoint != "" {
@@ -147,10 +130,20 @@ func resolveEndpoint(provider, endpoint string) string {
switch provider { switch provider {
case "deepseek": case "deepseek":
return "https://api.deepseek.com/v1" return "https://api.deepseek.com/v1"
case "glm": // 智谱 GLM 通用 OpenAI 兼容端点(编码套餐改用 https://open.bigmodel.cn/api/coding/paas/v4) // Logan.Li
return "https://open.bigmodel.cn/api/paas/v4"
case "qwen": case "qwen":
return "https://dashscope.aliyuncs.com/compatible-mode/v1" return "https://dashscope.aliyuncs.com/compatible-mode/v1"
case "moonshot": case "moonshot":
return "https://api.moonshot.cn/v1" return "https://api.moonshot.cn/v1"
case "kimi": // Kimi 编码套餐 OpenAI 兼容端点(注意:该端点有客户端白名单,通用调用可能被拒) // Logan.Li
return "https://api.kimi.com/coding/v1"
case "mimo": // 小米 MiMo OpenAI 兼容端点 // Logan.Li
return "https://api.mimo.mi.com/v1"
case "anthropic": // Anthropic 官方 Messages 协议(anthropic.go 客户端拼 /v1/messages) // Logan.Li
return "https://api.anthropic.com"
case "glm-coding": // 智谱 GLM 编码套餐 Anthropic 协议端点(模型 GLM-5.2) // Logan.Li
return "https://open.bigmodel.cn/api/anthropic"
case "ollama": case "ollama":
return "http://localhost:11434/v1" return "http://localhost:11434/v1"
default: // openai / custom default: // openai / custom
@@ -158,6 +151,15 @@ func resolveEndpoint(provider, endpoint string) string {
} }
} }
// resolveProtocol 根据提供商推断接入协议(决定走 OpenAI 还是 Anthropic 客户端) // Logan.Li
// 编码套餐(智谱 GLM 编码 / Anthropic 官方)走 Anthropic Messages 协议;其余走 OpenAI 兼容协议。
func resolveProtocol(provider string) string {
if provider == "anthropic" || provider == "glm-coding" {
return "anthropic"
}
return "openai"
}
// defaultSystemPrompt AIOps 运维助手默认系统提示词 // Logan.Li // defaultSystemPrompt AIOps 运维助手默认系统提示词 // Logan.Li
const defaultSystemPrompt = `你是 AIOps 智能运维助手,专注于企业级运维场景。你可以: const defaultSystemPrompt = `你是 AIOps 智能运维助手,专注于企业级运维场景。你可以:
1. 分析服务器、容器、K8s 集群的性能瓶颈与异常 1. 分析服务器、容器、K8s 集群的性能瓶颈与异常
+52 -194
View File
@@ -1,22 +1,41 @@
package controller package controller
import ( import (
"bufio"
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"net/http" "net/http"
"strings" "strings"
"time" "time"
"ezops/middleware" "ezops/middleware"
"ezops/model" "ezops/model"
"ezops/pkg/agentcli/llm"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// Chat AI 对话代理(OpenAI 兼容接口) // Logan.Li // buildLLMClient 从数据库配置构造 LLM 客户端(按 provider 协议自动路由 OpenAI/Anthropic) // Logan.Li
func (h *LLMHandler) buildLLMClient(cfg model.LLMConfig) llm.Client {
return llm.NewClient(llm.Config{
Protocol: resolveProtocol(cfg.Provider),
Endpoint: resolveEndpoint(cfg.Provider, cfg.Endpoint),
APIKey: cfg.APIKey,
Model: cfg.Model,
Temperature: cfg.Temperature,
MaxTokens: cfg.MaxTokens,
})
}
// toLLMMessages model.ChatMessage[] → llm.Message[](智能助手纯对话,仅填 Role/Content) // Logan.Li
func toLLMMessages(msgs []model.ChatMessage) []llm.Message {
out := make([]llm.Message, len(msgs))
for i, m := range msgs {
out[i] = llm.Message{Role: m.Role, Content: m.Content}
}
return out
}
// Chat AI 对话代理(统一走 llm.Client,按配置协议自动适配 OpenAI/Anthropic) // Logan.Li
// POST /api/v1/aiops/chat // POST /api/v1/aiops/chat
func (h *LLMHandler) Chat(c *gin.Context) { func (h *LLMHandler) Chat(c *gin.Context) {
var cfg model.LLMConfig var cfg model.LLMConfig
@@ -38,60 +57,22 @@ func (h *LLMHandler) Chat(c *gin.Context) {
} }
messages := append([]model.ChatMessage{{Role: "system", Content: systemPrompt}}, req.Messages...) messages := append([]model.ChatMessage{{Role: "system", Content: systemPrompt}}, req.Messages...)
// 构造 OpenAI 兼容请求 resp, err := h.buildLLMClient(cfg).Complete(c.Request.Context(), &llm.CompletionRequest{
payload := openaiChatRequest{ Messages: toLLMMessages(messages),
Model: cfg.Model, })
Messages: messages,
Temperature: cfg.Temperature,
MaxTokens: cfg.MaxTokens,
}
body, _ := json.Marshal(payload)
endpoint := resolveEndpoint(cfg.Provider, cfg.Endpoint)
url := endpoint + "/chat/completions"
httpReq, err := http.NewRequestWithContext(c.Request.Context(), http.MethodPost, url, bytes.NewReader(body))
if err != nil { if err != nil {
c.JSON(http.StatusOK, model.Response{Code: 500, Message: "构造请求失败: " + err.Error()}) c.JSON(http.StatusOK, model.Response{Code: 502, Message: "LLM 调用失败: " + err.Error()})
return return
} }
httpReq.Header.Set("Content-Type", "application/json") replyContent := resp.Message.Content
httpReq.Header.Set("Authorization", "Bearer "+cfg.APIKey)
resp, err := h.client.Do(httpReq)
if err != nil {
c.JSON(http.StatusOK, model.Response{Code: 502, Message: "LLM 服务连接失败,请检查网络或 Endpoint 配置: " + err.Error()})
return
}
defer resp.Body.Close()
raw, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
// 提取上游错误信息
msg := fmt.Sprintf("LLM 服务返回错误 (HTTP %d)", resp.StatusCode)
if upErr := parseOpenAIError(raw); upErr != "" {
msg += ": " + upErr
}
c.JSON(http.StatusOK, model.Response{Code: resp.StatusCode, Message: msg})
return
}
var chatResp openaiChatResponse
if err := json.Unmarshal(raw, &chatResp); err != nil || len(chatResp.Choices) == 0 {
c.JSON(http.StatusOK, model.Response{Code: 500, Message: "解析 LLM 响应失败"})
return
}
replyContent := chatResp.Choices[0].Message.Content
// 持久化对话历史(按用户隔离,成对写入 user + assistant) // Logan.Li // 持久化对话历史(按用户隔离,成对写入 user + assistant) // Logan.Li
// 仅在成功时入库;错误气泡为瞬时 UI 不持久化 // 仅在成功时入库;错误气泡为瞬时 UI 不持久化
uid := middleware.GetUserID(c) uid := middleware.GetUserID(c)
if uid != 0 && len(req.Messages) > 0 { if uid != 0 && len(req.Messages) > 0 {
// 取最后一条用户消息(前端已附加当前提问)
lastUser := req.Messages[len(req.Messages)-1] lastUser := req.Messages[len(req.Messages)-1]
now := time.Now() now := time.Now()
// session_id:前端传入或后端生成(兼容旧版客户端) // Logan.Li sid := req.SessionID // 前端传入或后端生成(兼容旧版客户端)
sid := req.SessionID
if sid == "" { if sid == "" {
sid = fmt.Sprintf("sess-%d", now.UnixMilli()) sid = fmt.Sprintf("sess-%d", now.UnixMilli())
} }
@@ -104,15 +85,15 @@ func (h *LLMHandler) Chat(c *gin.Context) {
c.JSON(http.StatusOK, model.Response{Code: 200, Data: model.ChatResponse{ c.JSON(http.StatusOK, model.Response{Code: 200, Data: model.ChatResponse{
Content: replyContent, Content: replyContent,
Model: cfg.Model, Model: cfg.Model,
TokensUsed: chatResp.Usage.TotalTokens, TokensUsed: resp.Usage.TotalTokens,
}}) }})
} }
// ChatStream AI 流式对话代理(SSE) // Logan.Li // ChatStream AI 流式对话代理(SSE) // Logan.Li
// POST /api/v1/aiops/chat/stream // POST /api/v1/aiops/chat/stream
// 客户端用 EventSource/fetch-stream 消费,每条 SSE data 为 {"content": "..."} // 客户端用 fetch-stream 消费,每条 SSE data 为 {"content": "..."};出错发 event: error;完成发 event: done。
// 出错时发送 event: error,data 为 {"message": "..."} // 上游协议差异(OpenAI delta / Anthropic content_block_delta)由 llm.Client.Stream 屏蔽,
// 完成后服务端持久化 user + assistant 两条历史,event: done 结束 // controller 只负责 SSE 转发 + 客户端断开检测 + 历史持久化。
func (h *LLMHandler) ChatStream(c *gin.Context) { func (h *LLMHandler) ChatStream(c *gin.Context) {
var cfg model.LLMConfig var cfg model.LLMConfig
if err := h.db.First(&cfg).Error; err != nil || !cfg.Enabled { if err := h.db.First(&cfg).Error; err != nil || !cfg.Enabled {
@@ -136,127 +117,37 @@ func (h *LLMHandler) ChatStream(c *gin.Context) {
c.Writer.Header().Set("Connection", "keep-alive") c.Writer.Header().Set("Connection", "keep-alive")
c.Writer.Header().Set("X-Accel-Buffering", "no") // Nginx 关闭缓冲 c.Writer.Header().Set("X-Accel-Buffering", "no") // Nginx 关闭缓冲
c.Writer.WriteHeader(http.StatusOK) c.Writer.WriteHeader(http.StatusOK)
// 注册写入心跳(立即 flush 一个注释行让客户端确认连接建立) c.Writer.Flush() // 立即 flush 让客户端确认连接建立
c.Writer.Flush()
// 组装系统提示词 + 上下文
systemPrompt := cfg.SystemPrompt systemPrompt := cfg.SystemPrompt
if strings.TrimSpace(systemPrompt) == "" { if strings.TrimSpace(systemPrompt) == "" {
systemPrompt = defaultSystemPrompt systemPrompt = defaultSystemPrompt
} }
messages := append([]model.ChatMessage{{Role: "system", Content: systemPrompt}}, req.Messages...) messages := append([]model.ChatMessage{{Role: "system", Content: systemPrompt}}, req.Messages...)
// 构造 OpenAI 兼容流式请求
payload := struct {
Model string `json:"model"`
Messages []model.ChatMessage `json:"messages"`
Temperature float64 `json:"temperature"`
MaxTokens int `json:"max_tokens,omitempty"`
Stream bool `json:"stream"`
}{
Model: cfg.Model,
Messages: messages,
Temperature: cfg.Temperature,
MaxTokens: cfg.MaxTokens,
Stream: true,
}
body, _ := json.Marshal(payload)
endpoint := resolveEndpoint(cfg.Provider, cfg.Endpoint) // 流式补全:逐 token 回调转发 SSE;ctx 取消(客户端断开)由 llm.Client 内部终止上游读取
url := endpoint + "/chat/completions"
httpReq, err := http.NewRequestWithContext(c.Request.Context(), http.MethodPost, url, bytes.NewReader(body))
if err != nil {
writeSSEError(c, 500, "构造请求失败: "+err.Error())
return
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+cfg.APIKey)
httpReq.Header.Set("Accept", "text/event-stream")
// 流式请求不能用全局 120s 超时客户端,构造无超时 client 让 ctx 控制 // Logan.Li
streamClient := &http.Client{Timeout: 0}
resp, err := streamClient.Do(httpReq)
if err != nil {
writeSSEError(c, 502, "LLM 服务连接失败,请检查网络或 Endpoint 配置: "+err.Error())
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
raw, _ := io.ReadAll(resp.Body)
msg := fmt.Sprintf("LLM 服务返回错误 (HTTP %d)", resp.StatusCode)
if e := parseOpenAIError(raw); e != "" {
msg += ": " + e
}
writeSSEError(c, resp.StatusCode, msg)
return
}
// 逐行解析上游 SSE,转发增量内容给前端 // Logan.Li
var contentBuilder strings.Builder var contentBuilder strings.Builder
reader := bufio.NewReader(resp.Body) _, streamErr := h.buildLLMClient(cfg).Stream(c.Request.Context(), &llm.CompletionRequest{
clientGone := false Messages: toLLMMessages(messages),
for { }, func(delta string) {
// 客户端断开时停止读取(c.Request.Context().Done() 在客户端断开时触发) // Logan.Li contentBuilder.WriteString(delta)
select { writeSSEChunk(c, delta)
case <-c.Request.Context().Done(): })
clientGone = true
default:
}
if clientGone {
break
}
line, readErr := reader.ReadString('\n')
line = strings.TrimSpace(line)
if line != "" {
// 仅处理 data: 开头的行
if strings.HasPrefix(line, "data:") {
data := strings.TrimSpace(strings.TrimPrefix(line, "data:"))
if data == "[DONE]" {
// 上游结束,跳出循环准备持久化 + 发送 [DONE]
break
}
// 解析 {"choices":[{"delta":{"content":"..."}}]}
var chunk struct {
Choices []struct {
Delta struct {
Content string `json:"content"`
} `json:"delta"`
} `json:"choices"`
}
if json.Unmarshal([]byte(data), &chunk) == nil && len(chunk.Choices) > 0 {
delta := chunk.Choices[0].Delta.Content
if delta != "" {
contentBuilder.WriteString(delta)
// 转发给前端:data: {"content":"..."}
writeSSEChunk(c, delta)
}
}
}
}
if readErr != nil {
if readErr != io.EOF {
// 读取出错(非正常结束)
if contentBuilder.Len() == 0 {
writeSSEError(c, 502, "LLM 流式响应读取失败: "+readErr.Error())
return
}
// 已有部分内容则正常结束
}
break
}
}
fullContent := contentBuilder.String() fullContent := contentBuilder.String()
// 持久化对话历史(仅在成功且非空时入库;错误/中断不入库避免污染历史) // Logan.Li // 出错且无任何内容 → 发 error 事件
if streamErr != nil && fullContent == "" {
writeSSEError(c, 502, "LLM 流式响应失败: "+streamErr.Error())
return
}
// 持久化 + 结束事件(仅在非空且客户端未断开时入库;错误/中断不入库避免污染历史) // Logan.Li
clientGone := c.Request.Context().Err() != nil
if fullContent != "" && !clientGone { if fullContent != "" && !clientGone {
uid := middleware.GetUserID(c) uid := middleware.GetUserID(c)
if uid != 0 { if uid != 0 {
lastUser := req.Messages[len(req.Messages)-1] lastUser := req.Messages[len(req.Messages)-1]
now := time.Now() now := time.Now()
// session_id:前端传入或后端生成(兼容旧版客户端) // Logan.Li
sid := req.SessionID sid := req.SessionID
if sid == "" { if sid == "" {
sid = fmt.Sprintf("sess-%d", now.UnixMilli()) sid = fmt.Sprintf("sess-%d", now.UnixMilli())
@@ -266,7 +157,6 @@ func (h *LLMHandler) ChatStream(c *gin.Context) {
{UserID: uid, SessionID: sid, Role: "assistant", Content: fullContent, Model: cfg.Model, CreatedAt: now.Add(time.Millisecond)}, {UserID: uid, SessionID: sid, Role: "assistant", Content: fullContent, Model: cfg.Model, CreatedAt: now.Add(time.Millisecond)},
}) })
} }
// 发送结束标志
c.Writer.WriteString("event: done\ndata: {\"model\":\"" + cfg.Model + "\"}\n\n") c.Writer.WriteString("event: done\ndata: {\"model\":\"" + cfg.Model + "\"}\n\n")
c.Writer.Flush() c.Writer.Flush()
} else if !clientGone { } else if !clientGone {
@@ -298,19 +188,6 @@ func writeSSEError(c *gin.Context, code int, message string) {
c.Writer.Flush() c.Writer.Flush()
} }
// parseOpenAIError 从上游错误响应中提取 message 字段 // Logan.Li
func parseOpenAIError(raw []byte) string {
var m struct {
Error struct {
Message string `json:"message"`
} `json:"error"`
}
if json.Unmarshal(raw, &m) == nil && m.Error.Message != "" {
return m.Error.Message
}
return ""
}
// TestLLMConnection 测试 LLM 连接(发送一条 ping 消息) // Logan.Li // TestLLMConnection 测试 LLM 连接(发送一条 ping 消息) // Logan.Li
// POST /api/v1/aiops/llm-config/test // POST /api/v1/aiops/llm-config/test
func (h *LLMHandler) TestLLMConnection(c *gin.Context) { func (h *LLMHandler) TestLLMConnection(c *gin.Context) {
@@ -319,31 +196,12 @@ func (h *LLMHandler) TestLLMConnection(c *gin.Context) {
c.JSON(http.StatusOK, model.Response{Code: 400, Message: "请先保存配置"}) c.JSON(http.StatusOK, model.Response{Code: 400, Message: "请先保存配置"})
return return
} }
payload := openaiChatRequest{ _, err := h.buildLLMClient(cfg).Complete(c.Request.Context(), &llm.CompletionRequest{
Model: cfg.Model, Messages: []llm.Message{{Role: "user", Content: "ping"}},
Messages: []model.ChatMessage{{Role: "user", Content: "ping"}}, })
Temperature: 0,
MaxTokens: 16,
}
body, _ := json.Marshal(payload)
endpoint := resolveEndpoint(cfg.Provider, cfg.Endpoint)
httpReq, _ := http.NewRequestWithContext(c.Request.Context(), http.MethodPost, endpoint+"/chat/completions", bytes.NewReader(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+cfg.APIKey)
resp, err := h.client.Do(httpReq)
if err != nil { if err != nil {
c.JSON(http.StatusOK, model.Response{Code: 502, Message: "连接失败: " + err.Error()}) c.JSON(http.StatusOK, model.Response{Code: 502, Message: "连接失败: " + err.Error()})
return return
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
raw, _ := io.ReadAll(resp.Body)
msg := fmt.Sprintf("HTTP %d", resp.StatusCode)
if e := parseOpenAIError(raw); e != "" {
msg += ": " + e
}
c.JSON(http.StatusOK, model.Response{Code: resp.StatusCode, Message: msg})
return
}
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "连接成功"}) c.JSON(http.StatusOK, model.Response{Code: 200, Message: "连接成功"})
} }
+34
View File
@@ -223,6 +223,21 @@ func (h *ServerHandler) CreateServer(c *gin.Context) {
server.AuthType = "password" server.AuthType = "password"
} }
// 关联 SSH 凭证:解密后覆盖认证字段(同 AssignCredential,避免单台 CRUD 忽略 credential_id) // Logan.Li
if req.CredentialID != nil && *req.CredentialID > 0 {
var cred model.Credential
if err := h.db.First(&cred, *req.CredentialID).Error; err != nil {
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: "凭据不存在"})
return
}
plain, err := h.crypt.Decrypt(cred.Content)
if err != nil {
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "凭据解密失败"})
return
}
applyCredential(cred, plain, &server)
}
if err := h.db.Create(&server).Error; err != nil { if err := h.db.Create(&server).Error; err != nil {
c.JSON(http.StatusInternalServerError, model.Response{ c.JSON(http.StatusInternalServerError, model.Response{
Code: 500, Code: 500,
@@ -297,6 +312,25 @@ func (h *ServerHandler) UpdateServer(c *gin.Context) {
if req.AuthType != "" { if req.AuthType != "" {
updates["auth_type"] = req.AuthType updates["auth_type"] = req.AuthType
} }
// 关联 SSH 凭证:解密后覆盖认证字段(applyCredential 填充已 load 的 server,再同步到 updates) // Logan.Li
if req.CredentialID != nil && *req.CredentialID > 0 {
var cred model.Credential
if err := h.db.First(&cred, *req.CredentialID).Error; err != nil {
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: "凭据不存在"})
return
}
plain, err := h.crypt.Decrypt(cred.Content)
if err != nil {
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "凭据解密失败"})
return
}
applyCredential(cred, plain, &server)
updates["ssh_key"] = server.SSHKey
updates["password"] = server.Password
updates["auth_type"] = server.AuthType
updates["username"] = server.Username
updates["port"] = server.Port
}
if req.OS != "" { if req.OS != "" {
updates["os"] = req.OS updates["os"] = req.OS
} }
+13 -14
View File
@@ -30,26 +30,24 @@ func (h *ServerHandler) BatchCreateServers(c *gin.Context) {
authType = "password" authType = "password"
} }
// 如果指定了凭据,提前解密 // Logan.Li // 如果指定了凭据,提前解密(plain 在循环内由 applyCredential 解析填充) // Logan.Li
var decryptedPwd, decryptedKey string var (
hasCred bool
cred model.Credential
credPlain string
)
if req.CredentialID != nil && *req.CredentialID > 0 { if req.CredentialID != nil && *req.CredentialID > 0 {
var cred model.Credential
if err := h.db.First(&cred, *req.CredentialID).Error; err != nil { if err := h.db.First(&cred, *req.CredentialID).Error; err != nil {
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: "凭据不存在"}) c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: "凭据不存在"})
return return
} }
plain, err := h.crypt.Decrypt(cred.Content) p, err := h.crypt.Decrypt(cred.Content)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "凭据解密失败"}) c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "凭据解密失败"})
return return
} }
if cred.SecretType == "ssh" { credPlain = p
decryptedKey = plain hasCred = true
authType = "key"
} else {
decryptedPwd = plain
authType = "password"
}
} }
result := model.BatchCreateResult{Errors: []string{}} result := model.BatchCreateResult{Errors: []string{}}
@@ -84,9 +82,7 @@ func (h *ServerHandler) BatchCreateServers(c *gin.Context) {
IP: ip, IP: ip,
Port: port, Port: port,
Username: username, Username: username,
AuthType: authType, AuthType: authType, // 无凭证时用请求 authType(默认 password)
Password: decryptedPwd,
SSHKey: decryptedKey,
OS: item.OS, OS: item.OS,
CPU: item.CPU, CPU: item.CPU,
Memory: item.Memory * 1024, // GB → MB Memory: item.Memory * 1024, // GB → MB
@@ -95,6 +91,9 @@ func (h *ServerHandler) BatchCreateServers(c *gin.Context) {
RegionID: req.RegionID, RegionID: req.RegionID,
Status: "stopped", Status: "stopped",
} }
if hasCred {
applyCredential(cred, credPlain, &server) // 有凭证则覆盖认证字段(ssh→私钥/ssh_user/ssh_port) // Logan.Li
}
if err := h.db.Create(&server).Error; err != nil { if err := h.db.Create(&server).Error; err != nil {
result.Failed++ result.Failed++
result.Errors = append(result.Errors, fmt.Sprintf("%s(%s): %s", hostname, ip, err.Error())) result.Errors = append(result.Errors, fmt.Sprintf("%s(%s): %s", hostname, ip, err.Error()))
+3 -3
View File
@@ -3,11 +3,11 @@ package model
import "time" import "time"
// LLMConfig 大语言模型配置(单行配置,类似 LDAPConfig) // Logan.Li // LLMConfig 大语言模型配置(单行配置,类似 LDAPConfig) // Logan.Li
// 支持 OpenAI 兼容接口(OpenAI / DeepSeek / 通义千问 / Moonshot / Ollama / 自定义) // 支持 OpenAI 兼容接口(OpenAI / DeepSeek / 智谱GLM / 通义千问 / Moonshot / Kimi / 小米MiMo / Anthropic(需代理) / Ollama / 自定义)
type LLMConfig struct { type LLMConfig struct {
ID uint `gorm:"primarykey" json:"id"` ID uint `gorm:"primarykey" json:"id"`
Provider string `gorm:"size:50;not null;default:openai" json:"provider"` // openai/deepseek/qwen/moonshot/ollama/custom Provider string `gorm:"size:50;not null;default:openai" json:"provider"` // openai/deepseek/glm/qwen/moonshot/kimi/mimo/anthropic/ollama/custom
Model string `gorm:"size:100;not null;default:gpt-4o-mini" json:"model"` // 模型名,如 gpt-4o-mini / deepseek-chat Model string `gorm:"size:100;not null;default:gpt-4o-mini" json:"model"` // 模型名,如 gpt-4o-mini / deepseek-v4-flash
APIKey string `gorm:"size:500" json:"api_key"` // API Key(GET 时会被脱敏) APIKey string `gorm:"size:500" json:"api_key"` // API Key(GET 时会被脱敏)
Endpoint string `gorm:"size:500" json:"endpoint"` // 基础地址,如 https://api.openai.com/v1 Endpoint string `gorm:"size:500" json:"endpoint"` // 基础地址,如 https://api.openai.com/v1
Temperature float64 `gorm:"default:0.7" json:"temperature"` // 采样温度 0-2 Temperature float64 `gorm:"default:0.7" json:"temperature"` // 采样温度 0-2
+2
View File
@@ -137,6 +137,7 @@ type CreateServerRequest struct {
Password string `json:"password"` Password string `json:"password"`
SSHKey string `json:"ssh_key"` SSHKey string `json:"ssh_key"`
AuthType string `json:"auth_type" binding:"omitempty,oneof=password key"` AuthType string `json:"auth_type" binding:"omitempty,oneof=password key"`
CredentialID *uint `json:"credential_id,omitempty"` // 关联 SSH 凭证,非空则后端解密覆盖认证字段
OS string `json:"os"` OS string `json:"os"`
CPU int `json:"cpu"` CPU int `json:"cpu"`
Memory int64 `json:"memory"` Memory int64 `json:"memory"`
@@ -158,6 +159,7 @@ type UpdateServerRequest struct {
Password string `json:"password"` Password string `json:"password"`
SSHKey string `json:"ssh_key"` SSHKey string `json:"ssh_key"`
AuthType string `json:"auth_type" binding:"omitempty,oneof=password key"` AuthType string `json:"auth_type" binding:"omitempty,oneof=password key"`
CredentialID *uint `json:"credential_id,omitempty"` // 关联 SSH 凭证,非空则后端解密覆盖认证字段
OS string `json:"os"` OS string `json:"os"`
CPU int `json:"cpu"` CPU int `json:"cpu"`
Memory int64 `json:"memory"` Memory int64 `json:"memory"`
+337
View File
@@ -0,0 +1,337 @@
// anthropic.go - Anthropic Messages 协议 LLM 客户端实现 // Logan.Li
// 支持官方 Claude(api.anthropic.com)及智谱 GLM 编码套餐(open.bigmodel.cn/api/anthropic)等
// 所有 Anthropic /v1/messages 兼容端点。与 openai.go 实现同一 Client 接口,由 client.go 按 Protocol 路由。
// 鉴权统一用 Authorization: Bearer(智谱编码套餐实测通过;官方 Claude 较新版本亦支持)+ anthropic-version。
package llm
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
// AnthropicClient Anthropic Messages 协议客户端 // Logan.Li
type AnthropicClient struct {
cfg Config
client *http.Client
}
// NewAnthropicClient 创建 Anthropic 兼容客户端 // Logan.Li
func NewAnthropicClient(cfg Config) Client {
timeout := cfg.Timeout
if timeout <= 0 {
timeout = 180 * time.Second
}
return &AnthropicClient{cfg: cfg, client: &http.Client{Timeout: timeout}}
}
// Name 返回客户端名称 // Logan.Li
func (c *AnthropicClient) Name() string { return "anthropic(" + c.cfg.Model + ")" }
// ---- 请求体结构(Anthropic Messages 格式) // Logan.Li ----
// anthropicMessage Anthropic 消息:content 可为字符串或 block 数组,用 RawMessage 原样嵌入。
type anthropicMessage struct {
Role string `json:"role"`
Content json.RawMessage `json:"content"`
}
// anthropicRequest Anthropic /v1/messages 请求体 // Logan.Li
type anthropicRequest struct {
Model string `json:"model"`
MaxTokens int `json:"max_tokens"`
System string `json:"system,omitempty"` // system 抽到顶层(不在 messages 内)
Messages []anthropicMessage `json:"messages"`
Tools []anthropicTool `json:"tools,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
Stream bool `json:"stream,omitempty"`
}
// anthropicTool Anthropic 工具定义(input_schema,对应 OpenAI 的 parameters) // Logan.Li
type anthropicTool struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema map[string]interface{} `json:"input_schema"`
}
// 响应 content block:text 或 tool_use(互斥字段) // Logan.Li
type anthropicBlock struct {
Type string `json:"type"` // text | tool_use
Text string `json:"text,omitempty"`
ID string `json:"id,omitempty"` // tool_use
Name string `json:"name,omitempty"` // tool_use
Input json.RawMessage `json:"input,omitempty"` // tool_use
}
// 请求侧 content block(assistant 输出 tool_use / user 输出 tool_result) // Logan.Li
type textBlock struct {
Type string `json:"type"` // 固定 "text"
Text string `json:"text"`
}
type toolUseBlock struct {
Type string `json:"type"` // 固定 "tool_use"
ID string `json:"id"`
Name string `json:"name"`
Input json.RawMessage `json:"input"`
}
type toolResultBlock struct {
Type string `json:"type"` // 固定 "tool_result"
ToolUseID string `json:"tool_use_id"`
Content string `json:"content"`
}
// anthropicResponse Anthropic /v1/messages 响应体 // Logan.Li
type anthropicResponse struct {
ID string `json:"id"`
Model string `json:"model"`
Content []anthropicBlock `json:"content"`
Usage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
} `json:"usage"`
Error *struct {
Type string `json:"type"`
Message string `json:"message"`
} `json:"error,omitempty"`
}
// buildMessages 把 OpenAI 风格 Message[] 转成 Anthropic messages + 顶层 system // Logan.Li
// - system role 抽到顶层 system 字段
// - tool role(工具结果)→ user 消息的 tool_result 块
// - assistant 带 tool_calls → content 含 tool_use 块
func (c *AnthropicClient) buildMessages(req *CompletionRequest) (string, []anthropicMessage) {
var system string
var msgs []anthropicMessage
for _, m := range req.Messages {
switch m.Role {
case "system":
if system == "" {
system = m.Content
} else {
system += "\n" + m.Content
}
case "tool":
raw, _ := json.Marshal([]toolResultBlock{{
Type: "tool_result", ToolUseID: m.ToolCallID, Content: m.Content,
}})
msgs = append(msgs, anthropicMessage{Role: "user", Content: raw})
case "assistant":
// assistant 消息可能含文本 + tool_use 计划,组装成 content block 数组
blocks := make([]interface{}, 0, 1+len(m.ToolCalls))
if m.Content != "" {
blocks = append(blocks, textBlock{Type: "text", Text: m.Content})
}
for _, tc := range m.ToolCalls {
args := json.RawMessage("{}")
if strings.TrimSpace(tc.Function.Arguments) != "" {
args = json.RawMessage(tc.Function.Arguments)
}
blocks = append(blocks, toolUseBlock{
Type: "tool_use", ID: tc.ID, Name: tc.Function.Name, Input: args,
})
}
raw, _ := json.Marshal(blocks)
msgs = append(msgs, anthropicMessage{Role: "assistant", Content: raw})
default: // user
raw, _ := json.Marshal(m.Content)
msgs = append(msgs, anthropicMessage{Role: "user", Content: raw})
}
}
return system, msgs
}
// buildTools OpenAI Tool[](parameters) → Anthropic [](input_schema) // Logan.Li
func buildTools(tools []Tool) []anthropicTool {
out := make([]anthropicTool, 0, len(tools))
for _, t := range tools {
out = append(out, anthropicTool{
Name: t.Function.Name, Description: t.Function.Description, InputSchema: t.Function.Parameters,
})
}
return out
}
// buildRequest 构造 Anthropic 请求体(Complete 与 Stream 共用) // Logan.Li
func (c *AnthropicClient) buildRequest(req *CompletionRequest, stream bool) ([]byte, error) {
system, msgs := c.buildMessages(req)
payload := anthropicRequest{
Model: c.cfg.Model, MaxTokens: c.cfg.MaxTokens, System: system,
Messages: msgs, Tools: buildTools(req.Tools),
Temperature: c.cfg.Temperature, Stream: stream,
}
if payload.MaxTokens <= 0 {
payload.MaxTokens = 4096
}
body, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("序列化请求失败: %w", err)
}
return body, nil
}
// newHTTPRequest 构造 POST /v1/messages 请求(Bearer + anthropic-version) // Logan.Li
func (c *AnthropicClient) newHTTPRequest(ctx context.Context, body []byte) (*http.Request, error) {
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, c.cfg.Endpoint+"/v1/messages", bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("构造请求失败: %w", err)
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+c.cfg.APIKey)
httpReq.Header.Set("anthropic-version", "2023-06-01")
return httpReq, nil
}
// Complete 一次性补全(Anthropic /v1/messages) // Logan.Li
// 响应 content blocks:text 拼为 Content,tool_use 转为 ToolCalls;usage 映射。
func (c *AnthropicClient) Complete(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error) {
body, err := c.buildRequest(req, false)
if err != nil {
return nil, err
}
httpReq, err := c.newHTTPRequest(ctx, body)
if err != nil {
return nil, err
}
resp, err := c.client.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("LLM 连接失败: %w", err)
}
defer resp.Body.Close()
raw, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("LLM 返回错误 (HTTP %d): %s", resp.StatusCode, extractErrorMessage(raw))
}
var parsed anthropicResponse
if err := json.Unmarshal(raw, &parsed); err != nil {
return nil, fmt.Errorf("解析 LLM 响应失败: %w", err)
}
if parsed.Error != nil && parsed.Error.Message != "" {
return nil, fmt.Errorf("LLM 错误: %s", parsed.Error.Message)
}
var textBuf strings.Builder
var toolCalls []ToolCall
for _, b := range parsed.Content {
switch b.Type {
case "text":
textBuf.WriteString(b.Text)
case "tool_use":
args := string(b.Input)
if strings.TrimSpace(args) == "" {
args = "{}"
}
toolCalls = append(toolCalls, ToolCall{
ID: b.ID, Type: "function",
Function: FunctionCall{Name: b.Name, Arguments: args},
})
}
}
return &CompletionResponse{
ID: parsed.ID,
Model: parsed.Model,
Message: Message{
Role: "assistant",
Content: textBuf.String(),
ToolCalls: toolCalls,
},
Usage: Usage{
PromptTokens: parsed.Usage.InputTokens,
CompletionTokens: parsed.Usage.OutputTokens,
TotalTokens: parsed.Usage.InputTokens + parsed.Usage.OutputTokens,
},
}, nil
}
// Stream 流式补全(Anthropic SSE) // Logan.Li
// 逐 token 回调 text_delta;usage 从 message_start(input) + message_delta(output) 取。
// 注:流式主要用于纯对话(智能助手),tool_use 流式增量在此不组装(Agent CLI 走 Complete)。
func (c *AnthropicClient) Stream(ctx context.Context, req *CompletionRequest, onDelta func(string)) (*CompletionResponse, error) {
body, err := c.buildRequest(req, true)
if err != nil {
return nil, err
}
httpReq, err := c.newHTTPRequest(ctx, body)
if err != nil {
return nil, err
}
httpReq.Header.Set("Accept", "text/event-stream")
streamClient := &http.Client{Timeout: 0} // 流式由 ctx 控制超时
resp, err := streamClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("LLM 连接失败: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
raw, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("LLM 返回错误 (HTTP %d): %s", resp.StatusCode, extractErrorMessage(raw))
}
var contentBuilder strings.Builder
var usage Usage
var eventType string
reader := bufio.NewReader(resp.Body)
for {
line, readErr := reader.ReadString('\n')
trimmed := strings.TrimSpace(line)
switch {
case strings.HasPrefix(trimmed, "event:"):
eventType = strings.TrimSpace(strings.TrimPrefix(trimmed, "event:"))
case strings.HasPrefix(trimmed, "data:"):
data := strings.TrimSpace(strings.TrimPrefix(trimmed, "data:"))
if data != "" && data != "[DONE]" {
switch eventType {
case "content_block_delta":
var d struct {
Delta struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"delta"`
}
if json.Unmarshal([]byte(data), &d) == nil && d.Delta.Type == "text_delta" && d.Delta.Text != "" {
contentBuilder.WriteString(d.Delta.Text)
if onDelta != nil {
onDelta(d.Delta.Text)
}
}
case "message_start":
var d struct {
Message struct {
Usage struct {
InputTokens int `json:"input_tokens"`
} `json:"usage"`
} `json:"message"`
}
if json.Unmarshal([]byte(data), &d) == nil {
usage.PromptTokens = d.Message.Usage.InputTokens
}
case "message_delta":
var d struct {
Usage struct {
OutputTokens int `json:"output_tokens"`
} `json:"usage"`
}
if json.Unmarshal([]byte(data), &d) == nil {
usage.CompletionTokens = d.Usage.OutputTokens
usage.TotalTokens = usage.PromptTokens + usage.CompletionTokens
}
}
}
eventType = "" // 消费完 data 重置,防默认事件串用
}
if readErr != nil {
break
}
}
return &CompletionResponse{
Model: c.cfg.Model,
Message: Message{Role: "assistant", Content: contentBuilder.String()},
Usage: usage,
}, nil
}
+14
View File
@@ -0,0 +1,14 @@
// client.go - LLM 客户端工厂(按 Config.Protocol 路由到具体实现) // Logan.Li
// 上层(controller/agentcli)统一调 NewClient,无需感知协议差异。
// 新增协议只需在此分支 + 实现对应 Client。
package llm
// NewClient 按 Config.Protocol 选择 LLM 客户端实现 // Logan.Li
// "anthropic" → Anthropic Messages 协议(Claude 官方 / 智谱 GLM 编码套餐等);
// 其余(含空值)→ OpenAI 兼容协议。
func NewClient(cfg Config) Client {
if cfg.Protocol == "anthropic" {
return NewAnthropicClient(cfg)
}
return NewOpenAIClient(cfg)
}
+89 -2
View File
@@ -4,17 +4,20 @@
package llm package llm
import ( import (
"bufio"
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"strings"
"time" "time"
) )
// Config LLM 客户端配置(endpoint 已由上层解析为完整 base url) // Logan.Li // Config LLM 客户端配置(endpoint 已由上层解析为完整 base url) // Logan.Li
type Config struct { type Config struct {
Protocol string // "openai" | "anthropic",由 client.go 工厂决定具体实现
Endpoint string // 完整 base url,如 https://api.deepseek.com/v1 Endpoint string // 完整 base url,如 https://api.deepseek.com/v1
APIKey string // Bearer token APIKey string // Bearer token
Model string // 模型名,如 deepseek-chat / gpt-4o-mini Model string // 模型名,如 deepseek-chat / gpt-4o-mini
@@ -29,8 +32,9 @@ type OpenAIClient struct {
client *http.Client client *http.Client
} }
// NewClient 创建 OpenAI 兼容客户端 // Logan.Li // NewOpenAIClient 创建 OpenAI 兼容客户端 // Logan.Li
func NewClient(cfg Config) Client { // 顶层工厂在 client.go(按 Protocol 路由到 OpenAI / Anthropic 实现)。
func NewOpenAIClient(cfg Config) Client {
timeout := cfg.Timeout timeout := cfg.Timeout
if timeout <= 0 { if timeout <= 0 {
timeout = 180 * time.Second timeout = 180 * time.Second
@@ -52,6 +56,7 @@ type openaiRequest struct {
ToolChoice string `json:"tool_choice,omitempty"` // "auto" 让模型自主决定 ToolChoice string `json:"tool_choice,omitempty"` // "auto" 让模型自主决定
Temperature float64 `json:"temperature,omitempty"` Temperature float64 `json:"temperature,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"` MaxTokens int `json:"max_tokens,omitempty"`
Stream bool `json:"stream,omitempty"` // 流式补全(Stream 方法置 true)
} }
// openaiResponse OpenAI chat/completions 响应体 // Logan.Li // openaiResponse OpenAI chat/completions 响应体 // Logan.Li
@@ -131,6 +136,88 @@ func (c *OpenAIClient) Complete(ctx context.Context, req *CompletionRequest) (*C
}, nil }, nil
} }
// Stream 流式补全(OpenAI 兼容 SSE),逐 token 回调 onDelta,返回聚合响应 // Logan.Li
// 解析 data: {"choices":[{"delta":{"content":"..."}}]} 增量;[DONE] 结束;末块 usage 若有则取。
func (c *OpenAIClient) Stream(ctx context.Context, req *CompletionRequest, onDelta func(string)) (*CompletionResponse, error) {
payload := openaiRequest{
Model: c.cfg.Model,
Messages: req.Messages,
Tools: req.Tools,
ToolChoice: "auto",
Temperature: c.cfg.Temperature,
MaxTokens: c.cfg.MaxTokens,
Stream: true,
}
if payload.MaxTokens <= 0 {
payload.MaxTokens = 4096
}
body, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("序列化请求失败: %w", err)
}
url := c.cfg.Endpoint + "/chat/completions"
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("构造请求失败: %w", err)
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+c.cfg.APIKey)
httpReq.Header.Set("Accept", "text/event-stream")
streamClient := &http.Client{Timeout: 0} // 流式由 ctx 控制超时
resp, err := streamClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("LLM 连接失败: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
raw, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("LLM 返回错误 (HTTP %d): %s", resp.StatusCode, extractErrorMessage(raw))
}
var contentBuilder strings.Builder
var usage Usage
reader := bufio.NewReader(resp.Body)
for {
line, readErr := reader.ReadString('\n')
line = strings.TrimSpace(line)
if line != "" && strings.HasPrefix(line, "data:") {
data := strings.TrimSpace(strings.TrimPrefix(line, "data:"))
if data == "[DONE]" {
break
}
var chunk struct {
Choices []struct {
Delta struct {
Content string `json:"content"`
} `json:"delta"`
} `json:"choices"`
Usage *Usage `json:"usage"`
}
if json.Unmarshal([]byte(data), &chunk) == nil {
if len(chunk.Choices) > 0 && chunk.Choices[0].Delta.Content != "" {
delta := chunk.Choices[0].Delta.Content
contentBuilder.WriteString(delta)
if onDelta != nil {
onDelta(delta)
}
}
if chunk.Usage != nil {
usage = *chunk.Usage
}
}
}
if readErr != nil {
break
}
}
return &CompletionResponse{
Model: c.cfg.Model,
Message: Message{Role: "assistant", Content: contentBuilder.String()},
Usage: usage,
}, nil
}
// extractErrorMessage 从上游错误响应提取 message 字段 // Logan.Li // extractErrorMessage 从上游错误响应提取 message 字段 // Logan.Li
func extractErrorMessage(raw []byte) string { func extractErrorMessage(raw []byte) string {
var m struct { var m struct {
+4
View File
@@ -65,9 +65,13 @@ type Usage struct {
} }
// Client LLM 客户端接口 // Logan.Li // Client LLM 客户端接口 // Logan.Li
// 实现按协议分两种:OpenAI 兼容(openai.go)与 Anthropic 兼容(anthropic.go),
// 由 client.go 工厂按 Config.Protocol 路由。上层(controller/agentcli)只面向本接口。
type Client interface { type Client interface {
// Complete 一次性补全(返回思考文本 + 工具调用计划)。 // Complete 一次性补全(返回思考文本 + 工具调用计划)。
Complete(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error) Complete(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)
// Stream 流式补全,逐 token 回调 onDelta;返回最终聚合响应(含完整 content + usage)。
Stream(ctx context.Context, req *CompletionRequest, onDelta func(string)) (*CompletionResponse, error)
// Name 返回客户端名称(用于日志/审计)。 // Name 返回客户端名称(用于日志/审计)。
Name() string Name() string
} }
@@ -27,16 +27,6 @@ import {
} from 'lucide-react'; } from 'lucide-react';
import type { JsonData } from '@/types/common'; import type { JsonData } from '@/types/common';
// LLM 提供商列表(OpenAI 兼容接口) // Logan.Li
const LLM_PROVIDERS: { id: string; name: string; defaultEndpoint: string; models: string[] }[] = [
{ id: 'openai', name: 'OpenAI', defaultEndpoint: 'https://api.openai.com/v1', models: ['gpt-4o-mini', 'gpt-4o', 'gpt-4-turbo'] },
{ id: 'deepseek', name: 'DeepSeek', defaultEndpoint: 'https://api.deepseek.com/v1', models: ['deepseek-chat', 'deepseek-reasoner'] },
{ id: 'qwen', name: '通义千问 (DashScope)', defaultEndpoint: 'https://dashscope.aliyuncs.com/compatible-mode/v1', models: ['qwen-plus', 'qwen-turbo', 'qwen-max'] },
{ id: 'moonshot', name: 'Moonshot 月之暗面', defaultEndpoint: 'https://api.moonshot.cn/v1', models: ['moonshot-v1-8k', 'moonshot-v1-32k'] },
{ id: 'ollama', name: 'Ollama 本地', defaultEndpoint: 'http://localhost:11434/v1', models: ['qwen2.5:7b', 'llama3.2', 'deepseek-r1:7b'] },
{ id: 'custom', name: '自定义 (OpenAI 兼容)', defaultEndpoint: '', models: [] },
];
const defaultConfig: LLMConfigData = { const defaultConfig: LLMConfigData = {
provider: 'openai', provider: 'openai',
model: 'gpt-4o-mini', model: 'gpt-4o-mini',
@@ -323,15 +313,6 @@ export function AIOpsAssistant() {
{config.model} {config.model}
</Badge> </Badge>
) : null} ) : null}
<Button
variant="outline"
size="sm"
className="border-slate-700 text-slate-300 hover:text-purple-400"
onClick={() => navigate('/ai-llm-config')}
>
<Settings className="h-4 w-4 mr-1" />
模型配置
</Button>
</div> </div>
</div> </div>
+18 -35
View File
@@ -13,19 +13,27 @@ import { toast } from 'sonner';
import { Loader2, Save, Wifi, KeyRound, Brain } from 'lucide-react'; import { Loader2, Save, Wifi, KeyRound, Brain } from 'lucide-react';
import { aiApi, type LLMConfigData } from '@/api'; import { aiApi, type LLMConfigData } from '@/api';
// OpenAI 兼容提供商列表 // Logan.Li // 提供商列表(后端按 provider 自动路由协议) // Logan.Li
// Anthropic 协议(glm-coding / anthropic):端点不含 /v1,后端拼 /v1/messages。
// OpenAI 协议(其余):端点含 /v1,后端拼 /chat/completions。
const PROVIDERS = [ const PROVIDERS = [
{ id: 'openai', name: 'OpenAI', endpoint: 'https://api.openai.com/v1', models: 'gpt-4o-mini, gpt-4o' }, { id: 'openai', name: 'OpenAI', endpoint: 'https://api.openai.com/v1', models: 'gpt-4o-mini, gpt-4o, gpt-4.1' },
{ id: 'deepseek', name: 'DeepSeek', endpoint: 'https://api.deepseek.com/v1', models: 'deepseek-chat, deepseek-reasoner' }, { id: 'deepseek', name: 'DeepSeek', endpoint: 'https://api.deepseek.com/v1', models: 'deepseek-v4-flash, deepseek-v4-pro' },
{ id: 'glm', name: '智谱 GLM (通用)', endpoint: 'https://open.bigmodel.cn/api/paas/v4', models: 'glm-4.6, glm-4.5, glm-4-flash' },
{ id: 'glm-coding', name: '智谱 GLM (编码套餐)', endpoint: 'https://open.bigmodel.cn/api/anthropic', models: 'GLM-5.2' },
{ id: 'qwen', name: '通义千问 (DashScope)', endpoint: 'https://dashscope.aliyuncs.com/compatible-mode/v1', models: 'qwen-plus, qwen-turbo, qwen-max' }, { id: 'qwen', name: '通义千问 (DashScope)', endpoint: 'https://dashscope.aliyuncs.com/compatible-mode/v1', models: 'qwen-plus, qwen-turbo, qwen-max' },
{ id: 'moonshot', name: 'Moonshot 月之暗面', endpoint: 'https://api.moonshot.cn/v1', models: 'moonshot-v1-8k, moonshot-v1-32k' }, { id: 'moonshot', name: 'Moonshot (Kimi)', endpoint: 'https://api.moonshot.cn/v1', models: 'kimi-latest, moonshot-v1-8k, moonshot-v1-32k' },
{ id: 'kimi', name: 'Kimi 编码套餐', endpoint: 'https://api.kimi.com/coding/v1', models: 'kimi-for-coding' },
{ id: 'mimo', name: '小米 MiMo', endpoint: 'https://api.mimo.mi.com/v1', models: 'mimo-v2-flash, mimo-v2' },
{ id: 'anthropic', name: 'Anthropic Claude', endpoint: 'https://api.anthropic.com', models: 'claude-sonnet-4-6, claude-opus-4-8, claude-haiku-4-5' },
{ id: 'ollama', name: 'Ollama 本地', endpoint: 'http://localhost:11434/v1', models: 'qwen2.5:7b, llama3.2' }, { id: 'ollama', name: 'Ollama 本地', endpoint: 'http://localhost:11434/v1', models: 'qwen2.5:7b, llama3.2' },
{ id: 'custom', name: '自定义 (OpenAI 兼容)', endpoint: '', models: '' }, { id: 'custom', name: '自定义 (OpenAI 兼容)', endpoint: '', models: '' },
]; ];
// 生成参数(温度/max_tokens/系统提示词)已按 Claude Code 模式精简:UI 不再暴露,走内置默认。 // Logan.Li
const defaultCfg: LLMConfigData = { const defaultCfg: LLMConfigData = {
provider: 'deepseek', model: 'deepseek-chat', api_key: '', provider: 'deepseek', model: 'deepseek-v4-flash', api_key: '',
endpoint: 'https://api.deepseek.com/v1', temperature: 0.3, max_tokens: 4096, endpoint: 'https://api.deepseek.com/v1', temperature: 0.7, max_tokens: 8192,
system_prompt: '', enabled: false, system_prompt: '', enabled: false,
}; };
@@ -105,36 +113,11 @@ export default function LLMConfigPage() {
</CardContent> </CardContent>
</Card> </Card>
<Card className="bg-slate-900/60 border-slate-800">
<CardHeader><CardTitle className="text-slate-200 text-base">生成参数</CardTitle></CardHeader>
<CardContent className="space-y-4">
<div className="grid grid-cols-2 gap-4">
<div className="space-y-1.5">
<Label className="text-slate-300">温度(Temperature)</Label>
<Input type="number" step="0.1" min="0" max="2" value={cfg.temperature}
onChange={(e) => set('temperature', Number(e.target.value))}
className="bg-slate-800 border-slate-700 text-slate-200" />
</div>
<div className="space-y-1.5">
<Label className="text-slate-300">最大输出 Token</Label>
<Input type="number" min="256" value={cfg.max_tokens}
onChange={(e) => set('max_tokens', Number(e.target.value))}
className="bg-slate-800 border-slate-700 text-slate-200" />
</div>
</div>
<div className="space-y-1.5">
<Label className="text-slate-300">系统提示词(可选,留空使用内置运维助手角色)</Label>
<textarea value={cfg.system_prompt} onChange={(e) => set('system_prompt', e.target.value)} rows={3}
className="w-full bg-slate-800 border border-slate-700 rounded-md px-3 py-2 text-sm text-slate-200 resize-none" />
</div>
<div className="flex items-center justify-between">
<Label className="text-slate-300">启用 LLM 服务</Label>
<Switch checked={cfg.enabled} onCheckedChange={(v) => set('enabled', v)} />
</div>
</CardContent>
</Card>
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<div className="flex items-center gap-2 text-sm text-slate-300">
<Switch checked={cfg.enabled} onCheckedChange={(v) => set('enabled', v)} />
<span>启用 LLM 服务</span>
</div>
<Button onClick={save} disabled={saving} className="bg-blue-600 hover:bg-blue-700"> <Button onClick={save} disabled={saving} className="bg-blue-600 hover:bg-blue-700">
{saving ? <Loader2 className="w-4 h-4 mr-1 animate-spin" /> : <Save className="w-4 h-4 mr-1" />} 保存配置 {saving ? <Loader2 className="w-4 h-4 mr-1 animate-spin" /> : <Save className="w-4 h-4 mr-1" />} 保存配置
</Button> </Button>
@@ -1,7 +1,11 @@
import { HardDrive, Database, Zap, Loader2 } from 'lucide-react'; import { useState } from 'react';
import { HardDrive, Database, Zap, Loader2, Sparkles } from 'lucide-react';
import { Badge } from '@/components/ui/badge'; import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import type { CapacityForecast } from './predictUtils'; import { toast } from 'sonner';
import { aiApi } from '@/api';
import { buildCapacityInsightPrompt, type CapacityForecast } from './predictUtils';
export function PredictiveCapacityTab({ export function PredictiveCapacityTab({
forecasts, forecasts,
@@ -10,6 +14,25 @@ export function PredictiveCapacityTab({
forecasts: CapacityForecast[]; forecasts: CapacityForecast[];
loading: boolean; loading: boolean;
}) { }) {
const [insight, setInsight] = useState('');
const [analyzing, setAnalyzing] = useState(false);
// AI 深度解读:把容量预测硬数据喂给 LLM,生成综合风险评估与运维建议 // Logan.Li
// 复用 aiApi.chat(双协议后自动支持 GLM-5.2 等);失败仅 toast,下方逐条模板建议仍可读。
const analyze = async () => {
const prompt = buildCapacityInsightPrompt(forecasts);
if (!prompt) return;
setAnalyzing(true);
try {
const res = await aiApi.chat([{ role: 'user', content: prompt }]);
setInsight(res?.content || '');
} catch {
toast.error('AI 解读失败,请检查大模型配置或稍后重试');
} finally {
setAnalyzing(false);
}
};
return ( return (
<Card className="bg-slate-800 border-slate-700"> <Card className="bg-slate-800 border-slate-700">
<CardHeader className="pb-3"> <CardHeader className="pb-3">
@@ -18,7 +41,22 @@ export function PredictiveCapacityTab({
<HardDrive className="h-5 w-5 text-amber-400" /> <HardDrive className="h-5 w-5 text-amber-400" />
容量规划预警 容量规划预警
</CardTitle> </CardTitle>
<span className="text-xs text-slate-500">基于日均增长率推算阈值触及时间</span> {!loading && forecasts.length > 0 && (
<Button
variant="outline"
size="sm"
onClick={analyze}
disabled={analyzing}
className="border-purple-500/30 text-purple-300 hover:text-purple-200 h-7"
>
{analyzing ? (
<Loader2 className="h-3.5 w-3.5 mr-1 animate-spin" />
) : (
<Sparkles className="h-3.5 w-3.5 mr-1" />
)}
AI 深度解读
</Button>
)}
</div> </div>
</CardHeader> </CardHeader>
<CardContent> <CardContent>
@@ -30,6 +68,16 @@ export function PredictiveCapacityTab({
<div className="text-center py-12 text-slate-500">暂无容量数据</div> <div className="text-center py-12 text-slate-500">暂无容量数据</div>
) : ( ) : (
<div className="space-y-4"> <div className="space-y-4">
{/* AI 深度解读结果(点击按钮后展示) */}
{insight && (
<div className="p-4 rounded-lg border border-purple-500/30 bg-purple-500/5">
<div className="flex items-center gap-2 mb-2">
<Sparkles className="h-4 w-4 text-purple-400" />
<span className="text-sm font-medium text-purple-300">AI 深度解读</span>
</div>
<p className="text-sm text-slate-300 whitespace-pre-wrap break-words">{insight}</p>
</div>
)}
{forecasts.map((forecast, idx) => ( {forecasts.map((forecast, idx) => (
<div key={idx} className="p-4 rounded-lg border border-slate-700 bg-slate-800/50"> <div key={idx} className="p-4 rounded-lg border border-slate-700 bg-slate-800/50">
<div className="flex items-center justify-between mb-3"> <div className="flex items-center justify-between mb-3">
@@ -319,3 +319,26 @@ export function trendColorClass(trend: string): string {
return 'text-slate-400'; return 'text-slate-400';
} }
} }
/**
* 构建容量预测「AI 深度解读」的 prompt —— 把硬数据序列化为结构化上下文喂给 LLM。 // Logan.Li
* 纯函数,供 PredictiveCapacityTab 调 aiApi.chat 使用。无数据返回空串(调用方据此禁用按钮)。
*/
export function buildCapacityInsightPrompt(forecasts: CapacityForecast[]): string {
if (forecasts.length === 0) return '';
const lines = forecasts.map((f) => {
const risk = f.daysUntilThreshold >= 999 ? '增长平缓,暂无触顶风险' : `${f.daysUntilThreshold} 天后触及阈值`;
return `- ${f.resource}:当前 ${f.currentUsage}% → 7天后预测 ${f.predictedUsage}%(阈值 ${f.threshold}%),${risk}`;
});
return [
'你是 AIOps 容量规划专家。基于以下服务器资源容量预测数据,给出综合风险评估与可执行的运维建议:',
'',
'【容量预测数据】',
...lines,
'',
'要求:',
'1. 指出最紧急的风险资源(触及阈值最快者优先)',
'2. 给出针对性处置建议(扩容 / 清理 / 优化,具体到操作命令或步骤)',
'3. 简洁中文,Markdown 无序列表,不超过 200 字',
].join('\n');
}