imbot

package
v0.260801.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2026 License: MPL-2.0 Imports: 24 Imported by: 0

Documentation

Overview

Package imbotsettings provides handlers for ImBot settings management.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterRoutes

func RegisterRoutes(router *swagger.RouteGroup, handler *Handler)

RegisterRoutes registers all ImBot settings routes with swagger documentation

Types

type BotManager

type BotManager struct {
	// contains filtered or unexported fields
}

BotManager manages the lifecycle of ImBot instances. It encapsulates the internal bot.Manager and provides a clean interface for the imbotsettings module to control bot lifecycle.

func NewBotManager

func NewBotManager(ctx context.Context, cfg *config.Config, channelRegistry *channel.Registry) (*BotManager, error)

NewBotManager creates a new BotManager with all required dependencies. channelRegistry is wired into the internal bot.Manager before the background sync loop starts (see below) so every bot it brings up — including bots enabled before the server started — registers itself as a remote.channel.Channel. Passing it here rather than through a later SetChannelRegistry call closes a startup race: periodicBotSync's initial sync runs in its own goroutine immediately after construction, and used to be able to Start() bots before a subsequent SetChannelRegistry call landed, permanently starting them with no channel (Claude Code hooks and the bot interaction API would then find "bot not running" for a bot that was, in fact, running). channelRegistry may be nil (e.g. swagger doc generation, which never drives real chats).

func (*BotManager) ChatStore added in v0.260801.1

func (bm *BotManager) ChatStore() (bot.ChatStoreInterface, error)

ChatStore returns the chat store shared by every running bot.

The store is owned by the StoreManager and must NOT be closed by callers: closing it would pull persistence out from under every running bot. Used by the GET /bots/:bot/chats API to list the chats a bot can reach (so callers of /notify and /interact can discover the channel-native chat_id those endpoints require).

func (*BotManager) GetStatus

func (bm *BotManager) GetStatus() []BotStatus

GetStatus returns the status of all configured bots.

func (*BotManager) GetStore

func (bm *BotManager) GetStore() *db.ImBotSettingsStore

GetStore returns the underlying settings store.

func (*BotManager) GetTBClient

func (bm *BotManager) GetTBClient() tbclient.TBClient

GetTBClient returns the TBClient for SmartGuide model configuration.

func (*BotManager) IsRunning

func (bm *BotManager) IsRunning(uuid string) bool

IsRunning checks if a bot is currently running.

func (*BotManager) PairingManager added in v0.260514.1

func (bm *BotManager) PairingManager() *bot.PairingManager

PairingManager returns the underlying TOFU pairing manager for HTTP/CLI handlers that need to mint, read, or rotate pairing codes.

func (*BotManager) RestartBot added in v0.260514.1

func (bm *BotManager) RestartBot(ctx context.Context, uuid string) error

RestartBot stops a single bot and starts it again, preserving its UUID. Useful for recovering from a panic-isolated bot or applying configuration changes without restarting the whole server. Waits for the stop to fully complete (up to 5s via WaitForStop in StopBot) before starting again so the new instance does not race with the old goroutine.

func (*BotManager) Shutdown

func (bm *BotManager) Shutdown()

Shutdown stops all running bots and cleans up resources.

func (*BotManager) StartAllEnabled

func (bm *BotManager) StartAllEnabled(ctx context.Context) error

StartAllEnabled starts all bots that have enabled: true in their settings. Logs errors for individual bots but continues starting others.

func (*BotManager) StartBot

func (bm *BotManager) StartBot(ctx context.Context, uuid string) error

StartBot starts a single bot by UUID. If the bot is already running, this is a no-op.

func (*BotManager) StopAll

func (bm *BotManager) StopAll()

StopAll stops all running bots.

func (*BotManager) StopBot

func (bm *BotManager) StopBot(uuid string) error

StopBot stops a single bot by UUID. If the bot is not running, this is a no-op. Waits up to 5 seconds for the bot to fully stop before returning.

