AddMessage adds a message to a session at the next position. Returns the ID of the created message item.
(ctx context.Context, sessionID string, msg *Message)
| 1023 | // AddMessage adds a message to a session at the next position. |
| 1024 | // Returns the ID of the created message item. |
| 1025 | func (s *SQLiteSessionStore) AddMessage(ctx context.Context, sessionID string, msg *Message) (int64, error) { |
| 1026 | if sessionID == "" { |
| 1027 | return 0, ErrEmptyID |
| 1028 | } |
| 1029 | |
| 1030 | msgJSON, err := json.Marshal(msg.Message) |
| 1031 | if err != nil { |
| 1032 | return 0, fmt.Errorf("marshaling message: %w", err) |
| 1033 | } |
| 1034 | |
| 1035 | // Insert a new message at the next position |
| 1036 | result, err := s.db.ExecContext(ctx, |
| 1037 | `INSERT INTO session_items (session_id, position, item_type, agent_name, message_json, implicit) |
| 1038 | VALUES (?, (SELECT COALESCE(MAX(position), -1) + 1 FROM session_items WHERE session_id = ?), 'message', ?, ?, ?)`, |
| 1039 | sessionID, sessionID, msg.AgentName, string(msgJSON), msg.Implicit) |
| 1040 | if err != nil { |
| 1041 | return 0, fmt.Errorf("inserting message: %w", err) |
| 1042 | } |
| 1043 | |
| 1044 | id, err := result.LastInsertId() |
| 1045 | if err != nil { |
| 1046 | return 0, fmt.Errorf("getting last insert id: %w", err) |
| 1047 | } |
| 1048 | |
| 1049 | slog.DebugContext(ctx, "[STORE] AddMessage", "session_id", sessionID, "message_id", id, "role", msg.Message.Role, "agent", msg.AgentName) |
| 1050 | return id, nil |
| 1051 | } |
| 1052 | |
| 1053 | // UpdateMessage updates an existing message by its ID. |
| 1054 | func (s *SQLiteSessionStore) UpdateMessage(ctx context.Context, messageID int64, msg *Message) error { |