Repository navigation
Expand file tree
/
Copy pathzcode_system.go
More file actions
127 lines (116 loc) · 3.54 KB
/
Copy pathzcode_system.go
File metadata and controls
127 lines (116 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package protocol
import (
_ "embed"
"encoding/json"
"strings"
"zcode2api/internal/server/fault"
"zcode2api/internal/server/shared"
)
//go:embed zcode_system.json
var zcodeReleaseJSON []byte
type zcodeReleaseProfile struct {
Version string `json:"version"`
MainSystem []any `json:"main_system"`
FinishingWork string `json:"finishing_work"`
MemoryNotes string `json:"memory_notes"`
}
// The main template always includes both sections. Subagent can include each independently.
var zcodeRelease = func() zcodeReleaseProfile {
var release zcodeReleaseProfile
if err := json.Unmarshal(zcodeReleaseJSON, &release); err != nil {
panic(err)
}
if release.Version == "" || len(release.MainSystem) != 3 {
panic("zcode_system.json must contain a version and exactly three main system blocks")
}
dynamic := shared.Obj(release.MainSystem[2])
dynamic["text"] = strings.Join([]string{shared.Str(dynamic["text"]), release.FinishingWork, release.MemoryNotes}, "\n\n")
return release
}()
func ZCodeVersion() string {
return zcodeRelease.Version
}
const (
TemplateMain = "main"
TemplateSubagent = "subagent"
MaxCustomTemplateBytes = 256 << 10
)
type SystemTemplate struct {
Mode string
Custom string
InsertEnabled bool
HarnessEnabled bool
FilesEnabled bool
CommentsEnabled bool
FinishingWorkEnabled bool
MemoryNotesEnabled bool
}
func NewSystemTemplate(mode, custom string) (SystemTemplate, error) {
if mode == "" {
mode = TemplateMain
}
template := SystemTemplate{Mode: mode, Custom: custom}
switch mode {
case TemplateMain, TemplateSubagent:
default:
return template, fault.Invalid("模板模式仅支持 main 或 subagent")
}
if len(custom) > MaxCustomTemplateBytes {
return template, fault.Invalid("自定义模板不能超过 256 KiB")
}
return template, nil
}
func (t SystemTemplate) SessionType() string {
if t.Mode == TemplateSubagent {
return "subagent"
}
return "main"
}
func (t SystemTemplate) QuerySource() string {
if t.SessionType() == "subagent" {
return "subagent"
}
return "main_turn"
}
func upstreamSystemBlocks(client []any, template SystemTemplate) ([]any, error) {
prefix := zcodeRelease.MainSystem
if template.SessionType() == "subagent" {
// Identity is fixed; disabling every optional section leaves an empty body.
prefix = []any{
shared.M{"type": "text", "text": zcodeSubagentIdentity},
shared.M{"type": "text", "text": subagentSystem(template)},
}
}
system := append(append([]any{}, prefix...), client...)
if len(system) == len(prefix) {
return system, nil
}
mergeAt := len(prefix) - 1
texts := []string{}
var control any
for _, value := range system[mergeAt:] {
block := shared.Obj(value)
if shared.Str(block["type"]) != "text" {
return nil, fault.Invalid("系统前缀之后仅支持普通 text 系统块")
}
for key := range block {
if key != "type" && key != "text" && key != "cache_control" {
return nil, fault.Invalid("系统块存在无法合并的字段")
}
}
if text := shared.Str(block["text"]); template.Mode != TemplateSubagent || text != "" {
texts = append(texts, text)
}
if block["cache_control"] != nil {
if control != nil && shared.JSON(control) != shared.JSON(block["cache_control"]) {
return nil, fault.Invalid("不能保留多个不同的 system 缓存策略")
}
control = block["cache_control"]
}
}
last := shared.M{"type": "text", "text": strings.Join(texts, "\n\n")}
if control != nil {
last["cache_control"] = control
}
return append(system[:mergeAt], last), nil
}