Skip to content

Commit a7c0095

Browse files
canisminor1990闻冰
authored andcommitted
♻️ refactor: Refactor api
1 parent 8698bc7 commit a7c0095

39 files changed

+477
-638
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,17 @@ Click button below to deploy your private plugins' gateway.
8686

8787
This project provides some additional configuration items set with environment variables:
8888

89-
| Environment Variable | Description | Default |
90-
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------- |
91-
| `ALLOW_ORIGINS` | Allow origins , string or string array | |
92-
| `OPENAI_API_KEY` | This is the API key you apply on the OpenAI account page | `sk-xxxxxx...xxxxxx` |
93-
| `OPENAI_PROXY_URL` | If you manually configure the OpenAI interface proxy, you can use this configuration item to override the default OpenAI API request base URL | `https://api.openai.com/v1` |
94-
| `AZURE_SPEECH_KEY` | This is the API key of Azure Speech Service | |
95-
| `AZURE_SPEECH_REGION` | This is the region of Azure Speech Service | |
96-
| `AZURE_SPEECH_PROXY_URL` | If you manually configure the AZURE Speech interface proxy, you can use this configuration item to override the default Speech API request base URL | `/api/azure-speech` |
97-
| `MICROSOFT_SPEECH_PROXY_URL` | If you manually configure the Microsoft Speech interface proxy, you can use this configuration item to override the default Speech API request base URL | `/api/microsoft-speech` |
98-
| `EDDGE_API_TOKEN` | This is the API key of Edge Speech Service | |
99-
| `EDDGE_PROXY_URL` | If you manually configure the Edge interface proxy, you can use this configuration item to override the default Edge wss request base URL | |
89+
| Environment Variable | Description | Default |
90+
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------- |
91+
| `ALLOW_ORIGINS` | Allow origins , string or string array | |
92+
| `OPENAI_API_KEY` | This is the API key you apply on the OpenAI account page | `sk-xxxxxx...xxxxxx` |
93+
| `OPENAI_PROXY_URL` | If you manually configure the OpenAI interface proxy, you can use this configuration item to override the default OpenAI API request base URL | `https://api.openai.com/v1` |
94+
| `AZURE_SPEECH_KEY` | This is the API key of Azure Speech Service | |
95+
| `AZURE_SPEECH_REGION` | This is the region of Azure Speech Service | |
96+
| `AZURE_SPEECH_PROXY_URL` | If you manually configure the AZURE Speech interface proxy, you can use this configuration item to override the default Speech API request base URL | `/api/azure-speech` |
97+
| `MICROSOFT_SPEECH_API_URL` | If you manually configure the Microsoft Speech interface proxy, you can use this configuration item to override the default Speech API request base URL | `/api/microsoft-speech` |
98+
| `EDGE_API_TOKEN` | This is the API key of Edge Speech Service | |
99+
| `EDGE_SPEECH_API_URL` | If you manually configure the Edge interface proxy, you can use this configuration item to override the default Edge wss request base URL | |
100100

101101
<div align="right">
102102

api/azure-speech.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

api/edge-speech.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { createEdgeSpeechComletion } from '../src/server/createEdgeSpeechComletion';
2+
import { EdgeSpeechPayload } from '../src/server/types';
3+
4+
export const config = {
5+
runtime: 'edge',
6+
};
7+
8+
export default async (req: Request) => {
9+
if (req.method !== 'POST') return new Response('Method Not Allowed', { status: 405 });
10+
const payload = (await req.json()) as EdgeSpeechPayload;
11+
const res = await createEdgeSpeechComletion({ payload });
12+
return res;
13+
};

api/microsoft-speech.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import cors from '../src/server/cors';
2-
import { getAllowOrigins } from '../src/server/getAllowOrigins';
3-
import { handleMicrosoftSpeechRequest } from '../src/server/handleMicrosoftSpeechRequest';
1+
import { createMicrosoftSpeechComletion } from '../src/server/createMicrosoftSpeechComletion';
2+
import { MicrosoftSpeechPayload } from '../src/server/types';
43

54
export const config = {
65
runtime: 'edge',
76
};
87

