|
| 1 | +// src/app/api/documents/get.ts |
| 2 | + |
| 3 | +'use server'; |
| 4 | +import { NextRequest, NextResponse } from 'next/server'; |
| 5 | +import * as git from 'isomorphic-git'; |
| 6 | +import fs from 'fs'; |
| 7 | +import path from 'path'; |
| 8 | +import { DOC_POOL_DIR, findTaxonomyDocRepoPath } from '@/app/api/utils'; |
| 9 | +import { KnowledgeFile } from '@/types'; |
| 10 | + |
| 11 | +const BASE_BRANCH = 'main'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Function to retrieve knowledge file content from a document pool. |
| 15 | + * @param filename - Name of the file to retrieve |
| 16 | + * @returns A KnowledgeFile object with the content |
| 17 | + */ |
| 18 | +const getKnowledgeFiles = async (filename: string): Promise<KnowledgeFile> => { |
| 19 | + const REPO_DIR = findTaxonomyDocRepoPath(); |
| 20 | + |
| 21 | + // Ensure the repository path exists |
| 22 | + if (!fs.existsSync(REPO_DIR)) { |
| 23 | + throw new Error('Taxonomy knowledge doc repository does not exist. No files present.'); |
| 24 | + } |
| 25 | + |
| 26 | + // Check if the branch exists |
| 27 | + const branches = await git.listBranches({ fs, dir: REPO_DIR }); |
| 28 | + if (!branches.includes(BASE_BRANCH)) { |
| 29 | + throw new Error(`Branch "${BASE_BRANCH}" does not exist.`); |
| 30 | + } |
| 31 | + |
| 32 | + // Checkout the specified branch |
| 33 | + await git.checkout({ fs, dir: REPO_DIR, ref: BASE_BRANCH }); |
| 34 | + |
| 35 | + // check if the file exists in the document pool |
| 36 | + const filePath = path.join(REPO_DIR, DOC_POOL_DIR, filename); |
| 37 | + |
| 38 | + if (!fs.existsSync(filePath)) { |
| 39 | + throw new Error(`File "${filename}" does not exist in document pool.`); |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + // Retrieve the latest commit SHA for the file on the specified branch |
| 44 | + const logs = await git.log({ |
| 45 | + fs, |
| 46 | + dir: REPO_DIR, |
| 47 | + ref: BASE_BRANCH, |
| 48 | + filepath: path.join(DOC_POOL_DIR, filename), |
| 49 | + depth: 1 // Only the latest commit |
| 50 | + }); |
| 51 | + |
| 52 | + if (logs.length === 0) { |
| 53 | + throw new Error(`File "${filename}" exist, but has no related commit history.`); |
| 54 | + } |
| 55 | + |
| 56 | + const latestCommit = logs[0]; |
| 57 | + |
| 58 | + const commitDate = new Date(latestCommit.commit.committer.timestamp * 1000).toISOString(); |
| 59 | + |
| 60 | + // Read the file content |
| 61 | + const fileContent = fs.readFileSync(filePath, 'utf-8'); |
| 62 | + |
| 63 | + const knowledgeFile: KnowledgeFile = { |
| 64 | + filename: path.basename(filename), |
| 65 | + content: fileContent, |
| 66 | + commitDate: commitDate |
| 67 | + }; |
| 68 | + return knowledgeFile; |
| 69 | + } catch (error) { |
| 70 | + console.error(`Failed to read file ${filename}:`, error); |
| 71 | + throw new Error(`File "${filename}" does not exist in document pool.`); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +/** |
| 76 | + * Handler to retrieve knowledge file content |
| 77 | + */ |
| 78 | +export async function GET(request: NextRequest) { |
| 79 | + const url = new URL(request.url); |
| 80 | + const filename = url.searchParams.get('filename'); |
| 81 | + if (filename != null) { |
| 82 | + try { |
| 83 | + const knowledgeFile = await getKnowledgeFiles(filename); |
| 84 | + return NextResponse.json({ file: knowledgeFile }, { status: 200 }); |
| 85 | + } catch (error) { |
| 86 | + console.error(`Failed to retrieve content of the file: ${filename}`, error); |
| 87 | + return NextResponse.json({ error: (error as Error).message }, { status: 500 }); |
| 88 | + } |
| 89 | + } else { |
| 90 | + console.error(`File name must be not empty.`); |
| 91 | + return NextResponse.json({ error: `File name must be not empty.` }, { status: 500 }); |
| 92 | + } |
| 93 | +} |
0 commit comments