|
| 1 | +import type { MothershipResource, MothershipResourceType } from '@/lib/copilot/resource-types' |
| 2 | + |
| 3 | +type ChatResource = MothershipResource |
| 4 | +type ResourceType = MothershipResourceType |
| 5 | + |
| 6 | +const RESOURCE_TOOL_NAMES = new Set([ |
| 7 | + 'user_table', |
| 8 | + 'workspace_file', |
| 9 | + 'create_workflow', |
| 10 | + 'edit_workflow', |
| 11 | + 'function_execute', |
| 12 | + 'knowledge_base', |
| 13 | + 'knowledge', |
| 14 | +]) |
| 15 | + |
| 16 | +export function isResourceToolName(toolName: string): boolean { |
| 17 | + return RESOURCE_TOOL_NAMES.has(toolName) |
| 18 | +} |
| 19 | + |
| 20 | +function asRecord(value: unknown): Record<string, unknown> { |
| 21 | + return value && typeof value === 'object' ? (value as Record<string, unknown>) : {} |
| 22 | +} |
| 23 | + |
| 24 | +function getOperation(params: Record<string, unknown> | undefined): string | undefined { |
| 25 | + const args = asRecord(params?.args) |
| 26 | + return (args.operation ?? params?.operation) as string | undefined |
| 27 | +} |
| 28 | + |
| 29 | +const READ_ONLY_TABLE_OPS = new Set(['get', 'get_schema', 'get_row', 'query_rows']) |
| 30 | +const READ_ONLY_KB_OPS = new Set(['get', 'query', 'list_tags', 'get_tag_usage']) |
| 31 | +const READ_ONLY_KNOWLEDGE_ACTIONS = new Set(['listed', 'queried']) |
| 32 | + |
| 33 | +/** |
| 34 | + * Extracts resource descriptors from a tool execution result. |
| 35 | + * Returns one or more resources for tools that create/modify workspace entities. |
| 36 | + * Read-only operations are excluded to avoid unnecessary cache invalidation. |
| 37 | + */ |
| 38 | +export function extractResourcesFromToolResult( |
| 39 | + toolName: string, |
| 40 | + params: Record<string, unknown> | undefined, |
| 41 | + output: unknown |
| 42 | +): ChatResource[] { |
| 43 | + if (!isResourceToolName(toolName)) return [] |
| 44 | + |
| 45 | + const result = asRecord(output) |
| 46 | + const data = asRecord(result.data) |
| 47 | + |
| 48 | + switch (toolName) { |
| 49 | + case 'user_table': { |
| 50 | + if (READ_ONLY_TABLE_OPS.has(getOperation(params) ?? '')) return [] |
| 51 | + |
| 52 | + if (result.tableId) { |
| 53 | + return [ |
| 54 | + { |
| 55 | + type: 'table', |
| 56 | + id: result.tableId as string, |
| 57 | + title: (result.tableName as string) || 'Table', |
| 58 | + }, |
| 59 | + ] |
| 60 | + } |
| 61 | + if (result.fileId) { |
| 62 | + return [ |
| 63 | + { |
| 64 | + type: 'file', |
| 65 | + id: result.fileId as string, |
| 66 | + title: (result.fileName as string) || 'File', |
| 67 | + }, |
| 68 | + ] |
| 69 | + } |
| 70 | + const table = asRecord(data.table) |
| 71 | + if (table.id) { |
| 72 | + return [{ type: 'table', id: table.id as string, title: (table.name as string) || 'Table' }] |
| 73 | + } |
| 74 | + const args = asRecord(params?.args) |
| 75 | + const tableId = |
| 76 | + (data.tableId as string) ?? (args.tableId as string) ?? (params?.tableId as string) |
| 77 | + if (tableId) { |
| 78 | + return [ |
| 79 | + { type: 'table', id: tableId as string, title: (data.tableName as string) || 'Table' }, |
| 80 | + ] |
| 81 | + } |
| 82 | + return [] |
| 83 | + } |
| 84 | + |
| 85 | + case 'workspace_file': { |
| 86 | + const file = asRecord(data.file) |
| 87 | + if (file.id) { |
| 88 | + return [{ type: 'file', id: file.id as string, title: (file.name as string) || 'File' }] |
| 89 | + } |
| 90 | + const fileId = (data.fileId as string) ?? (data.id as string) |
| 91 | + if (fileId) { |
| 92 | + const fileName = (data.fileName as string) || (data.name as string) || 'File' |
| 93 | + return [{ type: 'file', id: fileId, title: fileName }] |
| 94 | + } |
| 95 | + return [] |
| 96 | + } |
| 97 | + |
| 98 | + case 'function_execute': { |
| 99 | + if (result.tableId) { |
| 100 | + return [ |
| 101 | + { |
| 102 | + type: 'table', |
| 103 | + id: result.tableId as string, |
| 104 | + title: (result.tableName as string) || 'Table', |
| 105 | + }, |
| 106 | + ] |
| 107 | + } |
| 108 | + if (result.fileId) { |
| 109 | + return [ |
| 110 | + { |
| 111 | + type: 'file', |
| 112 | + id: result.fileId as string, |
| 113 | + title: (result.fileName as string) || 'File', |
| 114 | + }, |
| 115 | + ] |
| 116 | + } |
| 117 | + return [] |
| 118 | + } |
| 119 | + |
| 120 | + case 'create_workflow': |
| 121 | + case 'edit_workflow': { |
| 122 | + const workflowId = |
| 123 | + (result.workflowId as string) ?? |
| 124 | + (data.workflowId as string) ?? |
| 125 | + (params?.workflowId as string) |
| 126 | + if (workflowId) { |
| 127 | + const workflowName = |
| 128 | + (result.workflowName as string) ?? |
| 129 | + (data.workflowName as string) ?? |
| 130 | + (params?.workflowName as string) ?? |
| 131 | + 'Workflow' |
| 132 | + return [{ type: 'workflow', id: workflowId, title: workflowName }] |
| 133 | + } |
| 134 | + return [] |
| 135 | + } |
| 136 | + |
| 137 | + case 'knowledge_base': { |
| 138 | + if (READ_ONLY_KB_OPS.has(getOperation(params) ?? '')) return [] |
| 139 | + |
| 140 | + const kbId = |
| 141 | + (data.id as string) ?? |
| 142 | + (result.knowledgeBaseId as string) ?? |
| 143 | + (data.knowledgeBaseId as string) ?? |
| 144 | + (params?.knowledgeBaseId as string) |
| 145 | + if (kbId) { |
| 146 | + const kbName = |
| 147 | + (data.name as string) ?? (result.knowledgeBaseName as string) ?? 'Knowledge Base' |
| 148 | + return [{ type: 'knowledgebase', id: kbId, title: kbName }] |
| 149 | + } |
| 150 | + return [] |
| 151 | + } |
| 152 | + |
| 153 | + case 'knowledge': { |
| 154 | + const action = data.action as string | undefined |
| 155 | + if (READ_ONLY_KNOWLEDGE_ACTIONS.has(action ?? '')) return [] |
| 156 | + |
| 157 | + const kbArray = data.knowledge_bases as Array<Record<string, unknown>> | undefined |
| 158 | + if (!Array.isArray(kbArray)) return [] |
| 159 | + const resources: ChatResource[] = [] |
| 160 | + for (const kb of kbArray) { |
| 161 | + const id = kb.id as string | undefined |
| 162 | + if (id) { |
| 163 | + resources.push({ |
| 164 | + type: 'knowledgebase', |
| 165 | + id, |
| 166 | + title: (kb.name as string) || 'Knowledge Base', |
| 167 | + }) |
| 168 | + } |
| 169 | + } |
| 170 | + return resources |
| 171 | + } |
| 172 | + |
| 173 | + default: |
| 174 | + return [] |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +const DELETE_CAPABLE_TOOL_RESOURCE_TYPE: Record<string, ResourceType> = { |
| 179 | + delete_workflow: 'workflow', |
| 180 | + workspace_file: 'file', |
| 181 | + user_table: 'table', |
| 182 | + knowledge_base: 'knowledgebase', |
| 183 | +} |
| 184 | + |
| 185 | +export function hasDeleteCapability(toolName: string): boolean { |
| 186 | + return toolName in DELETE_CAPABLE_TOOL_RESOURCE_TYPE |
| 187 | +} |
| 188 | + |
| 189 | +/** |
| 190 | + * Extracts resource descriptors from a tool execution result when the tool |
| 191 | + * performed a deletion. Returns one or more deleted resources for tools that |
| 192 | + * destroy workspace entities. |
| 193 | + */ |
| 194 | +export function extractDeletedResourcesFromToolResult( |
| 195 | + toolName: string, |
| 196 | + params: Record<string, unknown> | undefined, |
| 197 | + output: unknown |
| 198 | +): ChatResource[] { |
| 199 | + const resourceType = DELETE_CAPABLE_TOOL_RESOURCE_TYPE[toolName] |
| 200 | + if (!resourceType) return [] |
| 201 | + |
| 202 | + const result = asRecord(output) |
| 203 | + const data = asRecord(result.data) |
| 204 | + const args = asRecord(params?.args) |
| 205 | + const operation = (args.operation ?? params?.operation) as string | undefined |
| 206 | + |
| 207 | + switch (toolName) { |
| 208 | + case 'delete_workflow': { |
| 209 | + const workflowId = (result.workflowId as string) ?? (params?.workflowId as string) |
| 210 | + if (workflowId && result.deleted) { |
| 211 | + return [ |
| 212 | + { type: resourceType, id: workflowId, title: (result.name as string) || 'Workflow' }, |
| 213 | + ] |
| 214 | + } |
| 215 | + return [] |
| 216 | + } |
| 217 | + |
| 218 | + case 'workspace_file': { |
| 219 | + if (operation !== 'delete') return [] |
| 220 | + const fileId = (data.id as string) ?? (args.fileId as string) |
| 221 | + if (fileId) { |
| 222 | + return [{ type: resourceType, id: fileId, title: (data.name as string) || 'File' }] |
| 223 | + } |
| 224 | + return [] |
| 225 | + } |
| 226 | + |
| 227 | + case 'user_table': { |
| 228 | + if (operation !== 'delete') return [] |
| 229 | + const tableId = (args.tableId as string) ?? (params?.tableId as string) |
| 230 | + if (tableId) { |
| 231 | + return [{ type: resourceType, id: tableId, title: 'Table' }] |
| 232 | + } |
| 233 | + return [] |
| 234 | + } |
| 235 | + |
| 236 | + case 'knowledge_base': { |
| 237 | + if (operation !== 'delete') return [] |
| 238 | + const kbId = (data.id as string) ?? (args.knowledgeBaseId as string) |
| 239 | + if (kbId) { |
| 240 | + return [{ type: resourceType, id: kbId, title: (data.name as string) || 'Knowledge Base' }] |
| 241 | + } |
| 242 | + return [] |
| 243 | + } |
| 244 | + |
| 245 | + default: |
| 246 | + return [] |
| 247 | + } |
| 248 | +} |
0 commit comments