generated from electron-react-boilerplate/electron-react-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from solidSpoon/whisper-issue
优化生成字幕的逻辑
- Loading branch information
Showing
17 changed files
with
466 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ExtendableError } from 'ts-error'; | ||
|
||
/** | ||
* Whisper 相应格式错误 | ||
*/ | ||
export class WhisperResponseFormatError extends ExtendableError { | ||
} | ||
|
||
/** | ||
* 任务被用户取消 | ||
*/ | ||
export class CancelByUserError extends ExtendableError { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { z } from 'zod'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
/** | ||
* 配置文件托管类 | ||
* @template T 配置类型 | ||
* @template S Zod Schema 类型 | ||
*/ | ||
export class ConfigTender<T, S extends z.ZodType<T>> { | ||
private readonly configPath: string; | ||
private readonly schema: S; | ||
|
||
constructor(configPath: string, schema: S, defaultValue?: T) { | ||
this.configPath = configPath; | ||
this.schema = schema; | ||
|
||
// 确保目录存在 | ||
const dir = path.dirname(configPath); | ||
if (!fs.existsSync(dir)) { | ||
fs.mkdirSync(dir, { recursive: true }); | ||
} | ||
|
||
// 如果文件不存在且提供了默认值,则创建文件 | ||
if (!fs.existsSync(configPath) && defaultValue) { | ||
this.save(defaultValue); | ||
} | ||
} | ||
|
||
/** | ||
* 读取整个配置 | ||
*/ | ||
get(): T { | ||
try { | ||
const content = fs.readFileSync(this.configPath, 'utf-8'); | ||
const parsed = JSON.parse(content); | ||
return this.schema.parse(parsed); | ||
} catch (error) { | ||
throw new Error(`Failed to read config: ${error}`); | ||
} | ||
} | ||
|
||
/** | ||
* 保存整个配置 | ||
*/ | ||
save(config: T): void { | ||
try { | ||
const validated = this.schema.parse(config); | ||
fs.writeFileSync(this.configPath, JSON.stringify(validated, null, 2)); | ||
} catch (error) { | ||
throw new Error(`Failed to save config: ${error}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.