there should be no field for title, it should be automatically handled e
This commit is contained in:
parent
0326822621
commit
feb0187704
4 changed files with 63 additions and 10 deletions
|
|
@ -2,3 +2,4 @@ import { config } from 'dotenv';
|
|||
config();
|
||||
|
||||
import '@/ai/flows/suggest-tags.ts';
|
||||
import '@/ai/flows/suggest-title.ts';
|
||||
|
|
|
|||
56
src/ai/flows/suggest-title.ts
Normal file
56
src/ai/flows/suggest-title.ts
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
// This file uses server-side code.
|
||||
'use server';
|
||||
|
||||
/**
|
||||
* @fileOverview AI-powered title suggestion for prompts.
|
||||
*
|
||||
* - suggestTitle - A function that suggests a relevant title for a given prompt.
|
||||
* - SuggestTitleInput - The input type for the suggestTitle function.
|
||||
* - SuggestTitleOutput - The return type for the suggestTitle function.
|
||||
*/
|
||||
|
||||
import {ai} from '@/ai/genkit';
|
||||
import {z} from 'genkit';
|
||||
|
||||
const SuggestTitleInputSchema = z.object({
|
||||
promptText: z
|
||||
.string()
|
||||
.describe('The text content of the prompt for which a title is to be suggested.'),
|
||||
});
|
||||
export type SuggestTitleInput = z.infer<typeof SuggestTitleInputSchema>;
|
||||
|
||||
const SuggestTitleOutputSchema = z.object({
|
||||
title: z
|
||||
.string()
|
||||
.describe('The suggested title for the given prompt.'),
|
||||
});
|
||||
export type SuggestTitleOutput = z.infer<typeof SuggestTitleOutputSchema>;
|
||||
|
||||
export async function suggestTitle(input: SuggestTitleInput): Promise<SuggestTitleOutput> {
|
||||
return suggestTitleFlow(input);
|
||||
}
|
||||
|
||||
const suggestTitlePrompt = ai.definePrompt({
|
||||
name: 'suggestTitlePrompt',
|
||||
input: {schema: SuggestTitleInputSchema},
|
||||
output: {schema: SuggestTitleOutputSchema},
|
||||
prompt: `You are a title generation expert.
|
||||
|
||||
Given the following prompt, suggest a short, concise, and descriptive title (max 5 words).
|
||||
|
||||
Prompt: {{{promptText}}}
|
||||
|
||||
Title:`,
|
||||
});
|
||||
|
||||
const suggestTitleFlow = ai.defineFlow(
|
||||
{
|
||||
name: 'suggestTitleFlow',
|
||||
inputSchema: SuggestTitleInputSchema,
|
||||
outputSchema: SuggestTitleOutputSchema,
|
||||
},
|
||||
async input => {
|
||||
const {output} = await suggestTitlePrompt(input);
|
||||
return output!;
|
||||
}
|
||||
);
|
||||
|
|
@ -104,15 +104,10 @@ export function CreatePromptDialog() {
|
|||
<DialogHeader>
|
||||
<DialogTitle>Create a new prompt</DialogTitle>
|
||||
<DialogDescription>
|
||||
Craft your next masterpiece. Add notes and tags to keep it organized.
|
||||
Craft your next masterpiece. A title will be automatically generated. Add notes and tags to keep it organized.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<form ref={formRef} action={formAction} className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="title">Title</Label>
|
||||
<Input id="title" name="title" placeholder="e.g., Blog Post Ideas" required />
|
||||
{state.errors?.title && <p className="text-sm text-destructive">{state.errors.title[0]}</p>}
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="content">Prompt</Label>
|
||||
<Textarea
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import { revalidatePath } from 'next/cache';
|
|||
import { prompts as dbPrompts } from './data';
|
||||
import type { Prompt } from './types';
|
||||
import { suggestTags as suggestTagsFlow } from '@/ai/flows/suggest-tags';
|
||||
import { suggestTitle as suggestTitleFlow } from '@/ai/flows/suggest-title';
|
||||
import { z } from 'zod';
|
||||
|
||||
// In-memory store, mimicking a database.
|
||||
|
|
@ -24,7 +25,6 @@ export async function getPrompts(query: string = ''): Promise<Prompt[]> {
|
|||
}
|
||||
|
||||
const CreatePromptSchema = z.object({
|
||||
title: z.string().min(1, { message: 'Title is required.' }),
|
||||
content: z.string().min(1, { message: 'Content is required.' }),
|
||||
notes: z.string().optional(),
|
||||
tags: z.string(), // JSON string of tags array
|
||||
|
|
@ -33,7 +33,6 @@ const CreatePromptSchema = z.object({
|
|||
|
||||
export async function createPrompt(prevState: any, formData: FormData) {
|
||||
const validatedFields = CreatePromptSchema.safeParse({
|
||||
title: formData.get('title'),
|
||||
content: formData.get('content'),
|
||||
notes: formData.get('notes'),
|
||||
tags: formData.get('tags'),
|
||||
|
|
@ -46,9 +45,11 @@ export async function createPrompt(prevState: any, formData: FormData) {
|
|||
};
|
||||
}
|
||||
|
||||
const { title, content, notes, tags } = validatedFields.data;
|
||||
const { content, notes, tags } = validatedFields.data;
|
||||
|
||||
try {
|
||||
const { title } = await suggestTitleFlow({ promptText: content });
|
||||
|
||||
const newPrompt: Prompt = {
|
||||
id: Date.now().toString(),
|
||||
title,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue