diff --git a/apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-registry/resource-registry.tsx b/apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-registry/resource-registry.tsx index bf3d2db19f..41529075ad 100644 --- a/apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-registry/resource-registry.tsx +++ b/apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-registry/resource-registry.tsx @@ -138,6 +138,7 @@ const RESOURCE_INVALIDATORS: Record< knowledgebase: (qc, _wId, id) => { qc.invalidateQueries({ queryKey: knowledgeKeys.lists() }) qc.invalidateQueries({ queryKey: knowledgeKeys.detail(id) }) + qc.invalidateQueries({ queryKey: knowledgeKeys.tagDefinitions(id) }) }, } diff --git a/apps/sim/lib/copilot/resource-extraction.test.ts b/apps/sim/lib/copilot/resource-extraction.test.ts new file mode 100644 index 0000000000..bb1b167709 --- /dev/null +++ b/apps/sim/lib/copilot/resource-extraction.test.ts @@ -0,0 +1,85 @@ +/** + * @vitest-environment node + */ +import { describe, expect, it } from 'vitest' +import { extractResourcesFromToolResult } from './resource-extraction' + +describe('extractResourcesFromToolResult', () => { + it('uses the knowledge base id for knowledge_base tag mutations', () => { + const resources = extractResourcesFromToolResult( + 'knowledge_base', + { + operation: 'update_tag', + args: { + knowledgeBaseId: 'kb_123', + tagDefinitionId: 'tag_456', + }, + }, + { + success: true, + message: 'Tag updated successfully', + data: { + id: 'tag_456', + displayName: 'Priority', + fieldType: 'text', + }, + } + ) + + expect(resources).toEqual([ + { + type: 'knowledgebase', + id: 'kb_123', + title: 'Knowledge Base', + }, + ]) + }) + + it('uses knowledgeBaseId from the tool result when update_tag args omit it', () => { + const resources = extractResourcesFromToolResult( + 'knowledge_base', + { + operation: 'update_tag', + args: { + tagDefinitionId: 'tag_456', + }, + }, + { + success: true, + message: 'Tag updated successfully', + data: { + id: 'tag_456', + knowledgeBaseId: 'kb_123', + displayName: 'Priority', + fieldType: 'text', + }, + } + ) + + expect(resources).toEqual([ + { + type: 'knowledgebase', + id: 'kb_123', + title: 'Knowledge Base', + }, + ]) + }) + + it('does not create resources for read-only knowledge base tag operations', () => { + const resources = extractResourcesFromToolResult( + 'knowledge_base', + { + operation: 'list_tags', + args: { + knowledgeBaseId: 'kb_123', + }, + }, + { + success: true, + data: [], + } + ) + + expect(resources).toEqual([]) + }) +}) diff --git a/apps/sim/lib/copilot/resource-extraction.ts b/apps/sim/lib/copilot/resource-extraction.ts index 94a7e9e160..5eeda39f02 100644 --- a/apps/sim/lib/copilot/resource-extraction.ts +++ b/apps/sim/lib/copilot/resource-extraction.ts @@ -155,11 +155,13 @@ export function extractResourcesFromToolResult( case 'knowledge_base': { if (READ_ONLY_KB_OPS.has(getOperation(params) ?? '')) return [] + const args = asRecord(params?.args) const kbId = - (data.id as string) ?? + (args.knowledgeBaseId as string) ?? + (params?.knowledgeBaseId as string) ?? (result.knowledgeBaseId as string) ?? (data.knowledgeBaseId as string) ?? - (params?.knowledgeBaseId as string) + (data.id as string) if (kbId) { const kbName = (data.name as string) ?? (result.knowledgeBaseName as string) ?? 'Knowledge Base' diff --git a/apps/sim/lib/copilot/tools/server/knowledge/knowledge-base.ts b/apps/sim/lib/copilot/tools/server/knowledge/knowledge-base.ts index 54f217f3b1..a459a27195 100644 --- a/apps/sim/lib/copilot/tools/server/knowledge/knowledge-base.ts +++ b/apps/sim/lib/copilot/tools/server/knowledge/knowledge-base.ts @@ -28,6 +28,7 @@ import { deleteTagDefinition, getDocumentTagDefinitions, getNextAvailableSlot, + getTagDefinitionById, getTagUsageStats, updateTagDefinition, } from '@/lib/knowledge/tags/service' @@ -525,6 +526,7 @@ export const knowledgeBaseServerTool: BaseServerTool