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
10 changes: 10 additions & 0 deletions .changeset/37874-ask-which-reading-length.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@objectstack/spec': patch
---

fix(spec): a legacy `ask_which` reading up to 60 characters reads as a question-card option

Clause-②: yes (widening) — widened: `parseAskQuestionEnvelope` now accepts a legacy `ask_which` result whose readings run up to 60 characters (it refused any reading over the envelope's 24-character label), and `ASK_QUESTION_LIMITS` gains `legacyReadingMaxLength: 60`.
Metadata-protocol: none

Real `ask_which` readings are short phrases, most of them longer than the new envelope's 24-character label, so holding them to that label left most of today's questions as plain text. A legacy reading now has its own limit, 60 characters (the envelope's description length), and becomes an option label with no description. Every other rule still applies to the legacy result: one question of at most 120 characters, two to four readings, no duplicate or blank reading, a non-empty reply. The new `asked` envelope keeps its 24-character label.
17 changes: 17 additions & 0 deletions .changeset/37874-question-card-wiring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@object-ui/plugin-chatbot': patch
'@object-ui/app-shell': patch
---

feat(plugin-chatbot): the question card takes the composer's place when the AI ends its turn on a question

Clause-②: yes (widening) — widened: `ChatbotEnhancedProps` gains the optional `conversationId`; `@object-ui/plugin-chatbot` (and its `/core` entry) exports `QUESTION_SKIP_STORAGE_PREFIX`; `QuestionCardQuestion` and `QuestionCardOption` keep their names and shape and are now the envelope types of `@objectstack/spec/ai` (`AskQuestionParsed`, `AskQuestionOptionParsed`), with `options` still read-only.
Metadata-protocol: none

`ChatbotEnhanced` now draws the question card itself. When a turn ends on a question tool's result that `parseAskQuestionEnvelope` reads, and the envelope's `reply` is the turn's last text, the card replaces the composer:

- It shows only once the stream has ended, and never while a typed message is queued (that message is sent, and sending something else skips the question), while a write approval waits in the same turn, after a skip, or on a `readOnly` transcript. Both shapes of a tool-ended turn count: one streamed message, or the two messages a reloaded conversation holds.
- The composer stays mounted, hidden and `inert`, so a draft and its attachments come back after Skip or an answer, and focus that was in it returns to it.
- One click answers: the option label goes out through `onSendMessage` as the next user message. If the send fails and the message is rolled back, the card returns with its choice still made.
- Skip is remembered on this device, per conversation, under `objectstack:ai-chat-question-skip:` plus the conversation id. The console passes its conversation id, and signing out clears these keys with the rest of the chat cache.
- The conversation draws the question from its envelope instead of its plain-text reply: while pending, the question alone; once answered or skipped, a collapsed "AI asks: …" that expands to its options. Copy still copies the full reply, and a pending question shows no Regenerate.
21 changes: 21 additions & 0 deletions content/docs/frontend/plugins/plugin-chatbot.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,27 @@ export function PendingQuestion({ messageId, onSend, onDismiss }: {
- Strings come from the `chatbot.questionCard.*` keys of `@object-ui/i18n`; the
card is right-to-left in Arabic.

`ChatbotEnhanced` draws the card itself. When a turn ends on a question tool's
result that `parseAskQuestionEnvelope` (`@objectstack/spec/ai`) reads — the
question envelope, or a legacy `ask_which` result — and the envelope's `reply`
is the turn's last text, the card takes the composer's place:

- It shows only once the stream has ended, and never while a typed message is
queued, while a write approval waits in the same turn, after a skip, or on a
`readOnly` transcript.
- The composer is not unmounted: it is hidden and `inert`, so a draft and its
attachments come back after Skip or an answer.
- An answer is sent through `onSendMessage` as the next user message. If the
send fails and the message is rolled back, the card returns with its choice.
- Skip is remembered on this device, per conversation, when the host passes
`conversationId`: the key is `QUESTION_SKIP_STORAGE_PREFIX` followed by
`:` and the conversation id. A host's sign-out cleanup should clear that
prefix.
- The conversation draws the question from its envelope: pending, the question
alone; answered or skipped, a collapsed "AI asks: …" that expands to its
options. Copy still copies the full reply, and a pending question has no
Regenerate.

## Examples

### Simple AI Chat
Expand Down
10 changes: 6 additions & 4 deletions content/docs/references/ai/ask-question.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ Until the question tool emits this envelope, the one producer in the field
is `ask_which`'s existing result,
`{ success: true, status: 'asked_which', question, readings, reply }`.
`parseAskQuestionEnvelope` reads it as one question whose options are
the readings, with no description, and holds the result to the same
`AskQuestionEnvelopeSchema` — one contract and one set of limits, so a
legacy result the card cannot draw (a reading over the label limit, more
than four readings) answers `null` and stays text, as it renders today.
the readings, with no description, under the envelope's limits with one
exception: a reading is a whole sentence-sized phrase, not a short label, so
it is held to its own limit, `ASK_QUESTION_LIMITS.legacyReadingMaxLength`,
and the card wraps it. Every other limit is the envelope's, so a legacy
result the card cannot draw (a reading over its limit, one reading or more
than four, a duplicate) answers `null` and stays text, as it renders today.
Stored conversations keep their old results, so this reading stays as long
as those rows can be shown.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ hand-patch a number here** — fix the code or the verdict and regenerate.

| Dir | Sites |
|---|---|
| `ai/` | 114 |
| `ai/` | 116 |
73 changes: 73 additions & 0 deletions docs/qa/platform-checklist/areas/ai.json
Original file line number Diff line number Diff line change
Expand Up @@ -1808,6 +1808,79 @@
"ref": "#35212"
}
]
},
{
"id": "ai.chat-question-card",
"title": "When the AI ends its turn on a clarifying question, a clickable question card takes the composer's place: one click answers with the next message, Skip gives the composer back with its draft, and the conversation shows the question collapsed",
"since": "v19",
"status": "active",
"revision": 1,
"priority": "P1",
"surface": "browser",
"personas": [
"seeded admin (admin@objectos.ai / admin123), signed in through the form, in the AI chat"
],
"fixtures": {
"app": "showcase",
"requires": [
"an AI service that serves an agent with a model and the ask_which tool, so an ambiguous question ends the turn on a question (service-ai is a Cloud/EE package: ai.open-edition-honest-degradation)",
"an ambiguous question, e.g. 'how much revenue this quarter' where the agent can read more than one revenue figure"
],
"knownGaps": [
"BLOCKED(environment): the open tree registers no AI service, so no turn ends on a question. When the card shows, what an answer sends and the skip mark are pinned (automated.ref)"
]
},
"steps": [
"type a few words in the composer without sending them, then ask an ambiguous question; wait for the turn to end",
"read the composer area and the last assistant message; check that the reply's Regenerate button is absent",
"click Skip; read the composer and its draft; reload the conversation and check the card does not come back",
"ask another ambiguous question and click one option; read the next user message"
],
"acceptance": [
{
"clause": "the card takes the composer's place once the turn ends on a question, and the conversation shows the question without its options",
"oracle": "dom",
"verify": "the question card is shown where the input was, the input is hidden but still in the DOM, and the last assistant message reads 'AI asks: QUESTION' (ui/packages/plugin-chatbot/src/chat/pendingQuestion.ts#findPendingQuestion)",
"evidence": "a screenshot of the composer area and the message"
},
{
"clause": "Skip gives the composer back with the draft typed before the question, and the card stays away after a reload on this device",
"oracle": "dom",
"verify": "the input holds the typed words, the question shows collapsed with its options inside, and localStorage holds objectstack:ai-chat-question-skip:CONVERSATION_ID (ui/packages/plugin-chatbot/src/chat/questionSkip.ts#QUESTION_SKIP_STORAGE_PREFIX)",
"evidence": "the input value, the message DOM and the storage entry"
},
{
"clause": "one click on an option sends its label as the next user message",
"oracle": "network",
"verify": "the chat request after the click carries the option label as the user message",
"evidence": "the request body"
}
],
"negative": [
"a card that shows while the turn still streams, a draft lost after Skip, or an answer sent twice is a FAIL"
],
"blocked": {
"by": "environment",
"ref": "service-ai is Cloud/EE: no open-tree AI service answers the chat (FOLLOW-UPS §18c, ai.console-ai-surface-gating)"
},
"automated": {
"kind": "unit",
"ref": "ui/packages/plugin-chatbot/src/__tests__/pendingQuestion-37874.test.ts, ui/packages/plugin-chatbot/src/__tests__/ChatbotEnhanced.questionCard-37874.test.tsx"
},
"source": [
"ui/packages/plugin-chatbot/src/chat/pendingQuestion.ts#findPendingQuestion",
"ui/packages/plugin-chatbot/src/chat/composerGate.tsx#ComposerGate",
"ui/packages/plugin-chatbot/src/chat/questionSkip.ts#QUESTION_SKIP_STORAGE_PREFIX",
"packages/spec/src/ai/ask-question.zod.ts#parseAskQuestionEnvelope"
],
"history": [
{
"revision": 1,
"date": "2026-11-27",
"change": "initial — the question card wired into the AI chat (question-card topic #37871, Q2b). Blocked(environment): no open-tree AI service; no live run made",
"ref": "#37874"
}
]
}
]
}
25 changes: 24 additions & 1 deletion packages/spec/src/ai/ask-question.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,31 @@ describe('parseAskQuestionEnvelope', () => {
expect(parseAskQuestionEnvelope({ ...LEGACY, readings: ['Only one'] })).toBeNull();
expect(parseAskQuestionEnvelope({ ...LEGACY, readings: ['A', 'B', 'C', 'D', 'E'] })).toBeNull();
expect(parseAskQuestionEnvelope({ ...LEGACY, readings: ['A', 'A'] })).toBeNull();
expect(parseAskQuestionEnvelope({ ...LEGACY, readings: ['x'.repeat(ASK_QUESTION_LIMITS.labelMaxLength + 1), 'B'] }))
expect(parseAskQuestionEnvelope({ ...LEGACY, readings: ['A', ' A '] })).toBeNull();
expect(parseAskQuestionEnvelope({ ...LEGACY, readings: ['A', ' '] })).toBeNull();
expect(parseAskQuestionEnvelope({
...LEGACY,
readings: ['x'.repeat(ASK_QUESTION_LIMITS.legacyReadingMaxLength + 1), 'B'],
})).toBeNull();
expect(parseAskQuestionEnvelope({ ...LEGACY, question: 'q'.repeat(ASK_QUESTION_LIMITS.questionMaxLength + 1) }))
.toBeNull();
expect(parseAskQuestionEnvelope({ ...LEGACY, reply: '' })).toBeNull();
});

