diff --git a/.changeset/37858-ask-ai-entry-button.md b/.changeset/37858-ask-ai-entry-button.md new file mode 100644 index 00000000000..afbf7862165 --- /dev/null +++ b/.changeset/37858-ask-ai-entry-button.md @@ -0,0 +1,19 @@ +--- +'@object-ui/app-shell': patch +'@object-ui/plugin-report': patch +--- + +feat(app-shell): the assistant's entry is an outlined 「Ask AI」 button at the end of every page's top-right group, and a route with no page header gets it in its corner + +Clause-②: no +Metadata-protocol: none + +The assistant's one standing entry was a 32 px transparent robot icon named only by its tooltip. It is now an outlined button with a chat-bubble icon and the words 「Ask AI」 (「问 AI」 in Chinese), read from the string Home's own 「Ask AI」 button already carries in every built-in pack (`home.askAI`), so no language pack changes. The words are the platform's; no app renames them. + +- **Where it sits.** Last in the page's top-right button group (comments, help, Ask AI), so it ends the page header's row and the record page's navigation row; after help on Home's bar and in the phone's top bar. A route whose page draws no button group (search results, a component page, the app editor) gets the same button over the empty top end corner of its content (top-right; top-left in a right-to-left language), and loses it as soon as its page draws a group. A page that keeps its own controls in that corner draws the group in its own top bar instead, so the button never covers them: the report builder (a new report, and a report's Edit) ends its header with Discard, Save, help, Ask AI, and the inbox ends its header row with its own buttons, help, Ask AI. +- **When the header runs out of room.** The console page header now measures whether its title (and, on a page that offers the folded actions, its description) still fits beside the words; when it does not, the words are hidden first and the icon stays, before the page's own actions fold and before the title is cut. They come back once there is room again, after any fold has opened. The decision reads the header's own width, never a breakpoint, so a longer translation hides its words sooner. +- **Phone.** The icon alone, a 44 px target. +- **Accessibility.** With its words shown the words are the button's accessible name; with the icon alone `aria-label` carries the same words. On a Mac the tooltip shows ⌘⇧I and the button declares `aria-keyshortcuts="Meta+Shift+I"`; on other platforms, where Ctrl+Shift+I opens the browser's developer tools, no shortcut is shown or declared. +- **Home.** Home's 「Ask AI」 opens the drawer through the same entry hook as the page's button (`useAskAiEntry`), still on the ask agent. + +`useShellPageActionsSlot()`'s render function takes one more optional prop, `assistantIconOnly`, which a header that measures its own width passes; a header that passes nothing gets the button with its words, as before. `ReportBuilder` (`@object-ui/plugin-report`) takes an optional `headerEnd`, drawn after Save at the end of its header; without it the header ends with Save, as before. The drawer keeps its title for now. diff --git a/ui/packages/app-shell/src/console/home/HomePage.tsx b/ui/packages/app-shell/src/console/home/HomePage.tsx index 1f30b198684..6760f624442 100644 --- a/ui/packages/app-shell/src/console/home/HomePage.tsx +++ b/ui/packages/app-shell/src/console/home/HomePage.tsx @@ -91,7 +91,7 @@ import { fetchPackages } from '../../views/studio-design/packages-io.js'; import { resolveAiApiBase } from '../../hooks/useAiSurface.js'; import { isAiStudioEnabled, isMarketplaceEnabled } from '../../runtime-config.js'; import { useNewPackageDoorPath } from '../../hooks/useNewPackageDoorPath.js'; -import { useOpenAssistant } from '../../layout/chatDockControl.js'; +import { useAskAiEntry } from '../../layout/AssistantTopBarButton.js'; import { isProductApp } from '../../utils/homePath.js'; import { useAddAppEntries, useMayAddApp } from '../../hooks/useAddAppEntries.js'; import { catalogServesBuild } from '../../hooks/surfaceAgent.js'; @@ -454,9 +454,11 @@ export { readRecentObjects, type RecentObjectReading }; export function HomePage({ checklist }: HomePageProps = {}) { const navigate = useNavigate(); // objectstack#34608 — the Home AI buttons open the same right-side drawer - // every console page has (in place, no navigation). - const openAssistant = useOpenAssistant(); - const openHomeAssistant = (agent: 'ask') => openAssistant({ agent }); + // every console page has (in place, no navigation). objectstack#37858 — + // through the Ask AI entry the top bar's button opens it with, so the two + // change together when opening comes to mean a new conversation. + const askAi = useAskAiEntry(); + const openHomeAssistant = (agent: 'ask') => askAi({ agent }); const { t, language } = useObjectTranslation(); const metadata = useMetadata(); const { apps, loading } = metadata; diff --git a/ui/packages/app-shell/src/console/home/__tests__/HomePage.askAiEntry-37858.test.tsx b/ui/packages/app-shell/src/console/home/__tests__/HomePage.askAiEntry-37858.test.tsx new file mode 100644 index 00000000000..221d8eae52f --- /dev/null +++ b/ui/packages/app-shell/src/console/home/__tests__/HomePage.askAiEntry-37858.test.tsx @@ -0,0 +1,176 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * objectstack#37858 — Home's 「Ask AI」 opens the assistant through the same + * entry as the page's Ask AI button (`useAskAiEntry`), so when opening comes + * to mean "a new conversation" the two change together. Today it still asks + * the `ask` agent, as it did. + * + * Real subject: `HomePage`, with the stand-ins of + * `HomePage.phoneTapTargets-29147.test.tsx` (the catalog moves the way the + * real `useAgents` does); the entry is observed, not run. + */ + +import '@testing-library/jest-dom/vitest'; +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; +import React from 'react'; +import { render, screen, cleanup, fireEvent } from '@testing-library/react'; + +const navigateMock = vi.fn(); + +const state = vi.hoisted(() => ({ + apps: [] as Array>, + /** The catalog's answer, once it has answered. */ + agents: [] as Array<{ name: string }>, + askAgent: false, + buildAgent: false, + /** Set once the stand-in has drawn the catalog's answer (objectstack#37227). */ + answered: false, +})); + +vi.mock('react-router-dom', async () => { + const { createElement } = await import('react'); + return { + useNavigate: () => navigateMock, + Link: ({ to, ...rest }: { to: string } & Record) => + createElement('a', { ...rest, href: to }), + }; +}); + +vi.mock('@object-ui/i18n', async (importOriginal) => ({ + ...(await importOriginal>()), + useObjectTranslation: () => ({ t: (key: string) => `«${key}»`, language: 'en' }), + useObjectLabel: () => { + const label = (x: { name?: string; label?: string }) => String(x?.label || x?.name || ''); + return { + appLabel: label, + objectLabel: label, + objectPluralLabel: label, + dashboardLabel: label, + pageLabel: label, + reportLabel: label, + viewLabel: (_object: string, _view: string, fallback?: string) => fallback ?? '', + }; + }, +})); + +vi.mock('@object-ui/auth', async (importOriginal) => ({ + ...(await importOriginal>()), + useAuth: () => ({ user: { id: 'u1', name: 'Dev Admin', email: 'dev.admin@example.com' } }), + useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }), +})); + +// The catalog moves the way the real `useAgents` does: an empty first frame, +// then loading, then the answer. +vi.mock('@object-ui/plugin-chatbot/core', async (importOriginal) => { + const { useEffect, useState } = await import('react'); + return { + ...(await importOriginal()), + useAgents: () => { + const [phase, setPhase] = useState<'idle' | 'loading' | 'answered'>('idle'); + useEffect(() => { + if (phase === 'idle') setPhase('loading'); + else if (phase === 'loading') setPhase('answered'); + }, [phase]); + if (phase === 'answered') state.answered = true; + return { agents: phase === 'answered' ? state.agents : [], isLoading: phase === 'loading' }; + }, + isAskAgent: () => state.askAgent, + agentHasCapability: () => state.buildAgent, + }; +}); + +vi.mock('../../../providers/MetadataProvider', () => ({ + useMetadata: () => ({ apps: state.apps, objects: [], dashboards: [], pages: [], reports: [], loading: false }), +})); +vi.mock('../../../context/NavigationContext', () => ({ + useNavigationContext: () => ({ currentAppName: 'crm' }), +})); +vi.mock('../../../hooks/useCanAuthorMetadata', () => ({ + useCanAuthorMetadata: () => true, + useCanReadPendingDrafts: () => true, +})); +vi.mock('../../../runtime-config', () => ({ + getRuntimeConfig: () => ({ branding: { productName: 'ObjectStack' } }), + isMarketplaceEnabled: () => true, + isAiStudioEnabled: () => true, +})); + +const now = new Date().toISOString(); +vi.mock('../../../hooks/useHomeInbox', () => ({ + useHomeInbox: () => ({ + pendingApprovals: [{ id: 'apr_1', processLabel: 'Discount approval', recordTitle: 'QUO-0007', createdAt: now }], + pendingApprovalsCount: 1, + pendingApprovalsStatus: 'ready', + pendingApprovalsReadable: true, + notifications: [{ id: 'n1', title: 'Lead assigned', createdAt: now }], + unreadTopicCount: 1, + notificationsStatus: 'ready', + notificationsReadable: true, + activity: { + value: [{ id: 'a1', user: 'Ada', description: 'updated a lead', timestamp: now, objectName: 'crm_lead' }], + status: 'ready', + readable: true, + }, + }), +})); +vi.mock('../../../hooks/useRecentItems', () => ({ + useRecentItems: () => ({ + recentItems: [{ id: 'object:crm_lead', type: 'object', name: 'crm_lead', href: '/apps/crm/crm_lead', visitedAt: now }], + }), +})); +vi.mock('../../../hooks/useFavorites', () => ({ useFavorites: () => ({ favorites: [] }) })); +vi.mock('../../../hooks/useAiSurface', () => ({ resolveAiApiBase: () => '' })); +vi.mock('../../../preview/usePendingDrafts', () => ({ + usePendingDrafts: () => ({ count: 1, entries: [{ name: 'crm_lead', label: 'Lead' }] }), +})); +vi.mock('../../../preview/usePublishAllDrafts', () => ({ + usePublishAllDrafts: () => ({ publishAll: async () => ({ ok: true }), publishing: false }), +})); +vi.mock('../../../hooks/useInboxBell', () => ({ useInboxBell: () => ({ markRead: vi.fn() }) })); +vi.mock('../../../layout/ActivityObjectLabel', () => ({ ActivityObjectLabel: () => null })); + +// objectstack#37858 — the entry the top bar's Ask AI button opens the drawer +// through, observed: Home's button must reach the drawer by the same door. +const askAiEntry = vi.hoisted(() => ({ calls: [] as unknown[] })); +vi.mock('../../../layout/AssistantTopBarButton', async (importOriginal) => ({ + ...(await importOriginal>()), + useAskAiEntry: () => (target?: unknown) => { + askAiEntry.calls.push(target); + }, +})); + +// The deployment notice reads its own state from the server for an administrator +// (objectstack#30120); its fetch, its gate and its copy are pinned in +// `deployment-notice/__tests__/DeploymentNotice.test.tsx`, so it is stubbed here +// like every other source this page reads. +vi.mock('../../deployment-notice/DeploymentNotice', () => ({ DeploymentNotice: () => null })); + +// The page reaches `capabilityLint`'s dynamic `@objectstack/lint` import; its +// first load is paid here, at collection, never inside a case's clock +// (`check:test-source-alias`). +import '@objectstack/lint'; +import { HomePage } from '../HomePage'; + +beforeEach(() => { + navigateMock.mockReset(); + askAiEntry.calls = []; + state.apps = [{ name: 'crm', label: 'CRM', icon: 'Box' }]; + state.agents = [{ name: 'ask' }]; + state.askAgent = true; + state.buildAgent = false; + state.answered = false; +}); + +afterEach(() => { + cleanup(); +}); + +describe("Home's Ask AI opens the assistant's one entry (objectstack#37858)", () => { + it('a click goes through the entry, on the ask agent, and navigates nowhere', async () => { + render(); + fireEvent.click(await screen.findByTestId('home-ask-ai')); + expect(askAiEntry.calls).toEqual([{ agent: 'ask' }]); + expect(navigateMock).not.toHaveBeenCalled(); + }); +}); diff --git a/ui/packages/app-shell/src/console/marketplace/MarketplacePackagePage.tsx b/ui/packages/app-shell/src/console/marketplace/MarketplacePackagePage.tsx index 9ca6d27f540..96adac7c1e1 100644 --- a/ui/packages/app-shell/src/console/marketplace/MarketplacePackagePage.tsx +++ b/ui/packages/app-shell/src/console/marketplace/MarketplacePackagePage.tsx @@ -1464,7 +1464,9 @@ function MarketplacePackagePageBody() { if (error || !data) { return (
- diff --git a/ui/packages/app-shell/src/layout/AppHeader.tsx b/ui/packages/app-shell/src/layout/AppHeader.tsx index 59eaa172201..e945760ddac 100644 --- a/ui/packages/app-shell/src/layout/AppHeader.tsx +++ b/ui/packages/app-shell/src/layout/AppHeader.tsx @@ -988,11 +988,6 @@ export function AppHeader({ */} {!phoneTab && } - {/* AI Assistant — only when the runtime serves AI (hidden on - Community Edition so it can't dead-end on an agent-less chat). - objectstack#34608: it opens and closes the drawer in place. */} - {aiEnabled && } - {/* Help & Documentation — the shared 「?」 menu (objectstack#36111: the page's button group draws the same component). The left-sidebar already links the hub, so this entry point earns @@ -1002,6 +997,13 @@ export function AppHeader({ appPackageId={currentAppPackageId} triggerClassName="h-8 w-8 hidden md:flex shrink-0" /> + + {/* Ask AI — only when the runtime serves AI (hidden on Community + Edition so it can't dead-end on an agent-less chat). + objectstack#34608: it opens and closes the drawer in place. + objectstack#37858: after help, as in the page's button group; + with its words on Home's bar, its icon alone (44 px) on a phone. */} + {aiEnabled && }
{/* Group 3: Account (theme + lang moved into avatar dropdown) */} diff --git a/ui/packages/app-shell/src/layout/AssistantTopBarButton.tsx b/ui/packages/app-shell/src/layout/AssistantTopBarButton.tsx index 3bf4754e868..4407e15eda8 100644 --- a/ui/packages/app-shell/src/layout/AssistantTopBarButton.tsx +++ b/ui/packages/app-shell/src/layout/AssistantTopBarButton.tsx @@ -2,69 +2,177 @@ * ObjectUI * Copyright (c) 2024-present ObjectStack Inc. * - * objectstack#34608 (ui ADR-0057 as amended) — the top-bar robot, the AI - * assistant's ONE standing entry: it opens and closes the right-side drawer - * without leaving the page. The floating bottom-right button it replaces is - * gone; what that button carried moved here: + * objectstack#34608 (ui ADR-0057 as amended) — the AI assistant's ONE standing + * entry: it opens and closes the right-side drawer without leaving the page. + * The floating bottom-right button it replaces is gone; what that button + * carried moved here: * - objectui#11666 — while a proposed plan awaits the user's approval the - * robot carries a marker, so a user who closed the chat still sees the + * entry carries a marker, so a user who closed the chat still sees the * assistant is waiting on them. Read off the assistant bus; only a change * of the plan's state clears it, never opening the chat. * + * objectstack#37858 — the entry is an outlined button that says what it does, + * 「Ask AI」, beside a chat-bubble icon, on every page: the + * page header's button group, the record page's navigation row, the phone's + * top bar, Home's bar, and the corner button of a route with no shell header. + * The words are the platform's — no app renames them — and they are Home's + * 「Ask AI」 string (`home.askAI`): Home's button opens the same entry + * (`useAskAiEntry`), so the one entry has one string in every pack. Two faces: + * - with its text, the text is the accessible name (no `aria-label`); + * - icon only — a phone (a 44 px target), or a header that ran out of room + * and asked for it (`iconOnly`) — `aria-label` carries the same words. + * The tooltip names the ⌘⇧I shortcut on a Mac only: on other platforms + * Ctrl+Shift+I opens the browser's developer tools first, so no shortcut is + * advertised there and `aria-keyshortcuts` is not set. + * * Under a layout with no drawer it is a link to the pre-drawer address, which * the console routes back into the drawer. On an app's AI inquiry page it is * not drawn (ui ADR-0057 Amendment A4). */ import * as React from 'react'; import { Link } from 'react-router-dom'; -import { Bot } from 'lucide-react'; -import { Button, cn } from '@object-ui/components'; +import { MessageCircle } from 'lucide-react'; +import { Button, Kbd, Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, cn } from '@object-ui/components'; import { useObjectTranslation } from '@object-ui/i18n'; import { usePlanApprovalPending } from '../assistant/assistantBus.js'; -import { useChatDockControl } from './chatDockControl.js'; +import { useChatDockControl, useOpenAssistant } from './chatDockControl.js'; +import type { ChatDockTarget } from './chatDockTarget.js'; +import { usePhoneViewport } from '../hooks/usePhoneViewport.js'; + +/** The drawer's shortcut in `aria-keyshortcuts` syntax; advertised on a Mac only. */ +export const ASK_AI_MAC_KEY_SHORTCUTS = 'Meta+Shift+I'; +/** The same shortcut as the tooltip's keycaps. */ +const ASK_AI_MAC_KEYCAPS = ['⌘', '⇧', 'I'] as const; + +/** Whether this browser runs on a Mac, where ⌘⇧I is free for the drawer. */ +function isMacPlatform(): boolean { + if (typeof navigator === 'undefined') return false; + const platform = + (navigator as Navigator & { userAgentData?: { platform?: string } }).userAgentData?.platform ?? + navigator.platform ?? + ''; + return /mac/i.test(platform); +} + +/** + * The Ask AI entry's opening, for a button elsewhere on a page that asks the + * assistant the way the standing entry does — Home's 「Ask AI」. Absent a + * target it opens the drawer on whatever it shows now: the drawer's own + * toggle, which is what the standing button runs (a handoff the drawer still + * holds is kept); with one, on that target. Under no drawer host it goes to + * the pre-drawer address. What opening means changes here and in the + * button's toggle together. + */ +export function useAskAiEntry(): (target?: ChatDockTarget) => void { + const control = useChatDockControl(); + const openAssistant = useOpenAssistant(); + return React.useCallback( + (target?: ChatDockTarget) => { + if (control && target === undefined) { + if (!control.expanded) control.toggle(); + return; + } + openAssistant(target); + }, + [control, openAssistant], + ); +} + +export interface AssistantTopBarButtonProps { + className?: string; + /** + * Draw the icon alone, named by `aria-label`: the header placing the button + * measured that its own content no longer fits beside the words. A phone + * draws the icon alone whatever this says. + */ + iconOnly?: boolean; +} -export function AssistantTopBarButton({ className }: { className?: string }) { +export function AssistantTopBarButton({ className, iconOnly = false }: AssistantTopBarButtonProps) { const { t } = useObjectTranslation(); const control = useChatDockControl(); const planPending = usePlanApprovalPending(); + const phone = usePhoneViewport(); + const mac = React.useMemo(() => isMacPlatform(), []); const markerId = React.useId(); - const label = t('topbar.aiAssistant', { defaultValue: 'AI Assistant' }); + // ui ADR-0057 Amendment A4 — hidden on the app's AI inquiry page, which is + // that page's one ask conversation. After every hook, so the hook order + // holds whichever way the page turns. + if (control?.askPageActive) return null; + const label = t('home.askAI', { defaultValue: 'Ask AI' }); + const iconAlone = iconOnly || phone; + // `aria-hidden` keeps the marker's words out of the name a text face takes + // from its content; the button still reads them as its description, which + // names the marker by id. const marker = planPending ? ( - -