Files
ARTEX/third_party/norma/noaadapter/identity.go
T
wangdefa f782b612ec build: 内置 norma 模块,构建不再依赖上游仓库
把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到
third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或
改写 tag 都不再影响本仓库构建。

- 已验证 GOPROXY=off 下可完整编译并打出发行包
- go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希)
- 升级方法与授权注意事项见 third_party/README.md
2026-10-20 21:10:41 -07:00

57 lines
2.0 KiB
Go

// Package noaadapter wires the pure noa engine into Norma: it projects
// llm.Message into noa's flat representation and back, persists state and
// archives, and exposes the Compress tool.
//
// See docs/noa-实施方案.md for the specification.
package noaadapter
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/Autumn-27/norma/noa"
)
// DeriveMessageID computes a message's stable identity.
//
// llm.Message has no id field, so noa derives one from the content. A content
// hash rather than an array index is not a stylistic choice: indices shift
// whenever a message is pruned, a nudge is injected, or a session resumes, and
// every ref in the map would break at once. A hash stays put.
//
// text must already have any ref tag stripped. A tag inside the hash would make
// the id change every time the tag is re-rendered, which is every turn.
func DeriveMessageID(role noa.Role, ct noa.ContentType, text, toolCallID, toolName string) string {
seed := string(role) + "|" + string(ct) + "|" + toolCallID + "|" + toolName + "|" + text
sum := sha256.Sum256([]byte(seed))
return "h_" + hex.EncodeToString(sum[:])[:16]
}
// ClusterCounter disambiguates messages that hash identically.
//
// Repeating a command produces byte-identical output, and two messages sharing
// an id would collapse into one in the ref map. The counter appends an
// occurrence suffix so each keeps its own identity, while the first occurrence
// keeps the bare hash — so an unchanged history projects to unchanged ids.
type ClusterCounter struct {
counts map[string]int
}
// NewClusterCounter returns a counter for one projection pass. It must be fresh
// per pass: reusing one across passes would give the same message a different
// suffix each time.
func NewClusterCounter() *ClusterCounter {
return &ClusterCounter{counts: map[string]int{}}
}
// Next returns the id to use for the next occurrence of base.
func (c *ClusterCounter) Next(base string) string {
n := c.counts[base]
c.counts[base] = n + 1
if n == 0 {
return base
}
return fmt.Sprintf("%s_%d", base, n)
}