Skip to content

Commit 9a086ff

Browse files
authored
fix(AI): AI is not triggered (#4422)
1 parent 7afd538 commit 9a086ff

File tree

2 files changed

+45
-35
lines changed

2 files changed

+45
-35
lines changed

docs/helpers/AI.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ title: AI
1414
AI Helper for CodeceptJS.
1515

1616
This helper class provides integration with the AI GPT-3.5 or 4 language model for generating responses to questions or prompts within the context of web pages. It allows you to interact with the GPT-3.5 model to obtain intelligent responses based on HTML fragments or general prompts.
17-
This helper should be enabled with any web helpers like Playwright or Puppeteer or WebDrvier to ensure the HTML context is available.
17+
This helper should be enabled with any web helpers like Playwright or Puppeteer or WebDriver to ensure the HTML context is available.
1818

1919
Use it only in development mode. It is recommended to run it only inside pause() mode.
2020

2121
## Configuration
2222

23-
This helper should be configured in codecept.json or codecept.conf.js
23+
This helper should be configured in codecept.conf.{js|ts}
2424

2525
- `chunkSize`: - The maximum number of characters to send to the AI API at once. We split HTML fragments by 8000 chars to not exceed token limit. Increase this value if you use GPT-4.
2626

lib/helper/AI.js

+43-33
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ const { beautify } = require('../utils')
1010
const output = require('../output')
1111
const { registerVariable } = require('../pause')
1212

13+
const gtpRole = {
14+
user: 'user',
15+
}
16+
1317
/**
1418
* AI Helper for CodeceptJS.
1519
*
1620
* This helper class provides integration with the AI GPT-3.5 or 4 language model for generating responses to questions or prompts within the context of web pages. It allows you to interact with the GPT-3.5 model to obtain intelligent responses based on HTML fragments or general prompts.
17-
* This helper should be enabled with any web helpers like Playwright or Puppeteer or WebDrvier to ensure the HTML context is available.
21+
* This helper should be enabled with any web helpers like Playwright or Puppeteer or WebDriver to ensure the HTML context is available.
1822
*
1923
* Use it only in development mode. It is recommended to run it only inside pause() mode.
2024
*
2125
* ## Configuration
2226
*
23-
* This helper should be configured in codecept.json or codecept.conf.js
27+
* This helper should be configured in codecept.conf.{js|ts}
2428
*
2529
* * `chunkSize`: (optional, default: 80000) - The maximum number of characters to send to the AI API at once. We split HTML fragments by 8000 chars to not exceed token limit. Increase this value if you use GPT-4.
2630
*/
@@ -33,6 +37,7 @@ class AI extends Helper {
3337
chunkSize: 80000,
3438
}
3539
this.options = { ...this.options, ...config }
40+
this.aiAssistant.enable(this.config)
3641
}
3742

3843
_beforeSuite() {
@@ -68,8 +73,8 @@ class AI extends Helper {
6873

6974
for (const chunk of htmlChunks) {
7075
const messages = [
71-
{ role: 'user', content: prompt },
72-
{ role: 'user', content: `Within this HTML: ${minifyHtml(chunk)}` },
76+
{ role: gtpRole.user, content: prompt },
77+
{ role: gtpRole.user, content: `Within this HTML: ${minifyHtml(chunk)}` },
7378
]
7479

7580
if (htmlChunks.length > 1)
@@ -104,8 +109,8 @@ class AI extends Helper {
104109
const html = await this.helper.grabHTMLFrom(locator)
105110

106111
const messages = [
107-
{ role: 'user', content: prompt },
108-
{ role: 'user', content: `Within this HTML: ${minifyHtml(html)}` },
112+
{ role: gtpRole.user, content: prompt },
113+
{ role: gtpRole.user, content: `Within this HTML: ${minifyHtml(html)}` },
109114
]
110115

111116
const response = await this._processAIRequest(messages)
@@ -121,7 +126,7 @@ class AI extends Helper {
121126
* @returns {Promise<string>} - A Promise that resolves to the generated response from the GPT model.
122127
*/
123128
async askGptGeneralPrompt(prompt) {
124-
const messages = [{ role: 'user', content: prompt }]
129+
const messages = [{ role: gtpRole.user, content: prompt }]
125130

126131
const response = await this._processAIRequest(messages)
127132

@@ -156,40 +161,45 @@ class AI extends Helper {
156161
* @returns {Promise<Object>} A promise that resolves to the requested page object.
157162
*/
158163
async askForPageObject(pageName, extraPrompt = null, locator = null) {
159-
const html = locator ? await this.helper.grabHTMLFrom(locator) : await this.helper.grabSource()
160-
161164
const spinner = ora(' Processing AI request...').start()
162-
await this.aiAssistant.setHtmlContext(html)
163-
const response = await this.aiAssistant.generatePageObject(extraPrompt, locator)
164-
spinner.stop()
165165

166-
if (!response[0]) {
167-
output.error('No response from AI')
168-
return ''
169-
}
166+
try {
167+
const html = locator ? await this.helper.grabHTMLFrom(locator) : await this.helper.grabSource()
168+
await this.aiAssistant.setHtmlContext(html)
169+
const response = await this.aiAssistant.generatePageObject(extraPrompt, locator)
170+
spinner.stop()
171+
172+
if (!response[0]) {
173+
output.error('No response from AI')
174+
return ''
175+
}
170176

171-
const code = beautify(response[0])
177+
const code = beautify(response[0])
172178

173-
output.print('----- Generated PageObject ----')
174-
output.print(code)
175-
output.print('-------------------------------')
179+
output.print('----- Generated PageObject ----')
180+
output.print(code)
181+
output.print('-------------------------------')
176182

177-
const fileName = path.join(output_dir, `${pageName}Page-${Date.now()}.js`)
183+
const fileName = path.join(output_dir, `${pageName}Page-${Date.now()}.js`)
178184

179-
output.print(output.styles.bold(`Page object for ${pageName} is saved to ${output.styles.bold(fileName)}`))
180-
fs.writeFileSync(fileName, code)
185+
output.print(output.styles.bold(`Page object for ${pageName} is saved to ${output.styles.bold(fileName)}`))
186+
fs.writeFileSync(fileName, code)
181187

182-
try {
183-
registerVariable('page', require(fileName))
184-
output.success('Page object registered for this session as `page` variable')
185-
output.print('Use `=>page.methodName()` in shell to run methods of page object')
186-
output.print('Use `click(page.locatorName)` to check locators of page object')
187-
} catch (err) {
188-
output.error('Error while registering page object')
189-
output.error(err.message)
190-
}
188+
try {
189+
registerVariable('page', require(fileName))
190+
output.success('Page object registered for this session as `page` variable')
191+
output.print('Use `=>page.methodName()` in shell to run methods of page object')
192+
output.print('Use `click(page.locatorName)` to check locators of page object')
193+
} catch (err) {
194+
output.error('Error while registering page object')
195+
output.error(err.message)
196+
}
191197

192-
return code
198+
return code
199+
} catch (e) {
200+
spinner.stop()
201+
throw Error(`Something went wrong! ${e.message}`)
202+
}
193203
}
194204

195205
async _processAIRequest(messages) {

0 commit comments

Comments
 (0)