Skip to content

Commit 5b11f3b

Browse files
garethpaulclaude
andcommitted
feat(web): suggest your next message in the composer
After the agent replies, the text generation model can suggest the message you are most likely to send next. It appears in the empty composer with a Tab hint, and Tab fills it in for editing or sending. Turn it on in Settings → General → Text generation; it is off by default. The server asks the model once per reply and shares the answer across clients and reloads. Mobile is not wired yet. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d14ace8 commit 5b11f3b

38 files changed

Lines changed: 936 additions & 10 deletions

‎apps/server/src/git/GitManager.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ function createTextGeneration(
360360
Effect.succeed({
361361
title: "Update workflow",
362362
}),
363+
generateNextMessageSuggestion: () => Effect.succeed({ suggestion: "" }),
363364
generateReviewGuide: () => Effect.succeed({ stops: [] }),
364365
...overrides,
365366
};
@@ -409,6 +410,7 @@ function createTextGeneration(
409410
}),
410411
),
411412
),
413+
generateNextMessageSuggestion: (input) => implementation.generateNextMessageSuggestion(input),
412414
generateReviewGuide: (input) => implementation.generateReviewGuide(input),
413415
};
414416
}

‎apps/server/src/observability/RpcInstrumentation.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const RPC_AGGREGATES = {
1818
[ORCHESTRATION_V2_WS_METHODS.getWorkflowScript]: "orchestration",
1919
[ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: "orchestration",
2020
[ORCHESTRATION_V2_WS_METHODS.getTurnItem]: "orchestration",
21+
[ORCHESTRATION_V2_WS_METHODS.suggestNextMessage]: "orchestration",
2122
[ORCHESTRATION_V2_WS_METHODS.getTurnDiff]: "orchestration",
2223
[ORCHESTRATION_V2_WS_METHODS.getFullThreadDiff]: "orchestration",
2324
[ORCHESTRATION_V2_WS_METHODS.searchThreads]: "orchestration",
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
import { assert, describe, it, vi } from "@effect/vitest";
2+
import {
3+
MessageId,
4+
ProjectId,
5+
ProviderInstanceId,
6+
TextGenerationError,
7+
ThreadId,
8+
type OrchestrationV2ConversationMessage,
9+
} from "@t3tools/contracts";
10+
import * as DateTime from "effect/DateTime";
11+
import * as Effect from "effect/Effect";
12+
import * as Layer from "effect/Layer";
13+
import * as Option from "effect/Option";
14+
15+
import * as ServerSettings from "../serverSettings.ts";
16+
import * as TextGeneration from "../textGeneration/TextGeneration.ts";
17+
import * as NextMessageSuggestionService from "./NextMessageSuggestionService.ts";
18+
import * as ProjectStore from "./ProjectStore.ts";
19+
import * as ThreadManagement from "./ThreadManagementService.ts";
20+
21+
const projectId = ProjectId.make("project:next-message");
22+
const threadId = ThreadId.make("thread:next-message");
23+
const environmentModel = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4-mini" };
24+
const projectModel = { instanceId: ProviderInstanceId.make("claudeAgent"), model: "haiku" };
25+
const now = DateTime.makeUnsafe("2026-10-24T00:00:00.000Z");
26+
27+
function message(
28+
role: "user" | "assistant",
29+
text: string,
30+
options: { readonly streaming?: boolean } = {},
31+
): OrchestrationV2ConversationMessage {
32+
return {
33+
id: MessageId.make(`message:${role}:${text}`),
34+
threadId,
35+
runId: null,
36+
nodeId: null,
37+
role,
38+
text,
39+
attachments: [],
40+
streaming: options.streaming ?? false,
41+
createdBy: role === "user" ? "user" : "agent",
42+
creationSource: "web",
43+
createdAt: now,
44+
updatedAt: now,
45+
};
46+
}
47+
48+
const request = message("user", "Fix the build");
49+
const reply = message("assistant", "Fixed it. Should I run the full suite?");
50+
51+
function makeLayer(input: {
52+
readonly settings?: Parameters<typeof ServerSettings.layerTest>[0];
53+
readonly messages: ReadonlyArray<OrchestrationV2ConversationMessage>;
54+
readonly worktreePath?: string | null;
55+
readonly generate?: TextGeneration.TextGeneration["Service"]["generateNextMessageSuggestion"];
56+
}) {
57+
const thread = { messages: input.messages };
58+
const generateNextMessageSuggestion = vi.fn(
59+
input.generate ?? (() => Effect.succeed({ suggestion: "Run the full suite" })),
60+
);
61+
const layer = NextMessageSuggestionService.layer.pipe(
62+
Layer.provide(
63+
Layer.mergeAll(
64+
ServerSettings.layerTest(input.settings),
65+
Layer.mock(ThreadManagement.ThreadManagementService)({
66+
getThreadRecords: (requestedThreadId) =>
67+
requestedThreadId === threadId
68+
? Effect.succeed({
69+
thread: { projectId, worktreePath: input.worktreePath ?? null },
70+
messages: thread.messages,
71+
} as unknown as Effect.Success<
72+
ReturnType<
73+
ThreadManagement.ThreadManagementService["Service"]["getThreadRecords"]
74+
>
75+
>)
76+
: Effect.die(`unexpected thread ${requestedThreadId}`),
77+
}),
78+
Layer.mock(ProjectStore.ProjectStoreV2)({
79+
get: (requestedProjectId) =>
80+
Effect.succeed(
81+
requestedProjectId === projectId
82+
? Option.some({ workspaceRoot: "/repo" } as never)
83+
: Option.none(),
84+
),
85+
}),
86+
Layer.mock(TextGeneration.TextGeneration)({ generateNextMessageSuggestion }),
87+
),
88+
),
89+
);
90+
return { layer, thread, generateNextMessageSuggestion };
91+
}
92+
93+
const suggest = (afterMessageId: MessageId) =>
94+
NextMessageSuggestionService.NextMessageSuggestionService.use((service) =>
95+
service.suggest({ threadId, afterMessageId }),
96+
);
97+
98+
const enabled = {
99+
nextMessageSuggestionsEnabled: true,
100+
textGenerationModelSelection: environmentModel,
101+
};
102+
103+
describe("NextMessageSuggestionService", () => {
104+
it.effect("refuses to run a model while suggestions are off", () => {
105+
const { layer, generateNextMessageSuggestion } = makeLayer({ messages: [request, reply] });
106+
return Effect.gen(function* () {
107+
const error = yield* suggest(reply.id).pipe(Effect.flip);
108+
assert.strictEqual(error.reason, "disabled");
109+
assert.strictEqual(generateNextMessageSuggestion.mock.calls.length, 0);
110+
}).pipe(Effect.provide(layer));
111+
});
112+
113+
it.effect("suggests a reply to the agent from the thread with the project's model", () => {
114+
const { layer, generateNextMessageSuggestion } = makeLayer({
115+
settings: {
116+
...enabled,
117+
projectSettingsOverrides: { [projectId]: { textGenerationModelSelection: projectModel } },
118+
},
119+
worktreePath: "/repo/.worktrees/fix-build",
120+
messages: [request, reply],
121+
});
122+
return Effect.gen(function* () {
123+
assert.deepStrictEqual(yield* suggest(reply.id), { suggestion: "Run the full suite" });
124+
const [generation] = generateNextMessageSuggestion.mock.calls[0]!;
125+
assert.strictEqual(generation.cwd, "/repo/.worktrees/fix-build");
126+
assert.deepStrictEqual(generation.modelSelection, projectModel);
127+
assert.include(generation.threadContext, "USER:\nFix the build");
128+
assert.include(
129+
generation.threadContext,
130+
"ASSISTANT:\nFixed it. Should I run the full suite?",
131+
);
132+
}).pipe(Effect.provide(layer));
133+
});
134+
135+
it.effect("runs in the project root with the environment's model by default", () => {
136+
const { layer, generateNextMessageSuggestion } = makeLayer({
137+
settings: enabled,
138+
messages: [request, reply],
139+
});
140+
return Effect.gen(function* () {
141+
yield* suggest(reply.id);
142+
const [generation] = generateNextMessageSuggestion.mock.calls[0]!;
143+
assert.strictEqual(generation.cwd, "/repo");
144+
assert.strictEqual(generation.modelSelection.instanceId, environmentModel.instanceId);
145+
assert.strictEqual(generation.modelSelection.model, environmentModel.model);
146+
}).pipe(Effect.provide(layer));
147+
});
148+
149+
it.effect("suggests nothing after a reply that is not the thread's latest", () =>
150+
Effect.gen(function* () {
151+
for (const messages of [
152+
[request, reply, message("user", "Also update the docs")],
153+
[request, reply, message("assistant", "Running the suite", { streaming: true })],
154+
[request],
155+
]) {
156+
const { layer, generateNextMessageSuggestion } = makeLayer({ settings: enabled, messages });
157+
const result = yield* suggest(reply.id).pipe(Effect.provide(layer));
158+
assert.deepStrictEqual(result, { suggestion: "" });
159+
assert.strictEqual(generateNextMessageSuggestion.mock.calls.length, 0);
160+
}
161+
}),
162+
);
163+
164+
it.effect("asks once the reply settles when an earlier request found it still streaming", () => {
165+
const { layer, thread, generateNextMessageSuggestion } = makeLayer({
166+
settings: enabled,
167+
messages: [request, { ...reply, streaming: true }],
168+
});
169+
return Effect.gen(function* () {
170+
assert.deepStrictEqual(yield* suggest(reply.id), { suggestion: "" });
171+
thread.messages = [request, reply];
172+
assert.deepStrictEqual(yield* suggest(reply.id), { suggestion: "Run the full suite" });
173+
assert.strictEqual(generateNextMessageSuggestion.mock.calls.length, 1);
174+
}).pipe(Effect.provide(layer));
175+
});
176+
177+
it.effect("asks the model once per reply however many clients request it", () => {
178+
const { layer, thread, generateNextMessageSuggestion } = makeLayer({
179+
settings: enabled,
180+
messages: [request, reply],
181+
});
182+
return Effect.gen(function* () {
183+
yield* Effect.all([suggest(reply.id), suggest(reply.id)], { concurrency: "unbounded" });
184+
yield* suggest(reply.id);
185+
assert.strictEqual(generateNextMessageSuggestion.mock.calls.length, 1);
186+
187+
const nextReply = message("assistant", "The suite passes.");
188+
thread.messages = [...thread.messages, message("user", "Yes"), nextReply];
189+
yield* suggest(nextReply.id);
190+
assert.strictEqual(generateNextMessageSuggestion.mock.calls.length, 2);
191+
}).pipe(Effect.provide(layer));
192+
});
193+
194+
it.effect("reports a provider failure and asks again on the next request", () => {
195+
const { layer, generateNextMessageSuggestion } = makeLayer({
196+
settings: enabled,
197+
messages: [request, reply],
198+
generate: () =>
199+
Effect.fail(
200+
new TextGenerationError({
201+
operation: "generateNextMessageSuggestion",
202+
detail: "Codex exited with code 1.",
203+
}),
204+
),
205+
});
206+
return Effect.gen(function* () {
207+
const error = yield* suggest(reply.id).pipe(Effect.flip);
208+
assert.strictEqual(error.reason, "generation-failed");
209+
assert.strictEqual(error.threadId, threadId);
210+
yield* suggest(reply.id).pipe(Effect.flip);
211+
assert.strictEqual(generateNextMessageSuggestion.mock.calls.length, 2);
212+
}).pipe(Effect.provide(layer));
213+
});
214+
});
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import {
2+
OrchestrationV2SuggestNextMessageError,
3+
type OrchestrationV2SuggestNextMessageInput,
4+
type OrchestrationV2SuggestNextMessageResult,
5+
} from "@t3tools/contracts";
6+
import { formatThreadTitleContext } from "@t3tools/provider-core/server/threadTitleContext";
7+
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
8+
import * as Cache from "effect/Cache";
9+
import * as Context from "effect/Context";
10+
import * as Effect from "effect/Effect";
11+
import * as Exit from "effect/Exit";
12+
import * as Layer from "effect/Layer";
13+
import * as Option from "effect/Option";
14+
15+
import * as ServerSettings from "../serverSettings.ts";
16+
import * as TextGeneration from "../textGeneration/TextGeneration.ts";
17+
import * as ProjectStore from "./ProjectStore.ts";
18+
import * as ThreadManagementService from "./ThreadManagementService.ts";
19+
20+
export class NextMessageSuggestionService extends Context.Service<
21+
NextMessageSuggestionService,
22+
{
23+
readonly suggest: (
24+
input: OrchestrationV2SuggestNextMessageInput,
25+
) => Effect.Effect<
26+
OrchestrationV2SuggestNextMessageResult,
27+
OrchestrationV2SuggestNextMessageError
28+
>;
29+
}
30+
>()("t3/orchestration-v2/NextMessageSuggestionService") {}
31+
32+
const NO_SUGGESTION: OrchestrationV2SuggestNextMessageResult = { suggestion: "" };
33+
34+
const make = Effect.gen(function* () {
35+
const threads = yield* ThreadManagementService.ThreadManagementService;
36+
const projects = yield* ProjectStore.ProjectStoreV2;
37+
const serverSettings = yield* ServerSettings.ServerSettingsService;
38+
const textGeneration = yield* TextGeneration.TextGeneration;
39+
40+
const unavailable = (input: OrchestrationV2SuggestNextMessageInput) => (cause?: unknown) =>
41+
new OrchestrationV2SuggestNextMessageError({
42+
threadId: input.threadId,
43+
reason: "unavailable",
44+
...(cause === undefined ? {} : { cause }),
45+
});
46+
47+
const generate = Effect.fn("NextMessageSuggestionService.generate")(function* (
48+
input: OrchestrationV2SuggestNextMessageInput,
49+
) {
50+
const projection = yield* threads
51+
.getThreadRecords(input.threadId, ["messages"], { messageRoles: ["user", "assistant"] })
52+
.pipe(Effect.mapError(unavailable(input)));
53+
// Once the thread has moved past the reply, or the agent is still writing, wait for the next one.
54+
const latest = projection.messages.at(-1);
55+
if (latest?.id !== input.afterMessageId || latest.role !== "assistant" || latest.streaming) {
56+
return NO_SUGGESTION;
57+
}
58+
59+
const [settings, project] = yield* Effect.all([
60+
serverSettings.getSettings,
61+
projects.get(projection.thread.projectId),
62+
]).pipe(Effect.mapError(unavailable(input)));
63+
if (Option.isNone(project)) return yield* unavailable(input)();
64+
65+
const { settings: projectSettings } = resolveProjectSettings(
66+
settings,
67+
projection.thread.projectId,
68+
);
69+
return yield* textGeneration
70+
.generateNextMessageSuggestion({
71+
cwd: projection.thread.worktreePath ?? project.value.workspaceRoot,
72+
threadContext: formatThreadTitleContext(projection.messages).message,
73+
modelSelection: projectSettings.textGenerationModelSelection,
74+
})
75+
.pipe(
76+
Effect.mapError(
77+
(cause) =>
78+
new OrchestrationV2SuggestNextMessageError({
79+
threadId: input.threadId,
80+
reason: "generation-failed",
81+
cause,
82+
}),
83+
),
84+
);
85+
});
86+
87+
// Keyed by reply, so every client that shows it, including after a reload, shares one model
88+
// call. Only model answers are kept: a failure, or a request that came before the reply
89+
// settled, must not stop a later request from asking.
90+
const suggestions = yield* Cache.makeWith(generate, {
91+
capacity: 256,
92+
timeToLive: (exit) => (Exit.isSuccess(exit) && exit.value !== NO_SUGGESTION ? "1 hour" : 0),
93+
});
94+
95+
const suggest: NextMessageSuggestionService["Service"]["suggest"] = Effect.fn(
96+
"NextMessageSuggestionService.suggest",
97+
)(function* (input) {
98+
const settings = yield* serverSettings.getSettings.pipe(Effect.mapError(unavailable(input)));
99+
if (!settings.nextMessageSuggestionsEnabled) {
100+
return yield* new OrchestrationV2SuggestNextMessageError({
101+
threadId: input.threadId,
102+
reason: "disabled",
103+
});
104+
}
105+
return yield* Cache.get(suggestions, {
106+
threadId: input.threadId,
107+
afterMessageId: input.afterMessageId,
108+
});
109+
});
110+
111+
return NextMessageSuggestionService.of({ suggest });
112+
});
113+
114+
export const layer = Layer.effect(NextMessageSuggestionService, make);

‎apps/server/src/provider/Drivers/CodexManagedProvider.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ export const makeManagedCodexProvider = Effect.fn("makeManagedCodexProvider")(fu
283283
protect("generateBranchName", nativeGeneration.generateBranchName(value)),
284284
generateThreadTitle: (value) =>
285285
protect("generateThreadTitle", nativeGeneration.generateThreadTitle(value)),
286+
generateNextMessageSuggestion: (value) =>
287+
protect(
288+
"generateNextMessageSuggestion",
289+
nativeGeneration.generateNextMessageSuggestion(value),
290+
),
286291
generateReviewGuide: (value) =>
287292
protect("generateReviewGuide", nativeGeneration.generateReviewGuide(value)),
288293
};

‎apps/server/src/server.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import * as SqlitePersistence from "./persistence/Sqlite.ts";
4444
import * as PullRequestFilesViewed from "./persistence/PullRequestFilesViewed.ts";
4545
import * as PullRequestReviewGuides from "./persistence/PullRequestReviewGuides.ts";
4646
import * as PullRequestReviewGuideService from "./pullRequest/PullRequestReviewGuideService.ts";
47+
import * as NextMessageSuggestionService from "./orchestration-v2/NextMessageSuggestionService.ts";
4748
import * as ServerLifecycleEvents from "./serverLifecycleEvents.ts";
4849
import * as AnalyticsService from "./telemetry/AnalyticsService.ts";
4950
import * as DelegatedTaskAnalytics from "./telemetry/DelegatedTaskAnalytics.ts";
@@ -719,6 +720,7 @@ const layerMakeRoutes = Layer.mergeAll(
719720
Layer.provide(
720721
PullRequestReviewGuideService.layer.pipe(Layer.provide(PullRequestReviewGuides.layer)),
721722
),
723+
Layer.provide(NextMessageSuggestionService.layer),
722724
// Both transports consume the same service instance, so caches single-flight across clients
723725
// and mutations observed on WebSocket invalidate patches subsequently read over HTTP.
724726
Layer.provide(layerPullRequestService),

0 commit comments

Comments
 (0)