ask

package
v0.260806.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var PermissionOptions = []PermissionOption{
	{Action: "allow", Inputs: []string{"1", "y", "yes"}, Label: "Allow", Icon: "✅", Approved: true, Remember: false},
	{Action: "deny", Inputs: []string{"2", "n", "no", "0"}, Label: "Deny", Icon: "❌", Approved: false, Remember: false},
	{Action: "always", Inputs: []string{"3", "a", "always"}, Label: "Always Allow", Icon: "🔄", Approved: true, Remember: true},
}

PermissionOptions is the configurable list of permission response options. Modify this slice to change inputs, display text, and keyboard buttons for all platforms.

Functions

func BuildDefaultPrompt

func BuildDefaultPrompt(req Request) string

BuildDefaultPrompt creates the default permission prompt text

func FormatPermissionInstructions

func FormatPermissionInstructions() string

FormatPermissionInstructions returns formatted text instructions for text-based approval. Used by platforms that don't support inline keyboards.

func NormalizeOptions

func NormalizeOptions(v any) []map[string]any

NormalizeOptions is identical to NormalizeQuestions; named separately for readability at call sites where the payload represents per-question options.

func NormalizeQuestions

func NormalizeQuestions(v any) []map[string]any

NormalizeQuestions coerces the heterogeneous "questions" payload into a canonical []map[string]any. Callers may serialize it as []interface{}, []map[string]any, or []any of map[string]any — all should yield identical downstream rendering. Returns nil for any other shape (including nil).

func ParseTextResponse

func ParseTextResponse(text string) (approved bool, remember bool, isValid bool)

ParseTextResponse parses user text input as a permission response Returns: (approved, remember, isValid)

Types

type AskUserQuestionHandler

type AskUserQuestionHandler struct{}

AskUserQuestionHandler handles the AskUserQuestion tool which presents multiple choice questions to the user

func NewAskUserQuestionHandler

func NewAskUserQuestionHandler() *AskUserQuestionHandler

NewAskUserQuestionHandler creates a new AskUserQuestionHandler

func (*AskUserQuestionHandler) BuildPrompt

func (h *AskUserQuestionHandler) BuildPrompt(req Request) string

BuildPrompt creates a prompt showing all questions and options

func (*AskUserQuestionHandler) CanHandle

func (h *AskUserQuestionHandler) CanHandle(toolName string, input map[string]interface{}) bool

CanHandle returns true for AskUserQuestion tool

func (*AskUserQuestionHandler) Description

func (h *AskUserQuestionHandler) Description() string

Description returns the handler description

func (*AskUserQuestionHandler) ParseResponse

func (h *AskUserQuestionHandler) ParseResponse(req Request, response Response) (Result, error)

ParseResponse parses the user's selection into the answers format

type DefaultToolHandler

type DefaultToolHandler struct{}

DefaultToolHandler is the fallback handler for tools without specific handlers

func NewDefaultToolHandler

func NewDefaultToolHandler() *DefaultToolHandler

NewDefaultToolHandler creates a new DefaultToolHandler

func (*DefaultToolHandler) BuildPrompt

func (h *DefaultToolHandler) BuildPrompt(req Request) string

BuildPrompt creates a simple permission prompt

func (*DefaultToolHandler) CanHandle

func (h *DefaultToolHandler) CanHandle(toolName string, input map[string]interface{}) bool

CanHandle returns true for all tools (acts as fallback)

func (*DefaultToolHandler) Description

func (h *DefaultToolHandler) Description() string

Description returns the handler description

func (*DefaultToolHandler) ParseResponse

func (h *DefaultToolHandler) ParseResponse(req Request, response Response) (Result, error)

ParseResponse parses a simple approve/deny response

type PermissionOption

type PermissionOption struct {
	Action   string   // callback action (e.g. "allow", "deny", "always")
	Inputs   []string // accepted text inputs (e.g. ["1", "y", "yes"])
	Label    string   // display label (e.g. "Allow")
	Icon     string   // emoji for keyboard button (e.g. "✅")
	Approved bool
	Remember bool
}

PermissionOption defines a single permission response option. This is the single source of truth for both keyboard buttons and text-based responses.

  • Action: callback action identifier (used in keyboard callback data)
  • Inputs: accepted text inputs for non-keyboard platforms (number | letter | word)
  • Label: display label for both keyboard button text and text instructions
  • Icon: emoji prefix for keyboard button

func FindPermissionByAction

func FindPermissionByAction(action string) *PermissionOption

FindPermissionByAction finds a PermissionOption by its callback action string. Returns nil if not found.

type Prompter

type Prompter interface {
	// Prompt sends a prompt to the user and returns the response
	Prompt(ctx context.Context, req Request) (Result, error)
}

Prompter handles the actual user interaction (production implementation: imchannel.IMPrompter).

type Request

