Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .changeset/37861-focus-follows-page-until-first-send.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
'@objectstack/spec': patch
'@object-ui/plugin-chatbot': patch
'@object-ui/app-shell': patch
'@object-ui/i18n': patch
---

feat(console): a new chat conversation is about the record on the page until its first send, shown as a chip at the start of the composer's line; the pin and the offer to switch records are gone

Clause-②: yes (widening) — `@object-ui/plugin-chatbot` `ChatbotEnhanced` gains the optional `promptInputHeaderSlot`; `@object-ui/app-shell` `UseChatConversationReturn` gains `conversationMetadata` and `createdConversationId`
Metadata-protocol: none
Removed locale keys (`@object-ui/i18n`, every pack): `console.ai.focus.pin`, `console.ai.focus.switchPrompt`, `console.ai.focus.switchAccept`, `console.ai.focus.switchDecline` — shipped only in 19 prereleases.

Framework ADR-0160 D6, seventh amendment ("The focus of a conversation"). Where the agent the chat runs declares `capabilities.focus: true`:

- **A conversation that has sent nothing is about the record on the page** (a record page, or a record open in a list's drawer). It follows the page with no prompt: another record is that record, and a list or a dashboard is no record, so the composer shows the page chip ("Viewing: X") there.
- **The chip sits inside the composer's frame, at the start of the line** (the right in Arabic): the object's icon, the record's name, and ×. × shows while the composer has focus or the pointer is over the chip, always on a touch screen, and has a 24px hit area. A record whose name cannot be read shows `object · id`.
- **× removes the focus and nothing adds it again** in that conversation, before or after the first send; the composer falls back to the page chip.
- **The first send fixes the focus**: that turn and every later one send it as `context.focus`, and navigation no longer changes it. A conversation sent without one never takes one. Only the conversation that send creates takes it: a thread a send made during the resolve joins, and a history row opened while the create is under way, keep their own.
- **A conversation opened again restores its own focus**: from this tab, or, in another tab or from history, from the `metadata.focus` its server row stores. The request body stays the authority.
- **Removed**: the pin on the page chip and the 「改为问《X》?」 offer to ask about another record, with their four locale keys (listed above). `console.ai.focus.pinned` now names the chip for assistive technology, and `.remove` labels ×.

Where the agent does not declare the flag, nothing changes: no chip, no `focus` key, and the page chip stays where it was.

`@object-ui/plugin-chatbot`: `ChatbotEnhanced` gains `promptInputHeaderSlot`, a row drawn inside the input's frame at the start of the line, in place of the `surfaceContextLabel` chip when given. `@object-ui/app-shell`: `useChatConversation` returns `conversationMetadata`, the `metadata` of the row it holds, and `createdConversationId`, the id of the row its own create minted last. `@objectstack/spec`: the `ToolExecutionContext.focus` docblock and the `capabilities.focus` description no longer speak of a pin; no key, type or gate changes.
4 changes: 2 additions & 2 deletions content/docs/references/api/protocol.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const result = AiAgentCapabilitiesSchema.parse(data);
| **canvas** | `boolean` | ✅ | Drives the Live Canvas split view (ADR-0037) |
| **debug** | `boolean` | ✅ | Exposes the build-doctor debug drawer |
| **resume** | `boolean` | ✅ | Turns resume durable multi-step runs (ADR-0013) |
| **focus** | `boolean` | optional | Honours a conversation focus: the chat request's `context.focus`, the record the user pinned the conversation to (`ToolExecutionContext.focus`). A client draws the pin and sends the key only when this is true; absent reads as false. |
| **focus** | `boolean` | optional | Honours a conversation focus: the chat request's `context.focus`, the record the conversation is about (`ToolExecutionContext.focus`). A client draws the focus chip and sends the key only when this is true; absent reads as false. |


---
Expand Down Expand Up @@ -77,7 +77,7 @@ const result = AiAgentCapabilitiesSchema.parse(data);
| **canvas** | `boolean` | ✅ | Drives the Live Canvas split view (ADR-0037) |
| **debug** | `boolean` | ✅ | Exposes the build-doctor debug drawer |
| **resume** | `boolean` | ✅ | Turns resume durable multi-step runs (ADR-0013) |
| **focus** | `boolean` | optional | Honours a conversation focus: the chat request's `context.focus`, the record the user pinned the conversation to (`ToolExecutionContext.focus`). A client draws the pin and sends the key only when this is true; absent reads as false. |
| **focus** | `boolean` | optional | Honours a conversation focus: the chat request's `context.focus`, the record the conversation is about (`ToolExecutionContext.focus`). A client draws the focus chip and sends the key only when this is true; absent reads as false. |


---
Expand Down
4 changes: 2 additions & 2 deletions packages/spec/src/api/protocol.zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4729,8 +4729,8 @@ export const AiAgentCapabilitiesSchema = lazySchema(() => z.object({
debug: z.boolean().describe('Exposes the build-doctor debug drawer'),
resume: z.boolean().describe('Turns resume durable multi-step runs (ADR-0013)'),
focus: z.boolean().optional().describe(
'Honours a conversation focus: the chat request\'s `context.focus`, the record the user pinned the '
+ 'conversation to (`ToolExecutionContext.focus`). A client draws the pin and sends the key only when '
'Honours a conversation focus: the chat request\'s `context.focus`, the record the conversation is '
+ 'about (`ToolExecutionContext.focus`). A client draws the focus chip and sends the key only when '
+ 'this is true; absent reads as false.',
),
}));
Expand Down
12 changes: 7 additions & 5 deletions packages/spec/src/contracts/ai-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1263,12 +1263,14 @@ export interface ToolExecutionContext {
mode?: string;
};
/**
* The record(s) the conversation is pinned to — the conversation's FOCUS,
* The record(s) the conversation is about — the conversation's FOCUS,
* as the client reports it on every turn (`context.focus` in the chat
* request body), beside the page in view ({@link surfaceContext}). Unlike
* the surface, a focus does not follow navigation: the user pinned it, and
* it stays until they remove or replace it, so "this policy" keeps meaning
* the pinned record while they browse elsewhere.
* request body), beside the page in view ({@link surfaceContext}). Until
* the conversation's first send, its focus is the record on the page
* unless the user removed it; the first send fixes it. Unlike the
* surface, a focus then does not follow navigation: it stays until the
* user removes it, so "this policy" keeps meaning the focused record
* while they browse elsewhere.
*
* Client-reported and advisory, exactly like {@link surfaceContext}:
* NEVER an access decision. A consumer that reads the record re-reads it
Expand Down
20 changes: 16 additions & 4 deletions ui/packages/app-shell/src/console/ai/AiChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ export function AiChatPage({ apiBase: apiBaseProp, defaultAgent: defaultAgentPro
initialMessages,
ensureConversation,
createError: conversationCreateError,
conversationMetadata,
createdConversationId,
} = useChatConversation({
// Gate resolution on the agent being known: resolving while `activeAgent`
// is still undefined (catalog loading) would bind a SCOPELESS conversation
Expand Down Expand Up @@ -250,10 +252,20 @@ export function AiChatPage({ apiBase: apiBaseProp, defaultAgent: defaultAgentPro
const [selectedModelId, setSelectedModelId] = useState<string | undefined>(undefined);
// objectstack#36999 — the conversation's focus, held here for the same
// reason: the keyed pane remounts when the first send mints the id, and that
// first turn must still carry the record the user pinned.
const conversationFocus = useConversationFocus(paneConversationId, conversationCreateError);
// A first send creates the conversation: the id it mints takes the focus
// pinned before it (and no other arriving id does).
// first turn must still carry the conversation's record. This page shows no
// record of its own, so a new conversation here has none (ADR-0160 D6,
// seventh amendment); one opened from history restores its own from this
// tab or from its row's metadata (objectstack#37861).
const conversationFocus = useConversationFocus(
paneConversationId,
conversationCreateError,
null,
scopeMatched ? conversationMetadata : undefined,
undefined,
createdConversationId,
);
// A first send creates the conversation: the id its create mints takes the
// focus the conversation had at that send (and no other arriving id does).
const { markSending: markFocusSending } = conversationFocus;
const ensureConversationForSend = useCallback(() => {
// With no user there is nothing to create: no mark, so no id that
Expand Down
168 changes: 71 additions & 97 deletions ui/packages/app-shell/src/console/ai/ChatFocusRow.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* objectstack#36999 — the composer's context row while the agent honours a
* conversation focus (`capabilities.focus` on its catalog row).
* objectstack#36999 / objectstack#37861 — the composer's context chip while
* the agent honours a conversation focus (`capabilities.focus` on its catalog
* row), drawn inside the input's frame at the start of the line.
*
* It takes the place of the page chip the composer draws today
* (`console.ai.discussing`), in the pane's in-flow composer row: the page chip
* with a pin on it when the page shows one record, or — once a record is
* pinned — the focus chip, naming the record by its display name, with a
* remove control. Opening another record while one is pinned asks once
* 「改为问《X》?」; a decline is remembered for the conversation.
* Framework ADR-0160 D6 (seventh amendment): with a focus, the chip names the
* record (the object's icon and the record's name) with × to remove it; ×
* shows while the composer has focus or the pointer is over the chip, and
* always on a touch screen. Without a focus — a list or a dashboard, a
* conversation sent without one, or after × — it is the page chip
* 「正在看:X」 (`console.ai.discussing`), with no ×. There is no pin and no
* offer to switch records: an unsent conversation follows the page on its
* own, and a sent one keeps the record it was sent with.
*
* The record's name is read client-side, as the user: the object definition
* and one `findOne`, through the same seams the record page reads with. A
* and one `findOne`, through the same seams the record page reads with. Until
* that answers, the label the conversation's metadata stored stands in; a
* record the user cannot read keeps its `object · id` pair.
*/

import * as React from 'react';
import { Pin, X } from 'lucide-react';
import { X } from 'lucide-react';
import { ObjectIconTile } from '@object-ui/components';
import { useObjectTranslation } from '@object-ui/i18n';
import { useMetadataItem } from '@object-ui/react';
import { useAdapter } from '../../providers/AdapterProvider.js';
import { getRecordDisplayName } from '../../utils/index.js';
import type { ChatSurfaceContext } from '../../layout/chatSurfaceContext.js';
import { sameChatFocus, type ChatFocus, type ConversationFocus } from '../../layout/chatFocus.js';
import type { ChatFocus, ConversationFocus } from '../../layout/chatFocus.js';

/** The record a surface shows on its own (a record page, a record open in a list's drawer), or null. */
export function surfaceRecord(surface: ChatSurfaceContext | undefined): ChatFocus | null {
const object = surface?.artifact?.type === 'object' ? surface.artifact.name : undefined;
const id = surface?.selection?.kind === 'record' ? surface.selection.id : undefined;
return object && id ? { object, id } : null;
/** What the chip shows of a record: its display name and its object's icon and colour. */
export interface ChatRecordFace {
/** The display name, undefined while it loads and when the record or its definition cannot be read. */
title?: string;
icon?: string;
color?: string;
}

/**
* The display name of `record`, read as the user: undefined while it loads,
* and when the record or its definition cannot be read.
*/
export function useChatRecordTitle(record: ChatFocus | null): string | undefined {
/** The face of `record`, read as the user. */
export function useChatRecordFace(record: ChatFocus | null): ChatRecordFace {
const dataSource = useAdapter() as { findOne?: (object: string, id: string) => unknown } | null;
const { item: objectDef } = useMetadataItem('object', record?.object ?? null);
const object = record?.object;
Expand Down Expand Up @@ -67,103 +69,75 @@ export function useChatRecordTitle(record: ChatFocus | null): string | undefined
};
}, [object, id, canRead, dataSource]);

if (!record || !answer || answer.key !== key || !answer.row || !objectDef) return undefined;
const def = record ? (objectDef as { icon?: unknown; color?: unknown } | undefined) : undefined;
const icon = typeof def?.icon === 'string' ? def.icon : undefined;
const color = typeof def?.color === 'string' ? def.color : undefined;
if (!record || !answer || answer.key !== key || !answer.row || !objectDef) return { icon, color };
const title = getRecordDisplayName(objectDef, answer.row, { fallback: '' });
return title || undefined;
return { title: title || undefined, icon, color };
}

