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

character

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Character

type Character struct {
	ID          int64     `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
	NovelID     int64     `gorm:"column:novel_id;not null;index"    json:"novel_id"`
	Name        string    `gorm:"column:name;not null;index"        json:"name"`
	Description string    `gorm:"column:description"                json:"description"` // 自然语言描述,LLM 填写
	Personality string    `gorm:"column:personality"                json:"personality"` // JSON 自由格式,LLM 填写。如 {"traits":["勇敢","冲动"],"brief":"一个热血青年"}
	Abilities   string    `gorm:"column:abilities"                  json:"abilities"`   // JSON 数组,如 ["剑术","隐身"]
	CreatedAt   time.Time `gorm:"column:created_at;autoCreateTime"  json:"created_at"`
	UpdatedAt   time.Time `gorm:"column:updated_at;autoUpdateTime"  json:"updated_at"`
}

Character 是角色元数据。 personality 和 abilities 为 JSON 自由格式,由 LLM 填写和读取,不做结构化约束。

func (Character) TableName

func (Character) TableName() string

TableName 指定 GORM 表名。

type CharacterRelation

type CharacterRelation struct {
	ID                int64     `gorm:"column:id;primaryKey;autoIncrement"           json:"id"`
	NovelID           int64     `gorm:"column:novel_id;not null;index"              json:"novel_id"`
	SourceCharacterID int64     `gorm:"column:source_character_id;not null;index"   json:"source_character_id"` // 关系发出方
	TargetCharacterID int64     `gorm:"column:target_character_id;not null;index"   json:"target_character_id"` // 关系接收方
	RelationDescribe  string    `gorm:"column:relation_describe;not null"          json:"relation_describe"`    // 自由文本,LLM 自行描述,如 "亦师亦敌"、"朋友、高中同学",工具描述的时候需要告诉llm详细描述而不是简单的type
	Description       string    `gorm:"column:description"                          json:"description"`         // 当前关系阶段的详细描述
	ChapterNumber     int       `gorm:"column:chapter_number"                      json:"chapter_number"`       // 此关系在哪个章节确立/变更,可用于拉取章节细节作为 LLM 上下文
	IsCurrent         bool      `gorm:"column:is_current;not null;index"            json:"is_current"`          // true=当前有效关系,false=历史记录
	CreatedAt         time.Time `gorm:"column:created_at;autoCreateTime"            json:"created_at"`
}

CharacterRelation 是角色之间有向关系的当前快照。

设计原则(与 Python 版本的关键差异):

  1. 自由文本 relation_describe — LLM 用自然语言描述关系(如 "朋友、高中同学、暗中嫉妒"), 不再受枚举 18 种类型的限制。单行可表达多面关系。
  2. 追加不可变 (append-only) — 关系变更时不修改旧行,而是 INSERT 新行并将旧行 is_current 置 false。 同一对 (source_id, target_id) 的全部记录按 created_at 排序即为完整演变历史。
  3. 不需要 evolved_from_id 指针链 — 用 (source,target) 配对 + 时间戳隐式排序替代显式链表, 消除断链风险和并发分叉问题。Python 因为枚举 type 导致一对角色需要多行并行记录, 每行各自演化,才被迫引入自引用链;自由文本 relation_describe 从根上消除了这个需求。
  4. 不需要 status 字段 — is_current 布尔替代 active/dormant/resolved/severed 四状态枚举, 关系状态只需区分"当前"和"历史"。

图查询支持:

  • 全图:WHERE novel_id = ? AND is_current = true → 所有节点+边,前端可渲染关系图
  • 单角色邻域:WHERE (source_id = ? OR target_id = ?) AND is_current = true
  • 两角色之间:WHERE (source_id = A AND target_id = B) OR (source_id = B AND target_id = A)

MCP 工具实现参考:

  • update_character_relationship: 变更时开启事务,旧行 SET is_current = false,INSERT 新行,COMMIT
  • get_character_network: 查当前全图,返回 nodes(Character 列表) + edges(CharacterRelation 列表) 可以加入ids参数只查涉及到的人的关系网
  • get_character_history: WHERE source_id = X AND target_id = Y ORDER BY created_at, 可选通过 chapter_number JOIN chapters 拉取对应章节摘要补充上下文

TODO 后续可实现前端的可视化关系图绘制

func (CharacterRelation) TableName

func (CharacterRelation) TableName() string

TableName 指定 GORM 表名。

type ListByNovelOptions

type ListByNovelOptions struct {
	PageParams storage.PageParams
	Search     string // 空字符串=不过滤,按 name LIKE 模糊匹配
	Order      string // 空字符串=默认 "updated_at DESC";否则直接作为 ORDER BY 子句(如 "name ASC")
}

ListByNovelOptions 是 ListByNovel 的可选参数。

type Store

type Store struct {
	DB *gorm.DB
	// contains filtered or unexported fields
}

Store 管理 Character 和 CharacterRelation 持久化。DB 导出供调用方做简单 CRUD。

func NewStore

func NewStore(db *gorm.DB, logger *slog.Logger) *Store

NewStore 创建 character 存储。

func (*Store) Deactivate

func (s *Store) Deactivate(ctx context.Context, relationID int64) error

Deactivate 将一条关系标记为非当前(is_current=false)。

func (*Store) GetByIDs

func (s *Store) GetByIDs(ctx context.Context, ids []int64) ([]Character, error)

GetByIDs 批量按 ID 取角色,用于关系查询时解析角色名。

func (*Store) GetHistory

func (s *Store) GetHistory(ctx context.Context, charA, charB int64) ([]CharacterRelation, error)

GetHistory 返回两角色间全部关系记录(含历史),按时间升序。 不考虑方向——A→B 或 B→A 都算。

func (*Store) ListBetweenCharacters

func (s *Store) ListBetweenCharacters(ctx context.Context, characterIDs []int64) ([]CharacterRelation, error)

ListBetweenCharacters 返回给定角色集合内部的当前关系边(两端都在集合内)。

func (*Store) ListByCharacter

func (s *Store) ListByCharacter(ctx context.Context, characterID int64) ([]CharacterRelation, error)

ListByCharacter 返回某角色所有当前关系(is_current=true),不限方向。

func (*Store) ListByCharacters

func (s *Store) ListByCharacters(ctx context.Context, characterIDs []int64) ([]CharacterRelation, error)

ListByCharacters 批量按角色 ID 取当前关系,用于 LLM 按需查询指定角色群的关系网。

func (*Store) ListByNovel

func (s *Store) ListByNovel(ctx context.Context, novelID int64, opts ListByNovelOptions) (*storage.PageResult[Character], error)

ListByNovel 分页列出某小说的角色,支持 name 搜索。

func (*Store) ListCurrentByNovel

func (s *Store) ListCurrentByNovel(ctx context.Context, novelID int64) ([]CharacterRelation, error)

ListCurrentByNovel 返回某小说全部当前有效关系(is_current=true)。 前端关系图渲染用,数据量大时不建议直接给 LLM。

Jump to

Keyboard shortcuts

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