alt/services/worker/internal/socket/handlers.go
toki ec938475e0 chore: socket rail implementation and task archival
- Add backtest and market socket handlers for API and Worker
- Add proto definitions for backtest and market services
- Update client socket integration and tests
- Generate protobuf code for Go and Dart
- Archive completed agent tasks under agent-task/archive/2026/05/
2026-05-30 22:57:18 +09:00

68 lines
1.9 KiB
Go

package socket
import (
protoSocket "git.toki-labs.com/toki/proto-socket/go"
altv1 "git.toki-labs.com/toki/alt/packages/contracts/gen/go/alt/v1"
)
type sessionHandler struct {
requestType string
register func(*protoSocket.WsClient)
}
// sessionHandlers returns the handlers registered on every worker session.
// Hello is always present; market and backtest command/query handlers attach
// through the same registry instead of bespoke registration paths.
func sessionHandlers(deps Deps) []sessionHandler {
handlers := []sessionHandler{
helloHandler(),
}
handlers = append(handlers, marketHandlers(deps)...)
handlers = append(handlers, backtestHandlers(deps)...)
return handlers
}
// registerSessionHandlers builds the OnClientConnected callback bound to the
// given dependencies.
func registerSessionHandlers(deps Deps) func(*protoSocket.WsClient) {
handlers := sessionHandlers(deps)
return func(client *protoSocket.WsClient) {
registerHandlers(client, handlers)
}
}
func registerHandlers(client *protoSocket.WsClient, handlers []sessionHandler) {
for _, handler := range handlers {
if handler.register == nil {
continue
}
handler.register(client)
}
}
func helloHandler() sessionHandler {
return sessionHandler{
requestType: protoSocket.TypeNameOf(&altv1.HelloRequest{}),
register: func(client *protoSocket.WsClient) {
protoSocket.AddRequestListenerTyped[*altv1.HelloRequest, *altv1.HelloResponse](&client.Communicator, handleHello)
},
}
}
func handleHello(req *altv1.HelloRequest) (*altv1.HelloResponse, error) {
protocolVersion := req.GetAltProtocolVersion()
if protocolVersion == "" {
protocolVersion = defaultAltProtocolVersion
}
return &altv1.HelloResponse{
ServerName: serverName,
ServerVersion: serverVersion,
AltProtocolVersion: protocolVersion,
Capabilities: []string{
"hello",
"market-read",
"worker-execution",
},
}, nil
}