驱动接入(全部本机真实例验收): - Elasticsearch 原生 SQL API、InfluxDB 原生 HTTP+Flux(官方 JDBC 付费许可,实测弃用) - ZooKeeper/MongoDB JVM agent(连接时自动下载官方驱动 jar,sha256 钉版本到 ~/.opsxdb/drivers/) - 达梦 JDBC agent 桥就绪(待厂商 jar+实例);TiDB 真实例验收 - 协议族代码审查放行 PolarDB/TDSQL/OceanBase/Kingbase/GaussDB/Oracle - 驱动能力模型:按驱动开关对象树分组/右键菜单/行编辑,NoSQL 树拍平(对象直挂库节点) 驱动管理: - 驱动管理弹框(状态/预装/离线包导入/JDK 检测)+ /api/drivers/* 三接口 - opsxdb-driver 驱动商店仓库(清单/fetch.sh/pack.sh 离线包/import.sh) - 连接时懒加载:DMG 零 Java 产物,首连自动下载(实测 ZK 九件套 1.35s),离线包自动回退 UI/交互: - 设置中心(⚙):常规/执行/结果/驱动/快捷键/关于,左导航+右面板(对标 opsxshell) - 实例级展开箭头(行首 SVG chevron,替代字体回退失真的 Unicode 三角) - 树内嵌套渲染修复:对象树挂在激活连接下;工具栏窄窗口不断行(按钮 nowrap+整颗换行) - 侧栏连接行精简(图标+连接名)、SQLite SSH 守卫、Oracle 死参数清理 质量: - Go 协议层/离线回退单测、E2E 门控测试(ES/ZK/Influx/Mongo/TiDB/JDBC 桥) - 驱动注册表单一事实来源 connection-types.json,README/BACKLOG/AGENTS 同步
118 lines
3.0 KiB
Go
118 lines
3.0 KiB
Go
package ai
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
defaultOpenAIBaseURL = "https://api.openai.com/v1"
|
|
defaultOpenAIModel = "gpt-4o-mini"
|
|
)
|
|
|
|
// OpenAIProvider OpenAI 兼容协议实现(DeepSeek/通义/本地 vLLM 同协议可用)。
|
|
type OpenAIProvider struct {
|
|
baseURL string
|
|
apiKey string
|
|
model string
|
|
hc *http.Client
|
|
}
|
|
|
|
func NewOpenAIProvider(cfg Config) *OpenAIProvider {
|
|
base := strings.TrimRight(strings.TrimSpace(cfg.BaseURL), "/")
|
|
if base == "" {
|
|
base = defaultOpenAIBaseURL
|
|
}
|
|
model := strings.TrimSpace(cfg.Model)
|
|
if model == "" {
|
|
model = defaultOpenAIModel
|
|
}
|
|
return &OpenAIProvider{
|
|
baseURL: base,
|
|
apiKey: cfg.APIKey,
|
|
model: model,
|
|
hc: &http.Client{Timeout: 60 * time.Second},
|
|
}
|
|
}
|
|
|
|
func (p *OpenAIProvider) Name() string { return "openai" }
|
|
|
|
type chatCompletionReq struct {
|
|
Model string `json:"model"`
|
|
Messages []Message `json:"messages"`
|
|
}
|
|
|
|
type chatCompletionRes struct {
|
|
Choices []struct {
|
|
Message Message `json:"message"`
|
|
} `json:"choices"`
|
|
Error *struct {
|
|
Message string `json:"message"`
|
|
} `json:"error"`
|
|
}
|
|
|
|
// Complete 单次非流式调用;ctx 全透传,HTTP 层 60s 超时兜底。
|
|
func (p *OpenAIProvider) Complete(ctx context.Context, req ChatReq) (ChatRes, error) {
|
|
model := p.model
|
|
if strings.TrimSpace(req.Model) != "" {
|
|
model = strings.TrimSpace(req.Model)
|
|
}
|
|
payload := chatCompletionReq{
|
|
Model: model,
|
|
Messages: BuildMessages(req),
|
|
}
|
|
body, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return ChatRes{}, err
|
|
}
|
|
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, p.baseURL+"/chat/completions", bytes.NewReader(body))
|
|
if err != nil {
|
|
return ChatRes{}, err
|
|
}
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
httpReq.Header.Set("Authorization", "Bearer "+p.apiKey)
|
|
|
|
resp, err := p.hc.Do(httpReq)
|
|
if err != nil {
|
|
return ChatRes{}, fmt.Errorf("AI 请求失败: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
raw, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
|
|
if err != nil {
|
|
return ChatRes{}, fmt.Errorf("读取 AI 响应失败: %w", err)
|
|
}
|
|
var out chatCompletionRes
|
|
if err := json.Unmarshal(raw, &out); err != nil {
|
|
return ChatRes{}, fmt.Errorf("解析 AI 响应失败(HTTP %d)", resp.StatusCode)
|
|
}
|
|
if out.Error != nil {
|
|
return ChatRes{}, fmt.Errorf("AI 服务返回错误: %s", out.Error.Message)
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
return ChatRes{}, fmt.Errorf("AI 服务 HTTP %d", resp.StatusCode)
|
|
}
|
|
if len(out.Choices) == 0 {
|
|
return ChatRes{}, fmt.Errorf("AI 服务未返回候选结果")
|
|
}
|
|
content := strings.TrimSpace(out.Choices[0].Message.Content)
|
|
return ChatRes{Content: content, SQL: extractSQL(content)}, nil
|
|
}
|
|
|
|
var sqlFence = regexp.MustCompile("(?s)```sql\\s*\n(.+?)```")
|
|
|
|
// extractSQL 从模型回复中提取 ```sql 代码块内容。
|
|
func extractSQL(content string) string {
|
|
m := sqlFence.FindStringSubmatch(content)
|
|
if len(m) < 2 {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(m[1])
|
|
}
|