bootstrap

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PlanReviewAuto = "auto"
	PlanReviewOn   = "on"
	PlanReviewOff  = "off"
)

PlanReview 取值枚举。

View Source
const CompactRatio = 0.85

CompactRatio 触发上下文压缩的相对阈值:tokens >= window * CompactRatio 时压缩。 0.85 是经验值,给"下一轮 prompt + 大工具结果"留 15% 头部空间,同时让大窗口 模型也能在 85% 主动压缩,避免在 1M 名义窗口下吃满才压(注意力衰退区)。

不暴露给用户配置:与已删除的 context_window 同源——多模型架构下让用户调 数字旋钮反复横跳,不如代码内固定一个合理值。

View Source
const DefaultContextWindow = 200000

DefaultContextWindow 模型未在 registry 登记时的兜底窗口大小。

View Source
const MinCompactReserve = 8000

MinCompactReserve 是 ReserveTokens 的下限。小窗口模型(如 32k 本地 qwen3:8b) 按 0.15 比例算 reserve 仅 4800,单次 commit_chapter 工具响应就能塞 5-8k, 一章正文 8-15k——会出现"压完立刻又超"。8000 兜底保证最坏场景下还有半轮缓冲。

Variables

This section is empty.

Functions

func CompactReserveTokens

func CompactReserveTokens(window int) int

CompactReserveTokens 按 CompactRatio 反算 ReserveTokens 并应用 MinCompactReserve floor:

threshold = window - reserve = window * CompactRatio
reserve   = max(MinCompactReserve, window * (1 - CompactRatio))

给 agentcore.context.Engine 的 EngineConfig.ReserveTokens 用。

func DefaultConfigDir

func DefaultConfigDir() string

DefaultConfigDir 返回 ~/.ainovel 目录路径;取不到家目录时返回空字符串。 仅用于读/写不强制存在的文件(如模型缓存),不会自动创建目录。

func DefaultConfigPath

func DefaultConfigPath() string

DefaultConfigPath 返回全局配置文件路径 ~/.ainovel/config.json。

func LogContextWindowChoice

func LogContextWindowChoice(role, model string, window int, source ContextWindowSource)

LogContextWindowChoice 打印某个角色的窗口决策。source=default 时发 Warn 提示 该模型未在 registry 命中(OpenRouter 也未收录),后续上下文压缩会按兜底窗口 触发——若模型实际窗口更大,可在配置文件用 context_window 显式指定,避免被提前压缩、丢史。

func ModelName

func ModelName(m agentcore.ChatModel) string

ModelName 从 ChatModel 中提取当前模型名,失败返回空字符串。 支持 SwappableModel 的热切换:调用时总是返回最新值。

func NeedsSetup

func NeedsSetup(flagPath string) bool

NeedsSetup 检查是否需要首次引导(配置文件不存在时触发)。

func SaveConfig

func SaveConfig(path string, cfg Config) error

SaveConfig 将配置写入指定路径(JSON 格式,缩进美化)。

Types

type Budget added in v0.7.0

type Budget struct {
	MaxCostUSD float64 `json:"max_cost_usd,omitempty"` // 美元上限;<=0 未启用
	WarnRatio  float64 `json:"warn_ratio,omitempty"`   // 告警阈值比例 (0,1),默认 0.8
}

Budget 全书成本预算配置。累计成本(meta/usage.json 口径)达 WarnRatio 比例时告警, 达到 MaxCostUSD 后 Host 拒绝派发新指令并暂停运行(in-flight 子代理自然完成,不强杀)。

func (Budget) Enabled added in v0.7.0

func (b Budget) Enabled() bool

Enabled 报告预算门禁是否启用。

func (Budget) WarnUSD added in v0.7.0

func (b Budget) WarnUSD() float64

WarnUSD 返回告警线金额;WarnRatio 非法时回落默认 0.8。

type Config

type Config struct {
	// 运行时字段(不序列化到 JSON)
	OutputDir string `json:"-"` // 输出根目录

	// 默认 LLM 配置
	Provider  string `json:"provider"` // 默认 provider(Providers map 中的 key)
	ModelName string `json:"model"`    // 默认模型名

	// Provider 凭证库
	Providers map[string]ProviderConfig `json:"providers,omitempty"`

	// 角色级模型覆盖
	Roles map[string]RoleConfig `json:"roles,omitempty"`

	// 创作参数
	Style string `json:"style,omitempty"`

	// ContextWindow 上下文压缩使用的窗口大小。留空(0)时按模型名自动解析:
	// registry 命中用模型真实窗口,未命中兜底 DefaultContextWindow。
	// 显式配置则优先生效——用于给 registry 查不到的自定义模型指定真实窗口,
	// 或把大窗口模型钉在更小的值上提前触发压缩(1M 名义窗口在 200k+ 通常已注意力衰退)。
	// 仅影响压缩阈值,不改变 LLM API 实际请求长度;配置值由用户自负其责。
	ContextWindow int `json:"context_window,omitempty"`

	// 多人格竞稿配置;为空或 personas < 2 时退回单 Writer 行为(完全向后兼容)。
	WritingContest WritingContest `json:"writing_contest,omitempty"`

	// 全书成本预算;MaxCostUSD<=0 视为未启用(完全向后兼容)。
	Budget Budget `json:"budget,omitempty"`

	// PlanReview 规划完成后是否暂停等待用户审阅大纲:"auto"(TUI 开/headless 关,默认)、"on"、"off"
	PlanReview string `json:"plan_review,omitempty"`
}

