Repository navigation
Expand file tree
/
Copy pathknowledge.go
More file actions
208 lines (199 loc) · 5.63 KB
/
Copy pathknowledge.go
File metadata and controls
208 lines (199 loc) · 5.63 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Package knowledge owns opt-in, profile-wide knowledge capture and browsing.
// Capture is local and model-free. Flush runs the native compiler.
package knowledge
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
_ "modernc.org/sqlite"
"github.com/Agent-Field/codeaf/internal/config"
"path/filepath"
)
const Model = config.DefaultKnowledgeModel
var ErrDisabled = errors.New("knowledge is not enabled for this profile")
type Source struct {
Project string `json:"project,omitempty"`
ID string `json:"id"`
Session string `json:"session"`
Role string `json:"role"`
TS string `json:"ts"`
Text string `json:"text"`
Kind string `json:"kind"`
}
type Link struct {
ID string `json:"id"`
Kind string `json:"kind"`
Reason string `json:"reason"`
}
type InlineLink struct {
Start int `json:"start"`
End int `json:"end"`
ID string `json:"id"`
Label string `json:"label,omitempty"`
}
type Evidence struct {
Source string `json:"source"`
Quote string `json:"quote"`
TS string `json:"ts,omitempty"`
Role string `json:"role,omitempty"`
}
type Paragraph struct {
Text string `json:"text"`
Claims []string `json:"claims"`
Evidence []Evidence `json:"evidence"`
InlineLinks []InlineLink `json:"inline_links"`
}
type Section struct {
Heading string `json:"heading"`
Paragraphs []Paragraph `json:"paragraphs"`
}
type Conflict struct {
Claims []string `json:"claims"`
Reason string `json:"reason"`
State string `json:"state"`
}
type Page struct {
Conflicts []Conflict `json:"conflicts,omitempty"`
OriginalEvidence []Evidence `json:"original_evidence,omitempty"`
Revision string `json:"revision,omitempty"`
HumanEditedAt string `json:"human_edited_at,omitempty"`
HumanEdited bool `json:"human_edited,omitempty"`
Markdown string `json:"manual_markdown,omitempty"`
ID string `json:"id"`
Title string `json:"title"`
Scope string `json:"scope"`
Kind string `json:"kind"`
Folder []string `json:"folder"`
Sections []Section `json:"sections"`
Links []Link `json:"links"`
Backlinks []Link `json:"backlinks"`
Aliases []string `json:"aliases"`
Claims []string `json:"claims"`
Evidence []Evidence `json:"evidence,omitempty"`
NeedsEditorialUpdate bool `json:"needs_editorial_update"`
LearningState string `json:"learning_state"`
}
type Snapshot struct {
Revision string
Build BuildStatus
Enabled bool
Pages []Page
Pending int
NeedsUpdate bool
LastError string
CostUSD float64
Model string
}
type Result struct {
Page Page
Snippet string
}
type Vault struct{ root, profile string }
func hash(s string) string { sum := sha256.Sum256([]byte(s)); return hex.EncodeToString(sum[:]) }
func Open(profile string) *Vault {
return &Vault{root: filepath.Join(profile, "knowledge"), profile: profile}
}
func (v *Vault) Directory() string { return v.root }
func materializePages(vault document) ([]Page, error) {
b, err := json.Marshal(vault)
if err != nil {
return nil, err
}
var pages []Page
var data struct {
Notes []Page `json:"notes"`
Claims []struct {
ID string `json:"id"`
Text string `json:"text"`
Kind string `json:"kind"`
Status string `json:"status"`
Evidence []Evidence `json:"evidence"`
} `json:"claims"`
Sources map[string]struct {
Role string `json:"role"`
TS string `json:"ts"`
} `json:"sources"`
}
if e := json.Unmarshal(b, &data); e != nil {
return nil, e
}
pages = data.Notes
claimIndex := make(map[string]int, len(data.Claims))
for i, c := range data.Claims {
claimIndex[c.ID] = i
}
for i := range pages {
p := &pages[i]
seen := map[string]bool{}
add := func(e Evidence) {
key := e.Source + "\x00" + e.Quote
if seen[key] {
return
}
seen[key] = true
if src, ok := data.Sources[e.Source]; ok {
e.Role = src.Role
e.TS = src.TS
}
p.Evidence = append(p.Evidence, e)
}
for _, e := range p.OriginalEvidence {
add(e)
}
for si := range p.Sections {
for pi := range p.Sections[si].Paragraphs {
para := &p.Sections[si].Paragraphs[pi]
cited := map[string]bool{}
var evidence []Evidence
cite := func(e Evidence) {
key := e.Source + "\x00" + e.Quote
if cited[key] {
return
}
cited[key] = true
if source, ok := data.Sources[e.Source]; ok {
e.Role = source.Role
e.TS = source.TS
}
evidence = append(evidence, e)
add(e)
}
for _, e := range para.Evidence {
cite(e)
}
for _, id := range para.Claims {
if index, ok := claimIndex[id]; ok && data.Claims[index].Status == "active" {
for _, e := range data.Claims[index].Evidence {
cite(e)
}
}
}
para.Evidence = evidence
}
}
var paragraphs []Paragraph
for _, id := range p.Claims {
index, ok := claimIndex[id]
if !ok {
continue
}
c := data.Claims[index]
if c.Status != "active" {
continue
}
for _, e := range c.Evidence {
add(e)
}
paragraphs = append(paragraphs, Paragraph{Text: "[" + c.Kind + "] " + c.Text, Claims: []string{c.ID}, Evidence: c.Evidence})
}
if p.Kind != "subject" && len(p.Sections) == 0 && len(paragraphs) > 0 {
p.Sections = []Section{{Heading: "Recorded knowledge", Paragraphs: paragraphs}}
}
}
return pages, nil
}
func (v *Vault) Flush(ctx context.Context, key string, force bool) error {
return v.compile(ctx, key, force)
}