Skip to content

Commit 5710064

Browse files
committed
feat(core): generate session titles from first prompt
Add SessionTitle service that generates a session title from the session's sole user message via the customizable "title" agent, then renames the session. Runs once per session, gated on durable history having exactly one user message (no default-title string comparison). Wire it into SessionRunner as a background fork after the first turn's prompt promotion makes the user message visible; forking before promotion caused the first-message lookup to see zero rows and silently no-op. Closes #34364
1 parent 4617b03 commit 5710064

7 files changed

Lines changed: 356 additions & 0 deletions

File tree

‎packages/core/src/location-services.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { ReferenceGuidance } from "./reference/guidance"
3131
import * as SessionRunnerLLM from "./session/runner/llm"
3232
import { SessionRunnerModel } from "./session/runner/model"
3333
import { SessionCompaction } from "./session/compaction"
34+
import { SessionTitle } from "./session/title"
3435
import { SessionTodo } from "./session/todo"
3536
import { SkillV2 } from "./skill"
3637
import { SkillGuidance } from "./skill/guidance"
@@ -86,6 +87,7 @@ export const locationServices = LayerNode.group([
8687
McpTool.node,
8788
SessionRunnerModel.node,
8889
SessionCompaction.node,
90+
SessionTitle.node,
8991
Snapshot.node,
9092
SessionRunnerLLM.node,
9193
])

‎packages/core/src/session/history.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,22 @@ export const entriesForRunner = Effect.fn("SessionHistory.entriesForRunner")(fun
9898
)
9999
})
100100

101+
/** Returns the session's sole user message, or `undefined` once a second one exists. */
102+
export const firstUserMessageIfOnly = Effect.fn("SessionHistory.firstUserMessageIfOnly")(function* (
103+
db: DatabaseService,
104+
sessionID: SessionSchema.ID,
105+
) {
106+
const rows = yield* db
107+
.select()
108+
.from(SessionMessageTable)
109+
.where(and(eq(SessionMessageTable.session_id, sessionID), eq(SessionMessageTable.type, "user")))
110+
.orderBy(asc(SessionMessageTable.seq))
111+
.limit(2)
112+
.all()
113+
.pipe(Effect.orDie)
114+
if (rows.length !== 1) return undefined
115+
const message = yield* decodeMessageRow(rows[0]).pipe(Effect.catch(() => Effect.succeed(undefined)))
116+
return message?.type === "user" ? message : undefined
117+
})
118+
101119
export * as SessionHistory from "./history"

