Skip to content

Commit 1894b8e

Browse files
committed
feat(claude): show SDK prompt suggestions as ghost text in the composer
The Claude Agent SDK can predict the user's next prompt after each turn (`promptSuggestions: true` emits one `prompt_suggestion` message after `result`, generated off the turn's prompt cache). The adapter received that message but dropped it, and never opted in, so the composer sat empty at the exact moment the user has to decide what to ask next. Opt in and carry the suggestion over the existing activity channel: the adapter maps `prompt_suggestion` to a new `turn.prompt-suggestion` runtime event attributed to the turn that just completed; ingestion stores it as a `prompt-suggestion` activity, hidden from the work log on web and mobile. The web composer derives the suggestion for the latest completed turn and renders it as faded text in the empty editor with a Tab hint. Tab inserts it into the draft (never sends), typing ignores it, and it clears once a newer user message or turn exists. Claude only; the user's own Claude settings (`promptSuggestionEnabled: false`, `CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION`) still suppress it, so there is no separate T3 toggle. Claude Fable 5 in Claude Code. Claude-Session: https://claude.ai/code/session_016YSyurTEFmMXRr9t8vNHTT
1 parent 4c51b4c commit 1894b8e

13 files changed

Lines changed: 533 additions & 3 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ function deriveWorkLogEntries(
337337
if (activity.kind === "task.updated" && !isTerminalBypassUpdate(activity)) continue;
338338
if (activity.kind === "tool.progress") continue;
339339
if (activity.kind === "context-window.updated") continue;
340+
// Composer prompt suggestions have no mobile surface yet.
341+
if (activity.kind === "prompt-suggestion") continue;
340342
if (activity.summary === "Checkpoint captured") continue;
341343
if (isNoContentRuntimeWarning(activity)) continue;
342344
if (isPlanBoundaryToolActivity(activity)) continue;

‎apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,6 +2918,16 @@ describe("ProviderRuntimeIngestion", () => {
29182918
},
29192919
});
29202920