it('holds a reading to its own limit, longer than the label limit', () => {
// Real ask_which readings run past the envelope's 24-character label
// (most of them do), so a reading gets the description's length.
expect(ASK_QUESTION_LIMITS.legacyReadingMaxLength).toBeGreaterThan(ASK_QUESTION_LIMITS.labelMaxLength);
const atEdge = 'r'.repeat(ASK_QUESTION_LIMITS.legacyReadingMaxLength);
const overLabel = 'Revenue booked when the deal closed';
expect(overLabel.length).toBeGreaterThan(ASK_QUESTION_LIMITS.labelMaxLength);
const parsed = parseAskQuestionEnvelope({ ...LEGACY, readings: [atEdge, overLabel] });
expect(parsed?.questions[0]?.options).toEqual([{ label: atEdge }, { label: overLabel }]);
});

it('keeps the new envelope at the label limit', () => {
const long = 'x'.repeat(ASK_QUESTION_LIMITS.labelMaxLength + 1);
expect(parseAskQuestionEnvelope(withQuestion({ options: [{ label: long }, { label: 'B' }] }))).toBeNull();
});

it('answers null for a malformed legacy result', () => {
Expand Down
66 changes: 50 additions & 16 deletions packages/spec/src/ai/ask-question.zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ import { strictObject } from '../shared/strict-object';
* is `ask_which`'s existing result,
* `{ success: true, status: 'asked_which', question, readings, reply }`.
* {@link parseAskQuestionEnvelope} reads it as one question whose options are
* the readings, with no description, and holds the result to the same
* {@link AskQuestionEnvelopeSchema} — one contract and one set of limits, so a
* legacy result the card cannot draw (a reading over the label limit, more
* than four readings) answers `null` and stays text, as it renders today.
* the readings, with no description, under the envelope's limits with one
* exception: a reading is a whole sentence-sized phrase, not a short label, so
* it is held to its own limit, `ASK_QUESTION_LIMITS.legacyReadingMaxLength`,
* and the card wraps it. Every other limit is the envelope's, so a legacy
* result the card cannot draw (a reading over its limit, one reading or more
* than four, a duplicate) answers `null` and stays text, as it renders today.
* Stored conversations keep their old results, so this reading stays as long
* as those rows can be shown.
*/
Expand Down Expand Up @@ -70,6 +72,13 @@ export const ASK_QUESTION_LIMITS = {
questionMaxLength: 120,
labelMaxLength: 24,
descriptionMaxLength: 60,
/**
* A legacy `ask_which` reading, which becomes an option label with no
* description. Readings were never held to the label limit, so they get
* the description's length instead; the new envelope's labels stay at
* `labelMaxLength`.
*/
legacyReadingMaxLength: 60,
} as const;

/** A non-empty string with at least one non-space character, at most `max` long. */
Expand Down Expand Up @@ -126,7 +135,18 @@ export const AskQuestionSchema = lazySchema(() => strictObject(
.max(ASK_QUESTION_LIMITS.maxOptions)
.describe('Two to four mutually exclusive options; labels unique, at most one recommended'),
},
).superRefine((question, ctx) => {
).superRefine(refineOptions));

/**
* The rules over a question's options that one option alone cannot hold:
* labels unique (compared trimmed, since a label is the answer text) and at
* most one option recommended. Shared by the envelope's question and the
* legacy reading's, so both refuse the same way.
*/
function refineOptions(
question: { options: ReadonlyArray<{ label: string; recommended?: boolean }> },
ctx: z.RefinementCtx,
) {
const seen = new Set<string>();
question.options.forEach((option, index) => {
const label = option.label.trim();
Expand All @@ -147,7 +167,7 @@ export const AskQuestionSchema = lazySchema(() => strictObject(
message: `${recommended} options are marked recommended: at most one per question`,
});
}
}));
}

