update internal-api

This commit is contained in:
hanif salafi 2025-08-04 17:17:51 +07:00
parent e126b56fa6
commit e8cc91569d
4 changed files with 12 additions and 118 deletions

View File

@ -5,7 +5,7 @@ API untuk mengelola riwayat chat menggunakan database SQLite lokal.
## Base URL
```
http://localhost:3001/api/chat-history
http://localhost:3001/internal-api/chat-history
```
## Database File
@ -13,7 +13,7 @@ Database SQLite disimpan di file `lib/db/chat_history.db` di dalam project.
## Endpoints
### 1. POST /api/chat-history
### 1. POST /internal-api/chat-history
Menyimpan atau memperbarui riwayat chat.
**Request Body:**
@ -45,7 +45,7 @@ Menyimpan atau memperbarui riwayat chat.
}
```
### 2. GET /api/chat-history
### 2. GET /internal-api/chat-history
Mendapatkan daftar riwayat chat berdasarkan user_id.
**Query Parameters:**
@ -53,7 +53,7 @@ Mendapatkan daftar riwayat chat berdasarkan user_id.
**Example:**
```
GET /api/chat-history?user_id=user_123
GET /internal-api/chat-history?user_id=user_123
```
**Response:**
@ -76,7 +76,7 @@ GET /api/chat-history?user_id=user_123
}
```
### 3. GET /api/chat-history/[session_id]
### 3. GET /internal-api/chat-history/[session_id]
Mendapatkan detail session dan pesan berdasarkan session_id.
**Path Parameters:**
@ -84,7 +84,7 @@ Mendapatkan detail session dan pesan berdasarkan session_id.
**Example:**
```
GET /api/chat-history/ded90fa8-97c9-4073-a442-f2a42064d7da
GET /internal-api/chat-history/ded90fa8-97c9-4073-a442-f2a42064d7da
```
**Response:**
@ -121,7 +121,7 @@ GET /api/chat-history/ded90fa8-97c9-4073-a442-f2a42064d7da
}
```
### 4. DELETE /api/chat-history/[session_id]
### 4. DELETE /internal-api/chat-history/[session_id]
Menghapus session chat berdasarkan session_id.
**Path Parameters:**
@ -132,7 +132,7 @@ Menghapus session chat berdasarkan session_id.
**Example:**
```
DELETE /api/chat-history/ded90fa8-97c9-4073-a442-f2a42064d7da?user_id=user_123
DELETE /internal-api/chat-history/ded90fa8-97c9-4073-a442-f2a42064d7da?user_id=user_123
```
**Response:**

View File

@ -1,106 +0,0 @@
import { NextRequest, NextResponse } from 'next/server';
import getDatabase from '@/lib/database';
interface ChatSession {
id: string;
session_id: string;
user_id: string;
agent_id: string;
title: string;
message_count: number;
created_at: string;
updated_at: string;
}
interface ChatMessage {
id: string;
session_id: string;
message_type: string;
content: string;
created_at: string;
}
// GET - Get session details by id
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params;
const db = getDatabase();
if (!id) {
return NextResponse.json({ error: 'ID is required' }, { status: 400 });
}
// Get session details by id
const session = db.prepare(`
SELECT * FROM chat_sessions
WHERE id = ?
`).get(id) as ChatSession | undefined;
if (!session) {
return NextResponse.json({ error: 'Session not found' }, { status: 404 });
}
// Get messages for this session
const messages = db.prepare(`
SELECT * FROM chat_messages
WHERE session_id = ?
ORDER BY created_at ASC
`).all(session.session_id) as ChatMessage[];
return NextResponse.json({
success: true,
session,
messages
}, { status: 200 });
} catch (error) {
console.error('Error fetching session details:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}
// DELETE - Delete a specific chat session
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params;
const { searchParams } = new URL(request.url);
const user_id = searchParams.get('user_id');
const db = getDatabase();
if (!id) {
return NextResponse.json({ error: 'ID is required' }, { status: 400 });
}
if (!user_id) {
return NextResponse.json({ error: 'User ID is required' }, { status: 400 });
}
// Check if session exists and belongs to user
const session = db.prepare(`
SELECT id FROM chat_sessions
WHERE id = ? AND user_id = ?
`).get(id, user_id) as { id: string } | undefined;
if (!session) {
return NextResponse.json({ error: 'Session not found or access denied' }, { status: 404 });
}
// Delete session (messages will be deleted automatically due to CASCADE)
db.prepare('DELETE FROM chat_sessions WHERE id = ?').run(id);
return NextResponse.json({
success: true,
message: 'Session deleted successfully'
}, { status: 200 });
} catch (error) {
console.error('Error deleting session:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}

View File

@ -54,7 +54,7 @@ export async function saveChatHistory(data: {
messages?: ChatMessage[];
}) {
try {
const response = await fetch('/api/chat-history', {
const response = await fetch('/internal-api/chat-history', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@ -82,7 +82,7 @@ export async function saveChatHistory(data: {
// Get chat history
export async function getChatHistory(user_id: string): Promise<ChatHistoryResponse> {
try {
const response = await fetch(`/api/chat-history?user_id=${user_id}`, {
const response = await fetch(`/internal-api/chat-history?user_id=${user_id}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
@ -103,7 +103,7 @@ export async function getChatHistory(user_id: string): Promise<ChatHistoryRespon
// Get session details
export async function getSessionDetail(id: string): Promise<SessionDetailResponse> {
try {
const response = await fetch(`/api/chat-history/${id}`, {
const response = await fetch(`/internal-api/chat-history/${id}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
@ -124,7 +124,7 @@ export async function getSessionDetail(id: string): Promise<SessionDetailRespons
// Delete chat session
export async function deleteChatSession(id: string, user_id: string) {
try {
const response = await fetch(`/api/chat-history/${id}?user_id=${user_id}`, {
const response = await fetch(`/internal-api/chat-history/${id}?user_id=${user_id}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',