config

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: None detected not legal advice Imports: 0 Imported by: 0

Documentation

Overview

Package config 定义全局配置类型和多级设置系统.

实现多级配置加载(默认值 -> 用户级 -> 项目级 -> 本地级 -> 环境变量 -> CLI 参数), 后面的覆盖前面的.

配置文件路径:

  • 用户级: ~/.flyto/settings.json
  • 项目级: <project>/.flyto/settings.json
  • 本地级: <project>/.flyto/settings.local.json

Package config 的模型角色系统.

设计目标:

  • 不硬编码任何模型 ID 到业务逻辑中
  • 通过角色(role)抽象不同用途的模型选择
  • 所有默认值可被用户配置覆盖
  • 支持注册第三方模型

角色:

  • main: 主对话模型(用户直接交互)
  • fast: 快速/廉价模型(摘要,压缩,分类)
  • thinking: 深度思考模型(复杂推理)
  • embed: 嵌入模型(语义搜索,未来扩展)

Index

Constants

This section is empty.

Variables

View Source
var DefaultModels = map[string]*ModelConfig{}

DefaultModels 预置的模型配置. 升华改进(ELEVATED): 引擎不预设任何供应商的模型信息-- 各 provider 通过 RegisterModels(registry) 注册自己的模型. 例如 anthropic.RegisterModels(reg) 注册 Anthropic 模型的定价和 context window. 替代方案:<预填 Anthropic 模型> - 否决:引擎核心耦合 Anthropic 定价.

View Source
var DefaultRoles = map[ModelRole]string{}

DefaultRoles 默认角色映射. 升华改进(ELEVATED): 引擎不预设任何模型--角色映射由消费层通过 Config.Model 和 ModelRegistry.SetRole() 显式配置.空 map 确保引擎对供应商完全无感知. 替代方案:<预填 Anthropic 模型> - 否决:引擎核心耦合 Anthropic.

Functions

func SaveSettings

func SaveSettings(scope Scope, cwd string, settings *Settings) error

SaveSettings 将设置保存到指定作用域的配置文件.

Types

type HookDef

type HookDef struct {
	Command string `json:"command"` // shell 命令
	Timeout int    `json:"timeout"` // 超时(秒),默认 30
}

HookDef 是单个 hook 的定义(与 hooks 包保持结构一致).

type MCPServerConfig

type MCPServerConfig struct {
	Name      string            `json:"name"`
	Transport string            `json:"transport"` // stdio / sse / http / ws
	Command   string            `json:"command,omitempty"`
	Args      []string          `json:"args,omitempty"`
	URL       string            `json:"url,omitempty"`
	Env       map[string]string `json:"env,omitempty"`
}

MCPServerConfig 是 MCP 服务器配置.

type ModelConfig

type ModelConfig = flyto.ModelInfo

ModelConfig 是 flyto.ModelInfo 的类型别名. 升华改进(ELEVATED): 消除 ModelConfig/ModelInfo 类型分裂-- 两套类型描述同一概念(模型规格),统一到公共契约类型 flyto.ModelInfo. 替代方案:<保留两套类型 + 转换函数> - 否决:维护两份字段定义,新增字段必须改两处.

type ModelRegistry

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

ModelRegistry 管理所有模型配置和角色映射. 线程安全,支持运行时动态修改.

func NewModelRegistry

func NewModelRegistry() *ModelRegistry

NewModelRegistry 用默认配置初始化模型注册表. 复制所有默认模型和角色映射,后续修改不影响默认值.

func (*ModelRegistry) AllModels

func (r *ModelRegistry) AllModels() []string

AllModels 返回所有已注册模型的 ID 列表.

func (*ModelRegistry) ContextWindow

func (r *ModelRegistry) ContextWindow(modelID string) int

ContextWindow 获取指定模型的上下文窗口大小. 如果模型未注册,返回默认值 200000.

func (*ModelRegistry) EstimateCost

func (r *ModelRegistry) EstimateCost(modelID string, inputTokens, outputTokens, cacheReadTokens, cacheWriteTokens int) float64

EstimateCost 估算 API 调用成本(美元). 考虑输入,输出,缓存读取和缓存写入四种价格.

func (*ModelRegistry) EstimateSimpleCost

func (r *ModelRegistry) EstimateSimpleCost(modelID string, inputTokens, outputTokens int) float64

EstimateSimpleCost 简化版成本估算(不含缓存),向后兼容.

func (*ModelRegistry) GetConfig

func (r *ModelRegistry) GetConfig(modelID string) *ModelConfig

