Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(aui): correct mention source to use conversation participants

Steven Enamakel9c260d67df47
diff --git a/app/src/features/conversations/aui/useMentionSource.ts b/app/src/features/conversations/aui/useMentionSource.ts
new file mode 100644
index 0000000000..d9f2561969
--- /dev/null
+++ b/app/src/features/conversations/aui/useMentionSource.ts
@@ -0,0 +1,165 @@
+/**
+ * The composer's `@` mention source: OpenHuman glue that feeds assistant-ui's
+ * `unstable_useMentionAdapter` for the vendored `ComposerTriggerPopover`.
+ *
+ * Two categories:
+ * - **Memory** — semantic recall over the memory tree
+ * (`openhuman.memory_tree_recall` via {@link memoryTreeRecall}) for the
+ * `@query` being typed, debounced. The adapter's own search is a substring
+ * filter, which would drop semantic hits whose label does not literally
+ * contain the query, so `search` is extended to always include them.
+ * - **Files** — the thread's ready artifacts, read from
+ * `chatRuntime.artifactsByThread` (the list the header `ChatFilesChip`
+ * hydrates). No new RPC.
+ *
+ * Choosing an item inserts `:type[label]{name=id}` through the default
+ * directive formatter — the syntax `DirectiveText` renders as a chip in the
+ * sent message.
+ */
+import {
+ type Unstable_IconComponent,
+ type Unstable_Mention,
+ type Unstable_TriggerAdapter,
+ unstable_useMentionAdapter,
+ useAuiState,
+} from '@assistant-ui/react';
+import debug from 'debug';
+import { AtSignIcon, BrainIcon, FileIcon } from 'lucide-react';
+import { useEffect, useMemo, useRef, useState } from 'react';
+
+import { useT } from '../../../lib/i18n/I18nContext';
+import type { ArtifactSnapshot } from '../../../store/chatRuntimeSlice';
+import { useAppSelector } from '../../../store/hooks';
+import { type Chunk, memoryTreeRecall } from '../../../utils/tauriCommands/memoryTree';
+
+const log = debug('openhuman:chat:mentions');
+
+const RECALL_K = 8;
+const RECALL_DEBOUNCE_MS = 200;
+const MIN_QUERY_LENGTH = 2;
+const MAX_LABEL_LENGTH = 48;
+
+const ICON_MAP: Record<string, Unstable_IconComponent> = {
+ memory: BrainIcon,
+ files: FileIcon,
+};
+
+const NO_ARTIFACTS: readonly ArtifactSnapshot[] = [];
+
+/** The query of a trailing `@mention` (caret assumed at the end), else `null`. */
+export function trailingMentionQuery(text: string): string | null {
+ const match = /(?:^|\s)@([^\s@]*)$/.exec(text);
+ return match ? (match[1] ?? '') : null;
+}
+
+/** One line, no directive delimiters, bounded — so it round-trips as a chip label. */
+function directiveSafe(text: string): string {
+ const flat = text
+ .replace(/[[\]{}]/g, '')
+ .replace(/\s+/g, ' ')
+ .trim();
+ return flat.length > MAX_LABEL_LENGTH
+ ? `${flat.slice(0, MAX_LABEL_LENGTH - 1).trimEnd()}…`
+ : flat;
+}
+
+export function memoryMentionsFromChunks(chunks: readonly Chunk[]): Unstable_Mention[] {
+ return chunks.map(chunk => ({
+ id: directiveSafe(chunk.id),
+ type: 'memory',
+ label: directiveSafe(chunk.content_preview || chunk.source_id),
+ description: chunk.source_kind,
+ icon: 'memory',
+ }));
+}
+
+export function fileMentionsFromArtifacts(
+ artifacts: readonly ArtifactSnapshot[]
+): Unstable_Mention[] {
+ return artifacts
+ .filter(artifact => artifact.status === 'ready')
+ .map(artifact => ({
+ id: directiveSafe(artifact.artifactId),
+ type: 'file',
+ label: directiveSafe(artifact.title),
+ ...(artifact.path ? { description: artifact.path } : {}),
+ icon: 'files',
+ }));
+}
+
+/** Spreadable props for `<ComposerTriggerPopover char="@" … />`. */
+export function useMentionSource(threadId: string | null) {
+ const { t } = useT();
+ const query = useAuiState(state => trailingMentionQuery(state.composer.text));
+ const artifacts = useAppSelector(state =>
+ threadId ? (state.chatRuntime.artifactsByThread[threadId] ?? NO_ARTIFACTS) : NO_ARTIFACTS
+ );
+ const [memory, setMemory] = useState<Unstable_Mention[]>([]);
+ const [isLoading, setIsLoading] = useState(false);
+ const requestSeq = useRef(0);
+
+ useEffect(() => {
+ if (query === null || query.length < MIN_QUERY_LENGTH) return;
+ const seq = ++requestSeq.current;
+ setIsLoading(true);
+ const timer = setTimeout(() => {
+ log('recall: query_len=%d', query.length);
+ memoryTreeRecall(query, RECALL_K)
+ .then(response => memoryMentionsFromChunks(response.chunks ?? []))
+ .catch(error => {
+ log('recall failed, no memory mentions: %o', error);
+ return [] as Unstable_Mention[];
+ })
+ .then(mentions => {
+ if (seq !== requestSeq.current) return;
+ log('recall: %d hit(s)', mentions.length);
+ setMemory(mentions);
+ setIsLoading(false);
+ });
+ }, RECALL_DEBOUNCE_MS);
+ return () => clearTimeout(timer);
+ }, [query]);
+
+ const categories = useMemo(
+ () => [
+ { id: 'memory', label: t('conversations.composer.mention.memory', 'Memory'), items: memory },
+ {
+ id: 'files',
+ label: t('conversations.composer.mention.files', 'Files'),
+ items: fileMentionsFromArtifacts(artifacts),
+ },
+ ],
+ [artifacts, memory, t]
+ );
+
+ const mention = unstable_useMentionAdapter({
+ categories,
+ includeModelContextTools: false,
+ iconMap: ICON_MAP,
+ fallbackIcon: AtSignIcon,
+ });
+
+ const adapter = useMemo<Unstable_TriggerAdapter>(() => {
+ const base = mention.adapter;
+ return {
+ categories: () => base.categories(),
+ categoryItems: id => base.categoryItems(id),
+ search: q => {
+ const matched = base.search?.(q) ?? [];
+ const seen = new Set(matched.map(item => item.id));
+ const semantic = base.categoryItems('memory').filter(item => !seen.has(item.id));
+ return [...matched, ...semantic];
+ },
+ };
+ }, [mention.adapter]);
+
+ return {
+ ...mention,
+ adapter,
+ isLoading,
+ backLabel: t('conversations.composer.trigger.back', 'Back'),
+ loadingLabel: t('conversations.composer.trigger.loading', 'Loading…'),
+ emptyCategoriesLabel: t('conversations.composer.trigger.emptyCategories', 'No items available'),
+ emptyItemsLabel: t('conversations.composer.mention.empty', 'No matching items'),
+ };
+}