File size: 5,043 Bytes
de35c56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Smart Travel AI Agent API Documentation

This document describes how a web application or frontend client can connect to and interact with the Smart Travel AI Agent hosted on Hugging Face Spaces.

## 🔗 Connection Information

- **Base URL**: `https://youmei295-planning-agent.hf.space`
- **CORS**: Enabled for all origins (`*`), so it can be called directly from frontend web applications (e.g., Next.js, React, Vue, Vanilla JS).
- **Communication Protocol**: HTTPS / REST API

---

## 🟢 1. Health Check Endpoint

Used to verify if the AI agent backend is up and running.

**Endpoint:** `GET /health` or `GET /`

**Response Example (200 OK):**
```json
{
  "status": "ok",
  "message": "Smart Travel AI API is running"
}
```

---

## 🚀 2. Trip Planning & Chat Endpoint

This is the main endpoint used to communicate with the agent. The system automatically routes the message to either a normal conversation (chat) or complex trip planning based on the user's intent.

**Endpoint:** `POST /api/plan`  
**Headers:** 
- `Content-Type: application/json`

### 📥 Input Format (Request Payload)

Send a JSON body with the following structure:

```json
{
  "message": "Mình muốn đi chơi cuối tuần ở Bình Thạnh, ưu tiên quán cafe yên tĩnh và ăn đồ nướng.",
  "start_date": "2024-12-01",  // (Optional) Expected start date, Format: YYYY-MM-DD
  "num_days": 1,               // (Optional) Number of days for the trip (Max: 3)
  "session_id": "user-123"     // (Optional) For tracking user sessions
}
```

**Field Details:**
- `message` *(string, required)*: The user's prompt, question, or requirement.
- `start_date` *(string, optional)*: Explicit start date chosen from the frontend UI.
- `num_days` *(integer, optional)*: Explicit number of days chosen from the frontend UI. If omitted, the AI will try to infer the duration from the `message`.
- `session_id` *(string, optional)*: An ID to keep track of the user's conversation thread (if applicable).

---

### 📤 Output Formats (Response)

The API returns different structures depending on how the AI categorizes the user's input.

#### Scenario A: Chat / Small Talk / Asking for more info
If the user just says "Hello" or provides insufficient information for a trip, the agent responds conversationally.

**Response Example (200 OK):**
```json
{
  "status": "chat",
  "total_cost": "0 VNĐ",
  "itinerary_markdown": "Xin chào! Bạn muốn lên kế hoạch đi chơi ở đâu? Hãy cho mình biết thêm thông tin nhé.",
  "timeline": []
}
```

#### Scenario B: Successful Trip Plan Generation
If the user provides a valid travel request, the system processes it, queries the database, and returns a detailed itinerary and timeline.

**Response Example (200 OK):**
```json
{
  "status": "success",
  "total_cost": "500.000 - 1.000.000 VNĐ",
  "itinerary_markdown": "## 🌟 Kế hoạch đi Bình Thạnh của bạn\n\n**Buổi sáng:** Cafe Yên...\n...",
  "timeline": [
    {
      "time": "08:00 - 10:00",
      "activity": "Uống cafe",
      "location": "Quán Cafe Yên",
      "location_id": "uuid-from-supabase",
      "cost_estimate": "50000"
    },
    {
      "time": "18:00 - 20:00",
      "activity": "Ăn đồ nướng",
      "location": "Panda BBQ",
      "location_id": "uuid-from-supabase",
      "cost_estimate": "250000"
    }
  ]
}
```

#### Scenario C: Error Handling
If an internal error occurs during processing.

**Response Example (500 Internal Server Error):**
```json
{
  "detail": "Lỗi kết nối cơ sở dữ liệu..."
}
```

---

## 💻 Example Connection Method (JavaScript/Fetch)

Here is a simple example of how a web app can connect to this API using the standard `fetch` API:

```javascript
async function requestTripPlan(userMessage) {
  const url = "https://youmei295-planning-agent.hf.space/api/plan";
  
  const payload = {
    message: userMessage,
    start_date: "2024-12-01", // Or retrieve from your UI date picker
    num_days: 1               // Or retrieve from your UI dropdown
  };

  try {
    const response = await fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(payload)
    });

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    const data = await response.json();

    if (data.status === "success") {
      console.log("Trip Planned Successfully!");
      console.log("Total Cost:", data.total_cost);
      console.log("Markdown Report:", data.itinerary_markdown);
      console.log("Timeline Array:", data.timeline);
      
      // TODO: Render the markdown and timeline array in your UI
    } else if (data.status === "chat") {
      console.log("Agent Message:", data.itinerary_markdown);
      
      // TODO: Show this message in a chat bubble
    }

  } catch (error) {
    console.error("Error communicating with AI Agent:", error);
  }
}

// Example usage:
// requestTripPlan("Mình muốn đi Quận 1 uống cafe và đi dạo.");
```