[docs]asyncdeflist_indices(INDEX_DIR:Path):"""List all available indices"""try:indices=[]# Check if index directory exists and has contentifnotINDEX_DIR.exists()ornotany(INDEX_DIR.glob('*')):# If INDEX_DIR exists but is empty, remove it firstifINDEX_DIR.exists():shutil.rmtree(INDEX_DIR)# Copy default indicesthis_script_path=Path(__file__).parent.parentdefault_indices_path=this_script_path/'indices'shutil.copytree(default_indices_path,INDEX_DIR)# List all index directoriesforindex_dirinINDEX_DIR.iterdir():ifindex_dir.is_dir():index_file=index_dir/'index.json'ifindex_file.exists():try:withopen(index_file,'r')asf:index_data=json.load(f)manifest=load_plugin_manifest()index_data['installed']=index_dir.nameinmanifest.get('indices',{}).get('installed',{})indices.append(index_data)exceptjson.JSONDecodeError:continue# Skip invalid JSON filesreturnJSONResponse({'success':True,'data':indices})exceptExceptionase:raiseHTTPException(status_code=500,detail=str(e))
[docs]asyncdefcreate_index(INDEX_DIR:Path,metadata:IndexMetadata):"""Create a new index directory with metadata"""try:index_dir=INDEX_DIR/metadata.nameifindex_dir.exists():returnJSONResponse({'success':False,'message':'Index already exists'})# Create index directory structureindex_dir.mkdir(parents=True)ensure_index_structure(index_dir)index_data={'name':metadata.name,'description':metadata.description,'version':metadata.version,'url':metadata.url,'trusted':metadata.trusted,'created_at':datetime.now().isoformat(),'plugins':[],'agents':[]}withopen(index_dir/'index.json','w')asf:json.dump(index_data,f,indent=2)returnJSONResponse({'success':True,'data':index_data})exceptExceptionase:raiseHTTPException(status_code=500,detail=str(e))
[docs]asyncdefupdate_index(INDEX_DIR:Path,index_name:str,metadata:IndexMetadata):"""Update index metadata"""try:index_dir=INDEX_DIR/index_nameindex_file=index_dir/'index.json'ifnotindex_file.exists():returnJSONResponse({'success':False,'message':'Index not found'})withopen(index_file,'r')asf:index_data=json.load(f)index_data.update({'name':metadata.name,'description':metadata.description,'version':metadata.version,'url':metadata.url,'trusted':metadata.trusted,'updated_at':datetime.now().isoformat()})withopen(index_file,'w')asf:json.dump(index_data,f,indent=2)# If name changed, rename the directoryifmetadata.name!=index_name:new_index_dir=INDEX_DIR/metadata.nameindex_dir.rename(new_index_dir)returnJSONResponse({'success':True,'data':index_data})exceptExceptionase:raiseHTTPException(status_code=500,detail=str(e))