@charivo/stt 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.
@@ -0,0 +1,46 @@
1
+ import { STTTranscriber, STTOptions } from '@charivo/core';
2
+
3
+ /**
4
+ * Web STT Transcriber - Web Speech API를 사용하는 STT Transcriber
5
+ *
6
+ * 브라우저 내장 음성 인식 기능을 사용
7
+ * 녹음 없이 실시간으로 음성을 텍스트로 변환
8
+ *
9
+ * 장점:
10
+ * - API 키 불필요
11
+ * - 무료
12
+ * - 실시간 인식
13
+ * - MediaRecorder 불필요 (마이크 직접 청취)
14
+ *
15
+ * 제약사항:
16
+ * - 브라우저 지원 필요 (Chrome, Edge 등)
17
+ * - 인터넷 연결 필요 (대부분의 브라우저에서)
18
+ */
19
+ declare class WebSTTTranscriber implements STTTranscriber {
20
+ private recognition;
21
+ private recording;
22
+ private isSupported;
23
+ private resolveTranscription?;
24
+ private rejectTranscription?;
25
+ private finalTranscript;
26
+ constructor();
27
+ /**
28
+ * Start real-time speech recognition
29
+ */
30
+ startRecording(options?: STTOptions): Promise<void>;
31
+ /**
32
+ * Stop speech recognition and return transcription
33
+ */
34
+ stopRecording(): Promise<string>;
35
+ /**
36
+ * Check if currently recording
37
+ */
38
+ isRecording(): boolean;
39
+ /**
40
+ * Check if Web Speech API is supported in this browser
41
+ */
42
+ isSupportedBrowser(): boolean;
43
+ }
44
+ declare function createWebSTTTranscriber(): WebSTTTranscriber;
45
+
46
+ export { WebSTTTranscriber, createWebSTTTranscriber };
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/web/index.ts
21
+ var web_exports = {};
22
+ __export(web_exports, {
23
+ WebSTTTranscriber: () => WebSTTTranscriber,
24
+ createWebSTTTranscriber: () => createWebSTTTranscriber
25
+ });
26
+ module.exports = __toCommonJS(web_exports);
27
+ var WebSTTTranscriber = class {
28
+ recognition = null;
29
+ recording = false;
30
+ isSupported;
31
+ resolveTranscription;
32
+ rejectTranscription;
33
+ finalTranscript = "";
34
+ constructor() {
35
+ const SpeechRecognitionAPI = getSpeechRecognitionConstructor();
36
+ this.isSupported = !!SpeechRecognitionAPI;
37
+ if (!this.isSupported) {
38
+ console.warn(
39
+ "Web Speech API is not supported in this browser. STT functionality will be limited."
40
+ );
41
+ }
42
+ }
43
+ /**
44
+ * Start real-time speech recognition
45
+ */
46
+ async startRecording(options) {
47
+ if (!this.isSupported) {
48
+ throw new Error(
49
+ "Web Speech API is not supported in this browser. Please use a supported browser (Chrome, Edge, etc.) or switch to OpenAI/Remote transcriber."
50
+ );
51
+ }
52
+ if (this.recording) {
53
+ throw new Error("Already recording");
54
+ }
55
+ const SpeechRecognitionAPI = getSpeechRecognitionConstructor();
56
+ if (!SpeechRecognitionAPI) {
57
+ throw new Error("Web Speech API is not supported in this browser");
58
+ }
59
+ this.recognition = new SpeechRecognitionAPI();
60
+ this.recognition.continuous = true;
61
+ this.recognition.interimResults = true;
62
+ this.recognition.maxAlternatives = 1;
63
+ this.recognition.lang = options?.language ?? "en-US";
64
+ this.finalTranscript = "";
65
+ return new Promise((resolve, reject) => {
66
+ this.recognition.onstart = () => {
67
+ this.recording = true;
68
+ resolve();
69
+ };
70
+ this.recognition.onresult = (event) => {
71
+ for (let i = event.resultIndex; i < event.results.length; i++) {
72
+ const transcript = event.results[i][0].transcript;
73
+ if (event.results[i].isFinal) {
74
+ this.finalTranscript += transcript + " ";
75
+ }
76
+ }
77
+ };
78
+ this.recognition.onend = () => {
79
+ this.recording = false;
80
+ if (this.resolveTranscription) {
81
+ this.resolveTranscription(this.finalTranscript.trim());
82
+ this.resolveTranscription = void 0;
83
+ this.rejectTranscription = void 0;
84
+ }
85
+ };
86
+ this.recognition.onerror = (event) => {
87
+ this.recording = false;
88
+ const error = new Error(
89
+ `Speech recognition error: ${event.error}${event.message ? " - " + event.message : ""}`
90
+ );
91
+ if (this.rejectTranscription) {
92
+ this.rejectTranscription(error);
93
+ this.resolveTranscription = void 0;
94
+ this.rejectTranscription = void 0;
95
+ }
96
+ reject(error);
97
+ };
98
+ try {
99
+ this.recognition.start();
100
+ } catch (error) {
101
+ reject(error);
102
+ }
103
+ });
104
+ }
105
+ /**
106
+ * Stop speech recognition and return transcription
107
+ */
108
+ async stopRecording() {
109
+ if (!this.recording || !this.recognition) {
110
+ throw new Error("Not recording");
111
+ }
112
+ return new Promise((resolve, reject) => {
113
+ this.resolveTranscription = resolve;
114
+ this.rejectTranscription = reject;
115
+ this.recognition.stop();
116
+ });
117
+ }
118
+ /**
119
+ * Check if currently recording
120
+ */
121
+ isRecording() {
122
+ return this.recording;
123
+ }
124
+ /**
125
+ * Check if Web Speech API is supported in this browser
126
+ */
127
+ isSupportedBrowser() {
128
+ return this.isSupported;
129
+ }
130
+ };
131
+ function createWebSTTTranscriber() {
132
+ return new WebSTTTranscriber();
133
+ }
134
+ function getSpeechRecognitionConstructor() {
135
+ const speechWindow = window;
136
+ return speechWindow.SpeechRecognition ?? speechWindow.webkitSpeechRecognition;
137
+ }
138
+ // Annotate the CommonJS export names for ESM import in node:
139
+ 0 && (module.exports = {
140
+ WebSTTTranscriber,
141
+ createWebSTTTranscriber
142
+ });
@@ -0,0 +1,116 @@
1
+ // src/web/index.ts
2
+ var WebSTTTranscriber = class {
3
+ recognition = null;
4
+ recording = false;
5
+ isSupported;
6
+ resolveTranscription;
7
+ rejectTranscription;
8
+ finalTranscript = "";
9
+ constructor() {
10
+ const SpeechRecognitionAPI = getSpeechRecognitionConstructor();
11
+ this.isSupported = !!SpeechRecognitionAPI;
12
+ if (!this.isSupported) {
13
+ console.warn(
14
+ "Web Speech API is not supported in this browser. STT functionality will be limited."
15
+ );
16
+ }
17
+ }
18
+ /**
19
+ * Start real-time speech recognition
20
+ */
21
+ async startRecording(options) {
22
+ if (!this.isSupported) {
23
+ throw new Error(
24
+ "Web Speech API is not supported in this browser. Please use a supported browser (Chrome, Edge, etc.) or switch to OpenAI/Remote transcriber."
25
+ );
26
+ }
27
+ if (this.recording) {
28
+ throw new Error("Already recording");
29
+ }
30
+ const SpeechRecognitionAPI = getSpeechRecognitionConstructor();
31
+ if (!SpeechRecognitionAPI) {
32
+ throw new Error("Web Speech API is not supported in this browser");
33
+ }
34
+ this.recognition = new SpeechRecognitionAPI();
35
+ this.recognition.continuous = true;
36
+ this.recognition.interimResults = true;
37
+ this.recognition.maxAlternatives = 1;
38
+ this.recognition.lang = options?.language ?? "en-US";
39
+ this.finalTranscript = "";
40
+ return new Promise((resolve, reject) => {
41
+ this.recognition.onstart = () => {
42
+ this.recording = true;
43
+ resolve();
44
+ };
45
+ this.recognition.onresult = (event) => {
46
+ for (let i = event.resultIndex; i < event.results.length; i++) {
47
+ const transcript = event.results[i][0].transcript;
48
+ if (event.results[i].isFinal) {
49
+ this.finalTranscript += transcript + " ";
50
+ }
51
+ }
52
+ };
53
+ this.recognition.onend = () => {
54
+ this.recording = false;
55
+ if (this.resolveTranscription) {
56
+ this.resolveTranscription(this.finalTranscript.trim());
57
+ this.resolveTranscription = void 0;
58
+ this.rejectTranscription = void 0;
59
+ }
60
+ };
61
+ this.recognition.onerror = (event) => {
62
+ this.recording = false;
63
+ const error = new Error(
64
+ `Speech recognition error: ${event.error}${event.message ? " - " + event.message : ""}`
65
+ );
66
+ if (this.rejectTranscription) {
67
+ this.rejectTranscription(error);
68
+ this.resolveTranscription = void 0;
69
+ this.rejectTranscription = void 0;
70
+ }
71
+ reject(error);
72
+ };
73
+ try {
74
+ this.recognition.start();
75
+ } catch (error) {
76
+ reject(error);
77
+ }
78
+ });
79
+ }
80
+ /**
81
+ * Stop speech recognition and return transcription
82
+ */
83
+ async stopRecording() {
84
+ if (!this.recording || !this.recognition) {
85
+ throw new Error("Not recording");
86
+ }
87
+ return new Promise((resolve, reject) => {
88
+ this.resolveTranscription = resolve;
89
+ this.rejectTranscription = reject;
90
+ this.recognition.stop();
91
+ });
92
+ }
93
+ /**
94
+ * Check if currently recording
95
+ */
96
+ isRecording() {
97
+ return this.recording;
98
+ }
99
+ /**
100
+ * Check if Web Speech API is supported in this browser
101
+ */
102
+ isSupportedBrowser() {
103
+ return this.isSupported;
104
+ }
105
+ };
106
+ function createWebSTTTranscriber() {
107
+ return new WebSTTTranscriber();
108
+ }
109
+ function getSpeechRecognitionConstructor() {
110
+ const speechWindow = window;
111
+ return speechWindow.SpeechRecognition ?? speechWindow.webkitSpeechRecognition;
112
+ }
113
+ export {
114
+ WebSTTTranscriber,
115
+ createWebSTTTranscriber
116
+ };
package/package.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "@charivo/stt",
3
+ "version": "0.2.0",
4
+ "description": "STT manager and browser adapters for Charivo",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ },
14
+ "./remote": {
15
+ "types": "./dist/remote/index.d.ts",
16
+ "import": "./dist/remote/index.mjs",
17
+ "require": "./dist/remote/index.js"
18
+ },
19
+ "./openai": {
20
+ "types": "./dist/openai/index.d.ts",
21
+ "import": "./dist/openai/index.mjs",
22
+ "require": "./dist/openai/index.js"
23
+ },
24
+ "./web": {
25
+ "types": "./dist/web/index.d.ts",
26
+ "import": "./dist/web/index.mjs",
27
+ "require": "./dist/web/index.js"
28
+ }
29
+ },
30
+ "keywords": [
31
+ "charivo",
32
+ "stt",
33
+ "speech-to-text",
34
+ "audio",
35
+ "transcription"
36
+ ],
37
+ "dependencies": {
38
+ "openai": "^4.47.1",
39
+ "@charivo/core": "0.7.0"
40
+ },
41
+ "devDependencies": {
42
+ "tsup": "^8.5.0",
43
+ "typescript": "^5.9.2"
44
+ },
45
+ "peerDependencies": {},
46
+ "files": [
47
+ "dist"
48
+ ],
49
+ "publishConfig": {
50
+ "access": "public"
51
+ },
52
+ "license": "MIT",
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "https://github.com/zeikar/charivo.git",
56
+ "directory": "packages/stt"
57
+ },
58
+ "author": {
59
+ "name": "Zeikar",
60
+ "url": "https://github.com/zeikar"
61
+ },
62
+ "homepage": "https://github.com/zeikar/charivo#readme",
63
+ "bugs": {
64
+ "url": "https://github.com/zeikar/charivo/issues"
65
+ },
66
+ "scripts": {
67
+ "build": "tsup",
68
+ "dev": "tsup --watch",
69
+ "test": "vitest",
70
+ "typecheck": "tsc --noEmit",
71
+ "clean": "rm -rf dist"
72
+ }
73
+ }