From 184a44c235bf6fd26d21d4c8ec359523aa78be36 Mon Sep 17 00:00:00 2001 From: Brent Salisbury Date: Mon, 23 Dec 2024 22:13:42 -0500 Subject: [PATCH] Remove validation for repo URL in native mode - Also change the field name to include server side filepath. Signed-off-by: Brent Salisbury --- src/app/api/native/upload/route.ts | 46 +++++++++++++----- .../DocumentInformation.tsx | 40 +--------------- .../KnowledgeQuestionAnswerPairs.tsx | 13 ++++- .../Knowledge/ReviewSubmission/index.tsx | 48 ++++++++----------- 4 files changed, 67 insertions(+), 80 deletions(-) diff --git a/src/app/api/native/upload/route.ts b/src/app/api/native/upload/route.ts index b67a452f..7473e4f2 100644 --- a/src/app/api/native/upload/route.ts +++ b/src/app/api/native/upload/route.ts @@ -6,7 +6,8 @@ import http from 'isomorphic-git/http/node'; import path from 'path'; import fs from 'fs'; -const LOCAL_TAXONOMY_DOCS_ROOT_DIR = process.env.NEXT_PUBLIC_LOCAL_TAXONOMY_ROOT_DIR || `${process.env.HOME}/.instructlab-ui`; +const TAXONOMY_DOCS_ROOT_DIR = process.env.NEXT_PUBLIC_TAXONOMY_ROOT_DIR || ''; +const TAXONOMY_DOCS_CONTAINER_MOUNT_DIR = '/tmp/.instructlab-ui'; const TAXONOMY_KNOWLEDGE_DOCS_REPO_URL = 'https://github.com/instructlab-public/taxonomy-knowledge-docs.git'; export async function POST(req: NextRequest) { @@ -30,8 +31,10 @@ export async function POST(req: NextRequest) { }); // Write the files to the repository + const docsRepoUrlTmp = path.join(docsRepoUrl, '/'); for (const file of filesWithTimestamp) { - const filePath = path.join(docsRepoUrl, file.fileName); + const filePath = path.join(docsRepoUrlTmp, file.fileName); + console.log(`Writing file to ${filePath} in taxonomy knowledge docs repository.`); fs.writeFileSync(filePath, file.fileContent); } @@ -51,9 +54,12 @@ export async function POST(req: NextRequest) { .join(', ')}\n\nSigned-off-by: ui@instructlab.ai` }); + console.log(`Successfully committed files to taxonomy knowledge docs repository with commit SHA: ${commitSha}`); + + const origTaxonomyDocsRepoDir = path.join(TAXONOMY_DOCS_ROOT_DIR, '/taxonomy-knowledge-docs'); return NextResponse.json( { - repoUrl: docsRepoUrl, + repoUrl: origTaxonomyDocsRepoDir, commitSha, documentNames: filesWithTimestamp.map((file: { fileName: string }) => file.fileName), prUrl: '' @@ -61,17 +67,32 @@ export async function POST(req: NextRequest) { { status: 201 } ); } catch (error) { - console.error('Failed to upload documents:', error); - return NextResponse.json({ error: 'Failed to upload documents' }, { status: 500 }); + console.error('Failed to upload knowledge documents:', error); + return NextResponse.json({ error: 'Failed to upload knowledge documents' }, { status: 500 }); } } async function cloneTaxonomyDocsRepo() { - const taxonomyDocsDirectoryPath = path.join(LOCAL_TAXONOMY_DOCS_ROOT_DIR, '/taxonomy-knowledge-docs'); - console.log(`Cloning taxonomy docs repository to ${taxonomyDocsDirectoryPath}...`); + // Check the location of the taxonomy repository and create the taxonomy-docs-repository parallel to that. + let remoteTaxonomyRepoDirFinal: string = ''; + // Check if directory pointed by remoteTaxonomyRepoDir exists and not empty + const remoteTaxonomyRepoContainerMountDir = path.join(TAXONOMY_DOCS_CONTAINER_MOUNT_DIR, '/taxonomy'); + const remoteTaxonomyRepoDir = path.join(TAXONOMY_DOCS_ROOT_DIR, '/taxonomy'); + if (fs.existsSync(remoteTaxonomyRepoContainerMountDir) && fs.readdirSync(remoteTaxonomyRepoContainerMountDir).length !== 0) { + remoteTaxonomyRepoDirFinal = TAXONOMY_DOCS_CONTAINER_MOUNT_DIR; + } else { + if (fs.existsSync(remoteTaxonomyRepoDir) && fs.readdirSync(remoteTaxonomyRepoDir).length !== 0) { + remoteTaxonomyRepoDirFinal = TAXONOMY_DOCS_ROOT_DIR; + } + } + if (remoteTaxonomyRepoDirFinal === '') { + return null; + } + + const taxonomyDocsDirectoryPath = path.join(remoteTaxonomyRepoDirFinal, '/taxonomy-knowledge-docs'); if (fs.existsSync(taxonomyDocsDirectoryPath)) { - console.log(`Using existing taxonomy knowledge docs repository at ${taxonomyDocsDirectoryPath}.`); + console.log(`Using existing taxonomy knowledge docs repository at ${remoteTaxonomyRepoDir}/taxonomy-knowledge-docs.`); return taxonomyDocsDirectoryPath; } else { console.log(`Taxonomy knowledge docs repository not found at ${taxonomyDocsDirectoryPath}. Cloning...`); @@ -83,12 +104,13 @@ async function cloneTaxonomyDocsRepo() { http, dir: taxonomyDocsDirectoryPath, url: TAXONOMY_KNOWLEDGE_DOCS_REPO_URL, - singleBranch: true, - depth: 1 + singleBranch: true }); - // Include the full path in the response for client display - console.log(`Repository cloned successfully to ${taxonomyDocsDirectoryPath}.`); + // Include the full path in the response for client display. Path displayed here is the one + // that user set in the environment variable. + console.log(`Taxonomy knowledge docs repository cloned successfully to ${remoteTaxonomyRepoDir}.`); + // Return the path that the UI sees (direct or mounted) return taxonomyDocsDirectoryPath; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; diff --git a/src/components/Contribute/Knowledge/Native/DocumentInformation/DocumentInformation.tsx b/src/components/Contribute/Knowledge/Native/DocumentInformation/DocumentInformation.tsx index 3b81185c..b2fdf019 100644 --- a/src/components/Contribute/Knowledge/Native/DocumentInformation/DocumentInformation.tsx +++ b/src/components/Contribute/Knowledge/Native/DocumentInformation/DocumentInformation.tsx @@ -79,25 +79,6 @@ const DocumentInformation: React.FC = ({ } }, [isEditForm]); - const validateRepo = (repoStr: string) => { - const repo = repoStr.trim(); - if (repo.length === 0) { - setDisableAction(true); - setValidRepo(ValidatedOptions.error); - return; - } - try { - new URL(repo); - setValidRepo(ValidatedOptions.success); - setDisableAction(!checkKnowledgeFormCompletion(knowledgeFormData)); - return; - } catch (e) { - setDisableAction(true); - setValidRepo(ValidatedOptions.warning); - return; - } - }; - const validateCommit = (commitStr: string) => { const commit = commitStr.trim(); if (commit.length > 0) { @@ -283,7 +264,7 @@ const DocumentInformation: React.FC = ({ {!useFileUpload ? ( <> - + = ({ placeholder="Enter repo URL where document exists" value={knowledgeDocumentRepositoryUrl} onChange={(_event, value) => setKnowledgeDocumentRepositoryUrl(value)} - onBlur={() => validateRepo(knowledgeDocumentRepositoryUrl)} /> - {validRepo === ValidatedOptions.error && ( - - - } variant={validRepo}> - Required field - - - - )} - {validRepo === ValidatedOptions.warning && ( - - - } variant="error"> - Please enter a valid URL. - - - - )} = ({ color: '#333' }} > - A commit SHA ({commitSha}) has already been selected in a previous seed example. All subsequent selections must use the - same commit SHA for consistency. + + A commit SHA ({commitSha}) has already been selected in a previous seed example. All subsequent selections must use the + same commit SHA for consistency. + + {/*A commit SHA ({commitSha}) has already been selected in a previous seed example. All subsequent selections must use the*/} + {/*same commit SHA for consistency.*/} )} diff --git a/src/components/Contribute/Knowledge/ReviewSubmission/index.tsx b/src/components/Contribute/Knowledge/ReviewSubmission/index.tsx index 577fa5da..3e39715c 100644 --- a/src/components/Contribute/Knowledge/ReviewSubmission/index.tsx +++ b/src/components/Contribute/Knowledge/ReviewSubmission/index.tsx @@ -23,19 +23,19 @@ export const ReviewSubmission: React.FC = ({ knowledgeFor {/* Knowledge Information */}

