vercel / ai-chatbot

A full-featured, hackable Next.js AI chatbot built by Vercel
https://chat.vercel.ai
Other
5.51k stars 1.62k forks source link

System messages `Cannot destructure property 'role' error` #332

Closed JoseAngelChepo closed 1 month ago

JoseAngelChepo commented 1 month ago

The last update "streamUI instead of render #324" not allow messages with "role: system" between conversation (only at start) with new prompt param system.

The "problem" is: The functions also uses messages with "role: system" to register the actions or events from the user in the context conversation.

The error Cannot destructure property 'role' error is consequence of the function below that return undefined for "role: system"

This function is inside of dependency "ai": "^3.1.1"

// core/prompt/convert-to-language-model-prompt.ts
function convertToLanguageModelPrompt(prompt) {
  const languageModelMessages = [];
  if (prompt.system != null) {
    languageModelMessages.push({ role: "system", content: prompt.system });
  }
  switch (prompt.type) {
    case "prompt": {
      languageModelMessages.push({
        role: "user",
        content: [{ type: "text", text: prompt.prompt }]
      });
      break;
    }
    case "messages": {
      languageModelMessages.push(
        ...prompt.messages.map((message) => {
          switch (message.role) {
            case "user": {
              if (typeof message.content === "string") {
                return {
                  role: "user",
                  content: [{ type: "text", text: message.content }]
                };
              }
              return {
                role: "user",
                content: message.content.map(
                  (part) => {
                    var _a;
                    switch (part.type) {
                      case "text": {
                        return part;
                      }
                      case "image": {
                        if (part.image instanceof URL) {
                          return {
                            type: "image",
                            image: part.image,
                            mimeType: part.mimeType
                          };
                        }
                        const imageUint8 = convertDataContentToUint8Array(
                          part.image
                        );
                        return {
                          type: "image",
                          image: imageUint8,
                          mimeType: (_a = part.mimeType) != null ? _a : detectImageMimeType(imageUint8)
                        };
                      }
                    }
                  }
                )
              };
            }
            case "assistant": {
              if (typeof message.content === "string") {
                return {
                  role: "assistant",
                  content: [{ type: "text", text: message.content }]
                };
              }
              return { role: "assistant", content: message.content };
            }
            case "tool": {
              return message;
            }
          }
        })
      );
      break;
    }
    default: {
      const _exhaustiveCheck = prompt;
      throw new Error(`Unsupported prompt type: ${_exhaustiveCheck}`);
    }
  }
  return languageModelMessages;
}

I tested in compiled module inside node_modules/ai/rsc/dist/rsc-server.msj function convertToLanguageModelPrompt by adding

case "system": {
    return message;
}

Result:

Captura de pantalla 2024-05-06 a la(s) 2 01 11 p m

This works but the correction must be made in the repository of the dependency "ai": "^3.1.1".

And this depends on whether it is considered good practice to send system messages inside of the conversation to save new context (user events)

athrael-soju commented 1 month ago

@JoseAngelChepo Great find. If you like, you can post this in https://github.com/vercel/ai-chatbot/issues/325#issuecomment-2094917193 to consolidate comments under one post.

Honestly, removing system messages in this update left me baffled, a little bit. It just wasn't necessary. Moreover, there is not a single template using ai-rsc, or genUI that isn't riddled with bugs. I have a local clone that resolves most of the critical issues, but I'm finding more bugs that I have the time to open and there isn't much support in resolving them.

Perhaps it's best to let things be for now, until a new template is written from scratch.