const CHIP =
'inline-flex min-w-0 max-w-full items-center gap-1 rounded-full border bg-muted/40 px-2 py-0.5 text-[11px] text-muted-foreground';
const CHIP_BUTTON =
'inline-flex shrink-0 items-center justify-center rounded-full p-0.5 hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring';
'group/focus-chip inline-flex min-w-0 max-w-full items-center gap-1 rounded-full border bg-muted/40 py-0.5 text-[11px] text-muted-foreground';
/**
* × — a 24px hit area around a 12px glyph (WCAG 2.5.8). Revealed while the
* pointer is over the chip, while anything in the composer has focus (the
* frame is the `group/input-group`), on its own keyboard focus, and always on
* a coarse pointer, where there is no hover.
*/
const REMOVE =
'-my-1 -me-1 inline-flex size-6 shrink-0 items-center justify-center rounded-full opacity-0 transition-opacity hover:bg-muted hover:text-foreground focus-visible:opacity-100 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring group-hover/focus-chip:opacity-100 group-focus-within/input-group:opacity-100 pointer-coarse:opacity-100';

export interface ChatFocusRowProps {
/** The host's focus state for the conversation on screen. */
focus: ConversationFocus;
/** The page in view (`context.surface`). */
surface?: ChatSurfaceContext;
/** The page chip's text, as the composer draws it today; absent → no page chip. */
surfaceLabel?: string;
/** The page chip's tooltip (the internal `type · name` pair). */
surfaceTitle?: string;
}

