|
| 1 | +import { db } from '@sim/db' |
| 2 | +import { workflow, workflowExecutionLogs } from '@sim/db/schema' |
| 3 | +import { createLogger } from '@sim/logger' |
| 4 | +import { and, desc, eq, type SQL } from 'drizzle-orm' |
| 5 | +import type { BaseServerTool, ServerToolContext } from '@/lib/copilot/tools/server/base-tool' |
| 6 | +import { checkWorkspaceAccess } from '@/lib/workspaces/permissions/utils' |
| 7 | + |
| 8 | +const logger = createLogger('GetExecutionSummaryServerTool') |
| 9 | + |
| 10 | +interface GetExecutionSummaryArgs { |
| 11 | + workspaceId: string |
| 12 | + workflowId?: string |
| 13 | + limit?: number |
| 14 | + status?: 'success' | 'error' | 'all' |
| 15 | +} |
| 16 | + |
| 17 | +interface ExecutionSummary { |
| 18 | + executionId: string |
| 19 | + workflowId: string |
| 20 | + workflowName: string | null |
| 21 | + status: string |
| 22 | + trigger: string |
| 23 | + startedAt: string |
| 24 | + durationMs: number | null |
| 25 | + cost: number | null |
| 26 | + error: string | null |
| 27 | +} |
| 28 | + |
| 29 | +function extractErrorMessage(executionData: any): string | null { |
| 30 | + if (!executionData) return null |
| 31 | + return ( |
| 32 | + executionData?.errorDetails?.error || |
| 33 | + executionData?.errorDetails?.message || |
| 34 | + executionData?.finalOutput?.error || |
| 35 | + executionData?.error || |
| 36 | + null |
| 37 | + ) |
| 38 | +} |
| 39 | + |
| 40 | +export const getExecutionSummaryServerTool: BaseServerTool< |
| 41 | + GetExecutionSummaryArgs, |
| 42 | + ExecutionSummary[] |
| 43 | +> = { |
| 44 | + name: 'get_execution_summary', |
| 45 | + async execute( |
| 46 | + rawArgs: GetExecutionSummaryArgs, |
| 47 | + context?: ServerToolContext |
| 48 | + ): Promise<ExecutionSummary[]> { |
| 49 | + const { workspaceId, workflowId, limit = 10, status = 'all' } = rawArgs || {} |
| 50 | + |
| 51 | + if (!workspaceId || typeof workspaceId !== 'string') { |
| 52 | + throw new Error('workspaceId is required') |
| 53 | + } |
| 54 | + if (!context?.userId) { |
| 55 | + throw new Error('Unauthorized access') |
| 56 | + } |
| 57 | + |
| 58 | + const access = await checkWorkspaceAccess(workspaceId, context.userId) |
| 59 | + if (!access.hasAccess) { |
| 60 | + throw new Error('Unauthorized workspace access') |
| 61 | + } |
| 62 | + |
| 63 | + const clampedLimit = Math.min(Math.max(1, limit), 20) |
| 64 | + |
| 65 | + logger.info('Fetching execution summary', { workspaceId, workflowId, limit: clampedLimit, status }) |
| 66 | + |
| 67 | + const conditions: SQL[] = [eq(workflowExecutionLogs.workspaceId, workspaceId)] |
| 68 | + |
| 69 | + if (workflowId) { |
| 70 | + conditions.push(eq(workflowExecutionLogs.workflowId, workflowId)) |
| 71 | + } |
| 72 | + |
| 73 | + if (status === 'error') { |
| 74 | + conditions.push(eq(workflowExecutionLogs.level, 'error')) |
| 75 | + } else if (status === 'success') { |
| 76 | + conditions.push(eq(workflowExecutionLogs.level, 'info')) |
| 77 | + } |
| 78 | + |
| 79 | + const rows = await db |
| 80 | + .select({ |
| 81 | + executionId: workflowExecutionLogs.executionId, |
| 82 | + workflowId: workflowExecutionLogs.workflowId, |
| 83 | + workflowName: workflow.name, |
| 84 | + status: workflowExecutionLogs.status, |
| 85 | + level: workflowExecutionLogs.level, |
| 86 | + trigger: workflowExecutionLogs.trigger, |
| 87 | + startedAt: workflowExecutionLogs.startedAt, |
| 88 | + totalDurationMs: workflowExecutionLogs.totalDurationMs, |
| 89 | + cost: workflowExecutionLogs.cost, |
| 90 | + executionData: workflowExecutionLogs.executionData, |
| 91 | + }) |
| 92 | + .from(workflowExecutionLogs) |
| 93 | + .leftJoin(workflow, eq(workflowExecutionLogs.workflowId, workflow.id)) |
| 94 | + .where(and(...conditions)) |
| 95 | + .orderBy(desc(workflowExecutionLogs.startedAt)) |
| 96 | + .limit(clampedLimit) |
| 97 | + |
| 98 | + const summaries: ExecutionSummary[] = rows.map((row) => { |
| 99 | + const costData = row.cost as any |
| 100 | + const errorMsg = |
| 101 | + row.level === 'error' ? extractErrorMessage(row.executionData) : null |
| 102 | + |
| 103 | + return { |
| 104 | + executionId: row.executionId, |
| 105 | + workflowId: row.workflowId, |
| 106 | + workflowName: row.workflowName, |
| 107 | + status: row.status, |
| 108 | + trigger: row.trigger, |
| 109 | + startedAt: row.startedAt.toISOString(), |
| 110 | + durationMs: row.totalDurationMs ?? null, |
| 111 | + cost: costData?.total ? Number(costData.total) : null, |
| 112 | + error: errorMsg |
| 113 | + ? typeof errorMsg === 'string' |
| 114 | + ? errorMsg |
| 115 | + : JSON.stringify(errorMsg) |
| 116 | + : null, |
| 117 | + } |
| 118 | + }) |
| 119 | + |
| 120 | + logger.info('Execution summary prepared', { |
| 121 | + count: summaries.length, |
| 122 | + workspaceId, |
| 123 | + }) |
| 124 | + |
| 125 | + return summaries |
| 126 | + }, |
| 127 | +} |
0 commit comments