Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Card } from '../ui/Card';
import { KnowledgeItem } from '../../services/knowledgeBaseService';
import { knowledgeBaseService } from '../../services/knowledgeBaseService';
import { useToast } from '../../contexts/ToastContext';
import { EditableTags } from './EditableTags';

interface EditKnowledgeItemModalProps {
item: KnowledgeItem;
Expand All @@ -26,7 +27,9 @@ export const EditKnowledgeItemModal: React.FC<EditKnowledgeItemModalProps> = ({
const [formData, setFormData] = useState({
title: item.title,
description: item.metadata?.description || '',
tags: item.metadata?.tags || [],
});
const [isUpdatingTags, setIsUpdatingTags] = useState(false);

const isInGroup = Boolean(item.metadata?.group_name);

Expand Down Expand Up @@ -62,6 +65,15 @@ export const EditKnowledgeItemModal: React.FC<EditKnowledgeItemModalProps> = ({
if (formData.description !== (item.metadata?.description || '')) {
updates.description = formData.description;
}

// Only include tags if they have changed (using immutable comparison)
const originalTags = item.metadata?.tags || [];
const sortedFormTags = [...formData.tags].sort();
const sortedOriginalTags = [...originalTags].sort();
const tagsChanged = JSON.stringify(sortedFormTags) !== JSON.stringify(sortedOriginalTags);
if (tagsChanged) {
updates.tags = formData.tags;
}

Comment on lines +69 to 77
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Compare and persist normalized tags (trim + case-insensitive) to avoid dupes like "React" vs "react".

Aligns with the PR objective on tag normalization. Also persist normalized tags via updates.tags.

-      // Only include tags if they have changed (using immutable comparison)
-      const originalTags = item.metadata?.tags || [];
-      const sortedFormTags = [...formData.tags].sort();
-      const sortedOriginalTags = [...originalTags].sort();
-      const tagsChanged = JSON.stringify(sortedFormTags) !== JSON.stringify(sortedOriginalTags);
-      if (tagsChanged) {
-        updates.tags = formData.tags;
-      }
+      // Only include tags if they have changed (normalized, order-insensitive)
+      const normalizeTags = (ts: string[]) =>
+        Array.from(new Set(ts.map(t => t.trim()).filter(Boolean).map(t => t.toLowerCase())));
+      const originalTags = item.metadata?.tags || [];
+      const normForm = normalizeTags(formData.tags).sort();
+      const normOriginal = normalizeTags(originalTags).sort();
+      const tagsChanged = JSON.stringify(normForm) !== JSON.stringify(normOriginal);
+      if (tagsChanged) {
+        updates.tags = normForm;
+      }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Only include tags if they have changed (using immutable comparison)
const originalTags = item.metadata?.tags || [];
const sortedFormTags = [...formData.tags].sort();
const sortedOriginalTags = [...originalTags].sort();
const tagsChanged = JSON.stringify(sortedFormTags) !== JSON.stringify(sortedOriginalTags);
if (tagsChanged) {
updates.tags = formData.tags;
}
// Only include tags if they have changed (normalized, order-insensitive)
const normalizeTags = (ts: string[]) =>
Array.from(
new Set(
ts
.map(t => t.trim())
.filter(Boolean)
.map(t => t.toLowerCase())
)
);
const originalTags = item.metadata?.tags || [];
const normForm = normalizeTags(formData.tags).sort();
const normOriginal = normalizeTags(originalTags).sort();
const tagsChanged = JSON.stringify(normForm) !== JSON.stringify(normOriginal);
if (tagsChanged) {
updates.tags = normForm;
}
🤖 Prompt for AI Agents
In archon-ui-main/src/components/knowledge-base/EditKnowledgeItemModal.tsx
around lines 69 to 77, the current tag-change check compares raw tags and may
miss duplicates with casing/whitespace differences; normalize both formData.tags
and item.metadata.tags by trimming each tag and converting to lowercase, sort
the normalized arrays and compare their JSON strings, and when tagsChanged is
true set updates.tags to the normalized form (not the raw formData.tags) so
persisted tags are deduplicated and stored normalized.

await knowledgeBaseService.updateKnowledgeItem(item.source_id, updates);

Expand All @@ -76,6 +88,27 @@ export const EditKnowledgeItemModal: React.FC<EditKnowledgeItemModalProps> = ({
}
};

const handleTagsUpdate = async (tags: string[]) => {
setIsUpdatingTags(true);
try {
setFormData(prev => ({ ...prev, tags }));
} catch (error) {
const errorMessage = error instanceof Error
? `Failed to update tags: ${error.message}`
: 'Failed to update tags: Unknown error occurred';

console.error('Tag update error:', error);
showToast(errorMessage, 'error');
throw error;
} finally {
setIsUpdatingTags(false);
}
};

const handleTagError = (error: string) => {
showToast(error, 'error');
};

const handleRemoveFromGroup = async () => {
if (!isInGroup) return;

Expand Down Expand Up @@ -187,6 +220,22 @@ export const EditKnowledgeItemModal: React.FC<EditKnowledgeItemModalProps> = ({
</div>
</div>

{/* Tags field */}
<div className="w-full">
<label className="block text-gray-600 dark:text-zinc-400 text-sm mb-1.5">
Tags
</label>
<div className="backdrop-blur-md bg-gradient-to-b dark:from-white/10 dark:to-black/30 from-white/80 to-white/60 border dark:border-zinc-800/80 border-gray-200 rounded-md px-3 py-2 transition-all duration-200 focus-within:border-pink-500 focus-within:shadow-[0_0_15px_rgba(236,72,153,0.5)]">
<EditableTags
tags={formData.tags}
onTagsUpdate={handleTagsUpdate}
maxVisibleTags={10}
isUpdating={isUpdatingTags}
onError={handleTagError}
/>
</div>
</div>

{/* Group info and remove button */}
{isInGroup && (
<div className="bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg p-3">
Expand Down
Loading