@yorha2b-lab/autodev 1.0.4 → 1.0.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yorha2b-lab/autodev",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "基于视觉大模型的前端(Umi+Antd)全自动 CRUD 代码生成器",
5
5
  "bin": {
6
6
  "autodev": "bin/autodev.js"
@@ -1,18 +1,28 @@
1
1
  const JSON5 = require('json5')
2
2
  const sharp = require('sharp')
3
+ const OpenAI = require('openai')
3
4
  const mockPrompt = require('../prompts/mock.js')
5
+ const { getConfig } = require('../utils/utils.js')
4
6
  const apiPrompt = require('../prompts/watch-api.js')
5
- const { getConfig, initOpenAI } = require('../utils/utils.js')
6
7
  const { UI_DESIGNER, API_DESIGNER, MOCK_DESIGNER } = require('../prompts/system.js')
7
8
 
8
9
  const config = getConfig()
9
- const openai = initOpenAI()
10
+
11
+ let client = null
12
+
13
+ const getOpenAI = () => {
14
+ if (!client) {
15
+ client = new OpenAI({ apiKey: process.env.API_KEY, baseURL: process.env.BASE_URL })
16
+ }
17
+ return client
18
+ }
10
19
 
11
20
  const askAI = async (model, messages, retryCount = 0) => {
12
21
 
13
22
  if (retryCount > 3) throw new Error('AI 重试次数耗尽,请检查网络或图片')
14
23
 
15
24
  try {
25
+ const openai = getOpenAI()
16
26
  const response = await openai.chat.completions.create({ model, messages, response_format: { type: 'json_object' } })
17
27
  let raw = response.choices[0].message.content.trim()
18
28
  raw = raw.replace(/^```json\n?/, '').replace(/\n?```$/, '')
@@ -1,6 +1,5 @@
1
1
  const fs = require('fs')
2
2
  const path = require('path')
3
- const OpenAI = require('openai')
4
3
 
5
4
  const getConfig = () => {
6
5
  const localConfigPath = path.join(process.cwd(), 'config.js')
@@ -13,10 +12,6 @@ const getConfig = () => {
13
12
  return config
14
13
  }
15
14
 
16
- const initOpenAI = () => {
17
- return new OpenAI({ apiKey: process.env.API_KEY, baseURL: process.env.BASE_URL })
18
- }
19
-
20
15
  /**
21
16
  * 大模型输出的标准 JSON 无法携带 render: (text) => <Tag> 等箭头函数。
22
17
  * 我们在 Prompt 中要求大模型用 _CODE_ 占位符包裹函数字符串,
@@ -80,4 +75,4 @@ const copyComponents = (options, componentsDir = 'src/components') => {
80
75
  fs.readdirSync(componentsDirectory).forEach(file => fs.copyFileSync(path.join(componentsDirectory, file), path.join(targetDir, file)))
81
76
  }
82
77
 
83
- module.exports = { getConfig, copyHooks, cleanCode, initOpenAI, copyComponents, getExistingMenus, generateSmartImports }
78
+ module.exports = { getConfig, copyHooks, cleanCode, copyComponents, getExistingMenus, generateSmartImports }