memory

package
v0.0.0-...-1c978d5 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package memory 提供长期记忆:以 memory_save / memory_search 能力 暴露给模型,由大脑决定何时记、何时查——这是"模型自主"的部分。 (会话级短期记忆见 session 包,由运行时自动织入,模型无感知。)

作用域(scope)是记忆的归属维度,多用户场景的隔离基础:

  • user:<id> 用户私有记忆(偏好、个人事实),by 终端用户隔离;
  • shared 域共享知识(SOP、流程),跨用户可见;
  • session:<id> 会话临时记忆(少用)。

读写不对称是有意的:用户面 agent 的对话写入只落用户桶(写收窄), 召回同时覆盖用户桶与共享池(读放开)——共享知识对所有用户可见, 但对话里的模型碰不到共享池的写入权。"记忆归谁"由框架包装层按 配置施加,模型无感知;往共享池写必须是配置显式授予,不是模型 运行时自选(治理归部署方,库不给自己放权)。

Store 后端可替换为 Redis / 向量库;scope 在向量后端对应 metadata 过滤。

Index

Constants

View Source
const SharedScope = "shared"

SharedScope 是域共享知识的作用域名。

Variables

This section is empty.

Functions

func AsCapabilities

func AsCapabilities(kv Store, scope ScopeConfig) []capability.Capability

AsCapabilities 把长期记忆包装成 memory_save / memory_search 两个能力, 按 scope 策略施加归属:save 写入 write scope(无身份 fail fast), search 覆盖 read scopes。scope 由框架注入,模型无感知。 save 是改动性操作但风险可控,标记 readonly 以免每次记录都触发审批; 接入敏感存储时可自行用 loop.GateApproval 收紧。

func Register

func Register(typ string, f Factory)

Register 注册后端类型(redis/向量库/自定义),实现方空导入即可在 配置里以 memory.store 引用(或 cap://store/memory/<name>)。

func SortHits

func SortHits(hits []Hit)

SortHits 按 Score 降序、Key 升序稳定排序(确定序,eval 可复现)。

func UserScope

func UserScope(userID string) string

UserScope 返回终端用户的作用域名。

Types

type Factory

type Factory func(conf map[string]any) (Store, error)

Factory 按配置构造长期记忆后端。

type Hit

type Hit struct {
	Scope string
	Key   string
	Value string
	Score float64
}

Hit 是一次检索命中。Score 只在同一后端内可比。

func ScanBucket

func ScanBucket(scope string, bucket map[string]string, query string) []Hit

ScanBucket 是关键词后端(inmemory/redis)共用的打分内核:查询分词后 对 key/value 做子串命中(key 权重更高),再叠加字符 bigram Jaccard 兜底(容错分词打不中的黏连中文)。返回 Score>0 的命中,未排序。

type ScopeConfig

type ScopeConfig struct {
	// Write 是对话 memory_save 的落点:user(默认)| shared | session。
	Write string
	// Read 是召回覆盖的作用域:缺省 [user, shared]。
	Read []string
}

ScopeConfig 是记忆作用域策略:对话写入落到哪一层、召回覆盖哪几层。

func (ScopeConfig) ReadScopes

func (c ScopeConfig) ReadScopes(ctx context.Context) []string

ReadScopes 解析当前 ctx 下召回覆盖的 scope 列表(缺省 user+shared; 无用户身份时用户桶自动略过,共享池仍可读)。

type Store

type Store interface {
	Put(ctx context.Context, scope, key, value string) error
	Search(ctx context.Context, scopes []string, query string, limit int) ([]Hit, error)
}

Store 是长期记忆的最小契约。scope 是归属维度(user:<id> / shared / session:<id>);Search 在给定的一组 scope 内检索。实现可以是关键词 匹配,也可以是向量检索(scope → metadata filter)。

Search 返回按相关度排好序的命中,scopes 顺序即优先级(先 user 后 shared:个人事实压过域共识,同键不同值时不丢失、不覆盖)。旧契约 map[string]string 三宗罪:无序(prompt cache 敌对)、跨 scope 同键 覆盖(方向还反了,shared 压 user)、超 limit 时随机子集(eval 不可 复现)——pre-1.0 硬切为 []Hit。

func New

func New(typ string, conf map[string]any) (Store, error)

New 按类型构造长期记忆后端,空类型默认 inmemory。后端未注册时 fail-fast。

Jump to

Keyboard shortcuts

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