Files
MyGoNavi/internal/ai/safety/guard.go
Syngnat 1bda751ada feat(ai-chat): 全面升级AI聊天面板并优化交互体验
- 消息管理:新增聊天气泡的重试、编辑与单条删除功能及相对应的持久化状态函数
- 快捷操作:支持长文一键滑动到底端,并在代码块内增加SQL一键送入编辑器的快捷执行机制
- 视觉优化:深化AI回复背景沉浸感,重绘AI洞察按钮并移除设置面板所有的冗余紫色调
- 设置调优:放宽模型初始必填限制,新增内置系统提示词(Builtin Prompt)全览面板
2026-03-22 20:54:29 +08:00

72 lines
1.7 KiB
Go

package safety
import (
"GoNavi-Wails/internal/ai"
)
// Guard AI SQL 安全策略守卫
type Guard struct {
permissionLevel ai.SQLPermissionLevel
}
// NewGuard 创建安全策略守卫
func NewGuard(level ai.SQLPermissionLevel) *Guard {
return &Guard{permissionLevel: level}
}
// SetPermissionLevel 设置权限级别
func (g *Guard) SetPermissionLevel(level ai.SQLPermissionLevel) {
g.permissionLevel = level
}
// GetPermissionLevel 获取当前权限级别
func (g *Guard) GetPermissionLevel() ai.SQLPermissionLevel {
return g.permissionLevel
}
// Check 检查 AI 生成的 SQL 是否在允许范围内
func (g *Guard) Check(sql string) ai.SafetyResult {
opType := ClassifySQL(sql)
allowed := g.isAllowed(opType)
requiresConfirm := g.requiresConfirmation(opType)
warningMessage := ""
if isHighRisk, msg := IsHighRiskSQL(sql); isHighRisk {
warningMessage = msg
requiresConfirm = true
}
return ai.SafetyResult{
Allowed: allowed,
OperationType: opType,
RequiresConfirm: requiresConfirm,
WarningMessage: warningMessage,
}
}
func (g *Guard) isAllowed(opType ai.SQLOperationType) bool {
switch g.permissionLevel {
case ai.PermissionReadOnly:
return opType == ai.SQLOpQuery
case ai.PermissionReadWrite:
return opType == ai.SQLOpQuery || opType == ai.SQLOpDML
case ai.PermissionFull:
return opType == ai.SQLOpQuery || opType == ai.SQLOpDML || opType == ai.SQLOpDDL
default:
return opType == ai.SQLOpQuery
}
}
func (g *Guard) requiresConfirmation(opType ai.SQLOperationType) bool {
switch opType {
case ai.SQLOpQuery:
return false
case ai.SQLOpDML:
return true // DML 始终需要确认
case ai.SQLOpDDL:
return true // DDL 始终需要确认
default:
return true
}
}