‎packages/core/src/session/runner/llm.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { SessionHistory } from "../history"
3030
import { SessionInput } from "../input"
3131
import { SessionSchema } from "../schema"
3232
import { SessionStore } from "../store"
33+
import { SessionTitle } from "../title"
3334
import { type RunError, Service } from "./index"
3435
import { SessionRunnerModel } from "./model"
3536
import { createLLMEventPublisher } from "./publish-llm-event"
@@ -107,6 +108,12 @@ export const layer = Layer.effect(
107108
const snapshots = yield* Snapshot.Service
108109
const db = (yield* Database.Service).db
109110
const compaction = yield* SessionCompaction.Service
111+
const title = yield* SessionTitle.Service
112+
// Title generation is a side effect of the first turn; it must not delay turn continuation.
113+
// Tracked per process so repeated wakes before the second user message arrives don't
114+
// re-fire a redundant LLM call; `SessionTitle` itself is idempotent based on durable history.
115+
const titleAttempted = new Set<SessionSchema.ID>()
116+
const forkTitle = yield* FiberSet.makeRuntime<never, void, never>()
110117
const getSession = Effect.fn("SessionRunner.getSession")(function* (sessionID: SessionSchema.ID) {
111118
const session = yield* store.get(sessionID)
112119
if (!session) return yield* Effect.die(`Session not found: ${sessionID}`)
@@ -394,6 +401,12 @@ export const layer = Layer.effect(
394401
let step = 1
395402
while (needsContinuation) {
396403
const result = yield* runTurn(input.sessionID, promotion, step)
404+
// Steer/queue promotion inside runTurn has already made the pending input a visible
405+
// user message by this point, so the first-user-message check below is reliable.
406+
if (!titleAttempted.has(input.sessionID)) {
407+
titleAttempted.add(input.sessionID)
408+
forkTitle(title.generateForFirstPrompt(yield* getSession(input.sessionID)).pipe(Effect.ignore))
409+
}
397410
needsContinuation = result.needsContinuation
398411
step = result.step + 1
399412
promotion = "steer"
@@ -428,6 +441,7 @@ export const node = makeLocationNode({
428441
ReferenceGuidance.node,
429442
McpGuidance.node,
430443
SessionCompaction.node,
444+
SessionTitle.node,
431445
Snapshot.node,
432446
Database.node,
433447
],

‎packages/core/src/session/title.ts‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
export * as SessionTitle from "./title"
2+
3+
import { LLM, LLMClient, LLMError, LLMEvent, Message, type LLMRequest } from "@opencode-ai/llm"
4+
import { Context, DateTime, Effect, Layer, Stream } from "effect"
5+
import { AgentV2 } from "../agent"
6+
import { Database } from "../database/database"
7+
import { EventV2 } from "../event"
8+
import { makeLocationNode } from "../effect/app-node"
9+
import { llmClient } from "../effect/app-node-platform"
10+
import { SessionEvent } from "./event"
11+
import { SessionHistory } from "./history"
12+
import { SessionRunnerModel } from "./runner/model"
13+
import { SessionSchema } from "./schema"
14+
15+
const MAX_LENGTH = 100
16+
17+
type Dependencies = {
18+
readonly events: EventV2.Interface
19+
readonly llm: {
20+
readonly stream: (request: LLMRequest) => Stream.Stream<LLMEvent, LLMError>
21+
}
22+
readonly agents: AgentV2.Interface
23+
readonly models: SessionRunnerModel.Interface
24+
}
25+
26+
export interface Interface {
27+
/** Generates a title from the session's first user message and renames the session. Runs at most once per session. */
28+
readonly generateForFirstPrompt: (session: SessionSchema.Info) => Effect.Effect<void>
29+
}
30+
31+
export class Service extends Context.Service<Service, Interface>()("@opencode/v2/SessionTitle") {}
32+
33+
const truncate = (value: string) => (value.length <= MAX_LENGTH ? value : `${value.slice(0, MAX_LENGTH - 3)}...`)
34+
35+
const make = (dependencies: Dependencies) => {
36+
const generateForFirstPrompt = Effect.fn("SessionTitle.generateForFirstPrompt")(function* (
37+
db: Database.Interface["db"],
38+
session: SessionSchema.Info,
39+
) {
40+
if (session.parentID) return
41+
const firstUser = yield* SessionHistory.firstUserMessageIfOnly(db, session.id)
42+
if (!firstUser) return
43+
const agent = yield* dependencies.agents.get(AgentV2.ID.make("title"))
44+
if (!agent) return
45+
const model = yield* (agent.model
46+
? dependencies.models.resolve({ ...session, model: agent.model })
47+
: dependencies.models.resolve(session)
48+
).pipe(Effect.catch(() => Effect.succeed(undefined)))
49+
if (!model) return
50+
const chunks: string[] = []
51+
let failed = false
52+
const streamed = yield* dependencies.llm
53+
.stream(
54+
LLM.request({
55+
model,
56+
system: agent.system,
57+
messages: [Message.user(firstUser.text)],
58+
tools: [],
59+
}),
60+
)
61+
.pipe(
62+
Stream.runForEach((event) => {
63+
if (LLMEvent.is.providerError(event)) failed = true
64+
if (LLMEvent.is.textDelta(event)) chunks.push(event.text)
65+
return Effect.void
66+
}),
67+
Effect.as(true),
68+
Effect.catchTag("LLM.Error", () => Effect.succeed(false)),
69+
)
70+
if (!streamed || failed) return
71+
const title = chunks
72+
.join("")
73+
.split("\n")
74+
.map((line) => line.trim())
75+
.find((line) => line.length > 0)
76+
if (!title) return
77+
yield* dependencies.events.publish(SessionEvent.Renamed, {
78+
sessionID: session.id,
79+
timestamp: yield* DateTime.now,
80+
title: truncate(title),
81+
})
82+
})
83+
return { generateForFirstPrompt }
84+
}
85+
86+
export const layer = Layer.effect(
87+
Service,
88+
Effect.gen(function* () {
89+
const events = yield* EventV2.Service
90+
const llm = yield* LLMClient.Service
91+
const agents = yield* AgentV2.Service
92+
const models = yield* SessionRunnerModel.Service
93+
const database = yield* Database.Service
94+
const title = make({ events, llm, agents, models })
95+
return Service.of({
96+
generateForFirstPrompt: (session) => title.generateForFirstPrompt(database.db, session),
97+
})
98+
}),
99+
)
100+
101+
export const node = makeLocationNode({
102+
service: Service,
103+
layer,
104+
deps: [EventV2.node, llmClient, AgentV2.node, SessionRunnerModel.node, Database.node],
105+
})

‎packages/core/test/session-runner-recorded.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { SessionV2 } from "@opencode-ai/core/session"
1515
import { locationServiceMapLayer } from "@opencode-ai/core/location-services"
1616
import { Snapshot } from "@opencode-ai/core/snapshot"
1717
import { SessionCompaction } from "@opencode-ai/core/session/compaction"
18+
import { SessionTitle } from "@opencode-ai/core/session/title"
1819
import { Prompt } from "@opencode-ai/core/session/prompt"
1920
import { SessionProjector } from "@opencode-ai/core/session/projector"
2021
import { SessionExecution } from "@opencode-ai/core/session/execution"
@@ -77,6 +78,7 @@ const mcpGuidance = Layer.mock(McpGuidance.Service, { load: () => Effect.succeed
7778
const config = Layer.succeed(Config.Service, Config.Service.of({ entries: () => Effect.succeed([]) }))
7879
const runner = SessionRunnerLLM.defaultLayer.pipe(
7980
Layer.provide(SessionCompaction.layer),
81+
Layer.provide(SessionTitle.layer),
8082
Layer.provide(Snapshot.noopLayer),
8183
Layer.provide(Database.defaultLayer),
8284
Layer.provide(SessionStore.defaultLayer),

‎packages/core/test/session-runner.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { Snapshot } from "@opencode-ai/core/snapshot"
2424
import { ContextSnapshotDecodeError } from "@opencode-ai/core/session/error"
2525
import { SessionEvent } from "@opencode-ai/core/session/event"
2626
import { SessionCompaction } from "@opencode-ai/core/session/compaction"
27+
import { SessionTitle } from "@opencode-ai/core/session/title"
2728
import { SessionInput } from "@opencode-ai/core/session/input"
2829
import { SessionMessage } from "@opencode-ai/core/session/message"
2930
import { Prompt } from "@opencode-ai/core/session/prompt"
@@ -232,6 +233,7 @@ const config = Layer.succeed(
232233
)
233234
const runner = SessionRunnerLLM.layer.pipe(
234235
Layer.provide(SessionCompaction.layer),
236+
Layer.provide(SessionTitle.layer),
235237
Layer.provide(Snapshot.noopLayer),
236238
Layer.provide(Database.defaultLayer),
237239
Layer.provide(SessionStore.defaultLayer),

0 commit comments

Comments
 (0)