cognee/cognee/api/v1/notebooks/routers/get_notebooks_router.py
Igor Ilic 288218b209
Merge dev into main (#1398)
<!-- .github/pull_request_template.md -->

## Description
Vector URL fix, MCP Fix

## Type of Change
<!-- Please check the relevant option -->
- [ ] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Code refactoring
- [ ] Performance improvement
- [ ] Other (please specify):

## Changes Made
<!-- List the specific changes made in this PR -->
- 
- 
- 

## Testing
<!-- Describe how you tested your changes -->

## Screenshots/Videos (if applicable)
<!-- Add screenshots or videos to help explain your changes -->

## Pre-submission Checklist
<!-- Please check all boxes that apply before submitting your PR -->
- [ ] **I have tested my changes thoroughly before submitting this PR**
- [ ] **This PR contains minimal changes necessary to address the
issue/feature**
- [ ] My code follows the project's coding standards and style
guidelines
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have added necessary documentation (if applicable)
- [ ] All new and existing tests pass
- [ ] I have searched existing PRs to ensure this change hasn't been
submitted already
- [ ] I have linked any relevant issues in the description
- [ ] My commits have clear and descriptive messages

## Related Issues
<!-- Link any related issues using "Fixes #issue_number" or "Relates to
#issue_number" -->

## Additional Notes
<!-- Add any additional notes, concerns, or context for reviewers -->

## DCO Affirmation
I affirm that all code in every commit of this pull request conforms to
the terms of the Topoteretes Developer Certificate of Origin.

---------

Co-authored-by: Boris <boris@topoteretes.com>
Co-authored-by: Boris Arzentar <borisarzentar@gmail.com>
2025-09-12 20:20:21 +02:00

97 lines
3.4 KiB
Python

from uuid import UUID
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from pydantic import Field
from typing import List, Optional
from fastapi import APIRouter, Depends
from cognee.api.DTO import InDTO
from cognee.infrastructure.databases.relational import get_async_session
from cognee.infrastructure.utils.run_async import run_async
from cognee.modules.notebooks.models import Notebook, NotebookCell
from cognee.modules.notebooks.operations import run_in_local_sandbox
from cognee.modules.users.models import User
from cognee.modules.users.methods import get_authenticated_user
from cognee.modules.notebooks.methods import (
create_notebook,
delete_notebook,
get_notebook,
get_notebooks,
update_notebook,
)
class NotebookData(InDTO):
name: Optional[str] = Field(...)
cells: Optional[List[NotebookCell]] = Field(default=[])
def get_notebooks_router():
router = APIRouter()
@router.get("")
async def get_notebooks_endpoint(user: User = Depends(get_authenticated_user)):
async with get_async_session() as session:
return await get_notebooks(user.id, session)
@router.post("")
async def create_notebook_endpoint(
notebook_data: NotebookData, user: User = Depends(get_authenticated_user)
):
return await create_notebook(
user.id, notebook_data.name, notebook_data.cells, deletable=True
)
@router.put("/{notebook_id}")
async def update_notebook_endpoint(
notebook_id: UUID, notebook_data: NotebookData, user: User = Depends(get_authenticated_user)
):
async with get_async_session(auto_commit=True) as session:
notebook: Notebook = await get_notebook(notebook_id, user.id, session)
if notebook is None:
return JSONResponse(status_code=404, content={"error": "Notebook not found"})
if notebook_data.name and notebook_data.name != notebook.name:
notebook.name = notebook_data.name
if notebook_data.cells:
notebook.cells = notebook_data.cells
return await update_notebook(notebook, session)
class RunCodeData(InDTO):
content: str = Field(...)
@router.post("/{notebook_id}/{cell_id}/run")
async def run_notebook_cell_endpoint(
notebook_id: UUID,
cell_id: UUID,
run_code: RunCodeData,
user: User = Depends(get_authenticated_user),
):
async with get_async_session() as session:
notebook: Notebook = await get_notebook(notebook_id, user.id, session)
if notebook is None:
return JSONResponse(status_code=404, content={"error": "Notebook not found"})
result, error = await run_async(run_in_local_sandbox, run_code.content)
return JSONResponse(
status_code=200, content={"result": jsonable_encoder(result), "error": error}
)
@router.delete("/{notebook_id}")
async def delete_notebook_endpoint(
notebook_id: UUID, user: User = Depends(get_authenticated_user)
):
async with get_async_session(auto_commit=True) as session:
notebook: Notebook = await get_notebook(notebook_id, user.id, session)
if notebook is None:
return JSONResponse(status_code=404, content={"error": "Notebook not found"})
return await delete_notebook(notebook, session)
return router