-
Notifications
You must be signed in to change notification settings - Fork 151
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export { z } from 'zod'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,66 @@ | |
*/ | ||
|
||
import { Channel } from '@genkit-ai/core/async'; | ||
import { z } from '@genkit-ai/core/zod'; | ||
|
||
interface RunOptions { | ||
headers?: Record<string, string>; | ||
} | ||
|
||
export type RemoteAction<I = any, O = any, S = any> = (( | ||
input?: I, | ||
options?: RunOptions | ||
) => Promise<O>) & { | ||
stream(input?: I, opts?: RunOptions): StreamingResponse<O, S>; | ||
}; | ||
|
||
/** | ||
* Streaming response from an action. | ||
*/ | ||
export interface StreamingResponse<O = any, S = any> { | ||
/** Iterator over the streaming chunks. */ | ||
stream: AsyncIterable<S>; | ||
/** Final output of the action. */ | ||
output: Promise<O>; | ||
} | ||
|
||
/** | ||
* Defines a remote action which can be invoked as a function or streamed. | ||
*/ | ||
export function defineRemoteAction< | ||
I extends z.ZodTypeAny = z.ZodTypeAny, | ||
O extends z.ZodTypeAny = z.ZodTypeAny, | ||
S extends z.ZodTypeAny = z.ZodTypeAny, | ||
Comment on lines
+45
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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://...'
}); There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 import type { someFlow } from "./flows/someFlow";
remoteAction<typeof someFlow>(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @inlined managed to make this work in https://www.npmjs.com/package/@genkit-ai/next |
||
>(actionOptions: { | ||
url: string; | ||
headers?: Record<string, string>; | ||
inputSchema?: I; | ||
outputSchema?: O; | ||
streamSchema?: S; | ||
}): RemoteAction<z.infer<I>, z.infer<O>, z.infer<S>> { | ||
const action = ( | ||
input?: z.infer<I>, | ||
runOptions?: RunOptions | ||
): Promise<z.infer<O>> => { | ||
return runFlow({ | ||
url: actionOptions.url, | ||
headers: runOptions?.headers ?? actionOptions.headers, | ||
input, | ||
}); | ||
}; | ||
(action as RemoteAction<z.infer<I>, z.infer<O>, z.infer<S>>).stream = ( | ||
input?: I, | ||
runOptions?: RunOptions | ||
): StreamingResponse<z.infer<O>, z.infer<S>> => { | ||
return streamFlow({ | ||
url: actionOptions.url, | ||
headers: runOptions?.headers ?? actionOptions.headers, | ||
input, | ||
}); | ||
}; | ||
|
||
return action as RemoteAction<z.infer<I>, z.infer<O>, z.infer<S>>; | ||
} | ||
|
||
const __flowStreamDelimiter = '\n\n'; | ||
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should consider exporting z from this file as well - |
There was a problem hiding this comment.
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 handdefine
feels more consistent with the server side sdk..