Help us shape the future of Go! Take the 2026 Go Developer Survey

imp

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: AGPL-3.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeText

func DecodeText(data []byte) (string, error)

DecodeText 将字节数据解码为 UTF-8 字符串,支持 GB18030 回退。

Types

type Chapter

type Chapter struct {
	Title   string
	Content string
}

Chapter 表示解析出的一章。

type GenerateTextFunc

type GenerateTextFunc func(ctx context.Context, providerName string, messages []map[string]any, model string) (string, error)

GenerateTextFunc 是调用 LLM 生成文本的函数签名。 import 包通过此接口解耦对 llm 包的直接依赖。

type ImportResult

type ImportResult struct {
	NovelID         int64            `json:"novel_id"`
	Title           string           `json:"title"`
	ChapterCount    int              `json:"chapter_count"`
	SkippedCount    int              `json:"skipped_count"`
	SkippedChapters []SkippedChapter `json:"skipped_chapters"`
	NeedsLLM        bool             `json:"needs_llm"`           // 正则分割结果不合理,提示前端可调用 LLM 分析
	FilePath        string           `json:"file_path,omitempty"` // NeedsLLM 时回传文件路径,供前端调 ImportWithLLM
}

ImportResult 是完整导入流程的结果。

func Import

func Import(ctx context.Context, logger *slog.Logger, db *gorm.DB, filePath string, gitName, gitEmail string, onProgress ProgressCallback) (*ImportResult, error)

Import 执行完整的小说导入流程:解析文件 → 创建 Novel → 写入章节 → git 提交。

func ImportWithResult

func ImportWithResult(ctx context.Context, logger *slog.Logger, db *gorm.DB, result *Result, filePath string, gitName, gitEmail string, onProgress ProgressCallback) (*ImportResult, error)

ImportWithResult 使用已有的解析结果执行完整导入流程。 用于 LLM 兜底场景:先 AnalyzeWithLLM → ImportWithResult。

type ProgressCallback

type ProgressCallback func(stage, message string, current, total, percent int, novelID int64)

ProgressCallback 是进度回调函数,app 层用它来推送前端事件。

type Result

type Result struct {
	Title           string
	Chapters        []Chapter
	SkippedChapters []SkippedChapter
	NeedsLLM        bool // 当正则无法正确分割时设为 true,提示前端可调用 LLM 分析
}

Result 是导入解析的结果。

func AnalyzeWithLLM

func AnalyzeWithLLM(ctx context.Context, filePath string, providerName string, modelID string, generateFn GenerateTextFunc) (*Result, error)

AnalyzeWithLLM 使用 LLM 分析小说文件的章节格式。 读取文件前 150 行,让 LLM 生成正则,编译后全文匹配分割。 ctx 用于取消 LLM 调用(如用户关闭窗口)。

func Parse

func Parse(filePath string, logger *slog.Logger) (*Result, error)

Parse 根据文件扩展名选择合适的解析器。

func ParseWithLLMPattern

func ParseWithLLMPattern(filePath string, llmPattern string) (*Result, error)

ParseWithLLMPattern 使用 LLM 返回的正则表达式对全文进行章节分割。 llmPattern 是 LLM 根据前 150 行分析后输出的 Go 正则字符串。 如果编译失败、匹配数不足或分割结果不合理,返回 error。

type SkippedChapter

type SkippedChapter struct {
	Title  string `json:"title"`
	Reason string `json:"reason"`
}

SkippedChapter 记录解析时跳过的章节。

Jump to

Keyboard shortcuts

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