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 Progress string `json:"progress,omitempty"`
37 Done bool `json:"done,omitempty"`
38}
39
40// MarshalJSON implements the [json.Marshaler] interface.
41func (e AgentEvent) MarshalJSON() ([]byte, error) {
42 type Alias AgentEvent
43 return json.Marshal(&struct {
44 Error string `json:"error,omitempty"`
45 Alias
46 }{
47 Error: func() string {
48 if e.Error != nil {
49 return e.Error.Error()
50 }
51 return ""
52 }(),
53 Alias: (Alias)(e),
54 })
55}
56
57// UnmarshalJSON implements the [json.Unmarshaler] interface.
58func (e *AgentEvent) UnmarshalJSON(data []byte) error {
59 type Alias AgentEvent
60 aux := &struct {
61 Error string `json:"error,omitempty"`
62 Alias
63 }{
64 Alias: (Alias)(*e),
65 }
66 if err := json.Unmarshal(data, &aux); err != nil {
67 return err
68 }
69 *e = AgentEvent(aux.Alias)
70 if aux.Error != "" {
71 e.Error = errors.New(aux.Error)
72 }
73 return nil
74}