common

package
v0.46.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxImageSize: 10MB
	MaxImageSize = 10 * 1024 * 1024
)

Variables

View Source
var (
	Yellow = ui.Yellow
	Green  = ui.Green
	Red    = ui.Red
	Cyan   = ui.Cyan
	Dim    = ui.Dim
)

Colors - ui パッケージの共通色を使用

View Source
var ConfirmInteractive = func(message string) ConfirmResult {

	result, err := ui.ConfirmSelector(message)
	if err != nil {

		return ConfirmResult{Action: "no"}
	}

	if result == "comment" {
		var reader *bufio.Reader
		if globalReader := ui.GetGlobalReader(); globalReader != nil {

			reader = globalReader.GetBufioReader()
		} else {
			reader = bufio.NewReader(os.Stdin)
		}
		comment, image := ReadMultiLineComment(reader)
		return ConfirmResult{Action: "comment", Comment: comment, Image: image}
	}
	return ConfirmResult{Action: result}
}

ConfirmInteractive は拡張確認プロンプト (矢印キー選択UI) Yes/No/Comment を矢印キーで選択、Enterで確定 y/n/c のショートカットキーも引き続きサポート 外部パッケージからも使用可能なようにエクスポート NOTE: テスト時は環境変数 XELYON_INTERACTIVE_CONFIRM=0 で無効化される

View Source
var FileExists = fileExistsImpl

FileExists はファイルが存在するかチェック NOTE: テスト用にグローバル変数としてオーバーライド可能

View Source
var GlobalAutoApprove = false

GlobalAutoApprove は --auto-approve フラグの状態を保持 Agent から SetAutoApprove() で設定される

View Source
var SimpleConfirm = func(message string) bool {
	reader := bufio.NewReader(os.Stdin)

	for {
		Yellow.Printf("%s (y/n): ", message)

		response, err := reader.ReadString('\n')
		if err != nil {

			return false
		}
		response = strings.ToLower(strings.TrimSpace(response))

		if response == "" {
			continue
		}

		return response == "y" || response == "yes" || response == "y" || response == "はい"
	}
}

SimpleConfirm はユーザーに確認を求める(テスト用にグローバル変数として定義) 空入力は無視してリトライする(AI実行中のEnter押下対策) ただしEOF時はfalseを返して終了する NOTE: テスト時は setupTestConfirm() でモックされる

View Source
var ToolSafetyLevels = map[string]ToolSafety{

	"read_file":       SafetyHigh,
	"search_file":     SafetyHigh,
	"search_code":     SafetyHigh,
	"list_dir":        SafetyHigh,
	"git_status":      SafetyHigh,
	"git_log":         SafetyHigh,
	"git_diff":        SafetyHigh,
	"lint":            SafetyHigh,
	"test":            SafetyHigh,
	"web_search":      SafetyHigh,
	"ask_question":    SafetyHigh,
	"serper":          SafetyHigh,
	"lsp_references":  SafetyHigh,
	"lsp_definition":  SafetyHigh,
	"lsp_hover":       SafetyHigh,
	"lsp_diagnostics": SafetyHigh,
	"lsp_rename":      SafetyHigh,

	"write_file":     SafetyMedium,
	"str_replace":    SafetyMedium,
	"append_file":    SafetyMedium,
	"prepend_file":   SafetyMedium,
	"insert_after":   SafetyMedium,
	"insert_before":  SafetyMedium,
	"copy_file":      SafetyMedium,
	"create_dir":     SafetyMedium,
	"git_add":        SafetyMedium,
	"git_commit":     SafetyMedium,
	"git_reset_soft": SafetyMedium,

	"delete_file":    SafetyLow,
	"delete_lines":   SafetyLow,
	"move_file":      SafetyLow,
	"git_push":       SafetyLow,
	"git_checkout":   SafetyLow,
	"git_branch":     SafetyLow,
	"git_stash":      SafetyLow,
	"git_reset_hard": SafetyLow,
	"git_force_push": SafetyLow,
	"bash":           SafetyLow,
	"command":        SafetyLow,
}

ToolSafetyLevels は各ツールの安全性レベルを定義

