Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Add FS explorer for uploaded logs [WIP]
Tomas Valenta1c943d182d09
diff --git a/components/AgentLogFilesList.tsx b/components/AgentLogFilesList.tsxindex dcf438f67..e7233ae76 100644--- a/components/AgentLogFilesList.tsx+++ b/components/AgentLogFilesList.tsx@@ -1,11 +1,11 @@import {+ useMemo,useRef,useState,} from 'react'import {ChevronRight,} from 'lucide-react'-import Link from 'next/link'import clsx from 'clsx'import { useRouter } from 'next/router'import Splitter from '@devbookhq/splitter'@@ -20,8 +20,7 @@ import {} from 'utils/agentLogs'import Spinner from 'components/Spinner'import AgentLogFileContent from 'components/AgentLogFileContent'-import { useGetLogFile } from 'hooks/useGetLogFile'-import Uploadtree from 'components/Uploadtree'+import UploadTree from './UploadTree'export interface Props {logUploads: LiteLogUpload[]@@ -32,7 +31,6 @@ function AgentLogFilesList({logUploads,defaultProjectID,}: Props) {- const getLogFile = useGetLogFile()const [isUploading, setIsUploading] = useState(false)const [openedLogUploads, setOpeneLogUploads] = useState<string[]>([])const [selectedLogFile, setSelectedLogFile] = useState<Omit<log_files, 'project_id' | 'type' | 'size' | 'log_upload_id' | 'content' | 'last_modified'> & { content: AgentPromptLogs | AgentNextActionLog }>()@@ -40,7 +38,7 @@ function AgentLogFilesList({const fileInput = useRef<HTMLInputElement>(null)const uploadFiles = useUploadLogs(defaultProjectID)- const sortedLogUploads = logUploads+ const sortedLogUploads = useMemo(() => logUploads// logUploads sorted by created_at - the newest first.sort((a, b) => {if (a.created_at > b.created_at) return -1@@ -59,8 +57,7 @@ function AgentLogFilesList({...logUpload,log_files: sortedLogFiles,}- })-+ }), [logUploads])function handleClickOnUpload() {// trigger the click event of the file input@@ -116,42 +113,6 @@ function AgentLogFilesList({return `/?${new URLSearchParams(query).toString()}`}- async function selectLogFile(logFile: any) {- // Log files in logUploads don't have content. We fetch the full log file lazily.- // setSelectedLogFile()-- // const logFile = await getLogFile(logFileID)- // if (!logFile) return--- // const parsedFileContent = JSON.parse(logFile.content)-- // // Specific to AutoGPT- // if (logFile.filename.includes('next_action')) {- // setSelectedLogFile({- // ...logFile,- // content: parsedFileContent,- // })- // } else if (logFile.filename.includes('full_message_history') || logFile.filename.includes('current_context')) {- // setSelectedLogFile({- // ...logFile,- // content: {- // //logs: parsedFileContent as AgentNextActionLog,- // logs: parsedFileContent as any,- // },- // })- // } else {- // setSelectedLogFile({- // ...logFile,- // content: {- // ...parsedFileContent,- // logs: parsedFileContent?.context || [],- // context: undefined,- // },- // })- // }- }-return (<main className="overflow-hidden flex flex-col max-h-full flex-1"><input@@ -195,7 +156,7 @@ function AgentLogFilesList({classes={['flex pr-2 overflow-auto', 'bg-gray-900 flex pl-2']}><div className="flex flex-col space-y-4 p-4 sm:p-6 lg:px-8 overflow-auto min-w-[320px]">- <Uploadtree logUploads={logUploads} />+ {/* <Uploadtree logUploads={logUploads} /> */}{sortedLogUploads.map((logUpload, i) => (<divkey={logUpload.id}@@ -235,38 +196,9 @@ function AgentLogFilesList({{openedLogUploads.includes(logUpload.id) && (<div className="flex flex-col space-y-3 border-l border-gray-800 pl-2 ml-[10px]">- {logUpload.log_files.map((logFile) => (- <span- key={logFile.id}- className={clsx(- 'rounded-md',- 'py-0.5',- 'px-2',- 'text-gray-200',- 'hover:bg-[#1F2437]',- 'transition-all',- 'w-full',- 'text-sm',- 'cursor-pointer',- 'font-mono',- 'flex',- 'items-center',- 'space-x-2',- )}- >- <Link- href={logFileURL(logFile.id)}- shallow={true}- onClick={() => {- setSelectedLogFile(logFile)- }}- >- {logFile.relativePath.split('/').map(p => (- <span key={p}>{'/ '}{p}</span>- ))}- </Link>- </span>- ))}+ <UploadTree+ logUpload={logUpload}+ /></div>)}</div>diff --git a/components/DirWrapper.tsx b/components/DirWrapper.tsxnew file mode 100644index 000000000..78b42e582--- /dev/null+++ b/components/DirWrapper.tsx@@ -0,0 +1,113 @@+import React, { memo } from 'react'++import Text from 'components/Text'++import {+ Metadata,+ NodeType,+} from '../filesystem/node'+import { useChildren } from '../hooks/filesystem'+import Dir from '../filesystem/dir'+import Filesystem from '../filesystem/filesystem'++import {+ FileProps,+ SelectHandler,+} from './FilesystemTree'++// DirProps are intended for a use by outside users of the Filesystem component.+export type DirProps = Pick<+ Props,+ 'fs' |+ 'name' |+ 'path' |+ 'level' |+ 'metadata' |+ 'isExpanded' |+ 'isSelected' |+ 'onSelect' |+ 'onSelect'+> & { children: React.ReactNode }++export interface Props {+ fs: Filesystem+ path: string+ name: string+ level: number+ metadata: Metadata+ isExpanded: boolean+ isSelected: boolean+ onSelect?: SelectHandler+ file: React.ComponentType<FileProps>+ dir: React.ComponentType<DirProps>+}++const DirWrapper = memo(function ({+ path,+ fs,+ name,+ level,+ metadata,+ onSelect,+ file: UserFile,+ dir: UserDir,+ isExpanded,+ isSelected,+}: Props) {+ const nodeChildren = useChildren(fs, path, isExpanded)++ return (+ <UserDir+ fs={fs}+ isExpanded={isExpanded}+ isSelected={isSelected}+ level={level}+ metadata={metadata}+ name={name}+ path={path}+ onSelect={onSelect}+ >+ {!nodeChildren || nodeChildren.length === 0 &&+ <Text+ className="text-center text-gray-500"+ size={Text.size.S3}+ text="Empty directory"+ />+ }+ {nodeChildren.length > 0 && nodeChildren.map(n => (+ <React.Fragment key={n.path}>+ {n.type === NodeType.Dir &&+ <DirWrapper+ dir={UserDir}+ file={UserFile}+ fs={fs}+ isExpanded={(n as Dir).isExpanded}+ isSelected={n.isSelected}+ key={n.path}+ level={n.level}+ metadata={n.metadata}+ name={n.name}+ path={n.path}+ onSelect={onSelect}+ />+ }+ {n.type === NodeType.File &&+ <UserFile+ fs={fs}+ isSelected={n.isSelected}+ key={n.path}+ level={n.level}+ metadata={n.metadata}+ name={n.name}+ path={n.path}+ onSelect={onSelect}+ />+ }+ </React.Fragment>+ ))}+ </UserDir>+ )+})++DirWrapper.displayName = 'DirWrapper'+export default DirWrapperdiff --git a/components/Filesystem/Dir.tsx b/components/Filesystem/Dir.tsxnew file mode 100644index 000000000..925a75c0a--- /dev/null+++ b/components/Filesystem/Dir.tsx@@ -0,0 +1,95 @@+import {+ ChevronDown as ChevronDownIcon,+ ChevronRight as ChevronRightIcon,+} from 'lucide-react'+import clsx from 'clsx'++import {+ DirProps,+ NodeType,+} from '../../filesystem'++function Dir({+ name,+ path,+ onSelect,+ metadata,+ children,+ fs,+ isExpanded,+ isSelected,+}: DirProps) {+ function handleOnClick(e: any) {+ fs.setIsDirExpanded(path, !isExpanded)++ // This conditions prevents deselecting when user clicks on a dir that's already selected.+ if (path !== fs.selectedPath) {+ fs.setIsNodeSelected(path, !isSelected)+ }++ onSelect?.(e,+ {+ type: NodeType.Dir,+ path,+ name,+ metadata,+ isSelected,+ isExpanded,+ })+ }++ const icon = isExpanded ? (+ <ChevronDownIcon+ className="text-white/60"+ size={16}+ />+ ) : (+ <ChevronRightIcon+ className="text-white/60"+ size={16}+ />+ )++ return (+ <div className="flex flex-col rounded">+ <div+ className={clsx(+ 'px-1',+ 'flex',+ 'items-center',+ 'rounded',+ 'space-x-1',+ 'cursor-pointer',+ 'hover:bg-gray-700/80',+ 'text-gray-300',+ { 'bg-gray-700': isSelected },+ { 'bg-transparent': !isSelected },+ )}+ onClick={handleOnClick}+ >+ {icon}+ <span className="truncate text-gray-400">{name}</span>+ </div>++ {isExpanded && (+ <div+ className="+ flex+ flex-col+ space-y-2+ lg:space-y-1+ mt-1+ ml-1+ pl-3+ border-l+ border-gray-700+ "+ >+ {children}+ </div>+ )}+ </div>+ )+}++export default Dirdiff --git a/components/Filesystem/File.tsx b/components/Filesystem/File.tsxnew file mode 100644index 000000000..616909dff--- /dev/null+++ b/components/Filesystem/File.tsx@@ -0,0 +1,92 @@+import {+ Database as DatabaseIcon,+ File as FileIcon,+ Settings as SettingsIcon,+} from 'lucide-react'+import clsx from 'clsx'+import path from 'path-browserify'++import {+ FileProps,+ NodeType,+} from '../../filesystem'++function File({+ name,+ path: filePath,+ onSelect,+ isSelected,+ metadata,+ fs,+}: FileProps) {+ function handleOnClick(e: any) {+ // This conditions prevents deselecting when user clicks on a file that's already selected.+ if (filePath !== fs.selectedPath) {+ fs.setIsNodeSelected(filePath, !isSelected)+ }+ onSelect?.(e,+ {+ type: NodeType.File,+ path: filePath,+ name,+ metadata,+ isSelected,+ isExpanded: false,+ })+ }+++ let icon = (+ <FileIcon+ size={16}+ />+ )++ const ext = path.extname(filePath).substring(1)+ if (ext === 'sql') {+ icon = (+ <DatabaseIcon+ className="+ text-red-500+ "+ size={16}+ />+ )+ } else if (ext === 'toml') {+ icon = (+ <SettingsIcon+ className="+ text-teal-800+ "+ size={16}+ />+ )+ }++ return (+ <div+ className={clsx(+ 'px-1',+ 'rounded',+ 'cursor-pointer',+ 'text-gray-300',+ 'flex',+ 'items-center',+ 'space-x-1',+ 'hover:bg-gray-700/80',+ { 'bg-gray-700': isSelected },+ { 'bg-transparent': !isSelected }+ )}+ onClick={handleOnClick}+ >+ {icon}+ <span+ className='truncate text-gray-400'+ >+ {name}+ </span>+ </div>+ )+}++export default Filediff --git a/components/Filesystem/index.tsx b/components/Filesystem/index.tsxnew file mode 100644index 000000000..e4f7e7a63--- /dev/null+++ b/components/Filesystem/index.tsx@@ -0,0 +1,122 @@+import {+ useCallback,+ useEffect,+} from 'react'+import clsx from 'clsx'++import FilesystemPrimitive, {+ DirNode,+ FileNode,+ NodeType,+ SelectHandler,+ useFilesystem,+} from '../../filesystem'++import Dir from './Dir'+import File from './File'++export interface FileInfo {+ isDir: boolean+ name: string+}++export interface Props {+ className?: string+ // Path where we should mount the filesytem.+ rootPath: string+ ignore?: string[]+ onFiletreeClick?: (path: string, type: NodeType) => void+ fetchContent: (dirpath: string) => FileInfo[]+}++function Filesystem({+ className,+ rootPath,+ ignore,+ fetchContent,+ onFiletreeClick,+}: Props) {+ const fs = useFilesystem({ rootPath })++ const fetchDirContent = useCallback(async (dirpath: string) => {+ const files = fetchContent(dirpath)+ const ns = files.map(f => (f.isDir ? new DirNode({ name: f.name }) : new FileNode({ name: f.name })))++ fs.add(dirpath, ns, ignore)+ }, [+ fs,+ fetchContent,+ ignore,+ ])++ const handleNodeSelect: SelectHandler = useCallback(async (_, node) => {+ const {+ type,+ path,+ metadata,+ isExpanded,+ } = node++ onFiletreeClick?.(path, type)++ // User is closing dir.+ if (isExpanded) return++ if (type === NodeType.Dir) {+ if (!metadata['isWatching']) {+ fs.setMetadata(path, {+ key: 'isWatching',+ value: true,+ })++ if (!fs.hasChildren(path)) {+ fetchDirContent(path)+ }+ }+ } else if (type === NodeType.File) {+ // TODO: Open file+ }+ },+ [+ fs,+ onFiletreeClick,+ fetchDirContent,+ ],)++ useEffect(function mountFilesystem() {+ fetchDirContent(rootPath)+ }, [+ rootPath,+ fetchDirContent,+ ])++ return (+ <div className="+ mt-2+ flex+ flex-col+ space-y-2+ py-2+ rounded-lg+ ">+ <FilesystemPrimitive.Tree+ className={clsx(+ 'px-2',+ 'flex-1',+ 'flex',+ 'flex-col',+ 'space-y-2',+ 'lg:space-y-1',+ 'overflow-y-auto',+ className,+ )}+ dir={Dir}+ file={File}+ fs={fs}+ onSelect={handleNodeSelect}+ />+ </div>+ )+}++export default Filesystemdiff --git a/components/FilesystemTree.tsx b/components/FilesystemTree.tsxnew file mode 100644index 000000000..daf691ad4--- /dev/null+++ b/components/FilesystemTree.tsx@@ -0,0 +1,117 @@+import React, {+ memo,+ useCallback,+ useMemo,+} from 'react'++import {+ Metadata,+ NodeType,+} from 'filesystem/node'+import { useChildren } from 'hooks/filesystem'+import Dir from 'filesystem/dir'+import DirWrapper, { DirProps } from './DirWrapper'++import Text from 'components/Text'+import { useFiletree } from 'hooks/useFiletree'+import Filesystem from 'filesystem/filesystem'++export type SelectHandler = (+ event: MouseEvent | TouchEvent,+ node: {+ type: NodeType+ name: string+ path: string+ metadata: Metadata+ isSelected: boolean+ isExpanded: boolean+ },+) => void++export interface FileProps {+ fs: Filesystem+ name: string+ path: string+ level: number+ metadata: Metadata+ isSelected: boolean+ onSelect?: SelectHandler+}++export interface Props {+ className?: string+ fs: Filesystem+ file?: React.ComponentType<FileProps>+ dir?: React.ComponentType<DirProps>+ onSelect?: SelectHandler+}++function FilesystemTree({+ className,+ fs,+ file,+ dir,+ onSelect,+}: Props) {+ const nodeChildren = useChildren(fs, fs.root.path, true)+ const UserFile = useMemo(() => (file ? memo(file) : () => null), [file])+ const UserDir = useMemo(() => (dir ? memo(dir) : () => null), [dir])++ const ft = useFiletree()++ const handleSelect: SelectHandler = useCallback((e, n) => {+ onSelect?.(e, n)+ ft.select({+ type: n.type,+ path: n.path,+ })+ }, [ft, onSelect])++ return (+ <div className={className}>+ {nodeChildren.length === 0 &&+ <Text+ className="text-center text-gray-500"+ size={Text.size.S3}+ text="Empty directory"+ />+ }++ {nodeChildren.length > 0 && nodeChildren.map(n => (+ <React.Fragment key={n.path}>+ {n.type === NodeType.Dir && (+ <DirWrapper+ dir={UserDir}+ file={UserFile}+ fs={fs}+ isExpanded={(n as Dir).isExpanded}+ isSelected={n.isSelected}+ key={n.path}+ level={n.level}+ metadata={n.metadata}+ name={n.name}+ path={n.path}+ //onSelect={onSelect}+ onSelect={handleSelect}+ />+ )}+ {n.type === NodeType.File && (+ <UserFile+ fs={fs}+ isSelected={n.isSelected}+ key={n.path}+ level={n.level}+ metadata={n.metadata}+ name={n.name}+ path={n.path}+ // onSelect={onSelect}+ onSelect={handleSelect}+ />+ )}+ </React.Fragment>+ ))}+ </div>+ )+}++export default memo(FilesystemTree)diff --git a/components/Uploadtree/NodeComponent.tsx b/components/Uploadtree/NodeComponent.tsxdeleted file mode 100644index 2ed84a636..000000000--- a/components/Uploadtree/NodeComponent.tsx+++ /dev/null@@ -1,37 +0,0 @@-// Node is either a log upload or a dir.--export interface Node {- id: string- name: string- children: Node[]-}--export interface Props {- node: Node-}--function NodeComponent({- node,-}: Props) {-- if (node.children.length === 0) {- // Leaft node- return (- <div>leaf</div>- )- }-- return (- <div className="flex flex-col">- {node.name}-- <div className="pl-2 flex flex-col">- {node.children.map(nodeChild => (- <NodeComponent key={nodeChild.id} node={nodeChild} />- ))}- </div>- </div>- )-}--export default NodeComponent\ No newline at end of filediff --git a/components/Uploadtree/index.tsx b/components/Uploadtree/index.tsxindex fb864cd04..aee463975 100644--- a/components/Uploadtree/index.tsx+++ b/components/Uploadtree/index.tsx@@ -1,84 +1,73 @@-import {- useEffect,- useMemo,- useState,-} from 'react'-import { LiteLogUpload } from 'utils/agentLogs'+import { useCallback } from 'react'+import path from 'path-browserify'+import Filesystem, { FileInfo } from 'components/Filesystem'+import { LiteLogUpload } from 'utils/agentLogs'+import { FiletreeProvider } from 'hooks/useFiletree'export interface Props {- logUploads: LiteLogUpload[]+ logUpload: LiteLogUpload}-function Uploadtree({- logUploads,+function UploadTree({+ logUpload,}: Props) {- const [openNodes, setOpenNodes] = useState<string[]>([])-- const sortedLogUploads = useMemo(() => {- return logUploads- // logUploads sorted by created_at - the newest first- .sort((a, b) => {- if (a.created_at > b.created_at) return -1- if (a.created_at < b.created_at) return 1- return 0- })- // Sort the log_files inside logUploads alphabtetical by relativePath- .map(logUpload => {- const sortedLogFiles = logUpload.log_files.sort((a, b) => {- if (a.relativePath > b.relativePath) return 1- if (a.relativePath < b.relativePath) return -1- return 0- })- return {- ...logUpload,- log_files: sortedLogFiles,- }+ const fetchLogDirContent = useCallback<(dirpath: string) => FileInfo[]>(dirpath => {+ const currentDir = dirpath.slice(1)+ const childFiles = logUpload+ .log_files+ .filter(f => {+ return path.dirname(f.relativePath) === currentDir})- }, [logUploads])- useEffect(function uploadsToTree() {- const tree = sortedLogUploads.map(logUpload => {- const relativePaths = logUpload.log_files.map(logFile => logFile.relativePath)- const relativePathsSplit = relativePaths.map(relativePath => relativePath.split('/'))+ console.log('childFiles', childFiles)- const tree = relativePathsSplit.reduce((acc, relativePathSplit) => {- let currentAcc = acc-- relativePathSplit.forEach((relativePathSplitPart, index) => {- if (currentAcc[relativePathSplitPart]) {- currentAcc = currentAcc[relativePathSplitPart]- } else {- currentAcc[relativePathSplitPart] = {}- currentAcc = currentAcc[relativePathSplitPart]- }-- if (index === relativePathSplit.length - 1) {- currentAcc = {- ...currentAcc,- id: logUpload.id,- name: relativePathSplitPart,- children: [],- }- }- })+ const filesInChildDirs = logUpload+ .log_files+ .filter(f => {+ const fileDir = path.dirname(f.relativePath)+ return fileDir !== currentDir && fileDir.startsWith(currentDir)+ })- return acc- }, {} as any)+ const childDirs = filesInChildDirs.reduce((acc, file) => {+ if (currentDir === '') {+ const fileDir = path.dirname(file.relativePath)+ const childDir = fileDir.split('/')[0]+ return acc.add(childDir)+ }+ const fileDir = path.dirname(file.relativePath)+ const childDirs = fileDir.slice(currentDir.length)+ const childDir = childDirs.split('/')[1]+ return acc.add(childDir)+ }, new Set<string>())- return tree- })+ const content = [+ ...childFiles.map(f => ({+ name: f.filename,+ isDir: false,+ })),+ ...Array.from(childDirs).map(dir => ({+ name: dir,+ isDir: true,+ })),+ ]- console.log('tree', tree)- }, [sortedLogUploads])+ console.log('content', content)+ return content+ }, [logUpload])return (<div className="flex flex-col items-start justify-start">- {/* <NodeComponent /> */}+ <FiletreeProvider>+ <Filesystem+ rootPath='/'+ fetchContent={fetchLogDirContent}+ />+ </FiletreeProvider></div>)}-export default Uploadtree\ No newline at end of file+export default UploadTreediff --git a/filesystem/dir.ts b/filesystem/dir.tsnew file mode 100644index 000000000..683d59f3b--- /dev/null+++ b/filesystem/dir.ts@@ -0,0 +1,33 @@+import Node, {+ Metadata,+ NodeType,+} from './node'++class Dir extends Node {+ readonly name: string+ readonly type = NodeType.Dir+ children = new Map<string, Node>()+ isExpanded = false++ constructor(+ {+ name,+ parent,+ metadata,+ isExpanded = false,+ }: {+ name: string,+ parent?: Dir, // Root dir doesn't have a parent.+ metadata?: Metadata,+ isExpanded?: boolean,+ }+ ) {+ super()+ this.name = name+ this.parent = parent+ if (metadata) this.metadata = metadata+ this.isExpanded = isExpanded+ }+}++export default Dirdiff --git a/filesystem/file.ts b/filesystem/file.tsnew file mode 100644index 000000000..39c52a44f--- /dev/null+++ b/filesystem/file.ts@@ -0,0 +1,27 @@+import Dir from './dir'+import Node, {+ Metadata,+ NodeType,+} from './node'++class File extends Node {+ readonly name: string+ readonly type = NodeType.File++ constructor({+ name,+ parent,+ metadata,+ }: {+ name: string,+ parent?: Dir+ metadata?: Metadata,+ }) {+ super()+ this.name = name+ this.parent = parent+ if (metadata) this.metadata = metadata+ }+}++export default Filediff --git a/filesystem/filesystem.ts b/filesystem/filesystem.tsnew file mode 100644index 000000000..a8672fcd3--- /dev/null+++ b/filesystem/filesystem.ts@@ -0,0 +1,285 @@+import { isMatch } from 'matcher'+import path from 'path-browserify'++import { logger } from '../utils/logger'+import Dir from './dir'+import Node, { NodeType } from './node'++const log = logger('filesystem')++type DirListener = (path: string, children: IterableIterator<Node>) => void+type MetadataListener<T = any> = (path: string, {+ key,+ newVal,+ oldVal,+}: { key: string; newVal?: T; oldVal?: T }) => void++class Filesystem {+ root: Dir+ private lastSelectedPath?: string+ get selectedPath() {+ return this.lastSelectedPath+ }+ private dirListeners = new Map<string, DirListener[]>()+ private metadataListeners = new Map<string, MetadataListener[]>()++ constructor(rootPath: string, initial: Node[]) {+ this.root = new Dir({ name: rootPath })+ initial.forEach(n => {+ n.parent = this.root+ this.root.children.set(n.name, n)+ })+ }++ /**+ * Removes all children and all listeners.+ */+ clear() {+ this.dirListeners.clear()+ this.metadataListeners.clear()++ this.root.children.forEach(c => {+ c.parent = undefined+ })+ this.root.children.clear()+ }++ /**+ * Registers a listener that gets notified when directory's child nodes change (a node is created or deleted).+ * @param path path to a directory+ * @param l listener function+ * @returns unsubscribe method+ */+ addDirListener(path: string, l: DirListener) {+ const ls = this.dirListeners.get(path) || []+ this.dirListeners.set(path, [...ls, l])+ return () => this.removeDirListener(path, l)+ }++ private removeDirListener(path: string, l: DirListener) {+ const ls = this.dirListeners.get(path) || []+ this.dirListeners.set(+ path,+ ls.filter(l1 => l1 !== l),+ )+ }++ private notifyDirListeners(path: string, children: IterableIterator<Node>) {+ const ls = this.dirListeners.get(path) || []+ ls.forEach(l => l(path, children))+ }++ /**+ * Registers a listener that gets notified when node's metadata value changes for the specified key.+ * @param path path to a node in the filesystem+ * @param key metadata key to listen to+ * @param l listener function+ * @returns unsubscribe method+ */+ addMetadataListener<T>(path: string, key: string, l: MetadataListener<T>) {+ const lkey = this.makeMetadataListenerKey(path, key)+ const ls = this.metadataListeners.get(lkey) || []+ this.metadataListeners.set(lkey, [...ls, l])+ return () => this.removeMetadataListener(path, key, l)+ }++ private removeMetadataListener(path: string, key: string, l: MetadataListener) {+ const lkey = this.makeMetadataListenerKey(path, key)+ const ls = this.metadataListeners.get(lkey) || []+ this.metadataListeners.set(+ lkey,+ ls.filter(l1 => l1 !== l),+ )+ }++ private notifyMetadataListeners<T>(path: string, {+ key,+ newVal,+ oldVal,+ }: { key: string; newVal?: T; oldVal?: T }) {+ const lkey = this.makeMetadataListenerKey(path, key)+ const ls = this.metadataListeners.get(lkey)+ ls?.forEach(l => {+ l(path, {+ key,+ oldVal,+ newVal,+ })+ })+ }++ /**+ * Tries to add new nodes to the specified directory.+ * @param dirpath directory where to insert a new node.+ * @param newNodes new nodes to add.+ * @returns an array of node names that couldn't be added because they already exist in the directory.+ */+ add(dirpath: string, newNodes: Node[], ignore?: string[]) {+ const n = this.find(dirpath)+ if (!n) throw new Error(`'${dirpath}': path doesn't exist on the filesystem. Can't create a new node`)+ if (n.type !== NodeType.Dir) throw new Error(`'${dirpath}': isn't a directory. Can't create a new node`)++ const dir = n as Dir++ const existing: string[] = []+ for (const nn of newNodes) {++ // Check if we should filter out a new node.+ if (ignore && ignore.length > 0) {+ if (isMatch(path.join(dir.path, nn.name), ignore)) continue+ }++ if (dir.children.has(nn.name)) {+ existing.push(nn.name)+ } else {+ nn.parent = dir+ dir.children.set(nn.name, nn)+ }+ }++ this.notifyDirListeners(dir.path, dir.children.values())+ return existing+ }++ /**+ * Tries to remove a node on the specified path.+ * @param pathToRemove path to a node that should be removed+ * @returns true if a node was removed, false if path doesn't exist in the filesystem+ */+ remove(pathToRemove: string) {+ const n = this.find(pathToRemove)+ if (!n) return false++ // This shouldn't ever happen.+ if (!n.parent) throw new Error(`Can't delete node. Node on path '${pathToRemove}' has no parent set`)++ // If we are removing a selected node, de-select it first.+ if (n.path === this.lastSelectedPath) {+ this.lastSelectedPath = undefined+ }++ // Parent is always a dir.+ const parent = n.parent as Dir+ const baseName = path.basename(pathToRemove)+ const child = parent.children.get(baseName)+ if (child) {+ child.parent = undefined+ parent.children.delete(baseName)+ }++ this.notifyDirListeners(parent.path, parent.children.values())++ return true+ }++ getChildren(path: string) {+ const n = this.find(path)+ if (!n) throw new Error(`'${path}': no such file or directory`)+ if (n.type !== NodeType.Dir) throw new Error(`'${path}': is not a directory`)+ return (n as Dir).children.values()+ }++ hasChildren(dirpath: string) {+ const n = this.find(dirpath)+ if (!n) throw new Error(`'${dirpath}': no such file or directory`)+ if (n.type !== NodeType.Dir) throw new Error(`'${dirpath}': is not a directory`)+ return (n as Dir).children.size > 0+ }++ setMetadata<T = unknown>(path: string, {+ key,+ value,+ }: { key: string; value: T | undefined }) {+ log('setMetadata', path, {+ key,+ value,+ })+ const n = this.find(path)+ if (!n) throw new Error(`${path}: no such file or directory. Can't set metadata`)++ const oldVal = n.metadata[key]+ n.metadata[key] = value++ this.notifyMetadataListeners<T>(path, {+ key,+ oldVal,+ newVal: value,+ })+ }++ getMetadata<T = unknown>(path: string, key: string): T {+ log('getMetadata', path, key)+ const n = this.find(path)+ if (!n) throw new Error(`${path}: no such file or directory. Can't get metadata`)+ return n.metadata[key]+ }++ setIsDirExpanded(dirpath: string, isExpanded: boolean) {+ const n = this.find(dirpath)+ if (!n) throw new Error(`${dirpath}: no such directory. Can't set isDirExpanded`)+ if (n.type !== NodeType.Dir) throw new Error(`${dirpath}: is not a directory. Can't set isDirExpanded`)++ const dir = n as Dir+ if (!dir.parent) throw new Error(`'${dirpath}: doesn't have parent. Can't set isDirExpanded to dir without parent`)++ if (dir.isExpanded !== isExpanded) {+ dir.isExpanded = isExpanded+ this.notifyDirListeners(dir.parent.path, dir.parent.children.values())+ }+ }++ setIsNodeSelected(path: string, isSelected: boolean) {+ // Deselect previously selected node if there was any.+ if (this.lastSelectedPath && this.lastSelectedPath !== path) {+ this.setIsNodeSelected(this.lastSelectedPath, false)+ this.lastSelectedPath = path+ }++ const n = this.find(path)+ if (!n) throw new Error(`${path}: no such file or directory. Can't set isNodeSelected`)+ if (!n.parent) throw new Error(`'${path}: doesn't have a parent. Can't set isNodeSelected to a node without a parent`)++ if (n.isSelected !== isSelected) {+ n.isSelected = isSelected+ this.lastSelectedPath = path+ this.notifyDirListeners(n.parent.path, n.parent.children.values())+ }+ }++ print(node: Dir = this.root, indent = 0) {+ const indentStr = ' '.repeat(indent)++ console.log(indentStr + node.path, `(${node.name})`)++ node.children.forEach(c => {+ if (c.type === NodeType.Dir) {+ this.print(c as Dir, indent + 1)+ } else {+ console.log(indentStr + ' ' + c.path, `(${c.name})`)+ }+ })+ }++ find(p: string, node: Node = this.root): Node | undefined {+ p = path.normalize(p)+ if (node.path === p) return node++ // No need to go deeper if the searched item on the+ // path `p` is on a lower dir level than we are at now.+ const pathLevel = p.split(path.sep).length - 1+ if (node.level > pathLevel) return++ if (node.type === NodeType.Dir) {+ for (const [, c] of (node as Dir).children) {+ const res = this.find(p, c)+ if (res) return res+ }+ }+ }++ private makeMetadataListenerKey(path: string, key: string) {+ return `${path}-${key}`+ }+}++export default Filesystemdiff --git a/filesystem/index.ts b/filesystem/index.tsnew file mode 100644index 000000000..5e2cd3f95--- /dev/null+++ b/filesystem/index.ts@@ -0,0 +1,30 @@+import Tree from 'components/FilesystemTree'+export type {+ Props as FilesystemTreeProps,+ FileProps,+ SelectHandler,+} from 'components/FilesystemTree'++import DirWrapper from '../components/DirWrapper'+export type {+ Props as DirWrapperProps,+ DirProps,+} from '../components/DirWrapper'++export { NodeType } from './node'+export type { Metadata } from './node'++export {+ useFilesystem,+ useMetadata,+} from '../hooks/filesystem'++export { default as Node } from './node'+export { default as FileNode } from './file'+export { default as DirNode } from './dir'++/* eslint import/no-anonymous-default-export: [2, {"allowObject": true}] */+export default {+ Tree,+ DirWrapper,+}diff --git a/filesystem/node.ts b/filesystem/node.tsnew file mode 100644index 000000000..1164889af--- /dev/null+++ b/filesystem/node.ts@@ -0,0 +1,53 @@+import path from 'path-browserify'++import Dir from './dir'++export enum NodeType {+ File = 'File',+ Dir = 'Dir',+}++export interface Metadata {+ [name: string]: any+}++abstract class Node {+ abstract name: string+ abstract type: NodeType+ metadata: Metadata = {}++ isSelected = false++ private _path?: string+ get path(): string {+ if (!this._path) {+ this._path = path.join(this.parent?.path || '/', this.name)+ }+ return this._path+ }++ // Root dir doesn't have a parent.+ private _parent?: Dir+ get parent(): Dir | undefined {+ return this._parent+ }+ set parent(par: Dir | undefined) {+ if (par) {+ this._parent = par+ this._path = path.join(par.path || '/', this.name)+ }+ }++ /**+ * Level indicates the depth of a node in the filesystem.+ * A dir in the root directory has a level 0, a file in this+ * dir has a level 1, etc.+ */+ get level(): number {+ if (this.parent) return this.parent.level + 1+ // This makes sure that a files in the root have level 0. The root itself has level -1.+ else return -1+ }+}++export default Nodediff --git a/hooks/filesystem.ts b/hooks/filesystem.tsnew file mode 100644index 000000000..79e116745--- /dev/null+++ b/hooks/filesystem.ts@@ -0,0 +1,88 @@+import {+ useCallback,+ useEffect,+ useState,+} from 'react'+import Filesystem from '../filesystem/filesystem'+import Node from '../filesystem/node'++export function useFilesystem({+ rootPath = '/',+ initial = [],+}: { rootPath?: string, initial?: Node[] } = {+ rootPath: '/',+ initial: [],+ }) {+ const [fs] = useState(() => new Filesystem(rootPath, initial))+ return fs+}++export function useChildren(fs: Filesystem, path: string, shouldRegisterListener: boolean) {+ const [children, setChildren] = useState<Node[]>([])++ useEffect(function getChildren() {+ if (!shouldRegisterListener) return++ const remove = fs.addDirListener(path, (_, children) => {+ setChildren([...children])+ })+ setChildren([...fs.getChildren(path)])+ return () => { remove() }+ }, [+ fs,+ path,+ shouldRegisterListener,+ ])++ return children+}++type MetadataValue<T> = T | undefined+type MetadataFuncValue<T> = (currentVal: T | undefined) => T | undefined+export function useMetadata<T>(fs: Filesystem, path: string, key: string, shouldRegisterListener: boolean): [MetadataValue<T>, (newValue: MetadataValue<T> | MetadataFuncValue<T>) => void] {+ // TODO: We can't detect if user didn't pass `initialValue` because that's the same as passing undefined+ // as the initial value.+ // So if a node already has some value for the key and user didn't pass any initialValue, we overwrite it+ // which we shouldn't do.++ const [val, setVal] = useState<T>()++ const setMetadata = useCallback<(value: MetadataValue<T> | ((currentVal: MetadataValue<T>) => MetadataValue<T>)) => void>(value => {+ let v: T | undefined+ if (value instanceof Function) {+ v = value(val)+ } else {+ v = value+ }++ fs.setMetadata<T>(path, {+ key,+ value: v,+ })+ // No need to call `setVal` because `setVal` will get called thanks+ // to the registered metadata listener below.+ }, [+ fs,+ path,+ key,+ val,+ ])++ useEffect(function registerMetadataListener() {+ if (!shouldRegisterListener) return++ const remove = fs.addMetadataListener<T>(path, key, (_, { newVal }) => {+ setVal(newVal)+ })+ setVal(fs.getMetadata<T>(path, key))+ return () => { remove() }+ }, [+ fs,+ path,+ key,+ setVal,+ shouldRegisterListener,+ ])++ return [val, setMetadata]+}diff --git a/hooks/useFiletree.tsx b/hooks/useFiletree.tsxnew file mode 100644index 000000000..0577c6882--- /dev/null+++ b/hooks/useFiletree.tsx@@ -0,0 +1,70 @@+import {+ ReactNode,+ createContext,+ useCallback,+ useContext,+ useMemo,+ useState,+} from 'react'++import { NodeType } from '../filesystem'++export type Node = {+ type: NodeType+ path: string+}++type Listener = (f: Node) => void++export const filetreeContext = createContext<{+ select: (f: Node) => void+ addSelectListener: (l: Listener) => void+ removeSelectListener: (l: Listener) => void+} | undefined>(undefined)++export interface FiletreeProviderProps {+ children: ReactNode+}++export function FiletreeProvider({ children }: FiletreeProviderProps) {+ const [listeners] = useState<((f: Node) => void)[]>([])++ const select = useCallback((f: Node) => {+ for (const l of listeners) {+ l(f)+ }+ }, [listeners])++ const addSelectListener = useCallback((l: Listener) => {+ listeners.push(l)+ }, [listeners])++ const removeSelectListener = useCallback((l: Listener) => {+ const idx = listeners.findIndex(l1 => l1 === l)+ listeners.splice(idx, 1)+ }, [listeners])++ const value = useMemo(() => ({+ select,+ addSelectListener,+ removeSelectListener,+ }), [+ select,+ addSelectListener,+ removeSelectListener,+ ])++ return (+ <filetreeContext.Provider value={value}>+ {children}+ </filetreeContext.Provider>+ )+}++export function useFiletree() {+ const ctx = useContext(filetreeContext)+ if (ctx === undefined) {+ throw new Error('useFiletree must be used within `FileTreeProvider`')+ }+ return ctx+}diff --git a/hooks/useGetLogFile.ts b/hooks/useGetLogFile.tsdeleted file mode 100644index 7834d7e41..000000000--- a/hooks/useGetLogFile.ts+++ /dev/null@@ -1,34 +0,0 @@-// Tries to fetch a log file from the DB based on the log file ID.-import { log_files } from '@prisma/client'-import { useCallback } from 'react'-import useSWRMutation from 'swr/mutation'--export interface GetLogFile {- id: string-}--export interface GetLogResponse {- // TODO-}--async function handleGetLog(url: string, { arg }: { arg: GetLogFile }) {- if (!arg.id) { return }-- const response = await fetch(`${url}/${arg.id}`, {- method: 'GET',- headers: {- 'Content-Type': 'application/json',- },- })- return await response.json() as log_files-}--export function useGetLogFile() {- const {- trigger: getLog,- } = useSWRMutation('/api/log_file', handleGetLog)-- return useCallback(async (logFileID: string) => getLog({- id: logFileID,- }), [getLog])-}\ No newline at end of filediff --git a/package-lock.json b/package-lock.jsonindex 2aa4e668e..407de401d 100644--- a/package-lock.json+++ b/package-lock.json@@ -36,12 +36,14 @@"jsonpath-plus": "^7.2.0","lodash.debounce": "^4.0.8","lucide-react": "^0.240.0",+ "matcher": "^5.0.0","micro-cors": "^0.1.1","nanoid": "^4.0.1","next": "13.2.3","next-superjson-plugin": "^0.5.6","openai": "^3.2.1","openapi-typescript-fetch": "^1.1.3",+ "path-browserify": "^1.0.1","posthog-js": "^1.63.3","posthog-node": "^3.1.1","prosemirror-state": "^1.4.2",@@ -65,6 +67,7 @@"@types/lodash.debounce": "^4.0.7","@types/micro-cors": "^0.1.3","@types/node": "^18.14.6",+ "@types/path-browserify": "^1.0.0","@types/react": "^18.0.28","@types/react-dom": "^18.0.11","autoprefixer": "^10.4.13",@@ -1884,6 +1887,12 @@"resolved": "https://registry.npmjs.org/@types/object.pick/-/object.pick-1.3.2.tgz","integrity": "sha512-sn7L+qQ6RLPdXRoiaE7bZ/Ek+o4uICma/lBFPyJEKDTPTBP1W8u0c4baj3EiS4DiqLs+Hk+KUGvMVJtAw3ePJg=="},+ "node_modules/@types/path-browserify": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/@types/path-browserify/-/path-browserify-1.0.0.tgz",+ "integrity": "sha512-XMCcyhSvxcch8b7rZAtFAaierBYdeHXVvg2iYnxOV0MCQHmPuRRmGZPFDRzPayxcGiiSL1Te9UIO+f3cuj0tfw==",+ "dev": true+ },"node_modules/@types/phoenix": {"version": "1.5.5","resolved": "https://registry.npmjs.org/@types/phoenix/-/phoenix-1.5.5.tgz",@@ -5657,6 +5666,31 @@"resolved": "https://registry.npmjs.org/markdown-it-task-lists/-/markdown-it-task-lists-2.1.1.tgz","integrity": "sha512-TxFAc76Jnhb2OUu+n3yz9RMu4CwGfaT788br6HhEDlvWfdeJcLUsxk1Hgw2yJio0OXsxv7pyIPmvECY7bMbluA=="},+ "node_modules/matcher": {+ "version": "5.0.0",+ "resolved": "https://registry.npmjs.org/matcher/-/matcher-5.0.0.tgz",+ "integrity": "sha512-s2EMBOWtXFc8dgqvoAzKJXxNHibcdJMV0gwqKUaw9E2JBJuGUK7DrNKrA6g/i+v72TT16+6sVm5mS3thaMLQUw==",+ "dependencies": {+ "escape-string-regexp": "^5.0.0"+ },+ "engines": {+ "node": "^12.20.0 || ^14.13.1 || >=16.0.0"+ },+ "funding": {+ "url": "https://github.com/sponsors/sindresorhus"+ }+ },+ "node_modules/matcher/node_modules/escape-string-regexp": {+ "version": "5.0.0",+ "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-5.0.0.tgz",+ "integrity": "sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==",+ "engines": {+ "node": ">=12"+ },+ "funding": {+ "url": "https://github.com/sponsors/sindresorhus"+ }+ },"node_modules/mdast-util-definitions": {"version": "5.1.2","resolved": "https://registry.npmjs.org/mdast-util-definitions/-/mdast-util-definitions-5.1.2.tgz",@@ -6848,6 +6882,11 @@"node": ">=6"}},+ "node_modules/path-browserify": {+ "version": "1.0.1",+ "resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz",+ "integrity": "sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g=="+ },"node_modules/path-exists": {"version": "4.0.0","resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",diff --git a/package.json b/package.jsonindex f6414a72c..0c0a3d3d7 100644--- a/package.json+++ b/package.json@@ -68,12 +68,14 @@"jsonpath-plus": "^7.2.0","lodash.debounce": "^4.0.8","lucide-react": "^0.240.0",+ "matcher": "^5.0.0","micro-cors": "^0.1.1","nanoid": "^4.0.1","next": "13.2.3","next-superjson-plugin": "^0.5.6","openai": "^3.2.1","openapi-typescript-fetch": "^1.1.3",+ "path-browserify": "^1.0.1","posthog-js": "^1.63.3","posthog-node": "^3.1.1","prosemirror-state": "^1.4.2",@@ -97,6 +99,7 @@"@types/lodash.debounce": "^4.0.7","@types/micro-cors": "^0.1.3","@types/node": "^18.14.6",+ "@types/path-browserify": "^1.0.0","@types/react": "^18.0.28","@types/react-dom": "^18.0.11","autoprefixer": "^10.4.13",diff --git a/pages/index.tsx b/pages/index.tsxindex 0e83e1da1..c8225e373 100644--- a/pages/index.tsx+++ b/pages/index.tsx@@ -228,6 +228,8 @@ export interface Props {}function Home({ projects, defaultProjectID }: Props) {++return (<DashboardHomedefaultProjectID={defaultProjectID}diff --git a/utils/logger.ts b/utils/logger.tsnew file mode 100644index 000000000..27d39c69a--- /dev/null+++ b/utils/logger.ts@@ -0,0 +1,7 @@+export function logger(namespace: string, color: string = 'orange') {+ return function (...args: any[]) {+ console.groupCollapsed(`%c[${namespace}]`, `color:${color};`, ...args)+ console.trace()+ console.groupEnd()+ }+}