mirror of
https://gitcode.com/JianFeeeee/homeagent-sdk.git
synced 2026-09-20 17:08:01 +00:00
Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 219b11a1f9 | |||
| da01af1ad7 | |||
| 741e284cd4 | |||
| ce5bff9275 | |||
| e256023399 | |||
| 092d8f4ab0 | |||
| 5ed8d65479 | |||
| 9f844123fe | |||
| ef0e58ee23 | |||
| 09b64dcb53 | |||
| 56485194df | |||
| 61f307be1a | |||
| 59c6e1844c | |||
| 5c1574be25 | |||
| 68497b4092 | |||
| 130f805b6e | |||
| cd1984e26e | |||
| 6184736fd4 | |||
| 81bfdfce1d | |||
| e3f93e254b | |||
| d57c5eaf3e |
6
.gitignore
vendored
6
.gitignore
vendored
@ -10,6 +10,11 @@ plugin.json
|
||||
build/
|
||||
dist/
|
||||
|
||||
# plugindev 预编译二进制:只作为 release 附件分发,不进仓库历史。
|
||||
# 此前 5 个平台各 26-28MB 被 git 跟踪(约 137MB),每次重编都在历史里
|
||||
# 再叠一份,而它们本质是可从源码复现的产物。
|
||||
bin/
|
||||
|
||||
# Test artifacts
|
||||
testdist/
|
||||
|
||||
@ -30,4 +35,3 @@ z_entry.c
|
||||
|
||||
# plugindev binary in tools/
|
||||
tools/plugindev/plugindev
|
||||
example/recoverydiag/
|
||||
|
||||
105
README.md
105
README.md
@ -36,6 +36,7 @@ type Plugin interface {
|
||||
| 设置 | `Settings()` | 访问设置 API |
|
||||
| 事件 | `Events()` | 访问事件订阅器(外部插件仅订阅) |
|
||||
| 注入 | `InjectText(source, channel, text)` / `InjectInterruptText(source, channel, text)` / `InjectTextNoMemory(source, channel, text)` | 向管道注入文本 |
|
||||
| 多模态注入 | `InjectInputMedia(source, channel, text, blocks)` / `InjectInputMediaSync(...)` / `InjectInterruptMedia(...)` | 注入带图片/音频的输入(1.1.0 新增) |
|
||||
| 自动重启 | `SetAutoRestart(enabled)` / `AutoRestart()` | 控制崩溃自动重启 |
|
||||
|
||||
### 阶段钩子
|
||||
@ -106,6 +107,30 @@ type 枚举值:
|
||||
| `InjectInterruptText(source, channel, text)` | 注入中断文本,打断当前处理,路由到指定通道 |
|
||||
| `InjectTextNoMemory(source, channel, text)` | 注入文本,不记入内存,路由到指定通道 |
|
||||
|
||||
### 多模态注入(1.1.0 新增)
|
||||
|
||||
| 方法 | 说明 |
|
||||
|------|------|
|
||||
| `InjectInputMedia(source, channel, text, blocks)` | 注入带媒体的输入,异步 |
|
||||
| `InjectInputMediaSync(source, channel, text, blocks)` | 注入带媒体的输入并同步等待回复文本 |
|
||||
| `InjectInterruptMedia(source, channel, text, blocks)` | 注入带媒体的中断,可抢占当前处理 |
|
||||
|
||||
`blocks` 是 `[]sdk.ContentBlock`,与 `SetToolBlocks` 用同一类型:
|
||||
|
||||
```go
|
||||
s.InjectInputMedia("myplugin", "webui", "帮我看看这张图", []sdk.ContentBlock{{
|
||||
Type: "image_url",
|
||||
ImageURL: &sdk.ImageURL{URL: "data:image/png;base64," + b64, Detail: "auto"},
|
||||
}})
|
||||
```
|
||||
|
||||
与 `SetToolBlocks` 的区别:`SetToolBlocks` 只能在工具处理函数内部调用,媒体要等到
|
||||
下一条 tool message 才到模型手上;这三个方法是插件**主动发起一轮带媒体的对话**,
|
||||
媒体在本轮就随消息发给模型,并自动落进媒体存储、挂上媒体记忆引用。
|
||||
|
||||
媒体块里的 `data:` URL 会被内核落盘去重;`http(s)` URL 只透传给模型,不入库
|
||||
(入库需要内核发起网络请求,涉及超时、鉴权与 SSRF)。
|
||||
|
||||
`source` 标识来源,`channel` 指定目标输出通道。
|
||||
|
||||
### Triple 扩展字段
|
||||
@ -115,6 +140,61 @@ Triple 数据结构新增字段:
|
||||
- `Confidence` — 置信度(0.0~1.0)
|
||||
- `SubjectType` — 主体类型
|
||||
- `ObjectType` — 客体类型
|
||||
- `SentenceText` — 原始句子文本(1.1.0 新增),写入 `sentences` 表;媒体引用挂在句子上
|
||||
- `MediaDigests` — 关联的媒体 digest 列表(1.1.0 新增)
|
||||
|
||||
### 记忆里的媒体(1.1.0 新增)
|
||||
|
||||
媒体在纯文本记忆里以**标记**形式存在,格式 `[<mime> <短digest>] <描述>`:
|
||||
|
||||
```
|
||||
[image/png a1b2c3d4e5f6] 一张紫蓝红三色带图
|
||||
```
|
||||
|
||||
描述文本是持久的语义记忆(检索靠它),digest 是回到字节的钥匙(反查靠它)。
|
||||
标记由内核生成,插件不必自己拼——**填 digest 就够**。
|
||||
|
||||
#### 图记忆
|
||||
|
||||
```go
|
||||
s.Memory().Commit([]sdk.Triple{{
|
||||
Subject: "配色图", Relation: "包含", Object: "三色带",
|
||||
MediaDigests: []string{"a1b2c3d4e5f6"}, // 短 digest 即可,内核补全
|
||||
}})
|
||||
```
|
||||
|
||||
没给 `SentenceText` 时内核会用标记本身充当句子——媒体必须有句子落点,
|
||||
否则引用无从挂起。
|
||||
|
||||
#### 知识库
|
||||
|
||||
```go
|
||||
s.DocMemory().InsertWithMedia(&sdk.Doc{
|
||||
Title: "带图笔记",
|
||||
Content: "正文",
|
||||
}, []sdk.MediaAttachment{
|
||||
{MIME: "image/png", Data: pngBytes, Name: "chart.png"}, // 新内容,落盘去重
|
||||
{Digest: "a1b2c3d4e5f6"}, // 引用已有内容
|
||||
})
|
||||
```
|
||||
|
||||
`Insert` 保持原签名不变,正文里已有的标记同样会被挂成文档级引用。
|
||||
`Query` 返回的 `Doc` 带 `MediaDigests` 与 `Attachments`(mime + 描述,
|
||||
**不含字节**——一次检索可能命中几十份媒体)。删除文档时引用自动释放。
|
||||
|
||||
#### 文本记忆
|
||||
|
||||
```go
|
||||
s.TextMemory().Append(sdk.TextEvent{
|
||||
Role: "user", Content: "看这张图",
|
||||
Attachments: []sdk.MediaAttachment{{MIME: "image/png", Data: pngBytes}},
|
||||
})
|
||||
```
|
||||
|
||||
`RecentEvents` 读回时正文里的标记会被反解成 `Attachments`。
|
||||
|
||||
媒体存储可在内核侧关闭(`core.memory.media.enabled=false`),此时以上接口
|
||||
全部退化为纯文本行为:不报错、不 panic,与本特性上线前一致。
|
||||
|
||||
### ToolDef 字段说明
|
||||
|
||||
@ -142,13 +222,21 @@ func New(name string, sett SettingsAPI, regTool ToolRegistrar, regStage StageReg
|
||||
|
||||
## plugindev 工具链
|
||||
|
||||
`plugindev` 提供插件开发全流程支持。仓库 `bin/` 提供各平台预制二进制(linux/darwin/windows × amd64/arm64),下载后直接加入 PATH 即可:
|
||||
`plugindev` 提供插件开发全流程支持。预编译二进制作为 **release 附件**分发(linux/darwin/windows × amd64/arm64),从
|
||||
[Releases](https://gitcode.com/JianFeeeee/homeagent-sdk/releases) 下载后加入 PATH 即可:
|
||||
|
||||
```bash
|
||||
curl -o plugindev https://gitcode.com/JianFeeeee/homeagent-sdk/-/raw/main/bin/plugindev_linux_amd64
|
||||
# 从 release 附件下载(以 v1.0.0 / linux amd64 为例)
|
||||
curl -Lo plugindev https://gitcode.com/JianFeeeee/homeagent-sdk/releases/download/v1.0.0/plugindev_linux_amd64
|
||||
chmod +x plugindev
|
||||
|
||||
# 或从源码自己编
|
||||
cd tools/plugindev && go build -o plugindev .
|
||||
```
|
||||
|
||||
> 二进制不再随仓库分发(旧的 `bin/` 目录已停用):5 个平台各 26-28MB,
|
||||
> 每次重编都在 git 历史里再叠一份,而它们本质是可从源码复现的产物。
|
||||
|
||||
| 命令 | 说明 |
|
||||
|------|------|
|
||||
| `plugindev init <name> [--lua]` | 初始化插件项目(生成 plg.json、plugin.go 或 main.lua、go.mod、README.md) |
|
||||
@ -180,7 +268,7 @@ chmod +x plugindev
|
||||
"version": "1.0.0",
|
||||
"description": "天气查询插件",
|
||||
"author": "HomeAgent",
|
||||
"entry": "plugin.so",
|
||||
"entry": "plugin.bin",
|
||||
"tags": ["weather", "forecast"],
|
||||
"targets": "linux/amd64,windows/amd64",
|
||||
"outdir": "dist",
|
||||
@ -202,7 +290,7 @@ chmod +x plugindev
|
||||
| `version` | string | 版本号 |
|
||||
| `description` | string | 插件描述 |
|
||||
| `author` | string | 作者 |
|
||||
| `entry` | string | 入口文件(`plugin.so` / `plugin.dll` / `main.lua`) |
|
||||
| `entry` | string | 入口文件(`plugin.bin` / `main.lua`)。v1.0.0 起 Go 插件统一为 `plugin.bin`,不再区分平台后缀 |
|
||||
| `tags` | string[] | 标签 |
|
||||
| `targets` | string | 构建目标,逗号分隔(如 `linux/amd64,windows/amd64`,Lua 插件为 `lua`) |
|
||||
| `outdir` | string | 输出目录(默认 `dist`) |
|
||||
@ -217,11 +305,14 @@ chmod +x plugindev
|
||||
`.hmap` 为 ZIP 归档,包含:
|
||||
|
||||
- `plugin.json` — 插件元数据
|
||||
- `plugin.so` — Go 编译产物(Linux)
|
||||
- `plugin.dll` — Go 编译产物(Windows)
|
||||
- `plugin.dylib` — Go 编译产物(macOS,bundle 模式)
|
||||
- `plugin.bin` — Go 编译产物(单平台构建)
|
||||
- `plugin.bin.<goos>.<goarch>` — 多平台 bundle 模式下每平台一份,
|
||||
安装时 pluginmgr 挑当前平台那份重命名为 `plugin.bin`
|
||||
- `main.lua` — Lua 插件入口(Lua 插件时)
|
||||
|
||||
> v1.0.0 起不再使用 `plugin.so`/`plugin.dll`/`plugin.dylib`——进程边界即 ABI 边界,
|
||||
> 不存在平台特定的动态库区分。旧产物新内核不会加载,会给出明确的重编提示。
|
||||
|
||||
## 插件生命周期
|
||||
|
||||
### 入口函数
|
||||
|
||||
125
README_EN.md
125
README_EN.md
@ -36,6 +36,7 @@ The SDK instance injected via `Start(sdk *PluginSDK)` provides:
|
||||
| Settings | `Settings()` | Access settings API |
|
||||
| Events | `Events()` | Access event subscriber (subscribe-only for external plugins) |
|
||||
| Inject | `InjectText(source, channel, text)` / `InjectInterruptText(source, channel, text)` / `InjectTextNoMemory(source, channel, text)` | Inject text into the agent pipeline |
|
||||
| Media inject | `InjectInputMedia(source, channel, text, blocks)` / `InjectInputMediaSync(...)` / `InjectInterruptMedia(...)` | Inject input carrying images/audio (added in 1.1.0) |
|
||||
| Auto-Restart | `SetAutoRestart(enabled)` / `AutoRestart()` | Control automatic restart on crash |
|
||||
|
||||
### Stage Hooks
|
||||
@ -106,6 +107,32 @@ Type enum values:
|
||||
| `InjectInterruptText(source, channel, text)` | Inject interrupt text, interrupt current processing, route to specified channel |
|
||||
| `InjectTextNoMemory(source, channel, text)` | Inject text without memory recording, route to specified channel |
|
||||
|
||||
### Multimodal Injection (added in 1.1.0)
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `InjectInputMedia(source, channel, text, blocks)` | Inject media-bearing input, asynchronous |
|
||||
| `InjectInputMediaSync(source, channel, text, blocks)` | Inject media-bearing input and wait for the reply text |
|
||||
| `InjectInterruptMedia(source, channel, text, blocks)` | Inject a media-bearing interrupt that can preempt current processing |
|
||||
|
||||
`blocks` is `[]sdk.ContentBlock`, the same type `SetToolBlocks` takes:
|
||||
|
||||
```go
|
||||
s.InjectInputMedia("myplugin", "webui", "take a look at this", []sdk.ContentBlock{{
|
||||
Type: "image_url",
|
||||
ImageURL: &sdk.ImageURL{URL: "data:image/png;base64," + b64, Detail: "auto"},
|
||||
}})
|
||||
```
|
||||
|
||||
How this differs from `SetToolBlocks`: that one is only callable inside a tool handler and
|
||||
its media reaches the model with the *next* tool message. These three let a plugin
|
||||
**initiate a turn that carries media** — the media goes out with this turn's message and is
|
||||
automatically stored in the media store with a memory reference attached.
|
||||
|
||||
`data:` URLs in the blocks are stored and deduplicated by the kernel; `http(s)` URLs are
|
||||
passed to the model only and never stored (storing them would require the kernel to make
|
||||
network requests, bringing timeouts, auth and SSRF into scope).
|
||||
|
||||
`source` identifies the origin, `channel` specifies the target output channel.
|
||||
|
||||
### Triple Extended Fields
|
||||
@ -115,6 +142,65 @@ The Triple data structure includes additional fields:
|
||||
- `Confidence` — confidence score (0.0–1.0)
|
||||
- `SubjectType` — subject type
|
||||
- `ObjectType` — object type
|
||||
- `SentenceText` — the original sentence (added in 1.1.0), written to the `sentences` table; media references hang off the sentence
|
||||
- `MediaDigests` — associated media digests (added in 1.1.0)
|
||||
|
||||
### Media in Memory (added in 1.1.0)
|
||||
|
||||
Inside plain-text memory, media is represented as a **marker** of the form
|
||||
`[<mime> <short digest>] <description>`:
|
||||
|
||||
```
|
||||
[image/png a1b2c3d4e5f6] a purple-blue-red three-band chart
|
||||
```
|
||||
|
||||
The description is the durable semantic memory (retrieval uses it); the digest is the key
|
||||
back to the bytes (reverse lookup uses it). Markers are generated by the kernel — a plugin
|
||||
never has to assemble one, it just **supplies the digest**.
|
||||
|
||||
#### Graph memory
|
||||
|
||||
```go
|
||||
s.Memory().Commit([]sdk.Triple{{
|
||||
Subject: "palette", Relation: "contains", Object: "three-band",
|
||||
MediaDigests: []string{"a1b2c3d4e5f6"}, // short digest is fine, the kernel resolves it
|
||||
}})
|
||||
```
|
||||
|
||||
With no `SentenceText`, the kernel uses the marker itself as the sentence — media must have
|
||||
a sentence to hang off, otherwise the reference has nowhere to attach.
|
||||
|
||||
#### Knowledge base
|
||||
|
||||
```go
|
||||
s.DocMemory().InsertWithMedia(&sdk.Doc{
|
||||
Title: "illustrated note",
|
||||
Content: "body",
|
||||
}, []sdk.MediaAttachment{
|
||||
{MIME: "image/png", Data: pngBytes, Name: "chart.png"}, // new content, stored and deduped
|
||||
{Digest: "a1b2c3d4e5f6"}, // reference existing content
|
||||
})
|
||||
```
|
||||
|
||||
`Insert` keeps its original signature; markers already present in the body are bound as
|
||||
document-level references too. `Query` fills `MediaDigests` and `Attachments` (mime plus
|
||||
description, **no bytes** — one query can match dozens of media items). Removing a document
|
||||
releases its references.
|
||||
|
||||
#### Text memory
|
||||
|
||||
```go
|
||||
s.TextMemory().Append(sdk.TextEvent{
|
||||
Role: "user", Content: "look at this",
|
||||
Attachments: []sdk.MediaAttachment{{MIME: "image/png", Data: pngBytes}},
|
||||
})
|
||||
```
|
||||
|
||||
`RecentEvents` decodes markers in the body back into `Attachments`.
|
||||
|
||||
The media store can be disabled kernel-side (`core.memory.media.enabled=false`); all of the
|
||||
above then degrades to plain-text behaviour — no errors, no panics, identical to how it
|
||||
behaved before this feature shipped.
|
||||
|
||||
### ToolDef Field Reference
|
||||
|
||||
@ -142,14 +228,30 @@ Plugin developers only need to implement the `Plugin` interface and export a `Ne
|
||||
|
||||
## plugindev Toolchain
|
||||
|
||||
`plugindev` provides full development workflow support:
|
||||
`plugindev` provides full development workflow support. Prebuilt binaries ship as **release assets**
|
||||
(linux/darwin/windows × amd64/arm64); download from
|
||||
[Releases](https://gitcode.com/JianFeeeee/homeagent-sdk/releases) and put it on your PATH:
|
||||
|
||||
```bash
|
||||
# From release assets (v1.0.0 / linux amd64 shown)
|
||||
curl -Lo plugindev https://gitcode.com/JianFeeeee/homeagent-sdk/releases/download/v1.0.0/plugindev_linux_amd64
|
||||
chmod +x plugindev
|
||||
|
||||
# Or build from source
|
||||
cd tools/plugindev && go build -o plugindev .
|
||||
```
|
||||
|
||||
> Binaries no longer ship inside the repository (the old `bin/` directory is retired): five
|
||||
> platforms at 26-28MB each piled another copy into git history on every rebuild, and they are
|
||||
> reproducible from source anyway.
|
||||
|
||||
| Command | Description |
|
||||
|---------|-------------|
|
||||
| `plugindev init` | Initialize plugin project (generates plg.json, entry template) |
|
||||
| `plugindev build` | Build plugin, output .hmap package |
|
||||
| `plugindev clean` | Clean build artifacts |
|
||||
| `plugindev debug` | Run plugin in local debug mode |
|
||||
| `plugindev init <name> [--lua]` | Initialize plugin project (generates plg.json, plugin.go or main.lua, go.mod, README.md) |
|
||||
| `plugindev build [flags]` | Build and package into a `.hmap` (supports cross-compilation and bundle mode) |
|
||||
| `plugindev clean` | Clean `build/` and `dist/` plus generated files |
|
||||
| `plugindev debug [dir]` | Load plugin source through the Yaegi Go interpreter and start an interactive REPL |
|
||||
| `plugindev sdk <command>` | SDK version management (list/install/use/path/current/latest) |
|
||||
|
||||
Supports both **Go** and **Lua** plugin languages.
|
||||
|
||||
@ -163,7 +265,7 @@ Supports both **Go** and **Lua** plugin languages.
|
||||
"version": "1.0.0",
|
||||
"description": "Weather plugin",
|
||||
"author": "HomeAgent",
|
||||
"entry": "plugin.so",
|
||||
"entry": "plugin.bin",
|
||||
"tags": ["weather", "forecast"],
|
||||
"targets": "linux/amd64,windows/amd64",
|
||||
"outdir": "dist",
|
||||
@ -185,7 +287,7 @@ Supports both **Go** and **Lua** plugin languages.
|
||||
| `version` | string | Version |
|
||||
| `description` | string | Plugin description |
|
||||
| `author` | string | Author |
|
||||
| `entry` | string | Entry file (`plugin.so` / `main.lua`) |
|
||||
| `entry` | string | Entry file (`plugin.bin` / `main.lua`). Since v1.0.0 Go plugins uniformly build to `plugin.bin`—no per-platform suffix |
|
||||
| `tags` | string[] | Tags |
|
||||
| `targets` | string | Build targets, comma-separated (e.g. `linux/amd64,windows/amd64`) |
|
||||
| `outdir` | string | Output directory (default `dist`) |
|
||||
@ -198,10 +300,15 @@ Supports both **Go** and **Lua** plugin languages.
|
||||
`.hmap` is a ZIP archive containing:
|
||||
|
||||
- `plugin.json` — plugin metadata
|
||||
- `plugin.so` — Go compiled artifact (Linux)
|
||||
- `plugin.dll` — Go compiled artifact (Windows)
|
||||
- `plugin.bin` — Go compiled artifact (single-platform build)
|
||||
- `plugin.bin.<goos>.<goarch>` — one per platform in bundle mode; on install pluginmgr picks
|
||||
the one matching the current platform and renames it to `plugin.bin`
|
||||
- `main.lua` — Lua plugin entry (for Lua plugins)
|
||||
|
||||
> Since v1.0.0 `plugin.so`/`plugin.dll`/`plugin.dylib` are no longer used—the process boundary
|
||||
> *is* the ABI boundary, so there is no platform-specific shared-library distinction. The new
|
||||
> kernel will not load old artifacts; it emits an explicit rebuild hint instead.
|
||||
|
||||
## Plugin Lifecycle
|
||||
|
||||
### Start & Stop
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -2,14 +2,18 @@
|
||||
"name": "a2a",
|
||||
"name_zh": "A2A 代理通信",
|
||||
"name_en": "A2A Agent Communication",
|
||||
"version": "1.0.0",
|
||||
"version": "1.3.0",
|
||||
"description": "Agent-to-Agent 协议通信插件,支持双向 A2A 通信:可查询其他 Agent 并回复其请求。提供 HTTP 服务端暴露本 Agent 能力。",
|
||||
"author": "HomeAgent",
|
||||
"entry": "plugin.so",
|
||||
"tags": ["a2a", "agent", "interop"],
|
||||
"tags": [
|
||||
"a2a",
|
||||
"agent",
|
||||
"interop"
|
||||
],
|
||||
"targets": "linux/amd64",
|
||||
"outdir": "dist",
|
||||
"bundle": true,
|
||||
"replaces": {},
|
||||
"source_dirs": []
|
||||
}
|
||||
}
|
||||
@ -21,15 +21,47 @@ type Plugin struct {
|
||||
srvMu sync.Mutex
|
||||
server *http.Server
|
||||
serverAddr string
|
||||
|
||||
// 会话表:session_id → 上下文前缀。A2A 无状态协议下由插件侧维护
|
||||
// 多轮上下文:同 session 的后续请求会把之前的对话拼进注入文本。
|
||||
sessMu sync.Mutex
|
||||
sessions map[string]*a2aSession
|
||||
}
|
||||
|
||||
// a2aSession 记录一个会话的轮次历史,用于延续上下文。
|
||||
type a2aSession struct {
|
||||
ID string
|
||||
History []string // 轮次文本 [user1, agent1, user2, agent2, ...]
|
||||
LastUsed time.Time
|
||||
}
|
||||
|
||||
// maxSessionTurns 单会话保留的最大轮次对数(防上下文无限膨胀)。
|
||||
const maxSessionTurns = 10
|
||||
|
||||
// sessionGCPeriod 会话过期清理周期;超过 2 小时未用的会话回收。
|
||||
const sessionGCPeriod = 30 * time.Minute
|
||||
|
||||
func (p *Plugin) Name() string { return p.name }
|
||||
|
||||
func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
s.SetAutoRestart(true)
|
||||
p.sdk = s
|
||||
p.sessions = make(map[string]*a2aSession)
|
||||
tp := p.name + "_"
|
||||
|
||||
// 注册自身为输出通道:agent 回复 emit 到本通道时有落点,
|
||||
// 且 output_list_channels 可见(agent 能主动向 a2a 会话推送消息)。
|
||||
if err := s.RegisterOutputChannel(p.name, 1, "A2A Agent 互联通道(外部 agent 查询的回复由此返回)", sdk.ChannelDef{}, func(args map[string]interface{}) (interface{}, error) {
|
||||
payload, _ := args["payload"].(string)
|
||||
log.Printf("[%s] channel output: %s", p.name, truncateRunes(payload, 120))
|
||||
return map[string]interface{}{"status": "ok"}, nil
|
||||
}); err != nil {
|
||||
log.Printf("[%s] register output channel: %v", p.name, err)
|
||||
}
|
||||
|
||||
// 会话 GC:后台周期回收长期不用的会话
|
||||
go p.sessionGCLoop()
|
||||
|
||||
s.Settings().RegisterDef(sdk.ConfigDef{
|
||||
Key: "listen", Default: "127.0.0.1:12000",
|
||||
Type: "string", DisplayName: "监听地址",
|
||||
@ -45,6 +77,7 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
"properties": map[string]interface{}{
|
||||
"agent_url": map[string]interface{}{"type": "string", "description": "目标 Agent 的 A2A 端点 URL"},
|
||||
"query": map[string]interface{}{"type": "string", "description": "发送给目标 Agent 的文本查询"},
|
||||
"session_id": map[string]interface{}{"type": "string", "description": "可选。上次调用返回的 session_id,传入可延续与该 agent 的多轮对话上下文"},
|
||||
"timeout": map[string]interface{}{"type": "integer", "description": "超时时间(秒),默认 60"},
|
||||
},
|
||||
"required": []string{"agent_url", "query"},
|
||||
@ -114,6 +147,66 @@ func (p *Plugin) Stop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// sessionGCLoop 周期清理超时会话。
|
||||
func (p *Plugin) sessionGCLoop() {
|
||||
ticker := time.NewTicker(sessionGCPeriod)
|
||||
defer ticker.Stop()
|
||||
for range ticker.C {
|
||||
p.sessMu.Lock()
|
||||
for id, sess := range p.sessions {
|
||||
if time.Since(sess.LastUsed) > 2*time.Hour {
|
||||
delete(p.sessions, id)
|
||||
}
|
||||
}
|
||||
p.sessMu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func truncateRunes(s string, n int) string {
|
||||
r := []rune(s)
|
||||
if len(r) <= n {
|
||||
return s
|
||||
}
|
||||
return string(r[:n]) + "..."
|
||||
}
|
||||
|
||||
// sessionMessages 返回指定会话的近 limit 条消息(时间正序),
|
||||
// 会话不存在返回 nil。消息格式 [{role, text, ts}]。
|
||||
func (p *Plugin) sessionMessages(sessionID string, limit int) []map[string]interface{} {
|
||||
p.sessMu.Lock()
|
||||
sess := p.sessions[sessionID]
|
||||
var hist []string
|
||||
var lastUsed time.Time
|
||||
if sess != nil {
|
||||
hist = append([]string{}, sess.History...)
|
||||
lastUsed = sess.LastUsed
|
||||
}
|
||||
p.sessMu.Unlock()
|
||||
if sess == nil {
|
||||
return nil
|
||||
}
|
||||
_ = lastUsed
|
||||
// History 交替 [user, agent, user, agent...],取末尾 limit 条,保持时间正序
|
||||
start := 0
|
||||
if len(hist) > limit {
|
||||
start = len(hist) - limit
|
||||
}
|
||||
msgs := make([]map[string]interface{}, 0, len(hist)-start)
|
||||
for i := start; i < len(hist); i++ {
|
||||
role, text := "user", hist[i]
|
||||
if after, ok := strings.CutPrefix(text, "用户: "); ok {
|
||||
role, text = "user", after
|
||||
} else if after, ok := strings.CutPrefix(text, "助手: "); ok {
|
||||
role, text = "agent", after
|
||||
}
|
||||
msgs = append(msgs, map[string]interface{}{
|
||||
"role": role,
|
||||
"text": text,
|
||||
})
|
||||
}
|
||||
return msgs
|
||||
}
|
||||
|
||||
func (p *Plugin) stopServer() {
|
||||
p.srvMu.Lock()
|
||||
defer p.srvMu.Unlock()
|
||||
@ -137,7 +230,12 @@ func (p *Plugin) startServer(addr string) error {
|
||||
return fmt.Errorf("listen %s: %v", addr, err)
|
||||
}
|
||||
|
||||
srv := &http.Server{Handler: mux}
|
||||
srv := &http.Server{
|
||||
Handler: mux,
|
||||
ReadTimeout: 30 * time.Second,
|
||||
WriteTimeout: 120 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
addrStr := listener.Addr().String()
|
||||
|
||||
p.srvMu.Lock()
|
||||
@ -186,7 +284,9 @@ func (p *Plugin) handleIncomingA2A(w http.ResponseWriter, r *http.Request) {
|
||||
ID string `json:"id"`
|
||||
Method string `json:"method"`
|
||||
Params struct {
|
||||
Query string `json:"query,omitempty"`
|
||||
Query string `json:"query,omitempty"`
|
||||
SessionID string `json:"session_id,omitempty"`
|
||||
Limit int `json:"limit,omitempty"`
|
||||
Message *struct {
|
||||
Role string `json:"role"`
|
||||
Parts []struct {
|
||||
@ -210,29 +310,96 @@ func (p *Plugin) handleIncomingA2A(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
queryText = strings.TrimSpace(queryText)
|
||||
}
|
||||
|
||||
// Inject into agent pipeline via interrupt (preempt current processing) or direct input
|
||||
if queryText != "" {
|
||||
p.sdk.InjectInterruptText("a2a", "webui", fmt.Sprintf("[来自A2A Agent的查询]\n%s", queryText))
|
||||
if queryText == "" {
|
||||
http.Error(w, "query/message.text required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Respond with task accepted
|
||||
// 会话:调用方可指定 session_id 延续多轮上下文;不指定则新建。
|
||||
sessionID := strings.TrimSpace(req.Params.SessionID)
|
||||
injectText := queryText
|
||||
p.sessMu.Lock()
|
||||
if sessionID != "" {
|
||||
sess := p.sessions[sessionID]
|
||||
if sess == nil {
|
||||
sess = &a2aSession{ID: sessionID, LastUsed: time.Now()}
|
||||
p.sessions[sessionID] = sess
|
||||
}
|
||||
sess.LastUsed = time.Now()
|
||||
// 有历史则把上下文拼在前面(截尾防爆量)
|
||||
if len(sess.History) > 0 {
|
||||
ctxText := strings.Join(sess.History, "\n")
|
||||
injectText = "[对话上下文]\n" + ctxText + "\n[本轮输入]\n" + queryText
|
||||
}
|
||||
} else {
|
||||
sessionID = fmt.Sprintf("a2a_%d", time.Now().UnixNano())
|
||||
p.sessions[sessionID] = &a2aSession{ID: sessionID, LastUsed: time.Now()}
|
||||
}
|
||||
p.sessMu.Unlock()
|
||||
|
||||
// 同步注入:阻塞等待 agent 处理完成拿回复(不再抢占打断、
|
||||
// 也不再回 202 让请求方永远等不到结果)。HTTP 超时由调用方控制。
|
||||
reply := p.sdk.InjectInputSync(p.name, p.name,
|
||||
fmt.Sprintf("[来自A2A Agent的查询 session=%s]\n%s\n[注意] 请直接以文本回复本查询,不要调用 output_send__%s——你的最终文本回复会被系统自动返回给请求方。", sessionID, injectText, p.name))
|
||||
|
||||
// 回复写回会话历史(下一轮作为上下文)
|
||||
p.sessMu.Lock()
|
||||
if sess := p.sessions[sessionID]; sess != nil {
|
||||
sess.History = append(sess.History, "用户: "+queryText, "助手: "+reply)
|
||||
if len(sess.History) > maxSessionTurns*2 {
|
||||
sess.History = sess.History[len(sess.History)-maxSessionTurns*2 :]
|
||||
}
|
||||
sess.LastUsed = time.Now()
|
||||
}
|
||||
p.sessMu.Unlock()
|
||||
|
||||
resp := map[string]interface{}{
|
||||
"jsonrpc": "2.0",
|
||||
"id": req.ID,
|
||||
"result": map[string]interface{}{
|
||||
"id": fmt.Sprintf("task_%d", time.Now().UnixNano()),
|
||||
"status": "submitted",
|
||||
"status": "completed",
|
||||
"session_id": sessionID,
|
||||
"message": map[string]interface{}{
|
||||
"role": "agent",
|
||||
"parts": []map[string]string{{"type": "text", "text": reply}},
|
||||
},
|
||||
},
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(resp)
|
||||
|
||||
case "tasks.get":
|
||||
case "tasks.get", "session.get":
|
||||
// 按 session_id 返回会话内近 N 条消息(默认 10 条)。
|
||||
sessionID := strings.TrimSpace(req.Params.SessionID)
|
||||
if sessionID == "" {
|
||||
sessionID = strings.TrimSpace(req.Params.Query)
|
||||
}
|
||||
limit := 10
|
||||
if req.Params.Limit > 0 && req.Params.Limit <= 100 {
|
||||
limit = req.Params.Limit
|
||||
}
|
||||
msgs := p.sessionMessages(sessionID, limit)
|
||||
if msgs == nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"jsonrpc": "2.0", "id": req.ID,
|
||||
"result": map[string]interface{}{
|
||||
"session_id": sessionID,
|
||||
"status": "not_found",
|
||||
"messages": []interface{}{},
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"jsonrpc": "2.0", "id": req.ID,
|
||||
"result": map[string]interface{}{"id": req.Params.Query, "status": "unknown"},
|
||||
"result": map[string]interface{}{
|
||||
"session_id": sessionID,
|
||||
"status": "completed",
|
||||
"messages": msgs,
|
||||
},
|
||||
})
|
||||
|
||||
default:
|
||||
@ -276,9 +443,10 @@ type A2ARequest struct {
|
||||
}
|
||||
|
||||
type A2AParams struct {
|
||||
Query string `json:"query,omitempty"`
|
||||
Message *A2AMessage `json:"message,omitempty"`
|
||||
TaskID string `json:"id,omitempty"`
|
||||
Query string `json:"query,omitempty"`
|
||||
SessionID string `json:"session_id,omitempty"`
|
||||
Message *A2AMessage `json:"message,omitempty"`
|
||||
TaskID string `json:"id,omitempty"`
|
||||
}
|
||||
|
||||
type A2AResponse struct {
|
||||
@ -291,6 +459,7 @@ type A2AResponse struct {
|
||||
type A2AResult struct {
|
||||
TaskID string `json:"id,omitempty"`
|
||||
Status string `json:"status,omitempty"`
|
||||
SessionID string `json:"session_id,omitempty"`
|
||||
Message *A2AMessage `json:"message,omitempty"`
|
||||
AgentCard *A2AAgentCard `json:"agent_card,omitempty"`
|
||||
}
|
||||
@ -356,6 +525,7 @@ func (p *Plugin) handleA2ADiscover(args map[string]interface{}) (interface{}, er
|
||||
func (p *Plugin) handleA2AQuery(args map[string]interface{}) (interface{}, error) {
|
||||
agentURL, _ := args["agent_url"].(string)
|
||||
query, _ := args["query"].(string)
|
||||
sessionID, _ := args["session_id"].(string) // 可选:延续对方会话
|
||||
timeoutSec := 60
|
||||
if v, ok := args["timeout"].(float64); ok && v > 0 {
|
||||
timeoutSec = int(v)
|
||||
@ -377,7 +547,8 @@ func (p *Plugin) handleA2AQuery(args map[string]interface{}) (interface{}, error
|
||||
ID: fmt.Sprintf("a2a_%d", time.Now().UnixNano()),
|
||||
Method: "tasks.send",
|
||||
Params: A2AParams{
|
||||
Message: &A2AMessage{Role: "user", Parts: []A2APart{{Text: query, Type: "text"}}},
|
||||
SessionID: sessionID,
|
||||
Message: &A2AMessage{Role: "user", Parts: []A2APart{{Text: query, Type: "text"}}},
|
||||
},
|
||||
}
|
||||
|
||||
@ -416,10 +587,18 @@ func (p *Plugin) handleA2AQuery(args map[string]interface{}) (interface{}, error
|
||||
replyText = strings.TrimSpace(replyText)
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
result := map[string]interface{}{
|
||||
"task_id": a2aResp.Result.TaskID, "status": a2aResp.Result.Status,
|
||||
"response": replyText,
|
||||
}, nil
|
||||
}
|
||||
if a2aResp.Result.SessionID != "" || sessionID != "" {
|
||||
result["session_id"] = a2aResp.Result.SessionID
|
||||
if result["session_id"] == "" {
|
||||
result["session_id"] = sessionID
|
||||
}
|
||||
result["note"] = "延续会话:下次调用传此 session_id 可保持上下文"
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ---- Management Handlers ----
|
||||
|
||||
@ -2,11 +2,15 @@
|
||||
"name": "acp",
|
||||
"name_zh": "ACP 代理通信",
|
||||
"name_en": "ACP Agent Client Protocol",
|
||||
"version": "1.0.0",
|
||||
"version": "1.2.0",
|
||||
"description": "Agent Client Protocol 通信插件:充当 ACP 服务端接受其他 Agent 的任务请求,同时提供客户端工具向远程 ACP Agent(如 opencode)发起会话并读取回复",
|
||||
"author": "HomeAgent",
|
||||
"entry": "plugin.so",
|
||||
"tags": ["acp", "agent", "interop"],
|
||||
"tags": [
|
||||
"acp",
|
||||
"agent",
|
||||
"interop"
|
||||
],
|
||||
"targets": "linux/amd64",
|
||||
"outdir": "dist",
|
||||
"bundle": true,
|
||||
|
||||
@ -34,8 +34,13 @@ type Plugin struct {
|
||||
type sessionState struct {
|
||||
ID string
|
||||
Replying []map[string]interface{}
|
||||
History []string // 轮次历史 [user, agent, user, agent...],延续上下文用
|
||||
LastUsed time.Time
|
||||
}
|
||||
|
||||
// maxSessionTurns 单会话保留的最大轮次对数。
|
||||
const maxSessionTurns = 10
|
||||
|
||||
func (p *Plugin) Name() string { return p.name }
|
||||
|
||||
func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
@ -44,6 +49,14 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
p.sessions = make(map[string]*sessionState)
|
||||
tp := p.name + "_"
|
||||
|
||||
// 注册自身为输出通道:agent 回复 emit 到本通道时有落点。
|
||||
// (回复主要走同步注入返回,此通道用于 agent 主动 output_send__acp)
|
||||
s.RegisterOutputChannel(p.name, 1, "ACP Agent 互联通道(外部 agent 会话的回复由此返回)", sdk.ChannelDef{}, func(args map[string]interface{}) (interface{}, error) {
|
||||
payload, _ := args["payload"].(string)
|
||||
log.Printf("[%s] channel output: %s", p.name, truncateStr(payload, 120))
|
||||
return map[string]interface{}{"status": "ok"}, nil
|
||||
})
|
||||
|
||||
s.Settings().RegisterDef(sdk.ConfigDef{
|
||||
Key: "listen", Default: "127.0.0.1:12001",
|
||||
Type: "string", DisplayName: "监听地址",
|
||||
@ -58,6 +71,7 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
"properties": map[string]interface{}{
|
||||
"server_url": map[string]interface{}{"type": "string", "description": "目标 ACP 服务端地址(如 http://127.0.0.1:13000)"},
|
||||
"prompt": map[string]interface{}{"type": "string", "description": "发送给目标 Agent 的任务描述"},
|
||||
"session_id": map[string]interface{}{"type": "string", "description": "可选。上次调用返回的 session_id,传入可延续与该 agent 的多轮对话上下文"},
|
||||
"timeout": map[string]interface{}{"type": "integer", "description": "等待回复超时(秒),默认 120"},
|
||||
},
|
||||
"required": []string{"server_url", "prompt"},
|
||||
@ -171,6 +185,7 @@ func (p *Plugin) handleSessionPost(w http.ResponseWriter, r *http.Request) {
|
||||
Text string `json:"text"`
|
||||
} `json:"request,omitempty"`
|
||||
SessionID string `json:"session_id,omitempty"`
|
||||
Limit int `json:"limit,omitempty"`
|
||||
Final bool `json:"final,omitempty"`
|
||||
} `json:"params,omitempty"`
|
||||
}
|
||||
@ -190,21 +205,109 @@ func (p *Plugin) handleSessionPost(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
sid := fmt.Sprintf("session_%d", time.Now().UnixNano())
|
||||
// 会话:调用方可指定 session_id 延续多轮;不指定则新建。
|
||||
sid := strings.TrimSpace(req.Params.SessionID)
|
||||
p.mu.Lock()
|
||||
p.sessions[sid] = &sessionState{ID: sid}
|
||||
if sid != "" {
|
||||
if _, exists := p.sessions[sid]; !exists {
|
||||
p.sessions[sid] = &sessionState{ID: sid, LastUsed: time.Now()}
|
||||
}
|
||||
} else {
|
||||
sid = fmt.Sprintf("session_%d", time.Now().UnixNano())
|
||||
p.sessions[sid] = &sessionState{ID: sid, LastUsed: time.Now()}
|
||||
}
|
||||
st := p.sessions[sid]
|
||||
p.mu.Unlock()
|
||||
|
||||
if p.sdk != nil {
|
||||
p.sdk.InjectInterruptText(p.name, "acp",
|
||||
fmt.Sprintf("[来自ACP Agent的请求请求 session %s]\n%s", sid, text))
|
||||
// 延续上下文
|
||||
injectText := text
|
||||
p.mu.Lock()
|
||||
if len(st.History) > 0 {
|
||||
ctxText := strings.Join(st.History, "\n")
|
||||
injectText = "[对话上下文]\n" + ctxText + "\n[本轮输入]\n" + text
|
||||
}
|
||||
p.mu.Unlock()
|
||||
|
||||
// 同步注入等待回复:不抢占打断,完整闭环返回文本。
|
||||
reply := ""
|
||||
if p.sdk != nil {
|
||||
reply = p.sdk.InjectInputSync(p.name, p.name,
|
||||
fmt.Sprintf("[来自ACP Agent的请求 session %s]\n%s\n[注意] 请直接以文本回复本请求,不要调用 output_send__%s——你的最终文本回复会被系统自动返回给请求方。", sid, injectText, p.name))
|
||||
}
|
||||
|
||||
// 写回历史 + 填充 Replying 供 SSE 消费
|
||||
p.mu.Lock()
|
||||
st.History = append(st.History, "用户: "+text, "助手: "+reply)
|
||||
if len(st.History) > maxSessionTurns*2 {
|
||||
st.History = st.History[len(st.History)-maxSessionTurns*2:]
|
||||
}
|
||||
st.LastUsed = time.Now()
|
||||
if reply != "" {
|
||||
st.Replying = append(st.Replying, map[string]interface{}{
|
||||
"type": "reply", "text": reply,
|
||||
})
|
||||
}
|
||||
p.mu.Unlock()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"jsonrpc": "2.0", "id": req.ID,
|
||||
"result": map[string]interface{}{
|
||||
"session": map[string]interface{}{"id": sid},
|
||||
"reply": reply,
|
||||
},
|
||||
})
|
||||
|
||||
case "session/get":
|
||||
// 按 session_id 返回会话内近 N 条消息(默认 10 条,时间正序)
|
||||
sid := req.Params.SessionID
|
||||
p.mu.RLock()
|
||||
st := p.sessions[sid]
|
||||
var hist []string
|
||||
if st != nil {
|
||||
hist = append([]string{}, st.History...)
|
||||
}
|
||||
p.mu.RUnlock()
|
||||
if st == nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"jsonrpc": "2.0", "id": req.ID,
|
||||
"result": map[string]interface{}{
|
||||
"session_id": sid,
|
||||
"status": "not_found",
|
||||
"messages": []interface{}{},
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
limit := 10
|
||||
if req.Params.Limit > 0 && req.Params.Limit <= 100 {
|
||||
limit = req.Params.Limit
|
||||
}
|
||||
start := 0
|
||||
if len(hist) > limit {
|
||||
start = len(hist) - limit
|
||||
}
|
||||
msgs := make([]map[string]interface{}, 0, len(hist)-start)
|
||||
for i := start; i < len(hist); i++ {
|
||||
role, text := "user", hist[i]
|
||||
if after, ok := strings.CutPrefix(text, "用户: "); ok {
|
||||
role, text = "user", after
|
||||
} else if after, ok := strings.CutPrefix(text, "助手: "); ok {
|
||||
role, text = "agent", after
|
||||
}
|
||||
msgs = append(msgs, map[string]interface{}{
|
||||
"role": role,
|
||||
"text": text,
|
||||
})
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"jsonrpc": "2.0", "id": req.ID,
|
||||
"result": map[string]interface{}{
|
||||
"session_id": sid,
|
||||
"status": "completed",
|
||||
"messages": msgs,
|
||||
},
|
||||
})
|
||||
|
||||
@ -212,12 +315,17 @@ func (p *Plugin) handleSessionPost(w http.ResponseWriter, r *http.Request) {
|
||||
sid := req.Params.SessionID
|
||||
p.mu.Lock()
|
||||
st := p.sessions[sid]
|
||||
if st != nil && req.Params.Final {
|
||||
st.Replying = append(st.Replying, map[string]interface{}{
|
||||
"type": "reply", "text": "done",
|
||||
})
|
||||
}
|
||||
p.mu.Unlock()
|
||||
if st == nil {
|
||||
http.Error(w, "session not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if req.Params.Final {
|
||||
// 客户端结束会话:标记并保留历史(后续可再 session/new 续)
|
||||
p.mu.Lock()
|
||||
st.LastUsed = time.Now()
|
||||
p.mu.Unlock()
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
@ -338,6 +446,7 @@ func (p *Plugin) handleAcpQuery(args map[string]interface{}) (interface{}, error
|
||||
if prompt == "" {
|
||||
return map[string]interface{}{"error": "prompt 不能为空"}, nil
|
||||
}
|
||||
sessionID, _ := args["session_id"].(string) // 可选:延续对方会话
|
||||
timeoutSec := 120
|
||||
if v, ok := args["timeout"].(float64); ok && v > 0 {
|
||||
timeoutSec = int(v)
|
||||
@ -346,12 +455,16 @@ func (p *Plugin) handleAcpQuery(args map[string]interface{}) (interface{}, error
|
||||
endpoint := serverURL + "/api/session"
|
||||
client := &http.Client{Timeout: time.Duration(timeoutSec) * time.Second}
|
||||
|
||||
params := map[string]interface{}{
|
||||
"request": map[string]interface{}{"text": prompt},
|
||||
}
|
||||
if sessionID != "" {
|
||||
params["session_id"] = sessionID
|
||||
}
|
||||
newBody, _ := json.Marshal(map[string]interface{}{
|
||||
"jsonrpc": "2.0", "id": "acp-" + fmt.Sprintf("%d", time.Now().UnixNano()),
|
||||
"method": "session/new",
|
||||
"params": map[string]interface{}{
|
||||
"request": map[string]interface{}{"text": prompt},
|
||||
},
|
||||
"params": params,
|
||||
})
|
||||
|
||||
req, _ := http.NewRequest("POST", endpoint, bytes.NewReader(newBody))
|
||||
@ -416,6 +529,7 @@ func (p *Plugin) handleAcpQuery(args map[string]interface{}) (interface{}, error
|
||||
"session_id": sid,
|
||||
"status": "completed",
|
||||
"reply": replyText,
|
||||
"note": "延续会话:下次调用传此 session_id 可保持上下文",
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@ -2,14 +2,19 @@
|
||||
"name": "ai_image",
|
||||
"name_zh": "AI绘图",
|
||||
"name_en": "AI Image",
|
||||
"version": "1.0.0",
|
||||
"version": "1.3.0",
|
||||
"description": "AI 图像生成插件,支持 OpenAI DALL·E / Stable Diffusion",
|
||||
"author": "HomeAgent",
|
||||
"entry": "plugin.so",
|
||||
"tags": ["ai", "image", "draw", "generate"],
|
||||
"tags": [
|
||||
"ai",
|
||||
"image",
|
||||
"draw",
|
||||
"generate"
|
||||
],
|
||||
"targets": "linux/amd64",
|
||||
"outdir": "dist",
|
||||
"bundle": true,
|
||||
"replaces": {},
|
||||
"source_dirs": []
|
||||
}
|
||||
}
|
||||
@ -5,7 +5,10 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@ -21,6 +24,8 @@ type Plugin struct {
|
||||
provider string
|
||||
model string
|
||||
size string
|
||||
baseURL string
|
||||
dataDir string // <data>/ai_images:生成本地图片存放目录
|
||||
}
|
||||
|
||||
func NewPluginFactory(name string, config map[string]interface{}) (sdk.Plugin, error) {
|
||||
@ -111,10 +116,15 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
DisplayName: "API Key", Description: "OpenAI / Stable Diffusion API Key",
|
||||
Category: "ai_image", Secret: true,
|
||||
})
|
||||
s.Settings().RegisterDef(sdk.ConfigDef{
|
||||
Key: "base_url", Default: "", Type: "string",
|
||||
DisplayName: "Base URL", Description: "自定义 OpenAI 兼容网关地址(不带 /v1 尾缀,如 http://127.0.0.1:8081);为空走官方 https://api.openai.com",
|
||||
Category: "ai_image",
|
||||
})
|
||||
s.Settings().RegisterDef(sdk.ConfigDef{
|
||||
Key: "provider", Default: "openai", Type: "string",
|
||||
DisplayName: "Provider", Description: "Image generation provider: openai / stability",
|
||||
Category: "ai_image",
|
||||
Category: "ai_image",
|
||||
})
|
||||
s.Settings().RegisterDef(sdk.ConfigDef{
|
||||
Key: "model", Default: "dall-e-3", Type: "string",
|
||||
@ -131,10 +141,23 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
p.provider = getSetting(s.Settings(), "provider", "openai")
|
||||
p.model = getSetting(s.Settings(), "model", "dall-e-3")
|
||||
p.size = getSetting(s.Settings(), "size", "1024x1024")
|
||||
p.baseURL = strings.TrimRight(strings.TrimSpace(getSetting(s.Settings(), "base_url", "")), "/")
|
||||
|
||||
// 生图本地存放目录:插件专属数据目录(SDK DataDir API,内核保证存在)。
|
||||
if p.sdk != nil {
|
||||
if dd := s.Settings().DataDir(); dd != "" {
|
||||
p.dataDir = dd
|
||||
}
|
||||
}
|
||||
if p.dataDir == "" {
|
||||
// 旧版内核无 DataDir API 时退到 /tmp
|
||||
p.dataDir = filepath.Join(os.TempDir(), "homeagent_ai_images")
|
||||
}
|
||||
os.MkdirAll(p.dataDir, 0755)
|
||||
|
||||
tp := p.name + "_"
|
||||
s.RegisterTool(tp+"generate", sdk.ToolDef{
|
||||
Name: tp + "generate", Description: "Generate image from text prompt using AI. Returns image URL.",
|
||||
Name: tp + "generate", Description: "Generate image from text prompt using AI. Downloads the result locally and returns a local file path (permanent, no expiry). To show the user, send it via output_send with type=image and payload=the returned path.",
|
||||
Parameters: map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
@ -209,6 +232,14 @@ func (p *Plugin) handleGenerate(args map[string]interface{}) (interface{}, error
|
||||
}
|
||||
|
||||
func (p *Plugin) generateOpenAI(prompt, model, size string, n int, apiKey string) (interface{}, error) {
|
||||
// 上游地址:base_url 非空时走自定义网关(如本机 llmsproxy),约定不带 /v1 尾缀;
|
||||
// 为空保持官方直连。兼容误配了 /v1 尾缀的情况(去重)。
|
||||
endpoint := "https://api.openai.com/v1/images/generations"
|
||||
if p.baseURL != "" {
|
||||
base := strings.TrimSuffix(p.baseURL, "/v1")
|
||||
endpoint = base + "/v1/images/generations"
|
||||
}
|
||||
|
||||
body := openAIReq{
|
||||
Model: model,
|
||||
Prompt: prompt,
|
||||
@ -217,8 +248,9 @@ func (p *Plugin) generateOpenAI(prompt, model, size string, n int, apiKey string
|
||||
ResponseFormat: "url",
|
||||
}
|
||||
|
||||
log.Printf("[ai_image] endpoint=%s baseURL=%q model=%q", endpoint, p.baseURL, model)
|
||||
b, _ := json.Marshal(body)
|
||||
req, _ := http.NewRequest("POST", "https://api.openai.com/v1/images/generations", bytes.NewReader(b))
|
||||
req, _ := http.NewRequest("POST", endpoint, bytes.NewReader(b))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
|
||||
@ -247,14 +279,74 @@ func (p *Plugin) generateOpenAI(prompt, model, size string, n int, apiKey string
|
||||
urls[i] = d.URL
|
||||
}
|
||||
|
||||
// 下载到本地 data 目录,返回本地文件路径(而非临时 S3 URL):
|
||||
// - S3 临时 URL 约 1 小时过期,且对无浏览器 UA 的客户端拒绝访问
|
||||
// - 本地路径可经 webui /files/ 永久下发给所有客户端(含 API key 客户端)
|
||||
localPaths := make([]string, len(urls))
|
||||
var errs []string
|
||||
for i, u := range urls {
|
||||
path, err := p.downloadImage(u, fmt.Sprintf("ai_%s_%d", model, time.Now().UnixNano()))
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Sprintf("第%d张下载失败: %v", i+1, err))
|
||||
continue
|
||||
}
|
||||
localPaths[i] = path
|
||||
}
|
||||
|
||||
content := fmt.Sprintf("Generated %d image(s) with model %s:", len(urls), model)
|
||||
for _, pth := range localPaths {
|
||||
if pth != "" {
|
||||
content += "\n" + pth
|
||||
}
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
content += "\n\n" + strings.Join(errs, "\n")
|
||||
}
|
||||
content += "\n\n已将图片保存到本地(不会过期)。如需展示请用 output_send__webui(payload=本地路径, type=image)。"
|
||||
return map[string]interface{}{
|
||||
"content": fmt.Sprintf("Generated %d image(s) with model %s:\n%s", len(urls), model, strings.Join(urls, "\n")),
|
||||
"images": urls,
|
||||
"prompt": prompt,
|
||||
"model": model,
|
||||
"content": content,
|
||||
"images": localPaths,
|
||||
"prompt": prompt,
|
||||
"model": model,
|
||||
"local_paths": localPaths,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// downloadImage 把生图返回的临时 URL 下载为本地文件,返回本地路径。
|
||||
// 带浏览器 UA 以规避图床对无 UA 客户端的拦截。
|
||||
func (p *Plugin) downloadImage(url, baseName string) (string, error) {
|
||||
dl := &http.Client{Timeout: 60 * time.Second}
|
||||
req, err := http.NewRequest("GET", url, 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)
|
||||
return "", fmt.Errorf("HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(b))[:200])
|
||||
}
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
ext := ".png"
|
||||
if ct := resp.Header.Get("Content-Type"); strings.Contains(ct, "jpeg") || strings.Contains(ct, "jpg") {
|
||||
ext = ".jpg"
|
||||
} else if 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
|
||||
}
|
||||
|
||||
type stabilityReq struct {
|
||||
TextPrompts []stabilityPrompt `json:"text_prompts"`
|
||||
Width int `json:"width"`
|
||||
@ -334,7 +426,7 @@ func (p *Plugin) generateStability(prompt, model, size string, n int, apiKey str
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"content": fmt.Sprintf("Generated %d image(s) via Stability AI:\n%s", len(urls), strings.Join(urls, "\n")),
|
||||
"content": fmt.Sprintf("Generated %d image(s) via Stability AI:\n%s\n\n图片已保存到本地,如需展示请用 output_send(type=image)。", len(urls), strings.Join(urls, "\n")),
|
||||
"images": urls,
|
||||
"prompt": prompt,
|
||||
"model": model,
|
||||
|
||||
@ -2,14 +2,18 @@
|
||||
"name": "bili",
|
||||
"name_zh": "B站视频下载",
|
||||
"name_en": "Bilibili Video Downloader",
|
||||
"version": "1.1.0",
|
||||
"version": "1.2.0",
|
||||
"description": "B站视频下载工具,基于 yt-dlp 引擎。支持查看视频清晰度列表、指定格式下载、可配置下载目录。",
|
||||
"author": "HomeAgent",
|
||||
"entry": "plugin.so",
|
||||
"tags": ["bili", "video", "download"],
|
||||
"tags": [
|
||||
"bili",
|
||||
"video",
|
||||
"download"
|
||||
],
|
||||
"targets": "linux/amd64",
|
||||
"outdir": "dist",
|
||||
"bundle": true,
|
||||
"replaces": {},
|
||||
"source_dirs": []
|
||||
}
|
||||
}
|
||||
@ -107,6 +107,14 @@ func (p *Plugin) handleBiliVideo(args map[string]interface{}) (interface{}, erro
|
||||
}
|
||||
}
|
||||
}
|
||||
// 安全校验:output_dir 是配置项,但避免被配成系统目录导致 yt-dlp 任意位置写。
|
||||
// 禁止根/家目录本身,且规范化后必须落在明确子目录内。
|
||||
outputDir = filepath.Clean(outputDir)
|
||||
for _, forbidden := range []string{"/", "/etc", "/usr", "/bin", "/sbin", "/boot", "/dev", "/proc", "/sys", "/var"} {
|
||||
if outputDir == forbidden {
|
||||
return nil, fmt.Errorf("output_dir 不能是系统目录 %s", forbidden)
|
||||
}
|
||||
}
|
||||
os.MkdirAll(outputDir, 0755)
|
||||
|
||||
var out bytes.Buffer
|
||||
|
||||
@ -2,14 +2,20 @@
|
||||
"name": "browser",
|
||||
"name_zh": "浏览器",
|
||||
"name_en": "Browser",
|
||||
"version": "2.0.0",
|
||||
"version": "2.3.0",
|
||||
"description": "统一浏览器插件:搜索、HTTP抓取(quick)、无头渲染(normal)、交互式浏览器(interactive/CDP)",
|
||||
"author": "HomeAgent",
|
||||
"entry": "plugin.so",
|
||||
"tags": ["web", "search", "fetch", "browser", "cdp"],
|
||||
"tags": [
|
||||
"web",
|
||||
"search",
|
||||
"fetch",
|
||||
"browser",
|
||||
"cdp"
|
||||
],
|
||||
"targets": "linux/amd64",
|
||||
"outdir": "dist",
|
||||
"bundle": true,
|
||||
"replaces": {},
|
||||
"source_dirs": []
|
||||
}
|
||||
}
|
||||
@ -13,6 +13,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -33,23 +34,49 @@ type Plugin struct {
|
||||
proxy string
|
||||
client *http.Client
|
||||
|
||||
sessions map[string]*BrowserSession
|
||||
nextID int
|
||||
wg sync.WaitGroup
|
||||
stopCh chan struct{}
|
||||
stopOnce sync.Once
|
||||
sessions map[string]*BrowserSession
|
||||
nextID int
|
||||
wg sync.WaitGroup
|
||||
stopCh chan struct{}
|
||||
stopOnce sync.Once
|
||||
profilesDir string // 持久化 profile 根目录(<data>/browser_profiles),空则禁用
|
||||
|
||||
// 共享浏览器单例:所有 agent 共用一个 Chromium 进程(全局 UserDataDir,
|
||||
// 登录态/cookies 跨 agent、跨会话、跨插件重启保留),每个 start 创建一个
|
||||
// 新标签页(CDP Target)。同 source 复用自己的标签页。浏览器进程在
|
||||
// 最后一个标签页关闭后保留(避免反复冷启动),仅插件 Stop 时回收。
|
||||
sharedAllocCtx context.Context
|
||||
sharedAllocCancel context.CancelFunc
|
||||
sharedMu sync.Mutex
|
||||
}
|
||||
|
||||
type BrowserSession struct {
|
||||
id string
|
||||
allocCtx context.Context
|
||||
allocCtx context.Context // 共享浏览器进程上下文(shared=true 时指向全局单例)
|
||||
cancel context.CancelFunc
|
||||
ctx context.Context
|
||||
ctx context.Context // 本会话的 Target 上下文(一个标签页)
|
||||
createdAt time.Time
|
||||
timeout time.Duration
|
||||
closed bool
|
||||
mu sync.Mutex
|
||||
currentURL string
|
||||
shared bool // true=共享浏览器的一个标签页;false=独占浏览器实例
|
||||
profileDir string // 非空表示使用持久化 profile(关闭时不删目录)
|
||||
sessionKey string // 共享模式下的复用键(agent 来源标识,同 key 复用同一标签页)
|
||||
}
|
||||
|
||||
// sanitizeProfileName 消毒 profile 名:仅保留字母数字-_,防路径穿越。
|
||||
func sanitizeProfileName(name string) string {
|
||||
var b []byte
|
||||
for _, c := range []byte(name) {
|
||||
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_' {
|
||||
b = append(b, c)
|
||||
}
|
||||
}
|
||||
if len(b) == 0 || string(b) == "." || string(b) == ".." {
|
||||
return ""
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func NewPluginFactory(name string, config map[string]interface{}) (sdk.Plugin, error) {
|
||||
@ -187,6 +214,13 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
p.proxy = readCfg(s.Settings(), "proxy", "")
|
||||
p.client = newHTTPClient(p.timeout, p.proxy)
|
||||
|
||||
// 持久化 profile 根目录:<data>/browser_profiles
|
||||
if dd, err := s.Settings().GetCore("daemon.data_dir"); err == nil {
|
||||
if s2, ok := dd.(string); ok && s2 != "" {
|
||||
p.profilesDir = filepath.Join(s2, "browser_profiles")
|
||||
}
|
||||
}
|
||||
|
||||
tp := p.name + "_"
|
||||
|
||||
cleaner := func(output string) string {
|
||||
@ -242,12 +276,13 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
|
||||
s.RegisterTool(tp+"start", sdk.ToolDef{
|
||||
Name: tp + "start",
|
||||
Description: "启动交互式浏览器会话(interactive 模式)。通过 CDP 连接 Chromium,支持导航、截图、点击、输入等操作。返回会话 ID。",
|
||||
Description: "启动交互式浏览器会话。优先连接 systemd 托管的共享浏览器后端(登录态全机共享、各 agent 独立标签页);后端未安装时返回 need_install 引导(调 browser_install);无法安装时自动降级本地临时模式。同来源复用已有标签页。",
|
||||
Parameters: map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"url": map[string]interface{}{"type": "string", "description": "初始导航 URL(可选)"},
|
||||
"timeout": map[string]interface{}{"type": "string", "description": "会话超时(如 5m, 10m,默认 10m)"},
|
||||
"profile": map[string]interface{}{"type": "string", "description": "持久化档案名(可选,如 main)。同名档案共享登录态与浏览历史;不指定则为一次性临时会话"},
|
||||
},
|
||||
},
|
||||
}, p.handleBrowserStart)
|
||||
@ -337,6 +372,15 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
},
|
||||
}, p.handleScroll)
|
||||
|
||||
s.RegisterTool(tp+"install", sdk.ToolDef{
|
||||
Name: tp + "install",
|
||||
Description: "安装并启动共享浏览器后端(homeagent-browser.service,systemd 托管)。前提:本机已有 chromium 二进制(无则先提示用户安装:apt install chromium 或等价命令)。安装后所有 agent 共享同一浏览器实例与登录态。",
|
||||
Parameters: map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{},
|
||||
},
|
||||
}, p.handleBrowserInstall)
|
||||
|
||||
s.RegisterTool(tp+"close", sdk.ToolDef{
|
||||
Name: tp + "close",
|
||||
Description: "关闭交互式浏览器会话,释放资源。",
|
||||
@ -663,6 +707,9 @@ func (p *Plugin) fetchWithChromium(rawURL string, maxChars int) (interface{}, er
|
||||
}, nil
|
||||
}
|
||||
|
||||
// handleRender 无头渲染 JS 页面并提取文本(normal 模式)。
|
||||
// 主路径走共享浏览器后端:开临时标签页(带全机登录态)→ 渲染 → 取 text → 关标签页;
|
||||
// 后端不可用时 failback 到独立 chromium --dump-dom(无登录态,仅保功能)。
|
||||
func (p *Plugin) handleRender(args map[string]interface{}) (interface{}, error) {
|
||||
rawURL := readArg(args, "url", "")
|
||||
if rawURL == "" {
|
||||
@ -672,32 +719,70 @@ func (p *Plugin) handleRender(args map[string]interface{}) (interface{}, error)
|
||||
return errResult(err.Error()), nil
|
||||
}
|
||||
waitSec := int64(readArg(args, "wait", float64(0)))
|
||||
if waitSec > 0 {
|
||||
time.Sleep(time.Duration(waitSec) * time.Second)
|
||||
|
||||
var title, html string
|
||||
rendered := false
|
||||
|
||||
ok, needInstall, _ := p.ensureBackend()
|
||||
if ok {
|
||||
remoteCtx, remoteCancel := chromedp.NewRemoteAllocator(context.Background(), cdpEndpoint)
|
||||
defer remoteCancel()
|
||||
tabCtx, tabCancel := chromedp.NewContext(remoteCtx)
|
||||
defer tabCancel()
|
||||
actions := []chromedp.Action{
|
||||
chromedp.Navigate(rawURL),
|
||||
chromedp.WaitReady("body"),
|
||||
}
|
||||
if waitSec > 0 {
|
||||
actions = append(actions, chromedp.Sleep(time.Duration(waitSec)*time.Second))
|
||||
}
|
||||
actions = append(actions,
|
||||
chromedp.Title(&title),
|
||||
chromedp.OuterHTML("html", &html),
|
||||
)
|
||||
// 整体限时 30s,防慢页拖死工具
|
||||
rctx, rcancel := context.WithTimeout(tabCtx, 30*time.Second)
|
||||
defer rcancel()
|
||||
if err := chromedp.Run(rctx, actions...); err == nil {
|
||||
rendered = true
|
||||
} else {
|
||||
log.Printf("[%s] render via backend failed (%v), fallback to dump-dom", p.name, err)
|
||||
}
|
||||
} else if needInstall {
|
||||
return map[string]interface{}{
|
||||
"error": "browser backend not installed",
|
||||
"need_install": true,
|
||||
"guide": "调用 browser_install 安装共享后端;或重试本工具自动降级为独立 chromium 渲染(不带登录态)",
|
||||
}, nil
|
||||
}
|
||||
var html string
|
||||
chromiumPath := "/usr/local/bin/chromium"
|
||||
if _, err := os.Stat(chromiumPath); err == nil {
|
||||
|
||||
if !rendered {
|
||||
chromiumPath := "/usr/local/bin/chromium"
|
||||
if _, err := os.Stat(chromiumPath); err != nil {
|
||||
if _, e2 := exec.LookPath("chromium"); e2 == nil {
|
||||
chromiumPath = "chromium"
|
||||
} else {
|
||||
return errResult("no chromium available"), nil
|
||||
}
|
||||
}
|
||||
var out bytes.Buffer
|
||||
cmd := exec.Command(chromiumPath, "--headless", "--disable-gpu", "--no-sandbox", "--dump-dom", rawURL)
|
||||
cmd.Stdout = &out
|
||||
if err := cmd.Run(); err != nil {
|
||||
return errResult("chromium: " + err.Error()), nil
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- cmd.Run() }()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
return errResult("chromium: " + err.Error()), nil
|
||||
}
|
||||
case <-time.After(30 * time.Second):
|
||||
cmd.Process.Kill()
|
||||
<-done // 回收子进程避免僵尸
|
||||
return errResult("chromium dump-dom timeout (30s)"), nil
|
||||
}
|
||||
html = out.String()
|
||||
} else {
|
||||
resp, err := http.Get(rawURL)
|
||||
if err != nil {
|
||||
return errResult("http get: " + err.Error()), nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
html = string(body)
|
||||
}
|
||||
title := ""
|
||||
if m := regexp.MustCompile(`<title>([^<]+)</title>`).FindStringSubmatch(html); len(m) > 1 {
|
||||
title = m[1]
|
||||
}
|
||||
|
||||
text := htmlToText(html)
|
||||
origLen := len(text)
|
||||
truncated := origLen > 5000
|
||||
@ -712,18 +797,71 @@ func (p *Plugin) handleRender(args map[string]interface{}) (interface{}, error)
|
||||
if truncated {
|
||||
result += fmt.Sprintf("\n\n...(仅显示前 5000 字符,共 %d 字符)", origLen)
|
||||
}
|
||||
return map[string]interface{}{"content": result, "title": title}, nil
|
||||
mode := "backend-tab"
|
||||
if !rendered {
|
||||
mode = "local-dump-dom"
|
||||
}
|
||||
return map[string]interface{}{"content": result, "title": title, "mode": mode}, nil
|
||||
}
|
||||
|
||||
// ── Interactive Browser Session (CDP) ─────────────────────
|
||||
|
||||
func (p *Plugin) handleBrowserStart(args map[string]interface{}) (interface{}, error) {
|
||||
timeoutStr := readArg(args, "timeout", "10m")
|
||||
timeout, err := time.ParseDuration(timeoutStr)
|
||||
func cdpReachable(endpoint string) bool {
|
||||
client := &http.Client{Timeout: 2 * time.Second}
|
||||
resp, err := client.Get(endpoint + "/json/version")
|
||||
if err != nil {
|
||||
timeout = 10 * time.Minute
|
||||
return false
|
||||
}
|
||||
resp.Body.Close()
|
||||
return resp.StatusCode == http.StatusOK
|
||||
}
|
||||
|
||||
// systemdUnitActive 检查 homeagent-browser.service 是否已安装。
|
||||
func systemdUnitInstalled() bool {
|
||||
out, err := exec.Command("systemctl", "cat", "homeagent-browser.service").CombinedOutput()
|
||||
return err == nil && len(out) > 0
|
||||
}
|
||||
|
||||
// startSystemdUnit 尝试 systemctl start(单元已安装但未运行时用)。
|
||||
func startSystemdUnit() error {
|
||||
return exec.Command("systemctl", "start", "homeagent-browser.service").Run()
|
||||
}
|
||||
|
||||
// cdpEndpoint 是共享 Chromium 后端的 CDP 地址(homeagent-browser.service)。
|
||||
const cdpEndpoint = "http://127.0.0.1:9222"
|
||||
|
||||
// ensureBackend 确保共享浏览器后端可用:探测 → 拉起已装服务 → 报告未装。
|
||||
// 返回 (ok, needInstall, err)。
|
||||
func (p *Plugin) ensureBackend() (bool, bool, error) {
|
||||
if cdpReachable(cdpEndpoint) {
|
||||
return true, false, nil
|
||||
}
|
||||
if systemdUnitInstalled() {
|
||||
if err := startSystemdUnit(); err == nil {
|
||||
// 等待 CDP 就绪(chromium 启动 ~1-3s)
|
||||
for i := 0; i < 10; i++ {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
if cdpReachable(cdpEndpoint) {
|
||||
return true, false, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return false, false, fmt.Errorf("browser backend service installed but failed to start")
|
||||
}
|
||||
return false, true, nil // 未安装
|
||||
}
|
||||
|
||||
// sharedTab 在共享后端上开一个新标签页(RemoteAllocator + NewContext)。
|
||||
func sharedTab(allocCtx context.Context) (context.Context, context.CancelFunc, error) {
|
||||
tabCtx, tabCancel := chromedp.NewContext(allocCtx)
|
||||
if err := chromedp.Run(tabCtx); err != nil {
|
||||
tabCancel()
|
||||
return nil, nil, err
|
||||
}
|
||||
return tabCtx, tabCancel, nil
|
||||
}
|
||||
|
||||
// localSpawnFailback 本地拉起一次性 Chromium(离线机器无法装 systemd 服务的兜底)。
|
||||
// 用临时 profile,登录态不跨会话保留——仅保证功能可用。
|
||||
func (p *Plugin) localSpawnFailback() (context.Context, context.CancelFunc, context.CancelFunc, error) {
|
||||
opts := append(chromedp.DefaultExecAllocatorOptions[:],
|
||||
chromedp.Flag("headless", true),
|
||||
chromedp.Flag("disable-gpu", true),
|
||||
@ -733,23 +871,83 @@ func (p *Plugin) handleBrowserStart(args map[string]interface{}) (interface{}, e
|
||||
if p.proxy != "" {
|
||||
opts = append(opts, chromedp.Flag("proxy-server", p.proxy))
|
||||
}
|
||||
|
||||
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
|
||||
allocCtx, cancelAlloc := chromedp.NewExecAllocator(context.Background(), opts...)
|
||||
ctx, _ := chromedp.NewContext(allocCtx)
|
||||
|
||||
// 立即分配浏览器和 Target,确保后续 Run 的 timeout context 不会杀死浏览器进程
|
||||
// chromedp 官方警告:首调用带 timeout 的 Run 会杀死整个浏览器
|
||||
if err := chromedp.Run(ctx); err != nil {
|
||||
cancel()
|
||||
return errResult("browser init failed: " + err.Error()), nil
|
||||
cancelAlloc()
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
return allocCtx, cancelAlloc, nil, nil
|
||||
}
|
||||
|
||||
func (p *Plugin) handleBrowserStart(args map[string]interface{}) (interface{}, error) {
|
||||
timeoutStr := readArg(args, "timeout", "10m")
|
||||
timeout, err := time.ParseDuration(timeoutStr)
|
||||
if err != nil {
|
||||
timeout = 10 * time.Minute
|
||||
}
|
||||
|
||||
session := &BrowserSession{
|
||||
allocCtx: allocCtx,
|
||||
cancel: cancel,
|
||||
ctx: ctx,
|
||||
createdAt: time.Now(),
|
||||
timeout: timeout,
|
||||
source := readArg(args, "source", "")
|
||||
if source == "" {
|
||||
source = "default"
|
||||
}
|
||||
|
||||
// 同 source 复用已有标签页
|
||||
p.mu.Lock()
|
||||
for _, s := range p.sessions {
|
||||
if s.shared && s.sessionKey == source && !s.closed {
|
||||
s.mu.Lock()
|
||||
id := s.id
|
||||
cur := s.currentURL
|
||||
s.mu.Unlock()
|
||||
p.mu.Unlock()
|
||||
return map[string]interface{}{
|
||||
"id": id,
|
||||
"status": "reused",
|
||||
"url": cur,
|
||||
"note": "已复用本来源的现有标签页(登录态全机共享)",
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
p.mu.Unlock()
|
||||
|
||||
var session *BrowserSession
|
||||
|
||||
// 路径一:systemd 托管的共享后端(主路径)
|
||||
ok, needInstall, berr := p.ensureBackend()
|
||||
if ok {
|
||||
remoteCtx, remoteCancel := chromedp.NewRemoteAllocator(context.Background(), cdpEndpoint)
|
||||
probe, _ := chromedp.NewContext(remoteCtx)
|
||||
if err := chromedp.Run(probe); err != nil {
|
||||
remoteCancel()
|
||||
return errResult("connect to browser backend failed: " + err.Error()), nil
|
||||
}
|
||||
tabCtx, tabCancel := chromedp.NewContext(remoteCtx)
|
||||
if err := chromedp.Run(tabCtx); err != nil {
|
||||
remoteCancel()
|
||||
return errResult("open tab failed: " + err.Error()), nil
|
||||
}
|
||||
session = &BrowserSession{
|
||||
allocCtx: remoteCtx,
|
||||
cancel: tabCancel,
|
||||
ctx: tabCtx,
|
||||
createdAt: time.Now(),
|
||||
timeout: timeout,
|
||||
shared: true,
|
||||
sessionKey: source,
|
||||
}
|
||||
} else if needInstall {
|
||||
guide := "浏览器后端未安装。请确认后调用 browser_install 工具完成安装:" +
|
||||
"需要本机有 chromium 二进制(apt install chromium 或等价命令)," +
|
||||
"插件会注册 homeagent-browser.service 并启动。" +
|
||||
"若本机无法联网安装 chromium,可继续用本地临时模式(重试 browser_start 即自动降级)。"
|
||||
return map[string]interface{}{
|
||||
"error": "backend not installed",
|
||||
"need_install": true,
|
||||
"guide": guide,
|
||||
}, nil
|
||||
} else {
|
||||
return errResult("browser backend error: " + berr.Error()), nil
|
||||
}
|
||||
|
||||
p.mu.Lock()
|
||||
@ -761,7 +959,7 @@ func (p *Plugin) handleBrowserStart(args map[string]interface{}) (interface{}, e
|
||||
|
||||
initURL := readArg(args, "url", "")
|
||||
if initURL != "" {
|
||||
if err := chromedp.Run(ctx,
|
||||
if err := chromedp.Run(session.ctx,
|
||||
chromedp.Navigate(initURL),
|
||||
chromedp.WaitReady("body"),
|
||||
); err != nil {
|
||||
@ -772,13 +970,13 @@ func (p *Plugin) handleBrowserStart(args map[string]interface{}) (interface{}, e
|
||||
return errResult("navigate failed: " + err.Error()), nil
|
||||
}
|
||||
session.currentURL = initURL
|
||||
p.sdk.InjectTextNoMemory(p.name, p.name, fmt.Sprintf("[浏览器 %s 已打开 %s]", id, initURL))
|
||||
}
|
||||
|
||||
log.Printf("[%s] created browser session %s: url=%s timeout=%v", p.name, id, initURL, timeout)
|
||||
log.Printf("[%s] created browser session %s: url=%s timeout=%v source=%s", p.name, id, initURL, timeout, source)
|
||||
return map[string]interface{}{
|
||||
"id": id,
|
||||
"status": "created",
|
||||
"mode": "shared-backend",
|
||||
"url": initURL,
|
||||
"timeout": timeout.String(),
|
||||
}, nil
|
||||
@ -1019,3 +1217,104 @@ func (p *Plugin) cleanupLoop() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── browser_install:安装 systemd 托管的共享浏览器后端 ──────────
|
||||
|
||||
// handleBrowserInstall 注册 homeagent-browser.service 并启动,验证 CDP 可达。
|
||||
// 返回给 agent 的结果含全机共享使用指南(由 agent 转述给用户)。
|
||||
func (p *Plugin) handleBrowserInstall(args map[string]interface{}) (interface{}, error) {
|
||||
if cdpReachable(cdpEndpoint) {
|
||||
return map[string]interface{}{"status": "already_running", "endpoint": cdpEndpoint}, nil
|
||||
}
|
||||
|
||||
// 探测 chromium 二进制
|
||||
chromePath := ""
|
||||
for _, c := range []string{
|
||||
"/usr/bin/chromium", "/usr/bin/chromium-browser",
|
||||
"/usr/local/bin/chromium", "/usr/bin/google-chrome",
|
||||
} {
|
||||
if _, err := os.Stat(c); err == nil {
|
||||
chromePath = c
|
||||
break
|
||||
}
|
||||
}
|
||||
if out, err := exec.LookPath("chromium"); err == nil && chromePath == "" {
|
||||
chromePath = out
|
||||
} else if out, err := exec.LookPath("google-chrome"); err == nil && chromePath == "" {
|
||||
chromePath = out
|
||||
}
|
||||
if chromePath == "" {
|
||||
return map[string]interface{}{
|
||||
"error": "chromium binary not found",
|
||||
"hint": "请先安装 chromium:apt install chromium 或等价命令,然后重试 browser_install",
|
||||
}, nil
|
||||
}
|
||||
|
||||
profileDir := ""
|
||||
if p.profilesDir != "" {
|
||||
profileDir = filepath.Join(p.profilesDir, "shared")
|
||||
os.MkdirAll(profileDir, 0755)
|
||||
} else {
|
||||
// profilesDir 未注入(无 data_dir),退到 /var/lib/homeagent-browser
|
||||
profileDir = "/var/lib/homeagent-browser"
|
||||
os.MkdirAll(profileDir, 0755)
|
||||
}
|
||||
|
||||
unit := fmt.Sprintf(`[Unit]
|
||||
Description=HomeAgent Shared Browser Backend (headless chromium, CDP :9222)
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=%s --headless --no-sandbox --disable-gpu --disable-dev-shm-usage --remote-debugging-port=9222 --user-data-dir=%s --window-size=1280,800 about:blank
|
||||
Restart=always
|
||||
RestartSec=3
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
`, chromePath, profileDir)
|
||||
|
||||
unitPath := "/etc/systemd/system/homeagent-browser.service"
|
||||
if err := os.WriteFile(unitPath, []byte(unit), 0644); err != nil {
|
||||
return map[string]interface{}{
|
||||
"error": "write unit failed (need root): " + err.Error(),
|
||||
"hint": "插件进程无权限写 /etc/systemd/system 时,请让用户手动执行安装命令(见 manual_cmds)",
|
||||
"manual_cmds": []string{
|
||||
"sudo tee /etc/systemd/system/homeagent-browser.service <<'EOF'\n" + unit + "EOF",
|
||||
"sudo systemctl daemon-reload",
|
||||
"sudo systemctl enable --now homeagent-browser.service",
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
for _, cmd := range [][]string{
|
||||
{"systemctl", "daemon-reload"},
|
||||
{"systemctl", "enable", "--now", "homeagent-browser.service"},
|
||||
} {
|
||||
if out, err := exec.Command(cmd[0], cmd[1:]...).CombinedOutput(); err != nil {
|
||||
return map[string]interface{}{
|
||||
"error": fmt.Sprintf("%v: %s", cmd, string(out)),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
// 等待 CDP 就绪
|
||||
for i := 0; i < 20; i++ {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
if cdpReachable(cdpEndpoint) {
|
||||
guide := "共享浏览器后端已就绪(CDP " + cdpEndpoint + ")。\n" +
|
||||
"全机共享说明:本机所有 agent(HomeAgent、pi、opencode、deepseekharness 等)都可连接此实例:" +
|
||||
"登录一次全机可用;各 agent 各自占用独立标签页互不干扰;\n" +
|
||||
"- HomeAgent 内部:browser_start 即自动连接本后端\n" +
|
||||
"- 其他 agent:让其浏览器工具/MCP 连接 CDP 端点 " + cdpEndpoint + "(如 playwright connectOverCDP / puppeteer connect)\n" +
|
||||
"- 服务由 systemd 托管:崩溃自动重启,登录态持久保存在 " + profileDir
|
||||
log.Printf("[%s] browser backend installed and running (chrome=%s profile=%s)", p.name, chromePath, profileDir)
|
||||
return map[string]interface{}{
|
||||
"status": "installed",
|
||||
"endpoint": cdpEndpoint,
|
||||
"chrome": chromePath,
|
||||
"profile": profileDir,
|
||||
"guide": guide,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
return map[string]interface{}{"error": "service started but CDP not reachable after 10s"}, nil
|
||||
}
|
||||
|
||||
@ -2,14 +2,19 @@
|
||||
"name": "calendar",
|
||||
"name_zh": "日历",
|
||||
"name_en": "Calendar",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"description": "日历事件管理,支持提醒和重复事件",
|
||||
"author": "HomeAgent",
|
||||
"entry": "plugin.so",
|
||||
"tags": ["calendar", "event", "reminder", "schedule"],
|
||||
"tags": [
|
||||
"calendar",
|
||||
"event",
|
||||
"reminder",
|
||||
"schedule"
|
||||
],
|
||||
"targets": "linux/amd64",
|
||||
"outdir": "dist",
|
||||
"bundle": true,
|
||||
"replaces": {},
|
||||
"source_dirs": []
|
||||
}
|
||||
}
|
||||
@ -656,7 +656,7 @@ func (p *Plugin) saveEventsLocked() {
|
||||
NextEventID: p.nextEventID,
|
||||
}
|
||||
b, _ := json.MarshalIndent(data, "", " ")
|
||||
os.WriteFile(p.eventsFile(), b, 0644)
|
||||
atomicWriteJSON(p.eventsFile(), b)
|
||||
}
|
||||
|
||||
// --- Helper: parse remind_before ---
|
||||
@ -1177,3 +1177,12 @@ func (p *Plugin) handleSearch(args map[string]interface{}) (interface{}, error)
|
||||
}
|
||||
return map[string]interface{}{"content": strings.Join(lines, "\n")}, nil
|
||||
}
|
||||
|
||||
// atomicWriteJSON 原子写 JSON:先写临时文件再 rename,避免进程崩溃截断数据文件。
|
||||
func atomicWriteJSON(path string, data []byte) error {
|
||||
tmp := path + ".tmp"
|
||||
if err := os.WriteFile(tmp, data, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmp, path)
|
||||
}
|
||||
|
||||
@ -2,14 +2,18 @@
|
||||
"name": "memo",
|
||||
"name_zh": "备忘录",
|
||||
"name_en": "Memo",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"description": "待办与备忘录插件。待办(todo_add/todo_complete/todo_list)会主动提醒;备忘录(memo_create/memo_list/memo_delete)纯记事不提醒。",
|
||||
"author": "HomeAgent",
|
||||
"entry": "plugin.so",
|
||||
"tags": ["memo", "todo", "notes"],
|
||||
"tags": [
|
||||
"memo",
|
||||
"todo",
|
||||
"notes"
|
||||
],
|
||||
"targets": "linux/amd64",
|
||||
"outdir": "dist",
|
||||
"bundle": true,
|
||||
"replaces": {},
|
||||
"source_dirs": []
|
||||
}
|
||||
}
|
||||
@ -224,7 +224,7 @@ func (p *Plugin) saveTodos() {
|
||||
"next_id": p.nextTID,
|
||||
}, "", " ")
|
||||
p.mu.RUnlock()
|
||||
os.WriteFile(p.todoPath, data, 0644)
|
||||
atomicWriteJSON(p.todoPath, data)
|
||||
}
|
||||
|
||||
func (p *Plugin) saveMemos() {
|
||||
@ -234,7 +234,7 @@ func (p *Plugin) saveMemos() {
|
||||
"next_id": p.nextMID,
|
||||
}, "", " ")
|
||||
p.mu.RUnlock()
|
||||
os.WriteFile(p.memoPath, data, 0644)
|
||||
atomicWriteJSON(p.memoPath, data)
|
||||
}
|
||||
|
||||
// ── 待办:未完成计数与提醒 ──
|
||||
@ -500,3 +500,12 @@ func (p *Plugin) cleanupData() {
|
||||
os.Remove(p.memoPath)
|
||||
}
|
||||
}
|
||||
|
||||
// atomicWriteJSON 原子写 JSON:先写临时文件再 rename,避免进程崩溃截断数据文件。
|
||||
func atomicWriteJSON(path string, data []byte) error {
|
||||
tmp := path + ".tmp"
|
||||
if err := os.WriteFile(tmp, data, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmp, path)
|
||||
}
|
||||
|
||||
@ -2,14 +2,17 @@
|
||||
"name": "qq",
|
||||
"name_zh": "QQ消息",
|
||||
"name_en": "qq",
|
||||
"version": "1.0.0",
|
||||
"version": "1.2.0",
|
||||
"description": "QQ 消息收发插件,通过 NapCat 协议桥接",
|
||||
"author": "HomeAgent",
|
||||
"entry": "plugin.so",
|
||||
"tags": ["qq", "messaging"],
|
||||
"tags": [
|
||||
"qq",
|
||||
"messaging"
|
||||
],
|
||||
"targets": "linux/amd64",
|
||||
"outdir": "dist",
|
||||
"bundle": false,
|
||||
"replaces": {},
|
||||
"source_dirs": []
|
||||
}
|
||||
}
|
||||
@ -107,13 +107,149 @@ type Plugin struct {
|
||||
downloadTasks []*DownloadTask
|
||||
typingMu sync.Mutex
|
||||
typingMap map[int64]*typingState
|
||||
|
||||
// msg_id → peer 映射 + 会话最新状态(<7 天兜底 get_history + list_chats)
|
||||
msgMu sync.Mutex
|
||||
msgMap map[int64]msgRef // message_id → {peer, time}
|
||||
chats map[int64]*chatMeta // peerID → 会话状态(群号或 QQ 号)
|
||||
}
|
||||
|
||||
|
||||
type typingState struct {
|
||||
userID int64
|
||||
stopCh chan struct{}
|
||||
}
|
||||
|
||||
// msgRef 一条已见过的消息的引用:只记录 msg_id → (peer, time) 映射,不缓存正文。
|
||||
// 用途:NapCat get_msg 的临时短号 <7 天失效时,据此把 get_msg 兜底为按 peer 拉 get_history。
|
||||
type msgRef struct {
|
||||
peerID int64
|
||||
isGroup bool
|
||||
time int64 // 秒级时间戳
|
||||
}
|
||||
|
||||
// chatMeta 一个会话(群/私聊)的最新状态,供 list_chats 展示。
|
||||
// 只维护最新一条的短摘要(≤qqLastSumLen 字符)与未读数,不缓存完整历史。
|
||||
type chatMeta struct {
|
||||
peerID int64
|
||||
isGroup bool
|
||||
name string
|
||||
unread int
|
||||
lastTime int64
|
||||
lastText string
|
||||
lastNick string
|
||||
}
|
||||
|
||||
const qqMsgTTL = 7 * 86400 // 7 天:msg_id → peer 映射的有效期
|
||||
const qqLastSumLen = 60 // list_chats 里最新一条摘要的最大长度
|
||||
|
||||
// snapshotMsg 记录一条策略允许的消息:更新 msg_id→peer 映射与会话未读/最新状态。
|
||||
// 不缓存消息正文(仅最新一条留 ≤qqLastSumLen 的摘要供列表展示)。
|
||||
func (p *Plugin) snapshotMsg(msgID, peerID int64, isGroup bool, t int64, nickname, text string) {
|
||||
if msgID <= 0 {
|
||||
return
|
||||
}
|
||||
p.msgMu.Lock()
|
||||
defer p.msgMu.Unlock()
|
||||
|
||||
// msg_id 映射(7 天 TTL,惰性清理)
|
||||
p.msgMap[msgID] = msgRef{peerID: peerID, isGroup: isGroup, time: t}
|
||||
now := time.Now().Unix()
|
||||
if len(p.msgMap) > 2000 { // 定期清理过期项
|
||||
for k, v := range p.msgMap {
|
||||
if now-v.time > qqMsgTTL {
|
||||
delete(p.msgMap, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ch := p.chats[peerID]
|
||||
if ch == nil {
|
||||
ch = &chatMeta{peerID: peerID, isGroup: isGroup}
|
||||
p.chats[peerID] = ch
|
||||
}
|
||||
if ch.name == "" {
|
||||
if isGroup {
|
||||
ch.name = fmt.Sprintf("群%d", peerID)
|
||||
} else {
|
||||
ch.name = nickname
|
||||
}
|
||||
}
|
||||
// 按到达次序维护未读与最新摘要:仅当本条更新时才更新 lastTime/lastText(保持按时间排)
|
||||
if t > ch.lastTime {
|
||||
ch.lastTime = t
|
||||
ch.lastText = text
|
||||
ch.lastNick = nickname
|
||||
}
|
||||
ch.unread++
|
||||
}
|
||||
|
||||
// lookupMsgRef 查 msg_id 映射,返回 (peer, isGroup, time, ok)。超过 7 天视为无效(交给 get_history)。
|
||||
func (p *Plugin) lookupMsgRef(msgID int64) (int64, bool, int64, bool) {
|
||||
p.msgMu.Lock()
|
||||
defer p.msgMu.Unlock()
|
||||
ref, ok := p.msgMap[msgID]
|
||||
if !ok {
|
||||
return 0, false, 0, false
|
||||
}
|
||||
now := time.Now().Unix()
|
||||
if now-ref.time > qqMsgTTL {
|
||||
delete(p.msgMap, msgID)
|
||||
return 0, false, 0, false
|
||||
}
|
||||
return ref.peerID, ref.isGroup, ref.time, true
|
||||
}
|
||||
|
||||
// markChatRead 清零某会话未读数(模型处理完该会话后调用)。
|
||||
func (p *Plugin) markChatRead(peerID int64) {
|
||||
p.msgMu.Lock()
|
||||
defer p.msgMu.Unlock()
|
||||
if ch := p.chats[peerID]; ch != nil {
|
||||
ch.unread = 0
|
||||
}
|
||||
}
|
||||
|
||||
// listChats 返回会话列表:按最新消息时间降序,含未读数与最新一条摘要。
|
||||
func (p *Plugin) listChats(capN int) []map[string]interface{} {
|
||||
p.msgMu.Lock()
|
||||
list := make([]*chatMeta, 0, len(p.chats))
|
||||
for _, c := range p.chats {
|
||||
list = append(list, c)
|
||||
}
|
||||
p.msgMu.Unlock()
|
||||
|
||||
// 降序(最新消息在前)
|
||||
for i := 1; i < len(list); i++ {
|
||||
for j := i; j > 0 && list[j].lastTime > list[j-1].lastTime; j-- {
|
||||
list[j], list[j-1] = list[j-1], list[j]
|
||||
}
|
||||
}
|
||||
if len(list) > capN {
|
||||
list = list[:capN]
|
||||
}
|
||||
|
||||
out := make([]map[string]interface{}, 0, len(list))
|
||||
for _, c := range list {
|
||||
typ := "private"
|
||||
if c.isGroup {
|
||||
typ = "group"
|
||||
}
|
||||
item := map[string]interface{}{
|
||||
"peer_id": c.peerID,
|
||||
"type": typ,
|
||||
"name": c.name,
|
||||
"unread": c.unread,
|
||||
"last_text": c.lastText,
|
||||
"last_nick": c.lastNick,
|
||||
}
|
||||
if c.lastTime > 0 {
|
||||
item["last_time"] = time.Unix(c.lastTime, 0).Format("2006-01-02 15:04")
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (p *Plugin) Name() string { return p.name }
|
||||
|
||||
func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
@ -150,6 +286,10 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
|
||||
p.httpClient = &http.Client{Timeout: 30 * time.Second}
|
||||
|
||||
// msg_id → peer 映射 + 会话状态(不缓存正文)
|
||||
p.msgMap = make(map[int64]msgRef)
|
||||
p.chats = make(map[int64]*chatMeta)
|
||||
|
||||
// 从 NapCat 获取 Bot 身份(阻塞等待,最多 5s)
|
||||
p.fetchBotInfo()
|
||||
if p.botID == 0 {
|
||||
@ -248,6 +388,27 @@ type 枚举: text(文字)/ voice(语音转文字后发送)/ image(图
|
||||
},
|
||||
}, p.handleGetHistory)
|
||||
|
||||
p.regTool(s, sdk.ToolDef{
|
||||
Name: tp + "list_chats", Description: "获取QQ会话列表,与真人客户端一致:按最新消息先后排序,每条标注会话(群/私聊)、会话名、未读消息数、最新一条消息摘要与时间。用于发现有未读消息的会话,再配合 qq_get_history 拉取对应会话内容、output_send__qq 回复。",
|
||||
NoMemory: false,
|
||||
Parameters: map[string]interface{}{
|
||||
"type": "object", "properties": map[string]interface{}{
|
||||
"count": map[string]interface{}{"type": "integer", "description": "最多返回会话数,默认10"},
|
||||
}, "required": []string{},
|
||||
},
|
||||
}, p.handleListChats)
|
||||
|
||||
p.regTool(s, sdk.ToolDef{
|
||||
Name: tp + "mark_read", Description: "将某个会话的未读计数清零(对象:群聊传 group_id,私聊传 user_id)。处理完某会话消息后可调用,让 list_chats 的未读数回到0,与真人客户端标记已读一致。",
|
||||
NoMemory: false,
|
||||
Parameters: map[string]interface{}{
|
||||
"type": "object", "properties": map[string]interface{}{
|
||||
"group_id": map[string]interface{}{"type": "integer", "description": "群号(与user_id二选一)"},
|
||||
"user_id": map[string]interface{}{"type": "integer", "description": "QQ号(与group_id二选一)"},
|
||||
}, "required": []string{},
|
||||
},
|
||||
}, p.handleMarkRead)
|
||||
|
||||
// ---- 查询 ----
|
||||
p.regTool(s, sdk.ToolDef{
|
||||
Name: tp + "get_groups", Description: "获取QQ群列表,可按关键词搜索群名",
|
||||
@ -739,6 +900,11 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
// 群消息到达即记录(用于排查 napcat→webhook 链路漏报/丢弃)
|
||||
if evt.MessageType == "group" {
|
||||
log.Printf("[qq] webhook recv group msg id=%d from=%d in=%d raw=%.100s",
|
||||
evt.MessageID, evt.UserID, evt.GroupID, evt.RawMessage)
|
||||
}
|
||||
|
||||
rawCQ := evt.RawMessage
|
||||
text := rawCQ
|
||||
@ -763,9 +929,32 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
if evt.MessageType == "group" {
|
||||
if !p.isGroupAllowed(evt.GroupID) {
|
||||
log.Printf("[qq] group msg from %d rejected: policy=%s", evt.GroupID, p.groupPolicy)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// ---- 记录 msg_id→peer 映射与会话状态(不缓存正文,仅最新一条短摘要)----
|
||||
// 策略允许的消息(群/私聊、是否 @bot 均记),供 get_msg 兜底与 list_chats 使用;
|
||||
// @bot 与否只决定是否发中断,不影响记录——与真人客户端一致看到全部会话。
|
||||
{
|
||||
peerID, isGroup := evt.UserID, false
|
||||
if evt.MessageType == "group" {
|
||||
peerID, isGroup = evt.GroupID, true
|
||||
}
|
||||
sum := text
|
||||
runes := []rune(sum)
|
||||
if len(runes) > qqLastSumLen {
|
||||
sum = string(runes[:qqLastSumLen]) + "…"
|
||||
}
|
||||
if evt.Time == 0 {
|
||||
evt.Time = time.Now().Unix()
|
||||
}
|
||||
p.snapshotMsg(evt.MessageID, peerID, isGroup, evt.Time, nickname, sum)
|
||||
}
|
||||
|
||||
if evt.MessageType == "group" {
|
||||
// 群消息必须 @ 机器人才响应
|
||||
if p.botID == 0 {
|
||||
log.Printf("[qq] bot ID unknown, rejecting group message from %d", evt.GroupID)
|
||||
@ -773,6 +962,9 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
if !p.isAtBot(evt.Message) {
|
||||
// 诊断:@ 解析失败时打印 at 段原文与 botID,定位漏报问题
|
||||
log.Printf("[qq] group msg from %d/%d not @bot (botID=%d, raw=%.120s)",
|
||||
evt.GroupID, evt.UserID, p.botID, rawCQ)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
@ -782,9 +974,9 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
outputTool := "output_send__" + p.name
|
||||
var interrupt string
|
||||
if evt.MessageType == "group" {
|
||||
interrupt = fmt.Sprintf("来自「%s」在群「%s」的消息(message_id=%d)。使用%sget_message(message_id=%d)获取消息正文。如果消息包含引用回复,使用%sget_history(group_id=%d)查看上下文。使用%s回复群聊", nickname, "群聊", evt.MessageID, tp, evt.MessageID, tp, evt.GroupID, outputTool)
|
||||
interrupt = fmt.Sprintf("来自「%s」在群「%s」的消息(message_id=%d)。先用%sget_message(message_id=%d)取正文;若取不到(消息已过期),改用%sget_history(group_id=%d)按会话拉取上下文,或用%slist_chats 查看未读会话。用%s回复群聊", nickname, "群聊", evt.MessageID, tp, evt.MessageID, tp, evt.GroupID, tp, outputTool)
|
||||
} else {
|
||||
interrupt = fmt.Sprintf("来自「%s」的私聊消息(message_id=%d)。使用%sget_message(message_id=%d)获取消息正文。使用%s回复对方", nickname, evt.MessageID, tp, evt.MessageID, outputTool)
|
||||
interrupt = fmt.Sprintf("来自「%s」的私聊消息(message_id=%d, user_id=%d)。先用%sget_message(message_id=%d)取正文;若取不到(消息已过期),改用%sget_history(user_id=%d)按会话拉取上下文,或用%slist_chats 查看未读会话。用%s回复对方", nickname, evt.MessageID, evt.UserID, tp, evt.MessageID, tp, evt.UserID, tp, outputTool)
|
||||
}
|
||||
if p.isAdmin(evt.UserID) {
|
||||
interrupt = "【重要!老大消息】" + interrupt
|
||||
@ -810,6 +1002,7 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
if evt.GroupID == rule.GroupID {
|
||||
mcMsg := fmt.Sprintf("%s 说 %s", nickname, text)
|
||||
go func(r ForwardRule, msg string) {
|
||||
defer func() { _ = recover() }()
|
||||
if err := rconSend(r.Host, r.Port, r.Password, "say "+msg); err != nil {
|
||||
log.Printf("[qq] rcon forward to %s:%d: %v", r.Host, r.Port, err)
|
||||
}
|
||||
@ -831,6 +1024,126 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// ======== Tool Handlers ========
|
||||
|
||||
// getMsgFromHistoryByTime 按 (peer, isGroup, targetTime) 从 NapCat 拉最近历史,返回距 targetTime 最近的完整消息。
|
||||
func (p *Plugin) getMsgFromHistoryByTime(peerID int64, isGroup bool, targetTime int64) (map[string]interface{}, bool) {
|
||||
ep := "get_friend_msg_history"
|
||||
params := map[string]interface{}{"user_id": peerID, "count": 50}
|
||||
if isGroup {
|
||||
ep = "get_group_msg_history"
|
||||
params = map[string]interface{}{"group_id": peerID, "count": 50}
|
||||
}
|
||||
raw, err := p.napcat(ep, params)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
rawStr, _ := rawString(raw)
|
||||
if rawStr == "" {
|
||||
return nil, false
|
||||
}
|
||||
var resp struct {
|
||||
Data *struct {
|
||||
Messages []interface{} `json:"messages"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if json.Unmarshal([]byte(rawStr), &resp) != nil || resp.Data == nil {
|
||||
return nil, false
|
||||
}
|
||||
var best map[string]interface{}
|
||||
bestAbs := int64(-1)
|
||||
for _, m := range resp.Data.Messages {
|
||||
mm, ok := m.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
mt, _ := mm["time"].(float64)
|
||||
t := int64(mt)
|
||||
if t == 0 {
|
||||
continue
|
||||
}
|
||||
abs := t - targetTime
|
||||
if abs < 0 {
|
||||
abs = -abs
|
||||
}
|
||||
if bestAbs < 0 || abs < bestAbs {
|
||||
bestAbs = abs
|
||||
best = mm
|
||||
}
|
||||
}
|
||||
if best == nil {
|
||||
return nil, false
|
||||
}
|
||||
return best, true
|
||||
}
|
||||
|
||||
// msgToGetMsgResult 把一条 NapCat 历史消息对象转成与 get_msg 同构的结果(历史包装语义)。
|
||||
func msgToGetMsgResult(msg map[string]interface{}) map[string]interface{} {
|
||||
nickname := ""
|
||||
if s, ok := msg["sender"].(map[string]interface{}); ok {
|
||||
if n, _ := s["nickname"].(string); n != "" {
|
||||
nickname = n
|
||||
}
|
||||
if c, _ := s["card"].(string); c != "" {
|
||||
nickname = c
|
||||
}
|
||||
}
|
||||
rawText, _ := msg["raw_message"].(string)
|
||||
content := rawText
|
||||
if content == "" {
|
||||
if segs, ok := msg["message"].([]interface{}); ok {
|
||||
var parts []string
|
||||
for _, seg := range segs {
|
||||
segMap, _ := seg.(map[string]interface{})
|
||||
if segMap == nil {
|
||||
continue
|
||||
}
|
||||
typ, _ := segMap["type"].(string)
|
||||
segData, _ := segMap["data"].(map[string]interface{})
|
||||
if segData == nil {
|
||||
continue
|
||||
}
|
||||
switch typ {
|
||||
case "text":
|
||||
if t, _ := segData["text"].(string); t != "" {
|
||||
parts = append(parts, t)
|
||||
}
|
||||
case "image":
|
||||
parts = append(parts, "[图片]")
|
||||
case "file":
|
||||
if n, _ := segData["name"].(string); n != "" {
|
||||
parts = append(parts, "[文件:"+n+"]")
|
||||
}
|
||||
default:
|
||||
if typ != "" {
|
||||
parts = append(parts, "["+typ+"]")
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(parts) > 0 {
|
||||
content = strings.Join(parts, " ")
|
||||
}
|
||||
}
|
||||
}
|
||||
mid, _ := msg["message_id"].(float64)
|
||||
uid, _ := msg["user_id"].(float64)
|
||||
gid, _ := msg["group_id"].(float64)
|
||||
mt, _ := msg["time"].(float64)
|
||||
mtType, _ := msg["message_type"].(string)
|
||||
loc := "私聊"
|
||||
if mtType == "group" || gid > 0 {
|
||||
loc = "群聊"
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"content": content,
|
||||
"message_id": int64(mid),
|
||||
"user_id": int64(uid),
|
||||
"group_id": int64(gid),
|
||||
"nickname": nickname,
|
||||
"message_type": mtType,
|
||||
"type": loc,
|
||||
"time": time.Unix(int64(mt), 0).Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) handleGetMessage(args map[string]interface{}) (interface{}, error) {
|
||||
msgID, err := convInt64(args["message_id"])
|
||||
if err != nil {
|
||||
@ -840,10 +1153,24 @@ func (p *Plugin) handleGetMessage(args map[string]interface{}) (interface{}, err
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 本地 msg_id→peer 映射命中且 <7 天 → 用 get_history 语义兜底(NapCat 临时短号失效也不怕)
|
||||
if peerID, isGroup, t, ok := p.lookupMsgRef(msgID); ok {
|
||||
if m, found := p.getMsgFromHistoryByTime(peerID, isGroup, t); found {
|
||||
// 找到同会话、时间最接近的消息,包装为 get_msg 同构返回
|
||||
res := msgToGetMsgResult(m)
|
||||
res["resolved_via"] = "history" // 标明由历史查询兜底
|
||||
return res, nil
|
||||
}
|
||||
// 历史窗口内没找到(消息可能被裁剪/更早),回退 NapCat 原查询
|
||||
}
|
||||
return p.getMsgFromNapcat(msgID)
|
||||
}
|
||||
|
||||
func (p *Plugin) getMsgFromNapcat(msgID int64) (interface{}, error) {
|
||||
raw, err := p.napcat("get_msg", map[string]interface{}{"message_id": msgID})
|
||||
if err != nil {
|
||||
return map[string]interface{}{
|
||||
"content": fmt.Sprintf("查询 NapCat 失败: %s", err),
|
||||
"content": fmt.Sprintf("查询 NapCat 失败: %s。该 message_id 可能已过期,请改用 qq_get_history 按会话拉取最近消息(或用 qq_list_chats 看未读会话)", err),
|
||||
"message_id": msgID,
|
||||
"not_found": true,
|
||||
}, nil
|
||||
@ -874,7 +1201,7 @@ func (p *Plugin) handleGetMessage(args map[string]interface{}) (interface{}, err
|
||||
}
|
||||
if err := json.Unmarshal([]byte(rawStr), &resp); err != nil || resp.Data == nil {
|
||||
return map[string]interface{}{
|
||||
"content": "解析 NapCat 响应失败",
|
||||
"content": "解析 NapCat 响应失败(消息可能已过期)。请改用 qq_get_history 按会话拉取最近消息,或用 qq_list_chats 查看未读会话",
|
||||
"message_id": msgID,
|
||||
"not_found": true,
|
||||
}, nil
|
||||
@ -980,6 +1307,7 @@ func (p *Plugin) handleGetMessage(args map[string]interface{}) (interface{}, err
|
||||
|
||||
// 异步标记已读
|
||||
go func() {
|
||||
defer func() { _ = recover() }() // 后台任务不允许 panic 冒泡带崩进程
|
||||
if d.MessageType == "group" && d.GroupID > 0 {
|
||||
p.napcat("mark_group_msg_as_read", map[string]interface{}{"group_id": d.GroupID})
|
||||
} else if d.UserID > 0 {
|
||||
@ -1070,20 +1398,32 @@ func (p *Plugin) handleChannelOutput(args map[string]interface{}) (interface{},
|
||||
}
|
||||
return p.napcat("send_private_msg", msg)
|
||||
|
||||
case "image":
|
||||
msg := map[string]interface{}{"message": fmt.Sprintf("[CQ:image,file=%s]", payload)}
|
||||
if groupID != 0 {
|
||||
msg["group_id"] = groupID
|
||||
} else {
|
||||
msg["user_id"] = userID
|
||||
case "image", "file":
|
||||
// 收敛到 output 通道:payload 支持本地路径或 http(s) URL。
|
||||
// 本地路径拷入 NapCat 共享目录转 file:// URI(与 voice 分支同模式),
|
||||
// 此后 agent 发本地文件不再需要单独的 upload_group_file 工具。
|
||||
uri := payload
|
||||
if !strings.HasPrefix(payload, "http://") && !strings.HasPrefix(payload, "https://") &&
|
||||
!strings.HasPrefix(payload, "file://") {
|
||||
if _, err := os.Stat(payload); err != nil {
|
||||
return nil, fmt.Errorf("%s 文件不存在: %s", rawType, payload)
|
||||
}
|
||||
os.MkdirAll(p.remoteDir, 0755)
|
||||
dest := filepath.Join(p.remoteDir, sanitizeFilename(filepath.Base(payload)))
|
||||
data, err := os.ReadFile(payload)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取文件失败: %w", err)
|
||||
}
|
||||
if err := os.WriteFile(dest, data, 0644); err != nil {
|
||||
return nil, fmt.Errorf("写入共享目录失败: %w", err)
|
||||
}
|
||||
uri = "file:///app/files/" + filepath.Base(dest)
|
||||
}
|
||||
if groupID != 0 {
|
||||
return p.napcat("send_group_msg", msg)
|
||||
cqTag := "file"
|
||||
if rawType == "image" {
|
||||
cqTag = "image"
|
||||
}
|
||||
return p.napcat("send_private_msg", msg)
|
||||
|
||||
case "file":
|
||||
msg := map[string]interface{}{"message": fmt.Sprintf("[CQ:file,file=%s]", payload)}
|
||||
msg := map[string]interface{}{"message": fmt.Sprintf("[CQ:%s,file=%s]", cqTag, uri)}
|
||||
if groupID != 0 {
|
||||
msg["group_id"] = groupID
|
||||
} else {
|
||||
@ -1191,6 +1531,31 @@ func (p *Plugin) handleSendFile(args map[string]interface{}) (interface{}, error
|
||||
return p.napcat("send_private_msg", params)
|
||||
}
|
||||
|
||||
func (p *Plugin) handleListChats(args map[string]interface{}) (interface{}, error) {
|
||||
count := 10
|
||||
if c, err := convInt64(args["count"]); err == nil && c > 0 && c < 100 {
|
||||
count = int(c)
|
||||
}
|
||||
chats := p.listChats(count)
|
||||
return map[string]interface{}{
|
||||
"chats": chats,
|
||||
"total": len(chats),
|
||||
"hint": "按最新消息先后排序;unread 为该会话未读消息数,处理完用 qq_mark_read 清零;用 qq_get_history(group_id/user_id) 拉取会话内容",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *Plugin) handleMarkRead(args map[string]interface{}) (interface{}, error) {
|
||||
if gid, err := convInt64(args["group_id"]); err == nil {
|
||||
p.markChatRead(gid)
|
||||
return map[string]interface{}{"status": "ok", "group_id": gid, "unread": 0}, nil
|
||||
}
|
||||
if uid, err := convInt64(args["user_id"]); err == nil {
|
||||
p.markChatRead(uid)
|
||||
return map[string]interface{}{"status": "ok", "user_id": uid, "unread": 0}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("need group_id or user_id")
|
||||
}
|
||||
|
||||
func (p *Plugin) handleGetHistory(args map[string]interface{}) (interface{}, error) {
|
||||
gid, gerr := convInt64(args["group_id"])
|
||||
uid, uerr := convInt64(args["user_id"])
|
||||
@ -1329,6 +1694,12 @@ func (p *Plugin) handleGetHistory(args map[string]interface{}) (interface{}, err
|
||||
if len(files) > 0 {
|
||||
result["files"] = files
|
||||
}
|
||||
// 拉取过某会话历史即视为已读(与真人客户端一致:看过=已读)
|
||||
if gerr == nil {
|
||||
p.markChatRead(gid)
|
||||
} else if uerr == nil {
|
||||
p.markChatRead(uid)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@ -1638,7 +2009,8 @@ func (p *Plugin) handleGetGroupFiles(args map[string]interface{}) (interface{},
|
||||
return resp, nil
|
||||
}
|
||||
dlURL := parsed.Data.URL
|
||||
httpResp, err := http.Get(dlURL)
|
||||
client := &http.Client{Timeout: 120 * time.Second}
|
||||
httpResp, err := client.Get(dlURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("download: %w", err)
|
||||
}
|
||||
@ -1693,6 +2065,11 @@ func (p *Plugin) handleDownloadFile(args map[string]interface{}) (interface{}, e
|
||||
task := p.addDownloadTask(fileID, filename)
|
||||
|
||||
go func(t *DownloadTask, fid, fname, furl string, gid, uid int64) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("[qq] download task %s panic: %v", fid, r)
|
||||
}
|
||||
}()
|
||||
savePath := ""
|
||||
errMsg := ""
|
||||
if furl != "" {
|
||||
|
||||
153
example/recoverydiag/diag_test.go
Normal file
153
example/recoverydiag/diag_test.go
Normal file
@ -0,0 +1,153 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var realCfg = "/home/newqqagent/config.db"
|
||||
var realLog = "/home/newqqagent/log"
|
||||
|
||||
func TestDiagTriage(t *testing.T) {
|
||||
p := &Plugin{name: "recoverydiag"}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
args map[string]interface{}
|
||||
want string
|
||||
}{
|
||||
{"signal", map[string]interface{}{"exit_code": 0, "signal": "SIGSEGV"}, "process_death"},
|
||||
{"oom", map[string]interface{}{"exit_code": 0, "signal": "SIGKILL", "crash_reason": "oom-kill"}, "process_starvation"},
|
||||
{"nonzero", map[string]interface{}{"exit_code": 1}, "process_death"},
|
||||
{"healthy", map[string]interface{}{"exit_code": 0}, "normal_stop"},
|
||||
{"alive", map[string]interface{}{"still_alive": true, "signal": "SIGKILL"}, "config_unreachable"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
r, _ := p.handleTriage(c.args)
|
||||
m, ok := r.(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("%s: not a map", c.name)
|
||||
}
|
||||
if got, _ := m["class"].(string); got != c.want {
|
||||
t.Errorf("%s: class = %q, want %q", c.name, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiagDB(t *testing.T) {
|
||||
if _, err := os.Stat(realCfg); err != nil {
|
||||
t.Skip("config.db not present, skipping")
|
||||
}
|
||||
p := &Plugin{name: "recoverydiag"}
|
||||
r, err := p.handleDB(map[string]interface{}{"db_path": realCfg})
|
||||
if err != nil {
|
||||
t.Fatalf("handleDB: %v", err)
|
||||
}
|
||||
m := r.(map[string]interface{})
|
||||
t.Logf("integrity=%v sources=%v verdict=%v summary=%v", m["integrity"], m["source_count"], m["verdict"], m["summary"])
|
||||
if m["integrity"] != "ok" {
|
||||
t.Errorf("integrity = %v, want ok", m["integrity"])
|
||||
}
|
||||
if m["source_count"] == 0 {
|
||||
t.Errorf("source_count == 0, expected LLM sources")
|
||||
}
|
||||
if got, _ := m["source_failed"].(int); got != 0 {
|
||||
t.Errorf("source_failed = %d, want 0 (all sources OK): %v", got, m["missing_fields"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiagLogScan(t *testing.T) {
|
||||
if _, err := os.Stat(realLog); err != nil {
|
||||
t.Skip("log dir not present, skipping")
|
||||
}
|
||||
p := &Plugin{name: "recoverydiag"}
|
||||
r, err := p.handleLogScan(map[string]interface{}{
|
||||
"log_dir": realLog,
|
||||
"since_minutes": 60 * 24 * 3,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("handleLogScan: %v", err)
|
||||
}
|
||||
m := r.(map[string]interface{})
|
||||
t.Logf("matched=%v counts=%v dominant=%v conclusion=%v", m["lines_matched"], m["counts"], m["dominant"], m["conclusion"])
|
||||
}
|
||||
|
||||
func TestDiagDelta(t *testing.T) {
|
||||
base := t.TempDir()
|
||||
cur := t.TempDir()
|
||||
sub := filepath.Join(base, "sub")
|
||||
os.MkdirAll(sub, 0755)
|
||||
|
||||
// modified: same path, different content
|
||||
os.WriteFile(filepath.Join(base, "a.txt"), []byte("hello"), 0644)
|
||||
os.WriteFile(filepath.Join(cur, "a.txt"), []byte("world!"), 0644)
|
||||
// created
|
||||
os.WriteFile(filepath.Join(cur, "b.txt"), []byte("new"), 0644)
|
||||
// deleted
|
||||
os.WriteFile(filepath.Join(base, "gone.txt"), []byte("bye"), 0644)
|
||||
// unchanged
|
||||
os.WriteFile(filepath.Join(base, "same.txt"), []byte("x"), 0644)
|
||||
os.WriteFile(filepath.Join(cur, "same.txt"), []byte("x"), 0644)
|
||||
|
||||
p := &Plugin{name: "recoverydiag"}
|
||||
r, err := p.handleDelta(map[string]interface{}{"baseline_dir": base, "current_dir": cur})
|
||||
if err != nil {
|
||||
t.Fatalf("handleDelta: %v", err)
|
||||
}
|
||||
m := r.(map[string]interface{})
|
||||
sum := m["summary"].(map[string]int)
|
||||
t.Logf("summary=%v total=%v", sum, m["total_diff"])
|
||||
if sum["created"] != 1 || sum["deleted"] != 1 || sum["modified"] != 1 {
|
||||
t.Errorf("summary = %v, want modified=1 created=1 deleted=1", sum)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiagLoc(t *testing.T) {
|
||||
p := &Plugin{name: "recoverydiag"}
|
||||
r, _ := p.handleLoc(map[string]interface{}{
|
||||
"triage": map[string]interface{}{"class": "process_death", "verdict": "down"},
|
||||
"db": map[string]interface{}{"verdict": "ok"},
|
||||
"log_scan": map[string]interface{}{"dominant": "panic"},
|
||||
"delta": map[string]interface{}{"summary": map[string]interface{}{"created": 0, "modified": 0, "deleted": 0}},
|
||||
})
|
||||
m := r.(map[string]interface{})
|
||||
// 经 JSON 往返,模拟内核把子结论以 JSON 传给 diag_loc 的真实路径
|
||||
raw, _ := json.Marshal(m)
|
||||
var dec map[string]interface{}
|
||||
json.Unmarshal(raw, &dec)
|
||||
hs := dec["ranked_hypotheses"].([]interface{})
|
||||
if len(hs) == 0 {
|
||||
t.Fatal("no hypotheses")
|
||||
}
|
||||
top := hs[0].(map[string]interface{})
|
||||
t.Logf("top cause=%v conf=%v rec=%v", top["cause"], top["confidence"], top["recommendation"])
|
||||
if top["cause"] != "code_panic_loop" {
|
||||
t.Errorf("expected code_panic_loop, got %v", top["cause"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiagLocPersist(t *testing.T) {
|
||||
kb := filepath.Join(t.TempDir(), "recovery_kb")
|
||||
p := &Plugin{name: "recoverydiag", dataDir: filepath.Dir(kb)}
|
||||
args := map[string]interface{}{
|
||||
"persist": true,
|
||||
"triage": map[string]interface{}{"class": "process_death", "verdict": "down"},
|
||||
"db": map[string]interface{}{"verdict": "ok"},
|
||||
"log_scan": map[string]interface{}{"dominant": "panic"},
|
||||
"delta": map[string]interface{}{"summary": map[string]interface{}{"created": 0, "modified": 0, "deleted": 0}},
|
||||
}
|
||||
if _, err := p.handleLoc(args); err != nil {
|
||||
t.Fatalf("handleLoc: %v", err)
|
||||
}
|
||||
entries, err := os.ReadDir(kb)
|
||||
if err != nil || len(entries) == 0 {
|
||||
t.Fatalf("expected persisted diag json, got err=%v entries=%v", err, entries)
|
||||
}
|
||||
data, _ := os.ReadFile(filepath.Join(kb, entries[0].Name()))
|
||||
if !strings.Contains(string(data), `"cause"`) {
|
||||
t.Errorf("persisted file missing cause field: %s", data)
|
||||
}
|
||||
}
|
||||
7
example/recoverydiag/go.mod
Normal file
7
example/recoverydiag/go.mod
Normal file
@ -0,0 +1,7 @@
|
||||
module recoverydiag
|
||||
|
||||
go 1.25.0
|
||||
|
||||
require gitcode.com/JianFeeeee/homeagent-sdk v0.0.0
|
||||
|
||||
replace gitcode.com/JianFeeeee/homeagent-sdk => ../../
|
||||
11
example/recoverydiag/main.go
Normal file
11
example/recoverydiag/main.go
Normal file
@ -0,0 +1,11 @@
|
||||
//go:build !windows || !cgo
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
sdk "gitcode.com/JianFeeeee/homeagent-sdk/sdk"
|
||||
)
|
||||
|
||||
func NewPlugin(name string, config map[string]interface{}) (sdk.Plugin, error) {
|
||||
return NewPluginFactory(name, config)
|
||||
}
|
||||
21
example/recoverydiag/plg.json
Normal file
21
example/recoverydiag/plg.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "recoverydiag",
|
||||
"name_zh": "恢复诊断",
|
||||
"name_en": "Recovery Diagnostics",
|
||||
"version": "0.2.0",
|
||||
"description": "快速检查/崩溃取证工具集:diag_triage(退出码/信号/存活粗分)、diag_db(config.db 完整性 + LLM 源解析校验)、diag_log_scan(日志签名命中)、diag_delta(last-good 快照 vs 现状 diff)、diag_loc(正交综合定位)。全部返回结论而非原文,确定性、不消耗 LLM token,供 guard / failback 恢复决策使用。",
|
||||
"author": "HomeAgent",
|
||||
"entry": "plugin.so",
|
||||
"tags": [
|
||||
"diag",
|
||||
"recovery",
|
||||
"diagnostics",
|
||||
"triage",
|
||||
"failback"
|
||||
],
|
||||
"targets": "linux/amd64",
|
||||
"outdir": "dist",
|
||||
"bundle": true,
|
||||
"replaces": {},
|
||||
"source_dirs": []
|
||||
}
|
||||
1023
example/recoverydiag/plugin.go
Normal file
1023
example/recoverydiag/plugin.go
Normal file
File diff suppressed because it is too large
Load Diff
@ -2,14 +2,19 @@
|
||||
"name": "rss",
|
||||
"name_zh": "RSS订阅",
|
||||
"name_en": "RSS",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"description": "RSS/Atom 订阅监控插件,自动检测更新并推送通知",
|
||||
"author": "HomeAgent",
|
||||
"entry": "plugin.so",
|
||||
"tags": ["rss", "feed", "subscription", "monitor"],
|
||||
"tags": [
|
||||
"rss",
|
||||
"feed",
|
||||
"subscription",
|
||||
"monitor"
|
||||
],
|
||||
"targets": "linux/amd64",
|
||||
"outdir": "dist",
|
||||
"bundle": true,
|
||||
"replaces": {},
|
||||
"source_dirs": []
|
||||
}
|
||||
}
|
||||
@ -467,7 +467,7 @@ func (p *Plugin) saveData() {
|
||||
SeenGUIDs: p.seenGUIDs,
|
||||
}
|
||||
b, _ := json.MarshalIndent(data, "", " ")
|
||||
os.WriteFile(p.dataFile(), b, 0644)
|
||||
atomicWriteJSON(p.dataFile(), b)
|
||||
}
|
||||
|
||||
// cleanupData 卸载时清理订阅数据目录(feeds.json 等)
|
||||
@ -486,3 +486,12 @@ func (p *Plugin) cleanupData() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
// atomicWriteJSON 原子写 JSON:先写临时文件再 rename,避免进程崩溃截断数据文件。
|
||||
func atomicWriteJSON(path string, data []byte) error {
|
||||
tmp := path + ".tmp"
|
||||
if err := os.WriteFile(tmp, data, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmp, path)
|
||||
}
|
||||
|
||||
129
meta/meta.go
129
meta/meta.go
@ -1,12 +1,39 @@
|
||||
// Package meta 收集 HomeAgent SDK 的全部元数据。
|
||||
// 版本号应与核心 meta.Version 保持一致。
|
||||
// ABI 版本与 Dispatch Method ID 应与核心仓 internal/meta/meta.go 保持一致。
|
||||
package meta
|
||||
|
||||
var (
|
||||
// Version 是 HomeAgent SDK 版本号。
|
||||
// 通过 `-ldflags="-X gitcode.com/JianFeeeee/homeagent-sdk/meta.Version=vX.Y.Z"` 注入。
|
||||
Version = "0.9.1"
|
||||
//
|
||||
// 版本号语义:**SDK 版本跟随核心的中版本,patch 位恒为 .0**。
|
||||
// 整条核心 1.1.x 线(1.1.0、1.1.1、1.1.7…)共用 SDK 1.1.0;
|
||||
// 只有核心进入 1.2.0 这种中版本跃迁时 SDK 才升到 1.2.0。
|
||||
// 这样插件开发者只需关心「我在为哪个中版本写插件」,
|
||||
// 不必跟着核心的每个 bugfix 换 SDK 依赖(见 核心仓 docs/git-branching.md §七)。
|
||||
//
|
||||
// 1.0.0:插件运行模型从 C ABI 动态库改为子进程 + 共享内存。
|
||||
// 公开 SDK 接口零改动,但产物形态变了(plugin.so → plugin.bin)。
|
||||
// 1.1.0:多模态贯通插件边界。**全部是新增,无签名变更**:
|
||||
// - Triple.SentenceText / Triple.MediaDigests
|
||||
// - Doc.MediaDigests / Doc.Attachments、MediaAttachment
|
||||
// - TextEvent.Attachments
|
||||
// - DocMemoryAPI.InsertWithMedia
|
||||
// - IOInjector 的 InjectInputMedia / InjectInputMediaSync /
|
||||
// InjectInterruptMedia;PluginSDK 补上缺失的 SetToolBlocks 包装
|
||||
// 同版修掉两处并发竞态(sdk/stress_test.go 的 -race 实证,不是理论风险):
|
||||
// PluginSDK 的 API 字段与 autoRestart 标志此前无锁,而写方
|
||||
// (内核注入 API、插件 SetAutoRestart)与读方(插件后台 goroutine
|
||||
// 注入、内核 registry 读 AutoRestart)天然跨 goroutine。
|
||||
// 存量插件不需要改一行也不需要重编:新增方法由**插件调用、内核实现**,
|
||||
// 不调就不受影响。想用新字段的插件重编即可。
|
||||
//
|
||||
// ❗发布分支上此值是**本条发布线的 SDK 定版**;main 上则是下一个未发布中版本
|
||||
// (见 核心仓 docs/git-branching.md §2.1 与 §七.1)。
|
||||
//
|
||||
// 本分支定版 1.1.0,服务整条核心 1.1.x 线(1.1.0、1.1.1、…):
|
||||
// patch 位恒为 .0,核心的 bugfix 不碰公开接口,SDK 号没有理由跟着动。
|
||||
Version = "1.1.0"
|
||||
|
||||
// Commit 是构建时的 Git commit hash。
|
||||
Commit = "unknown"
|
||||
@ -21,7 +48,15 @@ var (
|
||||
CoreModule = "gitcode.com/JianFeeeee/HomeAgent"
|
||||
|
||||
// CoreVersion 是此 SDK 所兼容的最低核心版本。
|
||||
CoreVersion = "0.9.1"
|
||||
//
|
||||
// 1.0.0 是硬下限而非建议值:0.9.x 内核只会 dlopen `.so`,
|
||||
// 本版工具链产出的 `plugin.bin` 在旧内核上根本不会被识别。
|
||||
//
|
||||
// ⚠️ 1.1.0 新增的媒体接口需要核心 **1.1.1+**(更早的核心没有
|
||||
// doc.insertWithMedia / io.injectMedia* 这些 RPC,调用会返回 unknown method)。
|
||||
// 这里仍写 1.0.0,因为它是「SDK 能在其上运行」的下限;
|
||||
// 媒体接口是可选能力,不用就不受影响。
|
||||
CoreVersion = "1.0.0"
|
||||
)
|
||||
|
||||
// FullVersion 返回完整的版本字符串。
|
||||
@ -29,79 +64,15 @@ func FullVersion() string {
|
||||
return SDKName + " v" + Version + " (" + Commit + ")"
|
||||
}
|
||||
|
||||
// ---- ABI 版本(与核心仓 internal/meta/meta.go 同步) ----
|
||||
// ABI 标识版本直接取内核版本号字符串(semver),与核心 Version 保持一致,不使用独立数字编码。
|
||||
// 协商层(C 结构体 int version 字段)使用 CABINum:由版本字符串派生的整数(major*100 + minor)。
|
||||
// 映射:v0.8.x → CABINum=800;v0.9.x → CABINum=900(invoke_stage 写回)。
|
||||
// 小版本(patch)演进不影响 ABI,CABINum 不变。version_min 保证旧 ABI 插件仍可加载。
|
||||
|
||||
var (
|
||||
// ABIVersion 是 ABI 标识版本(字符串 semver,与 SDK CoreVersion 对齐)。
|
||||
ABIVersion = CoreVersion
|
||||
// ABIVersionMin 是兼容的最低 ABI 标识版本。
|
||||
ABIVersionMin = "0.8.0"
|
||||
)
|
||||
|
||||
const (
|
||||
// CABINum 是 C 层协商用的整数版本(major*100 + minor),随 ABIVersion 派生。
|
||||
CABINum = 900
|
||||
// CABINumMin 是 C 层兼容的最低整数版本。
|
||||
// 旧工具链(v0.8 之前)写入的整数 version=1,无写回能力但与新内核结构兼容,
|
||||
// 因此最小值保持 1 以兼容全部旧插件(新插件 900 匹配,旧插件 1/2 通过);
|
||||
// 仅当未来内核 ABI 破坏兼容时才提高该值。
|
||||
CABINumMin = 1
|
||||
)
|
||||
|
||||
// ---- Dispatch Method IDs(与核心仓 internal/meta/meta.go 同步) ----
|
||||
const (
|
||||
CoreRegisterTool = 1
|
||||
CoreRegisterStage = 2
|
||||
CoreRegisterOutputCh = 3
|
||||
CoreRegisterPluginAPI = 4
|
||||
CoreInjectText = 5
|
||||
CoreInjectInterruptText = 6
|
||||
CoreInjectTextNoMemory = 7
|
||||
CoreSetAutoRestart = 8
|
||||
CoreMemoryRecall = 9
|
||||
CoreMemoryCommit = 10
|
||||
CoreMemoryIntrospect = 11
|
||||
CoreMemoryMerge = 12
|
||||
CoreMemoryPurge = 13
|
||||
CoreDocQuery = 14
|
||||
CoreKnowledgeSearch = 15
|
||||
CoreSettingsGet = 16
|
||||
CoreSettingsSet = 17
|
||||
CoreSettingsRegisterDef = 18
|
||||
CoreLLMListSources = 19
|
||||
CoreLLMSetSource = 20
|
||||
CoreSocialGetPerson = 21
|
||||
CoreSocialGetNetwork = 22
|
||||
CoreSubscribe = 23
|
||||
CoreUnsubscribe = 24
|
||||
CoreFreeString = 25
|
||||
CoreSettingsGetCore = 26
|
||||
CoreSettingsSetCore = 27
|
||||
CoreSettingsListCore = 28
|
||||
CoreSettingsGetPlugin = 29
|
||||
CoreSettingsSetPlugin = 30
|
||||
CoreSettingsListPlugin = 31
|
||||
CoreDocInsert = 32
|
||||
CoreDocRemove = 33
|
||||
CoreDocStats = 34
|
||||
CoreKnowledgeAdd = 35
|
||||
CoreKnowledgeList = 36
|
||||
CoreLLMCurrentSource = 37
|
||||
CoreSocialGetTrait = 38
|
||||
CoreSocialGetRelations = 39
|
||||
CoreSocialListPersons = 40
|
||||
CoreTextMemoryAppend = 41
|
||||
CoreSettingsList = 42
|
||||
CoreSettingsDefs = 43
|
||||
CoreSettingsDump = 44
|
||||
CoreSettingsPlugins = 45
|
||||
CoreRegisterInputCh = 46
|
||||
CoreInjectInputSync = 47
|
||||
CorePluginReloadOne = 48
|
||||
CorePluginListLoaded = 49
|
||||
CorePluginIsDisabled = 50
|
||||
)
|
||||
// ---- 协议版本 ----
|
||||
//
|
||||
// 子进程 RPC 的协议版本是一个独立的小整数,与 SDK/内核语义版本解耦:
|
||||
// 语义版本变动频繁(修 bug、加字段),而 wire 协议只在**帧格式或握手语义**
|
||||
// 变化时才升。当前值见核心仓 internal/plugin/proc/protocol.go 的 ProtocolVersion。
|
||||
//
|
||||
// C ABI 时代的 ABIVersion / CABINum / 51 个 Core<Method> 整数 ID 已随
|
||||
// Part 6.2 删除 internal/plugin/cabi/ 一并退场:
|
||||
// - 整数 method id 平移为 method 名字符串(proc/protocol.go 的 Method* 常量)
|
||||
// - 版本协商改为握手帧里的 protocol 字段
|
||||
//
|
||||
// 保留那些常量只会让人以为它们还在生效。
|
||||
|
||||
@ -25,13 +25,18 @@ type Relation struct {
|
||||
}
|
||||
|
||||
// Triple represents a subject-relation-object triple for the knowledge graph.
|
||||
//
|
||||
// SentenceText 是这条三元组的原句,会写进 sentences 表;媒体引用挂在句子上,
|
||||
// 所以 MediaDigests 非空时内核会保证句子存在(不给就自动合成一句)。
|
||||
type Triple struct {
|
||||
Subject string `json:"subject"`
|
||||
Relation string `json:"relation"`
|
||||
Object string `json:"object"`
|
||||
Confidence float64 `json:"confidence,omitempty"`
|
||||
SubjectType string `json:"subject_type,omitempty"`
|
||||
ObjectType string `json:"object_type,omitempty"`
|
||||
Subject string `json:"subject"`
|
||||
Relation string `json:"relation"`
|
||||
Object string `json:"object"`
|
||||
Confidence float64 `json:"confidence,omitempty"`
|
||||
SubjectType string `json:"subject_type,omitempty"`
|
||||
ObjectType string `json:"object_type,omitempty"`
|
||||
SentenceText string `json:"sentence_text,omitempty"`
|
||||
MediaDigests []string `json:"media_digests,omitempty"`
|
||||
}
|
||||
|
||||
// TextMemoryAPI provides access to chronological text event storage.
|
||||
@ -40,27 +45,52 @@ type TextMemoryAPI interface {
|
||||
}
|
||||
|
||||
// TextEvent represents a single text memory event.
|
||||
// MediaAttachment 描述一份与记忆关联的媒体。
|
||||
//
|
||||
// 两个方向共用一个类型:
|
||||
// - 写入(InsertWithMedia):给 Data + MIME 就是新内容;只给 Digest 则是引用已有内容。
|
||||
// - 读出(Query):内核只填 Digest/MIME/Description,**不回 Data**——
|
||||
// 一次检索可能命中几十张图,把字节全塞回插件会把 ABI 消息撑爆。
|
||||
// 需要字节时拿 Digest 单独取。
|
||||
type MediaAttachment struct {
|
||||
Digest string `json:"digest,omitempty"`
|
||||
MIME string `json:"mime,omitempty"`
|
||||
Data []byte `json:"data,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
}
|
||||
|
||||
type TextEvent struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
Channel string `json:"channel,omitempty"`
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
Channel string `json:"channel,omitempty"`
|
||||
Attachments []MediaAttachment `json:"attachments,omitempty"`
|
||||
}
|
||||
|
||||
// DocMemoryAPI provides access to the document vector store.
|
||||
type DocMemoryAPI interface {
|
||||
Query(text string, topK int) []*Doc
|
||||
Insert(doc *Doc) error
|
||||
// InsertWithMedia 写入文档并关联媒体。attachments 里带 Data 的会落进
|
||||
// 内容寻址存储(相同字节只存一份),只带 Digest 的直接引用已有内容。
|
||||
// 插件无需自己拼标记:内核会把 `[mime <短 digest>] <描述>` 补进 Content,
|
||||
// 让向量检索和后续蒸馏都能看到这份媒体。
|
||||
InsertWithMedia(doc *Doc, attachments []MediaAttachment) error
|
||||
Remove(id string)
|
||||
Stats() map[string]interface{}
|
||||
}
|
||||
|
||||
// Doc represents a document in the document store.
|
||||
//
|
||||
// MediaDigests / Attachments 在 Query 返回时由内核填充(仅元数据,不带字节)。
|
||||
type Doc struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
Score float64 `json:"score,omitempty"`
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
Score float64 `json:"score,omitempty"`
|
||||
MediaDigests []string `json:"media_digests,omitempty"`
|
||||
Attachments []MediaAttachment `json:"attachments,omitempty"`
|
||||
}
|
||||
|
||||
// SocialAPI provides read-only access to the social graph (person profiles and relationships).
|
||||
@ -75,9 +105,9 @@ type SocialAPI interface {
|
||||
|
||||
// PersonProfile represents a person's complete profile (traits + social relations).
|
||||
type PersonProfile struct {
|
||||
Name string `json:"name"`
|
||||
Traits map[string]string `json:"traits,omitempty"`
|
||||
Relations []SocialRelation `json:"relations,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Traits map[string]string `json:"traits,omitempty"`
|
||||
Relations []SocialRelation `json:"relations,omitempty"`
|
||||
}
|
||||
|
||||
// SocialRelation represents a social relationship between two persons.
|
||||
|
||||
279
sdk/plugin.go
279
sdk/plugin.go
@ -61,14 +61,18 @@ type StageContext struct {
|
||||
Memory []MemItem
|
||||
NoMemory bool
|
||||
Extra map[string]interface{}
|
||||
Errors []string // 阶段处理过程中的错误信息
|
||||
Errors []string // 阶段处理过程中的错误信息
|
||||
}
|
||||
|
||||
func (c *StageContext) RLock() { c.mu.RLock() }
|
||||
func (c *StageContext) RUnlock() { c.mu.RUnlock() }
|
||||
func (c *StageContext) Lock() { c.mu.Lock() }
|
||||
func (c *StageContext) Unlock() { c.mu.Unlock() }
|
||||
func (c *StageContext) IsResponded() bool { c.mu.RLock(); defer c.mu.RUnlock(); return c.Response != nil }
|
||||
func (c *StageContext) RLock() { c.mu.RLock() }
|
||||
func (c *StageContext) RUnlock() { c.mu.RUnlock() }
|
||||
func (c *StageContext) Lock() { c.mu.Lock() }
|
||||
func (c *StageContext) Unlock() { c.mu.Unlock() }
|
||||
func (c *StageContext) IsResponded() bool {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.Response != nil
|
||||
}
|
||||
|
||||
// MemItem represents a memory item in stage context.
|
||||
type MemItem struct {
|
||||
@ -100,8 +104,8 @@ type ToolDef struct {
|
||||
Plugin string `json:"plugin,omitempty"`
|
||||
Description string `json:"description"`
|
||||
Parameters map[string]interface{} `json:"parameters"`
|
||||
NoMemory bool `json:"no_memory,omitempty"` // 此工具输出不参与记忆计算,但原文保留
|
||||
Cleaner func(string) string `json:"-"` // 计算层过滤函数,不改原文;仅在向量化/jieba/蒸馏时调用
|
||||
NoMemory bool `json:"no_memory,omitempty"` // 此工具输出不参与记忆计算,但原文保留
|
||||
Cleaner func(string) string `json:"-"` // 计算层过滤函数,不改原文;仅在向量化/jieba/蒸馏时调用
|
||||
}
|
||||
|
||||
// IOInjector provides methods for injecting input and interrupts into the agent pipeline.
|
||||
@ -114,6 +118,12 @@ type IOInjector interface {
|
||||
// InjectInputSync 注入输入事件并同步等待 agent 回复,返回回复文本(无回复时返回空串)。
|
||||
// 用于通道消息的完整闭环:收到入站 → agent 处理 → 回复取回 → 送回通道。
|
||||
InjectInputSync(source, channel, text string) string
|
||||
// SetToolBlocks 插件工具注入多模态内容块(image_url/audio_url),内核在下一条
|
||||
// tool message 的 content 数组里带上这些块,让模型在后续轮次看到图/听到音频。
|
||||
SetToolBlocks(blocks []ContentBlock)
|
||||
InjectInputMedia(source, channel, text string, blocks []ContentBlock)
|
||||
InjectInputMediaSync(source, channel, text string, blocks []ContentBlock) string
|
||||
InjectInterruptMedia(source, channel, text string, blocks []ContentBlock)
|
||||
}
|
||||
|
||||
// EventType identifies the kind of system event.
|
||||
@ -218,6 +228,24 @@ type PluginSDK struct {
|
||||
events EventSubscriber
|
||||
plgMgr PluginMgrAPI
|
||||
|
||||
// apiMu 保护上面这些由内核注入的 API 字段,以及 autoRestart。
|
||||
//
|
||||
// 这些字段的写方与读方天然跨 goroutine:
|
||||
// - 写方是内核(加载/重载插件时注入 API)与插件自己(SetAutoRestart);
|
||||
// - 读方是插件在 Start() 里起的后台 goroutine(轮询、监听、定时器
|
||||
// 都要拿 injector 往管道里注消息),以及内核 registry —— 它在
|
||||
// 另一个 goroutine 读 AutoRestart() 决定崩溃后是否重启。
|
||||
// SetAutoRestart 的文档用法本身就是「连接建立后再决定能否自动重启」,
|
||||
// 而连接建立通常发生在后台 goroutine 里,于是这对读写必然并发。
|
||||
//
|
||||
// sdk/stress_test.go 的 -race 实测确认这是真竞态,不是理论风险。
|
||||
// 未加锁时的生产表现是偶发 nil 解引用崩溃(读到半个接口值)。
|
||||
//
|
||||
// 约定:只在持锁期间取字段值,取完立刻释放再调用。
|
||||
// 持锁调用会把 InjectInputSync 这类阻塞到 agent 回复(可达数分钟)的
|
||||
// 方法与 SetIOInjector 串到一起,让插件重载卡死。
|
||||
apiMu sync.RWMutex
|
||||
|
||||
autoRestart bool
|
||||
|
||||
stopMu sync.Mutex
|
||||
@ -244,28 +272,57 @@ func New(name string, sett SettingsAPI, regTool ToolRegistrar, regStage StageReg
|
||||
func (s *PluginSDK) PluginName() string { return s.name }
|
||||
|
||||
// Settings returns the settings API for reading/writing plugin configuration.
|
||||
// sett 在 New 时一次性写入且无 setter,故不需要加锁。
|
||||
func (s *PluginSDK) Settings() SettingsAPI { return s.sett }
|
||||
|
||||
// Memory returns the graph memory API (may be nil if not available).
|
||||
func (s *PluginSDK) Memory() MemoryAPI { return s.mem }
|
||||
func (s *PluginSDK) Memory() MemoryAPI {
|
||||
s.apiMu.RLock()
|
||||
defer s.apiMu.RUnlock()
|
||||
return s.mem
|
||||
}
|
||||
|
||||
// TextMemory returns the text memory API (may be nil if not available).
|
||||
func (s *PluginSDK) TextMemory() TextMemoryAPI { return s.textMem }
|
||||
func (s *PluginSDK) TextMemory() TextMemoryAPI {
|
||||
s.apiMu.RLock()
|
||||
defer s.apiMu.RUnlock()
|
||||
return s.textMem
|
||||
}
|
||||
|
||||
// DocMemory returns the document memory API (may be nil if not available).
|
||||
func (s *PluginSDK) DocMemory() DocMemoryAPI { return s.docMem }
|
||||
func (s *PluginSDK) DocMemory() DocMemoryAPI {
|
||||
s.apiMu.RLock()
|
||||
defer s.apiMu.RUnlock()
|
||||
return s.docMem
|
||||
}
|
||||
|
||||
// Knowledge returns the knowledge store API (may be nil if not available).
|
||||
func (s *PluginSDK) Knowledge() KnowledgeAPI { return s.know }
|
||||
func (s *PluginSDK) Knowledge() KnowledgeAPI {
|
||||
s.apiMu.RLock()
|
||||
defer s.apiMu.RUnlock()
|
||||
return s.know
|
||||
}
|
||||
|
||||
// LLM returns the LLM provider API (may be nil if not available).
|
||||
func (s *PluginSDK) LLM() LLMAPI { return s.llm }
|
||||
func (s *PluginSDK) LLM() LLMAPI {
|
||||
s.apiMu.RLock()
|
||||
defer s.apiMu.RUnlock()
|
||||
return s.llm
|
||||
}
|
||||
|
||||
// Social returns the social graph API (may be nil if not available).
|
||||
func (s *PluginSDK) Social() SocialAPI { return s.social }
|
||||
func (s *PluginSDK) Social() SocialAPI {
|
||||
s.apiMu.RLock()
|
||||
defer s.apiMu.RUnlock()
|
||||
return s.social
|
||||
}
|
||||
|
||||
// Events returns the event subscriber for listening to kernel events (may be nil if not available).
|
||||
func (s *PluginSDK) Events() EventSubscriber { return s.events }
|
||||
func (s *PluginSDK) Events() EventSubscriber {
|
||||
s.apiMu.RLock()
|
||||
defer s.apiMu.RUnlock()
|
||||
return s.events
|
||||
}
|
||||
|
||||
// RegisterTool registers a tool that the LLM can call.
|
||||
func (s *PluginSDK) RegisterTool(name string, def ToolDef, handler ToolHandler) error {
|
||||
@ -279,8 +336,9 @@ func (s *PluginSDK) RegisterTool(name string, def ToolDef, handler ToolHandler)
|
||||
}
|
||||
|
||||
// RegisterStage registers a handler for a pipeline stage.
|
||||
// scope: StageScopeGlobal (default) — receives all stage events.
|
||||
// StageScopeOwnTools — only before_toolcall/after_toolcall for this plugin's tools.
|
||||
//
|
||||
// scope: StageScopeGlobal (default) — receives all stage events.
|
||||
// StageScopeOwnTools — only before_toolcall/after_toolcall for this plugin's tools.
|
||||
func (s *PluginSDK) RegisterStage(stage Stage, handler StageHandler, scope ...StageScope) {
|
||||
if s.regStage == nil {
|
||||
return
|
||||
@ -330,8 +388,11 @@ func (s *PluginSDK) RegisterPluginAPI(name string) error {
|
||||
// def: 通道在记忆计算层的行为(NoMemory/Cleaner)
|
||||
// handler: receives args map with keys: payload (string), type (string), meta (string|optional)
|
||||
func (s *PluginSDK) RegisterOutputChannel(name string, caps int, desc string, def ChannelDef, handler ToolHandler) error {
|
||||
if s.regOutput != nil {
|
||||
return s.regOutput(name, caps, desc, def, handler)
|
||||
s.apiMu.RLock()
|
||||
reg := s.regOutput
|
||||
s.apiMu.RUnlock()
|
||||
if reg != nil {
|
||||
return reg(name, caps, desc, def, handler)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -340,57 +401,126 @@ func (s *PluginSDK) RegisterOutputChannel(name string, caps int, desc string, de
|
||||
// def.NoMemory: 此通道输入不参与记忆计算
|
||||
// def.Cleaner: 计算层对输入文本清洗后(不改原文)再向量化/提关键词
|
||||
func (s *PluginSDK) RegisterInputChannel(name string, def ChannelDef) error {
|
||||
if s.regInput != nil {
|
||||
return s.regInput(name, def)
|
||||
s.apiMu.RLock()
|
||||
reg := s.regInput
|
||||
s.apiMu.RUnlock()
|
||||
if reg != nil {
|
||||
return reg(name, def)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 以下 setter 由内核在启动/重载时调用,与插件后台 goroutine 的读并发,故加锁。
|
||||
|
||||
// SetOutputChannelRegistrar sets the output channel registrar (called by the core at startup).
|
||||
func (s *PluginSDK) SetOutputChannelRegistrar(r OutputChannelRegistrar) { s.regOutput = r }
|
||||
func (s *PluginSDK) SetOutputChannelRegistrar(r OutputChannelRegistrar) {
|
||||
s.apiMu.Lock()
|
||||
s.regOutput = r
|
||||
s.apiMu.Unlock()
|
||||
}
|
||||
|
||||
// SetInputChannelRegistrar sets the input channel registrar (called by the core at startup).
|
||||
func (s *PluginSDK) SetInputChannelRegistrar(r InputChannelRegistrar) { s.regInput = r }
|
||||
func (s *PluginSDK) SetInputChannelRegistrar(r InputChannelRegistrar) {
|
||||
s.apiMu.Lock()
|
||||
s.regInput = r
|
||||
s.apiMu.Unlock()
|
||||
}
|
||||
|
||||
// SetIOInjector sets the IO injector (called by the core at startup).
|
||||
func (s *PluginSDK) SetIOInjector(io IOInjector) { s.io = io }
|
||||
func (s *PluginSDK) SetIOInjector(io IOInjector) {
|
||||
s.apiMu.Lock()
|
||||
s.io = io
|
||||
s.apiMu.Unlock()
|
||||
}
|
||||
|
||||
// SetMemoryAPI sets the memory API (called by the core at startup).
|
||||
func (s *PluginSDK) SetMemoryAPI(mem MemoryAPI) { s.mem = mem }
|
||||
func (s *PluginSDK) SetTextMemoryAPI(tm TextMemoryAPI) { s.textMem = tm }
|
||||
func (s *PluginSDK) SetDocMemoryAPI(dm DocMemoryAPI) { s.docMem = dm }
|
||||
func (s *PluginSDK) SetKnowledgeAPI(kn KnowledgeAPI) { s.know = kn }
|
||||
func (s *PluginSDK) SetLLMAPI(llm LLMAPI) { s.llm = llm }
|
||||
func (s *PluginSDK) SetSocialAPI(social SocialAPI) { s.social = social }
|
||||
func (s *PluginSDK) SetEventSubscriber(es EventSubscriber) { s.events = es }
|
||||
func (s *PluginSDK) SetMemoryAPI(mem MemoryAPI) {
|
||||
s.apiMu.Lock()
|
||||
s.mem = mem
|
||||
s.apiMu.Unlock()
|
||||
}
|
||||
|
||||
func (s *PluginSDK) SetTextMemoryAPI(tm TextMemoryAPI) {
|
||||
s.apiMu.Lock()
|
||||
s.textMem = tm
|
||||
s.apiMu.Unlock()
|
||||
}
|
||||
|
||||
func (s *PluginSDK) SetDocMemoryAPI(dm DocMemoryAPI) {
|
||||
s.apiMu.Lock()
|
||||
s.docMem = dm
|
||||
s.apiMu.Unlock()
|
||||
}
|
||||
|
||||
func (s *PluginSDK) SetKnowledgeAPI(kn KnowledgeAPI) {
|
||||
s.apiMu.Lock()
|
||||
s.know = kn
|
||||
s.apiMu.Unlock()
|
||||
}
|
||||
|
||||
func (s *PluginSDK) SetLLMAPI(llm LLMAPI) {
|
||||
s.apiMu.Lock()
|
||||
s.llm = llm
|
||||
s.apiMu.Unlock()
|
||||
}
|
||||
|
||||
func (s *PluginSDK) SetSocialAPI(social SocialAPI) {
|
||||
s.apiMu.Lock()
|
||||
s.social = social
|
||||
s.apiMu.Unlock()
|
||||
}
|
||||
|
||||
func (s *PluginSDK) SetEventSubscriber(es EventSubscriber) {
|
||||
s.apiMu.Lock()
|
||||
s.events = es
|
||||
s.apiMu.Unlock()
|
||||
}
|
||||
|
||||
// SetPluginMgrAPI sets the plugin manager API (called by the bridge at startup).
|
||||
func (s *PluginSDK) SetPluginMgrAPI(pm PluginMgrAPI) { s.plgMgr = pm }
|
||||
func (s *PluginSDK) SetPluginMgrAPI(pm PluginMgrAPI) {
|
||||
s.apiMu.Lock()
|
||||
s.plgMgr = pm
|
||||
s.apiMu.Unlock()
|
||||
}
|
||||
|
||||
// PluginMgr returns the plugin manager API (ReloadOne / ReloadPlugins / list).
|
||||
// May be nil if the host did not wire it.
|
||||
func (s *PluginSDK) PluginMgr() PluginMgrAPI { return s.plgMgr }
|
||||
func (s *PluginSDK) PluginMgr() PluginMgrAPI {
|
||||
s.apiMu.RLock()
|
||||
defer s.apiMu.RUnlock()
|
||||
return s.plgMgr
|
||||
}
|
||||
|
||||
// ---- IO Convenience Methods ----
|
||||
|
||||
// injector 取当前 injector 的快照。
|
||||
//
|
||||
// 取完即释放锁再调用:InjectInputSync 会阻塞到 agent 回复(可达数分钟),
|
||||
// 若持锁调用,插件重载时的 SetIOInjector 会一起卡住。
|
||||
func (s *PluginSDK) injector() IOInjector {
|
||||
s.apiMu.RLock()
|
||||
defer s.apiMu.RUnlock()
|
||||
return s.io
|
||||
}
|
||||
|
||||
// InjectInterruptText injects a text interrupt that can preempt current LLM processing.
|
||||
func (s *PluginSDK) InjectInterruptText(source, channel, text string) {
|
||||
if s.io != nil {
|
||||
s.io.InjectInterruptText(source, channel, text)
|
||||
if io := s.injector(); io != nil {
|
||||
io.InjectInterruptText(source, channel, text)
|
||||
}
|
||||
}
|
||||
|
||||
// InjectText injects a text message into the agent pipeline.
|
||||
func (s *PluginSDK) InjectText(source, channel, text string) {
|
||||
if s.io != nil {
|
||||
s.io.InjectText(source, channel, text)
|
||||
if io := s.injector(); io != nil {
|
||||
io.InjectText(source, channel, text)
|
||||
}
|
||||
}
|
||||
|
||||
// InjectTextNoMemory injects a text message without generating memory.
|
||||
func (s *PluginSDK) InjectTextNoMemory(source, channel, text string) {
|
||||
if s.io != nil {
|
||||
s.io.InjectTextNoMemory(source, channel, text)
|
||||
if io := s.injector(); io != nil {
|
||||
io.InjectTextNoMemory(source, channel, text)
|
||||
}
|
||||
}
|
||||
|
||||
@ -398,18 +528,62 @@ func (s *PluginSDK) InjectTextNoMemory(source, channel, text string) {
|
||||
// returning the reply text (empty string if none). Replies must be dispatched back
|
||||
// to the source channel by the caller.
|
||||
func (s *PluginSDK) InjectInputSync(source, channel, text string) string {
|
||||
if s.io == nil {
|
||||
io := s.injector()
|
||||
if io == nil {
|
||||
return ""
|
||||
}
|
||||
return s.io.InjectInputSync(source, channel, text)
|
||||
return io.InjectInputSync(source, channel, text)
|
||||
}
|
||||
|
||||
// InjectInputMedia 注入带媒体内容块(image_url/audio_url)的输入。
|
||||
// blocks 会落进媒体存储被记忆引用捕获,同时作为当前轮 content 数组
|
||||
// 发给 LLM,让模型在「本轮」就看到图/听到音频——区别于 SetToolBlocks
|
||||
// 的「下一轮 tool message」语义。
|
||||
func (s *PluginSDK) InjectInputMedia(source, channel, text string, blocks []ContentBlock) {
|
||||
if io := s.injector(); io != nil {
|
||||
io.InjectInputMedia(source, channel, text, blocks)
|
||||
}
|
||||
}
|
||||
|
||||
// InjectInputMediaSync 注入带媒体内容块的输入并同步等待 agent 回复。
|
||||
func (s *PluginSDK) InjectInputMediaSync(source, channel, text string, blocks []ContentBlock) string {
|
||||
io := s.injector()
|
||||
if io == nil {
|
||||
return ""
|
||||
}
|
||||
return io.InjectInputMediaSync(source, channel, text, blocks)
|
||||
}
|
||||
|
||||
// InjectInterruptMedia 注入带媒体内容块的中断,可抢占当前 LLM 处理。
|
||||
// blocks 随中断消息一起发给模型。
|
||||
func (s *PluginSDK) InjectInterruptMedia(source, channel, text string, blocks []ContentBlock) {
|
||||
if io := s.injector(); io != nil {
|
||||
io.InjectInterruptMedia(source, channel, text, blocks)
|
||||
}
|
||||
}
|
||||
|
||||
// SetToolBlocks 在工具处理函数内注入多模态内容块,内核在下一条 tool message
|
||||
// 的 content 数组里带上它们。需要「本轮就让模型看到」时用 InjectInputMedia。
|
||||
func (s *PluginSDK) SetToolBlocks(blocks []ContentBlock) {
|
||||
if io := s.injector(); io != nil {
|
||||
io.SetToolBlocks(blocks)
|
||||
}
|
||||
}
|
||||
|
||||
// SetAutoRestart 设置插件是否允许内核自动重启(崩溃后自动重载)。
|
||||
// 默认 true。如果插件有无法恢复的状态(如外部连接),应设为 false。
|
||||
func (s *PluginSDK) SetAutoRestart(enabled bool) { s.autoRestart = enabled }
|
||||
func (s *PluginSDK) SetAutoRestart(enabled bool) {
|
||||
s.apiMu.Lock()
|
||||
s.autoRestart = enabled
|
||||
s.apiMu.Unlock()
|
||||
}
|
||||
|
||||
// AutoRestart 返回插件是否允许自动重启。
|
||||
func (s *PluginSDK) AutoRestart() bool { return s.autoRestart }
|
||||
func (s *PluginSDK) AutoRestart() bool {
|
||||
s.apiMu.RLock()
|
||||
defer s.apiMu.RUnlock()
|
||||
return s.autoRestart
|
||||
}
|
||||
|
||||
// RegisterStopHandler 注册插件停止阶段的清理回调。
|
||||
// 注册的 handler 会在插件 Stop() 之前按"后注册先执行"的顺序调用,
|
||||
@ -460,3 +634,22 @@ func (s *PluginSDK) RunOnRemoveHandlers() {
|
||||
handlers[i]()
|
||||
}
|
||||
}
|
||||
|
||||
// ContentBlock 是多模态内容块(OpenAI 格式:text/image_url/audio_url)。
|
||||
// 插件工具返回结果时可用 PluginSDK.SetToolBlocks 注入,让下一轮 LLM
|
||||
// 请求在 tool message 的 content 数组里带上图片/音频,实现"模型看图/听音频"。
|
||||
type ContentBlock struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text,omitempty"`
|
||||
ImageURL *ImageURL `json:"image_url,omitempty"`
|
||||
AudioURL *AudioURL `json:"audio_url,omitempty"`
|
||||
}
|
||||
|
||||
type ImageURL struct {
|
||||
URL string `json:"url"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
}
|
||||
|
||||
type AudioURL struct {
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
@ -19,6 +19,11 @@ type SettingsAPI interface {
|
||||
// ListCore lists core config keys matching the prefix.
|
||||
ListCore(prefix string) ([]string, error)
|
||||
|
||||
// DataDir returns the plugin-specific data directory (guaranteed to exist):
|
||||
// <daemon data>/plugin_data/<plugin_name>. Plugins should persist any
|
||||
// runtime files (generated images, caches, downloads) here.
|
||||
DataDir() string
|
||||
|
||||
// GetPlugin reads another plugin's config table.
|
||||
GetPlugin(plugin, key string) (interface{}, error)
|
||||
|
||||
|
||||
725
sdk/stress_test.go
Normal file
725
sdk/stress_test.go
Normal file
@ -0,0 +1,725 @@
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// SDK 公开接口的并发压力测试(1.1.0 媒体接口上线后新增)。
|
||||
//
|
||||
// 为什么这一层需要压测:SDK 是**被多个 goroutine 同时使用的共享对象**。
|
||||
// 一个插件的典型形态是 Start() 里起若干后台 goroutine(轮询、监听、定时器),
|
||||
// 它们各自持同一个 *PluginSDK 往里注入消息;内核侧同时还有 stage 扇出、
|
||||
// 工具调用、以及读 AutoRestart() 决定崩溃后是否重启。
|
||||
// 单线程单测全绿不代表这些并发路径成立。
|
||||
//
|
||||
// 关注点不是吞吐数字,而是不变量:
|
||||
// 1. 注入调用不丢、不串(媒体块必须与文本配对,不能张冠李戴)
|
||||
// 2. 状态字段的读写不产生数据竞争(-race 下必须干净)
|
||||
// 3. handler 注册/执行在并发下"恰好一次"
|
||||
// 4. 跨进程 JSON 序列化对新媒体类型必须字节级往返一致
|
||||
//
|
||||
// 媒体接口尤其需要 3 与 4:媒体块要经 JSON 过子进程边界,
|
||||
// 而 []byte 在 JSON 里是 base64,往返不一致的后果是图片静默损坏。
|
||||
|
||||
// ---------- 测试替身 ----------
|
||||
|
||||
// recordingInjector 记录每一次注入调用,用于验证"不丢不串"。
|
||||
type recordingInjector struct {
|
||||
mu sync.Mutex
|
||||
calls []injectCall
|
||||
|
||||
// 计数用原子量:并发路径上只增不减,可在不持锁时安全读。
|
||||
nText, nMedia, nInterrupt, nSync atomic.Int64
|
||||
}
|
||||
|
||||
type injectCall struct {
|
||||
kind string // text / media / interruptMedia / sync ...
|
||||
source string
|
||||
channel string
|
||||
text string
|
||||
blocks []ContentBlock
|
||||
}
|
||||
|
||||
func (r *recordingInjector) record(c injectCall) {
|
||||
r.mu.Lock()
|
||||
r.calls = append(r.calls, c)
|
||||
r.mu.Unlock()
|
||||
}
|
||||
|
||||
func (r *recordingInjector) InjectInterruptText(s, c, t string) {
|
||||
r.nInterrupt.Add(1)
|
||||
r.record(injectCall{kind: "interruptText", source: s, channel: c, text: t})
|
||||
}
|
||||
|
||||
func (r *recordingInjector) InjectText(s, c, t string) {
|
||||
r.nText.Add(1)
|
||||
r.record(injectCall{kind: "text", source: s, channel: c, text: t})
|
||||
}
|
||||
|
||||
func (r *recordingInjector) InjectTextNoMemory(s, c, t string) {
|
||||
r.nText.Add(1)
|
||||
r.record(injectCall{kind: "textNoMem", source: s, channel: c, text: t})
|
||||
}
|
||||
|
||||
func (r *recordingInjector) InjectInputSync(s, c, t string) string {
|
||||
r.nSync.Add(1)
|
||||
r.record(injectCall{kind: "sync", source: s, channel: c, text: t})
|
||||
return "reply:" + t
|
||||
}
|
||||
|
||||
func (r *recordingInjector) SetToolBlocks(blocks []ContentBlock) {
|
||||
r.record(injectCall{kind: "toolBlocks", blocks: blocks})
|
||||
}
|
||||
|
||||
func (r *recordingInjector) InjectInputMedia(s, c, t string, b []ContentBlock) {
|
||||
r.nMedia.Add(1)
|
||||
r.record(injectCall{kind: "media", source: s, channel: c, text: t, blocks: b})
|
||||
}
|
||||
|
||||
func (r *recordingInjector) InjectInputMediaSync(s, c, t string, b []ContentBlock) string {
|
||||
r.nMedia.Add(1)
|
||||
r.nSync.Add(1)
|
||||
r.record(injectCall{kind: "mediaSync", source: s, channel: c, text: t, blocks: b})
|
||||
return "reply:" + t
|
||||
}
|
||||
|
||||
func (r *recordingInjector) InjectInterruptMedia(s, c, t string, b []ContentBlock) {
|
||||
r.nMedia.Add(1)
|
||||
r.record(injectCall{kind: "interruptMedia", source: s, channel: c, text: t, blocks: b})
|
||||
}
|
||||
|
||||
func (r *recordingInjector) snapshot() []injectCall {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return append([]injectCall{}, r.calls...)
|
||||
}
|
||||
|
||||
var _ IOInjector = (*recordingInjector)(nil)
|
||||
|
||||
// imageBlock 构造一个带可识别 URL 的图片块。
|
||||
func imageBlock(tag string) ContentBlock {
|
||||
return ContentBlock{
|
||||
Type: "image_url",
|
||||
ImageURL: &ImageURL{URL: "data:image/png;base64," + tag, Detail: "auto"},
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 1. 媒体注入并发不丢不串 ----------
|
||||
|
||||
// 三个媒体注入方法在高并发下必须:调用数精确、且每次调用的 text 与 blocks 配对不错。
|
||||
//
|
||||
// "不串"是这里的关键断言。注入是插件里最容易被后台 goroutine 并发调用的入口,
|
||||
// 若实现里出现任何共享中间状态(比如把 blocks 暂存到 SDK 字段再读出),
|
||||
// 高并发下就会出现 A 的文本配上 B 的图——而两者单独看都"成功"了,不报错。
|
||||
func TestStress_MediaInjectionConcurrentNoCrossTalk(t *testing.T) {
|
||||
const workers, perWorker = 32, 200
|
||||
|
||||
inj := &recordingInjector{}
|
||||
s := &PluginSDK{name: "stress"}
|
||||
s.SetIOInjector(inj)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < workers; w++ {
|
||||
wg.Add(1)
|
||||
go func(w int) {
|
||||
defer wg.Done()
|
||||
for i := 0; i < perWorker; i++ {
|
||||
// tag 唯一标识这次调用,文本与图片 URL 里都带上它。
|
||||
tag := fmt.Sprintf("w%d-i%d", w, i)
|
||||
switch i % 3 {
|
||||
case 0:
|
||||
s.InjectInputMedia("src", "ch", tag, []ContentBlock{imageBlock(tag)})
|
||||
case 1:
|
||||
if got := s.InjectInputMediaSync("src", "ch", tag, []ContentBlock{imageBlock(tag)}); got != "reply:"+tag {
|
||||
t.Errorf("同步注入回复错位: got %q want %q", got, "reply:"+tag)
|
||||
}
|
||||
default:
|
||||
s.InjectInterruptMedia("src", "ch", tag, []ContentBlock{imageBlock(tag)})
|
||||
}
|
||||
}
|
||||
}(w)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
total := int64(workers * perWorker)
|
||||
if got := inj.nMedia.Load(); got != total {
|
||||
t.Fatalf("媒体注入调用数 = %d,期望 %d(有调用丢失)", got, total)
|
||||
}
|
||||
|
||||
// 逐条校验文本与媒体块配对:URL 必须含该次调用自己的 tag。
|
||||
seen := map[string]bool{}
|
||||
for _, c := range inj.snapshot() {
|
||||
if len(c.blocks) == 0 {
|
||||
continue
|
||||
}
|
||||
if c.blocks[0].ImageURL == nil {
|
||||
t.Fatalf("媒体块 ImageURL 丢失: %+v", c.blocks[0])
|
||||
}
|
||||
if !strings.HasSuffix(c.blocks[0].ImageURL.URL, c.text) {
|
||||
t.Fatalf("文本与媒体块错位: text=%q url=%q", c.text, c.blocks[0].ImageURL.URL)
|
||||
}
|
||||
if seen[c.text] {
|
||||
t.Fatalf("同一次调用被记录两次: %s", c.text)
|
||||
}
|
||||
seen[c.text] = true
|
||||
}
|
||||
if len(seen) != int(total) {
|
||||
t.Fatalf("去重后调用数 = %d,期望 %d", len(seen), total)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 2. 注入期间热替换 injector ----------
|
||||
|
||||
// 内核在插件运行期间可能重新注入 API(重载、恢复、子进程重连握手)。
|
||||
// 此时插件的后台 goroutine 仍在注入。这条路径若无同步就是对 s.io 的数据竞争,
|
||||
// 在 -race 下会被抓出;生产表现是偶发 nil 解引用崩溃。
|
||||
func TestStress_InjectorSwapDuringInjection(t *testing.T) {
|
||||
s := &PluginSDK{name: "stress"}
|
||||
s.SetIOInjector(&recordingInjector{})
|
||||
|
||||
stop := make(chan struct{})
|
||||
var injectors, swapper sync.WaitGroup
|
||||
|
||||
// 注入方:持续打直到 stop
|
||||
for w := 0; w < 8; w++ {
|
||||
injectors.Add(1)
|
||||
go func() {
|
||||
defer injectors.Done()
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
default:
|
||||
s.InjectInputMedia("src", "ch", "x", []ContentBlock{imageBlock("x")})
|
||||
s.InjectText("src", "ch", "y")
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// 替换方:反复换 injector(含换成 nil——内核卸载 API 时的真实状态)
|
||||
swapper.Add(1)
|
||||
go func() {
|
||||
defer swapper.Done()
|
||||
for i := 0; i < 500; i++ {
|
||||
if i%7 == 0 {
|
||||
s.SetIOInjector(nil)
|
||||
} else {
|
||||
s.SetIOInjector(&recordingInjector{})
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// 先等替换跑完,再告知注入方退出。
|
||||
// 顺序写反了就是死锁:注入方只依 close(stop) 退出。
|
||||
swapper.Wait()
|
||||
close(stop)
|
||||
injectors.Wait()
|
||||
// 断言就是「没崩、-race 没报」。nil injector 时必须静默跳过而非 panic。
|
||||
}
|
||||
|
||||
// ---------- 3. autoRestart 标志的并发读写 ----------
|
||||
|
||||
// SetAutoRestart 的文档用途是"插件有无法恢复的状态(如外部连接)时设为 false"——
|
||||
// 而连接建立本身通常是异步的,所以这个写入天然发生在后台 goroutine。
|
||||
// 内核侧 registry 在另一个 goroutine 读 AutoRestart() 决定崩溃后是否重启。
|
||||
// 这是一对跨 goroutine 的读写,必须同步。
|
||||
func TestStress_AutoRestartFlagConcurrent(t *testing.T) {
|
||||
s := &PluginSDK{name: "stress", autoRestart: true}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < 16; w++ {
|
||||
wg.Add(1)
|
||||
go func(w int) {
|
||||
defer wg.Done()
|
||||
for i := 0; i < 500; i++ {
|
||||
s.SetAutoRestart(i%2 == 0)
|
||||
}
|
||||
}(w)
|
||||
}
|
||||
// 读方模拟内核 registry
|
||||
for r := 0; r < 8; r++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := 0; i < 500; i++ {
|
||||
_ = s.AutoRestart()
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// ---------- 4. stop / onRemove handler 的"恰好一次" ----------
|
||||
|
||||
// RunStopHandlers 的契约是"执行后清空,幂等"。内核在停止插件时可能并发触发
|
||||
// (超时强杀与正常 Stop 竞争),handler 里往往是关连接、落盘——
|
||||
// 执行两次的后果从"重复写文件"到"close 已关闭的 channel 直接 panic"。
|
||||
func TestStress_StopHandlersExactlyOnce(t *testing.T) {
|
||||
const n = 300
|
||||
s := &PluginSDK{name: "stress"}
|
||||
|
||||
var counters [n]atomic.Int64
|
||||
for i := 0; i < n; i++ {
|
||||
i := i
|
||||
s.RegisterStopHandler(func() { counters[i].Add(1) })
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < 16; w++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
s.RunStopHandlers()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
if got := counters[i].Load(); got != 1 {
|
||||
t.Fatalf("handler %d 执行 %d 次,期望恰好 1 次", i, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 注册与执行并发:已注册的 handler 一次都不能多跑,未跑到的也不能被丢。
|
||||
// 断言用"每个 handler 的执行次数 <= 1"而非"总数相等"——
|
||||
// 与 RunStopHandlers 竞争的注册可能落在快照之后,那属于合法的未执行。
|
||||
func TestStress_StopHandlersRegisterWhileRunning(t *testing.T) {
|
||||
s := &PluginSDK{name: "stress"}
|
||||
const n = 500
|
||||
var counters [n]atomic.Int64
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := 0; i < n; i++ {
|
||||
i := i
|
||||
s.RegisterStopHandler(func() { counters[i].Add(1) })
|
||||
}
|
||||
}()
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := 0; i < 50; i++ {
|
||||
s.RunStopHandlers()
|
||||
}
|
||||
}()
|
||||
wg.Wait()
|
||||
s.RunStopHandlers() // 收尾:把剩下的都跑掉
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
if got := counters[i].Load(); got > 1 {
|
||||
t.Fatalf("handler %d 被执行 %d 次(重复执行)", i, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStress_OnRemoveHandlersExactlyOnce(t *testing.T) {
|
||||
const n = 200
|
||||
s := &PluginSDK{name: "stress"}
|
||||
|
||||
var counters [n]atomic.Int64
|
||||
for i := 0; i < n; i++ {
|
||||
i := i
|
||||
s.RegisterOnRemoveHandler(func() { counters[i].Add(1) })
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < 12; w++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
s.RunOnRemoveHandlers()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
if got := counters[i].Load(); got != 1 {
|
||||
t.Fatalf("onRemove handler %d 执行 %d 次,期望恰好 1 次", i, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 5. StageContext 并发读改写 ----------
|
||||
|
||||
// StageContext 是全部 stage handler 共享的可变状态,字段全导出、靠调用方自觉
|
||||
// 持 Lock/RLock。媒体链路让 Extra 成为新热点(media_blocks 挂在这里),
|
||||
// 而 map 的并发写在 Go 里是直接 fatal,recover 都接不住。
|
||||
//
|
||||
// 这条测试锁定的不变量:按约定持锁的并发读改写不丢更新、不 fatal。
|
||||
func TestStress_StageContextConcurrentExtraAndFinalText(t *testing.T) {
|
||||
ctx := &StageContext{Extra: map[string]interface{}{}}
|
||||
|
||||
const workers, rounds = 16, 200
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < workers; w++ {
|
||||
wg.Add(1)
|
||||
go func(w int) {
|
||||
defer wg.Done()
|
||||
for i := 0; i < rounds; i++ {
|
||||
// 写:模拟插件往 Extra 塞媒体块并追加文本(读-改-写)
|
||||
ctx.Lock()
|
||||
ctx.Extra[fmt.Sprintf("k%d-%d", w, i)] = []ContentBlock{imageBlock("x")}
|
||||
ctx.FinalText += "."
|
||||
ctx.Unlock()
|
||||
|
||||
// 读:模拟另一个 handler 检查是否已被响应
|
||||
_ = ctx.IsResponded()
|
||||
ctx.RLock()
|
||||
_ = len(ctx.Extra)
|
||||
ctx.RUnlock()
|
||||
}
|
||||
}(w)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
ctx.RLock()
|
||||
defer ctx.RUnlock()
|
||||
if len(ctx.Extra) != workers*rounds {
|
||||
t.Fatalf("Extra 键数 = %d,期望 %d(出现 lost update)", len(ctx.Extra), workers*rounds)
|
||||
}
|
||||
if len(ctx.FinalText) != workers*rounds {
|
||||
t.Fatalf("FinalText 长度 = %d,期望 %d(出现 lost update)", len(ctx.FinalText), workers*rounds)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 6. OwnTools scope 包装器的并发正确性 ----------
|
||||
|
||||
// StageScopeOwnTools 的包装闭环里要读 ctx.ToolCalls 判断归属。
|
||||
// 并发下若判断与执行之间状态被改写,就会出现"别人的工具触发了我的 handler"——
|
||||
// 后果是插件对不属于自己的工具结果动手,且没有任何错误。
|
||||
func TestStress_OwnToolsScopeNoCrossPluginLeak(t *testing.T) {
|
||||
var registered StageHandler
|
||||
s := &PluginSDK{
|
||||
name: "mine",
|
||||
regStage: func(stage Stage, h StageHandler) { registered = h },
|
||||
}
|
||||
|
||||
var fired atomic.Int64
|
||||
s.RegisterStage(StageBeforeToolcall, func(ctx *StageContext) error {
|
||||
fired.Add(1)
|
||||
ctx.RLock()
|
||||
defer ctx.RUnlock()
|
||||
// 触发了就必须确实是自己的工具
|
||||
if len(ctx.ToolCalls) == 0 || ctx.ToolCalls[0].Plugin != "mine" {
|
||||
t.Errorf("handler 被别的插件的工具触发: %+v", ctx.ToolCalls)
|
||||
}
|
||||
return nil
|
||||
}, StageScopeOwnTools)
|
||||
|
||||
if registered == nil {
|
||||
t.Fatal("handler 未注册")
|
||||
}
|
||||
|
||||
const workers, rounds = 16, 100
|
||||
var wg sync.WaitGroup
|
||||
var mineCount atomic.Int64
|
||||
for w := 0; w < workers; w++ {
|
||||
wg.Add(1)
|
||||
go func(w int) {
|
||||
defer wg.Done()
|
||||
for i := 0; i < rounds; i++ {
|
||||
// 每个 goroutine 用自己的 ctx——真实内核里 stage 扇出共享同一个
|
||||
// ctx,但那部分的并发由内核 host 仲裁;这里验证包装器本身。
|
||||
owner := "other"
|
||||
if (w+i)%2 == 0 {
|
||||
owner = "mine"
|
||||
mineCount.Add(1)
|
||||
}
|
||||
ctx := &StageContext{Extra: map[string]interface{}{}}
|
||||
ctx.ToolCalls = []ToolCall{{Plugin: owner, Name: "t"}}
|
||||
if err := registered(ctx); err != nil {
|
||||
t.Errorf("handler 返回错误: %v", err)
|
||||
}
|
||||
}
|
||||
}(w)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if got, want := fired.Load(), mineCount.Load(); got != want {
|
||||
t.Fatalf("handler 触发 %d 次,期望 %d 次(漏触发或跨插件触发)", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 7. 媒体类型的 JSON 往返(跨进程边界的真实形态) ----------
|
||||
|
||||
// 媒体块与附件要经 JSON 过子进程边界。[]byte 在 JSON 里是 base64,
|
||||
// 往返不一致的后果是图片字节静默损坏——落进 CAS 后 digest 校验才会发现,
|
||||
// 而那时已经无从追查是谁改坏的。
|
||||
func TestStress_MediaTypesJSONRoundTripAtScale(t *testing.T) {
|
||||
// 覆盖真实会遇到的边界:空、单字节、含 0x00、全 0xFF、超过 base64 分组边界的长度
|
||||
sizes := []int{0, 1, 2, 3, 255, 256, 1023, 4096, 65537}
|
||||
for _, n := range sizes {
|
||||
data := make([]byte, n)
|
||||
for i := range data {
|
||||
data[i] = byte(i * 7 % 256)
|
||||
}
|
||||
att := MediaAttachment{
|
||||
Digest: strings.Repeat("a", 64),
|
||||
MIME: "image/png",
|
||||
Data: data,
|
||||
Name: "图片-名字 with space & 符号.png",
|
||||
Description: "一张紫蓝红三色带图,含 emoji 🎨 与换行\n第二行",
|
||||
}
|
||||
b, err := json.Marshal(att)
|
||||
if err != nil {
|
||||
t.Fatalf("size=%d marshal: %v", n, err)
|
||||
}
|
||||
var back MediaAttachment
|
||||
if err := json.Unmarshal(b, &back); err != nil {
|
||||
t.Fatalf("size=%d unmarshal: %v", n, err)
|
||||
}
|
||||
if len(back.Data) != n {
|
||||
t.Fatalf("size=%d 往返后长度 = %d", n, len(back.Data))
|
||||
}
|
||||
for i := range data {
|
||||
if back.Data[i] != data[i] {
|
||||
t.Fatalf("size=%d 第 %d 字节损坏: %02x != %02x", n, i, back.Data[i], data[i])
|
||||
}
|
||||
}
|
||||
if back.Name != att.Name || back.Description != att.Description || back.MIME != att.MIME || back.Digest != att.Digest {
|
||||
t.Fatalf("size=%d 元数据往返不一致: %+v", n, back)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// omitempty 必须真的生效:读路径上内核不回 Data,若序列化仍产出 "data":null
|
||||
// 之类的键,跨进程消息会凭空变大,且插件侧无法区分"没有字节"与"空字节"。
|
||||
func TestStress_MediaTypesOmitEmpty(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
v interface{}
|
||||
absent []string
|
||||
present []string
|
||||
}{
|
||||
{
|
||||
name: "Triple 无媒体",
|
||||
v: Triple{Subject: "甲方", Relation: "签署", Object: "合同"},
|
||||
absent: []string{"media_digests", "sentence_text", "confidence", "subject_type", "object_type"},
|
||||
present: []string{"subject", "relation", "object"},
|
||||
},
|
||||
{
|
||||
name: "Triple 带媒体",
|
||||
v: Triple{Subject: "甲方", Relation: "包含", Object: "图", MediaDigests: []string{"abc12345"}, SentenceText: "句子"},
|
||||
absent: []string{"confidence"},
|
||||
present: []string{"media_digests", "sentence_text"},
|
||||
},
|
||||
{
|
||||
name: "Doc 读路径无字节",
|
||||
v: Doc{ID: "d1", Title: "标题", Content: "正文", Attachments: []MediaAttachment{{Digest: "abc12345", MIME: "image/png"}}},
|
||||
absent: []string{"\"data\"", "media_digests", "score"},
|
||||
present: []string{"attachments", "digest", "mime"},
|
||||
},
|
||||
{
|
||||
name: "TextEvent 无附件",
|
||||
v: TextEvent{Role: "user", Content: "hi"},
|
||||
absent: []string{"attachments", "channel"},
|
||||
present: []string{"role", "content"},
|
||||
},
|
||||
{
|
||||
name: "ContentBlock 纯文本",
|
||||
v: ContentBlock{Type: "text", Text: "hi"},
|
||||
absent: []string{"image_url", "audio_url"},
|
||||
present: []string{"type", "text"},
|
||||
},
|
||||
{
|
||||
name: "ContentBlock 图片",
|
||||
v: imageBlock("AAA"),
|
||||
absent: []string{"audio_url", "\"text\""},
|
||||
present: []string{"image_url", "detail"},
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
b, err := json.Marshal(c.v)
|
||||
if err != nil {
|
||||
t.Fatalf("%s marshal: %v", c.name, err)
|
||||
}
|
||||
s := string(b)
|
||||
for _, k := range c.absent {
|
||||
if strings.Contains(s, k) {
|
||||
t.Errorf("%s: 不该出现的键 %s —— %s", c.name, k, s)
|
||||
}
|
||||
}
|
||||
for _, k := range c.present {
|
||||
if !strings.Contains(s, k) {
|
||||
t.Errorf("%s: 缺少键 %s —— %s", c.name, k, s)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 媒体块在并发序列化下必须各自独立:ImageURL/AudioURL 是指针,
|
||||
// 若某处复用同一个指针再改写,序列化结果会互相污染。
|
||||
func TestStress_ContentBlockConcurrentMarshal(t *testing.T) {
|
||||
const workers, rounds = 16, 300
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < workers; w++ {
|
||||
wg.Add(1)
|
||||
go func(w int) {
|
||||
defer wg.Done()
|
||||
for i := 0; i < rounds; i++ {
|
||||
tag := fmt.Sprintf("w%d-i%d", w, i)
|
||||
blocks := []ContentBlock{
|
||||
{Type: "text", Text: tag},
|
||||
imageBlock(tag),
|
||||
{Type: "audio_url", AudioURL: &AudioURL{URL: "data:audio/wav;base64," + tag}},
|
||||
}
|
||||
b, err := json.Marshal(blocks)
|
||||
if err != nil {
|
||||
t.Errorf("marshal: %v", err)
|
||||
return
|
||||
}
|
||||
var back []ContentBlock
|
||||
if err := json.Unmarshal(b, &back); err != nil {
|
||||
t.Errorf("unmarshal: %v", err)
|
||||
return
|
||||
}
|
||||
if len(back) != 3 {
|
||||
t.Errorf("块数 = %d", len(back))
|
||||
return
|
||||
}
|
||||
if back[0].ImageURL != nil || back[0].AudioURL != nil {
|
||||
t.Errorf("文本块被填了媒体指针: %+v", back[0])
|
||||
}
|
||||
if back[1].ImageURL == nil || !strings.HasSuffix(back[1].ImageURL.URL, tag) {
|
||||
t.Errorf("图片块 URL 错位: %+v", back[1].ImageURL)
|
||||
}
|
||||
if back[1].AudioURL != nil {
|
||||
t.Errorf("图片块被填了音频指针")
|
||||
}
|
||||
if back[2].AudioURL == nil || !strings.HasSuffix(back[2].AudioURL.URL, tag) {
|
||||
t.Errorf("音频块 URL 错位: %+v", back[2].AudioURL)
|
||||
}
|
||||
}
|
||||
}(w)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// ---------- 8. 注册面的并发 ----------
|
||||
|
||||
// 插件在 Start() 里起多个 goroutine 分别注册工具是常见写法。
|
||||
// def.Plugin 的默认填充若不是每次调用独立的,就会出现工具归属错乱——
|
||||
// 表现是 OwnTools scope 失效、WebUI 里工具挂在别的插件名下。
|
||||
func TestStress_RegisterToolConcurrentPluginDefaulting(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
got := map[string]string{} // toolName -> def.Plugin
|
||||
|
||||
s := &PluginSDK{
|
||||
name: "mine",
|
||||
regTool: func(name string, def ToolDef, h ToolHandler) error {
|
||||
mu.Lock()
|
||||
got[name] = def.Plugin
|
||||
mu.Unlock()
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
const workers, perWorker = 16, 100
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < workers; w++ {
|
||||
wg.Add(1)
|
||||
go func(w int) {
|
||||
defer wg.Done()
|
||||
for i := 0; i < perWorker; i++ {
|
||||
name := fmt.Sprintf("tool_w%d_i%d", w, i)
|
||||
def := ToolDef{Description: "d", Parameters: map[string]interface{}{}}
|
||||
// 一半显式指定归属,一半靠 SDK 填默认值
|
||||
if i%2 == 0 {
|
||||
def.Plugin = "explicit"
|
||||
}
|
||||
if err := s.RegisterTool(name, def, func(map[string]interface{}) (interface{}, error) {
|
||||
return nil, nil
|
||||
}); err != nil {
|
||||
t.Errorf("RegisterTool: %v", err)
|
||||
}
|
||||
}
|
||||
}(w)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if len(got) != workers*perWorker {
|
||||
t.Fatalf("注册工具数 = %d,期望 %d", len(got), workers*perWorker)
|
||||
}
|
||||
for name, owner := range got {
|
||||
want := "mine"
|
||||
if isEvenSuffix(name) {
|
||||
want = "explicit"
|
||||
}
|
||||
if owner != want {
|
||||
t.Fatalf("工具 %s 归属 = %q,期望 %q", name, owner, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// isEvenSuffix 判断 tool_wX_iY 里的 Y 是否为偶数。
|
||||
func isEvenSuffix(name string) bool {
|
||||
idx := strings.LastIndex(name, "_i")
|
||||
if idx < 0 {
|
||||
return false
|
||||
}
|
||||
n := 0
|
||||
if _, err := fmt.Sscanf(name[idx+2:], "%d", &n); err != nil {
|
||||
return false
|
||||
}
|
||||
return n%2 == 0
|
||||
}
|
||||
|
||||
// nil 依赖下所有便捷方法必须静默降级而非 panic。
|
||||
//
|
||||
// 这是"媒体存储可关闭"在 SDK 层的对应物:内核未注入某个 API 时
|
||||
// (精简部署、插件权限不足、子进程握手尚未完成),插件的调用不该崩。
|
||||
func TestStress_NilDependenciesDegradeSilently(t *testing.T) {
|
||||
s := &PluginSDK{name: "bare"}
|
||||
|
||||
const workers = 16
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < workers; w++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := 0; i < 200; i++ {
|
||||
s.InjectText("s", "c", "t")
|
||||
s.InjectTextNoMemory("s", "c", "t")
|
||||
s.InjectInterruptText("s", "c", "t")
|
||||
if got := s.InjectInputSync("s", "c", "t"); got != "" {
|
||||
t.Errorf("无 injector 时同步注入应返回空串,got %q", got)
|
||||
}
|
||||
s.InjectInputMedia("s", "c", "t", []ContentBlock{imageBlock("x")})
|
||||
if got := s.InjectInputMediaSync("s", "c", "t", nil); got != "" {
|
||||
t.Errorf("无 injector 时媒体同步注入应返回空串,got %q", got)
|
||||
}
|
||||
s.InjectInterruptMedia("s", "c", "t", nil)
|
||||
|
||||
// getter 全部应返回 nil 而非 panic
|
||||
_ = s.Memory()
|
||||
_ = s.TextMemory()
|
||||
_ = s.DocMemory()
|
||||
_ = s.Knowledge()
|
||||
_ = s.LLM()
|
||||
_ = s.Social()
|
||||
_ = s.Events()
|
||||
_ = s.PluginMgr()
|
||||
_ = s.Settings()
|
||||
|
||||
// 注册面无 registrar 时应返回 nil error
|
||||
if err := s.RegisterTool("t", ToolDef{}, nil); err != nil {
|
||||
t.Errorf("无 registrar 时 RegisterTool 应返回 nil,got %v", err)
|
||||
}
|
||||
if err := s.RegisterPluginAPI("a"); err != nil {
|
||||
t.Errorf("无 registrar 时 RegisterPluginAPI 应返回 nil,got %v", err)
|
||||
}
|
||||
s.RegisterStage(StageOnInput, func(*StageContext) error { return nil })
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
@ -24,7 +24,8 @@ func cmdBuild(args []string) {
|
||||
// Read all config from plg.json first
|
||||
plg, err := readPlgJSON("plg.json")
|
||||
if err != nil {
|
||||
fmt.Printf("error: read plg.json: %v\n", err); os.Exit(1)
|
||||
fmt.Printf("error: read plg.json: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Base config from plg.json
|
||||
@ -39,11 +40,13 @@ func cmdBuild(args []string) {
|
||||
switch args[i] {
|
||||
case "--outdir":
|
||||
if i+1 < len(args) {
|
||||
outDir = args[i+1]; i++
|
||||
outDir = args[i+1]
|
||||
i++
|
||||
}
|
||||
case "--target":
|
||||
if i+1 < len(args) {
|
||||
targets = append(targets, args[i+1]); i++
|
||||
targets = append(targets, args[i+1])
|
||||
i++
|
||||
}
|
||||
case "--bundle":
|
||||
bundle = true
|
||||
@ -51,11 +54,13 @@ func cmdBuild(args []string) {
|
||||
bundle = false
|
||||
case "--sdk-path":
|
||||
if i+1 < len(args) {
|
||||
sdkPath = args[i+1]; i++
|
||||
sdkPath = args[i+1]
|
||||
i++
|
||||
}
|
||||
case "--replace", "-R":
|
||||
if i+1 < len(args) {
|
||||
cliReplaces = append(cliReplaces, args[i+1]); i++
|
||||
cliReplaces = append(cliReplaces, args[i+1])
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -68,18 +73,9 @@ func cmdBuild(args []string) {
|
||||
// Ensure go.mod exists with correct SDK path
|
||||
sdkModule := ensureGoMod(plg, sdkPath)
|
||||
|
||||
// First build: fetch the SDK module (generates go.sum with zip hash)
|
||||
// 保证 SDK 模块可解析,否则编译必死在 "missing go.sum entry"。
|
||||
if sdkModule != "" {
|
||||
if _, err := os.Stat("go.sum"); os.IsNotExist(err) {
|
||||
dl := exec.Command("go", "mod", "download", sdkModule)
|
||||
dl.Env = os.Environ()
|
||||
dl.Stdout = os.Stdout
|
||||
dl.Stderr = os.Stderr
|
||||
fmt.Println(" downloading SDK module deps...")
|
||||
if err := dl.Run(); err != nil {
|
||||
fmt.Printf(" error: go mod download: %v\n", err)
|
||||
}
|
||||
}
|
||||
ensureSDKResolvable(plg, sdkModule, sdkPath)
|
||||
}
|
||||
|
||||
// Merge plg.json replaces + CLI overrides
|
||||
@ -105,14 +101,17 @@ func cmdBuild(args []string) {
|
||||
}
|
||||
|
||||
// allBundleTargets 是 --bundle 模式构建的全部平台。
|
||||
// 每个 OS 只有一个架构(amd64),避免二进制文件名冲突。
|
||||
//
|
||||
// 子进程模式下各平台产物同名(plugin.bin)——进程边界即 ABI 边界,
|
||||
// 不存在平台特有扩展名,故 zip 内按平台加后缀区分;
|
||||
// 内核安装时按当前平台挑对应条目重命名为 plugin.bin。
|
||||
var allBundleTargets = []struct {
|
||||
target string
|
||||
entry string // 二进制在 zip 中的文件名
|
||||
}{
|
||||
{"linux/amd64", "plugin.so"},
|
||||
{"darwin/amd64", "plugin.dylib"},
|
||||
{"windows/amd64", "plugin.dll"},
|
||||
{"linux/amd64", "plugin.bin.linux.amd64"},
|
||||
{"darwin/amd64", "plugin.bin.darwin.amd64"},
|
||||
{"windows/amd64", "plugin.bin.windows.amd64"},
|
||||
}
|
||||
|
||||
func buildBundle(plg *PlgConfig, outDir string, sdkPath string) {
|
||||
@ -120,9 +119,13 @@ func buildBundle(plg *PlgConfig, outDir string, sdkPath string) {
|
||||
buildDir := "build"
|
||||
os.MkdirAll(buildDir, 0755)
|
||||
|
||||
// Auto-generate C ABI bridge for non-Windows
|
||||
bridgeCleanup := generateBridge("")
|
||||
defer bridgeCleanup()
|
||||
runtimeCleanup, err := generateProcRuntime()
|
||||
if err != nil {
|
||||
fmt.Printf(" error: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer runtimeCleanup()
|
||||
|
||||
thirdpartCleanup := linkThirdpart(plg, "linux/amd64")
|
||||
defer thirdpartCleanup()
|
||||
|
||||
@ -135,22 +138,18 @@ func buildBundle(plg *PlgConfig, outDir string, sdkPath string) {
|
||||
return
|
||||
}
|
||||
|
||||
outPath := filepath.Join(buildDir, cfg.entryFile)
|
||||
// 每平台产物落到独立路径,避免相互覆盖
|
||||
outName := fmt.Sprintf("%s_%s_%s", cfg.entryFile, cfg.goos, cfg.goarch)
|
||||
outPath := filepath.Join(buildDir, outName)
|
||||
|
||||
cmd := exec.Command("go", "build", "-buildmode=c-shared", "-o", outPath)
|
||||
// 零 cgo:跨平台交叉编译不需目标平台 C 工具链
|
||||
cmd := exec.Command("go", "build", "-trimpath", "-o", outPath)
|
||||
cmd.Env = os.Environ()
|
||||
cmd.Env = append(cmd.Env, "GOOS="+cfg.goos, "GOARCH="+cfg.goarch, "CGO_ENABLED=1")
|
||||
|
||||
if cfg.goos == "windows" {
|
||||
cc := detectWindowsCC()
|
||||
if cc != "" {
|
||||
cmd.Env = append(cmd.Env, "CC="+cc)
|
||||
}
|
||||
}
|
||||
|
||||
cmd.Env = append(cmd.Env, "GOOS="+cfg.goos, "GOARCH="+cfg.goarch, "CGO_ENABLED=0")
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
fmt.Printf(" compiling %s/%s (-buildmode=c-shared)...\n", cfg.goos, cfg.goarch)
|
||||
|
||||
fmt.Printf(" compiling %s/%s (子进程模式,CGO_ENABLED=0)...\n", cfg.goos, cfg.goarch)
|
||||
if err := cmd.Run(); err != nil {
|
||||
fmt.Printf(" error: build %s/%s: %v\n", cfg.goos, cfg.goarch, err)
|
||||
return
|
||||
@ -168,7 +167,7 @@ func buildBundle(plg *PlgConfig, outDir string, sdkPath string) {
|
||||
for p := range platforms {
|
||||
plats = append(plats, p)
|
||||
}
|
||||
writePluginJSON(plg, plats, "plugin.so")
|
||||
writePluginJSON(plg, plats, procEntryFile)
|
||||
|
||||
// package single .hmap with correctly named entries
|
||||
hmapPath := filepath.Join(outDir, fmt.Sprintf("%s_bundle.hmap", toSnake(plg.NameEn)))
|
||||
@ -176,7 +175,10 @@ func buildBundle(plg *PlgConfig, outDir string, sdkPath string) {
|
||||
fmt.Printf(" packaged %s\n", filepath.Base(hmapPath))
|
||||
}
|
||||
|
||||
func (p *PlgConfig) IsLua() bool { return p.Entry == "main.lua" }
|
||||
// IsLua 判断是否为 Lua 插件(走解释器,不经过 Go 编译)。
|
||||
//
|
||||
// 这是 entry 字段唯一仍在使用的用途:Go 插件不再看 entry 值,一律产出 plugin.bin。
|
||||
func (p *PlgConfig) IsLua() bool { return p.Entry == luaEntryFile }
|
||||
|
||||
func readPlgJSON(path string) (*PlgConfig, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
@ -228,9 +230,14 @@ func writePluginJSON(plg *PlgConfig, platforms []string, entry string) {
|
||||
type buildConfig struct {
|
||||
goos string
|
||||
goarch string
|
||||
entryFile string // "plugin.so" or "plugin.dll"
|
||||
entryFile string // 一律为 plugin.bin(进程边界即 ABI 边界,无平台特有扩展名)
|
||||
}
|
||||
|
||||
// resolveBuild 解析目标平台。
|
||||
//
|
||||
// 全平台统一产出 plugin.bin:子进程模式下不存在 .so/.dylib/.dll 的区分,
|
||||
// 因为进程边界本身就是 ABI 边界——这正是三套独立 ABI 实现收敛为
|
||||
// 单一 RPC 实现的直接后果(§9.2:Windows 不再是能力退化的第三套实现)。
|
||||
func resolveBuild(target string) (*buildConfig, string) {
|
||||
if target == "lua" || target == "" {
|
||||
return nil, "lua"
|
||||
@ -245,14 +252,8 @@ func resolveBuild(target string) (*buildConfig, string) {
|
||||
}
|
||||
|
||||
switch goos {
|
||||
case "linux":
|
||||
return &buildConfig{goos: goos, goarch: goarch, entryFile: "plugin.so"}, ""
|
||||
case "darwin":
|
||||
return &buildConfig{goos: goos, goarch: goarch, entryFile: "plugin.dylib"}, ""
|
||||
case "freebsd":
|
||||
return &buildConfig{goos: goos, goarch: goarch, entryFile: "plugin.so"}, ""
|
||||
case "windows":
|
||||
return &buildConfig{goos: goos, goarch: goarch, entryFile: "plugin.dll"}, ""
|
||||
case "linux", "darwin", "freebsd", "windows":
|
||||
return &buildConfig{goos: goos, goarch: goarch, entryFile: procEntryFile}, ""
|
||||
default:
|
||||
return nil, fmt.Sprintf("unsupported OS %q", goos)
|
||||
}
|
||||
@ -333,6 +334,128 @@ func ensureGoMod(plg *PlgConfig, sdkPath string) string {
|
||||
return sdkModule
|
||||
}
|
||||
|
||||
// ensureSDKResolvable 保证 SDK 模块在编译前可解析。
|
||||
//
|
||||
// 为何需要这个函数:gitcode 的模块不在 proxy.golang.org 上。只要 go.mod
|
||||
// 里的 SDK 靠 require 版本号解析,而本地又没 go.sum 条目,go build 就报
|
||||
// "missing go.sum entry";而原来那句 `go mod download <mod>` 会去公共 proxy
|
||||
// 拉一个永远拉不到的条目,超时后只打一行 warn 就继继编译,紧接着死在
|
||||
// 同一个错误上——新用户拿到的是两段无关的报错。
|
||||
//
|
||||
// 三级策略,按代价递增:
|
||||
// 1. go.mod 已有指向本地目录的 replace —— 什么都不用做(replace 到目录时
|
||||
// go 不需要也不校验 go.sum)。
|
||||
// 2. 能定位到本机 SDK 源码 —— 写入 replace。这是存量项目(go.mod 旧、
|
||||
// 无 replace)的救场路径。
|
||||
// 3. 都不行 —— 跑 `go mod tidy`(带 -mod=mod)让它自己去试,失败则给
|
||||
// 可操作的提示而不是让用户去猜。
|
||||
func ensureSDKResolvable(plg *PlgConfig, sdkModule, sdkPath string) {
|
||||
data, err := os.ReadFile("go.mod")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 策略 1:已有指向本地目录的 replace。
|
||||
// replace 目标带 / 或 . 开头的才是路径;指向另一个模块的 replace 不算。
|
||||
for _, line := range strings.Split(string(data), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if !strings.HasPrefix(line, "replace ") || !strings.Contains(line, sdkModule) {
|
||||
continue
|
||||
}
|
||||
parts := strings.Fields(line)
|
||||
if len(parts) < 4 {
|
||||
continue
|
||||
}
|
||||
target := parts[3]
|
||||
if strings.HasPrefix(target, ".") || strings.HasPrefix(target, "/") ||
|
||||
strings.Contains(target, ":/") || strings.Contains(target, ":\\") {
|
||||
return // 已指向本地目录,无需 go.sum
|
||||
}
|
||||
}
|
||||
|
||||
// 策略 2:能定位到本机 SDK 就写 replace。
|
||||
// resolveSDKPath 失败会 os.Exit,所以只在能确定拿到路径时调用它背后的探测。
|
||||
if root := findLocalSDK(sdkPath); root != "" {
|
||||
if appendGoModReplace(sdkModule, root) {
|
||||
fmt.Printf(" SDK 指向本机源码(已写入 go.mod replace):%s\n", root)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 策略 3:交给 go mod tidy。
|
||||
if _, err := os.Stat("go.sum"); err == nil {
|
||||
return // 已有 go.sum,不插手
|
||||
}
|
||||
fmt.Println(" 解析 SDK 依赖(go mod tidy)...")
|
||||
tidy := exec.Command("go", "mod", "tidy")
|
||||
tidy.Env = append(os.Environ(), "GOFLAGS=-mod=mod")
|
||||
if out, err := tidy.CombinedOutput(); err != nil {
|
||||
fmt.Printf(" warn: go mod tidy 失败:%v\n", err)
|
||||
if len(out) > 0 {
|
||||
fmt.Printf(" %s\n", strings.TrimSpace(string(out)))
|
||||
}
|
||||
fmt.Printf(" 提示:%s 不在公共 proxy 上。用以下任一方式指向本机 SDK:\n", sdkModule)
|
||||
fmt.Printf(" plugindev sdk install latest # 装一份到 ~/.homeagent/plugindev/sdk\n")
|
||||
fmt.Printf(" plugindev build --sdk-path <路径> # 或直接指定源码目录\n")
|
||||
}
|
||||
}
|
||||
|
||||
// findLocalSDK 探测本机 SDK 源码根目录,找不到返回空串。
|
||||
//
|
||||
// 与 resolveSDKPath 的区别:后者找不到就 os.Exit,适合“必须有”的调用点;
|
||||
// 这里是“有则更好”的探测,不能把构建搞挂。
|
||||
func findLocalSDK(sdkPath string) string {
|
||||
candidates := []string{}
|
||||
if sdkPath != "" {
|
||||
if abs, err := filepath.Abs(sdkPath); err == nil {
|
||||
candidates = append(candidates, abs)
|
||||
}
|
||||
}
|
||||
// plugindev 自身所在位置往上三级(tools/plugindev/plugindev → SDK 根)
|
||||
if self, err := os.Executable(); err == nil {
|
||||
candidates = append(candidates, filepath.Dir(filepath.Dir(filepath.Dir(self))))
|
||||
}
|
||||
// plugindev sdk use 选定的版本
|
||||
store := os.Getenv("HOMEAGENT_SDK_DIR")
|
||||
if store == "" {
|
||||
if home, err := os.UserHomeDir(); err == nil {
|
||||
store = filepath.Join(home, ".homeagent", "plugindev", "sdk")
|
||||
}
|
||||
}
|
||||
if store != "" {
|
||||
if d, err := os.ReadFile(filepath.Join(store, "current")); err == nil {
|
||||
if ver := strings.TrimSpace(string(d)); ver != "" {
|
||||
candidates = append(candidates, filepath.Join(store, ver))
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, c := range candidates {
|
||||
if c == "" {
|
||||
continue
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(c, "sdk", "plugin.go")); err == nil {
|
||||
return c
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// appendGoModReplace 向 go.mod 追加一条 replace,成功返回 true。
|
||||
func appendGoModReplace(module, localPath string) bool {
|
||||
data, err := os.ReadFile("go.mod")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
abs, err := filepath.Abs(localPath)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
abs = strings.ReplaceAll(abs, "\\", "/")
|
||||
s := strings.TrimRight(string(data), "\r\n")
|
||||
s += fmt.Sprintf("\n\nreplace %s => %s\n", module, abs)
|
||||
return os.WriteFile("go.mod", []byte(s), 0644) == nil
|
||||
}
|
||||
|
||||
func resolveSDKPath(sdkPath string) string {
|
||||
if sdkPath != "" {
|
||||
abs, _ := filepath.Abs(sdkPath)
|
||||
@ -403,7 +526,7 @@ func buildTarget(plg *PlgConfig, target, outDir, sdkPath string) {
|
||||
return
|
||||
}
|
||||
|
||||
// Resolve build config
|
||||
// Resolve build config(全平台统一产出 plugin.bin)
|
||||
cfg, errMsg := resolveBuild(target)
|
||||
if cfg == nil {
|
||||
fmt.Printf(" error: %s\n", errMsg)
|
||||
@ -414,9 +537,12 @@ func buildTarget(plg *PlgConfig, target, outDir, sdkPath string) {
|
||||
os.MkdirAll(buildDir, 0755)
|
||||
outPath := filepath.Join(buildDir, cfg.entryFile)
|
||||
|
||||
// Auto-generate C ABI bridge (all platforms use c-shared)
|
||||
bridgeCleanup := generateBridge(cfg.goos)
|
||||
defer bridgeCleanup()
|
||||
runtimeCleanup, err := generateProcRuntime()
|
||||
if err != nil {
|
||||
fmt.Printf(" error: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer runtimeCleanup()
|
||||
|
||||
// Auto-link thirdpart/ contents + source_dirs + replace targets
|
||||
thirdpartCleanup := linkThirdpart(plg, target)
|
||||
@ -425,28 +551,15 @@ func buildTarget(plg *PlgConfig, target, outDir, sdkPath string) {
|
||||
// Write plugin.json with the correct entry for this target
|
||||
writePluginJSON(plg, nil, cfg.entryFile)
|
||||
|
||||
cmd := exec.Command("go", "build", "-buildmode=c-shared", "-o", outPath)
|
||||
// 普通 go build + 零 cgo:交叉编译不再需要目标平台的 C 工具链
|
||||
// (旧路径靠 detectWindowsCC 找 MinGW,现在整个问题消失)。
|
||||
cmd := exec.Command("go", "build", "-trimpath", "-o", outPath)
|
||||
cmd.Env = os.Environ()
|
||||
cmd.Env = append(cmd.Env, "GOOS="+cfg.goos, "GOARCH="+cfg.goarch, "CGO_ENABLED=1")
|
||||
|
||||
// Auto-detect MinGW gcc on Windows
|
||||
if cfg.goos == "windows" {
|
||||
cc := detectWindowsCC()
|
||||
if cc != "" {
|
||||
cmd.Env = append(cmd.Env, "CC="+cc)
|
||||
}
|
||||
}
|
||||
|
||||
cmd.Env = append(cmd.Env, "GOOS="+cfg.goos, "GOARCH="+cfg.goarch, "CGO_ENABLED=0")
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
// DEBUG: list files before building
|
||||
entries, _ := os.ReadDir(".")
|
||||
for _, e := range entries {
|
||||
fmt.Printf(" [DEBUG] file: %s\n", e.Name())
|
||||
}
|
||||
|
||||
fmt.Printf(" compiling %s/%s (-buildmode=c-shared)...\n", cfg.goos, cfg.goarch)
|
||||
fmt.Printf(" compiling %s/%s (子进程模式,CGO_ENABLED=0)...\n", cfg.goos, cfg.goarch)
|
||||
if err := cmd.Run(); err != nil {
|
||||
fmt.Printf(" error: build %s/%s: %v\n", cfg.goos, cfg.goarch, err)
|
||||
return
|
||||
@ -465,8 +578,8 @@ func buildTarget(plg *PlgConfig, target, outDir, sdkPath string) {
|
||||
}
|
||||
|
||||
type binEntry struct {
|
||||
src string // 磁盘路径,如 build/plugin.so
|
||||
zip string // zip 中条目名,如 plugin.so
|
||||
src string // 磁盘路径,如 build/plugin.bin
|
||||
zip string // zip 中条目名,如 plugin.bin.linux.amd64
|
||||
}
|
||||
|
||||
// createBundleHmap 创建包含多平台二进制的 bundle .hmap 文件。
|
||||
@ -566,72 +679,8 @@ func toSnake(s string) string {
|
||||
return strings.ToLower(strings.ReplaceAll(s, " ", "_"))
|
||||
}
|
||||
|
||||
// detectWindowsCC looks for a MinGW-w64 gcc on Windows for c-shared builds.
|
||||
func detectWindowsCC() string {
|
||||
// Check CC from environment first
|
||||
if cc := os.Getenv("CC"); cc != "" {
|
||||
if _, err := exec.LookPath(cc); err == nil {
|
||||
return cc
|
||||
}
|
||||
}
|
||||
// Check common MinGW install paths
|
||||
candidates := []string{
|
||||
"C:\\mingw64\\bin\\gcc.exe",
|
||||
"C:\\MinGW\\bin\\gcc.exe",
|
||||
"C:\\msys64\\mingw64\\bin\\gcc.exe",
|
||||
"C:\\Users\\21989\\AppData\\Local\\Temp\\mingw64\\mingw64\\bin\\gcc.exe",
|
||||
}
|
||||
// Also search PATH for gcc
|
||||
if path, err := exec.LookPath("gcc"); err == nil {
|
||||
return path
|
||||
}
|
||||
for _, c := range candidates {
|
||||
if _, err := os.Stat(c); err == nil {
|
||||
return c
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// stripIncludeGuard strips preprocessor guards and C++ comments from a C header,
|
||||
// since these can confuse cgo's type resolution.
|
||||
// generateBridge generates the C ABI bridge files for non-Lua builds.
|
||||
// Returns a cleanup function to remove generated files.
|
||||
func generateBridge(goos string) func() {
|
||||
const bridgeFile = "z_bridge_gen.go"
|
||||
const cEntryFile = "z_entry.c"
|
||||
os.Remove(bridgeFile)
|
||||
os.Remove(cEntryFile)
|
||||
|
||||
var files []string
|
||||
|
||||
if goos == "windows" {
|
||||
if err := os.WriteFile(bridgeFile, []byte(tmplBridge), 0644); err != nil {
|
||||
fmt.Printf(" error: write bridge: %v\n", err)
|
||||
return func() {}
|
||||
}
|
||||
files = append(files, bridgeFile)
|
||||
} else {
|
||||
if err := os.WriteFile(bridgeFile, []byte(tmplLinuxBridge), 0644); err != nil {
|
||||
fmt.Printf(" error: write bridge: %v\n", err)
|
||||
return func() {}
|
||||
}
|
||||
files = append(files, bridgeFile)
|
||||
// Write C entry point file
|
||||
if err := os.WriteFile(cEntryFile, []byte(tmplPluginInitC), 0644); err != nil {
|
||||
fmt.Printf(" error: write C entry: %v\n", err)
|
||||
return func() {}
|
||||
}
|
||||
files = append(files, cEntryFile)
|
||||
}
|
||||
|
||||
return func() {
|
||||
for _, f := range files {
|
||||
os.Remove(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// linkThirdpart scans thirdpart/, source_dirs from plg.json, and replace target dirs
|
||||
// for source files, generating auto-import stubs. Returns cleanup function.
|
||||
func linkThirdpart(plg *PlgConfig, target string) func() {
|
||||
|
||||
@ -7,8 +7,6 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"gitcode.com/JianFeeeee/homeagent-sdk/meta"
|
||||
)
|
||||
|
||||
func (p *PlgConfig) ReplacesToSlice() []string {
|
||||
@ -62,9 +60,13 @@ type TemplateData struct {
|
||||
SDKModule string
|
||||
SDKVersion string
|
||||
|
||||
// C ABI
|
||||
CABIVersion int
|
||||
CABIHeader string
|
||||
// SDKLocalPath 是本机 SDK 源码绝对路径,写入生成的 go.mod 作为 replace 目标。
|
||||
//
|
||||
// 为何必须写:gitcode 的模块不在 proxy.golang.org 上,只 require 一个
|
||||
// 版本号的 go.mod 配上缺失的 go.sum,新用户第一次 `plugindev build`
|
||||
// 必定死在 "missing go.sum entry",而 `go mod tidy` 又会去公共 proxy 拉
|
||||
// 一个不存在的条目。有了本地 replace,go 完全不需要 go.sum 条目。
|
||||
SDKLocalPath string
|
||||
}
|
||||
|
||||
func cmdInit(args []string) {
|
||||
@ -115,7 +117,12 @@ func cmdInit(args []string) {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
entry := "plugin.so"
|
||||
// Go 插件统一产出 plugin.bin(v1.0.0 子进程模式)。
|
||||
//
|
||||
// 此前这里写 "plugin.so",scaffold 出来的 plg.json 就带着一个已退场的
|
||||
// entry 值,新手跟着模板走会误以为自己在做 C ABI 插件。
|
||||
// build 实际不看这个值(只用它区分 Lua),但模板不应误导。
|
||||
entry := "plugin.bin"
|
||||
var targets string
|
||||
if isLua {
|
||||
entry = "main.lua"
|
||||
@ -139,20 +146,19 @@ func cmdInit(args []string) {
|
||||
Tags: []string{name},
|
||||
Targets: targets,
|
||||
},
|
||||
IsLua: isLua,
|
||||
CABIVersion: meta.CABINum,
|
||||
CABIHeader: tmplCABIHeader,
|
||||
IsLua: isLua,
|
||||
}
|
||||
|
||||
// Detect SDK info for Go plugin go.mod.
|
||||
// 生成的 go.mod 只 require SDK 线上模块版本,不写本地路径 replace;
|
||||
// 本地调试请用 `plugindev build --sdk-path <path>` 或手动加 replace。
|
||||
// 生成的 go.mod 除 require 外还写一条指向本机 SDK 的 replace:
|
||||
// 否则 scaffold 出来的项目第一次 build 必定失败(详见 SDKLocalPath 注释)。
|
||||
if !isLua {
|
||||
sdkMod, goVer, _, sdkVer := detectSDKInfo()
|
||||
sdkMod, goVer, sdkRoot, sdkVer := detectSDKInfo()
|
||||
data.ModulePath = name
|
||||
data.GoVersion = goVer
|
||||
data.SDKModule = sdkMod
|
||||
data.SDKVersion = "v" + sdkVer
|
||||
data.SDKLocalPath = strings.ReplaceAll(sdkRoot, "\\", "/")
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
|
||||
91
tools/plugindev/proc_runtime.go
Normal file
91
tools/plugindev/proc_runtime.go
Normal file
@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// 子进程插件运行时(外部插件多进程化)。
|
||||
//
|
||||
// 模板为何是**真实 .go 源文件** + //go:embed,而不是 raw string:
|
||||
// 1100+ 行代码塞在字符串里写错只能等生成插件时才炸;作为源文件可被
|
||||
// gofmt / go vet / go/parser 直接检查(proc_runtime_test.go 的 16 项
|
||||
// 静态检查就以此为前提)。
|
||||
//
|
||||
// 构建从 `-buildmode=c-shared` + CGO_ENABLED=1 变成普通 `go build` +
|
||||
// CGO_ENABLED=0,交叉编译不再需要目标平台的 C 工具链(§3.1 连带消失项)。
|
||||
//
|
||||
// 设计依据:docs/zh/架构迁移评估.md §3、docs/zh/plugin-migration-plan.md Part 3/6
|
||||
|
||||
//go:embed templates/proc_main.go.tmpl
|
||||
//go:embed templates/proc_shm_unix.go.tmpl
|
||||
//go:embed templates/proc_shm_windows.go.tmpl
|
||||
var procTemplates embed.FS
|
||||
|
||||
// procRuntimeFiles 列出生成到插件目录的运行时文件。
|
||||
//
|
||||
// 共享段与事件通知的**传递机制**按平台不同(Unix 继承 fd,
|
||||
// Windows 命名内核对象),故拆成带 build tag 的两个文件;
|
||||
// 共享段**布局**与 RPC 逻辑完全平台无关,全在 proc_main 里。
|
||||
//
|
||||
// 这正是三套独立 ABI 实现收敛为单一 RPC 实现的效果:
|
||||
// 平台差异从「整套 stage 下发/写回逻辑各写一份」缩到「三个挂载函数」。
|
||||
var procRuntimeFiles = []struct {
|
||||
tmpl string // 内嵌模板路径
|
||||
out string // 生成到插件目录的文件名
|
||||
}{
|
||||
{"templates/proc_main.go.tmpl", "z_proc_gen.go"},
|
||||
{"templates/proc_shm_unix.go.tmpl", "z_proc_shm_unix.go"},
|
||||
{"templates/proc_shm_windows.go.tmpl", "z_proc_shm_windows.go"},
|
||||
}
|
||||
|
||||
// procEntryFile 是子进程插件的入口二进制名(与内核 internal/plugin/dynamic.go 的 binEntry 一致)。
|
||||
//
|
||||
// 全平台同名:进程边界本身就是 ABI 边界,不存在平台特有的动态库扩展名
|
||||
// (对比 C ABI 时代的 .so/.dylib/.dll 三套产物 + 三套 ABI 实现)。
|
||||
const procEntryFile = "plugin.bin"
|
||||
|
||||
// luaEntryFile 是 Lua 插件的入口。Lua 走解释器,不经过 Go 编译。
|
||||
const luaEntryFile = "main.lua"
|
||||
|
||||
// procGenFile 是生成的主运行时文件名(兼容旧注释引用)。
|
||||
// 前缀 z_ 使其在目录列表中排在业务代码之后。
|
||||
const procGenFile = "z_proc_gen.go"
|
||||
|
||||
// generateProcRuntime 把子进程运行时(平台无关主体 + 两个平台挂载实现)
|
||||
// 写入插件目录,返回清理函数。
|
||||
func generateProcRuntime() (func(), error) {
|
||||
// 清理历史 C ABI 产物:旧版 plugindev 生成过这两个文件,残留下来会与
|
||||
// 本模板的 main 冲突。无需人工清理就能从旧版升级。
|
||||
for _, stale := range []string{"z_bridge_gen.go", "z_entry.c"} {
|
||||
os.Remove(stale)
|
||||
}
|
||||
|
||||
var written []string
|
||||
cleanup := func() {
|
||||
for _, f := range written {
|
||||
os.Remove(f)
|
||||
}
|
||||
}
|
||||
|
||||
for _, rf := range procRuntimeFiles {
|
||||
data, err := procTemplates.ReadFile(rf.tmpl)
|
||||
if err != nil {
|
||||
cleanup()
|
||||
return nil, fmt.Errorf("读取内嵌模板 %s: %w", rf.tmpl, err)
|
||||
}
|
||||
if err := os.WriteFile(rf.out, data, 0644); err != nil {
|
||||
cleanup()
|
||||
return nil, fmt.Errorf("写入 %s: %w", rf.out, err)
|
||||
}
|
||||
written = append(written, rf.out)
|
||||
}
|
||||
return cleanup, nil
|
||||
}
|
||||
|
||||
// isProcEntry 已删除:Go 插件一律产出 plugin.bin,不再看 plg.json 的 entry 值。
|
||||
//
|
||||
// 为何忽略 entry:17 个存量插件的 plg.json 都写着 "plugin.so"。若把 entry 当作
|
||||
// 通道开关,迁移就得改 17 个文件——而「外部插件零改动」是本次迁移的硬约束。
|
||||
// entry 现在只用于区分 Lua(main.lua)与 Go 插件。
|
||||
399
tools/plugindev/proc_runtime_test.go
Normal file
399
tools/plugindev/proc_runtime_test.go
Normal file
@ -0,0 +1,399 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 子进程运行时模板的静态检查(Part 3)。
|
||||
//
|
||||
// 为什么需要这些测试:模板是插件的运行时半身,它与内核 internal/plugin/proc/
|
||||
// 的协议名、共享段布局、字段索引必须逐一对齐。任一处漂移都会导致
|
||||
// 「插件编译通过但运行时读错字段」——比编译错误难查得多。
|
||||
//
|
||||
// 模板改为真实 .go 源文件(而非 raw string)的直接收益就是这类检查可行。
|
||||
|
||||
func loadProcTemplate(t *testing.T) string {
|
||||
t.Helper()
|
||||
data, err := procTemplates.ReadFile("templates/proc_main.go.tmpl")
|
||||
if err != nil {
|
||||
t.Fatalf("读取内嵌模板: %v", err)
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
// stripComments 去掉源码中的注释(用空白填充以保持偏移),只留可执行代码。
|
||||
func stripComments(t *testing.T, src string) string {
|
||||
t.Helper()
|
||||
fs := token.NewFileSet()
|
||||
f, err := parser.ParseFile(fs, "proc_main.go", src, parser.ParseComments)
|
||||
if err != nil {
|
||||
t.Fatalf("解析模板: %v", err)
|
||||
}
|
||||
out := []byte(src)
|
||||
for _, cg := range f.Comments {
|
||||
s := fs.Position(cg.Pos()).Offset
|
||||
e := fs.Position(cg.End()).Offset
|
||||
for i := s; i < e && i < len(out); i++ {
|
||||
if out[i] != '\n' {
|
||||
out[i] = ' '
|
||||
}
|
||||
}
|
||||
}
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// 模板必须是合法 Go 源码。
|
||||
func TestProcTemplate_ParsesAsGo(t *testing.T) {
|
||||
src := loadProcTemplate(t)
|
||||
fs := token.NewFileSet()
|
||||
if _, err := parser.ParseFile(fs, "proc_main.go", src, parser.AllErrors); err != nil {
|
||||
t.Fatalf("模板不是合法 Go 源码: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 模板必须提供 main(),且不得含 cgo 痕迹。
|
||||
//
|
||||
// 零 cgo 是迁移的核心收益之一(§3.7 锁仲裁回内核后整个架构无 cgo);
|
||||
// 一旦有人往模板里加 import "C",交叉编译立刻退回需要目标平台 C 工具链。
|
||||
func TestProcTemplate_HasMainAndNoCgo(t *testing.T) {
|
||||
src := loadProcTemplate(t)
|
||||
|
||||
if !strings.Contains(src, "func main()") {
|
||||
t.Error("子进程模板必须有 main() 入口")
|
||||
}
|
||||
// 只检查代码,不检查注释——模板顶部的说明文字本身就提到了 C.CString/C.free
|
||||
code := stripComments(t, src)
|
||||
for _, forbidden := range []string{
|
||||
`import "C"`,
|
||||
"//export ",
|
||||
"C.CString",
|
||||
"C.GoString",
|
||||
"C.free",
|
||||
} {
|
||||
if strings.Contains(code, forbidden) {
|
||||
t.Errorf("模板不应含 cgo 痕迹 %q(零 cgo 是迁移的核心收益)", forbidden)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 模板引用的 method 名必须与内核 internal/plugin/proc/protocol.go 一致。
|
||||
//
|
||||
// 这里硬编码一份清单做对照:内核侧改了 method 名而模板没跟上时,
|
||||
// 表现是插件调用返回「未知 method」,测试能提前拦住。
|
||||
func TestProcTemplate_CoversAllCoreMethods(t *testing.T) {
|
||||
src := loadProcTemplate(t)
|
||||
|
||||
// 51 个 C ABI method id 平移后的名字(§3.2),加 stage 锁仲裁 2 个
|
||||
required := []string{
|
||||
// 注册面
|
||||
"tool.register", "stage.register", "output.register", "api.register", "input.register",
|
||||
// IO 注入
|
||||
"io.injectText", "io.injectInterrupt", "io.injectTextNoMem", "io.injectInputSync",
|
||||
"io.setToolBlocks",
|
||||
// 多模态注入(1.1.0 新增)。漏接线的后果是插件调 InjectInputMedia 静默无效果:
|
||||
// 模板不发这个 RPC,内核也就永远收不到,而两边都不报错。
|
||||
"io.injectMedia", "io.injectMediaSync", "io.injectInterruptMedia",
|
||||
// 生命周期
|
||||
"lifecycle.autoRestart",
|
||||
// 图记忆
|
||||
"memory.recall", "memory.commit", "memory.introspect", "memory.merge", "memory.purge",
|
||||
// 文档记忆
|
||||
"doc.query", "doc.insert", "doc.remove", "doc.stats",
|
||||
// 文档媒体(1.1.0 新增)
|
||||
"doc.insertWithMedia",
|
||||
// 知识库
|
||||
"knowledge.search", "knowledge.add", "knowledge.list",
|
||||
// 文本记忆
|
||||
"textmemory.append",
|
||||
// 设置
|
||||
"settings.get", "settings.set", "settings.registerDef",
|
||||
"settings.getCore", "settings.setCore", "settings.listCore",
|
||||
"settings.getPlugin", "settings.setPlugin", "settings.listPlugin",
|
||||
"settings.list", "settings.defs", "settings.dump", "settings.plugins",
|
||||
"settings.dataDir",
|
||||
// LLM
|
||||
"llm.listSources", "llm.setSource", "llm.currentSource",
|
||||
// 社交图
|
||||
"social.getPerson", "social.getNetwork", "social.getTrait",
|
||||
"social.getRelations", "social.listPersons",
|
||||
// 插件管理
|
||||
"plugin.reloadOne", "plugin.listLoaded", "plugin.isDisabled",
|
||||
// 共享段锁仲裁(新增,C ABI 下不存在此概念)
|
||||
"stage.lock", "stage.unlock",
|
||||
}
|
||||
for _, m := range required {
|
||||
if !strings.Contains(src, `"`+m+`"`) {
|
||||
t.Errorf("模板缺少 core method %q(内核已提供,插件侧未接线)", m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 模板必须处理内核发来的全部 7 个调用(原 C ABI 的 7 个 //export)。
|
||||
func TestProcTemplate_HandlesAllKernelCalls(t *testing.T) {
|
||||
src := loadProcTemplate(t)
|
||||
for _, m := range []string{
|
||||
"handshake",
|
||||
"plugin.init", "plugin.start", "plugin.stop",
|
||||
"tool.invoke", "stage.invoke", "output.invoke",
|
||||
} {
|
||||
if !strings.Contains(src, `case "`+m+`"`) {
|
||||
t.Errorf("模板未处理内核调用 %q", m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 共享段布局常量必须与内核 internal/plugin/proc/shm.go 一致。
|
||||
//
|
||||
// 字段索引错位是最危险的漂移:插件会读到相邻字段的数据,
|
||||
// 而两边都不报错(同为 []byte)。
|
||||
func TestProcTemplate_ShmLayoutMatchesKernel(t *testing.T) {
|
||||
src := loadProcTemplate(t)
|
||||
|
||||
// 与内核 shm.go 的 offXxx 常量对齐(值比较,不依赖 gofmt 的对齐空白)
|
||||
layout := map[string]string{
|
||||
"shmOffMagic": "0",
|
||||
"shmOffVersion": "4",
|
||||
"shmOffArenaBase": "8",
|
||||
"shmOffArenaCap": "12",
|
||||
"shmOffArenaUsed": "16",
|
||||
"shmOffCtxBase": "20",
|
||||
"shmOffSeq": "24",
|
||||
// 与内核 stageFieldCount / sliceSize 对齐
|
||||
"shmStageFieldCount": "18",
|
||||
"shmSliceSize": "8",
|
||||
"shmVersion": "1",
|
||||
}
|
||||
constRe := func(name, want string) bool {
|
||||
// gofmt 会对齐常量块,故容许 name 与 = 之间有任意空白
|
||||
re := regexp.MustCompile(`\b` + regexp.QuoteMeta(name) + `\s*=\s*` + regexp.QuoteMeta(want) + `\b`)
|
||||
return re.MatchString(src)
|
||||
}
|
||||
for name, want := range layout {
|
||||
if !constRe(name, want) {
|
||||
t.Errorf("共享段常量 %s 应为 %s(须与内核 internal/plugin/proc/shm.go 一致)", name, want)
|
||||
}
|
||||
}
|
||||
|
||||
// 字段枚举顺序:内核 stageField 的前若干项
|
||||
fieldOrder := []string{
|
||||
"fRawMessage = iota", "fUserID", "fGroupID", "fLLMText",
|
||||
"fReasoningContent", "fFinalText", "fResponse", "fPhase",
|
||||
"fContextMsgs", "fToolCalls", "fToolResults", "fMemory",
|
||||
"fTokenUsage", "fErrors",
|
||||
"fExtraMediaBlocks", "fExtraMediaType", "fExtraInputSource", "fExtraOutputChannel",
|
||||
}
|
||||
idx := -1
|
||||
for _, f := range fieldOrder {
|
||||
at := strings.Index(src, f)
|
||||
if at < 0 {
|
||||
t.Fatalf("模板缺少字段常量 %s", f)
|
||||
}
|
||||
if at <= idx {
|
||||
t.Errorf("字段常量 %s 的声明顺序与内核 stageField 枚举不一致", f)
|
||||
}
|
||||
idx = at
|
||||
}
|
||||
}
|
||||
|
||||
// stage 处理必须「拿锁 → 读 → handler → 只写脏字段 → 放锁」。
|
||||
//
|
||||
// 只写脏字段是消除 lost update 的核心:只读插件零写入,
|
||||
// 不可能覆盖其他插件的改写(对照 C ABI 副本模型实测 35.8~36.8% 丢失)。
|
||||
func TestProcTemplate_StageFlowUsesLockAndDirtyWrite(t *testing.T) {
|
||||
src := loadProcTemplate(t)
|
||||
|
||||
for _, want := range []string{
|
||||
"func handleStageInvoke(",
|
||||
"stage.lock",
|
||||
"readStageContext()",
|
||||
"takeStageSnapshot(",
|
||||
"writeStageDirty(",
|
||||
"stage.unlock",
|
||||
} {
|
||||
if !strings.Contains(src, want) {
|
||||
t.Errorf("stage 处理链路缺少 %q", want)
|
||||
}
|
||||
}
|
||||
|
||||
// 顺序检查:加锁必须在读取之前,写回必须在解锁之前
|
||||
iLock := strings.Index(src, `callCoreVoid("stage.lock"`)
|
||||
iRead := strings.Index(src, "readStageContext()")
|
||||
iWrite := strings.Index(src, "writeStageDirty(sc, snap)")
|
||||
if iLock < 0 || iRead < 0 || iWrite < 0 {
|
||||
t.Fatal("stage 链路关键调用缺失")
|
||||
}
|
||||
// readStageContext 的定义在前,调用在后;取 handleStageInvoke 内的位置
|
||||
stageFn := src[strings.Index(src, "func handleStageInvoke("):]
|
||||
iLockFn := strings.Index(stageFn, `callCoreVoid("stage.lock"`)
|
||||
iReadFn := strings.Index(stageFn, "readStageContext()")
|
||||
iWriteFn := strings.Index(stageFn, "writeStageDirty(sc, snap)")
|
||||
if !(iLockFn < iReadFn && iReadFn < iWriteFn) {
|
||||
t.Error("stage 链路顺序应为 加锁 → 读取 → 写回")
|
||||
}
|
||||
}
|
||||
|
||||
// 快照必须存序列化字符串而非 Go 值。
|
||||
//
|
||||
// ❗ 这是修 C ABI 侧 11.3 时踩过的坑:StageContext 的切片字段与读出的值
|
||||
// 共享底层内容,handler 原地改元素(sc.ToolResults[0].Result = x)时,
|
||||
// 直接持有 Go 值的快照会跟着变,脏字段计算失效、修复静默失效。
|
||||
func TestProcTemplate_SnapshotStoresSerializedStrings(t *testing.T) {
|
||||
src := loadProcTemplate(t)
|
||||
|
||||
if !strings.Contains(src, "strs map[int]string") ||
|
||||
!strings.Contains(src, "jsons map[int]string") {
|
||||
t.Error("stageSnapshot 必须存序列化字符串(切片共享底层数组,存 Go 值会让脏字段计算失效)")
|
||||
}
|
||||
if !strings.Contains(src, "json.Marshal(v)") {
|
||||
t.Error("takeStageSnapshot 应对容器字段做 json.Marshal")
|
||||
}
|
||||
}
|
||||
|
||||
// arena 用尽必须显式报错,不得静默截断(§4.4 风险登记)。
|
||||
func TestProcTemplate_ArenaExhaustionErrors(t *testing.T) {
|
||||
src := loadProcTemplate(t)
|
||||
if !strings.Contains(src, "arena 空间不足") {
|
||||
t.Error("shmWrite 在 arena 不足时必须报错,不得静默截断")
|
||||
}
|
||||
}
|
||||
|
||||
// 日志必须走 stderr:stdout 是 RPC 通道,写日志会破坏 NDJSON 帧。
|
||||
func TestProcTemplate_LogsToStderr(t *testing.T) {
|
||||
src := loadProcTemplate(t)
|
||||
if !strings.Contains(src, "log.SetOutput(os.Stderr)") {
|
||||
t.Error("日志必须走 stderr,否则会破坏 stdout 的 RPC 帧")
|
||||
}
|
||||
}
|
||||
|
||||
// 请求必须在独立 goroutine 里处理。
|
||||
//
|
||||
// handler 内会反向调用内核并等应答;若在读循环里同步处理,
|
||||
// 就没人读应答帧 → 死锁。
|
||||
func TestProcTemplate_DispatchesRequestsConcurrently(t *testing.T) {
|
||||
src := loadProcTemplate(t)
|
||||
if !strings.Contains(src, "go handleKernelRequest(&req)") {
|
||||
t.Error("请求须在独立 goroutine 处理(handler 内反向调用内核,同步处理会死锁)")
|
||||
}
|
||||
}
|
||||
|
||||
// 协议与共享段版本不匹配必须拒绝,不得半兼容运行。
|
||||
func TestProcTemplate_RejectsVersionMismatch(t *testing.T) {
|
||||
src := loadProcTemplate(t)
|
||||
for _, want := range []string{"协议版本不匹配", "共享段版本不匹配", "共享段魔数不匹配"} {
|
||||
if !strings.Contains(src, want) {
|
||||
t.Errorf("握手应校验并拒绝 %q", want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 全平台统一产出 plugin.bin。
|
||||
//
|
||||
// 这是三套独立 ABI 实现(.so/.dylib/.dll)收敛为单一 RPC 实现的直接后果:
|
||||
// 进程边界本身就是 ABI 边界,不存在平台特有的动态库扩展名。
|
||||
// §9.2 记录的「Windows DLL 路径只下发 3 字段、无写回」随之消失——
|
||||
// Windows 走的是与 Linux 完全相同的 RPC 实现。
|
||||
func TestResolveBuild_AllPlatformsProduceBin(t *testing.T) {
|
||||
for _, target := range []string{
|
||||
"linux/amd64", "linux/arm64",
|
||||
"darwin/amd64", "darwin/arm64",
|
||||
"windows/amd64",
|
||||
"freebsd/amd64",
|
||||
} {
|
||||
cfg, errMsg := resolveBuild(target)
|
||||
if cfg == nil {
|
||||
t.Fatalf("resolveBuild(%q) 失败: %s", target, errMsg)
|
||||
}
|
||||
if cfg.entryFile != procEntryFile {
|
||||
t.Errorf("%s: 产物应为 %s,实际 %s", target, procEntryFile, cfg.entryFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// lua 目标仍走解释器路径(entry 字段唯一仍在使用的用途)。
|
||||
func TestResolveBuild_LuaIsSeparatePath(t *testing.T) {
|
||||
for _, target := range []string{"lua", ""} {
|
||||
cfg, kind := resolveBuild(target)
|
||||
if cfg != nil {
|
||||
t.Errorf("%q 应返回 nil cfg(Lua 不经 Go 编译)", target)
|
||||
}
|
||||
if kind != "lua" {
|
||||
t.Errorf("%q 应识别为 lua,实际 %q", target, kind)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 不支持的平台明确报错,不静默产出错误产物。
|
||||
func TestResolveBuild_UnsupportedOSErrors(t *testing.T) {
|
||||
cfg, errMsg := resolveBuild("plan9/amd64")
|
||||
if cfg != nil {
|
||||
t.Error("不支持的平台应返回 nil cfg")
|
||||
}
|
||||
if !strings.Contains(errMsg, "unsupported") {
|
||||
t.Errorf("应给出 unsupported 提示,实际 %q", errMsg)
|
||||
}
|
||||
}
|
||||
|
||||
// bundle 产物在 zip 内按平台加后缀(全平台同名 plugin.bin 会相互覆盖)。
|
||||
func TestBundleTargets_HavePlatformSuffixedEntries(t *testing.T) {
|
||||
seen := map[string]bool{}
|
||||
for _, bt := range allBundleTargets {
|
||||
if seen[bt.entry] {
|
||||
t.Errorf("zip 条目名重复: %s(会相互覆盖)", bt.entry)
|
||||
}
|
||||
seen[bt.entry] = true
|
||||
if !strings.HasPrefix(bt.entry, procEntryFile+".") {
|
||||
t.Errorf("bundle 条目 %q 应以 %s. 为前缀", bt.entry, procEntryFile)
|
||||
}
|
||||
}
|
||||
if len(allBundleTargets) == 0 {
|
||||
t.Error("bundle 目标表不应为空")
|
||||
}
|
||||
}
|
||||
|
||||
// C ABI 工具链残留必须彻底清除:不得再有 .so/.dylib/.dll 产物路径,
|
||||
// 也不得再引用 c-shared 构建模式或 MinGW 探测。
|
||||
func TestToolchain_NoCABIResiduals(t *testing.T) {
|
||||
for _, f := range []string{"cmd_build.go", "templates.go", "cmd_init.go", "proc_runtime.go"} {
|
||||
data, err := os.ReadFile(f)
|
||||
if err != nil {
|
||||
t.Fatalf("读 %s: %v", f, err)
|
||||
}
|
||||
src := stripComments(t, string(data))
|
||||
for _, forbidden := range []string{
|
||||
"c-shared",
|
||||
"CGO_ENABLED=1",
|
||||
"detectWindowsCC",
|
||||
"generateBridge",
|
||||
"tmplLinuxBridge",
|
||||
"tmplPluginInitC",
|
||||
} {
|
||||
if strings.Contains(src, forbidden) {
|
||||
t.Errorf("%s 仍含 C ABI 残留 %q", f, forbidden)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Go 插件的构建不再读 plg.json 的 entry 值。
|
||||
//
|
||||
// 这是「外部插件零改动」的关键:17 个存量插件的 plg.json 都写着 "plugin.so",
|
||||
// 若把 entry 当通道开关,迁移就得改 17 个文件。
|
||||
func TestToolchain_IgnoresEntryForGoPlugins(t *testing.T) {
|
||||
data, err := os.ReadFile("cmd_build.go")
|
||||
if err != nil {
|
||||
t.Fatalf("读 cmd_build.go: %v", err)
|
||||
}
|
||||
src := stripComments(t, string(data))
|
||||
if strings.Contains(src, "isProcEntry") {
|
||||
t.Error("isProcEntry 应已删除——Go 插件一律产出 plugin.bin,不看 entry 值")
|
||||
}
|
||||
// entry 仅剩 Lua 判定这一处用途
|
||||
if !strings.Contains(src, "luaEntryFile") {
|
||||
t.Error("IsLua 应改用 luaEntryFile 常量")
|
||||
}
|
||||
}
|
||||
247
tools/plugindev/stagediff_test.go
Normal file
247
tools/plugindev/stagediff_test.go
Normal file
@ -0,0 +1,247 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
sdk "gitcode.com/JianFeeeee/homeagent-sdk/sdk"
|
||||
)
|
||||
|
||||
// 本测试验证 tmplLinuxBridge 中 snapshotWritable + changedFieldsOnly 的语义(plan.md 11.3)。
|
||||
// 模板字符串本身无法直接单测,这里以同一份逻辑复刻,防止回归。
|
||||
// ❗ 模板与本文件须同步修改。
|
||||
//
|
||||
// 关键陷阱(第一版实现踩过):stageContextWritable 返回的切片字段与 sc 共享底层数组,
|
||||
// handler 原地改元素时"before 快照"会跟着变,diff 看不到变更 → 修复静默失效。
|
||||
// 故 before 必须是**序列化后的字符串快照**。
|
||||
|
||||
func writable(sc *sdk.StageContext) map[string]interface{} {
|
||||
m := map[string]interface{}{
|
||||
"raw_message": sc.RawMessage,
|
||||
"user_id": sc.UserID,
|
||||
"group_id": sc.GroupID,
|
||||
"phase": string(sc.Phase),
|
||||
"llm_text": sc.LLMText,
|
||||
"final_text": sc.FinalText,
|
||||
"no_memory": sc.NoMemory,
|
||||
}
|
||||
if sc.Response != nil {
|
||||
m["response"] = *sc.Response
|
||||
}
|
||||
if len(sc.ToolCalls) > 0 {
|
||||
m["tool_calls"] = sc.ToolCalls
|
||||
}
|
||||
if len(sc.ToolResults) > 0 {
|
||||
m["tool_results"] = sc.ToolResults
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// snapshot 对应模板里的 snapshotWritable:逐字段序列化为不可变快照。
|
||||
func snapshot(sc *sdk.StageContext) map[string]string {
|
||||
snap := map[string]string{}
|
||||
for k, v := range writable(sc) {
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
snap[k] = string(b)
|
||||
}
|
||||
return snap
|
||||
}
|
||||
|
||||
// diffOnly 对应模板里的 changedFieldsOnly。
|
||||
func diffOnly(before map[string]string, after map[string]interface{}) map[string]interface{} {
|
||||
diff := map[string]interface{}{}
|
||||
keys := map[string]bool{}
|
||||
for k := range before {
|
||||
keys[k] = true
|
||||
}
|
||||
for k := range after {
|
||||
keys[k] = true
|
||||
}
|
||||
for k := range keys {
|
||||
bRaw, bHas := before[k]
|
||||
a, aHas := after[k]
|
||||
switch {
|
||||
case aHas && !bHas:
|
||||
diff[k] = a
|
||||
case aHas && bHas:
|
||||
ab, _ := json.Marshal(a)
|
||||
if bRaw != string(ab) {
|
||||
diff[k] = a
|
||||
}
|
||||
case bHas && !aHas:
|
||||
switch k {
|
||||
case "tool_calls":
|
||||
diff[k] = []sdk.ToolCall{}
|
||||
case "tool_results":
|
||||
diff[k] = []sdk.ToolResult{}
|
||||
}
|
||||
}
|
||||
}
|
||||
return diff
|
||||
}
|
||||
|
||||
// 只读插件(如 weather 的 AfterToolcall)不改任何字段 → 零回传。
|
||||
// 这是修复 lost update 的关键:旧实现会回传它收到的旧快照,覆盖 sanitizer 的清洗结果。
|
||||
func TestChangedFieldsOnly_ReadOnlyPluginReturnsNothing(t *testing.T) {
|
||||
sc := &sdk.StageContext{
|
||||
RawMessage: "hello",
|
||||
LLMText: "world",
|
||||
ToolResults: []sdk.ToolResult{
|
||||
{CallID: "c1", Name: "weather_query", Success: true, Result: "已清洗结果"},
|
||||
},
|
||||
}
|
||||
before := snapshot(sc)
|
||||
// 只读 handler:读了但没改
|
||||
_ = sc.ToolResults[0].Result
|
||||
diff := diffOnly(before, writable(sc))
|
||||
|
||||
if len(diff) != 0 {
|
||||
t.Fatalf("只读插件应零回传,实际回传 %d 个字段: %v", len(diff), diff)
|
||||
}
|
||||
}
|
||||
|
||||
// 改写插件(如 sanitizer 改 ToolResults)→ 只回传被改的字段。
|
||||
// ⚠️ 这里是原地改切片元素,正是共享底层数组陷阱的触发场景。
|
||||
func TestChangedFieldsOnly_WriterReturnsOnlyChanged(t *testing.T) {
|
||||
sc := &sdk.StageContext{
|
||||
RawMessage: "hello",
|
||||
LLMText: "world",
|
||||
ToolResults: []sdk.ToolResult{
|
||||
{CallID: "c1", Name: "weather_query", Success: true, Result: "带\x1b[31mANSI\x1b[0m脏数据"},
|
||||
},
|
||||
}
|
||||
before := snapshot(sc)
|
||||
// sanitizer handler:原地清洗 ToolResults
|
||||
sc.ToolResults[0].Result = "带ANSI脏数据"
|
||||
diff := diffOnly(before, writable(sc))
|
||||
|
||||
if len(diff) != 1 {
|
||||
t.Fatalf("应只回传 tool_results 一个字段,实际 %d 个: %v", len(diff), diff)
|
||||
}
|
||||
if _, ok := diff["tool_results"]; !ok {
|
||||
t.Fatalf("回传字段应为 tool_results,实际 %v", diff)
|
||||
}
|
||||
// raw_message / llm_text 未改,不应出现(否则会覆盖其他插件的改写)
|
||||
if _, ok := diff["raw_message"]; ok {
|
||||
t.Error("raw_message 未改却被回传(会覆盖其他插件的改写)")
|
||||
}
|
||||
if _, ok := diff["llm_text"]; ok {
|
||||
t.Error("llm_text 未改却被回传")
|
||||
}
|
||||
}
|
||||
|
||||
// 改写标量字段(如 before_output 改 FinalText)→ 只回传该字段。
|
||||
func TestChangedFieldsOnly_ScalarChange(t *testing.T) {
|
||||
sc := &sdk.StageContext{
|
||||
RawMessage: "hi",
|
||||
FinalText: " 带空白的回复 ",
|
||||
LLMText: "原始",
|
||||
}
|
||||
before := snapshot(sc)
|
||||
sc.FinalText = "带空白的回复"
|
||||
diff := diffOnly(before, writable(sc))
|
||||
|
||||
if len(diff) != 1 || diff["final_text"] != "带空白的回复" {
|
||||
t.Fatalf("应只回传 final_text,实际 %v", diff)
|
||||
}
|
||||
}
|
||||
|
||||
// 首次设置 response(短路)→ 回传。
|
||||
func TestChangedFieldsOnly_NewResponseIsReturned(t *testing.T) {
|
||||
sc := &sdk.StageContext{RawMessage: "hi"}
|
||||
before := snapshot(sc)
|
||||
resp := "被插件短路"
|
||||
sc.Response = &resp
|
||||
diff := diffOnly(before, writable(sc))
|
||||
|
||||
if v, ok := diff["response"]; !ok || v != "被插件短路" {
|
||||
t.Fatalf("新设置的 response 应回传,实际 %v", diff)
|
||||
}
|
||||
}
|
||||
|
||||
// 清空切片字段 → 显式回传空值让内核跟随。
|
||||
func TestChangedFieldsOnly_ClearedSliceIsReturnedAsEmpty(t *testing.T) {
|
||||
sc := &sdk.StageContext{
|
||||
ToolCalls: []sdk.ToolCall{{ID: "t1", Name: "cmd_run"}},
|
||||
}
|
||||
before := snapshot(sc)
|
||||
sc.ToolCalls = nil // 插件拒绝了全部工具调用
|
||||
diff := diffOnly(before, writable(sc))
|
||||
|
||||
v, ok := diff["tool_calls"]
|
||||
if !ok {
|
||||
t.Fatalf("清空 tool_calls 应显式回传空值,实际 %v", diff)
|
||||
}
|
||||
if arr, _ := v.([]sdk.ToolCall); len(arr) != 0 {
|
||||
t.Fatalf("应回传空切片,实际 %v", v)
|
||||
}
|
||||
}
|
||||
|
||||
// 复刻现网场景(实验 13):sanitizer 清洗后 weather 只读回传,清洗结果不得被覆盖。
|
||||
// 旧实现下 weather 会回传自己收到的旧快照(含脏数据),覆盖 sanitizer 的清洗(丢失率 1.6~4.3%)。
|
||||
func TestChangedFieldsOnly_ProductionScenarioNoOverwrite(t *testing.T) {
|
||||
dirty := "天气:晴 \x1b[31m28°C\x1b[0m"
|
||||
clean := "天气:晴 28°C"
|
||||
|
||||
// 内核下发的原始快照(两插件各拿到一份副本)
|
||||
kernelSnapshot := map[string]interface{}{
|
||||
"raw_message": "查天气",
|
||||
"llm_text": "",
|
||||
"final_text": "",
|
||||
"user_id": "u1",
|
||||
"group_id": "",
|
||||
"phase": "after_toolcall",
|
||||
"no_memory": false,
|
||||
"tool_results": []sdk.ToolResult{{CallID: "c1", Name: "weather_query", Result: dirty}},
|
||||
}
|
||||
|
||||
// sanitizer 副本:清洗
|
||||
scSan := &sdk.StageContext{
|
||||
RawMessage: "查天气",
|
||||
UserID: "u1",
|
||||
Phase: sdk.StageAfterToolcall,
|
||||
ToolResults: []sdk.ToolResult{{CallID: "c1", Name: "weather_query", Result: dirty}},
|
||||
}
|
||||
beforeSan := snapshot(scSan)
|
||||
scSan.ToolResults[0].Result = clean
|
||||
diffSan := diffOnly(beforeSan, writable(scSan))
|
||||
|
||||
// weather 副本:只读,不改
|
||||
scWea := &sdk.StageContext{
|
||||
RawMessage: "查天气",
|
||||
UserID: "u1",
|
||||
Phase: sdk.StageAfterToolcall,
|
||||
ToolResults: []sdk.ToolResult{{CallID: "c1", Name: "weather_query", Result: dirty}},
|
||||
}
|
||||
beforeWea := snapshot(scWea)
|
||||
diffWea := diffOnly(beforeWea, writable(scWea))
|
||||
|
||||
// weather 必须零回传,否则它的旧快照会覆盖 sanitizer 的清洗
|
||||
if len(diffWea) != 0 {
|
||||
t.Fatalf("weather 只读却回传 %v —— 会覆盖 sanitizer 清洗结果", diffWea)
|
||||
}
|
||||
// sanitizer 必须回传 tool_results
|
||||
if _, ok := diffSan["tool_results"]; !ok {
|
||||
t.Fatalf("sanitizer 改写了 tool_results 却未回传:%v", diffSan)
|
||||
}
|
||||
|
||||
// 内核按 sanitizer → weather 顺序应用 diff(weather 后到,是最坏情形)
|
||||
kernel := map[string]interface{}{}
|
||||
for k, v := range kernelSnapshot {
|
||||
kernel[k] = v
|
||||
}
|
||||
for k, v := range diffSan {
|
||||
kernel[k] = v
|
||||
}
|
||||
for k, v := range diffWea {
|
||||
kernel[k] = v
|
||||
}
|
||||
|
||||
res, _ := kernel["tool_results"].([]sdk.ToolResult)
|
||||
if len(res) == 0 || res[0].Result != clean {
|
||||
t.Fatalf("清洗结果被覆盖:期望 %q,实际 %v", clean, kernel["tool_results"])
|
||||
}
|
||||
}
|
||||
@ -19,7 +19,12 @@ const tmplGoMod = `module {{.ModulePath}}
|
||||
go {{.GoVersion}}
|
||||
|
||||
require {{.SDKModule}} {{.SDKVersion}}
|
||||
`
|
||||
{{if .SDKLocalPath}}
|
||||
// SDK 指向本机源码。gitcode 的模块不在 proxy.golang.org 上,
|
||||
// 没有这条 replace 就需要 go.sum 条目,而那个条目无处可拉。
|
||||
// 若你已有可访问的私有 proxy,可删掉本行。
|
||||
replace {{.SDKModule}} => {{.SDKLocalPath}}
|
||||
{{end}}`
|
||||
|
||||
const tmplPluginGo = `package main
|
||||
|
||||
@ -152,715 +157,6 @@ function plugin.stop() sdk.log("info", "{{.Plg.Name}} stopped") end
|
||||
return plugin
|
||||
`
|
||||
|
||||
// tmplBridge — Windows DLL C ABI bridge (unchanged)
|
||||
const tmplBridge = `//go:build windows && cgo
|
||||
|
||||
package main
|
||||
|
||||
/*
|
||||
#include <stdlib.h>
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
"sync"
|
||||
"unsafe"
|
||||
sdk "gitcode.com/JianFeeeee/homeagent-sdk/sdk"
|
||||
)
|
||||
|
||||
var (
|
||||
mu sync.Mutex
|
||||
handleMap = map[unsafe.Pointer]*bridgeState{}
|
||||
)
|
||||
|
||||
type bridgeState struct {
|
||||
plugin sdk.Plugin
|
||||
toolDefs map[string]sdk.ToolDef
|
||||
handlers map[string]sdk.ToolHandler
|
||||
stages map[string]sdk.StageHandler
|
||||
settings map[string]interface{}
|
||||
sdk *sdk.PluginSDK
|
||||
}
|
||||
|
||||
func newHandle(plg sdk.Plugin) unsafe.Pointer {
|
||||
mu.Lock(); defer mu.Unlock()
|
||||
h := C.malloc(C.size_t(1))
|
||||
handleMap[h] = &bridgeState{
|
||||
plugin: plg, toolDefs: make(map[string]sdk.ToolDef),
|
||||
handlers: make(map[string]sdk.ToolHandler), stages: make(map[string]sdk.StageHandler),
|
||||
settings: make(map[string]interface{}),
|
||||
}
|
||||
return h
|
||||
}
|
||||
func getState(h unsafe.Pointer) *bridgeState { mu.Lock(); defer mu.Unlock(); return handleMap[h] }
|
||||
func delState(h unsafe.Pointer) { mu.Lock(); defer mu.Unlock(); delete(handleMap, h); C.free(h) }
|
||||
|
||||
//export NewPlugin
|
||||
func NewPlugin(name *C.char, configJSON *C.char) unsafe.Pointer {
|
||||
goName := C.GoString(name)
|
||||
var config map[string]interface{}
|
||||
if configJSON != nil {
|
||||
var wrapper map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(C.GoString(configJSON)), &wrapper); err == nil {
|
||||
if c, ok := wrapper["config"].(map[string]interface{}); ok { config = c }
|
||||
}
|
||||
}
|
||||
plg, err := NewPluginFactory(goName, config)
|
||||
if err != nil { return nil }
|
||||
return newHandle(plg)
|
||||
}
|
||||
|
||||
//export StartPlugin
|
||||
func StartPlugin(handle unsafe.Pointer) C.int {
|
||||
bs := getState(handle)
|
||||
if bs == nil { return 1 }
|
||||
mockSett := &bridgeSettings{data: bs.settings}
|
||||
mockSDK := sdk.New(bs.plugin.Name(), mockSett,
|
||||
func(name string, def sdk.ToolDef, handler sdk.ToolHandler) error {
|
||||
bs.toolDefs[name] = def; bs.handlers[name] = handler; return nil
|
||||
},
|
||||
func(stage sdk.Stage, handler sdk.StageHandler) { bs.stages[string(stage)] = handler },
|
||||
func(name string) error { return nil },
|
||||
func(name string, caps int, desc string, def sdk.ChannelDef, handler sdk.ToolHandler) error { return nil },
|
||||
)
|
||||
mockSDK.SetInputChannelRegistrar(func(name string, def sdk.ChannelDef) error { return nil })
|
||||
bs.sdk = mockSDK
|
||||
if err := bs.plugin.Start(mockSDK); err != nil { return 1 }
|
||||
return 0
|
||||
}
|
||||
|
||||
//export StopPlugin
|
||||
func StopPlugin(handle unsafe.Pointer) C.int {
|
||||
bs := getState(handle)
|
||||
if bs == nil { return 1 }
|
||||
if bs.sdk != nil {
|
||||
bs.sdk.RunStopHandlers()
|
||||
}
|
||||
if err := bs.plugin.Stop(); err != nil { return 1 }
|
||||
return 0
|
||||
}
|
||||
|
||||
//export DestroyPlugin
|
||||
func DestroyPlugin(handle unsafe.Pointer) {
|
||||
if bs := getState(handle); bs != nil { delState(handle) }
|
||||
}
|
||||
|
||||
//export GetToolDefsJSON
|
||||
func GetToolDefsJSON(handle unsafe.Pointer) *C.char {
|
||||
bs := getState(handle)
|
||||
if bs == nil { return nil }
|
||||
defs := make([]sdk.ToolDef, 0, len(bs.toolDefs))
|
||||
for _, def := range bs.toolDefs { defs = append(defs, def) }
|
||||
b, _ := json.Marshal(defs)
|
||||
return C.CString(string(b))
|
||||
}
|
||||
|
||||
//export InvokeToolJSON
|
||||
func InvokeToolJSON(handle unsafe.Pointer, toolName *C.char, argsJSON *C.char) *C.char {
|
||||
bs := getState(handle)
|
||||
if bs == nil || toolName == nil { return nil }
|
||||
goName := C.GoString(toolName)
|
||||
handler, ok := bs.handlers[goName]
|
||||
if !ok { errMsg, _ := json.Marshal(map[string]interface{}{"error": "tool not found: " + goName}); return C.CString(string(errMsg)) }
|
||||
var args map[string]interface{}
|
||||
if argsJSON != nil { json.Unmarshal([]byte(C.GoString(argsJSON)), &args) }
|
||||
r, err := handler(args)
|
||||
if err != nil { errMsg, _ := json.Marshal(map[string]interface{}{"error": err.Error()}); return C.CString(string(errMsg)) }
|
||||
b, _ := json.Marshal(r)
|
||||
return C.CString(string(b))
|
||||
}
|
||||
|
||||
//export GetStagesJSON
|
||||
func GetStagesJSON(handle unsafe.Pointer) *C.char {
|
||||
bs := getState(handle)
|
||||
if bs == nil { return nil }
|
||||
type se struct { Stage string ` + "`" + `json:"stage"` + "`" + ` }
|
||||
var entries []se
|
||||
for s := range bs.stages { entries = append(entries, se{s}) }
|
||||
b, _ := json.Marshal(entries)
|
||||
return C.CString(string(b))
|
||||
}
|
||||
|
||||
//export InvokeStage
|
||||
func InvokeStage(handle unsafe.Pointer, stage *C.char, contextJSON *C.char) C.int {
|
||||
bs := getState(handle)
|
||||
if bs == nil || stage == nil { return 1 }
|
||||
goStage := C.GoString(stage)
|
||||
handler, ok := bs.stages[goStage]
|
||||
if !ok { return 1 }
|
||||
var ctx map[string]interface{}
|
||||
if contextJSON != nil { json.Unmarshal([]byte(C.GoString(contextJSON)), &ctx) }
|
||||
sc := &sdk.StageContext{}
|
||||
if ctx != nil {
|
||||
if v, ok := ctx["raw_message"].(string); ok { sc.RawMessage = v }
|
||||
if v, ok := ctx["user_id"].(string); ok { sc.UserID = v }
|
||||
if v, ok := ctx["phase"].(string); ok { sc.Phase = sdk.Stage(v) }
|
||||
}
|
||||
if err := handler(sc); err != nil { return 1 }
|
||||
return 0
|
||||
}
|
||||
|
||||
//export FreeCString
|
||||
func FreeCString(s *C.char) { C.free(unsafe.Pointer(s)) }
|
||||
|
||||
type bridgeSettings struct{ data map[string]interface{} }
|
||||
func (s *bridgeSettings) Get(key string) (interface{}, error) { v, ok := s.data[key]; if !ok { return nil, nil }; return v, nil }
|
||||
func (s *bridgeSettings) Set(key string, value interface{}) error { s.data[key] = value; return nil }
|
||||
func (s *bridgeSettings) List(prefix string) ([]string, error) {
|
||||
var keys []string
|
||||
for k := range s.data { if len(k) >= len(prefix) && k[:len(prefix)] == prefix { keys = append(keys, k) } }
|
||||
return keys, nil
|
||||
}
|
||||
func (s *bridgeSettings) GetCore(key string) (interface{}, error) { return nil, nil }
|
||||
func (s *bridgeSettings) SetCore(key string, value interface{}) error { return nil }
|
||||
func (s *bridgeSettings) ListCore(prefix string) ([]string, error) { return nil, nil }
|
||||
func (s *bridgeSettings) GetPlugin(plugin, key string) (interface{}, error) { return nil, nil }
|
||||
func (s *bridgeSettings) SetPlugin(plugin, key string, value interface{}) error { return nil }
|
||||
func (s *bridgeSettings) ListPlugin(plugin, prefix string) ([]string, error) { return nil, nil }
|
||||
func (s *bridgeSettings) RegisterDef(def sdk.ConfigDef) {}
|
||||
func (s *bridgeSettings) Defs(prefix string) []*sdk.ConfigDef { return nil }
|
||||
func (s *bridgeSettings) Dump() map[string]interface{} { return s.data }
|
||||
func (s *bridgeSettings) Plugins() []string { return nil }
|
||||
|
||||
func main() {}
|
||||
`
|
||||
|
||||
// tmplCABIHeader — shared C ABI type definitions for both core and plugin
|
||||
// 此模板中的常量应与 core/internal/meta/meta.go 保持一致(ABI 版本、dispatch method IDs)。
|
||||
const tmplCABIHeader = `
|
||||
#ifndef HOMEAGENT_CABI_H
|
||||
#define HOMEAGENT_CABI_H
|
||||
// HOMEAGENT_ABI_VERSION 与 sdk/meta/meta.go CABINum 同步(major*100+minor,v0.9.x→900)
|
||||
#define HOMEAGENT_ABI_VERSION 900
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// PluginAPI — implemented by the plugin, called by the core
|
||||
typedef struct {
|
||||
int version; int version_min;
|
||||
int (*init_plugin)(char*, char*, char**);
|
||||
int (*start_plugin)(void*, int, char**);
|
||||
int (*stop_plugin)(char**);
|
||||
int (*invoke_tool)(char*, char*, char**, char**);
|
||||
int (*invoke_stage)(char*, char*, char**, char**);
|
||||
int (*invoke_output)(char*, char*, char*, char**);
|
||||
void (*free_string)(char*);
|
||||
} PluginAPI;
|
||||
|
||||
// CoreAPI — implemented by the core, passed to plugin via start_plugin
|
||||
// Uses single dispatch function to avoid function pointer ABI issues
|
||||
typedef struct {
|
||||
int version; int version_min;
|
||||
int (*dispatch)(int method_id, void* ctx, char* s1, char* s2, char* s3, int i1, int i2, char** result, char** error);
|
||||
void* ctx;
|
||||
} CoreAPI;
|
||||
|
||||
// Dispatch method IDs (plugin→core SDK calls)
|
||||
enum {
|
||||
CORE_REGISTER_TOOL = 1,
|
||||
CORE_REGISTER_STAGE = 2,
|
||||
CORE_REGISTER_OUTPUT_CH = 3,
|
||||
CORE_REGISTER_PLUGIN_API = 4,
|
||||
CORE_INJECT_TEXT = 5,
|
||||
CORE_INJECT_INTERRUPT_TEXT = 6,
|
||||
CORE_INJECT_TEXT_NO_MEMORY = 7,
|
||||
CORE_INJECT_INPUT_SYNC = 47,
|
||||
CORE_SET_AUTO_RESTART = 8,
|
||||
CORE_MEMORY_RECALL = 9,
|
||||
CORE_MEMORY_COMMIT = 10,
|
||||
CORE_MEMORY_INTROSPECT = 11,
|
||||
CORE_MEMORY_MERGE = 12,
|
||||
CORE_MEMORY_PURGE = 13,
|
||||
CORE_DOC_QUERY = 14,
|
||||
CORE_KNOWLEDGE_SEARCH = 15,
|
||||
CORE_SETTINGS_GET = 16,
|
||||
CORE_SETTINGS_SET = 17,
|
||||
CORE_SETTINGS_REGISTER_DEF = 18,
|
||||
CORE_LLM_LIST_SOURCES = 19,
|
||||
CORE_LLM_SET_SOURCE = 20,
|
||||
CORE_SOCIAL_GET_PERSON = 21,
|
||||
CORE_SOCIAL_GET_NETWORK = 22,
|
||||
CORE_SUBSCRIBE = 23,
|
||||
CORE_UNSUBSCRIBE = 24,
|
||||
CORE_FREE_STRING = 25,
|
||||
CORE_SETTINGS_GET_CORE = 26,
|
||||
CORE_SETTINGS_SET_CORE = 27,
|
||||
CORE_SETTINGS_LIST_CORE = 28,
|
||||
CORE_SETTINGS_GET_PLUGIN = 29,
|
||||
CORE_SETTINGS_SET_PLUGIN = 30,
|
||||
CORE_SETTINGS_LIST_PLUGIN = 31,
|
||||
CORE_DOC_INSERT = 32,
|
||||
CORE_DOC_REMOVE = 33,
|
||||
CORE_DOC_STATS = 34,
|
||||
CORE_KNOWLEDGE_ADD = 35,
|
||||
CORE_KNOWLEDGE_LIST = 36,
|
||||
CORE_LLM_CURRENT_SOURCE = 37,
|
||||
CORE_SOCIAL_GET_TRAIT = 38,
|
||||
CORE_SOCIAL_GET_RELATIONS = 39,
|
||||
CORE_SOCIAL_LIST_PERSONS = 40,
|
||||
CORE_TEXT_MEMORY_APPEND = 41,
|
||||
CORE_SETTINGS_LIST = 42,
|
||||
CORE_SETTINGS_DEFS = 43,
|
||||
CORE_SETTINGS_DUMP = 44,
|
||||
CORE_SETTINGS_PLUGINS = 45,
|
||||
CORE_REGISTER_INPUT_CH = 46,
|
||||
CORE_INJECT_INPUT_SYNC = 47,
|
||||
CORE_PLUGIN_RELOAD_ONE = 48,
|
||||
CORE_PLUGIN_LIST_LOADED = 49,
|
||||
CORE_PLUGIN_IS_DISABLED = 50,
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
`
|
||||
|
||||
// tmplLinuxBridge — auto-generated Go bridge for Linux c-shared builds.
|
||||
// Called by plugin's Start() with a PluginSDK that wraps CoreAPI dispatch.
|
||||
// PluginSDK calls go through C ABI → CoreAPI dispatch → core's Go PluginSDK.
|
||||
const tmplLinuxBridge = `package main
|
||||
|
||||
/*
|
||||
#include <stdlib.h>
|
||||
int ha_dispatch(int method_id, void* core_api, char* s1, char* s2, char* s3, int i1, int i2, char** result, char** error);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
"unsafe"
|
||||
sdk "gitcode.com/JianFeeeee/homeagent-sdk/sdk"
|
||||
)
|
||||
|
||||
// ---- global state ----
|
||||
|
||||
var (
|
||||
mu sync.Mutex
|
||||
currentPlg sdk.Plugin
|
||||
currentSDK *sdk.PluginSDK
|
||||
coreAPI unsafe.Pointer
|
||||
|
||||
handlerMu sync.RWMutex
|
||||
coreAPIMu sync.RWMutex
|
||||
toolHandlers = map[string]sdk.ToolHandler{}
|
||||
stageHandlers = map[string]sdk.StageHandler{}
|
||||
outputHandlers = map[string]sdk.ToolHandler{}
|
||||
)
|
||||
|
||||
// ---- CoreAPI dispatch helpers ----
|
||||
|
||||
func callVoid(methodID int, s1, s2, s3 string, i1, i2 int) error {
|
||||
coreAPIMu.RLock()
|
||||
api := coreAPI
|
||||
coreAPIMu.RUnlock()
|
||||
var c1, c2, c3 *C.char
|
||||
if s1 != "" { c1 = C.CString(s1); defer C.free(unsafe.Pointer(c1)) }
|
||||
if s2 != "" { c2 = C.CString(s2); defer C.free(unsafe.Pointer(c2)) }
|
||||
if s3 != "" { c3 = C.CString(s3); defer C.free(unsafe.Pointer(c3)) }
|
||||
var cErr *C.char
|
||||
if C.ha_dispatch(C.int(methodID), api, c1, c2, c3, C.int(i1), C.int(i2), nil, &cErr) != 0 && cErr != nil {
|
||||
return fmt.Errorf("%s", C.GoString(cErr))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func callString(methodID int, s1, s2, s3 string, i1, i2 int) (string, error) {
|
||||
coreAPIMu.RLock()
|
||||
api := coreAPI
|
||||
coreAPIMu.RUnlock()
|
||||
var c1, c2, c3 *C.char
|
||||
if s1 != "" { c1 = C.CString(s1); defer C.free(unsafe.Pointer(c1)) }
|
||||
if s2 != "" { c2 = C.CString(s2); defer C.free(unsafe.Pointer(c2)) }
|
||||
if s3 != "" { c3 = C.CString(s3); defer C.free(unsafe.Pointer(c3)) }
|
||||
var strResult, cErr *C.char
|
||||
if C.ha_dispatch(C.int(methodID), api, c1, c2, c3, C.int(i1), C.int(i2), &strResult, &cErr) != 0 && cErr != nil {
|
||||
return "", fmt.Errorf("%s", C.GoString(cErr))
|
||||
}
|
||||
if strResult != nil {
|
||||
result := C.GoString(strResult)
|
||||
C.ha_dispatch(C.int(25), api, strResult, nil, nil, 0, 0, nil, nil)
|
||||
return result, nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// ---- buildPluginSDK: PluginSDK backed by CoreAPI dispatch ----
|
||||
// - ALL SDK methods route through C ABI → CoreAPI → core's PluginSDK
|
||||
// - Handlers for tools/stages/output are stored locally AND registered via dispatch
|
||||
|
||||
func buildPluginSDK(name string) *sdk.PluginSDK {
|
||||
sett := &dispatchSettings{}
|
||||
base := sdk.New(name, sett,
|
||||
func(toolName string, def sdk.ToolDef, handler sdk.ToolHandler) error {
|
||||
handlerMu.Lock()
|
||||
toolHandlers[toolName] = handler
|
||||
handlerMu.Unlock()
|
||||
b, _ := json.Marshal(def)
|
||||
return callVoid(1, toolName, string(b), "", 0, 0)
|
||||
},
|
||||
func(stage sdk.Stage, handler sdk.StageHandler) {
|
||||
handlerMu.Lock()
|
||||
stageHandlers[string(stage)] = handler
|
||||
handlerMu.Unlock()
|
||||
callVoid(2, string(stage), "", "", 0, 0)
|
||||
},
|
||||
func(name string) error { return callVoid(4, name, "", "", 0, 0) },
|
||||
func(name string, caps int, desc string, def sdk.ChannelDef, handler sdk.ToolHandler) error {
|
||||
handlerMu.Lock()
|
||||
outputHandlers[name] = handler
|
||||
handlerMu.Unlock()
|
||||
defJSON, _ := json.Marshal(def)
|
||||
return callVoid(3, name, desc, string(defJSON), caps, 0)
|
||||
},
|
||||
)
|
||||
base.SetIOInjector(dispatchIO{})
|
||||
base.SetMemoryAPI(dispatchMemory{})
|
||||
base.SetDocMemoryAPI(dispatchDocMemory{})
|
||||
base.SetKnowledgeAPI(dispatchKnowledge{})
|
||||
base.SetLLMAPI(dispatchLLM{})
|
||||
base.SetSocialAPI(dispatchSocial{})
|
||||
base.SetTextMemoryAPI(dispatchTextMemory{})
|
||||
base.SetPluginMgrAPI(dispatchPluginMgr{})
|
||||
base.SetInputChannelRegistrar(
|
||||
func(name string, def sdk.ChannelDef) error {
|
||||
defJSON, _ := json.Marshal(def)
|
||||
return callVoid(46, name, string(defJSON), "", 0, 0)
|
||||
},
|
||||
)
|
||||
return base
|
||||
}
|
||||
|
||||
// ---- dispatch IO (inline definitions) ----
|
||||
|
||||
type dispatchIO struct{}
|
||||
func (dispatchIO) InjectInterruptText(s, c, t string) { callVoid(6, s, c, t, 0, 0) }
|
||||
func (dispatchIO) InjectText(s, c, t string) { callVoid(5, s, c, t, 0, 0) }
|
||||
func (dispatchIO) InjectTextNoMemory(s, c, t string) { callVoid(7, s, c, t, 0, 0) }
|
||||
func (dispatchIO) InjectInputSync(s, c, t string) string { r, _ := callString(47, s, c, t, 0, 0); return r }
|
||||
|
||||
type dispatchMemory struct{}
|
||||
func (dispatchMemory) Recall(q []string, d int) ([]sdk.Entity, []sdk.Relation, error) {
|
||||
b, _ := json.Marshal(q); r, e := callString(9, string(b), "", "", d, 0)
|
||||
if e != nil || r == "" { return nil, nil, e }
|
||||
var v struct{ Entities []sdk.Entity; Relations []sdk.Relation }
|
||||
if e = json.Unmarshal([]byte(r), &v); e != nil { return nil, nil, e }
|
||||
if v.Entities == nil { v.Entities = []sdk.Entity{} }
|
||||
if v.Relations == nil { v.Relations = []sdk.Relation{} }
|
||||
return v.Entities, v.Relations, nil
|
||||
}
|
||||
func (dispatchMemory) Commit(t []sdk.Triple) error { b, _ := json.Marshal(t); return callVoid(10, string(b), "", "", 0, 0) }
|
||||
func (dispatchMemory) Introspect() (map[string]interface{}, error) { r, e := callString(11, "", "", "", 0, 0); if e != nil || r == "" { return nil, e }; var m map[string]interface{}; return m, json.Unmarshal([]byte(r), &m) }
|
||||
func (dispatchMemory) MergeEntities(s, t string) (int, error) { return 1, callVoid(12, s, t, "", 0, 0) }
|
||||
func (dispatchMemory) Purge(c map[string]string, m string) (int, error) { b, _ := json.Marshal(c); i := 0; if m == "hard" { i = 1 }; return 1, callVoid(13, string(b), "", "", i, 0) }
|
||||
|
||||
type dispatchDocMemory struct{}
|
||||
func (dispatchDocMemory) Query(t string, k int) []*sdk.Doc { r, e := callString(14, t, "", "", k, 0); if e != nil || r == "" { return nil }; var d []*sdk.Doc; json.Unmarshal([]byte(r), &d); return d }
|
||||
func (dispatchDocMemory) Insert(doc *sdk.Doc) error { b, _ := json.Marshal(doc); return callVoid(32, string(b), "", "", 0, 0) }
|
||||
func (dispatchDocMemory) Remove(id string) { callVoid(33, id, "", "", 0, 0) }
|
||||
func (dispatchDocMemory) Stats() map[string]interface{} { r, e := callString(34, "", "", "", 0, 0); if e != nil || r == "" { return nil }; var m map[string]interface{}; json.Unmarshal([]byte(r), &m); return m }
|
||||
|
||||
type dispatchKnowledge struct{}
|
||||
func (dispatchKnowledge) Search(q string, k int) ([]*sdk.Knowledge, error) { r, e := callString(15, q, "", "", k, 0); if e != nil || r == "" { return nil, e }; var v []*sdk.Knowledge; return v, json.Unmarshal([]byte(r), &v) }
|
||||
func (dispatchKnowledge) Add(n, c string) error { return callVoid(35, n, c, "", 0, 0) }
|
||||
func (dispatchKnowledge) List() ([]string, error) { r, e := callString(36, "", "", "", 0, 0); if e != nil || r == "" { return nil, e }; var v []string; return v, json.Unmarshal([]byte(r), &v) }
|
||||
|
||||
type dispatchLLM struct{}
|
||||
func (dispatchLLM) ListSources() []string { r, e := callString(19, "", "", "", 0, 0); if e != nil || r == "" { return nil }; var v []string; json.Unmarshal([]byte(r), &v); return v }
|
||||
func (dispatchLLM) SetSource(n string) error { return callVoid(20, n, "", "", 0, 0) }
|
||||
func (dispatchLLM) CurrentSource() string { r, e := callString(37, "", "", "", 0, 0); if e != nil || r == "" { return "" }; return r }
|
||||
|
||||
type dispatchSocial struct{}
|
||||
func (dispatchSocial) GetPerson(n string) (*sdk.PersonProfile, error) { r, e := callString(21, n, "", "", 0, 0); if e != nil || r == "" { return nil, e }; var v sdk.PersonProfile; return &v, json.Unmarshal([]byte(r), &v) }
|
||||
func (dispatchSocial) GetTrait(n, t string) (string, bool) { r, e := callString(38, n, t, "", 0, 0); if e != nil || r == "" { return "", false }; var m map[string]interface{}; json.Unmarshal([]byte(r), &m); v, _ := m["value"].(string); ok, _ := m["found"].(bool); return v, ok }
|
||||
func (dispatchSocial) GetRelations(name string) ([]sdk.SocialRelation, error) { r, e := callString(39, name, "", "", 0, 0); if e != nil || r == "" { return nil, e }; var v []sdk.SocialRelation; return v, json.Unmarshal([]byte(r), &v) }
|
||||
func (dispatchSocial) GetNetwork(n string, d int) ([]*sdk.PersonProfile, error) { r, e := callString(22, n, "", "", d, 0); if e != nil || r == "" { return nil, e }; var v []*sdk.PersonProfile; return v, json.Unmarshal([]byte(r), &v) }
|
||||
func (dispatchSocial) ListPersons() ([]string, error) { r, e := callString(40, "", "", "", 0, 0); if e != nil || r == "" { return nil, e }; var v []string; return v, json.Unmarshal([]byte(r), &v) }
|
||||
|
||||
type dispatchTextMemory struct{}
|
||||
func (dispatchTextMemory) Append(evt sdk.TextEvent) error { b, _ := json.Marshal(evt); return callVoid(41, string(b), "", "", 0, 0) }
|
||||
|
||||
// ---- dispatchPluginMgr (CORE_PLUGIN_RELOAD_ONE = 48) ----
|
||||
|
||||
type dispatchPluginMgr struct{}
|
||||
|
||||
func (dispatchPluginMgr) ReloadOne(name string) error {
|
||||
return callVoid(48, name, "", "", 0, 0)
|
||||
}
|
||||
|
||||
func (dispatchPluginMgr) ListLoadedPlugins() []string {
|
||||
r, e := callString(49, "", "", "", 0, 0)
|
||||
if e != nil || r == "" {
|
||||
return nil
|
||||
}
|
||||
var list []string
|
||||
if json.Unmarshal([]byte(r), &list) != nil {
|
||||
return nil
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
func (dispatchPluginMgr) IsPluginDisabled(name string) bool {
|
||||
r, e := callString(50, name, "", "", 0, 0)
|
||||
return e == nil && r == "1"
|
||||
}
|
||||
|
||||
// ---- dispatchSettings (inline) ----
|
||||
|
||||
type dispatchSettings struct{}
|
||||
func (d *dispatchSettings) Get(key string) (interface{}, error) {
|
||||
r, e := callString(16, key, "", "", 0, 0); if e != nil || r == "" { return nil, e }; var v interface{}; return v, json.Unmarshal([]byte(r), &v)
|
||||
}
|
||||
func (d *dispatchSettings) Set(key string, value interface{}) error {
|
||||
b, _ := json.Marshal(value); return callVoid(17, key, string(b), "", 0, 0)
|
||||
}
|
||||
func (d *dispatchSettings) RegisterDef(def sdk.ConfigDef) { b, _ := json.Marshal(def); callVoid(18, string(b), "", "", 0, 0) }
|
||||
func (d *dispatchSettings) List(prefix string) ([]string, error) {
|
||||
r, e := callString(42, prefix, "", "", 0, 0); if e != nil || r == "" { return nil, e }; var v []string; return v, json.Unmarshal([]byte(r), &v)
|
||||
}
|
||||
func (d *dispatchSettings) GetCore(key string) (interface{}, error) {
|
||||
r, e := callString(26, key, "", "", 0, 0); if e != nil || r == "" { return nil, e }; var v interface{}; return v, json.Unmarshal([]byte(r), &v)
|
||||
}
|
||||
func (d *dispatchSettings) SetCore(key string, value interface{}) error {
|
||||
b, _ := json.Marshal(value); return callVoid(27, key, string(b), "", 0, 0)
|
||||
}
|
||||
func (d *dispatchSettings) ListCore(prefix string) ([]string, error) {
|
||||
r, e := callString(28, prefix, "", "", 0, 0); if e != nil || r == "" { return nil, e }; var v []string; return v, json.Unmarshal([]byte(r), &v)
|
||||
}
|
||||
func (d *dispatchSettings) GetPlugin(plugin, key string) (interface{}, error) {
|
||||
r, e := callString(29, plugin, key, "", 0, 0); if e != nil || r == "" { return nil, e }; var v interface{}; return v, json.Unmarshal([]byte(r), &v)
|
||||
}
|
||||
func (d *dispatchSettings) SetPlugin(plugin, key string, value interface{}) error {
|
||||
b, _ := json.Marshal(value); return callVoid(30, plugin, key, string(b), 0, 0)
|
||||
}
|
||||
func (d *dispatchSettings) ListPlugin(plugin, prefix string) ([]string, error) {
|
||||
r, e := callString(31, plugin, prefix, "", 0, 0); if e != nil || r == "" { return nil, e }; var v []string; return v, json.Unmarshal([]byte(r), &v)
|
||||
}
|
||||
func (d *dispatchSettings) Defs(prefix string) []*sdk.ConfigDef {
|
||||
r, e := callString(43, prefix, "", "", 0, 0); if e != nil || r == "" { return nil }; var v []*sdk.ConfigDef; json.Unmarshal([]byte(r), &v); return v
|
||||
}
|
||||
func (d *dispatchSettings) Dump() map[string]interface{} {
|
||||
r, e := callString(44, "", "", "", 0, 0); if e != nil || r == "" { return nil }; var m map[string]interface{}; json.Unmarshal([]byte(r), &m); return m
|
||||
}
|
||||
func (d *dispatchSettings) Plugins() []string {
|
||||
r, e := callString(45, "", "", "", 0, 0); if e != nil || r == "" { return nil }; var v []string; json.Unmarshal([]byte(r), &v); return v
|
||||
}
|
||||
|
||||
// ---- Go callbacks (called from z_entry.c via C) ----
|
||||
|
||||
//export go_init_plugin
|
||||
func go_init_plugin(name *C.char, configJSON *C.char, errorOut **C.char) C.int {
|
||||
plg, err := NewPluginFactory(C.GoString(name), nil)
|
||||
if err != nil || plg == nil {
|
||||
if err != nil { *errorOut = C.CString(err.Error()) } else { *errorOut = C.CString("NewPluginFactory returned nil") }
|
||||
return 1
|
||||
}
|
||||
mu.Lock(); currentPlg = plg; mu.Unlock()
|
||||
_ = configJSON
|
||||
return 0
|
||||
}
|
||||
|
||||
//export go_start_plugin
|
||||
func go_start_plugin(coreAPIptr unsafe.Pointer, coreVersion C.int, errorOut **C.char) C.int {
|
||||
mu.Lock()
|
||||
plg := currentPlg
|
||||
coreAPIMu.Lock()
|
||||
coreAPI = coreAPIptr
|
||||
coreAPIMu.Unlock()
|
||||
mu.Unlock()
|
||||
_ = coreVersion
|
||||
if plg == nil { *errorOut = C.CString("not initialized"); return 1 }
|
||||
sdk := buildPluginSDK(plg.Name())
|
||||
mu.Lock(); currentSDK = sdk; mu.Unlock()
|
||||
if err := plg.Start(sdk); err != nil { *errorOut = C.CString(err.Error()); return 1 }
|
||||
return 0
|
||||
}
|
||||
|
||||
//export go_stop_plugin
|
||||
func go_stop_plugin(errorOut **C.char) C.int {
|
||||
mu.Lock()
|
||||
plg := currentPlg
|
||||
sdk := currentSDK
|
||||
currentPlg = nil
|
||||
currentSDK = nil
|
||||
coreAPIMu.Lock()
|
||||
coreAPI = nil
|
||||
coreAPIMu.Unlock()
|
||||
mu.Unlock()
|
||||
if sdk != nil {
|
||||
sdk.RunStopHandlers()
|
||||
}
|
||||
if plg != nil {
|
||||
if err := plg.Stop(); err != nil { *errorOut = C.CString(err.Error()); return 1 }
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
//export go_invoke_tool
|
||||
func go_invoke_tool(name *C.char, argsJSON *C.char, resultOut **C.char, errorOut **C.char) C.int {
|
||||
goName := C.GoString(name)
|
||||
handlerMu.RLock()
|
||||
h, ok := toolHandlers[goName]
|
||||
handlerMu.RUnlock()
|
||||
if !ok { *errorOut = C.CString("tool not found"); return 1 }
|
||||
var args map[string]interface{}
|
||||
if argsJSON != nil { json.Unmarshal([]byte(C.GoString(argsJSON)), &args) }
|
||||
r, err := h(args)
|
||||
if err != nil { *errorOut = C.CString(err.Error()); return 1 }
|
||||
b, _ := json.Marshal(r)
|
||||
*resultOut = C.CString(string(b))
|
||||
return 0
|
||||
}
|
||||
|
||||
// fillStageContext 将内核传来的 ctx JSON 填充到插件侧 StageContext。
|
||||
func fillStageContext(sc *sdk.StageContext, ctxJSON string) {
|
||||
var m map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(ctxJSON), &m); err != nil {
|
||||
return
|
||||
}
|
||||
if v, _ := m["raw_message"].(string); v != "" { sc.RawMessage = v }
|
||||
if v, _ := m["user_id"].(string); v != "" { sc.UserID = v }
|
||||
if v, _ := m["group_id"].(string); v != "" { sc.GroupID = v }
|
||||
if v, _ := m["phase"].(string); v != "" { sc.Phase = sdk.Stage(v) }
|
||||
if v, _ := m["llm_text"].(string); v != "" { sc.LLMText = v }
|
||||
if v, _ := m["final_text"].(string); v != "" { sc.FinalText = v }
|
||||
if v, _ := m["no_memory"].(bool); v { sc.NoMemory = true }
|
||||
if v, _ := m["response"].(string); v != "" { sc.Response = &v }
|
||||
if v, _ := m["tool_calls"].([]interface{}); len(v) > 0 {
|
||||
b, _ := json.Marshal(v); json.Unmarshal(b, &sc.ToolCalls)
|
||||
}
|
||||
if v, _ := m["tool_results"].([]interface{}); len(v) > 0 {
|
||||
b, _ := json.Marshal(v); json.Unmarshal(b, &sc.ToolResults)
|
||||
}
|
||||
}
|
||||
|
||||
// stageContextWritable 提取插件可写且内核会同步回去的字段。
|
||||
func stageContextWritable(sc *sdk.StageContext) map[string]interface{} {
|
||||
m := map[string]interface{}{
|
||||
"raw_message": sc.RawMessage,
|
||||
"user_id": sc.UserID,
|
||||
"group_id": sc.GroupID,
|
||||
"phase": string(sc.Phase),
|
||||
"llm_text": sc.LLMText,
|
||||
"final_text": sc.FinalText,
|
||||
"no_memory": sc.NoMemory,
|
||||
}
|
||||
if sc.Response != nil {
|
||||
m["response"] = *sc.Response
|
||||
}
|
||||
if len(sc.ToolCalls) > 0 {
|
||||
m["tool_calls"] = sc.ToolCalls
|
||||
}
|
||||
if len(sc.ToolResults) > 0 {
|
||||
m["tool_results"] = sc.ToolResults
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
//export go_invoke_stage
|
||||
func go_invoke_stage(stage *C.char, ctxJSON *C.char, resultOut **C.char, errorOut **C.char) C.int {
|
||||
goStage := C.GoString(stage)
|
||||
handlerMu.RLock()
|
||||
h, ok := stageHandlers[goStage]
|
||||
handlerMu.RUnlock()
|
||||
if !ok { return 0 }
|
||||
sc := &sdk.StageContext{}
|
||||
if ctxJSON != nil {
|
||||
fillStageContext(sc, C.GoString(ctxJSON))
|
||||
}
|
||||
if err := h(sc); err != nil { *errorOut = C.CString(err.Error()); return 1 }
|
||||
// ABI v2: 回传插件修改后的上下文(若调用方要求)
|
||||
if resultOut != nil {
|
||||
if b, err := json.Marshal(stageContextWritable(sc)); err == nil {
|
||||
*resultOut = C.CString(string(b))
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
//export go_invoke_output
|
||||
func go_invoke_output(channel *C.char, msgType *C.char, payloadJSON *C.char, errorOut **C.char) C.int {
|
||||
goChan := C.GoString(channel)
|
||||
handlerMu.RLock()
|
||||
h, ok := outputHandlers[goChan]
|
||||
handlerMu.RUnlock()
|
||||
if !ok { return 0 }
|
||||
// payloadJSON contains the full args JSON from output_send (e.g. {"content":"...","user_id":123})
|
||||
var args map[string]interface{}
|
||||
if payloadJSON != nil {
|
||||
json.Unmarshal([]byte(C.GoString(payloadJSON)), &args)
|
||||
}
|
||||
if _, err := h(args); err != nil { *errorOut = C.CString(err.Error()); return 1 }
|
||||
return 0
|
||||
}
|
||||
|
||||
//export go_free_string
|
||||
func go_free_string(ptr *C.char) { C.free(unsafe.Pointer(ptr)) }
|
||||
|
||||
func main() {}
|
||||
`
|
||||
|
||||
// tmplPluginInitC — C entry point for the plugin .so file.
|
||||
// Contains PluginAPI, CoreAPI (single dispatch), and ha_dispatch bridge.
|
||||
const tmplPluginInitC = `#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// HOMEAGENT_ABI_VERSION 与 sdk/meta/meta.go CABINum 同步(major*100+minor,v0.9.x→900)
|
||||
#define HOMEAGENT_ABI_VERSION 900
|
||||
|
||||
typedef struct {
|
||||
int version; int version_min;
|
||||
int (*init_plugin)(char*, char*, char**);
|
||||
int (*start_plugin)(void*, int, char**);
|
||||
int (*stop_plugin)(char**);
|
||||
int (*invoke_tool)(char*, char*, char**, char**);
|
||||
int (*invoke_stage)(char*, char*, char**, char**);
|
||||
int (*invoke_output)(char*, char*, char*, char**);
|
||||
void (*free_string)(char*);
|
||||
} PluginAPI;
|
||||
|
||||
typedef struct {
|
||||
int version; int version_min;
|
||||
int (*dispatch)(int, void*, char*, char*, char*, int, int, char**, char**);
|
||||
void* ctx;
|
||||
} CoreAPI;
|
||||
|
||||
extern int go_init_plugin(char*, char*, char**);
|
||||
extern int go_start_plugin(void*, int, char**);
|
||||
extern int go_stop_plugin(char**);
|
||||
extern int go_invoke_tool(char*, char*, char**, char**);
|
||||
extern int go_invoke_stage(char*, char*, char**, char**);
|
||||
extern int go_invoke_output(char*, char*, char*, char**);
|
||||
extern void go_free_string(char*);
|
||||
|
||||
int c_init_plugin(char* n, char* c, char** e) { return go_init_plugin(n, c, e); }
|
||||
int c_start_plugin(void* a, int v, char** e) { return go_start_plugin(a, v, e); }
|
||||
int c_stop_plugin(char** e) { return go_stop_plugin(e); }
|
||||
int c_invoke_tool(char* n, char* a, char** r, char** e) { return go_invoke_tool(n, a, r, e); }
|
||||
int c_invoke_stage(char* s, char* c, char** r, char** e) { return go_invoke_stage(s, c, r, e); }
|
||||
int c_invoke_output(char* c, char* m, char* p, char** e) { return go_invoke_output(c, m, p, e); }
|
||||
void c_free_string(char* p) { go_free_string(p); }
|
||||
|
||||
// ha_dispatch — called by Go bridge, passes through to CoreAPI dispatch
|
||||
int ha_dispatch(int id, void* api, char* s1, char* s2, char* s3, int i1, int i2, char** r, char** e) {
|
||||
CoreAPI* a = (CoreAPI*)api;
|
||||
if (!a || !a->dispatch) return 1;
|
||||
return a->dispatch(id, a->ctx, s1, s2, s3, i1, i2, r, e);
|
||||
}
|
||||
|
||||
PluginAPI* plugin_init(void) {
|
||||
static PluginAPI api;
|
||||
memset(&api, 0, sizeof(api));
|
||||
api.version = HOMEAGENT_ABI_VERSION; api.version_min = HOMEAGENT_ABI_VERSION;
|
||||
api.init_plugin = c_init_plugin; api.start_plugin = c_start_plugin; api.stop_plugin = c_stop_plugin;
|
||||
api.invoke_tool = c_invoke_tool; api.invoke_stage = c_invoke_stage; api.invoke_output = c_invoke_output;
|
||||
api.free_string = c_free_string;
|
||||
return &api;
|
||||
}
|
||||
`
|
||||
|
||||
// ============================================================
|
||||
// Remote Device Adapter Templates
|
||||
// ============================================================
|
||||
|
||||
1339
tools/plugindev/templates/proc_main.go.tmpl
Normal file
1339
tools/plugindev/templates/proc_main.go.tmpl
Normal file
File diff suppressed because it is too large
Load Diff
62
tools/plugindev/templates/proc_shm_unix.go.tmpl
Normal file
62
tools/plugindev/templates/proc_shm_unix.go.tmpl
Normal file
@ -0,0 +1,62 @@
|
||||
//go:build linux || darwin || freebsd
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// Unix 侧共享段挂载:内核经 ExtraFiles 传入继承的 fd。
|
||||
//
|
||||
// fd 布局(与内核 internal/plugin/proc/plugin.go 的 ExtraFiles 顺序一致):
|
||||
//
|
||||
// fd 3 = StageContext 段(memfd / 已 unlink 的临时文件)
|
||||
// fd 4 = 事件环段
|
||||
// fd 5 = 事件通知(Linux eventfd / macOS pipe 读端)
|
||||
//
|
||||
// 继承的 fd 无需文件名,也不残留——这是选 memfd 而非 /dev/shm 的原因。
|
||||
const (
|
||||
fdStageShm = 3
|
||||
fdEvtRingShm = 4
|
||||
fdEvtNotifier = 5
|
||||
)
|
||||
|
||||
// attachStageShm 挂载 StageContext 共享段。
|
||||
//
|
||||
// 各进程 mmap 到不同虚拟地址,段内一律用相对偏移而非指针,故仍能正确解引用
|
||||
// (实验 2 已验证父子 mmap 基址不同时偏移解引用正确)。
|
||||
func attachStageShm(size int) ([]byte, error) {
|
||||
return syscall.Mmap(fdStageShm, 0, size,
|
||||
syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
|
||||
}
|
||||
|
||||
// attachEvtRingShm 挂载事件环段。
|
||||
func attachEvtRingShm(size int) ([]byte, error) {
|
||||
return syscall.Mmap(fdEvtRingShm, 0, size,
|
||||
syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
|
||||
}
|
||||
|
||||
// openEvtNotifier 打开事件通知读端。
|
||||
func openEvtNotifier() (evtWaiter, error) {
|
||||
f := os.NewFile(fdEvtNotifier, "evtnotify")
|
||||
if f == nil {
|
||||
return nil, fmt.Errorf("fd %d 不是有效的通知句柄", fdEvtNotifier)
|
||||
}
|
||||
return &unixEvtWaiter{f: f}, nil
|
||||
}
|
||||
|
||||
// unixEvtWaiter 用 eventfd/pipe 的阻塞 Read 等待通知。
|
||||
//
|
||||
// os.NewFile 把 fd 注册进 runtime netpoller,Read 阻塞时只 park goroutine,
|
||||
// 不占 OS 线程(实验 1:200 个等待者仅增 1 个 OS 线程)。
|
||||
// 反面对照是经 cgo 调 sem_wait——那会阻塞整个 M。
|
||||
type unixEvtWaiter struct {
|
||||
f *os.File
|
||||
}
|
||||
|
||||
func (w *unixEvtWaiter) Wait(buf []byte) error {
|
||||
_, err := w.f.Read(buf)
|
||||
return err
|
||||
}
|
||||
153
tools/plugindev/templates/proc_shm_windows.go.tmpl
Normal file
153
tools/plugindev/templates/proc_shm_windows.go.tmpl
Normal file
@ -0,0 +1,153 @@
|
||||
//go:build windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Windows 侧共享段挂载:走命名对象而非继承 fd。
|
||||
//
|
||||
// 为何不能照抄 Unix:Windows 没有 fd 继承语义,`ExtraFiles` 在 os/exec 的
|
||||
// Windows 实现里不被支持。等价机制是命名内核对象——父进程用
|
||||
// CreateFileMapping / CreateEvent 建带名字的对象,子进程按同名 Open 拿到同一对象。
|
||||
//
|
||||
// 名字经环境变量传入(内核 internal/plugin/proc/plugin_windows.go 设置),
|
||||
// 而不是硬编码:多个 homed 实例并存时不能撞名。
|
||||
//
|
||||
// **这是 §9.2 的正解**:C ABI 时代 Windows 是第三套独立 ABI 实现,
|
||||
// stage 只下发 3 个字段且完全没有写回,sanitizer 这类改写型插件静默失效。
|
||||
// 现在 Windows 与 Unix 共用同一份 RPC 逻辑与同一份共享段布局,
|
||||
// 差异被收敛到本文件的三个函数里。
|
||||
const (
|
||||
envStageShmName = "HOMEAGENT_SHM_STAGE"
|
||||
envEvtRingName = "HOMEAGENT_SHM_EVTRING"
|
||||
envEvtEventName = "HOMEAGENT_EVT_EVENT"
|
||||
)
|
||||
|
||||
// Windows API 绑定:用 LazyDLL 而非 golang.org/x/sys/windows。
|
||||
//
|
||||
// 原因:OpenFileMappingW / OpenEventW 未被标准库 syscall 包导出。
|
||||
// 引入 x/sys 会给**每个插件的 go.mod 加一个新依赖**,
|
||||
// 而「外部插件零改动」是本次迁移的硬约束(插件仅依赖公开 SDK)。
|
||||
// LazyDLL 属于标准库 syscall,零新增依赖。
|
||||
var (
|
||||
kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||
procOpenFileMappingW = kernel32.NewProc("OpenFileMappingW")
|
||||
procOpenEventW = kernel32.NewProc("OpenEventW")
|
||||
)
|
||||
|
||||
const (
|
||||
winEventModifyState = 0x0002
|
||||
winSynchronize = 0x00100000
|
||||
)
|
||||
|
||||
// openFileMappingW 封装 OpenFileMappingW。
|
||||
func openFileMappingW(access uint32, inherit bool, name *uint16) (syscall.Handle, error) {
|
||||
var inheritFlag uintptr
|
||||
if inherit {
|
||||
inheritFlag = 1
|
||||
}
|
||||
r, _, err := procOpenFileMappingW.Call(
|
||||
uintptr(access), inheritFlag, uintptr(unsafe.Pointer(name)))
|
||||
if r == 0 {
|
||||
return 0, err
|
||||
}
|
||||
return syscall.Handle(r), nil
|
||||
}
|
||||
|
||||
// openEventW 封装 OpenEventW。
|
||||
func openEventW(access uint32, inherit bool, name *uint16) (syscall.Handle, error) {
|
||||
var inheritFlag uintptr
|
||||
if inherit {
|
||||
inheritFlag = 1
|
||||
}
|
||||
r, _, err := procOpenEventW.Call(
|
||||
uintptr(access), inheritFlag, uintptr(unsafe.Pointer(name)))
|
||||
if r == 0 {
|
||||
return 0, err
|
||||
}
|
||||
return syscall.Handle(r), nil
|
||||
}
|
||||
|
||||
// attachStageShm 按名字打开 StageContext 段并映射。
|
||||
func attachStageShm(size int) ([]byte, error) {
|
||||
return openNamedMapping(os.Getenv(envStageShmName), size, "StageContext 段")
|
||||
}
|
||||
|
||||
// attachEvtRingShm 按名字打开事件环段并映射。
|
||||
func attachEvtRingShm(size int) ([]byte, error) {
|
||||
return openNamedMapping(os.Getenv(envEvtRingName), size, "事件环段")
|
||||
}
|
||||
|
||||
// openNamedMapping 打开命名共享段并映射为 []byte。
|
||||
//
|
||||
// 与 Unix 的 mmap 语义对齐:MapViewOfFile 返回的地址在本进程虚拟空间,
|
||||
// 段内偏移仍是相对的,故跨进程解引用正确。
|
||||
func openNamedMapping(name string, size int, what string) ([]byte, error) {
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("%s 名字未经环境变量传入", what)
|
||||
}
|
||||
namePtr, err := syscall.UTF16PtrFromString(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s 名字非法: %w", what, err)
|
||||
}
|
||||
|
||||
h, err := openFileMappingW(syscall.FILE_MAP_WRITE, false, namePtr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("打开 %s(%s): %w", what, name, err)
|
||||
}
|
||||
|
||||
addr, err := syscall.MapViewOfFile(h, syscall.FILE_MAP_WRITE, 0, 0, uintptr(size))
|
||||
if err != nil {
|
||||
syscall.CloseHandle(h)
|
||||
return nil, fmt.Errorf("映射 %s: %w", what, err)
|
||||
}
|
||||
// 句柄不关:视图存活期间必须保持句柄有效,进程退出时由 OS 回收。
|
||||
|
||||
return unsafe.Slice((*byte)(unsafe.Pointer(addr)), size), nil
|
||||
}
|
||||
|
||||
// openEvtNotifier 按名字打开事件通知对象。
|
||||
func openEvtNotifier() (evtWaiter, error) {
|
||||
name := os.Getenv(envEvtEventName)
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("事件通知对象名字未经环境变量传入")
|
||||
}
|
||||
namePtr, err := syscall.UTF16PtrFromString(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("事件对象名字非法: %w", err)
|
||||
}
|
||||
h, err := openEventW(winSynchronize|winEventModifyState, false, namePtr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("打开事件对象(%s): %w", name, err)
|
||||
}
|
||||
return &windowsEvtWaiter{h: h}, nil
|
||||
}
|
||||
|
||||
// windowsEvtWaiter 用命名 Event 对象等待通知。
|
||||
//
|
||||
// 与 eventfd 的差异:Event 是二元信号而非计数器,多次 SetEvent 只对应
|
||||
// 一次唤醒。这不影响正确性——消费者被唤醒后按 readSeq 追 writeSeq
|
||||
// 批量 drain,一次唤醒能处理累积的全部事件。
|
||||
//
|
||||
// WaitForSingleObject 阻塞的是 OS 线程而非仅 goroutine,故不如 eventfd
|
||||
// 的 netpoller 路径省线程。每插件一个消费 goroutine,17 插件即 17 线程,
|
||||
// 在可接受范围(实验 5 实测 17 子进程共 84 线程)。
|
||||
type windowsEvtWaiter struct {
|
||||
h syscall.Handle
|
||||
}
|
||||
|
||||
func (w *windowsEvtWaiter) Wait(buf []byte) error {
|
||||
ev, err := syscall.WaitForSingleObject(w.h, syscall.INFINITE)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ev != syscall.WAIT_OBJECT_0 {
|
||||
return fmt.Errorf("等待事件对象返回 0x%x", ev)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -95,6 +95,29 @@ type IOInjector interface {
|
||||
InjectInterruptText(source, channel, text string)
|
||||
InjectText(source, channel, text string)
|
||||
InjectTextNoMemory(source, channel, text string)
|
||||
// 1.1.0 媒体注入。与公共 SDK 同构:插件在 yaegi 下调得通的方法,
|
||||
// 编成 plugin.bin 后必须也调得通,否则调试期与真实运行行为不一致。
|
||||
InjectInputMedia(source, channel, text string, blocks []ContentBlock)
|
||||
InjectInputMediaSync(source, channel, text string, blocks []ContentBlock) string
|
||||
InjectInterruptMedia(source, channel, text string, blocks []ContentBlock)
|
||||
SetToolBlocks(blocks []ContentBlock)
|
||||
}
|
||||
|
||||
// ContentBlock 与公共 SDK 同构(OpenAI 多模态内容块格式)。
|
||||
type ContentBlock struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text,omitempty"`
|
||||
ImageURL *ImageURL `json:"image_url,omitempty"`
|
||||
AudioURL *AudioURL `json:"audio_url,omitempty"`
|
||||
}
|
||||
|
||||
type ImageURL struct {
|
||||
URL string `json:"url"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
}
|
||||
|
||||
type AudioURL struct {
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
type EventType string
|
||||
@ -159,29 +182,33 @@ type mockSettings struct{ data map[string]interface{} }
|
||||
|
||||
func (s *mockSettings) Get(key string) (interface{}, error) {
|
||||
v, ok := s.data[key]
|
||||
if !ok { return nil, nil }
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
func (s *mockSettings) Set(key string, value interface{}) error { s.data[key] = value; return nil }
|
||||
func (s *mockSettings) List(prefix string) ([]string, error) {
|
||||
var ks []string
|
||||
for k := range s.data {
|
||||
if strings.HasPrefix(k, prefix) { ks = append(ks, k) }
|
||||
if strings.HasPrefix(k, prefix) {
|
||||
ks = append(ks, k)
|
||||
}
|
||||
}
|
||||
return ks, nil
|
||||
}
|
||||
func (s *mockSettings) GetCore(key string) (interface{}, error) { return nil, nil }
|
||||
func (s *mockSettings) SetCore(key string, value interface{}) error { return nil }
|
||||
func (s *mockSettings) ListCore(prefix string) ([]string, error) { return nil, nil }
|
||||
func (s *mockSettings) GetPlugin(p, k string) (interface{}, error) { return nil, nil }
|
||||
func (s *mockSettings) SetPlugin(p, k string, v interface{}) error { return nil }
|
||||
func (s *mockSettings) GetCore(key string) (interface{}, error) { return nil, nil }
|
||||
func (s *mockSettings) SetCore(key string, value interface{}) error { return nil }
|
||||
func (s *mockSettings) ListCore(prefix string) ([]string, error) { return nil, nil }
|
||||
func (s *mockSettings) GetPlugin(p, k string) (interface{}, error) { return nil, nil }
|
||||
func (s *mockSettings) SetPlugin(p, k string, v interface{}) error { return nil }
|
||||
func (s *mockSettings) ListPlugin(p, prefix string) ([]string, error) { return nil, nil }
|
||||
func (s *mockSettings) RegisterDef(def ConfigDef) {
|
||||
logf("config def: %s = %s", def.Key, def.Default)
|
||||
}
|
||||
func (s *mockSettings) Defs(prefix string) []*ConfigDef { return nil }
|
||||
func (s *mockSettings) Dump() map[string]interface{} { return s.data }
|
||||
func (s *mockSettings) Plugins() []string { return nil }
|
||||
func (s *mockSettings) Dump() map[string]interface{} { return s.data }
|
||||
func (s *mockSettings) Plugins() []string { return nil }
|
||||
|
||||
type Entity struct {
|
||||
Name string `json:"name"`
|
||||
@ -195,10 +222,21 @@ type Relation struct {
|
||||
Object string `json:"object"`
|
||||
}
|
||||
|
||||
// Triple 与公共 SDK 同构。
|
||||
//
|
||||
// ❗字段名曾是 `Predicate`,而公共 SDK 一直叫 `Relation`。
|
||||
// yaegi 解释器下插件写 `Relation:` 会报未知字段,写 `Predicate:` 则在
|
||||
// 编成 plugin.bin 时报错——谁都不对。没人发现是因为没有任何代码
|
||||
// 对着 mocksdk 编译,漂移不会被编译器抓到。
|
||||
type Triple struct {
|
||||
Subject string `json:"subject"`
|
||||
Predicate string `json:"predicate"`
|
||||
Object string `json:"object"`
|
||||
Subject string `json:"subject"`
|
||||
Relation string `json:"relation"`
|
||||
Object string `json:"object"`
|
||||
Confidence float64 `json:"confidence,omitempty"`
|
||||
SubjectType string `json:"subject_type,omitempty"`
|
||||
ObjectType string `json:"object_type,omitempty"`
|
||||
SentenceText string `json:"sentence_text,omitempty"`
|
||||
MediaDigests []string `json:"media_digests,omitempty"`
|
||||
}
|
||||
|
||||
type MemoryAPI interface {
|
||||
@ -212,37 +250,57 @@ type MemoryAPI interface {
|
||||
type mockMemory struct{}
|
||||
|
||||
func (mockMemory) Recall(q []string, d int) ([]Entity, []Relation, error) { return nil, nil, nil }
|
||||
func (mockMemory) Commit(t []Triple) error { return nil }
|
||||
func (mockMemory) Introspect() (map[string]interface{}, error) { return map[string]interface{}{}, nil }
|
||||
func (mockMemory) MergeEntities(s, t string) (int, error) { return 0, nil }
|
||||
func (mockMemory) Purge(c map[string]string, m string) (int, error) { return 0, nil }
|
||||
func (mockMemory) Commit(t []Triple) error { return nil }
|
||||
func (mockMemory) Introspect() (map[string]interface{}, error) { return map[string]interface{}{}, nil }
|
||||
func (mockMemory) MergeEntities(s, t string) (int, error) { return 0, nil }
|
||||
func (mockMemory) Purge(c map[string]string, m string) (int, error) { return 0, nil }
|
||||
|
||||
type Doc struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
Source string `json:"source"`
|
||||
// 1.1.0:媒体字段。与公共 SDK 保持同构,否则插件在 yaegi 下跑得通、
|
||||
// 编成 plugin.bin 却编不过(或反之)。
|
||||
MediaDigests []string `json:"media_digests,omitempty"`
|
||||
Attachments []MediaAttachment `json:"attachments,omitempty"`
|
||||
}
|
||||
|
||||
// MediaAttachment 与公共 SDK 同构:写入时给 Data+MIME,引用已有内容时只给 Digest。
|
||||
type MediaAttachment struct {
|
||||
Digest string `json:"digest,omitempty"`
|
||||
MIME string `json:"mime,omitempty"`
|
||||
Data []byte `json:"data,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
}
|
||||
|
||||
type DocMemoryAPI interface {
|
||||
Query(text string, topK int) []*Doc
|
||||
Insert(doc *Doc) error
|
||||
InsertWithMedia(doc *Doc, attachments []MediaAttachment) error
|
||||
Remove(id string)
|
||||
Stats() map[string]interface{}
|
||||
}
|
||||
|
||||
type mockDocMemory struct{}
|
||||
|
||||
func (mockDocMemory) Query(t string, k int) []*Doc { return nil }
|
||||
func (mockDocMemory) Insert(doc *Doc) error { return nil }
|
||||
func (mockDocMemory) Remove(id string) {}
|
||||
func (mockDocMemory) Stats() map[string]interface{} { return nil }
|
||||
func (mockDocMemory) Query(t string, k int) []*Doc { return nil }
|
||||
func (mockDocMemory) Insert(doc *Doc) error { return nil }
|
||||
func (mockDocMemory) InsertWithMedia(doc *Doc, atts []MediaAttachment) error {
|
||||
logf("doc_insert_with_media: %d 份附件", len(atts))
|
||||
return nil
|
||||
}
|
||||
func (mockDocMemory) Remove(id string) {}
|
||||
func (mockDocMemory) Stats() map[string]interface{} { return nil }
|
||||
|
||||
type TextEvent struct {
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
Source string `json:"source"`
|
||||
// 1.1.0:附件。读回时内核从正文标记反解,写入时内核把标记并进正文。
|
||||
Attachments []MediaAttachment `json:"attachments,omitempty"`
|
||||
}
|
||||
|
||||
type TextMemoryAPI interface {
|
||||
@ -305,8 +363,8 @@ type LLMAPI interface {
|
||||
type mockLLM struct{}
|
||||
|
||||
func (mockLLM) ListSources() []string { return nil }
|
||||
func (mockLLM) SetSource(n string) error { return nil }
|
||||
func (mockLLM) CurrentSource() string { return "" }
|
||||
func (mockLLM) SetSource(n string) error { return nil }
|
||||
func (mockLLM) CurrentSource() string { return "" }
|
||||
|
||||
type IOInjectorImpl struct{}
|
||||
|
||||
@ -319,27 +377,40 @@ func (IOInjectorImpl) InjectText(source, channel, text string) {
|
||||
func (IOInjectorImpl) InjectTextNoMemory(source, channel, text string) {
|
||||
logf("inject_text_no_memory: source=%s channel=%s", source, channel)
|
||||
}
|
||||
func (IOInjectorImpl) InjectInputMedia(source, channel, text string, blocks []ContentBlock) {
|
||||
logf("inject_input_media: source=%s channel=%s blocks=%d", source, channel, len(blocks))
|
||||
}
|
||||
func (IOInjectorImpl) InjectInputMediaSync(source, channel, text string, blocks []ContentBlock) string {
|
||||
logf("inject_input_media_sync: source=%s channel=%s blocks=%d", source, channel, len(blocks))
|
||||
return ""
|
||||
}
|
||||
func (IOInjectorImpl) InjectInterruptMedia(source, channel, text string, blocks []ContentBlock) {
|
||||
logf("inject_interrupt_media: source=%s channel=%s blocks=%d", source, channel, len(blocks))
|
||||
}
|
||||
func (IOInjectorImpl) SetToolBlocks(blocks []ContentBlock) {
|
||||
logf("set_tool_blocks: blocks=%d", len(blocks))
|
||||
}
|
||||
|
||||
type PluginSDK struct {
|
||||
Name string
|
||||
mu sync.RWMutex
|
||||
toolDefs map[string]ToolDef
|
||||
toolHandlers map[string]ToolHandler
|
||||
Name string
|
||||
mu sync.RWMutex
|
||||
toolDefs map[string]ToolDef
|
||||
toolHandlers map[string]ToolHandler
|
||||
stageHandlers map[string]StageHandler
|
||||
outChannels map[string]ToolHandler
|
||||
Settings SettingsAPI
|
||||
IO IOInjector
|
||||
outChannels map[string]ToolHandler
|
||||
Settings SettingsAPI
|
||||
IO IOInjector
|
||||
}
|
||||
|
||||
func New(name string) *PluginSDK {
|
||||
return &PluginSDK{
|
||||
Name: name,
|
||||
toolDefs: make(map[string]ToolDef),
|
||||
toolHandlers: make(map[string]ToolHandler),
|
||||
Name: name,
|
||||
toolDefs: make(map[string]ToolDef),
|
||||
toolHandlers: make(map[string]ToolHandler),
|
||||
stageHandlers: make(map[string]StageHandler),
|
||||
outChannels: make(map[string]ToolHandler),
|
||||
Settings: &mockSettings{data: map[string]interface{}{}},
|
||||
IO: IOInjectorImpl{},
|
||||
outChannels: make(map[string]ToolHandler),
|
||||
Settings: &mockSettings{data: map[string]interface{}{}},
|
||||
IO: IOInjectorImpl{},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user