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