@amaster.ai/client 1.1.0-beta.7 → 1.1.0-beta.71
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/README.md +340 -76
- package/dist/index.cjs +95 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +151 -23
- package/dist/index.d.ts +151 -23
- package/dist/index.js +97 -8
- package/dist/index.js.map +1 -1
- package/package.json +15 -11
- package/types/__tests__/type-checks.test-d.ts +163 -0
- package/types/asr.d.ts +299 -164
- package/types/auth/code-auth.d.ts +8 -157
- package/types/auth/index.d.ts +81 -96
- package/types/auth/oauth.d.ts +6 -143
- package/types/auth/password-auth.d.ts +38 -137
- package/types/auth/profile.d.ts +4 -103
- package/types/auth/user.d.ts +10 -34
- package/types/bpm.d.ts +305 -92
- package/types/common.d.ts +52 -44
- package/types/copilot.d.ts +62 -338
- package/types/entity.d.ts +65 -342
- package/types/function.d.ts +11 -88
- package/types/http.d.ts +95 -0
- package/types/index.d.ts +136 -354
- package/types/s3.d.ts +96 -0
- package/types/tts.d.ts +16 -130
- package/types/workflow.d.ts +16 -165
- package/types/auth/permissions.d.ts +0 -254
package/README.md
CHANGED
|
@@ -53,7 +53,7 @@ Each module contains:
|
|
|
53
53
|
🔄 **Token auto-refresh** - never worry about expiration
|
|
54
54
|
📦 **Type-safe** - Full TypeScript support with auto-completion
|
|
55
55
|
🎯 **Simple API** - Clean, consistent method naming
|
|
56
|
-
🌐 **Complete Integration** - Auth, Entity, BPM, Workflow, ASR, Copilot, Function, TTS
|
|
56
|
+
🌐 **Complete Integration** - Auth, Entity, BPM, Workflow, ASR, Copilot, Function, TTS, S3
|
|
57
57
|
|
|
58
58
|
## 📦 Installation
|
|
59
59
|
|
|
@@ -280,6 +280,307 @@ if (result.success) {
|
|
|
280
280
|
}
|
|
281
281
|
```
|
|
282
282
|
|
|
283
|
+
## 🎙️ ASR (Automatic Speech Recognition)
|
|
284
|
+
|
|
285
|
+
实时语音识别支持 WebSocket 流式识别和 HTTP 按压识别两种方式。
|
|
286
|
+
|
|
287
|
+
### WebSocket ASR(实时流式识别)
|
|
288
|
+
|
|
289
|
+
适合需要实时看到识别结果的场景,如语音输入、实时字幕等。
|
|
290
|
+
|
|
291
|
+
```tsx
|
|
292
|
+
import { useRef, useState } from "react";
|
|
293
|
+
import type { ASRClient } from "@amaster.ai/client";
|
|
294
|
+
|
|
295
|
+
function ASRRealtimeDemo() {
|
|
296
|
+
const asrRef = useRef<ASRClient | null>(null);
|
|
297
|
+
const [result, setResult] = useState("");
|
|
298
|
+
const [status, setStatus] = useState("idle"); // idle | connecting | ready | error | closed
|
|
299
|
+
|
|
300
|
+
const start = async () => {
|
|
301
|
+
let finalResult = "";
|
|
302
|
+
let temp = "";
|
|
303
|
+
|
|
304
|
+
setStatus("connecting");
|
|
305
|
+
|
|
306
|
+
const asrClient = client.asr({
|
|
307
|
+
onReady() {
|
|
308
|
+
setStatus("ready");
|
|
309
|
+
},
|
|
310
|
+
onTranscript(text, isFinal) {
|
|
311
|
+
if (!isFinal) {
|
|
312
|
+
temp = text;
|
|
313
|
+
setResult(finalResult + temp);
|
|
314
|
+
} else {
|
|
315
|
+
temp = "";
|
|
316
|
+
finalResult += text;
|
|
317
|
+
setResult(finalResult);
|
|
318
|
+
}
|
|
319
|
+
},
|
|
320
|
+
onError(err) {
|
|
321
|
+
console.error(err);
|
|
322
|
+
setStatus("error");
|
|
323
|
+
},
|
|
324
|
+
onClose() {
|
|
325
|
+
setStatus("closed");
|
|
326
|
+
},
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
await asrClient.connect();
|
|
330
|
+
await asrClient.startRecording();
|
|
331
|
+
asrRef.current = asrClient;
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
const stop = () => {
|
|
335
|
+
asrRef.current?.stopRecording();
|
|
336
|
+
asrRef.current?.close();
|
|
337
|
+
asrRef.current = null;
|
|
338
|
+
setStatus("idle");
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
return (
|
|
342
|
+
<div>
|
|
343
|
+
<div>状态: {status}</div>
|
|
344
|
+
<button onClick={start} disabled={status === "connecting" || status === "ready"}>
|
|
345
|
+
开始录音
|
|
346
|
+
</button>
|
|
347
|
+
<button onClick={stop} disabled={status !== "ready"}>
|
|
348
|
+
停止
|
|
349
|
+
</button>
|
|
350
|
+
<div>识别结果: {result || "(等待说话...)"}</div>
|
|
351
|
+
</div>
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
### HTTP ASR(按压识别)
|
|
357
|
+
|
|
358
|
+
适合按住说话、松开识别的场景,如语音消息、语音搜索等。
|
|
359
|
+
|
|
360
|
+
```tsx
|
|
361
|
+
import { useRef, useState } from "react";
|
|
362
|
+
import type { ASRHttpClient } from "@amaster.ai/client";
|
|
363
|
+
|
|
364
|
+
function ASRPressToTalkDemo() {
|
|
365
|
+
const clientRef = useRef<ASRHttpClient | null>(null);
|
|
366
|
+
const [recording, setRecording] = useState(false);
|
|
367
|
+
const [text, setText] = useState("");
|
|
368
|
+
const [error, setError] = useState<string | null>(null);
|
|
369
|
+
|
|
370
|
+
if (!clientRef.current) {
|
|
371
|
+
clientRef.current = client.asrHttp({
|
|
372
|
+
onRecordingStart() {
|
|
373
|
+
setRecording(true);
|
|
374
|
+
setText("");
|
|
375
|
+
setError(null);
|
|
376
|
+
},
|
|
377
|
+
onRecordingStop() {
|
|
378
|
+
setRecording(false);
|
|
379
|
+
},
|
|
380
|
+
onResult(result) {
|
|
381
|
+
setText(result);
|
|
382
|
+
},
|
|
383
|
+
onError(err) {
|
|
384
|
+
setError(err.message);
|
|
385
|
+
},
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const asrHttpClient = clientRef.current;
|
|
390
|
+
|
|
391
|
+
return (
|
|
392
|
+
<div>
|
|
393
|
+
<button
|
|
394
|
+
onMouseDown={() => asrHttpClient.startRecording()}
|
|
395
|
+
onMouseUp={() => asrHttpClient.stopRecording()}
|
|
396
|
+
onTouchStart={() => asrHttpClient.startRecording()}
|
|
397
|
+
onTouchEnd={() => asrHttpClient.stopRecording()}
|
|
398
|
+
style={{
|
|
399
|
+
padding: "12px 24px",
|
|
400
|
+
background: recording ? "#f87171" : "#4ade80",
|
|
401
|
+
}}
|
|
402
|
+
>
|
|
403
|
+
{recording ? "松开识别" : "按住说话"}
|
|
404
|
+
</button>
|
|
405
|
+
|
|
406
|
+
<div>
|
|
407
|
+
<strong>识别结果:</strong>
|
|
408
|
+
<div>{text || "(暂无)"}</div>
|
|
409
|
+
</div>
|
|
410
|
+
|
|
411
|
+
{error && <div style={{ color: "red" }}>错误:{error}</div>}
|
|
412
|
+
</div>
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
**ASR Client 配置说明:**
|
|
418
|
+
|
|
419
|
+
| 参数 | 类型 | 说明 |
|
|
420
|
+
| ------------------ | ------------------------------------------ | ------------------------------ |
|
|
421
|
+
| `onReady` | `() => void` | 会话创建完成 |
|
|
422
|
+
| `onSpeechStart` | `() => void` | 检测到语音开始(仅 WebSocket) |
|
|
423
|
+
| `onSpeechEnd` | `() => void` | 检测到语音结束(仅 WebSocket) |
|
|
424
|
+
| `onTranscript` | `(text: string, isFinal: boolean) => void` | 转写回调(WebSocket) |
|
|
425
|
+
| `onRecordingStart` | `() => void` | 录音开始回调(仅 HTTP) |
|
|
426
|
+
| `onRecordingStop` | `() => void` | 录音停止回调(仅 HTTP) |
|
|
427
|
+
| `onResult` | `(text: string) => void` | 识别结果回调(HTTP) |
|
|
428
|
+
| `onError` | `(error: Error) => void` | 错误回调 |
|
|
429
|
+
| `onClose` | `() => void` | 连接关闭回调(WebSocket) |
|
|
430
|
+
|
|
431
|
+
## 🔊 TTS (Text-to-Speech)
|
|
432
|
+
|
|
433
|
+
WebSocket 实时语音合成。
|
|
434
|
+
|
|
435
|
+
```tsx
|
|
436
|
+
import { useRef, useState } from "react";
|
|
437
|
+
import type { TTSClient } from "@amaster.ai/client";
|
|
438
|
+
|
|
439
|
+
function TTSDemo() {
|
|
440
|
+
const [voice, setVoice] = useState("Cherry");
|
|
441
|
+
const [connected, setConnected] = useState(false);
|
|
442
|
+
const [status, setStatus] = useState("disconnected");
|
|
443
|
+
const [text, setText] = useState("你好,欢迎使用实时语音合成服务。");
|
|
444
|
+
const clientRef = useRef<TTSClient | null>(null);
|
|
445
|
+
|
|
446
|
+
const connectTTS = () => {
|
|
447
|
+
if (clientRef.current) return;
|
|
448
|
+
|
|
449
|
+
const ttsClient = client.tts({
|
|
450
|
+
voice,
|
|
451
|
+
autoPlay: true,
|
|
452
|
+
audioFormat: "pcm",
|
|
453
|
+
sampleRate: 24000,
|
|
454
|
+
onReady: () => {
|
|
455
|
+
setConnected(true);
|
|
456
|
+
setStatus("connected");
|
|
457
|
+
},
|
|
458
|
+
onAudioStart: () => setStatus("playing"),
|
|
459
|
+
onAudioEnd: () => setStatus("connected"),
|
|
460
|
+
onAudioChunk: (chunks) => {
|
|
461
|
+
console.log("收到音频片段:", chunks.length);
|
|
462
|
+
},
|
|
463
|
+
onError: (err) => {
|
|
464
|
+
console.error("TTS Error:", err);
|
|
465
|
+
setStatus("error");
|
|
466
|
+
},
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
ttsClient.connect();
|
|
470
|
+
clientRef.current = ttsClient;
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
const sendTTS = () => {
|
|
474
|
+
if (!text || !clientRef.current) return;
|
|
475
|
+
clientRef.current.speak(text);
|
|
476
|
+
setStatus("sending");
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
const disconnectTTS = () => {
|
|
480
|
+
clientRef.current?.close();
|
|
481
|
+
clientRef.current = null;
|
|
482
|
+
setConnected(false);
|
|
483
|
+
setStatus("disconnected");
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
return (
|
|
487
|
+
<div>
|
|
488
|
+
<h3>🔊 实时语音合成</h3>
|
|
489
|
+
<div>状态: {status}</div>
|
|
490
|
+
|
|
491
|
+
<div>
|
|
492
|
+
<label>音色:</label>
|
|
493
|
+
<select value={voice} onChange={(e) => setVoice(e.target.value)}>
|
|
494
|
+
<option value="Cherry">Cherry - 甜美女声</option>
|
|
495
|
+
<option value="Serena">苏瑶 - 温柔小姐姐</option>
|
|
496
|
+
<option value="Ethan">晨煦 - 标准普通话</option>
|
|
497
|
+
<option value="Chelsie">千雪 - 二次元虚拟女友</option>
|
|
498
|
+
<option value="Peter">天津话</option>
|
|
499
|
+
</select>
|
|
500
|
+
</div>
|
|
501
|
+
|
|
502
|
+
<div>
|
|
503
|
+
<label>合成文本:</label>
|
|
504
|
+
<textarea rows={4} value={text} onChange={(e) => setText(e.target.value)} />
|
|
505
|
+
</div>
|
|
506
|
+
|
|
507
|
+
<div>
|
|
508
|
+
<button onClick={connectTTS} disabled={connected}>
|
|
509
|
+
连接
|
|
510
|
+
</button>
|
|
511
|
+
<button onClick={sendTTS} disabled={!connected}>
|
|
512
|
+
合成语音
|
|
513
|
+
</button>
|
|
514
|
+
<button onClick={disconnectTTS} disabled={!connected}>
|
|
515
|
+
断开
|
|
516
|
+
</button>
|
|
517
|
+
</div>
|
|
518
|
+
</div>
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
```
|
|
522
|
+
|
|
523
|
+
**TTS Client 配置说明:**
|
|
524
|
+
|
|
525
|
+
| 参数 | 类型 | 默认值 | 说明 |
|
|
526
|
+
| -------------- | ----------------------------------- | ---------- | ------------------------------ |
|
|
527
|
+
| `voice` | `string` | `"Cherry"` | 发音人名称 |
|
|
528
|
+
| `autoPlay` | `boolean` | `true` | 是否自动播放 |
|
|
529
|
+
| `audioFormat` | `"pcm" \| "mp3" \| "wav" \| "opus"` | `"pcm"` | 音频格式(内置播放仅支持 pcm) |
|
|
530
|
+
| `sampleRate` | `number` | `24000` | 采样率 |
|
|
531
|
+
| `onReady` | `() => void` | - | 会话就绪回调 |
|
|
532
|
+
| `onAudioStart` | `() => void` | - | 音频开始播放回调 |
|
|
533
|
+
| `onAudioEnd` | `() => void` | - | 音频播放结束回调 |
|
|
534
|
+
| `onAudioChunk` | `(chunks: string[]) => void` | - | 音频分片回调 |
|
|
535
|
+
| `onError` | `(error: Error) => void` | - | 错误回调 |
|
|
536
|
+
|
|
537
|
+
## 🤖 Copilot (AI Assistant)
|
|
538
|
+
|
|
539
|
+
AI assistant / chatbot API.
|
|
540
|
+
|
|
541
|
+
```typescript
|
|
542
|
+
// Simple chat
|
|
543
|
+
const result = await client.copilot.sendMessage([
|
|
544
|
+
{ role: "user", content: "Hello, how can you help?" },
|
|
545
|
+
]);
|
|
546
|
+
|
|
547
|
+
console.log(result.data.content);
|
|
548
|
+
|
|
549
|
+
// Streaming response
|
|
550
|
+
await client.copilot.sendMessage([{ role: "user", content: "Tell me a story" }], {
|
|
551
|
+
stream: true,
|
|
552
|
+
onChunk: (chunk) => console.log(chunk),
|
|
553
|
+
});
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
## ⚙️ Function Invocation
|
|
557
|
+
|
|
558
|
+
调用服务端函数。
|
|
559
|
+
|
|
560
|
+
```typescript
|
|
561
|
+
// Call a function
|
|
562
|
+
const result = await client.function.invoke<{ result: string }>("sendEmail", {
|
|
563
|
+
to: "user@example.com",
|
|
564
|
+
subject: "Hello",
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
if (result.data) {
|
|
568
|
+
console.log("Function result:", result.data.result);
|
|
569
|
+
}
|
|
570
|
+
```
|
|
571
|
+
|
|
572
|
+
## ☁️ S3 Storage
|
|
573
|
+
|
|
574
|
+
文件上传下载。
|
|
575
|
+
|
|
576
|
+
```typescript
|
|
577
|
+
// Upload file
|
|
578
|
+
await client.s3.upload(file);
|
|
579
|
+
|
|
580
|
+
// Download file
|
|
581
|
+
await client.s3.download("path/to/file");
|
|
582
|
+
```
|
|
583
|
+
|
|
283
584
|
## ⚙️ Configuration
|
|
284
585
|
|
|
285
586
|
### Client Options
|
|
@@ -303,6 +604,9 @@ interface AmasterClientOptions {
|
|
|
303
604
|
|
|
304
605
|
/** Refresh threshold in seconds (default: 300) */
|
|
305
606
|
refreshThreshold?: number;
|
|
607
|
+
|
|
608
|
+
/** Automatically handle OAuth callback on initialization (default: true) */
|
|
609
|
+
autoHandleOAuthCallback?: boolean;
|
|
306
610
|
}
|
|
307
611
|
```
|
|
308
612
|
|
|
@@ -360,6 +664,17 @@ if (result.success) {
|
|
|
360
664
|
}
|
|
361
665
|
```
|
|
362
666
|
|
|
667
|
+
### Custom HTTP Requests
|
|
668
|
+
|
|
669
|
+
```typescript
|
|
670
|
+
// Make a custom request with automatic auth
|
|
671
|
+
const { data, error } = await client.http.request({
|
|
672
|
+
url: "/custom/endpoint",
|
|
673
|
+
method: "POST",
|
|
674
|
+
data: { key: "value" },
|
|
675
|
+
});
|
|
676
|
+
```
|
|
677
|
+
|
|
363
678
|
## 🎨 Comparison with Individual Clients
|
|
364
679
|
|
|
365
680
|
### Before (Using separate clients)
|
|
@@ -444,101 +759,49 @@ Workflow execution API from `@amaster.ai/workflow-client`:
|
|
|
444
759
|
|
|
445
760
|
### `client.asr`
|
|
446
761
|
|
|
447
|
-
|
|
762
|
+
WebSocket real-time speech recognition:
|
|
448
763
|
|
|
449
|
-
- `connect()` - Connect to ASR service
|
|
764
|
+
- `connect()` - Connect to ASR service
|
|
450
765
|
- `startRecording()` - Start microphone recording
|
|
451
766
|
- `stopRecording()` - Stop recording
|
|
452
767
|
- `close()` - Close connection
|
|
453
768
|
|
|
454
|
-
|
|
769
|
+
### `client.asrHttp`
|
|
455
770
|
|
|
456
|
-
|
|
457
|
-
// Configure callbacks
|
|
458
|
-
client.asr = createASRClient({
|
|
459
|
-
onTranscript: (text, isFinal) => {
|
|
460
|
-
console.log(isFinal ? `[FINAL] ${text}` : `[INTERIM] ${text}`);
|
|
461
|
-
},
|
|
462
|
-
});
|
|
771
|
+
HTTP press-to-talk speech recognition:
|
|
463
772
|
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
773
|
+
- `startRecording()` - Start recording
|
|
774
|
+
- `stopRecording()` - Stop and get result
|
|
775
|
+
- `recognizeFile(file)` - Recognize audio file
|
|
776
|
+
- `recognizeUrl(url)` - Recognize audio URL
|
|
467
777
|
|
|
468
|
-
|
|
469
|
-
client.asr.stopRecording();
|
|
470
|
-
client.asr.close();
|
|
471
|
-
```
|
|
472
|
-
|
|
473
|
-
### `client.copilot`
|
|
474
|
-
|
|
475
|
-
AI assistant / chatbot API from `@amaster.ai/copilot-client`:
|
|
778
|
+
### `client.tts`
|
|
476
779
|
|
|
477
|
-
-
|
|
780
|
+
WebSocket real-time text-to-speech:
|
|
478
781
|
|
|
479
|
-
|
|
782
|
+
- `connect()` - Connect to TTS service
|
|
783
|
+
- `speak(text)` - Synthesize and play speech
|
|
784
|
+
- `play()` - Manually play audio
|
|
785
|
+
- `close()` - Close connection
|
|
480
786
|
|
|
481
|
-
|
|
482
|
-
// Simple chat
|
|
483
|
-
const result = await client.copilot.sendMessage([
|
|
484
|
-
{ role: "user", content: "Hello, how can you help?" },
|
|
485
|
-
]);
|
|
787
|
+
### `client.copilot`
|
|
486
788
|
|
|
487
|
-
|
|
789
|
+
AI assistant API:
|
|
488
790
|
|
|
489
|
-
|
|
490
|
-
await client.copilot.sendMessage([{ role: "user", content: "Tell me a story" }], {
|
|
491
|
-
stream: true,
|
|
492
|
-
onChunk: (chunk) => console.log(chunk),
|
|
493
|
-
});
|
|
494
|
-
```
|
|
791
|
+
- `sendMessage(messages, options?)` - Send messages to AI
|
|
495
792
|
|
|
496
793
|
### `client.function`
|
|
497
794
|
|
|
498
|
-
Function invocation API
|
|
795
|
+
Function invocation API:
|
|
499
796
|
|
|
500
797
|
- `invoke<T>(funcName, params?)` - Invoke a serverless function
|
|
501
798
|
|
|
502
|
-
|
|
799
|
+
### `client.s3`
|
|
503
800
|
|
|
504
|
-
|
|
505
|
-
// Call a function
|
|
506
|
-
const result = await client.function.invoke<{ result: string }>("sendEmail", {
|
|
507
|
-
to: "user@example.com",
|
|
508
|
-
subject: "Hello",
|
|
509
|
-
});
|
|
510
|
-
|
|
511
|
-
if (result.data) {
|
|
512
|
-
console.log("Function result:", result.data.result);
|
|
513
|
-
}
|
|
514
|
-
```
|
|
801
|
+
S3 storage API:
|
|
515
802
|
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
Real-time text-to-speech API from `@amaster.ai/tts-client`:
|
|
519
|
-
|
|
520
|
-
- `connect()` - Connect to TTS service via WebSocket
|
|
521
|
-
- `speak(text)` - Synthesize and play speech
|
|
522
|
-
- `play()` - Manually play audio (when autoPlay is off)
|
|
523
|
-
- `close()` - Close connection
|
|
524
|
-
|
|
525
|
-
**Example:**
|
|
526
|
-
|
|
527
|
-
```typescript
|
|
528
|
-
// Configure TTS
|
|
529
|
-
client.tts = createTTSClient({
|
|
530
|
-
voice: "Cherry",
|
|
531
|
-
autoPlay: true,
|
|
532
|
-
onAudioStart: () => console.log("Playing audio"),
|
|
533
|
-
});
|
|
534
|
-
|
|
535
|
-
// Connect and speak
|
|
536
|
-
await client.tts.connect();
|
|
537
|
-
await client.tts.speak("Hello, welcome to Amaster!");
|
|
538
|
-
|
|
539
|
-
// Close when done
|
|
540
|
-
client.tts.close();
|
|
541
|
-
```
|
|
803
|
+
- `upload(file)` - Upload file
|
|
804
|
+
- `download(path)` - Download file
|
|
542
805
|
|
|
543
806
|
## 🔐 Token Management Flow
|
|
544
807
|
|
|
@@ -572,10 +835,11 @@ All these packages are now integrated into `@amaster.ai/client`:
|
|
|
572
835
|
- `@amaster.ai/entity-client` - Entity CRUD operations
|
|
573
836
|
- `@amaster.ai/bpm-client` - Business Process Management
|
|
574
837
|
- `@amaster.ai/workflow-client` - Workflow execution
|
|
575
|
-
- `@amaster.ai/asr-client` - Speech recognition (WebSocket)
|
|
838
|
+
- `@amaster.ai/asr-client` - Speech recognition (WebSocket + HTTP)
|
|
576
839
|
- `@amaster.ai/copilot-client` - AI assistant / chatbot
|
|
577
840
|
- `@amaster.ai/function-client` - Function invocation
|
|
578
841
|
- `@amaster.ai/tts-client` - Text-to-speech (WebSocket)
|
|
842
|
+
- `@amaster.ai/s3-client` - S3 storage operations
|
|
579
843
|
- `@amaster.ai/http-client` - HTTP client foundation (internal)
|
|
580
844
|
|
|
581
845
|
## 💡 Inspiration
|
package/dist/index.cjs
CHANGED
|
@@ -8,11 +8,18 @@ var asrClient = require('@amaster.ai/asr-client');
|
|
|
8
8
|
var copilotClient = require('@amaster.ai/copilot-client');
|
|
9
9
|
var functionClient = require('@amaster.ai/function-client');
|
|
10
10
|
var ttsClient = require('@amaster.ai/tts-client');
|
|
11
|
+
var s3Client = require('@amaster.ai/s3-client');
|
|
11
12
|
var httpClient = require('@amaster.ai/http-client');
|
|
12
13
|
|
|
13
14
|
// src/client.ts
|
|
14
15
|
function createClient(options) {
|
|
15
|
-
const {
|
|
16
|
+
const {
|
|
17
|
+
baseURL,
|
|
18
|
+
headers = {},
|
|
19
|
+
onUnauthorized,
|
|
20
|
+
onTokenExpired,
|
|
21
|
+
autoHandleOAuthCallback
|
|
22
|
+
} = options;
|
|
16
23
|
const baseHttpClient = httpClient.createHttpClient({
|
|
17
24
|
baseURL,
|
|
18
25
|
headers
|
|
@@ -21,9 +28,34 @@ function createClient(options) {
|
|
|
21
28
|
baseURL,
|
|
22
29
|
headers,
|
|
23
30
|
onTokenExpired,
|
|
24
|
-
onUnauthorized
|
|
31
|
+
onUnauthorized,
|
|
32
|
+
autoHandleOAuthCallback
|
|
25
33
|
});
|
|
26
34
|
const createAuthenticatedHttpClient = () => {
|
|
35
|
+
let isRefreshing = false;
|
|
36
|
+
let refreshPromise = null;
|
|
37
|
+
function isTokenExpired(result) {
|
|
38
|
+
if (result.status !== 401) return false;
|
|
39
|
+
if (result.error?.message && /expired/i.test(result.error.message)) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
if (result.error?.details) {
|
|
43
|
+
const details = result.error.details;
|
|
44
|
+
if (typeof details === "string" && /expired/i.test(details)) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
if (typeof details === "object" && details !== null) {
|
|
48
|
+
const detailsObj = details;
|
|
49
|
+
if (typeof detailsObj.message === "string" && /expired/i.test(detailsObj.message)) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (typeof result.data === "string" && /expired/i.test(result.data)) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return !!auth.getAccessToken();
|
|
58
|
+
}
|
|
27
59
|
return {
|
|
28
60
|
async request(config) {
|
|
29
61
|
const token = auth.getAccessToken();
|
|
@@ -35,7 +67,32 @@ function createClient(options) {
|
|
|
35
67
|
...authHeaders
|
|
36
68
|
}
|
|
37
69
|
};
|
|
38
|
-
|
|
70
|
+
let result = await baseHttpClient.request(mergedConfig);
|
|
71
|
+
if (result.status === 401 && isTokenExpired(result)) {
|
|
72
|
+
if (!isRefreshing) {
|
|
73
|
+
isRefreshing = true;
|
|
74
|
+
refreshPromise = (async () => {
|
|
75
|
+
try {
|
|
76
|
+
const refreshResult = await auth.refreshToken();
|
|
77
|
+
return !!refreshResult.data;
|
|
78
|
+
} finally {
|
|
79
|
+
isRefreshing = false;
|
|
80
|
+
refreshPromise = null;
|
|
81
|
+
}
|
|
82
|
+
})();
|
|
83
|
+
}
|
|
84
|
+
const refreshed = await refreshPromise;
|
|
85
|
+
if (refreshed) {
|
|
86
|
+
const newToken = auth.getAccessToken();
|
|
87
|
+
result = await baseHttpClient.request({
|
|
88
|
+
...config,
|
|
89
|
+
headers: {
|
|
90
|
+
...config.headers,
|
|
91
|
+
...newToken ? { Authorization: `Bearer ${newToken}` } : {}
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
39
96
|
if (result.status === 401 && onUnauthorized) {
|
|
40
97
|
onUnauthorized();
|
|
41
98
|
}
|
|
@@ -44,22 +101,54 @@ function createClient(options) {
|
|
|
44
101
|
};
|
|
45
102
|
};
|
|
46
103
|
const authenticatedHttpClient = createAuthenticatedHttpClient();
|
|
104
|
+
let cachedUserUid = null;
|
|
105
|
+
auth.on("login", (user) => {
|
|
106
|
+
cachedUserUid = user?.uid ?? null;
|
|
107
|
+
});
|
|
108
|
+
auth.on("logout", () => {
|
|
109
|
+
cachedUserUid = null;
|
|
110
|
+
});
|
|
111
|
+
if (auth.isAuthenticated()) {
|
|
112
|
+
auth.getMe().then((result) => {
|
|
113
|
+
if (result.data?.uid) {
|
|
114
|
+
cachedUserUid = result.data.uid;
|
|
115
|
+
}
|
|
116
|
+
}).catch(() => {
|
|
117
|
+
});
|
|
118
|
+
}
|
|
47
119
|
const entity = entityClient.createEntityClient(authenticatedHttpClient);
|
|
48
120
|
const bpm = bpmClient.createBpmClient(authenticatedHttpClient);
|
|
49
121
|
const workflow = workflowClient.createWorkflowClient(authenticatedHttpClient);
|
|
50
122
|
const functionClient$1 = functionClient.createFunctionClient(authenticatedHttpClient);
|
|
51
|
-
const copilot = copilotClient.
|
|
52
|
-
|
|
53
|
-
|
|
123
|
+
const copilot = copilotClient.createCopilotClient(
|
|
124
|
+
authenticatedHttpClient,
|
|
125
|
+
baseURL,
|
|
126
|
+
() => auth.getAccessToken(),
|
|
127
|
+
() => cachedUserUid
|
|
128
|
+
);
|
|
129
|
+
const s3 = s3Client.createS3Client(authenticatedHttpClient);
|
|
130
|
+
const asr = asrClient.createASRClient({
|
|
131
|
+
getAccessToken: () => auth.getAccessToken()
|
|
132
|
+
});
|
|
133
|
+
const asrHttp = asrClient.createASRHttpClient({
|
|
134
|
+
getAccessToken: () => auth.getAccessToken(),
|
|
135
|
+
http: authenticatedHttpClient
|
|
136
|
+
});
|
|
137
|
+
const tts = ttsClient.createTTSClient({
|
|
138
|
+
getAccessToken: () => auth.getAccessToken()
|
|
139
|
+
});
|
|
54
140
|
const client = {
|
|
55
141
|
auth,
|
|
56
142
|
entity,
|
|
57
143
|
bpm,
|
|
58
144
|
workflow,
|
|
59
145
|
asr,
|
|
146
|
+
asrHttp,
|
|
60
147
|
copilot,
|
|
61
148
|
function: functionClient$1,
|
|
62
149
|
tts,
|
|
150
|
+
s3,
|
|
151
|
+
http: authenticatedHttpClient,
|
|
63
152
|
// Expose token management methods from auth client
|
|
64
153
|
isAuthenticated: () => auth.isAuthenticated(),
|
|
65
154
|
getAccessToken: () => auth.getAccessToken(),
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client.ts"],"names":["createHttpClient","createAuthClient","createEntityClient","createBpmClient","createWorkflowClient","functionClient","createFunctionClient","createCopilotA2UIClient","createASRClient","createTTSClient"],"mappings":";;;;;;;;;;;;;AA8FO,SAAS,aAAa,OAAA,EAA8C;AACzE,EAAA,MAAM,EAAE,OAAA,EAAS,OAAA,GAAU,EAAC,EAAG,cAAA,EAAgB,gBAAe,GAAI,OAAA;AAGlE,EAAA,MAAM,iBAAiBA,2BAAA,CAAiB;AAAA,IACtC,OAAA;AAAA,IACA;AAAA,GACD,CAAA;AAGD,EAAA,MAAM,OAAmBC,2BAAA,CAAiB;AAAA,IACxC,OAAA;AAAA,IACA,OAAA;AAAA,IACA,cAAA;AAAA,IACA;AAAA,GACD,CAAA;AAGD,EAAA,MAAM,gCAAgC,MAAkB;AACtD,IAAA,OAAO;AAAA,MACL,MAAM,QAAW,MAAA,EAAiD;AAEhE,QAAA,MAAM,KAAA,GAAQ,KAAK,cAAA,EAAe;AAGlC,QAAA,MAAM,WAAA,GAAc,QAAQ,EAAE,aAAA,EAAe,UAAU,KAAK,CAAA,CAAA,KAAO,EAAC;AACpE,QAAA,MAAM,YAAA,GAA8B;AAAA,UAClC,GAAG,MAAA;AAAA,UACH,OAAA,EAAS;AAAA,YACP,GAAG,MAAA,CAAO,OAAA;AAAA,YACV,GAAG;AAAA;AACL,SACF;AAGA,QAAA,MAAM,MAAA,GAAS,MAAM,cAAA,CAAe,OAAA,CAAW,YAAY,CAAA;AAG3D,QAAA,IAAI,MAAA,CAAO,MAAA,KAAW,GAAA,IAAO,cAAA,EAAgB;AAC3C,UAAA,cAAA,EAAe;AAAA,QACjB;AAEA,QAAA,OAAO,MAAA;AAAA,MACT;AAAA,KACF;AAAA,EACF,CAAA;AAGA,EAAA,MAAM,0BAA0B,6BAAA,EAA8B;AAG9D,EAAA,MAAM,MAAA,GAAuBC,gCAAmB,uBAAuB,CAAA;AACvE,EAAA,MAAM,GAAA,GAAiBC,0BAAgB,uBAAuB,CAAA;AAC9D,EAAA,MAAM,QAAA,GAA2BC,oCAAqB,uBAAuB,CAAA;AAC7E,EAAA,MAAMC,gBAAA,GAAiCC,oCAAqB,uBAAuB,CAAA;AACnF,EAAA,MAAM,OAAA,GAA6BC,sCAAwB,uBAAuB,CAAA;AAIlF,EAAA,MAAM,GAAA,GAAiBC,yBAAA,CAAgB,EAAE,CAAA;AACzC,EAAA,MAAM,GAAA,GAAiBC,yBAAA,CAAgB,EAAE,CAAA;AAGzC,EAAA,MAAM,MAAA,GAAwB;AAAA,IAC5B,IAAA;AAAA,IACA,MAAA;AAAA,IACA,GAAA;AAAA,IACA,QAAA;AAAA,IACA,GAAA;AAAA,IACA,OAAA;AAAA,IACA,QAAA,EAAUJ,gBAAA;AAAA,IACV,GAAA;AAAA;AAAA,IAGA,eAAA,EAAiB,MAAM,IAAA,CAAK,eAAA,EAAgB;AAAA,IAC5C,cAAA,EAAgB,MAAM,IAAA,CAAK,cAAA,EAAe;AAAA,IAC1C,cAAA,EAAgB,CAAC,KAAA,KAAkB,IAAA,CAAK,eAAe,KAAK,CAAA;AAAA,IAC5D,SAAA,EAAW,MAAM,IAAA,CAAK,SAAA;AAAU,GAClC;AAEA,EAAA,OAAO,MAAA;AACT","file":"index.cjs","sourcesContent":["/**\n * ============================================================================\n * @amaster.ai/client - Unified Amaster Client\n * ============================================================================\n * \n * Supabase-inspired unified API client for the Amaster platform\n * \n * Features:\n * - Single client instance for all services (auth, entity, bpm, workflow)\n * - Automatic token management and refresh\n * - Auto-attach authentication to all requests\n * - Centralized error handling\n * \n * @example\n * ```typescript\n * // With explicit baseURL\n * const client = createClient({\n * baseURL: 'https://api.amaster.ai',\n * onUnauthorized: () => window.location.href = '/login'\n * });\n * \n * // Auto-detect baseURL from env (Taro/Mini-program)\n * const client = createClient({\n * onUnauthorized: () => window.location.href = '/login'\n * });\n * \n * // Login\n * await client.auth.login({ email, password });\n * \n * // All subsequent requests automatically include auth token\n * await client.entity.list('default', 'users');\n * await client.bpm.startProcess({ processKey: 'approval' });\n * ```\n */\n\nimport { createAuthClient, type AuthClient } from \"@amaster.ai/auth-client\";\nimport { createEntityClient, type EntityClient } from \"@amaster.ai/entity-client\";\nimport { createBpmClient, type BpmClient } from \"@amaster.ai/bpm-client\";\nimport { createWorkflowClient, type WorkflowClient } from \"@amaster.ai/workflow-client\";\nimport { createASRClient, type ASRClient } from \"@amaster.ai/asr-client\";\nimport { createCopilotA2UIClient, type CopilotA2UIClient } from \"@amaster.ai/copilot-client\";\nimport { createFunctionClient, type FunctionClient } from \"@amaster.ai/function-client\";\nimport { createTTSClient, type TTSClient } from \"@amaster.ai/tts-client\";\nimport { createHttpClient, type HttpClient, type RequestConfig, type ClientResult } from \"@amaster.ai/http-client\";\nimport type { AmasterClient, AmasterClientOptions } from \"./types\";\n\n/**\n * Create a unified Amaster client instance\n * \n * This function creates a single client that provides access to all Amaster services:\n * - Authentication (login, register, logout)\n * - Entity CRUD operations\n * - BPM (Business Process Management)\n * - Workflow execution\n * \n * All sub-clients automatically share the same HTTP client and authentication state,\n * ensuring that tokens are consistently attached to all requests.\n * \n * @param options - Client configuration options\n * @returns A unified Amaster client instance\n * \n * @example\n * ```typescript\n * // Basic usage with explicit baseURL\n * const client = createClient({\n * baseURL: 'https://api.amaster.ai'\n * });\n * \n * // Auto-detect baseURL (for Taro/Mini-program or dev proxy)\n * const client = createClient({});\n * \n * // With authentication callbacks\n * const client = createClient({\n * baseURL: 'https://api.amaster.ai',\n * onUnauthorized: () => {\n * // Redirect to login or show auth modal\n * window.location.href = '/login';\n * },\n * onTokenExpired: () => {\n * console.log('Token expired, refreshing...');\n * }\n * });\n * \n * // Login\n * await client.auth.login({\n * email: 'user@example.com',\n * password: 'password123'\n * });\n * \n * // Now all requests automatically include the auth token\n * const users = await client.entity.list('default', 'users');\n * const tasks = await client.bpm.getMyTasks();\n * ```\n */\nexport function createClient(options: AmasterClientOptions): AmasterClient {\n const { baseURL, headers = {}, onUnauthorized, onTokenExpired } = options;\n\n // Create the base HTTP client\n const baseHttpClient = createHttpClient({\n baseURL,\n headers,\n });\n\n // Create the auth client first (it manages its own HTTP client internally)\n const auth: AuthClient = createAuthClient({\n baseURL,\n headers,\n onTokenExpired,\n onUnauthorized,\n });\n\n // Create a wrapper HTTP client that automatically adds the auth token\n const createAuthenticatedHttpClient = (): HttpClient => {\n return {\n async request<T>(config: RequestConfig): Promise<ClientResult<T>> {\n // Get the current token from auth client\n const token = auth.getAccessToken();\n \n // Merge Authorization header with existing headers\n const authHeaders = token ? { Authorization: `Bearer ${token}` } : {};\n const mergedConfig: RequestConfig = {\n ...config,\n headers: {\n ...config.headers,\n ...authHeaders,\n },\n };\n\n // Make the request with the updated config\n const result = await baseHttpClient.request<T>(mergedConfig);\n\n // Handle 401 errors\n if (result.status === 401 && onUnauthorized) {\n onUnauthorized();\n }\n\n return result;\n },\n };\n };\n\n // Create the authenticated HTTP client\n const authenticatedHttpClient = createAuthenticatedHttpClient();\n\n // Create other clients using the authenticated HTTP client\n const entity: EntityClient = createEntityClient(authenticatedHttpClient);\n const bpm: BpmClient = createBpmClient(authenticatedHttpClient);\n const workflow: WorkflowClient = createWorkflowClient(authenticatedHttpClient);\n const functionClient: FunctionClient = createFunctionClient(authenticatedHttpClient);\n const copilot: CopilotA2UIClient = createCopilotA2UIClient(authenticatedHttpClient);\n\n // ASR and TTS clients use WebSocket, create with default config\n // Users can reconfigure by accessing client.asr / client.tts directly\n const asr: ASRClient = createASRClient({});\n const tts: TTSClient = createTTSClient({});\n\n // Return unified client interface\n const client: AmasterClient = {\n auth,\n entity,\n bpm,\n workflow,\n asr,\n copilot,\n function: functionClient,\n tts,\n\n // Expose token management methods from auth client\n isAuthenticated: () => auth.isAuthenticated(),\n getAccessToken: () => auth.getAccessToken(),\n setAccessToken: (token: string) => auth.setAccessToken(token),\n clearAuth: () => auth.clearAuth(),\n };\n\n return client;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/client.ts"],"names":["createHttpClient","createAuthClient","createEntityClient","createBpmClient","createWorkflowClient","functionClient","createFunctionClient","createCopilotClient","createS3Client","createASRClient","createASRHttpClient","createTTSClient"],"mappings":";;;;;;;;;;;;;;AA2GO,SAAS,aAAa,OAAA,EAA8C;AACzE,EAAA,MAAM;AAAA,IACJ,OAAA;AAAA,IACA,UAAU,EAAC;AAAA,IACX,cAAA;AAAA,IACA,cAAA;AAAA,IACA;AAAA,GACF,GAAI,OAAA;AAGJ,EAAA,MAAM,iBAAiBA,2BAAA,CAAiB;AAAA,IACtC,OAAA;AAAA,IACA;AAAA,GACD,CAAA;AAGD,EAAA,MAAM,OAAmBC,2BAAA,CAAiB;AAAA,IACxC,OAAA;AAAA,IACA,OAAA;AAAA,IACA,cAAA;AAAA,IACA,cAAA;AAAA,IACA;AAAA,GACD,CAAA;AAGD,EAAA,MAAM,gCAAgC,MAAkB;AAEtD,IAAA,IAAI,YAAA,GAAe,KAAA;AACnB,IAAA,IAAI,cAAA,GAA0C,IAAA;AAM9C,IAAA,SAAS,eAAe,MAAA,EAAwC;AAC9D,MAAA,IAAI,MAAA,CAAO,MAAA,KAAW,GAAA,EAAK,OAAO,KAAA;AAGlC,MAAA,IAAI,MAAA,CAAO,OAAO,OAAA,IAAW,UAAA,CAAW,KAAK,MAAA,CAAO,KAAA,CAAM,OAAO,CAAA,EAAG;AAClE,QAAA,OAAO,IAAA;AAAA,MACT;AAGA,MAAA,IAAI,MAAA,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AAA;AAAA,IACA,IAAA,EAAM,uBAAA;AAAA;AAAA,IAGN,eAAA,EAAiB,MAAM,IAAA,CAAK,eAAA,EAAgB;AAAA,IAC5C,cAAA,EAAgB,MAAM,IAAA,CAAK,cAAA,EAAe;AAAA,IAC1C,cAAA,EAAgB,CAAC,KAAA,KAAkB,IAAA,CAAK,eAAe,KAAK,CAAA;AAAA,IAC5D,SAAA,EAAW,MAAM,IAAA,CAAK,SAAA;AAAU,GAClC;AAEA,EAAA,OAAO,MAAA;AACT","file":"index.cjs","sourcesContent":["/**\n * ============================================================================\n * @amaster.ai/client - Unified Amaster Client\n * ============================================================================\n *\n * Supabase-inspired unified API client for the Amaster platform\n *\n * Features:\n * - Single client instance for all services (auth, entity, bpm, workflow)\n * - Automatic token management and refresh\n * - Auto-attach authentication to all requests\n * - Centralized error handling\n *\n * @example\n * ```typescript\n * // With explicit baseURL\n * const client = createClient({\n * baseURL: 'https://api.amaster.ai',\n * onUnauthorized: () => window.location.href = '/login'\n * });\n *\n * // Auto-detect baseURL from env (Taro/Mini-program)\n * const client = createClient({\n * onUnauthorized: () => window.location.href = '/login'\n * });\n *\n * // Login\n * await client.auth.login({ email, password });\n *\n * // All subsequent requests automatically include auth token\n * await client.entity.list('default', 'users');\n * await client.bpm.startProcess({ processKey: 'approval' });\n * ```\n */\n\nimport { createAuthClient, type AuthClient } from \"@amaster.ai/auth-client\";\nimport { createEntityClient, type EntityClient } from \"@amaster.ai/entity-client\";\nimport { createBpmClient, type BpmClient } from \"@amaster.ai/bpm-client\";\nimport { createWorkflowClient, type WorkflowClient } from \"@amaster.ai/workflow-client\";\nimport {\n createASRClient,\n createASRHttpClient,\n type ASRClientConfig,\n type ASRClient,\n type ASRHttpClientConfig,\n type ASRHttpClient,\n} from \"@amaster.ai/asr-client\";\nimport { createCopilotClient, type CopilotClient } from \"@amaster.ai/copilot-client\";\nimport { createFunctionClient, type FunctionClient } from \"@amaster.ai/function-client\";\nimport { createTTSClient, TTSClientConfig, type TTSClient } from \"@amaster.ai/tts-client\";\nimport { createS3Client, type S3Client } from \"@amaster.ai/s3-client\";\nimport {\n createHttpClient,\n type HttpClient,\n type RequestConfig,\n type ClientResult,\n} from \"@amaster.ai/http-client\";\nimport type { AmasterClient, AmasterClientOptions } from \"./types\";\n\n/**\n * Create a unified Amaster client instance\n *\n * This function creates a single client that provides access to all Amaster services:\n * - Authentication (login, register, logout)\n * - Entity CRUD operations\n * - BPM (Business Process Management)\n * - Workflow execution\n *\n * All sub-clients automatically share the same HTTP client and authentication state,\n * ensuring that tokens are consistently attached to all requests.\n *\n * @param options - Client configuration options\n * @returns A unified Amaster client instance\n *\n * @example\n * ```typescript\n * // Basic usage with explicit baseURL\n * const client = createClient({\n * baseURL: 'https://api.amaster.ai'\n * });\n *\n * // Auto-detect baseURL (for Taro/Mini-program or dev proxy)\n * const client = createClient({});\n *\n * // With authentication callbacks\n * const client = createClient({\n * baseURL: 'https://api.amaster.ai',\n * onUnauthorized: () => {\n * // Redirect to login or show auth modal\n * window.location.href = '/login';\n * },\n * onTokenExpired: () => {\n * console.log('Token expired, refreshing...');\n * }\n * });\n *\n * // Login\n * await client.auth.login({\n * email: 'user@example.com',\n * password: 'password123'\n * });\n *\n * // Now all requests automatically include the auth token\n * const users = await client.entity.list('default', 'users');\n * const tasks = await client.bpm.getMyTasks();\n * ```\n */\nexport function createClient(options: AmasterClientOptions): AmasterClient {\n const {\n baseURL,\n headers = {},\n onUnauthorized,\n onTokenExpired,\n autoHandleOAuthCallback,\n } = options;\n\n // Create the base HTTP client\n const baseHttpClient = createHttpClient({\n baseURL,\n headers,\n });\n\n // Create the auth client first (it manages its own HTTP client internally)\n const auth: AuthClient = createAuthClient({\n baseURL,\n headers,\n onTokenExpired,\n onUnauthorized,\n autoHandleOAuthCallback,\n });\n\n // Create a wrapper HTTP client that automatically adds the auth token\n const createAuthenticatedHttpClient = (): HttpClient => {\n // Track if we're currently refreshing to avoid multiple simultaneous refreshes\n let isRefreshing = false;\n let refreshPromise: Promise<boolean> | null = null;\n\n /**\n * Check if 401 error is due to token expiration\n * Traefik JWT plugin returns plain text like \"Jwt is expired\" or \"Token is expired\"\n */\n function isTokenExpired(result: ClientResult<unknown>): boolean {\n if (result.status !== 401) return false;\n\n // Check error message (could be from backend JSON response)\n if (result.error?.message && /expired/i.test(result.error.message)) {\n return true;\n }\n\n // Check error details (traefik returns plain text in details)\n if (result.error?.details) {\n const details = result.error.details;\n if (typeof details === \"string\" && /expired/i.test(details)) {\n return true;\n }\n // Also check if details is an object with message field\n if (typeof details === \"object\" && details !== null) {\n const detailsObj = details as Record<string, unknown>;\n if (typeof detailsObj.message === \"string\" && /expired/i.test(detailsObj.message)) {\n return true;\n }\n }\n }\n\n // Check raw data (could be plain text from traefik)\n if (typeof result.data === \"string\" && /expired/i.test(result.data)) {\n return true;\n }\n\n // If we have a token but got 401, assume it might be expired\n return !!auth.getAccessToken();\n }\n\n return {\n async request<T>(config: RequestConfig): Promise<ClientResult<T>> {\n // Get the current token from auth client\n const token = auth.getAccessToken();\n\n // Merge Authorization header with existing headers\n const authHeaders = token ? { Authorization: `Bearer ${token}` } : {};\n const mergedConfig: RequestConfig = {\n ...config,\n headers: {\n ...config.headers,\n ...authHeaders,\n },\n };\n\n // Make the request with the updated config\n let result = await baseHttpClient.request<T>(mergedConfig);\n\n // Handle 401 errors with automatic token refresh\n if (result.status === 401 && isTokenExpired(result)) {\n // Attempt to refresh token\n if (!isRefreshing) {\n isRefreshing = true;\n refreshPromise = (async () => {\n try {\n const refreshResult = await auth.refreshToken();\n return !!refreshResult.data;\n } finally {\n isRefreshing = false;\n refreshPromise = null;\n }\n })();\n }\n\n const refreshed = await refreshPromise;\n\n if (refreshed) {\n // Retry with new token\n const newToken = auth.getAccessToken();\n result = await baseHttpClient.request<T>({\n ...config,\n headers: {\n ...config.headers,\n ...(newToken ? { Authorization: `Bearer ${newToken}` } : {}),\n },\n });\n }\n }\n\n // Trigger unauthorized if still 401\n if (result.status === 401 && onUnauthorized) {\n onUnauthorized();\n }\n\n return result;\n },\n };\n };\n\n const authenticatedHttpClient = createAuthenticatedHttpClient();\n\n let cachedUserUid: string | null = null;\n\n auth.on(\"login\", (user: { uid?: string }) => {\n cachedUserUid = user?.uid ?? null;\n });\n auth.on(\"logout\", () => {\n cachedUserUid = null;\n });\n\n if (auth.isAuthenticated()) {\n auth\n .getMe()\n .then((result) => {\n if (result.data?.uid) {\n cachedUserUid = result.data.uid;\n }\n })\n .catch(() => {});\n }\n\n // Create other clients using the authenticated HTTP client\n const entity: EntityClient = createEntityClient(authenticatedHttpClient);\n const bpm: BpmClient = createBpmClient(authenticatedHttpClient);\n const workflow: WorkflowClient = createWorkflowClient(authenticatedHttpClient);\n const functionClient: FunctionClient = createFunctionClient(authenticatedHttpClient);\n const copilot: CopilotClient = createCopilotClient(\n authenticatedHttpClient,\n baseURL,\n () => auth.getAccessToken(),\n () => cachedUserUid\n );\n const s3: S3Client = createS3Client(authenticatedHttpClient);\n\n // ASR and TTS clients use WebSocket, pass token getter for authentication\n // Token can be appended to WebSocket URL as query parameter\n const asr: (config: ASRClientConfig) => ASRClient = createASRClient({\n getAccessToken: () => auth.getAccessToken(),\n });\n const asrHttp: (config: ASRHttpClientConfig) => ASRHttpClient = createASRHttpClient({\n getAccessToken: () => auth.getAccessToken(),\n http: authenticatedHttpClient,\n });\n const tts: (config: TTSClientConfig) => TTSClient = createTTSClient({\n getAccessToken: () => auth.getAccessToken(),\n });\n\n // Return unified client interface\n const client: AmasterClient = {\n auth,\n entity,\n bpm,\n workflow,\n asr,\n asrHttp,\n copilot,\n function: functionClient,\n tts,\n s3,\n http: authenticatedHttpClient,\n\n // Expose token management methods from auth client\n isAuthenticated: () => auth.isAuthenticated(),\n getAccessToken: () => auth.getAccessToken(),\n setAccessToken: (token: string) => auth.setAccessToken(token),\n clearAuth: () => auth.clearAuth(),\n };\n\n return client;\n}\n"]}
|