Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(ai-assistant): open the assistant from the keyboard
Elian Doran425dd8917b5b
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 5ddd73c35c..57a4590679 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -205,6 +205,7 @@"editingNotes": "Editing notes","editNoteTitle": "in tree pane will switch from tree pane into note title. Enter from note title will switch focus to text editor. <kbd>Ctrl+.</kbd> will switch back from editor to tree pane.","createEditLink": "create / edit external link",+ "openAiAssistant": "open the AI assistant on the selection","createInternalLink": "create internal link","followLink": "follow link under cursor","insertDateTime": "insert current date and time at caret position",diff --git a/apps/client/src/widgets/dialogs/help.tsx b/apps/client/src/widgets/dialogs/help.tsxindex 64228601b3..bd54f040ba 100644--- a/apps/client/src/widgets/dialogs/help.tsx+++ b/apps/client/src/widgets/dialogs/help.tsx@@ -76,6 +76,9 @@ export default function HelpDialog() {<ul><KeyboardShortcut commands="editNoteTitle" description={t("help.editNoteTitle")} /><FixedKeyboardShortcut keys={["Ctrl+K"]} description={t("help.createEditLink")} />+ {/* The editor's own, like the link above it: registered by the AI assistant+ plugin rather than by a keyboard action, so it is not configurable. */}+ <FixedKeyboardShortcut keys={["Ctrl+Shift+K"]} description={t("help.openAiAssistant")} /><KeyboardShortcut commands="addLinkToText" description={t("help.createInternalLink")} /><KeyboardShortcut commands="followLinkUnderCursor" description={t("help.followLink")} /><KeyboardShortcut commands="insertDateTimeToText" description={t("help.insertDateTime")} />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.tsindex 23ab9addac..9ad09f687e 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@@ -1,4 +1,4 @@-import { _getModelData as getModelData, _setModelData as setModelData, ButtonView, ClassicEditor, CodeBlockEditing, Dialog, Essentials, Paragraph, SplitButtonView } from "ckeditor5";+import { _getModelData as getModelData, _setModelData as setModelData, ButtonView, ClassicEditor, CodeBlockEditing, Dialog, Essentials, keyCodes, Paragraph, SplitButtonView } from "ckeditor5";import { beforeEach, describe, expect, it, vi } from "vitest";import { createTestEditor } from "../../../test/editor-kit.js";@@ -218,6 +218,51 @@ describe("AiAssistantUI toolbar entry", () => {expect(showSpy).toHaveBeenCalled();});+ // The feature has to be reachable without leaving the text being written, and the toolbar+ // tooltip is where a keystroke is discovered.+ describe("the keystroke", () => {+ /** Presses Ctrl+Shift+K on the editor, reporting whether the editor took the key. */+ function press(editor: ClassicEditor): boolean {+ const preventDefault = vi.fn();+ const keyEvtData = {+ keyCode: keyCodes.k,+ ctrlKey: true,+ shiftKey: true,+ altKey: false,+ metaKey: false,+ preventDefault,+ stopPropagation: vi.fn()+ };++ editor.keystrokes.press(keyEvtData);+ // What `cancel()` does, so this says whether the editor took the key or let it through.+ return preventDefault.mock.calls.length > 0;+ }++ it("opens the assistant, and says so on the toolbar entry", async () => {+ const editor = await createEditor(QUICK_ACTIONS, createStreamStub().stream);+ const showSpy = vi.spyOn(editor.plugins.get(AiAssistantUI), "show");++ expect(press(editor)).toBe(true);+ expect(showSpy).toHaveBeenCalled();++ // The action half of the split button, which is the half the keystroke stands for.+ expect(createComponent(editor).buttonView.keystroke).toBe("Ctrl+Shift+K");+ const plain = (await createEditor(undefined, createStreamStub().stream))+ .ui.componentFactory.create("aiAssistant") as ButtonView;+ expect(plain.keystroke).toBe("Ctrl+Shift+K");+ });++ it("lets the key through when no LLM provider is configured", async () => {+ // Swallowing it would cost the user a keystroke to a feature that cannot run.+ const editor = await createEditor(QUICK_ACTIONS);+ const showSpy = vi.spyOn(editor.plugins.get(AiAssistantUI), "show");++ expect(press(editor)).toBe(false);+ expect(showSpy).not.toHaveBeenCalled();+ });+ });+describe("with quick actions configured", () => {let editor: ClassicEditor;let dropdown: QuickActionsDropdown;diff --git a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.ts b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.tsindex 9dcc3066c2..ec6948e2e0 100644--- a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.ts+++ b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.ts@@ -61,6 +61,17 @@ const SURROUNDINGS_LIMIT = 1500;/** What a run with nothing around it sends, and what the plugin holds while it is closed. */const EMPTY_SURROUNDINGS: AiSurroundings = { before: "", after: "" };+/**+ * Opens the assistant from the keyboard, which is how the feature is reached without leaving the+ * text being written.+ *+ * Neither of the idioms it would like is free: Ctrl+K creates a link, here as in every other+ * editor, and Ctrl+J is Trilium's Jump to note. This keeps the K that Cursor, Linear and Slack+ * made the key for "ask for something" without taking either — and CKEditor maps `Ctrl` to `Cmd`+ * on macOS, so it reads as ⌘⇧K there.+ */+const AI_ASSISTANT_KEYSTROKE = "Ctrl+Shift+K";+/*** The AI assistant's UI and orchestration: the toolbar entry (a split button whose menu holds the* host's quick actions), the dialog form, and the stream-preview-commit lifecycle.@@ -163,6 +174,17 @@ export default class AiAssistantUI extends Plugin {editor.ui.componentFactory.add("aiAssistant", (locale) => this._createToolbarComponent(locale));+ editor.keystrokes.set(AI_ASSISTANT_KEYSTROKE, (_data, cancel) => {+ const command = editor.commands.get("aiAssistant");+ // Nothing to open without a provider configured, and the keystroke has to fall through+ // rather than be swallowed by a feature that cannot run.+ if (!command?.isEnabled) {+ return;+ }+ editor.execute("aiAssistant");+ cancel();+ });+// While the assistant is closed the quick actions are offered against the selection, so// their enablement has to follow it.this.listenTo(editor.model.document.selection, "change:range", () => this._updateHasContext());@@ -232,6 +254,7 @@ export default class AiAssistantUI extends Plugin {buttonView.set({label: locale.t("AI assistant"),icon: aiIcon,+ keystroke: AI_ASSISTANT_KEYSTROKE,tooltip: true});/* v8 ignore next -- AiAssistantEditing always registers the command (it is required by this plugin) */@@ -256,6 +279,9 @@ export default class AiAssistantUI extends Plugin {splitButtonView.set({label: locale.t("AI assistant"),icon: aiIcon,+ // On the action half, which is the half the keystroke stands for: the arrow opens the+ // quick actions, and Ctrl+Shift+K opens the prompt.+ keystroke: AI_ASSISTANT_KEYSTROKE,tooltip: true});// A dropdown passes `isEnabled` down to its button, so this covers both halves of the