以下代碼可以使用，但是他只能在A網路UI用，不能在B網路UI用，在B使用會返回空。

export default {
  async fetch(request, env) {
    // 处理 CORS
    if (request.method === "OPTIONS") {
      return handleOptions(request);
    }

    if (request.method !== "POST") {
      return new Response("只允许 POST 请求", { status: 405 });
    }

    // 使用环境变量中的 API 密钥
    const apiKey = env.CLOUDFLARE_API_KEY;
    if (!apiKey) {
      return new Response("缺少 API 令牌", { status: 401 });
    }

    const accountId = env.CLOUDFLARE_ACCOUNT_ID;

    try {
      const body = await request.json();
      
      let model = body.model;
      if (!model.startsWith("@cf/")) {
        model = `@cf/${model}`;
      }
      
      const cfApiUrl = `https://api.cloudflare.com/client/v4/accounts/${accountId}/ai/run/${model}`;

      const aiResponse = await fetch(cfApiUrl, {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${apiKey}`,
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          messages: body.messages,
          max_tokens: body.max_tokens,
          temperature: body.temperature,
          // 可以根据需要添加其他参数
        }),
      }).then(res => res.json());

      if (!aiResponse.success) {
        throw new Error(aiResponse.errors[0].message);
      }

      // 直接返回纯文本响应
      const textResponse = aiResponse.result.response;

      return new Response(textResponse, {
        headers: {
          "Content-Type": "text/plain",
          "Access-Control-Allow-Origin": "*",
        },
      });
    } catch (error) {
      console.error('Error in worker:', error);
      return new Response('Internal Server Error: ' + error.message, { 
        status: 500,
        headers: { 'Content-Type': 'text/plain' }
      });
    }
  }
};

function handleOptions(request) {
  return new Response(null, {
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'POST, OPTIONS',
      'Access-Control-Allow-Headers': 'Content-Type, Authorization',
      'Access-Control-Max-Age': '86400',
    }
  });
}

以下是一個pages代碼，他也只能在AUI用不能在BUI用
/export default {
  async fetch(request, env) {
    // 处理预检请求
    if (request.method === 'OPTIONS') {
      return handleOptions(request);
    }

    const url = new URL(request.url);
    url.host = 'api.mistral.ai';

    let body;
    try {
      // 获取 body 数据并解析为 JSON
      body = await request.json();
    } catch (error) {
      return new Response('Invalid JSON', { status: 400 });
    }

    // 定义支持字段列表
    const allowedFields = ['model', 'messages', 'temperature', 'top_p', 'max_tokens', 'min_tokens', 'stream', 'stop', 'random_seed','response_format','tools','tool_choice','safe_prompt'];
    
    // 过滤 body 中不在支持字段列表中的字段
    const filteredBody = filterFields(body, allowedFields);
    
    const newBody = JSON.stringify(filteredBody);
    const modifiedRequest = new Request(url, {
      method: request.method,
      body: newBody,
      headers: request.headers
    });
    
    return fetch(modifiedRequest);
  }
}
// 过滤不支持参数
function filterFields(obj, allowedFields) {
  let result = {};
  for (const key in obj) {
    if (allowedFields.includes(key)) {
      result[key] = obj[key];
    }
  }
  return result;
}
// 处理预检请求的函数
function handleOptions(request) {
  const headers = {
    'Access-Control-Allow-Origin': '*', // 允许所有来源
    'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', // 允许的方法
    'Access-Control-Allow-Headers': 'Content-Type, Authorization', // 允许的头部
    'Access-Control-Max-Age': '86400', // 预检请求的缓存时间（秒）
  };

  return new Response(null, {
    status: 204,
    headers: headers
  });
}

但修改成這樣後，他能在AUI後也能在BUI用
const MISTRAL_API_URL = 'https://api.mistral.ai/v1';

export default {
  async fetch(request, env, ctx) {
    // 處理預檢請求
    if (request.method === 'OPTIONS') {
      return handleOptions(request);
    }

    // 解析請求 URL
    const url = new URL(request.url);
    
    // 構建轉發到 Mistral API 的 URL
    let mistralPath = url.pathname;
    // 如果路徑不是以 /v1 開頭，添加它
    if (!mistralPath.startsWith('/v1')) {
      mistralPath = '/v1' + mistralPath;
    }
    const mistralUrl = new URL(mistralPath, MISTRAL_API_URL);
    mistralUrl.search = url.search;

    console.log(`Forwarding request to: ${mistralUrl.toString()}`);

    let body;
    let headers = new Headers(request.headers);

    // 移除可能導致問題的頭部
    headers.delete('Host');
    headers.delete('Origin');

    // 檢查 Authorization 頭部是否存在
    if (!headers.has('Authorization')) {
      return new Response('Missing API Key', { status: 401 });
    }

    try {
      if (request.method === 'POST') {
        // 獲取並過濾 body 數據
        body = await request.json();
        console.log('Original request body:', JSON.stringify(body));
        body = filterFields(body, [
          'model', 'messages', 'temperature', 'top_p', 'max_tokens', 
          'stream', 'safe_mode', 'random_seed', 'response_format'
        ]);
        console.log('Filtered request body:', JSON.stringify(body));
      }

      // 創建新的請求
      const mistralRequest = new Request(mistralUrl, {
        method: request.method,
        headers: headers,
        body: body ? JSON.stringify(body) : undefined
      });

      // 發送請求到 Mistral API
      const response = await fetch(mistralRequest);

      // 檢查響應狀態
      if (!response.ok) {
        const errorBody = await response.text();
        console.error(`Mistral API error: ${response.status} ${errorBody}`);
        return new Response(`Mistral API error: ${response.status}\n${errorBody}`, { 
          status: response.status,
          headers: { 'Content-Type': 'text/plain' }
        });
      }

      // 創建新的響應並添加 CORS 頭部
      const newResponse = new Response(response.body, response);
      newResponse.headers.set('Access-Control-Allow-Origin', '*');
      return newResponse;

    } catch (error) {
      console.error('Error in worker:', error);
      return new Response('Internal Server Error: ' + error.message, { 
        status: 500,
        headers: { 'Content-Type': 'text/plain' }
      });
    }
  }
};

// 過濾字段函數
function filterFields(obj, allowedFields) {
  return Object.fromEntries(
    Object.entries(obj).filter(([key]) => allowedFields.includes(key))
  );
}

// 處理預檢請求的函數
function handleOptions(request) {
  return new Response(null, {
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
      'Access-Control-Allow-Headers': 'Content-Type, Authorization',
      'Access-Control-Max-Age': '86400',
    }
  });
}


**************************

我要你參考完代碼後，找出為什麼MISTRAL AI的代碼不能用在BUI，而修正後的可以，找到原因之後幫我修正我的CLOUDFLARE AI代碼