type Request struct {
	// ID is the unique identifier for this request
	ID string `json:"id"`

	// Type is the type of user interaction
	Type     Type   `json:"type"`
	ChatID   string `json:"chat_id"`
	Platform string `json:"platform"`
	BotUUID  string `json:"bot_uuid"`

	// SessionID is the session this request belongs to
	SessionID string `json:"session_id,omitempty"`

	// AgentType is the source agent type
	AgentType agentboot.AgentType `json:"agent_type"`

	// ToolName is the tool name for permission requests
	ToolName string `json:"tool_name,omitempty"`

	// Input is the tool input data
	Input map[string]interface{} `json:"input,omitempty"`

	// Title is an optional title for the prompt
	Title string `json:"title,omitempty"`

	// Message is the main prompt message
	Message string `json:"message,omitempty"`

	// Reason explains why this request is being made
	Reason string `json:"reason,omitempty"`

	// Timeout is the maximum time to wait for a response
	Timeout time.Duration `json:"timeout,omitempty"`

	// Metadata contains additional context (e.g., chat_id, platform for IM)
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

Request represents a request to ask the user something

func FromApprovalEvent

func FromApprovalEvent(e agentboot.ApprovalRequestEvent) *Request

FromApprovalEvent builds an ask.Request from an agentboot.ApprovalRequestEvent.

type Response

type Response struct {
	// Type indicates the response type: "button", "text", "selection"
	Type string `json:"type"`

	// Data contains the raw response data
	Data string `json:"data"`

	// Selections contains structured selections for multi-select scenarios
	Selections map[string]interface{} `json:"selections,omitempty"`
}

Response represents a user's raw response (from button click or text input)

type Result

type Result struct {
	// ID matches the request ID
	ID string `json:"id"`

	// Approved indicates if the request was approved (for permission/confirmation)
	Approved bool `json:"approved,omitempty"`

	// Response contains text input (for text_input type)
	Response string `json:"response,omitempty"`

	// Selection contains structured selections (for question type)
	// Key is typically the question index or ID, value is the selected option
	Selection map[string]interface{} `json:"selection,omitempty"`

	// Remember indicates this decision should be remembered
	Remember bool `json:"remember,omitempty"`

	// Reason explains the decision
	Reason string `json:"reason,omitempty"`

	// UpdatedInput contains modified tool input (for AskUserQuestion answers)
	UpdatedInput map[string]interface{} `json:"updated_input,omitempty"`
}

Result represents the user's response to an ask request

func ParseDefaultResponse

func ParseDefaultResponse(req Request, response Response) (Result, error)

ParseDefaultResponse parses standard allow/deny responses

func (*Result) ToApprovalResponse

func (r *Result) ToApprovalResponse() agentboot.ApprovalResponse

ToApprovalResponse converts an ask Result to an agentboot.ApprovalResponse. The Remember flag is intentionally not propagated to the agent — the ask subsystem owns AlwaysAllow caching internally.

type ToolHandler

type ToolHandler interface {
	// CanHandle returns true if this handler can handle the given tool
	CanHandle(toolName string, input map[string]interface{}) bool

	// Description returns a human-readable description of this handler
	Description() string
}

ToolHandler handles tool-specific ask requests Each tool type can have its own handler to customize the prompt and response parsing

type ToolHandlerRegistry

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

ToolHandlerRegistry manages tool-specific handlers

func NewToolHandlerRegistry

func NewToolHandlerRegistry() *ToolHandlerRegistry

NewToolHandlerRegistry creates a new registry with default handlers

func (*ToolHandlerRegistry) FindHandler

func (r *ToolHandlerRegistry) FindHandler(toolName string, input map[string]interface{}) ToolHandler

FindHandler finds a handler that can handle the given tool

func (*ToolHandlerRegistry) FindPromptBuilder

func (r *ToolHandlerRegistry) FindPromptBuilder(toolName string, input map[string]interface{}) ToolPromptBuilder

FindPromptBuilder finds a handler that implements ToolPromptBuilder

func (*ToolHandlerRegistry) FindResponseParser

func (r *ToolHandlerRegistry) FindResponseParser(toolName string, input map[string]interface{}) ToolResponseParser

FindResponseParser finds a handler that implements ToolResponseParser

func (*ToolHandlerRegistry) Register

func (r *ToolHandlerRegistry) Register(h ToolHandler)

Register adds a new handler to the registry Handlers are checked in reverse registration order (most recent first)

type ToolPromptBuilder

type ToolPromptBuilder interface {
	ToolHandler

	// BuildPrompt creates the prompt message for this tool
	BuildPrompt(req Request) string
}

ToolPromptBuilder builds prompts for tool requests

type ToolResponseParser

type ToolResponseParser interface {
	ToolHandler

	// ParseResponse parses user response into Result
	ParseResponse(req Request, response Response) (Result, error)
}

ToolResponseParser parses user responses into results

type Type

type Type string

Type defines the type of user interaction

const (
	// TypePermission is for tool approval requests
	TypePermission Type = "permission"
	// TypeQuestion is for multi-choice questions (AskUserQuestion tool)
	TypeQuestion Type = "question"
)

Jump to

Keyboard shortcuts

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