|
| 1 | +// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package software.aws.toolkits.jetbrains.common.clients |
| 5 | + |
| 6 | +import com.intellij.openapi.components.Service |
| 7 | +import com.intellij.openapi.components.service |
| 8 | +import com.intellij.openapi.project.Project |
| 9 | +import com.intellij.openapi.util.SystemInfo |
| 10 | +import software.amazon.awssdk.services.codewhispererruntime.CodeWhispererRuntimeClient |
| 11 | +import software.amazon.awssdk.services.codewhispererruntime.model.ArtifactType |
| 12 | +import software.amazon.awssdk.services.codewhispererruntime.model.ContentChecksumType |
| 13 | +import software.amazon.awssdk.services.codewhispererruntime.model.CreateTaskAssistConversationRequest |
| 14 | +import software.amazon.awssdk.services.codewhispererruntime.model.CreateTaskAssistConversationResponse |
| 15 | +import software.amazon.awssdk.services.codewhispererruntime.model.CreateUploadUrlResponse |
| 16 | +import software.amazon.awssdk.services.codewhispererruntime.model.DocGenerationEvent |
| 17 | +import software.amazon.awssdk.services.codewhispererruntime.model.GetTaskAssistCodeGenerationResponse |
| 18 | +import software.amazon.awssdk.services.codewhispererruntime.model.IdeCategory |
| 19 | +import software.amazon.awssdk.services.codewhispererruntime.model.OperatingSystem |
| 20 | +import software.amazon.awssdk.services.codewhispererruntime.model.OptOutPreference |
| 21 | +import software.amazon.awssdk.services.codewhispererruntime.model.SendTelemetryEventResponse |
| 22 | +import software.amazon.awssdk.services.codewhispererruntime.model.StartTaskAssistCodeGenerationResponse |
| 23 | +import software.amazon.awssdk.services.codewhispererruntime.model.TaskAssistPlanningUploadContext |
| 24 | +import software.amazon.awssdk.services.codewhispererruntime.model.UploadContext |
| 25 | +import software.amazon.awssdk.services.codewhispererruntime.model.UploadIntent |
| 26 | +import software.amazon.awssdk.services.codewhispererruntime.model.UserContext |
| 27 | +import software.amazon.awssdk.services.codewhispererstreaming.model.ExportIntent |
| 28 | +import software.aws.toolkits.core.utils.error |
| 29 | +import software.aws.toolkits.core.utils.getLogger |
| 30 | +import software.aws.toolkits.core.utils.info |
| 31 | +import software.aws.toolkits.jetbrains.common.session.Intent |
| 32 | +import software.aws.toolkits.jetbrains.core.awsClient |
| 33 | +import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnectionManager |
| 34 | +import software.aws.toolkits.jetbrains.core.credentials.pinning.QConnection |
| 35 | +import software.aws.toolkits.jetbrains.services.amazonq.clients.AmazonQStreamingClient |
| 36 | +import software.aws.toolkits.jetbrains.services.amazonqDoc.FEATURE_EVALUATION_PRODUCT_NAME |
| 37 | +import software.aws.toolkits.jetbrains.services.codemodernizer.utils.calculateTotalLatency |
| 38 | +import software.aws.toolkits.jetbrains.services.telemetry.ClientMetadata |
| 39 | +import software.aws.toolkits.jetbrains.settings.AwsSettings |
| 40 | +import java.time.Instant |
| 41 | +import software.amazon.awssdk.services.codewhispererruntime.model.ChatTriggerType as SyncChatTriggerType |
| 42 | + |
| 43 | +@Service(Service.Level.PROJECT) |
| 44 | +class AmazonQCodeGenerateClient(private val project: Project) { |
| 45 | + private fun getTelemetryOptOutPreference() = |
| 46 | + if (AwsSettings.getInstance().isTelemetryEnabled) { |
| 47 | + OptOutPreference.OPTIN |
| 48 | + } else { |
| 49 | + OptOutPreference.OPTOUT |
| 50 | + } |
| 51 | + |
| 52 | + private val docGenerationUserContext = ClientMetadata.getDefault().let { |
| 53 | + val osForFeatureDev: OperatingSystem = |
| 54 | + when { |
| 55 | + SystemInfo.isWindows -> OperatingSystem.WINDOWS |
| 56 | + SystemInfo.isMac -> OperatingSystem.MAC |
| 57 | + // For now, categorize everything else as "Linux" (Linux/FreeBSD/Solaris/etc.) |
| 58 | + else -> OperatingSystem.LINUX |
| 59 | + } |
| 60 | + |
| 61 | + UserContext.builder() |
| 62 | + .ideCategory(IdeCategory.JETBRAINS) |
| 63 | + .operatingSystem(osForFeatureDev) |
| 64 | + .product(FEATURE_EVALUATION_PRODUCT_NAME) |
| 65 | + .clientId(it.clientId) |
| 66 | + .ideVersion(it.awsVersion) |
| 67 | + .build() |
| 68 | + } |
| 69 | + |
| 70 | + fun connection() = ToolkitConnectionManager.getInstance(project).activeConnectionForFeature(QConnection.getInstance()) |
| 71 | + ?: error("Attempted to use connection while one does not exist") |
| 72 | + |
| 73 | + fun bearerClient() = connection().getConnectionSettings().awsClient<CodeWhispererRuntimeClient>() |
| 74 | + |
| 75 | + private val amazonQStreamingClient |
| 76 | + get() = AmazonQStreamingClient.getInstance(project) |
| 77 | + |
| 78 | + fun sendDocGenerationTelemetryEvent( |
| 79 | + docGenerationEvent: DocGenerationEvent, |
| 80 | + ): SendTelemetryEventResponse = |
| 81 | + bearerClient().sendTelemetryEvent { requestBuilder -> |
| 82 | + requestBuilder.telemetryEvent { telemetryEventBuilder -> |
| 83 | + telemetryEventBuilder.docGenerationEvent(docGenerationEvent) |
| 84 | + } |
| 85 | + requestBuilder.optOutPreference(getTelemetryOptOutPreference()) |
| 86 | + requestBuilder.userContext(docGenerationUserContext) |
| 87 | + } |
| 88 | + |
| 89 | + fun createTaskAssistConversation(): CreateTaskAssistConversationResponse = bearerClient().createTaskAssistConversation( |
| 90 | + CreateTaskAssistConversationRequest.builder().build() |
| 91 | + ) |
| 92 | + |
| 93 | + fun createTaskAssistUploadUrl(conversationId: String, contentChecksumSha256: String, contentLength: Long): CreateUploadUrlResponse = |
| 94 | + bearerClient().createUploadUrl { |
| 95 | + it.contentChecksumType(ContentChecksumType.SHA_256) |
| 96 | + .contentChecksum(contentChecksumSha256) |
| 97 | + .contentLength(contentLength) |
| 98 | + .artifactType(ArtifactType.SOURCE_CODE) |
| 99 | + .uploadIntent(UploadIntent.TASK_ASSIST_PLANNING) |
| 100 | + .uploadContext( |
| 101 | + UploadContext.builder() |
| 102 | + .taskAssistPlanningUploadContext( |
| 103 | + TaskAssistPlanningUploadContext.builder() |
| 104 | + .conversationId(conversationId) |
| 105 | + .build() |
| 106 | + ) |
| 107 | + .build() |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + fun startTaskAssistCodeGeneration(conversationId: String, uploadId: String, userMessage: String, intent: Intent): StartTaskAssistCodeGenerationResponse = |
| 112 | + bearerClient() |
| 113 | + .startTaskAssistCodeGeneration { request -> |
| 114 | + request |
| 115 | + .conversationState { |
| 116 | + it |
| 117 | + .conversationId(conversationId) |
| 118 | + .chatTriggerType(SyncChatTriggerType.MANUAL) |
| 119 | + .currentMessage { cm -> cm.userInputMessage { um -> um.content(userMessage) } } |
| 120 | + } |
| 121 | + .workspaceState { |
| 122 | + it |
| 123 | + .programmingLanguage { pl -> pl.languageName("javascript") } |
| 124 | + .uploadId(uploadId) |
| 125 | + } |
| 126 | + .intent(intent.name) |
| 127 | + } |
| 128 | + |
| 129 | + fun getTaskAssistCodeGeneration(conversationId: String, codeGenerationId: String): GetTaskAssistCodeGenerationResponse = bearerClient() |
| 130 | + .getTaskAssistCodeGeneration { |
| 131 | + it |
| 132 | + .conversationId(conversationId) |
| 133 | + .codeGenerationId(codeGenerationId) |
| 134 | + } |
| 135 | + |
| 136 | + suspend fun exportTaskAssistResultArchive(conversationId: String): MutableList<ByteArray> = amazonQStreamingClient.exportResultArchive( |
| 137 | + conversationId, |
| 138 | + ExportIntent.TASK_ASSIST, |
| 139 | + null, |
| 140 | + { e -> |
| 141 | + LOG.error(e) { "TaskAssist - ExportResultArchive stream exportId=$conversationId exportIntent=${ExportIntent.TASK_ASSIST} Failed: ${e.message} " } |
| 142 | + }, |
| 143 | + { startTime -> |
| 144 | + LOG.info { "TaskAssist - ExportResultArchive latency: ${calculateTotalLatency(startTime, Instant.now())}" } |
| 145 | + } |
| 146 | + ) |
| 147 | + |
| 148 | + companion object { |
| 149 | + private val LOG = getLogger<AmazonQCodeGenerateClient>() |
| 150 | + |
| 151 | + fun getInstance(project: Project) = project.service<AmazonQCodeGenerateClient>() |
| 152 | + } |
| 153 | +} |
0 commit comments