(
instruction: string,
filteredActions: ActionPlusApiInfo[],
org: Pick<
Organization,
| "id"
| "name"
| "description"
| "chatbot_instructions"
| "fun_loading_messages"
>,
dbData: { conversationId: number; index: number },
userDescription: string,
cache: LlmResponseCache,
streamInfo: (step: StreamingStepInput) => void,
userApiKey?: string,
)
| 77 | } as LoadingMessage; |
| 78 | |
| 79 | export async function runDataAnalysis( |
| 80 | instruction: string, |
| 81 | filteredActions: ActionPlusApiInfo[], |
| 82 | org: Pick< |
| 83 | Organization, |
| 84 | | "id" |
| 85 | | "name" |
| 86 | | "description" |
| 87 | | "chatbot_instructions" |
| 88 | | "fun_loading_messages" |
| 89 | >, |
| 90 | dbData: { conversationId: number; index: number }, |
| 91 | userDescription: string, |
| 92 | cache: LlmResponseCache, |
| 93 | streamInfo: (step: StreamingStepInput) => void, |
| 94 | userApiKey?: string, |
| 95 | ): Promise<ExecuteCode2Item[] | { error: string } | null> { |
| 96 | if (!org.fun_loading_messages) { |
| 97 | streamInfo({ role: "loading", content: "Performing data analysis" }); |
| 98 | } |
| 99 | // Either sends fun loading messages or does nothing |
| 100 | let slowFnWrapper = org.fun_loading_messages |
| 101 | ? sendFunLoadingMessages |
| 102 | : (p: Promise<any>) => p; |
| 103 | let llmResponse = await cache.checkBertieAnalyticsCache( |
| 104 | instruction, |
| 105 | filteredActions.map((a) => a.name), |
| 106 | org.id, |
| 107 | supabase, |
| 108 | ); |
| 109 | console.log("LLM response from cache:", llmResponse); |
| 110 | if (llmResponse) { |
| 111 | // Parse the result |
| 112 | let parsedCode; |
| 113 | if (llmResponse.includes("```")) { |
| 114 | parsedCode = parseOpusOrGPTDataAnalysis(llmResponse, filteredActions); |
| 115 | } else { |
| 116 | parsedCode = parseGeneratedCode(llmResponse, filteredActions); |
| 117 | } |
| 118 | if (parsedCode !== null) { |
| 119 | if ("error" in parsedCode) return parsedCode; |
| 120 | console.info("Parsed LLM response from cache:", parsedCode.code); |
| 121 | if (!org.fun_loading_messages) |
| 122 | streamInfo({ role: "loading", content: "Executing code" }); |
| 123 | // Send code to supabase edge function to execute |
| 124 | const res = await slowFnWrapper( |
| 125 | supabase.functions.invoke("execute-code-2", { |
| 126 | body: JSON.stringify({ |
| 127 | actionsPlusApi: filteredActions, |
| 128 | org, |
| 129 | code: parsedCode.code, |
| 130 | userApiKey, |
| 131 | }), |
| 132 | }), |
| 133 | streamInfo, |
| 134 | ); |
| 135 | |
| 136 | if (!res.error) { |
no test coverage detected