把 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
107 lines
2.6 KiB
Go
107 lines
2.6 KiB
Go
package llm
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
)
|
|
|
|
// Accumulator reassembles a complete assistant Message from a stream of
|
|
// normalized StreamEvents. The harness feeds every event to Add; once the
|
|
// stream ends, Message returns the finished assistant turn (text, thinking, and
|
|
// tool_use blocks in arrival order).
|
|
type Accumulator struct {
|
|
blocks []ContentBlock
|
|
toolInputs []*strings.Builder
|
|
textBuf *strings.Builder
|
|
thinkBuf *strings.Builder
|
|
thinkSig string // signature for the current thinking block
|
|
StopReason string
|
|
Usage Usage
|
|
}
|
|
|
|
// NewAccumulator returns an empty Accumulator.
|
|
func NewAccumulator() *Accumulator { return &Accumulator{} }
|
|
|
|
// Add folds one event into the accumulator.
|
|
func (a *Accumulator) Add(ev StreamEvent) {
|
|
switch ev.Type {
|
|
case SEMessageStart:
|
|
a.Usage.Add(ev.Usage)
|
|
case SETextDelta:
|
|
if a.textBuf == nil {
|
|
a.flushThinking()
|
|
a.textBuf = &strings.Builder{}
|
|
}
|
|
a.textBuf.WriteString(ev.Text)
|
|
case SEThinkingDelta:
|
|
if a.thinkBuf == nil {
|
|
a.flushText()
|
|
a.thinkBuf = &strings.Builder{}
|
|
}
|
|
a.thinkBuf.WriteString(ev.Text)
|
|
case SEThinkingSignature:
|
|
a.thinkSig = ev.Text
|
|
case SEToolUseStart:
|
|
a.flushText()
|
|
a.flushThinking()
|
|
a.blocks = append(a.blocks, ContentBlock{Type: BlockToolUse, ID: ev.ToolID, Name: ev.ToolName})
|
|
a.toolInputs = append(a.toolInputs, &strings.Builder{})
|
|
case SEToolInputJSON:
|
|
if n := len(a.toolInputs); n > 0 {
|
|
a.toolInputs[n-1].WriteString(ev.Text)
|
|
}
|
|
case SEMessageDelta:
|
|
if ev.StopReason != "" {
|
|
a.StopReason = ev.StopReason
|
|
}
|
|
a.Usage.Add(ev.Usage)
|
|
case SEMessageStop:
|
|
a.flushText()
|
|
a.flushThinking()
|
|
}
|
|
}
|
|
|
|
func (a *Accumulator) flushText() {
|
|
if a.textBuf != nil {
|
|
if s := a.textBuf.String(); s != "" {
|
|
a.blocks = append(a.blocks, TextBlock(s))
|
|
}
|
|
a.textBuf = nil
|
|
}
|
|
}
|
|
|
|
func (a *Accumulator) flushThinking() {
|
|
if a.thinkBuf != nil {
|
|
if s := a.thinkBuf.String(); s != "" {
|
|
a.blocks = append(a.blocks, ContentBlock{
|
|
Type: BlockThinking,
|
|
Thinking: s,
|
|
Signature: a.thinkSig,
|
|
})
|
|
}
|
|
a.thinkBuf = nil
|
|
a.thinkSig = ""
|
|
}
|
|
}
|
|
|
|
// Message finalizes and returns the assembled assistant message. Partial tool
|
|
// inputs are validated; malformed JSON falls back to "{}".
|
|
func (a *Accumulator) Message() Message {
|
|
a.flushText()
|
|
a.flushThinking()
|
|
ti := 0
|
|
out := make([]ContentBlock, len(a.blocks))
|
|
for i, b := range a.blocks {
|
|
if b.Type == BlockToolUse {
|
|
raw := strings.TrimSpace(a.toolInputs[ti].String())
|
|
ti++
|
|
if raw == "" || !json.Valid([]byte(raw)) {
|
|
raw = "{}"
|
|
}
|
|
b.Input = json.RawMessage(raw)
|
|
}
|
|
out[i] = b
|
|
}
|
|
return Message{Role: RoleAssistant, Content: out}
|
|
}
|