diff --git a/package.json b/package.json index 36c219d..48c9ddc 100644 --- a/package.json +++ b/package.json @@ -15,12 +15,13 @@ "build": "rollup --config rollup.config.ts --configPlugin typescript" }, "devDependencies": { - "typescript": "5.5.4", + "@bob-translate/types": "1.0.1", "@rollup/plugin-terser": "0.4.4", "@rollup/plugin-typescript": "11.1.6", "rollup": "4.20.0", "rollup-plugin-copy": "3.5.0", - "tslib": "2.6.3" + "tslib": "2.6.3", + "typescript": "5.5.4" }, "engines": { "node": ">=20" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f2bba1b..64c5ce1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,9 @@ importers: .: devDependencies: + '@bob-translate/types': + specifier: 1.0.1 + version: 1.0.1 '@rollup/plugin-terser': specifier: 0.4.4 version: 0.4.4(rollup@4.20.0) @@ -29,6 +32,9 @@ importers: packages: + '@bob-translate/types@1.0.1': + resolution: {integrity: sha512-QwaLIZ1bbuGCo66IzzN5HOQxEQloq2P9ZQIezaSA3KvA7UePyK1X49TSCM9VJTKNt/haE2ciYwgN9QquXz8YNA==} + '@jridgewell/gen-mapping@0.3.5': resolution: {integrity: sha512-IzL8ZoEDIBRWEzlCcRhOaCupYyN5gdIK+Q6fbFdPDg6HqX6jpkItn7DFIpW9LQzXG6Df9sA7+OKnq0qlz/GaQg==} engines: {node: '>=6.0.0'} @@ -413,6 +419,8 @@ packages: snapshots: + '@bob-translate/types@1.0.1': {} + '@jridgewell/gen-mapping@0.3.5': dependencies: '@jridgewell/set-array': 1.2.1 diff --git a/src/const.ts b/src/const.ts index 2bf7767..42e030f 100644 --- a/src/const.ts +++ b/src/const.ts @@ -42,6 +42,4 @@ export const HTTP_ERROR_CODES = { 511: "Network Authentication Required" } as const; -export type HttpErrorCode = keyof typeof HTTP_ERROR_CODES; - export const SYSTEM_PROMPT = "You are a translation engine that can only translate text and cannot interpret it." as const; \ No newline at end of file diff --git a/src/global.d.ts b/src/global.d.ts deleted file mode 100644 index 8c783b3..0000000 --- a/src/global.d.ts +++ /dev/null @@ -1,51 +0,0 @@ -/** - * @description Upstream update time: 10/13/23, 3:27 PM - * @remarks Bob 1.6.0+ 可用 - */ -declare const $env: import('./types/env.type').Env; - -/** - * @description Upstream update time: 5/22/22, 11:45 PM - */ -declare const $info: import('./types/info.type').Info; - -/** - * @description Upstream update time: 10/13/23, 3:27 PM - */ -declare const $option: import('./types/option.type').Option; - -/** - * @description Upstream update time: 5/22/22, 11:45 PM - */ -declare const $log: import('./types/log.type').Log; - -/** - * @description Upstream update time: 10/13/23, 3:27 PM - */ -declare const $http: import('./types/http.type').Http; - -/** - * @description Upstream update time: 10/13/23, 3:27 PM - * @remarks Bob 1.6.0+ 可用 - */ -declare const $websocket: import('./types/websocket.type').WebSocketConstructor; - -/** - * @description Upstream update time: 10/13/23, 3:27 PM - */ -declare const $file: import('./types/file.type').FileConstructor; - -/** - * @description Upstream update time: 3/14/23, 11:59 AM - */ -declare const $data: import('./types/data.type').DataConstructor; - -/** - * @description Upstream update time: 10/13/23, 3:27 PM - */ -declare const $timer: import('./types/timer.type').Timer; - -/** - * @description Upstream update time: 10/13/23, 3:27 PM - */ -declare const $signal: import('./types/signal.type').SignalConstructor; diff --git a/src/lang.ts b/src/lang.ts index 2058a21..7eb5784 100644 --- a/src/lang.ts +++ b/src/lang.ts @@ -116,6 +116,4 @@ export const supportLanguageList = [ ["zu", "zu"], ] as const; -export type Language = typeof supportLanguageList[number][0]; - export const langMap = new Map(supportLanguageList.map(([key, value]) => [key, value])); diff --git a/src/main.ts b/src/main.ts index 8686191..4d8b504 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,10 +1,13 @@ import { SYSTEM_PROMPT } from "./const"; import { langMap, supportLanguageList } from "./lang"; -import { ChatCompletion, ModelList } from "./types"; -import { HttpResponse } from "./types/http.type"; -import type { PluginValidate } from "./types/plugin-validate.type"; -import type { ServiceError } from "./types/service-error.type"; -import type { TextTranslate, TextTranslateQuery } from "./types/text-translate.type"; +import type { ChatCompletion, ModelList } from "./types"; +import type { + HttpResponse, + PluginValidate, + ServiceError, + TextTranslate, + TextTranslateQuery +} from "@bob-translate/types"; import { buildHeader, ensureHttpsAndNoTrailingSlash, diff --git a/src/types/data.type.ts b/src/types/data.type.ts deleted file mode 100644 index 4eb61f4..0000000 --- a/src/types/data.type.ts +++ /dev/null @@ -1,29 +0,0 @@ -export interface Data { - readonly length: number; - - toUTF8(): string | undefined; - toHex(useUpper?: boolean): string; - toBase64(): string; - toByteArray(): number[]; - - readUInt8(index: number): number; - writeUInt8(value: number, index: number): void; - - subData(start: number, end: number): Data; - /** - * - * @remarks 这个方法拼接一个 $data 对象的数据到当前对象的末尾,会改变当前对象 - * - */ - appendData(data: Data): void; -} - -export interface DataConstructor { - fromUTF8(text: string): Data; - fromHex(hex: string): Data; - fromBase64(base64: string): Data; - fromByteArray(array: number[]): Data; - fromData(data: Data): Data; - isData(object: any): object is Data; -} - diff --git a/src/types/env.type.ts b/src/types/env.type.ts deleted file mode 100644 index a7ce6b1..0000000 --- a/src/types/env.type.ts +++ /dev/null @@ -1,11 +0,0 @@ -/** - * - * @remarks Bob 1.6.0+ 可用 - * - */ -export interface Env { - appVersion: string; // e.g., "1.6.0" - appBuild: string; // e.g., "103" - macOSVersion: string; // e.g., "13.1.0" - macModel: string; // e.g., "MacBookPro18,4" -} diff --git a/src/types/file.type.ts b/src/types/file.type.ts deleted file mode 100644 index 5126535..0000000 --- a/src/types/file.type.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { Data } from "./data.type"; - -interface FileWriteOptions { - data: Data; // $data 类型 - path: string; // 目标路径 -} - -interface FileCopyMoveOptions { - src: string; // 源路径 - dst: string; // 目标路径 -} - -export interface FileConstructor { - read(path: string): any; // 返回 $data 类型 - write(options: FileWriteOptions): boolean; - delete(path: string): boolean; - list(path: string): string[]; - copy(options: FileCopyMoveOptions): boolean; - move(options: FileCopyMoveOptions): boolean; - mkdir(path: string): boolean; - exists(path: string): boolean; - isDirectory(path: string): boolean; -} diff --git a/src/types/http.type.ts b/src/types/http.type.ts deleted file mode 100644 index a6c4cab..0000000 --- a/src/types/http.type.ts +++ /dev/null @@ -1,87 +0,0 @@ -import { HttpErrorCode } from "../const"; -import { Data } from "./data.type"; -import { Signal } from "./signal.type"; - -type HttpMethod = - | 'GET' - | 'DELETE' - | 'HEAD' - | 'OPTIONS' - | 'POST' - | 'PUT'; - -interface HttpRequestFiles { - data: Data; // Binary data - name: string; // Name in the upload form - filename: string; // Filename after upload - contentType: string; // File format -} - -interface HttpRequestConfig { - method: HttpMethod; - url: string; - header?: Record; - body?: Record | Data; - files?: HttpRequestFiles[]; - timeout?: number; // Timeout in milliseconds - cancelSignal?: Signal; - handler?: (response: HttpResponse) => void; -} - -interface HttpStream { - text?: string; - rawData: Data; -} - -interface HttpStreamRequestConfig extends HttpRequestConfig { - streamHandler?: (stream: HttpStream) => void; -} - -/** - * @deprecated Bob 1.8.0 之前的结构 - */ -interface LegacyHttpResponseError { - domain: string; - code: number; - userInfo: any; - localizedDescription: string; - localizedFailureReason: string; - localizedRecoverySuggestion: string; -} - -interface HttpResponseError { - message: string; - debugMessage: string; -} - -interface HttpResponseInfo { - url: string; // url - MIMEType: string; // MIME 类型 - expectedContentLength: number; // 长度 - textEncodingName: string; // 编码 - suggestedFilename: string; // 建议的文件名 - statusCode: HttpErrorCode; // HTTP 状态码 - headers: Record; // HTTP header -} - -export interface HttpResponse> { - data: T | string | Data; - rawData: Data; - response: HttpResponseInfo; - error?: HttpResponseError | LegacyHttpResponseError; -} - -type HttpResponsePromise = Promise>; - -export interface Http { - request>(config: HttpRequestConfig): Promise; - /** - * @deprecated The method should not be used - */ - get>(config: HttpRequestConfig): Promise; - /** - * @deprecated The method should not be used - */ - post>(config: HttpRequestConfig): Promise; - streamRequest>(config: HttpStreamRequestConfig): Promise; -} diff --git a/src/types/info.type.ts b/src/types/info.type.ts deleted file mode 100644 index 234dd43..0000000 --- a/src/types/info.type.ts +++ /dev/null @@ -1,46 +0,0 @@ -interface IconObject { - identifier: string; - type: 'builtIn' | 'file'; - builtInId?: string; - file?: string; -} - -interface TextConfigObject { - type: 'secure' | 'visible'; - height?: number; - placeholderText?: string; - keyWords?: string[]; -} - -interface MenuObject { - title: string; - value: string; - defaultPluginIconIdentifier: string; - defaultPluginName: string; -} - -interface OptionObject { - identifier: string; - type: 'text' | 'menu'; - title: string; - defaultValue?: string; - textConfig?: TextConfigObject; - menuValues?: MenuObject[]; - desc?: string; - isKeyOption?: boolean; -} - -export interface Info { - identifier: string; - version: string; - category: string; - name: string; - summary?: string; - icon?: string; - icons?: IconObject[]; - author?: string; - homepage?: string; - appcast?: string; - minBobVersion?: string; - options?: Record[]; -} \ No newline at end of file diff --git a/src/types/log.type.ts b/src/types/log.type.ts deleted file mode 100644 index 3d5cdc0..0000000 --- a/src/types/log.type.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface Log { - info(object: any): void; - error(object: any): void; -} diff --git a/src/types/ocr.type.ts b/src/types/ocr.type.ts deleted file mode 100644 index 259b0af..0000000 --- a/src/types/ocr.type.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { Language } from "../lang"; -import { Data } from "./data.type"; -import { ServiceError } from "./service-error.type"; - -interface OcrText { - text: string; -} - -export interface OcrQuery { - image: Data; // 需要识别的图片数据 - from: Language; // 目前用户选中的源语言,可能是 auto。 - detectFrom: Exclude; // 图片中最可能的语言 -} - -export interface OcrResult { - from?: Language; // 图片中的文字的主要语种,可与查询参数中传入的 from 不一致,可不传。 - texts: Array; // 文本识别结果数组,按照段落分割,见 ocr text,必传。 - raw?: any; // 如果插件内部调用了某文本识别接口,可将接口原始数据传回,方便定位问题,可不传。 -} - -type OcrCompletion = (args: OcrResult | ServiceError) => void; - -export type Ocr = (query: OcrQuery, completion: OcrCompletion) => void; \ No newline at end of file diff --git a/src/types/options.type.ts b/src/types/options.type.ts deleted file mode 100644 index c0a5c08..0000000 --- a/src/types/options.type.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface Options { - token: string; - option: string; -} \ No newline at end of file diff --git a/src/types/plugin-validate.type.ts b/src/types/plugin-validate.type.ts deleted file mode 100644 index f17c05e..0000000 --- a/src/types/plugin-validate.type.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { ServiceError } from "./service-error.type"; - -interface ValidationResult { - result: boolean; - error?: ServiceError; -} - -export type ValidationCompletion = (result: ValidationResult) => void; - -export type PluginValidate = (completion: ValidationCompletion) => void; \ No newline at end of file diff --git a/src/types/service-error.type.ts b/src/types/service-error.type.ts deleted file mode 100644 index 7d0a1df..0000000 --- a/src/types/service-error.type.ts +++ /dev/null @@ -1,16 +0,0 @@ -// https://bobtranslate.com/plugin/object/serviceerror.html#service-error -type ServiceErrorType = - | 'unknown' - | 'param' - | 'unsupportedLanguage' - | 'secretKey' - | 'network' - | 'api' - | 'notFound'; - -export interface ServiceError { - type: ServiceErrorType; // 错误类型 - message: string; // 错误描述,用于展示给用户看 - addition?: string; // 附加信息,可以是任何可 json 序列化的数据类型,用于 debug - troubleshootingLink?: string; // 故障排除的链接,目前主要用在插件验证的功能 -} \ No newline at end of file diff --git a/src/types/signal.type.ts b/src/types/signal.type.ts deleted file mode 100644 index 574ea69..0000000 --- a/src/types/signal.type.ts +++ /dev/null @@ -1,13 +0,0 @@ -interface Disposable { - dispose(): void; -} - -export interface Signal { - send(data?: any): void; - subscribe(callback: (data: any) => void): Disposable; - removeAllSubscriber(): void; -} - -export interface SignalConstructor { - new(): Signal; -} diff --git a/src/types/text-translate.type.ts b/src/types/text-translate.type.ts deleted file mode 100644 index 9591802..0000000 --- a/src/types/text-translate.type.ts +++ /dev/null @@ -1,99 +0,0 @@ -import { Language } from "../lang"; -import { ServiceError } from "./service-error.type"; -import { Signal } from "./signal.type"; -import { TtsResult } from "./tts.type"; - -interface PhoneticObject { - type: 'us' | 'uk'; // 音标类型,值可以是 us 或 uk,分别对应美式音标和英式音标。 - value?: string; // 音标字符串。例如 ɡʊd。 - tts?: TtsResult; // result音标发音数据。 -} - -interface PartObject { - part: string; // 单词词性,例如 n.、vi. 等等。 - means: string[]; // 词义 string 数组。 -} - -interface ExchangeObject { - name: string; // 形式的名字,例如比较级、最高级等等。 - words: string[]; // 该形式对于的单词 string 数组,一般只有一个。 -} - -interface RelatedWordObject { - word: string; // 单词本身。 - means?: string[]; // 词义 string 数组。 -} - -interface RelatedWordPartObject { - part?: string; // 词性。 - words: Array; // 相关的单词数组,见 related word object。 -} - -interface AdditionObject { - name: string; // 附加内容名称。 - value: string; // 附加内容。 -} - -interface ToDictObject { - /** - * @remarks Bob 0.6.0+ 可用 - */ - word: string; // 单词/词组,一般英文查词会有。 - phonetics: Array; // 音标数据数组,一般英文查词会有,见 phonetic object。 - parts: Array; // 词性词义数组,一般英文查词会有,见 part object。 - exchanges?: Array; // 其他形式数组,一般英文查词会有,见 exchange object。 - relatedWordParts?: Array; // 相关的单词数组,一般中文查词会有,表示和该中文对应的英文单词有哪些,见 related word part object。 - additions?: Array; // 附加内容数组,考虑到以上字段无法覆盖所有词典内容,比如例句、记忆技巧等,可将相应数据添加到该数组,最终也会显示到翻译结果中,见 additions object。 -} - -export interface TextTranslateResult { - /** - * @deprecated The field should not be used - */ - fromParagraphs?: string[]; // 原文分段拆分过后的 string 数组,可不传。 - toParagraphs: string[]; // 译文分段拆分过后的 string 数组。 - toDict?: ToDictObject; // 词典结果,见 to dict object。 - from: Language; // 由翻译接口提供的源语种,可以与查询时的 from 不同。 - to: Language; // 由翻译接口提供的目标语种,可以与查询时的 to 不同。 - fromTTS?: TtsResult; // result原文的语音合成数据。 - toTTS?: TtsResult; // result译文的语音合成数据。 - - raw?: any; // 如果插件内部调用了某翻译接口,可将接口原始数据传回,方便定位问题。 -} - -interface TextTranslateCompletionResult { - result: TextTranslateResult | TtsResult; -}; - -interface TextTranslateCompletionError { - error: ServiceError -}; - -type HandleStream = (args: TextTranslateCompletionResult) => void; -type HandleCompletion = (args: TextTranslateCompletionResult | TextTranslateCompletionError) => void; - -export interface TextTranslateQuery { - text: string; // 需要翻译的文本 - from: Language; // 目前用户选中的源语言,可能是 auto - to: Language; // 用户选中的目标语种标准码 - detectFrom: Exclude; // 检测过后的源语种 - detectTo: Exclude; // 检测过后的目标语种 - /** - * @remarks Bob 1.8.0+ 可用 - */ - cancelSignal: Signal, - /** - * @remarks Bob 1.8.0+ 可用 - */ - onStream: HandleStream, - /** - * @remarks Bob 1.8.0+ 可用 - */ - onCompletion: HandleCompletion; // 用于回调翻译结果的函数 -} - -/** - * @param {TextTranslateQuery} query - 用于传入需要翻译的文本信息 - * @param {HandleCompletion} completion - Bob 1.8.0 之前可以调用本函数回调翻译结果,Bob 1.8.0+ 不再推荐使用 - */ -export type TextTranslate = (query: TextTranslateQuery, completion: HandleCompletion) => void; diff --git a/src/types/timer.type.ts b/src/types/timer.type.ts deleted file mode 100644 index 084a226..0000000 --- a/src/types/timer.type.ts +++ /dev/null @@ -1,11 +0,0 @@ - -interface TimerScheduleOptions { - interval: number; - repeats: boolean; - handler: () => void; -} - -export interface Timer { - schedule(options: TimerScheduleOptions): number; - invalidate(timerId: number): void; -} \ No newline at end of file diff --git a/src/types/tts.type.ts b/src/types/tts.type.ts deleted file mode 100644 index 22b4d11..0000000 --- a/src/types/tts.type.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { Language } from "../lang"; -import { ServiceError } from "./service-error.type"; - -export interface TtsQuery { - text: string; // 需要合成的文本 - lang: Exclude; // 当前文本的语言,一定不是 auto。 -} - -export interface TtsResult { - type: 'url' | 'base64'; // 数据类型,必传。 - value: string; // 值,必传。 - raw?: any; // 如果插件内部调用了某语音合成接口,可将接口原始数据传回,方便定位问题,可不传。 -} - -type TtsCompletion = (args: TtsResult | ServiceError) => void; - -export type Tts = (query: TtsQuery, completion: TtsCompletion) => void; \ No newline at end of file diff --git a/src/types/websocket.type.ts b/src/types/websocket.type.ts deleted file mode 100644 index e6768b5..0000000 --- a/src/types/websocket.type.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { Data } from "./data.type"; - -interface WebSocketConfig { - url: string; - allowSelfSignedSSLCertificates?: boolean; - timeoutInterval?: number; // 默认 60 秒 - header?: { [key: string]: string }; -} - -interface CloseConfig { - code?: number; // 参考 https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4 -} - -interface WebSocket { - readonly readyState: number; - open(): void; - close(config?: CloseConfig): void; - sendString(message: string): void; - sendData(data: Data): void; - ping(data?: Data): void; - pong(data?: Data): void; - listenOpen(callback: (socket: WebSocket) => void): void; - listenClose(callback: (socket: WebSocket, code: number, reason: string) => void): void; - listenError(callback: (socket: WebSocket, error: { code: number, message: string, type: string }) => void): void; - listenReceiveString(callback: (socket: WebSocket, message: string) => void): void; - listenReceiveData(callback: (socket: WebSocket, data: Data) => void): void; - listenReceivePing(callback: (socket: WebSocket, data: Data) => void): void; - listenReceivePong(callback: (socket: WebSocket, data: Data) => void): void; -} - -export interface WebSocketConstructor { - new(config: WebSocketConfig): WebSocket; -} \ No newline at end of file diff --git a/src/utils.ts b/src/utils.ts index eaac8bb..f1020c9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,8 +1,10 @@ +import type { + HttpResponse, + ServiceError, + TextTranslateQuery, + ValidationCompletion +} from "@bob-translate/types"; import { HTTP_ERROR_CODES } from "./const"; -import { HttpResponse } from "./types/http.type"; -import type { ValidationCompletion } from "./types/plugin-validate.type"; -import type { ServiceError } from "./types/service-error.type"; -import type { TextTranslateQuery } from "./types/text-translate.type"; function buildHeader(isAzureServiceProvider: boolean, apiKey: string): { diff --git a/tsconfig.json b/tsconfig.json index 024e683..a50d118 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,6 +12,9 @@ "strict": true, "target": "ES5", "useDefineForClassFields": false, + "types": [ + "@bob-translate/types" + ] }, "exclude": [ "dist",