Skip to content

Commit 0ec229c

Browse files
authored
Merge pull request #9 from lowcoding/v1.5.0
V1.5.0
2 parents 4b82b5c + b399a54 commit 0ec229c

File tree

16 files changed

+146
-49
lines changed

16 files changed

+146
-49
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",
55
"author": "wjkang <[email protected]>",
6-
"version": "1.4.11",
6+
"version": "1.5.0",
77
"icon": "asset/icon.png",
88
"publisher": "wjkang",
99
"repository": "https://github.com/lowcoding/lowcode-vscode",

src/utils/generate.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { getInnerLibs } from './lib';
1818
import { getOutputChannel } from './outputChannel';
1919
import { getLastAcitveTextEditor } from '../context';
2020
import { getSyncFolder } from './config';
21+
import { createChatCompletionForScript } from './openai';
2122

2223
export const genCodeByBlock = async (data: {
2324
material: string;
@@ -58,12 +59,14 @@ export const genCodeByBlock = async (data: {
5859
}
5960
});
6061
}
61-
const scriptFile = path.join(tempWorkPath, 'script/index.js');
62+
const scriptFile = path.join(block, 'script/index.js'); // 不能使用临时目录里的文件,会导致 ts-node 报错
6263
const hook = {
6364
beforeCompile: (context: any) =>
6465
<object | undefined>Promise.resolve(undefined),
6566
afterCompile: (context: any) =>
6667
<object | undefined>Promise.resolve(undefined),
68+
complete: (context: any) =>
69+
<object | undefined>Promise.resolve(undefined),
6770
};
6871
if (fs.existsSync(scriptFile)) {
6972
delete eval('require').cache[eval('require').resolve(scriptFile)];
@@ -74,6 +77,9 @@ export const genCodeByBlock = async (data: {
7477
if (script.afterCompile) {
7578
hook.afterCompile = script.afterCompile;
7679
}
80+
if (script.complete) {
81+
hook.complete = script.complete;
82+
}
7783
}
7884
const context = {
7985
model: data.model,
@@ -82,6 +88,11 @@ export const genCodeByBlock = async (data: {
8288
env: getEnv(),
8389
libs: getInnerLibs(),
8490
outputChannel: getOutputChannel(),
91+
log: getOutputChannel(),
92+
createBlockPath: path
93+
.join(data.path, ...data.createPath)
94+
.replace(/\\/g, '/'),
95+
createChatCompletion: createChatCompletionForScript,
8596
};
8697
data.model = {
8798
...data.model,
@@ -106,6 +117,7 @@ export const genCodeByBlock = async (data: {
106117
path.join(tempWorkPath, 'src'),
107118
path.join(data.path, ...data.createPath),
108119
);
120+
await hook.complete(context);
109121
fs.removeSync(tempWorkPath);
110122
} catch (ex: any) {
111123
fs.remove(tempWorkPath);
@@ -182,6 +194,8 @@ export const genCodeBySnippet = async (data: {
182194
env: getEnv(),
183195
libs: getInnerLibs(),
184196
outputChannel: getOutputChannel(),
197+
log: getOutputChannel(),
198+
createChatCompletion: createChatCompletionForScript,
185199
code: '',
186200
};
187201
const extendModel = await hook.beforeCompile(context);

src/utils/openai.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as https from 'https';
22
import { TextDecoder } from 'util';
3+
import { getChatGPTConfig } from './config';
34

45
export const createChatCompletion = (options: {
56
apiKey: string;
@@ -104,3 +105,19 @@ export const createChatCompletion = (options: {
104105
request.write(JSON.stringify(body));
105106
request.end();
106107
});
108+
109+
export const createChatCompletionForScript = (options: {
110+
messages: { role: 'system' | 'user' | 'assistant'; content: string }[];
111+
handleChunk?: (data: { text?: string; hasMore: boolean }) => void;
112+
}) => {
113+
const config = getChatGPTConfig();
114+
return createChatCompletion({
115+
hostname: config.hostname,
116+
apiPath: config.apiPath,
117+
apiKey: config.apiKey,
118+
model: config.model,
119+
messages: options.messages,
120+
maxTokens: config.maxTokens,
121+
handleChunk: options.handleChunk,
122+
});
123+
};

src/webview/controllers/script.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import { IMessage } from '../type';
66
import { getEnv, rootPath } from '../../utils/vscodeEnv';
77
import { getInnerLibs } from '../../utils/lib';
88
import { getOutputChannel } from '../../utils/outputChannel';
9+
import { createChatCompletionForScript } from '../../utils/openai';
910

1011
export const runScript = async (
1112
message: IMessage<{
1213
materialPath: string;
14+
createBlockPath?: string;
1315
script: string;
1416
params: string;
1517
model: object;
@@ -28,6 +30,9 @@ export const runScript = async (
2830
env: getEnv(),
2931
libs: getInnerLibs(),
3032
outputChannel: getOutputChannel(),
33+
log: getOutputChannel(),
34+
createBlockPath: message.data.createBlockPath,
35+
createChatCompletion: createChatCompletionForScript,
3136
};
3237
const extendModel = await script[message.data.script](context);
3338
return extendModel;

webview-react/src/components/AmisComponent/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export default (props: IProps) => {
165165
<br></br>
166166
<Space>
167167
<Button type="primary" size="small" onClick={handleOpenRunScriptModal}>
168-
执行脚本设置模板数据
168+
执行脚本
169169
</Button>
170170
<Button
171171
type="primary"

webview-react/src/components/FormilyComponent/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export default (props: IProps) => {
148148
setScriptModalVisible(true);
149149
}}
150150
>
151-
执行脚本设置模板数据
151+
执行脚本
152152
</Button>
153153
<Button
154154
type="primary"

webview-react/src/components/HeaderControl/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { getSchemaWebUrl } from '@/utils/schema';
1818
import ConfigSyncFolder from './components/ConfigSyncFolder';
1919

2020
export default () => {
21-
const { tab, setTab } = useModel('tab');
21+
const { tab } = useModel('tab');
2222
const presenter = usePresenter();
2323
const { model } = presenter;
2424
const menu = (
@@ -34,7 +34,6 @@ export default () => {
3434
<Menu.Item
3535
key="1"
3636
onClick={() => {
37-
setTab('empty');
3837
history.push('/snippets/add/10086');
3938
}}
4039
>

webview-react/src/components/RunScript/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const RunScript: React.FC<IProps> = (props) => {
3434
model: props.model,
3535
materialPath: props.materialPath,
3636
privateMaterials: props.privateMaterials,
37+
createBlockPath: localStorage.getItem('selectedFolder'),
3738
})
3839
.then((result) => {
3940
props.onOk(result);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default () => {
3939
model.setScriptModalVisible(true);
4040
}}
4141
>
42-
执行脚本设置模板数据
42+
执行脚本
4343
</Button>
4444
<Button
4545
type="primary"

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import React from 'react';
2-
import { Input, Row, message, Select } from 'antd';
2+
import { Input, Row, message, Select, Empty, Space, Button } from 'antd';
33
import styles from './index.less';
4-
import { genCodeByBlockMaterial } from '@/webview/service';
4+
import {
5+
executeVscodeCommand,
6+
genCodeByBlockMaterial,
7+
} from '@/webview/service';
58
import SelectDirectory from '@/components/SelectDirectory';
69
import { usePresenter } from './presenter';
710
import BlockItem from './components/BlockItem';
@@ -45,7 +48,7 @@ export default () => {
4548
</div>
4649
</div>
4750
<Row gutter={[16, 16]}>
48-
{model.materials.map((s) => (
51+
{model.materials?.map((s) => (
4952
<BlockItem
5053
key={s.id}
5154
blockItem={s}
@@ -56,6 +59,28 @@ export default () => {
5659
></BlockItem>
5760
))}
5861
</Row>
62+
{model.materials?.length === 0 && (
63+
<Empty
64+
image="https://gw.alipayobjects.com/zos/antfincdn/ZHrcdLPrvN/empty.svg"
65+
imageStyle={{
66+
height: 60,
67+
}}
68+
description={
69+
<span>暂无数据,可点击上面更多菜单,选择创建区块模板</span>
70+
}
71+
>
72+
<Button
73+
type="primary"
74+
onClick={() => {
75+
executeVscodeCommand({
76+
command: 'lowcode.openDownloadMaterials',
77+
});
78+
}}
79+
>
80+
下载物料
81+
</Button>
82+
</Empty>
83+
)}
5984
<SelectDirectory
6085
visible={model.directoryModalVsible}
6186
onCancel={() => {

0 commit comments

Comments
 (0)