-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontentEditorFlow.ts
More file actions
35 lines (31 loc) · 1.22 KB
/
contentEditorFlow.ts
File metadata and controls
35 lines (31 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import type { Flow } from '@genkit-ai/core';
import { ai } from '../config.js';
import { UserFacingError } from '../errors/UserFacingError.js';
import { ContentEditorInputSchema, ContentEditorOutputSchema } from '../schemas/index.js';
export const contentEditorFlow: Flow<typeof ContentEditorInputSchema, typeof ContentEditorOutputSchema> = ai.defineFlow(
{
name: 'contentEditorFlow',
inputSchema: ContentEditorInputSchema,
outputSchema: ContentEditorOutputSchema,
},
async (input, { context }) => {
const prompt = ai.prompt('content_editor');
const resp = context ? await prompt(input, { context }) : await prompt(input);
const output = resp?.output;
if (typeof output === 'string') {
return { edited: output };
}
if (output === null || output === undefined) {
throw new UserFacingError('Invalid model output for contentEditorFlow', {
details: { reason: 'No output returned from prompt', input },
});
}
const parsed = ContentEditorOutputSchema.safeParse(output);
if (!parsed.success) {
throw new UserFacingError('Schema validation failed for contentEditorFlow output', {
details: parsed.error.flatten(),
});
}
return parsed.data;
}
);