{"version":3,"sources":["../../src/util/message.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { PromptMessage } from '@modelcontextprotocol/sdk/types.js' with { 'resolution-mode': 'import' };\nimport { type MessageData, type Part } from 'genkit';\n\nconst ROLE_MAP = {\n  user: 'user',\n  assistant: 'model',\n} as const;\n\n/**\n * Converts an MCP (Model Context Protocol) PromptMessage into Genkit's\n * MessageData format. This involves mapping MCP roles (user, assistant) to\n * Genkit roles (user, model) and transforming the MCP content part into a\n * Genkit Part.\n *\n * @param message The MCP PromptMessage to convert.\n * @returns The corresponding Genkit MessageData object.\n */\nexport function fromMcpPromptMessage(message: PromptMessage): MessageData {\n  return {\n    role: ROLE_MAP[message.role],\n    content: [fromMcpPart(message.content)],\n  };\n}\n\n/**\n * Converts an MCP message content part (text, image, or resource) into a Genkit\n * Part.\n * - Text parts are directly mapped.\n * - Image parts are converted to Genkit media parts with a data URL.\n * - Resource parts currently result in an empty Genkit Part (further\n *   implementation may be needed).\n *\n * @param part The MCP PromptMessage content part to convert.\n * @returns The corresponding Genkit Part.\n */\nexport function fromMcpPart(part: PromptMessage['content']): Part {\n  switch (part.type) {\n    case 'text':\n      return { text: part.text };\n    case 'image':\n      return {\n        media: {\n          contentType: part.mimeType,\n          url: `data:${part.mimeType};base64,${part.data}`,\n        },\n      };\n    case 'resource':\n      return {\n        resource: {\n          uri: part.resource.uri,\n        },\n      };\n    default:\n      return {};\n  }\n}\n"],"mappings":"AAmBA,MAAM,WAAW;AAAA,EACf,MAAM;AAAA,EACN,WAAW;AACb;AAWO,SAAS,qBAAqB,SAAqC;AACxE,SAAO;AAAA,IACL,MAAM,SAAS,QAAQ,IAAI;AAAA,IAC3B,SAAS,CAAC,YAAY,QAAQ,OAAO,CAAC;AAAA,EACxC;AACF;AAaO,SAAS,YAAY,MAAsC;AAChE,UAAQ,KAAK,MAAM;AAAA,IACjB,KAAK;AACH,aAAO,EAAE,MAAM,KAAK,KAAK;AAAA,IAC3B,KAAK;AACH,aAAO;AAAA,QACL,OAAO;AAAA,UACL,aAAa,KAAK;AAAA,UAClB,KAAK,QAAQ,KAAK,QAAQ,WAAW,KAAK,IAAI;AAAA,QAChD;AAAA,MACF;AAAA,IACF,KAAK;AACH,aAAO;AAAA,QACL,UAAU;AAAA,UACR,KAAK,KAAK,SAAS;AAAA,QACrB;AAAA,MACF;AAAA,IACF;AACE,aAAO,CAAC;AAAA,EACZ;AACF;","names":[]}