* feat(cli): add workspace, config-dir, tools, and skills override flags Add --workspace, --config-dir, --tools, and --skills flags to `picoclaw agent` for single-shot invocations. Supports workspace override with bootstrap file injection (AGENTS.md, IDENTITY.md, SOUL.md, USER.md), tool allowlisting, and skills filtering. Also extracts FormatSkillsSummary from SkillsLoader for reuse, adds SetSkillsFilter to ContextBuilder, and wires SkillsFilter from agent config into the context builder. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat(channels): add MagicForm webhook channel plugin Implement MagicForm channel for webhook-based agentic task delegation. Supports Bearer token auth, async processing with HTTP callback, workspace path validation against configurable workspace_root, bootstrap file injection, tool/skill filtering via metadata, and per-conversation session key isolation. Includes Bus() accessor on BaseChannel for direct message publishing, MagicFormConfig with workspace_root for path traversal prevention, request body size limits, and TTL cleanup for stale request contexts. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat(agent): support per-request workspace, tool, and skill overrides Add workspace override, tool allowlisting, and skills filtering to the agent loop via processOptions metadata. When a workspace override is active (e.g. from MagicForm channel), creates isolated SessionManager and ContextBuilder instances per request. Threads effSessions/effContextBuilder through all downstream paths including runLLMIteration, forceCompression, maybeSummarize, and summarizeSession to ensure full workspace isolation. Adds defense-in- depth tool execution guard alongside LLM-facing tool definition filter. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(auth): add response body size limits to OAuth HTTP calls Wrap all io.ReadAll(resp.Body) calls in the OAuth flow with io.LimitReader capped at 1 MB to prevent memory exhaustion from malicious or unexpectedly large server responses. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(tools): add response body size limits to search providers Add 2 MB LimitReader to BraveSearch, Tavily, DuckDuckGo, and Perplexity search providers to prevent memory exhaustion from unexpectedly large responses. Matches existing pattern used by GLMSearchProvider. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(tools): add 20 MB file write size limit Reject writes exceeding 20 MB in WriteFileTool to prevent disk exhaustion from unexpectedly large LLM-generated file content. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(tools): use crypto/rand for temp file naming Replace predictable PID+nanosecond temp file names with cryptographically random hex strings to prevent symlink pre-placement attacks. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(skills): validate GitHub repo format and limit download size Add regex validation for 'owner/repo' format to prevent URL injection in InstallFromGitHub. Add 5 MB LimitReader on skill file downloads to prevent memory exhaustion. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(auth): allow OAuth credentials to be overridden via env vars Add PICOCLAW_OPENAI_CLIENT_ID, PICOCLAW_GOOGLE_CLIENT_ID, and PICOCLAW_GOOGLE_CLIENT_SECRET env var overrides. Hardcoded values remain as fallback defaults for backward compatibility. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(auth): sanitize OAuth error messages Replace raw HTTP response bodies in error messages with generic status-code-only messages. Full response details are logged at debug level for troubleshooting. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(launcher): HTML-escape error messages in auth callback Use html.EscapeString on all error text rendered in HTML responses to prevent XSS via crafted OAuth error parameters or error messages. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(launcher): remove internal details from HTTP error responses Log detailed errors server-side and return generic messages to clients to prevent information disclosure of file paths and internal state. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(launcher): add HTTP security headers middleware Add SecurityHeaders middleware setting X-Content-Type-Options, X-Frame-Options, and Content-Security-Policy on all responses to mitigate MIME sniffing, clickjacking, and content injection. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(skills): validate ClawHub registry base URL scheme Reject non-HTTPS registry URLs (unless localhost) to prevent config-based redirection to malicious skill servers. Falls back to the default https://clawhub.ai with a warning. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(skills): prevent workspace skills from shadowing builtins Builtin skill names are now reserved — workspace and global skills with the same name as a builtin are skipped with a warning log. LoadSkill also checks builtins first. This prevents supply chain attacks where a malicious skill replaces a trusted builtin. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(skills): warn when safety metadata is unavailable Add MetadataAvailable flag to InstallResult. When the registry metadata fetch fails (silent fallback), the user now sees a warning that safety checks could not be completed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(shell): add opt-in environment variable filtering When filter_env is enabled in config, shell commands run with a minimal allowlist of environment variables (PATH, HOME, LANG, TERM, etc. plus PICOCLAW_* prefix), preventing accidental leakage of sensitive env vars like API keys to spawned processes. Disabled by default for backward compat. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat(config): add workspace-local config.json overlay for per-tenant isolation Support per-workspace config overrides via --config-dir (CLI) or configDir (webhook), enabling different API keys, models, and agent settings per tenant. - Add LoadWorkspaceConfig/MergeWorkspaceConfig with raw JSON overlay to preserve unmentioned bool fields (prevents clobbering tool enabled flags) - Add Config.Clone() for safe per-request config copies in gateway mode - Gateway: per-request provider creation from workspace config, with effProvider/effModel threaded through all LLM call sites and summarization - CLI: workspace config merged before provider creation, CLI flags win - MagicForm: replace inline bootstrap fields with configDir path; agent loop copies bootstrap files and loads config.json from configDir - Fix --session flag: format as agent:main:cli:{key} so router honors it - Fix cron session key: format as agent:main:cron:{id} for isolation - Add shared CopyBootstrapFiles helper in pkg/agent/bootstrap.go - Add config/workspace.config.example.json Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(agent): honor workspace config overrides in model routing selectCandidates uses agent.Model/Candidates baked in at startup, which ignores per-request workspace config overrides (effProvider/effModel). When the workspace overrides the provider, skip routing and fallback candidates entirely to avoid cross-provider credential issues. When only the model is overridden, allow routing but use the effective model. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * style: fix linter errors (gci, gofumpt, golines, govet, unused) - gci: alphabetize magicform import in gateway/helpers.go - gofumpt: reformat multi-line function signature in agent/helpers.go - golines: wrap lines exceeding 120 chars across 5 files - govet: rename shadowed variables (err → wcErr, s → trimmed) - unused: remove maybeSummarize, forceCompression, summarizeSession wrappers superseded by their *With variants Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: nuestraai <nuestraai@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: admin-mf <admin@magicform.ai>
341 lines
9.9 KiB
Go
341 lines
9.9 KiB
Go
package channels
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/binary"
|
|
"encoding/hex"
|
|
"strconv"
|
|
"strings"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/sipeed/picoclaw/pkg/bus"
|
|
"github.com/sipeed/picoclaw/pkg/config"
|
|
"github.com/sipeed/picoclaw/pkg/identity"
|
|
"github.com/sipeed/picoclaw/pkg/logger"
|
|
"github.com/sipeed/picoclaw/pkg/media"
|
|
)
|
|
|
|
var (
|
|
uniqueIDCounter uint64
|
|
uniqueIDPrefix string
|
|
)
|
|
|
|
func init() {
|
|
// One-time read from crypto/rand for a unique prefix (single syscall).
|
|
var b [8]byte
|
|
if _, err := rand.Read(b[:]); err != nil {
|
|
// fallback to time-based prefix
|
|
binary.BigEndian.PutUint64(b[:], uint64(time.Now().UnixNano()))
|
|
}
|
|
uniqueIDPrefix = hex.EncodeToString(b[:])
|
|
}
|
|
|
|
// uniqueID generates a process-unique ID using a random prefix and an atomic counter.
|
|
// This ID is intended for internal correlation (e.g. media scope keys) and is NOT
|
|
// cryptographically secure — it must not be used in contexts where unpredictability matters.
|
|
func uniqueID() string {
|
|
n := atomic.AddUint64(&uniqueIDCounter, 1)
|
|
return uniqueIDPrefix + strconv.FormatUint(n, 16)
|
|
}
|
|
|
|
type Channel interface {
|
|
Name() string
|
|
Start(ctx context.Context) error
|
|
Stop(ctx context.Context) error
|
|
Send(ctx context.Context, msg bus.OutboundMessage) error
|
|
IsRunning() bool
|
|
IsAllowed(senderID string) bool
|
|
IsAllowedSender(sender bus.SenderInfo) bool
|
|
ReasoningChannelID() string
|
|
}
|
|
|
|
// BaseChannelOption is a functional option for configuring a BaseChannel.
|
|
type BaseChannelOption func(*BaseChannel)
|
|
|
|
// WithMaxMessageLength sets the maximum message length (in runes) for a channel.
|
|
// Messages exceeding this limit will be automatically split by the Manager.
|
|
// A value of 0 means no limit.
|
|
func WithMaxMessageLength(n int) BaseChannelOption {
|
|
return func(c *BaseChannel) { c.maxMessageLength = n }
|
|
}
|
|
|
|
// WithGroupTrigger sets the group trigger configuration for a channel.
|
|
func WithGroupTrigger(gt config.GroupTriggerConfig) BaseChannelOption {
|
|
return func(c *BaseChannel) { c.groupTrigger = gt }
|
|
}
|
|
|
|
// WithReasoningChannelID sets the reasoning channel ID where thoughts should be sent.
|
|
func WithReasoningChannelID(id string) BaseChannelOption {
|
|
return func(c *BaseChannel) { c.reasoningChannelID = id }
|
|
}
|
|
|
|
// MessageLengthProvider is an opt-in interface that channels implement
|
|
// to advertise their maximum message length. The Manager uses this via
|
|
// type assertion to decide whether to split outbound messages.
|
|
type MessageLengthProvider interface {
|
|
MaxMessageLength() int
|
|
}
|
|
|
|
type BaseChannel struct {
|
|
config any
|
|
bus *bus.MessageBus
|
|
running atomic.Bool
|
|
name string
|
|
allowList []string
|
|
maxMessageLength int
|
|
groupTrigger config.GroupTriggerConfig
|
|
mediaStore media.MediaStore
|
|
placeholderRecorder PlaceholderRecorder
|
|
owner Channel // the concrete channel that embeds this BaseChannel
|
|
reasoningChannelID string
|
|
}
|
|
|
|
func NewBaseChannel(
|
|
name string,
|
|
config any,
|
|
bus *bus.MessageBus,
|
|
allowList []string,
|
|
opts ...BaseChannelOption,
|
|
) *BaseChannel {
|
|
bc := &BaseChannel{
|
|
config: config,
|
|
bus: bus,
|
|
name: name,
|
|
allowList: allowList,
|
|
}
|
|
for _, opt := range opts {
|
|
opt(bc)
|
|
}
|
|
return bc
|
|
}
|
|
|
|
// MaxMessageLength returns the maximum message length (in runes) for this channel.
|
|
// A value of 0 means no limit.
|
|
func (c *BaseChannel) MaxMessageLength() int {
|
|
return c.maxMessageLength
|
|
}
|
|
|
|
// ShouldRespondInGroup determines whether the bot should respond in a group chat.
|
|
// Each channel is responsible for:
|
|
// 1. Detecting isMentioned (platform-specific)
|
|
// 2. Stripping bot mention from content (platform-specific)
|
|
// 3. Calling this method to get the group response decision
|
|
//
|
|
// Logic:
|
|
// - If isMentioned → always respond
|
|
// - If mention_only configured and not mentioned → ignore
|
|
// - If prefixes configured → respond if content starts with any prefix (strip it)
|
|
// - If prefixes configured but no match and not mentioned → ignore
|
|
// - Otherwise (no group_trigger configured) → respond to all (permissive default)
|
|
func (c *BaseChannel) ShouldRespondInGroup(isMentioned bool, content string) (bool, string) {
|
|
gt := c.groupTrigger
|
|
|
|
// Mentioned → always respond
|
|
if isMentioned {
|
|
return true, strings.TrimSpace(content)
|
|
}
|
|
|
|
// mention_only → require mention
|
|
if gt.MentionOnly {
|
|
return false, content
|
|
}
|
|
|
|
// Prefix matching
|
|
if len(gt.Prefixes) > 0 {
|
|
for _, prefix := range gt.Prefixes {
|
|
if prefix != "" && strings.HasPrefix(content, prefix) {
|
|
return true, strings.TrimSpace(strings.TrimPrefix(content, prefix))
|
|
}
|
|
}
|
|
// Prefixes configured but none matched and not mentioned → ignore
|
|
return false, content
|
|
}
|
|
|
|
// No group_trigger configured → permissive (respond to all)
|
|
return true, strings.TrimSpace(content)
|
|
}
|
|
|
|
func (c *BaseChannel) Name() string {
|
|
return c.name
|
|
}
|
|
|
|
func (c *BaseChannel) ReasoningChannelID() string {
|
|
return c.reasoningChannelID
|
|
}
|
|
|
|
func (c *BaseChannel) IsRunning() bool {
|
|
return c.running.Load()
|
|
}
|
|
|
|
func (c *BaseChannel) IsAllowed(senderID string) bool {
|
|
if len(c.allowList) == 0 {
|
|
return true
|
|
}
|
|
|
|
// Extract parts from compound senderID like "123456|username"
|
|
idPart := senderID
|
|
userPart := ""
|
|
if idx := strings.Index(senderID, "|"); idx > 0 {
|
|
idPart = senderID[:idx]
|
|
userPart = senderID[idx+1:]
|
|
}
|
|
|
|
for _, allowed := range c.allowList {
|
|
// Strip leading "@" from allowed value for username matching
|
|
trimmed := strings.TrimPrefix(allowed, "@")
|
|
allowedID := trimmed
|
|
allowedUser := ""
|
|
if idx := strings.Index(trimmed, "|"); idx > 0 {
|
|
allowedID = trimmed[:idx]
|
|
allowedUser = trimmed[idx+1:]
|
|
}
|
|
|
|
// Support either side using "id|username" compound form.
|
|
// This keeps backward compatibility with legacy Telegram allowlist entries.
|
|
if senderID == allowed ||
|
|
idPart == allowed ||
|
|
senderID == trimmed ||
|
|
idPart == trimmed ||
|
|
idPart == allowedID ||
|
|
(allowedUser != "" && senderID == allowedUser) ||
|
|
(userPart != "" && (userPart == allowed || userPart == trimmed || userPart == allowedUser)) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// IsAllowedSender checks whether a structured SenderInfo is permitted by the allow-list.
|
|
// It delegates to identity.MatchAllowed for each entry, providing unified matching
|
|
// across all legacy formats and the new canonical "platform:id" format.
|
|
func (c *BaseChannel) IsAllowedSender(sender bus.SenderInfo) bool {
|
|
if len(c.allowList) == 0 {
|
|
return true
|
|
}
|
|
|
|
for _, allowed := range c.allowList {
|
|
if identity.MatchAllowed(sender, allowed) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (c *BaseChannel) HandleMessage(
|
|
ctx context.Context,
|
|
peer bus.Peer,
|
|
messageID, senderID, chatID, content string,
|
|
media []string,
|
|
metadata map[string]string,
|
|
senderOpts ...bus.SenderInfo,
|
|
) {
|
|
// Use SenderInfo-based allow check when available, else fall back to string
|
|
var sender bus.SenderInfo
|
|
if len(senderOpts) > 0 {
|
|
sender = senderOpts[0]
|
|
}
|
|
if sender.CanonicalID != "" || sender.PlatformID != "" {
|
|
if !c.IsAllowedSender(sender) {
|
|
return
|
|
}
|
|
} else {
|
|
if !c.IsAllowed(senderID) {
|
|
return
|
|
}
|
|
}
|
|
|
|
// Set SenderID to canonical if available, otherwise keep the raw senderID
|
|
resolvedSenderID := senderID
|
|
if sender.CanonicalID != "" {
|
|
resolvedSenderID = sender.CanonicalID
|
|
}
|
|
|
|
scope := BuildMediaScope(c.name, chatID, messageID)
|
|
|
|
msg := bus.InboundMessage{
|
|
Channel: c.name,
|
|
SenderID: resolvedSenderID,
|
|
Sender: sender,
|
|
ChatID: chatID,
|
|
Content: content,
|
|
Media: media,
|
|
Peer: peer,
|
|
MessageID: messageID,
|
|
MediaScope: scope,
|
|
Metadata: metadata,
|
|
}
|
|
|
|
// Auto-trigger typing indicator, message reaction, and placeholder before publishing.
|
|
// Each capability is independent — all three may fire for the same message.
|
|
if c.owner != nil && c.placeholderRecorder != nil {
|
|
// Typing — independent pipeline
|
|
if tc, ok := c.owner.(TypingCapable); ok {
|
|
if stop, err := tc.StartTyping(ctx, chatID); err == nil {
|
|
c.placeholderRecorder.RecordTypingStop(c.name, chatID, stop)
|
|
}
|
|
}
|
|
// Reaction — independent pipeline
|
|
if rc, ok := c.owner.(ReactionCapable); ok && messageID != "" {
|
|
if undo, err := rc.ReactToMessage(ctx, chatID, messageID); err == nil {
|
|
c.placeholderRecorder.RecordReactionUndo(c.name, chatID, undo)
|
|
}
|
|
}
|
|
// Placeholder — independent pipeline
|
|
if pc, ok := c.owner.(PlaceholderCapable); ok {
|
|
if phID, err := pc.SendPlaceholder(ctx, chatID); err == nil && phID != "" {
|
|
c.placeholderRecorder.RecordPlaceholder(c.name, chatID, phID)
|
|
}
|
|
}
|
|
}
|
|
|
|
if err := c.bus.PublishInbound(ctx, msg); err != nil {
|
|
logger.ErrorCF("channels", "Failed to publish inbound message", map[string]any{
|
|
"channel": c.name,
|
|
"chat_id": chatID,
|
|
"error": err.Error(),
|
|
})
|
|
}
|
|
}
|
|
|
|
// Bus returns the underlying MessageBus. This is used by channels that need to
|
|
// publish directly (e.g. to set SessionKey on InboundMessage).
|
|
func (c *BaseChannel) Bus() *bus.MessageBus { return c.bus }
|
|
|
|
func (c *BaseChannel) SetRunning(running bool) {
|
|
c.running.Store(running)
|
|
}
|
|
|
|
// SetMediaStore injects a MediaStore into the channel.
|
|
func (c *BaseChannel) SetMediaStore(s media.MediaStore) { c.mediaStore = s }
|
|
|
|
// GetMediaStore returns the injected MediaStore (may be nil).
|
|
func (c *BaseChannel) GetMediaStore() media.MediaStore { return c.mediaStore }
|
|
|
|
// SetPlaceholderRecorder injects a PlaceholderRecorder into the channel.
|
|
func (c *BaseChannel) SetPlaceholderRecorder(r PlaceholderRecorder) {
|
|
c.placeholderRecorder = r
|
|
}
|
|
|
|
// GetPlaceholderRecorder returns the injected PlaceholderRecorder (may be nil).
|
|
func (c *BaseChannel) GetPlaceholderRecorder() PlaceholderRecorder {
|
|
return c.placeholderRecorder
|
|
}
|
|
|
|
// SetOwner injects the concrete channel that embeds this BaseChannel.
|
|
// This allows HandleMessage to auto-trigger TypingCapable / ReactionCapable / PlaceholderCapable.
|
|
func (c *BaseChannel) SetOwner(ch Channel) {
|
|
c.owner = ch
|
|
}
|
|
|
|
// BuildMediaScope constructs a scope key for media lifecycle tracking.
|
|
func BuildMediaScope(channel, chatID, messageID string) string {
|
|
id := messageID
|
|
if id == "" {
|
|
id = uniqueID()
|
|
}
|
|
return channel + ":" + chatID + ":" + id
|
|
}
|