98
export default async (req: Request) => {
109
if (req.method !== 'POST') return new Response('Method Not Allowed', { status: 405 });
11-
const origin = getAllowOrigins(req);
12-
if (!origin) return new Response('Origin Not Allowed', { status: 403 });
13-
const res = await handleMicrosoftSpeechRequest(req);
14-
return cors(req, new Response(res.body, res), { methods: ['POST'], origin });
10+
const payload = (await req.json()) as MicrosoftSpeechPayload;
11+
const res = await createMicrosoftSpeechComletion({ payload });
12+
return res;
1513
};

api/open-stt.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import OpenAI from 'openai';
2+
3+
import { OPENAI_API_KEY, OPENAI_PROXY_URL } from '@/const/api';
4+
5+
import { createOpenaiAudioTranscriptionsCompletion } from '../src/server/createOpenaiAudioTranscriptionsCompletion';
6+
import { OpenAISTTPayload } from '../src/server/types';
7+
8+
export const config = {
9+
runtime: 'edge',
10+
};
11+
12+
export default async (req: Request) => {
13+
if (req.method !== 'POST') return new Response('Method Not Allowed', { status: 405 });
14+
const payload = (await req.json()) as OpenAISTTPayload;
15+
if (!OPENAI_API_KEY) return new Response('OPENAI_API_KEY is not set', { status: 500 });
16+
const openai = new OpenAI({ apiKey: OPENAI_API_KEY, baseURL: OPENAI_PROXY_URL });
17+
const res = await createOpenaiAudioTranscriptionsCompletion({ openai, payload });
18+
return res;
19+
};

api/openai-tts.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import OpenAI from 'openai';
2+
3+
import { OPENAI_API_KEY, OPENAI_PROXY_URL } from '@/const/api';
4+
5+
import { createOpenaiAudioSpeechCompletion } from '../src/server/createOpenaiAudioSpeechCompletion';
6+
import { OpenAITTSPayload } from '../src/server/types';
7+
8+
export const config = {
9+
runtime: 'edge',
10+
};
11+
12+
export default async (req: Request) => {
13+
if (req.method !== 'POST') return new Response('Method Not Allowed', { status: 405 });
14+
const payload = (await req.json()) as OpenAITTSPayload;
15+
if (!OPENAI_API_KEY) return new Response('OPENAI_API_KEY is not set', { status: 500 });
16+
const openai = new OpenAI({ apiKey: OPENAI_API_KEY, baseURL: OPENAI_PROXY_URL });
17+
const res = await createOpenaiAudioSpeechCompletion({ openai, payload });
18+
return res;
19+
};

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,10 @@
6666
"antd-style": "^3",
6767
"lodash-es": "^4",
6868
"lucide-react": "latest",
69-
"microsoft-cognitiveservices-speech-sdk": "^1",
69+
"openai": "^4.17.3",
7070
"query-string": "^8",
7171
"react-error-boundary": "^4.0.11",
7272
"react-layout-kit": "^1",
73-
"ssml-document": "^1",
7473
"swr": "^2",
7574
"url-join": "^5",
7675
"uuid": "^9"

src/const/api.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import urlJoin from 'url-join';
2-
3-
export const MICROSOFT_SPPECH_URL =
1+
export const MICROSOFT_SPEECH_URL =
42
'https://southeastasia.api.speech.microsoft.com/accfreetrial/texttospeech/acc/v3.0-beta1/vcg/speak';
5-
export const MICROSOFT_SPEECH_PROXY_URL =
6-
process.env.MICROSOFT_SPEECH_PROXY_URL ||
7-
process.env.NEXT_PUBLIC_MICROSOFT_SPEECH_PROXY_URL ||
8-
'/api/microsoft-speech';
9-
export const AZURE_SPEECH_PROXY_URL =
10-
process.env.AZURE_SPEECH_PROXY_URL ||
11-
process.env.NEXT_PUBLIC_AZURE_SPEECH_PROXY_URL ||
12-
'/api/azure-speech';
3+
export const EDGE_SPEECH_URL =
4+
'wss://speech.platform.bing.com/consumer/speech/synthesize/readaloud/edge/v1';
5+
export const EDGE_API_TOKEN = '6A5AA1D4EAFF4E9FB37E23D68491D6F4';
6+
7+
export const MICROSOFT_SPEECH_API_URL = '/api/microsoft-speech';
8+
export const EDGE_SPEECH_API_URL = '/api/edge-speech';
9+
export const OPENAI_TTS_API_URL = '/api/openai-tts';
10+
export const OPENAI_STT_API_URL = '/api/openai-stt';
11+
1312
export const AZURE_SPEECH_KEY =
1413
process.env.AZURE_SPEECH_KEY || process.env.NEXT_PUBLIC_AZURE_SPEECH_KEY || '';
1514
export const AZURE_SPEECH_REGION =
@@ -20,14 +19,3 @@ export const OPENAI_PROXY_URL =
2019
process.env.OPENAI_PROXY_URL ||
2120
process.env.NEXT_PUBLIC_OPENAI_PROXY_URL ||
2221
'https://api.openai.com/v1';
23-
export const OPENAI_TTS_URL = (api?: string) => urlJoin(api || OPENAI_PROXY_URL, 'audio/speech');
24-
export const OPENAI_STT_URL = (api?: string) =>
25-
urlJoin(api || OPENAI_PROXY_URL, 'audio/transcriptions');
26-
export const EDDGE_PROXY_URL =
27-
process.env.EDDGE_PROXY_URL ||
28-
process.env.NEXT_PUBLIC_EDDGE_PROXY_UR ||
29-
'wss://speech.platform.bing.com/consumer/speech/synthesize/readaloud/edge/v1';
30-
export const EDDGE_API_TOKEN =
31-
process.env.EDDGE_API_TOKEN ||
32-
process.env.NEXT_PUBLIC_EDDGE_API_TOKEN ||
33-
'6A5AA1D4EAFF4E9FB37E23D68491D6F4';
File renamed without changes.

