Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(js/client): added defineRemoteAction remote action client #1849

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pavelgj
Copy link
Collaborator

@pavelgj pavelgj commented Feb 5, 2025

import { defineRemoteAction } from 'genkit/beta/client';

const myFlow = defineRemoteAction({
  url: 'http://.../myFlow',
  inputSchema: z.object({ foo: z.string() }),
  outputSchema: z.string(),
  streamSchema: z.string(),
});

const { stream } = myFlow.stream(
  { foo: 'bar' },
  { headers: { authentication: getAuthToken() } }
);

for await (const chunk of stream) {
  console.log(chunk);
}

Checklist (if applicable):

Comment on lines +45 to +47
I extends z.ZodTypeAny = z.ZodTypeAny,
O extends z.ZodTypeAny = z.ZodTypeAny,
S extends z.ZodTypeAny = z.ZodTypeAny,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ugh, not sure what to do about the generics here. We're not actually doing validation here so in some ways I wonder about omitting the schemas for now and having the generics just be the realized types. You shouldn't have to import the zod schema and all of zod just to have the correct types on your remote action.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm super torn on this... my thinking was that if you share zod schema between the client and server, you can reused them here... but not sure how nice it is ergonomically... ideally you should be able to just take the flow (or action) reference and infer all the typing from it...

remoteAction({
  action: myFlow,
  url: 'https://...'
});

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah but right now that's not very feasible because trying to import myFlow into a browser environment will blow up the world. Although maybe with a pure type import...

import type { someFlow } from "./flows/someFlow";

remoteAction<typeof someFlow>();

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

/**
* Defines a remote action which can be invoked as a function or streamed.
*/
export function defineRemoteAction<
Copy link
Collaborator

Choose a reason for hiding this comment

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

should it just be remoteAction since there's no registry we're defining it into? but on the other hand define feels more consistent with the server side sdk..

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should consider exporting z from this file as well - import {z} from "genkit" will blow up Next.js if you do it from a file that runs in the browser

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.

2 participants