fix(ai): route vip98k image tools correctly
This commit is contained in:
parent
b73afcdfdc
commit
4b5540f3c2
@ -650,6 +650,15 @@ function buildTaskBody(params: Vip98kTaskParams, invocation: Vip98kInvocation |
|
|||||||
if ("negativePrompt" in body || !invocation) body.negativePrompt = negativePrompt;
|
if ("negativePrompt" in body || !invocation) body.negativePrompt = negativePrompt;
|
||||||
if ("negative_prompt" in body || !invocation) body.negative_prompt = negativePrompt;
|
if ("negative_prompt" in body || !invocation) body.negative_prompt = negativePrompt;
|
||||||
}
|
}
|
||||||
|
const imageToolParamAliases = [
|
||||||
|
["scaleFactor", "scale_factor"],
|
||||||
|
["xScale", "x_scale"],
|
||||||
|
["yScale", "y_scale"],
|
||||||
|
] as const;
|
||||||
|
for (const [sourceKey, targetKey] of imageToolParamAliases) {
|
||||||
|
const value = imageParams[sourceKey];
|
||||||
|
if (value !== undefined && targetKey in body) body[targetKey] = value;
|
||||||
|
}
|
||||||
if (typeof videoParams.generateAudio === "boolean") {
|
if (typeof videoParams.generateAudio === "boolean") {
|
||||||
if ("generateAudio" in body || !invocation) body.generateAudio = videoParams.generateAudio;
|
if ("generateAudio" in body || !invocation) body.generateAudio = videoParams.generateAudio;
|
||||||
if ("generate_audio" in body || !invocation) body.generate_audio = videoParams.generateAudio;
|
if ("generate_audio" in body || !invocation) body.generate_audio = videoParams.generateAudio;
|
||||||
|
|||||||
@ -16,6 +16,14 @@ const KNOWN_IMAGE_TOOL_INPUTS: Record<string, KnownImageInput> = {
|
|||||||
"openai/gpt-image-2/edit": "images",
|
"openai/gpt-image-2/edit": "images",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const KNOWN_IMAGE_PROCESSING_TOOL_REQUESTS: Record<
|
||||||
|
string,
|
||||||
|
Record<string, unknown>
|
||||||
|
> = {
|
||||||
|
"remove-background": { image_url: "" },
|
||||||
|
enhance: { image_url: "", scale_factor: 4 },
|
||||||
|
};
|
||||||
|
|
||||||
function getInvocationToolId(invocation: Vip98kTaskInvocation | null) {
|
function getInvocationToolId(invocation: Vip98kTaskInvocation | null) {
|
||||||
if (!invocation || invocation.kind !== "tool") return null;
|
if (!invocation || invocation.kind !== "tool") return null;
|
||||||
const query = invocation.path.split("?", 2)[1] || "";
|
const query = invocation.path.split("?", 2)[1] || "";
|
||||||
@ -46,6 +54,22 @@ function createKnownImageToolInvocation(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createKnownProcessingToolInvocation(
|
||||||
|
modelId: string,
|
||||||
|
requestExample: Record<string, unknown>
|
||||||
|
): Vip98kTaskInvocation {
|
||||||
|
const encodedModelId = encodeURIComponent(modelId);
|
||||||
|
return {
|
||||||
|
kind: "tool",
|
||||||
|
method: "POST",
|
||||||
|
path: `/api/external/tools/generate?tool_id=${encodedModelId}`,
|
||||||
|
contentType: "application/json",
|
||||||
|
requestExample: { ...requestExample },
|
||||||
|
resultMethod: "GET",
|
||||||
|
resultPath: `/api/external/tools/result?tool_id=${encodedModelId}&taskId={task_id}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A frontend alias may resolve to a different VIP98K tool at runtime. In that
|
* A frontend alias may resolve to a different VIP98K tool at runtime. In that
|
||||||
* case the selected model's invocation cannot be reused because its endpoint
|
* case the selected model's invocation cannot be reused because its endpoint
|
||||||
@ -56,6 +80,14 @@ export function resolveVip98kTaskInvocation(
|
|||||||
configuredInvocation: Vip98kTaskInvocation | null
|
configuredInvocation: Vip98kTaskInvocation | null
|
||||||
) {
|
) {
|
||||||
const normalizedModelId = modelId.trim().toLowerCase();
|
const normalizedModelId = modelId.trim().toLowerCase();
|
||||||
|
const processingRequest = KNOWN_IMAGE_PROCESSING_TOOL_REQUESTS[normalizedModelId];
|
||||||
|
if (processingRequest) {
|
||||||
|
if (getInvocationToolId(configuredInvocation)?.toLowerCase() === normalizedModelId) {
|
||||||
|
return configuredInvocation;
|
||||||
|
}
|
||||||
|
return createKnownProcessingToolInvocation(modelId, processingRequest);
|
||||||
|
}
|
||||||
|
|
||||||
const knownImageInput = KNOWN_IMAGE_TOOL_INPUTS[normalizedModelId];
|
const knownImageInput = KNOWN_IMAGE_TOOL_INPUTS[normalizedModelId];
|
||||||
if (!knownImageInput) return configuredInvocation;
|
if (!knownImageInput) return configuredInvocation;
|
||||||
|
|
||||||
|
|||||||
@ -59,4 +59,28 @@ test.describe("VIP98K reference-image routing", () => {
|
|||||||
resolveVip98kTaskInvocation("other/image-model", textToImageInvocation)
|
resolveVip98kTaskInvocation("other/image-model", textToImageInvocation)
|
||||||
).toBe(textToImageInvocation);
|
).toBe(textToImageInvocation);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("routes remove-background through the dedicated external tool", () => {
|
||||||
|
const invocation = resolveVip98kTaskInvocation("remove-background", null);
|
||||||
|
|
||||||
|
expect(invocation?.path).toBe(
|
||||||
|
"/api/external/tools/generate?tool_id=remove-background"
|
||||||
|
);
|
||||||
|
expect(invocation?.resultPath).toBe(
|
||||||
|
"/api/external/tools/result?tool_id=remove-background&taskId={task_id}"
|
||||||
|
);
|
||||||
|
expect(invocation?.requestExample).toEqual({ image_url: "" });
|
||||||
|
});
|
||||||
|
|
||||||
|
test("routes enhance through the dedicated external tool", () => {
|
||||||
|
const invocation = resolveVip98kTaskInvocation("enhance", null);
|
||||||
|
|
||||||
|
expect(invocation?.path).toBe(
|
||||||
|
"/api/external/tools/generate?tool_id=enhance"
|
||||||
|
);
|
||||||
|
expect(invocation?.requestExample).toEqual({
|
||||||
|
image_url: "",
|
||||||
|
scale_factor: 4,
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user