From 10a8ab71c15de45be89dde3f90a6147d0dd1821a Mon Sep 17 00:00:00 2001 From: Theodore Li Date: Wed, 25 Mar 2026 09:59:29 -0700 Subject: [PATCH 1/2] fix(ui): fix kb id extraction logic for resource, sync tags --- .../resource-registry/resource-registry.tsx | 1 + .../lib/copilot/resource-extraction.test.ts | 55 +++++++++++++++++++ apps/sim/lib/copilot/resource-extraction.ts | 6 +- 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 apps/sim/lib/copilot/resource-extraction.test.ts 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..c77459f57f --- /dev/null +++ b/apps/sim/lib/copilot/resource-extraction.test.ts @@ -0,0 +1,55 @@ +/** + * @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('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' From 6968e42cba56e6a8a6e7a9e929e80f2712f1cb72 Mon Sep 17 00:00:00 2001 From: Theodore Li Date: Wed, 25 Mar 2026 10:19:41 -0700 Subject: [PATCH 2/2] Pass knowledge base id back on edit tag --- .../lib/copilot/resource-extraction.test.ts | 30 +++++++++++++++++++ .../tools/server/knowledge/knowledge-base.ts | 13 ++++++++ 2 files changed, 43 insertions(+) diff --git a/apps/sim/lib/copilot/resource-extraction.test.ts b/apps/sim/lib/copilot/resource-extraction.test.ts index c77459f57f..bb1b167709 100644 --- a/apps/sim/lib/copilot/resource-extraction.test.ts +++ b/apps/sim/lib/copilot/resource-extraction.test.ts @@ -35,6 +35,36 @@ describe('extractResourcesFromToolResult', () => { ]) }) + 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', 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