返回 DeepSeek-Reasonix
history_turn_index.go
根目录 / internal / session / history_turn_index.go
1 package session
2
3 import (
4 "context"
5 "encoding/json"
6 "reasonix/internal/sessioncontent"
7 )
8
9 func indexTurnEvent(ctx context.Context, content *sessioncontent.Store, state *historyBuildState, event Event) error {
10 if event.Kind == "tool/call" || event.Kind == "tool/start" || event.Kind == "tool/result" {
11 payload := event.Payload
12 if event.PayloadRef != nil {
13 var err error
14 payload, err = resolveContentPayload(ctx, content, *event.PayloadRef)
15 if err != nil {
16 return err
17 }
18 }
19 var body struct {
20 ID string `json:"id"`
21 RunState string `json:"runState"`
22 }
23 if err := json.Unmarshal(payload, &body); err != nil {
24 return err
25 }
26 status := body.RunState
27 if event.Kind == "tool/call" {
28 status = "pending"
29 }
30 if event.Kind == "tool/start" {
31 status = "running"
32 }
33 if status == "" {
34 status = "unknown"
35 }
36 if body.ID != "" {
37 if _, err := state.tx.ExecContext(ctx, `INSERT OR REPLACE INTO tool_states(call_id,sequence,state) VALUES(?,?,?)`, body.ID, event.Sequence, status); err != nil {
38 return err
39 }
40 }
41 }
42
43 if state.commitTurn != "" && (event.Kind == "assistant/attempt" || event.Kind == "tool/call") {
44 payload := event.Payload
45 if event.PayloadRef != nil {
46 var err error
47 payload, err = resolveContentPayload(ctx, content, *event.PayloadRef)
48 if err != nil {
49 return err
50 }
51 }
52 var body struct {
53 ID string `json:"id"`
54 Action string `json:"action"`
55 }
56 if err := json.Unmarshal(payload, &body); err != nil {
57 return err
58 }
59 if body.ID != "" && (event.Kind == "tool/call" || body.Action == "begin") {
60 _, err := state.tx.ExecContext(ctx, `INSERT OR IGNORE INTO turn_counts(turn_id,kind,id,sequence) VALUES(?,?,?,?)`, state.commitTurn, event.Kind, body.ID, event.Sequence)
61 return err
62 }
63 }
64 if state.commitTurn != "" {
65 switch event.Kind {
66 case "turn/start":
67 _, err := state.tx.ExecContext(ctx, `INSERT INTO turn_summaries(turn_id,start_sequence,started_at) VALUES(?,?,?) ON CONFLICT(turn_id) DO UPDATE SET start_sequence=excluded.start_sequence,started_at=excluded.started_at`, state.commitTurn, event.Sequence, state.commitTime)
68 return err
69 case "turn/end":
70 _, err := state.tx.ExecContext(ctx, `UPDATE turn_summaries SET end_sequence=?,ended_at=MAX(ended_at,?) WHERE turn_id=?`, event.Sequence, state.commitTime, state.commitTurn)
71 return err
72 }
73 }
74 return nil
75 }
76
76 lines GO