Files
HomeAgent/pkg/generation/generation.go
JianFeeeee f256098c9f refactor: 全面改名为 HologramAgent + 提示词技术内容下沉到 skill/工具描述 + 修 21 处乱码
## ① 全面改名 HomeAgent → HologramAgent

仓库已改名,但代码与文档里仍有大量旧名。本次按"用户能看到的优先"全量替换:

- 用户可见:登录页标题/页脚、默认人格第一句、CLI 状态输出、启动日志、
  plugin_install 的工具描述、AI 图片插件的 User-Agent
- 文档与脚本:README、assets/docs、docs/zh、deploy/*.sh、.github/workflows
- 鸿蒙工程:cmd/ohos/HomeAgent → cmd/ohos/HologramAgent(git mv)
  目录内的 module/app 描述同步;**证书路径原样保留**
  (default_HomeAgent_xxx.cer/p7b/p12 是签名材料的实际文件名,改了会找不到证书)
- 插件元数据:plugin.json 的 author

★ 必须保持原样的两处(改了会坏):
  · legacySeededPromptSignature = "你是 HomeAgent 的看板娘「小宅」…"
    —— 那是识别历史播种人格卡的**原文锚点**,改了就识别不出旧数据
  · 公开 SDK 的 ProxyAuthHomeAgent = "homeagent"
    —— 那是 SDK 契约里的值,属独立仓库,本仓不改

## ② 修 21 处乱码(U+FFFD)

这些是历史编辑留下的编码破坏,全在注释里,例如:
  「赌注重下���遍」→「赌注重下一遍」
  「「不��滤」」  →「「不过滤」」
  「**待��退场**」→「**待退场**」

★ 保留 providers/chineseclip/tokenizer.go 里的 2 处:那是**故意**用
  U+FFFD 演示"非法 UTF-8 字节被归一成 U+FFFD",不是乱码。

## ③ 提示词技术内容下沉(用户:技术性内容应该收敛到 skill 与 -help 中)

实测系统提示词 2309 字,其中两段几乎全是**模型无法据以行动的实现细节**:

  【工具执行顺序】1272 字(ParallelSafe 声明、批内降级、batchRunnable 判据…)
  【三层记忆】   1069 字(L1/L2/L3 编号、消费式删除、向量空间、指纹…)

收敛后 **2309 → 1723 字(-25%)**,保留的全是"模型据此做什么不同的事":

  工具顺序:默认并行 / 不要依赖先后 / 同通道保序 / 写类工具可能串行
  三层记忆:时序里能看最近 / doc_query 把历史事件按原始时间戳插回时序 /
           关系用 memory_recall / 成文知识用 knowledge_search

判断口径写进判据:新加一段提示词前先问"模型会据此做什么不同的事",
答不上来就该进 skill 或工具描述。

## 判据

prompt_memory_test.go 新增 2 条:
- TestSystemPromptHasNoImplementationJargon
  黑名单:ParallelSafe / batchRunnable / 消费式 / DenseCosine / fingerprint /
  向量空间 / 批内降级 / 自激
- TestSystemPromptStaysCompact:上限 2000 字(当前 1723)

变异:把 "ParallelSafe 声明、批内降级" 加回工具顺序段 ⇒ 第一条立刻红。

## 验证

- go build ./... / go vet ./... 干净
- go test ./... -count=1:58 包 0 失败
- make check-client-versions:客户端与内核版本一致
2026-10-22 20:49:44 +08:00

145 lines
4.5 KiB
Go

// Package generation defines the public SPI for text-generation providers used
// by background pipelines (notably triple distillation).
//
// The HologramAgent core depends only on this package. Model runtimes, tokenizers,
// prompt templates, sampling, and model-specific configuration belong in
// provider packages registered with Register.
//
// This mirrors pkg/embedding deliberately: a model may implement both SPIs from
// one weight set (an embedding front half + a generation head), registering
// under each. The two registries stay independent so a build can select an
// embedding provider and a generation provider separately — or the same model
// for both.
package generation
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"sync"
)
// Request is the model-neutral generation request.
//
// Prompt is the fully-rendered input text; the core does not apply chat
// templates. A provider whose model needs a chat template applies it from
// Prompt internally. JSONSchema, when non-empty, asks the provider to constrain
// output to that JSON Schema (providers that cannot MUST return
// ErrSchemaUnsupported rather than silently ignoring it — a silently ignored
// schema produces free-form text that the caller parses as JSON and fails on).
type Request struct {
Prompt string
MaxTokens int
Temperature float64
Stop []string
// JSONSchema is an optional JSON Schema (as a JSON string). Empty means
// unconstrained text.
JSONSchema string
}
// Response is the model-neutral generation result.
type Response struct {
Text string
// Truncated reports that generation stopped at MaxTokens rather than a
// natural stop. Callers parsing structured output should treat a truncated
// response as suspect.
Truncated bool
}
// Info describes one generation provider's identity.
type Info struct {
// Model is a human-readable identifier for diagnostics/logging.
Model string
// SupportsJSONSchema reports whether Generate honors Request.JSONSchema.
SupportsJSONSchema bool
}
// Provider is the public Go extension point for text generation.
//
// Implementations must be safe for concurrent Generate calls unless their
// factory documents otherwise and serializes internally.
type Provider interface {
Generate(context.Context, Request) (Response, error)
Info() Info
Close()
}
// Config contains provider-owned options. The core does not interpret option
// names or values; it passes core.memory.distill.generation.options.* through
// after stripping the prefix.
type Config struct {
Options map[string]string
}
// Factory constructs a provider instance.
type Factory func(Config) (Provider, error)
var (
// ErrSchemaUnsupported means the provider cannot constrain output to a JSON
// Schema. Callers must not assume the returned text is valid JSON.
ErrSchemaUnsupported = errors.New("generation: json schema not supported")
registryMu sync.RWMutex
registry = make(map[string]Factory)
)
// Register makes a provider factory available under name. It is normally called
// from a provider package's init function. Duplicate names panic so a build
// cannot silently select whichever package initialized last.
func Register(name string, factory Factory) {
name = strings.TrimSpace(name)
if name == "" {
panic("generation: register empty provider name")
}
if factory == nil {
panic("generation: register nil factory for " + name)
}
registryMu.Lock()
defer registryMu.Unlock()
if _, exists := registry[name]; exists {
panic("generation: provider already registered: " + name)
}
registry[name] = factory
}
// Open constructs a registered provider.
func Open(name string, cfg Config) (Provider, error) {
name = strings.TrimSpace(name)
registryMu.RLock()
factory := registry[name]
registryMu.RUnlock()
if factory == nil {
return nil, fmt.Errorf("generation: unknown provider %q (available: %s)", name, strings.Join(Names(), ", "))
}
provider, err := factory(cloneConfig(cfg))
if err != nil {
return nil, fmt.Errorf("generation: open provider %q: %w", name, err)
}
if provider == nil {
return nil, fmt.Errorf("generation: provider %q returned nil", name)
}
return provider, nil
}
// Names returns registered provider names in deterministic order.
func Names() []string {
registryMu.RLock()
defer registryMu.RUnlock()
names := make([]string, 0, len(registry))
for name := range registry {
names = append(names, name)
}
sort.Strings(names)
return names
}
func cloneConfig(cfg Config) Config {
out := Config{Options: make(map[string]string, len(cfg.Options))}
for key, value := range cfg.Options {
out.Options[key] = value
}
return out
}