🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
chore: add imageUrl & imageUrls input validation (apply suggestion 3)
Zhijie He4a29904516fb
diff --git a/packages/model-runtime/src/providers/qwen/createImage.test.ts b/packages/model-runtime/src/providers/qwen/createImage.test.tsindex 04ee33c4df2..c742fb3c50c 100644--- a/packages/model-runtime/src/providers/qwen/createImage.test.ts+++ b/packages/model-runtime/src/providers/qwen/createImage.test.ts@@ -658,21 +658,12 @@ describe('createQwenImage', () => {});});- it('should handle qwen-image-edit API errors', async () => {- global.fetch = vi.fn().mockResolvedValueOnce({- ok: false,- status: 400,- statusText: 'Bad Request',- json: async () => ({- message: 'Invalid image format',- }),- });-+ it('should throw error when imageUrl is missing for qwen-image-edit', async () => {const payload: CreateImagePayload = {model: 'qwen-image-edit',params: {prompt: 'Edit this image',- imageUrl: 'https://example.com/invalid-image.jpg',+ // imageUrl is missing},};@@ -684,131 +675,46 @@ describe('createQwenImage', () => {);});- it('should convert imageUrls array to imageUrl for qwen-image-edit', async () => {- const mockImageUrl =- 'https://dashscope.oss-cn-beijing.aliyuncs.com/aigc/imageUrls-converted.jpg';-+ it('should handle qwen-image-edit API errors', async () => {global.fetch = vi.fn().mockResolvedValueOnce({- ok: true,+ ok: false,+ status: 400,+ statusText: 'Bad Request',json: async () => ({- output: {- choices: [- {- message: {- content: [{ image: mockImageUrl }],- },- },- ],- },- request_id: 'req-imageUrls-123',+ message: 'Invalid image format',}),});const payload: CreateImagePayload = {model: 'qwen-image-edit',params: {- prompt: 'Edit this image to add a dog',- imageUrls: [- 'https://example.com/source-image-1.jpg',- 'https://example.com/source-image-2.jpg',- ],+ prompt: 'Edit this image',+ imageUrl: 'https://example.com/invalid-image.jpg',},};- const result = await createQwenImage(payload, mockOptions);-- expect(result).toEqual({- imageUrl: mockImageUrl,- });-- const [url, options] = (fetch as any).mock.calls[0];- const body = JSON.parse(options.body);-- // Verify that the first imageUrl from imageUrls array was used- expect(body.input.messages[0].content[0].image).toBe(- 'https://example.com/source-image-1.jpg',+ await expect(createQwenImage(payload, mockOptions)).rejects.toEqual(+ expect.objectContaining({+ errorType: 'ProviderBizError',+ provider: 'qwen',+ }),);});- it('should use first imageUrl when imageUrls has multiple elements', async () => {- const mockImageUrl = 'https://dashscope.oss-cn-beijing.aliyuncs.com/aigc/first-element.jpg';-- global.fetch = vi.fn().mockResolvedValueOnce({- ok: true,- json: async () => ({- output: {- choices: [- {- message: {- content: [{ image: mockImageUrl }],- },- },- ],- },- request_id: 'req-first-element',- }),- });-+ it('should throw error when imageUrl is not provided', async () => {const payload: CreateImagePayload = {model: 'qwen-image-edit',params: {- prompt: 'Use the first image only',- imageUrls: [- 'https://example.com/first-image.jpg',- 'https://example.com/second-image.jpg',- 'https://example.com/third-image.jpg',- ],+ prompt: 'Edit this image',+ // imageUrl not provided},};- await createQwenImage(payload, mockOptions);-- const [url, options] = (fetch as any).mock.calls[0];- const body = JSON.parse(options.body);-- // Should use only the first image from the array- expect(body.input.messages[0].content[0].image).toBe('https://example.com/first-image.jpg');- });-- it('should prioritize imageUrl over imageUrls when both are provided', async () => {- const mockImageUrl = 'https://dashscope.oss-cn-beijing.aliyuncs.com/aigc/priority-test.jpg';-- global.fetch = vi.fn().mockResolvedValueOnce({- ok: true,- json: async () => ({- output: {- choices: [- {- message: {- content: [{ image: mockImageUrl }],- },- },- ],- },- request_id: 'req-priority-test',+ await expect(createQwenImage(payload, mockOptions)).rejects.toEqual(+ expect.objectContaining({+ errorType: 'ProviderBizError',+ provider: 'qwen',}),- });-- const payload: CreateImagePayload = {- model: 'qwen-image-edit',- params: {- prompt: 'Test priority between imageUrl and imageUrls',- imageUrl: 'https://example.com/priority-image.jpg',- imageUrls: [- 'https://example.com/should-not-use-1.jpg',- 'https://example.com/should-not-use-2.jpg',- ],- },- };-- await createQwenImage(payload, mockOptions);-- const [url, options] = (fetch as any).mock.calls[0];- const body = JSON.parse(options.body);-- // Should use imageUrl, not imageUrls- expect(body.input.messages[0].content[0].image).toBe(- 'https://example.com/priority-image.jpg',);});});diff --git a/packages/model-runtime/src/providers/qwen/createImage.ts b/packages/model-runtime/src/providers/qwen/createImage.tsindex e9d926e8adf..d8b462914ee 100644--- a/packages/model-runtime/src/providers/qwen/createImage.ts+++ b/packages/model-runtime/src/providers/qwen/createImage.ts@@ -8,24 +8,24 @@ import { AgentRuntimeError } from '../../utils/createError';const log = createDebug('lobe-image:qwen');-const text2ImageModels = new Set([- 'wan2.5-t2i-preview',- 'wan2.2-t2i-flash',- 'wan2.2-t2i-plus',- 'wanx2.1-t2i-turbo',- 'wanx2.1-t2i-plus',- 'wanx2.0-t2i-turbo',- 'wanx-v1',- 'stable-diffusion-xl',- 'stable-diffusion-v1.5',- 'stable-diffusion-3.5-large',- 'stable-diffusion-3.5-large-turbo',- 'flux-schnell',- 'flux-dev',- 'flux-merged',-]);--const image2ImageModels = new Set(['wan2.5-i2i-preview']);+const text2ImageModels = [+ /^wan2\.(2|5)-t2i-/,+ /^wanx2\.(0|1)-t2i-/,+ /^wanx-v1/,+ /^stable-diffusion-/,+ /^flux-/,+];++const image2ImageModels = [/^wan2\.(2|5)-i2i-$/];++const imageRequiredModels = [/^qwen-image-edit/, /^wan2\.(2|5)-i2i-$/];++// Helper function to check if model matches any pattern in the array+function matchesModel(model: string, patterns: Array<string | RegExp>): boolean {+ return patterns.some((pattern) =>+ pattern instanceof RegExp ? pattern.test(model) : pattern === model,+ );+}interface QwenImageTaskResponse {output: {@@ -144,6 +144,17 @@ async function createMultimodalGeneration(const endpoint = `https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation`;log('Creating image with model: %s, endpoint: %s', model, endpoint);+ // Check if this model requires an image+ const requiresImage = matchesModel(model, imageRequiredModels);++ if (requiresImage && !params.imageUrl && (!params.imageUrls || params.imageUrls.length === 0)) {+ throw AgentRuntimeError.createImage({+ error: new Error(`imageUrl or imageUrls is required for model ${model}`),+ errorType: 'ProviderBizError',+ provider: 'qwen',+ });+ }+const content: Array<{ image: string | string[] } | { text: string }> = [{ text: params.prompt }];if (params.imageUrl) {@@ -252,8 +263,11 @@ export async function createQwenImage(const { model } = payload;try {- if (text2ImageModels.has(model) || image2ImageModels.has(model)) {- const endpoint = image2ImageModels.has(model) ? 'image2image' : 'text2image';+ const isText2Image = matchesModel(model, text2ImageModels);+ const isImage2Image = matchesModel(model, image2ImageModels);++ if (isText2Image || isImage2Image) {+ const endpoint = isImage2Image ? 'image2image' : 'text2image';log('Using %s API for model: %s', endpoint, model);const taskId = await createQwenImageTask(payload, apiKey, endpoint, provider);