@genkit-ai/anthropic
Version:
Genkit AI framework plugin for Anthropic APIs.
143 lines • 3.77 kB
JavaScript
import { MEDIA_TYPES, MediaTypeSchema } from "../../types.mjs";
import {
fromAnthropicCitation
} from "./citations.mjs";
import {
fromAnthropicCitation as fromAnthropicCitation2
} from "./citations.mjs";
function textBlockToPart(block) {
if (block.citations && block.citations.length > 0) {
const citations = block.citations.map((c) => 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 = 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 = 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(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}`
);
}
}
export {
citationsDeltaToPart,
convertDocumentSource,
createDocumentBlock,
fromAnthropicCitation2 as fromAnthropicCitation,
redactedThinkingBlockToPart,
textBlockToPart,
textDeltaToPart,
thinkingBlockToPart,
thinkingDeltaToPart,
toolUseBlockToPart,
webSearchToolResultBlockToPart
};
//# sourceMappingURL=shared.mjs.map