Skip to content

🤯 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.

feat: support switch dashscope URL based on baseURL

Zhijie He6a4118c6282c
diff --git a/packages/model-runtime/src/providers/qwen/createImage.test.ts b/packages/model-runtime/src/providers/qwen/createImage.test.ts
index c742fb3c50c..9b8f011f2b0 100644
--- a/packages/model-runtime/src/providers/qwen/createImage.test.ts
+++ b/packages/model-runtime/src/providers/qwen/createImage.test.ts
@@ -1,8 +1,8 @@
// @vitest-environment node
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-import type { CreateImageOptions } from '../../core/openaiCompatibleFactory';
-import type { CreateImagePayload } from '../../types/image';
+import { type CreateImageOptions } from '../../core/openaiCompatibleFactory';
+import { type CreateImagePayload } from '../../types/image';
import { createQwenImage } from './createImage';
// Mock the console.error to avoid polluting test output
@@ -23,6 +23,109 @@ afterEach(() => {
});
describe('createQwenImage', () => {
+ describe('Base URL handling', () => {
+ it('should use intl baseURL when provided', async () => {
+ const mockTaskId = 'task-123456';
+ const mockImageUrl = 'https://example.com/test-image.jpg';
+ const intlBaseUrl = 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1';
+
+ // Mock fetch for task creation and immediate success
+ global.fetch = vi
+ .fn()
+ .mockResolvedValueOnce({
+ ok: true,
+ json: async () => ({
+ output: { task_id: mockTaskId },
+ request_id: 'req-123',
+ }),
+ })
+ .mockResolvedValueOnce({
+ ok: true,
+ json: async () => ({
+ output: {
+ task_id: mockTaskId,
+ task_status: 'SUCCEEDED',
+ results: [{ url: mockImageUrl }],
+ },
+ request_id: 'req-124',
+ }),
+ });
+
+ const payload: CreateImagePayload = {
+ model: 'wanx-v1',
+ params: {
+ prompt: 'Test image',
+ },
+ };
+
+ const optionsWithCustomUrl: CreateImageOptions = {
+ apiKey: 'test-api-key',
+ provider: 'qwen',
+ baseURL: intlBaseUrl,
+ };
+
+ const result = await createQwenImage(payload, optionsWithCustomUrl);
+
+ // Verify the custom base URL is used (without /compatible-mode/v1)
+ expect(fetch).toHaveBeenCalledWith(
+ 'https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis',
+ expect.any(Object),
+ );
+
+ // Verify the task status query also uses the custom base URL
+ expect(fetch).toHaveBeenCalledWith(
+ 'https://dashscope-intl.aliyuncs.com/api/v1/tasks/task-123456',
+ expect.any(Object),
+ );
+
+ expect(result).toEqual({ imageUrl: mockImageUrl });
+ });
+
+ it('should use default baseURL when not provided', async () => {
+ const mockTaskId = 'task-123456';
+ const mockImageUrl = 'https://dashscope.oss-cn-beijing.aliyuncs.com/aigc/test-image.jpg';
+
+ // Mock fetch for task creation and immediate success
+ global.fetch = vi
+ .fn()
+ .mockResolvedValueOnce({
+ ok: true,
+ json: async () => ({
+ output: { task_id: mockTaskId },
+ request_id: 'req-123',
+ }),
+ })
+ .mockResolvedValueOnce({
+ ok: true,
+ json: async () => ({
+ output: {
+ task_id: mockTaskId,
+ task_status: 'SUCCEEDED',
+ results: [{ url: mockImageUrl }],
+ },
+ request_id: 'req-124',
+ }),
+ });
+
+ const payload: CreateImagePayload = {
+ model: 'wanx-v1',
+ params: {
+ prompt: 'Test image',
+ },
+ };
+
+ const result = await createQwenImage(payload, mockOptions);
+
+ // Verify the default base URL is used
+ expect(fetch).toHaveBeenCalledWith(
+ 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis',
+ expect.any(Object),
+ );
+
+ expect(result).toEqual({ imageUrl: mockImageUrl });
+ });
+ });
+
describe('Success scenarios', () => {
it('should successfully generate image with immediate success', async () => {
const mockTaskId = 'task-123456';
diff --git a/packages/model-runtime/src/providers/qwen/createImage.ts b/packages/model-runtime/src/providers/qwen/createImage.ts
index 320d3680bdd..13135d4484e 100644
--- a/packages/model-runtime/src/providers/qwen/createImage.ts
+++ b/packages/model-runtime/src/providers/qwen/createImage.ts
@@ -1,8 +1,8 @@
import createDebug from 'debug';
-import type { CreateImageOptions } from '../../core/openaiCompatibleFactory';
-import type { CreateImagePayload, CreateImageResponse } from '../../types/image';
-import type { TaskResult } from '../../utils/asyncifyPolling';
+import { type CreateImageOptions } from '../../core/openaiCompatibleFactory';
+import { type CreateImagePayload, type CreateImageResponse } from '../../types/image';
+import { type TaskResult } from '../../utils/asyncifyPolling';
import { asyncifyPolling } from '../../utils/asyncifyPolling';
import { AgentRuntimeError } from '../../utils/createError';
@@ -62,9 +62,10 @@ async function createQwenImageTask(
apiKey: string,
endpoint: 'text2image' | 'image2image',
provider: string,
+ baseUrl: string,
): Promise<string> {
const { model, params } = payload;
- const url = `https://dashscope.aliyuncs.com/api/v1/services/aigc/${endpoint}/image-synthesis`;
+ const url = `${baseUrl}/api/v1/services/aigc/${endpoint}/image-synthesis`;
log('Creating %s task with model: %s, endpoint: %s', endpoint, model, url);
const input: Record<string, any> = {
@@ -139,9 +140,10 @@ async function createQwenImageTask(
async function createMultimodalGeneration(
payload: CreateImagePayload,
apiKey: string,
+ baseUrl: string,
): Promise<CreateImageResponse> {
const { model, params } = payload;
- const endpoint = `https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation`;
+ const endpoint = `${baseUrl}/api/v1/services/aigc/multimodal-generation/generation`;
log('Creating image with model: %s, endpoint: %s', model, endpoint);
// Check if this model requires an image
@@ -225,8 +227,12 @@ async function createMultimodalGeneration(
/**
* Query the status of an image generation task
*/
-async function queryTaskStatus(taskId: string, apiKey: string): Promise<QwenImageTaskResponse> {
- const endpoint = `https://dashscope.aliyuncs.com/api/v1/tasks/${taskId}`;
+async function queryTaskStatus(
+ taskId: string,
+ apiKey: string,
+ baseUrl: string,
+): Promise<QwenImageTaskResponse> {
+ const endpoint = `${baseUrl}/api/v1/tasks/${taskId}`;
log('Querying task status for: %s', taskId);
@@ -262,9 +268,15 @@ export async function createQwenImage(
payload: CreateImagePayload,
options: CreateImageOptions,
): Promise<CreateImageResponse> {
- const { apiKey, provider } = options;
+ const { apiKey, baseURL, provider } = options;
const { model } = payload;
+ // Check if URL has /compatible-mode/v1 suffix and remove it
+ const suffixIndex = baseURL ? baseURL.indexOf('/compatible-mode/v1') : -1;
+ const dashscopeURL: string =
+ suffixIndex > -1 ? baseURL!.slice(0, suffixIndex) : 'https://dashscope.aliyuncs.com';
+ log('Using dashscopeURL: %s', dashscopeURL);
+
try {
const isText2Image = matchesModel(model, text2ImageModels);
const isImage2Image = matchesModel(model, image2ImageModels);
@@ -273,7 +285,7 @@ export async function createQwenImage(
const endpoint = isImage2Image ? 'image2image' : 'text2image';
log('Using %s API for model: %s', endpoint, model);
- const taskId = await createQwenImageTask(payload, apiKey, endpoint, provider);
+ const taskId = await createQwenImageTask(payload, apiKey, endpoint, provider, dashscopeURL);
const result = await asyncifyPolling<QwenImageTaskResponse, CreateImageResponse>({
checkStatus: (taskStatus: QwenImageTaskResponse): TaskResult<CreateImageResponse> => {
@@ -311,14 +323,14 @@ export async function createQwenImage(
debug: (message: any, ...args: any[]) => log(message, ...args),
error: (message: any, ...args: any[]) => log(message, ...args),
},
- pollingQuery: () => queryTaskStatus(taskId, apiKey),
+ pollingQuery: () => queryTaskStatus(taskId, apiKey, dashscopeURL),
});
return result;
}
log('Using multimodal-generation API for model: %s', model);
- return await createMultimodalGeneration(payload, apiKey);
+ return await createMultimodalGeneration(payload, apiKey, dashscopeURL);
} catch (error) {
log('Error in createQwenImage: %O', error);