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	PayloadTypeConfigChanged          PayloadType = "config_changed"
28	PayloadTypeSkillsEvent            PayloadType = "skills_event"
29)
30
31// Payload wraps a discriminated JSON payload with a type tag.
32type Payload struct {
33	Type    PayloadType     `json:"type"`
34	Payload json.RawMessage `json:"payload"`
35}
36
37// Subscriber can subscribe to events of type T.
38type Subscriber[T any] interface {
39	Subscribe(context.Context) <-chan Event[T]
40}
41
42type (
43	// EventType identifies the type of event.
44	EventType string
45
46	// Event represents an event in the lifecycle of a resource.
47	Event[T any] struct {
48		Type    EventType `json:"type"`
49		Payload T         `json:"payload"`
50	}
51
52	// Publisher can publish events of type T.
53	Publisher[T any] interface {
54		Publish(EventType, T)
55	}
56)