Skip to main content

Endpoint

GET /api/debate/history
Retrieves the authenticated user’s debate history, including complete debate transcripts, synthesis reports, and metadata. Results are sorted by creation date (most recent first).

Authentication

Requires valid NextAuth session. Returns 401 Unauthorized if not authenticated.

Request

No query parameters or request body required. The endpoint automatically filters debates by the authenticated user’s ID.

Example Request

curl -X GET https://your-domain.com/api/debate/history \
  -H "Cookie: next-auth.session-token=<token>"

Response

Returns a JSON array of debate objects, limited to the 20 most recent debates.

Response Fields

debates
array
Array of debate objects

Example Response

[
  {
    "id": "debate_1709510400000_abc123",
    "topic": "远程工作是否应该成为主流?",
    "userProfile": {
      "id": "user_123",
      "name": "张三",
      "avatar": "https://example.com/avatar.jpg",
      "bio": "科技从业者,关注未来工作方式"
    },
    "opponentProfile": {
      "id": "opponent_conservative",
      "name": "李明",
      "avatar": "https://example.com/opponent-avatar.jpg",
      "bio": "传统管理专家"
    },
    "messages": [
      {
        "role": "user",
        "name": "张三",
        "content": "我认为远程工作能够提高效率,减少通勤时间,让员工有更好的工作生活平衡。",
        "timestamp": 1709510400000
      },
      {
        "role": "opponent",
        "name": "李明",
        "content": "但是远程工作会降低团队凝聚力,沟通效率也会受到影响。",
        "timestamp": 1709510410000
      }
    ],
    "synthesis": {
      "consensus": [
        "远程工作确实存在沟通挑战",
        "不同行业适用性不同"
      ],
      "disagreements": [
        "效率提升是否显著",
        "团队文化能否维持"
      ],
      "winner": "user",
      "winnerReason": "提供了更多数据支持和实际案例,论证更充分",
      "conclusion": "远程工作的成功取决于组织文化、工具支持和管理方式的配合",
      "recommendations": [
        "建议企业采用混合办公模式",
        "重视远程协作工具的投资"
      ]
    },
    "status": "completed",
    "userId": "user_123",
    "createdAt": "2026-03-03T10:00:00.000Z",
    "updatedAt": "2026-03-03T10:05:30.000Z"
  },
  {
    "id": "debate_1709424000000_xyz789",
    "topic": "人工智能是否会取代人类工作?",
    "userProfile": { /* ... */ },
    "opponentProfile": { /* ... */ },
    "messages": [ /* ... */ ],
    "synthesis": { /* ... */ },
    "status": "completed",
    "userId": "user_123",
    "createdAt": "2026-03-02T10:00:00.000Z",
    "updatedAt": "2026-03-02T10:06:15.000Z"
  }
]

Response Characteristics

Sorting: Debates are sorted by createdAt in descending order (newest first)Limit: Maximum of 20 debates returnedFiltering: Only debates belonging to the authenticated user are returnedCleanup: MongoDB internal fields (_id, __v) are removed from the response

Error Responses

401 Unauthorized

Returned when the request lacks valid authentication.
{
  "error": "Unauthorized"
}

500 Internal Server Error

Returned when database connection or query fails.
{
  "error": "Failed to load history"
}

Use Cases

Display User’s Debate History

Retrieve all debates to show in a user dashboard or profile page.
fetch('/api/debate/history')
  .then(res => res.json())
  .then(debates => {
    debates.forEach(debate => {
      console.log(`${debate.topic} - Winner: ${debate.synthesis?.winner}`);
    });
  });

Filter Completed Debates

Filter for debates that have synthesis data.
fetch('/api/debate/history')
  .then(res => res.json())
  .then(debates => {
    const completed = debates.filter(d => d.status === 'completed' && d.synthesis);
    console.log(`${completed.length} completed debates`);
  });

Analyze Debate Performance

Calculate win rate from debate history.
fetch('/api/debate/history')
  .then(res => res.json())
  .then(debates => {
    const wins = debates.filter(d => d.synthesis?.winner === 'user').length;
    const total = debates.filter(d => d.synthesis).length;
    const winRate = (wins / total * 100).toFixed(1);
    console.log(`Win rate: ${winRate}%`);
  });

Database Schema Reference

The response structure matches the MongoDB Debate model defined in /src/models/Debate.ts:62-102.
The history endpoint returns debates in their stored format. Incomplete debates (status: "in_progress") may have empty messages arrays or null synthesis objects.

Start New Debate

Create a new debate session to add to your history

Build docs developers (and LLMs) love