sessiontitle

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

normalize.go 复刻官方 session-title/normalize:把不可信标题文本清洗为单行 (剥离转义/控制字符/方向控制、空白归一),并按 UTF-8 字节预算截断而不拆字符。

Package sessiontitle 提供会话标题生成能力(任务 S08:Session Title)。

对齐上游:packages/storage/session-title

会话标题的派生路径(latest-wins fold)已在 M05 中通过 session/session-title 事件 与 FoldSessionTitle 投影实现。本包补齐「标题怎么算出来」这部分:

  • Fallback:从首条用户消息内容取前缀(默认前 30 个 Unicode 字符)作为标题;
  • LLM Helper:可选地注入一个 LLMAdapter,让模型基于首条消息生成更精炼的标题; LLM 失败或未启用时回退到 Fallback,保证标题永不缺失。

设计要点:

  • 按 rune(Unicode 码点)截断而非按 byte,避免把多字节字符切断成乱码;
  • 只依赖首条 user 消息,与事件折叠解耦,可独立单测;
  • Generate 返回 {Title, Source},Source 标明标题来自 "llm" 还是 "fallback", 便于上层写入 session/title 事件时知晓来源。

Index

Constants

This section is empty.

Variables

View Source
var FallbackSystemPrompt = "" /* 166-byte string literal not displayed */

FallbackSystemPrompt 内置的标题提练系统提示词:要求模型给出一句话精炼标题。

Functions

func CleanTitle

func CleanTitle(input string) string

CleanTitle 移除转义与控制类字符,并把连续空白压成单个空格,返回单行文本。

func Fallback

func Fallback(firstUserMessage string) string

Fallback 从首条用户消息内容生成前缀标题(默认 30 字)。 空内容返回空串,由调用方决定是否落 session/title。

func FallbackMax

func FallbackMax(firstUserMessage string, maxRunes int) string

FallbackMax 按指定的最大字符数生成前缀标题。

func Normalize

func Normalize(input string, maxBytes int) string

Normalize 清洗标题并按字节预算截断。

func TrimTitle

func TrimTitle(s string) string

TrimTitle 清理 LLM 产出的标题:去掉首尾引号、句点等(可选加固)。

func TruncateUTF8

func TruncateUTF8(input string, maxBytes int) string

TruncateUTF8 按 UTF-8 字节预算返回最长的完整 rune 前缀,不拆分字符。

Types

type Generator

type Generator struct {
	// LLM 可选(nil 时强制走 fallback)。启用后模型基于首条用户消息提练标题。
	LLM llm.LLMAdapter
	// Model 显式指定用于标题生成的模型名;为空时由适配器默认决定。
	Model string
	// MaxRunes fallback 标题最大字符数;<=0 使用默认 30。
	MaxRunes int
	// System 传给 LLM 的系统提示词(可选,默认使用内置提练提示词)。
	System string
	// MaxTokens LLM 单次标题生成的最大 token 数。
	MaxTokens int
}

Generator 依据是否启用 LLM helper 生成会话标题。

func (*Generator) Generate

func (g *Generator) Generate(ctx context.Context, firstUserMessage string) Result

Generate 生成标题。优先调用 LLM helper;LLM 不可用或失败时回退到前缀截断。

type Result

type Result struct {
	Title  string `json:"title"`
	Source Source `json:"source"`
}

Result 是一次标题生成的结果。

type Source

type Source string

Source 标题来源。

const (
	SourceLLM      Source = "llm"      // 由 LLM helper 生成
	SourceFallback Source = "fallback" // 由前缀截断生成
)

标题来源枚举。

Jump to

Keyboard shortcuts

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