import React from 'react'; import { Header, Segment, Form, Button, Icon, Message, Dropdown, Input, TextArea } from 'semantic-ui-react'; import superagent from 'superagent'; import ExperimentalWrapper from './ExperimentalWrapper.jsx'; export default class KBUpload extends React.Component { constructor(props) { super(props); this.state = { sourceType: 'file', sourceId: '', sourceUrl: '', title: '', content: '', visibility: 'internal', loading: false, error: null, success: null }; } async onSubmit(e) { e.preventDefault(); const { sourceType, sourceId, sourceUrl, title, content, visibility } = this.state; if (!sourceId.trim()) { this.setState({ error: 'Source ID is required' }); return; } if (!content.trim()) { this.setState({ error: 'Content is required' }); return; } this.setState({ loading: true, error: null, success: null }); try { const res = await superagent .post('/api/admin/kb/ingest/document') .set('Content-Type', 'application/json') .accept('json') .send({ sourceType, sourceId: sourceId.trim(), sourceUrl: sourceUrl.trim() || undefined, title: title.trim() || undefined, content: content.trim(), visibility }); if (!res.body || !res.body.documentId) { throw new Error('Invalid response from server'); } this.setState({ loading: false, success: { documentId: res.body.documentId, chunkCount: res.body.chunkCount }, sourceId: '', sourceUrl: '', title: '', content: '' }); } catch (error) { console.error('Upload failed:', error); const errorMsg = error.response?.body?.details || error.response?.body?.error || error.message || 'Unknown error occurred'; this.setState({ error: errorMsg, loading: false }); } } render() { const { sourceType, sourceId, sourceUrl, title, content, visibility, loading, error, success } = this.state; const sourceTypeOptions = [ { key: 'file', text: 'File', value: 'file' }, { key: 'google_drive', text: 'Google Drive', value: 'google_drive' }, { key: 'notion', text: 'Notion', value: 'notion' }, { key: 'slack', text: 'Slack', value: 'slack' } ]; const visibilityOptions = [ { key: 'internal', text: 'Internal (all authenticated users)', value: 'internal' }, { key: 'public', text: 'Public', value: 'public' }, { key: 'restricted', text: 'Restricted (specific groups)', value: 'restricted' } ]; return (
Upload Document Add a new document to the knowledge base
{success && ( Document Uploaded Successfully

Document ID: {success.documentId}
Chunks created: {success.chunkCount}

)} {error && ( Upload Failed

{error}

)}
this.setState({ sourceType: value })} disabled={loading} /> this.setState({ sourceId: e.target.value })} disabled={loading} /> this.setState({ title: e.target.value })} disabled={loading} /> this.setState({ sourceUrl: e.target.value })} disabled={loading} /> this.setState({ visibility: value })} disabled={loading} />