Description
Feature Description
Currently, the convertToLanguageModelMessage function handles specific roles ("system", "user", "assistant", and "tool") for formatting messages into a language model-compliant format. However, it does not support custom roles, which limits flexibility in defining new message roles that may be required for extended functionalities.
To enhance the versatility of the message conversion, I propose adding support for custom roles in the convertToLanguageModelMessage function. This could be achieved by either:
Allowing the function to accept and pass through custom roles without explicit case statements.
Adding a configurable mechanism to register and handle custom roles dynamically.
Use Case
Adding a custom role, such as "moderator", for specific types of messages:
const customMessage = {
role: "moderator",
content: "Please adhere to community guidelines."
};
// Current implementation throws an error for unsupported roles.
With the proposed enhancement, the function would handle this custom role gracefully, allowing developers to extend the functionality without modifying the core code.
Additional context
Current code:
function convertToLanguageModelMessage(message) {
switch (message.role) {
case "system": {
return { role: "system", content: message.content };
}
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;
}
default: {
const _exhaustiveCheck = message;
throw new Error(`Unsupported message role: ${_exhaustiveCheck}`);
}
}
}
Proposed Changes:
Modify the switch statement to handle unknown roles gracefully.
Optionally, introduce a registry or configuration for custom roles to ensure proper formatting and handling.
This would extend the utility of defining and handling new message roles in the context of making a custom provider