fix: preserve bot reply anchors and status state
- commit
- 5e699ec40a50daa309fddc7b6395ea419b0a47d8
- author
- patriceckhart · · 2026-06-29 03:14 UTC
- parent
- 1ae0519
- signature
- unsigned
5 files · +36 -15
M packages/agent/botcmd.go +2 -1
| @@ -384,6 +384,7 @@ func botRun(rawTail []string, version string) error { | |||
| 384 | 384 | runner = bot.NewRunner(adapter, agent, bot.Config{ | |
| 385 | 385 | ZotHome: ZotHome(), | |
| 386 | 386 | Provider: resolved.Provider, | |
| 387 | + | Model: resolved.Model, | |
| 387 | 388 | AuthMethod: resolved.AuthMethod, | |
| 388 | 389 | CWD: args.CWD, | |
| 389 | 390 | RefreshCreds: func() error { | |
| @@ -397,7 +398,7 @@ func botRun(rawTail []string, version string) error { | |||
| 397 | 398 | } | |
| 398 | 399 | agent.Client = next.NewClient() | |
| 399 | 400 | agent.Model = next.Model | |
| 400 | - | runner.UpdateRuntimeConfig(next.Provider, next.AuthMethod, next.CWD) | |
| 401 | + | runner.UpdateRuntimeConfig(next.Provider, next.Model, next.AuthMethod, next.CWD) | |
| 401 | 402 | return nil | |
| 402 | 403 | }, | |
| 403 | 404 | }) | |
| @@ -384,6 +384,7 @@ func botRun(rawTail []string, version string) error { | |||||
| 384 | runner = bot.NewRunner(adapter, agent, bot.Config{ | 384 | runner = bot.NewRunner(adapter, agent, bot.Config{ | ||
| 385 | ZotHome: ZotHome(), | 385 | ZotHome: ZotHome(), | ||
| 386 | Provider: resolved.Provider, | 386 | Provider: resolved.Provider, | ||
| 387 | + | Model: resolved.Model, | |||
| 387 | AuthMethod: resolved.AuthMethod, | 388 | AuthMethod: resolved.AuthMethod, | ||
| 388 | CWD: args.CWD, | 389 | CWD: args.CWD, | ||
| 389 | RefreshCreds: func() error { | 390 | RefreshCreds: func() error { | ||
| @@ -397,7 +398,7 @@ func botRun(rawTail []string, version string) error { | |||||
| 397 | } | 398 | } | ||
| 398 | agent.Client = next.NewClient() | 399 | agent.Client = next.NewClient() | ||
| 399 | agent.Model = next.Model | 400 | agent.Model = next.Model | ||
| 400 | - | runner.UpdateRuntimeConfig(next.Provider, next.AuthMethod, next.CWD) | 401 | + | runner.UpdateRuntimeConfig(next.Provider, next.Model, next.AuthMethod, next.CWD) |
| 401 | return nil | 402 | return nil | ||
| 402 | }, | 403 | }, | ||
| 403 | }) | 404 | }) | ||
M packages/agent/modes/bot/adapter.go +7 -1
| @@ -28,6 +28,12 @@ const ( | |||
| 28 | 28 | CmdStop // cancel the active turn | |
| 29 | 29 | ) | |
| 30 | 30 | ||
| 31 | + | // SendOptions carries protocol-agnostic delivery hints for outbound messages. | |
| 32 | + | type SendOptions struct { | |
| 33 | + | // ReplyToMessageID is an optional adapter-owned message identifier to anchor replies. | |
| 34 | + | ReplyToMessageID string | |
| 35 | + | } | |
| 36 | + | ||
| 31 | 37 | // BotAdapter is the transport layer a concrete protocol must implement. | |
| 32 | 38 | // The Runner calls these methods; it never touches protocol types directly. | |
| 33 | 39 | type BotAdapter interface { | |
| @@ -39,7 +45,7 @@ type BotAdapter interface { | |||
| 39 | 45 | ) error | |
| 40 | 46 | ||
| 41 | 47 | // Send delivers a reply. The adapter chunks to protocol limits. | |
| 42 | - | Send(ctx context.Context, channelID, text string) error | |
| 48 | + | Send(ctx context.Context, channelID, text string, opts SendOptions) error | |
| 43 | 49 | ||
| 44 | 50 | // IndicateWorking fires a "typing…" signal; returns a stop func. | |
| 45 | 51 | // Return a no-op if the protocol doesn't support it. | |
| @@ -28,6 +28,12 @@ const ( | |||||
| 28 | CmdStop // cancel the active turn | 28 | CmdStop // cancel the active turn | ||
| 29 | ) | 29 | ) | ||
| 30 | 30 | ||||
| 31 | + | // SendOptions carries protocol-agnostic delivery hints for outbound messages. | |||
| 32 | + | type SendOptions struct { | |||
| 33 | + | // ReplyToMessageID is an optional adapter-owned message identifier to anchor replies. | |||
| 34 | + | ReplyToMessageID string | |||
| 35 | + | } | |||
| 36 | + | ||||
| 31 | // BotAdapter is the transport layer a concrete protocol must implement. | 37 | // BotAdapter is the transport layer a concrete protocol must implement. | ||
| 32 | // The Runner calls these methods; it never touches protocol types directly. | 38 | // The Runner calls these methods; it never touches protocol types directly. | ||
| 33 | type BotAdapter interface { | 39 | type BotAdapter interface { | ||
| @@ -39,7 +45,7 @@ type BotAdapter interface { | |||||
| 39 | ) error | 45 | ) error | ||
| 40 | 46 | ||||
| 41 | // Send delivers a reply. The adapter chunks to protocol limits. | 47 | // Send delivers a reply. The adapter chunks to protocol limits. | ||
| 42 | - | Send(ctx context.Context, channelID, text string) error | 48 | + | Send(ctx context.Context, channelID, text string, opts SendOptions) error |
| 43 | 49 | ||||
| 44 | // IndicateWorking fires a "typing…" signal; returns a stop func. | 50 | // IndicateWorking fires a "typing…" signal; returns a stop func. | ||
| 45 | // Return a no-op if the protocol doesn't support it. | 51 | // Return a no-op if the protocol doesn't support it. | ||
M packages/agent/modes/bot/runner.go +14 -10
| @@ -19,6 +19,7 @@ var stderr = func() io.Writer { return os.Stderr } | |||
| 19 | 19 | type Config struct { | |
| 20 | 20 | ZotHome string | |
| 21 | 21 | Provider string | |
| 22 | + | Model string | |
| 22 | 23 | AuthMethod string | |
| 23 | 24 | CWD string | |
| 24 | 25 | RefreshCreds func() error | |
| @@ -57,12 +58,13 @@ func NewRunner(adapter BotAdapter, agent *core.Agent, cfg Config) *Runner { | |||
| 57 | 58 | } | |
| 58 | 59 | } | |
| 59 | 60 | ||
| 60 | - | // UpdateRuntimeConfig updates provider/auth/cwd at runtime (e.g. after | |
| 61 | + | // UpdateRuntimeConfig updates provider/model/auth/cwd at runtime (e.g. after | |
| 61 | 62 | // credential refresh). This is thread-safe. | |
| 62 | - | func (r *Runner) UpdateRuntimeConfig(provider, authMethod, cwd string) { | |
| 63 | + | func (r *Runner) UpdateRuntimeConfig(provider, model, authMethod, cwd string) { | |
| 63 | 64 | r.mu.Lock() | |
| 64 | 65 | defer r.mu.Unlock() | |
| 65 | 66 | r.cfg.Provider = provider | |
| 67 | + | r.cfg.Model = model | |
| 66 | 68 | r.cfg.AuthMethod = authMethod | |
| 67 | 69 | r.cfg.CWD = cwd | |
| 68 | 70 | } | |
| @@ -105,9 +107,10 @@ func (r *Runner) handleCommand(cmd Command, msg InboundMessage) { | |||
| 105 | 107 | switch cmd { | |
| 106 | 108 | case CmdStart, CmdHelp: | |
| 107 | 109 | _ = r.adapter.Send(context.Background(), msg.ChannelID, | |
| 108 | - | "send me any message and i'll forward it to zot. attach an image and i'll pass it to the model. commands: /status, /stop, or plain stop.") | |
| 110 | + | "send me any message and i'll forward it to zot. attach an image and i'll pass it to the model. commands: /status, /stop, or plain stop.", | |
| 111 | + | SendOptions{ReplyToMessageID: msg.MessageID}) | |
| 109 | 112 | case CmdStatus: | |
| 110 | - | r.sendStatus(msg.ChannelID) | |
| 113 | + | r.sendStatus(msg.ChannelID, msg.MessageID) | |
| 111 | 114 | case CmdStop: | |
| 112 | 115 | r.cancelActiveTurn(msg.ChannelID, msg.MessageID) | |
| 113 | 116 | } | |
| @@ -199,7 +202,7 @@ func (r *Runner) runTurn(ctx context.Context, t queuedTurn) { | |||
| 199 | 202 | } | |
| 200 | 203 | ||
| 201 | 204 | // Adapter.Send is responsible for chunking to protocol limits. | |
| 202 | - | if err := r.adapter.Send(context.Background(), t.channelID, reply); err != nil { | |
| 205 | + | if err := r.adapter.Send(context.Background(), t.channelID, reply, SendOptions{}); err != nil { | |
| 203 | 206 | fmt.Fprintln(stderr(), "bot: send reply:", err) | |
| 204 | 207 | } | |
| 205 | 208 | } | |
| @@ -209,26 +212,27 @@ func (r *Runner) cancelActiveTurn(channelID, messageID string) { | |||
| 209 | 212 | r.mu.Lock() | |
| 210 | 213 | cancel := r.activeCtx | |
| 211 | 214 | r.mu.Unlock() | |
| 215 | + | opts := SendOptions{ReplyToMessageID: messageID} | |
| 212 | 216 | if cancel != nil { | |
| 213 | 217 | cancel() | |
| 214 | - | _ = r.adapter.Send(context.Background(), channelID, "cancelled the current turn.") | |
| 218 | + | _ = r.adapter.Send(context.Background(), channelID, "cancelled the current turn.", opts) | |
| 215 | 219 | } else { | |
| 216 | - | _ = r.adapter.Send(context.Background(), channelID, "nothing running.") | |
| 220 | + | _ = r.adapter.Send(context.Background(), channelID, "nothing running.", opts) | |
| 217 | 221 | } | |
| 218 | 222 | } | |
| 219 | 223 | ||
| 220 | 224 | // sendStatus describes agent state to the user. | |
| 221 | - | func (r *Runner) sendStatus(channelID string) { | |
| 225 | + | func (r *Runner) sendStatus(channelID, messageID string) { | |
| 222 | 226 | r.mu.Lock() | |
| 223 | 227 | busy := r.busy | |
| 224 | 228 | queued := len(r.queue) | |
| 225 | 229 | ctxUsed := r.lastCtxInput | |
| 226 | 230 | providerName := r.cfg.Provider | |
| 231 | + | model := r.cfg.Model | |
| 227 | 232 | authMethod := r.cfg.AuthMethod | |
| 228 | 233 | cwd := r.cfg.CWD | |
| 229 | 234 | r.mu.Unlock() | |
| 230 | 235 | ||
| 231 | - | model := r.agent.Model | |
| 232 | 236 | ctxMax := 0 | |
| 233 | 237 | if m, err := provider.FindModel(providerName, model); err == nil { | |
| 234 | 238 | ctxMax = m.ContextWindow | |
| @@ -250,5 +254,5 @@ func (r *Runner) sendStatus(channelID string) { | |||
| 250 | 254 | status += "\n" + extra | |
| 251 | 255 | } | |
| 252 | 256 | ||
| 253 | - | _ = r.adapter.Send(context.Background(), channelID, status) | |
| 257 | + | _ = r.adapter.Send(context.Background(), channelID, status, SendOptions{ReplyToMessageID: messageID}) | |
| 254 | 258 | } | |
| @@ -19,6 +19,7 @@ var stderr = func() io.Writer { return os.Stderr } | |||||
| 19 | type Config struct { | 19 | type Config struct { | ||
| 20 | ZotHome string | 20 | ZotHome string | ||
| 21 | Provider string | 21 | Provider string | ||
| 22 | + | Model string | |||
| 22 | AuthMethod string | 23 | AuthMethod string | ||
| 23 | CWD string | 24 | CWD string | ||
| 24 | RefreshCreds func() error | 25 | RefreshCreds func() error | ||
| @@ -57,12 +58,13 @@ func NewRunner(adapter BotAdapter, agent *core.Agent, cfg Config) *Runner { | |||||
| 57 | } | 58 | } | ||
| 58 | } | 59 | } | ||
| 59 | 60 | ||||
| 60 | - | // UpdateRuntimeConfig updates provider/auth/cwd at runtime (e.g. after | 61 | + | // UpdateRuntimeConfig updates provider/model/auth/cwd at runtime (e.g. after |
| 61 | // credential refresh). This is thread-safe. | 62 | // credential refresh). This is thread-safe. | ||
| 62 | - | func (r *Runner) UpdateRuntimeConfig(provider, authMethod, cwd string) { | 63 | + | func (r *Runner) UpdateRuntimeConfig(provider, model, authMethod, cwd string) { |
| 63 | r.mu.Lock() | 64 | r.mu.Lock() | ||
| 64 | defer r.mu.Unlock() | 65 | defer r.mu.Unlock() | ||
| 65 | r.cfg.Provider = provider | 66 | r.cfg.Provider = provider | ||
| 67 | + | r.cfg.Model = model | |||
| 66 | r.cfg.AuthMethod = authMethod | 68 | r.cfg.AuthMethod = authMethod | ||
| 67 | r.cfg.CWD = cwd | 69 | r.cfg.CWD = cwd | ||
| 68 | } | 70 | } | ||
| @@ -105,9 +107,10 @@ func (r *Runner) handleCommand(cmd Command, msg InboundMessage) { | |||||
| 105 | switch cmd { | 107 | switch cmd { | ||
| 106 | case CmdStart, CmdHelp: | 108 | case CmdStart, CmdHelp: | ||
| 107 | _ = r.adapter.Send(context.Background(), msg.ChannelID, | 109 | _ = r.adapter.Send(context.Background(), msg.ChannelID, | ||
| 108 | - | "send me any message and i'll forward it to zot. attach an image and i'll pass it to the model. commands: /status, /stop, or plain stop.") | 110 | + | "send me any message and i'll forward it to zot. attach an image and i'll pass it to the model. commands: /status, /stop, or plain stop.", |
| 111 | + | SendOptions{ReplyToMessageID: msg.MessageID}) | |||
| 109 | case CmdStatus: | 112 | case CmdStatus: | ||
| 110 | - | r.sendStatus(msg.ChannelID) | 113 | + | r.sendStatus(msg.ChannelID, msg.MessageID) |
| 111 | case CmdStop: | 114 | case CmdStop: | ||
| 112 | r.cancelActiveTurn(msg.ChannelID, msg.MessageID) | 115 | r.cancelActiveTurn(msg.ChannelID, msg.MessageID) | ||
| 113 | } | 116 | } | ||
| @@ -199,7 +202,7 @@ func (r *Runner) runTurn(ctx context.Context, t queuedTurn) { | |||||
| 199 | } | 202 | } | ||
| 200 | 203 | ||||
| 201 | // Adapter.Send is responsible for chunking to protocol limits. | 204 | // Adapter.Send is responsible for chunking to protocol limits. | ||
| 202 | - | if err := r.adapter.Send(context.Background(), t.channelID, reply); err != nil { | 205 | + | if err := r.adapter.Send(context.Background(), t.channelID, reply, SendOptions{}); err != nil { |
| 203 | fmt.Fprintln(stderr(), "bot: send reply:", err) | 206 | fmt.Fprintln(stderr(), "bot: send reply:", err) | ||
| 204 | } | 207 | } | ||
| 205 | } | 208 | } | ||
| @@ -209,26 +212,27 @@ func (r *Runner) cancelActiveTurn(channelID, messageID string) { | |||||
| 209 | r.mu.Lock() | 212 | r.mu.Lock() | ||
| 210 | cancel := r.activeCtx | 213 | cancel := r.activeCtx | ||
| 211 | r.mu.Unlock() | 214 | r.mu.Unlock() | ||
| 215 | + | opts := SendOptions{ReplyToMessageID: messageID} | |||
| 212 | if cancel != nil { | 216 | if cancel != nil { | ||
| 213 | cancel() | 217 | cancel() | ||
| 214 | - | _ = r.adapter.Send(context.Background(), channelID, "cancelled the current turn.") | 218 | + | _ = r.adapter.Send(context.Background(), channelID, "cancelled the current turn.", opts) |
| 215 | } else { | 219 | } else { | ||
| 216 | - | _ = r.adapter.Send(context.Background(), channelID, "nothing running.") | 220 | + | _ = r.adapter.Send(context.Background(), channelID, "nothing running.", opts) |
| 217 | } | 221 | } | ||
| 218 | } | 222 | } | ||
| 219 | 223 | ||||
| 220 | // sendStatus describes agent state to the user. | 224 | // sendStatus describes agent state to the user. | ||
| 221 | - | func (r *Runner) sendStatus(channelID string) { | 225 | + | func (r *Runner) sendStatus(channelID, messageID string) { |
| 222 | r.mu.Lock() | 226 | r.mu.Lock() | ||
| 223 | busy := r.busy | 227 | busy := r.busy | ||
| 224 | queued := len(r.queue) | 228 | queued := len(r.queue) | ||
| 225 | ctxUsed := r.lastCtxInput | 229 | ctxUsed := r.lastCtxInput | ||
| 226 | providerName := r.cfg.Provider | 230 | providerName := r.cfg.Provider | ||
| 231 | + | model := r.cfg.Model | |||
| 227 | authMethod := r.cfg.AuthMethod | 232 | authMethod := r.cfg.AuthMethod | ||
| 228 | cwd := r.cfg.CWD | 233 | cwd := r.cfg.CWD | ||
| 229 | r.mu.Unlock() | 234 | r.mu.Unlock() | ||
| 230 | 235 | ||||
| 231 | - | model := r.agent.Model | |||
| 232 | ctxMax := 0 | 236 | ctxMax := 0 | ||
| 233 | if m, err := provider.FindModel(providerName, model); err == nil { | 237 | if m, err := provider.FindModel(providerName, model); err == nil { | ||
| 234 | ctxMax = m.ContextWindow | 238 | ctxMax = m.ContextWindow | ||
| @@ -250,5 +254,5 @@ func (r *Runner) sendStatus(channelID string) { | |||||
| 250 | status += "\n" + extra | 254 | status += "\n" + extra | ||
| 251 | } | 255 | } | ||
| 252 | 256 | ||||
| 253 | - | _ = r.adapter.Send(context.Background(), channelID, status) | 257 | + | _ = r.adapter.Send(context.Background(), channelID, status, SendOptions{ReplyToMessageID: messageID}) |
| 254 | } | 258 | } | ||
M packages/agent/modes/bot/runner_test.go +3 -1
| @@ -15,7 +15,9 @@ type testAdapter struct{} | |||
| 15 | 15 | func (testAdapter) Run(context.Context, func(InboundMessage), func(Command, InboundMessage)) error { | |
| 16 | 16 | return nil | |
| 17 | 17 | } | |
| 18 | - | func (testAdapter) Send(context.Context, string, string) error { return nil } | |
| 18 | + | func (testAdapter) Send(context.Context, string, string, SendOptions) error { | |
| 19 | + | return nil | |
| 20 | + | } | |
| 19 | 21 | func (testAdapter) IndicateWorking(context.Context, string) func() { return func() {} } | |
| 20 | 22 | func (testAdapter) StatusText() string { return "" } | |
| 21 | 23 | ||
| @@ -15,7 +15,9 @@ type testAdapter struct{} | |||||
| 15 | func (testAdapter) Run(context.Context, func(InboundMessage), func(Command, InboundMessage)) error { | 15 | func (testAdapter) Run(context.Context, func(InboundMessage), func(Command, InboundMessage)) error { | ||
| 16 | return nil | 16 | return nil | ||
| 17 | } | 17 | } | ||
| 18 | - | func (testAdapter) Send(context.Context, string, string) error { return nil } | 18 | + | func (testAdapter) Send(context.Context, string, string, SendOptions) error { |
| 19 | + | return nil | |||
| 20 | + | } | |||
| 19 | func (testAdapter) IndicateWorking(context.Context, string) func() { return func() {} } | 21 | func (testAdapter) IndicateWorking(context.Context, string) func() { return func() {} } | ||
| 20 | func (testAdapter) StatusText() string { return "" } | 22 | func (testAdapter) StatusText() string { return "" } | ||
| 21 | 23 | ||||
M packages/agent/modes/telegram/adapter.go +10 -2
| @@ -191,15 +191,23 @@ func (a *Adapter) handleUpdate(ctx context.Context, u Update, | |||
| 191 | 191 | ||
| 192 | 192 | // Send delivers a reply to a Telegram chat. channelID is parsed back | |
| 193 | 193 | // to int64. Messages are chunked to 4000 runes (Telegram limit 4096). | |
| 194 | - | func (a *Adapter) Send(ctx context.Context, channelID, text string) error { | |
| 194 | + | func (a *Adapter) Send(ctx context.Context, channelID, text string, opts bot.SendOptions) error { | |
| 195 | 195 | chatID, err := strconv.ParseInt(channelID, 10, 64) | |
| 196 | 196 | if err != nil { | |
| 197 | 197 | return fmt.Errorf("invalid channelID %q: %w", channelID, err) | |
| 198 | 198 | } | |
| 199 | + | replyTo := 0 | |
| 200 | + | if opts.ReplyToMessageID != "" { | |
| 201 | + | if n, err := strconv.Atoi(opts.ReplyToMessageID); err == nil { | |
| 202 | + | replyTo = n | |
| 203 | + | } | |
| 204 | + | } | |
| 199 | 205 | for _, chunk := range chunkMessage(text, 4000) { | |
| 200 | - | if err := a.Client.SendMessage(ctx, chatID, chunk, 0); err != nil { | |
| 206 | + | if err := a.Client.SendMessage(ctx, chatID, chunk, replyTo); err != nil { | |
| 201 | 207 | return err | |
| 202 | 208 | } | |
| 209 | + | // Only the first chunk should be threaded under the original message. | |
| 210 | + | replyTo = 0 | |
| 203 | 211 | } | |
| 204 | 212 | return nil | |
| 205 | 213 | } | |
| @@ -191,15 +191,23 @@ func (a *Adapter) handleUpdate(ctx context.Context, u Update, | |||||
| 191 | 191 | ||||
| 192 | // Send delivers a reply to a Telegram chat. channelID is parsed back | 192 | // Send delivers a reply to a Telegram chat. channelID is parsed back | ||
| 193 | // to int64. Messages are chunked to 4000 runes (Telegram limit 4096). | 193 | // to int64. Messages are chunked to 4000 runes (Telegram limit 4096). | ||
| 194 | - | func (a *Adapter) Send(ctx context.Context, channelID, text string) error { | 194 | + | func (a *Adapter) Send(ctx context.Context, channelID, text string, opts bot.SendOptions) error { |
| 195 | chatID, err := strconv.ParseInt(channelID, 10, 64) | 195 | chatID, err := strconv.ParseInt(channelID, 10, 64) | ||
| 196 | if err != nil { | 196 | if err != nil { | ||
| 197 | return fmt.Errorf("invalid channelID %q: %w", channelID, err) | 197 | return fmt.Errorf("invalid channelID %q: %w", channelID, err) | ||
| 198 | } | 198 | } | ||
| 199 | + | replyTo := 0 | |||
| 200 | + | if opts.ReplyToMessageID != "" { | |||
| 201 | + | if n, err := strconv.Atoi(opts.ReplyToMessageID); err == nil { | |||
| 202 | + | replyTo = n | |||
| 203 | + | } | |||
| 204 | + | } | |||
| 199 | for _, chunk := range chunkMessage(text, 4000) { | 205 | for _, chunk := range chunkMessage(text, 4000) { | ||
| 200 | - | if err := a.Client.SendMessage(ctx, chatID, chunk, 0); err != nil { | 206 | + | if err := a.Client.SendMessage(ctx, chatID, chunk, replyTo); err != nil { |
| 201 | return err | 207 | return err | ||
| 202 | } | 208 | } | ||
| 209 | + | // Only the first chunk should be threaded under the original message. | |||
| 210 | + | replyTo = 0 | |||
| 203 | } | 211 | } | ||
| 204 | return nil | 212 | return nil | ||
| 205 | } | 213 | } | ||