}
// Event tells the clients what the agent does as it happens. Kind is the kind
-// of a logged message (user, reply, tool, echo), or one of busy, idle, and
-// error. ID is the ID of a logged message.
+// of a logged message (user, reply, tool, echo), or one of ready, busy, idle,
+// and error. ID is the ID of a logged message. Source names the client that
+// sent a user message, such as terminal or telegram, so that each client can
+// show the messages of the others.
type Event struct {
- Kind string `json:"kind"`
- ID int `json:"id,omitempty"`
- Text string `json:"text,omitempty"`
+ Kind string `json:"kind"`
+ ID int `json:"id,omitempty"`
+ Text string `json:"text,omitempty"`
+ Source string `json:"source,omitempty"`
}
// loop runs the turns of one agent. A user message goes to the log when it
return l, nil
}
-// Submit logs a user message and wakes the loop. The message joins the next
-// request: the first one of a new turn, or the next one of a running turn.
-func (l *loop) Submit(text string) (Message, error) {
- message, err := l.Append("user", text)
+// Submit logs a user message from the client source and wakes the loop. The
+// message joins the next request: the first one of a new turn, or the next one
+// of a running turn.
+func (l *loop) Submit(source, text string) (Message, error) {
+ message, err := l.record("user", source, text)
if err != nil {
return Message{}, err
}
return true
}
+// Busy tells if a turn runs.
+func (l *loop) Busy() bool {
+ l.mutex.Lock()
+ defer l.mutex.Unlock()
+
+ return l.cancel != nil
+}
+
// Append logs a message and tells the clients. The toolbox logs its calls and
// results through it.
func (l *loop) Append(kind, text string) (Message, error) {
+ return l.record(kind, "", text)
+}
+
+// record logs a message and tells the clients, with the client source of a user
+// message.
+func (l *loop) record(kind, source, text string) (Message, error) {
message, err := l.memory.Append(kind, text)
if err != nil {
return Message{}, err
}
l.notify(Event{
- Kind: kind,
- ID: message.ID,
- Text: text,
+ Kind: kind,
+ ID: message.ID,
+ Text: text,
+ Source: source,
})
return message, nil