diff --git a/internal/app/archiemessaging/compose.go b/internal/app/archiemessaging/compose.go index 7fd3098a4..aea590008 100644 --- a/internal/app/archiemessaging/compose.go +++ b/internal/app/archiemessaging/compose.go @@ -5,6 +5,7 @@ import ( "fmt" "log/slog" "net" + "slices" "strconv" "sync" "sync/atomic" @@ -31,11 +32,13 @@ type deps struct { // Presence reads which version each service reports for itself, to verify // update reports. Nil leaves them unverified. Presence storecontract.PresenceStore - Config ResolvedConfig - Log *slog.Logger - Chat messaging.ChatContract - Health *health.Registry - Settings *messaging.SettingsCommand + Config ResolvedConfig + // ExtensionChannels are channels served by installed channel extensions. + ExtensionChannels []*channelInstance + Log *slog.Logger + Chat messaging.ChatContract + Health *health.Registry + Settings *messaging.SettingsCommand // Secrets resolves channel credential references, including those naming an // extension engine. Secrets *secret.Registry @@ -169,6 +172,13 @@ func composeChannels(ctx context.Context, d deps) ([]*channelInstance, error) { } instances = append(instances, instance) } + for _, instance := range d.ExtensionChannels { + if slices.ContainsFunc(instances, func(have *channelInstance) bool { return have.name == instance.name }) { + d.Log.Warn("channel extension skipped: a built-in channel has the same name", "channel", instance.name) + continue + } + instances = append(instances, instance) + } return instances, nil } diff --git a/internal/app/archiemessaging/extension_channels.go b/internal/app/archiemessaging/extension_channels.go new file mode 100644 index 000000000..274961706 --- /dev/null +++ b/internal/app/archiemessaging/extension_channels.go @@ -0,0 +1,98 @@ +package archiemessaging + +import ( + "context" + "fmt" + "log/slog" + "os" + "path/filepath" + "slices" + "strings" + + "github.com/hashicorp/go-hclog" + + "github.com/samcharles93/archie-core/internal/channels" + "github.com/samcharles93/archie-core/internal/domain/storepkg" + "github.com/samcharles93/archie-core/internal/infrastructure/channelext" + "github.com/samcharles93/archie-core/internal/infrastructure/extension" + "github.com/samcharles93/archie-core/internal/secret" +) + +// refSuffix marks a setting that holds an "engine:key" secret reference. +const refSuffix = "_ref" + +// openExtensionChannels builds a channel for each installed channel package +// whose authority is accepted and that extension-settings enables. Changing +// the set takes a restart, as every channel binding does. Running extensions +// stop with ctx. +func openExtensionChannels(ctx context.Context, source extension.Source, secrets *secret.Registry, log *slog.Logger) []*channelInstance { + cache, err := os.UserCacheDir() + if err != nil { + log.Warn("channel extensions unavailable: no cache directory", "err", err) + return nil + } + settings, _, err := source.Enabled(ctx) + if err != nil { + log.Warn("channel extensions unavailable: extension settings", "err", err) + return nil + } + installed, err := source.Packages.ListInstalled(ctx, extension.OrgID) + if err != nil { + log.Warn("channel extensions unavailable: installed packages", "err", err) + return nil + } + host := extension.NewHost(hclog.New(&hclog.LoggerOptions{Name: "extension", Level: hclog.Warn})) + var instances []*channelInstance + for _, pkg := range installed { + setting, ok := settings[pkg.Name] + if !ok || !setting.Enabled || pkg.AcceptedAuthority == nil { + continue + } + index := slices.IndexFunc(pkg.Descriptor.Contributes.Extensions, func(x storepkg.Extension) bool { return x.Surface == storepkg.SurfaceChannel }) + if index < 0 { + continue + } + ch, err := buildExtensionChannel(ctx, host, source, filepath.Join(cache, "archie", "extensions", "channels"), pkg, pkg.Descriptor.Contributes.Extensions[index].Path, setting.Settings, secrets) + if err != nil { + log.Warn("channel extension not started", "channel", pkg.Name, "err", err) + continue + } + instances = append(instances, &channelInstance{ + name: pkg.Name, + channel: ch, + rebuild: func(ResolvedConfig) (channels.Channel, error) { return ch, nil }, + }) + } + go func() { + <-ctx.Done() + host.Close() + }() + return instances +} + +func buildExtensionChannel(ctx context.Context, host *extension.Host, source extension.Source, cacheDir string, pkg storepkg.Installed, file string, settings map[string]string, secrets *secret.Registry) (channels.Channel, error) { + binary, sum, err := extension.Materialize(ctx, source.Packages, cacheDir, pkg, file) + if err != nil { + return nil, err + } + plain := make(map[string]string, len(settings)) + resolved := make(map[string]string) + for key, value := range settings { + name, isRef := strings.CutSuffix(key, refSuffix) + if !isRef { + plain[key] = value + continue + } + engine, ref, ok := strings.Cut(value, ":") + if !ok { + return nil, fmt.Errorf("setting %q: want engine:key", key) + } + secretValue, err := secrets.Resolve(secret.SecretRef{Engine: engine, Key: ref}) + if err != nil { + return nil, fmt.Errorf("setting %q: %w", key, err) + } + resolved[name] = secretValue + } + spec := extension.Spec{Name: "channel/" + pkg.Name, Path: binary, SHA256: sum, Env: slices.Clone(pkg.AcceptedAuthority.Env)} + return channelext.New(host, pkg.Name, spec, plain, resolved), nil +} diff --git a/internal/app/archiemessaging/run.go b/internal/app/archiemessaging/run.go index c934465ea..ef277f73b 100644 --- a/internal/app/archiemessaging/run.go +++ b/internal/app/archiemessaging/run.go @@ -12,6 +12,7 @@ import ( "github.com/samcharles93/archie-core/internal/domain/presence" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc" + "github.com/samcharles93/archie-core/internal/infrastructure/extension" "github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc" "github.com/samcharles93/archie-core/internal/infrastructure/secretengine" "github.com/samcharles93/archie-core/internal/infrastructure/staterpc" @@ -36,7 +37,6 @@ func Run(ctx context.Context, o Options) error { secrets := secret.NewRegistry() health := newReadinessRegistry(cfg.Options, chat) var settings *messaging.SettingsCommand - var closeStateStore func() // channelStatusStore is the same client, held where compose can reach it: the // channel report is published to the store this process already dials. var channelStatusStore storecontract.ChannelStatusStore @@ -48,18 +48,17 @@ func Run(ctx context.Context, o Options) error { var settingsSource chatSettingsSource var applyReporter *applystatus.Reporter var appliedVersion int64 + var extensionChannels []*channelInstance if cfg.Options.StateStore.Target != "" { stateStore, closeClient, dialErr := staterpc.Dial(cfg.Options.StateStore.Target, presence.Messaging, cfg.Options.StateStore.Token, staterpc.WaitForPeer) if dialErr != nil { return fmt.Errorf("dial state store control plane (%s): %w", cfg.Options.StateStore.Target, dialErr) } - closeStateStore = closeClient - defer closeStateStore() + defer closeClient() controlPlaneClient := controlplanerpc.NewRPCClient(stateStore.ControlPlane()) reporter := applystatus.New(applystatus.Messaging, stateStore, log) - defer secretengine.Supervise(ctx, secrets, secretengine.Source{Query: controlPlaneClient, Packages: stateStore}, applystatus.Messaging, func(ctx context.Context, version int64, err error) { - reporter.Report(ctx, controlplanerpc.ExtensionSettingsKind, version, err) - }, log).Close() + extensions := extension.Source{Query: controlPlaneClient, Packages: stateStore} + defer superviseSecretEngines(ctx, extensions, secrets, reporter, log)() chatSettings, channelVersion, settingsErr := controlPlaneClient.RuntimeChatConfig(ctx, config.ChatConfig{ Telegram: cfg.Telegram, Email: cfg.Email, Webhook: cfg.Webhook, WebhookAddr: cfg.WebhookAddr, ShowToolCalls: cfg.ShowToolCalls, }) @@ -78,6 +77,7 @@ func Run(ctx context.Context, o Options) error { if settingsErr != nil { return fmt.Errorf("resolve database webhook secret: %w", settingsErr) } + extensionChannels = openExtensionChannels(ctx, extensions, secrets, log) settings = messaging.NewSettingsCommand(messagingControlPlane{client: stateStore.ControlPlane()}).WithIdentities(stateStore) channelStatusStore, presenceStore = stateStore, stateStore settingsSource, applyReporter, appliedVersion = controlPlaneClient, reporter, channelVersion @@ -89,17 +89,18 @@ func Run(ctx context.Context, o Options) error { } } srv, err := compose(ctx, deps{ - Secrets: secrets, - ChannelStatus: channelStatusStore, - Presence: presenceStore, - Config: cfg, - Log: log, - Chat: chat, - Health: health, - Settings: settings, - SettingsSource: settingsSource, - ApplyReporter: applyReporter, - AppliedVersion: appliedVersion, + Secrets: secrets, + ChannelStatus: channelStatusStore, + Presence: presenceStore, + Config: cfg, + ExtensionChannels: extensionChannels, + Log: log, + Chat: chat, + Health: health, + Settings: settings, + SettingsSource: settingsSource, + ApplyReporter: applyReporter, + AppliedVersion: appliedVersion, }) if err != nil { return err @@ -107,3 +108,11 @@ func Run(ctx context.Context, o Options) error { return srv.Start(ctx) } + +// superviseSecretEngines runs the secret-engine extensions and reports their +// settings outcome; the returned function stops them. +func superviseSecretEngines(ctx context.Context, source extension.Source, secrets *secret.Registry, reporter *applystatus.Reporter, log *slog.Logger) func() { + return secretengine.Supervise(ctx, secrets, source, applystatus.Messaging, func(ctx context.Context, version int64, err error) { + reporter.Report(ctx, controlplanerpc.ExtensionSettingsKind, version, err) + }, log).Close +} diff --git a/internal/contracts/channel/v1/channel.pb.go b/internal/contracts/channel/v1/channel.pb.go new file mode 100644 index 000000000..f5b46d57a --- /dev/null +++ b/internal/contracts/channel/v1/channel.pb.go @@ -0,0 +1,269 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.11 +// protoc (unknown) +// source: channel/v1/channel.proto + +package channelv1 + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" + unsafe "unsafe" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type RunResponse_State int32 + +const ( + RunResponse_STATE_UNSPECIFIED RunResponse_State = 0 + RunResponse_STATE_STARTING RunResponse_State = 1 + RunResponse_STATE_RUNNING RunResponse_State = 2 +) + +// Enum value maps for RunResponse_State. +var ( + RunResponse_State_name = map[int32]string{ + 0: "STATE_UNSPECIFIED", + 1: "STATE_STARTING", + 2: "STATE_RUNNING", + } + RunResponse_State_value = map[string]int32{ + "STATE_UNSPECIFIED": 0, + "STATE_STARTING": 1, + "STATE_RUNNING": 2, + } +) + +func (x RunResponse_State) Enum() *RunResponse_State { + p := new(RunResponse_State) + *p = x + return p +} + +func (x RunResponse_State) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (RunResponse_State) Descriptor() protoreflect.EnumDescriptor { + return file_channel_v1_channel_proto_enumTypes[0].Descriptor() +} + +func (RunResponse_State) Type() protoreflect.EnumType { + return &file_channel_v1_channel_proto_enumTypes[0] +} + +func (x RunResponse_State) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use RunResponse_State.Descriptor instead. +func (RunResponse_State) EnumDescriptor() ([]byte, []int) { + return file_channel_v1_channel_proto_rawDescGZIP(), []int{1, 0} +} + +type RunRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + // settings are the operator's values for this channel from extension + // settings. A key ending in _ref holds an "engine:key" secret reference the + // host resolves; the plugin receives the value in secrets, under the key + // without the suffix. + Settings map[string]string `protobuf:"bytes,1,rep,name=settings,proto3" json:"settings,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` + Secrets map[string]string `protobuf:"bytes,2,rep,name=secrets,proto3" json:"secrets,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` + // chat_broker_id is the plugin broker stream the host serves + // gateway.v1.ChatService on. + ChatBrokerId uint32 `protobuf:"varint,3,opt,name=chat_broker_id,json=chatBrokerId,proto3" json:"chat_broker_id,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *RunRequest) Reset() { + *x = RunRequest{} + mi := &file_channel_v1_channel_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *RunRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RunRequest) ProtoMessage() {} + +func (x *RunRequest) ProtoReflect() protoreflect.Message { + mi := &file_channel_v1_channel_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RunRequest.ProtoReflect.Descriptor instead. +func (*RunRequest) Descriptor() ([]byte, []int) { + return file_channel_v1_channel_proto_rawDescGZIP(), []int{0} +} + +func (x *RunRequest) GetSettings() map[string]string { + if x != nil { + return x.Settings + } + return nil +} + +func (x *RunRequest) GetSecrets() map[string]string { + if x != nil { + return x.Secrets + } + return nil +} + +func (x *RunRequest) GetChatBrokerId() uint32 { + if x != nil { + return x.ChatBrokerId + } + return 0 +} + +type RunResponse struct { + state protoimpl.MessageState `protogen:"open.v1"` + State RunResponse_State `protobuf:"varint,1,opt,name=state,proto3,enum=channel.v1.RunResponse_State" json:"state,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *RunResponse) Reset() { + *x = RunResponse{} + mi := &file_channel_v1_channel_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *RunResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RunResponse) ProtoMessage() {} + +func (x *RunResponse) ProtoReflect() protoreflect.Message { + mi := &file_channel_v1_channel_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RunResponse.ProtoReflect.Descriptor instead. +func (*RunResponse) Descriptor() ([]byte, []int) { + return file_channel_v1_channel_proto_rawDescGZIP(), []int{1} +} + +func (x *RunResponse) GetState() RunResponse_State { + if x != nil { + return x.State + } + return RunResponse_STATE_UNSPECIFIED +} + +var File_channel_v1_channel_proto protoreflect.FileDescriptor + +const file_channel_v1_channel_proto_rawDesc = "" + + "\n" + + "\x18channel/v1/channel.proto\x12\n" + + "channel.v1\"\xac\x02\n" + + "\n" + + "RunRequest\x12@\n" + + "\bsettings\x18\x01 \x03(\v2$.channel.v1.RunRequest.SettingsEntryR\bsettings\x12=\n" + + "\asecrets\x18\x02 \x03(\v2#.channel.v1.RunRequest.SecretsEntryR\asecrets\x12$\n" + + "\x0echat_broker_id\x18\x03 \x01(\rR\fchatBrokerId\x1a;\n" + + "\rSettingsEntry\x12\x10\n" + + "\x03key\x18\x01 \x01(\tR\x03key\x12\x14\n" + + "\x05value\x18\x02 \x01(\tR\x05value:\x028\x01\x1a:\n" + + "\fSecretsEntry\x12\x10\n" + + "\x03key\x18\x01 \x01(\tR\x03key\x12\x14\n" + + "\x05value\x18\x02 \x01(\tR\x05value:\x028\x01\"\x89\x01\n" + + "\vRunResponse\x123\n" + + "\x05state\x18\x01 \x01(\x0e2\x1d.channel.v1.RunResponse.StateR\x05state\"E\n" + + "\x05State\x12\x15\n" + + "\x11STATE_UNSPECIFIED\x10\x00\x12\x12\n" + + "\x0eSTATE_STARTING\x10\x01\x12\x11\n" + + "\rSTATE_RUNNING\x10\x022J\n" + + "\x0eChannelService\x128\n" + + "\x03Run\x12\x16.channel.v1.RunRequest\x1a\x17.channel.v1.RunResponse0\x01B\xb4\x01\n" + + "\x0ecom.channel.v1B\fChannelProtoP\x01ZKgithub.com/samcharles93/archie-core/internal/contracts/channel/v1;channelv1\xa2\x02\x03CXX\xaa\x02\n" + + "Channel.V1\xca\x02\n" + + "Channel\\V1\xe2\x02\x16Channel\\V1\\GPBMetadata\xea\x02\vChannel::V1b\x06proto3" + +var ( + file_channel_v1_channel_proto_rawDescOnce sync.Once + file_channel_v1_channel_proto_rawDescData []byte +) + +func file_channel_v1_channel_proto_rawDescGZIP() []byte { + file_channel_v1_channel_proto_rawDescOnce.Do(func() { + file_channel_v1_channel_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_channel_v1_channel_proto_rawDesc), len(file_channel_v1_channel_proto_rawDesc))) + }) + return file_channel_v1_channel_proto_rawDescData +} + +var file_channel_v1_channel_proto_enumTypes = make([]protoimpl.EnumInfo, 1) +var file_channel_v1_channel_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_channel_v1_channel_proto_goTypes = []any{ + (RunResponse_State)(0), // 0: channel.v1.RunResponse.State + (*RunRequest)(nil), // 1: channel.v1.RunRequest + (*RunResponse)(nil), // 2: channel.v1.RunResponse + nil, // 3: channel.v1.RunRequest.SettingsEntry + nil, // 4: channel.v1.RunRequest.SecretsEntry +} +var file_channel_v1_channel_proto_depIdxs = []int32{ + 3, // 0: channel.v1.RunRequest.settings:type_name -> channel.v1.RunRequest.SettingsEntry + 4, // 1: channel.v1.RunRequest.secrets:type_name -> channel.v1.RunRequest.SecretsEntry + 0, // 2: channel.v1.RunResponse.state:type_name -> channel.v1.RunResponse.State + 1, // 3: channel.v1.ChannelService.Run:input_type -> channel.v1.RunRequest + 2, // 4: channel.v1.ChannelService.Run:output_type -> channel.v1.RunResponse + 4, // [4:5] is the sub-list for method output_type + 3, // [3:4] is the sub-list for method input_type + 3, // [3:3] is the sub-list for extension type_name + 3, // [3:3] is the sub-list for extension extendee + 0, // [0:3] is the sub-list for field type_name +} + +func init() { file_channel_v1_channel_proto_init() } +func file_channel_v1_channel_proto_init() { + if File_channel_v1_channel_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: unsafe.Slice(unsafe.StringData(file_channel_v1_channel_proto_rawDesc), len(file_channel_v1_channel_proto_rawDesc)), + NumEnums: 1, + NumMessages: 4, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_channel_v1_channel_proto_goTypes, + DependencyIndexes: file_channel_v1_channel_proto_depIdxs, + EnumInfos: file_channel_v1_channel_proto_enumTypes, + MessageInfos: file_channel_v1_channel_proto_msgTypes, + }.Build() + File_channel_v1_channel_proto = out.File + file_channel_v1_channel_proto_goTypes = nil + file_channel_v1_channel_proto_depIdxs = nil +} diff --git a/internal/contracts/channel/v1/channel_grpc.pb.go b/internal/contracts/channel/v1/channel_grpc.pb.go new file mode 100644 index 000000000..cd87a9e59 --- /dev/null +++ b/internal/contracts/channel/v1/channel_grpc.pb.go @@ -0,0 +1,142 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.6.2 +// - protoc (unknown) +// source: channel/v1/channel.proto + +package channelv1 + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.64.0 or later. +const _ = grpc.SupportPackageIsVersion9 + +const ( + ChannelService_Run_FullMethodName = "/channel.v1.ChannelService/Run" +) + +// ChannelServiceClient is the client API for ChannelService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +// +// ChannelService is the surface a chat channel extension serves. The host +// launches one plugin process per enabled channel and serves the gateway +// ChatService back to it over the plugin broker, so a channel carries messages +// in and out through that service and never dials the gateway itself. +type ChannelServiceClient interface { + // Run starts the channel and keeps it running until the call is cancelled or + // the channel fails. The stream reports lifecycle: STARTING before each + // launch attempt, RUNNING once the channel can receive. A stream that ends + // with an error is a failed channel; the host restarts it. + Run(ctx context.Context, in *RunRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[RunResponse], error) +} + +type channelServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewChannelServiceClient(cc grpc.ClientConnInterface) ChannelServiceClient { + return &channelServiceClient{cc} +} + +func (c *channelServiceClient) Run(ctx context.Context, in *RunRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[RunResponse], error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + stream, err := c.cc.NewStream(ctx, &ChannelService_ServiceDesc.Streams[0], ChannelService_Run_FullMethodName, cOpts...) + if err != nil { + return nil, err + } + x := &grpc.GenericClientStream[RunRequest, RunResponse]{ClientStream: stream} + if err := x.ClientStream.SendMsg(in); err != nil { + return nil, err + } + if err := x.ClientStream.CloseSend(); err != nil { + return nil, err + } + return x, nil +} + +// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. +type ChannelService_RunClient = grpc.ServerStreamingClient[RunResponse] + +// ChannelServiceServer is the server API for ChannelService service. +// All implementations must embed UnimplementedChannelServiceServer +// for forward compatibility. +// +// ChannelService is the surface a chat channel extension serves. The host +// launches one plugin process per enabled channel and serves the gateway +// ChatService back to it over the plugin broker, so a channel carries messages +// in and out through that service and never dials the gateway itself. +type ChannelServiceServer interface { + // Run starts the channel and keeps it running until the call is cancelled or + // the channel fails. The stream reports lifecycle: STARTING before each + // launch attempt, RUNNING once the channel can receive. A stream that ends + // with an error is a failed channel; the host restarts it. + Run(*RunRequest, grpc.ServerStreamingServer[RunResponse]) error + mustEmbedUnimplementedChannelServiceServer() +} + +// UnimplementedChannelServiceServer must be embedded to have +// forward compatible implementations. +// +// NOTE: this should be embedded by value instead of pointer to avoid a nil +// pointer dereference when methods are called. +type UnimplementedChannelServiceServer struct{} + +func (UnimplementedChannelServiceServer) Run(*RunRequest, grpc.ServerStreamingServer[RunResponse]) error { + return status.Error(codes.Unimplemented, "method Run not implemented") +} +func (UnimplementedChannelServiceServer) mustEmbedUnimplementedChannelServiceServer() {} +func (UnimplementedChannelServiceServer) testEmbeddedByValue() {} + +// UnsafeChannelServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to ChannelServiceServer will +// result in compilation errors. +type UnsafeChannelServiceServer interface { + mustEmbedUnimplementedChannelServiceServer() +} + +func RegisterChannelServiceServer(s grpc.ServiceRegistrar, srv ChannelServiceServer) { + // If the following call panics, it indicates UnimplementedChannelServiceServer was + // embedded by pointer and is nil. This will cause panics if an + // unimplemented method is ever invoked, so we test this at initialization + // time to prevent it from happening at runtime later due to I/O. + if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { + t.testEmbeddedByValue() + } + s.RegisterService(&ChannelService_ServiceDesc, srv) +} + +func _ChannelService_Run_Handler(srv interface{}, stream grpc.ServerStream) error { + m := new(RunRequest) + if err := stream.RecvMsg(m); err != nil { + return err + } + return srv.(ChannelServiceServer).Run(m, &grpc.GenericServerStream[RunRequest, RunResponse]{ServerStream: stream}) +} + +// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. +type ChannelService_RunServer = grpc.ServerStreamingServer[RunResponse] + +// ChannelService_ServiceDesc is the grpc.ServiceDesc for ChannelService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var ChannelService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "channel.v1.ChannelService", + HandlerType: (*ChannelServiceServer)(nil), + Methods: []grpc.MethodDesc{}, + Streams: []grpc.StreamDesc{ + { + StreamName: "Run", + Handler: _ChannelService_Run_Handler, + ServerStreams: true, + }, + }, + Metadata: "channel/v1/channel.proto", +} diff --git a/internal/domain/storepkg/descriptor.go b/internal/domain/storepkg/descriptor.go index 9f2b98bb5..641b421e7 100644 --- a/internal/domain/storepkg/descriptor.go +++ b/internal/domain/storepkg/descriptor.go @@ -54,10 +54,11 @@ type Extension struct { const ( SurfaceSecretEngine = "secretengine" SurfaceForge = "forge" + SurfaceChannel = "channel" ) func validSurface(surface string) bool { - return surface == SurfaceSecretEngine || surface == SurfaceForge + return surface == SurfaceSecretEngine || surface == SurfaceForge || surface == SurfaceChannel } // PackageRef identifies a required package or Kit. References are pinned to diff --git a/internal/infrastructure/channelext/channelext.go b/internal/infrastructure/channelext/channelext.go new file mode 100644 index 000000000..d487554ed --- /dev/null +++ b/internal/infrastructure/channelext/channelext.go @@ -0,0 +1,129 @@ +// Package channelext adapts a chat channel extension, served over the +// channel.v1 gRPC surface, to the messaging service's channels.Channel. The +// host serves the gateway ChatService back to the extension over the plugin +// broker, so the extension never holds a gateway address or credential. +package channelext + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + + goplugin "github.com/hashicorp/go-plugin" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + + channelv1 "github.com/samcharles93/archie-core/internal/contracts/channel/v1" + "github.com/samcharles93/archie-core/internal/domain/messaging" + "github.com/samcharles93/archie-core/internal/domain/taskactions" + "github.com/samcharles93/archie-core/internal/infrastructure/extension" + "github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc" + "github.com/samcharles93/archie-core/internal/taskstate" +) + +// Surface is the go-plugin surface name a channel binary serves. +const Surface = "channel" + +// Plugin is the go-plugin shim for the surface. Impl is set only by a channel +// binary serving it. +type Plugin struct { + goplugin.NetRPCUnsupportedPlugin + Impl channelv1.ChannelServiceServer +} + +func (p *Plugin) GRPCServer(_ *goplugin.GRPCBroker, s *grpc.Server) error { + channelv1.RegisterChannelServiceServer(s, p.Impl) + return nil +} + +type client struct { + rpc channelv1.ChannelServiceClient + broker *goplugin.GRPCBroker +} + +func (*Plugin) GRPCClient(_ context.Context, broker *goplugin.GRPCBroker, c *grpc.ClientConn) (any, error) { + return &client{rpc: channelv1.NewChannelServiceClient(c), broker: broker}, nil +} + +// Channel is one channel extension. It satisfies channels.Channel. +type Channel struct { + name string + host *extension.Host + spec extension.Spec + settings map[string]string + secrets map[string]string +} + +// New returns a Channel that launches spec on Start. secrets are the resolved +// values of the settings' _ref entries. +func New(host *extension.Host, name string, spec extension.Spec, settings, secrets map[string]string) *Channel { + return &Channel{name: name, host: host, spec: spec, settings: settings, secrets: secrets} +} + +func (c *Channel) Name() string { return c.name } + +// Start launches the extension and blocks until it stops. The process dies +// with ctx. +func (c *Channel) Start(ctx context.Context, chat messaging.ChatContract, lifecycle messaging.Lifecycle) error { + lifecycle.ReportStarting() + raw, err := c.host.Start(ctx, c.spec, Surface, &Plugin{}) + if err != nil { + return err + } + defer c.host.Stop(c.spec.Name) + ext, ok := raw.(*client) + if !ok { + return fmt.Errorf("extension %q: unexpected client type %T", c.spec.Name, raw) + } + + id := ext.broker.NextId() + go ext.broker.AcceptAndServe(id, func(opts []grpc.ServerOption) *grpc.Server { + server := grpc.NewServer(opts...) + gatewayrpc.RegisterServer(server, restricted{chat}) + return server + }) + + stream, err := ext.rpc.Run(ctx, &channelv1.RunRequest{Settings: c.settings, Secrets: c.secrets, ChatBrokerId: id}) + if err != nil { + return fmt.Errorf("extension %q: run: %w", c.spec.Name, err) + } + for { + event, err := stream.Recv() + switch { + case errors.Is(err, io.EOF): + return nil + case ctx.Err() != nil: + return nil + case err != nil: + return fmt.Errorf("extension %q: %w", c.spec.Name, err) + } + switch event.GetState() { + case channelv1.RunResponse_STATE_STARTING: + lifecycle.ReportStarting() + case channelv1.RunResponse_STATE_RUNNING: + lifecycle.ReportRunning() + } + } +} + +func (c *Channel) Stop(context.Context) error { + c.host.Stop(c.spec.Name) + return nil +} + +// ConfigSchema is empty: an extension validates its own settings when it runs. +func (*Channel) ConfigSchema() json.RawMessage { return json.RawMessage(`{}`) } + +func (*Channel) ValidateConfig(map[string]any) error { return nil } + +// restricted is the chat surface a channel extension may use. A channel +// carries conversations; mutating tasks as an operator across identities is +// the dashboard's authority, not a channel's. +type restricted struct{ messaging.ChatContract } + +func (restricted) ApplyOperatorTaskAction(context.Context, taskactions.Actor, int64, taskstate.Action, taskactions.ActionPayload) (messaging.TaskActionResult, error) { + return messaging.TaskActionResult{}, status.Error(codes.PermissionDenied, "a channel extension cannot act as an operator") +} diff --git a/internal/infrastructure/channelext/channelext_test.go b/internal/infrastructure/channelext/channelext_test.go new file mode 100644 index 000000000..d3dfee254 --- /dev/null +++ b/internal/infrastructure/channelext/channelext_test.go @@ -0,0 +1,55 @@ +package channelext + +import ( + "context" + "net" + "testing" + + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + + gatewayv1 "github.com/samcharles93/archie-core/internal/contracts/gateway/v1" + "github.com/samcharles93/archie-core/internal/domain/messaging" + "github.com/samcharles93/archie-core/internal/domain/taskactions" + "github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc" + "github.com/samcharles93/archie-core/internal/taskstate" +) + +type recordingChat struct { + messaging.ChatContract + operatorCalls int +} + +func (c *recordingChat) ApplyOperatorTaskAction(context.Context, taskactions.Actor, int64, taskstate.Action, taskactions.ActionPayload) (messaging.TaskActionResult, error) { + c.operatorCalls++ + return messaging.TaskActionResult{}, nil +} + +// A channel extension reaches the gateway through the chat surface the host +// serves it; operator-level task actions must not be part of that surface. +func TestChannelExtensionCannotActAsOperator(t *testing.T) { + chat := &recordingChat{} + server := grpc.NewServer() + gatewayrpc.RegisterServer(server, restricted{chat}) + lc := net.ListenConfig{} + ln, err := lc.Listen(t.Context(), "tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + go func() { _ = server.Serve(ln) }() + t.Cleanup(server.Stop) + + conn, err := grpc.NewClient(ln.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = conn.Close() }) + + _, err = gatewayv1.NewChatServiceClient(conn).ApplyOperatorTaskAction(t.Context(), &gatewayv1.ApplyOperatorTaskActionRequest{TaskId: 1, Action: "retry"}) + if err == nil { + t.Fatal("operator task action succeeded through a channel extension's chat surface") + } + if chat.operatorCalls != 0 { + t.Fatalf("the gateway was asked to apply an operator action %d times", chat.operatorCalls) + } +} diff --git a/proto/channel/v1/channel.proto b/proto/channel/v1/channel.proto new file mode 100644 index 000000000..5b43fa9e6 --- /dev/null +++ b/proto/channel/v1/channel.proto @@ -0,0 +1,36 @@ +syntax = "proto3"; + +package channel.v1; + +// ChannelService is the surface a chat channel extension serves. The host +// launches one plugin process per enabled channel and serves the gateway +// ChatService back to it over the plugin broker, so a channel carries messages +// in and out through that service and never dials the gateway itself. +service ChannelService { + // Run starts the channel and keeps it running until the call is cancelled or + // the channel fails. The stream reports lifecycle: STARTING before each + // launch attempt, RUNNING once the channel can receive. A stream that ends + // with an error is a failed channel; the host restarts it. + rpc Run(RunRequest) returns (stream RunResponse); +} + +message RunRequest { + // settings are the operator's values for this channel from extension + // settings. A key ending in _ref holds an "engine:key" secret reference the + // host resolves; the plugin receives the value in secrets, under the key + // without the suffix. + map settings = 1; + map secrets = 2; + // chat_broker_id is the plugin broker stream the host serves + // gateway.v1.ChatService on. + uint32 chat_broker_id = 3; +} + +message RunResponse { + enum State { + STATE_UNSPECIFIED = 0; + STATE_STARTING = 1; + STATE_RUNNING = 2; + } + State state = 1; +}