File size: 10,797 Bytes
3e12ae4
d4abe4b
3e12ae4
 
 
 
 
 
 
 
 
 
 
d4abe4b
 
 
 
3e12ae4
 
d4abe4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e12ae4
 
d4abe4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a40d961
 
 
 
d4abe4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e12ae4
d4abe4b
 
 
 
 
 
 
 
 
 
 
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import type { FastifySwaggerUiOptions } from '@fastify/swagger-ui';

export const swaggerOptions = {
  openapi: '3.1.0',
  info: {
    title: 'MEDAGEN Backend API',
    description: 'AI Triage Assistant API với ReAct Agent và Gemini 2.5Flash',
    version: '2.0.0',
    contact: {
      name: 'MEDAGEN Team'
    }
  },
  servers: [
      {
        url: 'http://localhost:7860',
        description: 'Development server (Port 7860)'
      }
  ],
  tags: [
      {
        name: 'health',
        description: 'Health check endpoints'
      },
      {
        name: 'triage',
        description: 'AI Triage endpoints'
      },
      {
        name: 'cv',
        description: 'Computer Vision endpoints - Phân tích hình ảnh y tế'
      },
      {
        name: 'rag',
        description: 'RAG endpoints - Tìm kiếm hướng dẫn y tế'
      },
      {
        name: 'maps',
        description: 'Google Maps endpoints - Tìm cơ sở y tế'
      },
      {
        name: 'sessions',
        description: 'Session management endpoints'
      },
      {
        name: 'conversations',
        description: 'Conversation history endpoints'
      }
  ],
  components: {
      schemas: {
        HealthCheckRequest: {
          type: 'object',
          required: ['user_id'],
          properties: {
            text: {
              type: 'string',
              description: 'Mô tả triệu chứng của người dùng (bắt buộc nếu không có image_url)',
              example: 'Mắt trái đỏ và hơi mờ 2 ngày nay'
            },
            image_url: {
              type: 'string',
              format: 'uri',
              description: 'URL của hình ảnh (bắt buộc nếu không có text)',
              example: 'https://supabase.../image.jpg'
            },
            user_id: {
              type: 'string',
              description: 'ID của người dùng',
              example: 'abc123'
            },
            session_id: {
              type: 'string',
              format: 'uuid',
              description: 'Session ID để theo dõi lịch sử hội thoại (tùy chọn, sẽ tự động tạo mới nếu không có)',
              example: '550e8400-e29b-41d4-a716-446655440000'
            },
            location: {
              type: 'object',
              description: 'Vị trí của người dùng (tùy chọn)',
              properties: {
                lat: {
                  type: 'number',
                  description: 'Vĩ độ',
                  example: 10.78
                },
                lng: {
                  type: 'number',
                  description: 'Kinh độ',
                  example: 106.7
                }
              }
            }
          },
          oneOf: [
            { required: ['text'] },
            { required: ['image_url'] }
          ]
        },
        HealthCheckResponse: {
          type: 'object',
          properties: {
            triage_level: {
              type: 'string',
              enum: ['emergency', 'urgent', 'routine', 'self-care'],
              description: 'Mức độ triage'
            },
            symptom_summary: {
              type: 'string',
              description: 'Tóm tắt triệu chứng'
            },
            red_flags: {
              type: 'array',
              items: {
                type: 'string'
              },
              description: 'Các dấu hiệu nguy hiểm'
            },
            suspected_conditions: {
              type: 'array',
              items: {
                type: 'object',
                properties: {
                  name: {
                    type: 'string',
                    description: 'Tên tình trạng nghi ngờ'
                  },
                  source: {
                    type: 'string',
                    enum: ['cv_model', 'guideline', 'user_report', 'reasoning'],
                    description: 'Nguồn của tình trạng'
                  },
                  confidence: {
                    type: 'string',
                    enum: ['low', 'medium', 'high'],
                    description: 'Độ tin cậy'
                  }
                }
              }
            },
            cv_findings: {
              type: 'object',
              properties: {
                model_used: {
                  type: 'string',
                  enum: ['derm_cv', 'eye_cv', 'wound_cv', 'none'],
                  description: 'Model CV được sử dụng'
                },
                raw_output: {
                  type: 'object',
                  description: 'Kết quả raw từ CV model'
                }
              }
            },
            recommendation: {
              type: 'object',
              properties: {
                action: {
                  type: 'string',
                  description: 'Hành động khuyến nghị'
                },
                timeframe: {
                  type: 'string',
                  description: 'Thời gian khuyến nghị'
                },
                home_care_advice: {
                  type: 'string',
                  description: 'Lời khuyên chăm sóc tại nhà'
                },
                warning_signs: {
                  type: 'string',
                  description: 'Dấu hiệu cảnh báo'
                }
              }
            },
            nearest_clinic: {
              type: 'object',
              description: 'Cơ sở y tế gần nhất (nếu có location)',
              properties: {
                name: {
                  type: 'string',
                  description: 'Tên cơ sở y tế'
                },
                distance_km: {
                  type: 'number',
                  description: 'Khoảng cách (km)'
                },
                address: {
                  type: 'string',
                  description: 'Địa chỉ'
                },
                rating: {
                  type: 'number',
                  description: 'Đánh giá'
                }
              }
            },
            session_id: {
              type: 'string',
              format: 'uuid',
              description: 'Session ID để tiếp tục cuộc hội thoại trong các request tiếp theo'
            },
            message: {
              type: 'string',
              description: 'Markdown response từ LLM (natural language, không bị giới hạn bởi JSON structure)'
            }
          }
        },
        HealthStatus: {
          type: 'object',
          properties: {
            status: {
              type: 'string',
              example: 'ok'
            },
            timestamp: {
              type: 'string',
              format: 'date-time',
              description: 'Thời gian kiểm tra'
            },
            llm: {
              type: 'string',
              description: 'Model LLM đang sử dụng',
              example: 'gemini-2.5-flash'
            },
            cv_services: {
              type: 'object',
              properties: {
                derm_cv: {
                  type: 'string',
                  example: 'unknown'
                },
                eye_cv: {
                  type: 'string',
                  example: 'unknown'
                },
                wound_cv: {
                  type: 'string',
                  example: 'unknown'
                }
              }
            }
          }
        },
        ErrorResponse: {
          type: 'object',
          properties: {
            error: {
              type: 'string',
              description: 'Loại lỗi'
            },
            message: {
              type: 'string',
              description: 'Thông báo lỗi'
            },
            details: {
              type: 'array',
              items: {
                type: 'object'
              },
              description: 'Chi tiết lỗi (nếu có)'
            }
          }
        },
        ConversationSession: {
          type: 'object',
          properties: {
            id: {
              type: 'string',
              format: 'uuid',
              description: 'ID của session'
            },
            user_id: {
              type: 'string',
              description: 'ID của người dùng'
            },
            created_at: {
              type: 'string',
              format: 'date-time',
              description: 'Thời gian tạo'
            },
            updated_at: {
              type: 'string',
              format: 'date-time',
              description: 'Thời gian cập nhật cuối'
            }
          }
        },
        ConversationMessage: {
          type: 'object',
          properties: {
            id: {
              type: 'string',
              format: 'uuid',
              description: 'ID của message'
            },
            session_id: {
              type: 'string',
              format: 'uuid',
              description: 'ID của session'
            },
            user_id: {
              type: 'string',
              description: 'ID của người dùng'
            },
            role: {
              type: 'string',
              enum: ['user', 'assistant'],
              description: 'Vai trò của message'
            },
            content: {
              type: 'string',
              description: 'Nội dung message'
            },
            image_url: {
              type: 'string',
              format: 'uri',
              description: 'URL của hình ảnh (nếu có)'
            },
            triage_result: {
              type: 'object',
              description: 'Kết quả triage (chỉ có với assistant messages)'
            },
            created_at: {
              type: 'string',
              format: 'date-time',
              description: 'Thời gian tạo'
            }
          }
        }
      }
    }
};

export const swaggerUiOptions: FastifySwaggerUiOptions = {
  routePrefix: '/docs',
  uiConfig: {
    docExpansion: 'list',
    deepLinking: true,
    displayRequestDuration: true,
    persistAuthorization: true
  },
  staticCSP: false, // Disable strict CSP to allow Swagger UI to fetch JSON
  transformSpecification: (swaggerObject, request) => {
    // Ensure servers URL matches current request
    if (swaggerObject.servers && swaggerObject.servers.length > 0) {
      const protocol = request.headers['x-forwarded-proto'] || (request.protocol || 'http');
      const host = request.headers.host || 'localhost:7860';
      swaggerObject.servers[0].url = `${protocol}://${host}`;
    }
    return swaggerObject;
  },
  transformSpecificationClone: true
};