Skip to content

Commit

Permalink
♻️ refactor: refactor to class usage and ui
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Nov 16, 2023
1 parent ab4c8b6 commit 0a47f03
Show file tree
Hide file tree
Showing 80 changed files with 549 additions and 552 deletions.
6 changes: 4 additions & 2 deletions .dumirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import path from 'node:path';
import { description, homepage, name } from './package.json';

const isProduction = process.env.NODE_ENV === 'production';
const isWin = process.platform === 'win32';

const themeConfig = {
actions: [
Expand Down Expand Up @@ -42,17 +41,20 @@ const themeConfig = {
};

export default defineConfig({
/* eslint-disable sort-keys-fix/sort-keys-fix */
alias: {
'@lobehub/tts/react': path.join(__dirname, './src/react'),
},
/* eslint-enable */
apiParser: isProduction ? {} : false,
base: '/',
define: {
'process.env': process.env,
},
favicons: ['https://npm.elemecdn.com/@lobehub/assets-favicons/assets/favicon.ico'],
locales: [{ id: 'en-US', name: 'English' }],
mfsu: isWin ? undefined : {},
// mfsu: isWin ? undefined : {},
mfsu: false,
npmClient: 'pnpm',
publicPath: '/',
resolve: {
Expand Down
3 changes: 3 additions & 0 deletions .fatherrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export default defineConfig({
esm: {
output: 'dist',
overrides: {
'src/core': {
output: 'core',
},
'src/react': {
output: 'react',
},
Expand Down
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@ next-env.d.ts
.env
public/*.js
bun.lockb
react
server
react/**
server/**

core/**
6 changes: 3 additions & 3 deletions api/edge-speech.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createEdgeSpeechComletion } from '../src/server/createEdgeSpeechComletion';
import { EdgeSpeechPayload } from '../src/server/types';
import { EdgeSpeechPayload } from '@/core';
import { createEdgeSpeech } from '@/core/EdgeSpeechTTS/createEdgeSpeech';

export const config = {
runtime: 'edge',
Expand All @@ -10,5 +10,5 @@ export default async (req: Request) => {

const payload = (await req.json()) as EdgeSpeechPayload;

return createEdgeSpeechComletion({ payload });
return createEdgeSpeech({ payload });
};
6 changes: 3 additions & 3 deletions api/microsoft-speech.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createMicrosoftSpeechComletion } from '../src/server/createMicrosoftSpeechComletion';
import { MicrosoftSpeechPayload } from '../src/server/types';
import { MicrosoftSpeechPayload } from '@/core';
import { createMicrosoftSpeech } from '@/core/MicrosoftSpeechTTS/createMicrosoftSpeech';

export const config = {
runtime: 'edge',
Expand All @@ -9,5 +9,5 @@ export default async (req: Request) => {
if (req.method !== 'POST') return new Response('Method Not Allowed', { status: 405 });
const payload = (await req.json()) as MicrosoftSpeechPayload;

return createMicrosoftSpeechComletion({ payload });
return createMicrosoftSpeech({ payload });
};
6 changes: 3 additions & 3 deletions api/open-stt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import OpenAI from 'openai';

import { createOpenaiAudioTranscriptionsCompletion } from '../src/server/createOpenaiAudioTranscriptionsCompletion';
import { OpenAISTTPayload } from '../src/server/types';
import { OpenAISTTPayload } from '@/core';
import { createOpenaiAudioTranscriptions } from '@/server/createOpenaiAudioTranscriptions';

export const config = {
runtime: 'edge',
Expand All @@ -18,7 +18,7 @@ export default async (req: Request) => {
const payload = (await req.json()) as OpenAISTTPayload;

const openai = new OpenAI({ apiKey: OPENAI_API_KEY, baseURL: OPENAI_PROXY_URL });
const res = await createOpenaiAudioTranscriptionsCompletion({ openai, payload });
const res = await createOpenaiAudioTranscriptions({ openai, payload });

return new Response(JSON.stringify(res), {
headers: {
Expand Down
6 changes: 3 additions & 3 deletions api/openai-tts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import OpenAI from 'openai';

import { createOpenaiAudioSpeechCompletion } from '../src/server/createOpenaiAudioSpeechCompletion';
import { OpenAITTSPayload } from '../src/server/types';
import { OpenAITTSPayload } from '@/core';
import { createOpenaiAudioSpeech } from '@/server/createOpenaiAudioSpeech';

export const config = {
runtime: 'edge',
Expand All @@ -17,5 +17,5 @@ export default async (req: Request) => {

const openai = new OpenAI({ apiKey: OPENAI_API_KEY, baseURL: OPENAI_PROXY_URL });

return createOpenaiAudioSpeechCompletion({ openai, payload });
return createOpenaiAudioSpeech({ openai, payload });
};
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"module": "./dist/index.js"
"types": "./core/index.d.ts",
"import": "./core/index.js",
"module": "./core/index.js"
},
"./server": {
"types": "./server/index.d.ts",
Expand All @@ -32,22 +32,21 @@
"module": "./react/index.js"
}
},
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"main": "./core/index.js",
"module": "./core/index.js",
"types": "./core/index.d.ts",
"files": [
"dist",
"core",
"react",
"server"
],
"scripts": {
"build": "father build",
"ci": "npm run lint && npm run type-check && npm run doctor",
"clean": "rm react.d.ts server.d.ts",
"dev": "father dev",
"docs:build": "npm run setup && npm run build && dumi build",
"docs:build-analyze": "ANALYZE=1 dumi build",
"docs:dev": "npm run clean && npm run setup && dumi dev",
"docs:dev": "npm run setup && dumi dev",
"doctor": "father doctor",
"lint": "eslint \"{src,api,lib}/**/*.{js,jsx,ts,tsx}\" --fix",
"lint:md": "remark . --quiet --frail --output",
Expand Down
20 changes: 0 additions & 20 deletions src/class/EdgeSpeechTTS.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/class/MicrosoftSpeechTTS.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/class/OpenaiSTT.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/class/OpenaiTTS.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/class/SpeechSynthesTTS.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/const/api.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import qs from 'query-string';
import { v4 as uuidv4 } from 'uuid';

import { EDGE_API_TOKEN, EDGE_SPEECH_URL } from '../const/api';
import { EdgeSpeechPayload } from '../server/types';
import { genSSML } from '../utils/genSSML';
import { genSendContent } from '../utils/genSendContent';
import { getHeadersAndData } from '../utils/getHeadersAndData';
import { SsmlOptions, genSSML } from '@/core/utils/genSSML';
import { genSendContent } from '@/core/utils/genSendContent';
import { getHeadersAndData } from '@/core/utils/getHeadersAndData';

const configConent = JSON.stringify({
export interface EdgeSpeechPayload {
/**
* @title 语音合成的文本
*/
input: string;
/**
* @title SSML 语音合成的配置
*/
options: Pick<SsmlOptions, 'voice'>;
}

const EDGE_SPEECH_URL =
'wss://speech.platform.bing.com/consumer/speech/synthesize/readaloud/edge/v1';
const EDGE_API_TOKEN = '6A5AA1D4EAFF4E9FB37E23D68491D6F4';

const configContent = JSON.stringify({
context: {
synthesis: {
audio: {
Expand Down Expand Up @@ -37,26 +50,27 @@ const genHeader = (connectId: string) => {
};
};

interface CreateEdgeSpeechComletionOptions {
export interface CreateEdgeSpeechCompletionOptions {
payload: EdgeSpeechPayload;
}

export const createEdgeSpeechComletion = async ({
payload,
}: CreateEdgeSpeechComletionOptions): Promise<Response> => {
export const createEdgeSpeech = async (
{ payload }: CreateEdgeSpeechCompletionOptions,
{ proxyUrl, token }: { proxyUrl?: string; token?: string } = {},
): Promise<Response> => {
const { input, options } = payload;

const connectId = uuidv4().replaceAll('-', '');
const url = qs.stringifyUrl({
query: {
ConnectionId: connectId,
TrustedClientToken: EDGE_API_TOKEN,
TrustedClientToken: token ? token : EDGE_API_TOKEN,
},
url: EDGE_SPEECH_URL,
url: proxyUrl ? proxyUrl : EDGE_SPEECH_URL,
});

const { configHeader, contentHeader } = genHeader(connectId);
const config = genSendContent(configHeader, configConent);
const config = genSendContent(configHeader, configContent);
const content = genSendContent(contentHeader, genSSML(input, options));

return new Promise((resolve, reject) => {
Expand Down
File renamed without changes.
57 changes: 57 additions & 0 deletions src/core/EdgeSpeechTTS/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import edgeVoiceList from '@/core/EdgeSpeechTTS/edgeVoiceList';
import voiceName from '@/core/data/voiceList';
import { arrayBufferConvert } from '@/core/utils/arrayBufferConvert';
import { SsmlOptions } from '@/core/utils/genSSML';
import { getVoiceLocaleOptions } from '@/core/utils/getVoiceList';

import { createEdgeSpeech } from './createEdgeSpeech';
import { getEdgeVoiceOptions } from './options';

export interface EdgeSpeechPayload {
/**
* @title 语音合成的文本
*/
input: string;
/**
* @title SSML 语音合成的配置
*/
options: Pick<SsmlOptions, 'voice'>;
}

export class EdgeSpeechTTS {
private locale?: string;
private BASE_URL: string | undefined;

constructor({ baseURL, locale }: { baseURL?: string; locale?: string } = {}) {
this.locale = locale;
this.BASE_URL = baseURL;
}

get voiceOptions() {
return getEdgeVoiceOptions(this.locale);
}

static localeOptions = getVoiceLocaleOptions();
static voiceList = edgeVoiceList;
static voiceName = voiceName;
static completion = createEdgeSpeech;

private fetch = async (payload: EdgeSpeechPayload) => {
const response = await (this.BASE_URL
? fetch(this.BASE_URL, { body: JSON.stringify(payload), method: 'POST' })
: createEdgeSpeech({ payload }));

if (!response.ok) {
throw new Error('Network response was not ok');
}
return response;
};

create = async (payload: EdgeSpeechPayload): Promise<AudioBuffer> => {
const response = await this.fetch(payload);

const arrayBuffer = await response.arrayBuffer();

return arrayBufferConvert(arrayBuffer);
};
}
Loading

0 comments on commit 0a47f03

Please sign in to comment.