@reins-ai/voice 0.0.0-stage → 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 (67) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +81 -2
  3. package/dist/barge-in.d.ts +42 -0
  4. package/dist/barge-in.d.ts.map +1 -0
  5. package/dist/barge-in.js +78 -0
  6. package/dist/barge-in.js.map +1 -0
  7. package/dist/cli.d.ts +3 -0
  8. package/dist/cli.d.ts.map +1 -0
  9. package/dist/cli.js +100 -0
  10. package/dist/cli.js.map +1 -0
  11. package/dist/config.d.ts +32 -0
  12. package/dist/config.d.ts.map +1 -0
  13. package/dist/config.js +54 -0
  14. package/dist/config.js.map +1 -0
  15. package/dist/download.d.ts +18 -0
  16. package/dist/download.d.ts.map +1 -0
  17. package/dist/download.js +37 -0
  18. package/dist/download.js.map +1 -0
  19. package/dist/ear.d.ts +158 -0
  20. package/dist/ear.d.ts.map +1 -0
  21. package/dist/ear.js +387 -0
  22. package/dist/ear.js.map +1 -0
  23. package/dist/log.d.ts +6 -0
  24. package/dist/log.d.ts.map +1 -0
  25. package/dist/log.js +13 -0
  26. package/dist/log.js.map +1 -0
  27. package/dist/mic.d.ts +19 -0
  28. package/dist/mic.d.ts.map +1 -0
  29. package/dist/mic.js +43 -0
  30. package/dist/mic.js.map +1 -0
  31. package/dist/piper.d.ts +38 -0
  32. package/dist/piper.d.ts.map +1 -0
  33. package/dist/piper.js +90 -0
  34. package/dist/piper.js.map +1 -0
  35. package/dist/server.d.ts +21 -0
  36. package/dist/server.d.ts.map +1 -0
  37. package/dist/server.js +92 -0
  38. package/dist/server.js.map +1 -0
  39. package/dist/setup.d.ts +15 -0
  40. package/dist/setup.d.ts.map +1 -0
  41. package/dist/setup.js +73 -0
  42. package/dist/setup.js.map +1 -0
  43. package/dist/speak.d.ts +74 -0
  44. package/dist/speak.d.ts.map +1 -0
  45. package/dist/speak.js +188 -0
  46. package/dist/speak.js.map +1 -0
  47. package/dist/speech-check.d.ts +27 -0
  48. package/dist/speech-check.d.ts.map +1 -0
  49. package/dist/speech-check.js +51 -0
  50. package/dist/speech-check.js.map +1 -0
  51. package/dist/stdout-guard.d.ts +2 -0
  52. package/dist/stdout-guard.d.ts.map +1 -0
  53. package/dist/stdout-guard.js +8 -0
  54. package/dist/stdout-guard.js.map +1 -0
  55. package/dist/stt.d.ts +31 -0
  56. package/dist/stt.d.ts.map +1 -0
  57. package/dist/stt.js +86 -0
  58. package/dist/stt.js.map +1 -0
  59. package/dist/turn.d.ts +28 -0
  60. package/dist/turn.d.ts.map +1 -0
  61. package/dist/turn.js +60 -0
  62. package/dist/turn.js.map +1 -0
  63. package/dist/vad.d.ts +53 -0
  64. package/dist/vad.d.ts.map +1 -0
  65. package/dist/vad.js +120 -0
  66. package/dist/vad.js.map +1 -0
  67. package/package.json +31 -4
