@amaster.ai/client 1.1.0-beta.3 → 1.1.0-beta.31

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.
package/types/asr.d.ts CHANGED
@@ -1,31 +1,12 @@
1
1
  /**
2
- * ============================================================================
3
- * ASR (Automatic Speech Recognition) - Type Definitions
4
- * ============================================================================
5
- *
6
- * Real-time speech recognition using WebSocket connection.
2
+ * * Real-time speech recognition using WebSocket connection.
7
3
  *
8
4
  * @module asr
9
5
  */
10
6
 
11
7
  /**
12
8
  * ASR Client Configuration
13
- *
14
- * @example
15
- * Configure ASR with custom voice settings:
16
- * ```typescript
17
- * const client = createClient({ baseURL: 'https://api.amaster.ai' });
18
- *
19
- * // Reconfigure ASR client
20
- * client.asr = createASRClient({
21
- * audioFormat: 'pcm16',
22
- * sampleRate: 16000,
23
- * onTranscript: (text, isFinal) => {
24
- * console.log(isFinal ? `Final: ${text}` : `Interim: ${text}`);
25
- * },
26
- * onError: (error) => console.error('ASR Error:', error)
27
- * });
28
- * ```
9
+ *
29
10
  */
30
11
  export interface ASRClientConfig {
31
12
  /** Audio format, default 'pcm16' */
@@ -61,78 +42,20 @@ export interface ASRClientConfig {
61
42
  }
62
43
 
63
44
  /**
64
- * ASR Client API
65
- *
66
- * Real-time speech recognition client using WebSocket.
67
- *
68
- * @example
69
- * Basic usage:
70
- * ```typescript
71
- * const client = createClient({ baseURL: 'https://api.amaster.ai' });
72
- *
73
- * // Configure callbacks
74
- * client.asr = createASRClient({
75
- * onTranscript: (text, isFinal) => {
76
- * if (isFinal) {
77
- * console.log('Final transcript:', text);
78
- * } else {
79
- * console.log('Interim transcript:', text);
80
- * }
81
- * }
82
- * });
83
- *
84
- * // Connect and start recording
85
- * await client.asr.connect();
86
- * await client.asr.startRecording();
45
+ * ASR (Automatic Speech Recognition) Client API
87
46
  *
88
- * // Stop recording
89
- * client.asr.stopRecording();
47
+ * Provides real-time speech-to-text conversion via WebSocket.
90
48
  *
91
- * // Close connection
92
- * client.asr.close();
93
- * ```
94
- *
95
- * @example
96
- * With error handling:
97
- * ```typescript
98
- * const client = createClient({ baseURL: 'https://api.amaster.ai' });
99
- *
100
- * client.asr = createASRClient({
101
- * onReady: () => console.log('ASR ready'),
102
- * onSpeechStart: () => console.log('Speech detected'),
103
- * onSpeechEnd: () => console.log('Speech ended'),
104
- * onTranscript: (text, isFinal) => {
105
- * console.log(isFinal ? `[FINAL] ${text}` : `[INTERIM] ${text}`);
106
- * },
107
- * onError: (error) => {
108
- * console.error('ASR Error:', error.message);
109
- * },
110
- * onClose: () => {
111
- * console.log('ASR connection closed');
112
- * }
113
- * });
114
- *
115
- * try {
116
- * await client.asr.connect();
117
- * await client.asr.startRecording();
118
- * } catch (error) {
119
- * console.error('Failed to start ASR:', error);
120
- * }
121
- * ```
49
+ * @since 1.0.0
122
50
  */
123
- export interface ASRClient {
51
+ export interface ASRClientAPI {
124
52
  /**
125
53
  * Connect to ASR service
126
54
  *
127
55
  * Establishes WebSocket connection to the speech recognition service.
128
56
  *
129
57
  * @returns Promise that resolves when connected
130
- *
131
- * @example
132
- * ```typescript
133
- * await client.asr.connect();
134
- * console.log('Connected to ASR service');
135
- * ```
58
+ *
136
59
  */
137
60
  connect(): Promise<void>;
138
61
 
@@ -143,19 +66,7 @@ export interface ASRClient {
143
66
  * Requires microphone permission from the user.
144
67
  *
145
68
  * @returns Promise that resolves when recording starts
146
- *
147
- * @example
148
- * ```typescript
149
- * // Request microphone permission and start recording
150
- * try {
151
- * await client.asr.startRecording();
152
- * console.log('Recording started');
153
- * } catch (error) {
154
- * if (error.name === 'NotAllowedError') {
155
- * console.error('Microphone permission denied');
156
- * }
157
- * }
158
- * ```
69
+ *
159
70
  */
160
71
  startRecording(): Promise<void>;
161
72
 
@@ -163,16 +74,7 @@ export interface ASRClient {
163
74
  * Stop recording
164
75
  *
165
76
  * Stops capturing audio from the microphone but keeps the WebSocket connection open.
166
- *
167
- * @example
168
- * ```typescript
169
- * // Stop recording after 10 seconds
170
- * await client.asr.startRecording();
171
- * setTimeout(() => {
172
- * client.asr.stopRecording();
173
- * console.log('Recording stopped');
174
- * }, 10000);
175
- * ```
77
+ *
176
78
  */
177
79
  stopRecording(): void;
178
80
 
@@ -180,13 +82,7 @@ export interface ASRClient {
180
82
  * Close connection
181
83
  *
182
84
  * Closes the WebSocket connection and releases resources.
183
- *
184
- * @example
185
- * ```typescript
186
- * // Cleanup when done
187
- * client.asr.stopRecording();
188
- * client.asr.close();
189
- * ```
85
+ *
190
86
  */
191
87
  close(): void;
192
88
  }
@@ -1,9 +1,5 @@
1
1
  /**
2
- * ============================================================================
3
- * Verification Code Authentication - Type Definitions
4
- * ============================================================================
5
- *
6
- * Verification code-based authentication including:
2
+ * * Verification code-based authentication including:
7
3
  * - Email verification code login
8
4
  * - SMS verification code login
9
5
  * - Send verification code
@@ -24,46 +20,7 @@ export type CodeLoginType = 'email' | 'phone';
24
20
 
25
21
  /**
26
22
  * Verification code login parameters
27
- *
28
- * @example
29
- * Email code login:
30
- * ```typescript
31
- * // 1. Send verification code
32
- * await client.auth.sendCode({
33
- * type: 'email',
34
- * email: 'user@example.com'
35
- * });
36
- *
37
- * // 2. User receives code via email: "123456"
38
- *
39
- * // 3. Login with code
40
- * const result = await client.auth.codeLogin({
41
- * email: 'user@example.com',
42
- * code: '123456'
43
- * });
44
- *
45
- * if (result.data) {
46
- * console.log('Logged in successfully!');
47
- * }
48
- * ```
49
- *
50
- * @example
51
- * Phone code login:
52
- * ```typescript
53
- * // 1. Send SMS code
54
- * await client.auth.sendCode({
55
- * type: 'phone',
56
- * phone: '+86-13800138000'
57
- * });
58
- *
59
- * // 2. User receives SMS: "654321"
60
- *
61
- * // 3. Login with code
62
- * await client.auth.codeLogin({
63
- * phone: '+86-13800138000',
64
- * code: '654321'
65
- * });
66
- * ```
23
+ *
67
24
  */
68
25
  export interface CodeLoginParams {
69
26
  /** Login method (optional, auto-detected) */
@@ -119,45 +76,7 @@ export interface CodeAuthAPI {
119
76
  *
120
77
  * @param params - Email/phone and verification code
121
78
  * @returns User info and access token
122
- *
123
- * @example
124
- * Complete code login flow:
125
- * ```typescript
126
- * // 1. Send verification code
127
- * await client.auth.sendCode({
128
- * type: 'email',
129
- * email: 'user@example.com'
130
- * });
131
- *
132
- * // 2. User receives code: "123456"
133
- *
134
- * // 3. Login with code
135
- * const result = await client.auth.codeLogin({
136
- * email: 'user@example.com',
137
- * code: '123456'
138
- * });
139
- *
140
- * if (result.data) {
141
- * console.log('Logged in successfully!');
142
- * }
143
- * ```
144
- *
145
- * @example
146
- * SMS code login with error handling:
147
- * ```typescript
148
- * const result = await client.auth.codeLogin({
149
- * phone: '+86-13800138000',
150
- * code: userInputCode
151
- * });
152
- *
153
- * if (result.error) {
154
- * if (result.status === 400) {
155
- * console.error('Invalid or expired code');
156
- * } else {
157
- * console.error('Login failed:', result.error.message);
158
- * }
159
- * }
160
- * ```
79
+ *
161
80
  */
162
81
  codeLogin(params: CodeLoginParams): Promise<ClientResult<LoginResponse>>;
163
82
 
@@ -169,43 +88,7 @@ export interface CodeAuthAPI {
169
88
  *
170
89
  * @param params - Email or phone to send code to
171
90
  * @returns Success status
172
- *
173
- * @example
174
- * Send email verification code:
175
- * ```typescript
176
- * const result = await client.auth.sendCode({
177
- * type: 'email',
178
- * email: 'user@example.com'
179
- * });
180
- *
181
- * if (result.data?.success) {
182
- * console.log('Code sent to email');
183
- * showCodeInputForm();
184
- * }
185
- * ```
186
- *
187
- * @example
188
- * Send SMS verification code:
189
- * ```typescript
190
- * await client.auth.sendCode({
191
- * type: 'phone',
192
- * phone: '+86-13800138000'
193
- * });
194
- * console.log('SMS sent');
195
- * ```
196
- *
197
- * @example
198
- * With rate limiting handling:
199
- * ```typescript
200
- * const result = await client.auth.sendCode({
201
- * type: 'email',
202
- * email: 'user@example.com'
203
- * });
204
- *
205
- * if (result.status === 429) {
206
- * console.error('Too many requests. Please try again later.');
207
- * }
208
- * ```
91
+ *
209
92
  */
210
93
  sendCode(params: SendCodeParams): Promise<ClientResult<SuccessResponse>>;
211
94
 
@@ -216,39 +99,7 @@ export interface CodeAuthAPI {
216
99
  * Used during registration or sensitive operations.
217
100
  *
218
101
  * @returns Captcha ID and image (base64)
219
- *
220
- * @example
221
- * Display captcha to user:
222
- * ```typescript
223
- * const result = await client.auth.getCaptcha();
224
- * if (result.data) {
225
- * // Display image to user
226
- * const img = document.createElement('img');
227
- * img.src = result.data.captchaImage;
228
- * document.body.appendChild(img);
229
- *
230
- * // Save captchaId for later verification
231
- * const captchaId = result.data.captchaId;
232
- * }
233
- * ```
234
- *
235
- * @example
236
- * Use with registration:
237
- * ```typescript
238
- * // 1. Get captcha
239
- * const captchaResult = await client.auth.getCaptcha();
240
- * showCaptchaImage(captchaResult.data.captchaImage);
241
- *
242
- * // 2. Get user input
243
- * const userInput = await promptUserForCaptcha();
244
- *
245
- * // 3. Register with captcha
246
- * await client.auth.register({
247
- * email: 'user@example.com',
248
- * password: 'Password@123',
249
- * captcha: `${captchaResult.data.captchaId}:${userInput}`
250
- * });
251
- * ```
102
+ *
252
103
  */
253
104
  getCaptcha(): Promise<ClientResult<CaptchaResponse>>;
254
105
  }
@@ -1,43 +1,33 @@
1
1
  /**
2
- * ============================================================================
3
- * Authentication Module - Unified Entry Point
4
- * ============================================================================
2
+ * Authentication module - provides user authentication and management capabilities.
5
3
  *
6
- * This module provides comprehensive authentication capabilities.
7
- * Import from submodules for better tree-shaking and AI readability:
8
- *
9
- * - `@amaster.ai/client/auth/user` - User types
10
- * - `@amaster.ai/client/auth/password-auth` - Password login/register
11
- * - `@amaster.ai/client/auth/code-auth` - Verification code login
12
- * - `@amaster.ai/client/auth/oauth` - OAuth & social login
13
- * - `@amaster.ai/client/auth/permissions` - Permission checking helpers
14
- * - `@amaster.ai/client/auth/profile` - User profile management
4
+ * This module exports all auth-related types for convenient importing:
15
5
  *
16
6
  * @example
17
- * Import complete API:
18
- * ```typescript
19
- * import type { AuthClientAPI } from '@amaster.ai/client/auth';
20
- * ```
7
+ * // Import specific types
8
+ * import type { LoginParams, User, OAuthProvider } from '@amaster.ai/client/auth';
21
9
  *
22
10
  * @example
23
- * Import specific types (recommended for AI):
24
- * ```typescript
25
- * import type { LoginParams, User } from '@amaster.ai/client/auth/password-auth';
26
- * import type { hasPermission } from '@amaster.ai/client/auth/permissions';
27
- * import type { OAuthProvider } from '@amaster.ai/client/auth/oauth';
28
- * ```
11
+ * // Import multiple types
12
+ * import type {
13
+ * LoginParams,
14
+ * RegisterParams,
15
+ * User,
16
+ * Role,
17
+ * Permission
18
+ * } from '@amaster.ai/client/auth';
29
19
  *
30
20
  * @module auth
21
+ * @since 1.0.0
31
22
  */
32
23
 
33
24
  import type { ClientResult } from '../common';
34
25
 
35
- // Re-export all submodule types
26
+ // Re-export all submodule types for convenient importing
36
27
  export * from './user';
37
28
  export * from './password-auth';
38
29
  export * from './code-auth';
39
30
  export * from './oauth';
40
- export * from './permissions';
41
31
  export * from './profile';
42
32
 
43
33
  // Import for unified API
@@ -77,37 +67,7 @@ export interface SuccessResponse {
77
67
  *
78
68
  * Combines all authentication capabilities into a single interface.
79
69
  * All methods return a `ClientResult<T>` for consistent error handling.
80
- *
81
- * @example
82
- * Complete authentication flow:
83
- * ```typescript
84
- * const client = createClient({ baseURL: 'https://api.amaster.ai' });
85
- *
86
- * // 1. Register new user
87
- * await client.auth.register({
88
- * email: 'user@example.com',
89
- * password: 'Password@123',
90
- * displayName: 'John Doe'
91
- * });
92
- *
93
- * // 2. Login (token is automatically stored and attached to all requests)
94
- * await client.auth.login({
95
- * email: 'user@example.com',
96
- * password: 'Password@123'
97
- * });
98
- *
99
- * // 3. Get current user info
100
- * const userResult = await client.auth.getMe();
101
- * console.log(userResult.data);
102
- *
103
- * // 4. Update profile
104
- * await client.auth.updateProfile({
105
- * displayName: 'Jane Doe'
106
- * });
107
- *
108
- * // 5. Logout
109
- * await client.auth.logout();
110
- * ```
70
+ *
111
71
  */
112
72
  export interface AuthClientAPI
113
73
  extends PasswordAuthAPI,
@@ -122,22 +82,7 @@ export interface AuthClientAPI
122
82
  * All subsequent requests will be unauthenticated until login again.
123
83
  *
124
84
  * @returns Success status
125
- *
126
- * @example
127
- * ```typescript
128
- * await client.auth.logout();
129
- * console.log('Logged out successfully');
130
- *
131
- * // Token is cleared, requests are now unauthenticated
132
- * console.log(client.isAuthenticated()); // false
133
- * ```
134
- *
135
- * @example
136
- * Logout with redirect:
137
- * ```typescript
138
- * await client.auth.logout();
139
- * window.location.href = '/login';
140
- * ```
85
+ *
141
86
  */
142
87
  logout(): Promise<ClientResult<SuccessResponse>>;
143
88
 
@@ -149,32 +94,45 @@ export interface AuthClientAPI
149
94
  * This method is exposed for manual refresh scenarios.
150
95
  *
151
96
  * @returns New access token
97
+ *
98
+ */
99
+ refreshToken(): Promise<ClientResult<RefreshTokenResponse>>;
100
+
101
+ // ==================== Permission Checks ====================
102
+
103
+ /**
104
+ * Check if current user has a specific role (local check, fast)
152
105
  *
153
- * @example
154
- * ```typescript
155
- * const result = await client.auth.refreshToken();
156
- * if (result.data) {
157
- * console.log('Token refreshed:', result.data.accessToken);
158
- * }
159
- * ```
106
+ * Works for both authenticated and anonymous users.
107
+ * Checks against locally cached user roles.
160
108
  *
161
- * @example
162
- * Manual token refresh on 401:
163
- * ```typescript
164
- * try {
165
- * const result = await client.entity.list('default', 'users');
166
- * if (result.status === 401) {
167
- * // Try to refresh token
168
- * const refreshResult = await client.auth.refreshToken();
169
- * if (refreshResult.data) {
170
- * // Retry original request
171
- * return await client.entity.list('default', 'users');
172
- * }
173
- * }
174
- * } catch (error) {
175
- * // Handle error
176
- * }
177
- * ```
109
+ * @param roleCode - Role code to check (e.g., "admin", "user", "anonymous")
110
+ * @returns True if user has the role
111
+ *
178
112
  */
179
- refreshToken(): Promise<ClientResult<RefreshTokenResponse>>;
113
+ hasRole(roleCode: string): boolean;
114
+
115
+ /**
116
+ * Check if current user has a specific permission (local check, fast)
117
+ *
118
+ * Works for both authenticated and anonymous users.
119
+ * Checks against locally cached user permissions.
120
+ *
121
+ * @param resource - Resource name (e.g., "user", "order")
122
+ * @param action - Action name (e.g., "read", "write", "delete")
123
+ * @returns True if user has the permission
124
+ *
125
+ */
126
+ hasPermission(resource: string, action: string): boolean;
127
+
128
+ /**
129
+ * Check if current user is anonymous (not authenticated)
130
+ *
131
+ * Convenience method equivalent to `hasRole('anonymous')`.
132
+ * Returns true if user has the 'anonymous' role.
133
+ *
134
+ * @returns True if user is anonymous
135
+ *
136
+ */
137
+ isAnonymous(): boolean;
180
138
  }