The transport could carry an actor and nothing filled it. It does now, so a dashboard approval is recorded as the person whose credential verified, and an agent's token is recorded as the agent's action -- for an agent, agent_approved; for a person, human_approved; and for a caller whose identity archie could not verify, task_approved, which claims nothing about who did it. The actor travels the path the action takes: webui reads it from the request context the middleware resolved, carries it in the gateway request, the daemon passes it through the in-process adapter, and the NATS request body carries it to the service that records it. Scope stays separate from actor at every hop, because scope is which task a caller may touch and the actor is who touched it. A chat channel's bound identity is an actor too: a bot acting on its own channel's task is recorded as an agent's action rather than a person's, via ActorFromScope, whose empty kind is what keeps it out of human_approved. The agent-facing task_action tool passes that identity and never input["identity"]. The UI process's credential check is now filled by endpoint references rather than by configuration: -oidc-issuer and -oidc-audience on archie-ui, two URLs the process already understands the shape of, with the verifier built in Run because cmd/ parses flags and nothing else. The UI package still never reads config, and a configured provider that cannot be reached fails boot rather than falling back to the gate that names nobody. Evidence for this change, stated as measured: go build ./... and go vet ./... are clean, and every package this touches passes -- webui, domain/taskactions, infrastructure/taskactions, infrastructure/gatewayrpc, gateway, app/archieui, app/archied, domain/messaging, channels/telegram, cmd/archie-ui. task check could not be run to completion: it fails in cmd/archie-messaging on an untracked internal/buildinfo package another lane has in flight, which is not this change and was left untouched. The end-to-end flip is covered in internal/webui/api_tasks_actor_test.go: a person's credential produces a record naming that person with human_approved, an agent's produces agent_approved, an unverified request produces task_approved with no actor, and a request carrying forged actor fields cannot name one.
259 lines
8.3 KiB
Go
259 lines
8.3 KiB
Go
package gatewayrpc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/gateway/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
|
"github.com/samcharles93/archie-core/internal/domain/taskactions"
|
|
"github.com/samcharles93/archie-core/internal/taskstate"
|
|
)
|
|
|
|
type server struct {
|
|
pb.UnimplementedChatServiceServer
|
|
chat messaging.ChatContract
|
|
sessions messaging.SessionStore
|
|
}
|
|
|
|
func RegisterServer(registrar grpc.ServiceRegistrar, chat messaging.ChatContract, sessions ...messaging.SessionStore) {
|
|
var ss messaging.SessionStore
|
|
if len(sessions) > 0 {
|
|
ss = sessions[0]
|
|
}
|
|
pb.RegisterChatServiceServer(registrar, &server{chat: chat, sessions: ss})
|
|
}
|
|
|
|
func (s *server) sessionStore() (messaging.SessionStore, error) {
|
|
if s.sessions == nil {
|
|
return nil, status.Error(codes.Unavailable, "session store unavailable")
|
|
}
|
|
return s.sessions, nil
|
|
}
|
|
|
|
func (s *server) Snapshot(ctx context.Context, _ *pb.SnapshotRequest) (*pb.SnapshotResponse, error) {
|
|
v, err := s.chat.Snapshot(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return snapshotProto(v), nil
|
|
}
|
|
|
|
func (s *server) GetSession(ctx context.Context, r *pb.GetSessionRequest) (*pb.GetSessionResponse, error) {
|
|
v, found, err := s.chat.GetSession(ctx, r.SessionId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &pb.GetSessionResponse{Session: sessionProto(v), Found: found}, nil
|
|
}
|
|
|
|
func (s *server) RecentMessages(ctx context.Context, r *pb.RecentMessagesRequest) (*pb.RecentMessagesResponse, error) {
|
|
v, err := s.chat.RecentMessages(ctx, r.SessionId, int(r.Limit))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &pb.RecentMessagesResponse{Messages: mapValues(v, storedProto)}, nil
|
|
}
|
|
|
|
func (s *server) RecentTurns(ctx context.Context, r *pb.RecentTurnsRequest) (*pb.RecentTurnsResponse, error) {
|
|
v, err := s.chat.RecentTurns(ctx, r.SessionId, int(r.Limit))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &pb.RecentTurnsResponse{Turns: mapValues(v, turnProto)}, nil
|
|
}
|
|
|
|
func (s *server) Route(ctx context.Context, r *pb.RouteRequest) (*pb.RouteResponse, error) {
|
|
v, err := s.chat.Route(ctx, inboundValue(r.Message))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &pb.RouteResponse{Text: v.Text, SessionId: v.SessionID}, nil
|
|
}
|
|
|
|
func (s *server) Cancel(ctx context.Context, r *pb.CancelRequest) (*pb.CancelResponse, error) {
|
|
v, err := s.chat.Cancel(ctx, r.SessionId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &pb.CancelResponse{Cancelled: v.Cancelled, Dropped: int64(v.Dropped)}, nil
|
|
}
|
|
|
|
func (s *server) SetPersona(ctx context.Context, r *pb.SetPersonaRequest) (*pb.SetPersonaResponse, error) {
|
|
v, err := s.chat.SetPersona(ctx, r.SessionId, r.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &pb.SetPersonaResponse{Found: v}, nil
|
|
}
|
|
|
|
func (s *server) ApplyTaskAction(ctx context.Context, r *pb.ApplyTaskActionRequest) (*pb.ApplyTaskActionResponse, error) {
|
|
v, err := s.chat.ApplyTaskAction(ctx, r.Identity, r.TaskId, taskstate.Action(r.Action))
|
|
if err != nil {
|
|
return nil, taskActionStatus(err)
|
|
}
|
|
return &pb.ApplyTaskActionResponse{TaskId: v.TaskID, Action: v.Action, Message: v.Message}, nil
|
|
}
|
|
|
|
func (s *server) ApplyOperatorTaskAction(ctx context.Context, r *pb.ApplyOperatorTaskActionRequest) (*pb.ApplyOperatorTaskActionResponse, error) {
|
|
actor := taskactions.Actor{
|
|
Identity: identity.IdentityID(r.ActorId),
|
|
Kind: identity.Kind(r.ActorKind),
|
|
Principal: identity.IdentityID(r.PrincipalId),
|
|
}
|
|
v, err := s.chat.ApplyOperatorTaskAction(ctx, actor, r.TaskId, taskstate.Action(r.Action))
|
|
if err != nil {
|
|
return nil, taskActionStatus(err)
|
|
}
|
|
return &pb.ApplyOperatorTaskActionResponse{TaskId: v.TaskID, Action: v.Action, Message: v.Message}, nil
|
|
}
|
|
|
|
func (s *server) SaveSession(ctx context.Context, r *pb.SaveSessionRequest) (*pb.SaveSessionResponse, error) {
|
|
ss, e := s.sessionStore()
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
return &pb.SaveSessionResponse{}, ss.Save(ctx, sessionValue(r.Session))
|
|
}
|
|
|
|
func (s *server) GetSessionsByChannel(ctx context.Context, r *pb.GetSessionsByChannelRequest) (*pb.GetSessionsByChannelResponse, error) {
|
|
ss, e := s.sessionStore()
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
v, e := ss.GetByChannel(ctx, r.Platform, r.ChannelId)
|
|
return &pb.GetSessionsByChannelResponse{Sessions: mapValues(v, sessionProto)}, e
|
|
}
|
|
|
|
func (s *server) DeleteSession(ctx context.Context, r *pb.DeleteSessionRequest) (*pb.DeleteSessionResponse, error) {
|
|
ss, e := s.sessionStore()
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
return &pb.DeleteSessionResponse{}, ss.Delete(ctx, r.SessionId)
|
|
}
|
|
|
|
func (s *server) TouchSession(ctx context.Context, r *pb.TouchSessionRequest) (*pb.TouchSessionResponse, error) {
|
|
ss, e := s.sessionStore()
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
return &pb.TouchSessionResponse{}, ss.Touch(ctx, r.SessionId)
|
|
}
|
|
|
|
func (s *server) ListSessions(ctx context.Context, _ *pb.ListSessionsRequest) (*pb.ListSessionsResponse, error) {
|
|
ss, e := s.sessionStore()
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
v, e := ss.List(ctx)
|
|
return &pb.ListSessionsResponse{Sessions: mapValues(v, sessionProto)}, e
|
|
}
|
|
|
|
// storedMessages converts wire messages to canonical records, deriving
|
|
// roles from the owning session's bot identity (see
|
|
// messaging.RoleForSender). The wire carries no role; both sides derive from
|
|
// the same session, so they agree.
|
|
func (s *server) storedMessages(ctx context.Context, ss messaging.SessionStore, sessionID string, msgs []*pb.Message) ([]messaging.Message, error) {
|
|
var botUser string
|
|
if sc, err := ss.Get(ctx, sessionID); err != nil {
|
|
return nil, err
|
|
} else if sc != nil {
|
|
botUser = sc.Source.BotUser
|
|
}
|
|
out := make([]messaging.Message, 0, len(msgs))
|
|
for _, m := range msgs {
|
|
stored := storedValue(m)
|
|
stored.Role = messaging.RoleForSender(stored.Sender, botUser)
|
|
out = append(out, stored)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (s *server) SaveMessage(ctx context.Context, r *pb.SaveMessageRequest) (*pb.SaveMessageResponse, error) {
|
|
ss, e := s.sessionStore()
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
msgs, e := s.storedMessages(ctx, ss, r.SessionId, []*pb.Message{r.Message})
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
return &pb.SaveMessageResponse{}, ss.SaveMessage(ctx, r.SessionId, msgs[0])
|
|
}
|
|
|
|
func (s *server) DeleteRecentMessages(ctx context.Context, r *pb.DeleteRecentMessagesRequest) (*pb.DeleteRecentMessagesResponse, error) {
|
|
ss, e := s.sessionStore()
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
v, e := ss.DeleteRecentMessages(ctx, r.SessionId, int(r.Limit))
|
|
return &pb.DeleteRecentMessagesResponse{Deleted: int64(v)}, e
|
|
}
|
|
|
|
func (s *server) MessageCount(ctx context.Context, r *pb.MessageCountRequest) (*pb.MessageCountResponse, error) {
|
|
ss, e := s.sessionStore()
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
v, e := ss.MessageCount(ctx, r.SessionId)
|
|
return &pb.MessageCountResponse{Count: int64(v)}, e
|
|
}
|
|
|
|
func (s *server) SaveMessages(ctx context.Context, r *pb.SaveMessagesRequest) (*pb.SaveMessagesResponse, error) {
|
|
ss, e := s.sessionStore()
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
msgs, e := s.storedMessages(ctx, ss, r.SessionId, r.Messages)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
return &pb.SaveMessagesResponse{}, ss.SaveMessages(ctx, r.SessionId, msgs)
|
|
}
|
|
|
|
func (s *server) ReplaceMessages(ctx context.Context, r *pb.ReplaceMessagesRequest) (*pb.ReplaceMessagesResponse, error) {
|
|
ss, e := s.sessionStore()
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
msgs, e := s.storedMessages(ctx, ss, r.SessionId, r.Messages)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
return &pb.ReplaceMessagesResponse{}, ss.ReplaceMessages(ctx, r.SessionId, msgs, r.Superseded)
|
|
}
|
|
|
|
func (s *server) SearchMessages(ctx context.Context, r *pb.SearchMessagesRequest) (*pb.SearchMessagesResponse, error) {
|
|
ss, e := s.sessionStore()
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
v, e := ss.SearchMessages(ctx, r.SessionId, messaging.MessageQuery{Query: r.Query, Limit: int(r.Limit), Offset: int(r.Offset)})
|
|
return &pb.SearchMessagesResponse{Messages: mapValues(v.Messages, storedProto), NextOffset: int64(v.NextOffset), HasMore: v.HasMore, Truncated: v.Truncated}, e
|
|
}
|
|
|
|
func (s *server) Stream(r *pb.StreamRequest, out grpc.ServerStreamingServer[pb.StreamResponse]) error {
|
|
events, err := s.chat.Stream(out.Context(), inboundValue(r.Message))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for {
|
|
select {
|
|
case <-out.Context().Done():
|
|
return out.Context().Err()
|
|
case event, ok := <-events:
|
|
if !ok {
|
|
return nil
|
|
}
|
|
if err := out.Send(eventProto(event)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|