c1652e2bc6
Pure move: split the three multi-feature server files into per-concern files, with no behaviour change - same package, same exported and unexported identifiers, same call graph, same tests. - internal/server/chat.go -> chat.go (broker seam, client token, dispatcher), chat_hub.go, chat_claims.go, chat_permissions.go, chat_sessions.go, chat_stream.go, chat_commands.go - internal/stage/model.go -> model.go, model_run.go, model_failure.go, model_validation.go - internal/pipeline/runs.go -> runs.go (read side), runs_schema.go, runs_create.go, runs_mutate.go, runs_sessions.go Checked before committing: - Packaged templates and internal/author/skill/references: neither refers to internal/server/chat.go, internal/stage/model.go or internal/pipeline/runs.go, so nothing there needed updating (the change is a move with no behaviour change). - gofmt -l on the three packages: clean. go build ./..., go vet ./... and go test ./... pass. go.mod/go.sum unchanged. - No test, docs, android/, install or Docker files touched. Known stale path pointers deliberately left to the single docs writer (shared docs) and out of scope (android/): docs/chat-verification.md and android/README.md name the old file paths.
77 lines
2.8 KiB
Go
77 lines
2.8 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/opencode"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
func handleChatMessages(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) {
|
|
query := request.ChatQuery
|
|
if query == nil || !opencode.ValidSessionID(query.SessionID) {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
|
|
return
|
|
}
|
|
response := protocol.Message{Type: protocol.ChatMessages, ID: request.ID}
|
|
// Resume from retained server state first: a reconnecting client gets the
|
|
// accumulated turns with the same stable ids without re-reading OpenCode.
|
|
if messages := hub.snapshot(query.SessionID); len(messages) > 0 {
|
|
response.ChatMessages = messages
|
|
_ = write(response)
|
|
return
|
|
}
|
|
if broker == nil {
|
|
_ = write(response)
|
|
return
|
|
}
|
|
messages, err := broker.Messages(ctx, query.SessionID, query.Limit)
|
|
if err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
response.ChatMessages = messages
|
|
_ = write(response)
|
|
}
|
|
|
|
func handleChatSimple(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) {
|
|
if broker == nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
|
|
return
|
|
}
|
|
if request.Type == protocol.ChatInterrupt {
|
|
query := request.ChatQuery
|
|
if query == nil || !opencode.ValidSessionID(query.SessionID) {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
|
|
return
|
|
}
|
|
if err := broker.Interrupt(ctx, query.SessionID); err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
_ = write(protocol.Message{Type: protocol.ChatInterrupt, ID: request.ID})
|
|
return
|
|
}
|
|
reply := request.ChatReply
|
|
if reply == nil || !opencode.ValidSessionID(reply.SessionID) {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
|
|
return
|
|
}
|
|
if strings.TrimSpace(reply.RequestID) == "" {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a permission request id is required"})
|
|
return
|
|
}
|
|
switch reply.Decision {
|
|
case protocol.ChatDecisionOnce, protocol.ChatDecisionAlways, protocol.ChatDecisionReject:
|
|
default:
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid permission decision is required"})
|
|
return
|
|
}
|
|
if err := broker.Reply(ctx, *reply); err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
_ = write(protocol.Message{Type: protocol.ChatReply, ID: request.ID})
|
|
}
|