Skip to content

Commit

Permalink
Fixed duplicate article tag creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikepsinn committed Sep 9, 2024
1 parent e4190cc commit 9edd81c
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions lib/agents/researcher/researcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,13 @@ export async function writeArticle(
prompt,
});

debugger;
console.log("Article generated successfully!", result.object);

const report = result.object as unknown as ReportOutput;
report.searchResults = searchResults;
report.generationOptions = options;

// find or create category
// Find or create category
let category = await prisma.articleCategory.findFirst({
where: {
name: {
Expand All @@ -171,17 +170,32 @@ export async function writeArticle(
}
});

if (!category) {
// Create the category if it doesn't exist
category = await prisma.articleCategory.create({
data: {
name: report.categoryName,
slug: slugify(report.categoryName)
}
});
if (!category) {
category = await prisma.articleCategory.create({
data: {
name: report.categoryName,
slug: slugify(report.categoryName)
}
});
}

// Get or create tags
const tagObjects = await Promise.all(report.tags.map(async (tagName) => {
const existingTag = await prisma.articleTag.findFirst({
where: { name: { equals: tagName, mode: 'insensitive' } }
});

if (existingTag) {
return existingTag;
} else {
return prisma.articleTag.create({
data: { name: tagName, slug: slugify(tagName) }
});
}
}));

const slug = slugify(report.title + '-prompt-' + topic);

const slug = slugify(report.title + '-prompt-' + topic);
// Save the article to the database
const savedArticle = await prisma.article.create({
data: {
Expand All @@ -196,10 +210,7 @@ export async function writeArticle(
userId: userId,
categoryId: category.id,
tags: {
connectOrCreate: report.tags.map(tag => ({
where: { name: tag },
create: { name: tag, slug: slugify(tag) }
}))
connect: tagObjects.map(tag => ({ id: tag.id }))
},
sources: {
create: report.sources.map(source => ({
Expand Down

0 comments on commit 9edd81c

Please sign in to comment.