|
1 |
| -const _ = require('lodash'); |
2 |
| -const axios = require('axios'); |
3 |
| -const { jestAuthFileGenerationLog } = require('../utils/cmdPrint'); |
4 |
| -const { bold, reset, red, blue } = require('../utils/cmdPrint').colors; |
5 | 1 | const args = require('../utils/getArgs.js');
|
6 |
| -const {PYTHAGORA_UNIT_TESTS_VERSION,PYTHAGORA_API_SERVER} = require('../const/common'); |
7 |
| -const API_SERVER = args.pythagora_api_server || PYTHAGORA_API_SERVER; |
| 2 | +const {PYTHAGORA_API_SERVER} = require("@pythagora.io/js-code-processing").common; |
8 | 3 |
|
9 |
| -function extractGPTMessageFromStreamData(input) { |
10 |
| - const regex = /data: (.*?)\n/g; |
11 |
| - const substrings = []; |
12 |
| - let match; |
13 |
| - |
14 |
| - while ((match = regex.exec(input)) !== null) { |
15 |
| - substrings.push(match[1]); |
| 4 | +function getApiConfig() { |
| 5 | + return { |
| 6 | + apiUrl: args.pythagora_api_server || PYTHAGORA_API_SERVER, |
| 7 | + apiKey: args.openai_api_key || args.pythagora_api_key, |
| 8 | + apiKeyType: args.openai_api_key ? 'openai' : 'pythagora' |
16 | 9 | }
|
17 |
| - |
18 |
| - return substrings.map(s => JSON.parse(s)); |
19 |
| -} |
20 |
| - |
21 |
| -function setOptions({path, method, headers}) { |
22 |
| - let apiKey = args.openai_api_key || args.pythagora_api_key; |
23 |
| - const parsedUrl = new URL(API_SERVER); |
24 |
| - if (!apiKey) throw new Error('No API key provided. Please add --openai-api-key or --pythagora-api-key') |
25 |
| - let options = { |
26 |
| - protocol: parsedUrl.protocol.replace(':', ''), |
27 |
| - hostname: parsedUrl.hostname, |
28 |
| - port: parsedUrl.port, |
29 |
| - path: path || '/', |
30 |
| - method: method || 'POST', |
31 |
| - headers: headers || { |
32 |
| - 'Content-Type': 'application/json', |
33 |
| - 'apikey': apiKey, |
34 |
| - 'apikeytype': args.openai_api_key ? 'openai' : 'pythagora' |
35 |
| - }, |
36 |
| - }; |
37 |
| - |
38 |
| - if (!options.port) delete options.port; |
39 |
| - return options |
40 | 10 | }
|
41 | 11 |
|
42 |
| -async function makeRequest(data, options, customLogFunction) { |
43 |
| - let gptResponse = ''; |
44 |
| - let httpModule = options.protocol === 'http' ? require('http') : require('https'); |
45 |
| - let timeout; |
46 |
| - |
47 |
| - return new Promise((resolve, reject) => { |
48 |
| - const req = httpModule.request(_.omit(options, ['protocol']), function(res){ |
49 |
| - res.on('data', (chunk) => { |
50 |
| - try { |
51 |
| - clearTimeout(timeout); |
52 |
| - timeout = setTimeout(() => { |
53 |
| - reject(new Error("Request timeout")); |
54 |
| - }, 30000); |
55 |
| - |
56 |
| - let stringified = chunk.toString(); |
57 |
| - try { |
58 |
| - let json = JSON.parse(stringified); |
59 |
| - if (json.error || json.message) { |
60 |
| - gptResponse = json; |
61 |
| - return; |
62 |
| - } |
63 |
| - } catch (e) {} |
64 |
| - |
65 |
| - gptResponse += stringified; |
66 |
| - if (gptResponse.indexOf('pythagora_end:') > -1) return; |
67 |
| - if (customLogFunction) customLogFunction(gptResponse); |
68 |
| - else process.stdout.write(stringified); |
69 |
| - } catch (e) {} |
70 |
| - }); |
71 |
| - res.on('end', async function () { |
72 |
| - clearTimeout(timeout); |
73 |
| - |
74 |
| - process.stdout.write('\n'); |
75 |
| - if (res.statusCode >= 400) return reject(new Error(`Response status code: ${res.statusCode}. Error message: ${gptResponse}`)); |
76 |
| - if (gptResponse.error) return reject(new Error(`Error: ${gptResponse.error.message}. Code: ${gptResponse.error.code}`)); |
77 |
| - if (gptResponse.message) return reject(new Error(`Error: ${gptResponse.message}. Code: ${gptResponse.code}`)); |
78 |
| - gptResponse = gptResponse.split('pythagora_end:').pop(); |
79 |
| - return resolve(gptResponse); |
80 |
| - }); |
81 |
| - }); |
82 |
| - |
83 |
| - req.on('error', (e) => { |
84 |
| - clearTimeout(timeout); |
85 |
| - console.error("problem with request:"+e.message); |
86 |
| - reject(e); |
87 |
| - }); |
88 |
| - |
89 |
| - req.write(data); |
90 |
| - |
91 |
| - req.end(); |
92 |
| - }); |
93 |
| -} |
94 |
| - |
95 |
| -async function getUnitTests(data, customLogFunction) { |
96 |
| - let options = setOptions({path: '/api/generate-unit-tests'}); |
97 |
| - let tests, error; |
98 |
| - try { |
99 |
| - tests = await makeRequest(JSON.stringify(data), options, customLogFunction); |
100 |
| - } catch (e) { |
101 |
| - error = e; |
102 |
| - } finally { |
103 |
| - return {tests, error}; |
104 |
| - } |
105 |
| -} |
106 |
| - |
107 |
| -async function expandUnitTests(data, customLogFunction) { |
108 |
| - let options = setOptions({path: '/api/expand-unit-tests'}); |
109 |
| - let tests, error; |
110 |
| - try { |
111 |
| - tests = await makeRequest(JSON.stringify(data), options, customLogFunction); |
112 |
| - } catch (e) { |
113 |
| - error = e; |
114 |
| - } finally { |
115 |
| - return {tests, error}; |
116 |
| - } |
117 |
| -} |
118 |
| - |
119 |
| -async function getJestAuthFunction(loginMongoQueriesArray, loginRequestBody, loginEndpointPath) { |
120 |
| - jestAuthFileGenerationLog(); |
121 |
| - |
122 |
| - let options = setOptions({path: '/api/generate-jest-auth'}); |
123 |
| - return await makeRequest(JSON.stringify({loginMongoQueriesArray, loginRequestBody, loginEndpointPath}), options); |
124 |
| -} |
125 |
| - |
126 |
| - |
127 |
| -async function getJestTest(test) { |
128 |
| - let options = setOptions({path: '/api/generate-jest-test'}); |
129 |
| - return await makeRequest(JSON.stringify(test), options); |
130 |
| -} |
131 |
| - |
132 |
| -async function getJestTestName(test, usedNames) { |
133 |
| - let options = setOptions({path:'/api/generate-jest-test-name'}); |
134 |
| - return await makeRequest(JSON.stringify({ test }), options); |
135 |
| -} |
136 |
| - |
137 |
| -async function isEligibleForExport(test) { |
138 |
| - try { |
139 |
| - let options = setOptions({ path: '/api/check-if-eligible' }); |
140 |
| - |
141 |
| - const response = await axios.post( |
142 |
| - `${options.protocol}://${options.hostname}${options.port ? ':' + options.port : ''}${options.path}`, |
143 |
| - JSON.stringify({ test }), |
144 |
| - { headers: options.headers } |
145 |
| - ); |
146 |
| - |
147 |
| - return response.data; |
148 |
| - } catch (error) { |
149 |
| - console.log(error); |
150 |
| - return false; |
151 |
| - } |
152 |
| -} |
153 |
| - |
154 |
| -function checkForAPIKey() { |
155 |
| - if (!args.pythagora_api_key && !args.openai_api_key) { |
156 |
| - console.log(`${bold+red}No API key found!${reset}`); |
157 |
| - console.log('Please run:') |
158 |
| - console.log(`${bold+blue}npx pythagora --config --pythagora-api-key <YOUR_PYTHAGORA_API_KEY>${reset}`); |
159 |
| - console.log('or') |
160 |
| - console.log(`${bold+blue}npx pythagora --config --openai-api-key <YOUR_OPENAI_API_KEY>${reset}`); |
161 |
| - console.log(`You can get Pythagora API key here: https://mailchi.mp/f4f4d7270a7a/api-waitlist`); |
162 |
| - process.exit(0); |
163 |
| - } |
164 |
| -} |
165 |
| - |
166 |
| -module.exports = { |
167 |
| - getJestAuthFunction, |
168 |
| - getJestTest, |
169 |
| - getJestTestName, |
170 |
| - isEligibleForExport, |
171 |
| - getUnitTests, |
172 |
| - expandUnitTests, |
173 |
| - checkForAPIKey |
174 |
| -} |
| 12 | +module.exports = {getApiConfig}; |
0 commit comments