Files
MailUI4Agents/gateway/internal/models/address_test.go
JianFeeeee 0e754617a4 feat: AgentMail —— 以邮件为统一范式的多智能体协作平台
Go 单二进制网关 + React 前端 + opencode 桥接插件。部署产物是
「一个二进制加一个 .db 文件」:前端经 go:embed 打进二进制,
数据库默认内置 SQLite,systemd 托管。

核心设计
- 三维寻址 name@path.session,按最后一个 . 切分;session 位三态:
  省略=默认会话 / new=强制新建 / 具体别名=必须已存在(否则 404 无法送达)
- 会话别名默认复用 Agent 平台自己的命名机制(opencode 的 slug 与模型生成的
  标题),不在本侧另造一套;人显式定过的别名不被平台同步覆盖
- 对话树不建 tree_nodes 表:parent_mail_id 已完整编码树结构,
  再维护一张表就是第二份真相。用递归 CTE 查,按方向分块加载
- 附件内容存磁盘、按 sha256 内容寻址,数据库只存元数据;天然去重,
  且路径与用户 filename 无关,杜绝 ../ 穿越
- 配额约束的是模型的自主发信,不是 harness 的转发:插件代劳的权限询问与
  最终总结走免配额通道,靠上游消息 id 做幂等键而非计数
- 往返预算下沉到会话(写信时给、对话页里改)+ Agent 全局配额,两层都要过

后端 gateway/
- models/repo/handler/middleware/sse/blob 分层;两方言(SQLite/PostgreSQL)
  共用一份 repo 层 SQL,差异集中在 internal/db
- 多用户认证(bcrypt cost12、登录限速、会话隔离、权限边界)
- 密钥体系:Agent 密钥与用户密钥分表,三种生命周期;登记式密钥让全文
  只从客户端流向服务器一次
- 所有「判断 + 自增」都在同一条 UPDATE 里(配额、预算、one_time 密钥、
  附件挂载),并发下不会刷穿

前端 web/
- 三栏布局、三段式地址补全、权限卡片、密钥面板、配额面板、对话树、附件
- 全站纯 SVG 图标,不使用 emoji
- api/ 即可复用的客户端 SDK:基地址与凭证集中在 api/config.ts

插件 plugins/opencode-mail-bridge/
- 六个工具 + 两类自动转发(permission.ask 钩子接管平台原生权限询问、
  session.idle 时转发本轮总结)
2026-09-02 10:29:26 +08:00

95 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package models
import "testing"
func TestParseAddress(t *testing.T) {
cases := []struct {
in string
name string
path string
session string
mode SessionMode
}{
// 用户给出的两个例子
{"deepseekharness@/program.upadtefeature", "deepseekharness", "/program", "upadtefeature", SessionNamed},
{"pi@root.new", "pi", "root", "new", SessionNew},
// 常规形态
{"builder@ModelRouter.fix-memory-leak", "builder", "ModelRouter", "fix-memory-leak", SessionNamed},
{"@builder@ModelRouter.new", "builder", "ModelRouter", "new", SessionNew},
{"human@.new", "human", "", "new", SessionNew},
// 省略 session 位 = 默认会话(不等于 new
{"human", "human", "", "", SessionDefault},
{"ops@prod", "ops", "prod", "", SessionDefault},
// path 内含 . 与 /(按最后一个 . 切)
{"agent@/home/a.b/c.deploy", "agent", "/home/a.b/c", "deploy", SessionNamed},
}
for _, c := range cases {
got, err := ParseAddress(c.in)
if err != nil {
t.Fatalf("ParseAddress(%q) unexpected error: %v", c.in, err)
}
if got.Name != c.name || got.Path != c.path || got.Session != c.session {
t.Errorf("ParseAddress(%q) = {name:%q path:%q session:%q}, want {name:%q path:%q session:%q}",
c.in, got.Name, got.Path, got.Session, c.name, c.path, c.session)
}
if got.Mode() != c.mode {
t.Errorf("ParseAddress(%q).Mode() = %v, want %v", c.in, got.Mode(), c.mode)
}
}
}
// session 位省略与 new 必须是两种不同语义:
// 省略 → 默认会话new → 强制新建;其他 → 必须已存在。
func TestSessionModeSemantics(t *testing.T) {
def, _ := ParseAddress("pi@root")
if def.Mode() != SessionDefault || def.IsNewSession() || !def.IsDefaultSession() {
t.Errorf("pi@root 应为默认会话,得到 mode=%v isNew=%v", def.Mode(), def.IsNewSession())
}
new_, _ := ParseAddress("pi@root.new")
if new_.Mode() != SessionNew || !new_.IsNewSession() || new_.IsDefaultSession() {
t.Errorf("pi@root.new 应为新建,得到 mode=%v", new_.Mode())
}
named, _ := ParseAddress("pi@root.fix-leak")
if named.Mode() != SessionNamed || named.IsNewSession() || named.IsDefaultSession() {
t.Errorf("pi@root.fix-leak 应为具名会话,得到 mode=%v", named.Mode())
}
}
func TestParseAddressErrors(t *testing.T) {
for _, in := range []string{"", " ", "@", "@ModelRouter.new"} {
if _, err := ParseAddress(in); err == nil {
t.Errorf("ParseAddress(%q) expected error, got nil", in)
}
}
}
func TestParseAddressList(t *testing.T) {
list, err := ParseAddressList("pi@root.new, deepseekharness@/program.upadtefeature;ops@prod")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(list) != 3 {
t.Fatalf("got %d addresses, want 3", len(list))
}
if list[0].Name != "pi" || !list[0].IsNewSession() {
t.Errorf("addr[0] = %+v", list[0])
}
if list[1].Path != "/program" || list[1].Session != "upadtefeature" {
t.Errorf("addr[1] = %+v", list[1])
}
if list[2].Name != "ops" || list[2].Path != "prod" || list[2].Mode() != SessionDefault {
t.Errorf("addr[2] = %+v", list[2])
}
empty, err := ParseAddressList(" ")
if err != nil || empty != nil {
t.Errorf("empty list = %v, %v; want nil, nil", empty, err)
}
}