Files
gobridge/protocol.go
what b390effd8e feat: 添加 Python→Go 全双工回调支持(call_go)
- 新增 WithHandlers 选项,通过反射将 Go 结构体方法暴露给 Python
- 新增 callback/callback_result 消息类型,支持 Python 在处理中回调 Go
- client 侧新增 readResult,内联处理 callback,复用同一连接避免死锁
- Python 侧新增 call_go[T]() 泛型调用,支持 dataclass 自动构造
- 注入 GOBRIDGE_WORKER_ID/WORKER_COUNT 环境变量,支持多 worker 初始化分工
- 新增示例演示 Go→Python→Go→Python 四层全双工链路
- Python 包版本升至 0.1.1
2026-04-14 13:06:50 +08:00

29 lines
1.3 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 gobridge
import "encoding/json"
// 消息类型常量
const (
TypeCall = "call" // Go → Python: 调用请求
TypeResult = "result" // Python → Go: 调用结果
TypeError = "error" // 双向: 错误响应
TypeChunk = "chunk" // 双向: 流数据块
TypeEnd = "end" // 双向: 流结束标记
TypeCancel = "cancel" // Go → Python: 取消正在执行的调用ctx 取消时发送)
TypeCallback = "callback" // Python → Go: 调用 Go 注册方法
TypeCallbackResult = "callback_result" // Go → Python: Go 方法调用结果
)
// Message 是 Go 与 Python 之间传输的消息结构
// 使用 4 字节大端长度前缀 + JSON 编码
type Message struct {
ID uint64 `json:"id"`
Type string `json:"type"`
Method string `json:"method,omitempty"`
Args json.RawMessage `json:"args,omitempty"`
StreamInput bool `json:"stream_input,omitempty"` // 是否有流式输入参数
StreamArgIdx int `json:"stream_arg_idx,omitempty"` // 流式参数在 args 中的下标
Data json.RawMessage `json:"data,omitempty"` // 结果/数据块内容
Error string `json:"error,omitempty"`
}