Skip to content

Commit 52649ff

Browse files
committed
refactor(chat): share generic resource-title set across route and persistence
Dedupes the placeholder-title set that the chat-resource route and the server-side persistence merge each defined separately. They had already drifted ('Log' was missing from persistence); a single shared GENERIC_RESOURCE_TITLES keeps title-upgrade behavior consistent, including the new 'Scheduled Task' placeholder.
1 parent 68d32fe commit 52649ff

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

apps/sim/app/api/copilot/chat/resources/route.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
createUnauthorizedResponse,
1818
} from '@/lib/copilot/request/http'
1919
import type { ChatResource, ResourceType } from '@/lib/copilot/resources/persistence'
20+
import { GENERIC_RESOURCE_TITLES } from '@/lib/copilot/resources/types'
2021
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
2122

2223
const logger = createLogger('CopilotChatResourcesAPI')
@@ -31,15 +32,6 @@ const VALID_RESOURCE_TYPES = new Set<ResourceType>([
3132
'log',
3233
'integration',
3334
])
34-
const GENERIC_TITLES = new Set([
35-
'Table',
36-
'File',
37-
'Workflow',
38-
'Knowledge Base',
39-
'Folder',
40-
'Scheduled Task',
41-
'Log',
42-
])
4335

4436
export const POST = withRouteHandler(async (req: NextRequest) => {
4537
try {
@@ -85,7 +77,7 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
8577

8678
let merged: ChatResource[]
8779
if (prev) {
88-
if (GENERIC_TITLES.has(prev.title) && !GENERIC_TITLES.has(resource.title)) {
80+
if (GENERIC_RESOURCE_TITLES.has(prev.title) && !GENERIC_RESOURCE_TITLES.has(resource.title)) {
8981
merged = existing.map((r) =>
9082
`${r.type}:${r.id}` === key ? { ...r, title: resource.title } : r
9183
)

apps/sim/lib/copilot/resources/persistence.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { copilotChats } from '@sim/db/schema'
33
import { createLogger } from '@sim/logger'
44
import { toError } from '@sim/utils/errors'
55
import { eq, sql } from 'drizzle-orm'
6-
import type { MothershipResource } from './types'
6+
import { GENERIC_RESOURCE_TITLES, type MothershipResource } from './types'
77

88
export {
99
extractDeletedResourcesFromToolResult,
@@ -42,7 +42,6 @@ export async function persistChatResources(
4242

4343
const existing = Array.isArray(chat.resources) ? (chat.resources as ChatResource[]) : []
4444
const map = new Map<string, ChatResource>()
45-
const GENERIC = new Set(['Table', 'File', 'Workflow', 'Knowledge Base', 'Folder', 'Log'])
4645

4746
for (const r of existing) {
4847
map.set(`${r.type}:${r.id}`, r)
@@ -51,7 +50,10 @@ export async function persistChatResources(
5150
for (const r of toMerge) {
5251
const key = `${r.type}:${r.id}`
5352
const prev = map.get(key)
54-
if (!prev || (GENERIC.has(prev.title) && !GENERIC.has(r.title))) {
53+
if (
54+
!prev ||
55+
(GENERIC_RESOURCE_TITLES.has(prev.title) && !GENERIC_RESOURCE_TITLES.has(r.title))
56+
) {
5557
map.set(key, r)
5658
}
5759
}

apps/sim/lib/copilot/resources/types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ export function isEphemeralResource(resource: MothershipResource): boolean {
2525
return resource.type === 'generic' || resource.id === 'streaming-file'
2626
}
2727

28+
/**
29+
* Placeholder resource titles emitted before a specific name is known. A more
30+
* specific title may overwrite one of these during dedup; a specific title is
31+
* never downgraded back to a placeholder. Shared by the chat-resource route and
32+
* the server-side persistence merge so the two stay in lockstep.
33+
*/
34+
export const GENERIC_RESOURCE_TITLES = new Set<string>([
35+
'Table',
36+
'File',
37+
'Workflow',
38+
'Knowledge Base',
39+
'Folder',
40+
'Scheduled Task',
41+
'Log',
42+
])
43+
2844
export const VFS_DIR_TO_RESOURCE: Record<string, MothershipResourceType> = {
2945
tables: 'table',
3046
files: 'file',

0 commit comments

Comments
 (0)