Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions docs/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ All flows can define an `authPolicy` in their config. An auth policy is a functi
If this field is set, it is executed before the flow is invoked:

```ts
import { defineFlow, runFlow } from '@genkit-ai/flow';
import { defineFlow } from '@genkit-ai/flow';

export const selfSummaryFlow = defineFlow(
{
Expand All @@ -45,20 +45,18 @@ receive an error:

```ts
// Error: Authorization required.
await runFlow(selfSummaryFlow, { uid: 'abc-def' });
await selfSummaryFlow({ uid: 'abc-def' });

// Error: You may only summarize your own profile data.
await runFlow(
selfSummaryFlow,
await selfSummaryFlow(
{ uid: 'abc-def' },
{
withLocalAuthContext: { uid: 'hij-klm' },
}
);

// Success
await runFlow(
selfSummaryFlow,
await selfSummaryFlow(
{ uid: 'abc-def' },
{
withLocalAuthContext: { uid: 'abc-def' },
Expand Down Expand Up @@ -223,7 +221,7 @@ Firebase, you'll want to have a way to set up your own authorization checks
alongside the native flows. You have two options:

1. Use whatever server framework you like, and pass the auth context through via
`runFlow()` as noted above.
the flow call as noted above.

1. Use the built-in `startFlowsServer()` and provide Express middleware in the
flow config:
Expand Down Expand Up @@ -262,4 +260,4 @@ alongside the native flows. You have two options:
instructions.

Please note, if you go with (1), you the `middleware` configuration option will
be ignored by `runFlow()`.
be ignored by when the flow is invoked directly.
27 changes: 11 additions & 16 deletions docs/flows.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ When schema is specified Genkit will validate the schema for inputs and outputs.

## Running flows

Use the `runFlow` function to run the flow:
Run the flow by calling it directly like a normal function:

```js
const response = await runFlow(menuSuggestionFlow, 'French');
const response = await menuSuggestionFlow('French');
```

You can use the CLI to run flows as well:
Expand All @@ -63,35 +63,30 @@ genkit flow:run menuSuggestionFlow '"French"'
Here's a simple example of a flow that can stream values from a flow:

```javascript
export const menuSuggestionFlow = defineFlow(
export const menuSuggestionFlow = defineStreamingFlow(
{
name: 'menuSuggestionFlow',
streamSchema: z.string(),
},
async (restaurantTheme, streamingCallback) => {
if (streamingCallback) {
makeMenuItemSuggestionsAsync(restaurantTheme).subscribe((suggestion) => {
streamingCallback(suggestion);
});
}
makeMenuItemSuggestionsAsync(restaurantTheme).subscribe((suggestion) => {
streamingCallback(suggestion);
});
}
);
```

Note that `streamingCallback` can be undefined. It's only defined if the
invoking client is requesting streamed response.

To invoke a flow in streaming mode use `streamFlow` function:
To invoke a streaming flow, call it directly like a normal function and stream the results:

```javascript
const response = streamFlow(menuSuggestionFlow, 'French');
const { stream, output } = menuSuggestionFlow('French');

for await (const suggestion of response.stream()) {
for await (const suggestion of stream) {
console.log('suggestion', suggestion);
}
```

If the flow does not implement streaming `streamFlow` will behave identically to `runFlow`.
console.log('output', await output);
```

You can use the CLI to stream flows as well:

Expand Down
2 changes: 1 addition & 1 deletion docs/nextjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Node.js 20 or later.

```ts
export async function callMenuSuggestionFlow(theme: string) {
const flowResponse = await runFlow(menuSuggestionFlow, theme);
const flowResponse = await menuSuggestionFlow(theme);
console.log(flowResponse);
return flowResponse;
}
Expand Down
Loading