Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 3x 3x 3x 3x 10x | import { useState, useRef } from 'react';
import { api } from '../api';
import { vscodeNative } from '../vscodeNative';
export function useWorkspace({ notify, logAgentAction }) {
const [activeWorkspace, setActiveWorkspace] = useState(null);
const [workspaceList, setWorkspaceList] = useState([]);
const [workspaceReady, setWorkspaceReady] = useState(false);
const [workspaceFiles, setWorkspaceFiles] = useState([]);
const [workspaceCache, setWorkspaceCache] = useState({});
const [isIndexing, setIsIndexing] = useState(false);
const fileInputRef = useRef(null);
const getWorkspaceFileContext = (filePath) => workspaceCache[filePath] || "File not found.";
const handleDirectorySelect = async (e) => {
const files = Array.from(e.target.files);
if (files.length === 0) return;
setIsIndexing(true);
logAgentAction("Workspace", `Indexing research documents...`);
const cache = {};
const fileList = [];
for (const file of files) {
const isText = file.type.startsWith('text/') || file.name.endsWith('.md') || file.name.endsWith('.txt') || file.name.endsWith('.js') || file.name.endsWith('.py') || file.name.endsWith('.csv') || file.name.endsWith('.tex');
if (isText && file.size < 1024 * 500) {
try {
const content = await file.text();
const relativePath = file.webkitRelativePath || file.name;
cache[relativePath] = content;
fileList.push(relativePath);
} catch (err) { console.error(err); }
}
}
setWorkspaceCache(cache);
setWorkspaceFiles(fileList);
setIsIndexing(false);
logAgentAction("Workspace Active", `${fileList.length} documents connected.`);
};
const openPapersFolder = async () => {
const folder = await vscodeNative.pickFolder();
if (!folder) return;
setIsIndexing(true);
const progress = vscodeNative.startProgress('Indexing research documents...');
logAgentAction("Workspace", `Indexing research documents from ${folder}...`);
try {
const data = await api.indexWorkspace(folder);
if (data.error) { logAgentAction("Error", data.error); setIsIndexing(false); progress.done(); return; }
const cache = {};
const fileList = [];
(data.files || []).forEach(f => { cache[f.path] = f.content; fileList.push(f.path); });
setWorkspaceCache(cache);
setWorkspaceFiles(fileList);
logAgentAction("Workspace Active", `${fileList.length} documents connected.`);
api.updateWorkspace({ papersFolder: folder }).catch(err => console.warn('Sync:', err.message));
} catch (err) {
logAgentAction("Error", err.message);
}
setIsIndexing(false);
progress.done();
};
const indexPapersFolder = async (folder) => {
setIsIndexing(true);
try {
const data = await api.indexWorkspace(folder);
if (!data.error && data.files) {
const cache = {};
const fileList = [];
data.files.forEach(f => { cache[f.path] = f.content; fileList.push(f.path); });
setWorkspaceCache(cache);
setWorkspaceFiles(fileList);
logAgentAction("Workspace Restored", `${fileList.length} documents from ${folder}`);
}
} catch (err) { /* ignore */ }
setIsIndexing(false);
};
const loadWorkspaceData = async (ws, { setMessages, setKnowledge, setJournalEntries, setResearchMission, setResearchRoadmap, setCycleCount }) => {
setActiveWorkspace(ws);
setWorkspaceReady(true);
if (ws.researchMission) setResearchMission(ws.researchMission);
if (ws.researchRoadmap?.length > 0) setResearchRoadmap(ws.researchRoadmap);
if (ws.cycleCount) setCycleCount(ws.cycleCount);
api.getMessages().then(msgs => setMessages(msgs)).catch(() => notify('Failed to load messages'));
api.getKnowledge().then(items => setKnowledge(items)).catch(() => notify('Failed to load knowledge'));
api.getJournal().then(entries => setJournalEntries(entries)).catch(() => notify('Failed to load journal'));
if (ws.papersFolder) {
await indexPapersFolder(ws.papersFolder);
}
};
const handleOpenWorkspace = async (folderPath, name, callbacks) => {
try {
const ws = await api.openWorkspace(folderPath, name);
if (ws.error) { logAgentAction("Error", ws.error); return; }
callbacks.setMessages([]);
callbacks.setKnowledge([]);
callbacks.setJournalEntries([]);
callbacks.setSelectedArtifact(null);
callbacks.setResearchMission('');
callbacks.setResearchRoadmap([]);
callbacks.setCycleCount(0);
setWorkspaceCache({});
setWorkspaceFiles([]);
await loadWorkspaceData(ws, callbacks);
const list = await api.getWorkspaces();
setWorkspaceList(list);
} catch (err) { logAgentAction("Error", err.message); }
};
const showWorkspacePicker = async (callbacks) => {
const workspaces = await api.getWorkspaces().catch(() => []);
const items = [
{ label: '$(folder-opened) Browse for folder...', detail: 'browse', alwaysShow: true },
...workspaces.map(ws => ({
label: ws.name,
description: ws.path,
detail: ws.path,
})),
];
const picked = await vscodeNative.quickPick(items, { placeHolder: 'Select a research workspace' });
Eif (!picked) return;
if (picked.detail === 'browse') {
const folder = await vscodeNative.pickFolder();
if (folder) {
await handleOpenWorkspace(folder, undefined, callbacks);
}
} else {
await handleOpenWorkspace(picked.detail, undefined, callbacks);
}
};
return {
activeWorkspace, setActiveWorkspace,
workspaceList, setWorkspaceList,
workspaceReady, setWorkspaceReady,
workspaceFiles, setWorkspaceFiles,
workspaceCache, setWorkspaceCache,
isIndexing, setIsIndexing,
fileInputRef,
getWorkspaceFileContext,
handleDirectorySelect,
openPapersFolder,
indexPapersFolder,
loadWorkspaceData,
handleOpenWorkspace,
showWorkspacePicker,
};
}
|