tz-voice-chat 1.0.2 → 1.1.2
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 +31 -0
- package/dist/assets/llmWorker-CVEGn6yI.js +1 -0
- package/dist/index.cjs +2 -1
- package/dist/index.js +801 -438
- package/dist/style.css +1 -1
- package/package.json +18 -4
- package/server/index.js +263 -0
- package/server/tts.js +116 -0
package/dist/style.css
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
@charset "UTF-8";.ai-btn-root{display:inline-flex;flex-direction:column;align-items:center;gap:6px;position:relative;-webkit-user-select:none;user-select:none;padding-bottom:5px}.ai-btn-rings{position:absolute;top:0;left:50%;transform:translate(-50%);width:var(--tz-size);height:var(--tz-size);pointer-events:none;z-index:0}.ai-ring{position:absolute;top:0;right:0;bottom:0;left:0;border-radius:50%;border:1px solid transparent}@keyframes ai-ring-expand{0%{transform:scale(1);opacity:1}to{transform:scale(1.65);opacity:0}}.ai-btn-rings--idle .ai-ring--1{border-color:var(--tz-a-28);animation:ai-ring-expand 2.8s ease-out infinite}.ai-btn-rings--idle .ai-ring--2{display:none}.ai-btn-rings--listening .ai-ring--1{border-color:var(--tz-a-60);animation:ai-ring-expand 1.8s ease-out infinite}.ai-btn-rings--listening .ai-ring--2{border-color:var(--tz-a-32);animation:ai-ring-expand 1.8s ease-out .55s infinite}.ai-btn-rings--transcribing .ai-ring--1{border-color:var(--tz-a-50);animation:ai-ring-expand 1.4s ease-out infinite}.ai-btn-rings--transcribing .ai-ring--2{display:none}.ai-btn-rings--thinking .ai-ring--1{border-color:var(--tz-a-34);animation:ai-ring-expand 2s ease-out infinite}.ai-btn-rings--thinking .ai-ring--2{display:none}.ai-btn-rings--speaking .ai-ring--1{border-color:var(--tz-a-68);animation:ai-ring-expand 1.1s ease-out infinite}.ai-btn-rings--speaking .ai-ring--2{border-color:var(--tz-a-38);animation:ai-ring-expand 1.1s ease-out .35s infinite}.ai-btn-rings--ready .ai-ring--1{border-color:var(--tz-a-55);animation:ai-ring-expand 1.6s ease-out infinite}.ai-btn-rings--ready .ai-ring--2{display:none}.ai-btn{position:relative;z-index:1;width:var(--tz-size);height:var(--tz-size);border-radius:50%;border:2px solid transparent;overflow:hidden;display:flex;align-items:center;justify-content:center;cursor:pointer;transition:background .3s ease,border-color .3s ease,box-shadow .3s ease,transform .15s ease;-webkit-tap-highlight-color:transparent}.ai-btn:hover:not(:disabled){transform:scale(1.045)}.ai-btn:active:not(:disabled){transform:scale(.97)}.ai-btn:disabled{cursor:not-allowed;opacity:.5}.ai-btn__hover-stop{position:absolute;top:0;right:0;bottom:0;left:0;display:flex;align-items:center;justify-content:center;font-size:12px;color:#fffc;opacity:0;background:#00000061;transition:opacity .2s;border-radius:50%;pointer-events:none}.ai-btn:hover:not(:disabled) .ai-btn__hover-stop{opacity:1}.ai-btn--off{background:radial-gradient(circle at 42% 38%,var(--tz-a-d22),var(--tz-a-d30));border-color:var(--tz-a-30);box-shadow:0 0 16px var(--tz-a-25),0 4px 18px #0006;color:var(--tz-a-l20)}.ai-btn--off:hover:not(:disabled){border-color:var(--tz-a-55);color:var(--tz-a-l30)}.ai-btn--idle{background:radial-gradient(circle at 42% 38%,var(--tz-a-d8),var(--tz-a-d22));border-color:var(--tz-a-50);box-shadow:0 0 26px var(--tz-a-35),0 4px 18px #0006;color:var(--tz-a-l20)}.ai-btn--listening{background:radial-gradient(circle at 42% 38%,var(--tz-a-d22),var(--tz-a-d30));border-color:var(--tz-a-50);box-shadow:0 0 28px var(--tz-a-50),0 4px 18px #0006;color:var(--tz-a-l30)}.ai-btn--transcribing{background:radial-gradient(circle at 42% 38%,var(--tz-a-d15),var(--tz-a-d22));border-color:var(--tz-a-45);box-shadow:0 0 30px var(--tz-a-55),0 4px 18px #0006;color:var(--tz-a-l8)}.ai-btn--thinking{background:radial-gradient(circle at 42% 38%,var(--tz-a-d30),var(--tz-a-d42));border-color:var(--tz-a-35);box-shadow:0 0 26px var(--tz-a-50),0 4px 18px #0006;color:var(--tz-a-l20)}.ai-btn--speaking{background:radial-gradient(circle at 42% 38%,var(--tz-a-d8),var(--tz-a-d15));border-color:var(--tz-a-55);box-shadow:0 0 32px var(--tz-a-65),0 4px 18px #0006;color:var(--tz-a-l30)}.ai-btn--ready{background:radial-gradient(circle at 42% 38%,var(--tz-a-d8),var(--tz-a-d22));border-color:var(--tz-a-65);box-shadow:0 0 28px var(--tz-a-50),0 4px 18px #0006;color:var(--tz-a-l30)}.ai-btn--ready .ai-btn__mic{width:calc(var(--tz-size) * .5);height:calc(var(--tz-size) * .5)}.ai-btn__mic{width:calc(var(--tz-size) * .65);height:calc(var(--tz-size) * .65);opacity:.9;flex-shrink:0}.ai-btn__canvas{display:block;max-width:58px}.ai-btn__bars{display:flex;align-items:center;gap:3px}.ai-btn__bars span{display:block;width:4px;border-radius:2px;transform-origin:center}.ai-btn__bars--transcribing{height:37px}.ai-btn__bars--transcribing span{height:31px;background:linear-gradient(180deg,var(--tz-a-l20),var(--tz-a-d8));animation:ai-bar-transcribe .85s ease-in-out infinite}.ai-btn__bars--speaking{height:37px}.ai-btn__bars--speaking span{height:31px;background:linear-gradient(180deg,var(--tz-a-l30),var(--tz-a));animation:ai-bar-speak .55s ease-in-out infinite}.ai-btn__dots{display:flex;align-items:center;gap:6px}.ai-btn__dots span{display:block;width:8px;height:8px;border-radius:50%;background:var(--tz-a-l20);animation:ai-dot-think 1.1s ease-in-out infinite}@keyframes ai-bar-transcribe{0%,to{transform:scaleY(.1);opacity:.35}50%{transform:scaleY(1);opacity:1}}@keyframes ai-bar-speak{0%,to{transform:scaleY(.18)}50%{transform:scaleY(1)}}@keyframes ai-dot-think{0%,60%,to{transform:translateY(0);opacity:.4}30%{transform:translateY(-10px);opacity:1}}.ai-mic-sel{position:relative;opacity:0;visibility:hidden;transform:translateY(4px);transition:opacity .18s,visibility .18s,transform .18s}.ai-btn-root:hover .ai-mic-sel,.ai-mic-sel.open{opacity:1;visibility:visible;transform:translateY(0)}.ai-mic-sel__trigger{display:inline-flex;align-items:center;gap:5px;padding:5px 11px;border-radius:999px;background:var(--tz-u-d30-a85);border:1px solid var(--tz-u-l18-a18);color:var(--tz-u-l18);font-size:.73rem;cursor:pointer;transition:background .18s,border-color .18s,color .18s;white-space:nowrap;max-width:200px}.ai-mic-sel__trigger:hover{background:var(--tz-u-d22-a90);border-color:var(--tz-u-l18-a32);color:var(--tz-u-l37)}.ai-mic-sel__label{max-width:130px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.ai-mic-sel__caret{transition:transform .2s;flex-shrink:0;opacity:.7}.ai-mic-sel__caret.open{transform:rotate(180deg)}.ai-mic-sel__menu{position:absolute;bottom:calc(100% + 7px);left:50%;transform:translate(-50%);min-width:220px;background:var(--tz-u-d30);border:1px solid var(--tz-u-l18-a20);border-radius:10px;box-shadow:0 8px 30px #0000008c;overflow:hidden;z-index:200;list-style:none;margin:0;padding:4px}.ai-mic-sel__option{display:block;width:100%;padding:8px 12px;text-align:left;font-size:.8rem;color:var(--tz-u-l37);border-radius:7px;background:none;border:none;cursor:pointer;transition:background .15s;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.ai-mic-sel__option:hover{background:#3b82f624;color:#e2e8f0}.ai-mic-sel__option.selected{background:#3b82f633;color:#93c5fd}.ai-mic-sel__option.selected:before{content:"✓ "}
|
|
1
|
+
@charset "UTF-8";.ai-btn-root{display:inline-flex;flex-direction:column;align-items:center;gap:6px;position:relative;-webkit-user-select:none;user-select:none;padding-bottom:5px}.ai-btn-rings{position:absolute;top:0;left:50%;transform:translate(-50%);width:var(--tz-size);height:var(--tz-size);pointer-events:none;z-index:0}.ai-ring{position:absolute;top:0;right:0;bottom:0;left:0;border-radius:50%;border:1px solid transparent}@keyframes ai-ring-expand{0%{transform:scale(1);opacity:1}to{transform:scale(1.65);opacity:0}}.ai-btn-rings--idle .ai-ring--1{border-color:var(--tz-a-28);animation:ai-ring-expand 2.8s ease-out infinite}.ai-btn-rings--idle .ai-ring--2{display:none}.ai-btn-rings--listening .ai-ring--1{border-color:var(--tz-a-60);animation:ai-ring-expand 1.8s ease-out infinite}.ai-btn-rings--listening .ai-ring--2{border-color:var(--tz-a-32);animation:ai-ring-expand 1.8s ease-out .55s infinite}.ai-btn-rings--transcribing .ai-ring--1{border-color:var(--tz-a-50);animation:ai-ring-expand 1.4s ease-out infinite}.ai-btn-rings--transcribing .ai-ring--2{display:none}.ai-btn-rings--thinking .ai-ring--1{border-color:var(--tz-a-34);animation:ai-ring-expand 2s ease-out infinite}.ai-btn-rings--thinking .ai-ring--2{display:none}.ai-btn-rings--speaking .ai-ring--1{border-color:var(--tz-a-68);animation:ai-ring-expand 1.1s ease-out infinite}.ai-btn-rings--speaking .ai-ring--2{border-color:var(--tz-a-38);animation:ai-ring-expand 1.1s ease-out .35s infinite}.ai-btn-rings--ready .ai-ring--1{border-color:var(--tz-a-55);animation:ai-ring-expand 1.6s ease-out infinite}.ai-btn-rings--ready .ai-ring--2{display:none}.ai-btn{position:relative;z-index:1;width:var(--tz-size);height:var(--tz-size);border-radius:50%;border:2px solid transparent;overflow:hidden;display:flex;align-items:center;justify-content:center;cursor:pointer;transition:background .3s ease,border-color .3s ease,box-shadow .3s ease,transform .15s ease;-webkit-tap-highlight-color:transparent}.ai-btn:hover:not(:disabled){transform:scale(1.045)}.ai-btn:active:not(:disabled){transform:scale(.97)}.ai-btn:disabled{cursor:not-allowed;opacity:.5}.ai-btn__hover-stop{position:absolute;top:0;right:0;bottom:0;left:0;display:flex;align-items:center;justify-content:center;font-size:12px;color:#fffc;opacity:0;background:#00000061;transition:opacity .2s;border-radius:50%;pointer-events:none}.ai-btn:hover:not(:disabled) .ai-btn__hover-stop{opacity:1}.ai-btn--off{background:radial-gradient(circle at 42% 38%,var(--tz-a-d22),var(--tz-a-d30));border-color:var(--tz-a-30);box-shadow:0 0 16px var(--tz-a-25),0 4px 18px #0006;color:var(--tz-a-l20)}.ai-btn--off:hover:not(:disabled){border-color:var(--tz-a-55);color:var(--tz-a-l30)}.ai-btn--idle{background:radial-gradient(circle at 42% 38%,var(--tz-a-d8),var(--tz-a-d22));border-color:var(--tz-a-50);box-shadow:0 0 26px var(--tz-a-35),0 4px 18px #0006;color:var(--tz-a-l20)}.ai-btn--listening{background:radial-gradient(circle at 42% 38%,var(--tz-a-d22),var(--tz-a-d30));border-color:var(--tz-a-50);box-shadow:0 0 28px var(--tz-a-50),0 4px 18px #0006;color:var(--tz-a-l30)}.ai-btn--transcribing{background:radial-gradient(circle at 42% 38%,var(--tz-a-d15),var(--tz-a-d22));border-color:var(--tz-a-45);box-shadow:0 0 30px var(--tz-a-55),0 4px 18px #0006;color:var(--tz-a-l8)}.ai-btn--thinking{background:radial-gradient(circle at 42% 38%,var(--tz-a-d30),var(--tz-a-d42));border-color:var(--tz-a-35);box-shadow:0 0 26px var(--tz-a-50),0 4px 18px #0006;color:var(--tz-a-l20)}.ai-btn--speaking{background:radial-gradient(circle at 42% 38%,var(--tz-a-d8),var(--tz-a-d15));border-color:var(--tz-a-55);box-shadow:0 0 32px var(--tz-a-65),0 4px 18px #0006;color:var(--tz-a-l30)}.ai-btn--ready{background:radial-gradient(circle at 42% 38%,var(--tz-a-d8),var(--tz-a-d22));border-color:var(--tz-a-65);box-shadow:0 0 28px var(--tz-a-50),0 4px 18px #0006;color:var(--tz-a-l30)}.ai-btn--ready .ai-btn__mic{width:calc(var(--tz-size) * .5);height:calc(var(--tz-size) * .5)}.ai-btn__mic{width:calc(var(--tz-size) * .65);height:calc(var(--tz-size) * .65);opacity:.9;flex-shrink:0}.ai-btn__canvas{display:block;max-width:58px}.ai-btn__bars{display:flex;align-items:center;gap:3px}.ai-btn__bars span{display:block;width:4px;border-radius:2px;transform-origin:center}.ai-btn__bars--transcribing{height:37px}.ai-btn__bars--transcribing span{height:31px;background:linear-gradient(180deg,var(--tz-a-l20),var(--tz-a-d8));animation:ai-bar-transcribe .85s ease-in-out infinite}.ai-btn__bars--speaking{height:37px}.ai-btn__bars--speaking span{height:31px;background:linear-gradient(180deg,var(--tz-a-l30),var(--tz-a));animation:ai-bar-speak .55s ease-in-out infinite}.ai-btn__dots{display:flex;align-items:center;gap:6px}.ai-btn__dots span{display:block;width:8px;height:8px;border-radius:50%;background:var(--tz-a-l20);animation:ai-dot-think 1.1s ease-in-out infinite}@keyframes ai-bar-transcribe{0%,to{transform:scaleY(.1);opacity:.35}50%{transform:scaleY(1);opacity:1}}@keyframes ai-bar-speak{0%,to{transform:scaleY(.18)}50%{transform:scaleY(1)}}@keyframes ai-dot-think{0%,60%,to{transform:translateY(0);opacity:.4}30%{transform:translateY(-10px);opacity:1}}.ai-mic-sel{position:relative;opacity:0;visibility:hidden;transform:translateY(4px);transition:opacity .18s,visibility .18s,transform .18s}.ai-btn-root:hover .ai-mic-sel,.ai-mic-sel.open{opacity:1;visibility:visible;transform:translateY(0)}.ai-mic-sel__trigger{display:inline-flex;align-items:center;gap:5px;padding:5px 11px;border-radius:999px;background:var(--tz-u-d30-a85);border:1px solid var(--tz-u-l18-a18);color:var(--tz-u-l18);font-size:.73rem;cursor:pointer;transition:background .18s,border-color .18s,color .18s;white-space:nowrap;max-width:200px}.ai-mic-sel__trigger:hover{background:var(--tz-u-d22-a90);border-color:var(--tz-u-l18-a32);color:var(--tz-u-l37)}.ai-mic-sel__label{max-width:130px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.ai-mic-sel__caret{transition:transform .2s;flex-shrink:0;opacity:.7}.ai-mic-sel__caret.open{transform:rotate(180deg)}.ai-mic-sel__menu{position:absolute;bottom:calc(100% + 7px);left:50%;transform:translate(-50%);min-width:220px;background:var(--tz-u-d30);border:1px solid var(--tz-u-l18-a20);border-radius:10px;box-shadow:0 8px 30px #0000008c;overflow:hidden;z-index:200;list-style:none;margin:0;padding:4px}.ai-mic-sel__option{display:block;width:100%;padding:8px 12px;text-align:left;font-size:.8rem;color:var(--tz-u-l37);border-radius:7px;background:none;border:none;cursor:pointer;transition:background .15s;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.ai-mic-sel__option:hover{background:#3b82f624;color:#e2e8f0}.ai-mic-sel__option.selected{background:#3b82f633;color:#93c5fd}.ai-mic-sel__option.selected:before{content:"✓ "}:where(.tz-va){color-scheme:dark;color:#e2e8f0}:where(.tz-va) *,:where(.tz-va) *:before,:where(.tz-va) *:after{box-sizing:border-box}:where(.tz-va) button,:where(.tz-va) input{font:inherit}:where(.tz-va) button{cursor:pointer;border:none;padding:9px 16px;border-radius:10px;background:#2563eb;color:#fff;transition:transform .18s ease,filter .18s ease}:where(.tz-va) button:hover{transform:translateY(-1px)}:where(.tz-va) button:disabled{background:#94a3b866;cursor:not-allowed}:where(.tz-va) label{display:grid;gap:5px;font-size:.85rem;color:#cbd5e1}:where(.tz-va) input{border-radius:14px;border:1px solid rgba(148,163,184,.2);background:#0f172ab3;color:#e2e8f0;padding:12px 14px}.tz-va{max-width:980px;margin:0 auto;display:grid;gap:8px}.tz-va__card{background:#0f172ae6;border:1px solid rgba(148,163,184,.15);border-radius:12px;padding:12px 16px;box-shadow:0 8px 24px #0f172a40}.tz-va__header{display:flex;align-items:center;justify-content:space-between;gap:16px}.tz-va__header h1{margin:0;font-size:clamp(1.15rem,2vw,1.45rem)}.tz-va__chip{padding:5px 12px;border-radius:999px;font-weight:700;text-transform:uppercase;letter-spacing:.08em;font-size:.72rem;display:inline-flex;align-items:center;gap:6px;white-space:nowrap;flex-shrink:0;background:#64748b2e;color:#cbd5e1}.tz-va__chip--listening{background:#16a34a2e;color:#a7f3d0}.tz-va__chip--transcribing{background:#3b82f629;color:#bfdbfe}.tz-va__chip--thinking{background:#8b5cf62e;color:#ddd6fe}.tz-va__chip--speaking{background:#eab3082e;color:#fde68a}.tz-va__info{display:grid;gap:4px;font-size:.85rem}.tz-va__info-row{display:flex;gap:12px}.tz-va__info-label{flex:0 0 56px;color:#94a3b8;text-transform:uppercase;letter-spacing:.06em;font-size:.72rem;padding-top:2px}.tz-va__info-value{color:#e2e8f0;overflow-wrap:anywhere}.tz-va__progress{display:grid;gap:5px;margin-bottom:8px}.tz-va__progress label{display:flex;flex-direction:row;align-items:center;gap:10px;font-size:.76rem}.tz-va__progress .tz-va__bar{flex:1}.tz-va__bar{height:5px;border-radius:999px;background:#94a3b81f;overflow:hidden}.tz-va__bar>span{display:block;height:100%;background:linear-gradient(90deg,#60a5fa,#facc15);width:0%;transition:width .2s ease}.tz-va__button{display:flex;justify-content:center;padding:12px 0 8px}.tz-va__input-row{display:flex;padding:4px 0 8px}.tz-va__input{flex:1;padding:9px 14px;border-radius:999px;border:1px solid rgba(255,255,255,.12);background:#ffffff12;color:inherit;font-size:.9rem;outline:none;transition:border-color .18s,background .18s}.tz-va__input::placeholder{opacity:.45}.tz-va__input:focus{border-color:#f973168c;background:#ffffff1a}.tz-va__input:disabled{opacity:.4;cursor:not-allowed}.tz-va__note{color:#94a3b8;font-size:.8rem;line-height:1.5;margin:0}.tz-va__transcript{display:grid;gap:6px;max-height:380px;overflow-y:auto;scroll-behavior:smooth}.tz-va__transcript::-webkit-scrollbar{width:4px}.tz-va__transcript::-webkit-scrollbar-track{background:transparent}.tz-va__transcript::-webkit-scrollbar-thumb{background:#94a3b840;border-radius:2px}.tz-va__message{display:grid;gap:2px;padding:8px 12px;border-radius:10px;background:#1e293bcc}.tz-va__message p{margin:0;font-size:.92rem;line-height:1.5}.tz-va__message small{color:#64748b;font-size:.7rem;text-transform:uppercase;letter-spacing:.05em}.tz-va__message--user{border-left:3px solid #60a5fa}.tz-va__message--assistant{border-left:3px solid #facc15}.tz-va__message--streaming{border-left-color:#475569;opacity:.85}.tz-va__timings{display:flex;flex-wrap:wrap;gap:10px;margin-top:4px;font-size:.72rem;color:#94a3b8;font-variant-numeric:tabular-nums}@keyframes tz-va-cursor-blink{0%,49%{opacity:1}50%,to{opacity:0}}.tz-va__cursor{display:inline-block;width:2px;height:.85em;background:#94a3b8;margin-left:2px;vertical-align:text-bottom;border-radius:1px;animation:tz-va-cursor-blink .7s step-end infinite}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tz-voice-chat",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "React hook for a full voice-chat pipeline: VAD → Whisper STT → custom AI processor → Kokoro/OpenAI/WebSpeech TTS.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -10,10 +10,12 @@
|
|
|
10
10
|
"import": "./dist/index.js",
|
|
11
11
|
"require": "./dist/index.cjs"
|
|
12
12
|
},
|
|
13
|
-
"./style.css": "./dist/style.css"
|
|
13
|
+
"./style.css": "./dist/style.css",
|
|
14
|
+
"./server": "./server/index.js"
|
|
14
15
|
},
|
|
15
16
|
"files": [
|
|
16
17
|
"dist",
|
|
18
|
+
"server",
|
|
17
19
|
"README.md"
|
|
18
20
|
],
|
|
19
21
|
"scripts": {
|
|
@@ -22,13 +24,25 @@
|
|
|
22
24
|
},
|
|
23
25
|
"keywords": ["react", "hook", "voice", "chat", "speech", "stt", "tts", "whisper", "kokoro", "vad", "ai"],
|
|
24
26
|
"license": "MIT",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/yuriyo/tz-voice-chat.git",
|
|
30
|
+
"directory": "packages/tz-voice-chat"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/yuriyo/tz-voice-chat#readme",
|
|
33
|
+
"bugs": "https://github.com/yuriyo/tz-voice-chat/issues",
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
25
37
|
"peerDependencies": {
|
|
26
38
|
"react": ">=18",
|
|
27
39
|
"@huggingface/transformers": ">=3",
|
|
28
40
|
"@ricky0123/vad-web": ">=0.0.29",
|
|
29
|
-
"kokoro-js": "*"
|
|
41
|
+
"kokoro-js": "*",
|
|
42
|
+
"express": ">=4"
|
|
30
43
|
},
|
|
31
44
|
"peerDependenciesMeta": {
|
|
32
|
-
"kokoro-js": { "optional": true }
|
|
45
|
+
"kokoro-js": { "optional": true },
|
|
46
|
+
"express": { "optional": true }
|
|
33
47
|
}
|
|
34
48
|
}
|
package/server/index.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tz-voice-chat/server — Express router that backs the voice-assistant client.
|
|
3
|
+
*
|
|
4
|
+
* import express from 'express';
|
|
5
|
+
* import { createVoiceAssistantRouter } from 'tz-voice-chat/server';
|
|
6
|
+
*
|
|
7
|
+
* const app = express();
|
|
8
|
+
* const { router, ready } = createVoiceAssistantRouter(); // configured from env
|
|
9
|
+
* app.use('/api', router);
|
|
10
|
+
* await ready(); // pre-load server-side Kokoro
|
|
11
|
+
*
|
|
12
|
+
* Routes (relative to the mount point): GET /config, GET /stats, POST /chat,
|
|
13
|
+
* POST /tts, GET /models/* (Hugging Face model proxy).
|
|
14
|
+
*
|
|
15
|
+
* Every option defaults to the matching environment variable, so
|
|
16
|
+
* `createVoiceAssistantRouter()` with a populated .env is enough.
|
|
17
|
+
* Requires the optional peer deps `express` and (for TTS_PROVIDER=server-kokoro) `kokoro-js`.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import express from 'express';
|
|
21
|
+
import { Readable } from 'node:stream';
|
|
22
|
+
import { configureKokoro, loadKokoro, generateWav } from './tts.js';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {object} [options] Any value overrides its environment variable default.
|
|
26
|
+
* @param {'openrouter'|'huggingface'} [options.provider] OPENAI_PROVIDER (default 'openrouter')
|
|
27
|
+
* @param {string} [options.openrouterApiKey] OPENROUTER_API_KEY
|
|
28
|
+
* @param {string} [options.openrouterModel] OPENROUTER_MODEL (default 'gpt-4o-mini')
|
|
29
|
+
* @param {string} [options.huggingfaceApiKey] HUGGINGFACE_API_KEY
|
|
30
|
+
* @param {string} [options.ttsProvider] TTS_PROVIDER: server-kokoro | openai | kokoro | webspeech
|
|
31
|
+
* @param {string} [options.kokoroVoice] KOKORO_VOICE
|
|
32
|
+
* @param {string} [options.kokoroDtype] KOKORO_DTYPE
|
|
33
|
+
* @param {number} [options.kokoroSpeed] KOKORO_SPEED speech-rate multiplier (default 1)
|
|
34
|
+
* @param {string} [options.kokoroDevice] KOKORO_DEVICE (server-side)
|
|
35
|
+
* @param {string} [options.kokoroBrowserDevice] KOKORO_BROWSER_DEVICE
|
|
36
|
+
* @param {string} [options.openaiApiKey] OPENAI_API_KEY (for ttsProvider 'openai')
|
|
37
|
+
* @param {string} [options.openaiTtsModel] OPENAI_TTS_MODEL
|
|
38
|
+
* @param {string} [options.openaiTtsVoice] OPENAI_TTS_VOICE
|
|
39
|
+
* @param {string} [options.whisperModel] WHISPER_MODEL
|
|
40
|
+
* @param {string} [options.whisperDtype] WHISPER_DTYPE
|
|
41
|
+
* @param {string} [options.whisperDevice] WHISPER_DEVICE
|
|
42
|
+
* @param {string} [options.webSpeechVoice] WEBSPEECH_VOICE
|
|
43
|
+
* @param {number} [options.webSpeechRate] WEBSPEECH_RATE
|
|
44
|
+
* @param {number} [options.webSpeechPitch] WEBSPEECH_PITCH
|
|
45
|
+
* @returns {{ router: import('express').Router, ready: () => Promise<void> }}
|
|
46
|
+
*/
|
|
47
|
+
export function createVoiceAssistantRouter(options = {}) {
|
|
48
|
+
const env = process.env;
|
|
49
|
+
const o = {
|
|
50
|
+
provider: env.OPENAI_PROVIDER || 'openrouter',
|
|
51
|
+
openrouterApiKey: env.OPENROUTER_API_KEY,
|
|
52
|
+
openrouterModel: env.OPENROUTER_MODEL || 'gpt-4o-mini',
|
|
53
|
+
huggingfaceApiKey: env.HUGGINGFACE_API_KEY,
|
|
54
|
+
ttsProvider: env.TTS_PROVIDER || 'server-kokoro',
|
|
55
|
+
kokoroVoice: env.KOKORO_VOICE || 'af_bella',
|
|
56
|
+
kokoroDtype: env.KOKORO_DTYPE || 'q8',
|
|
57
|
+
kokoroDevice: env.KOKORO_DEVICE || 'cpu',
|
|
58
|
+
kokoroSpeed: parseFloat(env.KOKORO_SPEED ?? '1') || 1,
|
|
59
|
+
kokoroBrowserDevice: env.KOKORO_BROWSER_DEVICE || 'wasm',
|
|
60
|
+
openaiApiKey: env.OPENAI_API_KEY || '',
|
|
61
|
+
openaiTtsModel: env.OPENAI_TTS_MODEL || 'tts-1',
|
|
62
|
+
openaiTtsVoice: env.OPENAI_TTS_VOICE || 'alloy',
|
|
63
|
+
whisperModel: env.WHISPER_MODEL || 'onnx-community/whisper-tiny.en',
|
|
64
|
+
whisperDtype: env.WHISPER_DTYPE || 'q8',
|
|
65
|
+
whisperDevice: env.WHISPER_DEVICE || 'cpu',
|
|
66
|
+
webSpeechVoice: env.WEBSPEECH_VOICE || '',
|
|
67
|
+
webSpeechRate: parseFloat(env.WEBSPEECH_RATE ?? '1'),
|
|
68
|
+
webSpeechPitch: parseFloat(env.WEBSPEECH_PITCH ?? '1'),
|
|
69
|
+
...options,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
configureKokoro({ dtype: o.kokoroDtype, device: o.kokoroDevice, voice: o.kokoroVoice });
|
|
73
|
+
|
|
74
|
+
const router = express.Router();
|
|
75
|
+
router.use(express.json());
|
|
76
|
+
|
|
77
|
+
// Tells the client how it is configured so it can pick the server proxy or
|
|
78
|
+
// fall back to an in-browser LLM, and which TTS/STT settings to use.
|
|
79
|
+
router.get('/config', (req, res) => {
|
|
80
|
+
res.json({
|
|
81
|
+
hasApiKey: Boolean(o.openrouterApiKey) || Boolean(o.huggingfaceApiKey),
|
|
82
|
+
chatProvider: o.provider,
|
|
83
|
+
chatModel: o.provider === 'huggingface' ? 'gpt2' : o.openrouterModel,
|
|
84
|
+
kokoroDevice: o.kokoroDevice,
|
|
85
|
+
kokoroSpeed: o.kokoroSpeed,
|
|
86
|
+
kokoroDtype: o.kokoroDtype,
|
|
87
|
+
kokoroBrowserDevice: o.kokoroBrowserDevice,
|
|
88
|
+
whisperModel: o.whisperModel,
|
|
89
|
+
whisperDtype: o.whisperDtype,
|
|
90
|
+
whisperDevice: o.whisperDevice,
|
|
91
|
+
ttsProvider: o.ttsProvider,
|
|
92
|
+
kokoroVoice: o.kokoroVoice,
|
|
93
|
+
openaiTtsModel: o.openaiTtsModel,
|
|
94
|
+
openaiTtsVoice: o.openaiTtsVoice,
|
|
95
|
+
webSpeechVoice: o.webSpeechVoice,
|
|
96
|
+
webSpeechRate: o.webSpeechRate,
|
|
97
|
+
webSpeechPitch: o.webSpeechPitch,
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// Server process memory, shown in the client's info card.
|
|
102
|
+
router.get('/stats', (req, res) => {
|
|
103
|
+
const { rss, heapUsed } = process.memoryUsage();
|
|
104
|
+
res.json({ rss, heapUsed });
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
router.post('/chat', async (req, res) => {
|
|
108
|
+
const { messages } = req.body;
|
|
109
|
+
if (!messages || !Array.isArray(messages)) {
|
|
110
|
+
return res.status(400).send('Invalid request payload');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
if (o.provider === 'huggingface') {
|
|
115
|
+
if (!o.huggingfaceApiKey) {
|
|
116
|
+
return res.status(500).send('Hugging Face API key is missing');
|
|
117
|
+
}
|
|
118
|
+
const prompt = messages.map(m => `${m.role}: ${m.content}`).join('\n') + '\nassistant:';
|
|
119
|
+
const response = await fetch('https://api-inference.huggingface.co/models/gpt2', {
|
|
120
|
+
method: 'POST',
|
|
121
|
+
headers: {
|
|
122
|
+
Authorization: `Bearer ${o.huggingfaceApiKey}`,
|
|
123
|
+
'Content-Type': 'application/json',
|
|
124
|
+
},
|
|
125
|
+
body: JSON.stringify({ inputs: prompt, parameters: { max_new_tokens: 200, return_full_text: false } }),
|
|
126
|
+
});
|
|
127
|
+
const data = await response.json();
|
|
128
|
+
if (data.error) return res.status(500).send(data.error);
|
|
129
|
+
const text = Array.isArray(data) ? data[0]?.generated_text || '' : data.generated_text || '';
|
|
130
|
+
res.setHeader('Content-Type', 'text/plain;charset=UTF-8');
|
|
131
|
+
res.write(text);
|
|
132
|
+
res.end();
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (!o.openrouterApiKey) {
|
|
137
|
+
return res.status(500).send('OpenRouter API key is missing');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
|
|
141
|
+
method: 'POST',
|
|
142
|
+
headers: {
|
|
143
|
+
Authorization: `Bearer ${o.openrouterApiKey}`,
|
|
144
|
+
'Content-Type': 'application/json',
|
|
145
|
+
},
|
|
146
|
+
body: JSON.stringify({
|
|
147
|
+
model: o.openrouterModel,
|
|
148
|
+
messages,
|
|
149
|
+
stream: true,
|
|
150
|
+
max_tokens: 1250,
|
|
151
|
+
temperature: 0.7,
|
|
152
|
+
}),
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
if (!response.ok) {
|
|
156
|
+
return res.status(response.status).send(await response.text());
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
res.setHeader('Content-Type', 'text/plain;charset=UTF-8');
|
|
160
|
+
const reader = response.body.getReader();
|
|
161
|
+
const decoder = new TextDecoder();
|
|
162
|
+
while (true) {
|
|
163
|
+
const { done, value } = await reader.read();
|
|
164
|
+
if (done) break;
|
|
165
|
+
res.write(decoder.decode(value, { stream: true }));
|
|
166
|
+
}
|
|
167
|
+
res.end();
|
|
168
|
+
} catch (err) {
|
|
169
|
+
console.error(err);
|
|
170
|
+
res.status(500).send(err.message || 'Unexpected proxy error');
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
router.post('/tts', async (req, res) => {
|
|
175
|
+
const { text, voice, model, speed } = req.body;
|
|
176
|
+
if (!text) return res.status(400).send('text is required');
|
|
177
|
+
|
|
178
|
+
try {
|
|
179
|
+
if (o.ttsProvider === 'server-kokoro') {
|
|
180
|
+
const wavBuf = await generateWav(text, voice || o.kokoroVoice, Number(speed) > 0 ? Number(speed) : 1);
|
|
181
|
+
res.setHeader('Content-Type', 'audio/wav');
|
|
182
|
+
res.setHeader('Content-Length', String(wavBuf.length));
|
|
183
|
+
return res.end(wavBuf);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (o.ttsProvider === 'openai') {
|
|
187
|
+
if (!o.openaiApiKey) return res.status(500).send('OPENAI_API_KEY is not configured');
|
|
188
|
+
const response = await fetch('https://api.openai.com/v1/audio/speech', {
|
|
189
|
+
method: 'POST',
|
|
190
|
+
headers: {
|
|
191
|
+
Authorization: `Bearer ${o.openaiApiKey}`,
|
|
192
|
+
'Content-Type': 'application/json',
|
|
193
|
+
},
|
|
194
|
+
body: JSON.stringify({
|
|
195
|
+
model: model || o.openaiTtsModel,
|
|
196
|
+
voice: voice || o.openaiTtsVoice,
|
|
197
|
+
input: text,
|
|
198
|
+
}),
|
|
199
|
+
});
|
|
200
|
+
if (!response.ok) {
|
|
201
|
+
return res.status(response.status).send(await response.text());
|
|
202
|
+
}
|
|
203
|
+
res.setHeader('Content-Type', 'audio/mpeg');
|
|
204
|
+
const reader = response.body.getReader();
|
|
205
|
+
while (true) {
|
|
206
|
+
const { done, value } = await reader.read();
|
|
207
|
+
if (done) break;
|
|
208
|
+
res.write(value);
|
|
209
|
+
}
|
|
210
|
+
return res.end();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// 'kokoro' (browser) / 'webspeech' — the client synthesises audio itself.
|
|
214
|
+
return res.status(400).send(`TTS_PROVIDER "${o.ttsProvider}" does not use a server-side audio endpoint`);
|
|
215
|
+
} catch (err) {
|
|
216
|
+
console.error('[/tts]', err);
|
|
217
|
+
res.status(500).send(err.message || 'TTS error');
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// HuggingFace model proxy. Keeps all model traffic on our own origin: the browser
|
|
222
|
+
// (transformers.js with env.remoteHost = '<mount>/models/') requests
|
|
223
|
+
// GET /models/org/repo/resolve/revision/path/to/file
|
|
224
|
+
// which we proxy to https://huggingface.co/org/repo/resolve/revision/path/to/file.
|
|
225
|
+
// LFS redirects are followed server-side and the body is streamed, never buffered.
|
|
226
|
+
router.get('/models/*', async (req, res) => {
|
|
227
|
+
const hfPath = req.params[0];
|
|
228
|
+
|
|
229
|
+
// Allow only safe path characters — prevent open-redirect / SSRF abuse.
|
|
230
|
+
if (!/^[\w][\w.\-/]*$/.test(hfPath)) {
|
|
231
|
+
return res.status(400).json({ error: 'Invalid model path' });
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
try {
|
|
235
|
+
const upstream = await fetch(`https://huggingface.co/${hfPath}`, {
|
|
236
|
+
headers: {
|
|
237
|
+
'User-Agent': 'tz-voice-chat/1.0',
|
|
238
|
+
...(req.headers.range ? { Range: req.headers.range } : {}),
|
|
239
|
+
},
|
|
240
|
+
redirect: 'follow',
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
res.status(upstream.status);
|
|
244
|
+
for (const h of ['content-type', 'content-length', 'content-range', 'accept-ranges', 'cache-control', 'etag']) {
|
|
245
|
+
const v = upstream.headers.get(h);
|
|
246
|
+
if (v) res.setHeader(h, v);
|
|
247
|
+
}
|
|
248
|
+
Readable.fromWeb(upstream.body).pipe(res);
|
|
249
|
+
} catch (err) {
|
|
250
|
+
console.error('[/models proxy]', err.message);
|
|
251
|
+
if (!res.headersSent) res.status(502).json({ error: 'Model proxy failed' });
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// Load server-side Kokoro eagerly so the first TTS request has no cold start.
|
|
256
|
+
async function ready() {
|
|
257
|
+
if (o.ttsProvider === 'server-kokoro') await loadKokoro();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return { router, ready };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export { loadKokoro, generateWav };
|
package/server/tts.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* server/tts.js — Kokoro TTS running in Node.js via kokoro-js.
|
|
3
|
+
*
|
|
4
|
+
* The model is loaded once and kept in memory for the life of the process.
|
|
5
|
+
* Call loadKokoro() at startup for eager pre-loading; generateWav() lazy-loads
|
|
6
|
+
* on demand if the startup call was skipped.
|
|
7
|
+
*
|
|
8
|
+
* Adding a new provider in the future:
|
|
9
|
+
* Just add another export or another branch in server/index.js — this module
|
|
10
|
+
* stays Kokoro-only and the server routes to whichever backend it needs.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { KokoroTTS } from 'kokoro-js';
|
|
14
|
+
|
|
15
|
+
let DTYPE = process.env.KOKORO_DTYPE || 'q8';
|
|
16
|
+
let DEVICE = process.env.KOKORO_DEVICE || 'cpu'; // Node onnxruntime: cpu
|
|
17
|
+
let DEFAULT_VOICE = process.env.KOKORO_VOICE || 'af_bella';
|
|
18
|
+
|
|
19
|
+
/** Override the env-derived defaults (call before the model is first loaded). */
|
|
20
|
+
export function configureKokoro({ dtype, device, voice } = {}) {
|
|
21
|
+
if (dtype) DTYPE = dtype;
|
|
22
|
+
if (device) DEVICE = device;
|
|
23
|
+
if (voice) DEFAULT_VOICE = voice;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let _model = null;
|
|
27
|
+
let _loadPromise = null;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Start loading (or return the already-loaded) Kokoro model.
|
|
31
|
+
* Safe to call multiple times — returns the same Promise until the model is
|
|
32
|
+
* ready, then resolves immediately on every subsequent call.
|
|
33
|
+
* @returns {Promise<KokoroTTS>}
|
|
34
|
+
*/
|
|
35
|
+
export function loadKokoro() {
|
|
36
|
+
if (_model) return Promise.resolve(_model);
|
|
37
|
+
if (_loadPromise) return _loadPromise;
|
|
38
|
+
|
|
39
|
+
console.log(`[Kokoro] Loading model (dtype=${DTYPE}) …`);
|
|
40
|
+
|
|
41
|
+
_loadPromise = KokoroTTS.from_pretrained('onnx-community/Kokoro-82M-ONNX', {
|
|
42
|
+
dtype: DTYPE,
|
|
43
|
+
device: DEVICE,
|
|
44
|
+
}).then(model => {
|
|
45
|
+
_model = model;
|
|
46
|
+
_loadPromise = null;
|
|
47
|
+
console.log('[Kokoro] Model ready.');
|
|
48
|
+
return _model;
|
|
49
|
+
}).catch(err => {
|
|
50
|
+
_loadPromise = null;
|
|
51
|
+
throw err;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
return _loadPromise;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Generate speech and return a complete WAV file as a Node.js Buffer.
|
|
59
|
+
* @param {string} text
|
|
60
|
+
* @param {string} [voice]
|
|
61
|
+
* @param {number} [speed] speech-rate multiplier (default 1)
|
|
62
|
+
* @returns {Promise<Buffer>}
|
|
63
|
+
*/
|
|
64
|
+
export async function generateWav(text, voice = DEFAULT_VOICE, speed = 1) {
|
|
65
|
+
const tts = await loadKokoro();
|
|
66
|
+
const result = await tts.generate(text, { voice, speed });
|
|
67
|
+
|
|
68
|
+
// kokoro-js returns { audio: Float32Array, sampling_rate: number }
|
|
69
|
+
const samples = result?.audio ?? result;
|
|
70
|
+
const sampleRate = result?.sampling_rate ?? result?.samplingRate ?? 24000;
|
|
71
|
+
|
|
72
|
+
return encodePcmWav(samples, sampleRate);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Encode a mono Float32Array as a standard 16-bit PCM WAV file.
|
|
77
|
+
* @param {Float32Array} samples
|
|
78
|
+
* @param {number} sampleRate
|
|
79
|
+
* @returns {Buffer}
|
|
80
|
+
*/
|
|
81
|
+
function encodePcmWav(samples, sampleRate) {
|
|
82
|
+
const numSamples = samples.length;
|
|
83
|
+
const blockAlign = 2; // 1 channel × 2 bytes (16-bit)
|
|
84
|
+
const byteRate = sampleRate * blockAlign;
|
|
85
|
+
const dataSize = numSamples * blockAlign;
|
|
86
|
+
const buf = Buffer.allocUnsafe(44 + dataSize);
|
|
87
|
+
|
|
88
|
+
// RIFF chunk descriptor
|
|
89
|
+
buf.write('RIFF', 0, 'ascii');
|
|
90
|
+
buf.writeUInt32LE(36 + dataSize, 4);
|
|
91
|
+
buf.write('WAVE', 8, 'ascii');
|
|
92
|
+
|
|
93
|
+
// fmt sub-chunk
|
|
94
|
+
buf.write('fmt ', 12, 'ascii');
|
|
95
|
+
buf.writeUInt32LE(16, 16); // chunk size (PCM = 16)
|
|
96
|
+
buf.writeUInt16LE(1, 20); // audio format: PCM
|
|
97
|
+
buf.writeUInt16LE(1, 22); // num channels: mono
|
|
98
|
+
buf.writeUInt32LE(sampleRate, 24);
|
|
99
|
+
buf.writeUInt32LE(byteRate, 28);
|
|
100
|
+
buf.writeUInt16LE(blockAlign, 32);
|
|
101
|
+
buf.writeUInt16LE(16, 34); // bits per sample
|
|
102
|
+
|
|
103
|
+
// data sub-chunk
|
|
104
|
+
buf.write('data', 36, 'ascii');
|
|
105
|
+
buf.writeUInt32LE(dataSize, 40);
|
|
106
|
+
|
|
107
|
+
// Convert float32 → int16 PCM
|
|
108
|
+
let offset = 44;
|
|
109
|
+
for (let i = 0; i < numSamples; i++) {
|
|
110
|
+
const s = Math.max(-1, Math.min(1, samples[i]));
|
|
111
|
+
buf.writeInt16LE(s < 0 ? Math.round(s * 0x8000) : Math.round(s * 0x7FFF), offset);
|
|
112
|
+
offset += 2;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return buf;
|
|
116
|
+
}
|