Error 500 in SvelteKit when using session store and navigation push function

68 views Asked by At

I'm trying to store the result of a form submission in the session store and then redirect the user to a different route in SvelteKit. However, I'm encountering a 500 Internal Error. I've tried using the session and push functions from $app/stores and $app/navigation respectively, but the error persists. Here's the relevant part of my code:

<script context="module">
  import { DiscussServiceClient } from "@google-ai/generativelanguage";
  import { GoogleAuth } from "google-auth-library";
  import { session } from "$app/stores";
  import { push } from "$app/navigation";

  let dietType = "";
  let meals = {
    breakfast: false,
    lunch: false,
    snack: false,
    dinner: false,
  };

  const MODEL_NAME = "models/chat-bison-001";
  const API_KEY = import.meta.env.VITE_GoogleApiKey;

  const client = new DiscussServiceClient({
    authClient: new GoogleAuth().fromAPIKey(API_KEY),
  });

  const handleSubmit = async (event) => {
    event.preventDefault();
    let meal = Object.keys(meals)
      .filter((meal) => meals[meal])
      .join(", ");
    if (meal === "breakfast, lunch, snack, dinner") meal = "a day";

    const context = `You will plan a ${dietType} meal for ${meal}. And give them ingredients and procedures on how to make that meal. Include markdown such as button type list and headings.\n`;
    const examples = [];
    const messages = [];

    messages.push({ content: "NEXT REQUEST" });

    const result = await client.generateMessage({
      model: MODEL_NAME,
      temperature: 0.25,
      candidateCount: 1,
      top_k: 40,
      top_p: 0.95,
      prompt: {
        context: context,
        examples: examples,
        messages: messages,
      },
    });

    // Store the result in the session
    session.set({ result: JSON.stringify(result, null, 2) });

    // Redirect to the /output route
    push("/output");
  };
</script>

Any help would be appreciated.

"I'm trying to create a meal planning application using SvelteKit. In my application, I have a form where users can select a diet type and meals. When the form is submitted, I use the Google AI Generative Language model to generate a meal plan based on the user's selections. I then store the generated meal plan in the session store and redirect the user to a different route (/output) where the meal plan is displayed.

Here's what I did:

I created a handleSubmit function that is called when the form is submitted. In this function, I generate the meal plan and store it in the session store using session.set({ result: JSON.stringify(result, null, 2) });. After storing the result in the session, I redirect the user to the /output route using push("/output");. In the /output route, I use a load function to get the result from the session store and pass it as a prop to the component. What I was expecting:

I was expecting the user to be redirected to the /output route after the form submission, where the generated meal plan would be displayed.

However, I'm encountering a 500 Internal Error. I'm not sure what's causing this error or how to fix it. Any help would be appreciated."

0

There are 0 answers