ARTEX/server/chatbridge_api.go
shiv e84648f99d feat(chatbridge): task commands, main-agent chat and per-user access
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>
2026-11-27 11:30:06 +05:30

226 lines
6.2 KiB
Go

package server
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/Autumn-27/artex/chatbridge"
"github.com/Autumn-27/artex/db"
)
// HTTP API of the inbound chat bridges. Mounted behind requireAuth like the
// other admin APIs. A bridge is addressed by its transport kind; v1 has one
// bot per deployment, so there is one row per kind.
// chatBridgeDTO is the external form of a bridge. Config is masked: the bot
// token comes back as a masked hint, and sending that hint back unchanged
// keeps the stored token (same convention as notification channels).
type chatBridgeDTO struct {
Kind string `json:"kind"`
Configured bool `json:"configured"`
Enabled bool `json:"enabled"`
Config map[string]any `json:"config"`
AllowedChats []string `json:"allowed_chats"`
AllowedUsers []string `json:"allowed_users"`
SecretKeys []string `json:"secret_keys"`
Status chatBridgeStatus `json:"status"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
type chatBridgeRequest struct {
Enabled *bool `json:"enabled"`
Config map[string]any `json:"config"`
AllowedChats *[]string `json:"allowed_chats"`
AllowedUsers *[]string `json:"allowed_users"`
}
func (s *Server) chatBridgeDTO(kind string, b *db.ChatBridge) chatBridgeDTO {
tr, _ := chatbridge.Get(kind)
out := chatBridgeDTO{
Kind: kind,
Config: map[string]any{},
AllowedChats: []string{},
AllowedUsers: []string{},
SecretKeys: tr.SecretKeys(),
Status: s.chatBridges.Status(kind),
}
if b == nil {
return out
}
var cfg map[string]any
_ = json.Unmarshal(b.Config, &cfg)
out.Configured = true
out.Enabled = b.Enabled
out.Config = chatbridge.MaskConfig(kind, cfg)
out.AllowedChats = b.AllowedChats
out.AllowedUsers = b.AllowedUsers
updated := b.UpdatedAt
out.UpdatedAt = &updated
return out
}
// chatBridgeKind reads and checks the {kind} path value.
func chatBridgeKind(w http.ResponseWriter, r *http.Request) (chatbridge.Transport, bool) {
kind := r.PathValue("kind")
tr, ok := chatbridge.Get(kind)
if !ok {
writeErr(w, 404, fmt.Sprintf("unknown chat bridge %q, available: %s", kind, strings.Join(chatbridge.Kinds(), ", ")))
return nil, false
}
return tr, true
}
func (s *Server) chatBridgeList(w http.ResponseWriter, r *http.Request) {
pg := s.pg(w)
if pg == nil {
return
}
out := []chatBridgeDTO{}
for _, kind := range chatbridge.Kinds() {
b, err := pg.ChatBridgeByKind(r.Context(), kind)
if err != nil && !errors.Is(err, db.ErrChatBridgeNotFound) {
writeErr(w, 500, err.Error())
return
}
out = append(out, s.chatBridgeDTO(kind, b))
}
writeJSON(w, 200, out)
}
func (s *Server) chatBridgeSave(w http.ResponseWriter, r *http.Request) {
pg := s.pg(w)
if pg == nil {
return
}
tr, ok := chatBridgeKind(w, r)
if !ok {
return
}
kind := tr.Kind()
var req chatBridgeRequest
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 64<<10)).Decode(&req); err != nil {
writeErr(w, 400, "request body is not valid JSON: "+err.Error())
return
}
stored, err := pg.ChatBridgeByKind(r.Context(), kind)
if err != nil && !errors.Is(err, db.ErrChatBridgeNotFound) {
writeErr(w, 500, err.Error())
return
}
storedCfg := map[string]any{}
allowed, users := []string{}, []string{}
enabled := false
if stored != nil {
_ = json.Unmarshal(stored.Config, &storedCfg)
allowed, users = stored.AllowedChats, stored.AllowedUsers
enabled = stored.Enabled
}
cfg := storedCfg
if req.Config != nil {
cfg, err = chatbridge.PrepareConfigUpdate(kind, storedCfg, req.Config)
if err != nil {
writeErr(w, 400, err.Error())
return
}
}
if req.AllowedChats != nil {
allowed, err = chatbridge.NormalizeAllowedChats(tr, *req.AllowedChats)
if err != nil {
writeErr(w, 400, err.Error())
return
}
}
if req.AllowedUsers != nil {
users, err = chatbridge.NormalizeAllowedUsers(tr, *req.AllowedUsers)
if err != nil {
writeErr(w, 400, err.Error())
return
}
}
if req.Enabled != nil {
enabled = *req.Enabled
}
// A disabled bridge may hold a partial configuration (the form is saved
// before the token is at hand); an enabled one must be complete.
if enabled || cfgHas(cfg, tr.SecretKeys()) {
if err := tr.Validate(cfg); err != nil {
writeErr(w, 400, err.Error())
return
}
}
raw, err := json.Marshal(cfg)
if err != nil {
writeErr(w, 500, err.Error())
return
}
// Update ids are per bot: a new token makes the old cursor meaningless,
// so it is cleared in the same statement that stores the token.
saved, err := pg.SaveChatBridge(r.Context(), &db.ChatBridge{Kind: kind, Enabled: enabled, Config: raw, AllowedChats: allowed, AllowedUsers: users}, secretsChanged(tr, storedCfg, cfg))
if err != nil {
writeErr(w, 500, err.Error())
return
}
s.chatBridges.Reload(kind)
writeJSON(w, 200, s.chatBridgeDTO(kind, saved))
}
// chatBridgeTest asks the chat app which bot the stored configuration
// belongs to. It checks the token without sending any message.
func (s *Server) chatBridgeTest(w http.ResponseWriter, r *http.Request) {
pg := s.pg(w)
if pg == nil {
return
}
tr, ok := chatBridgeKind(w, r)
if !ok {
return
}
b, err := pg.ChatBridgeByKind(r.Context(), tr.Kind())
if errors.Is(err, db.ErrChatBridgeNotFound) {
writeErr(w, 400, "save the bot token first")
return
}
if err != nil {
writeErr(w, 500, err.Error())
return
}
var cfg map[string]any
_ = json.Unmarshal(b.Config, &cfg)
if err := tr.Validate(cfg); err != nil {
writeErr(w, 400, err.Error())
return
}
start := time.Now()
me, err := tr.Identity(r.Context(), cfg)
if err != nil {
// Transport errors are already free of the token (see chatbridge.telegramTransport.call).
writeErr(w, 502, err.Error())
return
}
writeJSON(w, 200, map[string]any{"ok": true, "bot": me, "latency_ms": time.Since(start).Milliseconds()})
}
func cfgHas(cfg map[string]any, keys []string) bool {
for _, k := range keys {
if s, _ := cfg[k].(string); strings.TrimSpace(s) != "" {
return true
}
}
return false
}
func secretsChanged(tr chatbridge.Transport, before, after map[string]any) bool {
for _, k := range tr.SecretKeys() {
if fmt.Sprint(before[k]) != fmt.Sprint(after[k]) {
return true
}
}
return false
}