UNPKG

@genkit-ai/anthropic

Version:

Genkit AI framework plugin for Anthropic APIs.

173 lines 5.33 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var shared_exports = {}; __export(shared_exports, { citationsDeltaToPart: () => citationsDeltaToPart, convertDocumentSource: () => convertDocumentSource, createDocumentBlock: () => createDocumentBlock, fromAnthropicCitation: () => import_citations2.fromAnthropicCitation, redactedThinkingBlockToPart: () => redactedThinkingBlockToPart, textBlockToPart: () => textBlockToPart, textDeltaToPart: () => textDeltaToPart, thinkingBlockToPart: () => thinkingBlockToPart, thinkingDeltaToPart: () => thinkingDeltaToPart, toolUseBlockToPart: () => toolUseBlockToPart, webSearchToolResultBlockToPart: () => webSearchToolResultBlockToPart }); module.exports = __toCommonJS(shared_exports); var import_types = require("../../types.js"); var import_citations = require("./citations.js"); var import_citations2 = require("./citations.js"); function textBlockToPart(block) { if (block.citations && block.citations.length > 0) { const citations = block.citations.map((c) => (0, import_citations.fromAnthropicCitation)(c)).filter((c) => c !== void 0); if (citations.length > 0) { return { text: block.text, metadata: { citations } }; } } return { text: block.text }; } function toolUseBlockToPart(block) { return { toolRequest: { ref: block.id, name: block.name, input: block.input } }; } function thinkingBlockToPart(block) { if (block.signature !== void 0) { return { reasoning: block.thinking, metadata: { thoughtSignature: block.signature } }; } return { reasoning: block.thinking }; } function redactedThinkingBlockToPart(block) { return { custom: { redactedThinking: block.data } }; } function webSearchToolResultBlockToPart(block) { return { text: `[Anthropic server tool result ${block.tool_use_id}] ${JSON.stringify(block.content)}`, metadata: { anthropicServerToolResult: { type: "web_search_tool_result", toolUseId: block.tool_use_id, content: block.content } } }; } function textDeltaToPart(delta) { return { text: delta.text }; } function thinkingDeltaToPart(delta) { return { reasoning: delta.thinking }; } function citationsDeltaToPart(delta) { const citation = (0, import_citations.fromAnthropicCitation)(delta.citation); if (citation) { return { text: "", metadata: { citations: [citation] } }; } return void 0; } function createDocumentBlock(options, sourceConverter) { return { type: "document", source: sourceConverter(options.source), ...options.title && { title: options.title }, ...options.context && { context: options.context }, ...options.citations && { citations: options.citations } }; } function convertDocumentSource(source, fileHandler) { switch (source.type) { case "text": return { type: "text", media_type: source.mediaType ?? "text/plain", data: source.data }; case "base64": return { type: "base64", media_type: source.mediaType, data: source.data }; case "file": return fileHandler(source.fileId); case "content": return { type: "content", content: source.content.map((item) => { if (item.type === "text") { return item; } const mediaTypeResult = import_types.MediaTypeSchema.safeParse( item.source.mediaType ); if (!mediaTypeResult.success) { throw new Error( `Unsupported image media type for Anthropic document content: ${item.source.mediaType}. Supported types: ${Object.values(import_types.MEDIA_TYPES).join(", ")}` ); } return { type: "image", source: { type: "base64", media_type: mediaTypeResult.data, data: item.source.data } }; }) }; case "url": return { type: "url", url: source.url }; default: throw new Error( `Unsupported document source type: ${source.type}` ); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { citationsDeltaToPart, convertDocumentSource, createDocumentBlock, fromAnthropicCitation, redactedThinkingBlockToPart, textBlockToPart, textDeltaToPart, thinkingBlockToPart, thinkingDeltaToPart, toolUseBlockToPart, webSearchToolResultBlockToPart }); //# sourceMappingURL=shared.js.map