|
| 1 | +package com.glean.api_client.glean_api_client.hooks; |
| 2 | + |
| 3 | +import com.glean.api_client.glean_api_client.models.errors.APIException; |
| 4 | +import com.glean.api_client.glean_api_client.models.errors.AsyncAPIException; |
| 5 | +import com.glean.api_client.glean_api_client.utils.AsyncHook; |
| 6 | +import com.glean.api_client.glean_api_client.utils.Hook; |
| 7 | +import com.glean.api_client.glean_api_client.utils.Utils; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.net.http.HttpResponse; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.util.Optional; |
| 14 | +import java.util.concurrent.CompletableFuture; |
| 15 | + |
| 16 | +/** |
| 17 | + * Hook that provides helpful error messages when developers incorrectly pass file objects |
| 18 | + * directly to agent run operations instead of using the two-step upload process. |
| 19 | + */ |
| 20 | +public final class AgentFileUploadErrorHook { |
| 21 | + |
| 22 | + private AgentFileUploadErrorHook() { |
| 23 | + // prevent instantiation |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Creates a synchronous AfterError hook for agent file upload errors. |
| 28 | + */ |
| 29 | + public static Hook.AfterError createSyncHook() { |
| 30 | + return (context, response, error) -> { |
| 31 | + if (response.isPresent()) { |
| 32 | + HttpResponse<InputStream> httpResponse = response.get(); |
| 33 | + String operationId = context.operationId(); |
| 34 | + |
| 35 | + if (("createAndWaitRun".equals(operationId) || "createAndStreamRun".equals(operationId)) |
| 36 | + && httpResponse.statusCode() == 400) { |
| 37 | + try { |
| 38 | + byte[] bodyBytes = Utils.extractByteArrayFromBody(httpResponse); |
| 39 | + String bodyText = new String(bodyBytes, StandardCharsets.UTF_8).toLowerCase(); |
| 40 | + |
| 41 | + if (bodyText.contains("permission")) { |
| 42 | + String helpfulMessage = buildFileUploadErrorMessage(); |
| 43 | + throw APIException.from(helpfulMessage, httpResponse); |
| 44 | + } |
| 45 | + } catch (IOException e) { |
| 46 | + // If we can't read the body, fall through to original error handling |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if (error.isPresent()) { |
| 52 | + throw error.get(); |
| 53 | + } |
| 54 | + return response.get(); |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates an asynchronous AfterError hook for agent file upload errors. |
| 60 | + */ |
| 61 | + public static AsyncHook.AfterError createAsyncHook() { |
| 62 | + return (context, response, error) -> { |
| 63 | + if (response != null) { |
| 64 | + String operationId = context.operationId(); |
| 65 | + |
| 66 | + if (("createAndWaitRun".equals(operationId) || "createAndStreamRun".equals(operationId)) |
| 67 | + && response.statusCode() == 400) { |
| 68 | + return response.body().toByteArray() |
| 69 | + .thenApply(bodyBytes -> { |
| 70 | + String bodyText = new String(bodyBytes, StandardCharsets.UTF_8).toLowerCase(); |
| 71 | + |
| 72 | + if (bodyText.contains("permission")) { |
| 73 | + String helpfulMessage = buildFileUploadErrorMessage(); |
| 74 | + throw new AsyncAPIException( |
| 75 | + helpfulMessage, |
| 76 | + response.statusCode(), |
| 77 | + bodyBytes, |
| 78 | + response, |
| 79 | + null); |
| 80 | + } |
| 81 | + |
| 82 | + return response; |
| 83 | + }) |
| 84 | + .exceptionally(ex -> { |
| 85 | + if (ex instanceof AsyncAPIException) { |
| 86 | + throw (AsyncAPIException) ex; |
| 87 | + } |
| 88 | + return response; |
| 89 | + }); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (error != null) { |
| 94 | + return CompletableFuture.failedFuture(error); |
| 95 | + } |
| 96 | + return CompletableFuture.completedFuture(response); |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + private static String buildFileUploadErrorMessage() { |
| 101 | + return "File upload error: Agent runs require a two-step process for file inputs.\n\n" + |
| 102 | + "Step 1: Upload the file using chat.uploadFiles() to get a file ID:\n" + |
| 103 | + " UploadchatfilesResponse uploadResponse = sdk.client().chat().uploadFiles(\n" + |
| 104 | + " UploadChatFilesRequest.builder()\n" + |
| 105 | + " .files(List.of(new File(\"path/to/file.csv\")))\n" + |
| 106 | + " .build());\n" + |
| 107 | + " String fileId = uploadResponse.uploadChatFilesResponse()\n" + |
| 108 | + " .flatMap(UploadChatFilesResponse::files)\n" + |
| 109 | + " .map(files -> files.get(0))\n" + |
| 110 | + " .flatMap(ChatFile::id)\n" + |
| 111 | + " .orElseThrow();\n\n" + |
| 112 | + "Step 2: Pass the file ID (as a string) to agents.run() in the input map:\n" + |
| 113 | + " AgentRunCreate runRequest = AgentRunCreate.builder()\n" + |
| 114 | + " .agentId(\"your-agent-id\")\n" + |
| 115 | + " .input(Map.of(\"fileId\", fileId))\n" + |
| 116 | + " .build();\n" + |
| 117 | + " CreateAndWaitRunResponse response = sdk.client().agents().run(runRequest);\n\n" + |
| 118 | + "See examples/AgentWithFileUpload.java for a complete working example."; |
| 119 | + } |
| 120 | +} |
0 commit comments