Knowledge Information

- Submission Summary: {knowledgeFormData.submissionSummary || 'N/A'} + Submission Summary: {knowledgeFormData.submissionSummary}

- Domain: {knowledgeFormData.domain || 'N/A'} + Domain: {knowledgeFormData.domain}

- Document Outline: {knowledgeFormData.documentOutline || 'N/A'} + Document Outline: {knowledgeFormData.documentOutline}

{/* File Path Information */}

File Path Information

- File Path: {knowledgeFormData.filePath || 'N/A'} + File Path: {knowledgeFormData.filePath}

{/* Seed Examples */} @@ -44,15 +44,15 @@ export const ReviewSubmission: React.FC = ({ knowledgeFor

Seed Example {index + 1}

- Context: {seedExample.context || 'N/A'} + Context: {seedExample.context}

{seedExample.questionAndAnswers.map((qa, qaIndex) => (

- Question {qaIndex + 1}: {qa.question || 'N/A'} + Question {qaIndex + 1}: {qa.question}

- Answer {qaIndex + 1}: {qa.answer || 'N/A'} + Answer {qaIndex + 1}: {qa.answer}

))} @@ -61,38 +61,32 @@ export const ReviewSubmission: React.FC = ({ knowledgeFor {/* Document Information */}

Document Information

- {knowledgeFormData.knowledgeDocumentRepositoryUrl && knowledgeFormData.knowledgeDocumentCommit ? ( -
-

- Repository URL: {knowledgeFormData.knowledgeDocumentRepositoryUrl} -

-

- Commit SHA: {knowledgeFormData.knowledgeDocumentCommit} -

-

- Document Names: {knowledgeFormData.documentName || 'N/A'} -

-
- ) : ( -

No Document Information Provided.

- )} +

+ Repository URL: {knowledgeFormData.knowledgeDocumentRepositoryUrl} +

+

+ Commit: {knowledgeFormData.knowledgeDocumentCommit} +

+

+ Document Name: {knowledgeFormData.documentName} +

{/* Attribution Information */}

Attribution Information

- Title of Work: {knowledgeFormData.titleWork || 'N/A'} + Title of Work: {knowledgeFormData.titleWork}

- Link to Work: {knowledgeFormData.linkWork || 'N/A'} + Link to Work: {knowledgeFormData.linkWork}

- Revision: {knowledgeFormData.revision || 'N/A'} + Revision: {knowledgeFormData.revision}

- License of Work: {knowledgeFormData.licenseWork || 'N/A'} + License of Work: {knowledgeFormData.licenseWork}

- Creators: {knowledgeFormData.creators || 'N/A'} + Creators: {knowledgeFormData.creators}

);