mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-06 12:24:44 +00:00
Part 2 of #5, on top of the bridge core. Access: chat_bridges.allowed_users (migration 0016). A private chat is served when its chat ID is allowed, as before. In a group the sender must also be an allowed user, and a group with no allowed users serves nobody, so joining an allowed group grants nothing by itself. A refused member gets one refusal naming their user ID. Commands: /tasks, /task [id], /pause [id], /resume [id], /findings [id]. Each chat selects its task with /task; the selection is stored in chat_bridge_chats and cleared when the task is deleted. /pause and /resume go through applyTaskControl, the same path as the web UI. Plain text goes to the selected task's main agent through startTaskChat, which the web chat handler now also uses, so the bot and the web chat share one conversation. The answer is relayed when the turn ends, through chatbridge.SafeText: code blocks dropped, the task's stored credentials and common secret shapes masked, cut to 1500 characters, with a link back. An answer is dropped if its chat is no longer allowed. Co-authored-by: krad <222954346+dar1k0@users.noreply.github.com>
412 lines
12 KiB
Go
412 lines
12 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"slices"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/Autumn-27/artex/chatbridge"
|
|
"github.com/Autumn-27/artex/db"
|
|
)
|
|
|
|
// The inbound chat bridge (issue #5): one long-poll loop per registered
|
|
// transport kind, started by server.New. The loop re-reads its row on every
|
|
// round, so saving the configuration (a new token, a new allowlist, enabling
|
|
// or disabling) takes effect without a restart; Reload interrupts a poll that
|
|
// is waiting so the change applies at once instead of after the poll window.
|
|
//
|
|
// Order of work for a batch: commit the cursor, then act. If ARTEX crashes in
|
|
// between, the updates of that batch are lost rather than executed twice. For
|
|
// an operator console that is the safe side: a lost "/pause" is retyped, a
|
|
// replayed one acts on a task the operator may have resumed since.
|
|
|
|
const (
|
|
// chatBridgeIdleWait is how long a disabled or unconfigured bridge sleeps
|
|
// before it looks at its row again. Saving the row wakes it earlier.
|
|
chatBridgeIdleWait = 30 * time.Second
|
|
chatBridgeMinRetry = 2 * time.Second
|
|
chatBridgeMaxRetry = time.Minute
|
|
chatBridgeSendWait = 15 * time.Second
|
|
// chatBridgeEmptyPollWait is the pause after a poll that returned nothing.
|
|
chatBridgeEmptyPollWait = time.Second
|
|
)
|
|
|
|
// chatBridgeStatus is the live state shown in the UI ("bot online").
|
|
type chatBridgeStatus struct {
|
|
// State is off (disabled or not configured), connecting, online or error.
|
|
State string `json:"state"`
|
|
Bot *chatbridge.Identity `json:"bot,omitempty"`
|
|
LastError string `json:"last_error,omitempty"`
|
|
LastErrorAt *time.Time `json:"last_error_at,omitempty"`
|
|
LastPollAt *time.Time `json:"last_poll_at,omitempty"`
|
|
LastUpdateAt *time.Time `json:"last_update_at,omitempty"`
|
|
}
|
|
|
|
type chatBridgeState struct {
|
|
wake chan struct{}
|
|
cancel context.CancelFunc // cancels the poll in flight; nil when none
|
|
status chatBridgeStatus
|
|
router *chatbridge.Router
|
|
sig string // hash of the config the router and identity were built for
|
|
retry time.Duration
|
|
}
|
|
|
|
type chatBridgeRunner struct {
|
|
pg *db.DB
|
|
// transport resolves a kind; tests substitute a fake.
|
|
transport func(kind string) (chatbridge.Transport, bool)
|
|
// handlers builds the command table of a bridge and the handler of plain
|
|
// text. server.New installs the task commands; without them the bridge
|
|
// knows only /help and /whoami.
|
|
handlers func(kind string) ([]chatbridge.Command, chatbridge.CommandFunc)
|
|
|
|
mu sync.Mutex
|
|
state map[string]*chatBridgeState
|
|
}
|
|
|
|
func newChatBridgeRunner(pg *db.DB) *chatBridgeRunner {
|
|
return &chatBridgeRunner{
|
|
pg: pg,
|
|
transport: chatbridge.Get,
|
|
handlers: defaultChatBridgeHandlers,
|
|
state: map[string]*chatBridgeState{},
|
|
}
|
|
}
|
|
|
|
// defaultChatBridgeHandlers is the handler table of a runner without a
|
|
// server: the commands that need no task and no text handler.
|
|
func defaultChatBridgeHandlers(kind string) ([]chatbridge.Command, chatbridge.CommandFunc) {
|
|
return defaultChatBridgeCommands(kind), nil
|
|
}
|
|
|
|
// defaultChatBridgeCommands are the commands that need no task. /help is
|
|
// built into the router.
|
|
func defaultChatBridgeCommands(string) []chatbridge.Command {
|
|
return []chatbridge.Command{{
|
|
Name: "whoami",
|
|
Summary: "show this chat's ID and your user ID",
|
|
Run: func(_ context.Context, req chatbridge.Request) string {
|
|
return fmt.Sprintf("Chat ID: %s\nYour user ID: %s", req.ChatID, req.UserID)
|
|
},
|
|
}}
|
|
}
|
|
|
|
func (r *chatBridgeRunner) get(kind string) *chatBridgeState {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
st := r.state[kind]
|
|
if st == nil {
|
|
st = &chatBridgeState{wake: make(chan struct{}, 1), status: chatBridgeStatus{State: "off"}}
|
|
r.state[kind] = st
|
|
}
|
|
return st
|
|
}
|
|
|
|
// Status returns a copy of a bridge's live state.
|
|
func (r *chatBridgeRunner) Status(kind string) chatBridgeStatus {
|
|
if r == nil {
|
|
return chatBridgeStatus{State: "off"}
|
|
}
|
|
st := r.get(kind)
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
out := st.status
|
|
if out.Bot != nil {
|
|
b := *out.Bot
|
|
out.Bot = &b
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Reload makes the loop of kind re-read its configuration now, interrupting
|
|
// a poll in flight.
|
|
func (r *chatBridgeRunner) Reload(kind string) {
|
|
if r == nil {
|
|
return
|
|
}
|
|
st := r.get(kind)
|
|
r.mu.Lock()
|
|
if st.cancel != nil {
|
|
st.cancel()
|
|
}
|
|
st.sig = "" // rebuild identity and router from the saved row
|
|
st.retry = 0
|
|
r.mu.Unlock()
|
|
select {
|
|
case st.wake <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// Run starts one loop per registered transport and returns when ctx ends.
|
|
func (r *chatBridgeRunner) Run(ctx context.Context) {
|
|
if r == nil || r.pg == nil {
|
|
return
|
|
}
|
|
var wg sync.WaitGroup
|
|
for _, kind := range chatbridge.Kinds() {
|
|
wg.Add(1)
|
|
go func(kind string) {
|
|
defer wg.Done()
|
|
r.loop(ctx, kind)
|
|
}(kind)
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
func (r *chatBridgeRunner) loop(ctx context.Context, kind string) {
|
|
st := r.get(kind)
|
|
for ctx.Err() == nil {
|
|
wait := r.step(ctx, kind)
|
|
if wait <= 0 {
|
|
continue
|
|
}
|
|
t := time.NewTimer(wait)
|
|
select {
|
|
case <-ctx.Done():
|
|
t.Stop()
|
|
return
|
|
case <-st.wake:
|
|
t.Stop()
|
|
case <-t.C:
|
|
}
|
|
}
|
|
}
|
|
|
|
// step runs one round for kind: load the row, make sure the bot identity is
|
|
// known, poll once and act on the batch. It returns how long to wait before
|
|
// the next round (0 = poll again at once).
|
|
func (r *chatBridgeRunner) step(ctx context.Context, kind string) time.Duration {
|
|
tag := chatBridgeLogTag(kind)
|
|
tr, ok := r.transport(kind)
|
|
if !ok {
|
|
return chatBridgeIdleWait
|
|
}
|
|
b, err := r.pg.ChatBridgeByKind(ctx, kind)
|
|
if errors.Is(err, db.ErrChatBridgeNotFound) || (err == nil && !b.Enabled) {
|
|
r.setOff(kind)
|
|
return chatBridgeIdleWait
|
|
}
|
|
if err != nil {
|
|
return r.fail(kind, fmt.Errorf("load configuration: %w", err))
|
|
}
|
|
var cfg map[string]any
|
|
if err := json.Unmarshal(b.Config, &cfg); err != nil {
|
|
return r.fail(kind, fmt.Errorf("configuration is not valid JSON: %w", err))
|
|
}
|
|
if err := tr.Validate(cfg); err != nil {
|
|
return r.fail(kind, err)
|
|
}
|
|
|
|
st := r.get(kind)
|
|
sig := configSig(b.Config)
|
|
r.mu.Lock()
|
|
known := st.sig == sig && st.router != nil
|
|
router := st.router
|
|
prevBot := st.status.Bot
|
|
r.mu.Unlock()
|
|
if !known {
|
|
r.setState(kind, "connecting")
|
|
me, err := tr.Identity(ctx, cfg)
|
|
if err != nil {
|
|
return r.fail(kind, err)
|
|
}
|
|
prev := router
|
|
cmds, text := r.handlers(kind)
|
|
router = &chatbridge.Router{BotUserName: me.UserName, Commands: cmds, Text: text}
|
|
if prevBot != nil && prevBot.ID == me.ID {
|
|
// Same bot, new settings: chats it already refused stay refused.
|
|
router.KeepRefusals(prev)
|
|
}
|
|
r.mu.Lock()
|
|
st.sig, st.router = sig, router
|
|
st.status.Bot = &me
|
|
r.mu.Unlock()
|
|
log.Printf("%s online as @%s, %d allowed chat(s), %d allowed group user(s)", tag, me.UserName, len(b.AllowedChats), len(b.AllowedUsers))
|
|
}
|
|
|
|
pollCtx, cancel := context.WithCancel(ctx)
|
|
r.mu.Lock()
|
|
st.cancel = cancel
|
|
r.mu.Unlock()
|
|
updates, err := tr.Poll(pollCtx, cfg, b.PollOffset)
|
|
r.mu.Lock()
|
|
st.cancel = nil
|
|
r.mu.Unlock()
|
|
cancel()
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
return 0
|
|
}
|
|
if errors.Is(err, context.Canceled) {
|
|
return 0 // Reload interrupted the poll: re-read the row at once
|
|
}
|
|
var ra *chatbridge.RetryAfterError
|
|
if errors.As(err, &ra) {
|
|
r.fail(kind, err)
|
|
// The wait comes from the server. Cap it so a broken or hostile
|
|
// API address cannot park the bridge for hours; polling early
|
|
// only earns another 429 with a fresh retry_after.
|
|
return min(max(ra.After, chatBridgeMinRetry), chatBridgeMaxRetry)
|
|
}
|
|
return r.fail(kind, err)
|
|
}
|
|
now := time.Now()
|
|
r.mu.Lock()
|
|
st.status.State, st.status.LastPollAt, st.retry = "online", &now, 0
|
|
r.mu.Unlock()
|
|
if len(updates) == 0 {
|
|
// A real long poll returns empty only after its window, so this
|
|
// pause costs nothing there; it keeps a self-hosted Bot API that
|
|
// ignores the timeout from spinning the loop.
|
|
return chatBridgeEmptyPollWait
|
|
}
|
|
|
|
next := b.PollOffset
|
|
for _, u := range updates {
|
|
if u.ID >= next {
|
|
next = u.ID + 1
|
|
}
|
|
}
|
|
wrote, err := r.pg.SetChatBridgeOffset(ctx, kind, b.ConfigVersion, next)
|
|
if err != nil {
|
|
// Acting without a committed cursor would repeat the batch on the
|
|
// next poll. Skip it and try again.
|
|
return r.fail(kind, fmt.Errorf("save update cursor: %w", err))
|
|
}
|
|
if !wrote {
|
|
// The configuration was saved while this poll ran (a new token, a
|
|
// new allowlist, or the bridge was disabled). The batch belongs to
|
|
// the old configuration: drop it and start over from the saved row.
|
|
log.Printf("%s configuration changed during a poll, %d update(s) dropped", tag, len(updates))
|
|
return 0
|
|
}
|
|
r.mu.Lock()
|
|
st.status.LastUpdateAt = &now
|
|
r.mu.Unlock()
|
|
|
|
for _, u := range updates {
|
|
decision, reply := router.Route(ctx, kind, chatbridge.Access{Chats: b.AllowedChats, Users: b.AllowedUsers}, u)
|
|
switch decision {
|
|
case chatbridge.DecisionRefused:
|
|
log.Printf("%s refused chat %s (%s, user %s @%s): not allowed", tag, u.ChatID, u.ChatType, u.UserID, u.UserName)
|
|
case chatbridge.DecisionStale:
|
|
log.Printf("%s dropped a message from chat %s sent %s ago (older than %s)", tag, u.ChatID, now.Sub(u.Date).Round(time.Second), chatbridge.MaxUpdateAge)
|
|
case chatbridge.DecisionHandled:
|
|
// The message text is not logged: it can carry anything the
|
|
// operator typed, including target details.
|
|
log.Printf("%s handled a message in chat %s from user %s", tag, u.ChatID, u.UserID)
|
|
}
|
|
if reply == "" {
|
|
continue
|
|
}
|
|
sendCtx, cancelSend := context.WithTimeout(ctx, chatBridgeSendWait)
|
|
if err := tr.Send(sendCtx, cfg, u.ChatID, reply); err != nil {
|
|
log.Printf("%s reply to chat %s failed: %v", tag, u.ChatID, err)
|
|
}
|
|
cancelSend()
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// errChatBridgeGone reports that an answer has nowhere to go any more.
|
|
var errChatBridgeGone = errors.New("the chat is no longer allowed on an enabled bridge")
|
|
|
|
// Deliver sends text to a chat outside the poll loop, for an answer that
|
|
// arrives after its command returned (a main-agent turn). It re-reads the
|
|
// bridge first: if the bridge was disabled or the chat removed from the
|
|
// allowlist in the meantime, the answer is dropped with errChatBridgeGone.
|
|
func (r *chatBridgeRunner) Deliver(ctx context.Context, kind, chatID, text string) error {
|
|
if r == nil || r.pg == nil {
|
|
return errChatBridgeGone
|
|
}
|
|
tr, ok := r.transport(kind)
|
|
if !ok {
|
|
return errChatBridgeGone
|
|
}
|
|
b, err := r.pg.ChatBridgeByKind(ctx, kind)
|
|
if errors.Is(err, db.ErrChatBridgeNotFound) {
|
|
return errChatBridgeGone
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !b.Enabled || !slices.Contains(b.AllowedChats, chatID) {
|
|
return errChatBridgeGone
|
|
}
|
|
var cfg map[string]any
|
|
if err := json.Unmarshal(b.Config, &cfg); err != nil {
|
|
return fmt.Errorf("configuration is not valid JSON: %w", err)
|
|
}
|
|
if err := tr.Validate(cfg); err != nil {
|
|
return err
|
|
}
|
|
sendCtx, cancel := context.WithTimeout(ctx, chatBridgeSendWait)
|
|
defer cancel()
|
|
return tr.Send(sendCtx, cfg, chatID, text)
|
|
}
|
|
|
|
func (r *chatBridgeRunner) setOff(kind string) {
|
|
st := r.get(kind)
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
st.status = chatBridgeStatus{State: "off"}
|
|
st.sig, st.router, st.retry = "", nil, 0
|
|
}
|
|
|
|
func (r *chatBridgeRunner) setState(kind, state string) {
|
|
st := r.get(kind)
|
|
r.mu.Lock()
|
|
st.status.State = state
|
|
r.mu.Unlock()
|
|
}
|
|
|
|
// fail records an error and returns the backoff before the next round:
|
|
// doubling from chatBridgeMinRetry up to chatBridgeMaxRetry, reset by the
|
|
// next successful poll or a Reload.
|
|
func (r *chatBridgeRunner) fail(kind string, err error) time.Duration {
|
|
st := r.get(kind)
|
|
now := time.Now()
|
|
r.mu.Lock()
|
|
first := st.status.LastError != err.Error()
|
|
st.status.State = "error"
|
|
st.status.LastError = err.Error()
|
|
st.status.LastErrorAt = &now
|
|
if st.retry == 0 {
|
|
st.retry = chatBridgeMinRetry
|
|
} else if st.retry < chatBridgeMaxRetry {
|
|
st.retry *= 2
|
|
if st.retry > chatBridgeMaxRetry {
|
|
st.retry = chatBridgeMaxRetry
|
|
}
|
|
}
|
|
wait := st.retry
|
|
r.mu.Unlock()
|
|
if first {
|
|
// Log a changed error once rather than on every retry.
|
|
log.Printf("%s %v (retrying in %s)", chatBridgeLogTag(kind), err, wait)
|
|
}
|
|
return wait
|
|
}
|
|
|
|
func configSig(cfg []byte) string {
|
|
sum := sha256.Sum256(cfg)
|
|
return hex.EncodeToString(sum[:8])
|
|
}
|
|
|
|
// chatBridgeLogTag is the log prefix of a bridge: [tgbot] for Telegram as
|
|
// issue #5 proposes, [chatbridge:<kind>] for later transports.
|
|
func chatBridgeLogTag(kind string) string {
|
|
if kind == chatbridge.KindTelegram {
|
|
return "[tgbot]"
|
|
}
|
|
return "[chatbridge:" + kind + "]"
|
|
}
|