src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ export { default as AudioVisualizer, type AudioVisualizerProps } from './AudioVi
33
export { default as azureVoiceList } from './data/azureVoiceList';
44
export { default as edgeVoiceList } from './data/edgeVoiceList';
55
export { default as voiceLocale } from './data/locales';
6-
export { default as nameList } from './data/nameList';
76
export { default as openaiVoiceList } from './data/openaiVoiceList';
7+
export { default as voiceList } from './data/voiceList';
88
export { useAudioPlayer } from './hooks/useAudioPlayer';
99
export { useAudioVisualizer } from './hooks/useAudioVisualizer';
1010
export { useBlobUrl } from './hooks/useBlobUrl';
1111
export { useStreamAudioPlayer } from './hooks/useStreamAudioPlayer';
12-
export { type AzureSpeechOptions, fetchAzureSpeech } from './services/fetchAzureSpeech';
1312
export { type EdgeSpeechOptions, fetchEdgeSpeech } from './services/fetchEdgeSpeech';
1413
export { fetchMicrosoftSpeech, type MicrosoftSpeechOptions } from './services/fetchMicrosoftSpeech';
1514
export { fetchOpenaiSTT, type OpenaiSttOptions } from './services/fetchOpenaiSTT';
1615
export { fetchOpenaiTTS, type OpenaiTtsOptions } from './services/fetchOpenaiTTS';
1716
export { useAudioRecorder } from './useAudioRecorder';
18-
export { useAzureSpeech } from './useAzureSpeech';
1917
export { useEdgeSpeech } from './useEdgeSpeech';
2018
export { useMicrosoftSpeech } from './useMicrosoftSpeech';
2119
export {
@@ -42,3 +40,9 @@ export {
4240
getSpeechSynthesVoiceOptions,
4341
getVoiceLocaleOptions,
4442
} from './utils/getVoiceList';
43+
export {
44+
EDGE_SPEECH_API_URL,
45+
MICROSOFT_SPEECH_API_URL,
46+
OPENAI_STT_API_URL,
47+
OPENAI_TTS_API_URL,
48+
} from '@/const/api';

src/server.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
export { handleAzureSpeechRequest } from './server/handleAzureSpeechRequest';
2-
export { handleMicrosoftSpeechRequest } from './server/handleMicrosoftSpeechRequest';
1+
export { createEdgeSpeechComletion } from '@/server/createEdgeSpeechComletion';
2+
export { createMicrosoftSpeechComletion } from '@/server/createMicrosoftSpeechComletion';
3+
export { createOpenaiAudioSpeechCompletion } from '@/server/createOpenaiAudioSpeechCompletion';
4+
export { createOpenaiAudioTranscriptionsCompletion } from '@/server/createOpenaiAudioTranscriptionsCompletion';
5+
export * from '@/server/types';

src/server/cors.ts

Lines changed: 0 additions & 140 deletions
This file was deleted.

0 commit comments

Comments
 (0)