/** The ask-question envelope a question tool's result carries. */
export const AskQuestionEnvelopeSchema = lazySchema(() => strictObject(
Expand Down Expand Up @@ -193,14 +213,27 @@ const AskWhichLegacyResultSchema = z.looseObject({
reply: z.string(),
});

/**
* The one question a legacy `ask_which` result converts to: the envelope's
* question rules, with each reading held to `legacyReadingMaxLength` instead
* of the label limit. Not exported, for the same reason as the result above.
*/
const AskWhichLegacyQuestionSchema = z.object({
question: text(ASK_QUESTION_LIMITS.questionMaxLength),
options: z.array(z.object({ label: text(ASK_QUESTION_LIMITS.legacyReadingMaxLength) }))
.min(ASK_QUESTION_LIMITS.minOptions)
.max(ASK_QUESTION_LIMITS.maxOptions),
}).superRefine(refineOptions);

/**
* Read a tool result as an ask-question envelope.
*
* Takes the already-unwrapped result object (peeling the stream's
* `{ type: 'text', value }` wrapper stays with the caller). Answers the parsed
* envelope for a valid {@link AskQuestionEnvelopeSchema} value or a legacy
* `ask_which` result that converts to one, and `null` for anything else —
* the caller then shows the result as the text it already is.
* `ask_which` result that converts to one (its option labels then run to
* `ASK_QUESTION_LIMITS.legacyReadingMaxLength`), and `null` for anything
* else — the caller then shows the result as the text it already is.
*/
export function parseAskQuestionEnvelope(value: unknown): AskQuestionEnvelopeParsed | null {
if (value === null || typeof value !== 'object') return null;
Expand All @@ -211,17 +244,18 @@ export function parseAskQuestionEnvelope(value: unknown): AskQuestionEnvelopePar
}
if (status === ASK_WHICH_LEGACY_STATUS) {
const legacy = AskWhichLegacyResultSchema.safeParse(value);
if (!legacy.success) return null;
const parsed = AskQuestionEnvelopeSchema.safeParse({
if (!legacy.success || legacy.data.reply.length === 0) return null;
const question = AskWhichLegacyQuestionSchema.safeParse({
question: legacy.data.question,
options: legacy.data.readings.map((label) => ({ label })),
});
if (!question.success) return null;
return {
success: true,
status: ASK_QUESTION_STATUS,
reply: legacy.data.reply,
questions: [{
question: legacy.data.question,
options: legacy.data.readings.map((label) => ({ label })),
}],
});
return parsed.success ? parsed.data : null;
questions: [question.data],
};
}
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions ui/packages/app-shell/src/console/ai/ChatPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,8 @@ export function ChatPane({
// area, in flow above the input.
promptHeaderSlot={focusRow ?? undefined}
messages={shownMessages}
// Keys the question card's skip marks to this conversation.
conversationId={conversationId}
placeholder={
activeAgent
? agentRouteName(activeAgent) === 'ask'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,10 @@ describe('useChatConversation — clears chat cache on logout / user switch', ()

it('purgeChatCaches removes only the ai-chat keys', () => {
seedChatCache();
// The question card's skip marks, one key per conversation.
localStorage.setItem('objectstack:ai-chat-question-skip:conv-1', JSON.stringify(['call_1']));
purgeChatCaches();
expect(localStorage.getItem('objectstack:ai-chat-question-skip:conv-1')).toBeNull();
expect(localStorage.getItem(`${CACHE_PREFIX}:u1`)).toBeNull();
expect(localStorage.getItem(`${CACHE_PREFIX}:u1:app:crm:build`)).toBeNull();
expect(localStorage.getItem(`${MESSAGE_PREFIX}:conv-1`)).toBeNull();
Expand Down
Loading
Loading