@holoscript/core 1.0.0-alpha.1 → 2.0.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 (127) hide show
  1. package/package.json +10 -9
  2. package/src/HoloScript2DParser.js +227 -0
  3. package/src/HoloScript2DParser.ts +5 -0
  4. package/src/HoloScriptCodeParser.js +1102 -0
  5. package/src/HoloScriptCodeParser.ts +145 -20
  6. package/src/HoloScriptDebugger.js +458 -0
  7. package/src/HoloScriptParser.js +338 -0
  8. package/src/HoloScriptPlusParser.js +371 -0
  9. package/src/HoloScriptPlusParser.ts +543 -0
  10. package/src/HoloScriptRuntime.js +1399 -0
  11. package/src/HoloScriptRuntime.test.js +351 -0
  12. package/src/HoloScriptRuntime.ts +257 -3
  13. package/src/HoloScriptTypeChecker.js +356 -0
  14. package/src/__tests__/GraphicsServices.test.js +357 -0
  15. package/src/__tests__/GraphicsServices.test.ts +427 -0
  16. package/src/__tests__/HoloScriptPlusParser.test.js +317 -0
  17. package/src/__tests__/HoloScriptPlusParser.test.ts +392 -0
  18. package/src/__tests__/integration.test.js +336 -0
  19. package/src/__tests__/performance.bench.js +218 -0
  20. package/src/__tests__/type-checker.test.js +60 -0
  21. package/src/__tests__/type-checker.test.ts +73 -0
  22. package/src/index.js +217 -0
  23. package/src/index.ts +158 -18
  24. package/src/interop/Interoperability.js +413 -0
  25. package/src/interop/Interoperability.ts +494 -0
  26. package/src/logger.js +42 -0
  27. package/src/parser/EnhancedParser.js +205 -0
  28. package/src/parser/EnhancedParser.ts +251 -0
  29. package/src/parser/HoloScriptPlusParser.js +928 -0
  30. package/src/parser/HoloScriptPlusParser.ts +1089 -0
  31. package/src/runtime/HoloScriptPlusRuntime.js +674 -0
  32. package/src/runtime/HoloScriptPlusRuntime.ts +861 -0
  33. package/src/runtime/PerformanceTelemetry.js +323 -0
  34. package/src/runtime/PerformanceTelemetry.ts +467 -0
  35. package/src/runtime/RuntimeOptimization.js +361 -0
  36. package/src/runtime/RuntimeOptimization.ts +416 -0
  37. package/src/services/HololandGraphicsPipelineService.js +506 -0
  38. package/src/services/HololandGraphicsPipelineService.ts +662 -0
  39. package/src/services/PlatformPerformanceOptimizer.js +356 -0
  40. package/src/services/PlatformPerformanceOptimizer.ts +503 -0
  41. package/src/state/ReactiveState.js +427 -0
  42. package/src/state/ReactiveState.ts +572 -0
  43. package/src/tools/DeveloperExperience.js +376 -0
  44. package/src/tools/DeveloperExperience.ts +438 -0
  45. package/src/traits/AIDriverTrait.js +322 -0
  46. package/src/traits/AIDriverTrait.test.js +329 -0
  47. package/src/traits/AIDriverTrait.test.ts +357 -0
  48. package/src/traits/AIDriverTrait.ts +474 -0
  49. package/src/traits/LightingTrait.js +313 -0
  50. package/src/traits/LightingTrait.test.js +410 -0
  51. package/src/traits/LightingTrait.test.ts +462 -0
  52. package/src/traits/LightingTrait.ts +505 -0
  53. package/src/traits/MaterialTrait.js +194 -0
  54. package/src/traits/MaterialTrait.test.js +286 -0
  55. package/src/traits/MaterialTrait.test.ts +329 -0
  56. package/src/traits/MaterialTrait.ts +324 -0
  57. package/src/traits/RenderingTrait.js +356 -0
  58. package/src/traits/RenderingTrait.test.js +363 -0
  59. package/src/traits/RenderingTrait.test.ts +427 -0
  60. package/src/traits/RenderingTrait.ts +555 -0
  61. package/src/traits/VRTraitSystem.js +740 -0
  62. package/src/traits/VRTraitSystem.ts +1040 -0
  63. package/src/traits/VoiceInputTrait.js +284 -0
  64. package/src/traits/VoiceInputTrait.test.js +226 -0
  65. package/src/traits/VoiceInputTrait.test.ts +252 -0
  66. package/src/traits/VoiceInputTrait.ts +401 -0
  67. package/src/types/AdvancedTypeSystem.js +226 -0
  68. package/src/types/AdvancedTypeSystem.ts +494 -0
  69. package/src/types/HoloScriptPlus.d.ts +853 -0
  70. package/src/types.js +6 -0
  71. package/src/types.ts +96 -1
  72. package/tsconfig.json +1 -1
  73. package/tsup.config.d.ts +2 -0
  74. package/tsup.config.js +18 -0
  75. package/LICENSE +0 -21
  76. package/dist/chunk-3X2EGU7Z.cjs +0 -52
  77. package/dist/chunk-3X2EGU7Z.cjs.map +0 -1
  78. package/dist/chunk-723TPVHD.js +0 -1074
  79. package/dist/chunk-723TPVHD.js.map +0 -1
  80. package/dist/chunk-EOKNAVDO.cjs +0 -424
  81. package/dist/chunk-EOKNAVDO.cjs.map +0 -1
  82. package/dist/chunk-HQZ3HUMY.js +0 -1087
  83. package/dist/chunk-HQZ3HUMY.js.map +0 -1
  84. package/dist/chunk-KWYIVRIH.js +0 -344
  85. package/dist/chunk-KWYIVRIH.js.map +0 -1
  86. package/dist/chunk-LKH4ZAN6.js +0 -421
  87. package/dist/chunk-LKH4ZAN6.js.map +0 -1
  88. package/dist/chunk-SATNCODL.js +0 -45
  89. package/dist/chunk-SATNCODL.js.map +0 -1
  90. package/dist/chunk-VMZN4EVR.cjs +0 -347
  91. package/dist/chunk-VMZN4EVR.cjs.map +0 -1
  92. package/dist/chunk-VV3UUUYP.cjs +0 -1089
  93. package/dist/chunk-VV3UUUYP.cjs.map +0 -1
  94. package/dist/chunk-XRYTSQHZ.cjs +0 -1076
  95. package/dist/chunk-XRYTSQHZ.cjs.map +0 -1
  96. package/dist/debugger.cjs +0 -19
  97. package/dist/debugger.cjs.map +0 -1
  98. package/dist/debugger.d.cts +0 -171
  99. package/dist/debugger.d.ts +0 -171
  100. package/dist/debugger.js +0 -6
  101. package/dist/debugger.js.map +0 -1
  102. package/dist/index.cjs +0 -755
  103. package/dist/index.cjs.map +0 -1
  104. package/dist/index.d.cts +0 -169
  105. package/dist/index.d.ts +0 -169
  106. package/dist/index.js +0 -699
  107. package/dist/index.js.map +0 -1
  108. package/dist/parser.cjs +0 -13
  109. package/dist/parser.cjs.map +0 -1
  110. package/dist/parser.d.cts +0 -154
  111. package/dist/parser.d.ts +0 -154
  112. package/dist/parser.js +0 -4
  113. package/dist/parser.js.map +0 -1
  114. package/dist/runtime.cjs +0 -13
  115. package/dist/runtime.cjs.map +0 -1
  116. package/dist/runtime.d.cts +0 -147
  117. package/dist/runtime.d.ts +0 -147
  118. package/dist/runtime.js +0 -4
  119. package/dist/runtime.js.map +0 -1
  120. package/dist/type-checker.cjs +0 -16
  121. package/dist/type-checker.cjs.map +0 -1
  122. package/dist/type-checker.d.cts +0 -105
  123. package/dist/type-checker.d.ts +0 -105
  124. package/dist/type-checker.js +0 -3
  125. package/dist/type-checker.js.map +0 -1
  126. package/dist/types-WQSk1Qs2.d.cts +0 -238
  127. package/dist/types-WQSk1Qs2.d.ts +0 -238
