@genkit-ai/anthropic
Version:
Genkit AI framework plugin for Anthropic APIs.
1 lines • 3.05 kB
Source Map (JSON)
{"version":3,"sources":["../src/utils.ts"],"sourcesContent":["/**\n * Copyright 2025 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 { CacheControlEphemeral } from '@anthropic-ai/sdk/resources/messages';\n\n/**\n * Creates a cache control metadata object for prompt caching.\n * Returns `{ cache_control: ... }` so it can be spread into metadata.\n *\n * @param options - Cache control options. Type defaults to 'ephemeral'.\n * @returns Object with cache_control property to spread into part metadata.\n *\n * @example\n * ```ts\n * import { anthropic, cacheControl } from '@genkit-ai/anthropic';\n *\n * const response = await ai.generate({\n * model: anthropic.model('claude-sonnet-4-5'),\n * system: {\n * text: longSystemPrompt,\n * metadata: { ...cacheControl() } // default ephemeral\n * },\n * messages: [{ role: 'user', content: [{ text: 'Hello' }] }]\n * });\n *\n * // Or with explicit TTL:\n * metadata: { ...cacheControl({ ttl: '1h' }) }\n * ```\n */\nexport function cacheControl(options?: Partial<CacheControlEphemeral>): {\n cache_control: CacheControlEphemeral;\n} {\n return {\n cache_control: {\n type: options?.type ?? 'ephemeral',\n ...(options?.ttl && { ttl: options.ttl }),\n },\n };\n}\n\n/**\n * Strips the 'anthropic/' namespace prefix if present.\n * Throws if the model name is missing.\n */\nexport function checkModelName(name?: string): string {\n const cleanName = name?.replace(/^anthropic\\//, '');\n if (!cleanName) {\n throw new Error('Model name is required.');\n }\n return cleanName;\n}\n\nexport function removeUndefinedProperties<T>(obj: T): T {\n if (typeof obj !== 'object' || obj === null) {\n return obj;\n }\n\n return Object.fromEntries(\n Object.entries(obj).filter(([_, value]) => value !== undefined)\n ) as T;\n}\n\nexport function isKnownKey<T extends object>(\n key: string | number | symbol,\n obj: T\n): key is keyof T {\n return key in obj;\n}\n"],"mappings":"AA0CO,SAAS,aAAa,SAE3B;AACA,SAAO;AAAA,IACL,eAAe;AAAA,MACb,MAAM,SAAS,QAAQ;AAAA,MACvB,GAAI,SAAS,OAAO,EAAE,KAAK,QAAQ,IAAI;AAAA,IACzC;AAAA,EACF;AACF;AAMO,SAAS,eAAe,MAAuB;AACpD,QAAM,YAAY,MAAM,QAAQ,gBAAgB,EAAE;AAClD,MAAI,CAAC,WAAW;AACd,UAAM,IAAI,MAAM,yBAAyB;AAAA,EAC3C;AACA,SAAO;AACT;AAEO,SAAS,0BAA6B,KAAW;AACtD,MAAI,OAAO,QAAQ,YAAY,QAAQ,MAAM;AAC3C,WAAO;AAAA,EACT;AAEA,SAAO,OAAO;AAAA,IACZ,OAAO,QAAQ,GAAG,EAAE,OAAO,CAAC,CAAC,GAAG,KAAK,MAAM,UAAU,MAAS;AAAA,EAChE;AACF;AAEO,SAAS,WACd,KACA,KACgB;AAChB,SAAO,OAAO;AAChB;","names":[]}