Skip to content
Open
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
24 changes: 11 additions & 13 deletions js/testapps/express/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ const ai = genkit({
export const jokeFlow = ai.defineFlow(
{ name: 'jokeFlow', inputSchema: z.string(), outputSchema: z.string() },
async (subject, { context, sendChunk }) => {
if (context!.auth!.username != 'Ali Baba') {
throw new UserFacingError('PERMISSION_DENIED', context!.auth!.username!);
if (context?.auth?.username && context.auth.username !== 'Ali Baba') {
throw new UserFacingError('PERMISSION_DENIED', context.auth.username);
}
return await ai.run('call-llm', async () => {
const llmResponse = await ai.generate({
Expand Down Expand Up @@ -95,18 +95,16 @@ const acls: Record<string, string> = {
jokeFlow: 'Ali Baba',
};

// curl http://localhost:5000/jokeFlow?stream=true -d '{"data": "banana"}' -H "content-type: application/json" -H "authorization: open sesame"
ai.flows.forEach((f) => {
app.post(
`/${f.name}`,
expressHandler(f, { contextProvider: auth(acls[f.name]) })
);
});
// curl http://localhost:8080/jokeFlow?stream=true -d '{"data": "banana"}' -H "content-type: application/json" -H "authorization: open sesame"
app.post(
'/jokeFlow',
expressHandler(jokeFlow, { contextProvider: auth(acls['jokeFlow']) })
);

// curl http://localhost:5000/jokeHandler?stream=true -d '{"data": "banana"}' -H "content-type: application/json"
// curl http://localhost:8080/jokeHandler?stream=true -d '{"data": "banana"}' -H "content-type: application/json"
app.post('/jokeHandler', expressHandler(jokeFlow));

// curl http://localhost:5000/jokeWithFlow?subject=banana
// curl http://localhost:8080/jokeWithFlow?subject=banana
app.get('/jokeWithFlow', async (req: Request, res: Response) => {
const subject = req.query['subject']?.toString();
if (!subject) {
Expand All @@ -116,7 +114,7 @@ app.get('/jokeWithFlow', async (req: Request, res: Response) => {
res.send(await jokeFlow(subject));
});

// curl http://localhost:5000/jokeStream?subject=banana
// curl http://localhost:8080/jokeStream?subject=banana
app.get('/jokeStream', async (req: Request, res: Response) => {
const subject = req.query['subject']?.toString();
if (!subject) {
Expand All @@ -142,7 +140,7 @@ app.get('/jokeStream', async (req: Request, res: Response) => {
res.end();
});

const port = process.env.PORT || 5000;
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});