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.

♻️ refactor: edge to node runtime (#10149)

YuTengjing2f4c25d8263b
diff --git a/packages/model-runtime/src/providers/azureai/index.ts b/packages/model-runtime/src/providers/azureai/index.ts
index b875f431963..a06281b190c 100644
--- a/packages/model-runtime/src/providers/azureai/index.ts
+++ b/packages/model-runtime/src/providers/azureai/index.ts
@@ -1,6 +1,7 @@
import createClient, { ModelClient } from '@azure-rest/ai-inference';
import { AzureKeyCredential } from '@azure/core-auth';
import { ModelProvider } from 'model-bank';
+import type { Readable as NodeReadable } from 'node:stream';
import OpenAI from 'openai';
import { systemToUserModels } from '../../const/models';
@@ -64,9 +65,40 @@ export class LobeAzureAI implements LobeRuntimeAI {
});
if (enableStreaming) {
- const stream = await response.asBrowserStream();
+ const unifiedStream = await (async () => {
+ if (typeof window === 'undefined') {
+ /**
+ * In Node.js the SDK exposes a Node readable stream, so we convert it to a Web ReadableStream
+ * to reuse the same streaming pipeline used by Edge/browser runtimes.
+ */
+ const streamModule = await import('node:stream');
+ const Readable = streamModule.Readable ?? streamModule.default.Readable;
+
+ if (!Readable) throw new Error('node:stream module missing Readable export');
+ if (typeof Readable.toWeb !== 'function')
+ throw new Error('Readable.toWeb is not a function');
+
+ const nodeResponse = await response.asNodeStream();
+ const nodeStream = nodeResponse.body;
+
+ if (!nodeStream) {
+ throw new Error('Azure AI response body is empty');
+ }
+
+ return Readable.toWeb(nodeStream as unknown as NodeReadable) as ReadableStream;
+ }
+
+ const browserResponse = await response.asBrowserStream();
+ const browserStream = browserResponse.body;
+
+ if (!browserStream) {
+ throw new Error('Azure AI response body is empty');
+ }
+
+ return browserStream;
+ })();
- const [prod, debug] = stream.body!.tee();
+ const [prod, debug] = unifiedStream.tee();
if (process.env.DEBUG_AZURE_AI_CHAT_COMPLETION === '1') {
debugStream(debug).catch(console.error);
diff --git a/packages/utils/src/server/__tests__/response.test.ts b/packages/utils/src/server/__tests__/response.test.ts
new file mode 100644
index 00000000000..a705de8c3da
--- /dev/null
+++ b/packages/utils/src/server/__tests__/response.test.ts
@@ -0,0 +1,79 @@
+import { describe, expect, it } from 'vitest';
+
+import { createNodeResponse } from '../response';
+
+describe('createNodeResponse', () => {
+ it('wraps successful Response with default headers', async () => {
+ const upstream = new Response('audio-chunk', {
+ headers: {
+ 'x-source': 'sdk',
+ },
+ status: 201,
+ statusText: 'Created',
+ });
+ upstream.headers.delete('content-type');
+
+ const result = await createNodeResponse(() => Promise.resolve(upstream), {
+ success: {
+ cacheControl: 'no-store',
+ defaultContentType: 'audio/mpeg',
+ },
+ });
+
+ expect(await result.text()).toBe('audio-chunk');
+ expect(result.status).toBe(201);
+ expect(result.headers.get('x-source')).toBe('sdk');
+ expect(result.headers.get('content-type')).toBe('audio/mpeg');
+ expect(result.headers.get('cache-control')).toBe('no-store');
+ });
+
+ it('delegates to onInvalidResponse when payload is not Response-like', async () => {
+ const fallback = new Response('invalid', { status: 500 });
+
+ const result = await createNodeResponse(() => Promise.resolve({} as any), {
+ onInvalidResponse: () => fallback,
+ });
+
+ expect(result).toBe(fallback);
+ });
+
+ it('normalizes thrown Response-like errors via error options', async () => {
+ const upstreamError = new Response(JSON.stringify({ error: 'boom' }), {
+ status: 429,
+ statusText: 'Too Many Requests',
+ });
+ upstreamError.headers.delete('content-type');
+
+ const result = await createNodeResponse(
+ async () => {
+ throw upstreamError;
+ },
+ {
+ error: {
+ cacheControl: 'no-store',
+ defaultContentType: 'application/json',
+ },
+ },
+ );
+
+ expect(result.status).toBe(429);
+ expect(result.headers.get('content-type')).toBe('application/json');
+ expect(result.headers.get('cache-control')).toBe('no-store');
+ expect(await result.json()).toEqual({ error: 'boom' });
+ });
+
+ it('delegates to onNonResponseError for unexpected exceptions', async () => {
+ const fallback = new Response('fallback', { status: 500 });
+
+ const result = await createNodeResponse(
+ async () => {
+ throw new Error('unexpected');
+ },
+ {
+ onNonResponseError: () => fallback,
+ },
+ );
+
+ expect(result).toBe(fallback);
+ });
+});
diff --git a/packages/utils/src/server/index.ts b/packages/utils/src/server/index.ts
index ad406733e80..388311e07a4 100644
--- a/packages/utils/src/server/index.ts
+++ b/packages/utils/src/server/index.ts
@@ -1,5 +1,6 @@
export * from './auth';
export * from './correctOIDCUrl';
export * from './geo';
+export * from './response';
export * from './responsive';
export * from './xor';
diff --git a/packages/utils/src/server/response.ts b/packages/utils/src/server/response.ts
new file mode 100644
index 00000000000..747b311b6aa
--- /dev/null
+++ b/packages/utils/src/server/response.ts
@@ -0,0 +1,110 @@
+/**
+ * Options for normalizing a Response so it can be consumed by the platform runtime.
+ */
+export interface EnsureNodeResponseOptions {
+ /**
+ * Force update the cache-control header, usually to disable caching for APIs.
+ */
+ cacheControl?: string;
+ /**
+ * Sets a default content-type header when the original Response omitted it.
+ */
+ defaultContentType?: string;
+ /**
+ * Force buffering even if a readable body stream exists.
+ */
+ forceBuffering?: boolean;
+}
+
+/**
+ * Checks whether a value structurally matches the minimal Response interface.
+ */
+export const isResponseLike = (value: unknown): value is Response => {
+ if (typeof value !== 'object' || value === null) return false;
+
+ const candidate = value as Partial<Response>;
+
+ return (
+ typeof candidate.arrayBuffer === 'function' &&
+ !!candidate.headers &&
+ typeof (candidate.headers as Headers).get === 'function' &&
+ typeof candidate.status === 'number' &&
+ typeof candidate.statusText === 'string'
+ );
+};
+
+/**
+ * Re-wraps an arbitrary Response-like object into the platform Response implementation.
+ *
+ * This is required because some SDKs (e.g., OpenAI) ship their own Response shim
+ * that is not recognized by Next.js when running in the Node.js runtime.
+ */
+export const ensureNodeResponse = async (
+ source: Response,
+ options: EnsureNodeResponseOptions = {},
+) => {
+ const headers = new Headers(source.headers);
+
+ if (options.defaultContentType && !headers.has('content-type')) {
+ headers.set('content-type', options.defaultContentType);
+ }
+
+ if (options.cacheControl) {
+ headers.set('cache-control', options.cacheControl);
+ }
+
+ const body = !options.forceBuffering && source.body ? source.body : await source.arrayBuffer();
+
+ return new Response(body, {
+ headers,
+ status: source.status,
+ statusText: source.statusText,
+ });
+};
+
+export interface CreateNodeResponseOptions {
+ /**
+ * Options applied when a Response-like error is thrown.
+ */
+ error?: EnsureNodeResponseOptions;
+ /**
+ * Callback when the resolved value is not Response-like.
+ */
+ onInvalidResponse?: (payload: unknown) => Response;
+ /**
+ * Callback when a non-Response error is thrown.
+ */
+ onNonResponseError?: (error: unknown) => Response;
+ /**
+ * Options applied when the resolved Response is normalized.
+ */
+ success?: EnsureNodeResponseOptions;
+}
+
+/**
+ * Runs a response factory and ensures every exit path returns a platform Response.
+ */
+export const createNodeResponse = async <T>(
+ responseCreator: () => Promise<T>,
+ options: CreateNodeResponseOptions = {},
+) => {
+ try {
+ const response = await responseCreator();
+
+ if (!isResponseLike(response)) {
+ if (options.onInvalidResponse) return options.onInvalidResponse(response);
+
+ throw new Error('Expected a Response-like object from responseCreator.');
+ }
+
+ return ensureNodeResponse(response, options.success);
+ } catch (error) {
+ if (isResponseLike(error)) {
+ return ensureNodeResponse(error, options.error);
+ }
+
+ if (options.onNonResponseError) return options.onNonResponseError(error);
+
+ throw error;
+ }
+};
diff --git a/src/app/(backend)/webapi/chat/azureai/route.test.ts b/src/app/(backend)/webapi/chat/azureai/route.test.ts
deleted file mode 100644
index 712be5ffbe2..00000000000
--- a/src/app/(backend)/webapi/chat/azureai/route.test.ts
+++ /dev/null
@@ -1,25 +0,0 @@
-// @vitest-environment edge-runtime
-import { describe, expect, it, vi } from 'vitest';
-
-import { POST as UniverseRoute } from '../[provider]/route';
-import { POST, runtime } from './route';
-
-vi.mock('../[provider]/route', () => ({
- POST: vi.fn().mockResolvedValue('mocked response'),
-}));
-
-describe('Configuration tests', () => {
- it('should have runtime set to "edge"', () => {
- expect(runtime).toBe('edge');
- });
-});
-
-describe('Groq POST function tests', () => {
- it('should call UniverseRoute with correct parameters', async () => {
- const mockRequest = new Request('https://example.com', { method: 'POST' });
- await POST(mockRequest);
- expect(UniverseRoute).toHaveBeenCalledWith(mockRequest, {
- params: Promise.resolve({ provider: 'azureai' }),
- });
- });
-});
diff --git a/src/app/(backend)/webapi/chat/azureai/route.ts b/src/app/(backend)/webapi/chat/azureai/route.ts
deleted file mode 100644
index 6a003cdf555..00000000000
--- a/src/app/(backend)/webapi/chat/azureai/route.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import { POST as UniverseRoute } from '../[provider]/route';
-
-export const runtime = 'edge';
-
-export const POST = async (req: Request) =>
- UniverseRoute(req, { params: Promise.resolve({ provider: 'azureai' }) });
diff --git a/src/app/(backend)/webapi/stt/openai/route.ts b/src/app/(backend)/webapi/stt/openai/route.ts
index 26ff12a9031..ea088b98c6b 100644
--- a/src/app/(backend)/webapi/stt/openai/route.ts
+++ b/src/app/(backend)/webapi/stt/openai/route.ts
@@ -3,8 +3,6 @@ import { createOpenaiAudioTranscriptions } from '@lobehub/tts/server';
import { createBizOpenAI } from '@/app/(backend)/_deprecated/createBizOpenAI';
-export const runtime = 'edge';
-
export const preferredRegion = [
'arn1',
'bom1',
diff --git a/src/app/(backend)/webapi/tts/edge/route.ts b/src/app/(backend)/webapi/tts/edge/route.ts
index c8df174ba38..84016b47343 100644
--- a/src/app/(backend)/webapi/tts/edge/route.ts
+++ b/src/app/(backend)/webapi/tts/edge/route.ts
@@ -1,9 +1,15 @@
import { EdgeSpeechPayload, EdgeSpeechTTS } from '@lobehub/tts';
-export const runtime = 'edge';
+import { createSpeechResponse } from '@/server/utils/createSpeechResponse';
export const POST = async (req: Request) => {
const payload = (await req.json()) as EdgeSpeechPayload;
- return await EdgeSpeechTTS.createRequest({ payload });
+ return createSpeechResponse(() => EdgeSpeechTTS.createRequest({ payload }), {
+ logTag: 'webapi/tts/edge',
+ messages: {
+ failure: 'Failed to synthesize speech',
+ invalid: 'Unexpected payload from Edge speech API',
+ },
+ });
};
diff --git a/src/app/(backend)/webapi/tts/microsoft/route.ts b/src/app/(backend)/webapi/tts/microsoft/route.ts
index 5e37c5d32b5..b14d6e29bb3 100644
--- a/src/app/(backend)/webapi/tts/microsoft/route.ts
+++ b/src/app/(backend)/webapi/tts/microsoft/route.ts
@@ -1,9 +1,15 @@
import { MicrosoftSpeechPayload, MicrosoftSpeechTTS } from '@lobehub/tts';
-export const runtime = 'edge';
+import { createSpeechResponse } from '@/server/utils/createSpeechResponse';
export const POST = async (req: Request) => {
const payload = (await req.json()) as MicrosoftSpeechPayload;
- return await MicrosoftSpeechTTS.createRequest({ payload });
+ return createSpeechResponse(() => MicrosoftSpeechTTS.createRequest({ payload }), {
+ logTag: 'webapi/tts/microsoft',
+ messages: {
+ failure: 'Failed to synthesize speech',
+ invalid: 'Unexpected payload from Microsoft speech API',
+ },
+ });
};
diff --git a/src/app/(backend)/webapi/tts/openai/route.ts b/src/app/(backend)/webapi/tts/openai/route.ts
index d9d6b9c6702..52d0b5e3f51 100644
--- a/src/app/(backend)/webapi/tts/openai/route.ts
+++ b/src/app/(backend)/webapi/tts/openai/route.ts
@@ -2,8 +2,7 @@ import { OpenAITTSPayload } from '@lobehub/tts';
import { createOpenaiAudioSpeech } from '@lobehub/tts/server';
import { createBizOpenAI } from '@/app/(backend)/_deprecated/createBizOpenAI';
-
-export const runtime = 'edge';
+import { createSpeechResponse } from '@/server/utils/createSpeechResponse';
export const preferredRegion = [
'arn1',
@@ -34,5 +33,18 @@ export const POST = async (req: Request) => {
// if resOrOpenAI is a Response, it means there is an error,just return it
if (openaiOrErrResponse instanceof Response) return openaiOrErrResponse;
- return await createOpenaiAudioSpeech({ openai: openaiOrErrResponse as any, payload });
+ return createSpeechResponse(
+ () =>
+ createOpenaiAudioSpeech({
+ openai: openaiOrErrResponse as any,
+ payload,
+ }),
+ {
+ logTag: 'webapi/tts/openai',
+ messages: {
+ failure: 'Failed to synthesize speech',
+ invalid: 'Unexpected payload from OpenAI TTS',
+ },
+ },
+ );
};
diff --git a/src/server/utils/createSpeechResponse.ts b/src/server/utils/createSpeechResponse.ts
new file mode 100644
index 00000000000..d164a87b0c1
--- /dev/null
+++ b/src/server/utils/createSpeechResponse.ts
@@ -0,0 +1,55 @@
+import { ChatErrorType } from '@lobechat/types';
+
+import { createErrorResponse } from '@/utils/errorResponse';
+import { createNodeResponse } from '@/utils/server/response';
+
+export interface CreateSpeechResponseOptions {
+ errorContentType?: string;
+ logTag: string;
+ messages?: {
+ failure?: string;
+ invalid?: string;
+ };
+ successContentType?: string;
+}
+
+/**
+ * Wraps a third-party speech SDK response so the Node.js runtime always receives
+ * a valid platform Response, while keeping logging and error handling consistent.
+ */
+export const createSpeechResponse = async <T>(
+ responseCreator: () => Promise<T>,
+ {
+ logTag,
+ successContentType = 'audio/mpeg',
+ errorContentType = 'application/json',
+ messages,
+ }: CreateSpeechResponseOptions,
+) => {
+ const prefix = `[${logTag}]`;
+ const invalidMessage = messages?.invalid ?? 'Unexpected payload from speech provider';
+ const failureMessage = messages?.failure ?? 'Failed to synthesize speech';
+
+ return createNodeResponse(responseCreator, {
+ error: {
+ cacheControl: 'no-store',
+ defaultContentType: errorContentType,
+ },
+ onInvalidResponse: (response) => {
+ console.error(`${prefix} ${invalidMessage}`, response);
+
+ return createErrorResponse(ChatErrorType.InternalServerError);
+ },
+ onNonResponseError: (error) => {
+ console.error(`${prefix} ${failureMessage}`, error);
+
+ return createErrorResponse(ChatErrorType.InternalServerError, {
+ message: error instanceof Error ? error.message : String(error),
+ });
+ },
+ success: {
+ cacheControl: 'no-store',
+ defaultContentType: successContentType,
+ },
+ });
+};