1package proto
 2
 3import (
 4	"encoding/json"
 5	"errors"
 6)
 7
 8type AgentEventType string
 9
10const (
11	AgentEventTypeError     AgentEventType = "error"
12	AgentEventTypeResponse  AgentEventType = "response"
13	AgentEventTypeSummarize AgentEventType = "summarize"
14)
15
16func (t AgentEventType) MarshalText() ([]byte, error) {
17	return []byte(t), nil
18}
19
20func (t *AgentEventType) UnmarshalText(text []byte) error {
21	*t = AgentEventType(text)
22	return nil
23}
24
25type AgentEvent struct {
26	Type    AgentEventType `json:"type"`
27	Message Message        `json:"message"`
28	Error   error          `json:"error,omitempty"`
29
30	// When summarizing
31	SessionID string `json:"session_id,omitempty"`
32	Progress  string `json:"progress,omitempty"`
33	Done      bool   `json:"done,omitempty"`
34}
35
36// MarshalJSON implements the [json.Marshaler] interface.
37func (e AgentEvent) MarshalJSON() ([]byte, error) {
38	type Alias AgentEvent
39	return json.Marshal(&struct {
40		Error string `json:"error,omitempty"`
41		Alias
42	}{
43		Error: func() string {
44			if e.Error != nil {
45				return e.Error.Error()
46			}
47			return ""
48		}(),
49		Alias: (Alias)(e),
50	})
51}
52
53// UnmarshalJSON implements the [json.Unmarshaler] interface.
54func (e *AgentEvent) UnmarshalJSON(data []byte) error {
55	type Alias AgentEvent
56	aux := &struct {
57		Error string `json:"error,omitempty"`
58		Alias
59	}{
60		Alias: (Alias)(*e),
61	}
62	if err := json.Unmarshal(data, &aux); err != nil {
63		return err
64	}
65	*e = AgentEvent(aux.Alias)
66	if aux.Error != "" {
67		e.Error = errors.New(aux.Error)
68	}
69	return nil
70}