|
| 1 | +"""V2 Import Router - ID-based data import operations. |
| 2 | +
|
| 3 | +This router uses v2 dependencies for consistent project ID handling. |
| 4 | +Import endpoints use project_id in the path for consistency with other v2 endpoints. |
| 5 | +""" |
| 6 | + |
| 7 | +import json |
| 8 | +import logging |
| 9 | + |
| 10 | +from fastapi import APIRouter, Form, HTTPException, UploadFile, status |
| 11 | + |
| 12 | +from basic_memory.deps import ( |
| 13 | + ChatGPTImporterV2Dep, |
| 14 | + ClaudeConversationsImporterV2Dep, |
| 15 | + ClaudeProjectsImporterV2Dep, |
| 16 | + MemoryJsonImporterV2Dep, |
| 17 | + ProjectIdPathDep, |
| 18 | +) |
| 19 | +from basic_memory.importers import Importer |
| 20 | +from basic_memory.schemas.importer import ( |
| 21 | + ChatImportResult, |
| 22 | + EntityImportResult, |
| 23 | + ProjectImportResult, |
| 24 | +) |
| 25 | + |
| 26 | +logger = logging.getLogger(__name__) |
| 27 | + |
| 28 | +router = APIRouter(prefix="/import", tags=["import-v2"]) |
| 29 | + |
| 30 | + |
| 31 | +@router.post("/chatgpt", response_model=ChatImportResult) |
| 32 | +async def import_chatgpt( |
| 33 | + project_id: ProjectIdPathDep, |
| 34 | + importer: ChatGPTImporterV2Dep, |
| 35 | + file: UploadFile, |
| 36 | + folder: str = Form("conversations"), |
| 37 | +) -> ChatImportResult: |
| 38 | + """Import conversations from ChatGPT JSON export. |
| 39 | +
|
| 40 | + Args: |
| 41 | + project_id: Validated numeric project ID from URL path |
| 42 | + file: The ChatGPT conversations.json file. |
| 43 | + folder: The folder to place the files in. |
| 44 | + importer: ChatGPT importer instance. |
| 45 | +
|
| 46 | + Returns: |
| 47 | + ChatImportResult with import statistics. |
| 48 | +
|
| 49 | + Raises: |
| 50 | + HTTPException: If import fails. |
| 51 | + """ |
| 52 | + logger.info(f"V2 Importing ChatGPT conversations for project {project_id}") |
| 53 | + return await import_file(importer, file, folder) |
| 54 | + |
| 55 | + |
| 56 | +@router.post("/claude/conversations", response_model=ChatImportResult) |
| 57 | +async def import_claude_conversations( |
| 58 | + project_id: ProjectIdPathDep, |
| 59 | + importer: ClaudeConversationsImporterV2Dep, |
| 60 | + file: UploadFile, |
| 61 | + folder: str = Form("conversations"), |
| 62 | +) -> ChatImportResult: |
| 63 | + """Import conversations from Claude conversations.json export. |
| 64 | +
|
| 65 | + Args: |
| 66 | + project_id: Validated numeric project ID from URL path |
| 67 | + file: The Claude conversations.json file. |
| 68 | + folder: The folder to place the files in. |
| 69 | + importer: Claude conversations importer instance. |
| 70 | +
|
| 71 | + Returns: |
| 72 | + ChatImportResult with import statistics. |
| 73 | +
|
| 74 | + Raises: |
| 75 | + HTTPException: If import fails. |
| 76 | + """ |
| 77 | + logger.info(f"V2 Importing Claude conversations for project {project_id}") |
| 78 | + return await import_file(importer, file, folder) |
| 79 | + |
| 80 | + |
| 81 | +@router.post("/claude/projects", response_model=ProjectImportResult) |
| 82 | +async def import_claude_projects( |
| 83 | + project_id: ProjectIdPathDep, |
| 84 | + importer: ClaudeProjectsImporterV2Dep, |
| 85 | + file: UploadFile, |
| 86 | + folder: str = Form("projects"), |
| 87 | +) -> ProjectImportResult: |
| 88 | + """Import projects from Claude projects.json export. |
| 89 | +
|
| 90 | + Args: |
| 91 | + project_id: Validated numeric project ID from URL path |
| 92 | + file: The Claude projects.json file. |
| 93 | + folder: The base folder to place the files in. |
| 94 | + importer: Claude projects importer instance. |
| 95 | +
|
| 96 | + Returns: |
| 97 | + ProjectImportResult with import statistics. |
| 98 | +
|
| 99 | + Raises: |
| 100 | + HTTPException: If import fails. |
| 101 | + """ |
| 102 | + logger.info(f"V2 Importing Claude projects for project {project_id}") |
| 103 | + return await import_file(importer, file, folder) |
| 104 | + |
| 105 | + |
| 106 | +@router.post("/memory-json", response_model=EntityImportResult) |
| 107 | +async def import_memory_json( |
| 108 | + project_id: ProjectIdPathDep, |
| 109 | + importer: MemoryJsonImporterV2Dep, |
| 110 | + file: UploadFile, |
| 111 | + folder: str = Form("conversations"), |
| 112 | +) -> EntityImportResult: |
| 113 | + """Import entities and relations from a memory.json file. |
| 114 | +
|
| 115 | + Args: |
| 116 | + project_id: Validated numeric project ID from URL path |
| 117 | + file: The memory.json file. |
| 118 | + folder: Optional destination folder within the project. |
| 119 | + importer: Memory JSON importer instance. |
| 120 | +
|
| 121 | + Returns: |
| 122 | + EntityImportResult with import statistics. |
| 123 | +
|
| 124 | + Raises: |
| 125 | + HTTPException: If import fails. |
| 126 | + """ |
| 127 | + logger.info(f"V2 Importing memory.json for project {project_id}") |
| 128 | + try: |
| 129 | + file_data = [] |
| 130 | + file_bytes = await file.read() |
| 131 | + file_str = file_bytes.decode("utf-8") |
| 132 | + for line in file_str.splitlines(): |
| 133 | + json_data = json.loads(line) |
| 134 | + file_data.append(json_data) |
| 135 | + |
| 136 | + result = await importer.import_data(file_data, folder) |
| 137 | + if not result.success: # pragma: no cover |
| 138 | + raise HTTPException( |
| 139 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 140 | + detail=result.error_message or "Import failed", |
| 141 | + ) |
| 142 | + except Exception as e: |
| 143 | + logger.exception("V2 Import failed") |
| 144 | + raise HTTPException( |
| 145 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 146 | + detail=f"Import failed: {str(e)}", |
| 147 | + ) |
| 148 | + return result |
| 149 | + |
| 150 | + |
| 151 | +async def import_file(importer: Importer, file: UploadFile, destination_folder: str): |
| 152 | + """Helper function to import a file using an importer instance. |
| 153 | +
|
| 154 | + Args: |
| 155 | + importer: The importer instance to use |
| 156 | + file: The file to import |
| 157 | + destination_folder: Destination folder for imported content |
| 158 | +
|
| 159 | + Returns: |
| 160 | + Import result from the importer |
| 161 | +
|
| 162 | + Raises: |
| 163 | + HTTPException: If import fails |
| 164 | + """ |
| 165 | + try: |
| 166 | + # Process file |
| 167 | + json_data = json.load(file.file) |
| 168 | + result = await importer.import_data(json_data, destination_folder) |
| 169 | + if not result.success: # pragma: no cover |
| 170 | + raise HTTPException( |
| 171 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 172 | + detail=result.error_message or "Import failed", |
| 173 | + ) |
| 174 | + |
| 175 | + return result |
| 176 | + |
| 177 | + except Exception as e: |
| 178 | + logger.exception("V2 Import failed") |
| 179 | + raise HTTPException( |
| 180 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 181 | + detail=f"Import failed: {str(e)}", |
| 182 | + ) |
0 commit comments