( params: Record<string, unknown>, context: ExecutionContext )
| 63 | type ManageJobParams = z.infer<typeof ManageJobParamsSchema> |
| 64 | |
| 65 | export async function executeCreateJob( |
| 66 | params: Record<string, unknown>, |
| 67 | context: ExecutionContext |
| 68 | ): Promise<ToolCallResult> { |
| 69 | const parsedParams = CreateJobParamsSchema.safeParse(params) |
| 70 | if (!parsedParams.success) { |
| 71 | return { success: false, error: 'Invalid create job parameters' } |
| 72 | } |
| 73 | |
| 74 | const rawParams: CreateJobParams = parsedParams.data |
| 75 | const timezone = rawParams.timezone || context.userTimezone || 'UTC' |
| 76 | const { title, prompt, cron, time, lifecycle, successCondition, maxRuns } = rawParams |
| 77 | |
| 78 | if (!prompt) { |
| 79 | return { success: false, error: 'prompt is required' } |
| 80 | } |
| 81 | |
| 82 | if (!cron && !time) { |
| 83 | return { success: false, error: 'At least one of cron or time must be provided' } |
| 84 | } |
| 85 | |
| 86 | if (!context.userId || !context.workspaceId) { |
| 87 | return { success: false, error: 'Missing user or workspace context' } |
| 88 | } |
| 89 | |
| 90 | let taskName: string | null = null |
| 91 | if (context.chatId) { |
| 92 | try { |
| 93 | const [chat] = await db |
| 94 | .select({ title: copilotChats.title }) |
| 95 | .from(copilotChats) |
| 96 | .where(eq(copilotChats.id, context.chatId)) |
| 97 | .limit(1) |
| 98 | taskName = chat?.title || null |
| 99 | } catch (err) { |
| 100 | logger.warn('Failed to look up chat title for job', { |
| 101 | chatId: context.chatId, |
| 102 | error: toError(err).message, |
| 103 | }) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | try { |
| 108 | const result = await performCreateJob({ |
| 109 | workspaceId: context.workspaceId, |
| 110 | userId: context.userId, |
| 111 | title, |
| 112 | prompt, |
| 113 | cronExpression: cron, |
| 114 | time, |
| 115 | timezone, |
| 116 | lifecycle, |
| 117 | successCondition, |
| 118 | maxRuns, |
| 119 | sourceChatId: context.chatId, |
| 120 | sourceTaskName: taskName, |
| 121 | }) |
| 122 | if (!result.success || !result.schedule) { |
no test coverage detected