@yorha2b-lab/autodev 1.0.5 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/autodev.js +25 -16
- package/package.json +1 -1
- package/src/services/llm.js +18 -1
package/bin/autodev.js
CHANGED
|
@@ -3,6 +3,7 @@ const fs = require('fs')
|
|
|
3
3
|
const path = require('path')
|
|
4
4
|
const chalk = require('chalk')
|
|
5
5
|
const figlet = require('figlet')
|
|
6
|
+
const { program } = require('commander')
|
|
6
7
|
|
|
7
8
|
console.log(chalk.cyan(figlet.textSync('AutoDev', { horizontalLayout: 'full' })))
|
|
8
9
|
console.log(chalk.gray('--------------------------------------------------'))
|
|
@@ -10,35 +11,43 @@ console.log(chalk.white(' [System] ') + chalk.green('YoRHa No.2 Type B Unit: ')
|
|
|
10
11
|
console.log(chalk.white(' [Mission] ') + chalk.yellow('Generate Frontend CRUD: ') + chalk.cyan('Awaiting Command'))
|
|
11
12
|
console.log(chalk.gray('--------------------------------------------------\n'))
|
|
12
13
|
|
|
13
|
-
const localEnv = path.resolve(process.cwd(), '.env')
|
|
14
|
-
const pkgEnv = path.resolve(__dirname, '../.env')
|
|
15
|
-
|
|
16
|
-
require("dotenv").config({
|
|
17
|
-
path: fs.existsSync(localEnv) ? localEnv : pkgEnv
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
const { program } = require('commander')
|
|
21
|
-
const watchApi = require('../src/commands/watch-api')
|
|
22
|
-
const watchPage = require('../src/commands/watch-page')
|
|
23
|
-
const watchPart = require('../src/commands/watch-part')
|
|
24
|
-
|
|
25
14
|
program.version('1.0.0').description('AI驱动的前端CRUD代码生成器')
|
|
26
|
-
|
|
27
15
|
program.option('-t, --template <type>', '指定前端框架模板', 'react')
|
|
28
16
|
|
|
17
|
+
program
|
|
18
|
+
.command('init')
|
|
19
|
+
.description('在当前目录初始化 .env 和 config.js 配置文件')
|
|
20
|
+
.action(() => {
|
|
21
|
+
const tplConfig = path.resolve(__dirname, '../config.js')
|
|
22
|
+
const tplEnv = path.resolve(__dirname, '../.env.example')
|
|
23
|
+
fs.copyFileSync(tplConfig, path.join(process.cwd(), 'config.js'))
|
|
24
|
+
fs.copyFileSync(tplEnv, path.join(process.cwd(), '.env'))
|
|
25
|
+
console.log('✅ 已生成 config.js')
|
|
26
|
+
console.log('✅ 已生成 .env')
|
|
27
|
+
})
|
|
28
|
+
|
|
29
29
|
program
|
|
30
30
|
.command('watch:page')
|
|
31
31
|
.description('监听截图,自动生成完整的增删改查页面')
|
|
32
|
-
.action(() =>
|
|
32
|
+
.action(() => {
|
|
33
|
+
const watchPage = require('../src/commands/watch-page')
|
|
34
|
+
watchPage(program.opts())
|
|
35
|
+
})
|
|
33
36
|
|
|
34
37
|
program
|
|
35
38
|
.command('watch:part')
|
|
36
39
|
.description('监听截图,自动生成局部 UI 组件')
|
|
37
|
-
.action(() =>
|
|
40
|
+
.action(() => {
|
|
41
|
+
const watchPart = require('../src/commands/watch-part')
|
|
42
|
+
watchPart(program.opts())
|
|
43
|
+
})
|
|
38
44
|
|
|
39
45
|
program
|
|
40
46
|
.command('watch:api')
|
|
41
47
|
.description('监听 Swagger,自动对齐真实接口字段')
|
|
42
|
-
.action(() =>
|
|
48
|
+
.action(() => {
|
|
49
|
+
const watchApi = require('../src/commands/watch-api')
|
|
50
|
+
watchApi()
|
|
51
|
+
})
|
|
43
52
|
|
|
44
53
|
program.parse(process.argv)
|
package/package.json
CHANGED
package/src/services/llm.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const path = require('path')
|
|
1
3
|
const JSON5 = require('json5')
|
|
2
4
|
const sharp = require('sharp')
|
|
3
5
|
const OpenAI = require('openai')
|
|
@@ -7,13 +9,28 @@ const apiPrompt = require('../prompts/watch-api.js')
|
|
|
7
9
|
const { UI_DESIGNER, API_DESIGNER, MOCK_DESIGNER } = require('../prompts/system.js')
|
|
8
10
|
|
|
9
11
|
const config = getConfig()
|
|
10
|
-
|
|
12
|
+
|
|
13
|
+
let client = null
|
|
14
|
+
|
|
15
|
+
const getOpenAI = () => {
|
|
16
|
+
if (!client) {
|
|
17
|
+
|
|
18
|
+
const localEnv = path.resolve(process.cwd(), '.env')
|
|
19
|
+
const pkgEnv = path.resolve(__dirname, '../.env')
|
|
20
|
+
require("dotenv").config({
|
|
21
|
+
path: fs.existsSync(localEnv) ? localEnv : pkgEnv
|
|
22
|
+
})
|
|
23
|
+
client = new OpenAI({ apiKey: process.env.API_KEY, baseURL: process.env.BASE_URL })
|
|
24
|
+
}
|
|
25
|
+
return client
|
|
26
|
+
}
|
|
11
27
|
|
|
12
28
|
const askAI = async (model, messages, retryCount = 0) => {
|
|
13
29
|
|
|
14
30
|
if (retryCount > 3) throw new Error('AI 重试次数耗尽,请检查网络或图片')
|
|
15
31
|
|
|
16
32
|
try {
|
|
33
|
+
const openai = getOpenAI()
|
|
17
34
|
const response = await openai.chat.completions.create({ model, messages, response_format: { type: 'json_object' } })
|
|
18
35
|
let raw = response.choices[0].message.content.trim()
|
|
19
36
|
raw = raw.replace(/^```json\n?/, '').replace(/\n?```$/, '')
|