@speechos/core 0.2.9 → 0.2.11

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.
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * WebSocket integration for SpeechOS SDK.
3
3
  *
4
- * Provides a direct WebSocket connection to the backend for voice sessions,
5
- * bypassing LiveKit for lower latency. Uses audio buffering to capture
4
+ * Provides a direct WebSocket connection to the backend for voice sessions.
5
+ * Uses audio buffering to capture
6
6
  * audio immediately while the connection is being established.
7
7
  */
8
8
  import type { CommandDefinition, CommandResult, ErrorSource, VoiceSessionOptions } from './types.js';
@@ -92,8 +92,9 @@ declare class WebSocketManager {
92
92
  /**
93
93
  * Request command matching using the transcript as input.
94
94
  * Note: The command definitions were already sent in the auth message via startVoiceSession.
95
+ * Returns an array of matched commands (empty array if no matches).
95
96
  */
96
- requestCommand(_commands: CommandDefinition[]): Promise<CommandResult | null>;
97
+ requestCommand(_commands: CommandDefinition[]): Promise<CommandResult[]>;
97
98
  /**
98
99
  * Stop audio capture and wait for all data to be sent.
99
100
  *
@@ -108,7 +109,7 @@ declare class WebSocketManager {
108
109
  * Wait for the WebSocket send buffer to drain.
109
110
  *
110
111
  * This ensures all audio data has been transmitted before we request
111
- * the transcript. Uses the same pattern as LiveKit's ReadableStream approach.
112
+ * the transcript.
112
113
  */
113
114
  private waitForBufferDrain;
114
115
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@speechos/core",
3
- "version": "0.2.9",
4
- "description": "Headless core SDK for SpeechOS - state, events, LiveKit integration",
3
+ "version": "0.2.11",
4
+ "description": "Headless core SDK for SpeechOS - state, events, WebSocket integration",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",
@@ -52,8 +52,5 @@
52
52
  "tslib": "^2.8.1",
53
53
  "typescript": "^5.7.3",
54
54
  "vitest": "^4.0.16"
55
- },
56
- "dependencies": {
57
- "livekit-client": "^2.16.1"
58
55
  }
59
56
  }
