Skip to content

refactor(gateway): 传输层抽象与分层 ActionRegistry(Step 1)#475

Merged
phantom5099 merged 2 commits into1024XEngineer:mainfrom
pionxe:main
Apr 27, 2026
Merged

refactor(gateway): 传输层抽象与分层 ActionRegistry(Step 1)#475
phantom5099 merged 2 commits into1024XEngineer:mainfrom
pionxe:main

Conversation

@pionxe
Copy link
Copy Markdown
Collaborator

@pionxe pionxe commented Apr 26, 2026

背景

NeoCode 网关层当前的 transport 协议(IPC/HTTP)和 action handler 均以硬编码方式耦合在核心文件中。每次新增接入面(gRPC)或新增能力(飞书、CLI 诊断),都需要直接修改 gateway_commands.go 和 bootstrap.go,违背"对扩展开放、对修改封闭"原则。

##做了什么

  1. 新增 TransportAdapter 接口——定义 ListenAddress / Serve / Close 三方法契约,Server 和 NetworkServer 零修改满足
  2. 新增分层 ActionRegistry——core map 在 initCore() 中固定注册全部 15 个内置 handler(不可覆盖),extended map 供 Register / MustRegister 动态挂载
  3. 分发链路切换——dispatchRequestFrame 从 var requestFrameHandlers map 改为 defaultRegistry.Lookup
  4. CLI 装配层重构——删除本地 gatewayServer / gatewayNetworkServer 接口,启动改为 []TransportAdapter 遍历,IPC 保持主阻塞、HTTP goroutine 降级语义不变
  5. 测试——新增 6 个 registry 单测(core 不可覆盖、扩展注册、重复冲突、非法输入、panic 路径)+ 编译期接口断言

验证

  • go build ./... 通过
  • go test ./... 全部通过
  • TUI 端到端对话链路不受影响

影响范围

  • 零业务语义变更,JSON-RPC 契约无变化
  • 核心 handler 均在 initCore() 中保留,安全边界不变
  • 新增 RegisterAction / MustRegisterAction 公开 API,供后续扩展包使用

Closes #474

引入轻量级 TransportAdapter 接口和分层的 ActionRegistry,
为后续 gRPC、飞书适配器、CLI 诊断等扩展扫清架构障碍。

变更内容:
- 新增 TransportAdapter 接口(contracts.go),Server 与
  NetworkServer 隐式实现,无需修改内部逻辑
- 新增分层 ActionRegistry(registry.go),core 层保护 15 个
  内置 handler 不可覆盖,extended 层供外部包动态注册
- bootstrap.go 分发链路从硬编码 var map 切换至
  defaultRegistry.Lookup,扩展动作可通过
  RegisterAction/MustRegisterAction 注册
- CLI 装配层统一使用 []TransportAdapter 遍历启动,保留
  IPC 主阻塞、HTTP goroutine 降级语义
- 清理 CLI 层本地 interface 定义,统一复用 gateway 包类型
- 补齐 registry 行为测试与编译期接口断言
@chatgpt-codex-connector
Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 26, 2026

Codecov Report

❌ Patch coverage is 80.95238% with 16 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/gateway/registry.go 83.05% 7 Missing and 3 partials ⚠️
internal/cli/gateway_commands.go 75.00% 5 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

fennoai[bot]

This comment was marked as outdated.

@phantom5099
Copy link
Copy Markdown
Collaborator

可以,方案上和代码实施我看着基本一致

@phantom5099
Copy link
Copy Markdown
Collaborator

/review

Copy link
Copy Markdown

@fennoai fennoai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

整体改动方向清晰,分层 ActionRegistryTransportAdapter 抽象基本合理。仅有 1 条建议:当前传输适配器与其名称通过两个并行切片维护,和“后续扩展传输层”目标存在可维护性冲突,建议改为单一结构体切片避免索引耦合。

Comment thread internal/cli/gateway_commands.go Outdated
logger.Printf("gateway ipc listen address: %s", ipcServer.ListenAddress())
logger.Printf("gateway network listen address: %s", networkServer.ListenAddress())
for index, adapter := range transportAdapters {
logger.Printf("gateway %s listen address: %s", transportNames[index], adapter.ListenAddress())
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transportAdapterstransportNames 通过索引绑定(并在后续日志中继续使用该假设),后续新增/重排适配器时很容易出现长度不一致导致 panic,或日志名称与真实适配器不一致。建议改为 []struct{name string; adapter gateway.TransportAdapter} 这类单一数据结构,避免并行切片耦合。

@phantom5099 phantom5099 merged commit 5216bf9 into 1024XEngineer:main Apr 27, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Step 1:轻量传输层抽象与分层 Action Registry (务实的地基重构)

2 participants