Skip to content

Conversation

@ssbushi
Copy link
Contributor

@ssbushi ssbushi commented Jan 13, 2026

JS Fix for #3968

Cfworker restrictions on:

  • Schema validation
  • Express APIs in the runtime (without export default?)
  • Virtual FS which affects runtime discovery

https://developers.cloudflare.com/workers/runtime-apis/nodejs/

Checklist (if applicable):

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ssbushi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the Genkit JavaScript SDK's adaptability to various runtime environments, particularly serverless platforms like Cloudflare Workers. It achieves this by providing an alternative Reflection API handler that adheres to standard Web API interfaces and by implementing a flexible schema validation mechanism that can operate without relying on dynamic code generation, which is often restricted in such environments. These changes broaden the deployment possibilities for Genkit applications, making them more versatile across different JavaScript runtimes.

Highlights

  • Cloudflare Worker Compatibility: Introduced a new createReflectionApiHandler function that is compatible with Web API Request/Response objects, enabling Genkit's Reflection API to run in serverless environments like Cloudflare Workers.
  • Schema Validation Adaptation: Added disableSchemaCodeGeneration to allow switching to an interpretation-based JSON schema validator (@cfworker/json-schema), which avoids the use of eval or new Function that are restricted in certain environments like Cloudflare Workers.
  • External Runtime Management: Enhanced the RuntimeManager to support specifying an external runtime URL via the GENKIT_RUNTIME_URL environment variable, including health checks for this external runtime.
  • Dependency Management: Removed the body-parser dependency and added @cfworker/json-schema as an optional dependency to support the new schema validation mode.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@ssbushi ssbushi requested a review from pavelgj January 13, 2026 16:55
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces compatibility with Cloudflare Worker environments by adding an alternative schema validation mechanism using @cfworker/json-schema and a new reflection API handler based on the standard Web API Request and Response objects. The changes are well-structured, including updates to dependency management and feature flags to enable this new mode. My review includes a couple of suggestions to refactor duplicated code in the new reflection API handler to improve maintainability.

Comment on lines 551 to 573
const actions = await registry.listResolvableActions();
const convertedActions: any = {};
Object.keys(actions).forEach((key) => {
const action = actions[key];
convertedActions[key] = {
key,
name: action.name,
description: action.description,
metadata: action.metadata,
};
if (action.inputSchema || action.inputJsonSchema) {
convertedActions[key].inputSchema = toJsonSchema({
schema: action.inputSchema,
jsonSchema: action.inputJsonSchema,
});
}
if (action.outputSchema || action.outputJsonSchema) {
convertedActions[key].outputSchema = toJsonSchema({
schema: action.outputSchema,
jsonSchema: action.outputJsonSchema,
});
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There's an opportunity to reduce code duplication here. The logic for converting actions to a serializable format is also present in the ReflectionServer class (lines 165-187).

To improve maintainability, you could extract this logic into a shared helper function that both ReflectionServer and createReflectionApiHandler can use.

For example:

function convertActionsForApi(actions: Record<string, Action>): any {
  const convertedActions: any = {};
  Object.keys(actions).forEach((key) => {
    const action = actions[key];
    convertedActions[key] = {
      key,
      name: action.name,
      description: action.description,
      metadata: action.metadata,
    };
    if (action.inputSchema || action.inputJsonSchema) {
      convertedActions[key].inputSchema = toJsonSchema({
        schema: action.inputSchema,
        jsonSchema: action.inputJsonSchema,
      });
    }
    if (action.outputSchema || action.outputJsonSchema) {
      convertedActions[key].outputSchema = toJsonSchema({
        schema: action.outputSchema,
        jsonSchema: action.outputJsonSchema,
      });
    }
  });
  return convertedActions;
}

Comment on lines 605 to 710
if (stream) {
const encoder = new TextEncoder();
const readable = new ReadableStream({
async start(controller) {
try {
const onTraceStartCallback = (metadata: any) => {
traceId = metadata.traceId;
activeActions.set(traceId!, {
abortController,
startTime: new Date(),
});
};

const result = await action.run(input, {
context,
onChunk: (chunk) => {
controller.enqueue(
encoder.encode(JSON.stringify(chunk) + '\n')
);
},
telemetryLabels,
onTraceStart: onTraceStartCallback,
abortSignal: abortController.signal,
});

await flushTracing();
controller.enqueue(
encoder.encode(
JSON.stringify({
result: result.result,
telemetry: { traceId: result.telemetry.traceId },
} as RunActionResponse)
)
);
controller.close();
} catch (err: any) {
const errorResponse: Status = {
code: isAbortError(err)
? StatusCodes.CANCELLED
: StatusCodes.INTERNAL,
message: isAbortError(err)
? 'Action was cancelled'
: err.message,
details: { stack: err.stack },
};
if (err.traceId) errorResponse.details.traceId = err.traceId;
controller.enqueue(
encoder.encode(
JSON.stringify({ error: errorResponse } as RunActionResponse)
)
);
controller.close();
} finally {
if (traceId) activeActions.delete(traceId);
}
},
});

return new Response(readable, {
headers: { 'Content-Type': 'text/plain', ...corsHeaders },
});
} else {
// Non-streaming
let result;
try {
result = await action.run(input, {
context,
telemetryLabels,
onTraceStart: (metadata) => {
traceId = metadata.traceId;
activeActions.set(traceId!, {
abortController,
startTime: new Date(),
});
},
abortSignal: abortController.signal,
});
await flushTracing();
} catch (err: any) {
const errorResponse: Status = {
code: isAbortError(err)
? StatusCodes.CANCELLED
: StatusCodes.INTERNAL,
message: isAbortError(err) ? 'Action was cancelled' : err.message,
details: { stack: err.stack, traceId },
};
return new Response(
JSON.stringify({ error: errorResponse } as RunActionResponse),
{
headers: { 'Content-Type': 'application/json', ...corsHeaders },
}
);
} finally {
if (traceId) activeActions.delete(traceId);
}

return new Response(
JSON.stringify({
result: result.result,
telemetry: { traceId: result.telemetry.traceId },
} as RunActionResponse),
{
headers: { 'Content-Type': 'application/json', ...corsHeaders },
}
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for handling streaming and non-streaming requests in runAction has significant code duplication. Specifically, the onTraceStart callback, error response creation, and the finally block for cleanup are very similar in both the if (stream) and else branches.

Consider refactoring this to reduce duplication and improve maintainability. For instance:

  1. Define the onTraceStart callback once before the if (stream) block.
  2. Create a helper function to build the error Status object.
  3. Structure the code to avoid duplicating the try...catch...finally blocks as much as possible.

This will make the code cleaner and easier to maintain in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant