Skip to content

Commit 3ca9ffb

Browse files
committed
feat(tui): add force push for queued messages
Allow users to interrupt current generation and immediately process a queued message by pressing Enter with an empty prompt. - Add SessionPrompt.force() to abort and restart loop with queued message - Add POST /session/:sessionID/force API endpoint - Show 'enter force' hint when a queued message exists Closes anomalyco#5770
1 parent 0ccf9bd commit 3ca9ffb

5 files changed

Lines changed: 133 additions & 1 deletion

File tree

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ export function Prompt(props: PromptProps) {
113113
return messages.findLast((m) => m.role === "user")
114114
})
115115

116+
const hasQueuedMessage = createMemo(() => {
117+
if (!props.sessionID) return false
118+
if (status().type === "idle") return false
119+
const messages = sync.data.message[props.sessionID]
120+
if (!messages) return false
121+
const pending = messages.findLast((m) => m.role === "assistant" && !m.time.completed)?.id
122+
if (!pending) return false
123+
return messages.some((m) => m.role === "user" && m.id > pending)
124+
})
125+
116126
const [store, setStore] = createStore<{
117127
prompt: PromptInfo
118128
mode: "normal" | "shell"
@@ -489,7 +499,12 @@ export function Prompt(props: PromptProps) {
489499
async function submit() {
490500
if (props.disabled) return
491501
if (autocomplete?.visible) return
492-
if (!store.prompt.input) return
502+
if (!store.prompt.input) {
503+
if (hasQueuedMessage() && props.sessionID) {
504+
sdk.client.session.force({ sessionID: props.sessionID }).catch(() => {})
505+
}
506+
return
507+
}
493508
const trimmed = store.prompt.input.trim()
494509
if (trimmed === "exit" || trimmed === "quit" || trimmed === ":q") {
495510
exit()
@@ -1063,6 +1078,11 @@ export function Prompt(props: PromptProps) {
10631078
{store.interrupt > 0 ? "again to interrupt" : "interrupt"}
10641079
</span>
10651080
</text>
1081+
<Show when={hasQueuedMessage()}>
1082+
<text fg={theme.text}>
1083+
enter <span style={{ fg: theme.textMuted }}>force</span>
1084+
</text>
1085+
</Show>
10661086
</box>
10671087
</Show>
10681088
<Show when={status().type !== "retry"}>

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,36 @@ export const SessionRoutes = lazy(() =>
379379
return c.json(true)
380380
},
381381
)
382+
.post(
383+
"/:sessionID/force",
384+
describeRoute({
385+
summary: "Force next queued message",
386+
description:
387+
"Abort current session and immediately process the next queued message. Returns false if no queued message exists.",
388+
operationId: "session.force",
389+
responses: {
390+
200: {
391+
description: "Force result",
392+
content: {
393+
"application/json": {
394+
schema: resolver(z.boolean()),
395+
},
396+
},
397+
},
398+
...errors(400, 404),
399+
},
400+
}),
401+
validator(
402+
"param",
403+
z.object({
404+
sessionID: z.string(),
405+
}),
406+
),
407+
async (c) => {
408+
const result = SessionPrompt.force(c.req.valid("param").sessionID)
409+
return c.json(result)
410+
},
411+
)
382412
.post(
383413
"/:sessionID/share",
384414
describeRoute({

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,23 @@ export namespace SessionPrompt {
254254
return
255255
}
256256

257+
export function force(sessionID: string) {
258+
const s = state()
259+
const match = s[sessionID]
260+
if (!match) return false
261+
if (match.callbacks.length === 0) return false
262+
log.info("force", { sessionID })
263+
for (const item of match.callbacks) {
264+
item.reject()
265+
}
266+
match.callbacks = []
267+
// delete state before aborting so old loop's defer() finds nothing
268+
delete s[sessionID]
269+
match.abort.abort()
270+
loop(sessionID)
271+
return true
272+
}
273+
257274
export const loop = fn(Identifier.schema("session"), async (sessionID) => {
258275
const abort = start(sessionID)
259276
if (!abort) {

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ import type {
102102
SessionDeleteErrors,
103103
SessionDeleteResponses,
104104
SessionDiffResponses,
105+
SessionForceErrors,
106+
SessionForceResponses,
105107
SessionForkResponses,
106108
SessionGetErrors,
107109
SessionGetResponses,
@@ -1101,6 +1103,36 @@ export class Session extends HeyApiClient {
11011103
})
11021104
}
11031105

1106+
/**
1107+
* Force next queued message
1108+
*
1109+
* Abort current session and immediately process the next queued message. Returns false if no queued message exists.
1110+
*/
1111+
public force<ThrowOnError extends boolean = false>(
1112+
parameters: {
1113+
sessionID: string
1114+
directory?: string
1115+
},
1116+
options?: Options<never, ThrowOnError>,
1117+
) {
1118+
const params = buildClientParams(
1119+
[parameters],
1120+
[
1121+
{
1122+
args: [
1123+
{ in: "path", key: "sessionID" },
1124+
{ in: "query", key: "directory" },
1125+
],
1126+
},
1127+
],
1128+
)
1129+
return (options?.client ?? this.client).post<SessionForceResponses, SessionForceErrors, ThrowOnError>({
1130+
url: "/session/{sessionID}/force",
1131+
...options,
1132+
...params,
1133+
})
1134+
}
1135+
11041136
/**
11051137
* Unshare session
11061138
*

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,6 +2985,39 @@ export type SessionAbortResponses = {
29852985

29862986
export type SessionAbortResponse = SessionAbortResponses[keyof SessionAbortResponses]
29872987

2988+
export type SessionForceData = {
2989+
body?: never
2990+
path: {
2991+
sessionID: string
2992+
}
2993+
query?: {
2994+
directory?: string
2995+
}
2996+
url: "/session/{sessionID}/force"
2997+
}
2998+
2999+
export type SessionForceErrors = {
3000+
/**
3001+
* Bad request
3002+
*/
3003+
400: BadRequestError
3004+
/**
3005+
* Not found
3006+
*/
3007+
404: NotFoundError
3008+
}
3009+
3010+
export type SessionForceError = SessionForceErrors[keyof SessionForceErrors]
3011+
3012+
export type SessionForceResponses = {
3013+
/**
3014+
* Force result
3015+
*/
3016+
200: boolean
3017+
}
3018+
3019+
export type SessionForceResponse = SessionForceResponses[keyof SessionForceResponses]
3020+
29883021
export type SessionUnshareData = {
29893022
body?: never
29903023
path: {

0 commit comments

Comments
 (0)