@genkit-ai/anthropic
Version:
Genkit AI framework plugin for Anthropic APIs.
1 lines • 6.29 kB
Source Map (JSON)
{"version":3,"sources":["../../../src/runner/converters/citations.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\n/**\n * Citation conversion utilities for Anthropic API responses.\n * Handles validation and conversion of citations from Anthropic's format to Genkit format.\n */\n\nimport { z } from 'genkit';\nimport type { AnthropicCitation } from '../../types.mjs';\n\n/** Structural type for Anthropic citations (works with both stable and beta APIs). */\nexport interface AnthropicCitationInput {\n type: string;\n cited_text: string;\n // document_index is optional since web search citations don't have it\n document_index?: number;\n document_title?: string | null;\n file_id?: string | null;\n start_char_index?: number;\n end_char_index?: number;\n start_page_number?: number;\n end_page_number?: number;\n start_block_index?: number;\n end_block_index?: number;\n}\n\n// --- Citation validation schemas ---\n\n/**\n * Base citation schema with common fields shared across all citation types.\n */\nconst baseCitationSchema = z.object({\n cited_text: z.string(),\n document_index: z.number(),\n document_title: z.string().nullable().optional(),\n file_id: z.string().nullable().optional(),\n});\n\n/**\n * Schema for character location citations (plain text documents).\n */\nconst charLocationCitationSchema = baseCitationSchema.extend({\n type: z.literal('char_location'),\n start_char_index: z.number(),\n end_char_index: z.number(),\n});\n\n/**\n * Schema for page location citations (PDF documents).\n */\nconst pageLocationCitationSchema = baseCitationSchema.extend({\n type: z.literal('page_location'),\n start_page_number: z.number(),\n end_page_number: z.number(),\n});\n\n/**\n * Schema for content block location citations (custom content documents).\n */\nconst contentBlockLocationCitationSchema = baseCitationSchema.extend({\n type: z.literal('content_block_location'),\n start_block_index: z.number(),\n end_block_index: z.number(),\n});\n\n/**\n * Discriminated union schema for all supported citation types.\n */\nconst citationSchema = z.discriminatedUnion('type', [\n charLocationCitationSchema,\n pageLocationCitationSchema,\n contentBlockLocationCitationSchema,\n]);\n\n/**\n * Converts Anthropic's citation format (snake_case) to genkit format (camelCase).\n * Only handles document-based citations (char_location, page_location, content_block_location).\n * Skips web search and other citation types that don't reference documents.\n */\nexport function fromAnthropicCitation(\n citation: AnthropicCitationInput\n): AnthropicCitation | undefined {\n // Skip citations without document_index (e.g., web search results)\n if (citation.document_index === undefined) {\n return undefined;\n }\n\n // Validate and parse citation with Zod\n const result = citationSchema.safeParse(citation);\n if (!result.success) {\n // Invalid citation structure, skip it gracefully\n return undefined;\n }\n\n const parsed = result.data;\n\n // Convert validated citation to Genkit format\n switch (parsed.type) {\n case 'char_location':\n return {\n type: 'char_location',\n citedText: parsed.cited_text,\n documentIndex: parsed.document_index,\n documentTitle: parsed.document_title ?? undefined,\n fileId: parsed.file_id ?? undefined,\n startCharIndex: parsed.start_char_index,\n endCharIndex: parsed.end_char_index,\n };\n case 'page_location':\n return {\n type: 'page_location',\n citedText: parsed.cited_text,\n documentIndex: parsed.document_index,\n documentTitle: parsed.document_title ?? undefined,\n fileId: parsed.file_id ?? undefined,\n startPageNumber: parsed.start_page_number,\n endPageNumber: parsed.end_page_number,\n };\n case 'content_block_location':\n return {\n type: 'content_block_location',\n citedText: parsed.cited_text,\n documentIndex: parsed.document_index,\n documentTitle: parsed.document_title ?? undefined,\n fileId: parsed.file_id ?? undefined,\n startBlockIndex: parsed.start_block_index,\n endBlockIndex: parsed.end_block_index,\n };\n }\n}\n"],"mappings":"AAqBA,SAAS,SAAS;AAwBlB,MAAM,qBAAqB,EAAE,OAAO;AAAA,EAClC,YAAY,EAAE,OAAO;AAAA,EACrB,gBAAgB,EAAE,OAAO;AAAA,EACzB,gBAAgB,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS;AAAA,EAC/C,SAAS,EAAE,OAAO,EA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