export function ChatFocusRow({ focus, surface, surfaceLabel, surfaceTitle }: ChatFocusRowProps) {
export function ChatFocusRow({ focus, surfaceLabel, surfaceTitle }: ChatFocusRowProps) {
const { t } = useObjectTranslation();
const pinned = focus.focus;
const pageRecord = surfaceRecord(surface);
const offerSwitch = Boolean(pinned && pageRecord && !sameChatFocus(pinned, pageRecord) && !focus.declined);
const pinnedTitle = useChatRecordTitle(pinned);
const pageTitle = useChatRecordTitle(offerSwitch ? pageRecord : null);
const record = focus.focus;
const face = useChatRecordFace(record);

if (pinned) {
const pair = `${pinned.object} · ${pinned.id}`;
if (record) {
const pair = `${record.object} · ${record.id}`;
const name = face.title || focus.label || pair;
const described = t('console.ai.focus.pinned', { defaultValue: 'Asking about: {{target}}', target: name });
return (
<div className="flex min-w-0 flex-1 flex-col items-start gap-1 px-3" data-testid="chat-focus-row">
<span className={CHIP} title={pair} data-testid="chat-focus-chip">
<Pin className="size-3 shrink-0" aria-hidden="true" />
<span className="truncate">
{t('console.ai.focus.pinned', { defaultValue: 'Asking about: {{target}}', target: pinnedTitle || pair })}
</span>
<button
type="button"
className={CHIP_BUTTON}
onClick={() => focus.setFocus(null)}
aria-label={t('console.ai.focus.remove', { defaultValue: 'Stop asking about this record' })}
title={t('console.ai.focus.remove', { defaultValue: 'Stop asking about this record' })}
data-testid="chat-focus-remove"
>
<X className="size-3" aria-hidden="true" />
</button>
<span
className={`${CHIP} ps-1 pe-2`}
role="group"
aria-label={described}
title={`${described} (${pair})`}
data-testid="chat-focus-chip"
data-focus-object={record.object}
data-focus-id={record.id}
>
<ObjectIconTile icon={face.icon} color={face.color} size="xs" className="shrink-0" />
<span className="truncate text-foreground/90" data-testid="chat-focus-name">
{name}
</span>
{offerSwitch && pageRecord ? (
<div className="flex min-w-0 max-w-full flex-wrap items-center gap-1.5 text-[11px]" role="group" data-testid="chat-focus-switch">
<span className="min-w-0 truncate text-muted-foreground">
{t('console.ai.focus.switchPrompt', {
defaultValue: 'Ask about "{{target}}" instead?',
target: pageTitle || `${pageRecord.object} · ${pageRecord.id}`,
})}
</span>
<button
type="button"
className="rounded px-1.5 py-0.5 font-medium text-foreground hover:bg-muted"
onClick={() => focus.setFocus(pageRecord)}
data-testid="chat-focus-switch-accept"
>
{t('console.ai.focus.switchAccept', { defaultValue: 'Switch' })}
</button>
<button
type="button"
className="rounded px-1.5 py-0.5 text-muted-foreground hover:bg-muted hover:text-foreground"
onClick={focus.decline}
data-testid="chat-focus-switch-decline"
>
{t('console.ai.focus.switchDecline', { defaultValue: 'Keep' })}
</button>
</div>
) : null}
</div>
<button
type="button"
className={REMOVE}
onClick={focus.dismiss}
aria-label={t('console.ai.focus.remove', { defaultValue: 'Stop asking about this record' })}
title={t('console.ai.focus.remove', { defaultValue: 'Stop asking about this record' })}
data-testid="chat-focus-remove"
>
<X className="size-3" aria-hidden="true" />
</button>
</span>
);
}

if (!surfaceLabel) return null;
return (
<div className="flex min-w-0 flex-1 px-3" data-testid="chat-focus-row">
<span className={CHIP} title={surfaceTitle} data-testid="surface-context-chip">
<span className="truncate">{surfaceLabel}</span>
{pageRecord ? (
<button
type="button"
className={CHIP_BUTTON}
onClick={() => focus.setFocus(pageRecord)}
aria-label={t('console.ai.focus.pin', { defaultValue: 'Keep asking about this record' })}
title={t('console.ai.focus.pin', { defaultValue: 'Keep asking about this record' })}
data-testid="chat-focus-pin"
>
<Pin className="size-3" aria-hidden="true" />
</button>
) : null}
</span>
</div>
<span className={`${CHIP} px-2`} title={surfaceTitle} data-testid="surface-context-chip">
<span className="truncate">{surfaceLabel}</span>
</span>
);
}
Loading
Loading