Files
kandev/apps/web/components/task/chat/tiptap-suggestion.tsx
T
João Salavisa a2d086efb8 feat(web): add @-mention for tasks in the chat composer (#963)
* feat(web): @-mention tasks in the chat composer

Lets users type @<taskname> in the chat input to reference other tasks.
The picked task is inserted as a chip and its metadata (id, workflow_id,
step, state) is appended to the prompt as a <kandev-system> block so the
agent can act on it via the kandev MCP tools.

Extends the existing TipTap mention extension with a new "task" kind;
task data comes from the kanban store (current workflow + visited
kanbanMulti snapshots), so no backend changes are needed.

* fix(web): harden task @-mentions from review feedback (#963)

- Sanitize interpolated task strings (title, id, workflow_id, step,
  state) before they land in the <kandev-system> block so a hostile
  task title (e.g. from Jira/Linear sync) can't terminate the block
  early via embedded newlines or angle brackets.
- Expand the task-mentions context block on the onSend code path too
  (used by preview-session-tabs), which was silently dropping the
  agent-facing task context when chips were present in the editor.
- Deduplicate task mentions by taskId in getTaskMentions so mentioning
  the same task twice no longer produces redundant lines.
- Skip stale state.kanban.tasks entries whose workflowStepId isn't in
  the current workflow's steps to prevent tagging them with the wrong
  workflow_id.

* fix(web): skip task mentions missing workflowId or workflowStepId (#963)

CodeRabbit flagged that getTaskMentions was filling missing
workflowId / workflowStepId with empty strings, propagating malformed
metadata into the prompt block. Skip such nodes during extraction.

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
2026-05-21 00:57:12 +01:00

246 lines
7.0 KiB
TypeScript

"use client";
import { PluginKey } from "@tiptap/pm/state";
import type {
SuggestionOptions,
SuggestionProps,
SuggestionKeyDownProps,
} from "@tiptap/suggestion";
import type { MentionItem } from "@/hooks/use-inline-mention";
import type { SlashCommand } from "@/hooks/use-inline-slash";
import { getFileName } from "@/lib/utils/file-path";
import type { MentionKind } from "./tiptap-mention-extension";
// ── Shared types ────────────────────────────────────────────────────
export type MenuState<T> = {
isOpen: boolean;
items: T[];
query: string;
clientRect: (() => DOMRect | null) | null;
command: ((item: T) => void) | null;
};
const EMPTY_MENTION_STATE: MenuState<MentionItem> = {
isOpen: false,
items: [],
query: "",
clientRect: null,
command: null,
};
const EMPTY_SLASH_STATE: MenuState<SlashCommand> = {
isOpen: false,
items: [],
query: "",
clientRect: null,
command: null,
};
// ── Mention suggestion ──────────────────────────────────────────────
export type MentionSuggestionCallbacks = {
getItems: (query: string) => Promise<MentionItem[]>;
};
export const MentionSuggestionPluginKey = new PluginKey("mentionSuggestion");
/**
* Creates a mention suggestion config for TipTap.
* `setMenuState` drives the React rendering of MentionMenu.
* `onKeyDown` is a stable callback the parent uses to delegate keystrokes.
*/
type MentionAttrs = {
id: string;
label: string;
kind: MentionKind;
path: string;
taskId?: string | null;
workflowId?: string | null;
workflowStepId?: string | null;
taskState?: string | null;
};
export function createMentionSuggestion(
callbacks: MentionSuggestionCallbacks,
setMenuState: (state: MenuState<MentionItem>) => void,
onKeyDown: (event: KeyboardEvent) => boolean,
): Partial<SuggestionOptions<MentionItem, MentionAttrs>> {
return {
char: "@",
pluginKey: MentionSuggestionPluginKey,
allowSpaces: false,
items: async ({ query }) => {
return callbacks.getItems(query);
},
command: ({ editor, range, props: mentionAttrs }) => {
editor
.chain()
.focus()
.insertContentAt(range, [
{ type: "contextMention", attrs: mentionAttrs },
{ type: "text", text: " " },
])
.run();
},
render: () => {
return {
onStart(props: SuggestionProps<MentionItem>) {
setMenuState({
isOpen: true,
items: props.items,
query: props.query,
clientRect: props.clientRect ?? null,
command: (item: MentionItem) => {
props.command(mentionItemToAttrs(item));
},
});
},
onUpdate(props: SuggestionProps<MentionItem>) {
setMenuState({
isOpen: true,
items: props.items,
query: props.query,
clientRect: props.clientRect ?? null,
command: (item: MentionItem) => {
props.command(mentionItemToAttrs(item));
},
});
},
onKeyDown(kd: SuggestionKeyDownProps) {
if (kd.event.key === "Escape") {
setMenuState(EMPTY_MENTION_STATE);
return true;
}
return onKeyDown(kd.event);
},
onExit() {
setMenuState(EMPTY_MENTION_STATE);
},
};
},
};
}
function mentionItemPath(item: MentionItem): string {
if (item.kind === "file") return item.label;
if (item.kind === "prompt") return `prompt:${item.id}`;
if (item.kind === "task") return `task:${item.task?.taskId ?? item.id}`;
return "plan:context";
}
function mentionItemToAttrs(item: MentionItem): MentionAttrs {
const name = item.kind === "file" ? getFileName(item.label) : item.label;
const path = mentionItemPath(item);
if (item.kind === "task" && item.task) {
return {
id: item.id,
label: name,
kind: item.kind,
path,
taskId: item.task.taskId,
workflowId: item.task.workflowId,
workflowStepId: item.task.workflowStepId,
taskState: item.task.state ?? null,
};
}
return { id: item.id, label: name, kind: item.kind, path };
}
// ── Slash command suggestion ────────────────────────────────────────
export type SlashSuggestionCallbacks = {
getCommands: () => SlashCommand[];
onAgentCommand: (commandName: string) => void;
};
export const SlashSuggestionPluginKey = new PluginKey("slashSuggestion");
export function createSlashSuggestion(
callbacks: SlashSuggestionCallbacks,
setMenuState: (state: MenuState<SlashCommand>) => void,
onKeyDown: (event: KeyboardEvent) => boolean,
): Partial<SuggestionOptions<SlashCommand>> {
return {
char: "/",
pluginKey: SlashSuggestionPluginKey,
allowSpaces: false,
items: ({ query }) => {
const allCommands = callbacks.getCommands();
if (!query) return allCommands;
const lq = query.toLowerCase();
return allCommands
.filter((cmd) => {
const name = cmd.agentCommandName?.toLowerCase();
return name?.startsWith(lq) || cmd.label.toLowerCase().includes(lq);
})
.sort((a, b) => {
const an = a.agentCommandName?.toLowerCase();
const bn = b.agentCommandName?.toLowerCase();
const aPre = an?.startsWith(lq) ?? false;
const bPre = bn?.startsWith(lq) ?? false;
if (aPre && !bPre) return -1;
if (!aPre && bPre) return 1;
return 0;
});
},
command: ({ editor, range, props: cmd }) => {
editor.chain().focus().deleteRange(range).run();
if (cmd.agentCommandName) {
callbacks.onAgentCommand(cmd.agentCommandName);
}
},
render: () => {
return {
onStart(props: SuggestionProps<SlashCommand>) {
if (props.items.length === 0) return;
setMenuState({
isOpen: true,
items: props.items,
query: props.query,
clientRect: props.clientRect ?? null,
command: (cmd: SlashCommand) => props.command(cmd),
});
},
onUpdate(props: SuggestionProps<SlashCommand>) {
if (props.items.length === 0) {
setMenuState(EMPTY_SLASH_STATE);
return;
}
setMenuState({
isOpen: true,
items: props.items,
query: props.query,
clientRect: props.clientRect ?? null,
command: (cmd: SlashCommand) => props.command(cmd),
});
},
onKeyDown(kd: SuggestionKeyDownProps) {
if (kd.event.key === "Escape") {
setMenuState(EMPTY_SLASH_STATE);
return true;
}
return onKeyDown(kd.event);
},
onExit() {
setMenuState(EMPTY_SLASH_STATE);
},
};
},
};
}