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