GetConfig 获取指定模型的配置. 如果模型未注册,返回 nil.

func (*ModelRegistry) GetConfigForRole

func (r *ModelRegistry) GetConfigForRole(role ModelRole) *ModelConfig

GetConfigForRole 获取指定角色对应的模型配置. 便捷方法,等价于 GetConfig(GetRole(role)). 如果角色未映射或模型未注册,返回 nil.

func (*ModelRegistry) GetRole

func (r *ModelRegistry) GetRole(role ModelRole) string

GetRole 获取角色对应的模型 ID. 如果角色未映射,返回空字符串.

func (*ModelRegistry) Register

func (r *ModelRegistry) Register(modelID string, cfg *ModelConfig)

Register 注册新模型或更新已有模型的配置. 支持注册第三方模型.

func (*ModelRegistry) SetRole

func (r *ModelRegistry) SetRole(role ModelRole, modelID string)

SetRole 设置角色对应的模型 ID. 如果 modelID 未注册,仍然可以设置(允许使用未预置的模型).

func (*ModelRegistry) String

func (r *ModelRegistry) String() string

String 返回角色映射的可读字符串表示.

type ModelRole

type ModelRole string

ModelRole 是模型角色枚举. 通过角色抽象,业务逻辑不直接引用具体的模型 ID.

const (
	// RoleMain 主对话模型(用户直接交互)
	RoleMain ModelRole = "main"
	// RoleFast 快速/廉价模型(摘要,压缩,分类)
	RoleFast ModelRole = "fast"
	// RoleThinking 深度思考模型(复杂推理)
	RoleThinking ModelRole = "thinking"
	// RoleEmbed 嵌入模型(语义搜索,未来用)
	RoleEmbed ModelRole = "embed"
)

type PermissionSettings

type PermissionSettings struct {
	// AllowedTools 始终允许的工具列表(支持通配符)
	AllowedTools []string `json:"allowed_tools,omitempty"`
	// DeniedTools 始终拒绝的工具列表
	DeniedTools []string `json:"denied_tools,omitempty"`
	// DefaultMode 默认权限模式
	DefaultMode string `json:"default_mode,omitempty"`
}

PermissionSettings 是权限规则配置.

type Scope

type Scope string

Scope 是配置的作用域枚举.

const (
	ScopeDefault Scope = "default" // 内置默认值
	ScopeUser    Scope = "user"    // ~/.flyto/settings.json
	ScopeProject Scope = "project" // .flyto/settings.json
	ScopeLocal   Scope = "local"   // .flyto/settings.local.json
)

type Settings

type Settings struct {
	// 权限规则
	Permissions PermissionSettings `json:"permissions,omitempty"`
	// MCP 服务器配置
	MCPServers map[string]MCPServerConfig `json:"mcp_servers,omitempty"`
	// 自定义指令(追加到系统提示词)
	CustomInstructions string `json:"custom_instructions,omitempty"`
	// Hook 定义,key 是 hook 类型(pre_tool_use, post_tool_use 等)
	Hooks map[string][]HookDef `json:"hooks,omitempty"`
	// 默认模型
	Model string `json:"model,omitempty"`
	// API Base URL 覆盖
	APIBaseURL string `json:"api_base_url,omitempty"`
	// 最大轮次
	MaxTurns int `json:"max_turns,omitempty"`
	// 最大预算(美元)
	MaxBudgetUSD float64 `json:"max_budget_usd,omitempty"`
	// 详细日志
	Verbose bool `json:"verbose,omitempty"`
}

Settings 是多级设置系统的主结构体,收敛所有可配置项.

func LoadSettings

func LoadSettings(cwd string) (*Settings, error)

LoadSettings 按优先级加载并合并设置. 加载顺序:默认值 -> 用户级 -> 项目级 -> 本地级 -> 环境变量覆盖. cwd 是项目工作目录,用于定位项目级和本地级配置文件.

type Watcher

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

Watcher 监听配置文件变化(基于轮询实现).

func NewWatcher

func NewWatcher(cwd string, interval time.Duration, onChange func(*Settings)) *Watcher

NewWatcher 创建配置文件监听器. interval 是轮询间隔,onChange 在配置发生变化时被调用.

func (*Watcher) Start

func (w *Watcher) Start()

Start 开始监听配置文件变化. 在后台 goroutine 中运行.

func (*Watcher) Stop

func (w *Watcher) Stop()

Stop 停止监听.

Jump to

Keyboard shortcuts

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