opencode-traffic-light 1.0.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 +79 -0
  3. package/package.json +27 -0
  4. package/src/index.ts +148 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 niushuai1991
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,79 @@
1
+ # OpenCode Traffic Light
2
+
3
+ A TUI plugin that adds a status indicator to your terminal title bar, giving you a quick visual cue of what OpenCode is doing.
4
+
5
+ ## Status Colors
6
+
7
+ | Color | Meaning |
8
+ |-------|---------|
9
+ | 🟢 Green | Idle, waiting for input, or pending permission/question |
10
+ | 🟡 Yellow | Busy but no active tool or text output (thinking) |
11
+ | 🔴 Red | Running tools or generating text |
12
+
13
+ ## Installation
14
+
15
+ ### Option 1: Install from npm (recommended)
16
+
17
+ Install the package:
18
+
19
+ ```bash
20
+ npm install opencode-traffic-light
21
+ ```
22
+
23
+ Then add to your project's `.opencode/tui.json`:
24
+
25
+ ```jsonc
26
+ {
27
+ "plugin": ["opencode-traffic-light"]
28
+ }
29
+ ```
30
+
31
+ Or install globally via `~/.config/opencode/tui.json`.
32
+
33
+ ### Option 2: Install from GitHub
34
+
35
+ Add to your project's `.opencode/tui.json`:
36
+
37
+ ```jsonc
38
+ {
39
+ "plugin": ["opencode-traffic-light@git+https://github.com/niushuai1991/opencode-traffic-light.git"]
40
+ }
41
+ ```
42
+
43
+ Or install globally via `~/.config/opencode/tui.json`.
44
+
45
+ ### Option 3: Local plugin file
46
+
47
+ Copy `src/index.ts` into your project's plugin directory:
48
+
49
+ ```
50
+ .opencode/plugins/traffic-light.ts
51
+ ```
52
+
53
+ ### Option 4: Global plugin file
54
+
55
+ Copy `src/index.ts` to the global plugin directory:
56
+
57
+ ```
58
+ ~/.config/opencode/plugins/traffic-light.ts
59
+ ```
60
+
61
+ ## Usage
62
+
63
+ The traffic light activates automatically. The terminal title will look like:
64
+
65
+ - `🟢 OC | My Session` — idle
66
+ - `🟡 OC | My Session` — thinking
67
+ - `🔴 OC | My Session` — working
68
+
69
+ ### Toggle
70
+
71
+ Run **"Toggle traffic light"** from the command palette to enable or disable the traffic light. The preference is persisted across sessions.
72
+
73
+ ### Environment Variable
74
+
75
+ Set `OPENCODE_DISABLE_TERMINAL_TITLE=1` to prevent the plugin from modifying the terminal title.
76
+
77
+ ## License
78
+
79
+ MIT
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "opencode-traffic-light",
3
+ "version": "1.0.0",
4
+ "description": "TUI plugin that adds a traffic-light status indicator to your OpenCode terminal title",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "niushuai1991",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/niushuai1991/opencode-traffic-light.git"
11
+ },
12
+ "homepage": "https://github.com/niushuai1991/opencode-traffic-light#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/niushuai1991/opencode-traffic-light/issues"
15
+ },
16
+ "keywords": ["opencode", "opencode-plugin", "tui", "terminal", "status", "traffic-light"],
17
+ "files": ["src", "README.md", "LICENSE"],
18
+ "exports": {
19
+ "./tui": "./src/index.ts"
20
+ },
21
+ "peerDependencies": {
22
+ "@opencode-ai/plugin": ">=1.0.0"
23
+ },
24
+ "engines": {
25
+ "opencode": ">=1.0.0"
26
+ }
27
+ }
package/src/index.ts ADDED
@@ -0,0 +1,148 @@
1
+ import type { TuiPlugin, TuiPluginModule } from "@opencode-ai/plugin/tui"
2
+
3
+ type TrafficLightColor = "green" | "yellow" | "red"
4
+
5
+ type TrafficLightInput = {
6
+ enabled: boolean
7
+ sessionStatus?: { type: string }
8
+ messages?: readonly { role: string; id: string }[]
9
+ pendingInput: boolean
10
+ parts?: readonly {
11
+ type: string
12
+ state?: { status?: string }
13
+ synthetic?: boolean
14
+ ignored?: boolean
15
+ }[]
16
+ }
17
+
18
+ function computeTrafficLight(input: TrafficLightInput): TrafficLightColor | null {
19
+ if (!input.enabled) return null
20
+ if (!input.sessionStatus || input.sessionStatus.type === "idle") return "green"
21
+ if (!input.messages || input.messages.length === 0) return "green"
22
+ if (input.pendingInput) return "green"
23
+
24
+ const lastAssistant = [...input.messages].reverse().find(m => m.role === "assistant")
25
+ if (!lastAssistant) return "yellow"
26
+ if (!input.parts || input.parts.length === 0) return "yellow"
27
+
28
+ const hasActiveTool = input.parts.some(
29
+ p => p.type === "tool" && (p.state?.status === "running" || p.state?.status === "pending"),
30
+ )
31
+ if (hasActiveTool) return "red"
32
+
33
+ const hasActiveText = input.parts.some(
34
+ p => p.type === "text" && !p.synthetic && !p.ignored,
35
+ )
36
+ if (hasActiveText) return "red"
37
+
38
+ return "yellow"
39
+ }
40
+
41
+ function statusEmoji(color: TrafficLightColor | null): string {
42
+ if (color === "green") return "\u{1F7E2}"
43
+ if (color === "yellow") return "\u{1F7E1}"
44
+ if (color === "red") return "\u{1F534}"
45
+ return ""
46
+ }
47
+
48
+ const EVENTS = [
49
+ "session.status",
50
+ "session.idle",
51
+ "message.updated",
52
+ "message.part.updated",
53
+ "message.part.delta",
54
+ "permission.asked",
55
+ "permission.replied",
56
+ "question.asked",
57
+ "question.replied",
58
+ "session.next.tool.called",
59
+ "session.next.tool.success",
60
+ "session.next.tool.failed",
61
+ "session.next.text.started",
62
+ "session.next.text.ended",
63
+ ] as const
64
+
65
+ const tui: TuiPlugin = async (api) => {
66
+ if (process.env.OPENCODE_DISABLE_TERMINAL_TITLE === "1") return
67
+
68
+ const KV_KEY = "traffic_light_enabled"
69
+ let enabled = api.kv.get<boolean>(KV_KEY, true)
70
+ let timer: ReturnType<typeof setTimeout> | undefined
71
+
72
+ function scheduleUpdate() {
73
+ if (timer !== undefined) clearTimeout(timer)
74
+ timer = setTimeout(doUpdateTitle, 0)
75
+ }
76
+
77
+ function doUpdateTitle() {
78
+ timer = undefined
79
+ const route = api.route.current
80
+ if (route.name !== "session") {
81
+ api.renderer.setTerminalTitle("OpenCode")
82
+ return
83
+ }
84
+
85
+ const sessionID = route.params.sessionID
86
+ const status = api.state.session.status(sessionID)
87
+ const messages = api.state.session.messages(sessionID)
88
+ const permissions = api.state.session.permission(sessionID)
89
+ const questions = api.state.session.question(sessionID)
90
+ const lastAssistant = messages && [...messages].reverse().find(m => m.role === "assistant")
91
+ const parts = lastAssistant ? api.state.part(lastAssistant.id) : undefined
92
+
93
+ const color = computeTrafficLight({
94
+ enabled,
95
+ sessionStatus: status ?? undefined,
96
+ messages,
97
+ pendingInput: permissions.length > 0 || questions.length > 0,
98
+ parts,
99
+ })
100
+ const emoji = statusEmoji(color)
101
+
102
+ const session = api.state.session.get(sessionID)
103
+ const isDefaultTitle = !session?.title || session.title.trim() === ""
104
+ if (isDefaultTitle) {
105
+ api.renderer.setTerminalTitle(`${emoji} OpenCode`)
106
+ return
107
+ }
108
+
109
+ const title =
110
+ session!.title.length > 40 ? session!.title.slice(0, 37) + "..." : session!.title
111
+ api.renderer.setTerminalTitle(`${emoji} OC | ${title}`)
112
+ }
113
+
114
+ const unsubscribers = EVENTS.map((event) => api.event.on(event, () => scheduleUpdate()))
115
+
116
+ api.keymap.registerLayer({
117
+ commands: [
118
+ {
119
+ name: "traffic_light.toggle",
120
+ title: "Toggle traffic light",
121
+ category: "Plugin",
122
+ namespace: "palette",
123
+ run() {
124
+ enabled = !enabled
125
+ api.kv.set(KV_KEY, enabled)
126
+ api.ui.toast({
127
+ variant: "info",
128
+ message: `Traffic light ${enabled ? "enabled" : "disabled"}`,
129
+ })
130
+ scheduleUpdate()
131
+ },
132
+ },
133
+ ],
134
+ bindings: [],
135
+ })
136
+
137
+ api.lifecycle.onDispose(() => {
138
+ if (timer !== undefined) clearTimeout(timer)
139
+ unsubscribers.forEach((fn) => fn())
140
+ })
141
+ }
142
+
143
+ const plugin: TuiPluginModule & { id: string } = {
144
+ id: "opencode-traffic-light",
145
+ tui,
146
+ }
147
+
148
+ export default plugin