perf: 上下文策略自适应优化
This commit is contained in:
@@ -59,6 +59,25 @@ func (p *Progress) NextChapter() int {
|
|||||||
return max + 1
|
return max + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ContextProfile 上下文加载策略,根据总章节数自适应。
|
||||||
|
type ContextProfile struct {
|
||||||
|
SummaryWindow int // 加载最近 N 章摘要
|
||||||
|
TimelineWindow int // 加载最近 N 章时间线
|
||||||
|
FullContext bool // true = 忽略窗口,全量加载
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewContextProfile 根据总章节数计算上下文策略。
|
||||||
|
func NewContextProfile(totalChapters int) ContextProfile {
|
||||||
|
switch {
|
||||||
|
case totalChapters <= 15:
|
||||||
|
return ContextProfile{FullContext: true}
|
||||||
|
case totalChapters <= 50:
|
||||||
|
return ContextProfile{SummaryWindow: 5, TimelineWindow: 10}
|
||||||
|
default:
|
||||||
|
return ContextProfile{SummaryWindow: 3, TimelineWindow: 8}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// RunMeta 运行元信息,持久化到 meta/run.json。
|
// RunMeta 运行元信息,持久化到 meta/run.json。
|
||||||
type RunMeta struct {
|
type RunMeta struct {
|
||||||
StartedAt string `json:"started_at"`
|
StartedAt string `json:"started_at"`
|
||||||
|
|||||||
@@ -39,3 +39,8 @@ func (s *Store) LoadRecentSummaries(current, count int) ([]domain.ChapterSummary
|
|||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadAllSummaries 加载 current 章之前的所有摘要(短篇全量模式)。
|
||||||
|
func (s *Store) LoadAllSummaries(current int) ([]domain.ChapterSummary, error) {
|
||||||
|
return s.LoadRecentSummaries(current, current)
|
||||||
|
}
|
||||||
|
|||||||
@@ -73,6 +73,12 @@ func (t *ContextTool) Execute(_ context.Context, args json.RawMessage) (json.Raw
|
|||||||
}
|
}
|
||||||
|
|
||||||
if a.Chapter > 0 {
|
if a.Chapter > 0 {
|
||||||
|
// 根据总章节数计算上下文策略
|
||||||
|
profile := domain.NewContextProfile(0)
|
||||||
|
if progress, err := t.store.LoadProgress(); err == nil && progress != nil && progress.TotalChapters > 0 {
|
||||||
|
profile = domain.NewContextProfile(progress.TotalChapters)
|
||||||
|
}
|
||||||
|
|
||||||
// 角色按 Tier 过滤:core/important 始终返回,secondary/decorative 按出场匹配
|
// 角色按 Tier 过滤:core/important 始终返回,secondary/decorative 按出场匹配
|
||||||
t.loadFilteredCharacters(result, a.Chapter)
|
t.loadFilteredCharacters(result, a.Chapter)
|
||||||
|
|
||||||
@@ -80,18 +86,35 @@ func (t *ContextTool) Execute(_ context.Context, args json.RawMessage) (json.Raw
|
|||||||
if entry, err := t.store.GetChapterOutline(a.Chapter); err == nil {
|
if entry, err := t.store.GetChapterOutline(a.Chapter); err == nil {
|
||||||
result["current_chapter_outline"] = entry
|
result["current_chapter_outline"] = entry
|
||||||
}
|
}
|
||||||
if summaries, err := t.store.LoadRecentSummaries(a.Chapter, 2); err == nil && len(summaries) > 0 {
|
if profile.FullContext {
|
||||||
result["recent_summaries"] = summaries
|
if summaries, err := t.store.LoadAllSummaries(a.Chapter); err == nil && len(summaries) > 0 {
|
||||||
|
result["recent_summaries"] = summaries
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if summaries, err := t.store.LoadRecentSummaries(a.Chapter, profile.SummaryWindow); err == nil && len(summaries) > 0 {
|
||||||
|
result["recent_summaries"] = summaries
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// V3: 状态数据分级加载
|
// 状态数据按策略加载
|
||||||
// timeline:只取最近 5 章的事件(避免后期全量膨胀)
|
if profile.FullContext {
|
||||||
if timeline, err := t.store.LoadRecentTimeline(a.Chapter, 5); err == nil && len(timeline) > 0 {
|
if timeline, err := t.store.LoadTimeline(); err == nil && len(timeline) > 0 {
|
||||||
result["timeline"] = timeline
|
result["timeline"] = timeline
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if timeline, err := t.store.LoadRecentTimeline(a.Chapter, profile.TimelineWindow); err == nil && len(timeline) > 0 {
|
||||||
|
result["timeline"] = timeline
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// foreshadow:只取未回收条目(已回收的对后续写作无意义)
|
// foreshadow:短篇全量,否则只取未回收条目
|
||||||
if foreshadow, err := t.store.LoadActiveForeshadow(); err == nil && len(foreshadow) > 0 {
|
if profile.FullContext {
|
||||||
result["foreshadow_ledger"] = foreshadow
|
if foreshadow, err := t.store.LoadForeshadowLedger(); err == nil && len(foreshadow) > 0 {
|
||||||
|
result["foreshadow_ledger"] = foreshadow
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if foreshadow, err := t.store.LoadActiveForeshadow(); err == nil && len(foreshadow) > 0 {
|
||||||
|
result["foreshadow_ledger"] = foreshadow
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// relationships:保持全量(pair-key 去重,数据量天然可控)
|
// relationships:保持全量(pair-key 去重,数据量天然可控)
|
||||||
if relationships, err := t.store.LoadRelationships(); err == nil && len(relationships) > 0 {
|
if relationships, err := t.store.LoadRelationships(); err == nil && len(relationships) > 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user