func (*BotManager) Sync

func (bm *BotManager) Sync(ctx context.Context) error

Sync ensures that running bots match the enabled settings. Starts bots that are enabled but not running, and stops bots that are running but disabled.

type BotStatus

type BotStatus struct {
	UUID     string `json:"uuid"`
	Name     string `json:"name"`
	Platform string `json:"platform"`
	Running  bool   `json:"running"`
	Error    string `json:"error,omitempty"`
}

BotStatus represents the runtime status of a bot.

type CreateRequest

type CreateRequest struct {
	UUID               string            `json:"uuid,omitempty"`
	Name               string            `json:"name,omitempty"`
	Platform           string            `json:"platform"`
	AuthType           string            `json:"auth_type"`
	Auth               map[string]string `json:"auth"`
	ProxyURL           string            `json:"proxy_url,omitempty"`
	ChatID             string            `json:"chat_id_lock,omitempty"`
	BashAllowlist      []string          `json:"bash_allowlist,omitempty"`
	DefaultCwd         string            `json:"default_cwd,omitempty"`   // Default working directory
	DefaultAgent       string            `json:"default_agent,omitempty"` // Default Agent UUID
	Enabled            bool              `json:"enabled"`
	Token              string            `json:"token,omitempty"`               // Legacy field
	SmartGuideProvider string            `json:"smartguide_provider,omitempty"` // Provider UUID
	SmartGuideModel    string            `json:"smartguide_model,omitempty"`    // Model identifier
	RequirePairing     *bool             `json:"require_pairing,omitempty"`     // TOFU pairing gate; nil → platform default
	// RemoteAgent is the remote_agent mount switch: whether this bot is used to
	// control Claude Code / SmartGuide from chat. nil → default (mounted). When
	// set true it also enables the bot (a mount with no live bot is useless).
	RemoteAgent *bool `json:"remote_agent,omitempty"`
}

CreateRequest represents the request to create ImBot settings

type DeleteResponse

type DeleteResponse struct {
	Success bool   `json:"success"`
	Message string `json:"message"`
}

DeleteResponse represents the response for delete operations

type FeishuRegHandler added in v0.260531.1

type FeishuRegHandler struct {
	// contains filtered or unexported fields
}

FeishuRegHandler drives the Feishu/Lark one-click app registration flow (OAuth 2.0 Device Authorization Grant, RFC 8628). The SDK's RegisterApp is a single blocking call that emits the QR link via a callback and then polls until the user authorizes, so it runs in a background goroutine while the HTTP layer exposes a start/status/cancel session model that mirrors the Weixin QR flow.

func NewFeishuRegHandler added in v0.260531.1

func NewFeishuRegHandler(settingsStore *db.ImBotSettingsStore) *FeishuRegHandler

NewFeishuRegHandler creates a new Feishu/Lark one-click registration handler.

func (*FeishuRegHandler) QRCancel added in v0.260531.1

func (h *FeishuRegHandler) QRCancel(c *gin.Context)

QRCancel cancels a pending registration session.

func (*FeishuRegHandler) QRStart added in v0.260531.1

func (h *FeishuRegHandler) QRStart(c *gin.Context)

QRStart initiates the one-click registration flow and returns the QR link.

func (*FeishuRegHandler) QRStatus added in v0.260531.1

func (h *FeishuRegHandler) QRStatus(c *gin.Context)

QRStatus reports the current state of the registration session.

type FeishuRegStartData added in v0.260531.1

type FeishuRegStartData struct {
	QRURL     string `json:"qr_url"`     // Verification link; render as a QR code or open directly
	ExpiresIn int    `json:"expires_in"` // Link lifetime in seconds
}

FeishuRegStartData is the data for the registration start response.

type FeishuRegStartRequest added in v0.260531.1

