1
0
Fork 0

Convert dataurl to blob

This commit is contained in:
Pabloader 2026-04-16 12:42:49 +00:00
parent cc8f99084b
commit 710e866de9
2 changed files with 15 additions and 6 deletions

View File

@ -284,8 +284,19 @@ namespace Prompt {
return lines.join('\n');
}
const objectUrls = new Map<string, string>();
function stripDataUrlImages(text: string): string {
return text.replace(/!\[([^\]]*)\]\(data:[^)]+\)/g, (_, alt) => `[image: ${alt}]`);
return text.replace(/!\[([^\]]*)\]\((data:([^;]*);base64,[^)]+)\)/g, (_, alt, dataUrl, type) => {
let blobUrl = objectUrls.get(dataUrl);
if (!blobUrl) {
const b64 = dataUrl.replace(/^data:[^;]*;base64,/, '');
const imageData = atob(b64);
const blob = new Blob([imageData], { type });
blobUrl = URL.createObjectURL(blob);
objectUrls.set(dataUrl, blobUrl);
}
return `![${alt}](${blobUrl})`;
});
}
export function substituteVars(state: AppState, text: string): string {

View File

@ -478,14 +478,14 @@ export namespace Tools {
if (!appState.imageModel) {
return 'Error: No image model configured';
}
const { width: defaultWidth, height: defaultHeight, negative_prompt, sampler_name } = appState.imageGenerationSettings;
const { width, height, negative_prompt, sampler_name } = appState.imageGenerationSettings;
const response = await LLM.generateImage(appState.connection, {
model: appState.imageModel.id,
prompt: args.prompt,
output_format: 'jpeg',
image_settings: {
width: args.width ?? defaultWidth,
height: args.height ?? defaultHeight,
width,
height,
negative_prompt: negative_prompt || undefined,
sampler_name: sampler_name || undefined,
},
@ -502,8 +502,6 @@ export namespace Tools {
description: 'Generate an image from a text prompt. Format prompt as tags: masterpiece, best quality, ...',
parameters: Type.Object({
prompt: Type.String({ description: 'The image generation prompt' }),
width: Type.Optional(Type.Integer({ description: 'Image width in pixels' })),
height: Type.Optional(Type.Integer({ description: 'Image height in pixels' })),
}),
}),
};