@luxass/pi-voice 0.0.0 → 0.2.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.
- package/README.md +30 -0
- package/package.json +15 -4
- package/src/index.ts +121 -91
- package/src/profile.ts +49 -25
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# @luxass/pi-voice
|
|
2
|
+
|
|
3
|
+
Record speech and paste the transcription into [Pi](https://github.com/badlogic/pi-mono)'s composer.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pi install npm:@luxass/pi-voice
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Recording requires `sox` on your `PATH`. For local transcription, you also need `whisper-cli`; the local profile's model picker can download a Whisper model. Alternatively, add an API transcription profile; that does not require a local Whisper installation. Run `/voice doctor` to check your setup.
|
|
12
|
+
|
|
13
|
+
## Use
|
|
14
|
+
|
|
15
|
+
Press **Ctrl+Shift+V** or run `/voice` to start recording. Press it again to stop, transcribe, and paste the result into the composer. The extension does not send your message for you.
|
|
16
|
+
|
|
17
|
+
| Command | Purpose |
|
|
18
|
+
| ---------------------- | ----------------------------------------------- |
|
|
19
|
+
| `/voice` | Start or stop recording and transcribe on stop. |
|
|
20
|
+
| `/voice cancel` | Discard the current recording. |
|
|
21
|
+
| `/voice device` | Choose an input device or the system default. |
|
|
22
|
+
| `/voice profile add` | Create a local or API transcription profile. |
|
|
23
|
+
| `/voice profile` | Switch between saved profiles. |
|
|
24
|
+
| `/voice profile model` | Change the active profile's model. |
|
|
25
|
+
| `/voice status` | Show the current profile and input device. |
|
|
26
|
+
| `/voice doctor` | Check recording and transcription setup. |
|
|
27
|
+
|
|
28
|
+
Without a saved profile, transcription uses the first model in `~/.cache/whisper`. Set `AGENT_VOICE_MODEL_DIR` to keep models elsewhere; downloads and discovery both use it. For API transcription, `/voice profile add` asks for the endpoint, model, request format, and **name of an environment variable** holding the API key. Keep the key in your environment, not in the settings file.
|
|
29
|
+
|
|
30
|
+
Settings are saved to Pi's agent directory as `voice-settings.json`. Set `AGENT_VOICE_SETTINGS` to use a different settings file.
|
package/package.json
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@luxass/pi-voice",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Voice recording and transcription for Pi",
|
|
4
5
|
"keywords": [
|
|
5
6
|
"pi-package"
|
|
6
7
|
],
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/luxass/pi-extensions",
|
|
11
|
+
"directory": "packages/pi-voice"
|
|
12
|
+
},
|
|
7
13
|
"files": [
|
|
8
|
-
"src"
|
|
14
|
+
"src",
|
|
15
|
+
"README.md"
|
|
9
16
|
],
|
|
10
17
|
"type": "module",
|
|
11
18
|
"publishConfig": {
|
|
12
19
|
"access": "public"
|
|
13
20
|
},
|
|
14
21
|
"dependencies": {
|
|
15
|
-
"@luxass/agent-voice": "0.
|
|
22
|
+
"@luxass/agent-voice": "0.3.0"
|
|
16
23
|
},
|
|
17
24
|
"peerDependencies": {
|
|
18
25
|
"@earendil-works/pi-ai": "*",
|
|
@@ -25,6 +32,10 @@
|
|
|
25
32
|
]
|
|
26
33
|
},
|
|
27
34
|
"scripts": {
|
|
28
|
-
"
|
|
35
|
+
"lint": "oxlint --deny-warnings .",
|
|
36
|
+
"lint:fix": "oxlint --fix --deny-warnings .",
|
|
37
|
+
"fmt": "oxfmt .",
|
|
38
|
+
"fmt:check": "oxfmt --check .",
|
|
39
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
29
40
|
}
|
|
30
41
|
}
|
package/src/index.ts
CHANGED
|
@@ -11,11 +11,29 @@ import {
|
|
|
11
11
|
getActiveProfile,
|
|
12
12
|
listInputDevices,
|
|
13
13
|
loadVoiceSettings,
|
|
14
|
+
runDoctor,
|
|
14
15
|
saveVoiceSettings,
|
|
15
16
|
transcribe,
|
|
17
|
+
type DoctorCheckId,
|
|
16
18
|
} from "@luxass/agent-voice";
|
|
17
19
|
|
|
18
|
-
import { promptProfile } from "./profile";
|
|
20
|
+
import { pickLocalModel, promptProfile } from "./profile";
|
|
21
|
+
|
|
22
|
+
const SUBCOMMANDS = [
|
|
23
|
+
{ value: "record", description: "start/stop recording, transcribe on stop" },
|
|
24
|
+
{ value: "cancel", description: "discard the in-progress recording" },
|
|
25
|
+
{ value: "device", description: "select input device" },
|
|
26
|
+
{ value: "profile", description: "select transcription profile" },
|
|
27
|
+
{ value: "profile add", description: "create a transcription profile" },
|
|
28
|
+
{ value: "profile model", description: "change the active profile's model" },
|
|
29
|
+
{ value: "doctor", description: "check recording and transcription setup" },
|
|
30
|
+
{ value: "status", description: "show profile and input" },
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
const DOCTOR_FIXES: Partial<Record<DoctorCheckId, string>> = {
|
|
34
|
+
device: "run /voice device",
|
|
35
|
+
model: "run /voice profile model",
|
|
36
|
+
};
|
|
19
37
|
|
|
20
38
|
export default function voice(pi: ExtensionAPI) {
|
|
21
39
|
const path = process.env.AGENT_VOICE_SETTINGS ?? join(getAgentDir(), "voice-settings.json");
|
|
@@ -27,9 +45,7 @@ export default function voice(pi: ExtensionAPI) {
|
|
|
27
45
|
const model =
|
|
28
46
|
transcription.type === "api"
|
|
29
47
|
? transcription.model
|
|
30
|
-
: transcription.model
|
|
31
|
-
? "auto model"
|
|
32
|
-
: basename(transcription.model, ".bin");
|
|
48
|
+
: basename(transcription.model ?? "auto model", ".bin");
|
|
33
49
|
return `voice ${state} · ${name} · ${model} · ${settings.inputDevice?.name ?? "system default"}`;
|
|
34
50
|
}
|
|
35
51
|
|
|
@@ -71,100 +87,114 @@ export default function voice(pi: ExtensionAPI) {
|
|
|
71
87
|
handler: toggle,
|
|
72
88
|
});
|
|
73
89
|
|
|
74
|
-
pi.registerCommand("voice
|
|
75
|
-
description: "start/stop recording,
|
|
76
|
-
handler: (_args, ctx) => toggle(ctx),
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
pi.registerCommand("voice-cancel", {
|
|
80
|
-
description: "discard the in-progress recording",
|
|
81
|
-
// oxlint-disable-next-line require-await
|
|
82
|
-
handler: async (_args, ctx) => {
|
|
83
|
-
if (!recorder.isRecording) return;
|
|
84
|
-
recorder.cancel();
|
|
85
|
-
showStatus(ctx.ui);
|
|
86
|
-
ctx.ui.notify("Recording discarded");
|
|
87
|
-
},
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
pi.registerCommand("voice-device", {
|
|
91
|
-
description: "select input device",
|
|
92
|
-
handler: async (_args, ctx) => {
|
|
93
|
-
if (recorder.isRecording) {
|
|
94
|
-
ctx.ui.notify("Stop recording before changing the input", "warning");
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
const devices = await listInputDevices();
|
|
98
|
-
const selected = await ctx.ui.select("Voice input device", [
|
|
99
|
-
"System default",
|
|
100
|
-
...devices.map((device) => device.name),
|
|
101
|
-
]);
|
|
102
|
-
if (selected === undefined) return;
|
|
103
|
-
const device = devices.find((candidate) => candidate.name === selected);
|
|
104
|
-
if (device) settings.inputDevice = device;
|
|
105
|
-
else delete settings.inputDevice;
|
|
106
|
-
saveVoiceSettings(path, settings);
|
|
107
|
-
showStatus(ctx.ui);
|
|
108
|
-
ctx.ui.notify(`Voice input: ${selected}`);
|
|
109
|
-
},
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
pi.registerCommand("voice-profile", {
|
|
113
|
-
description: "select transcription profile, `add` one, or change the API `model`",
|
|
90
|
+
pi.registerCommand("voice", {
|
|
91
|
+
description: "start/stop recording, or `/voice <subcommand>`",
|
|
114
92
|
getArgumentCompletions: (prefix) => {
|
|
115
|
-
const items =
|
|
116
|
-
{ value
|
|
117
|
-
|
|
118
|
-
].filter((item) => item.value.startsWith(prefix));
|
|
93
|
+
const items = SUBCOMMANDS.filter(({ value }) => value.startsWith(prefix)).map(
|
|
94
|
+
({ value, description }) => ({ value, label: value, description }),
|
|
95
|
+
);
|
|
119
96
|
return items.length > 0 ? items : null;
|
|
120
97
|
},
|
|
121
98
|
handler: async (args, ctx) => {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
ctx.ui.notify(`${name} is a local profile; switch profiles to change models`, "warning");
|
|
99
|
+
switch (args.trim().replaceAll(/\s+/gu, " ")) {
|
|
100
|
+
case "":
|
|
101
|
+
case "record": {
|
|
102
|
+
await toggle(ctx);
|
|
127
103
|
return;
|
|
128
104
|
}
|
|
129
|
-
const model = (await ctx.ui.input(`Model for ${name}`, transcription.model))?.trim();
|
|
130
|
-
if (!model) return;
|
|
131
|
-
transcription.model = model;
|
|
132
|
-
saveVoiceSettings(path, settings);
|
|
133
|
-
showStatus(ctx.ui);
|
|
134
|
-
ctx.ui.notify(`Voice profile ${name}: ${model}`);
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
if (subcommand === "add") {
|
|
138
|
-
const added = await promptProfile(ctx.ui, Object.keys(settings.profiles ?? {}));
|
|
139
|
-
if (!added) return;
|
|
140
|
-
settings.profiles = { ...settings.profiles, [added.name]: added.profile };
|
|
141
|
-
settings.activeProfile = added.name;
|
|
142
|
-
saveVoiceSettings(path, settings);
|
|
143
|
-
showStatus(ctx.ui);
|
|
144
|
-
ctx.ui.notify(`Voice profile: ${added.name}`);
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
if (!settings.profiles) {
|
|
148
|
-
ctx.ui.notify("No profiles yet; run /voice-profile add", "warning");
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
const selected = await ctx.ui.select(
|
|
152
|
-
"Voice transcription profile",
|
|
153
|
-
Object.keys(settings.profiles),
|
|
154
|
-
);
|
|
155
|
-
if (selected === undefined) return;
|
|
156
|
-
settings.activeProfile = selected;
|
|
157
|
-
saveVoiceSettings(path, settings);
|
|
158
|
-
showStatus(ctx.ui);
|
|
159
|
-
ctx.ui.notify(`Voice profile: ${selected}`);
|
|
160
|
-
},
|
|
161
|
-
});
|
|
162
105
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
106
|
+
case "cancel": {
|
|
107
|
+
if (!recorder.isRecording) return;
|
|
108
|
+
recorder.cancel();
|
|
109
|
+
showStatus(ctx.ui);
|
|
110
|
+
ctx.ui.notify("Recording discarded");
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
case "device": {
|
|
115
|
+
if (recorder.isRecording) {
|
|
116
|
+
ctx.ui.notify("Stop recording before changing the input", "warning");
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const devices = await listInputDevices();
|
|
120
|
+
const selected = await ctx.ui.select("Voice input device", [
|
|
121
|
+
"System default",
|
|
122
|
+
...devices.map((device) => device.name),
|
|
123
|
+
]);
|
|
124
|
+
if (selected == null) return;
|
|
125
|
+
settings.inputDevice = devices.find((device) => device.name === selected);
|
|
126
|
+
saveVoiceSettings(path, settings);
|
|
127
|
+
showStatus(ctx.ui);
|
|
128
|
+
ctx.ui.notify(`Voice input: ${selected}`);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
case "profile": {
|
|
133
|
+
if (!settings.profiles) {
|
|
134
|
+
ctx.ui.notify("No profiles yet; run /voice profile add", "warning");
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const selected = await ctx.ui.select(
|
|
138
|
+
"Voice transcription profile",
|
|
139
|
+
Object.keys(settings.profiles),
|
|
140
|
+
);
|
|
141
|
+
if (selected == null) return;
|
|
142
|
+
settings.activeProfile = selected;
|
|
143
|
+
saveVoiceSettings(path, settings);
|
|
144
|
+
showStatus(ctx.ui);
|
|
145
|
+
ctx.ui.notify(`Voice profile: ${selected}`);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
case "profile add": {
|
|
150
|
+
const added = await promptProfile(ctx.ui, Object.keys(settings.profiles ?? {}));
|
|
151
|
+
if (!added) return;
|
|
152
|
+
settings.profiles = { ...settings.profiles, [added.name]: added.profile };
|
|
153
|
+
settings.activeProfile = added.name;
|
|
154
|
+
saveVoiceSettings(path, settings);
|
|
155
|
+
showStatus(ctx.ui);
|
|
156
|
+
ctx.ui.notify(`Voice profile: ${added.name}`);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
case "profile model": {
|
|
161
|
+
const { name, transcription } = getActiveProfile(settings);
|
|
162
|
+
const model =
|
|
163
|
+
transcription.type === "api"
|
|
164
|
+
? (await ctx.ui.input(`Model for ${name}`, transcription.model))?.trim()
|
|
165
|
+
: await pickLocalModel(ctx.ui);
|
|
166
|
+
if (!model) return;
|
|
167
|
+
transcription.model = model;
|
|
168
|
+
// Without saved profiles the active profile is implicit; save it to keep the model.
|
|
169
|
+
settings.profiles ??= { [name]: transcription };
|
|
170
|
+
settings.activeProfile = name;
|
|
171
|
+
saveVoiceSettings(path, settings);
|
|
172
|
+
showStatus(ctx.ui);
|
|
173
|
+
ctx.ui.notify(`Voice profile ${name}: ${model}`);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
case "doctor": {
|
|
178
|
+
const checks = await runDoctor(settings);
|
|
179
|
+
const lines = checks.map(({ id, ok, detail, fix }) =>
|
|
180
|
+
ok ? `✓ ${id}: ${detail}` : `✗ ${id}: ${detail} → ${DOCTOR_FIXES[id] ?? fix}`,
|
|
181
|
+
);
|
|
182
|
+
ctx.ui.notify(lines.join("\n"), checks.every(({ ok }) => ok) ? "info" : "warning");
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
case "status": {
|
|
187
|
+
ctx.ui.notify(statusText());
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
default: {
|
|
192
|
+
ctx.ui.notify(
|
|
193
|
+
`Usage: /voice [${SUBCOMMANDS.map(({ value }) => value).join(" | ")}]`,
|
|
194
|
+
"warning",
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
168
198
|
},
|
|
169
199
|
});
|
|
170
200
|
|
package/src/profile.ts
CHANGED
|
@@ -1,25 +1,46 @@
|
|
|
1
|
-
import { basename } from "node:path";
|
|
2
|
-
|
|
3
1
|
import type { ExtensionUIContext } from "@earendil-works/pi-coding-agent";
|
|
4
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
downloadModel,
|
|
4
|
+
listLocalModels,
|
|
5
|
+
type LocalModel,
|
|
6
|
+
type TranscriptionProfile,
|
|
7
|
+
} from "@luxass/agent-voice";
|
|
8
|
+
|
|
9
|
+
const label = ({ name, installed, approxMB }: LocalModel) =>
|
|
10
|
+
installed ? name : `Download ${name} (${approxMB} MB)`;
|
|
11
|
+
|
|
12
|
+
function download(ui: ExtensionUIContext, name: string): Promise<string> {
|
|
13
|
+
let shown = -1;
|
|
14
|
+
return downloadModel(name, {
|
|
15
|
+
// Without a Content-Length, treat what arrived as the whole file.
|
|
16
|
+
onProgress: (received, total = received) => {
|
|
17
|
+
const percent = Math.floor((received / total) * 100);
|
|
18
|
+
if (percent === shown) return;
|
|
19
|
+
shown = percent;
|
|
20
|
+
ui.setStatus("voice-download", `downloading ${name}… ${percent}%`);
|
|
21
|
+
},
|
|
22
|
+
}).finally(() => {
|
|
23
|
+
ui.setStatus("voice-download", undefined);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
5
26
|
|
|
6
|
-
|
|
27
|
+
/** Pick an installed model, or one from the curated list, which is downloaded right away. */
|
|
28
|
+
export async function pickLocalModel(ui: ExtensionUIContext): Promise<string | undefined> {
|
|
29
|
+
const models = listLocalModels();
|
|
30
|
+
const picked = await ui.select(
|
|
31
|
+
"Whisper model",
|
|
32
|
+
models.map((model) => label(model)),
|
|
33
|
+
);
|
|
34
|
+
const model = models.find((candidate) => label(candidate) === picked);
|
|
35
|
+
return model == null || model.installed ? model?.path : download(ui, model.name);
|
|
36
|
+
}
|
|
7
37
|
|
|
8
38
|
async function promptLocal(ui: ExtensionUIContext): Promise<TranscriptionProfile | undefined> {
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
AUTO_MODEL,
|
|
12
|
-
...models.map((model) => basename(model)),
|
|
13
|
-
]);
|
|
14
|
-
if (picked === undefined) return undefined;
|
|
39
|
+
const model = await pickLocalModel(ui);
|
|
40
|
+
if (model == null) return undefined;
|
|
15
41
|
const language = await ui.input("Language code (blank for auto)", "en");
|
|
16
|
-
if (language
|
|
17
|
-
|
|
18
|
-
const profile: TranscriptionProfile = { type: "local" };
|
|
19
|
-
const model = models.find((candidate) => basename(candidate) === picked);
|
|
20
|
-
if (model) profile.model = model;
|
|
21
|
-
if (language.trim()) profile.language = language.trim();
|
|
22
|
-
return profile;
|
|
42
|
+
if (language == null) return undefined;
|
|
43
|
+
return { type: "local", model, language: language.trim() || undefined };
|
|
23
44
|
}
|
|
24
45
|
|
|
25
46
|
async function promptApi(ui: ExtensionUIContext): Promise<TranscriptionProfile | undefined> {
|
|
@@ -31,13 +52,16 @@ async function promptApi(ui: ExtensionUIContext): Promise<TranscriptionProfile |
|
|
|
31
52
|
"API key environment variable (blank for none)",
|
|
32
53
|
"OPENAI_API_KEY",
|
|
33
54
|
);
|
|
34
|
-
if (apiKeyEnv
|
|
55
|
+
if (apiKeyEnv == null) return undefined;
|
|
35
56
|
const format = await ui.select("Request format", ["multipart", "openrouter"]);
|
|
36
|
-
if (format
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
57
|
+
if (format == null) return undefined;
|
|
58
|
+
return {
|
|
59
|
+
type: "api",
|
|
60
|
+
endpoint,
|
|
61
|
+
model,
|
|
62
|
+
format: format === "openrouter" ? "openrouter" : "multipart",
|
|
63
|
+
apiKeyEnv: apiKeyEnv.trim() || undefined,
|
|
64
|
+
};
|
|
41
65
|
}
|
|
42
66
|
|
|
43
67
|
/** Ask for a profile name and its transcription settings. Returns undefined when the user backs out. */
|
|
@@ -51,7 +75,7 @@ export async function promptProfile(
|
|
|
51
75
|
return undefined;
|
|
52
76
|
|
|
53
77
|
const type = await ui.select("Transcription", ["local", "api"]);
|
|
54
|
-
|
|
55
|
-
|
|
78
|
+
if (type == null) return undefined;
|
|
79
|
+
const profile = type === "local" ? await promptLocal(ui) : await promptApi(ui);
|
|
56
80
|
return profile && { name, profile };
|
|
57
81
|
}
|