fix: move url utils from core to openai adapters
This commit is contained in:
parent
978a79dd95
commit
b4526bf4fb
9 changed files with 133 additions and 59 deletions
|
|
@ -30,6 +30,7 @@ import { safeParseToolCallArgs } from "../../tools/parseArgs.js";
|
|||
import { renderChatMessage, stripImages } from "../../util/messageContent.js";
|
||||
import { DEFAULT_REASONING_TOKENS } from "../constants.js";
|
||||
import { BaseLLM } from "../index.js";
|
||||
import { extractBase64FromDataUrl } from "../../util/url.js";
|
||||
|
||||
class Anthropic extends BaseLLM {
|
||||
static providerName = "anthropic";
|
||||
|
|
@ -105,14 +106,22 @@ class Anthropic extends BaseLLM {
|
|||
});
|
||||
}
|
||||
} else {
|
||||
parts.push({
|
||||
type: "image",
|
||||
source: {
|
||||
type: "base64",
|
||||
media_type: getAnthropicMediaTypeFromDataUrl(part.imageUrl.url),
|
||||
data: part.imageUrl.url.split(",")[1],
|
||||
},
|
||||
});
|
||||
const base64Data = extractBase64FromDataUrl(part.imageUrl.url);
|
||||
if (base64Data) {
|
||||
parts.push({
|
||||
type: "image",
|
||||
source: {
|
||||
type: "base64",
|
||||
media_type: getAnthropicMediaTypeFromDataUrl(part.imageUrl.url),
|
||||
data: base64Data,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
console.warn(
|
||||
"Anthropic: skipping image with invalid data URL format",
|
||||
part.imageUrl.url,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import { BaseLLM } from "../index.js";
|
|||
import { PROVIDER_TOOL_SUPPORT } from "../toolSupport.js";
|
||||
import { getSecureID } from "../utils/getSecureID.js";
|
||||
import { withLLMRetry } from "../utils/retry.js";
|
||||
import { parseDataUrl } from "../../util/url.js";
|
||||
|
||||
interface ModelConfig {
|
||||
formatPayload: (text: string) => any;
|
||||
|
|
@ -545,8 +546,9 @@ class Bedrock extends BaseLLM {
|
|||
if (part.type === "text") {
|
||||
blocks.push({ text: part.text });
|
||||
} else if (part.type === "imageUrl" && part.imageUrl) {
|
||||
try {
|
||||
const [mimeType, base64Data] = part.imageUrl.url.split(",");
|
||||
const parsed = parseDataUrl(part.imageUrl.url);
|
||||
if (parsed) {
|
||||
const { mimeType, base64Data } = parsed;
|
||||
const format = mimeType.split("/")[1]?.split(";")[0] || "jpeg";
|
||||
if (
|
||||
format === ImageFormat.JPEG ||
|
||||
|
|
@ -568,8 +570,8 @@ class Bedrock extends BaseLLM {
|
|||
part,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("Bedrock: failed to process image part", error, part);
|
||||
} else {
|
||||
console.warn("Bedrock: failed to process image part", part);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import {
|
|||
GeminiToolFunctionDeclaration,
|
||||
convertContinueToolToGeminiFunction,
|
||||
} from "./gemini-types";
|
||||
import { extractBase64FromDataUrl } from "../../util/url.js";
|
||||
|
||||
class Gemini extends BaseLLM {
|
||||
static providerName = "gemini";
|
||||
|
|
@ -184,16 +185,31 @@ class Gemini extends BaseLLM {
|
|||
}
|
||||
|
||||
continuePartToGeminiPart(part: MessagePart): GeminiChatContentPart {
|
||||
return part.type === "text"
|
||||
? {
|
||||
text: part.text,
|
||||
}
|
||||
: {
|
||||
inlineData: {
|
||||
mimeType: "image/jpeg",
|
||||
data: part.imageUrl?.url.split(",")[1],
|
||||
},
|
||||
};
|
||||
if (part.type === "text") {
|
||||
return {
|
||||
text: part.text,
|
||||
};
|
||||
}
|
||||
|
||||
let data = "";
|
||||
if (part.imageUrl?.url) {
|
||||
const extracted = extractBase64FromDataUrl(part.imageUrl.url);
|
||||
if (extracted) {
|
||||
data = extracted;
|
||||
} else {
|
||||
console.warn(
|
||||
"Gemini: skipping image with invalid data URL format",
|
||||
part.imageUrl.url,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
inlineData: {
|
||||
mimeType: "image/jpeg",
|
||||
data,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public prepareBody(
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import {
|
|||
import { renderChatMessage } from "../../util/messageContent.js";
|
||||
import { getRemoteModelInfo } from "../../util/ollamaHelper.js";
|
||||
import { BaseLLM } from "../index.js";
|
||||
import { extractBase64FromDataUrl } from "../../util/url.js";
|
||||
|
||||
type OllamaChatMessage = {
|
||||
role: ChatMessageRole;
|
||||
|
|
@ -303,10 +304,16 @@ class Ollama extends BaseLLM implements ModelInstaller {
|
|||
const images: string[] = [];
|
||||
message.content.forEach((part) => {
|
||||
if (part.type === "imageUrl" && part.imageUrl) {
|
||||
const image = part.imageUrl?.url.split(",").at(-1);
|
||||
const image = part.imageUrl?.url
|
||||
? extractBase64FromDataUrl(part.imageUrl.url)
|
||||
: undefined;
|
||||
if (image) {
|
||||
images.push(image);
|
||||
}
|
||||
} else if (part.imageUrl?.url) {
|
||||
console.warn(
|
||||
"Ollama: skipping image with invalid data URL format",
|
||||
part.imageUrl.url,
|
||||
);
|
||||
}
|
||||
});
|
||||
if (images.length > 0) {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,8 @@
|
|||
import {
|
||||
parseDataUrl as parseDataUrlFromAdapter,
|
||||
extractBase64FromDataUrl as extractBase64FromDataUrlFromAdapter,
|
||||
} from "@continuedev/openai-adapters";
|
||||
|
||||
export function canParseUrl(url: string): boolean {
|
||||
if ((URL as any)?.canParse) {
|
||||
return (URL as any).canParse(url);
|
||||
|
|
@ -9,3 +14,6 @@ export function canParseUrl(url: string): boolean {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export const parseDataUrl = parseDataUrlFromAdapter;
|
||||
export const extractBase64FromDataUrl = extractBase64FromDataUrlFromAdapter;
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import {
|
|||
CompletionUsage,
|
||||
} from "openai/resources/index";
|
||||
import { ChatCompletionCreateParams } from "openai/resources/index.js";
|
||||
import { extractBase64FromDataUrl } from "../util/url.js";
|
||||
import { AnthropicConfig } from "../types.js";
|
||||
import {
|
||||
chatChunk,
|
||||
|
|
@ -194,14 +195,22 @@ export class AnthropicApi implements BaseLlmApi {
|
|||
if (part.type === "image_url") {
|
||||
const dataUrl = part.image_url.url;
|
||||
if (dataUrl?.startsWith("data:")) {
|
||||
blocks.push({
|
||||
type: "image",
|
||||
source: {
|
||||
type: "base64",
|
||||
media_type: getAnthropicMediaTypeFromDataUrl(dataUrl),
|
||||
data: dataUrl.split(",")[1],
|
||||
},
|
||||
});
|
||||
const base64Data = extractBase64FromDataUrl(dataUrl);
|
||||
if (base64Data) {
|
||||
blocks.push({
|
||||
type: "image",
|
||||
source: {
|
||||
type: "base64",
|
||||
media_type: getAnthropicMediaTypeFromDataUrl(dataUrl),
|
||||
data: base64Data,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
console.warn(
|
||||
"Anthropic: skipping image with invalid data URL format",
|
||||
dataUrl,
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const text = part.type === "text" ? part.text : part.refusal;
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ import {
|
|||
|
||||
import { fromNodeProviderChain } from "@aws-sdk/credential-providers";
|
||||
import { fromStatic } from "@aws-sdk/token-providers";
|
||||
import { parseDataUrl } from "../util/url.js";
|
||||
import { BedrockConfig } from "../types.js";
|
||||
import { chatChunk, chatChunkFromDelta, embedding, rerank } from "../util.js";
|
||||
import { safeParseArgs } from "../util/parseArgs.js";
|
||||
|
|
@ -134,35 +135,35 @@ export class BedrockApi implements BaseLlmApi {
|
|||
throw new Error("Unsupported part type: input_audio");
|
||||
case "image_url":
|
||||
default:
|
||||
try {
|
||||
const [mimeType, base64Data] = (
|
||||
part as ChatCompletionContentPartImage
|
||||
).image_url.url.split(",");
|
||||
const format = mimeType.split("/")[1]?.split(";")[0] || "jpeg";
|
||||
if (
|
||||
format === ImageFormat.JPEG ||
|
||||
format === ImageFormat.PNG ||
|
||||
format === ImageFormat.WEBP ||
|
||||
format === ImageFormat.GIF
|
||||
) {
|
||||
return {
|
||||
image: {
|
||||
format,
|
||||
source: {
|
||||
bytes: Uint8Array.from(Buffer.from(base64Data, "base64")),
|
||||
},
|
||||
},
|
||||
};
|
||||
} else {
|
||||
console.warn(
|
||||
`Bedrock: skipping unsupported image part format: ${format}`,
|
||||
);
|
||||
return { text: "[Unsupported image format]" };
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("Bedrock: failed to process image part", error);
|
||||
const parsed = parseDataUrl(
|
||||
(part as ChatCompletionContentPartImage).image_url.url,
|
||||
);
|
||||
if (!parsed) {
|
||||
console.warn("Bedrock: failed to process image part - invalid URL");
|
||||
return { text: "[Failed to process image]" };
|
||||
}
|
||||
const { mimeType, base64Data } = parsed;
|
||||
const format = mimeType.split("/")[1]?.split(";")[0] || "jpeg";
|
||||
if (
|
||||
format === ImageFormat.JPEG ||
|
||||
format === ImageFormat.PNG ||
|
||||
format === ImageFormat.WEBP ||
|
||||
format === ImageFormat.GIF
|
||||
) {
|
||||
return {
|
||||
image: {
|
||||
format,
|
||||
source: {
|
||||
bytes: Uint8Array.from(Buffer.from(base64Data, "base64")),
|
||||
},
|
||||
},
|
||||
};
|
||||
} else {
|
||||
console.warn(
|
||||
`Bedrock: skipping unsupported image part format: ${format}`,
|
||||
);
|
||||
return { text: "[Unsupported image format]" };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -190,3 +190,4 @@ export {
|
|||
} from "./apis/AnthropicUtils.js";
|
||||
|
||||
export { isResponsesModel } from "./apis/openaiResponses.js";
|
||||
export { parseDataUrl, extractBase64FromDataUrl } from "./util/url.js";
|
||||
|
|
|
|||
21
packages/openai-adapters/src/util/url.ts
Normal file
21
packages/openai-adapters/src/util/url.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
export function parseDataUrl(dataUrl: string):
|
||||
| {
|
||||
mimeType: string;
|
||||
base64Data: string;
|
||||
}
|
||||
| undefined {
|
||||
const urlParts = dataUrl.split(",");
|
||||
|
||||
if (urlParts.length < 2) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const [mimeType, ...base64Parts] = urlParts;
|
||||
const base64Data = base64Parts.join(",");
|
||||
|
||||
return { mimeType, base64Data };
|
||||
}
|
||||
|
||||
export function extractBase64FromDataUrl(dataUrl: string): string | undefined {
|
||||
return parseDataUrl(dataUrl)?.base64Data;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue