@holoscript/core 1.0.0-alpha.2 → 2.0.1

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 (74) hide show
  1. package/package.json +2 -2
  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 +17 -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
@@ -0,0 +1,284 @@
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
+ * VoiceInputTrait - Enables speech recognition on HoloScript+ objects
9
+ */
10
+ export class VoiceInputTrait {
11
+ constructor(config) {
12
+ this.recognition = null;
13
+ this.isListening = false;
14
+ this.listeners = new Set();
15
+ this.interimTranscript = '';
16
+ this.commandCache = new Map();
17
+ this.config = {
18
+ showTranscript: false,
19
+ audioFeedback: true,
20
+ timeout: 10000,
21
+ ...config,
22
+ };
23
+ this.initializeRecognition();
24
+ this.buildCommandCache();
25
+ }
26
+ /**
27
+ * Initialize Web Speech API
28
+ */
29
+ initializeRecognition() {
30
+ // Use native Web Speech API or polyfill
31
+ const SpeechRecognition = globalThis.SpeechRecognition ||
32
+ globalThis.webkitSpeechRecognition;
33
+ if (!SpeechRecognition) {
34
+ console.error('Web Speech API not supported');
35
+ return;
36
+ }
37
+ this.recognition = new SpeechRecognition();
38
+ this.setupRecognitionHandlers();
39
+ }
40
+ /**
41
+ * Setup Web Speech API event handlers
42
+ */
43
+ setupRecognitionHandlers() {
44
+ if (!this.recognition)
45
+ return;
46
+ this.recognition.continuous = this.config.mode === 'continuous';
47
+ this.recognition.interimResults = true;
48
+ this.recognition.lang = this.config.languages?.[0] || 'en-US';
49
+ this.recognition.onstart = () => {
50
+ this.isListening = true;
51
+ this.interimTranscript = '';
52
+ if (this.config.audioFeedback) {
53
+ this.playBeep('start');
54
+ }
55
+ };
56
+ this.recognition.onresult = (event) => {
57
+ this.interimTranscript = '';
58
+ for (let i = event.resultIndex; i < event.results.length; i++) {
59
+ const transcript = event.results[i][0].transcript;
60
+ const confidence = event.results[i][0].confidence;
61
+ const isFinal = event.results[i].isFinal;
62
+ if (isFinal) {
63
+ this.processVoiceCommand(transcript, confidence);
64
+ }
65
+ else {
66
+ this.interimTranscript += transcript;
67
+ }
68
+ }
69
+ if (this.config.showTranscript) {
70
+ this.emitEvent({
71
+ type: 'interim',
72
+ result: {
73
+ transcript: this.interimTranscript,
74
+ confidence: 0,
75
+ isFinal: false,
76
+ language: this.recognition.lang,
77
+ timestamp: Date.now(),
78
+ },
79
+ hologramId: '',
80
+ });
81
+ }
82
+ };
83
+ this.recognition.onerror = (_event) => {
84
+ this.emitEvent({
85
+ type: 'error',
86
+ result: {
87
+ transcript: '',
88
+ confidence: 0,
89
+ isFinal: false,
90
+ language: this.recognition.lang,
91
+ timestamp: Date.now(),
92
+ },
93
+ hologramId: '',
94
+ });
95
+ };
96
+ this.recognition.onend = () => {
97
+ this.isListening = false;
98
+ if (this.config.audioFeedback) {
99
+ this.playBeep('end');
100
+ }
101
+ };
102
+ }
103
+ /**
104
+ * Process voice command with fuzzy matching
105
+ */
106
+ processVoiceCommand(transcript, confidence) {
107
+ if (confidence < this.config.confidenceThreshold) {
108
+ return;
109
+ }
110
+ const normalized = transcript.toLowerCase().trim();
111
+ let bestMatch = null;
112
+ let bestScore = 0;
113
+ // Try to find matching command
114
+ for (const command of this.config.commands || []) {
115
+ const cmdConfidence = command.confidence || this.config.confidenceThreshold;
116
+ // Exact match
117
+ if (normalized === command.phrase.toLowerCase()) {
118
+ if (confidence >= cmdConfidence) {
119
+ bestMatch = command;
120
+ bestScore = 1.0;
121
+ break;
122
+ }
123
+ }
124
+ // Fuzzy match with aliases
125
+ const allPhrases = [command.phrase, ...(command.aliases || [])];
126
+ for (const phrase of allPhrases) {
127
+ const score = this.fuzzyMatch(normalized, phrase.toLowerCase());
128
+ if (score > bestScore && score >= 0.7) {
129
+ bestScore = score;
130
+ bestMatch = command;
131
+ }
132
+ }
133
+ }
134
+ // Emit recognition result
135
+ this.emitEvent({
136
+ type: 'final',
137
+ result: {
138
+ transcript: normalized,
139
+ confidence,
140
+ isFinal: true,
141
+ language: this.recognition.lang,
142
+ matchedCommand: bestMatch || undefined,
143
+ timestamp: Date.now(),
144
+ },
145
+ hologramId: '',
146
+ });
147
+ if (bestMatch) {
148
+ if (this.config.audioFeedback) {
149
+ this.playBeep('success');
150
+ }
151
+ }
152
+ }
153
+ /**
154
+ * Fuzzy string matching (simple Levenshtein-like approach)
155
+ */
156
+ fuzzyMatch(input, target) {
157
+ if (input === target)
158
+ return 1.0;
159
+ if (input.length === 0 || target.length === 0)
160
+ return 0;
161
+ // Check if input is substring of target
162
+ if (target.includes(input)) {
163
+ return Math.min(1.0, input.length / target.length);
164
+ }
165
+ // Simple edit distance estimation
166
+ const distance = Math.abs(input.length - target.length);
167
+ const maxLen = Math.max(input.length, target.length);
168
+ return Math.max(0, 1.0 - distance / maxLen);
169
+ }
170
+ /**
171
+ * Build command index for faster lookup
172
+ */
173
+ buildCommandCache() {
174
+ for (const command of this.config.commands || []) {
175
+ this.commandCache.set(command.phrase.toLowerCase(), command);
176
+ for (const alias of command.aliases || []) {
177
+ this.commandCache.set(alias.toLowerCase(), command);
178
+ }
179
+ }
180
+ }
181
+ /**
182
+ * Start listening for voice input
183
+ */
184
+ startListening() {
185
+ if (!this.recognition || this.isListening)
186
+ return;
187
+ try {
188
+ this.recognition.start();
189
+ }
190
+ catch (error) {
191
+ console.error('Failed to start speech recognition:', error);
192
+ }
193
+ }
194
+ /**
195
+ * Stop listening for voice input
196
+ */
197
+ stopListening() {
198
+ if (!this.recognition || !this.isListening)
199
+ return;
200
+ try {
201
+ this.recognition.stop();
202
+ }
203
+ catch (error) {
204
+ console.error('Failed to stop speech recognition:', error);
205
+ }
206
+ }
207
+ /**
208
+ * Toggle listening state
209
+ */
210
+ toggleListening() {
211
+ if (this.isListening) {
212
+ this.stopListening();
213
+ }
214
+ else {
215
+ this.startListening();
216
+ }
217
+ }
218
+ /**
219
+ * Add command listener
220
+ */
221
+ on(listener) {
222
+ this.listeners.add(listener);
223
+ }
224
+ /**
225
+ * Remove command listener
226
+ */
227
+ off(listener) {
228
+ this.listeners.delete(listener);
229
+ }
230
+ /**
231
+ * Emit voice event to all listeners
232
+ */
233
+ emitEvent(event) {
234
+ for (const listener of this.listeners) {
235
+ listener(event);
236
+ }
237
+ }
238
+ /**
239
+ * Play audio feedback beep
240
+ */
241
+ playBeep(type) {
242
+ // AudioContext beep generation
243
+ try {
244
+ const audioContext = new globalThis.AudioContext();
245
+ const oscillator = audioContext.createOscillator();
246
+ const gainNode = audioContext.createGain();
247
+ oscillator.connect(gainNode);
248
+ gainNode.connect(audioContext.destination);
249
+ const now = audioContext.currentTime;
250
+ const duration = 0.1;
251
+ // Vary beep frequency by type
252
+ oscillator.frequency.value = type === 'start' ? 800 : type === 'success' ? 1000 : 600;
253
+ gainNode.gain.setValueAtTime(0.3, now);
254
+ gainNode.gain.exponentialRampToValueAtTime(0.01, now + duration);
255
+ oscillator.start(now);
256
+ oscillator.stop(now + duration);
257
+ }
258
+ catch (error) {
259
+ // Silently fail if audio not available
260
+ }
261
+ }
262
+ /**
263
+ * Get current listening state
264
+ */
265
+ isActive() {
266
+ return this.isListening;
267
+ }
268
+ /**
269
+ * Dispose and cleanup
270
+ */
271
+ dispose() {
272
+ if (this.recognition) {
273
+ this.recognition.abort();
274
+ }
275
+ this.listeners.clear();
276
+ this.commandCache.clear();
277
+ }
278
+ }
279
+ /**
280
+ * HoloScript+ @voice_input trait factory
281
+ */
282
+ export function createVoiceInputTrait(config) {
283
+ return new VoiceInputTrait(config);
284
+ }
@@ -0,0 +1,226 @@
1
+ /**
2
+ * VoiceInputTrait Tests
3
+ *
4
+ * Comprehensive tests for voice recognition and command matching
5
+ */
6
+ import { describe, it, expect, beforeEach, vi } from 'vitest';
7
+ import { VoiceInputTrait } from '../traits/VoiceInputTrait';
8
+ describe('VoiceInputTrait', () => {
9
+ let trait;
10
+ let config;
11
+ beforeEach(() => {
12
+ config = {
13
+ mode: 'continuous',
14
+ confidenceThreshold: 0.7,
15
+ commands: [
16
+ {
17
+ phrase: 'turn on',
18
+ aliases: ['activate', 'power up'],
19
+ action: 'activate',
20
+ },
21
+ {
22
+ phrase: 'move forward',
23
+ aliases: ['go forward', 'advance'],
24
+ action: 'move',
25
+ params: { direction: 'forward' },
26
+ },
27
+ {
28
+ phrase: 'stop',
29
+ aliases: ['halt', 'pause'],
30
+ action: 'stop',
31
+ },
32
+ ],
33
+ };
34
+ trait = new VoiceInputTrait(config);
35
+ });
36
+ describe('Initialization', () => {
37
+ it('should initialize with config', () => {
38
+ expect(trait).toBeDefined();
39
+ });
40
+ it('should not be listening initially', () => {
41
+ expect(trait.isActive()).toBe(false);
42
+ });
43
+ it('should accept custom confidence threshold', () => {
44
+ const customTrait = new VoiceInputTrait({
45
+ mode: 'push-to-talk',
46
+ confidenceThreshold: 0.85,
47
+ });
48
+ expect(customTrait).toBeDefined();
49
+ });
50
+ });
51
+ describe('Listening Control', () => {
52
+ it('should start listening', () => {
53
+ trait.startListening();
54
+ // Note: Web Speech API not available in test env, so this just tests the method exists
55
+ expect(trait).toBeDefined();
56
+ });
57
+ it('should stop listening', () => {
58
+ trait.stopListening();
59
+ expect(trait.isActive()).toBe(false);
60
+ });
61
+ it('should toggle listening state', () => {
62
+ const initialState = trait.isActive();
63
+ trait.toggleListening();
64
+ // Can't easily test state change without Web Speech API
65
+ expect(trait).toBeDefined();
66
+ });
67
+ });
68
+ describe('Event Listeners', () => {
69
+ it('should register event listener', () => {
70
+ const listener = vi.fn();
71
+ trait.on(listener);
72
+ expect(listener).not.toHaveBeenCalled();
73
+ });
74
+ it('should unregister event listener', () => {
75
+ const listener = vi.fn();
76
+ trait.on(listener);
77
+ trait.off(listener);
78
+ expect(trait).toBeDefined();
79
+ });
80
+ it('should allow multiple listeners', () => {
81
+ const listener1 = vi.fn();
82
+ const listener2 = vi.fn();
83
+ trait.on(listener1);
84
+ trait.on(listener2);
85
+ expect(trait).toBeDefined();
86
+ });
87
+ });
88
+ describe('Configuration', () => {
89
+ it('should support continuous mode', () => {
90
+ const continuousTrait = new VoiceInputTrait({
91
+ mode: 'continuous',
92
+ confidenceThreshold: 0.7,
93
+ });
94
+ expect(continuousTrait).toBeDefined();
95
+ });
96
+ it('should support push-to-talk mode', () => {
97
+ const pushTrait = new VoiceInputTrait({
98
+ mode: 'push-to-talk',
99
+ confidenceThreshold: 0.7,
100
+ });
101
+ expect(pushTrait).toBeDefined();
102
+ });
103
+ it('should support always-listening mode', () => {
104
+ const alwaysTrait = new VoiceInputTrait({
105
+ mode: 'always-listening',
106
+ confidenceThreshold: 0.5,
107
+ });
108
+ expect(alwaysTrait).toBeDefined();
109
+ });
110
+ it('should accept multiple languages', () => {
111
+ const multiLangTrait = new VoiceInputTrait({
112
+ mode: 'continuous',
113
+ confidenceThreshold: 0.7,
114
+ languages: ['en-US', 'es-ES', 'fr-FR'],
115
+ });
116
+ expect(multiLangTrait).toBeDefined();
117
+ });
118
+ it('should support audio feedback toggle', () => {
119
+ const feedbackTrait = new VoiceInputTrait({
120
+ mode: 'continuous',
121
+ confidenceThreshold: 0.7,
122
+ audioFeedback: true,
123
+ });
124
+ expect(feedbackTrait).toBeDefined();
125
+ });
126
+ it('should support transcript display', () => {
127
+ const transcriptTrait = new VoiceInputTrait({
128
+ mode: 'continuous',
129
+ confidenceThreshold: 0.7,
130
+ showTranscript: true,
131
+ });
132
+ expect(transcriptTrait).toBeDefined();
133
+ });
134
+ });
135
+ describe('Command Configuration', () => {
136
+ it('should support command phrases', () => {
137
+ const commands = [
138
+ { phrase: 'hello', action: 'greet' },
139
+ ];
140
+ const cmdTrait = new VoiceInputTrait({
141
+ mode: 'continuous',
142
+ confidenceThreshold: 0.7,
143
+ commands,
144
+ });
145
+ expect(cmdTrait).toBeDefined();
146
+ });
147
+ it('should support command aliases', () => {
148
+ const commands = [
149
+ {
150
+ phrase: 'turn on',
151
+ aliases: ['activate', 'power up', 'enable'],
152
+ action: 'activate',
153
+ },
154
+ ];
155
+ const cmdTrait = new VoiceInputTrait({
156
+ mode: 'continuous',
157
+ confidenceThreshold: 0.7,
158
+ commands,
159
+ });
160
+ expect(cmdTrait).toBeDefined();
161
+ });
162
+ it('should support per-command confidence', () => {
163
+ const commands = [
164
+ {
165
+ phrase: 'critical action',
166
+ action: 'critical',
167
+ confidence: 0.95,
168
+ },
169
+ {
170
+ phrase: 'normal action',
171
+ action: 'normal',
172
+ confidence: 0.7,
173
+ },
174
+ ];
175
+ const cmdTrait = new VoiceInputTrait({
176
+ mode: 'continuous',
177
+ confidenceThreshold: 0.7,
178
+ commands,
179
+ });
180
+ expect(cmdTrait).toBeDefined();
181
+ });
182
+ it('should support command parameters', () => {
183
+ const commands = [
184
+ {
185
+ phrase: 'go to location',
186
+ action: 'navigate',
187
+ params: { target: 'location' },
188
+ },
189
+ ];
190
+ const cmdTrait = new VoiceInputTrait({
191
+ mode: 'continuous',
192
+ confidenceThreshold: 0.7,
193
+ commands,
194
+ });
195
+ expect(cmdTrait).toBeDefined();
196
+ });
197
+ });
198
+ describe('Timeout Configuration', () => {
199
+ it('should support custom timeout', () => {
200
+ const timeoutTrait = new VoiceInputTrait({
201
+ mode: 'continuous',
202
+ confidenceThreshold: 0.7,
203
+ timeout: 5000,
204
+ });
205
+ expect(timeoutTrait).toBeDefined();
206
+ });
207
+ it('should use default timeout if not specified', () => {
208
+ const defaultTrait = new VoiceInputTrait({
209
+ mode: 'continuous',
210
+ confidenceThreshold: 0.7,
211
+ });
212
+ expect(defaultTrait).toBeDefined();
213
+ });
214
+ });
215
+ describe('Cleanup', () => {
216
+ it('should dispose resources', () => {
217
+ trait.dispose();
218
+ expect(trait).toBeDefined();
219
+ });
220
+ it('should stop listening on dispose', () => {
221
+ trait.startListening();
222
+ trait.dispose();
223
+ expect(trait.isActive()).toBe(false);
224
+ });
225
+ });
226
+ });