## Overview Adds **AI document classification** and a **classification-aware Files sidebar**: uploaded documents are automatically tagged with document-type labels (Invoice, Contract, Lab report, …), and the sidebar groups files under editable parent categories so a large library stays navigable. > [!IMPORTANT] > **This feature only runs in the SaaS build.** Classification depends on the AI engine and team-scoped label storage, so it's gated to SaaS end-to-end: > - The sidebar grouping is a `saas/`-layer override of the `fileSidebarGrouping` seam; every other build (OSS core, self-hosted proprietary, desktop) gets the null stub and renders the **unchanged flat, recency-sorted list** — no categories, no "Other", no picker. > - The classify/labels backend endpoints are gated on `policies.enabled` (on in SaaS) and live in `app/proprietary`, so they're absent from pure OSS and dormant in self-hosted unless explicitly enabled. > - The Python classifier is reached only via that gated path. > > Shared-layer changes that do compile everywhere are inert without the engine (dormant schema/field additions) or intentional (`GetInfoOnPDF` surfacing custom metadata). ## What it does - **Classifier (engine):** reads the first/last two pages of a PDF and assigns document-type labels from an allowed vocabulary. Labels are deliberately document-*type* descriptors — no deep-content/PII detection, since only a page window is read. - **Team label vocabulary:** ~270 built-in defaults across ~15 families, seeded per team. Editable by team leaders/admins in the Classification policy settings (import/export/reset). Team-scoped and shared; **per-user personal labels are intentionally out of scope** — the vocabulary is team-level only. - **Sidebar categories:** files group under parent categories (Financial, Legal, Medical, …), busiest-first, collapsible, with a "Recent" group on top and an "Other" group for anything uncategorised. The category structure (names, icons, membership, custom categories) is **device-local and user-editable** via a "Customize" picker — the only per-user personalization; it never changes the team's label vocabulary. - Classification results are written to PDF metadata (`StirlingPDFClassification`), read back to keep files in their groups without re-parsing. ## Architecture Spans all three layers, mirroring the existing policy/source subsystem conventions: - **`frontend/editor`** — sidebar grouping seam + SaaS override, category manager, labels editor, icon palette, file grouping, tests, `en-US` i18n. - **`app/proprietary` + `app/common` + `app/core`** — `ClassifyLabelController`, team-scoped `ClassificationLabelStore` (Jpa + in-process impls, same shape as `PolicyStore`/`SourceStore`), metadata read/write. - **`engine`** — the document-classifier agent, contracts, routes, tests. ## Screenshots **Files sidebar — grouped by category (SaaS)** ### Loading view <img width="2056" height="1046" alt="Screenshot 2026-07-07 at 5 12 56 PM" src="https://github.com/user-attachments/assets/1d712da5-50ae-4349-b0cd-e62665c3ec0c" /> ### Organized in the sidebar <img width="2056" height="1045" alt="Screenshot 2026-07-07 at 5 14 05 PM" src="https://github.com/user-attachments/assets/3ea4fe21-da51-4cea-bc3a-18ce040d3d05" /> **Customize categories picker** ### Personal settings to change how labels are grouped in an individual users editor <img width="2056" height="1044" alt="Screenshot 2026-07-07 at 5 52 42 PM" src="https://github.com/user-attachments/assets/40be03ce-0f63-4d1e-b58b-cec045d01cb2" /> **Classification labels editor (team settings)** <img width="2056" height="1042" alt="Screenshot 2026-07-07 at 5 53 00 PM" src="https://github.com/user-attachments/assets/337b0739-15c9-4749-9c6b-22e3b20825b8" /> ## Testing - Frontend `task frontend:check` — green (editor + portal tests, typecheck across all flavors, lint, label-drift guard). - Backend `task backend:check` (proprietary) and `:saas:test` — green. - Engine `task engine:check` — green.
108 lines
3.6 KiB
TypeScript
108 lines
3.6 KiB
TypeScript
/**
|
|
* Client-side import/export + validation for a classification-labels JSON file
|
|
* (`{"labels":[{"id":"invoice","name":"Invoice","icon":"receipt-long"}, …]}`).
|
|
* Sharing a label set between teams is done by exporting the JSON here and
|
|
* importing it on another team. Validation mirrors the backend
|
|
* (`LabelsValidator`) so a malformed file is caught before it's uploaded — the
|
|
* backend re-validates as the authority. A file may omit `id` (e.g. an older
|
|
* export); it is then derived from the name.
|
|
*/
|
|
|
|
import { downloadJsonAsFile } from "@app/utils/downloadUtils";
|
|
import { LABEL_ICON_KEYS } from "@app/data/labelIcons";
|
|
import {
|
|
labelId,
|
|
type ClassificationLabel,
|
|
} from "@app/data/classificationLabels";
|
|
|
|
// Kept in sync with the backend LabelsValidator (the authority); enforced here
|
|
// too so an oversized import is rejected before upload.
|
|
const MAX_LABELS = 500;
|
|
const MAX_TEXT_LENGTH = 128;
|
|
|
|
interface LabelsFileShape {
|
|
labels: ClassificationLabel[];
|
|
}
|
|
|
|
/** A label's effective id: the provided one, else derived from the name. */
|
|
function effectiveId(label: Partial<ClassificationLabel>): string {
|
|
const provided = typeof label.id === "string" ? label.id.trim() : "";
|
|
return provided || labelId(label.name?.trim() ?? "");
|
|
}
|
|
|
|
/** Human-readable problems with a candidate labels file; empty means valid. */
|
|
export function validateLabels(value: unknown): string[] {
|
|
const errors: string[] = [];
|
|
if (typeof value !== "object" || value === null) {
|
|
return ["File is not a labels object."];
|
|
}
|
|
const { labels } = value as Partial<LabelsFileShape>;
|
|
if (!Array.isArray(labels)) {
|
|
return ['File must have a "labels" list.'];
|
|
}
|
|
if (labels.length > MAX_LABELS) {
|
|
errors.push(`Too many labels (max ${MAX_LABELS}).`);
|
|
}
|
|
const seenIds = new Set<string>();
|
|
for (const label of labels) {
|
|
if (!isText(label?.name)) {
|
|
errors.push("Every label needs a non-empty name.");
|
|
continue;
|
|
}
|
|
const name = label.name.trim();
|
|
if (name.length > MAX_TEXT_LENGTH) {
|
|
errors.push(`Label "${name}" is over ${MAX_TEXT_LENGTH} characters.`);
|
|
}
|
|
const id = effectiveId(label);
|
|
if (!id) {
|
|
errors.push(`Label "${name}" has no usable id.`);
|
|
continue;
|
|
}
|
|
if (seenIds.has(id)) errors.push(`Duplicate label: ${name}`);
|
|
seenIds.add(id);
|
|
}
|
|
return errors;
|
|
}
|
|
|
|
/** Coerce a validated value into a normalized label list (trims, drops extras). */
|
|
export function normalizeLabels(
|
|
labels: ClassificationLabel[],
|
|
): ClassificationLabel[] {
|
|
return labels.map((label): ClassificationLabel => {
|
|
const name = label.name.trim();
|
|
const id = effectiveId(label);
|
|
// Keep the icon only if it's a known palette key, so a hand-crafted import
|
|
// can't set an unbundled key that renders blank.
|
|
return label.icon && LABEL_ICON_KEYS.has(label.icon)
|
|
? { id, name, icon: label.icon }
|
|
: { id, name };
|
|
});
|
|
}
|
|
|
|
/** Parse + validate a picked file, resolving to a normalized label list. */
|
|
export async function parseLabelsFile(
|
|
file: File,
|
|
): Promise<ClassificationLabel[]> {
|
|
let parsed: unknown;
|
|
try {
|
|
parsed = JSON.parse(await file.text());
|
|
} catch {
|
|
throw new Error("That file isn't valid JSON.");
|
|
}
|
|
const errors = validateLabels(parsed);
|
|
if (errors.length > 0) throw new Error(errors[0]);
|
|
return normalizeLabels((parsed as LabelsFileShape).labels);
|
|
}
|
|
|
|
/** Trigger a download of the labels as a pretty-printed JSON file. */
|
|
export function downloadLabels(
|
|
labels: ClassificationLabel[],
|
|
fileName = "classification-labels.json",
|
|
): void {
|
|
downloadJsonAsFile({ labels }, fileName);
|
|
}
|
|
|
|
function isText(value: unknown): value is string {
|
|
return typeof value === "string" && value.trim().length > 0;
|
|
}
|