X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/f59bb67ae1bc2c899ef4f6be9ce2ff6296c7ffcc..97729fc59a2fbd73e3f8a424cae4c825f5ba885b:/turn.go?ds=sidebyside diff --git a/turn.go b/turn.go index 75cbfb6..0b2b29e 100644 --- a/turn.go +++ b/turn.go @@ -17,12 +17,15 @@ type modelClient interface { } // 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 @@ -98,10 +101,11 @@ func newLoop(agent *Agent, memory *Memory, client modelClient, clock func() time 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 } @@ -133,18 +137,33 @@ func (l *loop) Interrupt() bool { 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