View Source
var ValidatePath = validatePathImpl

ValidatePath はパストラバーサル攻撃を防ぐためにパスを検証(テスト用にグローバル変数として定義)

Functions

func CleanupOldBackups

func CleanupOldBackups(filePath string) error

CleanupOldBackups は古いバックアップファイルを削除

func ConfirmApproved

func ConfirmApproved(message string) bool

ConfirmApproved is a compatibility helper that preserves the old bool-based API. NOTE: This drops comment/image information.

func CreateBackup

func CreateBackup(filePath string) (string, error)

CreateBackup はファイルのタイムスタンプ付きバックアップを作成

func DisplayContentToInsert

func DisplayContentToInsert(content string)

DisplayContentToInsert は挿入する内容を表示

func DisplayContextAround

func DisplayContextAround(matchIdx int, lines []string, contextLines int)

DisplayContextAround はマッチ行の周囲コンテキストを表示

func DisplayMultipleMatches

func DisplayMultipleMatches(matchIndices []int, lines []string)

DisplayMultipleMatches は複数マッチ時のエラー表示

func DisplayPatternNotFound

func DisplayPatternNotFound(pattern string, lines []string, maxPreview int)

DisplayPatternNotFound はパターンが見つからない場合のエラー表示

func EnsureGitignore

func EnsureGitignore(dir string) error

EnsureGitignore は .gitignore に *.bak* パターンを追加(初回のみ)

func FindGitRoot

func FindGitRoot(startDir string) string

FindGitRoot は .git ディレクトリのあるルートを探す

func FindGitignorePath

func FindGitignorePath(startDir string) string

FindGitignorePath は .gitignore のパスを検索(リポジトリルートから)

func FindWithNormalizedWhitespace

func FindWithNormalizedWhitespace(content, pattern string) (found bool, startIdx, endIdx int)

FindWithNormalizedWhitespace は正規化した状態で文字列を検索

func FormatSize

func FormatSize(bytes int64) string

FormatSize はバイト数を人間が読みやすい形式に変換

func GetCurrentTime

func GetCurrentTime() time.Time

GetCurrentTime returns the current time

func GetSafetyDescription

func GetSafetyDescription(safety ToolSafety) string

GetSafetyDescription は安全性レベルの説明を返す

func IsAutoApprovable

func IsAutoApprovable(toolName string, autoApprove bool) bool

IsAutoApprovable は --auto-approve フラグで自動承認可能かを判定

func IsInteractiveModeEnabled

func IsInteractiveModeEnabled() bool

IsInteractiveModeEnabled は対話的確認モードが有効かチェック デフォルトは有効。

無効化したい場合は以下のいずれかを設定:

  • XELYON_INTERACTIVE_CONFIRM=0
  • XELYON_INTERACTIVE_CONFIRM=false

func IsMediumToolAutoApprovable

func IsMediumToolAutoApprovable(toolName string) bool

IsMediumToolAutoApprovable は SafetyMedium ツールを設定に基づいて自動承認するか判定 config.tool_confirm.auto_approve_medium が true の場合、SafetyMedium ツールは確認なしで実行

func IsSafeToolAutoApprovable

func IsSafeToolAutoApprovable(toolName string) bool

IsSafeToolAutoApprovable は SafetyHigh ツールを設定に基づいて自動承認するか判定 config.tool_confirm.auto_approve_safe が true の場合、SafetyHigh ツールは確認なしで実行

func Max

func Max(a, b int) int

Max は2つの整数の大きい方を返す

func Min

func Min(a, b int) int

Min は2つの整数の小さい方を返す

func NormalizeLeadingWhitespace

func NormalizeLeadingWhitespace(s string) string

NormalizeLeadingWhitespace は行頭の空白のみを正規化 - タブをスペース4つに変換 - 行頭の空白を削除 - 行内の空白は保持(安全性重視)

func SetAutoApprove

func SetAutoApprove(enabled bool)

SetAutoApprove は --auto-approve フラグを設定

func ShowDiff

func ShowDiff(old, new, filename string)

ShowDiff は差分を表示

func ShowImprovedDiff

func ShowImprovedDiff(oldStr, newStr string)

