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:
Jaroslav Beneš
2026-07-21 20:00:15 +02:00
parent 21001f2eb8
commit a0f733063a
24 changed files with 836 additions and 143 deletions
+41 -66
View File
@@ -7,85 +7,60 @@
{% block library_content %}
<p class="admin-lede">
Documents, images and saved web pages you have collected. A model with the
knowledge tool searches these before it searches the web, and you can attach
any of them to a message.
Knowledge bases are collections of documents, images and saved web pages. Keep
them separate — one per subject, project or client — and a chat can be pointed
at just the ones it should draw on. Sharing happens here too: share a base and
everything in it comes with it.
</p>
<section class="card">
<h2 class="card__title">Add</h2>
<div class="grid grid--2">
<form method="post" action="/api/library/documents" enctype="multipart/form-data">
<div class="field">
<label class="field__label" for="doc-file">A file</label>
<input class="input" id="doc-file" type="file" name="file" required
accept="image/*,.pdf,.txt,.md,.csv,.json,.py,.js,.ts,.rs,.go,.sh,.sql,.yaml,.yml,.toml,.log">
<p class="field__hint">
Images, PDFs and text. PDFs have their text read once, now.
</p>
</div>
<button class="btn btn--primary" type="submit">Upload</button>
</form>
{% if error %}
<div class="alert alert--error">{{ icon("warning", "alert__icon") }} <span>{{ error }}</span></div>
{% endif %}
<form method="post" action="/api/library/documents/link">
<div class="field">
<label class="field__label" for="doc-url">A web page</label>
<input class="input" id="doc-url" type="url" name="url" required
placeholder="https://example.com/article">
<p class="field__hint">
Fetched now and kept as text, so it survives the page changing.
</p>
</div>
<button class="btn" type="submit">Save page</button>
</form>
</div>
</section>
<form method="get" action="/library/knowledge" class="btn-row" style="margin-bottom: var(--sp-5)">
<input class="input" type="search" name="q" value="{{ q }}" style="flex: 1"
placeholder="Search titles and contents…">
<button class="btn" type="submit">{{ icon("search", "icon--sm") }} Search</button>
{% if q %}<a class="btn btn--sm" href="/library/knowledge">Clear</a>{% endif %}
</form>
{% if not documents %}
<div class="empty">
{{ icon("archive", "empty__mark") }}
<h2 class="empty__title">{{ "Nothing found" if q else "The shelves are bare" }}</h2>
<p class="empty__text">
{% if q %}
No document matches “{{ q }}”.
{% else %}
Add a file or a web page above and it becomes searchable — by you, and by
any model you have given the knowledge tool.
{% endif %}
</p>
</div>
{% else %}
{% if bases %}
<ul class="model-list">
{% for document in documents %}
{% for base in bases %}
<li class="model-list__item">
<div style="min-width: 0">
<a href="/library/knowledge/{{ document.id }}"><strong>{{ document.title }}</strong></a>
<a href="/library/knowledge/{{ base.id }}"><strong>{{ base.name }}</strong></a>
<div class="text-xs faint">
{{ document.kind }}
{%- if document.pages %} · {{ document.pages }} page{{ '' if document.pages == 1 else 's' }}{% endif %}
{%- if document.size_bytes %} · {{ document.human_size }}{% endif %}
{%- if document.source_url %} · {{ document.source_url[:60] }}{% endif %}
{{ counts.get(base.id, 0) }} document{{ '' if counts.get(base.id, 0) == 1 else 's' }}
{%- if base.description %} · {{ base.description }}{% endif %}
</div>
{% if document.description %}
<div class="text-xs faint">{{ document.description }}</div>
{% endif %}
</div>
<div class="btn-row">
{% if document.extraction_error %}
<span class="badge badge--danger" title="{{ document.extraction_error }}">no text</span>
{% endif %}
{% if document.owner_id != user.id %}<span class="badge">shared</span>{% endif %}
{% if base.owner_id != user.id %}<span class="badge">shared with you</span>{% endif %}
</div>
</li>
{% endfor %}
</ul>
{% include "library/_pager.html" %}
{% else %}
<div class="empty">
{{ icon("archive", "empty__mark") }}
<h2 class="empty__title">No knowledge bases yet</h2>
<p class="empty__text">
Make one below, then put documents in it. A chat with no base attached
searches everything you have; a chat pointed at one searches only that.
</p>
</div>
{% endif %}
<section class="card" style="margin-top: var(--sp-6)">
<h2 class="card__title">New knowledge base</h2>
<form method="post" action="/api/library/bases">
<div class="grid grid--2">
<div class="field">
<label class="field__label" for="base-name">Name</label>
<input class="input" id="base-name" name="name" required maxlength="200"
placeholder="Contracts">
</div>
<div class="field">
<label class="field__label" for="base-description">Description</label>
<input class="input" id="base-description" name="description" maxlength="2000"
placeholder="What belongs in here.">
</div>
</div>
<button class="btn btn--primary" type="submit">{{ icon("plus", "icon--sm") }} Create</button>
</form>
</section>
{% endblock %}