Files
sim/apps/sim/app/api/copilot/chat/resources/route.ts
Siddharth Ganesan d6bf12da24 improvement(mothership): copilot, files, compaction, tools, persistence, duplication constraints (#3682)
* Improve

* Hide is hosted

* Remove hardcoded

* fix

* Fixes

* v0

* Fix bugs

* Restore settings

* Handle compaction event type

* Add keepalive

* File streaming

* Error tags

* Abort defense

* Edit hashes

* DB backed tools

* Fixes

* progress on autolayout improvements

* Abort fixes

* vertical insertion improvement

* Consolidate file attachments

* Fix lint

* Manage agent result card fix

* Remove hardcoded ff

* Fix file streaming

* Fix persisted writing file tab

* Fix lint

* Fix streaming file flash

* Always set url to /file on file view

* Edit perms for tables

* Fix file edit perms

* remove inline tool call json dump

* Enforce name uniqueness (#3679)

* Enforce name uniqueness

* Use established pattern for error handling

* Fix lint

* Fix lint

* Add kb name uniqueness to db

* Fix lint

* Handle name getting taken before restore

* Enforce duplicate file name

* Fix lint

---------

Co-authored-by: Theodore Li <theo@sim.ai>

* fix temp file creation

* fix types

* Streaming fixes

* type xml tag structures + return invalid id linter errors back to LLM

* Add image gen and viz tools

* Tags

* Workflow tags

* Fix lint

* Fix subagent abort

* Fix subagent persistence

* Fix subagent aborts

* Nuke db migs

* Re add db migrations

* Fix lint

---------

Co-authored-by: Theodore Li <teddy@zenobiapay.com>
Co-authored-by: Vikhyath Mondreti <vikhyath@simstudio.ai>
Co-authored-by: Theodore Li <theodoreqili@gmail.com>
Co-authored-by: Theodore Li <theo@sim.ai>
2026-03-22 00:46:13 -07:00

195 lines
6.5 KiB
TypeScript

import { db } from '@sim/db'
import { copilotChats } from '@sim/db/schema'
import { createLogger } from '@sim/logger'
import { and, eq, sql } from 'drizzle-orm'
import { type NextRequest, NextResponse } from 'next/server'
import { z } from 'zod'
import {
authenticateCopilotRequestSessionOnly,
createBadRequestResponse,
createInternalServerErrorResponse,
createNotFoundResponse,
createUnauthorizedResponse,
} from '@/lib/copilot/request-helpers'
import type { ChatResource, ResourceType } from '@/lib/copilot/resources'
const logger = createLogger('CopilotChatResourcesAPI')
const VALID_RESOURCE_TYPES = new Set<ResourceType>(['table', 'file', 'workflow', 'knowledgebase'])
const GENERIC_TITLES = new Set(['Table', 'File', 'Workflow', 'Knowledge Base'])
const AddResourceSchema = z.object({
chatId: z.string(),
resource: z.object({
type: z.enum(['table', 'file', 'workflow', 'knowledgebase']),
id: z.string(),
title: z.string(),
}),
})
const RemoveResourceSchema = z.object({
chatId: z.string(),
resourceType: z.enum(['table', 'file', 'workflow', 'knowledgebase']),
resourceId: z.string(),
})
const ReorderResourcesSchema = z.object({
chatId: z.string(),
resources: z.array(
z.object({
type: z.enum(['table', 'file', 'workflow', 'knowledgebase']),
id: z.string(),
title: z.string(),
})
),
})
export async function POST(req: NextRequest) {
try {
const { userId, isAuthenticated } = await authenticateCopilotRequestSessionOnly()
if (!isAuthenticated || !userId) {
return createUnauthorizedResponse()
}
const body = await req.json()
const { chatId, resource } = AddResourceSchema.parse(body)
// Ephemeral UI tab (client does not POST this; guard for old clients / bugs).
if (resource.id === 'streaming-file') {
return NextResponse.json({ success: true })
}
if (!VALID_RESOURCE_TYPES.has(resource.type)) {
return createBadRequestResponse(`Invalid resource type: ${resource.type}`)
}
const [chat] = await db
.select({ resources: copilotChats.resources })
.from(copilotChats)
.where(and(eq(copilotChats.id, chatId), eq(copilotChats.userId, userId)))
.limit(1)
if (!chat) {
return createNotFoundResponse('Chat not found or unauthorized')
}
const existing = Array.isArray(chat.resources) ? (chat.resources as ChatResource[]) : []
const key = `${resource.type}:${resource.id}`
const prev = existing.find((r) => `${r.type}:${r.id}` === key)
let merged: ChatResource[]
if (prev) {
if (GENERIC_TITLES.has(prev.title) && !GENERIC_TITLES.has(resource.title)) {
merged = existing.map((r) =>
`${r.type}:${r.id}` === key ? { ...r, title: resource.title } : r
)
} else {
merged = existing
}
} else {
merged = [...existing, resource]
}
await db
.update(copilotChats)
.set({ resources: sql`${JSON.stringify(merged)}::jsonb`, updatedAt: new Date() })
.where(eq(copilotChats.id, chatId))
logger.info('Added resource to chat', { chatId, resource })
return NextResponse.json({ success: true, resources: merged })
} catch (error) {
if (error instanceof z.ZodError) {
return createBadRequestResponse(error.errors.map((e) => e.message).join(', '))
}
logger.error('Error adding chat resource:', error)
return createInternalServerErrorResponse('Failed to add resource')
}
}
export async function PATCH(req: NextRequest) {
try {
const { userId, isAuthenticated } = await authenticateCopilotRequestSessionOnly()
if (!isAuthenticated || !userId) {
return createUnauthorizedResponse()
}
const body = await req.json()
const { chatId, resources: newOrder } = ReorderResourcesSchema.parse(body)
const [chat] = await db
.select({ resources: copilotChats.resources })
.from(copilotChats)
.where(and(eq(copilotChats.id, chatId), eq(copilotChats.userId, userId)))
.limit(1)
if (!chat) {
return createNotFoundResponse('Chat not found or unauthorized')
}
const existing = Array.isArray(chat.resources) ? (chat.resources as ChatResource[]) : []
const existingKeys = new Set(existing.map((r) => `${r.type}:${r.id}`))
const newKeys = new Set(newOrder.map((r) => `${r.type}:${r.id}`))
if (existingKeys.size !== newKeys.size || ![...existingKeys].every((k) => newKeys.has(k))) {
return createBadRequestResponse('Reordered resources must match existing resources')
}
await db
.update(copilotChats)
.set({ resources: sql`${JSON.stringify(newOrder)}::jsonb`, updatedAt: new Date() })
.where(eq(copilotChats.id, chatId))
logger.info('Reordered resources for chat', { chatId, count: newOrder.length })
return NextResponse.json({ success: true, resources: newOrder })
} catch (error) {
if (error instanceof z.ZodError) {
return createBadRequestResponse(error.errors.map((e) => e.message).join(', '))
}
logger.error('Error reordering chat resources:', error)
return createInternalServerErrorResponse('Failed to reorder resources')
}
}
export async function DELETE(req: NextRequest) {
try {
const { userId, isAuthenticated } = await authenticateCopilotRequestSessionOnly()
if (!isAuthenticated || !userId) {
return createUnauthorizedResponse()
}
const body = await req.json()
const { chatId, resourceType, resourceId } = RemoveResourceSchema.parse(body)
const [chat] = await db
.select({ resources: copilotChats.resources })
.from(copilotChats)
.where(and(eq(copilotChats.id, chatId), eq(copilotChats.userId, userId)))
.limit(1)
if (!chat) {
return createNotFoundResponse('Chat not found or unauthorized')
}
const existing = Array.isArray(chat.resources) ? (chat.resources as ChatResource[]) : []
const key = `${resourceType}:${resourceId}`
const merged = existing.filter((r) => `${r.type}:${r.id}` !== key)
await db
.update(copilotChats)
.set({ resources: sql`${JSON.stringify(merged)}::jsonb`, updatedAt: new Date() })
.where(eq(copilotChats.id, chatId))
logger.info('Removed resource from chat', { chatId, resourceType, resourceId })
return NextResponse.json({ success: true, resources: merged })
} catch (error) {
if (error instanceof z.ZodError) {
return createBadRequestResponse(error.errors.map((e) => e.message).join(', '))
}
logger.error('Error removing chat resource:', error)
return createInternalServerErrorResponse('Failed to remove resource')
}
}