Skip to content

Build your personal knowledge base with Trilium Notes

feat(ai-assistant): head the model list by provider instead of bracketing it

Elian Doran84c69787bb4a
diff --git a/apps/client/src/widgets/type_widgets/text/ai_model_picker.spec.ts b/apps/client/src/widgets/type_widgets/text/ai_model_picker.spec.ts
index 3d8af00471..228e40b0e9 100644
--- a/apps/client/src/widgets/type_widgets/text/ai_model_picker.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/ai_model_picker.spec.ts
@@ -38,8 +38,9 @@ const TWO_PROVIDERS = [
}
];
-function labelsOf(picker: ReturnType<typeof buildAiModelPicker>) {
- return (picker[0]?.children ?? []).map((row) => [row.label, row.iconClass]);
+/** Each row as it reads: the heading it opens a block with, its label, and its icon slot. */
+function rowsOf(picker: ReturnType<typeof buildAiModelPicker>) {
+ return (picker[0]?.children ?? []).map((row) => [row.heading ?? "", row.label, row.iconClass]);
}
describe("the AI assistant's model picker", () => {
@@ -56,10 +57,11 @@ describe("the AI assistant's model picker", () => {
// More than one provider is configured, so a row says which one it means: the same model id
// can be reached through two of them.
expect(picker[0].label).toBe("ai_assistant.model(model=GPT-5 (OpenAI))");
- expect(labelsOf(picker)).toEqual([
- ["Opus 5 (Anthropic)", "bx bx-empty"],
- ["Sonnet 5 (Anthropic)", "bx bx-empty"],
- ["GPT-5 (OpenAI)", "bx bx-check"]
+ // Headed by their provider rather than trailing it in brackets on every row.
+ expect(rowsOf(picker)).toEqual([
+ ["Anthropic", "Opus 5", "bx bx-empty"],
+ ["", "Sonnet 5", "bx bx-empty"],
+ ["OpenAI", "GPT-5", "bx bx-check"]
]);
});
@@ -70,7 +72,8 @@ describe("the AI assistant's model picker", () => {
const picker = buildAiModelPicker();
expect(picker[0].label).toBe("ai_assistant.model(model=Sonnet 5)");
- expect(labelsOf(picker)).toEqual([["Opus 5", "bx bx-empty"], ["Sonnet 5", "bx bx-check"]]);
+ // One provider distinguishes nothing, so no heading and no bracket.
+ expect(rowsOf(picker)).toEqual([["", "Opus 5", "bx bx-empty"], ["", "Sonnet 5", "bx bx-check"]]);
});
// Nothing flagged, so the first one answers — and the row says so.
@@ -87,8 +90,8 @@ describe("the AI assistant's model picker", () => {
const picker = buildAiModelPicker();
expect(picker[0].label).toBe("ai_assistant.model(model=Sonnet 5 (Anthropic))");
- expect(labelsOf(picker).filter(([, icon]) => icon === "bx bx-check"))
- .toEqual([["Sonnet 5 (Anthropic)", "bx bx-check"]]);
+ expect(rowsOf(picker).filter(([, , icon]) => icon === "bx bx-check"))
+ .toEqual([["", "Sonnet 5", "bx bx-check"]]);
});
// A row offering the only answer there is takes up space and settles nothing.
diff --git a/apps/client/src/widgets/type_widgets/text/ai_model_picker.ts b/apps/client/src/widgets/type_widgets/text/ai_model_picker.ts
index 035b382b4c..44a1ab6250 100644
--- a/apps/client/src/widgets/type_widgets/text/ai_model_picker.ts
+++ b/apps/client/src/widgets/type_widgets/text/ai_model_picker.ts
@@ -30,18 +30,19 @@ export function buildAiModelPicker(): AiQuickActionFooter[] {
return [];
}
- // The same model can be reached through two configured providers, so the rows have to say which
- // one they mean — but only where there is more than one to confuse them with.
- const name = (model: ModelOption) => {
- const short = shortModelName(model.name, model.provider);
- return groups.length > 1 && model.providerName ? `${short} (${model.providerName})` : short;
- };
+ const name = (model: ModelOption) => shortModelName(model.name, model.provider);
return [{
- label: t("ai_assistant.model", { model: name(current) }),
+ // The row above the list still has to name one model out of several providers on its own,
+ // with no heading over it to say whose it is.
+ label: t("ai_assistant.model", { model: nameWithProvider(current, groups.length) }),
iconClass: "bx bx-chip",
- children: models.map((model) => ({
+ children: groups.flatMap((group) => group.models.map((model, index) => ({
label: name(model),
+ // Headed by the provider it belongs to, as the chat's picker heads them — the same
+ // model can be reached through two of them, and a name saying so in brackets on every
+ // row says it over and over.
+ ...(index === 0 && groups.length > 1 ? { heading: group.name } : {}),
// The tick every checked row in Trilium wears, and the reserver that keeps the labels
// of the unticked ones lined up with it.
iconClass: model === current ? "bx bx-check" : "bx bx-empty",
@@ -50,7 +51,7 @@ export function buildAiModelPicker(): AiQuickActionFooter[] {
provider: model.provider,
providerId: model.providerId
} satisfies StoredModel))
- }))
+ })))
}];
}
@@ -87,6 +88,15 @@ function resolveModel(models: ModelOption[]): ModelOption | undefined {
return resolveStoredModel(models) ?? models.find((model) => model.isDefault) ?? models[0];
}
+/**
+ * A model named for somewhere with no heading to lean on — the row over the list. The provider
+ * comes along only when more than one is configured, since that is the only time it distinguishes.
+ */
+function nameWithProvider(model: ModelOption, providerCount: number): string {
+ const short = shortModelName(model.name, model.provider);
+ return providerCount > 1 && model.providerName ? `${short} (${model.providerName})` : short;
+}
+
/** What the `aiAssistantModel` option holds — the three fields it takes to name a model exactly. */
interface StoredModel {
model: string;
diff --git a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_config.ts b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_config.ts
index afa0145f45..5512f39b9c 100644
--- a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_config.ts
+++ b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_config.ts
@@ -154,6 +154,12 @@ export interface AiQuickActionFooter {
label: string;
/** The row's icon, as a Boxicon class list. */
iconClass?: string;
+ /**
+ * A heading over this row, opening the block it starts — how a long list of rows says what
+ * divides it. Already translated by the host, and only honoured inside a {@link children} list,
+ * which is where a list long enough to want dividing ends up.
+ */
+ heading?: string;
/**
* Rows this one opens onto, making it a submenu rather than something that runs. A row with
* children never runs, so {@link run} is beside the point for it.
diff --git a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.spec.ts b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.spec.ts
index 0b44da4eb3..33d1e9ed1c 100644
--- a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.spec.ts
+++ b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.spec.ts
@@ -416,6 +416,30 @@ describe("AiAssistantUI toolbar entry", () => {
expect(requests).toHaveLength(0);
});
+ // A list long enough to want dividing says what divides it. The menu definition carries
+ // buttons and submenus and nothing else, so the headings are put in beside the rows.
+ it("heads the blocks of a footer submenu that asked for headings", () => {
+ editor.plugins.get(AiAssistantUI).updateMenuFooter([{
+ label: "Model: Sonnet 5",
+ children: [
+ { label: "Sonnet 5", heading: "Anthropic", run: vi.fn() },
+ { label: "Opus 5", run: vi.fn() },
+ { label: "GPT-5", heading: "OpenAI", run: vi.fn() }
+ ]
+ }]);
+
+ dropdown.isOpen = false;
+ dropdown.isOpen = true;
+ const [picker] = openMenu(dropdown).menus.filter((menu) => menu.id.startsWith("__footer"));
+
+ // A heading is a plain view beside the rows, so it reads as a gap in the button list.
+ expect(picker.listView.items.map((item) => item.childView?.label ?? "---"))
+ .toEqual(["---", "Sonnet 5", "Opus 5", "---", "GPT-5"]);
+ expect([ ...picker.listView.items ].flatMap((item) =>
+ (item.childView ? [] : [(item as unknown as { element?: HTMLElement }).element?.textContent])))
+ .toEqual(["Anthropic", "OpenAI"]);
+ });
+
it("runs an action picked from inside a submenu", async () => {
// A submenu button delegates its `execute` up through its menu to the root rather than
// firing on the dropdown itself, so this is a different path from an inlined action.
diff --git a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.ts b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.ts
index f37ee8a543..57f8811790 100644
--- a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.ts
+++ b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.ts
@@ -238,6 +238,8 @@ export default class AiAssistantUI extends Plugin {
// where one set of actions ends and the next begins. Consecutive submenus need none: they
// read as one block of "openers" already.
let separatorAt: number[] = [];
+ /** Where each footer submenu wants headings, by its id. */
+ const headingsIn = new Map<string, Array<{ index: number; label: string }>>();
/**
* Lets go of the menu a previous draw left behind. `addMenuToDropdown` only ever assigns
@@ -264,6 +266,7 @@ export default class AiAssistantUI extends Plugin {
actionsById.clear();
footersById.clear();
groupsById.clear();
+ headingsIn.clear();
separatorAt = [];
const groups = this._quickActions;
@@ -272,13 +275,22 @@ export default class AiAssistantUI extends Plugin {
/** Records a footer row against its id and describes it, opener or leaf, to the menu. */
const defineFooter = (id: string, footer: AiQuickActionFooter): DropdownMenuDefinition[number] => {
footersById.set(id, footer);
- return footer.children
- ? {
- id,
- menu: footer.label,
- children: footer.children.map((child, index) => defineFooter(`${id}:${index}`, child))
- }
- : { id, label: footer.label };
+ if (!footer.children) {
+ return { id, label: footer.label };
+ }
+
+ // The headings its children asked for, as indices into the submenu's finished item
+ // list — the same bookkeeping the top level does for its rules, and for the same
+ // reason: a menu definition carries buttons and submenus, nothing else.
+ headingsIn.set(id, footer.children.flatMap((child, index) => (child.heading
+ ? [{ index, label: child.heading }]
+ : [])));
+
+ return {
+ id,
+ menu: footer.label,
+ children: footer.children.map((child, index) => defineFooter(`${id}:${index}`, child))
+ };
};
// Heads the menu: the typed prompt every row below it is a shortcut past, and what the
@@ -357,6 +369,10 @@ export default class AiAssistantUI extends Plugin {
// A footer submenu holds settings, not instructions, so nothing about it turns
// on there being content to work on.
addIcon(menu.buttonView, footer.iconClass);
+ // Back to front, as at the top level: each insertion shifts what follows it.
+ for (const { index, label } of [...(headingsIn.get(menu.id) ?? [])].reverse()) {
+ menu.listView.items.add(new ListHeaderView(locale, label), index);
+ }
continue;
}
@@ -925,6 +941,32 @@ function addIcon(button: ButtonView, iconClass: string | undefined): void {
}
}
+/**
+ * A heading over a block of rows, in the markup CKEditor styles its own list groups with — bold,
+ * small, ruled off from the block above.
+ *
+ * `ListItemGroupView` would bring the same look but wants to *own* the rows under it, and the menu
+ * builds those from a definition that has no notion of a group. A heading inserted beside them,
+ * the way the separators are, needs no such ownership. It is presentational: the rows keep their
+ * own roles, and a screen reader is not told a menu item sits here.
+ */
+class ListHeaderView extends View {
+
+ constructor(locale: Locale | undefined, label: string) {
+ super(locale);
+
+ this.setTemplate({
+ tag: "li",
+ attributes: { class: ["ck", "ck-list__group"], role: "presentation" },
+ children: [{
+ tag: "span",
+ attributes: { class: ["ck", "ck-label"] },
+ children: [{ text: label }]
+ }]
+ });
+ }
+}
+
/**
* An icon-pack glyph as a plain `<span>`. CKEditor's `IconView` takes SVG source, which a font
* icon has none of; Trilium owns this plugin, so the class list can be rendered directly.