type FeishuRegStartRequest struct {
	BotUUID     string `json:"bot_uuid" binding:"required"`
	BotName     string `json:"bot_name,omitempty"`     // Optional: bot display name (for deferred creation)
	BotPlatform string `json:"bot_platform,omitempty"` // Optional: "feishu" or "lark" (for deferred creation)
}

FeishuRegStartRequest is the request to start one-click app registration.

type FeishuRegStartResponse added in v0.260531.1

type FeishuRegStartResponse struct {
	Success bool               `json:"success"`
	Data    FeishuRegStartData `json:"data"`
	Error   string             `json:"error,omitempty"`
}

FeishuRegStartResponse is the response for starting one-click registration.

type FeishuRegStatusData added in v0.260531.1

type FeishuRegStatusData struct {
	Status      string `json:"status"`                 // pending, confirmed, expired, denied, error
	BotUUID     string `json:"bot_uuid,omitempty"`     // Real bot UUID after confirmed (may differ for deferred creation)
	TenantBrand string `json:"tenant_brand,omitempty"` // "feishu" or "lark", reported by the SDK on confirmation
}

FeishuRegStatusData is the data for the registration status response.

type FeishuRegStatusResponse added in v0.260531.1

type FeishuRegStatusResponse struct {
	Success bool                `json:"success"`
	Data    FeishuRegStatusData `json:"data,omitempty"`
	Error   string              `json:"error,omitempty"`
}

FeishuRegStatusResponse is the response for polling registration status.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler handles ImBot settings HTTP requests

func NewHandler

func NewHandler(ctx context.Context, cfg *config.Config, channelRegistry *channel.Registry) (*Handler, error)

NewHandler creates a new ImBot settings handler. channelRegistry is passed straight through to NewBotManager — see its doc comment for why this must happen at construction time rather than via a later SetChannelRegistry call.

func (*Handler) ChatIDLock added in v0.260801.1

func (h *Handler) ChatIDLock(botUUID string) string

ChatIDLock returns the chat-id lock configured for a bot (empty when none). Used by the GET /bots/:bot/chats lister to scope the shared chat store to the single chat a locked bot can reach.

func (*Handler) ChatStore added in v0.260801.1

func (h *Handler) ChatStore() (bot.ChatStoreInterface, error)

ChatStore returns the chat store shared by every running bot. It is owned by the StoreManager — do not Close it. Used by the notify module to implement GET /bots/:bot/chats.

func (*Handler) CreateSettings

func (h *Handler) CreateSettings(c *gin.Context)

CreateSettings creates a new ImBot configuration

func (*Handler) DeleteSettings

func (h *Handler) DeleteSettings(c *gin.Context)

DeleteSettings deletes an ImBot configuration

func (*Handler) GetPairingCode added in v0.260514.1

func (h *Handler) GetPairingCode(c *gin.Context)

GetPairingCode reveals the bot's current TOFU pairing code so the operator can /bind from their DM. The cleartext code is included in the response; every reveal is recorded in the audit log.

func (*Handler) GetPlatformConfig

func (h *Handler) GetPlatformConfig(c *gin.Context)

GetPlatformConfig returns auth configuration for a specific platform

func (*Handler) GetPlatforms

func (h *Handler) GetPlatforms(c *gin.Context)

GetPlatforms returns all supported ImBot platforms with their configurations

func (*Handler) GetSettings

func (h *Handler) GetSettings(c *gin.Context)

GetSettings returns a single ImBot configuration by UUID

func (*Handler) ListSettings

func (h *Handler) ListSettings(c *gin.Context)

ListSettings returns all ImBot configurations

func (*Handler) Reload added in v0.260514.1

func (h *Handler) Reload(c *gin.Context)

Reload is the HTTP handler for POST /imbot-admin/reload. Re-reads bot settings and starts/stops bots to match the current enabled flags. Does not restart bots whose enabled state has not changed.

func (*Handler) RestartBot added in v0.260514.1

func (h *Handler) RestartBot(c *gin.Context)

