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:
@@ -23,7 +23,17 @@ hand round, and "share my memories with the team" is a question nobody asked.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import Boolean, ForeignKey, Index, Integer, String, Text, UniqueConstraint
|
||||
from sqlalchemy import (
|
||||
Boolean,
|
||||
Column,
|
||||
ForeignKey,
|
||||
Index,
|
||||
Integer,
|
||||
String,
|
||||
Table,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
)
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from lembas.db.base import Base, Timestamps, UUIDPrimaryKey
|
||||
@@ -39,13 +49,55 @@ SOURCE_LINK = "link"
|
||||
|
||||
# Resource kinds that can be shared. Values are stored, so they are part of the
|
||||
# schema rather than an implementation detail.
|
||||
RESOURCE_DOCUMENT = "document"
|
||||
RESOURCE_BASE = "base"
|
||||
RESOURCE_NOTE = "note"
|
||||
RESOURCE_SKILL = "skill"
|
||||
|
||||
PRINCIPAL_USER = "user"
|
||||
PRINCIPAL_GROUP = "group"
|
||||
|
||||
# Which knowledge bases a chat draws on. A chat with none searches everything
|
||||
# its owner can see; a chat with some is scoped to those, which is the point --
|
||||
# "answer from the contract folder" is a different question from "answer from
|
||||
# everything I have ever uploaded".
|
||||
chat_knowledge_bases = Table(
|
||||
"chat_knowledge_bases",
|
||||
Base.metadata,
|
||||
Column("chat_id", String(32), ForeignKey("chats.id", ondelete="CASCADE"), primary_key=True),
|
||||
Column(
|
||||
"base_id",
|
||||
String(32),
|
||||
ForeignKey("knowledge_bases.id", ondelete="CASCADE"),
|
||||
primary_key=True,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class KnowledgeBase(UUIDPrimaryKey, Timestamps, Base):
|
||||
"""A named collection of documents.
|
||||
|
||||
Sharing lives here rather than on the individual document: "this folder is
|
||||
the team's" is the granularity people actually think in, and per-document
|
||||
grants would mean answering "who can see this?" by checking every file.
|
||||
A document is visible to whoever can see the base it is in.
|
||||
"""
|
||||
|
||||
__tablename__ = "knowledge_bases"
|
||||
__table_args__ = (UniqueConstraint("owner_id", "name"),)
|
||||
|
||||
owner_id: Mapped[str] = mapped_column(
|
||||
String(32), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
|
||||
)
|
||||
name: Mapped[str] = mapped_column(String(200), nullable=False)
|
||||
description: Mapped[str] = mapped_column(Text, default="")
|
||||
|
||||
documents: Mapped[list[Document]] = relationship(
|
||||
back_populates="base", cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<KnowledgeBase {self.name!r}>"
|
||||
|
||||
|
||||
class Document(UUIDPrimaryKey, Timestamps, Base):
|
||||
"""One item in a knowledge library: a file, an image or a saved web page.
|
||||
@@ -61,6 +113,12 @@ class Document(UUIDPrimaryKey, Timestamps, Base):
|
||||
owner_id: Mapped[str] = mapped_column(
|
||||
String(32), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
|
||||
)
|
||||
# Nullable only so the column could be added to an existing table. The
|
||||
# service always sets it, and a startup sweep files anything that predates
|
||||
# bases into its owner's default -- see documents.sweep_unfiled.
|
||||
base_id: Mapped[str | None] = mapped_column(
|
||||
String(32), ForeignKey("knowledge_bases.id", ondelete="CASCADE"), index=True
|
||||
)
|
||||
|
||||
title: Mapped[str] = mapped_column(String(300), nullable=False)
|
||||
description: Mapped[str] = mapped_column(Text, default="")
|
||||
@@ -82,6 +140,8 @@ class Document(UUIDPrimaryKey, Timestamps, Base):
|
||||
truncated: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
|
||||
extraction_error: Mapped[str] = mapped_column(Text, default="")
|
||||
|
||||
base: Mapped[KnowledgeBase] = relationship(back_populates="documents")
|
||||
|
||||
@property
|
||||
def is_image(self) -> bool:
|
||||
return self.kind == "image"
|
||||
|
||||
Reference in New Issue
Block a user