opencode-queue 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +49 -0
  3. package/index.ts +146 -0
  4. package/package.json +42 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 mirsella
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # opencode-queue
2
+
3
+ Queue OpenCode input until the current agent run is actually idle.
4
+
5
+ This plugin adds a `/queue ...` prefix that keeps the current run focused instead of injecting your next message into the still-running loop.
6
+
7
+ ## What it does
8
+
9
+ - Queues normal prompts entered while a session is busy
10
+ - Queues slash commands like `/queue /review` and `/queue /commit`
11
+ - Keeps queued placeholder messages visible in the UI
12
+ - Filters queued placeholders out of model input so they do not interrupt the current run
13
+ - Replays queued input in order once the session becomes idle
14
+
15
+ ## Install
16
+
17
+ Add it to your OpenCode plugin list:
18
+
19
+ ```jsonc
20
+ {
21
+ "plugin": ["opencode-queue"]
22
+ }
23
+ ```
24
+
25
+ OpenCode installs npm plugins automatically at startup.
26
+
27
+ Restart OpenCode after installing.
28
+
29
+ ## Usage
30
+
31
+ While the agent is busy:
32
+
33
+ ```text
34
+ /queue continue after the current task finishes
35
+ /queue /review
36
+ /queue /commit
37
+ ```
38
+
39
+ The queued message is shown as `[queued] ...` and is sent automatically when the current run becomes idle.
40
+
41
+ ## Notes
42
+
43
+ - This is a `/queue` plugin, not a keyboard shortcut plugin. OpenCode plugins cannot currently register custom TUI keybindings.
44
+ - Idle `/queue some text` is treated like a normal prompt with the `/queue` prefix removed.
45
+ - Idle `/queue /command` is left alone and is not intercepted.
46
+
47
+ ## License
48
+
49
+ MIT
package/index.ts ADDED
@@ -0,0 +1,146 @@
1
+ import type { Plugin } from "@opencode-ai/plugin"
2
+ import type {
3
+ AgentPartInput,
4
+ FilePart,
5
+ FilePartInput,
6
+ SubtaskPartInput,
7
+ TextPart,
8
+ TextPartInput,
9
+ } from "@opencode-ai/sdk"
10
+
11
+ const QUEUE = /^\/queue(?:\s+([\s\S]*))?$/
12
+ const COMMAND = /^\/(\S+)(?:\s+([\s\S]*))?$/
13
+
14
+ type InputPart = TextPartInput | FilePartInput | AgentPartInput | SubtaskPartInput
15
+ type Info = { agent: string; model: { providerID: string; modelID: string } }
16
+ type Item = { info: Info; command: string; arguments: string } | { info: Info; parts: InputPart[] }
17
+
18
+ const label = (body: string, files: number) => {
19
+ const text = body.trim() || `${files} attachment${files === 1 ? "" : "s"}`
20
+ return text.length > 72 ? `${text.slice(0, 69)}...` : text
21
+ }
22
+
23
+ const parseCommand = (body: string) => {
24
+ const match = body.trim().match(COMMAND)
25
+ return match ? { command: match[1], arguments: match[2] ?? "" } : undefined
26
+ }
27
+
28
+ const QueuePlugin: Plugin = async ({ client }) => {
29
+ const queue = new Map<string, Item[]>()
30
+ const hidden = new Set<string>()
31
+ const busy = new Set<string>()
32
+ const flushing = new Set<string>()
33
+
34
+ const toast = (message: string, variant: "info" | "error") =>
35
+ client.tui.showToast({ body: { message, variant, duration: 2500 } }).catch(() => undefined)
36
+
37
+ const flush = async (sid: string) => {
38
+ if (flushing.has(sid)) return
39
+
40
+ const list = queue.get(sid)
41
+ if (!list?.length) return
42
+
43
+ flushing.add(sid)
44
+
45
+ try {
46
+ while (list.length) {
47
+ const item = list.shift()
48
+ if (!item) break
49
+
50
+ if ("command" in item) {
51
+ await client.session.command({
52
+ path: { id: sid },
53
+ body: {
54
+ agent: item.info.agent,
55
+ model: `${item.info.model.providerID}/${item.info.model.modelID}`,
56
+ command: item.command,
57
+ arguments: item.arguments,
58
+ },
59
+ })
60
+ continue
61
+ }
62
+
63
+ await client.session.prompt({
64
+ path: { id: sid },
65
+ body: {
66
+ agent: item.info.agent,
67
+ model: item.info.model,
68
+ parts: item.parts.map((part) => ({ ...part, id: undefined })),
69
+ },
70
+ })
71
+ }
72
+ } catch (error) {
73
+ const message = error instanceof Error ? error.message : String(error)
74
+ console.error("QueuePlugin failed to flush queued input", error)
75
+ await toast(`Queue failed: ${message}`, "error")
76
+ } finally {
77
+ if (list.length) queue.set(sid, list)
78
+ else queue.delete(sid)
79
+ flushing.delete(sid)
80
+ }
81
+ }
82
+
83
+ return {
84
+ event: async ({ event }) => {
85
+ if (event.type !== "session.status") return
86
+
87
+ const sid = event.properties.sessionID
88
+ if (event.properties.status.type !== "idle") {
89
+ busy.add(sid)
90
+ return
91
+ }
92
+
93
+ busy.delete(sid)
94
+ await flush(sid)
95
+ },
96
+ "chat.message": async ({ sessionID }, output) => {
97
+ const text = output.parts.find((part): part is TextPart => part.type === "text" && !part.synthetic)
98
+ if (!text) return
99
+
100
+ const body = text.text.match(QUEUE)?.[1]
101
+ if (body === undefined) return
102
+
103
+ const files = output.parts.filter((part): part is FilePart => part.type === "file")
104
+ if (!body.trim() && !files.length) return
105
+
106
+ if (!busy.has(sessionID)) {
107
+ if (body.trimStart().startsWith("/")) return
108
+ text.text = body
109
+ return
110
+ }
111
+
112
+ const parts = output.parts.flatMap((part): InputPart[] => {
113
+ switch (part.type) {
114
+ case "text":
115
+ if (part.id !== text.id) return [{ ...part }]
116
+ return body ? [{ ...part, text: body }] : []
117
+ case "file":
118
+ case "agent":
119
+ case "subtask":
120
+ return [{ ...part }]
121
+ default:
122
+ console.warn("QueuePlugin skipped unexpected part", part.type)
123
+ return []
124
+ }
125
+ })
126
+
127
+ const info = { agent: output.message.agent, model: { ...output.message.model } }
128
+ const command = parseCommand(body)
129
+ const item = command ? { info, ...command } : { info, parts }
130
+ const list = queue.get(sessionID)
131
+ if (list) list.push(item)
132
+ else queue.set(sessionID, [item])
133
+
134
+ const note = label(body, files.length)
135
+ hidden.add(output.message.id)
136
+ text.text = `[queued] ${note}`
137
+ await toast(`Queued: ${note}`, "info")
138
+ },
139
+ "experimental.chat.messages.transform": async (_, output) => {
140
+ output.messages = output.messages.filter((msg) => !hidden.has(msg.info.id))
141
+ },
142
+ }
143
+ }
144
+
145
+ export { QueuePlugin }
146
+ export default QueuePlugin
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/package.json",
3
+ "name": "opencode-queue",
4
+ "version": "0.1.0",
5
+ "type": "module",
6
+ "description": "Queue OpenCode prompts and slash commands until the agent is idle",
7
+ "main": "./index.ts",
8
+ "keywords": [
9
+ "opencode",
10
+ "opencode-plugin",
11
+ "plugin",
12
+ "queue"
13
+ ],
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/mirsella/opencode-queue.git"
17
+ },
18
+ "bugs": {
19
+ "url": "https://github.com/mirsella/opencode-queue/issues"
20
+ },
21
+ "homepage": "https://github.com/mirsella/opencode-queue#readme",
22
+ "license": "MIT",
23
+ "files": [
24
+ "index.ts",
25
+ "README.md",
26
+ "LICENSE"
27
+ ],
28
+ "exports": {
29
+ ".": "./index.ts"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "scripts": {
35
+ "typecheck": "tsc --noEmit"
36
+ },
37
+ "devDependencies": {
38
+ "@opencode-ai/plugin": "latest",
39
+ "@opencode-ai/sdk": "latest",
40
+ "typescript": "^5.9.3"
41
+ }
42
+ }