Skip to content

Commit e8f5850

Browse files
authored
fix(server): Pi editor dialogs prefill the answer composer (#17206)
1 parent eb1cf7b commit e8f5850

22 files changed

Lines changed: 570 additions & 155 deletions

‎apps/mobile/src/features/threads/QuestionAttachments.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ export function QuestionAttachments(props: {
189189
<TextInputWrapper onPaste={paste}>
190190
<TextInput
191191
value={props.value}
192+
multiline={props.question.initialAnswer !== undefined}
193+
submitBehavior={props.question.initialAnswer !== undefined ? "newline" : undefined}
192194
editable={!props.disabled}
193195
onChangeText={props.onChangeText}
194196
onFocus={() => props.onInputFocusChange?.(true)}

‎apps/mobile/src/lib/threadActivity.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,6 +2011,29 @@ const multiSelectQuestion = {
20112011
} as const;
20122012

20132013
describe("pending user input answers", () => {
2014+
it("preserves exact editor text, including a deliberately cleared answer", () => {
2015+
const question = { ...singleSelectQuestion, initialAnswer: " Proposed message\n" };
2016+
expect(
2017+
buildPendingUserInputAnswers([question], {
2018+
runtime: { customAnswer: question.initialAnswer },
2019+
}),
2020+
).toEqual({ runtime: question.initialAnswer });
2021+
expect(
2022+
buildPendingUserInputAnswers([question], {
2023+
runtime: { customAnswer: " Edited message\n\n" },
2024+
}),
2025+
).toEqual({ runtime: " Edited message\n\n" });
2026+
expect(buildPendingUserInputAnswers([question], { runtime: { customAnswer: "" } })).toEqual({
2027+
runtime: "",
2028+
});
2029+
expect(buildPendingUserInputAnswers([question], { runtime: { customAnswer: " \n" } })).toEqual({
2030+
runtime: " \n",
2031+
});
2032+
expect(setPendingUserInputCustomAnswer(question, { selectedOptionValues: ["Go"] }, "")).toEqual(
2033+
{ customAnswer: "" },
2034+
);
2035+
});
2036+
20142037
it("replaces single-select options and toggles multi-select options", () => {
20152038
expect(
20162039
togglePendingUserInputOptionSelection(

‎apps/mobile/src/lib/threadActivity.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,21 +399,25 @@ function resolvePendingUserInputAnswer(
399399
): string | ReadonlyArray<string> | null {
400400
if (draft?.attachmentsBlocked) return null;
401401
const customAnswer =
402-
question.allowCustomAnswer === false ? null : normalizeDraftAnswer(draft?.customAnswer);
403-
if (customAnswer) {
402+
question.allowCustomAnswer === false
403+
? null
404+
: question.initialAnswer !== undefined
405+
? (draft?.customAnswer ?? null)
406+
: normalizeDraftAnswer(draft?.customAnswer);
407+
if (customAnswer !== null && customAnswer.length > 0) {
404408
return customAnswer;
405409
}
406410

407411
const selectedOptionValues = normalizeSelectedOptionValues(question, draft?.selectedOptionValues);
408412
if (question.multiSelect) {
409413
return selectedOptionValues.length > 0
410414
? selectedOptionValues
411-
: question.allowCustomAnswer !== false && (draft?.attachmentCount ?? 0) > 0
412-
? ""
413-
: null;
415+
: (customAnswer ??
416+
(question.allowCustomAnswer !== false && (draft?.attachmentCount ?? 0) > 0 ? "" : null));
414417
}
415418
return (
416419
selectedOptionValues[0] ??
420+
customAnswer ??
417421
(question.allowCustomAnswer !== false && (draft?.attachmentCount ?? 0) > 0 ? "" : null)
418422
);
419423
}
@@ -1633,7 +1637,7 @@ export function setPendingUserInputCustomAnswer(
16331637
}
16341638

16351639
const selectedOptionValues =
1636-
customAnswer.trim().length > 0
1640+
question.initialAnswer !== undefined || customAnswer.trim().length > 0
16371641
? undefined
16381642
: normalizeSelectedOptionValues(question, draft?.selectedOptionValues);
16391643
return {

‎apps/mobile/src/state/use-selected-thread-requests.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { type ProviderApprovalDecision, type RuntimeRequestId } from "@t3tools/c
1818
import {
1919
type PendingThreadRequests,
2020
type ThreadUserInputQuestion,
21+
seedUserInputDraftAnswers,
2122
} from "@t3tools/client-runtime/state/thread-requests";
2223
import { AuthOrchestrationOperateScope } from "@t3tools/contracts";
2324
import { Atom } from "effect/reactivity";
@@ -104,6 +105,19 @@ export function useSelectedThreadRequests() {
104105
const activePendingApproval = activePendingApprovals[0] ?? null;
105106
const activePendingUserInputs = pendingRequests?.userInputs ?? EMPTY_PENDING_REQUESTS.userInputs;
106107
const activePendingUserInput = activePendingUserInputs[0] ?? null;
108+
useEffect(() => {
109+
if (!activePendingUserInput || !selectedThreadShell) return;
110+
const requestKey = scopedRequestKey(
111+
selectedThreadShell.environmentId,
112+
activePendingUserInput.requestId,
113+
);
114+
const existing = appAtomRegistry.get(userInputDraftsByRequestKeyAtom);
115+
const drafts = existing[requestKey] ?? {};
116+
const seeded = seedUserInputDraftAnswers(activePendingUserInput.questions, drafts);
117+
if (seeded !== drafts) {
118+
appAtomRegistry.set(userInputDraftsByRequestKeyAtom, { ...existing, [requestKey]: seeded });
119+
}
120+
}, [activePendingUserInput, selectedThreadShell]);
107121
const questionServerConfigs = useServerConfigs();
108122
const attachmentDrafts = useAtomValue(composerDraftsAtom);
109123
const preparationCounts = useAtomValue(questionAttachmentPreparationAtom);

‎apps/server/src/mcp/toolkits/thread/tools.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ const question = Schema.Struct({
141141
),
142142
multiSelect: Schema.optional(Schema.Boolean),
143143
allowCustomAnswer: Schema.optional(Schema.Boolean),
144+
initialAnswer: Schema.optional(Schema.String),
144145
required: Schema.optional(Schema.Boolean),
145146
});
146147
const pendingRequest = Schema.Struct({

‎apps/server/src/orchestration-v2/Adapters/PiAdapterV2.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,65 @@ describe("PiAdapterV2", () => {
24002400
}).pipe(Effect.scoped, Effect.provide(layerTest)),
24012401
);
24022402

2403+
it.effect("keeps the complete editor prefill editable and returns exact edited text", () =>
2404+
Effect.gen(function* () {
2405+
const fake = yield* makeFakePi;
2406+
const { runtime, takeEvent } = yield* openRuntime(fake);
2407+
yield* runtime.ensureThread({
2408+
threadId: THREAD_ID,
2409+
modelSelection: modelSelection("default"),
2410+
runtimePolicy,
2411+
});
2412+
const prefill = ` fix(server): retain full editor content\n\n${"detail\n".repeat(500)} `;
2413+
yield* fake.emit({
2414+
type: "extension_ui_request",
2415+
id: "ui-editor",
2416+
method: "editor",
2417+
title: "Commit message",
2418+
prefill,
2419+
});
2420+
const event = yield* takeEvent(
2421+
(event) =>
2422+
event.type === "turn_item.updated" && event.turnItem.type === "user_input_request",
2423+
);
2424+
if (event.type !== "turn_item.updated" || event.turnItem.type !== "user_input_request")
2425+
return;
2426+
assert.equal(event.turnItem.questions[0]?.initialAnswer, prefill);
2427+
assert.deepEqual(event.turnItem.questions[0]?.options, []);
2428+
assert.equal(event.turnItem.questions[0]?.question, "Commit message");
2429+
const edited = " fix(server): edited commit message\n\n";
2430+
yield* runtime.respondToRuntimeRequest({
2431+
requestId: event.turnItem.requestId,
2432+
answers: { "ui-editor": edited },
2433+
});
2434+
const response = yield* fake.takeRequest("extension_ui_response");
2435+
assert.equal(response["value"], edited);
2436+
assert.isUndefined(response["cancelled"]);
2437+
yield* fake.emit({
2438+
type: "extension_ui_request",
2439+
id: "ui-editor-cleared",
2440+
method: "editor",
2441+
title: "Commit message",
2442+
prefill,
2443+
});
2444+
const cleared = yield* takeEvent(
2445+
(event) =>
2446+
event.type === "turn_item.updated" &&
2447+
event.turnItem.type === "user_input_request" &&
2448+
event.turnItem.questions.some((question) => question.id === "ui-editor-cleared"),
2449+
);
2450+
if (cleared.type !== "turn_item.updated" || cleared.turnItem.type !== "user_input_request")
2451+
return;
2452+
yield* runtime.respondToRuntimeRequest({
2453+
requestId: cleared.turnItem.requestId,
2454+
answers: { "ui-editor-cleared": "" },
2455+
});
2456+
const emptyResponse = yield* fake.takeRequest("extension_ui_response");
2457+
assert.equal(emptyResponse["value"], "");
2458+
assert.isUndefined(emptyResponse["cancelled"]);
2459+
}).pipe(Effect.scoped, Effect.provide(layerTest)),
2460+
);
2461+
24032462
it.effect("raises bridge edit confirmations as file-change approvals", () =>
24042463
Effect.gen(function* () {
24052464
const fake = yield* makeFakePi;

‎apps/server/src/orchestration-v2/Adapters/PiAdapterV2.ts‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,25 +3160,21 @@ function piQuestion(
31603160
? event["options"]
31613161
.filter((option): option is string => typeof option === "string")
31623162
.map((option) => ({ label: option || "Empty value", description: option, value: option }))
3163-
: [
3164-
{
3165-
label: "Submit empty value",
3166-
description: "Send an empty string to the extension.",
3167-
value: "",
3168-
},
3169-
];
3170-
// The user-input contract has no prefill field, so an editor dialog's
3171-
// prefill is surfaced inside the question text; without it the user would
3172-
// edit blind against content they cannot see.
3173-
const prefill = method === "editor" ? recordString(event, "prefill") : undefined;
3163+
: method === "editor"
3164+
? []
3165+
: [
3166+
{
3167+
label: "Submit empty value",
3168+
description: "Send an empty string to the extension.",
3169+
value: "",
3170+
},
3171+
];
31743172
const question = recordString(event, "message") ?? recordString(event, "placeholder") ?? title;
31753173
return {
31763174
id: questionId,
31773175
header: title,
3178-
question:
3179-
prefill === undefined || prefill.length === 0
3180-
? question
3181-
: `${question}\n\nCurrent value:\n${prefill.slice(0, 2_000)}`,
3176+
question,
3177+
...(method === "editor" ? { initialAnswer: recordString(event, "prefill") ?? "" } : {}),
31823178
options,
31833179
};
31843180
}

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ import {
217217
togglePendingUserInputOptionSelection,
218218
type PendingUserInputDraftAnswer,
219219
} from "../pendingUserInput";
220+
import { seedUserInputDraftAnswers } from "@t3tools/client-runtime/state/thread-requests";
220221
import { useUiStateStore } from "../uiStateStore";
221222
import { useWorkspaceMutationRefresh } from "../hooks/useWorkspaceMutationRefresh";
222223
import {
@@ -3312,6 +3313,19 @@ export default function ChatView(props: ChatViewProps) {
33123313
activeThreadId,
33133314
activePendingUserInput?.requestId,
33143315
]);
3316+
const activePendingAnswerDrafts =
3317+
pendingUserInputAnswersByRequestId[activePendingRequestKey] ?? EMPTY_PENDING_USER_INPUT_ANSWERS;
3318+
if (
3319+
activePendingUserInput &&
3320+
seedUserInputDraftAnswers(activePendingUserInput.questions, activePendingAnswerDrafts) !==
3321+
activePendingAnswerDrafts
3322+
) {
3323+
setPendingUserInputAnswersByRequestId((existing) => {
3324+
const drafts = existing[activePendingRequestKey] ?? EMPTY_PENDING_USER_INPUT_ANSWERS;
3325+
const seeded = seedUserInputDraftAnswers(activePendingUserInput.questions, drafts);
3326+
return seeded === drafts ? existing : { ...existing, [activePendingRequestKey]: seeded };
3327+
});
3328+
}
33153329
const pendingQuestionDraftKeys = useMemo(
33163330
() =>
33173331
activeThreadId
@@ -10294,6 +10308,7 @@ export default function ChatView(props: ChatViewProps) {
1029410308
[questionId]: setPendingUserInputCustomAnswer(
1029510309
existing[activePendingRequestKey]?.[questionId],
1029610310
value,
10311+
question,
1029710312
),
1029810313
},
1029910314
}));

‎apps/web/src/components/ComposerPromptEditor.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export type {
99

1010
/**
1111
* The composer editor. Tiptap in both modes: the `richTextEnabled` setting
12-
* toggles Markdown styling, never the engine. Plain mode renders every
13-
* marker as a literal character and serializes byte-identically.
12+
* toggles Markdown styling, never the engine. Plain mode keeps Markdown
13+
* markers literal; `literalText` also preserves context-token sources as text.
1414
*/
1515
export function ComposerPromptEditor(props: ComposerPromptEditorProps) {
1616
return <ComposerPromptEditorTiptap {...props} />;

0 commit comments

Comments
 (0)