Files
cc-connect/core/interfaces.go
T
2026-02-28 23:44:59 +08:00

83 lines
2.9 KiB
Go

package core
import (
"context"
"errors"
)
// Platform abstracts a messaging platform (Feishu, DingTalk, Slack, etc.).
type Platform interface {
Name() string
Start(handler MessageHandler) error
Reply(ctx context.Context, replyCtx any, content string) error
Send(ctx context.Context, replyCtx any, content string) error
Stop() error
}
// ErrNotSupported indicates a platform doesn't support a particular operation.
var ErrNotSupported = errors.New("operation not supported by this platform")
// MessageUpdater is an optional interface for platforms that support updating messages.
type MessageUpdater interface {
UpdateMessage(ctx context.Context, replyCtx any, content string) error
}
// MessageHandler is called by platforms when a new message arrives.
type MessageHandler func(p Platform, msg *Message)
// Agent abstracts an AI coding assistant (Claude Code, Cursor, Gemini CLI, etc.).
// All agents must support persistent bidirectional sessions via StartSession.
type Agent interface {
Name() string
// StartSession creates or resumes an interactive session with a persistent process.
StartSession(ctx context.Context, sessionID string) (AgentSession, error)
// ListSessions returns sessions known to the agent backend.
ListSessions(ctx context.Context) ([]AgentSessionInfo, error)
Stop() error
}
// AgentSession represents a running interactive agent session with a persistent process.
type AgentSession interface {
// Send sends a user message to the running agent process via stdin.
Send(prompt string) error
// RespondPermission sends a permission decision back to the agent process.
RespondPermission(requestID string, result PermissionResult) error
// Events returns the channel that emits agent events (kept open across turns).
Events() <-chan Event
// CurrentSessionID returns the current agent-side session ID.
CurrentSessionID() string
// Alive returns true if the underlying process is still running.
Alive() bool
// Close terminates the session and its underlying process.
Close() error
}
// PermissionResult represents the user's decision on a permission request.
type PermissionResult struct {
Behavior string `json:"behavior"` // "allow" or "deny"
UpdatedInput map[string]any `json:"updatedInput,omitempty"` // echoed back for allow
Message string `json:"message,omitempty"` // reason for deny
}
// ToolAuthorizer is an optional interface for agents that support dynamic tool authorization.
type ToolAuthorizer interface {
AddAllowedTools(tools ...string) error
GetAllowedTools() []string
}
// ModeSwitcher is an optional interface for agents that support runtime permission mode switching.
type ModeSwitcher interface {
SetMode(mode string)
GetMode() string
PermissionModes() []PermissionModeInfo
}
// PermissionModeInfo describes a permission mode for display.
type PermissionModeInfo struct {
Key string
Name string
NameZh string
Desc string
DescZh string
}