Skip to content

Commit ed31459

Browse files
committed
feat: support stream output
1 parent 2462f4b commit ed31459

File tree

6 files changed

+229
-56
lines changed

6 files changed

+229
-56
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ ChatGPT 向我们展示了 GPT 模型的伟大之处,所以我使用 ChatGPT
3535

3636
## 使用方法
3737

38-
1. 安装 [Bob](https://bobtranslate.com/guide/#%E5%AE%89%E8%A3%85) (版本 >= 0.50),一款 macOS 平台的翻译和 OCR 软件
38+
1. 安装 [Bob](https://bobtranslate.com/guide/#%E5%AE%89%E8%A3%85),一款 macOS 平台的翻译和 OCR 软件;[openai-translator.bobplugin](https://github.com/yetone/bob-plugin-openai-translator/releases/latest) >= **1.0.0** 以后默认开启流式输出,需要 Bob 版本 >= **1.8.0**
3939

40-
2. 下载此插件: [openai-polisher.bobplugin](https://github.com/openai-translator/bob-plugin-openai-polisher/releases/latest)
40+
2. 下载此插件: [openai-polisher.bobplugin](https://github.com/yetone/bob-plugin-openai-polisher/releases)
4141

4242
3. 安装此插件:
4343
![安装步骤](https://user-images.githubusercontent.com/1206493/222712959-4a4b27e2-b129-408a-a8af-24a3a89df2dd.gif)

docs/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ ChatGPT has shown us the greatness of the GPT model, so I used ChatGPT's API to
3535

3636
## Usage
3737

38-
1. Install [Bob](https://bobtranslate.com/guide/#%E5%AE%89%E8%A3%85) (version >= 0.50), a macOS translation and OCR software
38+
1. Install [Bob](https://bobtranslate.com/guide/#%E5%AE%89%E8%A3%85), a translation and OCR software for macOS platform; [openai-translator.bobplugin](https://github.com/yetone/bob-plugin-openai-translator/releases/latest) >= **1.0.0** enables streaming output by default after installation, requiring Bob version >= **1.8.0**.
3939

4040
2. Download this plugin: [openai-polisher.bobplugin](https://github.com/openai-translator/bob-plugin-openai-polisher/releases/latest)
4141

global.d.ts

+44-4
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,41 @@ declare namespace Bob {
126126
'yua' = '尤卡坦玛雅语',
127127
'zu' = '祖鲁语',
128128
}
129+
129130
type Languages = Array<keyof typeof LanguagesEnum>;
130131
type supportLanguages = Languages;
131132
type Language = keyof typeof LanguagesEnum;
132133

134+
135+
interface DataPayload {
136+
message: string;
137+
}
138+
139+
interface Disposable {
140+
dispose: () => void;
141+
}
142+
143+
interface Signal {
144+
send: (data?: DataPayload) => void;
145+
subscribe: (callback: (data?: DataPayload) => void) => Disposable;
146+
removeAllSubscriber: () => void;
147+
}
148+
133149
// https://ripperhe.gitee.io/bob/#/plugin/quickstart/translate
134150
type Translate = (query: TranslateQuery, completion: Completion) => void;
135151
type completionResult = { result: Result };
136-
type CompletionResult = { error: ServiceError };
137-
type Completion = (args: completionResult | CompletionResult) => void;
152+
type CompletionError = { error: ServiceError };
153+
type Completion = (args: completionResult | CompletionError) => void;
154+
type HandleStream = (args: completionResult) => void;
138155
interface TranslateQuery {
139156
text: string; // 需要翻译的文本
140157
from: Language; // 用户选中的源语种标准码
141158
to: Language; // 用户选中的目标语种标准码
142159
detectFrom: Exclude<Language, 'auto'>; // 检测过后的源语种
143160
detectTo: Exclude<Language, 'auto'>; // 检测过后的目标语种
161+
cancelSignal: Signal,
162+
onStream: HandleStream,
163+
onCompletion: Completion; // 用于回调翻译结果的函数
144164
}
145165
interface OcrQuery {
146166
from: Language; // 目前用户选中的源语言
@@ -164,7 +184,7 @@ declare namespace Bob {
164184
author?: string; // 插件作者。
165185
homepage?: string; // 插件主页网址。
166186
appcast?: string; // 插件发布信息 URL。
167-
minBobVersion?: string; // 最低支持本插件的 Bob 版本,建议填写您开发插件时候的调试插件的 Bob 版本,目前应该是 0.5.0。
187+
minBobVersion?: string; // 最低支持本插件的 Bob 版本,建议填写您开发插件时候的调试插件的 Bob 版本,目前应该是 1.8.0。
168188
options?: OptionObject[];
169189
}
170190
interface MenuObject {
@@ -203,6 +223,7 @@ declare namespace Bob {
203223
polishingMode: "simplicity" | "detailed";
204224
};
205225

226+
206227
// https://ripperhe.gitee.io/bob/#/plugin/api/log
207228
interface Log {
208229
info: (msg: string) => void; // 用于打印一些常规的信息
@@ -214,6 +235,7 @@ declare namespace Bob {
214235
request<T = any, R = HttpResponsePromise<T>>(config: HttpRequestConfig): Promise<R>;
215236
get<T = any, R = HttpResponsePromise<T>>(config: HttpRequestConfig): Promise<R>;
216237
post<T = any, R = HttpResponsePromise<T>>(config: HttpRequestConfig): Promise<R>;
238+
streamRequest<T = any, R = HttpResponsePromise<T>>(config: HttpStreamRequestConfig): Promise<R>;
217239
}
218240
type HttpMethod =
219241
| 'get'
@@ -239,6 +261,20 @@ declare namespace Bob {
239261
handler?: (resp: HttpResponse) => void;
240262
timeout?: number;
241263
}
264+
265+
interface HttpStreamRequestConfig {
266+
url: string;
267+
method?: HttpMethod;
268+
header?: any;
269+
params?: any;
270+
body?: any;
271+
files?: HttpRequestFiles;
272+
handler?: (resp: HttpResponse) => void;
273+
cancelSignal?: Signal;
274+
streamHandler?: (stream: { text: string, rawData: Data }) => void
275+
timeout?: number;
276+
}
277+
242278
interface HttpRequestFiles {
243279
data: DataObject; // 二进制数据
244280
name: string; // 上传表单中的名称
@@ -390,8 +426,12 @@ declare var $option: Bob.Option;
390426
declare var $log: Bob.Log;
391427
declare var $data: Bob.Data;
392428
declare var $file: Bob.File;
429+
declare var $signal: {
430+
new: () => Bob.Signal;
431+
};
432+
393433

394434
declare function supportLanguages(): Bob.supportLanguages;
395-
declare function translate(query: Bob.TranslateQuery, completion: Bob.Completion): void;
435+
declare function translate(query: Bob.TranslateQuery): void;
396436
declare function ocr(query: Bob.OcrQuery, completion: Bob.Completion): void;
397437
declare function tts(query: Bob.TTSQuery, completion: Bob.Completion): void;

scripts/update_release.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def update_appcast(version, desc):
1515
'desc': desc,
1616
'sha256': file_hash,
1717
'url': f'https://github.com/yetone/bob-plugin-openai-polisher/releases/download/v{version}/{release_file.name}',
18-
'minBobVersion': '0.5.0'
18+
'minBobVersion': '1.8.0'
1919
}
2020
appcast_file = Path('appcast.json')
2121
if appcast_file.is_file():

src/info.json

+41-8
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,38 @@
88
"author": "yetone <[email protected]>",
99
"homepage": "https://github.com/yetone/bob-plugin-openai-polisher",
1010
"appcast": "https://raw.githubusercontent.com/yetone/bob-plugin-openai-polisher/main/appcast.json",
11-
"minBobVersion": "0.5.0",
11+
"minBobVersion": "1.8.0",
1212
"options": [
1313
{
1414
"identifier": "apiUrl",
1515
"type": "text",
1616
"title": "API URL",
1717
"defaultValue": "https://api.openai.com",
18-
"desc": "可选项。如果您的网络环境需要代理才能访问 OpenAI API, 可在这里修改为反代 API 的地址,默认为 https://api.openai.com"
18+
"desc": "可选项。如果您的网络环境需要代理才能访问 OpenAI API, 可在这里修改为反代 API 的地址",
19+
"textConfig": {
20+
"type": "visible",
21+
"placeholderText": "https://api.openai.com"
22+
}
1923
},
2024
{
2125
"identifier": "deploymentName",
2226
"type": "text",
2327
"title": "Dep. Name",
24-
"desc": "可选项。如果您使用的是 Azure OpenAI Service,需要填写对应的 deployment ID"
28+
"desc": "可选项。此值为在部署模型时为部署选择的自定义名称,可在 Azure 门户中的 “资源管理“>“部署“下查看",
29+
"textConfig": {
30+
"type": "visible"
31+
}
2532
},
2633
{
2734
"identifier": "apiKeys",
2835
"type": "text",
2936
"title": "API KEY",
30-
"desc": "必填项。可以用英文逗号分割多个 API KEY 以实现额度加倍及负载均衡"
37+
"desc": "必填项。可以用英文逗号分割多个 API KEY 以实现额度加倍及负载均衡",
38+
"textConfig": {
39+
"type": "secure",
40+
"height": "40",
41+
"placeholderText": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
42+
}
3143
},
3244
{
3345
"identifier": "model",
@@ -72,15 +84,36 @@
7284
{
7385
"identifier": "customSystemPrompt",
7486
"type": "text",
75-
"title": "Sys PPT",
87+
"title": "系统指令",
7688
"defaultValue": "Revise the following sentences to make them more clear, concise, and coherent.",
77-
"desc": "可选项。自定义 System Prompt;必须开启明文显示才可输入中文"
89+
"desc": "可选项。自定义 System Prompt,填写则会覆盖默认的 System Prompt。自定义 Prompt可使用以下变量:\n\n`$text` - 需要润色的文本,即翻译窗口输入框内的文本 `$sourceLang` - 原文语言,即翻译窗口输入框内文本的语言,比如「简体中文」\n\n`$targetLang` - 目标语言,可以在翻译窗口中手动选择或自动检测,比如「English」",
90+
"textConfig": {
91+
"type": "visible",
92+
"height": "100",
93+
"placeholderText": "Revise the following sentences to make them more clear, concise, and coherent.",
94+
"keyWords": [
95+
"$text",
96+
"$sourceLang",
97+
"$targetLang"
98+
]
99+
}
78100
},
79101
{
80102
"identifier": "customUserPrompt",
81103
"type": "text",
82-
"title": "User PPT",
83-
"desc": "可选项。自定义 User Prompt,输入内容会自动拼接在句尾"
104+
"title": "用户指令",
105+
"defaultValue": "$text",
106+
"desc": "可选项。自定义 User Prompt,填写则会覆盖默认的 User Prompt,默认值为`$text`(即翻译窗口输入框内的文本)\n\n自定义 Prompt 中可以使用与系统指令中相同的变量",
107+
"textConfig": {
108+
"type": "visible",
109+
"height": "100",
110+
"placeholderText": "$text",
111+
"keyWords": [
112+
"$text",
113+
"$sourceLang",
114+
"$targetLang"
115+
]
116+
}
84117
},
85118
{
86119
"identifier": "polishingMode",

0 commit comments

Comments
 (0)