Skip to content

Commit 4714c50

Browse files
committed
feat: code refactoring, major updates
1. Support gpt-4 models 2. Support Azure OpenAI service 3. Support custom user prompt
1 parent 0f3d38a commit 4714c50

File tree

4 files changed

+670
-92
lines changed

4 files changed

+670
-92
lines changed

global.d.ts

+397
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,397 @@
1+
declare namespace Bob {
2+
// https://ripperhe.gitee.io/bob/#/plugin/addtion/language
3+
enum LanguagesEnum {
4+
'auto' = '自动',
5+
'zh-Hans' = '中文简体',
6+
'zh-Hant' = '中文繁体',
7+
'yue' = '粤语',
8+
'wyw' = '文言文',
9+
'pysx' = '拼音缩写',
10+
'en' = '英语',
11+
'ja' = '日语',
12+
'ko' = '韩语',
13+
'fr' = '法语',
14+
'de' = '德语',
15+
'es' = '西班牙语',
16+
'it' = '意大利语',
17+
'ru' = '俄语',
18+
'pt' = '葡萄牙语',
19+
'nl' = '荷兰语',
20+
'pl' = '波兰语',
21+
'ar' = '阿拉伯语',
22+
'af' = '南非语',
23+
'am' = '阿姆哈拉语',
24+
'az' = '阿塞拜疆语',
25+
'be' = '白俄罗斯语',
26+
'bg' = '保加利亚语',
27+
'bn' = '孟加拉语',
28+
'bo' = '藏语',
29+
'bs' = '波斯尼亚语',
30+
'ca' = '加泰隆语',
31+
'ceb' = '宿务语',
32+
'chr' = '切罗基语',
33+
'co' = '科西嘉语',
34+
'cs' = '捷克语',
35+
'cy' = '威尔士语',
36+
'da' = '丹麦语',
37+
'el' = '希腊语',
38+
'eo' = '世界语',
39+
'et' = '爱沙尼亚语',
40+
'eu' = '巴斯克语',
41+
'fa' = '波斯语',
42+
'fi' = '芬兰语',
43+
'fj' = '斐济语',
44+
'fy' = '弗里西语',
45+
'ga' = '爱尔兰语',
46+
'gd' = '苏格兰盖尔语',
47+
'gl' = '加利西亚语',
48+
'gu' = '古吉拉特语',
49+
'ha' = '豪萨语',
50+
'haw' = '夏威夷语',
51+
'he' = '希伯来语',
52+
'hi' = '印地语',
53+
'hmn' = '苗语',
54+
'hr' = '克罗地亚语',
55+
'ht' = '海地克里奥尔语',
56+
'hu' = '匈牙利语',
57+
'hy' = '亚美尼亚语',
58+
'id' = '印尼语',
59+
'ig' = '伊博语',
60+
'is' = '冰岛语',
61+
'jw' = '爪哇语',
62+
'ka' = '格鲁吉亚语',
63+
'kk' = '哈萨克语',
64+
'km' = '高棉语',
65+
'kn' = '卡纳达语',
66+
'ku' = '库尔德语',
67+
'ky' = '柯尔克孜语',
68+
'la' = '老挝语',
69+
'lb' = '卢森堡语',
70+
'lo' = '老挝语',
71+
'lt' = '立陶宛语',
72+
'lv' = '拉脱维亚语',
73+
'mg' = '马尔加什语',
74+
'mi' = '毛利语',
75+
'mk' = '马其顿语',
76+
'ml' = '马拉雅拉姆语',
77+
'mn' = '蒙古语',
78+
'mr' = '马拉地语',
79+
'ms' = '马来语',
80+
'mt' = '马耳他语',
81+
'mww' = '白苗语',
82+
'my' = '缅甸语',
83+
'ne' = '尼泊尔语',
84+
'no' = '挪威语',
85+
'ny' = '齐切瓦语',
86+
'or' = '奥里亚语',
87+
'otq' = '克雷塔罗奥托米语',
88+
'pa' = '旁遮普语',
89+
'ps' = '普什图语',
90+
'ro' = '罗马尼亚语',
91+
'rw' = '卢旺达语',
92+
'sd' = '信德语',
93+
'si' = '僧伽罗语',
94+
'sk' = '斯洛伐克语',
95+
'sl' = '斯洛文尼亚语',
96+
'sm' = '萨摩亚语',
97+
'sn' = '修纳语',
98+
'so' = '索马里语',
99+
'sq' = '阿尔巴尼亚语',
100+
'sr' = '塞尔维亚语',
101+
'sr-Cyrl' = '塞尔维亚语-西里尔文',
102+
'sr-Latn' = '塞尔维亚语-拉丁文',
103+
'st' = '塞索托语',
104+
'su' = '巽他语',
105+
'sv' = '瑞典语',
106+
'sw' = '斯瓦希里语',
107+
'ta' = '泰米尔语',
108+
'te' = '泰卢固语',
109+
'tg' = '塔吉克语',
110+
'th' = '泰语',
111+
'tk' = '土库曼语',
112+
'tl' = '菲律宾语',
113+
'tlh' = '克林贡语',
114+
'to' = '汤加语',
115+
'tr' = '土耳其语',
116+
'tt' = '鞑靼语',
117+
'ty' = '塔希提语',
118+
'ug' = '维吾尔语',
119+
'uk' = '乌克兰语',
120+
'ur' = '乌尔都语',
121+
'uz' = '乌兹别克语',
122+
'vi' = '越南语',
123+
'xh' = '科萨语',
124+
'yi' = '意第绪语',
125+
'yo' = '约鲁巴语',
126+
'yua' = '尤卡坦玛雅语',
127+
'zu' = '祖鲁语',
128+
}
129+
type Languages = Array<keyof typeof LanguagesEnum>;
130+
type supportLanguages = Languages;
131+
type Language = keyof typeof LanguagesEnum;
132+
133+
// https://ripperhe.gitee.io/bob/#/plugin/quickstart/translate
134+
type Translate = (query: TranslateQuery, completion: Completion) => void;
135+
type completionResult = { result: Result };
136+
type CompletionResult = { error: ServiceError };
137+
type Completion = (args: completionResult | CompletionResult) => void;
138+
interface TranslateQuery {
139+
text: string; // 需要翻译的文本
140+
from: Language; // 用户选中的源语种标准码
141+
to: Language; // 用户选中的目标语种标准码
142+
detectFrom: Exclude<Language, 'auto'>; // 检测过后的源语种
143+
detectTo: Exclude<Language, 'auto'>; // 检测过后的目标语种
144+
}
145+
interface OcrQuery {
146+
from: Language; // 目前用户选中的源语言
147+
image: Data; // 需要识别的图片数据
148+
detectFrom: Exclude<Language, 'auto'>; // 图片中最可能的语言,如果插件不具备检测语种的能力,可直接使用该属性。
149+
}
150+
interface TTSQuery {
151+
text: string; // 需要合成的文本
152+
lang: Exclude<Language, 'auto'>; // 当前文本的语种。
153+
}
154+
type Query = TranslateQuery | OcrQuery | TTSQuery;
155+
156+
// https://ripperhe.gitee.io/bob/#/plugin/quickstart/info
157+
interface Info {
158+
identifier: string; // 插件的唯一标识符,必须由数字、小写字母和 . 组成。
159+
category: 'translate' | 'ocr' | 'tts'; // 插件类别,分别对应文本翻译、文本识别和语音合成。
160+
version: string; // 插件版本号,必须由数字、小写字母和 . 组成。
161+
name: string; // 插件名称,无限制,建议别太长。
162+
summary?: string; // 插件描述信息。
163+
icon?: string; // 插件图标标识符,如果插件根目录有 icon.png 文件,则会将其作为插件图标,不会读取该字段;如果没有,会读取该字段,值可以为 这个图标列表 中所包含的任意一个ID。
164+
author?: string; // 插件作者。
165+
homepage?: string; // 插件主页网址。
166+
appcast?: string; // 插件发布信息 URL。
167+
minBobVersion?: string; // 最低支持本插件的 Bob 版本,建议填写您开发插件时候的调试插件的 Bob 版本,目前应该是 0.5.0。
168+
options?: OptionObject[];
169+
}
170+
interface MenuObject {
171+
title: string; // 菜单选项名称,用于展示。
172+
value: string; // 当前菜单被选中时的值。
173+
}
174+
interface OptionObject {
175+
identifier: string; // 选项唯一标识符,取值时使用。
176+
type: 'text' | 'menu'; // 选项类型,分别对应输入框和菜单。
177+
title: string; // 选项名称,用于展示。
178+
defaultValue?: string; // 默认值。
179+
menuValues?: MenuObject[]; // type 为 menu 时必须有菜单选项数组,详情见 menu object。
180+
}
181+
182+
// https://ripperhe.gitee.io/bob/#/plugin/quickstart/publish
183+
interface Appcast {
184+
identifier: string; // 插件的唯一标识符,需和插件 info.json 文件中的唯一标识符一致。
185+
versions: Array<VersionObject>; // 版本信息数组,请倒序排列,新版本放前面。具体结构看 version object。
186+
}
187+
interface VersionObject {
188+
version: string; // 版本号,请与对应插件包 info.json 中的信息一致。
189+
desc: string; // 插件的更新内容。
190+
sha256: string; // 插件包 SHA256 哈希值,会和从 url 中下载的插件包进行校验。
191+
url: string; // 插件包下载地址。
192+
minBobVersion?: string; // 最低支持本插件的 Bob 版本,请与对应插件包 info.json 中的信息一致。
193+
}
194+
195+
// https://ripperhe.gitee.io/bob/#/plugin/api/option
196+
type Option = {
197+
apiKeys: string;
198+
apiUrl: string;
199+
customSystemPrompt: string;
200+
customUserPrompt: string;
201+
deploymentName: string;
202+
model: string;
203+
polishingMode: "simplicity" | "detailed";
204+
};
205+
206+
// https://ripperhe.gitee.io/bob/#/plugin/api/log
207+
interface Log {
208+
info: (msg: string) => void; // 用于打印一些常规的信息
209+
error: (msg: string) => void; // 用于打印错误信息
210+
}
211+
212+
// https://ripperhe.gitee.io/bob/#/plugin/api/http
213+
interface Http {
214+
request<T = any, R = HttpResponsePromise<T>>(config: HttpRequestConfig): Promise<R>;
215+
get<T = any, R = HttpResponsePromise<T>>(config: HttpRequestConfig): Promise<R>;
216+
post<T = any, R = HttpResponsePromise<T>>(config: HttpRequestConfig): Promise<R>;
217+
}
218+
type HttpMethod =
219+
| 'get'
220+
| 'GET'
221+
| 'delete'
222+
| 'DELETE'
223+
| 'head'
224+
| 'HEAD'
225+
| 'options'
226+
| 'OPTIONS'
227+
| 'post'
228+
| 'POST'
229+
| 'put'
230+
| 'PUT';
231+
232+
interface HttpRequestConfig {
233+
url: string;
234+
method?: HttpMethod;
235+
header?: any;
236+
params?: any;
237+
body?: any;
238+
files?: HttpRequestFiles;
239+
handler?: (resp: HttpResponse) => void;
240+
timeout?: number;
241+
}
242+
interface HttpRequestFiles {
243+
data: DataObject; // 二进制数据
244+
name: string; // 上传表单中的名称
245+
filename: string; // 上传之后的文件名
246+
'content-type': string; // 文件格式
247+
}
248+
interface HttpResponse<T = any> {
249+
data: T; // object / string / $data 解析过后的数据
250+
rawData: DataObject;
251+
response: HttpResponseInfo; // 请求响应信息
252+
error: HttpResponseError;
253+
}
254+
interface HttpResponseInfo {
255+
url: string; // url
256+
MIMEType: string; // MIME 类型
257+
expectedContentLength: number; // 长度
258+
textEncodingName: string; // 编码
259+
suggestedFilename: string; // 建议的文件名
260+
statusCode: number; // HTTP 状态码
261+
headers: any; // HTTP header
262+
}
263+
interface HttpResponseError {
264+
domain: string;
265+
code: number;
266+
userInfo: any;
267+
localizedDescription: string; // 描述
268+
localizedFailureReason: string; // 原因
269+
localizedRecoverySuggestion: string; // 建议
270+
}
271+
type HttpResponsePromise<T = any> = Promise<HttpResponse<T>>;
272+
273+
// https://ripperhe.gitee.io/bob/#/plugin/api/file
274+
interface File {
275+
read(path: string): DataObject;
276+
write(object: { data: DataObject; path: string }): boolean;
277+
delete(path: string): boolean;
278+
list(path: string): string[];
279+
copy(object: { src: string; dst: string }): boolean;
280+
move(object: { src: string; dst: string }): boolean;
281+
mkdir(path: string): boolean;
282+
exists(path: string): boolean;
283+
isDirectory(path: string): boolean;
284+
}
285+
286+
// https://ripperhe.gitee.io/bob/#/plugin/api/data
287+
interface Data {
288+
fromUTF8: (data: string) => DataObject;
289+
fromHex: (data: string) => DataObject;
290+
fromBase64: (data: string) => DataObject;
291+
fromByteArray(data: number[]): DataObject;
292+
fromData: (data: DataObject) => DataObject;
293+
isData: (data: any) => boolean;
294+
}
295+
interface DataObject {
296+
length: number;
297+
toUTF8(): string | undefined;
298+
toHex(useUpper?: boolean): string;
299+
toBase64(): string;
300+
toByteArray(): number[];
301+
readUInt8(index: number): number;
302+
writeUInt8(value: number, index: number): void;
303+
subData(start: number, end: number): DataObject;
304+
appendData(data: this): this;
305+
}
306+
307+
// https://ripperhe.gitee.io/bob/#/plugin/object/serviceerror
308+
interface ServiceError {
309+
type: ServiceErrorType; // 错误类型
310+
message: string; // 错误描述,用于展示给用户看
311+
addtion: string; // 附加信息,可以是任何可 json 序列化的数据类型,用于 debug
312+
}
313+
enum ServiceErrorEnum {
314+
unknown = '未知错误',
315+
param = '参数错误',
316+
unsupportLanguage = '不支持的语种',
317+
secretKey = '缺少秘钥',
318+
network = '网络异常,网络请失败',
319+
api = '服务接口异常',
320+
}
321+
type ServiceErrorType = keyof typeof ServiceErrorEnum;
322+
323+
// https://ripperhe.gitee.io/bob/#/plugin/object/translateresult
324+
interface TranslateResult {
325+
from: Language; // 由翻译接口提供的源语种,可以与查询时的 from 不同。
326+
to: Language; // 由翻译接口提供的目标语种,可以与查询时的 to 不同。
327+
toParagraphs: string[]; // 译文分段拆分过后的 string 数组。
328+
fromParagraphs?: string[]; // 原文分段拆分过后的 string 数组。
329+
toDict?: ToDictObject; // 词典结果,见 to dict object。
330+
fromTTS?: TTSResult; // result原文的语音合成数据。
331+
toTTS?: TTSResult; // result译文的语音合成数据。
332+
raw?: any; // 如果插件内部调用了某翻译接口,可将接口原始数据传回,方便定位问题。
333+
}
334+
interface ToDictObject {
335+
phonetics: Array<PhoneticObject>; // 音标数据数组,一般英文查词会有,见 phonetic object。
336+
parts: Array<PartObject>; // 词性词义数组,一般英文查词会有,见 part object。
337+
exchanges?: Array<ExchangeObject>; // 其他形式数组,一般英文查词会有,见 exchange object。
338+
relatedWordParts?: Array<RelatedWordPartObject>; // 相关的单词数组,一般中文查词会有,表示和该中文对应的英文单词有哪些,见 related word part object。
339+
addtions?: Array<AddtionObject>; // 附加内容数组,考虑到以上字段无法覆盖所有词典内容,比如例句、记忆技巧等,可将相应数据添加到该数组,最终也会显示到翻译结果中,见 addtion object。
340+
}
341+
interface PhoneticObject {
342+
type: 'us' | 'uk'; // 音标类型,值可以是 us 或 uk,分别对应美式音标和英式音标。
343+
value?: string; // 音标字符串。例如 ɡʊd。
344+
tts?: TTSResult; // result音标发音数据。
345+
}
346+
interface PartObject {
347+
part: string; // 单词词性,例如 n.、vi....
348+
means: string[]; // 词义 string 数组。
349+
}
350+
interface ExchangeObject {
351+
name: string; // 形式的名字,例如 比较级、最高级...
352+
words: string[]; // 该形式对于的单词 string 数组,一般只有一个
353+
}
354+
interface RelatedWordPartObject {
355+
part?: string; // 词性。
356+
words: Array<RelatedWordObject>; // 相关的单词数组,见 related word object。
357+
}
358+
interface RelatedWordObject {
359+
word: string; // 单词本身。
360+
means?: string[]; // 词义 string 数组。
361+
}
362+
interface AddtionObject {
363+
name: string; // 附加内容名称。
364+
value: string; // 附加内容。
365+
}
366+
367+
// https://ripperhe.gitee.io/bob/#/plugin/object/ttsresult
368+
interface TTSResult {
369+
type: 'url' | 'base64'; // 数据类型,必传。
370+
value: string; // 值,必传。
371+
raw?: any; // 如果插件内部调用了某语音合成接口,可将接口原始数据传回,方便定位问题,可不传。
372+
}
373+
374+
// https://ripperhe.gitee.io/bob/#/plugin/object/ocrresult
375+
interface OcrResult {
376+
from?: Language; // 图片中的文字的主要语种,可与查询参数中传入的 from 不一致,可不传。
377+
texts: Array<OcrText>; // 文本识别结果数组,按照段落分割,见 ocr text,必传。
378+
raw?: any; // 如果插件内部调用了某文本识别接口,可将接口原始数据传回,方便定位问题,可不传。
379+
}
380+
interface OcrText {
381+
text: string;
382+
}
383+
384+
type Result = TranslateResult | OcrResult | TTSResult;
385+
}
386+
387+
declare var $http: Bob.Http;
388+
declare var $info: Bob.Info;
389+
declare var $option: Bob.Option;
390+
declare var $log: Bob.Log;
391+
declare var $data: Bob.Data;
392+
declare var $file: Bob.File;
393+
394+
declare function supportLanguages(): Bob.supportLanguages;
395+
declare function translate(query: Bob.TranslateQuery, completion: Bob.Completion): void;
396+
declare function ocr(query: Bob.OcrQuery, completion: Bob.Completion): void;
397+
declare function tts(query: Bob.TTSQuery, completion: Bob.Completion): void;

0 commit comments

Comments
 (0)