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
26 changes: 26 additions & 0 deletions packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,25 @@ export namespace SessionPrompt {
continue
}
SessionCompaction.prune({ sessionID })

// Allow plugins to optionally resume this session before it completes
const sessionInfo = await Session.get(sessionID)
const beforeIdleOutput = { resumePrompt: undefined as string | undefined }
await Plugin.trigger("session.before.idle", { sessionID, parentSessionID: sessionInfo?.parentID }, beforeIdleOutput)

if (beforeIdleOutput.resumePrompt) {
log.info("resuming session", { sessionID })
delete state()[sessionID]
const resumeAgent = await lastAgent(sessionID)
const resumeModel = await lastModel(sessionID)
await prompt({
sessionID,
agent: resumeAgent,
model: resumeModel,
parts: [{ type: "text", text: beforeIdleOutput.resumePrompt }],
})
}

for await (const item of MessageV2.stream(sessionID)) {
if (item.info.role === "user") continue
const queued = state()[sessionID]?.callbacks ?? []
Expand All @@ -640,6 +659,13 @@ export namespace SessionPrompt {
return Provider.defaultModel()
}

async function lastAgent(sessionID: string) {
for await (const item of MessageV2.stream(sessionID)) {
if (item.info.role === "user" && item.info.agent) return item.info.agent
}
return Agent.defaultAgent()
}

async function resolveTools(input: {
agent: Agent.Info
model: Provider.Model
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,9 @@ export interface Hooks {
input: { sessionID: string; messageID: string; partID: string },
output: { text: string },
) => Promise<void>

"session.before.idle"?: (
input: { sessionID: string; parentSessionID?: string },
output: { resumePrompt?: string },
) => Promise<void>
}