2921+
harness.emit({
2922+
type: "turn.prompt-suggestion",
2923+
eventId: asEventId("evt-turn-prompt-suggestion"),
2924+
provider: ProviderDriverKind.make("codex"),
2925+
createdAt: now,
2926+
threadId: asThreadId("thread-1"),
2927+
turnId: asTurnId("turn-p1"),
2928+
payload: { suggestion: "Run the tests again" },
2929+
});
2930+
29212931
harness.emit({
29222932
type: "turn.diff.updated",
29232933
eventId: asEventId("evt-turn-diff-updated"),
@@ -2944,6 +2954,9 @@ describe("ProviderRuntimeIngestion", () => {
29442954
entry.activities.some(
29452955
(activity: ProviderRuntimeTestActivity) => activity.kind === "runtime.warning",
29462956
) &&
2957+
entry.activities.some(
2958+
(activity: ProviderRuntimeTestActivity) => activity.kind === "prompt-suggestion",
2959+
) &&
29472960
entry.checkpoints.some(
29482961
(checkpoint: ProviderRuntimeTestCheckpoint) => checkpoint.turnId === "turn-p1",
29492962
),
@@ -2961,6 +2974,13 @@ describe("ProviderRuntimeIngestion", () => {
29612974
expect(planActivity?.kind).toBe("turn.plan.updated");
29622975
expect(Array.isArray(planPayload?.plan)).toBe(true);
29632976

2977+
const suggestionActivity = thread.activities.find(
2978+
(activity: ProviderRuntimeTestActivity) => activity.id === "evt-turn-prompt-suggestion",
2979+
);
2980+
expect(suggestionActivity?.kind).toBe("prompt-suggestion");
2981+
expect(suggestionActivity?.turnId).toBe("turn-p1");
2982+
expect(suggestionActivity?.payload).toEqual({ suggestion: "Run the tests again" });
2983+
29642984
const toolUpdate = thread.activities.find(
29652985
(activity: ProviderRuntimeTestActivity) => activity.id === "evt-item-updated",
29662986
);

‎apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,23 @@ export function runtimeEventToActivities(
508508
];
509509
}
510510

511+
case "turn.prompt-suggestion": {
512+
// Predicted next user prompt for the turn that just completed. Clients
513+
// read the newest row for the latest turn; the work log hides it.
514+
return [
515+
{
516+
id: event.eventId,
517+
createdAt: event.createdAt,
518+
tone: "info",
519+
kind: "prompt-suggestion",
520+
summary: "Prompt suggestion",
521+
payload: { suggestion: event.payload.suggestion },
522+
turnId: toTurnId(event.turnId) ?? null,
523+
...maybeSequence,
524+
},
525+
];
526+
}
527+
511528
case "user-input.requested": {
512529
return [
513530
{

‎apps/server/src/provider/Layers/ClaudeAdapter.test.ts‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ describe("ClaudeAdapterLive", () => {
349349
assert.deepEqual(createInput?.options.settingSources, ["user", "project", "local"]);
350350
assert.equal(createInput?.options.permissionMode, "bypassPermissions");
351351
assert.equal(createInput?.options.allowDangerouslySkipPermissions, true);
352+
assert.equal(createInput?.options.promptSuggestions, true);
352353
}).pipe(
353354
Effect.provideService(Random.Random, makeDeterministicRandomService()),
354355
Effect.provide(harness.layer),
@@ -847,6 +848,132 @@ describe("ClaudeAdapterLive", () => {
847848
);
848849
});
849850

851+
it.effect("emits turn.prompt-suggestion for the turn that just completed", () => {
852+
const harness = makeHarness();
853+
return Effect.gen(function* () {
854+
const adapter = yield* ClaudeAdapter;
855+
856+
const runtimeEventsFiber = yield* Stream.takeUntil(
857+
adapter.streamEvents,
858+
(event) => event.type === "turn.prompt-suggestion",
859+
).pipe(Stream.runCollect, Effect.forkChild);
860+
861+
const session = yield* adapter.startSession({
862+
threadId: THREAD_ID,
863+
provider: ProviderDriverKind.make("claudeAgent"),
864+
runtimeMode: "full-access",
865+
});
866+
const turn = yield* adapter.sendTurn({
867+
threadId: session.threadId,
868+
input: "hello",
869+
attachments: [],
870+
});
871+
872+
harness.query.emit({
873+
type: "assistant",
874+
session_id: "sdk-session-1",
875+
uuid: "assistant-1",
876+
parent_tool_use_id: null,
877+
message: {
878+
id: "assistant-message-1",
879+
content: [{ type: "text", text: "Hi" }],
880+
},
881+
} as unknown as SDKMessage);
882+
harness.query.emit({
883+
type: "result",
884+
subtype: "success",
885+
is_error: false,
886+
errors: [],
887+
session_id: "sdk-session-1",
888+
uuid: "result-1",
889+
} as unknown as SDKMessage);
890+
// The SDK delivers the suggestion after `result`, once the turn is closed.
891+
harness.query.emit({
892+
type: "prompt_suggestion",
893+
suggestion: " Now run the tests ",
894+
session_id: "sdk-session-1",
895+
uuid: "ps-1",
896+
} as unknown as SDKMessage);
897+
898+
const runtimeEvents = Array.from(yield* Fiber.join(runtimeEventsFiber));
899+
const completedIndex = runtimeEvents.findIndex((event) => event.type === "turn.completed");
900+
const suggestion = runtimeEvents.at(-1);
901+
assert.notEqual(completedIndex, -1);
902+
assert.equal(suggestion?.type, "turn.prompt-suggestion");
903+
if (suggestion?.type === "turn.prompt-suggestion") {
904+
assert.equal(suggestion.payload.suggestion, "Now run the tests");
905+
assert.equal(String(suggestion.turnId), String(turn.turnId));
906+
}
907+
assert.isBelow(completedIndex, runtimeEvents.length - 1);
908+
}).pipe(
909+
Effect.provideService(Random.Random, makeDeterministicRandomService()),
910+
Effect.provide(harness.layer),
911+
);
912+
});
913+
914+
it.effect(
915+
"keeps a trailing suggestion on its own turn when the next turn already started",
916+
() => {
917+
const harness = makeHarness();
918+
return Effect.gen(function* () {
919+
const adapter = yield* ClaudeAdapter;
920+
921+
const session = yield* adapter.startSession({
922+
threadId: THREAD_ID,
923+
provider: ProviderDriverKind.make("claudeAgent"),
924+
runtimeMode: "full-access",
925+
});
926+
const firstTurn = yield* adapter.sendTurn({
927+
threadId: session.threadId,
928+
input: "hello",
929+
attachments: [],
930+
});
931+
const firstTurnCompleted = yield* Stream.takeUntil(
932+
adapter.streamEvents,
933+
(event) => event.type === "turn.completed",
934+
).pipe(Stream.runDrain, Effect.forkChild);
935+
harness.query.emit({
936+
type: "result",
937+
subtype: "success",
938+
is_error: false,
939+
errors: [],
940+
session_id: "sdk-session-1",
941+
uuid: "result-1",
942+
} as unknown as SDKMessage);
943+
yield* Fiber.join(firstTurnCompleted);
944+
945+
// The user sends again before the SDK delivers the first turn's
946+
// suggestion; it must not be re-homed onto the new turn.
947+
const secondTurn = yield* adapter.sendTurn({
948+
threadId: session.threadId,
949+
input: "and again",
950+
attachments: [],
951+
});
952+
const runtimeEventsFiber = yield* Stream.takeUntil(
953+
adapter.streamEvents,
954+
(event) => event.type === "turn.prompt-suggestion",
955+
).pipe(Stream.runCollect, Effect.forkChild);
956+
harness.query.emit({
957+
type: "prompt_suggestion",
958+
suggestion: "Now run the tests",
959+
session_id: "sdk-session-1",
960+
uuid: "ps-1",
961+
} as unknown as SDKMessage);
962+
963+
const runtimeEvents = Array.from(yield* Fiber.join(runtimeEventsFiber));
964+
const suggestion = runtimeEvents.at(-1);
965+
assert.equal(suggestion?.type, "turn.prompt-suggestion");
966+
if (suggestion?.type === "turn.prompt-suggestion") {
967+
assert.equal(String(suggestion.turnId), String(firstTurn.turnId));
968+
assert.notEqual(String(suggestion.turnId), String(secondTurn.turnId));
969+
}
970+
}).pipe(
971+
Effect.provideService(Random.Random, makeDeterministicRandomService()),
972+
Effect.provide(harness.layer),
973+
);
974+
},
975+
);
976+
850977
it.effect("maps Claude stream/runtime messages to canonical provider runtime events", () => {
851978
const harness = makeHarness();
852979
return Effect.gen(function* () {
@@ -2546,6 +2673,11 @@ describe("ClaudeAdapterLive", () => {
25462673
yield* Effect.yieldNow;
25472674

25482675
const warnings = runtimeEvents.filter((event) => event.type === "runtime.warning");
2676+
// A suggestion with no turn to attach to is dropped, not surfaced.
2677+
assert.equal(
2678+
runtimeEvents.some((event) => event.type === "turn.prompt-suggestion"),
2679+
false,
2680+
);
25492681
// Exactly one warning: the high-priority notification. Nothing else.
25502682
assert.deepEqual(
25512683
warnings.map((event) => event.payload.message),

‎apps/server/src/provider/Layers/ClaudeAdapter.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ interface ClaudeSessionContext {
309309
/** Task ids that have started and not yet reached a terminal state. */
310310
readonly liveTaskIds: Set<string>;
311311
turnState: ClaudeTurnState | undefined;
312+
/**
313+
* Turn id of the most recently completed turn. `prompt_suggestion` arrives
314+
* after `result` (turnState is already cleared), so the suggestion is
315+
* attributed to this turn.
316+
*/
317+
lastCompletedTurnId: TurnId | undefined;
312318
lastKnownContextWindow: number | undefined;
313319
lastKnownTokenUsage: ThreadTokenUsageSnapshot | undefined;
314320
lastKnownTotalProcessedTokens: number | undefined;
@@ -2418,6 +2424,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
24182424
});
24192425

24202426
const updatedAt = yield* nowIso;
2427+
context.lastCompletedTurnId = turnState.turnId;
24212428
context.turnState = undefined;
24222429
context.session = {
24232430
...context.session,
@@ -2429,6 +2436,40 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
24292436
yield* updateResumeCursor(context);
24302437
});
24312438

2439+
const handlePromptSuggestion = Effect.fn("handlePromptSuggestion")(function* (
2440+
context: ClaudeSessionContext,
2441+
message: Extract<SDKMessage, { type: "prompt_suggestion" }>,
2442+
) {
2443+
const suggestion = message.suggestion.trim();
2444+
if (suggestion.length === 0) {
2445+
return;
2446+
}
2447+
// The SDK emits the suggestion after `result`, i.e. after turn.completed
2448+
// cleared turnState. Attribute it to the turn that just finished, never to
2449+
// a turn the user may already have started meanwhile: that turn's own
2450+
// suggestion (if any) follows its own result.
2451+
const turnId = context.lastCompletedTurnId;
2452+
if (!turnId) {
2453+
return;
2454+
}
2455+
const stamp = yield* makeEventStamp();
2456+
yield* offerRuntimeEvent({
2457+
type: "turn.prompt-suggestion",
2458+
eventId: stamp.eventId,
2459+
provider: PROVIDER,
2460+
createdAt: stamp.createdAt,
2461+
threadId: context.session.threadId,
2462+
turnId,
2463+
payload: { suggestion },
2464+
providerRefs: nativeProviderRefs(context),
2465+
raw: {
2466+
source: "claude.sdk.message" as const,
2467+
method: "claude/prompt_suggestion",
2468+
payload: message,
2469+
},
2470+
});
2471+
});
2472+
24322473
const handleStreamEvent = Effect.fn("handleStreamEvent")(function* (
24332474
context: ClaudeSessionContext,
24342475
message: SDKMessage,
@@ -3609,8 +3650,8 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
36093650
case "rate_limit_event":
36103651
yield* handleSdkTelemetryMessage(context, message);
36113652
return;
3612-
// Composer prompt suggestions have no T3 surface; consumed deliberately.
36133653
case "prompt_suggestion":
3654+
yield* handlePromptSuggestion(context, message);
36143655
return;
36153656
default: {
36163657
// Exhaustiveness guard (see handleSystemMessage): new SDK top-level
@@ -4327,6 +4368,11 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
43274368
...(existingResumeSessionId ? { resume: existingResumeSessionId } : {}),
43284369
...(newSessionId ? { sessionId: newSessionId } : {}),
43294370
includePartialMessages: true,
4371+
// Opt in to the SDK's predicted next prompt (one `prompt_suggestion`
4372+
// after each turn's `result`). The user's own Claude settings still
4373+
// apply: `promptSuggestionEnabled: false` in settings.json or
4374+
// CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION=false suppresses them.
4375+
promptSuggestions: true,
43304376
canUseTool,
43314377
onUserDialog,
43324378
supportedDialogKinds: ["resume_return"],
@@ -4429,6 +4475,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
44294475
turnState: undefined,
44304476
lastKnownContextWindow: initialContextWindow,
44314477
lastKnownTokenUsage: undefined,
4478+
lastCompletedTurnId: undefined,
44324479
lastKnownTotalProcessedTokens: undefined,
44334480
lastAssistantUuid: resumeState?.resumeSessionAt,
44344481
lastThreadStartedId: undefined,

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import {
6969
type TerminalContextDraft,
7070
} from "~/lib/terminalContext";
7171
import { cn, isMacPlatform } from "~/lib/utils";
72+
import { Kbd } from "~/components/ui/kbd";
7273
import { basenameOfPath } from "~/pierre-icons";
7374
import {
7475
COMPOSER_INLINE_CHIP_DECORATOR_CLASS_NAME,
@@ -884,6 +885,11 @@ interface ComposerPromptEditorProps {
884885
skills: ReadonlyArray<ServerProviderSkill>;
885886
disabled: boolean;
886887
placeholder: string;
888+
/**
889+
* Provider-predicted next prompt. Rendered as ghost text in place of the
890+
* placeholder while the editor is empty; the parent accepts it on Tab.
891+
*/
892+
promptSuggestion?: string | null;
887893
className?: string;
888894
onRemoveTerminalContext: (contextId: string) => void;
889895
onChange: (
@@ -1533,6 +1539,7 @@ function ComposerPromptEditorInner({
15331539
skills,
15341540
disabled,
15351541
placeholder,
1542+
promptSuggestion,
15361543
className,
15371544
onRemoveTerminalContext,
15381545
onChange,
@@ -1758,13 +1765,25 @@ function ComposerPromptEditorInner({
17581765
className,
17591766
)}
17601767
data-testid="composer-editor"
1761-
aria-placeholder={placeholder}
1768+
aria-placeholder={
1769+
promptSuggestion
1770+
? `Suggested: ${promptSuggestion}. Press Tab to accept.`
1771+
: placeholder
1772+
}
17621773
placeholder={<span />}
17631774
onPaste={onPaste}
17641775
/>
17651776
}
17661777
placeholder={
1767-
terminalContexts.length > 0 ? null : (
1778+
terminalContexts.length > 0 ? null : promptSuggestion ? (
1779+
<div
1780+
className="pointer-events-none absolute inset-0 flex items-baseline gap-2 leading-relaxed text-placeholder"
1781+
data-testid="composer-prompt-suggestion"
1782+
>
1783+
<span className="min-w-0 truncate">{promptSuggestion}</span>
1784+
<Kbd className="shrink-0">Tab</Kbd>
1785+
</div>
1786+
) : (
17681787
<div className="pointer-events-none absolute inset-0 leading-relaxed text-placeholder">
17691788
{placeholder}
17701789
</div>
@@ -1794,6 +1813,7 @@ export function ComposerPromptEditor({
17941813
skills,
17951814
disabled,
17961815
placeholder,
1816+
promptSuggestion,
17971817
className,
17981818
onRemoveTerminalContext,
17991819
onChange,
@@ -1838,6 +1858,7 @@ export function ComposerPromptEditor({
18381858
editorRef={editorRef}
18391859
{...(onCommandKeyDown ? { onCommandKeyDown } : {})}
18401860
{...(className ? { className } : {})}
1861+
{...(promptSuggestion ? { promptSuggestion } : {})}
18411862
/>
18421863
</LexicalComposer>
18431864
);

0 commit comments

Comments
 (0)