events.go

 1package pubsub
 2
 3import (
 4	"context"
 5	"encoding/json"
 6)
 7
 8const (
 9	CreatedEvent EventType = "created"
10	UpdatedEvent EventType = "updated"
11	DeletedEvent EventType = "deleted"
12)
13
14// PayloadType identifies the type of event payload for discriminated
15// deserialization over JSON.
16type PayloadType = string
17
18const (
19	PayloadTypeLSPEvent               PayloadType = "lsp_event"
20	PayloadTypeMCPEvent               PayloadType = "mcp_event"
21	PayloadTypePermissionRequest      PayloadType = "permission_request"
22	PayloadTypePermissionNotification PayloadType = "permission_notification"
23	PayloadTypeMessage                PayloadType = "message"
24	PayloadTypeSession                PayloadType = "session"
25	PayloadTypeFile                   PayloadType = "file"
26	PayloadTypeAgentEvent             PayloadType = "agent_event"
27)
28
29// Payload wraps a discriminated JSON payload with a type tag.
30type Payload struct {
31	Type    PayloadType     `json:"type"`
32	Payload json.RawMessage `json:"payload"`
33}
34
35// Subscriber can subscribe to events of type T.
36type Subscriber[T any] interface {
37	Subscribe(context.Context) <-chan Event[T]
38}
39
40type (
41	// EventType identifies the type of event.
42	EventType string
43
44	// Event represents an event in the lifecycle of a resource.
45	Event[T any] struct {
46		Type    EventType `json:"type"`
47		Payload T         `json:"payload"`
48	}
49
50	// Publisher can publish events of type T.
51	Publisher[T any] interface {
52		Publish(EventType, T)
53	}
54)