diff --git a/apps/realtime/src/database/operations.ts b/apps/realtime/src/database/operations.ts index 38a98b14bb3..4904daccb8f 100644 --- a/apps/realtime/src/database/operations.ts +++ b/apps/realtime/src/database/operations.ts @@ -30,7 +30,7 @@ const socketDb = drizzle( prepare: false, idle_timeout: 10, connect_timeout: 20, - max: 30, + max: 15, onnotice: () => {}, }), { schema } diff --git a/apps/sim/app/api/mcp/servers/[id]/refresh/route.ts b/apps/sim/app/api/mcp/servers/[id]/refresh/route.ts index 9e15224ead0..7bab3fade1f 100644 --- a/apps/sim/app/api/mcp/servers/[id]/refresh/route.ts +++ b/apps/sim/app/api/mcp/servers/[id]/refresh/route.ts @@ -2,7 +2,7 @@ import { db } from '@sim/db' import { mcpServers, workflow, workflowBlocks } from '@sim/db/schema' import { createLogger } from '@sim/logger' import { toError } from '@sim/utils/errors' -import { and, eq, isNull } from 'drizzle-orm' +import { and, eq, inArray, isNull } from 'drizzle-orm' import type { NextRequest } from 'next/server' import { mcpServerIdParamsSchema } from '@/lib/api/contracts/mcp' import { validationErrorResponse } from '@/lib/api/server' @@ -77,13 +77,11 @@ async function syncToolSchemasToWorkflows( subBlocks: workflowBlocks.subBlocks, }) .from(workflowBlocks) - .where(eq(workflowBlocks.type, 'agent')) + .where(and(eq(workflowBlocks.type, 'agent'), inArray(workflowBlocks.workflowId, workflowIds))) const updatedWorkflowIds = new Set() for (const block of agentBlocks) { - if (!workflowIds.includes(block.workflowId)) continue - const subBlocks = block.subBlocks as Record | null if (!subBlocks) continue diff --git a/apps/sim/app/api/mcp/tools/stored/route.ts b/apps/sim/app/api/mcp/tools/stored/route.ts index 59fa5f5102f..3606e05115d 100644 --- a/apps/sim/app/api/mcp/tools/stored/route.ts +++ b/apps/sim/app/api/mcp/tools/stored/route.ts @@ -2,7 +2,7 @@ import { db } from '@sim/db' import { workflow, workflowBlocks } from '@sim/db/schema' import { createLogger } from '@sim/logger' import { toError } from '@sim/utils/errors' -import { eq } from 'drizzle-orm' +import { and, eq, inArray } from 'drizzle-orm' import type { NextRequest } from 'next/server' import { withRouteHandler } from '@/lib/core/utils/with-route-handler' import { withMcpAuth } from '@/lib/mcp/middleware' @@ -33,13 +33,13 @@ export const GET = withRouteHandler( const agentBlocks = await db .select({ workflowId: workflowBlocks.workflowId, subBlocks: workflowBlocks.subBlocks }) .from(workflowBlocks) - .where(eq(workflowBlocks.type, 'agent')) + .where( + and(eq(workflowBlocks.type, 'agent'), inArray(workflowBlocks.workflowId, workflowIds)) + ) const storedTools: StoredMcpTool[] = [] for (const block of agentBlocks) { - if (!workflowMap.has(block.workflowId)) continue - const subBlocks = block.subBlocks as Record | null if (!subBlocks) continue diff --git a/apps/sim/lib/copilot/vfs/workspace-vfs.ts b/apps/sim/lib/copilot/vfs/workspace-vfs.ts index 6e5cd70bb7d..3db1683bd19 100644 --- a/apps/sim/lib/copilot/vfs/workspace-vfs.ts +++ b/apps/sim/lib/copilot/vfs/workspace-vfs.ts @@ -17,7 +17,7 @@ import { } from '@sim/db/schema' import { createLogger } from '@sim/logger' import { toError } from '@sim/utils/errors' -import { and, desc, eq, isNotNull, isNull, ne } from 'drizzle-orm' +import { and, desc, eq, isNotNull, isNull, ne, sql } from 'drizzle-orm' import { listApiKeys } from '@/lib/api-key/service' import { buildWorkspaceMd, type WorkspaceMdData } from '@/lib/copilot/chat/workspace-context' import { extractDocumentStyle } from '@/lib/copilot/vfs/document-style' @@ -1157,7 +1157,27 @@ export class WorkspaceVFS { .select({ id: copilotChats.id, title: copilotChats.title, - messages: copilotChats.messages, + messageCount: sql`COALESCE(jsonb_array_length(${copilotChats.messages}), 0)`, + messages: sql`COALESCE(( + SELECT jsonb_agg( + jsonb_build_object( + 'role', m->>'role', + 'content', m->'content', + 'contentBlocks', COALESCE(( + SELECT jsonb_agg(jsonb_build_object('type', 'text', 'content', b->'content')) + FROM jsonb_array_elements( + CASE WHEN jsonb_typeof(m->'contentBlocks') = 'array' + THEN m->'contentBlocks' + ELSE '[]'::jsonb + END + ) AS b + WHERE b->>'type' = 'text' + ), '[]'::jsonb) + ) + ) + FROM jsonb_array_elements(${copilotChats.messages}) AS m + WHERE m->>'role' IN ('user', 'assistant') + ), '[]'::jsonb)`, createdAt: copilotChats.createdAt, updatedAt: copilotChats.updatedAt, }) @@ -1177,13 +1197,14 @@ export class WorkspaceVFS { const safeName = sanitizeName(title) const prefix = `tasks/${safeName}/` const messages = Array.isArray(task.messages) ? task.messages : [] + const messageCount = Number(task.messageCount) || 0 this.files.set( `${prefix}session.md`, serializeTaskSession({ id: task.id, title, - messageCount: messages.length, + messageCount, createdAt: task.createdAt, updatedAt: task.updatedAt, }) diff --git a/packages/db/db.ts b/packages/db/db.ts index 6868bbaeeb7..9e5597fb57b 100644 --- a/packages/db/db.ts +++ b/packages/db/db.ts @@ -11,7 +11,7 @@ const postgresClient = postgres(connectionString, { prepare: false, idle_timeout: 20, connect_timeout: 30, - max: 30, + max: 15, onnotice: () => {}, })