UNPKG

@genkit-ai/anthropic

Version:

Genkit AI framework plugin for Anthropic APIs.

113 lines (109 loc) 4.01 kB
import { Part } from 'genkit'; import { AnthropicDocumentOptions } from '../../types.mjs'; import { AnthropicCitationInput } from './citations.mjs'; export { fromAnthropicCitation } from './citations.mjs'; import '@anthropic-ai/sdk'; import '@anthropic-ai/sdk/resources/messages'; /** * Copyright 2025 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Shared utilities for converting Anthropic content blocks to Genkit Parts. * Uses structural typing so both stable and beta API types work with these functions. */ /** * Converts a text block to a Genkit Part, including citations if present. * Uses structural typing for compatibility with both stable and beta APIs. */ declare function textBlockToPart(block: { text: string; citations?: AnthropicCitationInput[] | null; }): Part; /** * Converts a tool_use block to a Genkit Part. */ declare function toolUseBlockToPart(block: { id: string; name: string; input: unknown; }): Part; /** * Converts a thinking block to a Genkit Part, including signature metadata if present. */ declare function thinkingBlockToPart(block: { thinking: string; signature?: string; }): Part; /** * Converts a redacted thinking block to a Genkit Part. */ declare function redactedThinkingBlockToPart(block: { data: string; }): Part; /** * Converts a web_search_tool_result block to a Genkit Part. */ declare function webSearchToolResultBlockToPart(block: { tool_use_id: string; content: unknown; }): Part; /** * Converts a text_delta to a Genkit Part. */ declare function textDeltaToPart(delta: { text: string; }): Part; /** * Converts a thinking_delta to a Genkit Part. */ declare function thinkingDeltaToPart(delta: { thinking: string; }): Part; /** * Converts a citations_delta to a Genkit Part for streaming. * Returns a text part with empty text and citation data in metadata. * Empty text is intentional: genkit's `.text` getter concatenates all text parts, * so empty strings contribute nothing to the final text while preserving the citation * in the parts array for consumers who need to access citation metadata. */ declare function citationsDeltaToPart(delta: { type: 'citations_delta'; citation: AnthropicCitationInput; }): Part | undefined; /** * Document block type constraint for generics. */ type DocumentBlockBase = { type: 'document'; source: unknown; title?: string | null; context?: string | null; citations?: { enabled?: boolean; } | null; }; /** * Converts AnthropicDocumentOptions to Anthropic's document block format. * Works for both stable and beta APIs via generics. */ declare function createDocumentBlock<T extends DocumentBlockBase>(options: AnthropicDocumentOptions, sourceConverter: (source: AnthropicDocumentOptions['source']) => T['source']): T; /** * Converts document source options to Anthropic's source format. * Works for both stable and beta APIs via a file handler callback. * The file handler is called for 'file' type sources, allowing different * behavior (error for stable, conversion for beta). */ declare function convertDocumentSource<T>(source: AnthropicDocumentOptions['source'], fileHandler: (fileId: string) => T): T; export { AnthropicCitationInput, citationsDeltaToPart, convertDocumentSource, createDocumentBlock, redactedThinkingBlockToPart, textBlockToPart, textDeltaToPart, thinkingBlockToPart, thinkingDeltaToPart, toolUseBlockToPart, webSearchToolResultBlockToPart };