import {
  ActionExecutionMessage,
  AgentStateMessage,
  ImageMessage,
  ResultMessage,
  Role,
  TextMessage
} from "./chunk-OWIGJONH.mjs";
import {
  __name
} from "./chunk-FHD4JECV.mjs";

// src/graphql/message-conversion/agui-to-gql.ts
function extractAgentName(message) {
  if (message.role !== "assistant") {
    throw new Error(`Cannot extract agent name from message with role ${message.role}`);
  }
  return message.agentName || "unknown";
}
__name(extractAgentName, "extractAgentName");
function isAgentStateMessage(message) {
  return message.role === "assistant" && "agentName" in message && "state" in message;
}
__name(isAgentStateMessage, "isAgentStateMessage");
function hasImageProperty(message) {
  const canContainImage = message.role === "assistant" || message.role === "user";
  if (!canContainImage || message.image === void 0) {
    return false;
  }
  const isMalformed = message.image.format === void 0 || message.image.bytes === void 0;
  if (isMalformed) {
    return false;
  }
  return true;
}
__name(hasImageProperty, "hasImageProperty");
function aguiToGQL(messages, actions, coAgentStateRenders) {
  const gqlMessages = [];
  messages = Array.isArray(messages) ? messages : [
    messages
  ];
  const toolCallNames = {};
  for (const message of messages) {
    if (isAgentStateMessage(message)) {
      const agentName = extractAgentName(message);
      const state = "state" in message && message.state ? message.state : {};
      gqlMessages.push(new AgentStateMessage({
        id: message.id,
        agentName,
        state,
        role: Role.assistant
      }));
      if ("generativeUI" in message && message.generativeUI && coAgentStateRenders) {
        coAgentStateRenders[agentName] = {
          name: agentName,
          render: message.generativeUI
        };
      }
      continue;
    }
    if (hasImageProperty(message)) {
      gqlMessages.push(aguiMessageWithImageToGQLMessage(message));
      continue;
    }
    if (message.role === "assistant" && message.toolCalls) {
      gqlMessages.push(aguiTextMessageToGQLMessage(message));
      for (const toolCall of message.toolCalls) {
        toolCallNames[toolCall.id] = toolCall.function.name;
        const actionExecMsg = aguiToolCallToGQLActionExecution(toolCall, message.id);
        if ("generativeUI" in message && message.generativeUI && actions) {
          const actionName = toolCall.function.name;
          const specificAction = Object.values(actions).find((action) => action.name === actionName);
          const wildcardAction = Object.values(actions).find((action) => action.name === "*");
          if (specificAction) {
            specificAction.render = message.generativeUI;
          } else if (wildcardAction) {
            wildcardAction.render = message.generativeUI;
          }
        }
        gqlMessages.push(actionExecMsg);
      }
      continue;
    }
    if (message.role === "developer" || message.role === "system" || message.role === "assistant" || message.role === "user") {
      gqlMessages.push(aguiTextMessageToGQLMessage(message));
      continue;
    }
    if (message.role === "tool") {
      gqlMessages.push(aguiToolMessageToGQLResultMessage(message, toolCallNames));
      continue;
    }
    throw new Error(`Unknown message role: "${message.role}" in message with id: ${message.id}`);
  }
  return gqlMessages;
}
__name(aguiToGQL, "aguiToGQL");
function aguiTextMessageToGQLMessage(message) {
  if (message.role !== "developer" && message.role !== "system" && message.role !== "assistant" && message.role !== "user") {
    throw new Error(`Cannot convert message with role ${message.role} to TextMessage`);
  }
  let roleValue;
  if (message.role === "developer") {
    roleValue = Role.developer;
  } else if (message.role === "system") {
    roleValue = Role.system;
  } else if (message.role === "assistant") {
    roleValue = Role.assistant;
  } else {
    roleValue = Role.user;
  }
  return new TextMessage({
    id: message.id,
    content: message.content || "",
    role: roleValue
  });
}
__name(aguiTextMessageToGQLMessage, "aguiTextMessageToGQLMessage");
function aguiToolCallToGQLActionExecution(toolCall, parentMessageId) {
  if (toolCall.type !== "function") {
    throw new Error(`Unsupported tool call type: ${toolCall.type}`);
  }
  let argumentsObj;
  if (typeof toolCall.function.arguments === "string") {
    try {
      argumentsObj = JSON.parse(toolCall.function.arguments);
    } catch (error) {
      console.warn(`Failed to parse tool call arguments for ${toolCall.function.name}:`, error);
      argumentsObj = {};
    }
  } else if (typeof toolCall.function.arguments === "object" && toolCall.function.arguments !== null) {
    argumentsObj = toolCall.function.arguments;
  } else {
    console.warn(`Invalid tool call arguments type for ${toolCall.function.name}:`, typeof toolCall.function.arguments);
    argumentsObj = {};
  }
  return new ActionExecutionMessage({
    id: toolCall.id,
    name: toolCall.function.name,
    arguments: argumentsObj,
    parentMessageId
  });
}
__name(aguiToolCallToGQLActionExecution, "aguiToolCallToGQLActionExecution");
function aguiToolMessageToGQLResultMessage(message, toolCallNames) {
  if (message.role !== "tool") {
    throw new Error(`Cannot convert message with role ${message.role} to ResultMessage`);
  }
  if (!message.toolCallId) {
    throw new Error("Tool message must have a toolCallId");
  }
  const actionName = toolCallNames[message.toolCallId] || "unknown";
  let resultContent;
  const messageContent = message.content || "";
  if (typeof messageContent === "string") {
    resultContent = messageContent;
  } else if (typeof messageContent === "object" && messageContent !== null) {
    try {
      resultContent = JSON.stringify(messageContent);
    } catch (error) {
      console.warn(`Failed to stringify tool result for ${actionName}:`, error);
      resultContent = String(messageContent);
    }
  } else {
    resultContent = String(messageContent);
  }
  return new ResultMessage({
    id: message.id,
    result: resultContent,
    actionExecutionId: message.toolCallId,
    actionName: message.toolName || actionName
  });
}
__name(aguiToolMessageToGQLResultMessage, "aguiToolMessageToGQLResultMessage");
function aguiMessageWithRenderToGQL(message, actions, coAgentStateRenders) {
  if (message.role === "assistant" && "generativeUI" in message && message.generativeUI && !message.toolCalls) {
    const gqlMessages = [];
    gqlMessages.push(new AgentStateMessage({
      id: message.id,
      agentName: "unknown",
      state: {},
      role: Role.assistant
    }));
    if (coAgentStateRenders) {
      coAgentStateRenders.unknown = {
        name: "unknown",
        render: message.generativeUI
      };
    }
    return gqlMessages;
  }
  return aguiToGQL([
    message
  ], actions, coAgentStateRenders);
}
__name(aguiMessageWithRenderToGQL, "aguiMessageWithRenderToGQL");
function aguiMessageWithImageToGQLMessage(message) {
  if (!hasImageProperty(message)) {
    throw new Error(`Cannot convert message to ImageMessage: missing format or bytes`);
  }
  let roleValue;
  if (message.role === "assistant") {
    roleValue = Role.assistant;
  } else {
    roleValue = Role.user;
  }
  if (message.role !== "assistant" && message.role !== "user") {
    throw new Error(`Cannot convert message with role ${message.role} to ImageMessage`);
  }
  return new ImageMessage({
    id: message.id,
    format: message.image.format,
    bytes: message.image.bytes,
    role: roleValue
  });
}
__name(aguiMessageWithImageToGQLMessage, "aguiMessageWithImageToGQLMessage");

export {
  aguiToGQL,
  aguiTextMessageToGQLMessage,
  aguiToolCallToGQLActionExecution,
  aguiToolMessageToGQLResultMessage,
  aguiMessageWithRenderToGQL,
  aguiMessageWithImageToGQLMessage
};
//# sourceMappingURL=chunk-62NE5S6M.mjs.map