Skip to content

Commit

Permalink
feat: try catch of extension and various quality of life updates
Browse files Browse the repository at this point in the history
  • Loading branch information
yaslam-dev committed Oct 7, 2023
1 parent cfeee02 commit fc169ea
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 50 deletions.
47 changes: 25 additions & 22 deletions ext-src/commands/AnalyzeDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,28 @@ export function activateAnalyzeCommand(context: vscode.ExtensionContext) {
let inflightJobId: string | undefined

const analyzeState = new Analyze(context)
const sessionState = new Session(context).get()
if (!sessionState) return
const sessionToken = new Session(context).get()?.value
if (!sessionToken) {
throw new Error('activateAnalyzeCommand: Session Token is undefined')
}

const editor = vscode.window.activeTextEditor

if (!editor) {
vscode.window.showErrorMessage(CONSTANTS.editorNotSelectorError)

return
throw new Error('activateAnalyzeCommand: Editor is undefined')
}

if (!Util.isValidDocument(editor.document)) {
vscode.window.showErrorMessage(CONSTANTS.editorSelectedIsInvalid)
return
throw new Error('activateAnalyzeCommand: Selected Document is not valid')
}
const documentMetaData = Util.extractMetaDataFromDocument(editor.document)

debugChannel.appendLine(`Metabob: Starting Analysis for ${documentMetaData.filePath}`)

Util.withProgress<SubmitRepresentationResponse>(
handleDocumentAnalyze(documentMetaData, sessionState.value, analyzeState),
handleDocumentAnalyze(documentMetaData, sessionToken, analyzeState),
CONSTANTS.analyzeCommandProgressMessage
).then(response => {
if (response.status === 'pending' || response.status === 'running') {
Expand All @@ -43,23 +44,25 @@ export function activateAnalyzeCommand(context: vscode.ExtensionContext) {
}
})

if (isInQueue) {
Util.withProgress<SubmitRepresentationResponse>(
handleDocumentAnalyze(documentMetaData, sessionState.value, analyzeState, inflightJobId),
CONSTANTS.analyzeCommandQueueMessage
).then(response => {
switch (response.status) {
case 'failed':
case 'complete':
isInQueue = false
break
case 'pending':
case 'running':
isInQueue = true
break
}
})
if (!isInQueue) {
return
}

Util.withProgress<SubmitRepresentationResponse>(
handleDocumentAnalyze(documentMetaData, sessionToken, analyzeState, inflightJobId),
CONSTANTS.analyzeCommandQueueMessage
).then(response => {
switch (response.status) {
case 'failed':
case 'complete':
isInQueue = false
break
case 'pending':
case 'running':
isInQueue = true
break
}
})
}

context.subscriptions.push(vscode.commands.registerCommand(command, commandHandler))
Expand Down
54 changes: 29 additions & 25 deletions ext-src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,35 @@ export function activate(context: vscode.ExtensionContext): void {

const analyzeDocumentOnSaveConfig = AnalyzeDocumentOnSaveConfig()

// Create User Session, If already created get the refresh token
// otherwise, ping server every 60 second to not destroy the token
// if the user has not done any activity
createOrUpdateUserSession(context)

// Analyze command that hit /analyze endpoint with current file content
// then decorate current file with error
activateAnalyzeCommand(context)

// If the user Discard a suggestion, it would be removed from decoration
// and the global state as well
activateDiscardCommand(context)

// If the user feels suggestion is good, he can endorse that suggestion
// Used to notify the model about positive feedback
activateEndorseCommand(context)

// Deprecated
activateFocusRecommendCommand(context)

// When the user click the detail button on the problem
activateDetailSuggestionCommand(context)

// Whenever the user clicks the fix button
activateFixSuggestionCommand(context)
try {
// Create User Session, If already created get the refresh token
// otherwise, ping server every 60 second to not destroy the token
// if the user has not done any activity
createOrUpdateUserSession(context)

// Analyze command that hit /analyze endpoint with current file content
// then decorate current file with error
activateAnalyzeCommand(context)

// If the user Discard a suggestion, it would be removed from decoration
// and the global state as well
activateDiscardCommand(context)

// If the user feels suggestion is good, he can endorse that suggestion
// Used to notify the model about positive feedback
activateEndorseCommand(context)

// Deprecated
activateFocusRecommendCommand(context)

// When the user click the detail button on the problem
activateDetailSuggestionCommand(context)

// Whenever the user clicks the fix button
activateFixSuggestionCommand(context)
} catch (error: any) {
debugChannel.appendLine(`Metabob: ${error}`)
}

// Analyze on Save functionality is only ran if the user enabled it.
if (analyzeDocumentOnSaveConfig && analyzeDocumentOnSaveConfig === true) {
Expand Down
5 changes: 3 additions & 2 deletions ext-src/helpers/CreateOrUpdateUserSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ export async function createOrUpdateUserSession(context: vscode.ExtensionContext
supplementaryId: requestParamId
}
}
const sessionToken = sessionState.get()

const sessionToken = sessionState.get()?.value
if (sessionToken) {
payload['sessionToken'] = sessionToken.value
payload['sessionToken'] = sessionToken
}

const response = await sessionService.createUserSession(payload)
Expand Down
2 changes: 1 addition & 1 deletion ext-src/helpers/HandleDocumentAnalyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const handleDocumentAnalyze = async (
}
})

await analyzeState.set(results)
analyzeState.set(results)

const decorationFromResponse = Util.transformResponseToDecorations(verifiedResponse.results, editor, jobId)
editor.setDecorations(decorationFromResponse.decorationType, [])
Expand Down

0 comments on commit fc169ea

Please sign in to comment.