@@ -6,30 +6,25 @@ import path from 'path';
66import fs from 'fs' ;
77
88const LOCAL_TAXONOMY_ROOT_DIR = process . env . NEXT_PUBLIC_LOCAL_TAXONOMY_ROOT_DIR || `${ process . env . HOME } /.instructlab-ui` ;
9+ const LOCAL_TAXONOMY_DIR = path . join ( LOCAL_TAXONOMY_ROOT_DIR , '/taxonomy' ) ;
910const TAXONOMY_REPO_URL = process . env . NEXT_PUBLIC_TAXONOMY_REPO_URL || 'https://github.com/instructlab/taxonomy.git' ;
1011const SKILLS = 'compositional_skills' ;
1112const KNOWLEDGE = 'knowledge' ;
1213const CHECK_INTERVAL = 300000 ; // 5 minute
1314let lastChecked = 0 ;
1415
1516async function cloneTaxonomyRepo ( ) : Promise < boolean > {
16- const taxonomyDirectoryPath = path . join ( LOCAL_TAXONOMY_ROOT_DIR , '/taxonomy' ) ;
17-
18- if ( fs . existsSync ( taxonomyDirectoryPath ) ) {
19- fs . rmdirSync ( taxonomyDirectoryPath , { recursive : true } ) ;
20- }
21-
2217 try {
2318 await git . clone ( {
2419 fs,
2520 http,
26- dir : taxonomyDirectoryPath ,
21+ dir : LOCAL_TAXONOMY_DIR ,
2722 url : TAXONOMY_REPO_URL ,
2823 singleBranch : true
2924 } ) ;
3025
3126 // Include the full path in the response for client display
32- console . log ( `Local Taxonomy repository cloned successfully to ${ taxonomyDirectoryPath } .` ) ;
27+ console . log ( `Local Taxonomy repository cloned successfully to ${ LOCAL_TAXONOMY_DIR } .` ) ;
3328 return true ;
3429 } catch ( error : unknown ) {
3530 const errorMessage = error instanceof Error ? error . message : 'Unknown error occurred' ;
@@ -38,11 +33,9 @@ async function cloneTaxonomyRepo(): Promise<boolean> {
3833 }
3934}
4035
41- async function deleteTaxonomyRepo ( ) : Promise < void > {
42- const taxonomyDirectoryPath = path . join ( LOCAL_TAXONOMY_ROOT_DIR , '/taxonomy' ) ;
43-
44- if ( fs . existsSync ( taxonomyDirectoryPath ) ) {
45- fs . rmdirSync ( taxonomyDirectoryPath , { recursive : true } ) ;
36+ function deleteTaxonomyRepo ( ) {
37+ if ( fs . existsSync ( LOCAL_TAXONOMY_DIR ) ) {
38+ fs . rmdirSync ( LOCAL_TAXONOMY_DIR , { recursive : true } ) ;
4639 }
4740}
4841
@@ -59,9 +52,7 @@ async function getRemoteHeadHash(): Promise<string | null> {
5952
6053async function getLocalHeadHash ( ) : Promise < string | null > {
6154 try {
62- const taxonomyDirectoryPath = path . join ( LOCAL_TAXONOMY_ROOT_DIR , '/taxonomy' ) ;
63-
64- const head = await git . resolveRef ( { fs, dir : taxonomyDirectoryPath , ref : 'HEAD' } ) ;
55+ const head = await git . resolveRef ( { fs, dir : LOCAL_TAXONOMY_DIR , ref : 'HEAD' } ) ;
6556 return head || null ;
6657 } catch ( error ) {
6758 console . error ( 'Failed to get local head hash:' , error ) ;
@@ -70,6 +61,11 @@ async function getLocalHeadHash(): Promise<string | null> {
7061}
7162
7263async function checkForUpdates ( ) : Promise < void > {
64+ if ( ! fs . existsSync ( LOCAL_TAXONOMY_DIR ) ) {
65+ await cloneTaxonomyRepo ( ) ;
66+ return ;
67+ }
68+
7369 const currentTime = Date . now ( ) ;
7470 if ( currentTime - lastChecked < CHECK_INTERVAL ) {
7571 return ;
@@ -82,16 +78,16 @@ async function checkForUpdates(): Promise<void> {
8278
8379 if ( remoteHash && localHash && remoteHash !== localHash ) {
8480 console . log ( `${ timestamp } : New changes detected, updating repository...` ) ;
85- await deleteTaxonomyRepo ( ) ;
81+ deleteTaxonomyRepo ( ) ;
8682 await cloneTaxonomyRepo ( ) ;
8783 } else {
8884 console . log ( `${ timestamp } : No new changes detected in taxonomy repo.` ) ;
8985 }
9086}
9187
92- function getFirstLevelDirectories ( directoryPath : string ) : string [ ] {
88+ async function getFirstLevelDirectories ( directoryPath : string ) : Promise < string [ ] > {
9389 try {
94- checkForUpdates ( ) ;
90+ await checkForUpdates ( ) ;
9591 return fs
9692 . readdirSync ( directoryPath )
9793 . map ( ( name ) => path . join ( directoryPath , name ) )
@@ -110,11 +106,11 @@ export async function POST(req: NextRequest) {
110106 try {
111107 let dirPath = '' ;
112108 if ( root_path === 'skills' ) {
113- dirPath = path . join ( LOCAL_TAXONOMY_ROOT_DIR , 'taxonomy' , SKILLS , dir_name ) ;
109+ dirPath = path . join ( LOCAL_TAXONOMY_DIR , SKILLS , dir_name ) ;
114110 } else {
115- dirPath = path . join ( LOCAL_TAXONOMY_ROOT_DIR , 'taxonomy' , KNOWLEDGE , dir_name ) ;
111+ dirPath = path . join ( LOCAL_TAXONOMY_DIR , KNOWLEDGE , dir_name ) ;
116112 }
117- const dirs = getFirstLevelDirectories ( dirPath ) ;
113+ const dirs = await getFirstLevelDirectories ( dirPath ) ;
118114 return NextResponse . json ( { data : dirs } , { status : 201 } ) ;
119115 } catch ( error ) {
120116 console . error ( 'Failed to get the tree for path:' , root_path , error ) ;
0 commit comments