Skip to content

Commit

Permalink
Remove validation for repo URL in native mode
Browse files Browse the repository at this point in the history
- Also change the field name to include server side filepath.

Signed-off-by: Brent Salisbury <[email protected]>
  • Loading branch information
nerdalert committed Dec 24, 2024
1 parent d9c6d17 commit 184a44c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 80 deletions.
46 changes: 34 additions & 12 deletions src/app/api/native/upload/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}

Expand All @@ -51,27 +54,45 @@ export async function POST(req: NextRequest) {
.join(', ')}\n\nSigned-off-by: [email protected]`
});

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: ''
},
{ 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...`);
Expand All @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,6 @@ const DocumentInformation: React.FC<Props> = ({
}
}, [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) {
Expand Down Expand Up @@ -283,7 +264,7 @@ const DocumentInformation: React.FC<Props> = ({
</Modal>
{!useFileUpload ? (
<>
<FormGroup isRequired key={'doc-info-details-id'} label="Repo URL">
<FormGroup isRequired key={'doc-info-details-id'} label="Repo URL or Server Side File Path">
<TextInput
isRequired
type="url"
Expand All @@ -292,26 +273,7 @@ const DocumentInformation: React.FC<Props> = ({
placeholder="Enter repo URL where document exists"
value={knowledgeDocumentRepositoryUrl}
onChange={(_event, value) => setKnowledgeDocumentRepositoryUrl(value)}
onBlur={() => validateRepo(knowledgeDocumentRepositoryUrl)}
/>
{validRepo === ValidatedOptions.error && (
<FormHelperText>
<HelperText>
<HelperTextItem icon={<ExclamationCircleIcon />} variant={validRepo}>
Required field
</HelperTextItem>
</HelperText>
</FormHelperText>
)}
{validRepo === ValidatedOptions.warning && (
<FormHelperText>
<HelperText>
<HelperTextItem icon={<ExclamationCircleIcon />} variant="error">
Please enter a valid URL.
</HelperTextItem>
</HelperText>
</FormHelperText>
)}
</FormGroup>
<FormGroup isRequired key={'doc-info-details-commit_sha'} label="Commit SHA">
<TextInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,17 @@ const KnowledgeQuestionAnswerPairsNative: React.FC<Props> = ({
color: '#333'
}}
>
A commit SHA (<strong>{commitSha}</strong>) has already been selected in a previous seed example. All subsequent selections must use the
same commit SHA for consistency.
<Alert
variant="warning"
isInline
title="All knowledge files need to originate from the same commit or 'Document Information' submission"
style={{ marginBottom: '20px' }}
>
A commit SHA (<strong>{commitSha}</strong>) has already been selected in a previous seed example. All subsequent selections must use the
same commit SHA for consistency.
</Alert>
{/*A commit SHA (<strong>{commitSha}</strong>) has already been selected in a previous seed example. All subsequent selections must use the*/}
{/*same commit SHA for consistency.*/}
</div>
)}

Expand Down
48 changes: 21 additions & 27 deletions src/components/Contribute/Knowledge/ReviewSubmission/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ export const ReviewSubmission: React.FC<ReviewSubmissionProps> = ({ knowledgeFor
{/* Knowledge Information */}
<h3>Knowledge Information</h3>
<p>
<strong>Submission Summary:</strong> {knowledgeFormData.submissionSummary || 'N/A'}
<strong>Submission Summary:</strong> {knowledgeFormData.submissionSummary}
</p>
<p>
<strong>Domain:</strong> {knowledgeFormData.domain || 'N/A'}
<strong>Domain:</strong> {knowledgeFormData.domain}
</p>
<p>
<strong>Document Outline:</strong> {knowledgeFormData.documentOutline || 'N/A'}
<strong>Document Outline:</strong> {knowledgeFormData.documentOutline}
</p>

{/* File Path Information */}
<h3>File Path Information</h3>
<p>
<strong>File Path:</strong> {knowledgeFormData.filePath || 'N/A'}
<strong>File Path:</strong> {knowledgeFormData.filePath}
</p>

{/* Seed Examples */}
Expand All @@ -44,15 +44,15 @@ export const ReviewSubmission: React.FC<ReviewSubmissionProps> = ({ knowledgeFor
<div key={index}>
<h4>Seed Example {index + 1}</h4>
<p>
<strong>Context:</strong> {seedExample.context || 'N/A'}
<strong>Context:</strong> {seedExample.context}
</p>
{seedExample.questionAndAnswers.map((qa, qaIndex) => (
<div key={qaIndex}>
<p>
<strong>Question {qaIndex + 1}:</strong> {qa.question || 'N/A'}
<strong>Question {qaIndex + 1}:</strong> {qa.question}
</p>
<p>
<strong>Answer {qaIndex + 1}:</strong> {qa.answer || 'N/A'}
<strong>Answer {qaIndex + 1}:</strong> {qa.answer}
</p>
</div>
))}
Expand All @@ -61,38 +61,32 @@ export const ReviewSubmission: React.FC<ReviewSubmissionProps> = ({ knowledgeFor

{/* Document Information */}
<h3>Document Information</h3>
{knowledgeFormData.knowledgeDocumentRepositoryUrl && knowledgeFormData.knowledgeDocumentCommit ? (
<div>
<p>
<strong>Repository URL:</strong> {knowledgeFormData.knowledgeDocumentRepositoryUrl}
</p>
<p>
<strong>Commit SHA:</strong> {knowledgeFormData.knowledgeDocumentCommit}
</p>
<p>
<strong>Document Names:</strong> {knowledgeFormData.documentName || 'N/A'}
</p>
</div>
) : (
<p>No Document Information Provided.</p>
)}
<p>
<strong>Repository URL:</strong> {knowledgeFormData.knowledgeDocumentRepositoryUrl}
</p>
<p>
<strong>Commit:</strong> {knowledgeFormData.knowledgeDocumentCommit}
</p>
<p>
<strong>Document Name:</strong> {knowledgeFormData.documentName}
</p>

{/* Attribution Information */}
<h3>Attribution Information</h3>
<p>
<strong>Title of Work:</strong> {knowledgeFormData.titleWork || 'N/A'}
<strong>Title of Work:</strong> {knowledgeFormData.titleWork}
</p>
<p>
<strong>Link to Work:</strong> {knowledgeFormData.linkWork || 'N/A'}
<strong>Link to Work:</strong> {knowledgeFormData.linkWork}
</p>
<p>
<strong>Revision:</strong> {knowledgeFormData.revision || 'N/A'}
<strong>Revision:</strong> {knowledgeFormData.revision}
</p>
<p>
<strong>License of Work:</strong> {knowledgeFormData.licenseWork || 'N/A'}
<strong>License of Work:</strong> {knowledgeFormData.licenseWork}
</p>
<p>
<strong>Creators:</strong> {knowledgeFormData.creators || 'N/A'}
<strong>Creators:</strong> {knowledgeFormData.creators}
</p>
</div>
);
Expand Down

0 comments on commit 184a44c

Please sign in to comment.