@@ -0,0 +1,252 @@
1
+ /**
2
+ * VoiceInputTrait Tests
3
+ *
4
+ * Comprehensive tests for voice recognition and command matching
5
+ */
6
+
7
+ import { describe, it, expect, beforeEach, vi } from 'vitest';
8
+ import { VoiceInputTrait, type VoiceInputConfig, type VoiceCommand } from '../traits/VoiceInputTrait';
9
+
10
+ describe('VoiceInputTrait', () => {
11
+ let trait: VoiceInputTrait;
12
+ let config: VoiceInputConfig;
13
+
14
+ beforeEach(() => {
15
+ config = {
16
+ mode: 'continuous',
17
+ confidenceThreshold: 0.7,
18
+ commands: [
19
+ {
20
+ phrase: 'turn on',
21
+ aliases: ['activate', 'power up'],
22
+ action: 'activate',
23
+ },
24
+ {
25
+ phrase: 'move forward',
26
+ aliases: ['go forward', 'advance'],
27
+ action: 'move',
28
+ params: { direction: 'forward' },
29
+ },
30
+ {
31
+ phrase: 'stop',
32
+ aliases: ['halt', 'pause'],
33
+ action: 'stop',
34
+ },
35
+ ],
36
+ };
37
+ trait = new VoiceInputTrait(config);
38
+ });
39
+
40
+ describe('Initialization', () => {
41
+ it('should initialize with config', () => {
42
+ expect(trait).toBeDefined();
43
+ });
44
+
45
+ it('should not be listening initially', () => {
46
+ expect(trait.isActive()).toBe(false);
47
+ });
48
+
49
+ it('should accept custom confidence threshold', () => {
50
+ const customTrait = new VoiceInputTrait({
51
+ mode: 'push-to-talk',
52
+ confidenceThreshold: 0.85,
53
+ });
54
+ expect(customTrait).toBeDefined();
55
+ });
56
+ });
57
+
58
+ describe('Listening Control', () => {
59
+ it('should start listening', () => {
60
+ trait.startListening();
61
+ // Note: Web Speech API not available in test env, so this just tests the method exists
62
+ expect(trait).toBeDefined();
63
+ });
64
+
65
+ it('should stop listening', () => {
66
+ trait.stopListening();
67
+ expect(trait.isActive()).toBe(false);
68
+ });
69
+
70
+ it('should toggle listening state', () => {
71
+ const initialState = trait.isActive();
72
+ trait.toggleListening();
73
+ // Can't easily test state change without Web Speech API
74
+ expect(trait).toBeDefined();
75
+ });
76
+ });
77
+
78
+ describe('Event Listeners', () => {
79
+ it('should register event listener', () => {
80
+ const listener = vi.fn();
81
+ trait.on(listener);
82
+ expect(listener).not.toHaveBeenCalled();
83
+ });
84
+
85
+ it('should unregister event listener', () => {
86
+ const listener = vi.fn();
87
+ trait.on(listener);
88
+ trait.off(listener);
89
+ expect(trait).toBeDefined();
90
+ });
91
+
92
+ it('should allow multiple listeners', () => {
93
+ const listener1 = vi.fn();
94
+ const listener2 = vi.fn();
95
+ trait.on(listener1);
96
+ trait.on(listener2);
97
+ expect(trait).toBeDefined();
98
+ });
99
+ });
100
+
101
+ describe('Configuration', () => {
102
+ it('should support continuous mode', () => {
103
+ const continuousTrait = new VoiceInputTrait({
104
+ mode: 'continuous',
105
+ confidenceThreshold: 0.7,
106
+ });
107
+ expect(continuousTrait).toBeDefined();
108
+ });
109
+
110
+ it('should support push-to-talk mode', () => {
111
+ const pushTrait = new VoiceInputTrait({
112
+ mode: 'push-to-talk',
113
+ confidenceThreshold: 0.7,
114
+ });
115
+ expect(pushTrait).toBeDefined();
116
+ });
117
+
118
+ it('should support always-listening mode', () => {
119
+ const alwaysTrait = new VoiceInputTrait({
120
+ mode: 'always-listening',
121
+ confidenceThreshold: 0.5,
122
+ });
123
+ expect(alwaysTrait).toBeDefined();
124
+ });
125
+
126
+ it('should accept multiple languages', () => {
127
+ const multiLangTrait = new VoiceInputTrait({
128
+ mode: 'continuous',
129
+ confidenceThreshold: 0.7,
130
+ languages: ['en-US', 'es-ES', 'fr-FR'],
131
+ });
132
+ expect(multiLangTrait).toBeDefined();
133
+ });
134
+
135
+ it('should support audio feedback toggle', () => {
136
+ const feedbackTrait = new VoiceInputTrait({
137
+ mode: 'continuous',
138
+ confidenceThreshold: 0.7,
139
+ audioFeedback: true,
140
+ });
141
+ expect(feedbackTrait).toBeDefined();
142
+ });
143
+
144
+ it('should support transcript display', () => {
145
+ const transcriptTrait = new VoiceInputTrait({
146
+ mode: 'continuous',
147
+ confidenceThreshold: 0.7,
148
+ showTranscript: true,
149
+ });
150
+ expect(transcriptTrait).toBeDefined();
151
+ });
152
+ });
153
+
154
+ describe('Command Configuration', () => {
155
+ it('should support command phrases', () => {
156
+ const commands: VoiceCommand[] = [
157
+ { phrase: 'hello', action: 'greet' },
158
+ ];
159
+ const cmdTrait = new VoiceInputTrait({
160
+ mode: 'continuous',
161
+ confidenceThreshold: 0.7,
162
+ commands,
163
+ });
164
+ expect(cmdTrait).toBeDefined();
165
+ });
166
+
167
+ it('should support command aliases', () => {
168
+ const commands: VoiceCommand[] = [
169
+ {
170
+ phrase: 'turn on',
171
+ aliases: ['activate', 'power up', 'enable'],
172
+ action: 'activate',
173
+ },
174
+ ];
175
+ const cmdTrait = new VoiceInputTrait({
176
+ mode: 'continuous',
177
+ confidenceThreshold: 0.7,
178
+ commands,
179
+ });
180
+ expect(cmdTrait).toBeDefined();
181
+ });
182
+
183
+ it('should support per-command confidence', () => {
184
+ const commands: VoiceCommand[] = [
185
+ {
186
+ phrase: 'critical action',
187
+ action: 'critical',
188
+ confidence: 0.95,
189
+ },
190
+ {
191
+ phrase: 'normal action',
192
+ action: 'normal',
193
+ confidence: 0.7,
194
+ },
195
+ ];
196
+ const cmdTrait = new VoiceInputTrait({
197
+ mode: 'continuous',
198
+ confidenceThreshold: 0.7,
199
+ commands,
200
+ });
201
+ expect(cmdTrait).toBeDefined();
202
+ });
203
+
204
+ it('should support command parameters', () => {
205
+ const commands: VoiceCommand[] = [
206
+ {
207
+ phrase: 'go to location',
208
+ action: 'navigate',
209
+ params: { target: 'location' },
210
+ },
211
+ ];
212
+ const cmdTrait = new VoiceInputTrait({
213
+ mode: 'continuous',
214
+ confidenceThreshold: 0.7,
215
+ commands,
216
+ });
217
+ expect(cmdTrait).toBeDefined();
218
+ });
219
+ });
220
+
221
+ describe('Timeout Configuration', () => {
222
+ it('should support custom timeout', () => {
223
+ const timeoutTrait = new VoiceInputTrait({
224
+ mode: 'continuous',
225
+ confidenceThreshold: 0.7,
226
+ timeout: 5000,
227
+ });
228
+ expect(timeoutTrait).toBeDefined();
229
+ });
230
+
231
+ it('should use default timeout if not specified', () => {
232
+ const defaultTrait = new VoiceInputTrait({
233
+ mode: 'continuous',
234
+ confidenceThreshold: 0.7,
235
+ });
236
+ expect(defaultTrait).toBeDefined();
237
+ });
238
+ });
239
+
240
+ describe('Cleanup', () => {
241
+ it('should dispose resources', () => {
242
+ trait.dispose();
243
+ expect(trait).toBeDefined();
244
+ });
245
+
246
+ it('should stop listening on dispose', () => {
247
+ trait.startListening();
248
+ trait.dispose();
249
+ expect(trait.isActive()).toBe(false);
250
+ });
251
+ });
252
+ });
@@ -0,0 +1,401 @@
1
+ /**
2
+ * @holoscript/core VoiceInput Trait
3
+ *
4
+ * Enables voice-driven interactions for HoloScript+ objects
5
+ * Integrates speech recognition with confidence-based command parsing
6
+ */
7
+
8
+ export type VoiceInputMode = 'continuous' | 'push-to-talk' | 'always-listening';
9
+ export type ConfidenceThreshold = number; // 0.0 to 1.0
10
+
11
+ /**
12
+ * Voice input configuration for HoloScript+ objects
13
+ */
14
+ export interface VoiceInputConfig {
15
+ /** Speech recognition mode */
16
+ mode: VoiceInputMode;
17
+
18
+ /** Minimum confidence level (0-1) to trigger command */
19
+ confidenceThreshold: ConfidenceThreshold;
20
+
21
+ /** Languages supported (BCP 47 codes) */
22
+ languages?: string[];
23
+
24
+ /** Commands this object responds to */
25
+ commands?: VoiceCommand[];
26
+
27
+ /** Enable speech-to-text display */
28
+ showTranscript?: boolean;
29
+
30
+ /** Audio feedback (beep on recognition) */
31
+ audioFeedback?: boolean;
32
+
33
+ /** Max command processing time (ms) */
34
+ timeout?: number;
35
+ }
36
+
37
+ /**
38
+ * Voice command definition
39
+ */
40
+ export interface VoiceCommand {
41
+ /** Primary trigger phrase */
42
+ phrase: string;
43
+
44
+ /** Alternative phrases (fuzzy matching) */
45
+ aliases?: string[];
46
+
47
+ /** Confidence threshold for this specific command */
48
+ confidence?: number;
49
+
50
+ /** Action to execute */
51
+ action: string;
52
+
53
+ /** Parameters extracted from command */
54
+ params?: Record<string, string>;
55
+ }
56
+
57
+ /**
58
+ * Voice recognition result
59
+ */
60
+ export interface VoiceRecognitionResult {
61
+ /** Transcribed text */
62
+ transcript: string;
63
+
64
+ /** Confidence (0-1) */
65
+ confidence: number;
66
+
67
+ /** Is final result or interim? */
68
+ isFinal: boolean;
69
+
70
+ /** Language detected */
71
+ language: string;
72
+
73
+ /** Matched command if any */
74
+ matchedCommand?: VoiceCommand;
75
+
76
+ /** Timestamp */
77
+ timestamp: number;
78
+ }
79
+
80
+ /**
81
+ * Voice input event
82
+ */
83
+ export interface VoiceInputEvent {
84
+ type: 'start' | 'interim' | 'final' | 'error' | 'timeout';
85
+ result: VoiceRecognitionResult;
86
+ hologramId: string;
87
+ }
88
+
89
+ /**
90
+ * VoiceInputTrait - Enables speech recognition on HoloScript+ objects
91
+ */
92
+ export class VoiceInputTrait {
93
+ private config: VoiceInputConfig;
94
+ private recognition: any = null;
95
+ private isListening: boolean = false;
96
+ private listeners: Set<(event: VoiceInputEvent) => void> = new Set();
97
+ private interimTranscript: string = '';
98
+ private commandCache: Map<string, VoiceCommand> = new Map();
99
+
100
+ constructor(config: VoiceInputConfig) {
101
+ this.config = {
102
+ showTranscript: false,
103
+ audioFeedback: true,
104
+ timeout: 10000,
105
+ ...config,
106
+ };
107
+
108
+ this.initializeRecognition();
109
+ this.buildCommandCache();
110
+ }
111
+
112
+ /**
113
+ * Initialize Web Speech API
114
+ */
115
+ private initializeRecognition(): void {
116
+ // Use native Web Speech API or polyfill
117
+ const SpeechRecognition = (globalThis as any).SpeechRecognition ||
118
+ (globalThis as any).webkitSpeechRecognition;
119
+
120
+ if (!SpeechRecognition) {
121
+ console.error('Web Speech API not supported');
122
+ return;
123
+ }
124
+
125
+ this.recognition = new SpeechRecognition();
126
+ this.setupRecognitionHandlers();
127
+ }
128
+
129
+ /**
130
+ * Setup Web Speech API event handlers
131
+ */
132
+ private setupRecognitionHandlers(): void {
133
+ if (!this.recognition) return;
134
+
135
+ this.recognition.continuous = this.config.mode === 'continuous';
136
+ this.recognition.interimResults = true;
137
+ this.recognition.lang = this.config.languages?.[0] || 'en-US';
138
+
139
+ this.recognition.onstart = () => {
140
+ this.isListening = true;
141
+ this.interimTranscript = '';
142
+ if (this.config.audioFeedback) {
143
+ this.playBeep('start');
144
+ }
145
+ };
146
+
147
+ this.recognition.onresult = (event: any) => {
148
+ this.interimTranscript = '';
149
+
150
+ for (let i = event.resultIndex; i < event.results.length; i++) {
151
+ const transcript = event.results[i][0].transcript;
152
+ const confidence = event.results[i][0].confidence;
153
+ const isFinal = event.results[i].isFinal;
154
+
155
+ if (isFinal) {
156
+ this.processVoiceCommand(transcript, confidence);
157
+ } else {
158
+ this.interimTranscript += transcript;
159
+ }
160
+ }
161
+
162
+ if (this.config.showTranscript) {
163
+ this.emitEvent({
164
+ type: 'interim',
165
+ result: {
166
+ transcript: this.interimTranscript,
167
+ confidence: 0,
168
+ isFinal: false,
169
+ language: this.recognition.lang,
170
+ timestamp: Date.now(),
171
+ },
172
+ hologramId: '',
173
+ });
174
+ }
175
+ };
176
+
177
+ this.recognition.onerror = (_event: any) => {
178
+ this.emitEvent({
179
+ type: 'error',
180
+ result: {
181
+ transcript: '',
182
+ confidence: 0,
183
+ isFinal: false,
184
+ language: this.recognition.lang,
185
+ timestamp: Date.now(),
186
+ },
187
+ hologramId: '',
188
+ });
189
+ };
190
+
191
+ this.recognition.onend = () => {
192
+ this.isListening = false;
193
+ if (this.config.audioFeedback) {
194
+ this.playBeep('end');
195
+ }
196
+ };
197
+ }
198
+
199
+ /**
200
+ * Process voice command with fuzzy matching
201
+ */
202
+ private processVoiceCommand(
203
+ transcript: string,
204
+ confidence: number
205
+ ): void {
206
+ if (confidence < this.config.confidenceThreshold) {
207
+ return;
208
+ }
209
+
210
+ const normalized = transcript.toLowerCase().trim();
211
+ let bestMatch: VoiceCommand | null = null;
212
+ let bestScore: number = 0;
213
+
214
+ // Try to find matching command
215
+ for (const command of this.config.commands || []) {
216
+ const cmdConfidence = command.confidence || this.config.confidenceThreshold;
217
+
218
+ // Exact match
219
+ if (normalized === command.phrase.toLowerCase()) {
220
+ if (confidence >= cmdConfidence) {
221
+ bestMatch = command;
222
+ bestScore = 1.0;
223
+ break;
224
+ }
225
+ }
226
+
227
+ // Fuzzy match with aliases
228
+ const allPhrases = [command.phrase, ...(command.aliases || [])];
229
+ for (const phrase of allPhrases) {
230
+ const score = this.fuzzyMatch(normalized, phrase.toLowerCase());
231
+ if (score > bestScore && score >= 0.7) {
232
+ bestScore = score;
233
+ bestMatch = command;
234
+ }
235
+ }
236
+ }
237
+
238
+ // Emit recognition result
239
+ this.emitEvent({
240
+ type: 'final',
241
+ result: {
242
+ transcript: normalized,
243
+ confidence,
244
+ isFinal: true,
245
+ language: this.recognition.lang,
246
+ matchedCommand: bestMatch || undefined,
247
+ timestamp: Date.now(),
248
+ },
249
+ hologramId: '',
250
+ });
251
+
252
+ if (bestMatch) {
253
+ if (this.config.audioFeedback) {
254
+ this.playBeep('success');
255
+ }
256
+ }
257
+ }
258
+
259
+ /**
260
+ * Fuzzy string matching (simple Levenshtein-like approach)
261
+ */
262
+ private fuzzyMatch(input: string, target: string): number {
263
+ if (input === target) return 1.0;
264
+ if (input.length === 0 || target.length === 0) return 0;
265
+
266
+ // Check if input is substring of target
267
+ if (target.includes(input)) {
268
+ return Math.min(1.0, input.length / target.length);
269
+ }
270
+
271
+ // Simple edit distance estimation
272
+ const distance = Math.abs(input.length - target.length);
273
+ const maxLen = Math.max(input.length, target.length);
274
+ return Math.max(0, 1.0 - distance / maxLen);
275
+ }
276
+
277
+ /**
278
+ * Build command index for faster lookup
279
+ */
280
+ private buildCommandCache(): void {
281
+ for (const command of this.config.commands || []) {
282
+ this.commandCache.set(command.phrase.toLowerCase(), command);
283
+ for (const alias of command.aliases || []) {
284
+ this.commandCache.set(alias.toLowerCase(), command);
285
+ }
286
+ }
287
+ }
288
+
289
+ /**
290
+ * Start listening for voice input
291
+ */
292
+ public startListening(): void {
293
+ if (!this.recognition || this.isListening) return;
294
+
295
+ try {
296
+ this.recognition.start();
297
+ } catch (error) {
298
+ console.error('Failed to start speech recognition:', error);
299
+ }
300
+ }
301
+
302
+ /**
303
+ * Stop listening for voice input
304
+ */
305
+ public stopListening(): void {
306
+ if (!this.recognition || !this.isListening) return;
307
+
308
+ try {
309
+ this.recognition.stop();
310
+ } catch (error) {
311
+ console.error('Failed to stop speech recognition:', error);
312
+ }
313
+ }
314
+
315
+ /**
316
+ * Toggle listening state
317
+ */
318
+ public toggleListening(): void {
319
+ if (this.isListening) {
320
+ this.stopListening();
321
+ } else {
322
+ this.startListening();
323
+ }
324
+ }
325
+
326
+ /**
327
+ * Add command listener
328
+ */
329
+ public on(listener: (event: VoiceInputEvent) => void): void {
330
+ this.listeners.add(listener);
331
+ }
332
+
333
+ /**
334
+ * Remove command listener
335
+ */
336
+ public off(listener: (event: VoiceInputEvent) => void): void {
337
+ this.listeners.delete(listener);
338
+ }
339
+
340
+ /**
341
+ * Emit voice event to all listeners
342
+ */
343
+ private emitEvent(event: VoiceInputEvent): void {
344
+ for (const listener of this.listeners) {
345
+ listener(event);
346
+ }
347
+ }
348
+
349
+ /**
350
+ * Play audio feedback beep
351
+ */
352
+ private playBeep(type: 'start' | 'end' | 'success'): void {
353
+ // AudioContext beep generation
354
+ try {
355
+ const audioContext = new (globalThis as any).AudioContext();
356
+ const oscillator = audioContext.createOscillator();
357
+ const gainNode = audioContext.createGain();
358
+
359
+ oscillator.connect(gainNode);
360
+ gainNode.connect(audioContext.destination);
361
+
362
+ const now = audioContext.currentTime;
363
+ const duration = 0.1;
364
+
365
+ // Vary beep frequency by type
366
+ oscillator.frequency.value = type === 'start' ? 800 : type === 'success' ? 1000 : 600;
367
+ gainNode.gain.setValueAtTime(0.3, now);
368
+ gainNode.gain.exponentialRampToValueAtTime(0.01, now + duration);
369
+
370
+ oscillator.start(now);
371
+ oscillator.stop(now + duration);
372
+ } catch (error) {
373
+ // Silently fail if audio not available
374
+ }
375
+ }
376
+
377
+ /**
378
+ * Get current listening state
379
+ */
380
+ public isActive(): boolean {
381
+ return this.isListening;
382
+ }
383
+
384
+ /**
385
+ * Dispose and cleanup
386
+ */
387
+ public dispose(): void {
388
+ if (this.recognition) {
389
+ this.recognition.abort();
390
+ }
391
+ this.listeners.clear();
392
+ this.commandCache.clear();
393
+ }
394
+ }
395
+
396
+ /**
397
+ * HoloScript+ @voice_input trait factory
398
+ */
399
+ export function createVoiceInputTrait(config: VoiceInputConfig): VoiceInputTrait {
400
+ return new VoiceInputTrait(config);
401
+ }