CREATE TABLE IF NOT EXISTS library_thread_messages (
  id SERIAL PRIMARY KEY,
  thread_id TEXT NOT NULL REFERENCES library_threads(id) ON DELETE CASCADE,
  message_id TEXT NOT NULL,
  backend_id TEXT NOT NULL DEFAULT '',
  query TEXT NOT NULL,
  created_at TIMESTAMP DEFAULT NOW() NOT NULL,
  response_blocks JSONB DEFAULT '[]',
  status TEXT DEFAULT 'answering'
);

CREATE UNIQUE INDEX IF NOT EXISTS idx_thread_messages_thread_message
  ON library_thread_messages(thread_id, message_id);
