( answer_id: string, supabase: SupabaseClient<Database>, )
| 891 | } |
| 892 | |
| 893 | async function pullInMessageData( |
| 894 | answer_id: string, |
| 895 | supabase: SupabaseClient<Database>, |
| 896 | ): Promise<{ |
| 897 | answer: UIAnswerType; |
| 898 | messagesData: UIMessageData[]; |
| 899 | messagesToViz: StreamingStepInput[]; |
| 900 | }> { |
| 901 | const { data, error } = await supabase |
| 902 | .from("approval_answers") |
| 903 | .select( |
| 904 | "id,group_id,org_id,is_generating,generation_failed,approved, approval_questions(text, primary_question), approval_answer_messages(id, raw_text, message_idx, message_type, generated_output)", |
| 905 | ) |
| 906 | .match({ id: answer_id }) |
| 907 | .single(); |
| 908 | if (error) throw new Error(error.message); |
| 909 | const primaryQuestion = data.approval_questions.find( |
| 910 | (q) => q.primary_question, |
| 911 | ); |
| 912 | if (!primaryQuestion) throw new Error("No primary question found"); |
| 913 | |
| 914 | const messagesToViz = data.approval_answer_messages |
| 915 | .sort((a, b) => a.message_idx - b.message_idx) |
| 916 | .map((m) => { |
| 917 | if (m.message_type === "user") { |
| 918 | const userText = getUserMessageText( |
| 919 | primaryQuestion.text, |
| 920 | JSON.parse(m.raw_text), |
| 921 | ); |
| 922 | return { role: "user", content: userText } as UserMessage; |
| 923 | } else if (m.message_type === "text") |
| 924 | return { role: "assistant", content: m.raw_text } as AssistantMessage; |
| 925 | else if (m.message_type === "code") { |
| 926 | return m.generated_output.map((item) => |
| 927 | item && |
| 928 | typeof item === "object" && |
| 929 | "name" in item && |
| 930 | "content" in item && |
| 931 | item.name === "plot" |
| 932 | ? { |
| 933 | role: "graph", |
| 934 | // @ts-ignore |
| 935 | content: JSON.parse(item.content), |
| 936 | } |
| 937 | : item, |
| 938 | ); |
| 939 | } else if (m.message_type === "error") { |
| 940 | return { role: "error", content: m.raw_text }; |
| 941 | } else if (m.message_type === "function") { |
| 942 | return m.generated_output.map( |
| 943 | (item) => |
| 944 | item && |
| 945 | typeof item === "object" && |
| 946 | "name" in item && |
| 947 | "content" in item && |
| 948 | item, |
| 949 | ); |
| 950 | } |
no test coverage detected