feat: 模块化命令框架 + runbook 严格断言与查询结果回传

- core/module: 命令模板、门控(feature flag/confirm/dry-run)、ecode 错误标签、ResultFormatter
- runbook executor: http.get/http.post/assert.json(expect_success 严格断言)、目标主机管理、任务落库、最终 stdout 直接回传
- cf 模块: /cf dns list 支持域名自动解析 zone_id;dns list/zones/workers list 格式化回传;API success:false 判真失败
- 全部 CF/CPA runbook 接入严格断言,杜绝假阳性 success
- 新增 cpa/cfdav/mail/sub 模块与 runbooks;QQ 订阅到期提醒调度
- Web 后台: 模块设置(CF/CPA/cfdav/AI)、OPS 任务、渠道管理、审计页
This commit is contained in:
2026-09-13 07:42:17 +08:00
parent 9085c490f3
commit 81939825d1
86 changed files with 6885 additions and 467 deletions
+103
View File
@@ -0,0 +1,103 @@
package ai
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
)
type Client struct {
BaseURL string
APIKey string
Model string
Timeout time.Duration
}
type chatMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
type chatRequest struct {
Model string `json:"model"`
Messages []chatMessage `json:"messages"`
Temperature float64 `json:"temperature"`
}
type chatResponse struct {
Choices []struct {
Message chatMessage `json:"message"`
} `json:"choices"`
Error *struct {
Message string `json:"message"`
} `json:"error"`
}
func (c *Client) Suggest(userInput string) (string, error) {
return c.chat(userInput)
}
func (c *Client) Explain(result string) (string, error) {
return "", nil
}
func commandGuide() string {
b, err := os.ReadFile("docs/ai_command_guide.md")
if err != nil {
return ""
}
return strings.TrimSpace(string(b))
}
func (c *Client) chat(userInput string) (string, error) {
if strings.TrimSpace(c.BaseURL) == "" || strings.TrimSpace(c.APIKey) == "" || strings.TrimSpace(c.Model) == "" {
return "", errors.New("ai config missing")
}
base := strings.TrimRight(c.BaseURL, "/")
url := base + "/chat/completions"
sys := "你是命令翻译器。把用户的自然语言转换成系统支持的标准命令。只输出一行命令,不要解释。若无法确定,输出 FAIL。\n\n可用命令知识库:\n" + commandGuide() + "\n\n规则:严格按命令格式输出。缺少关键参数时输出 FAIL。不要猜测 zone_id/record_id/backup_id。"
req := chatRequest{
Model: c.Model,
Messages: []chatMessage{
{Role: "system", Content: sys},
{Role: "user", Content: userInput},
},
Temperature: 0,
}
body, _ := json.Marshal(req)
client := &http.Client{Timeout: c.Timeout}
httpReq, _ := http.NewRequest("POST", url, bytes.NewReader(body))
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+c.APIKey)
resp, err := client.Do(httpReq)
if err != nil {
return "", err
}
defer resp.Body.Close()
b, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
if resp.StatusCode == 429 {
return "", fmt.Errorf("ai rate limited")
}
return "", fmt.Errorf("ai http %d", resp.StatusCode)
}
var out chatResponse
if err := json.Unmarshal(b, &out); err != nil {
return "", err
}
if out.Error != nil && out.Error.Message != "" {
return "", errors.New(out.Error.Message)
}
if len(out.Choices) == 0 {
return "", errors.New("empty ai response")
}
return strings.TrimSpace(out.Choices[0].Message.Content), nil
}