Skip to content
Open
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
92 changes: 75 additions & 17 deletions packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,19 @@ const layer = Layer.effect(

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)))
const mdlOpt = yield* Effect.fnUntraced(function* () {
if (ag.model) return yield* provider.getModel(ag.model.providerID, ag.model.modelID)
return (yield* provider.getSmallModel(input.providerID)) ??
(yield* provider.getModel(input.providerID, input.modelID))
})().pipe(
Effect.tapError((err) => Effect.logWarning("title model resolution failed", { error: err })),
Effect.option,
)
if (Option.isNone(mdlOpt)) {
yield* Effect.logWarning("title skipped: no model available")
return
}
const mdl = mdlOpt.value
const msgs = onlySubtasks
? [{ role: "user" as const, content: subtasks.map((p) => p.prompt).join("\n") }]
: yield* MessageV2.toModelMessagesEffect(context, mdl)
Expand All @@ -227,7 +236,6 @@ const layer = Layer.effect(
agent: ag,
user: firstInfo,
system: [],
small: true,
tools: {},
model: mdl,
sessionID: input.session.id,
Expand All @@ -238,18 +246,61 @@ const layer = Layer.effect(
Stream.filter(LLMEvent.is.textDelta),
Stream.map((e) => e.text),
Stream.mkString,
Effect.orDie,
Effect.catchAll((err) =>
Effect.logWarning("title generation LLM call failed", { error: err }).pipe(Effect.as(""))
),
)
const cleaned = text
.replace(/<think>[\s\S]*?<\/think>\s*/g, "")
.split("\n")
.map((line) => line.trim())
.find((line) => line.length > 0)
if (!cleaned) return
const t = cleaned.length > 100 ? cleaned.substring(0, 97) + "..." : cleaned
yield* sessions
.setTitle({ sessionID: input.session.id, title: t })
.pipe(Effect.catchCause((cause) => Effect.logError("failed to generate title", { error: Cause.squash(cause) })))
const extractTitle = (raw: string) => {
const t = raw
.replace(/<think>[\s\S]*?<\/think>\s*/g, "")
.split("\n")
.map((line) => line.trim())
.find((line) => line.length > 0)
if (!t) return
return t.length > 100 ? t.substring(0, 97) + "..." : t
}
const setTitle = (title: string) =>
sessions
.setTitle({ sessionID: input.session.id, title })
.pipe(Effect.catchCause((cause) => Effect.logError("failed to persist title", { error: Cause.squash(cause) })))
const first = extractTitle(text)
if (first) {
yield* setTitle(first)
return
}
yield* Effect.logInfo("title generation deferred, will retry after delay")
yield* Effect.sleep("10 seconds")
const fresh = yield* sessions.get(input.session.id).pipe(Effect.option)
if (Option.isNone(fresh) || !Session.isDefaultTitle(fresh.value.title)) return
const retryMsgs = yield* sessions.messages({ sessionID: input.session.id }).pipe(
Effect.catchAll((err) =>
Effect.logWarning("title retry: failed to fetch messages", { error: err }).pipe(Effect.as([] as SessionV1.WithParts[]))
),
)
if (retryMsgs.length === 0) return
const retryModelMsgs = yield* MessageV2.toModelMessagesEffect(retryMsgs, mdl)
const retryText = yield* llm
.stream({
agent: ag,
user: firstInfo,
system: [],
tools: {},
model: mdl,
sessionID: input.session.id,
retries: 2,
messages: [{ role: "user", content: "Generate a title for this conversation:\n" }, ...retryModelMsgs],
})
.pipe(
Stream.filter(LLMEvent.is.textDelta),
Stream.map((e) => e.text),
Stream.mkString,
Effect.catchAll((err) =>
Effect.logWarning("title generation retry LLM call failed", { error: err }).pipe(Effect.as(""))
),
)
const second = extractTitle(retryText)
if (!second) return
yield* setTitle(second)
})

const handleSubtask = Effect.fn("SessionPrompt.handleSubtask")(function* (input: {
Expand Down Expand Up @@ -1136,7 +1187,14 @@ const layer = Layer.effect(
modelID: lastUser.model.modelID,
providerID: lastUser.model.providerID,
history: msgs,
}).pipe(Effect.ignore, Effect.forkIn(scope))
}).pipe(
Effect.catchCause((cause) =>
Effect.logWarning("title generation background task failed", {
error: Cause.squash(cause),
}),
),
Effect.forkIn(scope),
)

const model = yield* getModel(lastUser.model.providerID, lastUser.model.modelID, sessionID)
const task = tasks.pop()
Expand Down
Loading