mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-10-15 12:15:16 +00:00
## 背景 仓库早已迁移到 GitHub(`github.com/JianFeeeee/HomeAgent`,CI/CD 都在那边跑), gitcode 保留为**国内只读镜像**(`ci.yml` 的 sync-gitcode-main 快进推送)。 但 go.mod 的 module 路径一直是 `gitcode.com/JianFeeeee/...` —— 既与权威源 不符,`meta.SourceURL`(AGPL §13 的源码提供入口)也指向镜像而非实际运行的代码。 ## 关键区分(这是本次改动安全的前提) 两个 module 是**不同**的,影响面差一个数量级: | module | 谁依赖 | 改它的后果 | |---|---|---| | `.../HomeAgent`(主仓) | **只有主仓自己** | 内部重构,零外部影响 | | `.../homeagent-sdk`(SDK 仓) | 主仓 49 文件 + 所有外部插件 | 需重编插件 | ★ 实测确认:**插件不 import 主仓 module**(`strings qq/plugin.bin | grep HomeAgent` = 0), 只依赖 SDK 仓。而插件与内核的边界是 **RPC 方法名字符串** (`plugin.init` / `tool.invoke`…,见 proc/protocol.go),**与 Go module 名无关**。 ⇒ module 改名**不改变任何 wire 协议**,现网已装的插件照常运行。 SDK 的 GitHub 仓名是 `homeagentsdk`(无连字符),module 路径必须与之精确匹配, 否则 `go get` 会因路径不符失败,故取 `github.com/JianFeeeee/homeagentsdk`。 ## 改动 - `go.mod` / `go.sum`:两个 module 路径 + require/replace - 277 个 Go 文件 + Makefile + 打包脚本的 import 与 ldflags 路径 - SDK 仓 `meta.CoreModule`、`tools/hmapdev` 的 module 声明与工程模板 - 对外文档/链接的仓库地址(README、PLUGIN_DEV、OVERVIEW 等) ## 明确**保留** gitcode 的地方(镜像机制,不能动) - `GITCODE_TOKEN`、`GC_URL="…@gitcode.com"`(CI 同步 job) - `gitcode.com/api/v5/...`(release 附件上传) - git remote `gitcode`(镜像拉取/推送) - `docs/zh/ci-cd-runbook.md`(整篇讲镜像运维) ## 验证 - `go build ./...` / `go vet ./...` 绿 - `go test ./... -count=1` → 48 包 ok / 0 FAIL - hmapdev 工具链编译通过(`third_party/homeagent-sdk/tools/hmapdev`) - 全仓残留检查:module 形态的 `gitcode.com/JianFeeeee` = 0 - 镜像机制抽验:GC_URL / @gitcode / api/v5 / remote 均完好 ## 顺带修正 `hmapdev/templates.go` 的注释此前写「gitcode 的模块不在 proxy.golang.org 上」—— module 已迁 GitHub,该论断不再成立。改为如实说明**为何仍默认 replace 到本机 SDK**: 离线可构建、避免版本漂移、内网可用。理由与托管地无关,故 replace 保留。
301 lines
8.8 KiB
Go
301 lines
8.8 KiB
Go
package aiimage
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
"os"
|
||
"path/filepath"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/JianFeeeee/HomeAgent/internal/plugin"
|
||
sdk "github.com/JianFeeeee/HomeAgent/internal/sdk"
|
||
)
|
||
|
||
func init() {
|
||
plugin.RegisterPluginMeta("ai_image", "AI 生图", "AI Image")
|
||
plugin.RegisterFactory("ai_image", func(name string, config map[string]interface{}) (sdk.Plugin, error) {
|
||
return New(name), nil
|
||
})
|
||
}
|
||
|
||
type Plugin struct {
|
||
name string
|
||
client *http.Client
|
||
apiKey string
|
||
baseURL string
|
||
model string
|
||
size string
|
||
dataDir string
|
||
}
|
||
|
||
func New(name string) *Plugin {
|
||
return &Plugin{
|
||
name: name,
|
||
client: &http.Client{Timeout: 120 * time.Second},
|
||
}
|
||
}
|
||
|
||
func (p *Plugin) Name() string { return p.name }
|
||
|
||
func getSettingString(s sdk.SettingsAPI, key, def string) string {
|
||
if s == nil {
|
||
return def
|
||
}
|
||
v, err := s.Get(key)
|
||
if err != nil || v == nil {
|
||
return def
|
||
}
|
||
if sv, ok := v.(string); ok && sv != "" {
|
||
return sv
|
||
}
|
||
return def
|
||
}
|
||
|
||
func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||
s.SetAutoRestart(true)
|
||
|
||
// 插件专属数据目录(SDK DataDir API,内核保证存在)
|
||
if dd := s.Settings().DataDir(); dd != "" {
|
||
p.dataDir = dd
|
||
}
|
||
if p.dataDir != "" {
|
||
os.MkdirAll(p.dataDir, 0755)
|
||
}
|
||
|
||
s.Settings().RegisterDef(sdk.ConfigDef{
|
||
Key: "base_url", Default: "http://127.0.0.1:8081/v1", Type: "string",
|
||
DisplayName: "Base URL", Description: "OpenAI 兼容生图网关地址(默认本机 llmsproxy)",
|
||
Category: "ai_image",
|
||
})
|
||
s.Settings().RegisterDef(sdk.ConfigDef{
|
||
Key: "api_key", Default: "", Type: "password",
|
||
DisplayName: "API Key", Description: "生图网关 API Key(默认使用内核 LLM key,留空则取 core.llm.api_key)",
|
||
Category: "ai_image",
|
||
})
|
||
s.Settings().RegisterDef(sdk.ConfigDef{
|
||
Key: "model", Default: "", Type: "string",
|
||
DisplayName: "Model", Description: "生图模型 ID,留空由网关 AUTO 决定(如 flux-1)",
|
||
Category: "ai_image",
|
||
})
|
||
s.Settings().RegisterDef(sdk.ConfigDef{
|
||
Key: "size", Default: "1024x1024", Type: "string",
|
||
DisplayName: "Size", Description: "默认图片尺寸,如 1024x1024",
|
||
Category: "ai_image",
|
||
})
|
||
|
||
p.apiKey = getSettingString(s.Settings(), "api_key", "")
|
||
p.baseURL = strings.TrimRight(getSettingString(s.Settings(), "base_url", "http://127.0.0.1:8081/v1"), "/")
|
||
p.model = getSettingString(s.Settings(), "model", "")
|
||
p.size = getSettingString(s.Settings(), "size", "1024x1024")
|
||
|
||
if p.apiKey == "" {
|
||
if v, _ := s.Settings().GetCore("llm.api_key"); v != nil {
|
||
if sv, ok := v.(string); ok && sv != "" {
|
||
p.apiKey = sv
|
||
}
|
||
}
|
||
}
|
||
|
||
s.RegisterTool("ai_image_generate", sdk.ToolDef{
|
||
Name: "ai_image_generate", Description: "Generate image from text prompt using AI. Returns image URL.",
|
||
Parameters: map[string]interface{}{
|
||
"type": "object",
|
||
"properties": map[string]interface{}{
|
||
"prompt": map[string]interface{}{"type": "string", "description": "Text description of the image to generate"},
|
||
"size": map[string]interface{}{"type": "string", "description": "Image size (1024x1024, etc.), default from config"},
|
||
"model": map[string]interface{}{"type": "string", "description": "Model override (e.g. flux-1)"},
|
||
"n": map[string]interface{}{"type": "integer", "description": "Number of images to generate (1-10), default 1"},
|
||
},
|
||
"required": []string{"prompt"},
|
||
},
|
||
// 外部调用,插件内无共享可变状态
|
||
ParallelSafe: true,
|
||
}, p.handleGenerate)
|
||
|
||
return nil
|
||
}
|
||
|
||
func (p *Plugin) Stop() error { return nil }
|
||
|
||
type genRequest struct {
|
||
Model string `json:"model"`
|
||
Prompt string `json:"prompt"`
|
||
N int `json:"n"`
|
||
Size string `json:"size"`
|
||
ResponseFormat string `json:"response_format"`
|
||
}
|
||
|
||
type genResp struct {
|
||
Created int64 `json:"created"`
|
||
Data []struct {
|
||
URL string `json:"url"`
|
||
B64JSON string `json:"b64_json"`
|
||
RevisedPrompt string `json:"revised_prompt"`
|
||
} `json:"data"`
|
||
Error *struct {
|
||
Message string `json:"message"`
|
||
Type string `json:"type"`
|
||
} `json:"error"`
|
||
}
|
||
|
||
func (p *Plugin) handleGenerate(args map[string]interface{}) (interface{}, error) {
|
||
prompt, _ := args["prompt"].(string)
|
||
if strings.TrimSpace(prompt) == "" {
|
||
return map[string]interface{}{"isError": true, "content": "prompt is required"}, nil
|
||
}
|
||
|
||
key := p.apiKey
|
||
if key == "" {
|
||
return map[string]interface{}{"isError": true, "content": "生图 API key 未配置(plugin.ai_image.api_key 或 core.llm.api_key)"}, nil
|
||
}
|
||
|
||
model := p.model
|
||
if m, ok := args["model"].(string); ok && m != "" {
|
||
model = m
|
||
}
|
||
size := p.size
|
||
if sz, ok := args["size"].(string); ok && sz != "" {
|
||
size = sz
|
||
}
|
||
n := 1
|
||
if nv, ok := args["n"].(float64); ok {
|
||
n = int(nv)
|
||
if n < 1 {
|
||
n = 1
|
||
}
|
||
if n > 10 {
|
||
n = 10
|
||
}
|
||
}
|
||
|
||
body := genRequest{Model: model, Prompt: prompt, N: n, Size: size, ResponseFormat: "url"}
|
||
raw, _ := json.Marshal(body)
|
||
|
||
url := p.baseURL + "/images/generations"
|
||
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(raw))
|
||
if err != nil {
|
||
return map[string]interface{}{"isError": true, "content": "构造请求失败: " + err.Error()}, nil
|
||
}
|
||
req.Header.Set("Content-Type", "application/json")
|
||
req.Header.Set("Authorization", "Bearer "+key)
|
||
|
||
resp, err := p.client.Do(req)
|
||
if err != nil {
|
||
return map[string]interface{}{"isError": true, "content": "生图请求失败: " + err.Error()}, nil
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
respBody, _ := io.ReadAll(resp.Body)
|
||
if resp.StatusCode != 200 {
|
||
return map[string]interface{}{"isError": true, "content": fmt.Sprintf("生图 API error (status %d): %s", resp.StatusCode, string(respBody))}, nil
|
||
}
|
||
|
||
var result genResp
|
||
if err := json.Unmarshal(respBody, &result); err != nil {
|
||
return map[string]interface{}{"isError": true, "content": "解析生图响应失败: " + err.Error()}, nil
|
||
}
|
||
if result.Error != nil {
|
||
return map[string]interface{}{"isError": true, "content": "生图 API error: " + result.Error.Message}, nil
|
||
}
|
||
if len(result.Data) == 0 {
|
||
return map[string]interface{}{"isError": true, "content": "no images returned"}, nil
|
||
}
|
||
|
||
var urls []string
|
||
for _, d := range result.Data {
|
||
imgURL := d.URL
|
||
if imgURL == "" && d.B64JSON != "" {
|
||
imgURL = "data:image/png;base64," + d.B64JSON
|
||
}
|
||
if imgURL != "" {
|
||
urls = append(urls, imgURL)
|
||
}
|
||
}
|
||
if len(urls) == 0 {
|
||
return map[string]interface{}{"isError": true, "content": "生图响应中没有可用图片"}, nil
|
||
}
|
||
|
||
// 下载到插件数据目录,返回本地文件路径(而非临时 S3 URL):
|
||
// - S3 临时 URL 约 1 小时过期,且对无浏览器 UA 客户端拒绝访问(agent 裸 curl 验证必败)
|
||
// - 本地路径经 webui /files/ 永久下发,支持 output_send type=image
|
||
var localPaths []string
|
||
var dlErrs []string
|
||
for i, u := range urls {
|
||
if strings.HasPrefix(u, "data:") {
|
||
continue // base64 内联图不落盘
|
||
}
|
||
path, err := p.downloadImage(u, fmt.Sprintf("ai_%d_%d", time.Now().UnixNano(), i))
|
||
if err != nil {
|
||
dlErrs = append(dlErrs, fmt.Sprintf("第%d张保存失败: %v", i+1, err))
|
||
continue
|
||
}
|
||
localPaths = append(localPaths, path)
|
||
}
|
||
|
||
content := fmt.Sprintf("Generated %d image(s) with model %s", len(urls), model)
|
||
if len(localPaths) > 0 {
|
||
content += "\n本地文件:\n" + strings.Join(localPaths, "\n")
|
||
content += "\n\n图片已保存到本地(不会过期)。如需展示请用 output_send__webui(payload=本地路径, type=image)。"
|
||
}
|
||
if len(dlErrs) > 0 {
|
||
content += "\n\n" + strings.Join(dlErrs, "\n")
|
||
}
|
||
if len(localPaths) < len(urls) {
|
||
content += "\n原始 URL(1小时内有效):\n" + strings.Join(urls, "\n")
|
||
}
|
||
|
||
return map[string]interface{}{
|
||
"content": content,
|
||
"images": urls,
|
||
"local_paths": localPaths,
|
||
"prompt": prompt,
|
||
"model": model,
|
||
}, nil
|
||
}
|
||
|
||
// downloadImage 把生图返回的临时 URL 下载为本地文件,返回路径。
|
||
// 带浏览器 UA 规避图床对无 UA 客户端的拦截。
|
||
func (p *Plugin) downloadImage(imgURL, baseName string) (string, error) {
|
||
if p.dataDir == "" {
|
||
return "", fmt.Errorf("data dir unavailable")
|
||
}
|
||
dl := &http.Client{Timeout: 60 * time.Second}
|
||
req, err := http.NewRequest(http.MethodGet, imgURL, nil)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; HomeAgent/1.0)")
|
||
resp, err := dl.Do(req)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
defer resp.Body.Close()
|
||
if resp.StatusCode != http.StatusOK {
|
||
b, _ := io.ReadAll(resp.Body)
|
||
if len(b) > 200 {
|
||
b = b[:200]
|
||
}
|
||
return "", fmt.Errorf("HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(b)))
|
||
}
|
||
data, err := io.ReadAll(resp.Body)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
ext := ".png"
|
||
switch ct := resp.Header.Get("Content-Type"); {
|
||
case strings.Contains(ct, "jpeg"), strings.Contains(ct, "jpg"):
|
||
ext = ".jpg"
|
||
case strings.Contains(ct, "webp"):
|
||
ext = ".webp"
|
||
}
|
||
path := filepath.Join(p.dataDir, baseName+ext)
|
||
if err := os.WriteFile(path, data, 0644); err != nil {
|
||
return "", err
|
||
}
|
||
return path, nil
|
||
}
|