Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ const item: ApprovalRequest = {
test.each([
{ isComposing: true, keyCode: 13 },
{ isComposing: false, keyCode: 229 },
])("keeps IME confirmation in Other without submitting (%j)", (ime) => {
])("keeps IME confirmation in a written answer without submitting (%j)", (ime) => {
const onRespond = mock(() => {});
const view = render(
<QuestionForm form={form} item={item} onRespond={onRespond} />,
);
const input = view.getByRole("textbox", { name: "Other" });
fireEvent.click(view.getByRole("button", { name: "Write my own answer" }));
const input = view.getByRole("textbox", { name: "Write my own answer" });
fireEvent.compositionStart(input);
fireEvent.change(input, { target: { value: "각" } });
fireEvent.keyDown(input, { key: "Enter", ...ime });
Expand All @@ -56,3 +57,77 @@ test.each([
values: { choice_custom: "각" },
});
});

test("changing the suggested choice keeps its note in the submitted answer", () => {
const respond = mock();
const view = render(
<QuestionForm
item={{
id: "question",
kind: "approval_request",
status: "pending",
targetItemId: null,
startedAtMs: 0,
title: "Delivery",
}}
form={{
message: "Delivery",
fields: [
{
id: "delivery",
input: "single",
required: true,
options: [
{ label: "Mail", value: "mail" },
{ label: "Courier", value: "courier" },
],
},
{ id: "delivery_note", input: "text", answerFor: "delivery" },
],
}}
onRespond={respond}
/>,
);
fireEvent.click(view.getByRole("button", { name: /^Mail/ }));
fireEvent.change(view.getByRole("textbox", { name: "Add a note" }), {
target: { value: "Use the side entrance" },
});
fireEvent.click(view.getByRole("button", { name: /^Courier/ }));
fireEvent.click(view.getByRole("button", { name: "Submit" }));
expect(respond).toHaveBeenCalledWith("question", {
type: "form",
values: { delivery: "courier", delivery_note: "Use the side entrance" },
});
});

test("describes the linked text input in both answer modes", () => {
const description = "Include the desk number.";
const view = render(
<QuestionForm
form={{
...form,
fields: [
...form.fields.filter((field) => field.id === "choice"),
{
id: "provider_note",
input: "text",
answerFor: "choice",
description,
},
],
}}
item={item}
onRespond={mock()}
/>,
);
for (const [action, name] of [
["Write my own answer", "Write my own answer"],
["Sample", "Add a note"],
] as const) {
fireEvent.click(view.getByRole("button", { name: new RegExp(action) }));
const input = view.getByRole("textbox", { name });
expect(input.getAttribute("aria-describedby")).toBe(
view.getByText(description).id,
);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Plural, Trans, useLingui } from "@lingui/react/macro";
import {
answerText,
type FormValues,
isInvalidNumber,
isPageAnswered,
isPageComplete,
questionPages,
} from "@superset/chat/core";
import type {
Expand Down Expand Up @@ -131,11 +131,7 @@ export function QuestionForm({
);
}

const blocked = pages.some(
(entry) =>
(entry.field.required && !isPageAnswered(entry, values)) ||
isInvalidNumber(entry.field, values[entry.field.id]),
);
const blocked = pages.some((entry) => !isPageComplete(entry, values));
const submit = () => {
if (blocked) return;
const filled = Object.fromEntries(
Expand All @@ -154,9 +150,20 @@ export function QuestionForm({
};
const next = () => (last ? submit() : goTo(index + 1));

const pick = (value: string) => {
const pick = (value: string | null) => {
if (!page) return;
const { field } = page;
const { field, other } = page;
if (value === null) {
if (!other) return;
setValues((current) => {
if (current[field.id] === undefined && other.id in current)
return current;
const next = { ...current, [other.id]: "" };
delete next[field.id];
return next;
});
return;
}
if (field.input === "multi") {
setValues((current) => {
const picked = Array.isArray(current[field.id])
Expand All @@ -172,8 +179,14 @@ export function QuestionForm({
return;
}
const unpick = values[field.id] === value;
setValues((current) => ({ ...current, [field.id]: unpick ? "" : value }));
if (!unpick && !last) goTo(index + 1);
setValues((current) => {
const next = { ...current, [field.id]: value };
if (unpick) delete next[field.id];
if (other && (unpick || current[field.id] === undefined))
delete next[other.id];
return next;
});
if (!other && !unpick && !last) goTo(index + 1);
};

const choices = page ? choicesFor(page.field) : [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { FormField } from "@superset/chat/protocol";
import { isImeComposing } from "@superset/ui/lib/keyboard";
import { cn } from "@superset/ui/utils";
import { Check, PencilLine } from "lucide-react";
import { useId, useRef } from "react";

type Choice = NonNullable<FormField["options"]>[number];

Expand All @@ -25,7 +26,7 @@ export function QuestionPage({
question: string;
choices: Choice[];
values: FormValues;
onPick: (value: string) => void;
onPick: (value: string | null) => void;
onType: (fieldId: string, text: string) => void;
onEnter: () => void;
}) {
Expand All @@ -40,6 +41,13 @@ export function QuestionPage({
const textField = freeform ? field : other;
const textValue = textField ? values[textField.id] : undefined;
const typed = answerText(textValue);
const inputRef = useRef<HTMLInputElement>(null);
const descriptionId = useId();
const writtenChoice = field.input === "single" && other !== undefined;
const ownAnswer =
writtenChoice && current === undefined && textValue !== undefined;
const note = writtenChoice && current !== undefined;
const description = ownAnswer || note ? other?.description : undefined;
const invalid = isInvalidNumber(field, values[field.id]);

return (
Expand Down Expand Up @@ -101,7 +109,28 @@ export function QuestionPage({
</button>
);
})}
{textField && (
{writtenChoice && (
<button
type="button"
aria-pressed={ownAnswer}
className={cn(
"flex items-center gap-3 rounded-lg border px-3 py-2 text-left text-sm font-medium",
ownAnswer
? "border-foreground/25 bg-foreground/[0.07]"
: "border-transparent hover:bg-foreground/[0.04]",
)}
onClick={() => {
onPick(null);
requestAnimationFrame(() => inputRef.current?.focus());
}}
>
<span className="flex size-[18px] shrink-0 items-center justify-center rounded-full border border-foreground/40">
{ownAnswer && <Check className="size-3" />}
</span>
<Trans>Write my own answer</Trans>
</button>
)}
{textField && (!writtenChoice || ownAnswer || note) && (
<label
className={cn(
"flex items-center gap-3 rounded-lg border px-3 py-2 text-sm transition-colors",
Expand All @@ -112,9 +141,22 @@ export function QuestionPage({
)}
>
<PencilLine className="size-[18px] shrink-0 p-0.5 text-muted-foreground" />
{note && (
<span className="shrink-0 text-muted-foreground">
<Trans>Add a note</Trans>
</span>
)}
<input
ref={inputRef}
aria-describedby={description ? descriptionId : undefined}
aria-invalid={invalid || undefined}
aria-label={textField.description ?? textField.title ?? t`Other`}
aria-label={
note
? t`Add a note`
: ownAnswer
? t`Write my own answer`
: (textField.description ?? textField.title ?? t`Other`)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
className="h-6 min-w-0 flex-1 bg-transparent outline-none placeholder:text-muted-foreground"
inputMode={
field.input === "integer"
Expand All @@ -133,18 +175,25 @@ export function QuestionPage({
? t`Enter a whole number`
: field.input === "number"
? t`Enter a number`
: freeform
? t`Type your answer`
: multi
? t`Something else? Add it here`
: t`Something else? Type your own answer`
: note
? undefined
: freeform || ownAnswer
? t`Type your answer`
: multi
? t`Something else? Add it here`
: t`Something else? Type your own answer`
}
step={field.input === "integer" ? 1 : "any"}
type={numeric ? "number" : "text"}
value={typeof textValue === "string" ? textValue : typed}
/>
</label>
)}
{description && (
<p id={descriptionId} className="px-3 text-muted-foreground text-xs">
{description}
</p>
)}
{invalid && (
<p className="px-3 text-destructive text-xs">
{field.input === "integer" ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Trans, useLingui } from "@lingui/react/macro";
import {
answerText,
type FormValues,
isInvalidNumber,
isPageAnswered,
isPageComplete,
questionPages,
} from "@superset/chat/core";
import type {
Expand Down Expand Up @@ -46,11 +46,7 @@ export function QuestionForm({
void onRespond(approval.id, decision).finally(() => setSending(false));
};

const blocked = pages.some(
(entry) =>
(entry.field.required && !isPageAnswered(entry, values)) ||
isInvalidNumber(entry.field, values[entry.field.id]),
);
const blocked = pages.some((entry) => !isPageComplete(entry, values));
const submit = () => {
if (blocked || sending) return;
const filled = Object.fromEntries(
Expand Down
57 changes: 57 additions & 0 deletions packages/chat-runtime/src/harness/acp/acpAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,63 @@ describe("AcpAdapter", () => {
await adapter.dispose();
});

it("returns a written Codex answer without requiring an unrelated choice", async () => {
const agent = new FakeAcpAgent();
const { adapter, events } = startAdapter(agent);
await flush();
const id = agent.requestElicitation({
message: "Delivery?",
requestedSchema: {
type: "object",
required: ["delivery"],
properties: {
delivery: { type: "string", oneOf: [{ const: "Mail" }] },
delivery_custom: { type: "string" },
delivery_note: {
type: "string",
_meta: { codex: { role: "user_note", questionId: "delivery" } },
},
second_note: {
type: "string",
_meta: { codex: { role: "user_note", questionId: "delivery" } },
},
},
},
});
await flush();
const approval = itemsOf(events).find(
(item) => item.kind === "approval_request",
);
expect(approval).toMatchObject({
form: {
fields: [
{ id: "delivery" },
{ id: "delivery_custom" },
{ id: "delivery_note", answerFor: "delivery" },
{ id: "second_note", answerFor: "delivery" },
],
},
});
for (const field of ["delivery_custom", "second_note"]) {
adapter.respondToApproval(approval?.id ?? "", {
type: "form",
values: { [field]: "Pickup" },
});
await flush();
expect(agent.responseTo(id)).toBeUndefined();
}
adapter.respondToApproval(approval?.id ?? "", {
type: "form",
values: { delivery_note: "Pickup" },
});
await flush();
expect(agent.responseTo(id)?.result).toEqual({
action: "accept",
content: { delivery_note: "Pickup" },
});
await adapter.dispose();
});

it("types a generic form's answers by its schema", async () => {
const agent = new FakeAcpAgent();
const { adapter, events } = startAdapter(agent);
Expand Down
Loading
Loading