genkitx-aws-bedrock
    Preparing search index...

    Function onCallGenkit

    Implementation of onCallGenkit

    • Creates a Lambda handler for a Genkit flow.

      This function wraps a Genkit flow to create an AWS Lambda handler that:

      • Handles CORS automatically
      • Supports ContextProvider for authentication/authorization
      • Provides proper error handling
      • Returns standardized response format

      Type Parameters

      • F extends Flow<ZodTypeAny, ZodTypeAny, ZodTypeAny>

      Parameters

      • flow: F

        The Genkit flow to wrap

      Returns CallableLambdaFunction<F>

      A Lambda handler function

      import { genkit, z } from 'genkit';
      import { onCallGenkit } from 'genkitx-aws-bedrock';
      import { awsBedrock, amazonNovaProV1 } from 'genkitx-aws-bedrock';

      const ai = genkit({
      plugins: [awsBedrock()],
      model: amazonNovaProV1(),
      });

      const myFlow = ai.defineFlow(
      { name: 'myFlow', inputSchema: z.string(), outputSchema: z.string() },
      async (input) => {
      const { text } = await ai.generate({ prompt: input });
      return text;
      }
      );

      export const handler = onCallGenkit(myFlow);
      import { UserFacingError, getContext } from 'genkit';
      import type { ContextProvider } from 'genkit/context';

      interface AuthContext {
      auth: { user: { id: string; name: string } };
      }

      const authProvider: ContextProvider<AuthContext> = async (req) => {
      const token = req.headers['authorization'];
      if (!token) {
      throw new UserFacingError('UNAUTHENTICATED', 'Missing auth token');
      }
      const user = await verifyToken(token);
      return { auth: { user } };
      };

      // In your flow, access context via getContext()
      const myFlow = ai.defineFlow(
      { name: 'myFlow', inputSchema: z.string(), outputSchema: z.string() },
      async (input, { context }) => {
      const { auth } = context;
      console.log('User:', auth.user.name);
      // ...
      }
      );

      export const handler = onCallGenkit(
      { contextProvider: authProvider },
      myFlow
      );
    • Creates a Lambda handler for a Genkit flow with options.

      Type Parameters

      • C extends ActionContext
      • F extends Flow<ZodTypeAny, ZodTypeAny, ZodTypeAny>

      Parameters

      • opts: LambdaOptions<C, FlowInput<F>> & { streaming: true }

        Configuration options for the Lambda handler

      • flow: F

        The Genkit flow to wrap

      Returns StreamifyHandler<APIGatewayProxyEventV2, void>

      A Lambda handler function, or a StreamifyHandler when streaming: true

    • Creates a Lambda handler for a Genkit flow.

      This function wraps a Genkit flow to create an AWS Lambda handler that:

      • Handles CORS automatically
      • Supports ContextProvider for authentication/authorization
      • Provides proper error handling
      • Returns standardized response format

      Type Parameters

      • C extends ActionContext
      • F extends Flow<ZodTypeAny, ZodTypeAny, ZodTypeAny>

      Parameters

      Returns CallableLambdaFunction<F>

      A Lambda handler function

      import { genkit, z } from 'genkit';
      import { onCallGenkit } from 'genkitx-aws-bedrock';
      import { awsBedrock, amazonNovaProV1 } from 'genkitx-aws-bedrock';

      const ai = genkit({
      plugins: [awsBedrock()],
      model: amazonNovaProV1(),
      });

      const myFlow = ai.defineFlow(
      { name: 'myFlow', inputSchema: z.string(), outputSchema: z.string() },
      async (input) => {
      const { text } = await ai.generate({ prompt: input });
      return text;
      }
      );

      export const handler = onCallGenkit(myFlow);
      import { UserFacingError, getContext } from 'genkit';
      import type { ContextProvider } from 'genkit/context';

      interface AuthContext {
      auth: { user: { id: string; name: string } };
      }

      const authProvider: ContextProvider<AuthContext> = async (req) => {
      const token = req.headers['authorization'];
      if (!token) {
      throw new UserFacingError('UNAUTHENTICATED', 'Missing auth token');
      }
      const user = await verifyToken(token);
      return { auth: { user } };
      };

      // In your flow, access context via getContext()
      const myFlow = ai.defineFlow(
      { name: 'myFlow', inputSchema: z.string(), outputSchema: z.string() },
      async (input, { context }) => {
      const { auth } = context;
      console.log('User:', auth.user.name);
      // ...
      }
      );

      export const handler = onCallGenkit(
      { contextProvider: authProvider },
      myFlow
      );