Skip to content

Commit

Permalink
Merge pull request aws#6654 from KevinDing1/master
Browse files Browse the repository at this point in the history
fix(amazonq): fix uploading file method error handling for /doc
  • Loading branch information
KevinDing1 authored Feb 25, 2025
2 parents 4421290 + f556ec8 commit 5d9cbf3
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 40 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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import {
prepareRepoData,
PrepareRepoDataOptions,
TelemetryHelper,
ContentLengthError,
maxRepoSizeBytes,
} from 'aws-core-vscode/amazonqFeatureDev'
import { assertTelemetry, getWorkspaceFolder, TestFolder } from 'aws-core-vscode/test'
import { fs, AmazonqCreateUpload, ZipStream } from 'aws-core-vscode/shared'
import { MetricName, Span } from 'aws-core-vscode/telemetry'
import sinon from 'sinon'
import { CodeWhispererSettings } from 'aws-core-vscode/codewhisperer'
import { CurrentWsFolders } from 'aws-core-vscode/amazonq'
import { ContentLengthError, CurrentWsFolders } from 'aws-core-vscode/amazonq'
import path from 'path'

const testDevfilePrepareRepo = async (devfileEnabled: boolean) => {
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/amazonq/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*
*/

/**
* Shared error type for content length validation.
* When thrown from common components, individual agents can catch and transform this error
* to provide their own customized error messages.
*/
import { ToolkitError } from '../shared/errors'

export class ContentLengthError extends ToolkitError {
constructor(message: string) {
super(message, { code: 'ContentLengthError' })
}
}
1 change: 1 addition & 0 deletions packages/core/src/amazonq/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export { ExtensionMessage } from '../amazonq/webview/ui/commands'
export { CodeReference } from '../codewhispererChat/view/connector/connector'
export { extractAuthFollowUp } from './util/authUtils'
export { Messenger } from './commons/connector/baseMessenger'
export { ContentLengthError } from './errors'
import { FeatureContext } from '../shared/featureConfig'

/**
Expand Down
7 changes: 4 additions & 3 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 { ContentLengthError } from '../errors'

export const SvgFileExtension = '.svg'

Expand Down Expand Up @@ -184,9 +185,9 @@ export async function prepareRepoData(
zipFileChecksum: zipResult.hash,
}
} catch (error) {
getLogger().debug(`featureDev: Failed to prepare repo: ${error}`)
getLogger().debug(`Failed to prepare repo: ${error}`)
if (error instanceof ToolkitError && error.code === 'ContentLengthError') {
throw new ContentLengthError()
throw new ContentLengthError(error.message)
}
throw new PrepareRepoFailedError()
}
Expand Down
46 changes: 28 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 { ContentLengthError as CommonAmazonQContentLengthError } from '../../amazonq/errors'

export class Session {
private _state?: SessionState | Omit<SessionState, 'uploadId'>
Expand Down Expand Up @@ -126,28 +128,36 @@ 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) {
getLogger().debug(`Content length validation failed: ${e.message}`)
throw new ContentLengthError()
}
throw e
}

return resp.interaction
}

public async updateFilesPaths(
Expand Down
43 changes: 26 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 { ContentLengthError as CommonAmazonQContentLengthError } from '../../amazonq/errors'
export class Session {
private _state?: SessionState | Omit<SessionState, 'uploadId'>
private task: string = ''
Expand Down Expand Up @@ -137,25 +138,33 @@ 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) {
getLogger().debug(`Content length validation failed: ${e.message}`)
throw new ContentLengthError()
}
throw e
}
}

public async updateFilesPaths(params: UpdateFilesPathsParams) {
Expand Down

0 comments on commit 5d9cbf3

Please sign in to comment.