feat: implement ADK wrapper functions for Agent Engine sessions#1252
feat: implement ADK wrapper functions for Agent Engine sessions#1252
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a Firebase Cloud Functions wrapper proxy for Google Cloud's Agent Engine (ADK), providing secure, authenticated access to reasoning engine endpoints for client applications. The implementation includes session management, memory operations, and streaming query support. Key feedback includes correcting the Node.js runtime version in package.json, fixing a bug in adk.ts where streaming response data was incorrectly parsed as a string instead of a Protobuf value, and improving API error handling by replacing generic errors with structured HttpsError responses for unauthenticated requests.
| "logs": "firebase functions:log" | ||
| }, | ||
| "engines": { | ||
| "node": "24" |
| const parsed = JSON.parse(response.data.toString('utf8')); | ||
| chunks.push(parsed); | ||
| if (parsed.content && parsed.content.parts) { | ||
| fullText += parsed.content.parts.map((p: any) => p.text || "").join(""); | ||
| } |
There was a problem hiding this comment.
The response.data field in the aiplatform SDK's stream response is a google.protobuf.Value object, not a Buffer. Using JSON.parse(response.data.toString('utf8')) will fail. Use helpers.fromValue(response.data) to correctly parse the Protobuf value into a JavaScript object. Additionally, using optional chaining ensures the code is resilient to unexpected response structures.
| const parsed = JSON.parse(response.data.toString('utf8')); | |
| chunks.push(parsed); | |
| if (parsed.content && parsed.content.parts) { | |
| fullText += parsed.content.parts.map((p: any) => p.text || "").join(""); | |
| } | |
| const parsed = helpers.fromValue(response.data) as any; | |
| chunks.push(parsed); | |
| if (parsed?.content?.parts) { | |
| fullText += parsed.content.parts.map((p: any) => p.text || "").join(""); | |
| } |
| @@ -0,0 +1,19 @@ | |||
| import { onCall } from "firebase-functions/v2/https"; | |||
| if (!uid) { | ||
| throw new Error("Unauthorized"); | ||
| } |
There was a problem hiding this comment.
Use HttpsError with the unauthenticated code instead of a generic Error. Generic errors are returned to the client as INTERNAL (500), which hides the actual cause and is poor practice for API design. This pattern should be followed in all other endpoint files as well.
| if (!uid) { | |
| throw new Error("Unauthorized"); | |
| } | |
| if (!uid) { | |
| throw new HttpsError("unauthenticated", "The function must be called while authenticated."); | |
| } |
No description provided.