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:
1 parent
c0ee8e4ddd
commit
0b2e43ea9e
16 files changed
+689
-296
No files matched your search
@@ -166,6 +166,7 @@ func (ac *agentConn) startTask(sshCfg remoteexec.SSHConfig, llmCfg model.LLMConf
|
||||
// 构建引擎
|
||||
engine, err := agentcli.NewEngine(agentcli.EngineConfig{
|
||||
LLM: llm.Config{
|
||||
Protocol: resolveProtocol(llmCfg.Provider),
|
||||
Endpoint: resolveEndpoint(llmCfg.Provider, llmCfg.Endpoint),
|
||||
APIKey: llmCfg.APIKey, Model: llmCfg.Model,
|
||||
Temperature: llmCfg.Temperature, MaxTokens: llmCfg.MaxTokens,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -43,15 +44,7 @@ func (h *CredentialHandler) AssignCredential(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if cred.SecretType == "ssh" {
|
||||
server.SSHKey = plain
|
||||
server.AuthType = "key"
|
||||
server.Password = ""
|
||||
} else {
|
||||
server.Password = plain
|
||||
server.AuthType = "password"
|
||||
server.SSHKey = ""
|
||||
}
|
||||
applyCredential(cred, plain, &server)
|
||||
|
||||
if err := h.db.Save(&server).Error; err != nil {
|
||||
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: "凭据已关联"})
|
||||
}
|
||||
|
||||
// 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
@@ -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
|
||||
func resolveEndpoint(provider, endpoint string) string {
|
||||
if endpoint != "" {
|
||||
@@ -147,10 +130,20 @@ func resolveEndpoint(provider, endpoint string) string {
|
||||
switch provider {
|
||||
case "deepseek":
|
||||
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":
|
||||
return "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
||||
case "moonshot":
|
||||
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":
|
||||
return "http://localhost:11434/v1"
|
||||
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
|
||||
const defaultSystemPrompt = `你是 AIOps 智能运维助手,专注于企业级运维场景。你可以:
|
||||
1. 分析服务器、容器、K8s 集群的性能瓶颈与异常
|
||||
|
||||
+52
-194
@@ -1,22 +1,41 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ezops/middleware"
|
||||
"ezops/model"
|
||||
"ezops/pkg/agentcli/llm"
|
||||
|
||||
"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
|
||||
func (h *LLMHandler) Chat(c *gin.Context) {
|
||||
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...)
|
||||
|
||||
// 构造 OpenAI 兼容请求
|
||||
payload := openaiChatRequest{
|
||||
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))
|
||||
resp, err := h.buildLLMClient(cfg).Complete(c.Request.Context(), &llm.CompletionRequest{
|
||||
Messages: toLLMMessages(messages),
|
||||
})
|
||||
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
|
||||
}
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
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
|
||||
replyContent := resp.Message.Content
|
||||
|
||||
// 持久化对话历史(按用户隔离,成对写入 user + assistant) // Logan.Li
|
||||
// 仅在成功时入库;错误气泡为瞬时 UI 不持久化
|
||||
uid := middleware.GetUserID(c)
|
||||
if uid != 0 && len(req.Messages) > 0 {
|
||||
// 取最后一条用户消息(前端已附加当前提问)
|
||||
lastUser := req.Messages[len(req.Messages)-1]
|
||||
now := time.Now()
|
||||
// session_id:前端传入或后端生成(兼容旧版客户端) // Logan.Li
|
||||
sid := req.SessionID
|
||||
sid := req.SessionID // 前端传入或后端生成(兼容旧版客户端)
|
||||
if sid == "" {
|
||||
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{
|
||||
Content: replyContent,
|
||||
Model: cfg.Model,
|
||||
TokensUsed: chatResp.Usage.TotalTokens,
|
||||
TokensUsed: resp.Usage.TotalTokens,
|
||||
}})
|
||||
}
|
||||
|
||||
// ChatStream AI 流式对话代理(SSE) // Logan.Li
|
||||
// POST /api/v1/aiops/chat/stream
|
||||
// 客户端用 EventSource/fetch-stream 消费,每条 SSE data 为 {"content": "..."}
|
||||
// 出错时发送 event: error,data 为 {"message": "..."}
|
||||
// 完成后服务端持久化 user + assistant 两条历史,event: done 结束
|
||||
// 客户端用 fetch-stream 消费,每条 SSE data 为 {"content": "..."};出错发 event: error;完成发 event: done。
|
||||
// 上游协议差异(OpenAI delta / Anthropic content_block_delta)由 llm.Client.Stream 屏蔽,
|
||||
// controller 只负责 SSE 转发 + 客户端断开检测 + 历史持久化。
|
||||
func (h *LLMHandler) ChatStream(c *gin.Context) {
|
||||
var cfg model.LLMConfig
|
||||
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("X-Accel-Buffering", "no") // Nginx 关闭缓冲
|
||||
c.Writer.WriteHeader(http.StatusOK)
|
||||
// 注册写入心跳(立即 flush 一个注释行让客户端确认连接建立)
|
||||
c.Writer.Flush()
|
||||
c.Writer.Flush() // 立即 flush 让客户端确认连接建立
|
||||
|
||||
// 组装系统提示词 + 上下文
|
||||
systemPrompt := cfg.SystemPrompt
|
||||
if strings.TrimSpace(systemPrompt) == "" {
|
||||
systemPrompt = defaultSystemPrompt
|
||||
}
|
||||
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)
|
||||
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
|
||||
// 流式补全:逐 token 回调转发 SSE;ctx 取消(客户端断开)由 llm.Client 内部终止上游读取
|
||||
var contentBuilder strings.Builder
|
||||
reader := bufio.NewReader(resp.Body)
|
||||
clientGone := false
|
||||
for {
|
||||
// 客户端断开时停止读取(c.Request.Context().Done() 在客户端断开时触发) // Logan.Li
|
||||
select {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
_, streamErr := h.buildLLMClient(cfg).Stream(c.Request.Context(), &llm.CompletionRequest{
|
||||
Messages: toLLMMessages(messages),
|
||||
}, func(delta string) {
|
||||
contentBuilder.WriteString(delta)
|
||||
writeSSEChunk(c, delta)
|
||||
})
|
||||
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 {
|
||||
uid := middleware.GetUserID(c)
|
||||
if uid != 0 {
|
||||
lastUser := req.Messages[len(req.Messages)-1]
|
||||
now := time.Now()
|
||||
// session_id:前端传入或后端生成(兼容旧版客户端) // Logan.Li
|
||||
sid := req.SessionID
|
||||
if sid == "" {
|
||||
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)},
|
||||
})
|
||||
}
|
||||
// 发送结束标志
|
||||
c.Writer.WriteString("event: done\ndata: {\"model\":\"" + cfg.Model + "\"}\n\n")
|
||||
c.Writer.Flush()
|
||||
} else if !clientGone {
|
||||
@@ -298,19 +188,6 @@ func writeSSEError(c *gin.Context, code int, message string) {
|
||||
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
|
||||
// POST /api/v1/aiops/llm-config/test
|
||||
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: "请先保存配置"})
|
||||
return
|
||||
}
|
||||
payload := openaiChatRequest{
|
||||
Model: cfg.Model,
|
||||
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)
|
||||
_, err := h.buildLLMClient(cfg).Complete(c.Request.Context(), &llm.CompletionRequest{
|
||||
Messages: []llm.Message{{Role: "user", Content: "ping"}},
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, model.Response{Code: 502, Message: "连接失败: " + err.Error()})
|
||||
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: "连接成功"})
|
||||
}
|
||||
@@ -223,6 +223,21 @@ func (h *ServerHandler) CreateServer(c *gin.Context) {
|
||||
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 {
|
||||
c.JSON(http.StatusInternalServerError, model.Response{
|
||||
Code: 500,
|
||||
@@ -297,6 +312,25 @@ func (h *ServerHandler) UpdateServer(c *gin.Context) {
|
||||
if 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 != "" {
|
||||
updates["os"] = req.OS
|
||||
}
|
||||
|
||||
@@ -30,26 +30,24 @@ func (h *ServerHandler) BatchCreateServers(c *gin.Context) {
|
||||
authType = "password"
|
||||
}
|
||||
|
||||
// 如果指定了凭据,提前解密 // Logan.Li
|
||||
var decryptedPwd, decryptedKey string
|
||||
// 如果指定了凭据,提前解密(plain 在循环内由 applyCredential 解析填充) // Logan.Li
|
||||
var (
|
||||
hasCred bool
|
||||
cred model.Credential
|
||||
credPlain string
|
||||
)
|
||||
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)
|
||||
p, err := h.crypt.Decrypt(cred.Content)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "凭据解密失败"})
|
||||
return
|
||||
}
|
||||
if cred.SecretType == "ssh" {
|
||||
decryptedKey = plain
|
||||
authType = "key"
|
||||
} else {
|
||||
decryptedPwd = plain
|
||||
authType = "password"
|
||||
}
|
||||
credPlain = p
|
||||
hasCred = true
|
||||
}
|
||||
|
||||
result := model.BatchCreateResult{Errors: []string{}}
|
||||
@@ -84,9 +82,7 @@ func (h *ServerHandler) BatchCreateServers(c *gin.Context) {
|
||||
IP: ip,
|
||||
Port: port,
|
||||
Username: username,
|
||||
AuthType: authType,
|
||||
Password: decryptedPwd,
|
||||
SSHKey: decryptedKey,
|
||||
AuthType: authType, // 无凭证时用请求 authType(默认 password)
|
||||
OS: item.OS,
|
||||
CPU: item.CPU,
|
||||
Memory: item.Memory * 1024, // GB → MB
|
||||
@@ -95,6 +91,9 @@ func (h *ServerHandler) BatchCreateServers(c *gin.Context) {
|
||||
RegionID: req.RegionID,
|
||||
Status: "stopped",
|
||||
}
|
||||
if hasCred {
|
||||
applyCredential(cred, credPlain, &server) // 有凭证则覆盖认证字段(ssh→私钥/ssh_user/ssh_port) // Logan.Li
|
||||
}
|
||||
if err := h.db.Create(&server).Error; err != nil {
|
||||
result.Failed++
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("%s(%s): %s", hostname, ip, err.Error()))
|
||||
|
||||
Reference in new issue
Block a user