|
| 1 | +// src/app/api/playground/ragchat/index-files/route.ts |
| 2 | +'use server'; |
| 3 | + |
| 4 | +import { NextRequest, NextResponse } from 'next/server'; |
| 5 | +import fetch from 'node-fetch'; |
| 6 | + |
| 7 | +async function authenticate(USERNAME: string, API_KEY: string, DS_HOST: string, retries: number = 3): Promise<string> { |
| 8 | + for (let attempt = 0; attempt < retries; attempt++) { |
| 9 | + try { |
| 10 | + console.log('Starting authentication...'); |
| 11 | + const authResponse = await fetch(`${DS_HOST}/api/cps/user/v1/user/token`, { |
| 12 | + method: 'POST', |
| 13 | + headers: { |
| 14 | + 'Content-Type': 'application/json', |
| 15 | + Authorization: `Basic ${Buffer.from(`${USERNAME}:${API_KEY}`).toString('base64')}` |
| 16 | + }, |
| 17 | + body: JSON.stringify({}) |
| 18 | + }); |
| 19 | + |
| 20 | + const authText = await authResponse.text(); |
| 21 | + console.log('Auth response text:', authText); |
| 22 | + |
| 23 | + if (!authResponse.ok) { |
| 24 | + throw new Error(authText); |
| 25 | + } |
| 26 | + |
| 27 | + const authData = JSON.parse(authText); |
| 28 | + console.log('Authentication successful. Token obtained.'); |
| 29 | + return authData.access_token; |
| 30 | + } catch (error) { |
| 31 | + console.error(`Authentication attempt ${attempt + 1} failed:`, error.message); |
| 32 | + if (attempt < retries - 1) { |
| 33 | + console.log('Retrying in 3 seconds...'); |
| 34 | + await new Promise((resolve) => setTimeout(resolve, 3000)); |
| 35 | + } else { |
| 36 | + throw new Error('Failed to authenticate after multiple attempts'); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +async function fetchDocuments(DS_HOST: string, PROJ_KEY: string, dsIndexKey: string, token: string, retries: number = 3) { |
| 43 | + for (let attempt = 0; attempt < retries; attempt++) { |
| 44 | + try { |
| 45 | + console.log('Fetching documents...'); |
| 46 | + const response = await fetch(`${DS_HOST}/api/cps/public/v2/project/${PROJ_KEY}/data_indices/${dsIndexKey}/documents/`, { |
| 47 | + method: 'GET', |
| 48 | + headers: { |
| 49 | + 'Content-Type': 'application/json', |
| 50 | + Authorization: `Bearer ${token}` |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + console.log('API response status:', response.status); |
| 55 | + |
| 56 | + if (!response.ok) { |
| 57 | + const errorText = await response.text(); |
| 58 | + throw new Error(errorText); |
| 59 | + } |
| 60 | + |
| 61 | + const data = await response.json(); |
| 62 | + console.log('Fetched documents:', data.documents); |
| 63 | + return data.documents.filter((doc: any) => doc.status === 'SUCCESS'); |
| 64 | + } catch (error) { |
| 65 | + console.error(`Fetch attempt ${attempt + 1} failed:`, error.message); |
| 66 | + if (attempt < retries - 1) { |
| 67 | + console.log('Retrying in 3 seconds...'); |
| 68 | + await new Promise((resolve) => setTimeout(resolve, 3000)); |
| 69 | + } else { |
| 70 | + throw new Error('Failed to fetch documents after multiple attempts'); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export async function GET(req: NextRequest) { |
| 77 | + const { searchParams } = new URL(req.url); |
| 78 | + const dsIndexKey = searchParams.get('indexKey'); |
| 79 | + const USERNAME = process.env.DS_USERNAME; |
| 80 | + const API_KEY = process.env.DS_API_KEY; |
| 81 | + const DS_HOST = process.env.DS_HOST; |
| 82 | + const PROJ_KEY = process.env.DS_PROJ_KEY; |
| 83 | + |
| 84 | + console.log('Received request for data index:', dsIndexKey); |
| 85 | + |
| 86 | + if (!dsIndexKey || !USERNAME || !API_KEY || !DS_HOST || !PROJ_KEY) { |
| 87 | + console.error('Missing required parameters or environment variables', { dsIndexKey, USERNAME, API_KEY, DS_HOST, PROJ_KEY }); |
| 88 | + return NextResponse.json({ error: 'Missing required parameters or environment variables' }, { status: 400 }); |
| 89 | + } |
| 90 | + |
| 91 | + try { |
| 92 | + const token = await authenticate(USERNAME, API_KEY, DS_HOST); |
| 93 | + const documents = await fetchDocuments(DS_HOST, PROJ_KEY, dsIndexKey, token); |
| 94 | + return NextResponse.json({ documents }, { status: 200 }); |
| 95 | + } catch (error) { |
| 96 | + console.error('Server error:', error.message); |
| 97 | + return NextResponse.json({ error: error.message }, { status: 500 }); |
| 98 | + } |
| 99 | +} |
0 commit comments