@fatihdogmus/opencode-voice-input 1.0.0 → 1.0.1
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.
- package/package.json +10 -2
- package/src/contract.ts +0 -9
- package/src/gui.tsx +0 -274
- package/src/inject.tsx +0 -167
- package/src/main.ts +0 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fatihdogmus/opencode-voice-input",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Voice prompts for OpenCode Desktop and the TUI, transcribed with the OpenAI account selected in OpenCode",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -35,13 +35,21 @@
|
|
|
35
35
|
"./gui-experimental": "./dist/gui-experimental.cjs"
|
|
36
36
|
},
|
|
37
37
|
"files": [
|
|
38
|
-
"src",
|
|
38
|
+
"src/index.ts",
|
|
39
|
+
"src/rpc.ts",
|
|
40
|
+
"src/tui.tsx",
|
|
41
|
+
"src/tui-audio.ts",
|
|
39
42
|
"dist"
|
|
40
43
|
],
|
|
41
44
|
"dependencies": {
|
|
42
45
|
"@opencode/plugin": "2.2.0",
|
|
43
46
|
"effect": "4.0.1"
|
|
44
47
|
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"@opentui/core": ">=0.6.6",
|
|
50
|
+
"@opentui/solid": ">=0.6.6",
|
|
51
|
+
"solid-js": ">=1.9.0"
|
|
52
|
+
},
|
|
45
53
|
"devDependencies": {
|
|
46
54
|
"@opencode/theme": "2.2.0",
|
|
47
55
|
"@opentui/core": "0.7.0",
|
package/src/contract.ts
DELETED
package/src/gui.tsx
DELETED
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
import { createSignal, onCleanup } from "solid-js"
|
|
2
|
-
import { Command, ComposerPill, Plugin, Style, type ComposerPillView, type SetupContext } from "@opencode/plugin/gui"
|
|
3
|
-
import { Voice } from "./contract"
|
|
4
|
-
import { composerOf, CSS, injectComposerButtons, typeInto } from "./inject"
|
|
5
|
-
import { VoiceRpc } from "./rpc"
|
|
6
|
-
import { main } from "./main"
|
|
7
|
-
|
|
8
|
-
const definition = Plugin.define({
|
|
9
|
-
i18n: {
|
|
10
|
-
en: {
|
|
11
|
-
group: "Voice input",
|
|
12
|
-
"pill.idle": "🎙 Dictate",
|
|
13
|
-
"pill.idle.title": "Record a voice prompt and transcribe it with your OpenAI account",
|
|
14
|
-
"pill.starting": "🎙 Starting…",
|
|
15
|
-
"pill.recording": "Stop {{time}}",
|
|
16
|
-
"pill.recording.title": "Stop recording and transcribe",
|
|
17
|
-
"pill.transcribing": "Transcribing…",
|
|
18
|
-
"button.idle": "Dictate (⌘⌥M)",
|
|
19
|
-
"button.recording": "Stop and transcribe (⌘⌥M)",
|
|
20
|
-
"button.busy": "Transcribing…",
|
|
21
|
-
"command.toggle": "Start or stop voice dictation",
|
|
22
|
-
"command.cancel": "Cancel voice dictation",
|
|
23
|
-
"error.title": "Voice input failed",
|
|
24
|
-
"error.microphone":
|
|
25
|
-
"Microphone access was denied. Allow OpenCode in System Settings › Privacy & Security › Microphone.",
|
|
26
|
-
nospeech: "No speech was heard",
|
|
27
|
-
copied: "No prompt is open, so the transcript was copied to the clipboard",
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
type Ctx = SetupContext<typeof definition>
|
|
33
|
-
|
|
34
|
-
type Phase = { kind: "idle" } | { kind: "starting" } | { kind: "recording"; since: number } | { kind: "transcribing" }
|
|
35
|
-
|
|
36
|
-
const MAX_RECORDING_MS = 10 * 60_000
|
|
37
|
-
const MIME_TYPES = ["audio/webm;codecs=opus", "audio/webm", "audio/ogg;codecs=opus", "audio/mp4"]
|
|
38
|
-
|
|
39
|
-
const toBase64 = (blob: Blob) =>
|
|
40
|
-
new Promise<string>((resolve, reject) => {
|
|
41
|
-
const reader = new FileReader()
|
|
42
|
-
reader.onload = () => resolve(String(reader.result).replace(/^data:[^,]*,/, ""))
|
|
43
|
-
reader.onerror = () => reject(reader.error ?? new Error("Could not read the recording"))
|
|
44
|
-
reader.readAsDataURL(blob)
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
const message = (error: unknown) => {
|
|
48
|
-
// Plugin RPC errors carry the user-facing reason in data.message.
|
|
49
|
-
const data = (error as { data?: { message?: unknown } } | undefined)?.data
|
|
50
|
-
if (typeof data?.message === "string") return data.message
|
|
51
|
-
return error instanceof Error ? error.message : String(error)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
class Recorder {
|
|
55
|
-
private stream?: MediaStream
|
|
56
|
-
private recorder?: MediaRecorder
|
|
57
|
-
private chunks: Blob[] = []
|
|
58
|
-
|
|
59
|
-
async start() {
|
|
60
|
-
this.stream = await navigator.mediaDevices.getUserMedia({
|
|
61
|
-
audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true },
|
|
62
|
-
})
|
|
63
|
-
const mimeType = MIME_TYPES.find((type) => MediaRecorder.isTypeSupported(type))
|
|
64
|
-
this.chunks = []
|
|
65
|
-
this.recorder = new MediaRecorder(this.stream, mimeType ? { mimeType } : undefined)
|
|
66
|
-
this.recorder.ondataavailable = (event) => {
|
|
67
|
-
if (event.data.size > 0) this.chunks.push(event.data)
|
|
68
|
-
}
|
|
69
|
-
this.recorder.start(250)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
stop() {
|
|
73
|
-
const recorder = this.recorder
|
|
74
|
-
if (!recorder) return Promise.resolve(undefined)
|
|
75
|
-
return new Promise<Blob>((resolve) => {
|
|
76
|
-
recorder.onstop = () => {
|
|
77
|
-
const blob = new Blob(this.chunks, { type: recorder.mimeType || "audio/webm" })
|
|
78
|
-
this.release()
|
|
79
|
-
resolve(blob)
|
|
80
|
-
}
|
|
81
|
-
recorder.stop()
|
|
82
|
-
})
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
release() {
|
|
86
|
-
if (this.recorder?.state === "recording") this.recorder.stop()
|
|
87
|
-
this.stream?.getTracks().forEach((track) => track.stop())
|
|
88
|
-
this.stream = undefined
|
|
89
|
-
this.recorder = undefined
|
|
90
|
-
this.chunks = []
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const setup = (ctx: Ctx) => {
|
|
95
|
-
const voice = ctx.main(Voice)
|
|
96
|
-
const recorder = new Recorder()
|
|
97
|
-
const [phase, setPhase] = createSignal<Phase>({ kind: "idle" })
|
|
98
|
-
const [now, setNow] = createSignal(Date.now())
|
|
99
|
-
let ticker: ReturnType<typeof setInterval> | undefined
|
|
100
|
-
const [owner, setOwner] = createSignal<HTMLElement>()
|
|
101
|
-
const placement = ctx.options.placement === "pill" ? "pill" : "composer"
|
|
102
|
-
|
|
103
|
-
const language = () => (typeof ctx.options.language === "string" ? ctx.options.language : "")
|
|
104
|
-
|
|
105
|
-
const stopTicker = () => {
|
|
106
|
-
if (ticker) clearInterval(ticker)
|
|
107
|
-
ticker = undefined
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const fail = (error: unknown) => {
|
|
111
|
-
stopTicker()
|
|
112
|
-
recorder.release()
|
|
113
|
-
setPhase({ kind: "idle" })
|
|
114
|
-
setOwner(undefined)
|
|
115
|
-
ctx.toast.show({ variant: "error", title: ctx.t("error.title"), description: message(error) })
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const insert = (text: string) => {
|
|
119
|
-
if (!text) {
|
|
120
|
-
ctx.toast.show({ title: ctx.t("nospeech") })
|
|
121
|
-
return
|
|
122
|
-
}
|
|
123
|
-
const composer = ctx.screen.current()?.composer
|
|
124
|
-
const target = composerOf(owner())
|
|
125
|
-
// The plugin API reaches the routed session's prompt; a new-session composer is only reachable through the DOM.
|
|
126
|
-
if (!composer && target && typeInto(target, text)) return
|
|
127
|
-
if (!composer) {
|
|
128
|
-
void ctx.system.copy(text)
|
|
129
|
-
ctx.toast.show({ title: ctx.t("copied") })
|
|
130
|
-
return
|
|
131
|
-
}
|
|
132
|
-
const current = composer.text()
|
|
133
|
-
composer.insert(current && !/\s$/.test(current) ? ` ${text}` : text)
|
|
134
|
-
composer.focus()
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const start = async (host?: HTMLElement) => {
|
|
138
|
-
if (phase().kind !== "idle") return
|
|
139
|
-
setOwner(host)
|
|
140
|
-
setPhase({ kind: "starting" })
|
|
141
|
-
try {
|
|
142
|
-
if (!(await voice.requestMicrophone())) throw new Error(ctx.t("error.microphone"))
|
|
143
|
-
await recorder.start()
|
|
144
|
-
const since = Date.now()
|
|
145
|
-
setNow(since)
|
|
146
|
-
setPhase({ kind: "recording", since })
|
|
147
|
-
ticker = setInterval(() => {
|
|
148
|
-
setNow(Date.now())
|
|
149
|
-
if (Date.now() - since > MAX_RECORDING_MS) void stop()
|
|
150
|
-
}, 500)
|
|
151
|
-
} catch (error) {
|
|
152
|
-
fail(error)
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const stop = async () => {
|
|
157
|
-
if (phase().kind !== "recording") return
|
|
158
|
-
stopTicker()
|
|
159
|
-
setPhase({ kind: "transcribing" })
|
|
160
|
-
try {
|
|
161
|
-
const blob = await recorder.stop()
|
|
162
|
-
if (!blob || blob.size === 0) throw new Error(ctx.t("nospeech"))
|
|
163
|
-
const audio = await toBase64(blob)
|
|
164
|
-
const result = await ctx.client.rpc(VoiceRpc).transcribe({ audio, mimeType: blob.type, language: language() })
|
|
165
|
-
const text = (result as { text: string }).text
|
|
166
|
-
setPhase({ kind: "idle" })
|
|
167
|
-
insert(text)
|
|
168
|
-
setOwner(undefined)
|
|
169
|
-
} catch (error) {
|
|
170
|
-
fail(error)
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const cancel = () => {
|
|
175
|
-
if (phase().kind !== "recording" && phase().kind !== "starting") return false
|
|
176
|
-
stopTicker()
|
|
177
|
-
recorder.release()
|
|
178
|
-
setPhase({ kind: "idle" })
|
|
179
|
-
setOwner(undefined)
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const toggle = (host?: HTMLElement) => {
|
|
183
|
-
const kind = phase().kind
|
|
184
|
-
if (kind === "idle") void start(host ?? hostOfFocusedComposer())
|
|
185
|
-
else if (kind === "recording") void stop()
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const elapsed = (since: number) => {
|
|
189
|
-
const seconds = Math.max(0, Math.floor((now() - since) / 1000))
|
|
190
|
-
return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
const buttons =
|
|
194
|
-
placement === "composer"
|
|
195
|
-
? injectComposerButtons({
|
|
196
|
-
phase: () => phase().kind,
|
|
197
|
-
owner,
|
|
198
|
-
elapsed: () => {
|
|
199
|
-
const current = phase()
|
|
200
|
-
return current.kind === "recording" ? elapsed(current.since) : ""
|
|
201
|
-
},
|
|
202
|
-
title: () => {
|
|
203
|
-
const kind = phase().kind
|
|
204
|
-
if (kind === "recording") return ctx.t("button.recording")
|
|
205
|
-
if (kind === "idle") return ctx.t("button.idle")
|
|
206
|
-
return ctx.t("button.busy")
|
|
207
|
-
},
|
|
208
|
-
onClick: (host) => toggle(host),
|
|
209
|
-
})
|
|
210
|
-
: undefined
|
|
211
|
-
if (buttons) ctx.add(Style, CSS)
|
|
212
|
-
|
|
213
|
-
function hostOfFocusedComposer() {
|
|
214
|
-
return composerOf(undefined)?.querySelector<HTMLElement>("[data-voice-input]") ?? undefined
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
ctx.add(ComposerPill, {
|
|
218
|
-
id: "mic",
|
|
219
|
-
// The pill stands in when the mic can't be placed next to the send button, e.g. after an app update moves it.
|
|
220
|
-
show: (): ComposerPillView | undefined => {
|
|
221
|
-
if (buttons && buttons.count() > 0) return undefined
|
|
222
|
-
const current = phase()
|
|
223
|
-
switch (current.kind) {
|
|
224
|
-
case "idle":
|
|
225
|
-
return {
|
|
226
|
-
label: ctx.t("pill.idle"),
|
|
227
|
-
title: ctx.t("pill.idle.title"),
|
|
228
|
-
run: () => toggle(),
|
|
229
|
-
}
|
|
230
|
-
case "starting":
|
|
231
|
-
return { label: ctx.t("pill.starting") }
|
|
232
|
-
case "recording":
|
|
233
|
-
return {
|
|
234
|
-
label: ctx.t("pill.recording", { time: elapsed(current.since) }),
|
|
235
|
-
title: ctx.t("pill.recording.title"),
|
|
236
|
-
icon: "stop",
|
|
237
|
-
tone: "danger",
|
|
238
|
-
run: () => toggle(),
|
|
239
|
-
}
|
|
240
|
-
case "transcribing":
|
|
241
|
-
return { label: ctx.t("pill.transcribing"), icon: "outline-wave-audio", tone: "warning" }
|
|
242
|
-
}
|
|
243
|
-
},
|
|
244
|
-
})
|
|
245
|
-
|
|
246
|
-
ctx.add(Command, (): Command => ({
|
|
247
|
-
id: "toggle",
|
|
248
|
-
title: ctx.t("command.toggle"),
|
|
249
|
-
group: ctx.t("group"),
|
|
250
|
-
keywords: "voice mic microphone dictate speech record transcribe",
|
|
251
|
-
section: "prompt",
|
|
252
|
-
bind: "mod+alt+m",
|
|
253
|
-
editable: true,
|
|
254
|
-
slash: { name: "dictate" },
|
|
255
|
-
run: () => toggle(),
|
|
256
|
-
}))
|
|
257
|
-
|
|
258
|
-
ctx.add(Command, (): Command => ({
|
|
259
|
-
id: "cancel",
|
|
260
|
-
title: ctx.t("command.cancel"),
|
|
261
|
-
group: ctx.t("group"),
|
|
262
|
-
section: "prompt",
|
|
263
|
-
enabled: phase().kind === "recording" || phase().kind === "starting",
|
|
264
|
-
run: () => cancel(),
|
|
265
|
-
}))
|
|
266
|
-
|
|
267
|
-
onCleanup(() => {
|
|
268
|
-
buttons?.dispose()
|
|
269
|
-
stopTicker()
|
|
270
|
-
recorder.release()
|
|
271
|
-
})
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
export default Plugin.define({ ...definition, setup, main })
|
package/src/inject.tsx
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
import { createEffect, createSignal, Match, Switch, type Accessor } from "solid-js"
|
|
2
|
-
import { render } from "solid-js/web"
|
|
3
|
-
|
|
4
|
-
// Internal DOM of OpenCode Desktop's composer (app/src/composer/editor/editor.tsx), not plugin API:
|
|
5
|
-
// <form data-component="composer"> … <div class="flex h-11 …"> ← static row
|
|
6
|
-
// <div>+ menu</div> <div data-slot="composer-controls"/> <div data-slot="composer-actions"/> ← send button
|
|
7
|
-
// The mic goes into the static row right before composer-actions, so Solid never reconciles over it.
|
|
8
|
-
const ACTIONS = '[data-component="composer"] [data-slot="composer-actions"]'
|
|
9
|
-
const HOST = "data-voice-input"
|
|
10
|
-
|
|
11
|
-
export type ButtonPhase = "idle" | "starting" | "recording" | "transcribing"
|
|
12
|
-
|
|
13
|
-
export interface ButtonModel {
|
|
14
|
-
phase: Accessor<ButtonPhase>
|
|
15
|
-
owner: Accessor<HTMLElement | undefined>
|
|
16
|
-
elapsed: Accessor<string>
|
|
17
|
-
title: Accessor<string>
|
|
18
|
-
onClick(host: HTMLElement): void
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export const CSS = `
|
|
22
|
-
[${HOST}] { display: flex; align-items: center; gap: 4px; flex-shrink: 0; margin-inline-end: 6px; }
|
|
23
|
-
[${HOST}] > button[data-component="icon-button-v2"] { width: 28px; height: 28px; border-radius: 6px; }
|
|
24
|
-
[${HOST}] > button svg { width: 16px; height: 16px; display: block; }
|
|
25
|
-
[${HOST}][data-phase="recording"] > button { color: var(--v2-state-fg-danger); background-color: color-mix(in srgb, var(--v2-state-fg-danger) 14%, transparent); }
|
|
26
|
-
[${HOST}][data-phase="recording"] > button:hover { background-color: color-mix(in srgb, var(--v2-state-fg-danger) 22%, transparent); }
|
|
27
|
-
[${HOST}][data-phase="transcribing"] > button svg, [${HOST}][data-phase="starting"] > button svg { animation: voice-input-spin .9s linear infinite; }
|
|
28
|
-
[${HOST}] [data-slot="voice-input-time"] { font-size: 11px; font-weight: 530; font-variant-numeric: tabular-nums; color: var(--v2-state-fg-danger); }
|
|
29
|
-
[${HOST}][data-phase="recording"] [data-slot="voice-input-dot"] { width: 6px; height: 6px; border-radius: 9999px; background: var(--v2-state-fg-danger); animation: voice-input-pulse 1.2s ease-in-out infinite; }
|
|
30
|
-
@keyframes voice-input-spin { to { transform: rotate(360deg); } }
|
|
31
|
-
@keyframes voice-input-pulse { 50% { opacity: .25; } }
|
|
32
|
-
:root[data-motion="reduce"] [${HOST}] * { animation: none !important; }
|
|
33
|
-
`
|
|
34
|
-
|
|
35
|
-
const MicIcon = () => (
|
|
36
|
-
<svg viewBox="0 0 16 16" fill="none" aria-hidden="true">
|
|
37
|
-
<rect x="5.5" y="1.5" width="5" height="8.5" rx="2.5" stroke="currentColor" />
|
|
38
|
-
<path d="M3 7.75a5 5 0 0 0 10 0M8 12.75v1.75M5.75 14.5h4.5" stroke="currentColor" stroke-linecap="round" />
|
|
39
|
-
</svg>
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
const StopIcon = () => (
|
|
43
|
-
<svg viewBox="0 0 16 16" aria-hidden="true">
|
|
44
|
-
<rect x="4" y="4" width="8" height="8" rx="1.5" fill="currentColor" />
|
|
45
|
-
</svg>
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
const SpinnerIcon = () => (
|
|
49
|
-
<svg viewBox="0 0 16 16" fill="none" aria-hidden="true">
|
|
50
|
-
<circle cx="8" cy="8" r="5.5" stroke="currentColor" stroke-opacity=".25" stroke-width="1.5" />
|
|
51
|
-
<path d="M13.5 8A5.5 5.5 0 0 0 8 2.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" />
|
|
52
|
-
</svg>
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
function MicButton(props: { host: HTMLElement; model: ButtonModel }) {
|
|
56
|
-
// Only the composer that started a recording shows its progress; the others stay a plain, disabled mic.
|
|
57
|
-
const phase = () => {
|
|
58
|
-
const current = props.model.phase()
|
|
59
|
-
const owner = props.model.owner()
|
|
60
|
-
return current === "idle" || !owner || owner === props.host ? current : "busy"
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
createEffect(() => {
|
|
64
|
-
props.host.dataset.phase = phase() === "busy" ? "idle" : phase()
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
return (
|
|
68
|
-
<>
|
|
69
|
-
<Switch>
|
|
70
|
-
<Match when={phase() === "recording"}>
|
|
71
|
-
<span data-slot="voice-input-dot" />
|
|
72
|
-
<span data-slot="voice-input-time">{props.model.elapsed()}</span>
|
|
73
|
-
</Match>
|
|
74
|
-
</Switch>
|
|
75
|
-
<button
|
|
76
|
-
type="button"
|
|
77
|
-
data-component="icon-button-v2"
|
|
78
|
-
data-size="normal"
|
|
79
|
-
data-variant="ghost-muted"
|
|
80
|
-
title={props.model.title()}
|
|
81
|
-
aria-label={props.model.title()}
|
|
82
|
-
aria-pressed={phase() === "recording"}
|
|
83
|
-
disabled={phase() === "busy" || phase() === "transcribing" || phase() === "starting"}
|
|
84
|
-
// Keep focus and the caret in the prompt, as the composer's own controls do.
|
|
85
|
-
onMouseDown={(event) => event.preventDefault()}
|
|
86
|
-
onClick={(event) => {
|
|
87
|
-
event.preventDefault()
|
|
88
|
-
event.stopPropagation()
|
|
89
|
-
props.model.onClick(props.host)
|
|
90
|
-
}}
|
|
91
|
-
>
|
|
92
|
-
<Switch fallback={<MicIcon />}>
|
|
93
|
-
<Match when={phase() === "recording"}>
|
|
94
|
-
<StopIcon />
|
|
95
|
-
</Match>
|
|
96
|
-
<Match when={phase() === "transcribing" || phase() === "starting"}>
|
|
97
|
-
<SpinnerIcon />
|
|
98
|
-
</Match>
|
|
99
|
-
</Switch>
|
|
100
|
-
</button>
|
|
101
|
-
</>
|
|
102
|
-
)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** Keeps a mic button before the send button of every composer in the window. Returns how many are mounted. */
|
|
106
|
-
export function injectComposerButtons(model: ButtonModel) {
|
|
107
|
-
const mounted = new Map<HTMLElement, () => void>()
|
|
108
|
-
const [count, setCount] = createSignal(0)
|
|
109
|
-
let frame = 0
|
|
110
|
-
|
|
111
|
-
const scan = () => {
|
|
112
|
-
frame = 0
|
|
113
|
-
for (const actions of document.querySelectorAll<HTMLElement>(ACTIONS)) {
|
|
114
|
-
const row = actions.parentElement
|
|
115
|
-
if (!row || row.querySelector(`:scope > [${HOST}]`)) continue
|
|
116
|
-
const host = document.createElement("div")
|
|
117
|
-
host.setAttribute(HOST, "")
|
|
118
|
-
row.insertBefore(host, actions)
|
|
119
|
-
mounted.set(
|
|
120
|
-
host,
|
|
121
|
-
render(() => <MicButton host={host} model={model} />, host),
|
|
122
|
-
)
|
|
123
|
-
}
|
|
124
|
-
for (const [host, dispose] of mounted) {
|
|
125
|
-
if (host.isConnected) continue
|
|
126
|
-
dispose()
|
|
127
|
-
mounted.delete(host)
|
|
128
|
-
}
|
|
129
|
-
setCount(mounted.size)
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const observer = new MutationObserver(() => {
|
|
133
|
-
if (!frame) frame = requestAnimationFrame(scan)
|
|
134
|
-
})
|
|
135
|
-
observer.observe(document.body, { childList: true, subtree: true })
|
|
136
|
-
scan()
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
count,
|
|
140
|
-
dispose() {
|
|
141
|
-
observer.disconnect()
|
|
142
|
-
if (frame) cancelAnimationFrame(frame)
|
|
143
|
-
for (const [host, dispose] of mounted) {
|
|
144
|
-
dispose()
|
|
145
|
-
host.remove()
|
|
146
|
-
}
|
|
147
|
-
mounted.clear()
|
|
148
|
-
},
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/** The composer element a mic host, or failing that the focused or first visible composer, belongs to. */
|
|
153
|
-
export function composerOf(host: HTMLElement | undefined) {
|
|
154
|
-
const from = host?.closest<HTMLElement>('[data-component="composer"]')
|
|
155
|
-
if (from?.isConnected) return from
|
|
156
|
-
const focused = document.activeElement?.closest<HTMLElement>('[data-component="composer"]')
|
|
157
|
-
if (focused) return focused
|
|
158
|
-
return [...document.querySelectorAll<HTMLElement>('[data-component="composer"]')].find((el) => el.offsetParent)
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/** Types text into a composer's editor through the browser's own input path, for composers the plugin API can't reach. */
|
|
162
|
-
export function typeInto(composer: HTMLElement, text: string) {
|
|
163
|
-
const editor = composer.querySelector<HTMLElement>('[data-component="composer-editor"]')
|
|
164
|
-
if (!editor) return false
|
|
165
|
-
editor.focus()
|
|
166
|
-
return document.execCommand("insertText", false, text)
|
|
167
|
-
}
|
package/src/main.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { systemPreferences } from "electron"
|
|
2
|
-
import type { Plugin } from "@opencode/plugin/gui"
|
|
3
|
-
import { Voice } from "./contract"
|
|
4
|
-
|
|
5
|
-
export const main: Plugin.MainSetup = (ctx) => {
|
|
6
|
-
ctx.provide(Voice, {
|
|
7
|
-
requestMicrophone: async () => {
|
|
8
|
-
if (process.platform !== "darwin") return true
|
|
9
|
-
if (systemPreferences.getMediaAccessStatus("microphone") === "granted") return true
|
|
10
|
-
return systemPreferences.askForMediaAccess("microphone")
|
|
11
|
-
},
|
|
12
|
-
})
|
|
13
|
-
}
|