An open source design system that's fully customizable and agent ready
TypeScript
fix(sandbox): add @ mention and / command triggers to AI Chat template (#1410)
Kenton Quatmancd77e6013d8f
diff --git a/packages/cli/templates/pages/ai-chat/page.tsx b/packages/cli/templates/pages/ai-chat/page.tsxindex cbc39403b1..ae4bfe0286 100644--- a/packages/cli/templates/pages/ai-chat/page.tsx+++ b/packages/cli/templates/pages/ai-chat/page.tsx@@ -26,8 +26,14 @@ import {XDSChatSystemMessage,useXDSChatDictation,type XDSChatComposerInputHandle,+ type XDSChatComposerTrigger,} from '@xds/core/Chat';import {XDSMarkdown} from '@xds/core/Markdown';+import {+ createStaticSource,+ XDSTypeaheadItem,+ type XDSSearchableItem,+} from '@xds/core/Typeahead';import {XDSTimestamp} from '@xds/core/Timestamp';import {XDSToggleButton, XDSToggleButtonGroup} from '@xds/core/ToggleButton';import {XDSButton} from '@xds/core/Button';@@ -35,7 +41,7 @@ import {XDSToken} from '@xds/core/Token';import {XDSCard} from '@xds/core/Card';import {XDSGrid} from '@xds/core/Grid';-import {XDSDropdownMenu} from '@xds/core/DropdownMenu';+import {XDSDropdownMenu, XDSDropdownMenuItem} from '@xds/core/DropdownMenu';import {ChatBubbleOvalLeftIcon,FolderIcon,@@ -173,6 +179,86 @@ const MODE_OPTIONS = [{key: 'deep', label: 'Deep Mode', icon: ClockIcon},] as const;+// ============= TRIGGER DATA =============++const MENTION_ITEMS: XDSSearchableItem<{role: string}>[] = [+ {id: 'cindy', label: 'Cindy Zhang', auxiliaryData: {role: 'Design Systems'}},+ {id: 'alex', label: 'Alex Johnson', auxiliaryData: {role: 'Frontend'}},+ {id: 'sam', label: 'Sam Rivera', auxiliaryData: {role: 'Backend'}},+ {id: 'jordan', label: 'Jordan Lee', auxiliaryData: {role: 'Product'}},+ {id: 'taylor', label: 'Taylor Kim', auxiliaryData: {role: 'Design'}},+ {+ id: 'morgan',+ label: 'Morgan Chen',+ auxiliaryData: {role: 'Infrastructure'},+ },+];++const COMMAND_ITEMS: XDSSearchableItem<{description: string}>[] = [+ {+ id: 'summarize',+ label: 'summarize',+ auxiliaryData: {description: 'Summarize the conversation'},+ },+ {+ id: 'translate',+ label: 'translate',+ auxiliaryData: {description: 'Translate text to another language'},+ },+ {+ id: 'search',+ label: 'search',+ auxiliaryData: {description: 'Search the web or documents'},+ },+ {+ id: 'code',+ label: 'code',+ auxiliaryData: {description: 'Generate or explain code'},+ },+ {+ id: 'help',+ label: 'help',+ auxiliaryData: {description: 'Show available commands'},+ },+];++const mentionSource = createStaticSource(MENTION_ITEMS);+const commandSource = createStaticSource(COMMAND_ITEMS);++const mentionTrigger: XDSChatComposerTrigger = {+ character: '@',+ searchSource: mentionSource,+ renderItem: item => (+ <XDSTypeaheadItem+ item={item}+ description={(item.auxiliaryData as {role: string})?.role}+ />+ ),+ onSelect: item => ({+ value: `@${item.id}`,+ label: item.label,+ variant: 'blue' as const,+ }),+};++const commandTrigger: XDSChatComposerTrigger = {+ character: '/',+ searchSource: commandSource,+ renderItem: item => (+ <XDSTypeaheadItem+ item={item}+ description={(item.auxiliaryData as {description: string})?.description}+ />+ ),+ onSelect: item => ({+ value: `/${item.label}`,+ label: `/${item.label}`,+ variant: 'yellow' as const,+ }),+};++const composerTriggers = [mentionTrigger, commandTrigger];+// ============= MESSAGE TYPES =============type ChatMessage =@@ -379,6 +465,7 @@ export default function AIChatTemplate() {input={<XDSChatComposerInputref={composerInputRef}+ triggers={composerTriggers}style={{minHeight: '44px'}}/>}@@ -401,27 +488,43 @@ export default function AIChatTemplate() {}headerActions={<>- <XDSButton- label="Reference"- variant="ghost"- size="sm"- icon={<AtSymbolIcon style={{width: 16, height: 16}} />}- isIconOnly- onClick={() => {- const input = composerInputRef.current;- if (!input) return;- input.focus();- const sel = window.getSelection();- if (sel) {- sel.selectAllChildren(document.activeElement!);- sel.collapseToEnd();- }- input.insertText('@');- document.activeElement?.dispatchEvent(- new Event('input', {bubbles: true}),- );+ <XDSDropdownMenu+ button={{+ label: 'Reference',+ variant: 'ghost',+ size: 'sm',+ icon: (+ <AtSymbolIcon style={{width: 16, height: 16}} />+ ),+ isIconOnly: true,}}- />+ hasChevron={false}+ menuWidth={240}>+ {MENTION_ITEMS.map(item => (+ <XDSDropdownMenuItem+ key={item.id}+ label={item.label}+ description={item.auxiliaryData?.role}+ onClick={() => {+ composerInputRef.current?.focus();+ // Move cursor to end so token is appended+ const sel = window.getSelection();+ if (sel && document.activeElement) {+ sel.selectAllChildren(document.activeElement);+ sel.collapseToEnd();+ }+ composerInputRef.current?.insertToken({+ value: `@${item.id}`,+ label: item.label,+ variant: 'blue',+ });+ document.activeElement?.dispatchEvent(+ new Event('input', {bubbles: true}),+ );+ }}+ />+ ))}+ </XDSDropdownMenu><XDSButtonlabel="Attach"variant="ghost"@@ -615,6 +718,7 @@ export default function AIChatTemplate() {input={<XDSChatComposerInputref={composerInputRef}+ triggers={composerTriggers}style={{minHeight: '44px'}}/>}@@ -637,27 +741,41 @@ export default function AIChatTemplate() {}headerActions={<>- <XDSButton- label="Reference"- variant="ghost"- size="sm"- icon={<AtSymbolIcon style={{width: 16, height: 16}} />}- isIconOnly- onClick={() => {- const input = composerInputRef.current;- if (!input) return;- input.focus();- const sel = window.getSelection();- if (sel) {- sel.selectAllChildren(document.activeElement!);- sel.collapseToEnd();- }- input.insertText('@');- document.activeElement?.dispatchEvent(- new Event('input', {bubbles: true}),- );+ <XDSDropdownMenu+ button={{+ label: 'Reference',+ variant: 'ghost',+ size: 'sm',+ icon: <AtSymbolIcon style={{width: 16, height: 16}} />,+ isIconOnly: true,}}- />+ hasChevron={false}+ menuWidth={240}>+ {MENTION_ITEMS.map(item => (+ <XDSDropdownMenuItem+ key={item.id}+ label={item.label}+ description={item.auxiliaryData?.role}+ onClick={() => {+ composerInputRef.current?.focus();+ // Move cursor to end so token is appended+ const sel = window.getSelection();+ if (sel && document.activeElement) {+ sel.selectAllChildren(document.activeElement);+ sel.collapseToEnd();+ }+ composerInputRef.current?.insertToken({+ value: `@${item.id}`,+ label: item.label,+ variant: 'blue',+ });+ document.activeElement?.dispatchEvent(+ new Event('input', {bubbles: true}),+ );+ }}+ />+ ))}+ </XDSDropdownMenu><XDSButtonlabel="Attach"variant="ghost"