Config 小说应用配置。

func LoadConfig

func LoadConfig(flagPath string) (Config, error)

LoadConfig 按优先级加载并合并配置:

  1. ~/.ainovel/config.json(全局)
  2. ./ainovel.json(项目级覆盖)
  3. flagPath 指定的路径(最高优先级)

func LoadConfigFile

func LoadConfigFile(path string) (Config, error)

LoadConfigFile 读取单个 JSON 配置文件,支持 // 行注释。 不做任何合并,仅返回该文件自身的配置。文件不存在时返回错误。

func RunSetup

func RunSetup() (Config, error)

RunSetup 运行首次引导,返回生成的配置。

func (Config) CandidateModels

func (c Config) CandidateModels(provider string) []string

CandidateModels 返回某个 provider 下可供切换的模型列表。 优先使用 provider 显式声明的 models;同时补充当前配置中已出现过的该 provider 模型。

func (*Config) DefaultProviderConfig

func (c *Config) DefaultProviderConfig() ProviderConfig

DefaultProviderConfig 返回默认 provider 的凭证配置。

func (*Config) EffectivePlanReview added in v0.8.0

func (c *Config) EffectivePlanReview(interactive bool) bool

EffectivePlanReview 报告规划审阅门禁是否启用。 auto(默认/空值):交互式入口(TUI)启用、headless 关闭。 interactive 由入口层装配 Host 时显式传入,不复用 startup.Request.Interactive。

func (*Config) FillDefaults

func (c *Config) FillDefaults()

FillDefaults 填充默认值。

func (Config) ResolveContextWindow

func (c Config) ResolveContextWindow(modelName string) (int, ContextWindowSource)

ResolveContextWindow 解析上下文压缩使用的有效窗口,按优先级:

  1. 配置文件 ContextWindow > 0 → 直接用(最高优先级,可超过模型真窗口)
  2. models.DefaultRegistry 按模型名查询(OpenRouter 基线 + 24h 刷新)
  3. 兜底 DefaultContextWindow(自定义代理 / 未知模型)

注意:返回值仅用于压缩阈值计算,不会缩小 LLM API 真实可发请求长度。

func (*Config) ValidateBase

func (c *Config) ValidateBase() error

ValidateBase 校验基础配置。

type ContextWindowSource

type ContextWindowSource string

ContextWindowSource 标记窗口取值的来源,供日志/诊断使用。

const (
	CtxWindowConfig   ContextWindowSource = "config"   // 配置文件 context_window 显式指定
	CtxWindowRegistry ContextWindowSource = "registry" // OpenRouter 基线命中
	CtxWindowDefault  ContextWindowSource = "default"  // 兜底(自定义代理/未知模型)
)

type FailoverEvent

type FailoverEvent struct {
	Role         string
	Reason       string
	FromProvider string
	FromModel    string
	ToProvider   string
	ToModel      string
	Err          error
}

FailoverEvent 表示一次显式 provider 切换。 Reason 为短标签(rate_limit / timeout / stream_idle / network),用于结构化日志。

type FailoverReporter

type FailoverReporter func(FailoverEvent)

FailoverReporter 在发生显式切换时被调用。

type ModelRef

type ModelRef struct {
	Provider string `json:"provider"` // provider 名称(Providers map 中的 key)
	Model    string `json:"model"`    // 模型名(原样透传,不做任何解析)
}

ModelRef 表示一个 provider/model 组合。

type ModelSet

type ModelSet struct {
	Default *SwappableModel
	// contains filtered or unexported fields
}

ModelSet 持有按角色分配的模型实例,未配置的角色回退到默认模型。

func NewModelSet

func NewModelSet(cfg Config) (*ModelSet, error)

NewModelSet 根据配置创建多模型集合。 相同 provider+model 组合复用同一个实例。

func (*ModelSet) CurrentSelection

func (ms *ModelSet) CurrentSelection(role string) (provider, model string, explicit bool)

CurrentSelection 返回角色当前生效的 provider/model。 role 为空或 "default" 时返回默认模型。

func (*ModelSet) ForRole

func (ms *ModelSet) ForRole(role string) agentcore.ChatModel

ForRole 返回指定角色的模型,未配置时返回默认模型。

func (*ModelSet) ForRoleWithFailover

func (ms *ModelSet) ForRoleWithFailover(role string, report FailoverReporter) agentcore.ChatModel

ForRoleWithFailover 返回带有单次请求级 fallback 的角色模型。 仅当该角色显式配置了 fallbacks 时生效;未配置时退化为普通模型。

func (*ModelSet) Summary

func (ms *ModelSet) Summary() string

Summary 返回模型分配摘要(供日志使用)。

func (*ModelSet) Swap

func (ms *ModelSet) Swap(role, provider, model string) error

Swap 切换默认模型或指定角色模型。 role 为空或 "default" 时切换默认模型;其他角色切换为显式覆盖。

type ProviderConfig

type ProviderConfig struct {
	Type    string   `json:"type,omitempty"`     // API 协议类型(openai/anthropic/gemini),自定义代理时指定
	APIKey  string   `json:"api_key,omitempty"`  // API Key
	BaseURL string   `json:"base_url,omitempty"` // API Base URL
	Models  []string `json:"models,omitempty"`   // 可选模型列表,供 TUI 切换时展示
	// ExtraBody 透传给该 provider 每次请求的额外参数(如 temperature/top_p/min_p/
	// presence_penalty,或厂商特有键如 nvidia 开 think 的 chat_template_kwargs)。
	// OpenAI 兼容端逐字并入请求体(即 extra_body 约定);值由用户自负其责。
	ExtraBody map[string]any `json:"extra_body,omitempty"`
}

ProviderConfig 定义单个 LLM 提供商的凭证。

func (ProviderConfig) ProviderType

func (pc ProviderConfig) ProviderType(name string) (string, error)

ProviderType 返回有效的 API 协议类型。 优先使用显式 Type;否则要求 provider 名本身已在 litellm 注册表中。

func (ProviderConfig) RequiresAPIKey

func (pc ProviderConfig) RequiresAPIKey(name string) bool

RequiresAPIKey 返回该 provider 是否必须显式配置 api_key。 约定: 1. ollama / bedrock 允许无 key; 2. 显式指定 Type 的配置视为自定义代理,允许无 key; 3. 其他 provider 默认要求 key,保持对官方托管接口的保守校验。

type RoleConfig

type RoleConfig struct {
	Provider  string     `json:"provider"`            // 主 provider 名称(Providers map 中的 key)
	Model     string     `json:"model"`               // 主模型名(原样透传,不做任何解析)
	Fallbacks []ModelRef `json:"fallbacks,omitempty"` // 显式备用 provider/model 列表
}

RoleConfig 定义单个角色的模型覆盖。

type SwappableModel

type SwappableModel struct {
	*agentcore.SwappableModel
	// contains filtered or unexported fields
}

SwappableModel 是可热切换的 ChatModel 包装器。 已开始的请求继续使用旧实例;后续请求自动切到新实例。

func NewSwappableModel

func NewSwappableModel(provider, name string, model agentcore.ChatModel) *SwappableModel

func (*SwappableModel) Current

func (m *SwappableModel) Current() (provider, name string)

func (*SwappableModel) Info

func (m *SwappableModel) Info() llm.ModelInfo

func (*SwappableModel) ProviderName

func (m *SwappableModel) ProviderName() string

func (*SwappableModel) Swap

func (m *SwappableModel) Swap(provider, name string, model agentcore.ChatModel)

type WritingContest

type WritingContest struct {
	// Personas 是作者名列表(如 ["乌贼","卖报小郎君","土豆"])。
	// 数量即并行 Writer 数;< 2 时不启用竞稿。
	// 每个作者名必须有对应人格画像:把该作者作品语料放入 ./simulate/personas/<作者名>/
	// 并运行 /simulate 生成;缺任一画像则竞稿整体禁用(启动日志会列出缺失项),重启生效。
	// 运行期写手的文风信号 = 人格画像与主画像的融合画像(经 novel_context 注入,单信号)。
	Personas []string `json:"personas,omitempty"`
	// Judge 可选,指定选优裁判模型;缺省复用 editor 角色模型。
	Judge *ModelRef `json:"judge,omitempty"`
	// Concurrency=true 时候选生成阶段并发(一次 parallel subagent 调用);
	// 缺省/false 为串行(逐个补齐,现状行为)。personas<2 时此开关无意义。
	Concurrency bool `json:"concurrency,omitempty"`
	// Mode 竞稿模式:""/"full"(默认,候选写全章)或 "synopsis"(两段式:候选只写
	// 梗概+开头试写,中选后由该 persona 写全章。token 成本约降为 full 模式的 1/N)。
	Mode string `json:"mode,omitempty"`
}

WritingContest 多人格竞稿配置。

func (WritingContest) Enabled

func (w WritingContest) Enabled() bool

Enabled 报告是否启用竞稿(至少 2 个 persona)。

func (WritingContest) Normalize

func (w WritingContest) Normalize() WritingContest

Normalize 去空白、去重、保序,返回规整后的副本。

func (WritingContest) SynopsisMode added in v0.7.0

func (w WritingContest) SynopsisMode() bool

SynopsisMode 报告是否启用两段式(梗概竞稿)。未知值按 full 处理。

Jump to

Keyboard shortcuts

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