feat: add slash command cmd

This commit is contained in:
Patrick Erichsen 2025-04-03 18:15:30 -07:00
parent 57d46636c8
commit d4e1609d2a
10 changed files with 89 additions and 84 deletions

View file

@ -5,14 +5,13 @@ import { MAIN_EDITOR_INPUT_ID } from "../../pages/gui/Chat";
import { useAppDispatch, useAppSelector } from "../../redux/hooks";
import { setNewestCodeblocksForInput } from "../../redux/slices/sessionSlice";
import { useMainEditor } from "../mainInput/TipTapEditor";
import {
createParagraphNodeFromSlashCmdDescription,
createPromptBlockNodeFromSlashCmdDescription,
} from "../mainInput/TipTapEditor/extensions";
import { ConversationStarterCard } from "./ConversationStarterCard";
const NUM_CARDS_TO_RENDER = 5;
/**
* Displays a grid of conversation starter cards for bookmarked slash commands
*/
export function ConversationStarterCards() {
const dispatch = useAppDispatch();
const { mainEditor } = useMainEditor();
@ -34,13 +33,7 @@ export function ConversationStarterCards() {
}),
);
mainEditor?.commands.setContent({
type: "doc",
content: [
createPromptBlockNodeFromSlashCmdDescription(command),
createParagraphNodeFromSlashCmdDescription(command),
],
});
mainEditor?.commands.insertSlashCommand(command);
}
if (bookmarkedSlashCommands.length === 0) {

View file

@ -4,7 +4,7 @@ import { CodeblockPreview } from "../toolbar-previews/CodeblockPreview";
export const CODEBLOCK_NAME = "codeblock";
export const CodeBlockExtension = Node.create({
export const CodeblockExtension = Node.create({
name: CODEBLOCK_NAME,
group: "block",

View file

@ -1,7 +1,5 @@
import { mergeAttributes, Node } from "@tiptap/core";
import { ReactNodeViewRenderer } from "@tiptap/react";
import { ContextItemWithId, SlashCommandDescription } from "core";
import { MAIN_EDITOR_INPUT_ID } from "../../../../pages/gui/Chat";
import { PromptBlockPreview } from "../toolbar-previews/PromptBlockPreview";
export interface PromptOptions {
@ -16,8 +14,6 @@ export const PROMPT_BLOCK_NAME = "prompt-block";
export const PromptExtension = Node.create<PromptOptions>({
name: PROMPT_BLOCK_NAME,
group: "block",
// atom: true,
// draggable: true,
addOptions() {
return {
@ -51,31 +47,4 @@ export const PromptExtension = Node.create<PromptOptions>({
addNodeView() {
return ReactNodeViewRenderer(PromptBlockPreview);
},
});
/**
* Creates a prompt block node from a slash command description
* @param command The slash command description to create the block from
* @param inputId Optional input ID, defaults to main editor input ID
* @returns A node object representing the prompt block
*/
export const createPromptBlockNodeFromSlashCmdDescription = (
command: SlashCommandDescription,
inputId: string = MAIN_EDITOR_INPUT_ID,
) => {
return {
type: PromptExtension.name,
attrs: {
item: {
content: command.prompt,
name: command.name,
description: command.description || "",
id: {
providerTitle: "prompt",
itemId: command.name,
},
} as ContextItemWithId,
inputId,
},
};
};
});

View file

@ -6,6 +6,8 @@ import { PluginKey } from "@tiptap/pm/state";
import Suggestion, { SuggestionOptions } from "@tiptap/suggestion";
import { ContextItemWithId, SlashCommandDescription } from "core";
import { v4 as uuidv4 } from "uuid";
import { MAIN_EDITOR_INPUT_ID } from "../../../../pages/gui/Chat";
import { PROMPT_BLOCK_NAME } from "./PromptExtension";
export type SlashCommandOptions = {
HTMLAttributes: Record<string, any>;
@ -18,6 +20,22 @@ export const SLASH_CMD_SUGGESTION_CHAR = "/";
export const SlashCommandPluginKey = new PluginKey(SLASH_CMD_NAME);
declare module "@tiptap/core" {
interface Commands<ReturnType> {
slashCommand: {
/**
* Insert a slash command and its associated prompt block
* @param command The slash command description
* @param inputId The input ID (defaults to MAIN_EDITOR_INPUT_ID)
*/
insertSlashCommand: (
command: SlashCommandDescription,
inputId?: string,
) => ReturnType;
};
}
}
export const SlashCommandExtension = Node.create<SlashCommandOptions>({
name: SLASH_CMD_NAME,
@ -58,9 +76,6 @@ export const SlashCommandExtension = Node.create<SlashCommandOptions>({
const promptId = uuidv4();
// Create a generic "promptToPromptBlockExtension" fn
// For here and in the conversation starters
editor
.chain()
.focus()
@ -200,30 +215,58 @@ export const SlashCommandExtension = Node.create<SlashCommandOptions>({
}),
];
},
});
/**
* Creates a paragraph node containing a slash command reference
* @param command The slash command description to create the reference from
* @returns A node object representing the paragraph with slash command
*/
export const createParagraphNodeFromSlashCmdDescription = (
command: SlashCommandDescription,
) => {
return {
type: "paragraph",
content: [
{
type: SlashCommandExtension.name,
attrs: {
id: command.name,
label: command.name,
addCommands() {
return {
insertSlashCommand:
(
command: SlashCommandDescription,
inputId: string = MAIN_EDITOR_INPUT_ID,
) =>
({ chain }) => {
// Create the document with both a prompt block and the slash command paragraph
return chain()
.setContent({
type: "doc",
content: [
// Add the prompt block at the beginning
{
type: PROMPT_BLOCK_NAME,
attrs: {
item: {
content: command.prompt,
name: command.name,
description: command.description || "",
id: {
providerTitle: "prompt",
itemId: command.name,
},
} as ContextItemWithId,
inputId,
},
},
// Add the paragraph with the slash command
{
type: "paragraph",
content: [
{
type: this.name,
attrs: {
id: command.name,
label: command.name,
},
},
{
type: "text",
text: " ",
},
],
},
],
})
.focus()
.run();
},
},
{
type: "text",
text: " ",
},
],
};
};
};
},
});

View file

@ -1,5 +1,5 @@
export * from "./AddCodeToEditExtension";
export * from "./CodeBlockExtension";
export * from "./CodeblockExtension";
export * from "./MentionExtension";
export * from "./PromptExtension";
export * from "./SlashCommandExtension";
export * from "./SlashCommandExtension";

View file

@ -9,7 +9,7 @@ import {
} from "../../../redux/slices/sessionSlice";
import { AppDispatch } from "../../../redux/store";
import { loadSession, saveCurrentSession } from "../../../redux/thunks/session";
import { CodeBlockExtension } from "./extensions";
import { CodeblockExtension } from "./extensions";
/**
* Hook for setting up main editor specific webview listeners
@ -114,7 +114,7 @@ export function useMainEditorWebviewListeners({
if (el.attrs?.item?.name === contextItem.name) {
return; // Prevent exact duplicate code blocks
}
if (el.type === CodeBlockExtension.name) {
if (el.type === CodeblockExtension.name) {
index += 2;
} else {
break;
@ -124,7 +124,7 @@ export function useMainEditorWebviewListeners({
editor
.chain()
.insertContentAt(index, {
type: CodeBlockExtension.name,
type: CodeblockExtension.name,
attrs: {
item: contextItem,
inputId,

View file

@ -364,7 +364,7 @@ export function createEditorConfig(options: {
class: "prompt-block",
},
}),
ContinueExtensions.CodeBlockExtension,
ContinueExtensions.CodeblockExtension,
],
editorProps: {
attributes: {

View file

@ -19,7 +19,7 @@ import { getUriFileExtension } from "core/util/uri";
import { IIdeMessenger } from "../../../../context/IdeMessenger";
import { setIsGatheringContext } from "../../../../redux/slices/sessionSlice";
import {
CodeBlockExtension,
CodeblockExtension,
MentionExtension,
SlashCommandExtension,
} from "../extensions";
@ -86,7 +86,7 @@ function processEditorContent(editorState: JSONContent) {
return [...parts, { type: "text", text }];
}
case CodeBlockExtension.name: {
case CodeblockExtension.name: {
if (!p.attrs?.item) {
console.warn("codeBlock has no item attribute");
return parts;

View file

@ -1,7 +1,7 @@
import { Editor } from "@tiptap/react";
import { KeyboardEvent } from "react";
import { getPlatform, isWebEnvironment } from "../../../util";
import { CodeBlockExtension } from "../TipTapEditor/extensions";
import { CodeblockExtension } from "../TipTapEditor/extensions";
const isWebEnv = isWebEnvironment();
@ -104,7 +104,7 @@ export const handleJetBrainsMetaBackspace = (editor: Editor) => {
for (let i = doc.content.childCount - 1; i >= 0; i--) {
const node = doc.content.child(i);
if (node.type.name === CodeBlockExtension.name) {
if (node.type.name === CodeblockExtension.name) {
continue;
}

View file

@ -1,6 +1,6 @@
import { createAsyncThunk } from "@reduxjs/toolkit";
import { ChatHistoryItem, ContextItemWithId } from "core";
import { CodeBlockExtension } from "../../components/mainInput/TipTapEditor/extensions";
import { CodeblockExtension } from "../../components/mainInput/TipTapEditor/extensions";
import { updateFileSymbols } from "../slices/sessionSlice";
import { ThunkApiType } from "../store";
@ -26,7 +26,7 @@ export function getContextItemsFromHistory(
.flatMap((item) => item.editorState.content)
.filter(
(content) =>
content?.type === CodeBlockExtension.name &&
content?.type === CodeblockExtension.name &&
content?.attrs?.item?.uri?.value &&
content.attrs.item.uri?.type === "file",
)