chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
/**
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { extractDeletedResourcesFromToolResult, extractResourcesFromToolResult } from './extraction'
|
||||
|
||||
describe('extractResourcesFromToolResult', () => {
|
||||
it('extracts file resources from create_file results', () => {
|
||||
const resources = extractResourcesFromToolResult(
|
||||
'create_file',
|
||||
{
|
||||
fileName: 'notes.md',
|
||||
},
|
||||
{
|
||||
success: true,
|
||||
message: 'File "notes.md" created successfully',
|
||||
data: {
|
||||
id: 'file_123',
|
||||
name: 'notes.md',
|
||||
contentType: 'text/markdown',
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(resources).toEqual([
|
||||
{
|
||||
type: 'file',
|
||||
id: 'file_123',
|
||||
title: 'notes.md',
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
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([])
|
||||
})
|
||||
|
||||
it.each([
|
||||
['generate_video', 'ad-clip.mp4'],
|
||||
['generate_audio', 'voiceover.mp3'],
|
||||
['ffmpeg', 'final-ad.mp4'],
|
||||
])('auto-opens the generated file from %s results', (toolName, fileName) => {
|
||||
const resources = extractResourcesFromToolResult(
|
||||
toolName,
|
||||
{},
|
||||
{
|
||||
success: true,
|
||||
message: `Saved at "files/${fileName}"`,
|
||||
fileId: 'file_media_123',
|
||||
fileName,
|
||||
}
|
||||
)
|
||||
|
||||
expect(resources).toEqual([{ type: 'file', id: 'file_media_123', title: fileName }])
|
||||
})
|
||||
|
||||
it('does not create a resource for ffmpeg probe (no file written)', () => {
|
||||
const resources = extractResourcesFromToolResult(
|
||||
'ffmpeg',
|
||||
{ operation: 'probe' },
|
||||
{
|
||||
success: true,
|
||||
message: 'Probed media',
|
||||
probe: { durationSeconds: 12.5, width: 1080, height: 1920 },
|
||||
}
|
||||
)
|
||||
|
||||
expect(resources).toEqual([])
|
||||
})
|
||||
|
||||
it('auto-opens a scheduledtask resource from manage_scheduled_task create results', () => {
|
||||
const resources = extractResourcesFromToolResult(
|
||||
'manage_scheduled_task',
|
||||
{ operation: 'create', args: { title: 'Daily Report' } },
|
||||
{ jobId: 'sched_123', title: 'Daily Report', message: 'Job created successfully.' }
|
||||
)
|
||||
|
||||
expect(resources).toEqual([{ type: 'scheduledtask', id: 'sched_123', title: 'Daily Report' }])
|
||||
})
|
||||
|
||||
it('auto-opens a scheduledtask resource on update, falling back to the args title', () => {
|
||||
const resources = extractResourcesFromToolResult(
|
||||
'manage_scheduled_task',
|
||||
{ operation: 'update', args: { jobId: 'sched_123', title: 'Renamed Task' } },
|
||||
{ jobId: 'sched_123', updated: ['title'], message: 'Job updated successfully' }
|
||||
)
|
||||
|
||||
expect(resources).toEqual([{ type: 'scheduledtask', id: 'sched_123', title: 'Renamed Task' }])
|
||||
})
|
||||
|
||||
it('does not auto-open for read-only manage_scheduled_task operations', () => {
|
||||
expect(
|
||||
extractResourcesFromToolResult(
|
||||
'manage_scheduled_task',
|
||||
{ operation: 'list' },
|
||||
{ jobs: [], count: 0 }
|
||||
)
|
||||
).toEqual([])
|
||||
expect(
|
||||
extractResourcesFromToolResult(
|
||||
'manage_scheduled_task',
|
||||
{ operation: 'get', args: { jobId: 'sched_123' } },
|
||||
{ id: 'sched_123', title: 'Daily Report' }
|
||||
)
|
||||
).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('extractDeletedResourcesFromToolResult', () => {
|
||||
it('removes scheduledtask resources on manage_scheduled_task delete', () => {
|
||||
const resources = extractDeletedResourcesFromToolResult(
|
||||
'manage_scheduled_task',
|
||||
{ operation: 'delete', args: { jobIds: ['sched_1', 'sched_2'] } },
|
||||
{ deleted: ['sched_1', 'sched_2'], notFound: [] }
|
||||
)
|
||||
|
||||
expect(resources).toEqual([
|
||||
{ type: 'scheduledtask', id: 'sched_1', title: 'Scheduled Task' },
|
||||
{ type: 'scheduledtask', id: 'sched_2', title: 'Scheduled Task' },
|
||||
])
|
||||
})
|
||||
|
||||
it('does not remove anything for non-delete manage_scheduled_task ops', () => {
|
||||
expect(
|
||||
extractDeletedResourcesFromToolResult(
|
||||
'manage_scheduled_task',
|
||||
{ operation: 'update', args: { jobId: 'sched_1' } },
|
||||
{ jobId: 'sched_1', updated: ['title'] }
|
||||
)
|
||||
).toEqual([])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,320 @@
|
||||
import {
|
||||
CreateFile,
|
||||
CreateWorkflow,
|
||||
DeleteWorkflow,
|
||||
DownloadToWorkspaceFile,
|
||||
EditWorkflow,
|
||||
Ffmpeg,
|
||||
FunctionExecute,
|
||||
GenerateAudio,
|
||||
GenerateImage,
|
||||
GenerateVideo,
|
||||
Knowledge,
|
||||
KnowledgeBase,
|
||||
ManageScheduledTask,
|
||||
UserTable,
|
||||
WorkspaceFile,
|
||||
} from '@/lib/copilot/generated/tool-catalog-v1'
|
||||
import type { MothershipResource, MothershipResourceType } from './types'
|
||||
|
||||
type ChatResource = MothershipResource
|
||||
type ResourceType = MothershipResourceType
|
||||
|
||||
const RESOURCE_TOOL_NAMES: Set<string> = new Set([
|
||||
UserTable.id,
|
||||
CreateFile.id,
|
||||
WorkspaceFile.id,
|
||||
DownloadToWorkspaceFile.id,
|
||||
CreateWorkflow.id,
|
||||
EditWorkflow.id,
|
||||
FunctionExecute.id,
|
||||
KnowledgeBase.id,
|
||||
Knowledge.id,
|
||||
ManageScheduledTask.id,
|
||||
GenerateImage.id,
|
||||
GenerateVideo.id,
|
||||
GenerateAudio.id,
|
||||
Ffmpeg.id,
|
||||
])
|
||||
|
||||
export function isResourceToolName(toolName: string): boolean {
|
||||
return RESOURCE_TOOL_NAMES.has(toolName)
|
||||
}
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === 'object' ? (value as Record<string, unknown>) : {}
|
||||
}
|
||||
|
||||
function getOperation(params: Record<string, unknown> | undefined): string | undefined {
|
||||
const args = asRecord(params?.args)
|
||||
return (args.operation ?? params?.operation) as string | undefined
|
||||
}
|
||||
|
||||
function getWorkspaceFileTarget(
|
||||
params: Record<string, unknown> | undefined
|
||||
): Record<string, unknown> {
|
||||
return asRecord(params?.target)
|
||||
}
|
||||
|
||||
const READ_ONLY_TABLE_OPS = new Set(['get', 'get_schema', 'get_row', 'query_rows'])
|
||||
const READ_ONLY_KB_OPS = new Set(['get', 'query', 'list_tags', 'get_tag_usage'])
|
||||
const READ_ONLY_KNOWLEDGE_ACTIONS = new Set(['listed', 'queried'])
|
||||
|
||||
/**
|
||||
* Extracts resource descriptors from a tool execution result.
|
||||
* Returns one or more resources for tools that create/modify workspace entities.
|
||||
* Read-only operations are excluded to avoid unnecessary cache invalidation.
|
||||
*/
|
||||
export function extractResourcesFromToolResult(
|
||||
toolName: string,
|
||||
params: Record<string, unknown> | undefined,
|
||||
output: unknown
|
||||
): ChatResource[] {
|
||||
if (!isResourceToolName(toolName)) return []
|
||||
|
||||
const result = asRecord(output)
|
||||
const data = asRecord(result.data)
|
||||
|
||||
switch (toolName) {
|
||||
case UserTable.id: {
|
||||
if (READ_ONLY_TABLE_OPS.has(getOperation(params) ?? '')) return []
|
||||
|
||||
if (result.tableId) {
|
||||
return [
|
||||
{
|
||||
type: 'table',
|
||||
id: result.tableId as string,
|
||||
title: (result.tableName as string) || 'Table',
|
||||
},
|
||||
]
|
||||
}
|
||||
if (result.fileId) {
|
||||
return [
|
||||
{
|
||||
type: 'file',
|
||||
id: result.fileId as string,
|
||||
title: (result.fileName as string) || 'File',
|
||||
},
|
||||
]
|
||||
}
|
||||
const table = asRecord(data.table)
|
||||
if (table.id) {
|
||||
return [{ type: 'table', id: table.id as string, title: (table.name as string) || 'Table' }]
|
||||
}
|
||||
const args = asRecord(params?.args)
|
||||
const tableId =
|
||||
(data.tableId as string) ?? (args.tableId as string) ?? (params?.tableId as string)
|
||||
if (tableId) {
|
||||
return [
|
||||
{ type: 'table', id: tableId as string, title: (data.tableName as string) || 'Table' },
|
||||
]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
case CreateFile.id:
|
||||
case WorkspaceFile.id: {
|
||||
const file = asRecord(data.file)
|
||||
if (file.id) {
|
||||
return [{ type: 'file', id: file.id as string, title: (file.name as string) || 'File' }]
|
||||
}
|
||||
const fileId = (data.fileId as string) ?? (data.id as string)
|
||||
if (fileId) {
|
||||
const fileName = (data.fileName as string) || (data.name as string) || 'File'
|
||||
return [{ type: 'file', id: fileId, title: fileName }]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
case FunctionExecute.id: {
|
||||
if (result.tableId) {
|
||||
return [
|
||||
{
|
||||
type: 'table',
|
||||
id: result.tableId as string,
|
||||
title: (result.tableName as string) || 'Table',
|
||||
},
|
||||
]
|
||||
}
|
||||
if (result.fileId) {
|
||||
return [
|
||||
{
|
||||
type: 'file',
|
||||
id: result.fileId as string,
|
||||
title: (result.fileName as string) || 'File',
|
||||
},
|
||||
]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
case DownloadToWorkspaceFile.id:
|
||||
case GenerateImage.id:
|
||||
case GenerateVideo.id:
|
||||
case GenerateAudio.id:
|
||||
case Ffmpeg.id: {
|
||||
// ffmpeg's probe op writes no file (no fileId) → no resource/auto-open.
|
||||
if (result.fileId) {
|
||||
return [
|
||||
{
|
||||
type: 'file',
|
||||
id: result.fileId as string,
|
||||
title: (result.fileName as string) || 'Generated File',
|
||||
},
|
||||
]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
case CreateWorkflow.id:
|
||||
case EditWorkflow.id: {
|
||||
const workflowId =
|
||||
(result.workflowId as string) ??
|
||||
(data.workflowId as string) ??
|
||||
(params?.workflowId as string)
|
||||
if (workflowId) {
|
||||
const workflowName =
|
||||
(result.workflowName as string) ??
|
||||
(data.workflowName as string) ??
|
||||
(params?.workflowName as string) ??
|
||||
'Workflow'
|
||||
return [{ type: 'workflow', id: workflowId, title: workflowName }]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
case KnowledgeBase.id: {
|
||||
if (READ_ONLY_KB_OPS.has(getOperation(params) ?? '')) return []
|
||||
|
||||
const args = asRecord(params?.args)
|
||||
const kbId =
|
||||
(args.knowledgeBaseId as string) ??
|
||||
(params?.knowledgeBaseId as string) ??
|
||||
(result.knowledgeBaseId as string) ??
|
||||
(data.knowledgeBaseId as string) ??
|
||||
(data.id as string)
|
||||
if (kbId) {
|
||||
const kbName =
|
||||
(data.name as string) ?? (result.knowledgeBaseName as string) ?? 'Knowledge Base'
|
||||
return [{ type: 'knowledgebase', id: kbId, title: kbName }]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
case Knowledge.id: {
|
||||
const action = data.action as string | undefined
|
||||
if (READ_ONLY_KNOWLEDGE_ACTIONS.has(action ?? '')) return []
|
||||
|
||||
const kbArray = data.knowledge_bases as Array<Record<string, unknown>> | undefined
|
||||
if (!Array.isArray(kbArray)) return []
|
||||
const resources: ChatResource[] = []
|
||||
for (const kb of kbArray) {
|
||||
const id = kb.id as string | undefined
|
||||
if (id) {
|
||||
resources.push({
|
||||
type: 'knowledgebase',
|
||||
id,
|
||||
title: (kb.name as string) || 'Knowledge Base',
|
||||
})
|
||||
}
|
||||
}
|
||||
return resources
|
||||
}
|
||||
|
||||
case ManageScheduledTask.id: {
|
||||
// Read-only ops never auto-open; only create/update surface the task.
|
||||
const op = getOperation(params)
|
||||
if (op === 'list' || op === 'get') return []
|
||||
const jobId = (result.jobId as string) ?? (data.jobId as string)
|
||||
if (jobId) {
|
||||
const args = asRecord(params?.args)
|
||||
const title = (result.title as string) ?? (args.title as string) ?? 'Scheduled Task'
|
||||
return [{ type: 'scheduledtask', id: jobId, title }]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
default:
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
const DELETE_CAPABLE_TOOL_RESOURCE_TYPE: Record<string, ResourceType> = {
|
||||
[DeleteWorkflow.id]: 'workflow',
|
||||
[WorkspaceFile.id]: 'file',
|
||||
[UserTable.id]: 'table',
|
||||
[KnowledgeBase.id]: 'knowledgebase',
|
||||
[ManageScheduledTask.id]: 'scheduledtask',
|
||||
}
|
||||
|
||||
export function hasDeleteCapability(toolName: string): boolean {
|
||||
return toolName in DELETE_CAPABLE_TOOL_RESOURCE_TYPE
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts resource descriptors from a tool execution result when the tool
|
||||
* performed a deletion. Returns one or more deleted resources for tools that
|
||||
* destroy workspace entities.
|
||||
*/
|
||||
export function extractDeletedResourcesFromToolResult(
|
||||
toolName: string,
|
||||
params: Record<string, unknown> | undefined,
|
||||
output: unknown
|
||||
): ChatResource[] {
|
||||
const resourceType = DELETE_CAPABLE_TOOL_RESOURCE_TYPE[toolName]
|
||||
if (!resourceType) return []
|
||||
|
||||
const result = asRecord(output)
|
||||
const data = asRecord(result.data)
|
||||
const args = asRecord(params?.args)
|
||||
const operation = (args.operation ?? params?.operation) as string | undefined
|
||||
|
||||
switch (toolName) {
|
||||
case DeleteWorkflow.id: {
|
||||
const workflowId = (result.workflowId as string) ?? (params?.workflowId as string)
|
||||
if (workflowId && result.deleted) {
|
||||
return [
|
||||
{ type: resourceType, id: workflowId, title: (result.name as string) || 'Workflow' },
|
||||
]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
case WorkspaceFile.id: {
|
||||
if (operation !== 'delete') return []
|
||||
const target = getWorkspaceFileTarget(params)
|
||||
const fileId = (data.id as string) ?? (target.fileId as string) ?? (args.fileId as string)
|
||||
if (fileId) {
|
||||
return [{ type: resourceType, id: fileId, title: (data.name as string) || 'File' }]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
case UserTable.id: {
|
||||
if (operation !== 'delete') return []
|
||||
const tableId = (args.tableId as string) ?? (params?.tableId as string)
|
||||
if (tableId) {
|
||||
return [{ type: resourceType, id: tableId, title: 'Table' }]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
case KnowledgeBase.id: {
|
||||
if (operation !== 'delete') return []
|
||||
const kbId = (data.id as string) ?? (args.knowledgeBaseId as string)
|
||||
if (kbId) {
|
||||
return [{ type: resourceType, id: kbId, title: (data.name as string) || 'Knowledge Base' }]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
case ManageScheduledTask.id: {
|
||||
if (operation !== 'delete') return []
|
||||
const deletedIds = Array.isArray(result.deleted) ? (result.deleted as string[]) : []
|
||||
return deletedIds.map((id) => ({ type: resourceType, id, title: 'Scheduled Task' }))
|
||||
}
|
||||
|
||||
default:
|
||||
return []
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import { db } from '@sim/db'
|
||||
import { copilotChats } from '@sim/db/schema'
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { toError } from '@sim/utils/errors'
|
||||
import { eq, sql } from 'drizzle-orm'
|
||||
import { GENERIC_RESOURCE_TITLES, type MothershipResource } from './types'
|
||||
|
||||
export {
|
||||
extractDeletedResourcesFromToolResult,
|
||||
extractResourcesFromToolResult,
|
||||
hasDeleteCapability,
|
||||
isResourceToolName,
|
||||
} from './extraction'
|
||||
export type {
|
||||
MothershipResource as ChatResource,
|
||||
MothershipResourceType as ResourceType,
|
||||
} from './types'
|
||||
|
||||
const logger = createLogger('CopilotResources')
|
||||
|
||||
type ChatResource = MothershipResource
|
||||
|
||||
/**
|
||||
* Appends resources to a chat's JSONB resources column, deduplicating by type+id.
|
||||
* Updates the title of existing resources if the new title is more specific.
|
||||
*/
|
||||
export async function persistChatResources(
|
||||
chatId: string,
|
||||
newResources: ChatResource[]
|
||||
): Promise<void> {
|
||||
const toMerge = newResources.filter((r) => r.id !== 'streaming-file')
|
||||
if (toMerge.length === 0) return
|
||||
|
||||
try {
|
||||
const [chat] = await db
|
||||
.select({ resources: copilotChats.resources })
|
||||
.from(copilotChats)
|
||||
.where(eq(copilotChats.id, chatId))
|
||||
.limit(1)
|
||||
|
||||
if (!chat) return
|
||||
|
||||
const existing = Array.isArray(chat.resources) ? (chat.resources as ChatResource[]) : []
|
||||
const map = new Map<string, ChatResource>()
|
||||
|
||||
for (const r of existing) {
|
||||
map.set(`${r.type}:${r.id}`, r)
|
||||
}
|
||||
|
||||
for (const r of toMerge) {
|
||||
const key = `${r.type}:${r.id}`
|
||||
const prev = map.get(key)
|
||||
if (
|
||||
!prev ||
|
||||
(GENERIC_RESOURCE_TITLES.has(prev.title) && !GENERIC_RESOURCE_TITLES.has(r.title))
|
||||
) {
|
||||
map.set(key, r)
|
||||
}
|
||||
}
|
||||
|
||||
const merged = Array.from(map.values())
|
||||
|
||||
await db
|
||||
.update(copilotChats)
|
||||
.set({ resources: sql`${JSON.stringify(merged)}::jsonb` })
|
||||
.where(eq(copilotChats.id, chatId))
|
||||
} catch (err) {
|
||||
logger.warn('Failed to persist chat resources', {
|
||||
chatId,
|
||||
error: toError(err).message,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes resources from a chat's JSONB resources column by type+id.
|
||||
*/
|
||||
export async function removeChatResources(chatId: string, toRemove: ChatResource[]): Promise<void> {
|
||||
if (toRemove.length === 0) return
|
||||
|
||||
try {
|
||||
const [chat] = await db
|
||||
.select({ resources: copilotChats.resources })
|
||||
.from(copilotChats)
|
||||
.where(eq(copilotChats.id, chatId))
|
||||
.limit(1)
|
||||
|
||||
if (!chat) return
|
||||
|
||||
const existing = Array.isArray(chat.resources) ? (chat.resources as ChatResource[]) : []
|
||||
const removeKeys = new Set(toRemove.map((r) => `${r.type}:${r.id}`))
|
||||
const filtered = existing.filter((r) => !removeKeys.has(`${r.type}:${r.id}`))
|
||||
|
||||
if (filtered.length === existing.length) return
|
||||
|
||||
await db
|
||||
.update(copilotChats)
|
||||
.set({ resources: sql`${JSON.stringify(filtered)}::jsonb` })
|
||||
.where(eq(copilotChats.id, chatId))
|
||||
} catch (err) {
|
||||
logger.warn('Failed to remove chat resources', {
|
||||
chatId,
|
||||
error: toError(err).message,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
export const MothershipResourceType = {
|
||||
table: 'table',
|
||||
file: 'file',
|
||||
workflow: 'workflow',
|
||||
knowledgebase: 'knowledgebase',
|
||||
folder: 'folder',
|
||||
filefolder: 'filefolder',
|
||||
task: 'task',
|
||||
scheduledtask: 'scheduledtask',
|
||||
log: 'log',
|
||||
integration: 'integration',
|
||||
generic: 'generic',
|
||||
} as const
|
||||
export type MothershipResourceType =
|
||||
(typeof MothershipResourceType)[keyof typeof MothershipResourceType]
|
||||
|
||||
export interface MothershipResource {
|
||||
type: MothershipResourceType
|
||||
id: string
|
||||
title: string
|
||||
path?: string
|
||||
}
|
||||
|
||||
export function isEphemeralResource(resource: MothershipResource): boolean {
|
||||
return resource.type === 'generic' || resource.id === 'streaming-file'
|
||||
}
|
||||
|
||||
/** Placeholder resource titles that a more specific title may overwrite during dedup. */
|
||||
export const GENERIC_RESOURCE_TITLES = new Set<string>([
|
||||
'Table',
|
||||
'File',
|
||||
'Workflow',
|
||||
'Knowledge Base',
|
||||
'Folder',
|
||||
'Scheduled Task',
|
||||
'Log',
|
||||
])
|
||||
|
||||
export const VFS_DIR_TO_RESOURCE: Record<string, MothershipResourceType> = {
|
||||
tables: 'table',
|
||||
files: 'file',
|
||||
workflows: 'workflow',
|
||||
knowledgebases: 'knowledgebase',
|
||||
folders: 'folder',
|
||||
jobs: 'scheduledtask',
|
||||
} as const
|
||||
Reference in New Issue
Block a user