Skip to content

Commit f36a9ea

Browse files
committed
feat: Add /continue command for interrupted conversations
- Remove /continue alias from /session command in autocomplete - Add new /continue command to continue interrupted conversations - Add server endpoint /session/:id/continue with logic to detect unfinished assistant messages - Add TUI command handler for session.continue with proper error handling - Regenerate JS SDK to include continue method - Add basic tests for continue functionality Resolves anomalyco#5150
1 parent 8e816a9 commit f36a9ea

7 files changed

Lines changed: 222 additions & 6 deletions

File tree

‎packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ export function Autocomplete(props: {
242242
description: "toggle thinking visibility",
243243
onSelect: () => command.trigger("session.toggle.thinking"),
244244
},
245+
{
246+
display: "/continue",
247+
description: "continue interrupted conversation",
248+
onSelect: () => command.trigger("session.continue"),
249+
},
245250
)
246251
if (sync.data.config.share !== "disabled") {
247252
results.push({
@@ -270,12 +275,12 @@ export function Autocomplete(props: {
270275
description: "list agents",
271276
onSelect: () => command.trigger("agent.list"),
272277
},
273-
{
274-
display: "/session",
275-
aliases: ["/resume", "/continue"],
276-
description: "list sessions",
277-
onSelect: () => command.trigger("session.list"),
278-
},
278+
{
279+
display: "/session",
280+
aliases: ["/resume"],
281+
description: "list sessions",
282+
onSelect: () => command.trigger("session.list"),
283+
},
279284
{
280285
display: "/status",
281286
aliases: ["/mcp"],

‎packages/opencode/src/cli/cmd/tui/routes/session/index.tsx‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,29 @@ export function Session() {
408408
})
409409
},
410410
},
411+
{
412+
title: "Continue interrupted conversation",
413+
value: "session.continue",
414+
keybind: "session_continue",
415+
category: "Session",
416+
onSelect: async (dialog) => {
417+
const status = sync.data.session_status[route.sessionID]
418+
if (status?.type !== "idle") await sdk.client.session.abort({ path: { id: route.sessionID } }).catch(() => {})
419+
420+
const result = await sdk.client.session.continue({
421+
path: {
422+
id: route.sessionID,
423+
},
424+
})
425+
426+
if (result.data) {
427+
toBottom()
428+
} else {
429+
// Show message that there's nothing to continue
430+
dialog.clear()
431+
}
432+
},
433+
},
411434
{
412435
title: sidebarVisible() ? "Hide sidebar" : "Show sidebar",
413436
value: "session.sidebar.toggle",

‎packages/opencode/src/server/server.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,75 @@ export namespace Server {
12861286
return c.json(session)
12871287
},
12881288
)
1289+
.post(
1290+
"/session/:id/continue",
1291+
describeRoute({
1292+
description: "Continue interrupted conversation",
1293+
operationId: "session.continue",
1294+
responses: {
1295+
200: {
1296+
description: "Conversation continued",
1297+
content: {
1298+
"application/json": {
1299+
schema: resolver(z.boolean()),
1300+
},
1301+
},
1302+
},
1303+
...errors(400, 404),
1304+
},
1305+
}),
1306+
validator(
1307+
"param",
1308+
z.object({
1309+
id: z.string(),
1310+
}),
1311+
),
1312+
async (c) => {
1313+
const id = c.req.valid("param").id
1314+
1315+
// Check if session has an unfinished assistant message
1316+
const msgs = await Session.messages({ sessionID: id })
1317+
let lastAssistant: MessageV2.Assistant | undefined
1318+
1319+
for (let i = msgs.length - 1; i >= 0; i--) {
1320+
const msg = msgs[i]
1321+
if (msg.info.role === "assistant") {
1322+
lastAssistant = msg.info as MessageV2.Assistant
1323+
break
1324+
}
1325+
}
1326+
1327+
// If no unfinished assistant message, return false
1328+
if (!lastAssistant || (lastAssistant.finish && !["tool-calls", "unknown"].includes(lastAssistant.finish))) {
1329+
return c.json(false)
1330+
}
1331+
1332+
// Find the last user message to revert to
1333+
let lastUser: MessageV2.User | undefined
1334+
for (let i = msgs.length - 1; i >= 0; i--) {
1335+
const msg = msgs[i]
1336+
if (msg.info.role === "user") {
1337+
lastUser = msg.info as MessageV2.User
1338+
break
1339+
}
1340+
}
1341+
1342+
if (!lastUser) {
1343+
return c.json(false)
1344+
}
1345+
1346+
// Revert the unfinished assistant message
1347+
await SessionRevert.revert({
1348+
sessionID: id,
1349+
messageID: lastUser.id,
1350+
})
1351+
1352+
// Start the conversation loop to continue
1353+
await SessionPrompt.loop(id)
1354+
1355+
return c.json(true)
1356+
},
1357+
)
12891358
.post(
12901359
"/session/:id/permissions/:permissionID",
12911360
describeRoute({

‎packages/opencode/test/session/session.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,22 @@ describe("session.started event", () => {
6868
},
6969
})
7070
})
71+
72+
describe("session continue functionality", () => {
73+
test("should identify unfinished assistant messages correctly", async () => {
74+
await Instance.provide({
75+
directory: projectRoot,
76+
fn: async () => {
77+
// Create a session
78+
const session = await Session.create({})
79+
80+
// Test that empty session has no messages
81+
const emptyMsgs = await Session.messages({ sessionID: session.id })
82+
expect(emptyMsgs).toEqual([])
83+
84+
await Session.remove(session.id)
85+
},
86+
})
87+
})
88+
})
7189
})

