Skip to content

Commit

Permalink
fix(amazonq): fix uploading file method error handling for /doc
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinDing1 committed Feb 24, 2025
1 parent 4421290 commit 60b342b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "Bug Fix",
"description": "Amazon Q /doc: Fix uploading file method throwing incorrect workspace too large error message"
}
13 changes: 13 additions & 0 deletions packages/core/src/amazonq/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*
*/

import { ToolkitError } from '../shared/errors'

export class CommonAmazonQContentLengthError extends ToolkitError {
constructor(message: string) {
super(message, { code: 'CommonAmazonQContentLengthError' })
}
}
5 changes: 3 additions & 2 deletions packages/core/src/amazonq/util/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getWorkspaceFoldersByPrefixes,
} from '../../shared/utilities/workspaceUtils'

import { ContentLengthError, PrepareRepoFailedError } from '../../amazonqFeatureDev/errors'
import { PrepareRepoFailedError } from '../../amazonqFeatureDev/errors'
import { getLogger } from '../../shared/logger/logger'
import { maxFileSizeBytes } from '../../amazonqFeatureDev/limits'
import { CurrentWsFolders, DeletedFileInfo, NewFileInfo, NewFileZipContents } from '../../amazonqDoc/types'
Expand All @@ -28,6 +28,7 @@ import { ZipStream } from '../../shared/utilities/zipStream'
import { isPresent } from '../../shared/utilities/collectionUtils'
import { AuthUtil } from '../../codewhisperer/util/authUtil'
import { TelemetryHelper } from '../util/telemetryHelper'
import { CommonAmazonQContentLengthError } from '../errors'

export const SvgFileExtension = '.svg'

Expand Down Expand Up @@ -186,7 +187,7 @@ export async function prepareRepoData(
} catch (error) {
getLogger().debug(`featureDev: Failed to prepare repo: ${error}`)
if (error instanceof ToolkitError && error.code === 'ContentLengthError') {
throw new ContentLengthError()
throw new CommonAmazonQContentLengthError(error.message)
}
throw new PrepareRepoFailedError()
}
Expand Down
45 changes: 27 additions & 18 deletions packages/core/src/amazonqDoc/session/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import fs from '../../shared/fs/fs'
import globals from '../../shared/extensionGlobals'
import { extensionVersion } from '../../shared/vscode/env'
import { getLogger } from '../../shared/logger/logger'
import { ContentLengthError } from '../errors'
import { CommonAmazonQContentLengthError } from '../../amazonq/errors'

export class Session {
private _state?: SessionState | Omit<SessionState, 'uploadId'>
Expand Down Expand Up @@ -126,28 +128,35 @@ export class Session {
return this.nextInteraction(msg, mode, folderPath)
}
private async nextInteraction(msg: string, mode: Mode, folderPath?: string) {
const resp = await this.state.interact({
task: this.task,
msg,
fs: this.config.fs,
mode: mode,
folderPath: folderPath,
messenger: this.messenger,
telemetry: this.telemetry,
tokenSource: this.state.tokenSource,
uploadHistory: this.state.uploadHistory,
})
try {
const resp = await this.state.interact({
task: this.task,
msg,
fs: this.config.fs,
mode: mode,
folderPath: folderPath,
messenger: this.messenger,
telemetry: this.telemetry,
tokenSource: this.state.tokenSource,
uploadHistory: this.state.uploadHistory,
})

if (resp.nextState) {
if (!this.state?.tokenSource?.token.isCancellationRequested) {
this.state?.tokenSource?.cancel()
}

if (resp.nextState) {
if (!this.state?.tokenSource?.token.isCancellationRequested) {
this.state?.tokenSource?.cancel()
// Move to the next state
this._state = resp.nextState
}

// Move to the next state
this._state = resp.nextState
return resp.interaction
} catch (e) {
if (e instanceof CommonAmazonQContentLengthError) {
throw new ContentLengthError()
}
throw e
}

return resp.interaction
}

public async updateFilesPaths(
Expand Down
42 changes: 25 additions & 17 deletions packages/core/src/amazonqFeatureDev/session/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
type SessionStateConfig,
UpdateFilesPathsParams,
} from '../../amazonq/commons/types'
import { ConversationIdNotFoundError } from '../errors'
import { ContentLengthError, ConversationIdNotFoundError } from '../errors'
import { featureDevChat, referenceLogText, featureDevScheme } from '../constants'
import fs from '../../shared/fs/fs'
import { FeatureDevClient } from '../client/featureDev'
Expand All @@ -33,6 +33,7 @@ import { UpdateAnswerMessage } from '../../amazonq/commons/connector/connectorMe
import { FollowUpTypes } from '../../amazonq/commons/types'
import { SessionConfig } from '../../amazonq/commons/session/sessionConfigFactory'
import { Messenger } from '../../amazonq/commons/connector/baseMessenger'
import { CommonAmazonQContentLengthError } from '../../amazonq/errors'
export class Session {
private _state?: SessionState | Omit<SessionState, 'uploadId'>
private task: string = ''
Expand Down Expand Up @@ -137,25 +138,32 @@ export class Session {
}

private async nextInteraction(msg: string) {
const resp = await this.state.interact({
task: this.task,
msg,
fs: this.config.fs,
messenger: this.messenger,
telemetry: this.telemetry,
tokenSource: this.state.tokenSource,
uploadHistory: this.state.uploadHistory,
})
try {
const resp = await this.state.interact({
task: this.task,
msg,
fs: this.config.fs,
messenger: this.messenger,
telemetry: this.telemetry,
tokenSource: this.state.tokenSource,
uploadHistory: this.state.uploadHistory,
})

if (resp.nextState) {
if (!this.state?.tokenSource?.token.isCancellationRequested) {
this.state?.tokenSource?.cancel()
if (resp.nextState) {
if (!this.state?.tokenSource?.token.isCancellationRequested) {
this.state?.tokenSource?.cancel()
}
// Move to the next state
this._state = resp.nextState
}
// Move to the next state
this._state = resp.nextState
}

return resp.interaction
return resp.interaction
} catch (e) {
if (e instanceof CommonAmazonQContentLengthError) {
throw new ContentLengthError()
}
throw e
}
}

public async updateFilesPaths(params: UpdateFilesPathsParams) {
Expand Down

0 comments on commit 60b342b

Please sign in to comment.