@holoscript/core 2.0.0 → 2.0.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.
Files changed (137) hide show
  1. package/LICENSE +21 -0
  2. package/dist/chunk-3N67RLQP.cjs +1298 -0
  3. package/dist/chunk-3N67RLQP.cjs.map +1 -0
  4. package/dist/chunk-3X2EGU7Z.cjs +52 -0
  5. package/dist/chunk-3X2EGU7Z.cjs.map +1 -0
  6. package/dist/chunk-4CV4JOE5.js +24 -0
  7. package/dist/chunk-4CV4JOE5.js.map +1 -0
  8. package/dist/chunk-4OHVW4XR.cjs +1027 -0
  9. package/dist/chunk-4OHVW4XR.cjs.map +1 -0
  10. package/dist/chunk-CZLDE2OZ.cjs +28 -0
  11. package/dist/chunk-CZLDE2OZ.cjs.map +1 -0
  12. package/{src/HoloScriptRuntime.ts → dist/chunk-EU6CZMGJ.js} +437 -794
  13. package/dist/chunk-EU6CZMGJ.js.map +1 -0
  14. package/dist/chunk-KWYIVRIH.js +344 -0
  15. package/dist/chunk-KWYIVRIH.js.map +1 -0
  16. package/dist/chunk-MCP6D4LT.js +1025 -0
  17. package/dist/chunk-MCP6D4LT.js.map +1 -0
  18. package/dist/chunk-SATNCODL.js +45 -0
  19. package/dist/chunk-SATNCODL.js.map +1 -0
  20. package/dist/chunk-VMZN4EVR.cjs +347 -0
  21. package/dist/chunk-VMZN4EVR.cjs.map +1 -0
  22. package/{src/HoloScriptDebugger.ts → dist/chunk-VYIDLUCV.js} +118 -257
  23. package/dist/chunk-VYIDLUCV.js.map +1 -0
  24. package/dist/chunk-WFI4T3XB.cjs +424 -0
  25. package/dist/chunk-WFI4T3XB.cjs.map +1 -0
  26. package/dist/debugger.cjs +20 -0
  27. package/dist/debugger.cjs.map +1 -0
  28. package/dist/debugger.d.cts +171 -0
  29. package/dist/debugger.d.ts +171 -0
  30. package/dist/debugger.js +7 -0
  31. package/dist/debugger.js.map +1 -0
  32. package/dist/index.cjs +6006 -0
  33. package/dist/index.cjs.map +1 -0
  34. package/dist/index.d.cts +2482 -0
  35. package/dist/index.d.ts +2482 -0
  36. package/dist/index.js +5926 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/parser.cjs +14 -0
  39. package/dist/parser.cjs.map +1 -0
  40. package/dist/parser.d.cts +139 -0
  41. package/dist/parser.d.ts +139 -0
  42. package/dist/parser.js +5 -0
  43. package/dist/parser.js.map +1 -0
  44. package/dist/runtime.cjs +14 -0
  45. package/dist/runtime.cjs.map +1 -0
  46. package/dist/runtime.d.cts +180 -0
  47. package/dist/runtime.d.ts +180 -0
  48. package/dist/runtime.js +5 -0
  49. package/dist/runtime.js.map +1 -0
  50. package/dist/type-checker.cjs +17 -0
  51. package/dist/type-checker.cjs.map +1 -0
  52. package/dist/type-checker.d.cts +105 -0
  53. package/dist/type-checker.d.ts +105 -0
  54. package/dist/type-checker.js +4 -0
  55. package/dist/type-checker.js.map +1 -0
  56. package/dist/types-D6g4ACjP.d.cts +262 -0
  57. package/dist/types-D6g4ACjP.d.ts +262 -0
  58. package/package.json +11 -8
  59. package/src/HoloScript2DParser.js +0 -227
  60. package/src/HoloScript2DParser.ts +0 -261
  61. package/src/HoloScriptCodeParser.js +0 -1102
  62. package/src/HoloScriptCodeParser.ts +0 -1188
  63. package/src/HoloScriptDebugger.js +0 -458
  64. package/src/HoloScriptParser.js +0 -338
  65. package/src/HoloScriptParser.ts +0 -397
  66. package/src/HoloScriptPlusParser.js +0 -371
  67. package/src/HoloScriptPlusParser.ts +0 -543
  68. package/src/HoloScriptRuntime.js +0 -1399
  69. package/src/HoloScriptRuntime.test.js +0 -351
  70. package/src/HoloScriptRuntime.test.ts +0 -436
  71. package/src/HoloScriptTypeChecker.js +0 -356
  72. package/src/HoloScriptTypeChecker.ts +0 -475
  73. package/src/__tests__/GraphicsServices.test.js +0 -357
  74. package/src/__tests__/GraphicsServices.test.ts +0 -427
  75. package/src/__tests__/HoloScriptPlusParser.test.js +0 -317
  76. package/src/__tests__/HoloScriptPlusParser.test.ts +0 -392
  77. package/src/__tests__/integration.test.js +0 -336
  78. package/src/__tests__/integration.test.ts +0 -416
  79. package/src/__tests__/performance.bench.js +0 -218
  80. package/src/__tests__/performance.bench.ts +0 -262
  81. package/src/__tests__/type-checker.test.js +0 -60
  82. package/src/__tests__/type-checker.test.ts +0 -73
  83. package/src/index.js +0 -217
  84. package/src/index.ts +0 -426
  85. package/src/interop/Interoperability.js +0 -413
  86. package/src/interop/Interoperability.ts +0 -494
  87. package/src/logger.js +0 -42
  88. package/src/logger.ts +0 -57
  89. package/src/parser/EnhancedParser.js +0 -205
  90. package/src/parser/EnhancedParser.ts +0 -251
  91. package/src/parser/HoloScriptPlusParser.js +0 -928
  92. package/src/parser/HoloScriptPlusParser.ts +0 -1089
  93. package/src/runtime/HoloScriptPlusRuntime.js +0 -674
  94. package/src/runtime/HoloScriptPlusRuntime.ts +0 -861
  95. package/src/runtime/PerformanceTelemetry.js +0 -323
  96. package/src/runtime/PerformanceTelemetry.ts +0 -467
  97. package/src/runtime/RuntimeOptimization.js +0 -361
  98. package/src/runtime/RuntimeOptimization.ts +0 -416
  99. package/src/services/HololandGraphicsPipelineService.js +0 -506
  100. package/src/services/HololandGraphicsPipelineService.ts +0 -662
  101. package/src/services/PlatformPerformanceOptimizer.js +0 -356
  102. package/src/services/PlatformPerformanceOptimizer.ts +0 -503
  103. package/src/state/ReactiveState.js +0 -427
  104. package/src/state/ReactiveState.ts +0 -572
  105. package/src/tools/DeveloperExperience.js +0 -376
  106. package/src/tools/DeveloperExperience.ts +0 -438
  107. package/src/traits/AIDriverTrait.js +0 -322
  108. package/src/traits/AIDriverTrait.test.js +0 -329
  109. package/src/traits/AIDriverTrait.test.ts +0 -357
  110. package/src/traits/AIDriverTrait.ts +0 -474
  111. package/src/traits/LightingTrait.js +0 -313
  112. package/src/traits/LightingTrait.test.js +0 -410
  113. package/src/traits/LightingTrait.test.ts +0 -462
  114. package/src/traits/LightingTrait.ts +0 -505
  115. package/src/traits/MaterialTrait.js +0 -194
  116. package/src/traits/MaterialTrait.test.js +0 -286
  117. package/src/traits/MaterialTrait.test.ts +0 -329
  118. package/src/traits/MaterialTrait.ts +0 -324
  119. package/src/traits/RenderingTrait.js +0 -356
  120. package/src/traits/RenderingTrait.test.js +0 -363
  121. package/src/traits/RenderingTrait.test.ts +0 -427
  122. package/src/traits/RenderingTrait.ts +0 -555
  123. package/src/traits/VRTraitSystem.js +0 -740
  124. package/src/traits/VRTraitSystem.ts +0 -1040
  125. package/src/traits/VoiceInputTrait.js +0 -284
  126. package/src/traits/VoiceInputTrait.test.js +0 -226
  127. package/src/traits/VoiceInputTrait.test.ts +0 -252
  128. package/src/traits/VoiceInputTrait.ts +0 -401
  129. package/src/types/AdvancedTypeSystem.js +0 -226
  130. package/src/types/AdvancedTypeSystem.ts +0 -494
  131. package/src/types/HoloScriptPlus.d.ts +0 -853
  132. package/src/types.js +0 -6
  133. package/src/types.ts +0 -369
  134. package/tsconfig.json +0 -23
  135. package/tsup.config.d.ts +0 -2
  136. package/tsup.config.js +0 -18
  137. package/tsup.config.ts +0 -19
@@ -1,252 +0,0 @@
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
- });
@@ -1,401 +0,0 @@
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
- }