RestartBot is the HTTP handler for POST /imbot-admin/restart/:uuid. Restarts a single bot without affecting the rest of the server.

func (*Handler) RestartBotByUUID added in v0.260514.1

func (h *Handler) RestartBotByUUID(ctx context.Context, uuid string) error

RestartBotByUUID stops then starts a single bot. Used by both the admin HTTP endpoint and the LifecycleController interface.

func (*Handler) RotatePairingCode added in v0.260514.1

func (h *Handler) RotatePairingCode(c *gin.Context)

RotatePairingCode mints a fresh pairing code, replacing any existing one. The previous code is invalidated immediately. Every rotation is audited.

func (*Handler) Shutdown

func (h *Handler) Shutdown()

Shutdown stops all running bots and cleans up resources

func (*Handler) StartAllEnabled

func (h *Handler) StartAllEnabled(ctx context.Context) error

StartAllEnabled starts all enabled bots (delegates to BotManager)

func (*Handler) StopAll

func (h *Handler) StopAll()

StopAll stops all running bots (delegates to BotManager)

func (*Handler) Sync

func (h *Handler) Sync(ctx context.Context) error

Sync ensures running bots match enabled settings (delegates to BotManager)

func (*Handler) ToggleSettings

func (h *Handler) ToggleSettings(c *gin.Context)

ToggleSettings toggles the enabled status of an ImBot configuration

func (*Handler) UpdateSettings

func (h *Handler) UpdateSettings(c *gin.Context)

UpdateSettings updates an existing ImBot configuration

type LifecycleController added in v0.260514.1

type LifecycleController interface {
	StartAllEnabled(ctx context.Context) error
	StopAll()
	RestartBotByUUID(ctx context.Context, uuid string) error
	Sync(ctx context.Context) error
	Shutdown()
}

LifecycleController is the narrow surface the server uses to drive the imbot module's lifecycle. Replacing the previous untyped interface{} + inline type assertions makes the contract explicit and is the single seam at which an out-of-process implementation could later be swapped in.

type ListResponse

type ListResponse struct {
	Success  bool          `json:"success"`
	Settings []db.Settings `json:"settings"`
}

ListResponse represents the response for listing ImBot settings

type PairingCodeResponse added in v0.260514.1

type PairingCodeResponse struct {
	Success   bool   `json:"success"`
	Active    bool   `json:"active"`               // false = no live code (bot stopped, expired, or non-TOFU)
	Code      string `json:"code,omitempty"`       // cleartext pairing code, present iff Active
	ExpiresAt string `json:"expires_at,omitempty"` // RFC3339 expiry, present iff Active
	Message   string `json:"message,omitempty"`
}

PairingCodeResponse represents the response for pairing-code reveal/rotate.

type PlatformConfig

type PlatformConfig struct {
	Platform    string            `json:"platform"`
	DisplayName string            `json:"display_name"`
	AuthType    string            `json:"auth_type"`
	Category    string            `json:"category"`
	Fields      []imbot.FieldSpec `json:"fields"`
}

PlatformConfig represents a platform configuration

type PlatformConfigResponse

type PlatformConfigResponse struct {
	Success  bool           `json:"success"`
	Platform PlatformConfig `json:"platform"`
}

PlatformConfigResponse represents the response for platform config

type PlatformsResponse

type PlatformsResponse struct {
	Success    bool             `json:"success"`
	Platforms  []PlatformConfig `json:"platforms"`
	Categories gin.H            `json:"categories"`
}

PlatformsResponse represents the response for listing platforms

type QRStartData

type QRStartData struct {
	QrCodeID   string `json:"qrcode_id"`
	QrCodeData string `json:"qrcode_data"`
	ExpiresIn  int    `json:"expires_in"`
}

QRStartData is the data for QR start response

type QRStartRequest

