feat: forward rawRequest and disconnect AbortSignal into onCallGenkit…#1891
feat: forward rawRequest and disconnect AbortSignal into onCallGenkit…#1891IzaakGough wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces CALLABLE_RAW_REQUEST and CALLABLE_RESPONSE_SIGNAL symbols to expose the underlying raw request and abort signal within the Genkit action context in onCallGenkit. Corresponding tests were added to ensure these values are correctly forwarded and that the abort signal functions as expected upon client disconnection. Feedback was provided to use optional chaining when accessing the response signal to avoid potential runtime errors in environments where the response object might be missing.
| } = {}; | ||
| copyIfPresent(context, req, "auth", "app", "instanceIdToken"); | ||
| context[CALLABLE_RAW_REQUEST] = req.rawRequest; | ||
| context[CALLABLE_RESPONSE_SIGNAL] = res.signal; |
There was a problem hiding this comment.
The res parameter in the onCall handler is marked as optional in its type definition. Accessing res.signal directly could lead to a runtime error if the handler is invoked without a second argument (e.g., in certain unit testing scenarios). Using optional chaining res?.signal is safer and more idiomatic here.
context[CALLABLE_RESPONSE_SIGNAL] = res?.signal;
Summary
Fixes #1888
Expose the underlying callable raw request and a client-disconnect
AbortSignalto Genkit actions invoked viahttps.onCallGenkit, so Genkit flows can read request-level details and reliably stop work when the client disconnects (especially for SSE streaming).Changes
src/common/providers/https.ts:src/v2/providers/https.tsso callers can reference the same symbol instances.onCallGenkitto populate the Genkit action context for both JSON and streaming callables:Why
res.signalprovides a standardAbortSignalthat aborts when the client disconnects.Tests
Updated
spec/v2/providers/https.spec.tsto verify:rawRequestandAbortSignalare forwarded into Genkit context for JSON requests.AbortSignalis aborted when the client disconnects (req.emit("close")), and the handler stops accordingly.Usage
Inside a Genkit action invoked via
https.onCallGenkit, read:opts.context[https.CALLABLE_RAW_REQUEST](Express Request)opts.context[https.CALLABLE_RESPONSE_SIGNAL](AbortSignal)Verification
Before the fix
Before the fix running the MRE produces the following logs:
These continue until:
Known Limitation / Follow-Up
There is still an underlying timing/race issue around disconnect vs. in-flight work completion
It appears to be governed by upstream behavior (request/stream lifecycle and/or Genkit action execution), and isn’t something we can reliably fix within firebase-functions alone; addressing it likely requires changes in the upstream caller/runtime (or Genkit).