feat: migrate from msgpack to protobuf (handler-base v1.0.0)
- Replace msgpack encoding with protobuf wire format - Update field names to proto convention - Use pointer slices for repeated message fields ([]*DocumentSource) - Rewrite tests for proto round-trips
This commit is contained in:
19
main.go
19
main.go
@@ -16,6 +16,7 @@ import (
|
||||
"git.daviestechlabs.io/daviestechlabs/handler-base/handler"
|
||||
"git.daviestechlabs.io/daviestechlabs/handler-base/messages"
|
||||
"git.daviestechlabs.io/daviestechlabs/handler-base/natsutil"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -43,13 +44,13 @@ func main() {
|
||||
|
||||
h := handler.New("voice.request", cfg)
|
||||
|
||||
h.OnTypedMessage(func(ctx context.Context, msg *nats.Msg) (any, error) {
|
||||
req, err := natsutil.Decode[messages.VoiceRequest](msg.Data)
|
||||
if err != nil {
|
||||
h.OnTypedMessage(func(ctx context.Context, msg *nats.Msg) (proto.Message, error) {
|
||||
var req messages.VoiceRequest
|
||||
if err := natsutil.Decode(msg.Data, &req); err != nil {
|
||||
return &messages.VoiceResponse{Error: "Invalid request encoding"}, nil
|
||||
}
|
||||
|
||||
requestID := req.RequestID
|
||||
requestID := req.RequestId
|
||||
if requestID == "" {
|
||||
requestID = "unknown"
|
||||
}
|
||||
@@ -64,8 +65,8 @@ func main() {
|
||||
|
||||
slog.Info("processing voice request", "request_id", requestID)
|
||||
|
||||
errResp := func(msg string) (any, error) {
|
||||
return &messages.VoiceResponse{RequestID: requestID, Error: msg}, nil
|
||||
errResp := func(msg string) (proto.Message, error) {
|
||||
return &messages.VoiceResponse{RequestId: requestID, Error: msg}, nil
|
||||
}
|
||||
|
||||
// 1. Audio arrives as raw bytes — no base64 decode needed
|
||||
@@ -146,7 +147,7 @@ func main() {
|
||||
|
||||
// Build typed response
|
||||
result := &messages.VoiceResponse{
|
||||
RequestID: requestID,
|
||||
RequestId: requestID,
|
||||
Response: responseText,
|
||||
Audio: responseAudio,
|
||||
}
|
||||
@@ -160,13 +161,13 @@ func main() {
|
||||
if len(documents) < limit {
|
||||
limit = len(documents)
|
||||
}
|
||||
result.Sources = make([]messages.DocumentSource, limit)
|
||||
result.Sources = make([]*messages.DocumentSource, limit)
|
||||
for i := 0; i < limit; i++ {
|
||||
text := documents[i].Document
|
||||
if len(text) > 200 {
|
||||
text = text[:200]
|
||||
}
|
||||
result.Sources[i] = messages.DocumentSource{Text: text, Score: documents[i].Score}
|
||||
result.Sources[i] = &messages.DocumentSource{Text: text, Score: documents[i].Score}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user