@bytexbyte/nxtlinq-ai-agent-sdk 1.6.20 → 1.6.22
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/dist/components/context/ChatBotContext.d.ts.map +1 -1
- package/dist/components/context/ChatBotContext.js +286 -1
- package/dist/components/types/ChatBotTypes.d.ts +8 -0
- package/dist/components/types/ChatBotTypes.d.ts.map +1 -1
- package/dist/components/ui/MessageInput.d.ts.map +1 -1
- package/dist/components/ui/MessageInput.js +11 -2
- package/dist/components/ui/MessageList.d.ts.map +1 -1
- package/dist/components/ui/MessageList.js +22 -2
- package/dist/components/ui/ModelSelector.d.ts.map +1 -1
- package/dist/components/ui/ModelSelector.js +14 -5
- package/dist/core/lib/textToSpeech.d.ts +22 -0
- package/dist/core/lib/textToSpeech.d.ts.map +1 -0
- package/dist/core/lib/textToSpeech.js +109 -0
- package/package.json +1 -1
- package/umd/nxtlinq-ai-agent.umd.js +138 -134
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { SpeechConfig, SpeechSynthesizer } from 'microsoft-cognitiveservices-speech-sdk';
|
|
2
|
+
import Cookie from 'universal-cookie';
|
|
3
|
+
// Hardcoded Azure Speech credentials
|
|
4
|
+
const SPEECH_KEY = '8b2cec35ebcd4e6e94da56537c5e910c';
|
|
5
|
+
const SPEECH_REGION = 'westcentralus';
|
|
6
|
+
const cookie = new Cookie();
|
|
7
|
+
/**
|
|
8
|
+
* Default getSpeechToken function that directly calls Azure token service
|
|
9
|
+
* Uses hardcoded SPEECH_KEY and SPEECH_REGION to get Azure Speech token
|
|
10
|
+
*/
|
|
11
|
+
export async function getDefaultSpeechToken() {
|
|
12
|
+
try {
|
|
13
|
+
const tokenResponse = await fetch(`https://${SPEECH_REGION}.api.cognitive.microsoft.com/sts/v1.0/issueToken`, {
|
|
14
|
+
method: 'POST',
|
|
15
|
+
headers: {
|
|
16
|
+
'Ocp-Apim-Subscription-Key': SPEECH_KEY,
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
if (!tokenResponse.ok) {
|
|
20
|
+
const errorText = await tokenResponse.text();
|
|
21
|
+
return {
|
|
22
|
+
authToken: '',
|
|
23
|
+
region: '',
|
|
24
|
+
error: `Failed to get token: ${errorText}`
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
const token = await tokenResponse.text();
|
|
28
|
+
return {
|
|
29
|
+
authToken: token,
|
|
30
|
+
region: SPEECH_REGION
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
console.error('Failed to fetch Azure Speech token:', error);
|
|
35
|
+
return {
|
|
36
|
+
authToken: '',
|
|
37
|
+
region: '',
|
|
38
|
+
error: error instanceof Error ? error.message : 'Failed to fetch token'
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get or refresh Azure Cognitive Services Speech token
|
|
44
|
+
*/
|
|
45
|
+
export async function getTokenOrRefresh(getToken) {
|
|
46
|
+
const speechToken = cookie.get('speech-token');
|
|
47
|
+
if (speechToken === undefined) {
|
|
48
|
+
try {
|
|
49
|
+
const tokenRes = await getToken();
|
|
50
|
+
if (tokenRes.error) {
|
|
51
|
+
return tokenRes;
|
|
52
|
+
}
|
|
53
|
+
// Cache token in cookie for 9 minutes (540 seconds)
|
|
54
|
+
// Token typically expires in 10 minutes
|
|
55
|
+
cookie.set('speech-token', tokenRes.region + ':' + tokenRes.authToken, {
|
|
56
|
+
maxAge: 540,
|
|
57
|
+
path: '/'
|
|
58
|
+
});
|
|
59
|
+
return tokenRes;
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
const err = e;
|
|
63
|
+
console.error('Failed to get speech token:', err);
|
|
64
|
+
return {
|
|
65
|
+
authToken: '',
|
|
66
|
+
region: '',
|
|
67
|
+
error: err.response?.data || 'Failed to get speech token'
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
// Token exists in cookie, parse it
|
|
73
|
+
const idx = speechToken.indexOf(':');
|
|
74
|
+
if (idx === -1) {
|
|
75
|
+
// Invalid format, fetch new token
|
|
76
|
+
cookie.remove('speech-token', { path: '/' });
|
|
77
|
+
return getTokenOrRefresh(getToken);
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
authToken: speechToken.slice(idx + 1),
|
|
81
|
+
region: speechToken.slice(0, idx)
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Convert text to audio buffer using Azure Cognitive Services Speech SDK
|
|
87
|
+
*/
|
|
88
|
+
export default async function textToBuffer(text, getToken) {
|
|
89
|
+
const tokenRes = await getTokenOrRefresh(getToken);
|
|
90
|
+
if (tokenRes.error || !tokenRes.authToken || !tokenRes.region) {
|
|
91
|
+
console.error('Failed to get speech token:', tokenRes.error);
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
const speechConfig = SpeechConfig.fromAuthorizationToken(tokenRes.authToken, tokenRes.region);
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
96
|
+
// @ts-ignore
|
|
97
|
+
const synthesizer = new SpeechSynthesizer(speechConfig, null);
|
|
98
|
+
const res = await new Promise((resolve) => {
|
|
99
|
+
synthesizer.speakTextAsync(text, (result) => {
|
|
100
|
+
resolve(result);
|
|
101
|
+
}, (err) => resolve(err));
|
|
102
|
+
});
|
|
103
|
+
synthesizer.close();
|
|
104
|
+
if (typeof res === 'string') {
|
|
105
|
+
console.error('Text-to-speech error:', res);
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
return res.audioData;
|
|
109
|
+
}
|
package/package.json
CHANGED