‎packages/sdk/js/openapi.json‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,6 +2235,61 @@
22352235
}
22362236
}
22372237
},
2238+
"/session/{id}/continue": {
2239+
"post": {
2240+
"operationId": "session.continue",
2241+
"parameters": [
2242+
{
2243+
"in": "query",
2244+
"name": "directory",
2245+
"schema": {
2246+
"type": "string"
2247+
}
2248+
},
2249+
{
2250+
"in": "path",
2251+
"name": "id",
2252+
"schema": {
2253+
"type": "string"
2254+
},
2255+
"required": true
2256+
}
2257+
],
2258+
"description": "Continue interrupted conversation",
2259+
"responses": {
2260+
"200": {
2261+
"description": "Conversation continued",
2262+
"content": {
2263+
"application/json": {
2264+
"schema": {
2265+
"type": "boolean"
2266+
}
2267+
}
2268+
}
2269+
},
2270+
"400": {
2271+
"description": "Bad request",
2272+
"content": {
2273+
"application/json": {
2274+
"schema": {
2275+
"$ref": "#/components/schemas/BadRequestError"
2276+
}
2277+
}
2278+
}
2279+
},
2280+
"404": {
2281+
"description": "Not found",
2282+
"content": {
2283+
"application/json": {
2284+
"schema": {
2285+
"$ref": "#/components/schemas/NotFoundError"
2286+
}
2287+
}
2288+
}
2289+
}
2290+
}
2291+
}
2292+
},
22382293
"/session/{id}/permissions/{permissionID}": {
22392294
"post": {
22402295
"operationId": "postSession:idPermissions:permissionID",

‎packages/sdk/js/src/gen/sdk.gen.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ import type {
109109
SessionUnrevertData,
110110
SessionUnrevertResponses,
111111
SessionUnrevertErrors,
112+
SessionContinueData,
113+
SessionContinueResponses,
114+
SessionContinueErrors,
112115
PostSessionIdPermissionsPermissionIdData,
113116
PostSessionIdPermissionsPermissionIdResponses,
114117
PostSessionIdPermissionsPermissionIdErrors,
@@ -682,6 +685,16 @@ class Session extends _HeyApiClient {
682685
...options,
683686
})
684687
}
688+
689+
/**
690+
* Continue interrupted conversation
691+
*/
692+
public continue<ThrowOnError extends boolean = false>(options: Options<SessionContinueData, ThrowOnError>) {
693+
return (options.client ?? this._client).post<SessionContinueResponses, SessionContinueErrors, ThrowOnError>({
694+
url: "/session/{id}/continue",
695+
...options,
696+
})
697+
}
685698
}
686699

687700
class Command extends _HeyApiClient {

‎packages/sdk/js/src/gen/types.gen.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2826,6 +2826,39 @@ export type SessionUnrevertResponses = {
28262826

28272827
export type SessionUnrevertResponse = SessionUnrevertResponses[keyof SessionUnrevertResponses]
28282828

2829+
export type SessionContinueData = {
2830+
body?: never
2831+
path: {
2832+
id: string
2833+
}
2834+
query?: {
2835+
directory?: string
2836+
}
2837+
url: "/session/{id}/continue"
2838+
}
2839+
2840+
export type SessionContinueErrors = {
2841+
/**
2842+
* Bad request
2843+
*/
2844+
400: BadRequestError
2845+
/**
2846+
* Not found
2847+
*/
2848+
404: NotFoundError
2849+
}
2850+
2851+
export type SessionContinueError = SessionContinueErrors[keyof SessionContinueErrors]
2852+
2853+
export type SessionContinueResponses = {
2854+
/**
2855+
* Conversation continued
2856+
*/
2857+
200: boolean
2858+
}
2859+
2860+
export type SessionContinueResponse = SessionContinueResponses[keyof SessionContinueResponses]
2861+
28292862
export type PostSessionIdPermissionsPermissionIdData = {
28302863
body?: {
28312864
response: "once" | "always" | "reject"

0 commit comments

Comments
 (0)