(req: NextRequest)
| 53 | type WriteActionDescriptionType = z.infer<typeof WriteActionDescriptionZod>; |
| 54 | |
| 55 | export default async function handler(req: NextRequest): Promise<Response> { |
| 56 | if (req.method !== "POST") { |
| 57 | return new Response("Only POST requests allowed", { status: 405, headers }); |
| 58 | } |
| 59 | const reqBody = await req.json(); |
| 60 | if ( |
| 61 | !isValidBody<WriteActionDescriptionType>(reqBody, WriteActionDescriptionZod) |
| 62 | ) { |
| 63 | return new Response("Invalid request body", { status: 400, headers }); |
| 64 | } |
| 65 | // Check key is service level key |
| 66 | const authHeader = req.headers.get("Authorization"); |
| 67 | if ( |
| 68 | !authHeader || |
| 69 | !authHeader.includes(process.env.SERVICE_LEVEL_KEY_SUPABASE!) |
| 70 | ) { |
| 71 | return new Response("Unauthorized", { status: 401, headers }); |
| 72 | } |
| 73 | await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 74 | |
| 75 | // Get the actions from the database |
| 76 | const orgResponse = await supabase |
| 77 | .from("actions") |
| 78 | .select("*, organizations(name, description)") |
| 79 | .eq("org_id", reqBody.org_id) |
| 80 | .in("id", reqBody.action_ids); |
| 81 | if (orgResponse.error) { |
| 82 | console.error(orgResponse.error); |
| 83 | return new Response(orgResponse.error.message, { status: 500, headers }); |
| 84 | } |
| 85 | console.log( |
| 86 | "Total actions found:", |
| 87 | orgResponse.data.map((a) => a.name), |
| 88 | ); |
| 89 | let actions = orgResponse.data.filter((a) => !a.filtering_description); |
| 90 | // Limit to 40 actions max |
| 91 | if (actions.length > 40) { |
| 92 | actions = actions.filter((a) => a.active).slice(0, 40); |
| 93 | } |
| 94 | console.log( |
| 95 | "Chosen action names:", |
| 96 | actions.map((a) => a.name), |
| 97 | ); |
| 98 | |
| 99 | // Generate action filtering_descriptions |
| 100 | await Promise.all( |
| 101 | actions.map(async (action) => { |
| 102 | const prompt = writeActionDescriptionPrompt({ |
| 103 | action, |
| 104 | org: action.organizations!, |
| 105 | }); |
| 106 | console.log("WRITE ACTIONS PROMPT:", prompt[0].content); |
| 107 | const llmOut = await exponentialRetryWrapper( |
| 108 | getLLMResponse, |
| 109 | [ |
| 110 | prompt, |
| 111 | writeActionDescriptionLLMParams, |
| 112 | process.env.WRITE_ACTION_FILTERING_DESCRIPTION_MODEL!, |
nothing calls this directly
no test coverage detected