in packages/core/src/amazonq/session/sessionState.ts [61:171]
protected abstract getScheme(): string
protected abstract getTimeoutErrorCode(): string
protected abstract handleGenerationComplete(
messenger: BaseMessenger,
newFileInfo: NewFileInfo[],
action: SessionStateAction
): void
async generateCode({
messenger,
fs,
codeGenerationId,
telemetry: telemetry,
workspaceFolders,
action,
}: {
messenger: BaseMessenger
fs: VirtualFileSystem
codeGenerationId: string
telemetry: any
workspaceFolders: CurrentWsFolders
action: SessionStateAction
}): Promise<{
newFiles: NewFileInfo[]
deletedFiles: DeletedFileInfo[]
references: CodeReference[]
codeGenerationRemainingIterationCount?: number
codeGenerationTotalIterationCount?: number
}> {
let codeGenerationRemainingIterationCount = undefined
let codeGenerationTotalIterationCount = undefined
for (
let pollingIteration = 0;
pollingIteration < this.pollCount && !this.isCancellationRequested;
++pollingIteration
) {
const codegenResult = await this.config.proxyClient.getCodeGeneration(this.conversationId, codeGenerationId)
codeGenerationRemainingIterationCount = codegenResult.codeGenerationRemainingIterationCount
codeGenerationTotalIterationCount = codegenResult.codeGenerationTotalIterationCount
getLogger().debug(`Codegen response: %O`, codegenResult)
telemetry.setCodeGenerationResult(codegenResult.codeGenerationStatus.status)
switch (codegenResult.codeGenerationStatus.status as CodeGenerationStatus) {
case CodeGenerationStatus.COMPLETE: {
const { newFileContents, deletedFiles, references } =
await this.config.proxyClient.exportResultArchive(this.conversationId)
const logFileInfo = newFileContents.find(
(file: { zipFilePath: string; fileContent: string }) =>
file.zipFilePath === RunCommandLogFileName
)
if (logFileInfo) {
logFileInfo.fileContent = truncate(logFileInfo.fileContent, 10000000, '\n... [truncated]') // Limit to max 20MB
getLogger().info(`sessionState: Run Command logs, ${logFileInfo.fileContent}`)
newFileContents.splice(newFileContents.indexOf(logFileInfo), 1)
}
const newFileInfo = registerNewFiles(
fs,
newFileContents,
this.uploadId,
workspaceFolders,
this.conversationId,
this.getScheme()
)
telemetry.setNumberOfFilesGenerated(newFileInfo.length)
this.handleGenerationComplete(messenger, newFileInfo, action)
return {
newFiles: newFileInfo,
deletedFiles: getDeletedFileInfos(deletedFiles, workspaceFolders),
references,
codeGenerationRemainingIterationCount,
codeGenerationTotalIterationCount,
}
}
case CodeGenerationStatus.PREDICT_READY:
case CodeGenerationStatus.IN_PROGRESS: {
if (codegenResult.codeGenerationStatusDetail) {
this.handleProgress(messenger, action, codegenResult.codeGenerationStatusDetail)
}
await new Promise((f) => globals.clock.setTimeout(f, this.requestDelay))
break
}
case CodeGenerationStatus.PREDICT_FAILED:
case CodeGenerationStatus.DEBATE_FAILED:
case CodeGenerationStatus.FAILED: {
throw this.handleError(messenger, codegenResult)
}
default: {
const errorMessage = `Unknown status: ${codegenResult.codeGenerationStatus.status}\n`
throw new ToolkitError(errorMessage, { code: 'UnknownCodeGenError' })
}
}
}
if (!this.isCancellationRequested) {
const errorMessage = i18n('AWS.amazonq.featureDev.error.codeGen.timeout')
throw new ToolkitError(errorMessage, { code: this.getTimeoutErrorCode() })
}
return {
newFiles: [],
deletedFiles: [],
references: [],
codeGenerationRemainingIterationCount: codeGenerationRemainingIterationCount,
codeGenerationTotalIterationCount: codeGenerationTotalIterationCount,
}
}