From db93e901a2e4bd5c72b69956bcf36a0647290f8d Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 13:01:53 +0000 Subject: [PATCH 1/6] feat(app-shell): an opening from the Ask AI entry follows ui ADR-0057 A5 (WIP) Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01L9qibzv3KSUSav5J2uw97s --- .../app-shell/src/assistant/assistantBus.ts | 12 ++ .../src/assistant/writeApprovalPending.ts | 43 ++++ .../app-shell/src/console/ai/ChatPane.tsx | 20 ++ .../src/hooks/useConversationList.ts | 46 +++++ .../src/layout/AssistantTopBarButton.tsx | 20 +- ui/packages/app-shell/src/layout/ChatDock.tsx | 185 +++++++++++++----- .../src/layout/ChatDockConversation.tsx | 41 +++- .../app-shell/src/layout/chatDockControl.tsx | 16 +- .../app-shell/src/layout/chatDockTarget.ts | 22 +++ .../src/layout/useConsoleChatDock.tsx | 169 ++++++++++++++-- .../app-shell/src/views/ObjectDataPage.tsx | 11 +- .../app-shell/src/views/recordDrawerStack.tsx | 14 +- 12 files changed, 511 insertions(+), 88 deletions(-) create mode 100644 ui/packages/app-shell/src/assistant/writeApprovalPending.ts diff --git a/ui/packages/app-shell/src/assistant/assistantBus.ts b/ui/packages/app-shell/src/assistant/assistantBus.ts index 2463421f033..6a4faea3262 100644 --- a/ui/packages/app-shell/src/assistant/assistantBus.ts +++ b/ui/packages/app-shell/src/assistant/assistantBus.ts @@ -249,6 +249,18 @@ export function publishPlanApprovalPending(reading: { for (const l of planApprovalListeners) l(); } +/** + * ui ADR-0057 Amendment A5, row 3 — whether `conversationId` holds a proposal + * awaiting `userId`'s approval, as a mounted chat last read it. Read outside + * render, at the moment the drawer is opened from the entry, so it is a plain + * read, not a hook. A reading taken for another user is no answer. + */ +export function isApprovalPendingIn(conversationId: string | undefined, userId: string | undefined): boolean { + if (!conversationId) return false; + const reading = planApprovalOwners.get(conversationId); + return reading !== undefined && reading.owner === (userId ?? ''); +} + /** * True while any conversation of the signed-in user has a proposed plan * awaiting approval, as last read by a mounted chat. Read by the launchers. diff --git a/ui/packages/app-shell/src/assistant/writeApprovalPending.ts b/ui/packages/app-shell/src/assistant/writeApprovalPending.ts new file mode 100644 index 00000000000..35dc9d6b5f4 --- /dev/null +++ b/ui/packages/app-shell/src/assistant/writeApprovalPending.ts @@ -0,0 +1,43 @@ +/** + * ObjectUI + * Copyright (c) 2024-present ObjectStack Inc. + * + * ui ADR-0057 Amendment A5, row 3 — whether a conversation holds a write the + * AI proposed that still waits for the user's approval: a tool call the + * framework parked as a pending action (`pendingActionId`, state + * `approval-requested`) that the user has not decided in this chat, or whose + * decision failed and so still waits. The chat host publishes the answer to + * the assistant bus (`publishPlanApprovalPending`), where the drawer's host + * reads it when the drawer is opened from the entry. + */ + +interface ToolCallReading { + toolCallId: string; + state?: string; + pendingActionId?: string; +} + +interface MessageReading { + toolInvocations?: readonly ToolCallReading[]; +} + +/** The decision a chat holds for one tool call; only its `state` is read. */ +interface DecisionReading { + state: string; +} + +/** How many proposed writes in `messages` still await the user's approval. */ +export function countWritesAwaitingApproval( + messages: readonly MessageReading[], + decisions: Readonly>, +): number { + let count = 0; + for (const message of messages) { + for (const tool of message.toolInvocations ?? []) { + if (tool.state !== 'approval-requested' || !tool.pendingActionId) continue; + const decision = decisions[tool.toolCallId]; + if (decision === undefined || decision.state === 'error') count += 1; + } + } + return count; +} diff --git a/ui/packages/app-shell/src/console/ai/ChatPane.tsx b/ui/packages/app-shell/src/console/ai/ChatPane.tsx index 932c2cafc65..8168e80b1b3 100644 --- a/ui/packages/app-shell/src/console/ai/ChatPane.tsx +++ b/ui/packages/app-shell/src/console/ai/ChatPane.tsx @@ -11,6 +11,7 @@ import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { useObjectTranslation } from '@object-ui/i18n'; +import { useAuth } from '@object-ui/auth'; import { translateMetadataType } from '../../views/metadata-admin/i18n.js'; import { Select, @@ -47,6 +48,8 @@ import { } from '@object-ui/plugin-chatbot/core'; import { useOpenAssistant } from '../../layout/chatDockControl.js'; +import { publishPlanApprovalPending } from '../../assistant/assistantBus.js'; +import { countWritesAwaitingApproval } from '../../assistant/writeApprovalPending.js'; import type { ChatSurfaceContext } from '../../layout/chatSurfaceContext.js'; import { buildFocusContext, type ConversationFocus } from '../../layout/chatFocus.js'; import { ChatFocusRow } from './ChatFocusRow.js'; @@ -422,6 +425,23 @@ export function ChatPane({ }, }); + // ui ADR-0057 Amendment A5, row 3 — a write the AI proposed that still waits + // for this user's approval is published per conversation to the assistant + // bus, where it outlives this pane: the drawer's host reads it when the + // drawer is next opened from the entry, and reopens this conversation. + const { user } = useAuth(); + const approvalOwner = user?.id; + const writesAwaiting = countWritesAwaitingApproval(runtimeMessages, hitl.decisions); + useEffect(() => { + if (!conversationId) return; + publishPlanApprovalPending({ + userId: approvalOwner, + conversationId, + pending: writesAwaiting > 0, + count: writesAwaiting, + }); + }, [approvalOwner, conversationId, writesAwaiting]); + // The real send, shared by a normal submit and by the deferred-first-message // replay below (resetSuppression → send → onSent, in that order). // diff --git a/ui/packages/app-shell/src/hooks/useConversationList.ts b/ui/packages/app-shell/src/hooks/useConversationList.ts index b1a5786a90d..9959292d94a 100644 --- a/ui/packages/app-shell/src/hooks/useConversationList.ts +++ b/ui/packages/app-shell/src/hooks/useConversationList.ts @@ -280,3 +280,49 @@ export function useConversationList( return { conversations, isLoading, error, refetch, remove, rename }; } + +export interface UseConversationLabelOptions { + apiBase: string; + /** The conversation the drawer shows; absent before the first send creates it. */ + conversationId: string | undefined; + /** Bump to read the label again (a sent turn, the server titling the thread). */ + refreshKey?: number | string; +} + +export interface ConversationLabelReading { + /** The label the history list gives this conversation's row; absent when it has none yet. */ + label: string | undefined; + /** False while the label of the conversation now shown has not been read yet. */ + settled: boolean; +} + +/** + * ui ADR-0057 Amendment A5 — the drawer's header names the conversation it + * shows by the label its history row carries: the title the server gave it, + * else the latest message the user wrote in it (`normalize`, the list's own + * reading of one row). A read that fails settles with no label. + */ +export function useConversationLabel({ + apiBase, + conversationId, + refreshKey, +}: UseConversationLabelOptions): ConversationLabelReading { + const [reading, setReading] = useState<{ id: string; label: string | undefined } | undefined>(undefined); + useEffect(() => { + if (!conversationId) return; + let cancelled = false; + fetchConversationDetail(apiBase, conversationId).then( + (row) => { + if (!cancelled) setReading({ id: conversationId, label: row?.title || row?.preview || undefined }); + }, + () => { + if (!cancelled) setReading({ id: conversationId, label: undefined }); + }, + ); + return () => { + cancelled = true; + }; + }, [apiBase, conversationId, refreshKey]); + if (!conversationId) return { label: undefined, settled: true }; + return reading?.id === conversationId ? { label: reading.label, settled: true } : { label: undefined, settled: false }; +} diff --git a/ui/packages/app-shell/src/layout/AssistantTopBarButton.tsx b/ui/packages/app-shell/src/layout/AssistantTopBarButton.tsx index 4407e15eda8..14c7962f620 100644 --- a/ui/packages/app-shell/src/layout/AssistantTopBarButton.tsx +++ b/ui/packages/app-shell/src/layout/AssistantTopBarButton.tsx @@ -56,22 +56,26 @@ function isMacPlatform(): boolean { /** * 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. + * assistant the way the standing entry does — Home's 「Ask AI」, the record + * pane's. With the drawer closed it is an opening from the entry (ui ADR-0057 + * Amendment A5, the host's `openEntry`): a new conversation after the user's + * own close, else as A5's table says; a target's `agent` is the agent the + * entry asks. With the drawer open, a target opens on it as before and no + * target leaves the drawer as it is. Under no drawer host it goes to the + * pre-drawer address. */ 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(); + if (control && !control.expanded) { + if (control.openEntry) control.openEntry(target?.agent); + else if (target === undefined) control.toggle(); + else control.open(target); return; } + if (control && target === undefined) return; openAssistant(target); }, [control, openAssistant], diff --git a/ui/packages/app-shell/src/layout/ChatDock.tsx b/ui/packages/app-shell/src/layout/ChatDock.tsx index ff0a30c1a8e..b7493ad291e 100644 --- a/ui/packages/app-shell/src/layout/ChatDock.tsx +++ b/ui/packages/app-shell/src/layout/ChatDock.tsx @@ -39,7 +39,7 @@ import { SheetHeader, SheetTitle, } from '@object-ui/components'; -import { History, Maximize2, MessagesSquare, Minimize2, MoreHorizontal, Share2, X } from 'lucide-react'; +import { History, Maximize2, MessagesSquare, Minimize2, MoreHorizontal, Plus, Share2, X } from 'lucide-react'; import { formatNumber, useObjectTranslation } from '@object-ui/i18n'; import { useAgents } from '@object-ui/plugin-chatbot/core'; import { resolveApiBase } from '../console/ai/resolveApiBase.js'; @@ -54,6 +54,7 @@ import { writeStoredDockFullWindow, type ChatDockConversationActions, type ChatDockTarget, + type ShownConversation, } from './chatDockTarget.js'; import { clampDockWidth, @@ -362,12 +363,97 @@ function useDockBodyActions() { return { actions, setActions, shareOpen, setShareOpen }; } +/** + * ui ADR-0057 Amendment A5 — the header's words: the label of the thread in + * view as its history row carries it, "New conversation" before the first + * send, nothing while an existing thread's label is read (or before the body + * reports). A caller's `title` wins; a caller's own body has no thread to + * name, so it reads "New conversation". + */ +function useDockHeaderLabel( + title: string | undefined, + actions: ChatDockConversationActions | undefined, + ownBody: boolean, +): string { + const { t } = useObjectTranslation(); + const fresh = t('console.ai.newConversation', { defaultValue: 'New conversation' }); + if (title !== undefined) return title; + if (ownBody) return fresh; + if (!actions || actions.labelPending) return ''; + return actions.label ?? fresh; +} + +interface DockHeaderButtonsProps { + actions: ChatDockConversationActions | undefined; + /** The rail and the sheet keep the history behind a button; absent in the full window. */ + history?: { open: boolean; onToggle: () => void }; + /** Finger-sized on the phone sheet. */ + size: 'rail' | 'sheet'; + /** Closes the history before a new thread replaces the one in view. */ + onStartNew?: () => void; +} + +/** + * ui ADR-0057 Amendment A5 — the header's + and history buttons, as icons: + * + starts a new conversation on the agent in use, the clock shows the + * history over the chat (pressed while it does) and goes back to the chat. + */ +function DockHeaderButtons({ actions, history, size, onStartNew }: DockHeaderButtonsProps) { + const { t } = useObjectTranslation(); + const box = size === 'sheet' ? 'h-9 w-9' : 'h-7 w-7'; + const icon = size === 'sheet' ? 'h-4 w-4' : 'h-3.5 w-3.5'; + const startNew = actions?.startNew; + const newLabel = t('console.ai.newConversation', { defaultValue: 'New conversation' }); + const historyLabel = history?.open + ? t('console.ai.dock.backToChat', { defaultValue: 'Back to chat' }) + : t('console.ai.dock.history', { defaultValue: 'History' }); + return ( + <> + {startNew ? ( + + ) : null} + {history ? ( + + ) : null} + + ); +} + /** objectstack#34608 — what the drawer shows (see `chatDockTarget.ts`). */ export interface ChatDockThreadProps { /** Absent → the ambient thread (see {@link ChatDockConversation}). */ target?: ChatDockTarget; /** Replaces the target in place: a history row or a minted fresh thread. */ onTargetChange?: (target: ChatDockTarget | undefined) => void; + /** + * ui ADR-0057 Amendment A5 — told the conversation the default body shows, + * so the host can record it at a close. Unused with `children`. + */ + onShownConversationChange?: (shown: ShownConversation) => void; } export interface ChatDockPanelProps extends ChatDockThreadProps { @@ -391,7 +477,7 @@ export interface ChatDockPanelProps extends ChatDockThreadProps { surfaceContext?: ChatSurfaceContext; /** objectui#8219 — the chip's display label for `surfaceContext.artifact`. Unused with `children`. */ surfaceArtifactLabel?: string; - /** Header title override; default "Assistant". */ + /** Header title override; default: the thread's label (ui ADR-0057 Amendment A5). */ title?: string; /** * Body override. Default mounts {@link ChatDockConversation} (the console's @@ -423,6 +509,7 @@ export function ChatDockPanel({ title, target, onTargetChange, + onShownConversationChange, children, }: ChatDockPanelProps) { const { t } = useObjectTranslation(); @@ -431,6 +518,8 @@ export function ChatDockPanel({ const full = dock.fullWindow; const closeLabel = t('console.ai.dock.close', { defaultValue: 'Close' }); const body = useDockBodyActions(); + const ownBody = children !== undefined; + const label = useDockHeaderLabel(title, ownBody ? undefined : body.actions, ownBody); return (