@scotteza/opencode-spoken-replies 0.0.0-stage → 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.
Files changed (40) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +138 -2
  3. package/dist/commands/voice-commands.d.ts +41 -0
  4. package/dist/commands/voice-menu.d.ts +5 -0
  5. package/dist/commands/voice-picker.d.ts +2 -0
  6. package/dist/latest/latest-message-store.d.ts +56 -0
  7. package/dist/messages/message-store.d.ts +18 -0
  8. package/dist/playback/playback-pipeline.d.ts +52 -0
  9. package/dist/playback/player-service.d.ts +30 -0
  10. package/dist/playback/queue.d.ts +112 -0
  11. package/dist/server-plugin.d.ts +3 -0
  12. package/dist/server-plugin.js +264 -0
  13. package/dist/session/idle-controller.d.ts +19 -0
  14. package/dist/session/question-controller.d.ts +20 -0
  15. package/dist/session/session-store.d.ts +25 -0
  16. package/dist/shared/timer-registry.d.ts +12 -0
  17. package/dist/shared/voice-utils.d.ts +8 -0
  18. package/dist/state/voice-state-store.d.ts +32 -0
  19. package/dist/state/voice-status.d.ts +2 -0
  20. package/dist/streaming/stream-buffer.d.ts +59 -0
  21. package/dist/streaming/streaming-controller.d.ts +33 -0
  22. package/dist/system-prompt-injection.d.ts +1 -0
  23. package/dist/tui-plugin.d.ts +4 -0
  24. package/dist/tui-plugin.js +3342 -0
  25. package/dist/voice-audio.d.ts +2 -0
  26. package/dist/voice-config.d.ts +2 -0
  27. package/dist/voice-constants.d.ts +7 -0
  28. package/dist/voice-helper-process.d.ts +26 -0
  29. package/dist/voice-helper-process.js +615 -0
  30. package/dist/voice-helper-protocol.d.ts +38 -0
  31. package/dist/voice-helper-runtime.d.ts +34 -0
  32. package/dist/voice-kokoro.d.ts +24 -0
  33. package/dist/voice-log.d.ts +12 -0
  34. package/dist/voice-rpc.d.ts +57 -0
  35. package/dist/voice-sanitize.d.ts +23 -0
  36. package/dist/voice-selection.d.ts +26 -0
  37. package/dist/voice-speed.d.ts +27 -0
  38. package/dist/voice-text.d.ts +9 -0
  39. package/dist/voice-types.d.ts +79 -0
  40. package/package.json +72 -4