type QRStartRequest struct {
	BotUUID     string `json:"bot_uuid" binding:"required"`
	BotType     string `json:"bot_type,omitempty"`     // Optional bot type (default: "3")
	BotName     string `json:"bot_name,omitempty"`     // Optional: bot display name (for deferred creation)
	BotPlatform string `json:"bot_platform,omitempty"` // Optional: platform (for deferred creation)
}

QRStartRequest is the request to start QR login

type QRStartResponse

type QRStartResponse struct {
	Success bool        `json:"success"`
	Data    QRStartData `json:"data"`
	Error   string      `json:"error,omitempty"`
}

QRStartResponse is the response for QR start

type QRStatusData

type QRStatusData struct {
	Status  string `json:"status"`             // wait, scaned, confirmed, expired
	BotUUID string `json:"bot_uuid,omitempty"` // Real bot UUID after confirmed (may differ from session UUID for new bots)
}

QRStatusData is the data for QR status response

type QRStatusResponse

type QRStatusResponse struct {
	Success bool         `json:"success"`
	Data    QRStatusData `json:"data,omitempty"`
	Error   string       `json:"error,omitempty"`
}

QRStatusResponse is the response for QR status

type SettingsResponse

type SettingsResponse struct {
	Success  bool        `json:"success"`
	Settings db.Settings `json:"settings"`
}

SettingsResponse represents the response for a single ImBot settings

type ToggleResponse

type ToggleResponse struct {
	Success bool `json:"success"`
	Enabled bool `json:"enabled"`
}

ToggleResponse represents the response for toggling ImBot settings

type UpdateRequest

type UpdateRequest struct {
	Name               string            `json:"name,omitempty"`
	Platform           string            `json:"platform,omitempty"`
	AuthType           string            `json:"auth_type,omitempty"`
	Auth               map[string]string `json:"auth,omitempty"`
	ProxyURL           string            `json:"proxy_url,omitempty"`
	ChatID             string            `json:"chat_id_lock,omitempty"`
	BashAllowlist      []string          `json:"bash_allowlist,omitempty"`
	DefaultCwd         *string           `json:"default_cwd,omitempty"`         // Pointer for partial update
	DefaultAgent       *string           `json:"default_agent,omitempty"`       // Pointer for partial update
	Enabled            *bool             `json:"enabled,omitempty"`             // Pointer to allow partial update
	Token              string            `json:"token,omitempty"`               // Legacy field
	SmartGuideProvider *string           `json:"smartguide_provider,omitempty"` // Provider UUID
	SmartGuideModel    *string           `json:"smartguide_model,omitempty"`    // Model identifier
	RequirePairing     *bool             `json:"require_pairing,omitempty"`     // TOFU pairing gate; nil → unchanged
	// RemoteAgent toggles the remote_agent mount (control Claude Code / SmartGuide
	// from chat). nil → unchanged. Setting it true also enables the bot (cascade);
	// setting it false leaves Enabled as-is but the bot stops if it was the only
	// active mount.
	RemoteAgent *bool `json:"remote_agent,omitempty"`
}

UpdateRequest represents the request to update ImBot settings

type WeChatQRLoginHandler

type WeChatQRLoginHandler struct {
	// contains filtered or unexported fields
}

WeChatQRLoginHandler handles Weixin QR code login flow

func NewWeChatQRLoginHandler

func NewWeChatQRLoginHandler(settingsStore *db.ImBotSettingsStore) *WeChatQRLoginHandler

NewWeChatQRLoginHandler creates a new Weixin QR login handler

func (*WeChatQRLoginHandler) QRCancel

func (h *WeChatQRLoginHandler) QRCancel(c *gin.Context)

QRCancel cancels the pending QR login

func (*WeChatQRLoginHandler) QRStart

func (h *WeChatQRLoginHandler) QRStart(c *gin.Context)

QRStart initiates the QR code login flow

func (*WeChatQRLoginHandler) QRStatus

func (h *WeChatQRLoginHandler) QRStatus(c *gin.Context)

QRStatus polls the QR code login status

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL