Documentation
¶
Index ¶
Constants ¶
const PreferencesTokenBudget = 8000
PreferencesTokenBudget 是注入 system 消息时偏好的软上限(token)。 设计依据:8k 占 200k 上下文窗口 4%,中文 8k 约等于 4-6k 字,能装 60-160 条偏好。 超预算时按 updated_at DESC 保留最近活跃的,末尾追加截断提示(让 LLM 知道有内容被截断、应合并而非新建)。
Variables ¶
This section is empty.
Functions ¶
func CountPreferencesTokens ¶
func CountPreferencesTokens(items []PreferenceItem) (int, error)
CountPreferencesTokens 估算偏好列表全量 token 数(不截断),用于 App 层报告超预算状态。 与 FormatPreferences 输出格式一致,但不做截断,返回完整文本的 token 数。 用于 GetPreferences 返回 token_count + over_budget,前端可据此显示但不阻塞。
func FormatPreferences ¶
func FormatPreferences(items []PreferenceItem) (string, int)
FormatPreferences 格式化偏好列表为可读文本,按全局/小说专属分组,每条带 id 前缀。 用于 NovelProfile 注入 system 消息,agent 看到后可通过 id 调 upsert_preference 更新。 空 items 返回空字符串。
排序:调用方(NovelProfile)SQL 已按 is_global DESC, updated_at DESC 排序—— 全局组在前、组内最近活跃在前。FormatPreferences 信任调用方顺序,不重复排序。 超预算时丢弃"最久未活动"=列表末尾的条目,保留用户最近创建/更新的。
超预算处理:逐条累加 token,超 PreferencesTokenBudget 时停止拼接并丢弃剩余条目, 末尾追加截断提示。返回 (text, truncatedCount)。
Types ¶
type ListOptions ¶
type ListOptions struct {
PageParams storage.PageParams
Search string // 空字符串=不过滤,按 content LIKE OR category LIKE 模糊匹配
Order string // 空字符串=默认 created_at ASC(重构前硬编码值,Order 保留约束)
}
ListOptions 是 ListGlobalPreferences / ListNovelPreferences 的可选参数。
type PreferenceItem ¶
type PreferenceItem struct {
ID int64 `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
NovelID int64 `gorm:"column:novel_id;index" json:"novel_id"` // IsGlobal=true 时无意义
IsGlobal bool `gorm:"column:is_global;not null;index" json:"is_global"` // true=用户全局,false=特定小说
Category string `gorm:"column:category" json:"category"` // LLM 自行归类,自由文本
Content string `gorm:"column:content;not null" json:"content"` // 偏好内容
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime" json:"updated_at"` // 最近活动时间,用于排序(注入截断保留活跃)
}
PreferenceItem 是创作偏好条目。 IsGlobal=true 表示用户级偏好(对所有小说生效),IsGlobal=false 表示特定小说的偏好。
type Store ¶
Store 管理 Novel 持久化。DB 导出供调用方做简单 CRUD。
func (*Store) ListGlobalPreferences ¶
func (s *Store) ListGlobalPreferences(ctx context.Context, opts ListOptions) (*storage.PageResult[PreferenceItem], error)
ListGlobalPreferences 只返回全局偏好(所有小说共享)。 支持搜索(content/category LIKE)和分页,前端全量拉取(Size=-1)和全局搜索复用。
func (*Store) ListNovelPreferences ¶
func (s *Store) ListNovelPreferences(ctx context.Context, novelID int64, opts ListOptions) (*storage.PageResult[PreferenceItem], error)
ListNovelPreferences 只返回某小说的专属偏好(不含全局),前端编辑用。 支持搜索(content/category LIKE)和分页,前端全量拉取(Size=-1)和全局搜索复用。