ShowImprovedDiff は改善された差分表示 ui.ShowColoredDiff を使用してインライン形式で表示

func ShowPreview

func ShowPreview(content string)

ShowPreview は新規ファイルのプレビューを表示

func Truncate

func Truncate(s string, maxLen int) string

Truncate は文字列を指定長で切り詰め

func ValidatePathAllowParent

func ValidatePathAllowParent(path string) (string, error)

ValidatePathAllowParent は親ディレクトリへのアクセスを許可 (特定の用途でのみ使用)

Types

type ConfirmAction

type ConfirmAction string

ConfirmAction is the normalized action returned by confirmation prompts.

const (
	ConfirmYes     ConfirmAction = "yes"
	ConfirmNo      ConfirmAction = "no"
	ConfirmComment ConfirmAction = "comment"
)

type ConfirmDecision

type ConfirmDecision struct {
	Action  ConfirmAction
	Comment string
	Image   *ImageData
}

ConfirmDecision is the unified result for all confirmation prompts. Comment/Image are only set when Action==ConfirmComment.

func Confirm

func Confirm(message string) ConfirmDecision

Confirm asks user for confirmation and optionally captures feedback. If interactive confirmation is enabled, it supports y/n/c and multi-line comments. Otherwise it falls back to legacy y/n confirm.

func ConfirmWithAutoApproveDecision

func ConfirmWithAutoApproveDecision(toolName, message string) ConfirmDecision

ConfirmWithAutoApproveDecision は危険度を考慮した確認プロンプト toolName: 実行するツール名 message: 確認メッセージ - auto-approve の場合は yes を返す - それ以外は Confirm(message) を呼び、y/n/c の結果を返す

type ConfirmResult

type ConfirmResult struct {
	Action  string     // "yes", "no", "comment"
	Comment string     // "comment" の場合のコメント内容
	Image   *ImageData // 画像データ(オプション)
}

ConfirmResult は確認結果

type ImageData

type ImageData struct {
	Path      string // 元のファイルパス
	MediaType string // "image/png", "image/jpeg" 等
	Base64    string // Base64エンコードされた画像データ
	Size      int64  // ファイルサイズ(バイト)
}

ImageData は画像データを表す

func ConfirmWithFeedback

func ConfirmWithFeedback(message string) (approved bool, comment string, image *ImageData)

ConfirmWithFeedback は対話的確認を行う(互換ラッパー) interactiveMode=true の場合は y/n/c、false の場合は従来の y/n

NOTE: 新コードでは Confirm() / ConfirmDecision の利用を推奨。

func LoadImage

func LoadImage(path string) (*ImageData, error)

LoadImage は画像ファイルを読み込んでBase64エンコードする

func ReadMultiLineComment

func ReadMultiLineComment(reader *bufio.Reader) (string, *ImageData)

ReadMultiLineComment は複数行コメントを読み取る 空行2回で入力終了 image:プレフィックスで画像を指定可能 /paste(または /p)で Paste Mode を起動して長文を挿入可能

type PatternMatchResult

type PatternMatchResult struct {
	MatchIdx     int   // マッチした行インデックス(-1 = not found)
	MatchCount   int   // マッチ数
	MatchIndices []int // 全マッチ位置
}

PatternMatchResult はパターンマッチの結果を保持

func FindPatternInLines

func FindPatternInLines(lines []string, pattern string) PatternMatchResult

FindPatternInLines は行配列からパターンを検索 Tier 1: Exact match, Tier 2: Normalized whitespace match

type ToolSafety

type ToolSafety int

ToolSafety はツールの安全性レベル

const (
	// SafetyHigh - 読み取り専用操作(常に自動承認OK)
	SafetyHigh ToolSafety = iota
	// SafetyMedium - 書き込み操作(--auto-approve で承認)
	SafetyMedium
	// SafetyLow - 破壊的操作(常に確認必須)
	SafetyLow
)

func GetToolSafety

func GetToolSafety(toolName string) ToolSafety

GetToolSafety は指定されたツールの安全性レベルを返す 定義されていないツールは SafetyMedium(中レベル)として扱う

Jump to

Keyboard shortcuts

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