Skip to content

Commit 8e961f4

Browse files
authored
Merge pull request #31 from lowcoding/feat/v1.8.7
🐞 fix: openai stream format
2 parents e64f4cc + 860c7c2 commit 8e961f4

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "lowcode",
44
"description": "lowcode tool, support ChatGPT and other LLM",
55
"author": "wjkang <[email protected]>",
6-
"version": "1.8.6",
6+
"version": "1.8.7",
77
"icon": "asset/icon.png",
88
"publisher": "wjkang",
99
"repository": "https://github.com/lowcoding/lowcode-vscode",

src/utils/openai.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-continue */
12
import * as https from 'https';
23
import { TextDecoder } from 'util';
34
import { getChatGPTConfig } from './config';
@@ -8,7 +9,7 @@ export const createChatCompletion = (options: {
89
}) =>
910
new Promise<string>((resolve) => {
1011
let combinedResult = '';
11-
let error = '发生错误:';
12+
const error = '发生错误:';
1213
const config = getChatGPTConfig();
1314
const request = https.request(
1415
{
@@ -22,35 +23,40 @@ export const createChatCompletion = (options: {
2223
},
2324
},
2425
(res) => {
26+
let preDataLast = '';
2527
res.on('data', async (chunk) => {
2628
const text = new TextDecoder('utf-8').decode(chunk);
2729
const data = text.split('\n\n').filter((s) => s);
2830
for (let i = 0; i < data.length; i++) {
2931
try {
3032
let element = data[i];
31-
if (element.includes('data: ')) {
33+
if (i === data.length - 1 && !data[i].endsWith('}')) {
34+
preDataLast = data[i];
35+
continue;
36+
}
37+
if (element.startsWith('data: ')) {
3238
if (element.trim() === 'data:') {
3339
// 处理只返回了 data: 的情况
34-
return;
40+
continue;
3541
}
36-
} else if (element.includes('delta')) {
42+
} else {
3743
// 处理没有 data 开头
38-
element = `data: ${element}`;
44+
element = preDataLast + element;
3945
}
4046
if (element.includes('data: ')) {
4147
if (element.includes('[DONE]')) {
4248
if (options.handleChunk) {
4349
options.handleChunk({ text: '' });
4450
}
45-
return;
51+
continue;
4652
}
4753
// remove 'data: '
4854
const data = JSON.parse(element.replace('data: ', ''));
4955
if (data.finish_reason === 'stop') {
5056
if (options.handleChunk) {
5157
options.handleChunk({ text: '' });
5258
}
53-
return;
59+
continue;
5460
}
5561
const openaiRes = data.choices[0].delta.content;
5662
if (openaiRes) {
@@ -62,19 +68,14 @@ export const createChatCompletion = (options: {
6268
combinedResult += openaiRes;
6369
}
6470
} else {
65-
if (options.handleChunk) {
66-
options.handleChunk({
67-
text: element,
68-
});
69-
}
70-
return;
71+
console.log('no includes data: ', element);
7172
}
7273
} catch (e) {
7374
console.error({
74-
e,
75+
e: (e as Error).toString(),
7576
element: data[i],
7677
});
77-
error = (e as Error).toString();
78+
// error = (e as Error).toString();
7879
}
7980
}
8081
});
@@ -84,7 +85,6 @@ export const createChatCompletion = (options: {
8485
text: e.toString(),
8586
});
8687
}
87-
// reject(e);
8888
resolve(e.toString());
8989
});
9090
res.on('end', () => {

webview-react/src/pages/blocks/Detail/index.tsx

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -136,26 +136,6 @@ export default () => {
136136
</Button>
137137
</Space>
138138
</Footer>
139-
<Modal
140-
title="修改表单数据"
141-
maskClosable={false}
142-
visible={model.tempFormDataModal.visible}
143-
onCancel={presenter.handleUpdateModelCancel}
144-
onOk={presenter.handleUpdateModelOk}
145-
okText="确定"
146-
cancelText="取消"
147-
>
148-
<CodeMirror
149-
domId="modelCodeMirror"
150-
lint
151-
value={JSON.stringify(model.tempFormDataModal.formData, null, 2)}
152-
onChange={(value) => {
153-
model.setTempFormDataModal((s) => {
154-
s.formData = JSON.parse(value);
155-
});
156-
}}
157-
/>
158-
</Modal>
159139
<SelectDirectory
160140
visible={model.directoryModalVsible}
161141
loading={model.loading}
@@ -191,6 +171,27 @@ export default () => {
191171
}}
192172
onOk={presenter.handleRunScriptResult}
193173
/>
174+
<Modal
175+
title="修改表单数据"
176+
maskClosable={false}
177+
visible={model.tempFormDataModal.visible}
178+
onCancel={presenter.handleUpdateModelCancel}
179+
onOk={presenter.handleUpdateModelOk}
180+
okText="确定"
181+
cancelText="取消"
182+
zIndex={9999}
183+
>
184+
<CodeMirror
185+
domId="modelCodeMirror"
186+
lint
187+
value={JSON.stringify(model.tempFormDataModal.formData, null, 2)}
188+
onChange={(value) => {
189+
model.setTempFormDataModal((s) => {
190+
s.formData = JSON.parse(value);
191+
});
192+
}}
193+
/>
194+
</Modal>
194195
</div>
195196
);
196197
};

0 commit comments

Comments
 (0)