@copilotkit/runtime
Version:
<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />
1 lines • 4.3 kB
Source Map (JSON)
{"version":3,"file":"google-genai-adapter.mjs","names":[],"sources":["../../../src/service-adapters/google/google-genai-adapter.ts"],"sourcesContent":["/**\n * Copilot Runtime adapter for Google Generative AI (e.g. Gemini).\n *\n * ## Example\n *\n * ```ts\n * import { CopilotRuntime, GoogleGenerativeAIAdapter } from \"@copilotkit/runtime\";\n * const { GoogleGenerativeAI } = require(\"@google/generative-ai\");\n *\n * const genAI = new GoogleGenerativeAI(process.env[\"GOOGLE_API_KEY\"]);\n *\n * const copilotKit = new CopilotRuntime();\n *\n * return new GoogleGenerativeAIAdapter({ model: \"gemini-2.5-flash\", apiVersion: \"v1\" });\n * ```\n */\nimport { LangChainAdapter } from \"../langchain/langchain-adapter\";\n\ninterface GoogleGenerativeAIAdapterOptions {\n /**\n * A custom Google Generative AI model to use.\n */\n model?: string;\n /**\n * The API version to use (e.g. \"v1\" or \"v1beta\"). Defaults to \"v1\".\n */\n apiVersion?: \"v1\" | \"v1beta\";\n /**\n * The API key to use.\n */\n apiKey?: string;\n}\n\nconst DEFAULT_MODEL = \"gemini-2.5-flash\";\nconst DEFAULT_API_VERSION: GoogleGenerativeAIAdapterOptions[\"apiVersion\"] =\n \"v1\";\nlet hasWarnedDefaultGoogleModel = false;\n\nexport class GoogleGenerativeAIAdapter extends LangChainAdapter {\n public provider = \"google\";\n public model: string = DEFAULT_MODEL;\n\n constructor(options?: GoogleGenerativeAIAdapterOptions) {\n if (\n !hasWarnedDefaultGoogleModel &&\n !options?.model &&\n !options?.apiVersion\n ) {\n console.warn(\n `You are using the GoogleGenerativeAIAdapter without explicitly setting a model or apiVersion. ` +\n `CopilotKit will default to apiVersion=\"v1\" and model=\"${DEFAULT_MODEL}\". ` +\n `To silence this warning, pass model and apiVersion when constructing the adapter.`,\n );\n hasWarnedDefaultGoogleModel = true;\n }\n\n super({\n chainFn: async ({ messages, tools, threadId }) => {\n // Lazy require for optional peer dependencies\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const { ChatGoogle } = require(\"@langchain/google-gauth\");\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const { AIMessage } = require(\"@langchain/core/messages\");\n\n // Filter out empty assistant messages to prevent Gemini validation errors\n // Gemini specifically rejects conversations containing AIMessages with empty content\n const filteredMessages = messages.filter((message) => {\n // Keep all non-AI messages (HumanMessage, SystemMessage, ToolMessage, etc.)\n if (!(message instanceof AIMessage)) {\n return true;\n }\n\n // For AIMessages, only keep those with non-empty content\n // Also keep AIMessages with tool_calls even if content is empty\n const aiMsg = message as any;\n return (\n (aiMsg.content && String(aiMsg.content).trim().length > 0) ||\n (aiMsg.tool_calls && aiMsg.tool_calls.length > 0)\n );\n });\n\n this.model = options?.model ?? DEFAULT_MODEL;\n const model = new ChatGoogle({\n apiKey: options?.apiKey ?? process.env.GOOGLE_API_KEY,\n modelName: this.model,\n apiVersion: options?.apiVersion ?? DEFAULT_API_VERSION,\n }).bindTools(tools);\n\n return model.stream(filteredMessages, {\n metadata: { conversation_id: threadId },\n });\n },\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAiCA,MAAM,gBAAgB;AACtB,MAAM,sBACJ;AACF,IAAI,8BAA8B;AAElC,IAAa,4BAAb,cAA+C,iBAAiB;CAI9D,YAAY,SAA4C;AACtD,MACE,CAAC,+BACD,CAAC,SAAS,SACV,CAAC,SAAS,YACV;AACA,WAAQ,KACN,uJAC2D,cAAc,sFAE1E;AACD,iCAA8B;;AAGhC,QAAM,EACJ,SAAS,OAAO,EAAE,UAAU,OAAO,eAAe;GAGhD,MAAM,EAAE,yBAAuB,0BAA0B;GAEzD,MAAM,EAAE,wBAAsB,2BAA2B;GAIzD,MAAM,mBAAmB,SAAS,QAAQ,YAAY;AAEpD,QAAI,EAAE,mBAAmB,WACvB,QAAO;IAKT,MAAM,QAAQ;AACd,WACG,MAAM,WAAW,OAAO,MAAM,QAAQ,CAAC,MAAM,CAAC,SAAS,KACvD,MAAM,cAAc,MAAM,WAAW,SAAS;KAEjD;AAEF,QAAK,QAAQ,SAAS,SAAS;AAO/B,UANc,IAAI,WAAW;IAC3B,QAAQ,SAAS,UAAU,QAAQ,IAAI;IACvC,WAAW,KAAK;IAChB,YAAY,SAAS,cAAc;IACpC,CAAC,CAAC,UAAU,MAAM,CAEN,OAAO,kBAAkB,EACpC,UAAU,EAAE,iBAAiB,UAAU,EACxC,CAAC;KAEL,CAAC;kBArDc;eACK"}