imp

package
v0.5.2 Latest Latest
Warning

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

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

Documentation

Overview

Package imp 实现外部小说章节的导入与反推。

核心思路:用 LLM 反推 foundation + 每章事实,复用现有 save_foundation / commit_chapter 工具的原子三件套落盘。导入完成后 store 状态等同于"写完 N 章 后崩溃",调用方调 host.Resume() 即可无缝续写。

不走 Coordinator:导入是确定性回放,不属于 LLM 决策范畴;让 Coordinator 介入只会引入不确定性。本包直接调 LLM 客户端 + 调工具。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PersistChapter

func PersistChapter(
	ctx context.Context,
	st *store.Store,
	commitTool *tools.CommitChapterTool,
	chapter int,
	title, content string,
	a *ChapterAnalysis,
) error

PersistChapter 把分析结果落盘:先写章节草稿,再调 commit_chapter 执行原子三件套。 已完成章节会被 commit_chapter 自身的幂等检查跳过,仍返回 nil 让循环继续。

func PersistFoundation

func PersistFoundation(ctx context.Context, st *store.Store, scale domain.PlanningTier, fr *FoundationResult) error

PersistFoundation 把反推结果写入 Store,顺序与 Architect 长篇 prompt 一致: premise → characters → world_rules → layered_outline → compass。导入正文作为第一卷 落成分层大纲,使导入的书可被续写、可扩展。每步都触发 save_foundation 同款落盘逻辑。

不直接调 SaveFoundationTool 是因为这里是确定性回放,无需走 LLM 工具调度。 但保持与 SaveFoundationTool 相同的副作用:phase 推进、checkpoint 追加。

func Run

func Run(ctx context.Context, deps Deps, opts Options) (<-chan Event, error)

Run 执行完整 import 流程:split → foundation → chapter loop。 在自己的 goroutine 中跑;Events 通道由本函数关闭。

设计取舍:

  • 完整流程是阻塞执行(CLI 长任务),调用方负责开 goroutine 监听通道;
  • 任意一步失败都直接结束,发 StageError 事件;
  • chapter 阶段对已完成章节静默跳过(commit_chapter 的幂等是兜底,但跳过 LLM 更省 token)。

Types

type Chapter

type Chapter struct {
	Title   string
	Content string
}

Chapter 是切分后的单个章节。

func SplitFile

func SplitFile(path string) ([]Chapter, error)

SplitFile 把单个文本文件切分成章节列表。

type ChapterAnalysis

type ChapterAnalysis struct {
	Summary             string
	Characters          []string
	KeyEvents           []string
	TimelineEvents      []domain.TimelineEvent
	ForeshadowUpdates   []domain.ForeshadowUpdate
	RelationshipChanges []domain.RelationshipEntry
	StateChanges        []domain.StateChange
	HookType            string
	DominantStrand      string
}

ChapterAnalysis 是单章反推的结构化产物,字段直接对齐 commit_chapter 入参。

func AnalyzeChapter

func AnalyzeChapter(
	ctx context.Context,
	llm LLMChat,
	systemPrompt string,
	chapter int,
	chapterTitle, chapterContent string,
	premise, charactersBlock string,
	activeHooks []domain.ForeshadowEntry,
) (*ChapterAnalysis, error)

AnalyzeChapter 用一次 LLM 调用,从单章正文反推 commit_chapter 所需事实。 hooksContext 是已知伏笔池的快照(可空),用于让 LLM 复用既有 ID。

type Deps

type Deps struct {
	Store      *store.Store
	CommitTool *tools.CommitChapterTool
	LLM        LLMChat // 同一模型即可,foundation/analyzer 都是结构化反推
	Prompts    Prompts
}

Deps 把 runner 需要的可插拔依赖一次性传入,方便测试 mock。

type Event

type Event struct {
	Time    time.Time
	Stage   Stage
	Current int    // chapter 阶段的当前章号;其它阶段为 0
	Total   int    // 总章数
	Message string // 人类可读描述
	Err     error  // StageError 时携带
}

Event 是导入流程对外发出的进度事件。

type FoundationResult

type FoundationResult struct {
	Premise    string                 // Markdown 字符串
	Characters []domain.Character     // 角色档案
	WorldRules []domain.WorldRule     // 世界规则
	Volumes    []domain.VolumeOutline // 分层大纲:导入正文作为第一卷(可续写、可扩展)
	Compass    *domain.StoryCompass   // 续写方向锚点(ending_direction / open_threads / estimated_scale)
}

FoundationResult 是 Foundation 反推的结构化产物。

func ReverseFoundation

func ReverseFoundation(ctx context.Context, llm LLMChat, systemPrompt string, chapters []Chapter) (*FoundationResult, error)

ReverseFoundation 用一次 LLM 调用,从已切分的章节正文反推 foundation。 不调用 save_foundation,纯函数;持久化由调用方决定。

type LLMChat

type LLMChat interface {
	Generate(ctx context.Context, messages []agentcore.Message, tools []agentcore.ToolSpec, opts ...agentcore.CallOption) (*agentcore.LLMResponse, error)
}

LLMChat 是 imp 包对 ChatModel 的最小依赖:仅需要一次普通文本生成。 抽出独立接口便于单测注入 mock,避免直接耦合 agentcore 客户端。

type Options

type Options struct {
	// SourcePath 必填。单个 txt/md 文件路径。
	SourcePath string

	// ResumeFrom 可选。从第 N 章开始导入;0 / 1 表示从头。
	// 若 > 1,会跳过 Foundation 反推(认为已落盘)。
	ResumeFrom int
}

Options 控制导入行为。

type Prompts

type Prompts struct {
	Foundation string // 反推 foundation
	Analyzer   string // 反推单章
}

Prompts 是 imp 流程使用的两段提示词。

type Stage

type Stage string

Stage 表示导入流程的当前阶段。

const (
	StageSplitting  Stage = "splitting"
	StageFoundation Stage = "foundation"
	StageChapter    Stage = "chapter"
	StageDone       Stage = "done"
	StageError      Stage = "error"
)

Jump to

Keyboard shortcuts

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