@@ -1,199 +0,0 @@
1
- /**
2
- * LiveKit integration for SpeechOS SDK
3
- * Handles room connections, audio streaming, and transcription requests
4
- */
5
- import { Room } from "livekit-client";
6
- import type { LiveKitTokenResponse, ErrorSource, CommandDefinition, CommandResult, VoiceSessionOptions } from "./types.js";
7
- /**
8
- * A deferred promise with timeout support.
9
- * Encapsulates resolve/reject/timeout in a single object for cleaner async handling.
10
- */
11
- export declare class Deferred<T> {
12
- readonly promise: Promise<T>;
13
- private _resolve;
14
- private _reject;
15
- private _timeoutId;
16
- private _settled;
17
- constructor();
18
- /**
19
- * Set a timeout that will reject the promise with the given error
20
- */
21
- setTimeout(ms: number, errorMessage: string, errorCode: string, errorSource: ErrorSource): void;
22
- resolve(value: T): void;
23
- reject(error: Error): void;
24
- private clearTimeout;
25
- get isSettled(): boolean;
26
- }
27
- /**
28
- * LiveKit connection manager
29
- */
30
- declare class LiveKitManager {
31
- private room;
32
- private tokenData;
33
- private micTrack;
34
- private cachedTokenData;
35
- private tokenCacheTimestamp;
36
- private tokenPrefetchPromise;
37
- private tokenRefreshTimer;
38
- private autoRefreshEnabled;
39
- private pendingTranscript;
40
- private pendingEditText;
41
- private pendingCommand;
42
- private pendingTrackSubscribed;
43
- private editOriginalText;
44
- private sessionSettings;
45
- /**
46
- * Check if the cached token is still valid (within TTL)
47
- */
48
- private isCachedTokenValid;
49
- /**
50
- * Pre-fetch a LiveKit token for later use
51
- * Call this early (e.g., when widget expands) to reduce latency when starting a voice session.
52
- * If a prefetch is already in progress, returns the existing promise.
53
- * If a valid cached token exists, returns it immediately.
54
- */
55
- prefetchToken(): Promise<LiveKitTokenResponse>;
56
- /**
57
- * Fetch a LiveKit token from the backend
58
- * Uses cached token if valid, otherwise fetches a fresh one.
59
- * Includes language settings and user vocabulary which are stored in the VoiceSession.
60
- */
61
- fetchToken(): Promise<LiveKitTokenResponse>;
62
- /**
63
- * Internal method to fetch a fresh token from the server
64
- */
65
- private fetchTokenFromServer;
66
- /**
67
- * Connect to a LiveKit room (fresh connection each time)
68
- */
69
- connect(): Promise<Room>;
70
- /**
71
- * Wait until the agent is ready to receive audio
72
- * Resolves when LocalTrackSubscribed event is received
73
- */
74
- waitUntilReady(): Promise<void>;
75
- /**
76
- * Set up LiveKit room event listeners
77
- */
78
- private setupRoomEvents;
79
- /**
80
- * Handle incoming data messages from the agent
81
- */
82
- private handleDataMessage;
83
- /**
84
- * Publish microphone audio track
85
- * Uses the device ID from session settings if set
86
- */
87
- enableMicrophone(): Promise<void>;
88
- /**
89
- * Log information about the current microphone track
90
- */
91
- private logMicrophoneInfo;
92
- /**
93
- * Disable microphone audio track
94
- */
95
- disableMicrophone(): Promise<void>;
96
- /**
97
- * Send a data message to the room
98
- */
99
- sendDataMessage(message: object): Promise<void>;
100
- /**
101
- * Start a voice session with pre-connect audio buffering
102
- * Fetches a fresh token, then enables mic with preConnectBuffer to capture audio while connecting.
103
- * Agent subscription happens in the background - we don't block on it.
104
- *
105
- * @param options - Session options including action type and parameters
106
- */
107
- startVoiceSession(options?: VoiceSessionOptions): Promise<void>;
108
- /**
109
- * Wait for the agent to subscribe to our audio track in the background
110
- * Handles timeout errors without blocking the main flow
111
- */
112
- private waitForAgentSubscription;
113
- /**
114
- * Enable microphone with pre-connect buffering
115
- * This starts capturing audio locally before the room is connected,
116
- * buffering it until the connection is established.
117
- */
118
- private enableMicrophoneWithPreConnectBuffer;
119
- /**
120
- * Stop the voice session and request the transcript
121
- * Returns a promise that resolves with the transcript text
122
- * @throws Error if timeout occurs waiting for transcript
123
- */
124
- stopVoiceSession(): Promise<string>;
125
- /**
126
- * Alias for stopVoiceSession - granular API naming
127
- */
128
- stopAndGetTranscript(): Promise<string>;
129
- /**
130
- * Request text editing using the transcript as instructions
131
- * Sends the original text to the backend, which applies the spoken instructions
132
- * Returns a promise that resolves with the edited text
133
- * @throws Error if timeout occurs waiting for edited text
134
- */
135
- requestEditText(originalText: string): Promise<string>;
136
- /**
137
- * Alias for requestEditText - granular API naming
138
- */
139
- stopAndEdit(originalText: string): Promise<string>;
140
- /**
141
- * Request command matching using the transcript as input
142
- * Sends command definitions to the backend, which matches the user's speech against them
143
- * Returns a promise that resolves with the matched command or null if no match
144
- * @throws Error if timeout occurs waiting for command result
145
- */
146
- requestCommand(commands: CommandDefinition[]): Promise<CommandResult | null>;
147
- /**
148
- * Alias for requestCommand - granular API naming
149
- */
150
- stopAndCommand(commands: CommandDefinition[]): Promise<CommandResult | null>;
151
- /**
152
- * Disconnect from the current room
153
- * Clears the token so a fresh one is fetched for the next session
154
- */
155
- disconnect(): Promise<void>;
156
- /**
157
- * Invalidate the cached token
158
- * Call this when settings change that would affect the token (language, vocabulary)
159
- */
160
- invalidateTokenCache(): void;
161
- /**
162
- * Start auto-refreshing the token while the widget is expanded.
163
- * Call this after a voice session completes to immediately fetch a fresh token
164
- * (since each command requires its own token) and keep it fresh for subsequent commands.
165
- */
166
- startAutoRefresh(): void;
167
- /**
168
- * Stop auto-refreshing the token.
169
- * Call this when the widget collapses or user navigates away.
170
- */
171
- stopAutoRefresh(): void;
172
- /**
173
- * Schedule a token refresh before the current cache expires.
174
- * Handles computer sleep by checking elapsed time on each refresh attempt.
175
- */
176
- private scheduleTokenRefresh;
177
- /**
178
- * Perform the auto-refresh, handling computer sleep scenarios.
179
- */
180
- private performAutoRefresh;
181
- /**
182
- * Get the current room instance
183
- */
184
- getRoom(): Room | null;
185
- /**
186
- * Get the current token data
187
- */
188
- getTokenData(): LiveKitTokenResponse | null;
189
- /**
190
- * Check if connected to a room
191
- */
192
- isConnected(): boolean;
193
- /**
194
- * Check if microphone is enabled
195
- */
196
- isMicrophoneEnabled(): boolean;
197
- }
198
- export declare const livekit: LiveKitManager;
199
- export {};
package/dist/livekit.d.ts DELETED
@@ -1,199 +0,0 @@
1
- /**
2
- * LiveKit integration for SpeechOS SDK
3
- * Handles room connections, audio streaming, and transcription requests
4
- */
5
- import { Room } from "livekit-client";
6
- import type { LiveKitTokenResponse, ErrorSource, CommandDefinition, CommandResult, VoiceSessionOptions } from "./types.js";
7
- /**
8
- * A deferred promise with timeout support.
9
- * Encapsulates resolve/reject/timeout in a single object for cleaner async handling.
10
- */
11
- export declare class Deferred<T> {
12
- readonly promise: Promise<T>;
13
- private _resolve;
14
- private _reject;
15
- private _timeoutId;
16
- private _settled;
17
- constructor();
18
- /**
19
- * Set a timeout that will reject the promise with the given error
20
- */
21
- setTimeout(ms: number, errorMessage: string, errorCode: string, errorSource: ErrorSource): void;
22
- resolve(value: T): void;
23
- reject(error: Error): void;
24
- private clearTimeout;
25
- get isSettled(): boolean;
26
- }
27
- /**
28
- * LiveKit connection manager
29
- */
30
- declare class LiveKitManager {
31
- private room;
32
- private tokenData;
33
- private micTrack;
34
- private cachedTokenData;
35
- private tokenCacheTimestamp;
36
- private tokenPrefetchPromise;
37
- private tokenRefreshTimer;
38
- private autoRefreshEnabled;
39
- private pendingTranscript;
40
- private pendingEditText;
41
- private pendingCommand;
42
- private pendingTrackSubscribed;
43
- private editOriginalText;
44
- private sessionSettings;
45
- /**
46
- * Check if the cached token is still valid (within TTL)
47
- */
48
- private isCachedTokenValid;
49
- /**
50
- * Pre-fetch a LiveKit token for later use
51
- * Call this early (e.g., when widget expands) to reduce latency when starting a voice session.
52
- * If a prefetch is already in progress, returns the existing promise.
53
- * If a valid cached token exists, returns it immediately.
54
- */
55
- prefetchToken(): Promise<LiveKitTokenResponse>;
56
- /**
57
- * Fetch a LiveKit token from the backend
58
- * Uses cached token if valid, otherwise fetches a fresh one.
59
- * Includes language settings and user vocabulary which are stored in the VoiceSession.
60
- */
61
- fetchToken(): Promise<LiveKitTokenResponse>;
62
- /**
63
- * Internal method to fetch a fresh token from the server
64
- */
65
- private fetchTokenFromServer;
66
- /**
67
- * Connect to a LiveKit room (fresh connection each time)
68
- */
69
- connect(): Promise<Room>;
70
- /**
71
- * Wait until the agent is ready to receive audio
72
- * Resolves when LocalTrackSubscribed event is received
73
- */
74
- waitUntilReady(): Promise<void>;
75
- /**
76
- * Set up LiveKit room event listeners
77
- */
78
- private setupRoomEvents;
79
- /**
80
- * Handle incoming data messages from the agent
81
- */
82
- private handleDataMessage;
83
- /**
84
- * Publish microphone audio track
85
- * Uses the device ID from session settings if set
86
- */
87
- enableMicrophone(): Promise<void>;
88
- /**
89
- * Log information about the current microphone track
90
- */
91
- private logMicrophoneInfo;
92
- /**
93
- * Disable microphone audio track
94
- */
95
- disableMicrophone(): Promise<void>;
96
- /**
97
- * Send a data message to the room
98
- */
99
- sendDataMessage(message: object): Promise<void>;
100
- /**
101
- * Start a voice session with pre-connect audio buffering
102
- * Fetches a fresh token, then enables mic with preConnectBuffer to capture audio while connecting.
103
- * Agent subscription happens in the background - we don't block on it.
104
- *
105
- * @param options - Session options including action type and parameters
106
- */
107
- startVoiceSession(options?: VoiceSessionOptions): Promise<void>;
108
- /**
109
- * Wait for the agent to subscribe to our audio track in the background
110
- * Handles timeout errors without blocking the main flow
111
- */
112
- private waitForAgentSubscription;
113
- /**
114
- * Enable microphone with pre-connect buffering
115
- * This starts capturing audio locally before the room is connected,
116
- * buffering it until the connection is established.
117
- */
118
- private enableMicrophoneWithPreConnectBuffer;
119
- /**
120
- * Stop the voice session and request the transcript
121
- * Returns a promise that resolves with the transcript text
122
- * @throws Error if timeout occurs waiting for transcript
123
- */
124
- stopVoiceSession(): Promise<string>;
125
- /**
126
- * Alias for stopVoiceSession - granular API naming
127
- */
128
- stopAndGetTranscript(): Promise<string>;
129
- /**
130
- * Request text editing using the transcript as instructions
131
- * Sends the original text to the backend, which applies the spoken instructions
132
- * Returns a promise that resolves with the edited text
133
- * @throws Error if timeout occurs waiting for edited text
134
- */
135
- requestEditText(originalText: string): Promise<string>;
136
- /**
137
- * Alias for requestEditText - granular API naming
138
- */
139
- stopAndEdit(originalText: string): Promise<string>;
140
- /**
141
- * Request command matching using the transcript as input
142
- * Sends command definitions to the backend, which matches the user's speech against them
143
- * Returns a promise that resolves with the matched command or null if no match
144
- * @throws Error if timeout occurs waiting for command result
145
- */
146
- requestCommand(commands: CommandDefinition[]): Promise<CommandResult | null>;
147
- /**
148
- * Alias for requestCommand - granular API naming
149
- */
150
- stopAndCommand(commands: CommandDefinition[]): Promise<CommandResult | null>;
151
- /**
152
- * Disconnect from the current room
153
- * Clears the token so a fresh one is fetched for the next session
154
- */
155
- disconnect(): Promise<void>;
156
- /**
157
- * Invalidate the cached token
158
- * Call this when settings change that would affect the token (language, vocabulary)
159
- */
160
- invalidateTokenCache(): void;
161
- /**
162
- * Start auto-refreshing the token while the widget is expanded.
163
- * Call this after a voice session completes to immediately fetch a fresh token
164
- * (since each command requires its own token) and keep it fresh for subsequent commands.
165
- */
166
- startAutoRefresh(): void;
167
- /**
168
- * Stop auto-refreshing the token.
169
- * Call this when the widget collapses or user navigates away.
170
- */
171
- stopAutoRefresh(): void;
172
- /**
173
- * Schedule a token refresh before the current cache expires.
174
- * Handles computer sleep by checking elapsed time on each refresh attempt.
175
- */
176
- private scheduleTokenRefresh;
177
- /**
178
- * Perform the auto-refresh, handling computer sleep scenarios.
179
- */
180
- private performAutoRefresh;
181
- /**
182
- * Get the current room instance
183
- */
184
- getRoom(): Room | null;
185
- /**
186
- * Get the current token data
187
- */
188
- getTokenData(): LiveKitTokenResponse | null;
189
- /**
190
- * Check if connected to a room
191
- */
192
- isConnected(): boolean;
193
- /**
194
- * Check if microphone is enabled
195
- */
196
- isMicrophoneEnabled(): boolean;
197
- }
198
- export declare const livekit: LiveKitManager;
199
- export {};