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
30 changes: 24 additions & 6 deletions packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from "path"
import path from "path"
import os from "os"
import z from "zod"
import { SessionID, MessageID, PartID } from "./schema"
Expand Down Expand Up @@ -209,12 +209,30 @@ export namespace SessionPrompt {
const subtasks = firstUser.parts.filter((p): p is MessageV2.SubtaskPart => p.type === "subtask")
const onlySubtasks = subtasks.length > 0 && firstUser.parts.every((p) => p.type === "subtask")

// Truncate the first line of the user's message directly as the title.
// This is instant and costs no tokens. Falls through to LLM-based
// naming only when a dedicated title agent model is explicitly configured.
const ag = yield* agents.get("title")
if (!ag) return
const mdl = ag.model
? yield* provider.getModel(ag.model.providerID, ag.model.modelID)
: ((yield* provider.getSmallModel(input.providerID)) ??
(yield* provider.getModel(input.providerID, input.modelID)))
if (!ag?.model) {
const text = firstUser.parts
.filter((p): p is MessageV2.TextPart => p.type === "text" && !p.synthetic)
.map((p) => p.text)
.join(" ")
.trim()
if (!text) return
const firstLine = text.split("\n")[0].trim()
const t = firstLine.length > 100 ? firstLine.substring(0, 97) + "..." : firstLine
if (!t) return
yield* sessions
.setTitle({ sessionID: input.session.id, title: t })
.pipe(
Effect.catchCause((cause) =>
Effect.sync(() => log.error("failed to set title from prompt", { error: Cause.squash(cause) })),
),
)
return
}
const mdl = yield* provider.getModel(ag.model.providerID, ag.model.modelID)
const msgs = onlySubtasks
? [{ role: "user" as const, content: subtasks.map((p) => p.prompt).join("\n") }]
: yield* MessageV2.toModelMessagesEffect(context, mdl)
Expand Down
108 changes: 108 additions & 0 deletions packages/opencode/test/session/prompt-effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1239,3 +1239,111 @@ unix(
),
30_000,
)

// Title auto-generation semantics

it.live("loop sets session title from first user message when no title model is configured", () =>
provideTmpdirServer(
Effect.fnUntraced(function* ({ llm }) {
const prompt = yield* SessionPrompt.Service
const sessions = yield* Session.Service

const chat = yield* sessions.create({
permission: [{ permission: "*", pattern: "*", action: "allow" }],
})
yield* prompt.prompt({
sessionID: chat.id,
agent: "build",
model: ref,
noReply: true,
parts: [{ type: "text", text: "How do I fix the memory leak in my Unity game?" }],
})
yield* llm.text("here is my answer")
yield* prompt.loop({ sessionID: chat.id })

const updated = yield* sessions.get(chat.id)
expect(updated.title).toBe("How do I fix the memory leak in my Unity game?")
}),
{ git: true, config: providerCfg },
),
)

it.live("loop truncates long first message to 100 chars with ellipsis as title", () =>
provideTmpdirServer(
Effect.fnUntraced(function* ({ llm }) {
const prompt = yield* SessionPrompt.Service
const sessions = yield* Session.Service

const longText = "A".repeat(120)
const chat = yield* sessions.create({
permission: [{ permission: "*", pattern: "*", action: "allow" }],
})
yield* prompt.prompt({
sessionID: chat.id,
agent: "build",
model: ref,
noReply: true,
parts: [{ type: "text", text: longText }],
})
yield* llm.text("ok")
yield* prompt.loop({ sessionID: chat.id })

const updated = yield* sessions.get(chat.id)
expect(updated.title).toBe("A".repeat(97) + "...")
}),
{ git: true, config: providerCfg },
),
)

it.live("loop uses only first line of multiline user message as title", () =>
provideTmpdirServer(
Effect.fnUntraced(function* ({ llm }) {
const prompt = yield* SessionPrompt.Service
const sessions = yield* Session.Service

const chat = yield* sessions.create({
permission: [{ permission: "*", pattern: "*", action: "allow" }],
})
yield* prompt.prompt({
sessionID: chat.id,
agent: "build",
model: ref,
noReply: true,
parts: [{ type: "text", text: "First line of message\nSecond line should be ignored\nThird line too" }],
})
yield* llm.text("ok")
yield* prompt.loop({ sessionID: chat.id })

const updated = yield* sessions.get(chat.id)
expect(updated.title).toBe("First line of message")
}),
{ git: true, config: providerCfg },
),
)

it.live("loop does not overwrite manually set session title", () =>
provideTmpdirServer(
Effect.fnUntraced(function* ({ llm }) {
const prompt = yield* SessionPrompt.Service
const sessions = yield* Session.Service

const chat = yield* sessions.create({
title: "My Custom Title",
permission: [{ permission: "*", pattern: "*", action: "allow" }],
})
yield* prompt.prompt({
sessionID: chat.id,
agent: "build",
model: ref,
noReply: true,
parts: [{ type: "text", text: "This should not override the title" }],
})
yield* llm.text("ok")
yield* prompt.loop({ sessionID: chat.id })

const updated = yield* sessions.get(chat.id)
expect(updated.title).toBe("My Custom Title")
}),
{ git: true, config: providerCfg },
),
)
Loading