Knowledge bases, and a file input that lines up
**Bases.** Documents now live in named collections rather than one flat pile, and a chat can be pointed at particular ones — "answer from the contracts folder" is a different question from "answer from everything I have ever uploaded". A chat with none attached still searches everything its owner can see, because empty means unscoped, not empty. The harness names the attached bases. Without that the model cannot tell "there is nothing about this" from "I am only allowed to see one folder", and it phrases a miss as the former. **Sharing moves to the base.** A document is visible to whoever can see the base it lives in, so `Document` is gone from the shareable types and `documents.visible()` filters through `base_id`. "This folder is the team's" is the granularity people think in; per-document grants meant answering "who can see this?" by checking every file. Moving a document between bases changes who can see it, so the destination has to be one you own. `Document.base_id` is nullable only because the column had to be added to a table that already had rows. `sweep_unfiled()` runs at startup beside the orphaned-upload sweep and files anything predating bases into its owner's default, which is what makes "always set" true everywhere else. **The file input.** `.input` gave it a fixed height and horizontal padding, so the browser's own button sat hard against the left edge while the filename floated off the centre line. A file input is two controls in one box and neither inherits anything useful, so it gets its own rule: no horizontal padding, the button sized to `--control-h` with the divider that separates it, and the text centred with line-height rather than flexbox, which file inputs do not lay out reliably. 437 tests, ruff clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+149
-25
@@ -27,6 +27,7 @@ from lembas.db.models import (
|
||||
PRINCIPAL_USER,
|
||||
Document,
|
||||
Group,
|
||||
KnowledgeBase,
|
||||
Note,
|
||||
Skill,
|
||||
SkillRevision,
|
||||
@@ -92,33 +93,54 @@ async def library_home(user: RequiredUser):
|
||||
|
||||
|
||||
# --- Knowledge ---------------------------------------------------------------
|
||||
# Route order matters: /library/knowledge/document/{id} must be registered
|
||||
# before /library/knowledge/{base_id}, or "document" is parsed as a base id.
|
||||
# FastAPI matches in registration order and this has bitten before.
|
||||
@router.get("/library/knowledge")
|
||||
async def knowledge_list(
|
||||
request: Request, db: Db, user: RequiredUser, q: str = "", page: int = 1, saved: str = ""
|
||||
):
|
||||
if q.strip():
|
||||
# Search returns best-match order and its own limit, so it is not paged.
|
||||
rows = documents_service.search(db, user, q, limit=PAGE_SIZE)
|
||||
pager = {"page": 1, "pages": 1, "total": len(rows)}
|
||||
else:
|
||||
rows, pager = _page(
|
||||
db, documents_service.visible(db, user).order_by(Document.created_at.desc()), page
|
||||
async def knowledge_list(request: Request, db: Db, user: RequiredUser, error: str = ""):
|
||||
"""The bases, not the documents. A library is a set of places first."""
|
||||
bases = list(db.scalars(documents_service.visible_bases(db, user).order_by(KnowledgeBase.name)))
|
||||
counts = {
|
||||
base.id: db.scalar(
|
||||
select(func.count()).select_from(Document).where(Document.base_id == base.id)
|
||||
)
|
||||
or 0
|
||||
for base in bases
|
||||
}
|
||||
return render(
|
||||
request,
|
||||
"library/knowledge.html",
|
||||
{
|
||||
"section": "knowledge",
|
||||
"documents": rows,
|
||||
"q": q,
|
||||
"pager": pager,
|
||||
"saved": saved,
|
||||
"bases": bases,
|
||||
"counts": counts,
|
||||
"error": error,
|
||||
**sidebar_context(db, user),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/library/knowledge/{document_id}")
|
||||
@router.post("/api/library/bases")
|
||||
async def create_base(
|
||||
db: Db, user: RequiredUser, name: str = Form(""), description: str = Form("")
|
||||
) -> Response:
|
||||
try:
|
||||
base = documents_service.create_base(
|
||||
db, owner=user, name=name, description=description
|
||||
)
|
||||
except ValueError as exc:
|
||||
from urllib.parse import quote
|
||||
|
||||
return RedirectResponse(
|
||||
f"/library/knowledge?error={quote(str(exc))}",
|
||||
status_code=status.HTTP_303_SEE_OTHER,
|
||||
)
|
||||
return RedirectResponse(
|
||||
f"/library/knowledge/{base.id}", status_code=status.HTTP_303_SEE_OTHER
|
||||
)
|
||||
|
||||
|
||||
@router.get("/library/knowledge/document/{document_id}")
|
||||
async def knowledge_detail(request: Request, db: Db, user: RequiredUser, document_id: str):
|
||||
document = documents_service.get(db, document_id, user)
|
||||
if document is None:
|
||||
@@ -129,38 +151,129 @@ async def knowledge_detail(request: Request, db: Db, user: RequiredUser, documen
|
||||
{
|
||||
"section": "knowledge",
|
||||
"document": document,
|
||||
**_shared_context(db, user, document),
|
||||
"is_owner": sharing.can_write(document, user),
|
||||
# Only bases this person owns: moving a document into one they can
|
||||
# merely read would hand it to that base's owner.
|
||||
"user_bases": list(
|
||||
db.scalars(
|
||||
select(KnowledgeBase)
|
||||
.where(KnowledgeBase.owner_id == user.id)
|
||||
.order_by(KnowledgeBase.name)
|
||||
)
|
||||
),
|
||||
**sidebar_context(db, user),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/library/knowledge/{base_id}")
|
||||
async def base_detail(
|
||||
request: Request, db: Db, user: RequiredUser, base_id: str, q: str = "", page: int = 1
|
||||
):
|
||||
base = documents_service.get_base(db, base_id, user)
|
||||
if base is None:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, "That knowledge base is not available.")
|
||||
|
||||
if q.strip():
|
||||
rows = documents_service.search(db, user, q, limit=PAGE_SIZE, base_ids=[base.id])
|
||||
pager = {"page": 1, "pages": 1, "total": len(rows)}
|
||||
else:
|
||||
rows, pager = _page(
|
||||
db,
|
||||
documents_service.visible(db, user, base_ids=[base.id]).order_by(
|
||||
Document.created_at.desc()
|
||||
),
|
||||
page,
|
||||
)
|
||||
return render(
|
||||
request,
|
||||
"library/base_detail.html",
|
||||
{
|
||||
"section": "knowledge",
|
||||
"base": base,
|
||||
"documents": rows,
|
||||
"q": q,
|
||||
"pager": pager,
|
||||
**_shared_context(db, user, base),
|
||||
**sidebar_context(db, user),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/api/library/bases/{base_id}")
|
||||
async def update_base(request: Request, db: Db, user: RequiredUser, base_id: str) -> Response:
|
||||
base = documents_service.get_base(db, base_id, user)
|
||||
if base is None:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, "That knowledge base is not available.")
|
||||
if not sharing.can_write(base, user):
|
||||
raise HTTPException(status.HTTP_403_FORBIDDEN, "That base is not yours to change.")
|
||||
|
||||
form = await request.form()
|
||||
name = " ".join(str(form.get("name", "")).split())[:200]
|
||||
if name:
|
||||
base.name = name
|
||||
base.description = str(form.get("description", "")).strip()[:2000]
|
||||
db.commit()
|
||||
_apply_shares(db, user, base, form)
|
||||
return RedirectResponse(
|
||||
f"/library/knowledge/{base.id}", status_code=status.HTTP_303_SEE_OTHER
|
||||
)
|
||||
|
||||
|
||||
@router.post("/api/library/bases/{base_id}/delete")
|
||||
async def delete_base(db: Db, user: RequiredUser, base_id: str) -> Response:
|
||||
base = documents_service.get_base(db, base_id, user)
|
||||
if base is None or not sharing.can_write(base, user):
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, "That knowledge base is not available.")
|
||||
documents_service.delete_base(db, base)
|
||||
return RedirectResponse("/library/knowledge", status_code=status.HTTP_303_SEE_OTHER)
|
||||
|
||||
|
||||
@router.post("/api/library/documents")
|
||||
async def upload_document(
|
||||
db: Db, user: RequiredUser, file: UploadFile = File(...), title: str = Form("")
|
||||
db: Db,
|
||||
user: RequiredUser,
|
||||
file: UploadFile = File(...),
|
||||
title: str = Form(""),
|
||||
base_id: str = Form(""),
|
||||
) -> Response:
|
||||
base = documents_service.get_base(db, base_id, user) if base_id else None
|
||||
if base is not None and not sharing.can_write(base, user):
|
||||
raise HTTPException(status.HTTP_403_FORBIDDEN, "That base is not yours to add to.")
|
||||
|
||||
payload = await file.read(files_service.MAX_UPLOAD_BYTES + 1)
|
||||
try:
|
||||
document = documents_service.store_upload(
|
||||
db, owner=user, payload=payload, filename=file.filename or "file", title=title
|
||||
db,
|
||||
owner=user,
|
||||
payload=payload,
|
||||
filename=file.filename or "file",
|
||||
title=title,
|
||||
base=base,
|
||||
)
|
||||
except files_service.FileError as exc:
|
||||
raise HTTPException(status.HTTP_400_BAD_REQUEST, str(exc)) from exc
|
||||
return RedirectResponse(
|
||||
f"/library/knowledge/{document.id}", status_code=status.HTTP_303_SEE_OTHER
|
||||
f"/library/knowledge/{document.base_id}", status_code=status.HTTP_303_SEE_OTHER
|
||||
)
|
||||
|
||||
|
||||
@router.post("/api/library/documents/link")
|
||||
async def save_link(db: Db, user: RequiredUser, url: str = Form(...)) -> Response:
|
||||
async def save_link(
|
||||
db: Db, user: RequiredUser, url: str = Form(...), base_id: str = Form("")
|
||||
) -> Response:
|
||||
base = documents_service.get_base(db, base_id, user) if base_id else None
|
||||
if base is not None and not sharing.can_write(base, user):
|
||||
raise HTTPException(status.HTTP_403_FORBIDDEN, "That base is not yours to add to.")
|
||||
|
||||
config = settings_store.search(db)
|
||||
try:
|
||||
page = await fetch(url, allow_private=bool(config.get("allow_private_fetch")))
|
||||
except FetchError as exc:
|
||||
raise HTTPException(status.HTTP_400_BAD_REQUEST, exc.message) from exc
|
||||
document = documents_service.store_page(db, owner=user, page=page)
|
||||
document = documents_service.store_page(db, owner=user, page=page, base=base)
|
||||
return RedirectResponse(
|
||||
f"/library/knowledge/{document.id}", status_code=status.HTTP_303_SEE_OTHER
|
||||
f"/library/knowledge/{document.base_id}", status_code=status.HTTP_303_SEE_OTHER
|
||||
)
|
||||
|
||||
|
||||
@@ -177,10 +290,18 @@ async def update_document(
|
||||
form = await request.form()
|
||||
document.title = str(form.get("title", document.title)).strip()[:300] or document.title
|
||||
document.description = str(form.get("description", "")).strip()[:2000]
|
||||
|
||||
# Moving between bases changes who can see it, which is the whole point of
|
||||
# bases -- so the destination has to be one this person can write to.
|
||||
wanted = str(form.get("base_id", "")).strip()
|
||||
if wanted and wanted != document.base_id:
|
||||
destination = documents_service.get_base(db, wanted, user)
|
||||
if destination is not None and sharing.can_write(destination, user):
|
||||
document.base_id = destination.id
|
||||
|
||||
db.commit()
|
||||
_apply_shares(db, user, document, form)
|
||||
return RedirectResponse(
|
||||
f"/library/knowledge/{document.id}", status_code=status.HTTP_303_SEE_OTHER
|
||||
f"/library/knowledge/document/{document.id}", status_code=status.HTTP_303_SEE_OTHER
|
||||
)
|
||||
|
||||
|
||||
@@ -189,8 +310,11 @@ async def delete_document(db: Db, user: RequiredUser, document_id: str) -> Respo
|
||||
document = documents_service.get(db, document_id, user)
|
||||
if document is None or not sharing.can_write(document, user):
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, "That document is not available.")
|
||||
base_id = document.base_id
|
||||
documents_service.delete(db, document)
|
||||
return RedirectResponse("/library/knowledge", status_code=status.HTTP_303_SEE_OTHER)
|
||||
return RedirectResponse(
|
||||
f"/library/knowledge/{base_id}", status_code=status.HTTP_303_SEE_OTHER
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/library/documents/{document_id}/content")
|
||||
|
||||
Reference in New Issue
Block a user