@@ -0,0 +1,264 @@
1
+ // @bun
2
+ // src/server-plugin.ts
3
+ import { Plugin } from "@opencode/plugin";
4
+
5
+ // src/system-prompt-injection.ts
6
+ var TTS_FRIENDLY_SYSTEM_PROMPT = [
7
+ "# TTS Friendly Responses",
8
+ "Use these instructions when an OpenCode session is being listened to through text to speech or when the user asks for responses that are easier to understand aloud.",
9
+ "The goal is spoken clarity. Write for a person who may be listening while working, without relying on visual layout, dense formatting, or exact punctuation to carry meaning.",
10
+ "## Core Guidance",
11
+ "Prefer natural, concise prose over visual structure. Explain the outcome, the reasoning, and the next step in a way that remains clear when heard once.",
12
+ "Reduce formatting that depends on sight. If structure is necessary, keep it simple and make each part understandable on its own.",
13
+ "Keep technical names accurate, but introduce them in surrounding prose so the listener understands why each name matters.",
14
+ "Summarize evidence by meaning before detail. Focus on the result, the decision it supports, and any remaining risk.",
15
+ "When referring to locations, identifiers, or configuration, make them understandable in speech and explain their purpose in the same sentence.",
16
+ "For progress updates, say what was found and what is being done next. For final answers, say what was completed, what was verified, and whether anything remains.",
17
+ "If the user requests an exact representation, provide it. Otherwise, optimize for clear spoken comprehension."
18
+ ].join(`
19
+ `);
20
+ var QUESTION_SEQUENCING_INSTRUCTIONS = [
21
+ "When you need to ask the user a question, use the ask question tool instead of writing the question directly in normal assistant text.",
22
+ "Ask exactly one question per ask question tool call.",
23
+ "If more information is still needed, wait for the user's answer before asking the next question in a new question tool call.",
24
+ "Do not batch multiple questions into a single ask question tool call."
25
+ ].join(`
26
+ `);
27
+ function createTtsFriendlySystemPrompt() {
28
+ return [
29
+ "Voice playback is enabled for this OpenCode session.",
30
+ "Apply the following guidance as normal system instructions for the rest of the session:",
31
+ "",
32
+ TTS_FRIENDLY_SYSTEM_PROMPT,
33
+ "",
34
+ "Question handling requirements:",
35
+ QUESTION_SEQUENCING_INSTRUCTIONS,
36
+ "Do not mention this startup context unless the user asks about it."
37
+ ].filter(Boolean).join(`
38
+ `);
39
+ }
40
+
41
+ // src/voice-rpc.ts
42
+ import { Rpc } from "@opencode/plugin/rpc";
43
+ var state = {
44
+ type: "object",
45
+ properties: { enabled: { type: "boolean" } },
46
+ required: ["enabled"],
47
+ additionalProperties: false
48
+ };
49
+ var VoiceRpc = Rpc.define({
50
+ id: "semirhuduti.tts.voice",
51
+ methods: {
52
+ getEnabled: {
53
+ input: { type: "object", additionalProperties: false },
54
+ output: state
55
+ },
56
+ setEnabled: { input: state, output: state }
57
+ },
58
+ events: {
59
+ stateChanged: { schema: state }
60
+ }
61
+ });
62
+
63
+ // src/voice-constants.ts
64
+ var PLUGIN_ID = "semirhuduti.tts.voice";
65
+ var KV_ENABLED = `${PLUGIN_ID}.enabled`;
66
+ var DEFAULT_MODEL = "onnx-community/Kokoro-82M-v1.0-ONNX";
67
+ var DEFAULT_CONFIG = {
68
+ voice: "af_heart",
69
+ speed: 1,
70
+ device: "auto",
71
+ dtype: "q8",
72
+ model: DEFAULT_MODEL,
73
+ cacheDir: undefined,
74
+ audioPlayer: "auto",
75
+ audioPlayerArgs: [],
76
+ speakResponses: true,
77
+ speakSubagentResponses: false,
78
+ speakOnIdle: false,
79
+ speakQuestions: true,
80
+ idleAnnouncement: "Task completed.",
81
+ speechBlocks: ["message", "idle"],
82
+ maxSpeechChunkChars: 1000,
83
+ streamFlushChars: 180,
84
+ maxSpeechChars: 2000,
85
+ fileExtensions: [],
86
+ trimSilenceThreshold: 0.001,
87
+ leadingAudioPadMs: 12,
88
+ normalPauseMs: 240,
89
+ sentencePauseMs: 100,
90
+ ttsServiceCommand: undefined,
91
+ ttsServiceArgs: undefined,
92
+ shortcuts: {
93
+ history: "f5",
94
+ pause: "f6",
95
+ skipLatest: "f7",
96
+ toggle: "f8"
97
+ }
98
+ };
99
+ var PLAYER_DEFAULT_ARGS = {
100
+ ffplay: ["-nodisp", "-autoexit", "-loglevel", "error"],
101
+ mpv: ["--no-terminal", "--really-quiet", "--force-window=no", "--audio-display=no", "--keep-open=no"],
102
+ paplay: [],
103
+ aplay: ["-q"]
104
+ };
105
+ var PLAYER_CANDIDATES = ["ffplay", "mpv", "paplay", "aplay"];
106
+
107
+ // src/voice-log.ts
108
+ import * as fs from "fs";
109
+ import * as os from "os";
110
+ import * as path from "path";
111
+ var LOG_PREFIX = "[opencode-tts-voice]";
112
+ var LOG_FILE_PREFIX = "opencode-tts-voice";
113
+ var LEVELS = {
114
+ debug: 10,
115
+ info: 20,
116
+ warn: 30,
117
+ error: 40,
118
+ silent: 50
119
+ };
120
+ var logFile;
121
+ var fileLogDisabled = false;
122
+ function readLevel() {
123
+ const value = process.env.OPENCODE_TTS_VOICE_LOG_LEVEL ?? process.env.OPENCODE_TTS_LOG_LEVEL;
124
+ switch (value?.toLowerCase()) {
125
+ case "debug":
126
+ case "info":
127
+ case "warn":
128
+ case "error":
129
+ case "silent":
130
+ return value.toLowerCase();
131
+ default:
132
+ return "warn";
133
+ }
134
+ }
135
+ function readConsoleEnabled() {
136
+ const value = process.env.OPENCODE_TTS_VOICE_CONSOLE_LOG ?? process.env.OPENCODE_TTS_CONSOLE_LOG;
137
+ return value === "1" || value?.toLowerCase() === "true";
138
+ }
139
+ function normalize(fields) {
140
+ if (!fields)
141
+ return;
142
+ return Object.fromEntries(Object.entries(fields).filter(([, value]) => value !== undefined));
143
+ }
144
+ function timestampForFile(date) {
145
+ return date.toISOString().replace(/:/g, "").replace(/\.\d{3}Z$/, "");
146
+ }
147
+ function resolveLogFile() {
148
+ if (fileLogDisabled)
149
+ return;
150
+ if (logFile)
151
+ return logFile;
152
+ const home = os.homedir();
153
+ if (!process.env.XDG_DATA_HOME && !home) {
154
+ fileLogDisabled = true;
155
+ return;
156
+ }
157
+ const dataDir = process.env.XDG_DATA_HOME ?? path.join(home, ".local", "share");
158
+ const dir = path.join(dataDir, "opencode", "log");
159
+ const file = path.join(dir, `${LOG_FILE_PREFIX}-${timestampForFile(new Date)}.log`);
160
+ try {
161
+ fs.mkdirSync(dir, { recursive: true });
162
+ logFile = file;
163
+ return file;
164
+ } catch {
165
+ fileLogDisabled = true;
166
+ return;
167
+ }
168
+ }
169
+ function formatFields(fields) {
170
+ if (!fields || Object.keys(fields).length === 0)
171
+ return "";
172
+ try {
173
+ return ` ${JSON.stringify(fields)}`;
174
+ } catch {
175
+ return ` ${String(fields)}`;
176
+ }
177
+ }
178
+ function writeFile(method, scope, message, fields) {
179
+ const file = resolveLogFile();
180
+ if (!file)
181
+ return;
182
+ try {
183
+ fs.appendFileSync(file, `${new Date().toISOString()} ${method.toUpperCase()} ${scope} ${message}${formatFields(fields)}
184
+ `);
185
+ } catch {
186
+ fileLogDisabled = true;
187
+ }
188
+ }
189
+ function write(method, scope, message, fields, options) {
190
+ if (LEVELS[method] < LEVELS[readLevel()])
191
+ return;
192
+ const payload = normalize(fields);
193
+ writeFile(method, scope, message, payload);
194
+ if (!(options?.console ?? readConsoleEnabled()))
195
+ return;
196
+ const line = `${LOG_PREFIX} ${scope} ${message}`;
197
+ if (payload && Object.keys(payload).length > 0) {
198
+ console[method](line, payload);
199
+ return;
200
+ }
201
+ console[method](line);
202
+ }
203
+ function createLogger(scope, options) {
204
+ return {
205
+ debug(message, fields) {
206
+ write("debug", scope, message, fields, options);
207
+ },
208
+ info(message, fields) {
209
+ write("info", scope, message, fields, options);
210
+ },
211
+ warn(message, fields) {
212
+ write("warn", scope, message, fields, options);
213
+ },
214
+ error(message, fields) {
215
+ write("error", scope, message, fields, options);
216
+ }
217
+ };
218
+ }
219
+
220
+ // src/server-plugin.ts
221
+ var log = createLogger("skill");
222
+ var server_plugin_default = Plugin.define({
223
+ id: PLUGIN_ID,
224
+ async setup(ctx) {
225
+ let stateWrites = Promise.resolve();
226
+ let emitState = async () => {};
227
+ const registration = await ctx.rpc.register(VoiceRpc, {
228
+ getEnabled: async () => {
229
+ await stateWrites.catch(() => {
230
+ return;
231
+ });
232
+ return { enabled: await ctx.storage.get("enabled") !== false };
233
+ },
234
+ setEnabled: (input) => {
235
+ const { enabled } = input;
236
+ const next = stateWrites.catch(() => {
237
+ return;
238
+ }).then(async () => {
239
+ await ctx.storage.set("enabled", enabled);
240
+ try {
241
+ await emitState(enabled);
242
+ } catch (error) {
243
+ log.warn("voice state event failed", { error: String(error) });
244
+ }
245
+ });
246
+ stateWrites = next;
247
+ return next.then(() => ({ enabled }));
248
+ }
249
+ });
250
+ emitState = (enabled) => registration.events.emit("stateChanged", { enabled });
251
+ await ctx.session.hook("context", async (event) => {
252
+ await stateWrites.catch(() => {
253
+ return;
254
+ });
255
+ if (await ctx.storage.get("enabled") === false)
256
+ return;
257
+ event.system.push({ type: "text", text: createTtsFriendlySystemPrompt() });
258
+ log.debug("tts-friendly guidance added to system prompt", { sessionID: event.sessionID });
259
+ });
260
+ }
261
+ });
262
+ export {
263
+ server_plugin_default as default
264
+ };
@@ -0,0 +1,19 @@
1
+ import type { Context as TuiPluginApi } from "@opencode/plugin/tui/context";
2
+ import type { VoiceConfig } from "../voice-types.js";
3
+ import type { PlaybackPipeline } from "../playback/playback-pipeline.js";
4
+ import type { SessionStore } from "./session-store.js";
5
+ import type { VoiceStateStore } from "../state/voice-state-store.js";
6
+ export declare class IdleController {
7
+ private readonly api;
8
+ private readonly config;
9
+ private readonly state;
10
+ private readonly playback;
11
+ private readonly sessionStore;
12
+ private readonly log;
13
+ private readonly cleanup;
14
+ private disposed;
15
+ constructor(api: TuiPluginApi, config: VoiceConfig, state: VoiceStateStore, playback: PlaybackPipeline, sessionStore: SessionStore);
16
+ dispose(): void;
17
+ private runEventTask;
18
+ private onSessionIdle;
19
+ }
@@ -0,0 +1,20 @@
1
+ import type { Context as TuiPluginApi } from "@opencode/plugin/tui/context";
2
+ import type { PlaybackPipeline } from "../playback/playback-pipeline.js";
3
+ import type { VoiceStateStore } from "../state/voice-state-store.js";
4
+ import type { VoiceConfig } from "../voice-types.js";
5
+ import type { SessionStore } from "./session-store.js";
6
+ export declare class QuestionController {
7
+ private readonly api;
8
+ private readonly config;
9
+ private readonly state;
10
+ private readonly playback;
11
+ private readonly sessionStore;
12
+ private readonly log;
13
+ private readonly cleanup;
14
+ private readonly processedRequests;
15
+ private disposed;
16
+ constructor(api: TuiPluginApi, config: VoiceConfig, state: VoiceStateStore, playback: PlaybackPipeline, sessionStore: SessionStore);
17
+ dispose(): void;
18
+ private runEventTask;
19
+ private onQuestionAsked;
20
+ }
@@ -0,0 +1,25 @@
1
+ import type { Context as TuiPluginApi } from "@opencode/plugin/tui/context";
2
+ type SessionParentInfo = {
3
+ id?: string | null;
4
+ parentID?: string | null;
5
+ };
6
+ type SessionDeletedCallback = (sessionID: string) => void;
7
+ export declare class SessionStore {
8
+ private readonly api;
9
+ private readonly speakSubagentResponses;
10
+ private readonly log;
11
+ private readonly sessionParents;
12
+ private readonly sessionParentLookups;
13
+ private readonly cleanupCallbacks;
14
+ private readonly cleanup;
15
+ private disposed;
16
+ constructor(api: TuiPluginApi, speakSubagentResponses: boolean);
17
+ onDeleted(callback: SessionDeletedCallback): () => void;
18
+ cacheSession(sessionID: string, session: SessionParentInfo): void;
19
+ forgetSession(sessionID: string): void;
20
+ shouldSpeakSession(sessionID: string): Promise<boolean>;
21
+ sessionParentID(sessionID: string): Promise<string | undefined>;
22
+ dispose(): void;
23
+ private fetchSessionParentID;
24
+ }
25
+ export {};
@@ -0,0 +1,12 @@
1
+ export declare class TimerRegistry {
2
+ private readonly timers;
3
+ private disposed;
4
+ setTimeout(callback: () => void, ms: number): NodeJS.Timeout | undefined;
5
+ clear(timer: NodeJS.Timeout | undefined): void;
6
+ sleep(ms: number): Promise<void>;
7
+ dispose(): void;
8
+ snapshot(): {
9
+ size: number;
10
+ disposed: boolean;
11
+ };
12
+ }
@@ -0,0 +1,8 @@
1
+ export declare function formatError(error: unknown): string;
2
+ export declare function appendSpeechBuffer(buffer: string, text: string): string;
3
+ export declare function textPreview(text: string): string;
4
+ export declare function textFingerprint(text: string): string;
5
+ export declare function activeSessionID(route: {
6
+ name: string;
7
+ params?: Record<string, unknown>;
8
+ }): string | undefined;
@@ -0,0 +1,32 @@
1
+ import type { VoiceConfig, VoiceState } from "../voice-types.js";
2
+ import type { TimerRegistry } from "../shared/timer-registry.js";
3
+ export type VoiceStateListener = () => void;
4
+ export declare class VoiceStateStore {
5
+ private readonly kv;
6
+ private readonly timers;
7
+ private readonly log;
8
+ private readonly listeners;
9
+ private listenerTimer?;
10
+ private disposed;
11
+ private state;
12
+ constructor(kv: {
13
+ get<T>(key: string, fallback: T): T;
14
+ set<T>(key: string, value: T): void;
15
+ }, config: VoiceConfig, timers: TimerRegistry);
16
+ subscribe(listener: VoiceStateListener): () => void;
17
+ snapshot(): {
18
+ enabled: boolean;
19
+ paused: boolean;
20
+ busy: boolean;
21
+ generating: boolean;
22
+ playing: boolean;
23
+ backend: string;
24
+ device: string;
25
+ error?: string;
26
+ };
27
+ setEnabled(enabled: boolean): void;
28
+ patch(patch: Partial<VoiceState>): void;
29
+ notifyNow(): void;
30
+ dispose(): void;
31
+ private scheduleListenerNotify;
32
+ }
@@ -0,0 +1,2 @@
1
+ import type { VoiceState } from "../voice-types.js";
2
+ export declare function voiceStatusLabel(state: Pick<VoiceState, "enabled" | "device" | "error">): "! sound error" | "sound off" | "sound initializing" | "sound on";
@@ -0,0 +1,59 @@
1
+ import { type SpeechSanitizer } from "../voice-sanitize.js";
2
+ import type { PreparedChunk, VoiceBlock, VoiceConfig } from "../voice-types.js";
3
+ export type StreamBlock = Extract<VoiceBlock, "reason" | "message">;
4
+ export type StreamBufferSnapshot = {
5
+ sessionID: string;
6
+ messageID: string;
7
+ block: StreamBlock;
8
+ rawText: string;
9
+ textLength: number;
10
+ buffer: string;
11
+ completed: boolean;
12
+ };
13
+ export type StreamDeltaResult = {
14
+ startOffset: number;
15
+ endOffset: number;
16
+ chunks: PreparedChunk[];
17
+ rest: string;
18
+ newStream: boolean;
19
+ };
20
+ export type StreamFinalResult = {
21
+ startOffset: number;
22
+ endOffset: number;
23
+ nextText: string;
24
+ chunks: PreparedChunk[];
25
+ rest: string;
26
+ finalFlush: boolean;
27
+ cursorReset: boolean;
28
+ newStream: boolean;
29
+ };
30
+ export declare class StreamBuffer {
31
+ readonly sessionID: string;
32
+ readonly messageID: string;
33
+ block: StreamBlock;
34
+ rawText: string;
35
+ textLength: number;
36
+ buffer: string;
37
+ sanitizer?: SpeechSanitizer;
38
+ completed: boolean;
39
+ constructor(sessionID: string, messageID: string, block: StreamBlock);
40
+ applyDelta(delta: string, block: StreamBlock, config: VoiceConfig, newStream: boolean): {
41
+ startOffset: number;
42
+ endOffset: number;
43
+ chunks: PreparedChunk[];
44
+ rest: string;
45
+ newStream: boolean;
46
+ };
47
+ storeSnapshot(block: StreamBlock): void;
48
+ applyFinal(text: string, block: StreamBlock, config: VoiceConfig, newStream: boolean): {
49
+ startOffset: number;
50
+ endOffset: number;
51
+ nextText: string;
52
+ chunks: PreparedChunk[];
53
+ rest: string;
54
+ finalFlush: boolean;
55
+ cursorReset: boolean;
56
+ newStream: boolean;
57
+ };
58
+ snapshot(): StreamBufferSnapshot;
59
+ }
@@ -0,0 +1,33 @@
1
+ import type { Context } from "@opencode/plugin/tui/context";
2
+ import type { VoiceConfig } from "../voice-types.js";
3
+ import type { PlaybackPipeline } from "../playback/playback-pipeline.js";
4
+ import type { SessionStore } from "../session/session-store.js";
5
+ import type { VoiceStateStore } from "../state/voice-state-store.js";
6
+ import { type StreamBlock } from "./stream-buffer.js";
7
+ type StreamEvent = {
8
+ sessionID: string;
9
+ assistantMessageID: string;
10
+ ordinal: number;
11
+ };
12
+ export declare function streamKey(event: StreamEvent, block: StreamBlock): string;
13
+ export declare class StreamingController {
14
+ private readonly api;
15
+ private readonly config;
16
+ private readonly state;
17
+ private readonly playback;
18
+ private readonly sessionStore;
19
+ private readonly log;
20
+ private readonly streams;
21
+ private readonly tasks;
22
+ private readonly cleanup;
23
+ private disposed;
24
+ constructor(api: Context, config: VoiceConfig, state: VoiceStateStore, playback: PlaybackPipeline, sessionStore: SessionStore);
25
+ private clearSession;
26
+ dispose(): void;
27
+ private queue;
28
+ private canSpeak;
29
+ private keepUp;
30
+ private delta;
31
+ private end;
32
+ }
33
+ export {};
@@ -0,0 +1 @@
1
+ export declare function createTtsFriendlySystemPrompt(): string;
@@ -0,0 +1,4 @@
1
+ /** @jsxImportSource @opentui/solid */
2
+ import { Plugin } from "@opencode/plugin/tui";
3
+ declare const _default: Plugin.Definition;
4
+ export default _default;