addItemTx inserts a session item within a transaction.
(ctx context.Context, tx *sql.Tx, sessionID string, position int, item Item)
| 1141 | |
| 1142 | // addItemTx inserts a session item within a transaction. |
| 1143 | func (s *SQLiteSessionStore) addItemTx(ctx context.Context, tx *sql.Tx, sessionID string, position int, item Item) error { |
| 1144 | switch { |
| 1145 | case item.Message != nil: |
| 1146 | msgJSON, err := json.Marshal(item.Message.Message) |
| 1147 | if err != nil { |
| 1148 | return fmt.Errorf("marshaling message: %w", err) |
| 1149 | } |
| 1150 | _, err = tx.ExecContext(ctx, |
| 1151 | `INSERT INTO session_items (session_id, position, item_type, agent_name, message_json, implicit) |
| 1152 | VALUES (?, ?, 'message', ?, ?, ?)`, |
| 1153 | sessionID, position, item.Message.AgentName, string(msgJSON), item.Message.Implicit) |
| 1154 | return err |
| 1155 | |
| 1156 | case item.SubSession != nil: |
| 1157 | // Recursively add the sub-session |
| 1158 | subSession := item.SubSession |
| 1159 | subSession.ParentID = sessionID |
| 1160 | |
| 1161 | if err := s.addSessionTx(ctx, tx, subSession); err != nil { |
| 1162 | return fmt.Errorf("inserting nested sub-session: %w", err) |
| 1163 | } |
| 1164 | |
| 1165 | for i, subItem := range subSession.Messages { |
| 1166 | if err := s.addItemTx(ctx, tx, subSession.ID, i, subItem); err != nil { |
| 1167 | return fmt.Errorf("inserting nested sub-session item %d: %w", i, err) |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | _, err := tx.ExecContext(ctx, |
| 1172 | `INSERT INTO session_items (session_id, position, item_type, subsession_id) |
| 1173 | VALUES (?, ?, 'subsession', ?)`, |
| 1174 | sessionID, position, subSession.ID) |
| 1175 | return err |
| 1176 | |
| 1177 | case item.Summary != "": |
| 1178 | _, err := tx.ExecContext(ctx, |
| 1179 | `INSERT INTO session_items (session_id, position, item_type, summary_text, first_kept_entry) |
| 1180 | VALUES (?, ?, 'summary', ?, ?)`, |
| 1181 | sessionID, position, item.Summary, item.FirstKeptEntry) |
| 1182 | return err |
| 1183 | |
| 1184 | case item.Error != nil: |
| 1185 | errJSON, err := json.Marshal(item.Error) |
| 1186 | if err != nil { |
| 1187 | return fmt.Errorf("marshaling error: %w", err) |
| 1188 | } |
| 1189 | _, err = tx.ExecContext(ctx, |
| 1190 | `INSERT INTO session_items (session_id, position, item_type, message_json) |
| 1191 | VALUES (?, ?, 'error', ?)`, |
| 1192 | sessionID, position, string(errJSON)) |
| 1193 | return err |
| 1194 | |
| 1195 | default: |
| 1196 | return nil // Empty item, skip |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | // AddSummary adds a summary item to a session at the next position. |
no test coverage detected