Skip to content

Commit

Permalink
Co-authored-by: ThomasDh-C <ThomasDh-C@users.noreply.github.com>
Browse files Browse the repository at this point in the history
  • Loading branch information
areibman committed May 12, 2024
1 parent 7314e8d commit 21af4a7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
55 changes: 51 additions & 4 deletions electron-react-app/src/renderer/components/MainScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,47 @@ function preorderTraversal(
return result;
}

function buildTree(paths) {
const root = { name: 'root', children: [] };

paths.forEach(({ dst_path }) => {
const parts = dst_path.split('/');
let currentLevel = root.children;

parts.forEach((part, index) => {
let existingPath = currentLevel.find(p => p.name === part);

if (!existingPath) {
if (index === parts.length - 1) {
// It's a file
existingPath = { name: part };
} else {
// It's a directory
existingPath = { name: part, children: [] };
}
currentLevel.push(existingPath);
}

if (existingPath.children) {
currentLevel = existingPath.children;
}
});
});

return root;
}

function MainScreen() {
const [selectedFile, setSelectedFile] = useState(null);
const [filePath, setFilePath] = useState('');

// Function to handle file selection
const handleFileSelect = (fileData: any) => {
setSelectedFile(fileData);
};

const preOrderedFiles = preorderTraversal(files, '', -1).slice(1);
const [preOrderedFiles, setPreOrderedFiles] = useState([]);
// const preOrderedFiles = preorderTraversal(files, '', -1).slice(1);
const [acceptedState, setAcceptedState] = React.useState(
preOrderedFiles.reduce(
(acc, file) => ({ ...acc, [file.fullfilename]: false }),
Expand All @@ -59,10 +91,24 @@ function MainScreen() {
);

const handleBatch = async () => {
const response = await fetch('http://example.com/files');
const response = await fetch('http://localhost:8000/batch', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ path: filePath }),
});
const data = await response.json();
console.log(data);
// setFilesReturned(data);
const treeData = buildTree(data);
const preOrderedTreeData = preorderTraversal(treeData, '', -1).slice(1);
console.log(treeData);
setPreOrderedFiles(preOrderedTreeData);
setAcceptedState(
preOrderedTreeData.reduce(
(acc, file) => ({ ...acc, [file.fullfilename]: false }),
{},
),
);
};

// Add the className 'dark' to main div to enable dark mode
Expand All @@ -84,6 +130,7 @@ function MainScreen() {
className="flex-1 rounded-lg"
placeholder="Enter file path"
type="text"
onChange={(e) => setFilePath(e.target.value)}
/>
</div>
<div className="flex-1" />
Expand Down
2 changes: 1 addition & 1 deletion electron-react-app/src/renderer/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
http-equiv="Content-Security-Policy"
content="script-src 'self' 'unsafe-inline'"
/>
<title>Hello Electron React!</title>
<title>LlamaFS</title>
</head>
<body>
<div id="root"></div>
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ click
asciitree
fastapi
weave
# agentops
# langchain
# langchain_core
agentops
langchain
langchain_core
watchdog
19 changes: 17 additions & 2 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from asciitree.drawing import BOX_LIGHT, BoxStyle
from fastapi import FastAPI, HTTPException
from fastapi.responses import StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
from groq import Groq
from llama_index.core import SimpleDirectoryReader
from pydantic import BaseModel
Expand Down Expand Up @@ -42,6 +43,18 @@ class CommitRequest(Request):

app = FastAPI()

origins = [
"*"
]

app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"], # Or restrict to ['POST', 'GET', etc.]
allow_headers=["*"],
)


@app.get("/")
async def root():
Expand All @@ -53,7 +66,8 @@ async def batch(request: Request):

path = request.path
if not os.path.exists(path):
raise HTTPException(status_code=400, detail="Path does not exist in filesystem")
raise HTTPException(
status_code=400, detail="Path does not exist in filesystem")

summaries = await get_dir_summaries(path)
# Get file tree
Expand Down Expand Up @@ -84,7 +98,8 @@ async def batch(request: Request):
async def watch(request: Request):
path = request.path
if not os.path.exists(path):
raise HTTPException(status_code=400, detail="Path does not exist in filesystem")
raise HTTPException(
status_code=400, detail="Path does not exist in filesystem")

response_queue = queue.Queue()

Expand Down

0 comments on commit 21af4a7

Please sign in to comment.