package/dist/mic.js ADDED
@@ -0,0 +1,43 @@
1
+ import { spawn, spawnSync } from 'node:child_process';
2
+ /** The sample rate Whisper expects; the microphone is recorded at it directly. */
3
+ export const SAMPLE_RATE = 16_000;
4
+ /** How ffmpeg opens the microphone on each operating system. */
5
+ export function inputArgs(platform, device) {
6
+ if (platform === 'win32')
7
+ return ['-f', 'dshow', '-i', `audio=${device}`];
8
+ if (platform === 'darwin')
9
+ return ['-f', 'avfoundation', '-i', `:${device ?? 'default'}`];
10
+ return ['-f', 'pulse', '-i', device ?? 'default'];
11
+ }
12
+ /** On Windows the microphone must be named: the first one ffmpeg lists. */
13
+ export function findWindowsMic(ffmpeg) {
14
+ const { stderr } = spawnSync(ffmpeg, ['-hide_banner', '-list_devices', 'true', '-f', 'dshow', '-i', 'dummy'], {
15
+ encoding: 'utf8',
16
+ windowsHide: true,
17
+ });
18
+ return /"([^"]+)" \(audio\)/.exec(stderr ?? '')?.[1];
19
+ }
20
+ /** The computer microphone through ffmpeg. It is only open while listening. */
21
+ export function createFfmpegMic({ ffmpeg, device }) {
22
+ return () => {
23
+ const child = spawn(ffmpeg, ['-hide_banner', '-loglevel', 'error', ...inputArgs(process.platform, device), '-ac', '1', '-ar', String(SAMPLE_RATE), '-f', 's16le', '-'], { stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true });
24
+ let stderr = '';
25
+ child.stderr.on('data', (chunk) => (stderr += chunk));
26
+ let stopped = false;
27
+ return {
28
+ onData: (listener) => child.stdout.on('data', listener),
29
+ onExit: (listener) => {
30
+ child.on('error', (error) => listener(`No puedo arrancar ffmpeg (${ffmpeg}): ${error.message}`));
31
+ child.on('close', () => {
32
+ if (!stopped)
33
+ listener(stderr.trim() || 'The microphone closed');
34
+ });
35
+ },
36
+ stop: () => {
37
+ stopped = true;
38
+ child.kill();
39
+ },
40
+ };
41
+ };
42
+ }
43
+ //# sourceMappingURL=mic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mic.js","sourceRoot":"","sources":["../src/mic.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAEtD,kFAAkF;AAClF,MAAM,CAAC,MAAM,WAAW,GAAG,MAAM,CAAC;AAWlC,gEAAgE;AAChE,MAAM,UAAU,SAAS,CAAC,QAAyB,EAAE,MAA0B;IAC7E,IAAI,QAAQ,KAAK,OAAO;QAAE,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC,CAAC;IAC1E,IAAI,QAAQ,KAAK,QAAQ;QAAE,OAAO,CAAC,IAAI,EAAE,cAAc,EAAE,IAAI,EAAE,IAAI,MAAM,IAAI,SAAS,EAAE,CAAC,CAAC;IAC1F,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,SAAS,CAAC,CAAC;AACpD,CAAC;AAED,2EAA2E;AAC3E,MAAM,UAAU,cAAc,CAAC,MAAc;IAC3C,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC,cAAc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BACjG,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;oBACrB,IAAI,CAAC,OAAO;wBAAE,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,uBAAuB,CAAC,CAAC;gBACnE,CAAC,CAAC,CAAC;YACL,CAAC;YACD,IAAI,EAAE,GAAG,EAAE;gBACT,OAAO,GAAG,IAAI,CAAC;gBACf,KAAK,CAAC,IAAI,EAAE,CAAC;YACf,CAAC;SACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,38 @@
1
+ import { type Fetch } from './download.js';
2
+ /**
3
+ * Piper (MIT): natural local voices, fast on a regular CPU. Pinned version with the SHA-256 of each download
4
+ * computed when pinning it: it is an executable, so nothing else is accepted.
5
+ */
6
+ export declare const PIPER_VERSION = "2023.11.14-2";
7
+ export interface CatalogVoice {
8
+ key: string;
9
+ language: {
10
+ code: string;
11
+ family: string;
12
+ };
13
+ quality: string;
14
+ files: Record<string, {
15
+ size_bytes: number;
16
+ md5_digest: string;
17
+ }>;
18
+ }
19
+ export type Catalog = Record<string, CatalogVoice>;
20
+ /**
21
+ * The voice for a language: one for its exact variant if any (es_MX), otherwise one for its family (es);
22
+ * among several, the best quality according to QUALITY_PREFERENCE and, on a tie, the first by name.
23
+ */
24
+ export declare function chooseVoice(catalog: Catalog, language: string): CatalogVoice | undefined;
25
+ /** The piper executable for this machine: downloaded, verified and extracted the first time. */
26
+ export declare function ensurePiper(dataDir: string, { platform, arch, fetchImpl }?: {
27
+ platform?: NodeJS.Platform;
28
+ arch?: string;
29
+ fetchImpl?: Fetch;
30
+ }): Promise<string>;
31
+ /** The voice catalog, stored the first time. */
32
+ export declare function loadCatalog(dataDir: string, fetchImpl?: Fetch): Promise<Catalog>;
33
+ /** The files of a voice, verified with the catalog hashes. Returns the model and its sample rate. */
34
+ export declare function ensureVoice(dataDir: string, voice: CatalogVoice, fetchImpl?: Fetch): Promise<{
35
+ model: string;
36
+ sampleRate: number;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AAE,KAAK,GAAG,OAAO,CAAC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC,CAczI"}
package/dist/piper.js ADDED
@@ -0,0 +1,90 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { existsSync } from 'node:fs';
3
+ import { readFile, rm } from 'node:fs/promises';
4
+ import { join } from 'node:path';
5
+ import { download } from './download.js';
6
+ /**
7
+ * Piper (MIT): natural local voices, fast on a regular CPU. Pinned version with the SHA-256 of each download
8
+ * computed when pinning it: it is an executable, so nothing else is accepted.
9
+ */
10
+ export const PIPER_VERSION = '2023.11.14-2';
11
+ const PIPER_URL = `https://github.com/rhasspy/piper/releases/download/${PIPER_VERSION}`;
12
+ const PIPER_ASSETS = {
13
+ 'win32-x64': { file: 'piper_windows_amd64.zip', size: 22477236, sha256: 'f3c58906402b24f3a96d92145f58acba6d86c9b5db896d207f78dc80811efcea' },
14
+ 'linux-x64': { file: 'piper_linux_x86_64.tar.gz', size: 26460462, sha256: 'a50cb45f355b7af1f6d758c1b360717877ba0a398cc8cbe6d2a7a3a26e225992' },
15
+ 'linux-arm64': { file: 'piper_linux_aarch64.tar.gz', size: 26004717, sha256: 'fea0fd2d87c54dbc7078d0f878289f404bd4d6eea6e7444a77835d1537ab88eb' },
16
+ 'darwin-x64': { file: 'piper_macos_x64.tar.gz', size: 19146927, sha256: 'ced85c0a3df13945b1e623b878a48fdc2854d5c485b4b67f62857cf551deaf8b' },
17
+ 'darwin-arm64': { file: 'piper_macos_aarch64.tar.gz', size: 19146957, sha256: '6b1eb03b3735946cb35216e063e7eebcc33a6bbf5dd96ec0217959bf1cdcb0cc' },
18
+ };
19
+ /**
20
+ * The official Piper voice catalog, at a pinned revision of the repository. It carries the hash of every
21
+ * file.
22
+ */
23
+ const VOICES_URL = 'https://huggingface.co/rhasspy/piper-voices/resolve/e7229f7d33a10e145e109b886d661984201ac3c4';
24
+ /**
25
+ * Piper quality levels, from the best balance of naturalness and speed to the worst. This is Piper's own
26
+ * scale (x_low, low, medium, high): medium sounds natural and is fast; high is slightly better and slower.
27
+ */
28
+ const QUALITY_PREFERENCE = ['medium', 'high', 'low', 'x_low'];
29
+ /**
30
+ * The voice for a language: one for its exact variant if any (es_MX), otherwise one for its family (es);
31
+ * among several, the best quality according to QUALITY_PREFERENCE and, on a tie, the first by name.
32
+ */
33
+ export function chooseVoice(catalog, language) {
34
+ const code = language.replace('-', '_');
35
+ const family = code.split('_')[0].toLowerCase();
36
+ const all = Object.values(catalog);
37
+ const exact = all.filter((v) => v.language.code.toLowerCase() === code.toLowerCase());
38
+ const candidates = exact.length ? exact : all.filter((v) => v.language.family === family);
39
+ const rank = (v) => {
40
+ const index = QUALITY_PREFERENCE.indexOf(v.quality);
41
+ return index < 0 ? QUALITY_PREFERENCE.length : index;
42
+ };
43
+ return candidates.sort((a, b) => rank(a) - rank(b) || a.key.localeCompare(b.key))[0];
44
+ }
45
+ /** The piper executable for this machine: downloaded, verified and extracted the first time. */
46
+ export async function ensurePiper(dataDir, { platform = process.platform, arch = process.arch, fetchImpl } = {}) {
47
+ const asset = PIPER_ASSETS[`${platform}-${arch}`];
48
+ if (!asset)
49
+ throw new Error(`Piper has no build for ${platform}-${arch}: use --tts system`);
50
+ const dir = join(dataDir, 'piper', PIPER_VERSION);
51
+ const exe = join(dir, 'piper', platform === 'win32' ? 'piper.exe' : 'piper');
52
+ if (existsSync(exe))
53
+ return exe;
54
+ const archive = await download(`${PIPER_URL}/${asset.file}`, join(dir, asset.file), asset, fetchImpl);
55
+ await extract(archive, dir, platform);
56
+ await rm(archive);
57
+ if (!existsSync(exe))
58
+ throw new Error(`No encuentro piper tras descomprimir ${asset.file}`);
59
+ return exe;
60
+ }
61
+ /** Extracts with tar: on Windows the system one (it opens .zip); elsewhere the usual one. */
62
+ function extract(archive, into, platform) {
63
+ const tar = platform === 'win32' ? join(process.env['SystemRoot'] ?? 'C:\\Windows', 'System32', 'tar.exe') : 'tar';
64
+ return new Promise((resolve, reject) => {
65
+ const child = spawn(tar, ['-xf', archive, '-C', into], { stdio: ['ignore', 'ignore', 'pipe'], windowsHide: true });
66
+ let stderr = '';
67
+ child.stderr.on('data', (chunk) => (stderr += chunk));
68
+ child.on('error', reject);
69
+ child.on('close', (code) => (code === 0 ? resolve() : reject(new Error(`No puedo descomprimir Piper: ${stderr.trim()}`))));
70
+ });
71
+ }
72
+ /** The voice catalog, stored the first time. */
73
+ export async function loadCatalog(dataDir, fetchImpl) {
74
+ const file = await download(`${VOICES_URL}/voices.json`, join(dataDir, 'voices', 'voices.json'), {}, fetchImpl);
75
+ return JSON.parse(await readFile(file, 'utf8'));
76
+ }
77
+ /** The files of a voice, verified with the catalog hashes. Returns the model and its sample rate. */
78
+ export async function ensureVoice(dataDir, voice, fetchImpl) {
79
+ const paths = {};
80
+ for (const [path, { size_bytes, md5_digest }] of Object.entries(voice.files)) {
81
+ if (!path.endsWith('.onnx') && !path.endsWith('.onnx.json'))
82
+ continue;
83
+ paths[path.endsWith('.json') ? 'config' : 'model'] = await download(`${VOICES_URL}/${path}`, join(dataDir, 'voices', path), { size: size_bytes, md5: md5_digest }, fetchImpl);
84
+ }
85
+ if (!paths['model'] || !paths['config'])
86
+ throw new Error(`Voice ${voice.key} has no model in the catalog`);
87
+ const config = JSON.parse(await readFile(paths['config'], 'utf8'));
88
+ return { model: paths['model'], sampleRate: config.audio?.sample_rate ?? 22_050 };
89
+ }
90
+ //# sourceMappingURL=piper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"piper.js","sourceRoot":"","sources":["../src/piper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EAAE,QAAQ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AO,CAAC,GAAG,MAAM,QAAQ,CACjE,GAAG,UAAU,IAAI,IAAI,EAAE,EACvB,IAAI,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,EAC7B,EAAE,IAAI,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,EAAE,EACrC,SAAS,CACV,CAAC;IACJ,CAAC;IACD,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,SAAS,KAAK,CAAC,GAAG,8BAA8B,CAAC,CAAC;IAC3G,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,CAAyC,CAAC;IAC3G,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE,UAAU,EAAE,MAAM,CAAC,KAAK,EAAE,WAAW,IAAI,MAAM,EAAE,CAAC;AACpF,CAAC"}
@@ -0,0 +1,21 @@
1
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
+ /** Fixed instructions for the AI: how to hold a voice conversation with the user. */
3
+ export declare const INSTRUCTIONS: string;
4
+ export declare const SILENCE = "(silence: the user has not said anything)";
5
+ export declare const CUT = "Cut off: the user talked while it was playing. Stop what you were doing and call listen.";
6
+ export interface VoiceTools {
7
+ listen(): Promise<string>;
8
+ /**
9
+ * Starts saying the text. With `wait`, resolves when playback ends, with `false` if the user cut it off.
10
+ */
11
+ speak(text: string, language?: string, wait?: boolean): Promise<boolean | void>;
12
+ stop(): void;
13
+ /** Ends the waiting listen now, without consuming what was heard. */
14
+ interrupt?(): void;
15
+ }
16
+ /**
17
+ * The voice MCP server. One tool at a time, in order. Each new call ends any listen still waiting (e.g. one
18
+ * the AI abandoned); otherwise a speak would queue behind it without playing.
19
+ */
20
+ export declare function createVoiceServer(tools: VoiceTools): Server;
21
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAGnE,qFAAqF;AACrF,eAAO,MAAM,YAAY,QAiBb,CAAC;AAEb,eAAO,MAAM,OAAO,8CAA8C,CAAC;AACnE,eAAO,MAAM,GAAG,6FAA6F,CAAC;AA+B9G,MAAM,WAAW,UAAU;IACzB,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAC1B;;OAEG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC;IAChF,IAAI,IAAI,IAAI,CAAC;IACb,qEAAqE;IACrE,SAAS,CAAC,IAAI,IAAI,CAAC;CACpB;AAKD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAmC3D"}
package/dist/server.js ADDED
@@ -0,0 +1,92 @@
1
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
+ import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
3
+ /** Fixed instructions for the AI: how to hold a voice conversation with the user. */
4
+ export const INSTRUCTIONS = [
5
+ 'Local voice tools: the microphone listens all the time, also while you speak, so the user can answer or interrupt you at once. Each long pause from the user closes a sentence. listen returns everything they said since the last call, whole (or waits for their next sentence to end); speak reads a text out loud to them and returns right away, while it plays.',
6
+ 'To talk with the user by voice: greet them with speak, then repeat without stopping: listen → do what they ask → speak a short answer → listen.',
7
+ 'Keep listening until the user tells you to stop (for example "stop", "that is all" or "goodbye"). If listen hears nothing, call listen again.',
8
+ 'When the voice conversation ends, call stop: it closes the microphone and clears anything pending. If you do not call listen again within 30 s, the microphone closes by itself.',
9
+ 'The user may be talking to someone else: if what you hear is not addressed to you, do not answer or act; call listen again.',
10
+ 'The transcription may have errors: if it makes no sense or something is missing (e.g. a digit of a code), ask the user to repeat it.',
11
+ 'If listen returns something that repeats what you just said, it is your own echo from the speakers: ignore it and call listen again.',
12
+ 'As soon as the user finishes talking, answer quickly and briefly: the sooner you go back to listen, the more natural the conversation.',
13
+ 'Since speak does not wait for playback to end, when the user asks for something on the page, first say a short sentence and do the action while it plays; then tell the result.',
14
+ 'When you read something from the page while marking it with highlight, go sentence by sentence, one response at a time: highlight the first one; then, in each response, speak the highlighted sentence with wait: true and then highlight the next one (so it is ready while the previous one plays). If speak answers that you were cut off, stop everything and call listen.',
15
+ 'If the user talks while you speak, your voice stops by itself and listen starts with "(the user interrupted you while you were speaking)": do not carry on with what you were saying; attend to what they ask.',
16
+ 'Answer briefly and quickly, in one or two sentences: the less you think before speaking, the better. Do not offer options or suggest things the user did not ask for: if they are just chatting, chat and wait for them to ask for something.',
17
+ 'Do one thing at a time and in order. If the user cuts you off or says no, stop everything you were doing, not just the voice.',
18
+ 'What you pass to speak is heard: short, natural sentences, with no markdown, lists or emojis.',
19
+ 'Answer in the language the user speaks to you, and pass that language to speak in language (es, en, fr…), so it plays with a voice for that language.',
20
+ 'If the user says a code they see on a page, pass it to that page pairing tool.',
21
+ ].join('\n');
22
+ export const SILENCE = '(silence: the user has not said anything)';
23
+ export const CUT = 'Cut off: the user talked while it was playing. Stop what you were doing and call listen.';
24
+ const TOOLS = [
25
+ {
26
+ name: 'listen',
27
+ description: 'Returns what the user said into the microphone since the last call; if they have not said anything yet, waits for their next sentence to end. If they say nothing for 60 s, returns a silence notice.',
28
+ inputSchema: { type: 'object', properties: {} },
29
+ },
30
+ {
31
+ name: 'stop',
32
+ description: 'Ends the voice conversation: closes the microphone and clears everything heard. Call it when the user says stop, that is all, or goodbye.',
33
+ inputSchema: { type: 'object', properties: {} },
34
+ },
35
+ {
36
+ name: 'speak',
37
+ description: 'Reads a text out loud to the user through the speakers, with a natural voice for the language of the text. Returns right away, while it plays, so you can keep working. With wait: true, returns when playback ends. Short sentences, no markdown.',
38
+ inputSchema: {
39
+ type: 'object',
40
+ properties: {
41
+ text: { type: 'string', description: 'What to say' },
42
+ language: { type: 'string', description: 'Language of the text: es, en, fr, de… (or es_MX, pt_BR for a variant)' },
43
+ wait: { type: 'boolean', description: 'true: returns when playback ends (e.g. to highlight what you are reading, sentence by sentence). By default, returns right away.' },
44
+ },
45
+ required: ['text'],
46
+ },
47
+ },
48
+ ];
49
+ const text = (value) => ({ content: [{ type: 'text', text: value }] });
50
+ const error = (message) => ({ isError: true, content: [{ type: 'text', text: message }] });
51
+ /**
52
+ * The voice MCP server. One tool at a time, in order. Each new call ends any listen still waiting (e.g. one
53
+ * the AI abandoned); otherwise a speak would queue behind it without playing.
54
+ */
55
+ export function createVoiceServer(tools) {
56
+ const server = new Server({ name: 'sanoriva-voice', version: '0.0.0' }, { capabilities: { tools: {} }, instructions: INSTRUCTIONS });
57
+ let queue = Promise.resolve();
58
+ server.setRequestHandler(ListToolsRequestSchema, () => ({ tools: TOOLS }));
59
+ server.setRequestHandler(CallToolRequestSchema, ({ params }) => {
60
+ const run = async () => {
61
+ try {
62
+ if (params.name === 'listen')
63
+ return text((await tools.listen()) || SILENCE);
64
+ if (params.name === 'stop') {
65
+ tools.stop();
66
+ return text('Microphone closed');
67
+ }
68
+ if (params.name === 'speak') {
69
+ const value = String(params.arguments?.['text'] ?? '').trim();
70
+ if (!value)
71
+ return error('Missing the text to say');
72
+ const language = String(params.arguments?.['language'] ?? '').trim();
73
+ const wait = params.arguments?.['wait'] === true;
74
+ const played = await tools.speak(value, /^[a-z]{2,3}([_-][A-Za-z]{2})?$/.test(language) ? language : undefined, wait);
75
+ if (wait && played === false)
76
+ return error(CUT);
77
+ return text(wait ? 'Said' : 'Playing');
78
+ }
79
+ return error(`Unknown tool ${params.name}`);
80
+ }
81
+ catch (e) {
82
+ return error(e instanceof Error ? e.message : String(e));
83
+ }
84
+ };
85
+ tools.interrupt?.();
86
+ const result = queue.then(run);
87
+ queue = result;
88
+ return result;
89
+ });
90
+ return server;
91
+ }
92
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,qBAAqB,EAAuB,sBAAsB,EAAa,MAAM,oCAAoC,CAAC;AAEnI,qFAAqF;AACrF,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,uWAAuW;IACvW,iJAAiJ;IACjJ,+IAA+I;IAC/I,kLAAkL;IAClL,6HAA6H;IAC7H,sIAAsI;IACtI,sIAAsI;IACtI,wIAAwI;IACxI,iLAAiL;IACjL,iXAAiX;IACjX,gNAAgN;IAChN,+OAA+O;IAC/O,+HAA+H;IAC/H,+FAA+F;IAC/F,uJAAuJ;IACvJ,gFAAgF;CACjF,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAEb,MAAM,CAAC,MAAM,OAAO,GAAG,2CAA2C,CAAC;AACnE,MAAM,CAAC,MAAM,GAAG,GAAG,0FAA0F,CAAC;AAE9G,MAAM,KAAK,GAAW;IACpB;QACE,IAAI,EAAE,QAAQ;QACd,WAAW,EACT,uMAAuM;QACzM,WAAW,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,EAAE,EAAE;KAChD;IACD;QACE,IAAI,EAAE,MAAM;QACZ,WAAW,EACT,2IAA2I;QAC7I,WAAW,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,EAAE,EAAE;KAChD;IACD;QACE,IAAI,EAAE,OAAO;QACb,WAAW,EACT,oPAAoP;QACtP,WAAW,EAAE;YACX,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE;gBACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,aAAa,EAAE;gBACpD,QAAQ,EAAE,EAAE,IAA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AAC,CAAC;YAC3D,CAAC;QACH,CAAC,CAAC;QACF,KAAK,CAAC,SAAS,EAAE,EAAE,CAAC;QACpB,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC/B,KAAK,GAAG,MAAM,CAAC;QACf,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { VoiceConfig } from './config.js';
2
+ /** A setup step: returns what it did, or throws an Error explaining how to fix it. */
3
+ export interface SetupStep {
4
+ name: string;
5
+ run: () => Promise<string>;
6
+ }
7
+ /** Runs the steps in order, even if one fails, and reports each one. Returns true when all succeed. */
8
+ export declare function runSetup(steps: SetupStep[], print?: (line: string) => void): Promise<boolean>;
9
+ /**
10
+ * Prepares everything for the first conversation: checks ffmpeg, ffplay and the microphone, and downloads the
11
+ * models (Whisper, Silero, Smart Turn, Piper and the voice for the language). It installs nothing on the
12
+ * system.
13
+ */
14
+ export declare function setupSteps(config: VoiceConfig): SetupStep[];
15
+ //# sourceMappingURL=setup.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../src/setup.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAO/C,sFAAsF;AACtF,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;CAC5B;AAED,uGAAuG;AACvG,wBAAsB,QAAQ,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE,KAAK,GAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAoB,GAAG,OAAO,CAAC,OAAO,CAAC,CAalH;AAeD;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,WAAW,GAAG,SAAS,EAAE,CA6B3D"}
package/dist/setup.js ADDED
@@ -0,0 +1,73 @@
1
+ import { spawnSync } from 'node:child_process';
2
+ import { findWindowsMic } from './mic.js';
3
+ import { createPiperSpeaker } from './speak.js';
4
+ import { createSileroCheck } from './speech-check.js';
5
+ import { createWhisper } from './stt.js';
6
+ import { createTurnCheck } from './turn.js';
7
+ /** Runs the steps in order, even if one fails, and reports each one. Returns true when all succeed. */
8
+ export async function runSetup(steps, print = console.error) {
9
+ let ok = true;
10
+ for (const step of steps) {
11
+ const started = Date.now();
12
+ try {
13
+ const done = await step.run();
14
+ print(`✓ ${step.name}: ${done} (${((Date.now() - started) / 1000).toFixed(1)} s)`);
15
+ }
16
+ catch (error) {
17
+ ok = false;
18
+ print(`✗ ${step.name}: ${error.message}`);
19
+ }
20
+ }
21
+ return ok;
22
+ }
23
+ /** How to install ffmpeg (it includes ffplay) on each operating system. */
24
+ const FFMPEG_HELP = {
25
+ win32: 'install it with "winget install Gyan.FFmpeg" and open a new terminal, or point to it with --ffmpeg <path>',
26
+ darwin: 'install it with "brew install ffmpeg", or point to it with --ffmpeg <path>',
27
+ linux: 'install it with your package manager (e.g. "sudo apt install ffmpeg"), or point to it with --ffmpeg <path>',
28
+ };
29
+ function checkProgram(program) {
30
+ const { status, stdout, error } = spawnSync(program, ['-version'], { encoding: 'utf8', windowsHide: true });
31
+ if (error || status !== 0)
32
+ throw new Error(`${program} not found: ${FFMPEG_HELP[process.platform] ?? FFMPEG_HELP.linux}`);
33
+ return stdout.split('\n')[0].trim();
34
+ }
35
+ /**
36
+ * Prepares everything for the first conversation: checks ffmpeg, ffplay and the microphone, and downloads the
37
+ * models (Whisper, Silero, Smart Turn, Piper and the voice for the language). It installs nothing on the
38
+ * system.
39
+ */
40
+ export function setupSteps(config) {
41
+ return [
42
+ { name: 'ffmpeg (microphone)', run: async () => checkProgram(config.ffmpeg) },
43
+ { name: 'ffplay (speaker)', run: async () => checkProgram(config.ffplay) },
44
+ {
45
+ name: 'Microphone',
46
+ run: async () => {
47
+ if (config.mic)
48
+ return config.mic;
49
+ if (process.platform !== 'win32')
50
+ return 'system default';
51
+ const mic = findWindowsMic(config.ffmpeg);
52
+ if (!mic)
53
+ throw new Error('ffmpeg sees no microphone: plug one in or choose it with --mic "<name>"');
54
+ return mic;
55
+ },
56
+ },
57
+ {
58
+ name: `Whisper (${config.model}, ${config.device})`,
59
+ run: async () => (await createWhisper({ model: config.model, language: config.language, cacheDir: config.cacheDir, device: config.device }), 'downloaded and tested'),
60
+ },
61
+ { name: 'Silero VAD', run: async () => (await createSileroCheck({ cacheDir: config.dataDir }), 'downloaded') },
62
+ { name: 'Smart Turn', run: async () => (await createTurnCheck({ cacheDir: config.dataDir }), 'downloaded') },
63
+ ...(config.tts === 'piper'
64
+ ? [
65
+ {
66
+ name: `Piper and its voice (${config.voice ?? config.language ?? 'en'})`,
67
+ run: async () => (await createPiperSpeaker({ ...config }).prepare(config.language), 'downloaded'),
68
+ },
69
+ ]
70
+ : []),
71
+ ];
72
+ }
73
+ //# sourceMappingURL=setup.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setup.js","sourceRoot":"","sources":["../src/setup.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAG/C,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAQ5C,uGAAuG;AACvG,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,KAAkB,EAAE,QAAgC,OAAO,CAAC,KAAK;IAC9F,IAAI,EAAE,GAAG,IAAI,CAAC;IACd,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE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G,CAAC,MAAM,CAAC,GAAG,KAAK,OAAO;YACxB,CAAC,CAAC;gBACE;oBACE,IAAI,EAAE,wBAAwB,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,QAAQ,IAAI,IAAI,GAAG;oBACxE,GAAG,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,kBAAkB,CAAC,EAAE,GAAG,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,YAAY,CAAC;iBAClG;aACF;YACH,CAAC,CAAC,EAAE,CAAC;KACR,CAAC;AACJ,CAAC"}
@@ -0,0 +1,74 @@
1
+ import type { Fetch } from './download.js';
2
+ export interface SpeakCommand {
3
+ command: string;
4
+ args: string[];
5
+ env?: Record<string, string>;
6
+ }
7
+ /** How to read a text out loud with the local system voice. `lang` is a 2-letter code (es, en…). */
8
+ export declare function speakCommand(platform: NodeJS.Platform, text: string, lang?: string): SpeakCommand;
9
+ export type RunCommand = (command: SpeakCommand, signal?: AbortSignal) => Promise<void>;
10
+ /**
11
+ * Reads a text out loud. `language` is the language of the text (es, en, fr…), to pick the voice; `signal`
12
+ * cuts it off.
13
+ */
14
+ export type Speaker = (text: string, language?: string, signal?: AbortSignal) => Promise<void>;
15
+ /** The system voice (Windows, macOS or espeak-ng): always available, but it sounds robotic. */
16
+ export declare function createSpeaker({ lang, platform, run }?: {
17
+ lang?: string;
18
+ platform?: NodeJS.Platform;
19
+ run?: RunCommand;
20
+ }): Speaker;
21
+ /** Piper generates the audio (16-bit PCM) and ffplay plays it while it is generated. */
22
+ export declare function piperCommands({ piper, model, sampleRate, ffplay }: {
23
+ piper: string;
24
+ model: string;
25
+ sampleRate: number;
26
+ ffplay: string;
27
+ }): {
28
+ synth: {
29
+ command: string;
30
+ args: string[];
31
+ };
32
+ player: {
33
+ command: string;
34
+ args: string[];
35
+ };
36
+ };
37
+ /**
38
+ * The level of each 30 ms chunk of 16-bit PCM audio, with the time it will play. Piper generates faster than
39
+ * real time, so the time comes from the position in the audio, not from when it arrives.
40
+ */
41
+ export declare function pcmLevels(sampleRate: number, onLevel: (level: number, at: number, first: boolean) => void): (chunk: Buffer, now?: number) => void;
42
+ export interface PiperSpeakerOptions {
43
+ dataDir: string;
44
+ ffplay: string;
45
+ /** A fixed voice from the catalog (es_ES-davefx-medium…). Without it, a voice is chosen per language. */
46
+ voice?: string;
47
+ /** The language when the AI gives none. */
48
+ language?: string;
49
+ fetchImpl?: Fetch;
50
+ /** Receives the level and play time of each 30 ms chunk, to tell the echo from the user's voice. */
51
+ onLevel?: (level: number, at: number, first: boolean) => void;
52
+ }
53
+ /**
54
+ * Natural voices with Piper. Each language's voice comes from the official Piper catalog and is downloaded
55
+ * the first time it is needed, verified with its hash. Piper itself is also downloaded the first time.
56
+ */
57
+ export declare function createPiperSpeaker({ dataDir, ffplay, voice, language, fetchImpl, onLevel }: PiperSpeakerOptions): Speaker & {
58
+ warm(): void;
59
+ prepare(lang?: string): Promise<void>;
60
+ };
61
+ /**
62
+ * Speaks without blocking: each sentence waits for the previous one, but the caller goes on at once (the AI
63
+ * can act on the page while it plays). Resolves, when that sentence ends, with whether it played whole
64
+ * (`false`: it was cut off).
65
+ */
66
+ export declare function speechQueue(speaker: Speaker, onError?: (error: Error) => void): {
67
+ (text: string, language?: string): Promise<boolean>;
68
+ cancel(): void;
69
+ };
70
+ /** A short soft tone, generated by ffplay with no files: "I heard you, I'm thinking". */
71
+ export declare function chimeCommand(ffplay: string): SpeakCommand;
72
+ /** Plays the tone without waiting for it to end; a failure does not matter, since it is only a cue. */
73
+ export declare function createChime(ffplay: string, run?: RunCommand): () => void;
74
+ //# sourceMappingURL=speak.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"speak.d.ts","sourceRoot":"","sources":["../src/speak.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAG3C,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,C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