Skip to content

Commit fb04f86

Browse files
committed
feat: add progress bars in eval & send commands
1 parent 7438d71 commit fb04f86

6 files changed

Lines changed: 119 additions & 74 deletions

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"project": "./tsconfig.json"
66
},
77
"rules": {
8-
"no-console": ["warn", { "allow": ["error", "info", "warn"] }],
8+
"no-console": ["warn", { "allow": ["debug", "error", "info", "warn"] }],
99

1010
"import/no-extraneous-dependencies": "off",
1111

src/commands/addOrRemoveCurrentDocument.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { window } from 'vscode'
2+
13
import { DocumentInfo } from '../libs/DocumentInfo'
24
import { stackManager } from '../libs/stackManager'
35
import { getCurrentDocumentPath } from '../utils/getCurrentDocumentPath'
@@ -9,17 +11,21 @@ type AddOrRemoveCurrentDocumentArgs = {
911
export function addOrRemoveCurrentDocument(args?: AddOrRemoveCurrentDocumentArgs) {
1012
const currentDocumentAbsolutePath = args ? args.absolutePath : getCurrentDocumentPath()
1113

12-
const isCurrentDocumentSelected = stackManager.documentInfos.some(
14+
const maybeExistingDocumentMatch = stackManager.documentInfos.find(
1315
({ absolutePath }) => absolutePath === currentDocumentAbsolutePath,
1416
)
1517

16-
if (!isCurrentDocumentSelected) {
18+
if (!maybeExistingDocumentMatch) {
1719
const currentDocumentInfo = new DocumentInfo()
1820

1921
stackManager.documentInfos = [...stackManager.documentInfos, currentDocumentInfo]
22+
23+
window.showInformationMessage(`OpenAI Forge: ${currentDocumentInfo.relativePath} unselected.`)
2024
} else {
2125
stackManager.documentInfos = stackManager.documentInfos.filter(
2226
({ absolutePath }) => absolutePath !== currentDocumentAbsolutePath,
2327
)
28+
29+
window.showInformationMessage(`OpenAI Forge: ${maybeExistingDocumentMatch.relativePath} selected.`)
2430
}
2531
}
Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { window, workspace } from 'vscode'
1+
import { ProgressLocation, window, workspace } from 'vscode'
22

33
import { DocumentInfo } from '../libs/DocumentInfo'
44
import { stackManager } from '../libs/stackManager'
@@ -10,29 +10,41 @@ import { runEvaluator } from '../utils/runEvaluator'
1010
import type { WebSocket } from 'ws'
1111

1212
export async function evaluateAndSendCurrentDocumentOrStack(webSocket: WebSocket) {
13-
const excludeProjectInfo = workspace.getConfiguration('openai-forge').get<boolean>('promt.excludeProjectInfo')
14-
15-
const currentWorkspaceInfo = await getCurrentWorkspaceInfo()
16-
const currentOrStackDocumentInfos = stackManager.documentInfos.length
17-
? stackManager.documentInfos
18-
: [new DocumentInfo()]
19-
20-
const errorOutput = await runEvaluator(currentOrStackDocumentInfos, currentWorkspaceInfo)
21-
if (!errorOutput) {
22-
window.showWarningMessage('OpenAI Forge: No error output to send. Aborting.')
23-
24-
return
25-
}
26-
27-
const message = await formatPrompt(currentOrStackDocumentInfos, {
28-
errorOutput,
29-
workspaceInfo: !excludeProjectInfo ? currentWorkspaceInfo : undefined,
30-
})
31-
32-
const webSocketData: WebSocketData = {
33-
action: WebSocketDataAction.ASK,
34-
message,
35-
}
36-
37-
webSocket.send(JSON.stringify(webSocketData))
13+
await window.withProgress(
14+
{
15+
location: ProgressLocation.Notification,
16+
},
17+
async progress => {
18+
progress.report({ message: 'OpenAI Forge: Detecting current project Technology Stack...' })
19+
20+
const currentWorkspaceInfo = await getCurrentWorkspaceInfo()
21+
22+
progress.report({ message: 'OpenAI Forge: Evaluating code to check for errors...' })
23+
24+
const currentOrStackDocumentInfos = stackManager.documentInfos.length
25+
? stackManager.documentInfos
26+
: [new DocumentInfo()]
27+
28+
const errorOutput = await runEvaluator(currentOrStackDocumentInfos, currentWorkspaceInfo)
29+
if (!errorOutput) {
30+
window.showWarningMessage('OpenAI Forge: No error output to send. Aborted.')
31+
32+
return
33+
}
34+
35+
progress.report({ message: 'OpenAI Forge: Sending source code & errors to ChatGPT...' })
36+
37+
const excludeProjectInfo = workspace.getConfiguration('openai-forge').get<boolean>('promt.excludeProjectInfo')
38+
const message = await formatPrompt(currentOrStackDocumentInfos, {
39+
errorOutput,
40+
workspaceInfo: !excludeProjectInfo ? currentWorkspaceInfo : undefined,
41+
})
42+
const webSocketData: WebSocketData = {
43+
action: WebSocketDataAction.ASK,
44+
message,
45+
}
46+
47+
webSocket.send(JSON.stringify(webSocketData))
48+
},
49+
)
3850
}
Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { window, workspace } from 'vscode'
1+
import { ProgressLocation, window, workspace } from 'vscode'
22

33
import { DocumentInfo } from '../libs/DocumentInfo'
44
import { stackManager } from '../libs/stackManager'
@@ -9,28 +9,37 @@ import { getCurrentWorkspaceInfo } from '../utils/getCurrentWorkspaceInfo'
99
import type { WebSocket } from 'ws'
1010

1111
export async function sendCurrentDocument(webSocket: WebSocket) {
12-
const excludeProjectInfo = workspace.getConfiguration('openai-forge').get<boolean>('promt.excludeProjectInfo')
13-
14-
const currentWorkspaceInfo = !excludeProjectInfo ? await getCurrentWorkspaceInfo() : undefined
15-
const currentOrStackDocumentInfos = stackManager.documentInfos.length
16-
? stackManager.documentInfos
17-
: [new DocumentInfo()]
18-
19-
const maybeUserMessage = await window.showInputBox({
20-
placeHolder: 'Just press enter to skip',
21-
prompt: 'Do you want to add a message to the prompt?',
22-
})
23-
const userMessage = maybeUserMessage && maybeUserMessage.trim().length > 0 ? maybeUserMessage : undefined
24-
25-
const message = await formatPrompt(currentOrStackDocumentInfos, {
26-
userMessage,
27-
workspaceInfo: currentWorkspaceInfo,
28-
})
29-
30-
const webSocketData: WebSocketData = {
31-
action: WebSocketDataAction.ASK,
32-
message,
33-
}
34-
35-
webSocket.send(JSON.stringify(webSocketData))
12+
await window.withProgress(
13+
{
14+
location: ProgressLocation.Notification,
15+
},
16+
async progress => {
17+
progress.report({ message: 'OpenAI Forge: Detecting current project Technology Stack...' })
18+
19+
const excludeProjectInfo = workspace.getConfiguration('openai-forge').get<boolean>('promt.excludeProjectInfo')
20+
const currentWorkspaceInfo = !excludeProjectInfo ? await getCurrentWorkspaceInfo() : undefined
21+
const currentOrStackDocumentInfos = stackManager.documentInfos.length
22+
? stackManager.documentInfos
23+
: [new DocumentInfo()]
24+
25+
const maybeUserMessage = await window.showInputBox({
26+
placeHolder: 'Just press enter to skip',
27+
prompt: 'Do you want to add a message to the prompt?',
28+
})
29+
const userMessage = maybeUserMessage && maybeUserMessage.trim().length > 0 ? maybeUserMessage : undefined
30+
31+
progress.report({ message: 'OpenAI Forge: Sending source code & errors to ChatGPT...' })
32+
33+
const message = await formatPrompt(currentOrStackDocumentInfos, {
34+
userMessage,
35+
workspaceInfo: currentWorkspaceInfo,
36+
})
37+
const webSocketData: WebSocketData = {
38+
action: WebSocketDataAction.ASK,
39+
message,
40+
}
41+
42+
webSocket.send(JSON.stringify(webSocketData))
43+
},
44+
)
3645
}

src/utils/getWorkspaceEvaluators.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { globby } from 'globby'
22
import { dirname, sep } from 'path'
3+
import { equals } from 'ramda'
34

45
import { IGNORED_DIRECTORY_GLOBS } from '../constants'
56
import { type WorkspaceEvaluator } from '../types'
@@ -25,13 +26,15 @@ export async function getWorkspaceEvaluators(workspacePath: string): Promise<Wor
2526
const evaluators: WorkspaceEvaluator[] = absolutePathsSortedByDepth.reduce((previousEvaluators, absolutePath) => {
2627
if (
2728
absolutePath.endsWith(`${sep}Cargo.toml`) &&
28-
!previousEvaluators.find(evaluator => evaluator.command.includes('cargo run'))
29+
!previousEvaluators.find(
30+
evaluator => evaluator.command.includes('cargo') && equals(['build'], evaluator.commandArgs),
31+
)
2932
) {
3033
return [
3134
...previousEvaluators,
3235
{
3336
command: 'cargo',
34-
commandArgs: ['run'],
37+
commandArgs: ['build'],
3538
extensions: ['.rs'],
3639
extraExtensions: ['.toml'],
3740
workingDirectoryPath: dirname(absolutePath),

src/utils/runEvaluator.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { execa } from 'execa'
2+
import { relative, sep } from 'path'
23
import { intersection } from 'ramda'
4+
import { ProgressLocation, window } from 'vscode'
35

46
import type { DocumentInfo } from '../libs/DocumentInfo'
57
import type { WorkspaceInfo } from '../types'
@@ -8,27 +10,40 @@ export async function runEvaluator(
810
documentInfos: DocumentInfo[],
911
workspaceInfo: WorkspaceInfo,
1012
): Promise<string | undefined> {
11-
const documentExtensions = documentInfos.map(documentInfo => documentInfo.extension)
13+
return window.withProgress<string | undefined>(
14+
{
15+
location: ProgressLocation.Notification,
16+
},
17+
async progress => {
18+
const documentExtensions = documentInfos.map(documentInfo => documentInfo.extension)
1219

13-
// eslint-disable-next-line no-restricted-syntax
14-
for (const evaluator of workspaceInfo.evaluators) {
15-
if (!intersection(evaluator.extensions, documentExtensions).length) {
16-
// eslint-disable-next-line no-continue
17-
continue
18-
}
20+
// eslint-disable-next-line no-restricted-syntax
21+
for (const evaluator of workspaceInfo.evaluators) {
22+
if (!intersection(evaluator.extensions, documentExtensions).length) {
23+
// eslint-disable-next-line no-continue
24+
continue
25+
}
1926

20-
// eslint-disable-next-line no-await-in-loop
21-
const { stderr } = await execa(evaluator.command, evaluator.commandArgs, {
22-
cwd: evaluator.workingDirectoryPath,
23-
reject: false,
24-
})
27+
progress.report({
28+
message: `Running \`${evaluator.command} ${evaluator.commandArgs.join(' ')}\` in .${sep}${relative(
29+
workspaceInfo.rootPath,
30+
evaluator.workingDirectoryPath,
31+
)}`,
32+
})
33+
// eslint-disable-next-line no-await-in-loop
34+
const { stderr } = await execa(evaluator.command, evaluator.commandArgs, {
35+
cwd: evaluator.workingDirectoryPath,
36+
reject: false,
37+
})
2538

26-
if (!stderr.trim().length) {
27-
return undefined
28-
}
39+
if (!stderr.trim().length) {
40+
return Promise.resolve(undefined)
41+
}
2942

30-
return stderr
31-
}
43+
return Promise.resolve(stderr)
44+
}
3245

33-
return undefined
46+
return Promise.resolve(undefined)
47+
},
48+
)
3449
}

0 commit comments

Comments
 (0)