aictx

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package aictx 提供 internal/ai 各子包共享的 context 键、决策原语与 slot 协议。

本包没有任何 internal/ai/* 依赖,是 ai 体系的最底层公共类型。其他子包 (audit/policy/permission/helper/tool/runner) 都向上依赖本包。

Index

Constants

View Source
const (
	SourcePolicyAllow = "policy_allow" // 命令策略白名单放行
	SourcePolicyDeny  = "policy_deny"  // 命令策略黑名单拒绝
	SourceUserAllow   = "user_allow"   // 用户手动允许
	SourceUserDeny    = "user_deny"    // 用户手动拒绝
	SourceGrantAllow  = "grant_allow"  // Grant 预批准匹配放行
	SourceGrantDeny   = "grant_deny"   // Grant 权限申请被拒绝
)

决策来源常量

Variables

This section is empty.

Functions

func ArgInt

func ArgInt(args map[string]any, key string) int

ArgInt 是 ArgInt64 的 int 收窄包装。

func ArgInt64

func ArgInt64(args map[string]any, key string) int64

ArgInt64 从 tool 参数 map 中提取 int64,兼容 JSON 反序列化后的 float64 / json.Number。

func ArgString

func ArgString(args map[string]any, key string) string

ArgString 从 tool 参数 map 中提取 string 值,缺失/类型不匹配返回空串。

func GetAuditSource

func GetAuditSource(ctx context.Context) string

GetAuditSource 获取审计来源

func GetConversationID

func GetConversationID(ctx context.Context) int64

GetConversationID 获取会话 ID

func GetGrantSessionID

func GetGrantSessionID(ctx context.Context) string

GetGrantSessionID 获取授权会话 ID

func GetPolicyLang

func GetPolicyLang(ctx context.Context) string

GetPolicyLang 获取策略消息的语言

func GetSessionID

func GetSessionID(ctx context.Context) string

GetSessionID 获取会话 ID

func NotifyDataChanged

func NotifyDataChanged(resource string)

NotifyDataChanged 安全广播一次变更事件,未注入通知器时静默忽略。

func RecordDecision

func RecordDecision(ctx context.Context, result CheckResult)

RecordDecision 在工具 handler 中写入决策结果,供 audit middleware 读取。 没有 slot(如 opsctl 直调 handler 路径)时为 no-op。

func SetDataChangeNotifier

func SetDataChangeNotifier(n DataChangeNotifier)

SetDataChangeNotifier 注入数据变更通知器(应在应用启动时调用)。

func WithAuditSource

func WithAuditSource(ctx context.Context, source string) context.Context

WithAuditSource 注入审计来源

func WithCheckResultSlot

func WithCheckResultSlot(ctx context.Context, slot *CheckResult) context.Context

WithCheckResultSlot 把 slot 挂到 ctx 上,供后续 RecordDecision 写入。 通常由 audit middleware 在 c.Next() 之前调用。

func WithConversationID

func WithConversationID(ctx context.Context, id int64) context.Context

WithConversationID 注入会话 ID

func WithGrantSessionID

func WithGrantSessionID(ctx context.Context, id string) context.Context

WithGrantSessionID 注入授权会话 ID

func WithPolicyLang

func WithPolicyLang(ctx context.Context, lang string) context.Context

WithPolicyLang 设置策略消息的语言("zh-cn", "en" 等)

func WithSessionID

func WithSessionID(ctx context.Context, id string) context.Context

WithSessionID 注入会话 ID(opsctl session 或 AI session)

Types

type CheckResult

type CheckResult struct {
	Decision       Decision
	Message        string   // 返回给 AI 的消息
	HintRules      []string // 拒绝时的允许规则提示
	DecisionSource string   // 决策来源(SourcePolicyAllow 等常量)
	MatchedPattern string   // 匹配的命令模式
}

CheckResult 权限检查结果

func GetCheckResult

func GetCheckResult(ctx context.Context) *CheckResult

GetCheckResult 读取当前 ctx 上挂的 *CheckResult slot 值。 返回 nil 表示未挂 slot(opsctl 直调路径)。

func (CheckResult) DecisionString

func (r CheckResult) DecisionString() string

DecisionString 返回决策的字符串表示(用于审计日志存储)

type DataChangeNotifier

type DataChangeNotifier interface {
	NotifyDataChanged(resource string)
}

DataChangeNotifier 由 app 层注入,向前端广播数据变更(资产/分组等), 触发 UI 自动刷新。

type Decision

type Decision int

Decision 权限判定结果

const (
	Allow       Decision = iota // 直接放行
	Deny                        // 拒绝
	NeedConfirm                 // 需要用户确认
)

Jump to

Keyboard shortcuts

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