Repository navigation
Expand file tree
/
Copy pathopencode.go
More file actions
1573 lines (1490 loc) · 52.2 KB
/
Copy pathopencode.go
File metadata and controls
1573 lines (1490 loc) · 52.2 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package sqlite
import (
"context"
"crypto/rand"
"database/sql"
"encoding/json"
"errors"
"fmt"
neturl "net/url"
"os"
"path/filepath"
"slices"
"strings"
"time"
"github.com/inference-sh/agentprotocol/transcript"
)
func init() {
transcript.Register("opencode", Opencode)
transcript.Register("kilo", Kilo)
}
// Opencode is the opencode session store, and Kilo the store of Kilo Code,
// which forked opencode's schema. Both keep every session in one database
// with session, message and part tables whose payloads are JSON in a `data`
// column; a session belongs to a project keyed by its worktree directory.
var (
Opencode transcript.Codec = openCodec{"opencode", ".local/share/opencode/opencode.db"}
Kilo transcript.Codec = openCodec{"kilo", ".local/share/kilo/kilo.db"}
)
type openCodec struct{ agent, rel string }
func (c openCodec) Open(home string) (transcript.Store, error) {
return &openStore{agent: c.agent, path: filepath.Join(home, c.rel)}, nil
}
type openStore struct{ agent, path string }
// openMessage is the message `data` payload. Only the fields the codec reads
// or writes are named; Vendor keeps the rest for a same-agent write.
type openMessage struct {
Role string `json:"role"`
ParentID string `json:"parentID,omitempty"`
Time openTime `json:"time"`
Agent string `json:"agent,omitempty"`
ProviderID string `json:"providerID,omitempty"`
ModelID string `json:"modelID,omitempty"`
// Model is the model a prompt was sent with.
Model json.RawMessage `json:"model,omitempty"`
// Summary is true on the assistant message a compaction wrote. A user
// message keeps an object of file diffs under the same key.
Summary json.RawMessage `json:"summary,omitempty"`
Finish string `json:"finish,omitempty"`
Error *openError `json:"error,omitempty"`
}
type openError struct {
Name string `json:"name"`
}
type openTime struct {
Created int64 `json:"created,omitempty"`
Completed int64 `json:"completed,omitempty"`
}
// openPart is the part `data` payload.
type openPart struct {
ID string `json:"-"`
Type string `json:"type"`
Text string `json:"text,omitempty"`
Synthetic bool `json:"synthetic,omitempty"`
Ignored bool `json:"ignored,omitempty"`
Metadata json.RawMessage `json:"metadata,omitempty"`
Tool string `json:"tool,omitempty"`
CallID string `json:"callID,omitempty"`
State *openToolState `json:"state,omitempty"`
// TailStartID is set on a compaction part to the first message the
// compaction keeps after its summary.
TailStartID string `json:"tail_start_id,omitempty"`
openFile
}
// openFile is what a file part holds, and what each of a tool's attachments
// holds: its type, its name, and a URL that is a data: URL once opencode has
// read the file (prompt.ts resolvePart) or the file: URL of a text file or
// directory it inlined as text instead.
type openFile struct {
Mime string `json:"mime,omitempty"`
Filename string `json:"filename,omitempty"`
URL string `json:"url,omitempty"`
}
type openToolState struct {
Status string `json:"status"`
Input json.RawMessage `json:"input,omitempty"`
Output string `json:"output,omitempty"`
Error string `json:"error,omitempty"`
Metadata json.RawMessage `json:"metadata,omitempty"`
Attachments []openFile `json:"attachments,omitempty"`
Time struct {
// Compacted is set when a prune cleared the output for the model.
Compacted int64 `json:"compacted,omitempty"`
} `json:"time"`
}
// openRevert is the session's revert column: the message, and optionally
// the part within it, from which the person undid the conversation.
type openRevert struct {
MessageID string `json:"messageID"`
PartID string `json:"partID,omitempty"`
}
// openVendor carries what a write needs from a session read from the store:
// the agent, provider and model its messages ran with, which a message
// appended to it must name too.
type openVendor struct {
Agent string
ProviderID string
ModelID string
}
func (st *openStore) List(ctx context.Context, cwd string) ([]transcript.Info, error) {
if _, err := os.Stat(st.path); err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
db, done, err := openRO(st.path)
if err != nil {
return nil, err
}
defer done()
defer db.Close()
cols, err := openColumns(ctx, db, "session")
if err != nil {
return nil, fmt.Errorf("opencode: list: %w", err)
}
// opencode lists the sessions a person started: a subagent's session
// has a parent and an archived one a time_archived (session.ts listGlobal
// with roots, and the archived filter every listing applies).
var where []string
args := []any{}
if cols["parent_id"] {
where = append(where, "parent_id IS NULL")
}
if cols["time_archived"] {
where = append(where, "time_archived IS NULL")
}
if cwd != "" {
where = append(where, "directory = ?")
args = append(args, cwd)
}
q := "SELECT id, directory, title, time_updated FROM session"
if len(where) > 0 {
q += " WHERE " + strings.Join(where, " AND ")
}
rows, err := db.QueryContext(ctx, q, args...)
if err != nil {
return nil, fmt.Errorf("opencode: list: %w", err)
}
defer rows.Close()
var out []transcript.Info
for rows.Next() {
var id, dir, title string
var updated int64
if err := rows.Scan(&id, &dir, &title, &updated); err != nil {
return nil, err
}
out = append(out, transcript.Info{ID: id, CWD: dir, Title: title, Updated: time.UnixMilli(updated).UTC(), Path: st.path})
}
transcript.SortNewest(out)
return out, rows.Err()
}
// openRow is one message as read, with its parts.
type openRow struct {
id string
raw json.RawMessage
m openMessage
parts []openPart
}
func (st *openStore) Read(ctx context.Context, id string) (*transcript.Session, error) {
if _, err := os.Stat(st.path); errors.Is(err, os.ErrNotExist) {
return nil, transcript.ErrNotFound
}
db, done, err := openRO(st.path)
if err != nil {
return nil, err
}
defer done()
defer db.Close()
s := &transcript.Session{ID: id, Agent: st.agent}
var created, updated int64
err = db.QueryRowContext(ctx, "SELECT directory, title, time_created, time_updated FROM session WHERE id = ?", id).
Scan(&s.CWD, &s.Title, &created, &updated)
if err == sql.ErrNoRows {
return nil, transcript.ErrNotFound
}
if err != nil {
return nil, fmt.Errorf("opencode: read session: %w", err)
}
s.Created, s.Updated = time.UnixMilli(created).UTC(), time.UnixMilli(updated).UTC()
rev, err := openRevertOf(ctx, db, id)
if err != nil {
return nil, err
}
vendor := &openVendor{}
s.Vendor = vendor
// Parts, grouped by message.
partRows, err := db.QueryContext(ctx, "SELECT id, message_id, data FROM part WHERE session_id = ? ORDER BY time_created, id", id)
if err != nil {
return nil, fmt.Errorf("opencode: read parts: %w", err)
}
defer partRows.Close()
partsByMsg := map[string][]openPart{}
for partRows.Next() {
var partID, msgID, data string
if err := partRows.Scan(&partID, &msgID, &data); err != nil {
return nil, err
}
var p openPart
if err := json.Unmarshal([]byte(data), &p); err != nil {
return nil, fmt.Errorf("opencode: part: %w", err)
}
p.ID = partID
partsByMsg[msgID] = append(partsByMsg[msgID], p)
}
if err := partRows.Err(); err != nil {
return nil, err
}
msgRows, err := db.QueryContext(ctx, "SELECT id, data FROM message WHERE session_id = ? ORDER BY time_created, id", id)
if err != nil {
return nil, fmt.Errorf("opencode: read messages: %w", err)
}
defer msgRows.Close()
var msgs []openRow
for msgRows.Next() {
var msgID, data string
if err := msgRows.Scan(&msgID, &data); err != nil {
return nil, err
}
r := openRow{id: msgID, raw: json.RawMessage(data), parts: partsByMsg[msgID]}
if err := json.Unmarshal([]byte(data), &r.m); err != nil {
return nil, fmt.Errorf("opencode: message: %w", err)
}
if r.m.Agent != "" {
vendor.Agent = r.m.Agent
}
if r.m.Role == "assistant" && r.m.ProviderID != "" {
vendor.ProviderID, vendor.ModelID = r.m.ProviderID, r.m.ModelID
s.Model = r.m.ModelID
}
msgs = append(msgs, r)
}
if err := msgRows.Err(); err != nil {
return nil, err
}
resume, err := openResumeModel(ctx, db, id, msgs, st.agent == "kilo")
if err != nil {
return nil, err
}
s.Entries = openEntries(msgs, rev, st.agent == "kilo", resume)
return s, nil
}
// openView is one part as the person and the model each receive it.
type openView struct {
id string
blocks []transcript.Block
model, user bool
// sent is what the model gets of the part when that is not blocks;
// empty when it gets nothing, nil when it gets blocks.
sent []transcript.Block
}
// addTo appends the view to an entry: its blocks to what the person sees,
// and what the model gets of it to the entry's ModelContent once any part
// of the entry reaches the model differently.
func (v openView) addTo(e *transcript.Entry) {
if v.sent != nil && e.ModelContent == nil {
e.ModelContent = append([]transcript.Block{}, e.Content...)
}
if e.ModelContent != nil {
if v.sent != nil {
e.ModelContent = append(e.ModelContent, v.sent...)
} else {
e.ModelContent = append(e.ModelContent, v.blocks...)
}
}
e.Content = append(e.Content, v.blocks...)
}
// openEntries turns messages into entries the way opencode builds the
// model's context on a prompt: the revert cleanup that runs first
// (revert.ts cleanup), then filterCompacted and toModelMessages
// (message-v2.ts). What the person sees follows the TUI. A message whose
// parts differ in who they are for, such as a prompt with a synthetic
// reminder appended, becomes one entry per run of parts with the same
// audience: the first keeps the message id, the others take the id of
// their first part.
func openEntries(msgs []openRow, rev *openRevert, kilo bool, resume openModelRef) []transcript.Entry {
views := make([][]openView, len(msgs))
for i, r := range msgs {
views[i] = openViews(r, kilo, resume)
}
// The TUI hides every message from the revert point on, and the next
// prompt deletes them before the model sees anything. With a part named,
// the message holding it keeps its earlier parts.
live, reverted := len(msgs), len(msgs)
if rev != nil {
for i, r := range msgs {
if r.id != rev.MessageID {
continue
}
live, reverted = i, i
for j := i; j < len(msgs); j++ {
for k := range views[j] {
views[j][k].user = false
if j > i || rev.PartID == "" {
views[j][k].model = false
}
}
}
if rev.PartID == "" {
break
}
live = i + 1
if !slices.ContainsFunc(r.parts, func(p openPart) bool { return p.ID == rev.PartID }) {
break
}
for k := range views[i] {
if views[i][k].id >= rev.PartID {
views[i][k].model = false
}
}
if kilo && r.m.Role == "assistant" {
// Kilo clears the provider error of the message it keeps
// part of (kilo revert.ts cleanup), so its parts are sent.
msgs[i].m.Error = nil
}
break
}
}
// An assistant message that failed is not sent, unless it was aborted
// after producing something (message-v2.ts toModelMessages).
unsent := make([]bool, len(msgs))
for i, r := range msgs {
unsent[i] = i >= live || r.m.Role == "assistant" && r.m.Error != nil && !openAbortedWithContent(r)
if unsent[i] {
for k := range views[i] {
views[i][k].model = false
}
}
}
compaction, carrier := openCompaction(msgs[:live], views)
var out []transcript.Entry
for i, r := range msgs {
start := len(out)
for _, v := range views[i] {
if len(v.blocks) == 0 {
continue
}
if n := len(out); n > start {
last := &out[n-1]
if last.Audience == openAudience(v.model, v.user) {
v.addTo(last)
continue
}
}
id := r.id
if len(out) > start {
id = v.id
}
e := openEntry(r, id, v.model, v.user, nil)
v.addTo(&e)
out = append(out, e)
}
// An answer whose every part the model is given as nothing, such as
// one that only reasoned, is left out of the request
// (toModelMessages keeps a message only with a part to send).
for k := start; k < len(out); k++ {
if e := &out[k]; e.ModelContent != nil && len(e.ModelContent) == 0 {
e.Audience = openAudience(false, e.Audience.User())
e.ModelContent = nil
}
}
if len(out) == start {
// A message with no part the entry can hold, like an answer that
// only stepped, is still a message.
out = append(out, openEntry(r, r.id, !unsent[i], i < reverted, nil))
}
if i == carrier {
out[start].Compaction = compaction
}
}
return out
}
func openEntry(r openRow, id string, model, user bool, blocks []transcript.Block) transcript.Entry {
// opencode's parentID names the prompt an answer replies to, not a
// previous row: the session is a list in store order, so it is not an
// entry link.
e := transcript.Entry{ID: id, Role: transcript.Role(r.m.Role), Content: blocks, Audience: openAudience(model, user), Raw: r.raw}
if r.m.Time.Created > 0 {
e.Time = time.UnixMilli(r.m.Time.Created).UTC()
}
return e
}
func openAudience(model, user bool) transcript.Audience {
switch {
case model && user:
return transcript.AudienceAll
case model:
return transcript.AudienceModel
case user:
return transcript.AudienceUser
}
return transcript.AudienceNone
}
func openAbortedWithContent(r openRow) bool {
if r.m.Error.Name != "MessageAbortedError" {
return false
}
return slices.ContainsFunc(r.parts, func(p openPart) bool { return p.Type != "step-start" && p.Type != "reasoning" })
}
// openCompaction finds the compaction filterCompacted applies: the newest
// compaction prompt whose summary finished without error. It returns what
// that compaction does and the index of the message carrying it, which is
// the summary filterCompacted places right after the prompt; -1 when there
// is none. The model then gets the prompt (as the fixed question opencode
// sends for it) and the summary, the kept tail from tail_start_id up to the
// prompt, and everything after the summary. The prompt shows the person
// only a divider, so it is for nobody outside the summary.
func openCompaction(msgs []openRow, views [][]openView) (*transcript.Compaction, int) {
completed := map[string]bool{}
for i := len(msgs) - 1; i >= 0; i-- {
r := msgs[i]
if r.m.Role == "assistant" && string(r.m.Summary) == "true" && r.m.Finish != "" && r.m.Error == nil {
completed[r.m.ParentID] = true
continue
}
if r.m.Role != "user" || !completed[r.id] {
continue
}
k := slices.IndexFunc(r.parts, func(p openPart) bool { return p.Type == "compaction" })
if k < 0 {
continue
}
c := &transcript.Compaction{}
if tail := r.parts[k].TailStartID; tail != "" && tail != r.id {
// A tail that is not an earlier message leaves filterCompacted
// looking for it through the whole session, which then reaches
// the model uncut.
if !slices.ContainsFunc(msgs[:i], func(m openRow) bool { return m.id == tail }) {
return nil, -1
}
c.Keep = tail
}
s := -1
for j := i + 1; j < len(msgs); j++ {
if m := msgs[j]; m.m.Role == "assistant" && string(m.m.Summary) == "true" && m.m.ParentID == r.id {
s = j
break
}
}
if s < 0 {
return nil, -1
}
c.Summary = append(c.Summary, openModelView(msgs[i], views[i])...)
c.Summary = append(c.Summary, openModelView(msgs[s], views[s])...)
for k := range views[i] {
views[i][k].model, views[i][k].user = false, false
}
for k := range views[s] {
views[s][k].model = false
}
return c, s
}
return nil, -1
}
// openModelView is a message as the model receives it, one entry, for
// everyone when the person is shown it too.
func openModelView(r openRow, views []openView) []transcript.Entry {
e := openEntry(r, r.id, true, false, nil)
shown := false
for _, v := range views {
if v.model {
v.addTo(&e)
shown = shown || v.user && len(v.blocks) > 0
}
}
if len(e.Content) == 0 || e.ModelContent != nil && len(e.ModelContent) == 0 {
return nil
}
e.Audience = openAudience(true, shown)
e.Raw = nil
return []transcript.Entry{e}
}
// kiloTransient is the part metadata key Kilo marks UI-only text with.
const kiloTransient = "kilocode.lifecycle"
// openViews maps each part of a message to its blocks and who gets them.
func openViews(r openRow, kilo bool, resume openModelRef) []openView {
var out []openView
for _, p := range r.parts {
v := openView{id: p.ID, model: true, user: true}
text := func(t string) {
v.blocks = []transcript.Block{{Kind: transcript.BlockText, Text: t}}
}
switch r.m.Role {
case "user":
switch p.Type {
case "text":
// The model is not given ignored text; the TUI does not show
// synthetic text (message-v2.ts toModelMessages, TUI
// UserMessage).
if p.Text != "" {
text(p.Text)
}
v.model, v.user = !p.Ignored, !p.Synthetic
case "file":
// A text file or a directory reaches the model as the
// synthetic text opencode read it into; the part itself is
// only shown (message-v2.ts toModelMessages). The file is
// the person's whether or not the TUI draws it (it does not
// beneath a prompt without typed text): as a model-only
// entry it would read as opencode's own context and be
// left behind when the session moves.
v.blocks = []transcript.Block{mediaBlock(p.Mime, p.URL, p.Filename)}
v.model = p.Mime != "text/plain" && p.Mime != "application/x-directory"
case "compaction":
text("What did we do so far?")
v.user = false
case "subtask":
text("The following tool was executed by the user")
v.user = false
}
case "assistant":
switch p.Type {
case "text":
if p.Text != "" {
text(p.Text)
}
if kilo && (p.Ignored || openTransient(p)) {
// Kilo keeps local UI notices out of future prompts
// (kilo message-v2.ts toModelMessages).
v.model = false
}
case "reasoning":
v.blocks = []transcript.Block{{Kind: transcript.BlockReasoning, Text: p.Text}}
v.sent = openReasoningSent(p, r.m, resume)
case "tool":
v.blocks = openToolBlocks(p, kilo)
}
}
out = append(out, v)
}
return out
}
// openResumeModel is the model opencode resumes a session on over ACP: the
// model the session records, else that of its last prompt that names one,
// else that of its last answer (acp/service.ts restoreSession,
// restoreFromMessages). Kilo records none on the session and starts from
// the messages. Either falls back to the configured default when that
// model is gone; the reader cannot see the configuration and assumes it is
// still there.
func openResumeModel(ctx context.Context, db *sql.DB, id string, msgs []openRow, kilo bool) (openModelRef, error) {
if !kilo {
cols, err := openColumns(ctx, db, "session")
if err != nil {
return openModelRef{}, err
}
if cols["model"] {
var raw sql.NullString
if err := db.QueryRowContext(ctx, "SELECT model FROM session WHERE id = ?", id).Scan(&raw); err != nil {
return openModelRef{}, fmt.Errorf("opencode: read session: %w", err)
}
var m struct {
ID string `json:"id"`
ProviderID string `json:"providerID"`
}
if raw.Valid && json.Unmarshal([]byte(raw.String), &m) == nil && m.ID != "" && m.ProviderID != "" {
return openModelRef{ProviderID: m.ProviderID, ModelID: m.ID}, nil
}
}
}
for i := len(msgs) - 1; i >= 0; i-- {
var m openModelRef
if msgs[i].m.Role == "user" && json.Unmarshal(msgs[i].m.Model, &m) == nil && m.ProviderID != "" && m.ModelID != "" {
return m, nil
}
}
for i := len(msgs) - 1; i >= 0; i-- {
if m := msgs[i].m; m.ProviderID != "" && m.ModelID != "" {
return openModelRef{ProviderID: m.ProviderID, ModelID: m.ModelID}, nil
}
}
return openModelRef{}, nil
}
// openReplayHandles names, for the providers whose SDK replays reasoning
// only by what the provider issued with it, the part metadata keys that
// carry it: OpenAI's Responses API an item id or encrypted content, and
// Anthropic and Bedrock a signature or redacted data. Without one the SDK
// skips the part ("Non-OpenAI reasoning parts are not supported",
// "unsupported reasoning metadata"; opencode's transform drops unsigned
// Bedrock reasoning itself). The other SDKs send the text: OpenAI-compatible
// ones as reasoning_content, Google as a thought part.
var openReplayHandles = map[string][]string{
"openai": {"itemId", "reasoningEncryptedContent"},
"azure": {"itemId", "reasoningEncryptedContent"},
"anthropic": {"signature", "redactedData"},
"amazon-bedrock": {"signature", "redactedContent", "redactedData"},
}
// openReasoningSent is what the model gets of a reasoning part, nil when
// it gets the part as it is (message-v2.ts toModelMessages). An answer
// that ran on another model than the one the session resumes on gives its
// reasoning as plain text, when there is any; on the same model the
// reasoning goes to the provider with the part's metadata, and the
// provider's SDK decides (openReplayHandles).
func openReasoningSent(p openPart, m openMessage, resume openModelRef) []transcript.Block {
if m.ProviderID != resume.ProviderID || m.ModelID != resume.ModelID {
if strings.TrimSpace(p.Text) == "" {
return []transcript.Block{}
}
return []transcript.Block{{Kind: transcript.BlockText, Text: p.Text}}
}
keys, signed := openReplayHandles[m.ProviderID]
if !signed {
if p.Text == "" {
return []transcript.Block{}
}
return nil
}
var meta map[string]map[string]json.RawMessage
_ = json.Unmarshal(p.Metadata, &meta)
for _, ns := range meta {
for _, k := range keys {
if v, ok := ns[k]; ok && string(v) != "null" {
return nil
}
}
}
return []transcript.Block{}
}
func openTransient(p openPart) bool {
var meta map[string]json.RawMessage
if json.Unmarshal(p.Metadata, &meta) != nil {
return false
}
var v string
return json.Unmarshal(meta[kiloTransient], &v) == nil && v == "transient"
}
// openToolBlocks splits a tool part into its call and, once there is one,
// its result, as toModelMessages sends them. A call still pending or
// running when the session stopped is answered as interrupted, and one
// interrupted by an abort sends the output it had. The images and files a
// completed call returned follow its result.
func openToolBlocks(p openPart, kilo bool) []transcript.Block {
use := transcript.Block{Kind: transcript.BlockToolUse, ToolID: p.CallID, Name: p.Tool}
if p.State == nil {
return []transcript.Block{use}
}
use.Input = p.State.Input
res := transcript.Block{Kind: transcript.BlockToolResult, ToolID: p.CallID, Name: p.Tool, Status: transcript.StatusOK}
switch p.State.Status {
case "completed":
// A pruned output (state.time.compacted) reaches the model as
// "[Old tool result content cleared]"; the person still sees it
// all, and the entry keeps it.
res.Text = p.State.Output
return append([]transcript.Block{use, res}, openAttachments(p, kilo)...)
case "error":
var meta struct {
Interrupted bool `json:"interrupted"`
Output json.RawMessage `json:"output"`
}
var out string
if json.Unmarshal(p.State.Metadata, &meta) == nil && meta.Interrupted && json.Unmarshal(meta.Output, &out) == nil {
res.Text = out
break
}
res.Text, res.Status = p.State.Error, transcript.StatusError
case "pending", "running":
res.Text, res.Status = "[Tool execution was interrupted]", transcript.StatusError
default:
return []transcript.Block{use}
}
return []transcript.Block{use, res}
}
// openAttachments are the images and files a completed tool call returned,
// as the model is given them: only those held as data: URLs, none once a
// prune cleared the output, and in Kilo none from send_file, whose
// attachments are for delivery to a phone (message-v2.ts toModelMessages
// and toModelOutput). The TUI shows no attachment, so one the model is not
// given is for nobody and stays only in the row. A provider that takes no
// media in a tool result gets them in a user message after the call's; the
// block keeps them with the result either way.
func openAttachments(p openPart, kilo bool) []transcript.Block {
if p.State.Time.Compacted != 0 || kilo && p.Tool == "send_file" {
return nil
}
var out []transcript.Block
for _, a := range p.State.Attachments {
if !strings.HasPrefix(a.URL, "data:") || !strings.Contains(a.URL, ",") {
continue
}
b := mediaBlock(a.Mime, a.URL, a.Filename)
b.ToolID = p.CallID
out = append(out, b)
}
return out
}
// mediaBlock is an image or a file an agent keeps as a URL: the bytes of a
// data: URL, or else the URL as the block's URI. mediaType is the type the
// agent records beside the URL, if any; the data: URL's own names it
// otherwise.
func mediaBlock(mediaType, url, name string) transcript.Block {
b := transcript.Block{Kind: transcript.BlockFile, MediaType: mediaType, URI: url, Name: name}
if mt, data, ok := transcript.ParseDataURL(url); ok {
b.URI, b.Data = "", data
if b.MediaType == "" {
b.MediaType = mt
}
}
if strings.HasPrefix(b.MediaType, "image/") {
b.Kind = transcript.BlockImage
}
return b
}
// mediaURL is a block's content as a URL: a data: URL of its bytes, or the
// URI it points at.
func mediaURL(b transcript.Block) string {
if len(b.Data) == 0 {
return b.URI
}
mt := b.MediaType
if mt == "" {
mt = "application/octet-stream"
}
return transcript.DataURL(mt, b.Data)
}
// openRevertOf reads a session's revert state, nil when there is none or
// the store predates it.
func openRevertOf(ctx context.Context, q openQuerier, sessionID string) (*openRevert, error) {
cols, err := openColumns(ctx, q, "session")
if err != nil {
return nil, err
}
if !cols["revert"] {
return nil, nil
}
var raw sql.NullString
if err := q.QueryRowContext(ctx, "SELECT revert FROM session WHERE id = ?", sessionID).Scan(&raw); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return nil, fmt.Errorf("opencode: read revert: %w", err)
}
if !raw.Valid || raw.String == "" || raw.String == "null" {
return nil, nil
}
var r openRevert
if err := json.Unmarshal([]byte(raw.String), &r); err != nil {
return nil, fmt.Errorf("opencode: revert: %w", err)
}
if r.MessageID == "" {
return nil, nil
}
return &r, nil
}
type openQuerier interface {
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}
func openColumns(ctx context.Context, q openQuerier, table string) (map[string]bool, error) {
rows, err := q.QueryContext(ctx, "SELECT name FROM pragma_table_info(?)", table)
if err != nil {
return nil, err
}
defer rows.Close()
out := map[string]bool{}
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
return nil, err
}
out[name] = true
}
return out, rows.Err()
}
// Write persists a session without disturbing anything it read. Entries read
// from the store (Raw set) keep their message and part rows exactly; rows of
// entries the session no longer holds are removed; new entries are inserted
// after them as messages opencode accepts, with every field its message and
// part schemas require. New entries appended to a session under a revert
// first remove the reverted messages, as a prompt does. A session opencode
// does not have yet gets a session row, bound to its directory's project.
func (st *openStore) Write(ctx context.Context, s *transcript.Session) (string, error) {
if s.Agent != st.agent {
s = s.Portable().Lower(openCaps)
}
if err := os.MkdirAll(filepath.Dir(st.path), 0o755); err != nil {
return "", err
}
db, err := openRW(st.path)
if err != nil {
return "", err
}
defer db.Close()
if err := openSchema(ctx, db); err != nil {
return "", err
}
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return "", err
}
defer tx.Rollback()
ids := openIDs{}
now := time.Now()
if s.Created.IsZero() {
s.Created = now
}
exists := false
if s.ID != "" {
var one int
switch err := tx.QueryRowContext(ctx, "SELECT 1 FROM session WHERE id = ?", s.ID).Scan(&one); {
case err == nil:
exists = true
case !errors.Is(err, sql.ErrNoRows):
return "", err
}
} else {
s.ID = ids.next("ses", now, true)
}
if !exists {
if err := insertOpenSession(ctx, tx, s); err != nil {
return "", err
}
}
// Keep what was read; drop rows for entries the session no longer has.
keep := map[string]bool{}
for _, e := range s.Messages() {
if e.Raw != nil {
keep[e.ID] = true
}
}
stale, err := openMessageIDs(ctx, tx, s.ID)
if err != nil {
return "", err
}
for _, id := range stale {
if keep[id] {
continue
}
for _, q := range []string{"DELETE FROM part WHERE message_id = ?", "DELETE FROM message WHERE id = ?"} {
if _, err := tx.ExecContext(ctx, q, id); err != nil {
return "", fmt.Errorf("opencode: remove message: %w", err)
}
}
}
adding := slices.ContainsFunc(s.Messages(), func(e transcript.Entry) bool {
return e.Raw == nil && (e.Role == transcript.RoleUser || e.Role == transcript.RoleAssistant)
})
if adding && exists {
// A prompt into a session under a revert first deletes what was
// reverted; messages appended without doing so would sit behind
// the revert point, hidden, and go with the rest on the next prompt.
if err := openCleanup(ctx, tx, s.ID, st.agent == "kilo", now); err != nil {
return "", err
}
}
lastUser, err := openLastUser(ctx, tx, s.ID)
if err != nil {
return "", err
}
d, err := openDefaults(ctx, tx, s)
if err != nil {
return "", err
}
results := toolResults(s)
at := now
added := false
kilo := st.agent == "kilo"
insert := func(data func(ms int64) any, parts func(msgID string, ms int64) []openPartOut) (string, error) {
at = at.Add(time.Millisecond)
ms := at.UnixMilli()
msgID := ids.next("msg", at, false)
raw, err := json.Marshal(data(ms))
if err != nil {
return "", err
}
if _, err := tx.ExecContext(ctx,
"INSERT INTO message (id, session_id, time_created, time_updated, data) VALUES (?, ?, ?, ?, ?)",
msgID, s.ID, ms, ms, string(raw)); err != nil {
return "", fmt.Errorf("opencode: write message: %w", err)
}
for _, part := range parts(msgID, ms) {
pd, err := json.Marshal(part)
if err != nil {
return "", err
}
if _, err := tx.ExecContext(ctx,
"INSERT INTO part (id, message_id, session_id, time_created, time_updated, data) VALUES (?, ?, ?, ?, ?, ?)",
ids.next("prt", at, false), msgID, s.ID, ms, ms, string(pd)); err != nil {
return "", fmt.Errorf("opencode: write part: %w", err)
}
}
added = true
return msgID, nil
}
user := func(ms int64) any {
return openUser{Role: "user", Time: openTime{Created: ms}, Agent: d.agent, Model: openModelRef{ProviderID: d.provider, ModelID: d.model}}
}
answer := func(parent, agent string, summary bool) func(ms int64) any {
return func(ms int64) any {
return openAssistant{
ParentID: parent, Role: "assistant", Mode: agent, Agent: agent, Summary: summary,
Path: openPath{CWD: s.CWD, Root: s.CWD}, Tokens: openTokens{Cache: openCache{}},
ModelID: d.model, ProviderID: d.provider, Time: openTime{Created: ms, Completed: ms}, Finish: "stop",
}
}
}
// held maps each written entry to the message holding it, a tool result
// to the answer whose tool part holds it, so a compaction can name the
// first message it keeps.
held := map[string]string{}
lastAnswer := ""
retired := openRetired(s.Entries)
for i, e := range s.Entries {
if e.Raw != nil {
continue
}
if e.Role == transcript.RoleOpaque && e.Compaction != nil {
// A compaction is a prompt holding a compaction part and the
// summary answering it (compaction.ts create, process), placed
// after the tail it keeps (message-v2.ts filterCompacted).
tail := openKept(s.Entries[:i], e.Compaction.Keep, held)
auto := false
prompt, err := insert(user, func(string, int64) []openPartOut {
return []openPartOut{{Type: "compaction", Auto: &auto, TailStartID: tail}}
})
if err != nil {
return "", err
}
lastUser = prompt
summary := summaryText(e.Compaction.Summary)
if _, err := insert(answer(prompt, "compaction", true), func(string, int64) []openPartOut {
return []openPartOut{{Type: "text", Text: &summary}}
}); err != nil {
return "", err
}
continue
}
var data func(ms int64) any
// An entry shown and not sent is its text, marked ignored, which
// the model is not given and the TUI shows (message-v2.ts
// toModelMessages). Only a prompt can be one in opencode; Kilo
// also ignores an answer's text. History a later compaction
// retires that ignored text cannot hold (a tool call, an image,
// opencode's answers) is written as it is: the compaction keeps
// it from the model all the same (filterCompacted).
shownOnly := e.Audience == transcript.AudienceUser
if shownOnly && retired[i] && !openIgnorable(e, kilo) {
shownOnly = false
}
if shownOnly && (len(openIgnored(e)) == 0 || e.Role == transcript.RoleAssistant && !kilo) {
continue
}