diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md deleted file mode 100644 index 7712d974276..00000000000 --- a/CODE_OF_CONDUCT.md +++ /dev/null @@ -1,128 +0,0 @@ -# Contributor Covenant Code of Conduct - -## Our Pledge - -We as members, contributors, and leaders pledge to make participation in our -community a harassment-free experience for everyone, regardless of age, body -size, visible or invisible disability, ethnicity, sex characteristics, gender -identity and expression, level of experience, education, socio-economic status, -nationality, personal appearance, race, religion, or sexual identity -and orientation. - -We pledge to act and interact in ways that contribute to an open, welcoming, -diverse, inclusive, and healthy community. - -## Our Standards - -Examples of behavior that contributes to a positive environment for our -community include: - -* Demonstrating empathy and kindness toward other people -* Being respectful of differing opinions, viewpoints, and experiences -* Giving and gracefully accepting constructive feedback -* Accepting responsibility and apologizing to those affected by our mistakes, - and learning from the experience -* Focusing on what is best not just for us as individuals, but for the - overall community - -Examples of unacceptable behavior include: - -* The use of sexualized language or imagery, and sexual attention or - advances of any kind -* Trolling, insulting or derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or email - address, without their explicit permission -* Other conduct which could reasonably be considered inappropriate in a - professional setting - -## Enforcement Responsibilities - -Community leaders are responsible for clarifying and enforcing our standards of -acceptable behavior and will take appropriate and fair corrective action in -response to any behavior that they deem inappropriate, threatening, offensive, -or harmful. - -Community leaders have the right and responsibility to remove, edit, or reject -comments, commits, code, wiki edits, issues, and other contributions that are -not aligned to this Code of Conduct, and will communicate reasons for moderation -decisions when appropriate. - -## Scope - -This Code of Conduct applies within all community spaces, and also applies when -an individual is officially representing the community in public spaces. -Examples of representing our community include using an official e-mail address, -posting via an official social media account, or acting as an appointed -representative at an online or offline event. - -## Enforcement - -Instances of abusive, harassing, or otherwise unacceptable behavior may be -reported to the community leaders responsible for enforcement at -flynn.zhang@foxmail.com. -All complaints will be reviewed and investigated promptly and fairly. - -All community leaders are obligated to respect the privacy and security of the -reporter of any incident. - -## Enforcement Guidelines - -Community leaders will follow these Community Impact Guidelines in determining -the consequences for any action they deem in violation of this Code of Conduct: - -### 1. Correction - -**Community Impact**: Use of inappropriate language or other behavior deemed -unprofessional or unwelcome in the community. - -**Consequence**: A private, written warning from community leaders, providing -clarity around the nature of the violation and an explanation of why the -behavior was inappropriate. A public apology may be requested. - -### 2. Warning - -**Community Impact**: A violation through a single incident or series -of actions. - -**Consequence**: A warning with consequences for continued behavior. No -interaction with the people involved, including unsolicited interaction with -those enforcing the Code of Conduct, for a specified period of time. This -includes avoiding interactions in community spaces as well as external channels -like social media. Violating these terms may lead to a temporary or -permanent ban. - -### 3. Temporary Ban - -**Community Impact**: A serious violation of community standards, including -sustained inappropriate behavior. - -**Consequence**: A temporary ban from any sort of interaction or public -communication with the community for a specified period of time. No public or -private interaction with the people involved, including unsolicited interaction -with those enforcing the Code of Conduct, is allowed during this period. -Violating these terms may lead to a permanent ban. - -### 4. Permanent Ban - -**Community Impact**: Demonstrating a pattern of violation of community -standards, including sustained inappropriate behavior, harassment of an -individual, or aggression toward or disparagement of classes of individuals. - -**Consequence**: A permanent ban from any sort of public interaction within -the community. - -## Attribution - -This Code of Conduct is adapted from the [Contributor Covenant][homepage], -version 2.0, available at -https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. - -Community Impact Guidelines were inspired by [Mozilla's code of conduct -enforcement ladder](https://github.com/mozilla/diversity). - -[homepage]: https://www.contributor-covenant.org - -For answers to common questions about this code of conduct, see the FAQ at -https://www.contributor-covenant.org/faq. Translations are available at -https://www.contributor-covenant.org/translations. diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index d3e4193eef2..00000000000 --- a/Dockerfile +++ /dev/null @@ -1,68 +0,0 @@ -FROM node:18-alpine AS base - -FROM base AS deps - -RUN apk add --no-cache libc6-compat - -WORKDIR /app - -COPY package.json yarn.lock ./ - -RUN yarn config set registry 'https://registry.npmmirror.com/' -RUN yarn install - -FROM base AS builder - -RUN apk update && apk add --no-cache git - -ENV OPENAI_API_KEY="" -ENV GOOGLE_API_KEY="" -ENV CODE="" - -WORKDIR /app -COPY --from=deps /app/node_modules ./node_modules -COPY . . - -RUN yarn build - -FROM base AS runner -WORKDIR /app - -RUN apk add proxychains-ng - -ENV PROXY_URL="" -ENV OPENAI_API_KEY="" -ENV GOOGLE_API_KEY="" -ENV CODE="" -ENV ENABLE_MCP="" - -COPY --from=builder /app/public ./public -COPY --from=builder /app/.next/standalone ./ -COPY --from=builder /app/.next/static ./.next/static -COPY --from=builder /app/.next/server ./.next/server - -RUN mkdir -p /app/app/mcp && chmod 777 /app/app/mcp -COPY --from=builder /app/app/mcp/mcp_config.default.json /app/app/mcp/mcp_config.json - -EXPOSE 3000 - -CMD if [ -n "$PROXY_URL" ]; then \ - export HOSTNAME="0.0.0.0"; \ - protocol=$(echo $PROXY_URL | cut -d: -f1); \ - host=$(echo $PROXY_URL | cut -d/ -f3 | cut -d: -f1); \ - port=$(echo $PROXY_URL | cut -d: -f3); \ - conf=/etc/proxychains.conf; \ - echo "strict_chain" > $conf; \ - echo "proxy_dns" >> $conf; \ - echo "remote_dns_subnet 224" >> $conf; \ - echo "tcp_read_time_out 15000" >> $conf; \ - echo "tcp_connect_time_out 8000" >> $conf; \ - echo "localnet 127.0.0.0/255.0.0.0" >> $conf; \ - echo "localnet ::1/128" >> $conf; \ - echo "[ProxyList]" >> $conf; \ - echo "$protocol $host $port" >> $conf; \ - cat /etc/proxychains.conf; \ - proxychains -f $conf node server.js; \ - else \ - node server.js; \ - fi diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 4864ab00d2c..00000000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2023-2025 NextChat - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/README.md b/README.md index 01a223d3b63..e215bc4ccf1 100644 --- a/README.md +++ b/README.md @@ -1,482 +1,36 @@ -
+This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app). - - icon - +## Getting Started -

NextChat

+First, run the development server: -English / [简体中文](./README_CN.md) - -ChatGPTNextWeb%2FChatGPT-Next-Web | Trendshift - -✨ Light and Fast AI Assistant,with Claude, DeepSeek, GPT4 & Gemini Pro support. - -[![Saas][Saas-image]][saas-url] -[![Web][Web-image]][web-url] -[![Windows][Windows-image]][download-url] -[![MacOS][MacOS-image]][download-url] -[![Linux][Linux-image]][download-url] - -[NextChatAI](https://nextchat.club?utm_source=readme) / [iOS APP](https://apps.apple.com/us/app/nextchat-ai/id6743085599) / [Web App Demo](https://app.nextchat.club) / [Desktop App](https://github.com/Yidadaa/ChatGPT-Next-Web/releases) / [Enterprise Edition](#enterprise-edition) - -[saas-url]: https://nextchat.club?utm_source=readme -[saas-image]: https://img.shields.io/badge/NextChat-Saas-green?logo=microsoftedge -[web-url]: https://app.nextchat.club/ -[download-url]: https://github.com/Yidadaa/ChatGPT-Next-Web/releases -[Web-image]: https://img.shields.io/badge/Web-PWA-orange?logo=microsoftedge -[Windows-image]: https://img.shields.io/badge/-Windows-blue?logo=windows -[MacOS-image]: https://img.shields.io/badge/-MacOS-black?logo=apple -[Linux-image]: https://img.shields.io/badge/-Linux-333?logo=ubuntu - -[Deploy on Zeabur](https://zeabur.com/templates/ZBUEFA) [Deploy on Vercel](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat) [Open in Gitpod](https://gitpod.io/#https://github.com/ChatGPTNextWeb/NextChat) - -[](https://monica.im/?utm=nxcrp) - -
- -## ❤️ Sponsor AI API - - - icon - - -[302.AI](https://302.ai/) is a pay-as-you-go AI application platform that offers the most comprehensive AI APIs and online applications available. - -## 🥳 Cheer for NextChat iOS Version Online! - -> [👉 Click Here to Install Now](https://apps.apple.com/us/app/nextchat-ai/id6743085599) - -> [❤️ Source Code Coming Soon](https://github.com/ChatGPTNextWeb/NextChat-iOS) - -![Github iOS Image](https://github.com/user-attachments/assets/e0aa334f-4c13-4dc9-8310-e3b09fa4b9f3) - -## 🫣 NextChat Support MCP ! - -> Before build, please set env ENABLE_MCP=true - - - -## Enterprise Edition - -Meeting Your Company's Privatization and Customization Deployment Requirements: - -- **Brand Customization**: Tailored VI/UI to seamlessly align with your corporate brand image. -- **Resource Integration**: Unified configuration and management of dozens of AI resources by company administrators, ready for use by team members. -- **Permission Control**: Clearly defined member permissions, resource permissions, and knowledge base permissions, all controlled via a corporate-grade Admin Panel. -- **Knowledge Integration**: Combining your internal knowledge base with AI capabilities, making it more relevant to your company's specific business needs compared to general AI. -- **Security Auditing**: Automatically intercept sensitive inquiries and trace all historical conversation records, ensuring AI adherence to corporate information security standards. -- **Private Deployment**: Enterprise-level private deployment supporting various mainstream private cloud solutions, ensuring data security and privacy protection. -- **Continuous Updates**: Ongoing updates and upgrades in cutting-edge capabilities like multimodal AI, ensuring consistent innovation and advancement. - -For enterprise inquiries, please contact: **business@nextchat.dev** - -## Screenshots - -![Settings](./docs/images/settings.png) - -![More](./docs/images/more.png) - -## Features - -- **Deploy for free with one-click** on Vercel in under 1 minute -- Compact client (~5MB) on Linux/Windows/MacOS, [download it now](https://github.com/Yidadaa/ChatGPT-Next-Web/releases) -- Fully compatible with self-deployed LLMs, recommended for use with [RWKV-Runner](https://github.com/josStorer/RWKV-Runner) or [LocalAI](https://github.com/go-skynet/LocalAI) -- Privacy first, all data is stored locally in the browser -- Markdown support: LaTex, mermaid, code highlight, etc. -- Responsive design, dark mode and PWA -- Fast first screen loading speed (~100kb), support streaming response -- New in v2: create, share and debug your chat tools with prompt templates (mask) -- Awesome prompts powered by [awesome-chatgpt-prompts-zh](https://github.com/PlexPt/awesome-chatgpt-prompts-zh) and [awesome-chatgpt-prompts](https://github.com/f/awesome-chatgpt-prompts) -- Automatically compresses chat history to support long conversations while also saving your tokens -- I18n: English, 简体中文, 繁体中文, 日本語, Français, Español, Italiano, Türkçe, Deutsch, Tiếng Việt, Русский, Čeština, 한국어, Indonesia - -
- -![主界面](./docs/images/cover.png) - -
- -## Roadmap - -- [x] System Prompt: pin a user defined prompt as system prompt [#138](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/138) -- [x] User Prompt: user can edit and save custom prompts to prompt list -- [x] Prompt Template: create a new chat with pre-defined in-context prompts [#993](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/993) -- [x] Share as image, share to ShareGPT [#1741](https://github.com/Yidadaa/ChatGPT-Next-Web/pull/1741) -- [x] Desktop App with tauri -- [x] Self-host Model: Fully compatible with [RWKV-Runner](https://github.com/josStorer/RWKV-Runner), as well as server deployment of [LocalAI](https://github.com/go-skynet/LocalAI): llama/gpt4all/rwkv/vicuna/koala/gpt4all-j/cerebras/falcon/dolly etc. -- [x] Artifacts: Easily preview, copy and share generated content/webpages through a separate window [#5092](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/pull/5092) -- [x] Plugins: support network search, calculator, any other apis etc. [#165](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/165) [#5353](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5353) - - [x] network search, calculator, any other apis etc. [#165](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/165) [#5353](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5353) -- [x] Supports Realtime Chat [#5672](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5672) -- [ ] local knowledge base - -## What's New - -- 🚀 v2.15.8 Now supports Realtime Chat [#5672](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5672) -- 🚀 v2.15.4 The Application supports using Tauri fetch LLM API, MORE SECURITY! [#5379](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5379) -- 🚀 v2.15.0 Now supports Plugins! Read this: [NextChat-Awesome-Plugins](https://github.com/ChatGPTNextWeb/NextChat-Awesome-Plugins) -- 🚀 v2.14.0 Now supports Artifacts & SD -- 🚀 v2.10.1 support Google Gemini Pro model. -- 🚀 v2.9.11 you can use azure endpoint now. -- 🚀 v2.8 now we have a client that runs across all platforms! -- 🚀 v2.7 let's share conversations as image, or share to ShareGPT! -- 🚀 v2.0 is released, now you can create prompt templates, turn your ideas into reality! Read this: [ChatGPT Prompt Engineering Tips: Zero, One and Few Shot Prompting](https://www.allabtai.com/prompt-engineering-tips-zero-one-and-few-shot-prompting/). - -## Get Started - -1. Get [OpenAI API Key](https://platform.openai.com/account/api-keys); -2. Click - [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FYidadaa%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=chatgpt-next-web&repository-name=ChatGPT-Next-Web), remember that `CODE` is your page password; -3. Enjoy :) - -## FAQ - -[English > FAQ](./docs/faq-en.md) - -## Keep Updated - -If you have deployed your own project with just one click following the steps above, you may encounter the issue of "Updates Available" constantly showing up. This is because Vercel will create a new project for you by default instead of forking this project, resulting in the inability to detect updates correctly. - -We recommend that you follow the steps below to re-deploy: - -- Delete the original repository; -- Use the fork button in the upper right corner of the page to fork this project; -- Choose and deploy in Vercel again, [please see the detailed tutorial](./docs/vercel-cn.md). - -### Enable Automatic Updates - -> If you encounter a failure of Upstream Sync execution, please [manually update code](./README.md#manually-updating-code). - -After forking the project, due to the limitations imposed by GitHub, you need to manually enable Workflows and Upstream Sync Action on the Actions page of the forked project. Once enabled, automatic updates will be scheduled every hour: - -![Automatic Updates](./docs/images/enable-actions.jpg) - -![Enable Automatic Updates](./docs/images/enable-actions-sync.jpg) - -### Manually Updating Code - -If you want to update instantly, you can check out the [GitHub documentation](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork) to learn how to synchronize a forked project with upstream code. - -You can star or watch this project or follow author to get release notifications in time. - -## Access Password - -This project provides limited access control. Please add an environment variable named `CODE` on the vercel environment variables page. The value should be passwords separated by comma like this: - -``` -code1,code2,code3 -``` - -After adding or modifying this environment variable, please redeploy the project for the changes to take effect. - -## Environment Variables - -### `CODE` (optional) - -Access password, separated by comma. - -### `OPENAI_API_KEY` (required) - -Your openai api key, join multiple api keys with comma. - -### `BASE_URL` (optional) - -> Default: `https://api.openai.com` - -> Examples: `http://your-openai-proxy.com` - -Override openai api request base url. - -### `OPENAI_ORG_ID` (optional) - -Specify OpenAI organization ID. - -### `AZURE_URL` (optional) - -> Example: https://{azure-resource-url}/openai - -Azure deploy url. - -### `AZURE_API_KEY` (optional) - -Azure Api Key. - -### `AZURE_API_VERSION` (optional) - -Azure Api Version, find it at [Azure Documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions). - -### `GOOGLE_API_KEY` (optional) - -Google Gemini Pro Api Key. - -### `GOOGLE_URL` (optional) - -Google Gemini Pro Api Url. - -### `ANTHROPIC_API_KEY` (optional) - -anthropic claude Api Key. - -### `ANTHROPIC_API_VERSION` (optional) - -anthropic claude Api version. - -### `ANTHROPIC_URL` (optional) - -anthropic claude Api Url. - -### `BAIDU_API_KEY` (optional) - -Baidu Api Key. - -### `BAIDU_SECRET_KEY` (optional) - -Baidu Secret Key. - -### `BAIDU_URL` (optional) - -Baidu Api Url. - -### `BYTEDANCE_API_KEY` (optional) - -ByteDance Api Key. - -### `BYTEDANCE_URL` (optional) - -ByteDance Api Url. - -### `ALIBABA_API_KEY` (optional) - -Alibaba Cloud Api Key. - -### `ALIBABA_URL` (optional) - -Alibaba Cloud Api Url. - -### `IFLYTEK_URL` (Optional) - -iflytek Api Url. - -### `IFLYTEK_API_KEY` (Optional) - -iflytek Api Key. - -### `IFLYTEK_API_SECRET` (Optional) - -iflytek Api Secret. - -### `CHATGLM_API_KEY` (optional) - -ChatGLM Api Key. - -### `CHATGLM_URL` (optional) - -ChatGLM Api Url. - -### `DEEPSEEK_API_KEY` (optional) - -DeepSeek Api Key. - -### `DEEPSEEK_URL` (optional) - -DeepSeek Api Url. - -### `HIDE_USER_API_KEY` (optional) - -> Default: Empty - -If you do not want users to input their own API key, set this value to 1. - -### `DISABLE_GPT4` (optional) - -> Default: Empty - -If you do not want users to use GPT-4, set this value to 1. - -### `ENABLE_BALANCE_QUERY` (optional) - -> Default: Empty - -If you do want users to query balance, set this value to 1. - -### `DISABLE_FAST_LINK` (optional) - -> Default: Empty - -If you want to disable parse settings from url, set this to 1. - -### `CUSTOM_MODELS` (optional) - -> Default: Empty -> Example: `+llama,+claude-2,-gpt-3.5-turbo,gpt-4-1106-preview=gpt-4-turbo` means add `llama, claude-2` to model list, and remove `gpt-3.5-turbo` from list, and display `gpt-4-1106-preview` as `gpt-4-turbo`. - -To control custom models, use `+` to add a custom model, use `-` to hide a model, use `name=displayName` to customize model name, separated by comma. - -User `-all` to disable all default models, `+all` to enable all default models. - -For Azure: use `modelName@Azure=deploymentName` to customize model name and deployment name. - -> Example: `+gpt-3.5-turbo@Azure=gpt35` will show option `gpt35(Azure)` in model list. -> If you only can use Azure model, `-all,+gpt-3.5-turbo@Azure=gpt35` will `gpt35(Azure)` the only option in model list. - -For ByteDance: use `modelName@bytedance=deploymentName` to customize model name and deployment name. - -> Example: `+Doubao-lite-4k@bytedance=ep-xxxxx-xxx` will show option `Doubao-lite-4k(ByteDance)` in model list. - -### `DEFAULT_MODEL` (optional) - -Change default model - -### `VISION_MODELS` (optional) - -> Default: Empty -> Example: `gpt-4-vision,claude-3-opus,my-custom-model` means add vision capabilities to these models in addition to the default pattern matches (which detect models containing keywords like "vision", "claude-3", "gemini-1.5", etc). - -Add additional models to have vision capabilities, beyond the default pattern matching. Multiple models should be separated by commas. - -### `WHITE_WEBDAV_ENDPOINTS` (optional) - -You can use this option if you want to increase the number of webdav service addresses you are allowed to access, as required by the format: - -- Each address must be a complete endpoint - > `https://xxxx/yyy` -- Multiple addresses are connected by ', ' - -### `DEFAULT_INPUT_TEMPLATE` (optional) - -Customize the default template used to initialize the User Input Preprocessing configuration item in Settings. - -### `STABILITY_API_KEY` (optional) - -Stability API key. - -### `STABILITY_URL` (optional) - -Customize Stability API url. - -### `ENABLE_MCP` (optional) - -Enable MCP(Model Context Protocol)Feature - -### `SILICONFLOW_API_KEY` (optional) - -SiliconFlow API Key. - -### `SILICONFLOW_URL` (optional) - -SiliconFlow API URL. - -### `AI302_API_KEY` (optional) - -302.AI API Key. - -### `AI302_URL` (optional) - -302.AI API URL. - -## Requirements - -NodeJS >= 18, Docker >= 20 - -## Development - -[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web) - -Before starting development, you must create a new `.env.local` file at project root, and place your api key into it: - -``` -OPENAI_API_KEY= - -# if you are not able to access openai service, use this BASE_URL -BASE_URL=https://chatgpt1.nextweb.fun/api/proxy -``` - -### Local Development - -```shell -# 1. install nodejs and yarn first -# 2. config local env vars in `.env.local` -# 3. run -yarn install +```bash +npm run dev +# or yarn dev +# or +pnpm dev +# or +bun dev ``` -## Deployment - -### Docker (Recommended) - -```shell -docker pull yidadaa/chatgpt-next-web - -docker run -d -p 3000:3000 \ - -e OPENAI_API_KEY=sk-xxxx \ - -e CODE=your-password \ - yidadaa/chatgpt-next-web -``` - -You can start service behind a proxy: - -```shell -docker run -d -p 3000:3000 \ - -e OPENAI_API_KEY=sk-xxxx \ - -e CODE=your-password \ - -e PROXY_URL=http://localhost:7890 \ - yidadaa/chatgpt-next-web -``` - -If your proxy needs password, use: - -```shell --e PROXY_URL="http://127.0.0.1:7890 user pass" -``` - -If enable MCP, use: - -``` -docker run -d -p 3000:3000 \ - -e OPENAI_API_KEY=sk-xxxx \ - -e CODE=your-password \ - -e ENABLE_MCP=true \ - yidadaa/chatgpt-next-web -``` - -### Shell - -```shell -bash <(curl -s https://raw.githubusercontent.com/Yidadaa/ChatGPT-Next-Web/main/scripts/setup.sh) -``` - -## Synchronizing Chat Records (UpStash) - -| [简体中文](./docs/synchronise-chat-logs-cn.md) | [English](./docs/synchronise-chat-logs-en.md) | [Italiano](./docs/synchronise-chat-logs-es.md) | [日本語](./docs/synchronise-chat-logs-ja.md) | [한국어](./docs/synchronise-chat-logs-ko.md) - -## Documentation - -> Please go to the [docs][./docs] directory for more documentation instructions. - -- [Deploy with cloudflare (Deprecated)](./docs/cloudflare-pages-en.md) -- [Frequent Ask Questions](./docs/faq-en.md) -- [How to add a new translation](./docs/translation.md) -- [How to use Vercel (No English)](./docs/vercel-cn.md) -- [User Manual (Only Chinese, WIP)](./docs/user-manual-cn.md) +Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. -## Translation +You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. -If you want to add a new translation, read this [document](./docs/translation.md). +This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel. -## Donation +## Learn More -[Buy Me a Coffee](https://www.buymeacoffee.com/yidadaa) +To learn more about Next.js, take a look at the following resources: -## Special Thanks +- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. +- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. -### Contributors +You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome! - - - +## Deploy on Vercel -## LICENSE +The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. -[MIT](https://opensource.org/license/mit/) +Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details. diff --git a/README_CN.md b/README_CN.md deleted file mode 100644 index f4c441ad006..00000000000 --- a/README_CN.md +++ /dev/null @@ -1,392 +0,0 @@ -
- - - icon - - -

NextChat

- -一键免费部署你的私人 ChatGPT 网页应用,支持 Claude, GPT4 & Gemini Pro 模型。 - -[NextChatAI](https://nextchat.club?utm_source=readme) / [企业版](#%E4%BC%81%E4%B8%9A%E7%89%88) / [演示 Demo](https://chat-gpt-next-web.vercel.app/) / [反馈 Issues](https://github.com/Yidadaa/ChatGPT-Next-Web/issues) / [加入 Discord](https://discord.gg/zrhvHCr79N) - -[Deploy on Zeabur](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat) [Deploy on Zeabur](https://zeabur.com/templates/ZBUEFA) [Open in Gitpod](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web) - -
- -## Sponsor AI API - - - icon - - -[302.AI](https://302.ai/) 是一个按需付费的AI应用平台,提供市面上最全的AI API和AI在线应用。 - -## 企业版 - -满足您公司私有化部署和定制需求 - -- **品牌定制**:企业量身定制 VI/UI,与企业品牌形象无缝契合 -- **资源集成**:由企业管理人员统一配置和管理数十种 AI 资源,团队成员开箱即用 -- **权限管理**:成员权限、资源权限、知识库权限层级分明,企业级 Admin Panel 统一控制 -- **知识接入**:企业内部知识库与 AI 能力相结合,比通用 AI 更贴近企业自身业务需求 -- **安全审计**:自动拦截敏感提问,支持追溯全部历史对话记录,让 AI 也能遵循企业信息安全规范 -- **私有部署**:企业级私有部署,支持各类主流私有云部署,确保数据安全和隐私保护 -- **持续更新**:提供多模态、智能体等前沿能力持续更新升级服务,常用常新、持续先进 - -企业版咨询: **business@nextchat.dev** - - - -## 开始使用 - -1. 准备好你的 [OpenAI API Key](https://platform.openai.com/account/api-keys); -2. 点击右侧按钮开始部署: - [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FYidadaa%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&env=GOOGLE_API_KEY&project-name=chatgpt-next-web&repository-name=ChatGPT-Next-Web),直接使用 Github 账号登录即可,记得在环境变量页填入 API Key 和[页面访问密码](#配置页面访问密码) CODE; -3. 部署完毕后,即可开始使用; -4. (可选)[绑定自定义域名](https://vercel.com/docs/concepts/projects/domains/add-a-domain):Vercel 分配的域名 DNS 在某些区域被污染了,绑定自定义域名即可直连。 - -
- -![主界面](./docs/images/cover.png) - -
- -## 保持更新 - -如果你按照上述步骤一键部署了自己的项目,可能会发现总是提示“存在更新”的问题,这是由于 Vercel 会默认为你创建一个新项目而不是 fork 本项目,这会导致无法正确地检测更新。 -推荐你按照下列步骤重新部署: - -- 删除掉原先的仓库; -- 使用页面右上角的 fork 按钮,fork 本项目; -- 在 Vercel 重新选择并部署,[请查看详细教程](./docs/vercel-cn.md#如何新建项目)。 - -### 打开自动更新 - -> 如果你遇到了 Upstream Sync 执行错误,请[手动 Sync Fork 一次](./README_CN.md#手动更新代码)! - -当你 fork 项目之后,由于 Github 的限制,需要手动去你 fork 后的项目的 Actions 页面启用 Workflows,并启用 Upstream Sync Action,启用之后即可开启每小时定时自动更新: - -![自动更新](./docs/images/enable-actions.jpg) - -![启用自动更新](./docs/images/enable-actions-sync.jpg) - -### 手动更新代码 - -如果你想让手动立即更新,可以查看 [Github 的文档](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork) 了解如何让 fork 的项目与上游代码同步。 - -你可以 star/watch 本项目或者 follow 作者来及时获得新功能更新通知。 - -## 配置页面访问密码 - -> 配置密码后,用户需要在设置页手动填写访问码才可以正常聊天,否则会通过消息提示未授权状态。 - -> **警告**:请务必将密码的位数设置得足够长,最好 7 位以上,否则[会被爆破](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/518)。 - -本项目提供有限的权限控制功能,请在 Vercel 项目控制面板的环境变量页增加名为 `CODE` 的环境变量,值为用英文逗号分隔的自定义密码: - -``` -code1,code2,code3 -``` - -增加或修改该环境变量后,请**重新部署**项目使改动生效。 - -## 环境变量 - -> 本项目大多数配置项都通过环境变量来设置,教程:[如何修改 Vercel 环境变量](./docs/vercel-cn.md)。 - -### `OPENAI_API_KEY` (必填项) - -OpenAI 密钥,你在 openai 账户页面申请的 api key,使用英文逗号隔开多个 key,这样可以随机轮询这些 key。 - -### `CODE` (可选) - -访问密码,可选,可以使用逗号隔开多个密码。 - -**警告**:如果不填写此项,则任何人都可以直接使用你部署后的网站,可能会导致你的 token 被急速消耗完毕,建议填写此选项。 - -### `BASE_URL` (可选) - -> Default: `https://api.openai.com` - -> Examples: `http://your-openai-proxy.com` - -OpenAI 接口代理 URL,如果你手动配置了 openai 接口代理,请填写此选项。 - -> 如果遇到 ssl 证书问题,请将 `BASE_URL` 的协议设置为 http。 - -### `OPENAI_ORG_ID` (可选) - -指定 OpenAI 中的组织 ID。 - -### `AZURE_URL` (可选) - -> 形如:https://{azure-resource-url}/openai - -Azure 部署地址。 - -### `AZURE_API_KEY` (可选) - -Azure 密钥。 - -### `AZURE_API_VERSION` (可选) - -Azure Api 版本,你可以在这里找到:[Azure 文档](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions)。 - -### `GOOGLE_API_KEY` (可选) - -Google Gemini Pro 密钥. - -### `GOOGLE_URL` (可选) - -Google Gemini Pro Api Url. - -### `ANTHROPIC_API_KEY` (可选) - -anthropic claude Api Key. - -### `ANTHROPIC_API_VERSION` (可选) - -anthropic claude Api version. - -### `ANTHROPIC_URL` (可选) - -anthropic claude Api Url. - -### `BAIDU_API_KEY` (可选) - -Baidu Api Key. - -### `BAIDU_SECRET_KEY` (可选) - -Baidu Secret Key. - -### `BAIDU_URL` (可选) - -Baidu Api Url. - -### `BYTEDANCE_API_KEY` (可选) - -ByteDance Api Key. - -### `BYTEDANCE_URL` (可选) - -ByteDance Api Url. - -### `ALIBABA_API_KEY` (可选) - -阿里云(千问)Api Key. - -### `ALIBABA_URL` (可选) - -阿里云(千问)Api Url. - -### `IFLYTEK_URL` (可选) - -讯飞星火Api Url. - -### `IFLYTEK_API_KEY` (可选) - -讯飞星火Api Key. - -### `IFLYTEK_API_SECRET` (可选) - -讯飞星火Api Secret. - -### `CHATGLM_API_KEY` (可选) - -ChatGLM Api Key. - -### `CHATGLM_URL` (可选) - -ChatGLM Api Url. - -### `DEEPSEEK_API_KEY` (可选) - -DeepSeek Api Key. - -### `DEEPSEEK_URL` (可选) - -DeepSeek Api Url. - -### `HIDE_USER_API_KEY` (可选) - -如果你不想让用户自行填入 API Key,将此环境变量设置为 1 即可。 - -### `DISABLE_GPT4` (可选) - -如果你不想让用户使用 GPT-4,将此环境变量设置为 1 即可。 - -### `ENABLE_BALANCE_QUERY` (可选) - -如果你想启用余额查询功能,将此环境变量设置为 1 即可。 - -### `DISABLE_FAST_LINK` (可选) - -如果你想禁用从链接解析预制设置,将此环境变量设置为 1 即可。 - -### `WHITE_WEBDAV_ENDPOINTS` (可选) - -如果你想增加允许访问的webdav服务地址,可以使用该选项,格式要求: - -- 每一个地址必须是一个完整的 endpoint - > `https://xxxx/xxx` -- 多个地址以`,`相连 - -### `CUSTOM_MODELS` (可选) - -> 示例:`+qwen-7b-chat,+glm-6b,-gpt-3.5-turbo,gpt-4-1106-preview=gpt-4-turbo` 表示增加 `qwen-7b-chat` 和 `glm-6b` 到模型列表,而从列表中删除 `gpt-3.5-turbo`,并将 `gpt-4-1106-preview` 模型名字展示为 `gpt-4-turbo`。 -> 如果你想先禁用所有模型,再启用指定模型,可以使用 `-all,+gpt-3.5-turbo`,则表示仅启用 `gpt-3.5-turbo` - -用来控制模型列表,使用 `+` 增加一个模型,使用 `-` 来隐藏一个模型,使用 `模型名=展示名` 来自定义模型的展示名,用英文逗号隔开。 - -在Azure的模式下,支持使用`modelName@Azure=deploymentName`的方式配置模型名称和部署名称(deploy-name) - -> 示例:`+gpt-3.5-turbo@Azure=gpt35`这个配置会在模型列表显示一个`gpt35(Azure)`的选项。 -> 如果你只能使用Azure模式,那么设置 `-all,+gpt-3.5-turbo@Azure=gpt35` 则可以让对话的默认使用 `gpt35(Azure)` - -在ByteDance的模式下,支持使用`modelName@bytedance=deploymentName`的方式配置模型名称和部署名称(deploy-name) - -> 示例: `+Doubao-lite-4k@bytedance=ep-xxxxx-xxx`这个配置会在模型列表显示一个`Doubao-lite-4k(ByteDance)`的选项 - -### `DEFAULT_MODEL` (可选) - -更改默认模型 - -### `VISION_MODELS` (可选) - -> 默认值:空 -> 示例:`gpt-4-vision,claude-3-opus,my-custom-model` 表示为这些模型添加视觉能力,作为对默认模式匹配的补充(默认会检测包含"vision"、"claude-3"、"gemini-1.5"等关键词的模型)。 - -在默认模式匹配之外,添加更多具有视觉能力的模型。多个模型用逗号分隔。 - -### `DEFAULT_INPUT_TEMPLATE` (可选) - -自定义默认的 template,用于初始化『设置』中的『用户输入预处理』配置项 - -### `STABILITY_API_KEY` (optional) - -Stability API密钥 - -### `STABILITY_URL` (optional) - -自定义的Stability API请求地址 - -### `ENABLE_MCP` (optional) - -启用MCP(Model Context Protocol)功能 - -### `SILICONFLOW_API_KEY` (optional) - -SiliconFlow API Key. - -### `SILICONFLOW_URL` (optional) - -SiliconFlow API URL. - -### `AI302_API_KEY` (optional) - -302.AI API Key. - -### `AI302_URL` (optional) - -302.AI API URL. - -## 开发 - -点击下方按钮,开始二次开发: - -[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web) - -在开始写代码之前,需要在项目根目录新建一个 `.env.local` 文件,里面填入环境变量: - -``` -OPENAI_API_KEY= - -# 中国大陆用户,可以使用本项目自带的代理进行开发,你也可以自由选择其他代理地址 -BASE_URL=https://b.nextweb.fun/api/proxy -``` - -### 本地开发 - -1. 安装 nodejs 18 和 yarn,具体细节请询问 ChatGPT; -2. 执行 `yarn install && yarn dev` 即可。⚠️ 注意:此命令仅用于本地开发,不要用于部署! -3. 如果你想本地部署,请使用 `yarn install && yarn build && yarn start` 命令,你可以配合 pm2 来守护进程,防止被杀死,详情询问 ChatGPT。 - -## 部署 - -### 宝塔面板部署 - -> [简体中文 > 如何通过宝塔一键部署](./docs/bt-cn.md) - -### 容器部署 (推荐) - -> Docker 版本需要在 20 及其以上,否则会提示找不到镜像。 - -> ⚠️ 注意:docker 版本在大多数时间都会落后最新的版本 1 到 2 天,所以部署后会持续出现“存在更新”的提示,属于正常现象。 - -```shell -docker pull yidadaa/chatgpt-next-web - -docker run -d -p 3000:3000 \ - -e OPENAI_API_KEY=sk-xxxx \ - -e CODE=页面访问密码 \ - yidadaa/chatgpt-next-web -``` - -你也可以指定 proxy: - -```shell -docker run -d -p 3000:3000 \ - -e OPENAI_API_KEY=sk-xxxx \ - -e CODE=页面访问密码 \ - --net=host \ - -e PROXY_URL=http://127.0.0.1:7890 \ - yidadaa/chatgpt-next-web -``` - -如需启用 MCP 功能,可以使用: - -```shell -docker run -d -p 3000:3000 \ - -e OPENAI_API_KEY=sk-xxxx \ - -e CODE=页面访问密码 \ - -e ENABLE_MCP=true \ - yidadaa/chatgpt-next-web -``` - -如果你的本地代理需要账号密码,可以使用: - -```shell --e PROXY_URL="http://127.0.0.1:7890 user password" -``` - -如果你需要指定其他环境变量,请自行在上述命令中增加 `-e 环境变量=环境变量值` 来指定。 - -### 本地部署 - -在控制台运行下方命令: - -```shell -bash <(curl -s https://raw.githubusercontent.com/Yidadaa/ChatGPT-Next-Web/main/scripts/setup.sh) -``` - -⚠️ 注意:如果你安装过程中遇到了问题,请使用 docker 部署。 - -## 鸣谢 - -### 捐赠者 - -> 见英文版。 - -### 贡献者 - -[见项目贡献者列表](https://github.com/Yidadaa/ChatGPT-Next-Web/graphs/contributors) - -### 相关项目 - -- [one-api](https://github.com/songquanpeng/one-api): 一站式大模型额度管理平台,支持市面上所有主流大语言模型 - -## 开源协议 - -[MIT](https://opensource.org/license/mit/) diff --git a/README_JA.md b/README_JA.md deleted file mode 100644 index 917cc1262c0..00000000000 --- a/README_JA.md +++ /dev/null @@ -1,323 +0,0 @@ -
-プレビュー - -

NextChat

- -ワンクリックで無料であなた専用の ChatGPT ウェブアプリをデプロイ。GPT3、GPT4 & Gemini Pro モデルをサポート。 - -[NextChatAI](https://nextchat.club?utm_source=readme) / [企業版](#企業版) / [デモ](https://chat-gpt-next-web.vercel.app/) / [フィードバック](https://github.com/Yidadaa/ChatGPT-Next-Web/issues) / [Discordに参加](https://discord.gg/zrhvHCr79N) - -[Zeaburでデプロイ](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat) [Zeaburでデプロイ](https://zeabur.com/templates/ZBUEFA) [Gitpodで開く](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web) - -
- -## Sponsor AI API - - - icon - - -[302.AI](https://302.ai/) は、オンデマンドで支払うAIアプリケーションプラットフォームで、最も安全なAI APIとAIオンラインアプリケーションを提供します。 - -## 企業版 - -あなたの会社のプライベートデプロイとカスタマイズのニーズに応える - -- **ブランドカスタマイズ**:企業向けに特別に設計された VI/UI、企業ブランドイメージとシームレスにマッチ -- **リソース統合**:企業管理者が数十種類のAIリソースを統一管理、チームメンバーはすぐに使用可能 -- **権限管理**:メンバーの権限、リソースの権限、ナレッジベースの権限を明確にし、企業レベルのAdmin Panelで統一管理 -- **知識の統合**:企業内部のナレッジベースとAI機能を結びつけ、汎用AIよりも企業自身の業務ニーズに近づける -- **セキュリティ監査**:機密質問を自動的にブロックし、すべての履歴対話を追跡可能にし、AIも企業の情報セキュリティ基準に従わせる -- **プライベートデプロイ**:企業レベルのプライベートデプロイ、主要なプライベートクラウドデプロイをサポートし、データのセキュリティとプライバシーを保護 -- **継続的な更新**:マルチモーダル、エージェントなどの最先端機能を継続的に更新し、常に最新であり続ける - -企業版のお問い合わせ: **business@nextchat.dev** - -## 始めに - -1. [OpenAI API Key](https://platform.openai.com/account/api-keys)を準備する; -2. 右側のボタンをクリックしてデプロイを開始: - [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FYidadaa%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&env=GOOGLE_API_KEY&project-name=chatgpt-next-web&repository-name=ChatGPT-Next-Web) 、GitHubアカウントで直接ログインし、環境変数ページにAPI Keyと[ページアクセスパスワード](#設定ページアクセスパスワード) CODEを入力してください; -3. デプロイが完了したら、すぐに使用を開始できます; -4. (オプション)[カスタムドメインをバインド](https://vercel.com/docs/concepts/projects/domains/add-a-domain):Vercelが割り当てたドメインDNSは一部の地域で汚染されているため、カスタムドメインをバインドすると直接接続できます。 - -
- -![メインインターフェース](./docs/images/cover.png) - -
- -## 更新を維持する - -もし上記の手順に従ってワンクリックでプロジェクトをデプロイした場合、「更新があります」というメッセージが常に表示されることがあります。これは、Vercel がデフォルトで新しいプロジェクトを作成するためで、本プロジェクトを fork していないことが原因です。そのため、正しく更新を検出できません。 - -以下の手順に従って再デプロイすることをお勧めします: - -- 元のリポジトリを削除する -- ページ右上の fork ボタンを使って、本プロジェクトを fork する -- Vercel で再度選択してデプロイする、[詳細な手順はこちらを参照してください](./docs/vercel-ja.md)。 - -### 自動更新を開く - -> Upstream Sync の実行エラーが発生した場合は、[手動で Sync Fork](./README_JA.md#手動でコードを更新する) してください! - -プロジェクトを fork した後、GitHub の制限により、fork 後のプロジェクトの Actions ページで Workflows を手動で有効にし、Upstream Sync Action を有効にする必要があります。有効化後、毎時の定期自動更新が可能になります: - -![自動更新](./docs/images/enable-actions.jpg) - -![自動更新を有効にする](./docs/images/enable-actions-sync.jpg) - -### 手動でコードを更新する - -手動で即座に更新したい場合は、[GitHub のドキュメント](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork)を参照して、fork したプロジェクトを上流のコードと同期する方法を確認してください。 - -このプロジェクトをスターまたはウォッチしたり、作者をフォローすることで、新機能の更新通知をすぐに受け取ることができます。 - -## ページアクセスパスワードを設定する - -> パスワードを設定すると、ユーザーは設定ページでアクセスコードを手動で入力しない限り、通常のチャットができず、未承認の状態であることを示すメッセージが表示されます。 - -> **警告**:パスワードの桁数は十分に長く設定してください。7桁以上が望ましいです。さもないと、[ブルートフォース攻撃を受ける可能性があります](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/518)。 - -このプロジェクトは限られた権限管理機能を提供しています。Vercel プロジェクトのコントロールパネルで、環境変数ページに `CODE` という名前の環境変数を追加し、値をカンマで区切ったカスタムパスワードに設定してください: - -``` -code1,code2,code3 -``` - -この環境変数を追加または変更した後、**プロジェクトを再デプロイ**して変更を有効にしてください。 - -## 環境変数 - -> 本プロジェクトのほとんどの設定は環境変数で行います。チュートリアル:[Vercel の環境変数を変更する方法](./docs/vercel-ja.md)。 - -### `OPENAI_API_KEY` (必須) - -OpenAI の API キー。OpenAI アカウントページで申請したキーをカンマで区切って複数設定できます。これにより、ランダムにキーが選択されます。 - -### `CODE` (オプション) - -アクセスパスワード。カンマで区切って複数設定可能。 - -**警告**:この項目を設定しないと、誰でもデプロイしたウェブサイトを利用でき、トークンが急速に消耗する可能性があるため、設定をお勧めします。 - -### `BASE_URL` (オプション) - -> デフォルト: `https://api.openai.com` - -> 例: `http://your-openai-proxy.com` - -OpenAI API のプロキシ URL。手動で OpenAI API のプロキシを設定している場合はこのオプションを設定してください。 - -> SSL 証明書の問題がある場合は、`BASE_URL` のプロトコルを http に設定してください。 - -### `OPENAI_ORG_ID` (オプション) - -OpenAI の組織 ID を指定します。 - -### `AZURE_URL` (オプション) - -> 形式: https://{azure-resource-url}/openai/deployments/{deploy-name} -> `CUSTOM_MODELS` で `displayName` 形式で {deploy-name} を設定した場合、`AZURE_URL` から {deploy-name} を省略できます。 - -Azure のデプロイ URL。 - -### `AZURE_API_KEY` (オプション) - -Azure の API キー。 - -### `AZURE_API_VERSION` (オプション) - -Azure API バージョン。[Azure ドキュメント](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions)で確認できます。 - -### `GOOGLE_API_KEY` (オプション) - -Google Gemini Pro API キー。 - -### `GOOGLE_URL` (オプション) - -Google Gemini Pro API の URL。 - -### `ANTHROPIC_API_KEY` (オプション) - -Anthropic Claude API キー。 - -### `ANTHROPIC_API_VERSION` (オプション) - -Anthropic Claude API バージョン。 - -### `ANTHROPIC_URL` (オプション) - -Anthropic Claude API の URL。 - -### `BAIDU_API_KEY` (オプション) - -Baidu API キー。 - -### `BAIDU_SECRET_KEY` (オプション) - -Baidu シークレットキー。 - -### `BAIDU_URL` (オプション) - -Baidu API の URL。 - -### `BYTEDANCE_API_KEY` (オプション) - -ByteDance API キー。 - -### `BYTEDANCE_URL` (オプション) - -ByteDance API の URL。 - -### `ALIBABA_API_KEY` (オプション) - -アリババ(千问)API キー。 - -### `ALIBABA_URL` (オプション) - -アリババ(千问)API の URL。 - -### `HIDE_USER_API_KEY` (オプション) - -ユーザーが API キーを入力できないようにしたい場合は、この環境変数を 1 に設定します。 - -### `DISABLE_GPT4` (オプション) - -ユーザーが GPT-4 を使用できないようにしたい場合は、この環境変数を 1 に設定します。 - -### `ENABLE_BALANCE_QUERY` (オプション) - -バランスクエリ機能を有効にしたい場合は、この環境変数を 1 に設定します。 - -### `DISABLE_FAST_LINK` (オプション) - -リンクからのプリセット設定解析を無効にしたい場合は、この環境変数を 1 に設定します。 - -### `WHITE_WEBDAV_ENDPOINTS` (オプション) - -アクセス許可を与える WebDAV サービスのアドレスを追加したい場合、このオプションを使用します。フォーマット要件: - -- 各アドレスは完全なエンドポイントでなければなりません。 - > `https://xxxx/xxx` -- 複数のアドレスは `,` で接続します。 - -### `CUSTOM_MODELS` (オプション) - -> 例:`+qwen-7b-chat,+glm-6b,-gpt-3.5-turbo,gpt-4-1106-preview=gpt-4-turbo` は `qwen-7b-chat` と `glm-6b` をモデルリストに追加し、`gpt-3.5-turbo` を削除し、`gpt-4-1106-preview` のモデル名を `gpt-4-turbo` として表示します。 -> すべてのモデルを無効にし、特定のモデルを有効にしたい場合は、`-all,+gpt-3.5-turbo` を使用します。これは `gpt-3.5-turbo` のみを有効にすることを意味します。 - -モデルリストを管理します。`+` でモデルを追加し、`-` でモデルを非表示にし、`モデル名=表示名` でモデルの表示名をカスタマイズし、カンマで区切ります。 - -Azure モードでは、`modelName@Azure=deploymentName` 形式でモデル名とデプロイ名(deploy-name)を設定できます。 - -> 例:`+gpt-3.5-turbo@Azure=gpt35` この設定でモデルリストに `gpt35(Azure)` のオプションが表示されます。 - -ByteDance モードでは、`modelName@bytedance=deploymentName` 形式でモデル名とデプロイ名(deploy-name)を設定できます。 - -> 例: `+Doubao-lite-4k@bytedance=ep-xxxxx-xxx` この設定でモデルリストに `Doubao-lite-4k(ByteDance)` のオプションが表示されます。 - -### `DEFAULT_MODEL` (オプション) - -デフォルトのモデルを変更します。 - -### `VISION_MODELS` (オプション) - -> デフォルト:空 -> 例:`gpt-4-vision,claude-3-opus,my-custom-model` は、これらのモデルにビジョン機能を追加します。これはデフォルトのパターンマッチング("vision"、"claude-3"、"gemini-1.5"などのキーワードを含むモデルを検出)に加えて適用されます。 - -デフォルトのパターンマッチングに加えて、追加のモデルにビジョン機能を付与します。複数のモデルはカンマで区切ります。 - -### `DEFAULT_INPUT_TEMPLATE` (オプション) - -『設定』の『ユーザー入力前処理』の初期設定に使用するテンプレートをカスタマイズします。 - -### `AI302_API_KEY` (オプション) - -302.AI API キー. - -### `AI302_URL` (オプション) - -302.AI API の URL. - -## 開発 - -下のボタンをクリックして二次開発を開始してください: - -[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web) - -コードを書く前に、プロジェクトのルートディレクトリに `.env.local` ファイルを新規作成し、環境変数を記入します: - -``` -OPENAI_API_KEY= -``` - -### ローカル開発 - -1. Node.js 18 と Yarn をインストールします。具体的な方法は ChatGPT にお尋ねください。 -2. `yarn install && yarn dev` を実行します。⚠️ 注意:このコマンドはローカル開発用であり、デプロイには使用しないでください。 -3. ローカルでデプロイしたい場合は、`yarn install && yarn build && yarn start` コマンドを使用してください。プロセスを守るために pm2 を使用することもできます。詳細は ChatGPT にお尋ねください。 - -## デプロイ - -### コンテナデプロイ(推奨) - -> Docker バージョンは 20 以上が必要です。それ以下だとイメージが見つからないというエラーが出ます。 - -> ⚠️ 注意:Docker バージョンは最新バージョンより 1~2 日遅れることが多いため、デプロイ後に「更新があります」の通知が出続けることがありますが、正常です。 - -```shell -docker pull yidadaa/chatgpt-next-web - -docker run -d -p 3000:3000 \ - -e OPENAI_API_KEY=sk-xxxx \ - -e CODE=ページアクセスパスワード \ - yidadaa/chatgpt-next-web -``` - -プロキシを指定することもできます: - -```shell -docker run -d -p 3000:3000 \ - -e OPENAI_API_KEY=sk-xxxx \ - -e CODE=ページアクセスパスワード \ - --net=host \ - -e PROXY_URL=http://127.0.0.1:7890 \ - yidadaa/chatgpt-next-web -``` - -ローカルプロキシがアカウントとパスワードを必要とする場合は、以下を使用できます: - -```shell --e PROXY_URL="http://127.0.0.1:7890 user password" -``` - -他の環境変数を指定する必要がある場合は、上記のコマンドに `-e 環境変数=環境変数値` を追加して指定してください。 - -### ローカルデプロイ - -コンソールで以下のコマンドを実行します: - -```shell -bash <(curl -s https://raw.githubusercontent.com/Yidadaa/ChatGPT-Next-Web/main/scripts/setup.sh) -``` - -⚠️ 注意:インストール中に問題が発生した場合は、Docker を使用してデプロイしてください。 - -## 謝辞 - -### 寄付者 - -> 英語版をご覧ください。 - -### 貢献者 - -[プロジェクトの貢献者リストはこちら](https://github.com/Yidadaa/ChatGPT-Next-Web/graphs/contributors) - -### 関連プロジェクト - -- [one-api](https://github.com/songquanpeng/one-api): 一つのプラットフォームで大規模モデルのクォータ管理を提供し、市場に出回っているすべての主要な大規模言語モデルをサポートします。 - -## オープンソースライセンス - -[MIT](https://opensource.org/license/mit/) diff --git a/README_KO.md b/README_KO.md deleted file mode 100644 index 40ba5a6a1c4..00000000000 --- a/README_KO.md +++ /dev/null @@ -1,492 +0,0 @@ -
- - - icon - - -

NextChat

- -영어 / [简体中文](./README_CN.md) - - - ChatGPTNextWeb%2FChatGPT-Next-Web | Trendshift - - -✨ 빠르고 가벼운 AI 어시스턴트, Claude, DeepSeek, GPT-4, Gemini Pro 지원 - -[![Saas][Saas-image]][saas-url] -[![Web][Web-image]][web-url] -[![Windows][Windows-image]][download-url] -[![MacOS][MacOS-image]][download-url] -[![Linux][Linux-image]][download-url] - -[NextChatAI 웹사이트](https://nextchat.club?utm_source=readme) / [iOS 앱](https://apps.apple.com/us/app/nextchat-ai/id6743085599) / [웹 데모](https://app.nextchat.club) / [데스크톱 앱](https://github.com/Yidadaa/ChatGPT-Next-Web/releases) / [엔터프라이즈 버전](#enterprise-edition) - -[saas-url]: https://nextchat.club?utm_source=readme -[saas-image]: https://img.shields.io/badge/NextChat-Saas-green?logo=microsoftedge -[web-url]: https://app.nextchat.club/ -[download-url]: https://github.com/Yidadaa/ChatGPT-Next-Web/releases -[Web-image]: https://img.shields.io/badge/Web-PWA-orange?logo=microsoftedge -[Windows-image]: https://img.shields.io/badge/-Windows-blue?logo=windows -[MacOS-image]: https://img.shields.io/badge/-MacOS-black?logo=apple -[Linux-image]: https://img.shields.io/badge/-Linux-333?logo=ubuntu - -[Deploy on Zeabur](https://zeabur.com/templates/ZBUEFA) [Deploy on Vercel](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat) [Open in Gitpod](https://gitpod.io/#https://github.com/ChatGPTNextWeb/NextChat) - -[](https://monica.im/?utm=nxcrp) - -
- -## ❤️ AI API 후원사 - - - icon - - -[302.AI](https://302.ai/)는 사용한 만큼만 비용을 지불하는 AI 애플리케이션 플랫폼으로, 다양한 AI API 및 온라인 애플리케이션을 제공합니다. - -## 🥳 NextChat iOS 버전 출시! - -> 👉 [지금 설치하기](https://apps.apple.com/us/app/nextchat-ai/id6743085599) - -> ❤️ [소스 코드 곧 공개 예정](https://github.com/ChatGPTNextWeb/NextChat-iOS) - -![Github iOS Image](https://github.com/user-attachments/assets/e0aa334f-4c13-4dc9-8310-e3b09fa4b9f3) - -## 🫣 NextChat, MCP 지원! - -> 빌드 전 환경 변수(env) `ENABLE_MCP=true` 설정 필요 - - - -## 엔터프라이즈 버전 - -회사 내부 시스템에 맞춘 프라이빗 배포 및 맞춤형 커스터마이징 지원: - -- **브랜드 커스터마이징**: 기업 이미지에 맞는 UI/UX 테마 적용 -- **리소스 통합 관리**: 다양한 AI 모델을 통합하여 팀원이 손쉽게 사용 가능 -- **권한 제어**: 관리자 패널을 통한 멤버·리소스·지식 베이스 권한 설정 -- **지식 통합**: 사내 문서 및 데이터와 AI를 결합한 맞춤형 답변 제공 -- **보안 감사**: 민감한 질문 차단 및 모든 기록 추적 가능 -- **프라이빗 배포 지원**: 주요 클라우드 서비스에 맞춘 배포 옵션 -- **지속적 업데이트**: 멀티모달 등 최신 AI 기능 지속 반영 - -엔터프라이즈 문의: **business@nextchat.dev** - -## 🖼️ 스크린샷 - -![설정](./docs/images/settings.png) -![기타](./docs/images/more.png) - -## 주요 기능 소개 - -- Vercel에서 원클릭 무료 배포 (1분 내 완성) -- 모든 OS(Linux/Windows/MacOS)에서 사용 가능한 클라이언트 (~5MB) [지금 다운 받기](https://github.com/Yidadaa/ChatGPT-Next-Web/releases) -- 자체 LLM 서버와 완벽 호환. [RWKV-Runner](https://github.com/josStorer/RWKV-Runner) 또는 [LocalAI](https://github.com/go-skynet/LocalAI)와 함께 사용하는 것을 추천 -- 개인 정보 보호: 모든 대화 기록은 브라우저에만 저장 -- Markdown 지원: LaTex, Mermaid, 코드 하이라이팅 등 -- 반응형 디자인, 다크 모드, PWA 지원 -- 빠른 초기 로딩 속도 (~100kb), 스트리밍 응답 -- 프롬프트 템플릿 생성/공유/디버깅 지원 (v2) -- v2: 프롬프트 템플릿 기반 도구 생성, 공유, 디버깅 가능 -- 고급 프롬프트 내장 [awesome-chatgpt-prompts-zh](https://github.com/PlexPt/awesome-chatgpt-prompts-zh) and [awesome-chatgpt-prompts](https://github.com/f/awesome-chatgpt-prompts) -- 긴 대화 내용 자동 압축 저장으로 토큰 절약 -- I18n: English, 简体中文, 繁体中文, 日本語, Français, Español, Italiano, Türkçe, Deutsch, Tiếng Việt, Русский, Čeština, 한국어, Indonesia - -
- -![主界面](./docs/images/cover.png) - -
- -## 개발 로드맵 - -- [x] 시스템 프롬프트: 사용자가 정의한 프롬프트를 시스템 프롬프트로 고정하기 [#138](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/138) -- [x] 사용자 프롬프트: 사용자 정의 프롬프트를 편집 및 저장하여 리스트로 관리 가능 -- [x] 프롬프트 템플릿: 사전 정의된 인컨텍스트 프롬프트로 새 채팅 생성 [#993](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/993) -- [x] 이미지로 공유하거나 ShareGPT로 공유 [#1741](https://github.com/Yidadaa/ChatGPT-Next-Web/pull/1741) -- [x] Tauri 기반 데스크톱 앱 -- [x] 자체 모델 호스팅: [RWKV-Runner](https://github.com/josStorer/RWKV-Runner), [LocalAI](https://github.com/go-skynet/LocalAI) 등 서버 배포 모델들과 완벽 호환 (llama, gpt4all, rwkv, vicuna, koala, gpt4all-j, cerebras, falcon, dolly 등) -- [x] 아티팩트: 생성된 콘텐츠 및 웹페이지를 별도 창으로 미리보기, 복사, 공유 가능 [#5092](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/pull/5092) -- [x] 플러그인: 웹 검색, 계산기, 기타 외부 API 기능 지원 [#165](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/165) [#5353](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5353) -- [x] 실시간 채팅 지원 [#5672](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5672) -- [ ] 로컬 지식 베이스 지원 예정 - -## 🚀 최근 업데이트 - -- 🚀 v2.15.8 실시간 채팅 지원 [#5672](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5672) -- 🚀 v2.15.4 Tauri 기반 LLM API 호출 기능 추가 → 보안 강화 [#5379](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5379) -- 🚀 v2.15.0 플러그인 기능 추가 → [NextChat-Awesome-Plugins](https://github.com/ChatGPTNextWeb/NextChat-Awesome-Plugins) -- 🚀 v2.14.0 아티팩트 및 Stable Diffusion 기능 추가 -- 🚀 v2.10.1 Google Gemini Pro 모델 지원 -- 🚀 v2.9.11 Azure Endpoint 사용 가능 -- 🚀 v2.8 모든 플랫폼에서 실행 가능한 클라이언트 출시 -- 🚀 v2.7 대화 내용을 이미지로, 또는 ShareGPT로 공유 가능 -- 🚀 v2.0 릴리즈: 프롬프트 템플릿 생성 및 아이디어 구현 가능! → [ChatGPT Prompt Engineering Tips](https://www.allabtai.com/prompt-engineering-tips-zero-one-and-few-shot-prompting/) - -## 시작하기 - -1. [OpenAI API 키](https://platform.openai.com/account/api-keys)를 발급받습니다. -2. - [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FYidadaa%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=chatgpt-next-web&repository-name=ChatGPT-Next-Web) 버튼을 클릭해 Vercel에 배포합니다. `CODE`는 페이지 비밀번호라는 점을 기억하세요. - -3. Enjoy :) - -## FAQ - -[FAQ](./docs/faq-ko.md) - -## 최신 상태 유지 (Keep Updated) - -Vercel로 배포한 경우, "Updates Available" 메시지가 계속 나타날 수 있습니다. 이는 프로젝트를 포크하지 않고 새로 생성했기 때문입니다. - -다음 절차에 따라 다시 배포를 권장합니다: - -1. 기존 레포 삭제 -2. 우측 상단 "Fork" 버튼 클릭 → 포크 생성 -3. 포크된 프로젝트를 다시 Vercel에 배포 - → [자세한 튜토리얼 보기](./docs/vercel-ko.md) - -### 자동 업데이트 활성화 (Enable Automatic Updates) - -> Upstream Sync 오류 발생 시, [수동으로 코드 업데이트](./README_KO.md#manually-updating-code)하세요. - -프로젝트 포크 후에는 GitHub의 제약으로 인해 Actions 페이지에서 아래 항목들을 수동으로 활성화해야 합니다: - -- `Workflows` -- `Upstream Sync Action` - -이후 매 시간 자동으로 업데이트됩니다: - -![자동 업데이트 활성화](./docs/images/enable-actions.jpg) -![업스트림 동기화 활성화](./docs/images/enable-actions-sync.jpg) - -### 수동 업데이트 방법 (Manually Updating Code) - -즉시 업데이트가 필요한 경우, [깃헙 문서](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork)를 참고해 포크된 프로젝트를 upstream code와 동기화하세요. - -릴리스 알림을 원하시면 star 또는 watch를 눌러주세요. - -## 접근 비밀번호 설정 (Access Password) - -이 프로젝트는 제한된 접근 제어를 제공합니다. -Vercel 환경 변수에 `CODE`를 다음 형식으로 추가하세요. value는 ,를 통해 구분된 비밀번호여야 합니다.: - -``` -code1,code2,code3 -``` - -수정 후 반드시 다시 배포해야 적용됩니다. - -## 환경 변수 (Environment Variables) - -### `CODE` (선택 사항) - -접속 비밀번호. 쉼표로 구분합니다. - -### `OPENAI_API_KEY` (필수) - -당신의 OpenAI API 키, 여러 개를 사용하려면 쉼표로 연결합니다. - -### `BASE_URL` (선택 사항) - -> 기본값: `https://api.openai.com` - -> 예시: `http://your-openai-proxy.com` - -OpenAI API 요청의 기본 URL을 재정의합니다. - -### `OPENAI_ORG_ID` (선택 사항) - -OpenAI organization ID를 지정합니다. - -### `AZURE_URL` (선택 사항) - -> 예시: https://{azure-resource-url}/openai - -Azure 배포 URL입니다. - -### `AZURE_API_KEY` (선택 사항) - -Azure API 키입니다. - -### `AZURE_API_VERSION` (선택 사항) - -Azure API 버전입니다. [Azure 문서](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions)에서 확인할 수 있습니다. - -### `GOOGLE_API_KEY` (선택 사항) - -Google Gemini Pro API 키입니다. - -### `GOOGLE_URL` (선택 사항) - -Google Gemini Pro API URL입니다. - -### `ANTHROPIC_API_KEY` (선택 사항) - -Anthropic Claude API 키입니다. - -### `ANTHROPIC_API_VERSION` (선택 사항) - -Anthropic Claude API 버전입니다. - -### `ANTHROPIC_URL` (선택 사항) - -Anthropic Claude API URL입니다. - -### `BAIDU_API_KEY` (선택 사항) - -Baidu API 키입니다. - -### `BAIDU_SECRET_KEY` (선택 사항) - -Baidu Secret 키입니다. - -### `BAIDU_URL` (선택 사항) - -Baidu API URL입니다. - -### `BYTEDANCE_API_KEY` (선택 사항) - -ByteDance API 키입니다. - -### `BYTEDANCE_URL` (선택 사항) - -ByteDance API URL입니다. - -### `ALIBABA_API_KEY` (선택 사항) - -Alibaba Cloud API 키입니다. - -### `ALIBABA_URL` (선택 사항) - -Alibaba Cloud API URL입니다. - -### `IFLYTEK_URL` (선택 사항) - -iflytek API URL입니다. - -### `IFLYTEK_API_KEY` (선택 사항) - -iflytek API 키입니다. - -### `IFLYTEK_API_SECRET` (선택 사항) - -iflytek API 시크릿입니다. - -### `CHATGLM_API_KEY` (선택 사항) - -ChatGLM API 키입니다. - -### `CHATGLM_URL` (선택 사항) - -ChatGLM API URL입니다. - -### `DEEPSEEK_API_KEY` (선택 사항) - -DeepSeek API 키입니다. - -### `DEEPSEEK_URL` (선택 사항) - -DeepSeek API URL입니다. - -### `HIDE_USER_API_KEY` (선택 사항) - -> 기본값: 비어 있음 - -사용자가 자신의 API 키를 입력하지 못하게 하려면 이 값을 1로 설정하세요. - -### `DISABLE_GPT4` (선택 사항) - -> 기본값: 비어 있음 - -사용자가 GPT-4를 사용하지 못하게 하려면 이 값을 1로 설정하세요. - -### `ENABLE_BALANCE_QUERY` (선택 사항) - -> 기본값: 비어 있음 - -사용자가 쿼리 잔액을 조회할 수 있도록 하려면 이 값을 1로 설정하세요. - -### `DISABLE_FAST_LINK` (선택 사항) - -> 기본값: 비어 있음 - -URL에서 설정을 파싱하는 기능을 비활성화하려면 이 값을 1로 설정하세요. - -### `CUSTOM_MODELS` (선택 사항) - -> 기본값: 비어 있음 -> 예시: `+llama,+claude-2,-gpt-3.5-turbo,gpt-4-1106-preview=gpt-4-turbo` -이는 `llama`, `claude-2`를 모델 리스트에 추가하고, `gpt-3.5-turbo`를 제거하며, `gpt-4-1106-preview`를 `gpt-4-turbo`로 표시합니다. - -사용자 지정 모델 제어 시 `+`는 추가, `-`는 제거, `이름=표시이름`은 모델명 커스터마이징을 의미합니다. 쉼표로 구분하세요. - -- `-all`은 기본 모델을 모두 비활성화 -- `+all`은 기본 모델을 모두 활성화 - -Azure 용법 예시: `modelName@Azure=deploymentName` → 배포 이름을 커스터마이징 가능 -> 예시: `+gpt-3.5-turbo@Azure=gpt35` → 리스트에 `gpt35(Azure)` 표시됨 -> Azure 모델만 사용할 경우: `-all,+gpt-3.5-turbo@Azure=gpt35` - -ByteDance 용법 예시: `modelName@bytedance=deploymentName` -> 예시: `+Doubao-lite-4k@bytedance=ep-xxxxx-xxx` → `Doubao-lite-4k(ByteDance)`로 표시됨 - -### `DEFAULT_MODEL` (선택 사항) - -기본 모델을 변경합니다. - -### `VISION_MODELS` (선택 사항) - -> 기본값: 비어 있음 -> 예시: `gpt-4-vision,claude-3-opus,my-custom-model` -위의 모델들에 시각 기능을 부여합니다 (기본적으로 `"vision"`, `"claude-3"`, `"gemini-1.5"` 키워드를 포함한 모델은 자동 인식됨). 기본 모델 외에도 모델을 추가할 수 있습니다. 쉼표로 구분하세요. - -### `WHITE_WEBDAV_ENDPOINTS` (선택 사항) - -접속 허용할 WebDAV 서비스 주소를 늘리고자 할 때 사용합니다. - -- 각 주소는 완전한 endpoint 여야 함: `https://xxxx/yyy` -- 여러 주소는 `,`로 구분 - -### `DEFAULT_INPUT_TEMPLATE` (선택 사항) - -설정 메뉴의 사용자 입력 전처리 구성 항목 초기화 시 사용할 기본 템플릿을 설정합니다. - -### `STABILITY_API_KEY` (선택 사항) - -Stability API 키입니다. - -### `STABILITY_URL` (선택 사항) - -Stability API URL을 커스터마이징합니다. - -### `ENABLE_MCP` (선택 사항) - -MCP (Model Context Protocol) 기능을 활성화합니다. - -### `SILICONFLOW_API_KEY` (선택 사항) - -SiliconFlow API 키입니다. - -### `SILICONFLOW_URL` (선택 사항) - -SiliconFlow API URL입니다. - -### `AI302_API_KEY` (선택 사항) - -302.AI API 키입니다. - -### `AI302_URL` (선택 사항) - -302.AI API URL입니다. - -## 요구 사항 (Requirements) - -NodeJS >= 18, Docker >= 20 - -## 개발 (Development) - -[![Gitpod에서 열기](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web) - -개발을 시작하기 전에 프로젝트 루트에 `.env.local` 파일을 만들고, 아래와 같이 API 키를 입력하세요: - -``` -OPENAI_API_KEY=<여기에 API 키 입력> - -# OpenAI 서비스를 사용할 수 없는 경우 아래 BASE_URL 사용 -BASE_URL=https://chatgpt1.nextweb.fun/api/proxy -``` - -### 로컬 개발 실행 - -```shell -# 1. Node.js와 Yarn을 먼저 설치 -# 2. `.env.local` 파일에 환경 변수 설정 -# 3. 실행 -yarn install -yarn dev -``` - -## 배포 (Deployment) - -### Docker (권장) - -```shell -docker pull yidadaa/chatgpt-next-web - -docker run -d -p 3000:3000 \ - -e OPENAI_API_KEY=sk-xxxx \ - -e CODE=your-password \ - yidadaa/chatgpt-next-web -``` - -서비스에 프록시를 사용하려면: - -```shell -docker run -d -p 3000:3000 \ - -e OPENAI_API_KEY=sk-xxxx \ - -e CODE=your-password \ - -e PROXY_URL=http://localhost:7890 \ - yidadaa/chatgpt-next-web -``` - -프록시에 인증이 필요한 경우: - -```shell --e PROXY_URL="http://127.0.0.1:7890 user pass" -``` - -MCP를 활성화하려면: - -``` -docker run -d -p 3000:3000 \ - -e OPENAI_API_KEY=sk-xxxx \ - -e CODE=your-password \ - -e ENABLE_MCP=true \ - yidadaa/chatgpt-next-web -``` - -### 로컬 배포 - -콘솔에서 다음 명령을 실행하세요. - -```shell -bash <(curl -s https://raw.githubusercontent.com/Yidadaa/ChatGPT-Next-Web/main/scripts/setup.sh) -``` - -⚠️ 참고: 설치 중에 문제가 발생하면 Docker 배포를 사용하세요. - -## 채팅 기록 동기화 (UpStash) - -| [简体中文](./docs/synchronise-chat-logs-cn.md) | [English](./docs/synchronise-chat-logs-en.md) | [Italiano](./docs/synchronise-chat-logs-es.md) | [日本語](./docs/synchronise-chat-logs-ja.md) | [한국어](./docs/synchronise-chat-logs-ko.md) - -## 문서 (Documentation) - -> 더 많은 문서는 [docs](./docs) 디렉토리를 참고하세요. - -- [Cloudflare 배포 가이드 (폐기됨)](./docs/cloudflare-pages-ko.md) -- [자주 묻는 질문](./docs/faq-ko.md) -- [새 번역 추가 방법](./docs/translation.md) -- [Vercel 사용법 (중문)](./docs/vercel-cn.md) -- [사용자 매뉴얼 (중문, 작성 중)](./docs/user-manual-cn.md) - -## 번역 (Translation) - -새로운 번역을 추가하고 싶다면, [이 문서](./docs/translation.md)를 읽어보세요. - -## 후원 (Donation) - -[Buy Me a Coffee](https://www.buymeacoffee.com/yidadaa) - -## 특별 감사 (Special Thanks) - -### 기여자 (Contributors) - - - - - -## 라이선스 (LICENSE) - -[MIT](https://opensource.org/license/mit/) diff --git a/app/api/302ai.ts b/app/api/302ai.ts deleted file mode 100644 index b4636bcd7be..00000000000 --- a/app/api/302ai.ts +++ /dev/null @@ -1,128 +0,0 @@ -import { getServerSideConfig } from "@/app/config/server"; -import { - AI302_BASE_URL, - ApiPath, - ModelProvider, - ServiceProvider, -} from "@/app/constant"; -import { prettyObject } from "@/app/utils/format"; -import { NextRequest, NextResponse } from "next/server"; -import { auth } from "@/app/api/auth"; -import { isModelNotavailableInServer } from "@/app/utils/model"; - -const serverConfig = getServerSideConfig(); - -export async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[302.AI Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const authResult = auth(req, ModelProvider["302.AI"]); - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - try { - const response = await request(req); - return response; - } catch (e) { - console.error("[302.AI] ", e); - return NextResponse.json(prettyObject(e)); - } -} - -async function request(req: NextRequest) { - const controller = new AbortController(); - - // alibaba use base url or just remove the path - let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath["302.AI"], ""); - - let baseUrl = serverConfig.ai302Url || AI302_BASE_URL; - - if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, -1); - } - - console.log("[Proxy] ", path); - console.log("[Base Url]", baseUrl); - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - - const fetchUrl = `${baseUrl}${path}`; - const fetchOptions: RequestInit = { - headers: { - "Content-Type": "application/json", - Authorization: req.headers.get("Authorization") ?? "", - }, - method: req.method, - body: req.body, - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - // #1815 try to refuse some request to some models - if (serverConfig.customModels && req.body) { - try { - const clonedBody = await req.text(); - fetchOptions.body = clonedBody; - - const jsonBody = JSON.parse(clonedBody) as { model?: string }; - - // not undefined and is false - if ( - isModelNotavailableInServer( - serverConfig.customModels, - jsonBody?.model as string, - ServiceProvider["302.AI"] as string, - ) - ) { - return NextResponse.json( - { - error: true, - message: `you are not allowed to use ${jsonBody?.model} model`, - }, - { - status: 403, - }, - ); - } - } catch (e) { - console.error(`[302.AI] filter`, e); - } - } - try { - const res = await fetch(fetchUrl, fetchOptions); - - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/api/[provider]/[...path]/route.ts b/app/api/[provider]/[...path]/route.ts deleted file mode 100644 index e8af34f29f8..00000000000 --- a/app/api/[provider]/[...path]/route.ts +++ /dev/null @@ -1,85 +0,0 @@ -import { ApiPath } from "@/app/constant"; -import { NextRequest } from "next/server"; -import { handle as openaiHandler } from "../../openai"; -import { handle as azureHandler } from "../../azure"; -import { handle as googleHandler } from "../../google"; -import { handle as anthropicHandler } from "../../anthropic"; -import { handle as baiduHandler } from "../../baidu"; -import { handle as bytedanceHandler } from "../../bytedance"; -import { handle as alibabaHandler } from "../../alibaba"; -import { handle as moonshotHandler } from "../../moonshot"; -import { handle as stabilityHandler } from "../../stability"; -import { handle as iflytekHandler } from "../../iflytek"; -import { handle as deepseekHandler } from "../../deepseek"; -import { handle as siliconflowHandler } from "../../siliconflow"; -import { handle as xaiHandler } from "../../xai"; -import { handle as chatglmHandler } from "../../glm"; -import { handle as proxyHandler } from "../../proxy"; -import { handle as ai302Handler } from "../../302ai"; - -async function handle( - req: NextRequest, - { params }: { params: { provider: string; path: string[] } }, -) { - const apiPath = `/api/${params.provider}`; - console.log(`[${params.provider} Route] params `, params); - switch (apiPath) { - case ApiPath.Azure: - return azureHandler(req, { params }); - case ApiPath.Google: - return googleHandler(req, { params }); - case ApiPath.Anthropic: - return anthropicHandler(req, { params }); - case ApiPath.Baidu: - return baiduHandler(req, { params }); - case ApiPath.ByteDance: - return bytedanceHandler(req, { params }); - case ApiPath.Alibaba: - return alibabaHandler(req, { params }); - // case ApiPath.Tencent: using "/api/tencent" - case ApiPath.Moonshot: - return moonshotHandler(req, { params }); - case ApiPath.Stability: - return stabilityHandler(req, { params }); - case ApiPath.Iflytek: - return iflytekHandler(req, { params }); - case ApiPath.DeepSeek: - return deepseekHandler(req, { params }); - case ApiPath.XAI: - return xaiHandler(req, { params }); - case ApiPath.ChatGLM: - return chatglmHandler(req, { params }); - case ApiPath.SiliconFlow: - return siliconflowHandler(req, { params }); - case ApiPath.OpenAI: - return openaiHandler(req, { params }); - case ApiPath["302.AI"]: - return ai302Handler(req, { params }); - default: - return proxyHandler(req, { params }); - } -} - -export const GET = handle; -export const POST = handle; - -export const runtime = "edge"; -export const preferredRegion = [ - "arn1", - "bom1", - "cdg1", - "cle1", - "cpt1", - "dub1", - "fra1", - "gru1", - "hnd1", - "iad1", - "icn1", - "kix1", - "lhr1", - "pdx1", - "sfo1", - "sin1", - "syd1", -]; diff --git a/app/api/alibaba.ts b/app/api/alibaba.ts deleted file mode 100644 index 20f6caefa8d..00000000000 --- a/app/api/alibaba.ts +++ /dev/null @@ -1,129 +0,0 @@ -import { getServerSideConfig } from "@/app/config/server"; -import { - ALIBABA_BASE_URL, - ApiPath, - ModelProvider, - ServiceProvider, -} from "@/app/constant"; -import { prettyObject } from "@/app/utils/format"; -import { NextRequest, NextResponse } from "next/server"; -import { auth } from "@/app/api/auth"; -import { isModelNotavailableInServer } from "@/app/utils/model"; - -const serverConfig = getServerSideConfig(); - -export async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[Alibaba Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const authResult = auth(req, ModelProvider.Qwen); - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - try { - const response = await request(req); - return response; - } catch (e) { - console.error("[Alibaba] ", e); - return NextResponse.json(prettyObject(e)); - } -} - -async function request(req: NextRequest) { - const controller = new AbortController(); - - // alibaba use base url or just remove the path - let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Alibaba, ""); - - let baseUrl = serverConfig.alibabaUrl || ALIBABA_BASE_URL; - - if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, -1); - } - - console.log("[Proxy] ", path); - console.log("[Base Url]", baseUrl); - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - - const fetchUrl = `${baseUrl}${path}`; - const fetchOptions: RequestInit = { - headers: { - "Content-Type": "application/json", - Authorization: req.headers.get("Authorization") ?? "", - "X-DashScope-SSE": req.headers.get("X-DashScope-SSE") ?? "disable", - }, - method: req.method, - body: req.body, - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - // #1815 try to refuse some request to some models - if (serverConfig.customModels && req.body) { - try { - const clonedBody = await req.text(); - fetchOptions.body = clonedBody; - - const jsonBody = JSON.parse(clonedBody) as { model?: string }; - - // not undefined and is false - if ( - isModelNotavailableInServer( - serverConfig.customModels, - jsonBody?.model as string, - ServiceProvider.Alibaba as string, - ) - ) { - return NextResponse.json( - { - error: true, - message: `you are not allowed to use ${jsonBody?.model} model`, - }, - { - status: 403, - }, - ); - } - } catch (e) { - console.error(`[Alibaba] filter`, e); - } - } - try { - const res = await fetch(fetchUrl, fetchOptions); - - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/api/anthropic.ts b/app/api/anthropic.ts deleted file mode 100644 index b96637b2c8c..00000000000 --- a/app/api/anthropic.ts +++ /dev/null @@ -1,170 +0,0 @@ -import { getServerSideConfig } from "@/app/config/server"; -import { - ANTHROPIC_BASE_URL, - Anthropic, - ApiPath, - ServiceProvider, - ModelProvider, -} from "@/app/constant"; -import { prettyObject } from "@/app/utils/format"; -import { NextRequest, NextResponse } from "next/server"; -import { auth } from "./auth"; -import { isModelNotavailableInServer } from "@/app/utils/model"; -import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare"; - -const ALLOWD_PATH = new Set([Anthropic.ChatPath, Anthropic.ChatPath1]); - -export async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[Anthropic Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const subpath = params.path.join("/"); - - if (!ALLOWD_PATH.has(subpath)) { - console.log("[Anthropic Route] forbidden path ", subpath); - return NextResponse.json( - { - error: true, - msg: "you are not allowed to request " + subpath, - }, - { - status: 403, - }, - ); - } - - const authResult = auth(req, ModelProvider.Claude); - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - try { - const response = await request(req); - return response; - } catch (e) { - console.error("[Anthropic] ", e); - return NextResponse.json(prettyObject(e)); - } -} - -const serverConfig = getServerSideConfig(); - -async function request(req: NextRequest) { - const controller = new AbortController(); - - let authHeaderName = "x-api-key"; - let authValue = - req.headers.get(authHeaderName) || - req.headers.get("Authorization")?.replaceAll("Bearer ", "").trim() || - serverConfig.anthropicApiKey || - ""; - - let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Anthropic, ""); - - let baseUrl = - serverConfig.anthropicUrl || serverConfig.baseUrl || ANTHROPIC_BASE_URL; - - if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, -1); - } - - console.log("[Proxy] ", path); - console.log("[Base Url]", baseUrl); - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - - // try rebuild url, when using cloudflare ai gateway in server - const fetchUrl = cloudflareAIGatewayUrl(`${baseUrl}${path}`); - - const fetchOptions: RequestInit = { - headers: { - "Content-Type": "application/json", - "Cache-Control": "no-store", - "anthropic-dangerous-direct-browser-access": "true", - [authHeaderName]: authValue, - "anthropic-version": - req.headers.get("anthropic-version") || - serverConfig.anthropicApiVersion || - Anthropic.Vision, - }, - method: req.method, - body: req.body, - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - // #1815 try to refuse some request to some models - if (serverConfig.customModels && req.body) { - try { - const clonedBody = await req.text(); - fetchOptions.body = clonedBody; - - const jsonBody = JSON.parse(clonedBody) as { model?: string }; - - // not undefined and is false - if ( - isModelNotavailableInServer( - serverConfig.customModels, - jsonBody?.model as string, - ServiceProvider.Anthropic as string, - ) - ) { - return NextResponse.json( - { - error: true, - message: `you are not allowed to use ${jsonBody?.model} model`, - }, - { - status: 403, - }, - ); - } - } catch (e) { - console.error(`[Anthropic] filter`, e); - } - } - // console.log("[Anthropic request]", fetchOptions.headers, req.method); - try { - const res = await fetch(fetchUrl, fetchOptions); - - // console.log( - // "[Anthropic response]", - // res.status, - // " ", - // res.headers, - // res.url, - // ); - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/api/artifacts/route.ts b/app/api/artifacts/route.ts deleted file mode 100644 index 4707e795f0a..00000000000 --- a/app/api/artifacts/route.ts +++ /dev/null @@ -1,73 +0,0 @@ -import md5 from "spark-md5"; -import { NextRequest, NextResponse } from "next/server"; -import { getServerSideConfig } from "@/app/config/server"; - -async function handle(req: NextRequest, res: NextResponse) { - const serverConfig = getServerSideConfig(); - const storeUrl = () => - `https://api.cloudflare.com/client/v4/accounts/${serverConfig.cloudflareAccountId}/storage/kv/namespaces/${serverConfig.cloudflareKVNamespaceId}`; - const storeHeaders = () => ({ - Authorization: `Bearer ${serverConfig.cloudflareKVApiKey}`, - }); - if (req.method === "POST") { - const clonedBody = await req.text(); - const hashedCode = md5.hash(clonedBody).trim(); - const body: { - key: string; - value: string; - expiration_ttl?: number; - } = { - key: hashedCode, - value: clonedBody, - }; - try { - const ttl = parseInt(serverConfig.cloudflareKVTTL as string); - if (ttl > 60) { - body["expiration_ttl"] = ttl; - } - } catch (e) { - console.error(e); - } - const res = await fetch(`${storeUrl()}/bulk`, { - headers: { - ...storeHeaders(), - "Content-Type": "application/json", - }, - method: "PUT", - body: JSON.stringify([body]), - }); - const result = await res.json(); - console.log("save data", result); - if (result?.success) { - return NextResponse.json( - { code: 0, id: hashedCode, result }, - { status: res.status }, - ); - } - return NextResponse.json( - { error: true, msg: "Save data error" }, - { status: 400 }, - ); - } - if (req.method === "GET") { - const id = req?.nextUrl?.searchParams?.get("id"); - const res = await fetch(`${storeUrl()}/values/${id}`, { - headers: storeHeaders(), - method: "GET", - }); - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: res.headers, - }); - } - return NextResponse.json( - { error: true, msg: "Invalid request" }, - { status: 400 }, - ); -} - -export const POST = handle; -export const GET = handle; - -export const runtime = "edge"; diff --git a/app/api/auth.ts b/app/api/auth.ts deleted file mode 100644 index 8c78c70c865..00000000000 --- a/app/api/auth.ts +++ /dev/null @@ -1,129 +0,0 @@ -import { NextRequest } from "next/server"; -import { getServerSideConfig } from "../config/server"; -import md5 from "spark-md5"; -import { ACCESS_CODE_PREFIX, ModelProvider } from "../constant"; - -function getIP(req: NextRequest) { - let ip = req.ip ?? req.headers.get("x-real-ip"); - const forwardedFor = req.headers.get("x-forwarded-for"); - - if (!ip && forwardedFor) { - ip = forwardedFor.split(",").at(0) ?? ""; - } - - return ip; -} - -function parseApiKey(bearToken: string) { - const token = bearToken.trim().replaceAll("Bearer ", "").trim(); - const isApiKey = !token.startsWith(ACCESS_CODE_PREFIX); - - return { - accessCode: isApiKey ? "" : token.slice(ACCESS_CODE_PREFIX.length), - apiKey: isApiKey ? token : "", - }; -} - -export function auth(req: NextRequest, modelProvider: ModelProvider) { - const authToken = req.headers.get("Authorization") ?? ""; - - // check if it is openai api key or user token - const { accessCode, apiKey } = parseApiKey(authToken); - - const hashedCode = md5.hash(accessCode ?? "").trim(); - - const serverConfig = getServerSideConfig(); - console.log("[Auth] allowed hashed codes: ", [...serverConfig.codes]); - console.log("[Auth] got access code:", accessCode); - console.log("[Auth] hashed access code:", hashedCode); - console.log("[User IP] ", getIP(req)); - console.log("[Time] ", new Date().toLocaleString()); - - if (serverConfig.needCode && !serverConfig.codes.has(hashedCode) && !apiKey) { - return { - error: true, - msg: !accessCode ? "empty access code" : "wrong access code", - }; - } - - if (serverConfig.hideUserApiKey && !!apiKey) { - return { - error: true, - msg: "you are not allowed to access with your own api key", - }; - } - - // if user does not provide an api key, inject system api key - if (!apiKey) { - const serverConfig = getServerSideConfig(); - - // const systemApiKey = - // modelProvider === ModelProvider.GeminiPro - // ? serverConfig.googleApiKey - // : serverConfig.isAzure - // ? serverConfig.azureApiKey - // : serverConfig.apiKey; - - let systemApiKey: string | undefined; - - switch (modelProvider) { - case ModelProvider.Stability: - systemApiKey = serverConfig.stabilityApiKey; - break; - case ModelProvider.GeminiPro: - systemApiKey = serverConfig.googleApiKey; - break; - case ModelProvider.Claude: - systemApiKey = serverConfig.anthropicApiKey; - break; - case ModelProvider.Doubao: - systemApiKey = serverConfig.bytedanceApiKey; - break; - case ModelProvider.Ernie: - systemApiKey = serverConfig.baiduApiKey; - break; - case ModelProvider.Qwen: - systemApiKey = serverConfig.alibabaApiKey; - break; - case ModelProvider.Moonshot: - systemApiKey = serverConfig.moonshotApiKey; - break; - case ModelProvider.Iflytek: - systemApiKey = - serverConfig.iflytekApiKey + ":" + serverConfig.iflytekApiSecret; - break; - case ModelProvider.DeepSeek: - systemApiKey = serverConfig.deepseekApiKey; - break; - case ModelProvider.XAI: - systemApiKey = serverConfig.xaiApiKey; - break; - case ModelProvider.ChatGLM: - systemApiKey = serverConfig.chatglmApiKey; - break; - case ModelProvider.SiliconFlow: - systemApiKey = serverConfig.siliconFlowApiKey; - break; - case ModelProvider.GPT: - default: - if (req.nextUrl.pathname.includes("azure/deployments")) { - systemApiKey = serverConfig.azureApiKey; - } else { - systemApiKey = serverConfig.apiKey; - } - } - - if (systemApiKey) { - console.log("[Auth] use system api key"); - req.headers.set("Authorization", `Bearer ${systemApiKey}`); - } else { - console.log("[Auth] admin did not provide an api key"); - } - } else { - console.log("[Auth] use user api key"); - } - - return { - error: false, - }; -} diff --git a/app/api/azure.ts b/app/api/azure.ts deleted file mode 100644 index 39d872e8cf8..00000000000 --- a/app/api/azure.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { ModelProvider } from "@/app/constant"; -import { prettyObject } from "@/app/utils/format"; -import { NextRequest, NextResponse } from "next/server"; -import { auth } from "./auth"; -import { requestOpenai } from "./common"; - -export async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[Azure Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const subpath = params.path.join("/"); - - const authResult = auth(req, ModelProvider.GPT); - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - try { - return await requestOpenai(req); - } catch (e) { - console.error("[Azure] ", e); - return NextResponse.json(prettyObject(e)); - } -} diff --git a/app/api/baidu.ts b/app/api/baidu.ts deleted file mode 100644 index 0f4e05ee86c..00000000000 --- a/app/api/baidu.ts +++ /dev/null @@ -1,144 +0,0 @@ -import { getServerSideConfig } from "@/app/config/server"; -import { - BAIDU_BASE_URL, - ApiPath, - ModelProvider, - ServiceProvider, -} from "@/app/constant"; -import { prettyObject } from "@/app/utils/format"; -import { NextRequest, NextResponse } from "next/server"; -import { auth } from "@/app/api/auth"; -import { isModelNotavailableInServer } from "@/app/utils/model"; -import { getAccessToken } from "@/app/utils/baidu"; - -const serverConfig = getServerSideConfig(); - -export async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[Baidu Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const authResult = auth(req, ModelProvider.Ernie); - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - if (!serverConfig.baiduApiKey || !serverConfig.baiduSecretKey) { - return NextResponse.json( - { - error: true, - message: `missing BAIDU_API_KEY or BAIDU_SECRET_KEY in server env vars`, - }, - { - status: 401, - }, - ); - } - - try { - const response = await request(req); - return response; - } catch (e) { - console.error("[Baidu] ", e); - return NextResponse.json(prettyObject(e)); - } -} - -async function request(req: NextRequest) { - const controller = new AbortController(); - - let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Baidu, ""); - - let baseUrl = serverConfig.baiduUrl || BAIDU_BASE_URL; - - if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, -1); - } - - console.log("[Proxy] ", path); - console.log("[Base Url]", baseUrl); - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - - const { access_token } = await getAccessToken( - serverConfig.baiduApiKey as string, - serverConfig.baiduSecretKey as string, - ); - const fetchUrl = `${baseUrl}${path}?access_token=${access_token}`; - - const fetchOptions: RequestInit = { - headers: { - "Content-Type": "application/json", - }, - method: req.method, - body: req.body, - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - // #1815 try to refuse some request to some models - if (serverConfig.customModels && req.body) { - try { - const clonedBody = await req.text(); - fetchOptions.body = clonedBody; - - const jsonBody = JSON.parse(clonedBody) as { model?: string }; - - // not undefined and is false - if ( - isModelNotavailableInServer( - serverConfig.customModels, - jsonBody?.model as string, - ServiceProvider.Baidu as string, - ) - ) { - return NextResponse.json( - { - error: true, - message: `you are not allowed to use ${jsonBody?.model} model`, - }, - { - status: 403, - }, - ); - } - } catch (e) { - console.error(`[Baidu] filter`, e); - } - } - try { - const res = await fetch(fetchUrl, fetchOptions); - - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/api/bytedance.ts b/app/api/bytedance.ts deleted file mode 100644 index 51b39ceb7cb..00000000000 --- a/app/api/bytedance.ts +++ /dev/null @@ -1,129 +0,0 @@ -import { getServerSideConfig } from "@/app/config/server"; -import { - BYTEDANCE_BASE_URL, - ApiPath, - ModelProvider, - ServiceProvider, -} from "@/app/constant"; -import { prettyObject } from "@/app/utils/format"; -import { NextRequest, NextResponse } from "next/server"; -import { auth } from "@/app/api/auth"; -import { isModelNotavailableInServer } from "@/app/utils/model"; - -const serverConfig = getServerSideConfig(); - -export async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[ByteDance Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const authResult = auth(req, ModelProvider.Doubao); - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - try { - const response = await request(req); - return response; - } catch (e) { - console.error("[ByteDance] ", e); - return NextResponse.json(prettyObject(e)); - } -} - -async function request(req: NextRequest) { - const controller = new AbortController(); - - let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.ByteDance, ""); - - let baseUrl = serverConfig.bytedanceUrl || BYTEDANCE_BASE_URL; - - if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, -1); - } - - console.log("[Proxy] ", path); - console.log("[Base Url]", baseUrl); - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - - const fetchUrl = `${baseUrl}${path}`; - - const fetchOptions: RequestInit = { - headers: { - "Content-Type": "application/json", - Authorization: req.headers.get("Authorization") ?? "", - }, - method: req.method, - body: req.body, - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - // #1815 try to refuse some request to some models - if (serverConfig.customModels && req.body) { - try { - const clonedBody = await req.text(); - fetchOptions.body = clonedBody; - - const jsonBody = JSON.parse(clonedBody) as { model?: string }; - - // not undefined and is false - if ( - isModelNotavailableInServer( - serverConfig.customModels, - jsonBody?.model as string, - ServiceProvider.ByteDance as string, - ) - ) { - return NextResponse.json( - { - error: true, - message: `you are not allowed to use ${jsonBody?.model} model`, - }, - { - status: 403, - }, - ); - } - } catch (e) { - console.error(`[ByteDance] filter`, e); - } - } - - try { - const res = await fetch(fetchUrl, fetchOptions); - - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/api/common.ts b/app/api/common.ts deleted file mode 100644 index b7e41fa2647..00000000000 --- a/app/api/common.ts +++ /dev/null @@ -1,186 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; -import { getServerSideConfig } from "../config/server"; -import { OPENAI_BASE_URL, ServiceProvider } from "../constant"; -import { cloudflareAIGatewayUrl } from "../utils/cloudflare"; -import { getModelProvider, isModelNotavailableInServer } from "../utils/model"; - -const serverConfig = getServerSideConfig(); - -export async function requestOpenai(req: NextRequest) { - const controller = new AbortController(); - - const isAzure = req.nextUrl.pathname.includes("azure/deployments"); - - var authValue, - authHeaderName = ""; - if (isAzure) { - authValue = - req.headers - .get("Authorization") - ?.trim() - .replaceAll("Bearer ", "") - .trim() ?? ""; - - authHeaderName = "api-key"; - } else { - authValue = req.headers.get("Authorization") ?? ""; - authHeaderName = "Authorization"; - } - - let path = `${req.nextUrl.pathname}`.replaceAll("/api/openai/", ""); - - let baseUrl = - (isAzure ? serverConfig.azureUrl : serverConfig.baseUrl) || OPENAI_BASE_URL; - - if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, -1); - } - - console.log("[Proxy] ", path); - console.log("[Base Url]", baseUrl); - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - - if (isAzure) { - const azureApiVersion = - req?.nextUrl?.searchParams?.get("api-version") || - serverConfig.azureApiVersion; - baseUrl = baseUrl.split("/deployments").shift() as string; - path = `${req.nextUrl.pathname.replaceAll( - "/api/azure/", - "", - )}?api-version=${azureApiVersion}`; - - // Forward compatibility: - // if display_name(deployment_name) not set, and '{deploy-id}' in AZURE_URL - // then using default '{deploy-id}' - if (serverConfig.customModels && serverConfig.azureUrl) { - const modelName = path.split("/")[1]; - let realDeployName = ""; - serverConfig.customModels - .split(",") - .filter((v) => !!v && !v.startsWith("-") && v.includes(modelName)) - .forEach((m) => { - const [fullName, displayName] = m.split("="); - const [_, providerName] = getModelProvider(fullName); - if (providerName === "azure" && !displayName) { - const [_, deployId] = (serverConfig?.azureUrl ?? "").split( - "deployments/", - ); - if (deployId) { - realDeployName = deployId; - } - } - }); - if (realDeployName) { - console.log("[Replace with DeployId", realDeployName); - path = path.replaceAll(modelName, realDeployName); - } - } - } - - const fetchUrl = cloudflareAIGatewayUrl(`${baseUrl}/${path}`); - console.log("fetchUrl", fetchUrl); - const fetchOptions: RequestInit = { - headers: { - "Content-Type": "application/json", - "Cache-Control": "no-store", - [authHeaderName]: authValue, - ...(serverConfig.openaiOrgId && { - "OpenAI-Organization": serverConfig.openaiOrgId, - }), - }, - method: req.method, - body: req.body, - // to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - // #1815 try to refuse gpt4 request - if (serverConfig.customModels && req.body) { - try { - const clonedBody = await req.text(); - fetchOptions.body = clonedBody; - - const jsonBody = JSON.parse(clonedBody) as { model?: string }; - - // not undefined and is false - if ( - isModelNotavailableInServer( - serverConfig.customModels, - jsonBody?.model as string, - [ - ServiceProvider.OpenAI, - ServiceProvider.Azure, - jsonBody?.model as string, // support provider-unspecified model - ], - ) - ) { - return NextResponse.json( - { - error: true, - message: `you are not allowed to use ${jsonBody?.model} model`, - }, - { - status: 403, - }, - ); - } - } catch (e) { - console.error("[OpenAI] gpt4 filter", e); - } - } - - try { - const res = await fetch(fetchUrl, fetchOptions); - - // Extract the OpenAI-Organization header from the response - const openaiOrganizationHeader = res.headers.get("OpenAI-Organization"); - - // Check if serverConfig.openaiOrgId is defined and not an empty string - if (serverConfig.openaiOrgId && serverConfig.openaiOrgId.trim() !== "") { - // If openaiOrganizationHeader is present, log it; otherwise, log that the header is not present - console.log("[Org ID]", openaiOrganizationHeader); - } else { - console.log("[Org ID] is not set up."); - } - - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - - // Conditionally delete the OpenAI-Organization header from the response if [Org ID] is undefined or empty (not setup in ENV) - // Also, this is to prevent the header from being sent to the client - if (!serverConfig.openaiOrgId || serverConfig.openaiOrgId.trim() === "") { - newHeaders.delete("OpenAI-Organization"); - } - - // The latest version of the OpenAI API forced the content-encoding to be "br" in json response - // So if the streaming is disabled, we need to remove the content-encoding header - // Because Vercel uses gzip to compress the response, if we don't remove the content-encoding header - // The browser will try to decode the response with brotli and fail - newHeaders.delete("content-encoding"); - - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/api/config/route.ts b/app/api/config/route.ts deleted file mode 100644 index 855a5db017e..00000000000 --- a/app/api/config/route.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { NextResponse } from "next/server"; - -import { getServerSideConfig } from "../../config/server"; - -const serverConfig = getServerSideConfig(); - -// Danger! Do not hard code any secret value here! -// 警告!不要在这里写入任何敏感信息! -const DANGER_CONFIG = { - needCode: serverConfig.needCode, - hideUserApiKey: serverConfig.hideUserApiKey, - disableGPT4: serverConfig.disableGPT4, - hideBalanceQuery: serverConfig.hideBalanceQuery, - disableFastLink: serverConfig.disableFastLink, - customModels: serverConfig.customModels, - defaultModel: serverConfig.defaultModel, - visionModels: serverConfig.visionModels, -}; - -declare global { - type DangerConfig = typeof DANGER_CONFIG; -} - -async function handle() { - return NextResponse.json(DANGER_CONFIG); -} - -export const GET = handle; -export const POST = handle; - -export const runtime = "edge"; diff --git a/app/api/deepseek.ts b/app/api/deepseek.ts deleted file mode 100644 index a9879ecedeb..00000000000 --- a/app/api/deepseek.ts +++ /dev/null @@ -1,128 +0,0 @@ -import { getServerSideConfig } from "@/app/config/server"; -import { - DEEPSEEK_BASE_URL, - ApiPath, - ModelProvider, - ServiceProvider, -} from "@/app/constant"; -import { prettyObject } from "@/app/utils/format"; -import { NextRequest, NextResponse } from "next/server"; -import { auth } from "@/app/api/auth"; -import { isModelNotavailableInServer } from "@/app/utils/model"; - -const serverConfig = getServerSideConfig(); - -export async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[DeepSeek Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const authResult = auth(req, ModelProvider.DeepSeek); - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - try { - const response = await request(req); - return response; - } catch (e) { - console.error("[DeepSeek] ", e); - return NextResponse.json(prettyObject(e)); - } -} - -async function request(req: NextRequest) { - const controller = new AbortController(); - - // alibaba use base url or just remove the path - let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.DeepSeek, ""); - - let baseUrl = serverConfig.deepseekUrl || DEEPSEEK_BASE_URL; - - if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, -1); - } - - console.log("[Proxy] ", path); - console.log("[Base Url]", baseUrl); - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - - const fetchUrl = `${baseUrl}${path}`; - const fetchOptions: RequestInit = { - headers: { - "Content-Type": "application/json", - Authorization: req.headers.get("Authorization") ?? "", - }, - method: req.method, - body: req.body, - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - // #1815 try to refuse some request to some models - if (serverConfig.customModels && req.body) { - try { - const clonedBody = await req.text(); - fetchOptions.body = clonedBody; - - const jsonBody = JSON.parse(clonedBody) as { model?: string }; - - // not undefined and is false - if ( - isModelNotavailableInServer( - serverConfig.customModels, - jsonBody?.model as string, - ServiceProvider.DeepSeek as string, - ) - ) { - return NextResponse.json( - { - error: true, - message: `you are not allowed to use ${jsonBody?.model} model`, - }, - { - status: 403, - }, - ); - } - } catch (e) { - console.error(`[DeepSeek] filter`, e); - } - } - try { - const res = await fetch(fetchUrl, fetchOptions); - - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/api/glm.ts b/app/api/glm.ts deleted file mode 100644 index 8431c5db5b0..00000000000 --- a/app/api/glm.ts +++ /dev/null @@ -1,129 +0,0 @@ -import { getServerSideConfig } from "@/app/config/server"; -import { - CHATGLM_BASE_URL, - ApiPath, - ModelProvider, - ServiceProvider, -} from "@/app/constant"; -import { prettyObject } from "@/app/utils/format"; -import { NextRequest, NextResponse } from "next/server"; -import { auth } from "@/app/api/auth"; -import { isModelNotavailableInServer } from "@/app/utils/model"; - -const serverConfig = getServerSideConfig(); - -export async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[GLM Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const authResult = auth(req, ModelProvider.ChatGLM); - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - try { - const response = await request(req); - return response; - } catch (e) { - console.error("[GLM] ", e); - return NextResponse.json(prettyObject(e)); - } -} - -async function request(req: NextRequest) { - const controller = new AbortController(); - - // alibaba use base url or just remove the path - let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.ChatGLM, ""); - - let baseUrl = serverConfig.chatglmUrl || CHATGLM_BASE_URL; - - if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, -1); - } - - console.log("[Proxy] ", path); - console.log("[Base Url]", baseUrl); - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - - const fetchUrl = `${baseUrl}${path}`; - console.log("[Fetch Url] ", fetchUrl); - const fetchOptions: RequestInit = { - headers: { - "Content-Type": "application/json", - Authorization: req.headers.get("Authorization") ?? "", - }, - method: req.method, - body: req.body, - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - // #1815 try to refuse some request to some models - if (serverConfig.customModels && req.body) { - try { - const clonedBody = await req.text(); - fetchOptions.body = clonedBody; - - const jsonBody = JSON.parse(clonedBody) as { model?: string }; - - // not undefined and is false - if ( - isModelNotavailableInServer( - serverConfig.customModels, - jsonBody?.model as string, - ServiceProvider.ChatGLM as string, - ) - ) { - return NextResponse.json( - { - error: true, - message: `you are not allowed to use ${jsonBody?.model} model`, - }, - { - status: 403, - }, - ); - } - } catch (e) { - console.error(`[GLM] filter`, e); - } - } - try { - const res = await fetch(fetchUrl, fetchOptions); - - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/api/google.ts b/app/api/google.ts deleted file mode 100644 index 707892c33d0..00000000000 --- a/app/api/google.ts +++ /dev/null @@ -1,133 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; -import { auth } from "./auth"; -import { getServerSideConfig } from "@/app/config/server"; -import { ApiPath, GEMINI_BASE_URL, ModelProvider } from "@/app/constant"; -import { prettyObject } from "@/app/utils/format"; - -const serverConfig = getServerSideConfig(); - -export async function handle( - req: NextRequest, - { params }: { params: { provider: string; path: string[] } }, -) { - console.log("[Google Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const authResult = auth(req, ModelProvider.GeminiPro); - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - const bearToken = - req.headers.get("x-goog-api-key") || req.headers.get("Authorization") || ""; - const token = bearToken.trim().replaceAll("Bearer ", "").trim(); - - const apiKey = token ? token : serverConfig.googleApiKey; - - if (!apiKey) { - return NextResponse.json( - { - error: true, - message: `missing GOOGLE_API_KEY in server env vars`, - }, - { - status: 401, - }, - ); - } - try { - const response = await request(req, apiKey); - return response; - } catch (e) { - console.error("[Google] ", e); - return NextResponse.json(prettyObject(e)); - } -} - -export const GET = handle; -export const POST = handle; - -export const runtime = "edge"; -export const preferredRegion = [ - "bom1", - "cle1", - "cpt1", - "gru1", - "hnd1", - "iad1", - "icn1", - "kix1", - "pdx1", - "sfo1", - "sin1", - "syd1", -]; - -async function request(req: NextRequest, apiKey: string) { - const controller = new AbortController(); - - let baseUrl = serverConfig.googleUrl || GEMINI_BASE_URL; - - let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Google, ""); - - if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, -1); - } - - console.log("[Proxy] ", path); - console.log("[Base Url]", baseUrl); - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - const fetchUrl = `${baseUrl}${path}${ - req?.nextUrl?.searchParams?.get("alt") === "sse" ? "?alt=sse" : "" - }`; - - console.log("[Fetch Url] ", fetchUrl); - const fetchOptions: RequestInit = { - headers: { - "Content-Type": "application/json", - "Cache-Control": "no-store", - "x-goog-api-key": - req.headers.get("x-goog-api-key") || - (req.headers.get("Authorization") ?? "").replace("Bearer ", ""), - }, - method: req.method, - body: req.body, - // to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - try { - const res = await fetch(fetchUrl, fetchOptions); - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/api/iflytek.ts b/app/api/iflytek.ts deleted file mode 100644 index 6624f74e9ab..00000000000 --- a/app/api/iflytek.ts +++ /dev/null @@ -1,129 +0,0 @@ -import { getServerSideConfig } from "@/app/config/server"; -import { - IFLYTEK_BASE_URL, - ApiPath, - ModelProvider, - ServiceProvider, -} from "@/app/constant"; -import { prettyObject } from "@/app/utils/format"; -import { NextRequest, NextResponse } from "next/server"; -import { auth } from "@/app/api/auth"; -import { isModelNotavailableInServer } from "@/app/utils/model"; -// iflytek - -const serverConfig = getServerSideConfig(); - -export async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[Iflytek Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const authResult = auth(req, ModelProvider.Iflytek); - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - try { - const response = await request(req); - return response; - } catch (e) { - console.error("[Iflytek] ", e); - return NextResponse.json(prettyObject(e)); - } -} - -async function request(req: NextRequest) { - const controller = new AbortController(); - - // iflytek use base url or just remove the path - let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Iflytek, ""); - - let baseUrl = serverConfig.iflytekUrl || IFLYTEK_BASE_URL; - - if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, -1); - } - - console.log("[Proxy] ", path); - console.log("[Base Url]", baseUrl); - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - - const fetchUrl = `${baseUrl}${path}`; - const fetchOptions: RequestInit = { - headers: { - "Content-Type": "application/json", - Authorization: req.headers.get("Authorization") ?? "", - }, - method: req.method, - body: req.body, - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - // try to refuse some request to some models - if (serverConfig.customModels && req.body) { - try { - const clonedBody = await req.text(); - fetchOptions.body = clonedBody; - - const jsonBody = JSON.parse(clonedBody) as { model?: string }; - - // not undefined and is false - if ( - isModelNotavailableInServer( - serverConfig.customModels, - jsonBody?.model as string, - ServiceProvider.Iflytek as string, - ) - ) { - return NextResponse.json( - { - error: true, - message: `you are not allowed to use ${jsonBody?.model} model`, - }, - { - status: 403, - }, - ); - } - } catch (e) { - console.error(`[Iflytek] filter`, e); - } - } - try { - const res = await fetch(fetchUrl, fetchOptions); - - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/api/moonshot.ts b/app/api/moonshot.ts deleted file mode 100644 index 792d14d3334..00000000000 --- a/app/api/moonshot.ts +++ /dev/null @@ -1,128 +0,0 @@ -import { getServerSideConfig } from "@/app/config/server"; -import { - MOONSHOT_BASE_URL, - ApiPath, - ModelProvider, - ServiceProvider, -} from "@/app/constant"; -import { prettyObject } from "@/app/utils/format"; -import { NextRequest, NextResponse } from "next/server"; -import { auth } from "@/app/api/auth"; -import { isModelNotavailableInServer } from "@/app/utils/model"; - -const serverConfig = getServerSideConfig(); - -export async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[Moonshot Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const authResult = auth(req, ModelProvider.Moonshot); - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - try { - const response = await request(req); - return response; - } catch (e) { - console.error("[Moonshot] ", e); - return NextResponse.json(prettyObject(e)); - } -} - -async function request(req: NextRequest) { - const controller = new AbortController(); - - // alibaba use base url or just remove the path - let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Moonshot, ""); - - let baseUrl = serverConfig.moonshotUrl || MOONSHOT_BASE_URL; - - if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, -1); - } - - console.log("[Proxy] ", path); - console.log("[Base Url]", baseUrl); - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - - const fetchUrl = `${baseUrl}${path}`; - const fetchOptions: RequestInit = { - headers: { - "Content-Type": "application/json", - Authorization: req.headers.get("Authorization") ?? "", - }, - method: req.method, - body: req.body, - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - // #1815 try to refuse some request to some models - if (serverConfig.customModels && req.body) { - try { - const clonedBody = await req.text(); - fetchOptions.body = clonedBody; - - const jsonBody = JSON.parse(clonedBody) as { model?: string }; - - // not undefined and is false - if ( - isModelNotavailableInServer( - serverConfig.customModels, - jsonBody?.model as string, - ServiceProvider.Moonshot as string, - ) - ) { - return NextResponse.json( - { - error: true, - message: `you are not allowed to use ${jsonBody?.model} model`, - }, - { - status: 403, - }, - ); - } - } catch (e) { - console.error(`[Moonshot] filter`, e); - } - } - try { - const res = await fetch(fetchUrl, fetchOptions); - - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/api/openai.ts b/app/api/openai.ts deleted file mode 100644 index 5bb3a5dcb64..00000000000 --- a/app/api/openai.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { type OpenAIListModelResponse } from "@/app/client/platforms/openai"; -import { getServerSideConfig } from "@/app/config/server"; -import { ModelProvider, OpenaiPath } from "@/app/constant"; -import { prettyObject } from "@/app/utils/format"; -import { NextRequest, NextResponse } from "next/server"; -import { auth } from "./auth"; -import { requestOpenai } from "./common"; - -const ALLOWED_PATH = new Set(Object.values(OpenaiPath)); - -function getModels(remoteModelRes: OpenAIListModelResponse) { - const config = getServerSideConfig(); - - if (config.disableGPT4) { - remoteModelRes.data = remoteModelRes.data.filter( - (m) => - !( - m.id.startsWith("gpt-4") || - m.id.startsWith("chatgpt-4o") || - m.id.startsWith("o1") || - m.id.startsWith("o3") - ) || m.id.startsWith("gpt-4o-mini"), - ); - } - - return remoteModelRes; -} - -export async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[OpenAI Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const subpath = params.path.join("/"); - - if (!ALLOWED_PATH.has(subpath)) { - console.log("[OpenAI Route] forbidden path ", subpath); - return NextResponse.json( - { - error: true, - msg: "you are not allowed to request " + subpath, - }, - { - status: 403, - }, - ); - } - - const authResult = auth(req, ModelProvider.GPT); - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - try { - const response = await requestOpenai(req); - - // list models - if (subpath === OpenaiPath.ListModelPath && response.status === 200) { - const resJson = (await response.json()) as OpenAIListModelResponse; - const availableModels = getModels(resJson); - return NextResponse.json(availableModels, { - status: response.status, - }); - } - - return response; - } catch (e) { - console.error("[OpenAI] ", e); - return NextResponse.json(prettyObject(e)); - } -} diff --git a/app/api/proxy.ts b/app/api/proxy.ts deleted file mode 100644 index b3e5e7b7b93..00000000000 --- a/app/api/proxy.ts +++ /dev/null @@ -1,89 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; -import { getServerSideConfig } from "@/app/config/server"; - -export async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[Proxy Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - const serverConfig = getServerSideConfig(); - - // remove path params from searchParams - req.nextUrl.searchParams.delete("path"); - req.nextUrl.searchParams.delete("provider"); - - const subpath = params.path.join("/"); - const fetchUrl = `${req.headers.get( - "x-base-url", - )}/${subpath}?${req.nextUrl.searchParams.toString()}`; - const skipHeaders = ["connection", "host", "origin", "referer", "cookie"]; - const headers = new Headers( - Array.from(req.headers.entries()).filter((item) => { - if ( - item[0].indexOf("x-") > -1 || - item[0].indexOf("sec-") > -1 || - skipHeaders.includes(item[0]) - ) { - return false; - } - return true; - }), - ); - // if dalle3 use openai api key - const baseUrl = req.headers.get("x-base-url"); - if (baseUrl?.includes("api.openai.com")) { - if (!serverConfig.apiKey) { - return NextResponse.json( - { error: "OpenAI API key not configured" }, - { status: 500 }, - ); - } - headers.set("Authorization", `Bearer ${serverConfig.apiKey}`); - } - - const controller = new AbortController(); - const fetchOptions: RequestInit = { - headers, - method: req.method, - body: req.body, - // to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - - try { - const res = await fetch(fetchUrl, fetchOptions); - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - - // The latest version of the OpenAI API forced the content-encoding to be "br" in json response - // So if the streaming is disabled, we need to remove the content-encoding header - // Because Vercel uses gzip to compress the response, if we don't remove the content-encoding header - // The browser will try to decode the response with brotli and fail - newHeaders.delete("content-encoding"); - - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/api/siliconflow.ts b/app/api/siliconflow.ts deleted file mode 100644 index e298a21d43c..00000000000 --- a/app/api/siliconflow.ts +++ /dev/null @@ -1,128 +0,0 @@ -import { getServerSideConfig } from "@/app/config/server"; -import { - SILICONFLOW_BASE_URL, - ApiPath, - ModelProvider, - ServiceProvider, -} from "@/app/constant"; -import { prettyObject } from "@/app/utils/format"; -import { NextRequest, NextResponse } from "next/server"; -import { auth } from "@/app/api/auth"; -import { isModelNotavailableInServer } from "@/app/utils/model"; - -const serverConfig = getServerSideConfig(); - -export async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[SiliconFlow Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const authResult = auth(req, ModelProvider.SiliconFlow); - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - try { - const response = await request(req); - return response; - } catch (e) { - console.error("[SiliconFlow] ", e); - return NextResponse.json(prettyObject(e)); - } -} - -async function request(req: NextRequest) { - const controller = new AbortController(); - - // alibaba use base url or just remove the path - let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.SiliconFlow, ""); - - let baseUrl = serverConfig.siliconFlowUrl || SILICONFLOW_BASE_URL; - - if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, -1); - } - - console.log("[Proxy] ", path); - console.log("[Base Url]", baseUrl); - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - - const fetchUrl = `${baseUrl}${path}`; - const fetchOptions: RequestInit = { - headers: { - "Content-Type": "application/json", - Authorization: req.headers.get("Authorization") ?? "", - }, - method: req.method, - body: req.body, - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - // #1815 try to refuse some request to some models - if (serverConfig.customModels && req.body) { - try { - const clonedBody = await req.text(); - fetchOptions.body = clonedBody; - - const jsonBody = JSON.parse(clonedBody) as { model?: string }; - - // not undefined and is false - if ( - isModelNotavailableInServer( - serverConfig.customModels, - jsonBody?.model as string, - ServiceProvider.SiliconFlow as string, - ) - ) { - return NextResponse.json( - { - error: true, - message: `you are not allowed to use ${jsonBody?.model} model`, - }, - { - status: 403, - }, - ); - } - } catch (e) { - console.error(`[SiliconFlow] filter`, e); - } - } - try { - const res = await fetch(fetchUrl, fetchOptions); - - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/api/stability.ts b/app/api/stability.ts deleted file mode 100644 index 2646ace858e..00000000000 --- a/app/api/stability.ts +++ /dev/null @@ -1,99 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; -import { getServerSideConfig } from "@/app/config/server"; -import { ModelProvider, STABILITY_BASE_URL } from "@/app/constant"; -import { auth } from "@/app/api/auth"; - -export async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[Stability] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const controller = new AbortController(); - - const serverConfig = getServerSideConfig(); - - let baseUrl = serverConfig.stabilityUrl || STABILITY_BASE_URL; - - if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, -1); - } - - let path = `${req.nextUrl.pathname}`.replaceAll("/api/stability/", ""); - - console.log("[Stability Proxy] ", path); - console.log("[Stability Base Url]", baseUrl); - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - - const authResult = auth(req, ModelProvider.Stability); - - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - const bearToken = req.headers.get("Authorization") ?? ""; - const token = bearToken.trim().replaceAll("Bearer ", "").trim(); - - const key = token ? token : serverConfig.stabilityApiKey; - - if (!key) { - return NextResponse.json( - { - error: true, - message: `missing STABILITY_API_KEY in server env vars`, - }, - { - status: 401, - }, - ); - } - - const fetchUrl = `${baseUrl}/${path}`; - console.log("[Stability Url] ", fetchUrl); - const fetchOptions: RequestInit = { - headers: { - "Content-Type": req.headers.get("Content-Type") || "multipart/form-data", - Accept: req.headers.get("Accept") || "application/json", - Authorization: `Bearer ${key}`, - }, - method: req.method, - body: req.body, - // to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - try { - const res = await fetch(fetchUrl, fetchOptions); - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/api/tencent/route.ts b/app/api/tencent/route.ts deleted file mode 100644 index fc4f8c79edf..00000000000 --- a/app/api/tencent/route.ts +++ /dev/null @@ -1,117 +0,0 @@ -import { getServerSideConfig } from "@/app/config/server"; -import { TENCENT_BASE_URL, ModelProvider } from "@/app/constant"; -import { prettyObject } from "@/app/utils/format"; -import { NextRequest, NextResponse } from "next/server"; -import { auth } from "@/app/api/auth"; -import { getHeader } from "@/app/utils/tencent"; - -const serverConfig = getServerSideConfig(); - -async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[Tencent Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const authResult = auth(req, ModelProvider.Hunyuan); - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - try { - const response = await request(req); - return response; - } catch (e) { - console.error("[Tencent] ", e); - return NextResponse.json(prettyObject(e)); - } -} - -export const GET = handle; -export const POST = handle; - -export const runtime = "edge"; -export const preferredRegion = [ - "arn1", - "bom1", - "cdg1", - "cle1", - "cpt1", - "dub1", - "fra1", - "gru1", - "hnd1", - "iad1", - "icn1", - "kix1", - "lhr1", - "pdx1", - "sfo1", - "sin1", - "syd1", -]; - -async function request(req: NextRequest) { - const controller = new AbortController(); - - let baseUrl = serverConfig.tencentUrl || TENCENT_BASE_URL; - - if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, -1); - } - - console.log("[Base Url]", baseUrl); - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - - const fetchUrl = baseUrl; - - const body = await req.text(); - const headers = await getHeader( - body, - serverConfig.tencentSecretId as string, - serverConfig.tencentSecretKey as string, - ); - const fetchOptions: RequestInit = { - headers, - method: req.method, - body, - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - try { - const res = await fetch(fetchUrl, fetchOptions); - - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/api/upstash/[action]/[...key]/route.ts b/app/api/upstash/[action]/[...key]/route.ts deleted file mode 100644 index fcfef471862..00000000000 --- a/app/api/upstash/[action]/[...key]/route.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; - -async function handle( - req: NextRequest, - { params }: { params: { action: string; key: string[] } }, -) { - const requestUrl = new URL(req.url); - const endpoint = requestUrl.searchParams.get("endpoint"); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - const [...key] = params.key; - // only allow to request to *.upstash.io - if (!endpoint || !new URL(endpoint).hostname.endsWith(".upstash.io")) { - return NextResponse.json( - { - error: true, - msg: "you are not allowed to request " + params.key.join("/"), - }, - { - status: 403, - }, - ); - } - - // only allow upstash get and set method - if (params.action !== "get" && params.action !== "set") { - console.log("[Upstash Route] forbidden action ", params.action); - return NextResponse.json( - { - error: true, - msg: "you are not allowed to request " + params.action, - }, - { - status: 403, - }, - ); - } - - const targetUrl = `${endpoint}/${params.action}/${params.key.join("/")}`; - - const method = req.method; - const shouldNotHaveBody = ["get", "head"].includes( - method?.toLowerCase() ?? "", - ); - - const fetchOptions: RequestInit = { - headers: { - authorization: req.headers.get("authorization") ?? "", - }, - body: shouldNotHaveBody ? null : req.body, - method, - // @ts-ignore - duplex: "half", - }; - - console.log("[Upstash Proxy]", targetUrl, fetchOptions); - const fetchResult = await fetch(targetUrl, fetchOptions); - - console.log("[Any Proxy]", targetUrl, { - status: fetchResult.status, - statusText: fetchResult.statusText, - }); - - return fetchResult; -} - -export const POST = handle; -export const GET = handle; -export const OPTIONS = handle; - -export const runtime = "edge"; diff --git a/app/api/webdav/[...path]/route.ts b/app/api/webdav/[...path]/route.ts deleted file mode 100644 index bb7743bda40..00000000000 --- a/app/api/webdav/[...path]/route.ts +++ /dev/null @@ -1,167 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; -import { STORAGE_KEY, internalAllowedWebDavEndpoints } from "../../../constant"; -import { getServerSideConfig } from "@/app/config/server"; - -const config = getServerSideConfig(); - -const mergedAllowedWebDavEndpoints = [ - ...internalAllowedWebDavEndpoints, - ...config.allowedWebDavEndpoints, -].filter((domain) => Boolean(domain.trim())); - -const normalizeUrl = (url: string) => { - try { - return new URL(url); - } catch (err) { - return null; - } -}; - -async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - const folder = STORAGE_KEY; - const fileName = `${folder}/backup.json`; - - const requestUrl = new URL(req.url); - let endpoint = requestUrl.searchParams.get("endpoint"); - let proxy_method = requestUrl.searchParams.get("proxy_method") || req.method; - - // Validate the endpoint to prevent potential SSRF attacks - if ( - !endpoint || - !mergedAllowedWebDavEndpoints.some((allowedEndpoint) => { - const normalizedAllowedEndpoint = normalizeUrl(allowedEndpoint); - const normalizedEndpoint = normalizeUrl(endpoint as string); - - return ( - normalizedEndpoint && - normalizedEndpoint.hostname === normalizedAllowedEndpoint?.hostname && - normalizedEndpoint.pathname.startsWith( - normalizedAllowedEndpoint.pathname, - ) - ); - }) - ) { - return NextResponse.json( - { - error: true, - msg: "Invalid endpoint", - }, - { - status: 400, - }, - ); - } - - if (!endpoint?.endsWith("/")) { - endpoint += "/"; - } - - const endpointPath = params.path.join("/"); - const targetPath = `${endpoint}${endpointPath}`; - - // only allow MKCOL, GET, PUT - if ( - proxy_method !== "MKCOL" && - proxy_method !== "GET" && - proxy_method !== "PUT" - ) { - return NextResponse.json( - { - error: true, - msg: "you are not allowed to request " + targetPath, - }, - { - status: 403, - }, - ); - } - - // for MKCOL request, only allow request ${folder} - if (proxy_method === "MKCOL" && !targetPath.endsWith(folder)) { - return NextResponse.json( - { - error: true, - msg: "you are not allowed to request " + targetPath, - }, - { - status: 403, - }, - ); - } - - // for GET request, only allow request ending with fileName - if (proxy_method === "GET" && !targetPath.endsWith(fileName)) { - return NextResponse.json( - { - error: true, - msg: "you are not allowed to request " + targetPath, - }, - { - status: 403, - }, - ); - } - - // for PUT request, only allow request ending with fileName - if (proxy_method === "PUT" && !targetPath.endsWith(fileName)) { - return NextResponse.json( - { - error: true, - msg: "you are not allowed to request " + targetPath, - }, - { - status: 403, - }, - ); - } - - const targetUrl = targetPath; - - const method = proxy_method || req.method; - const shouldNotHaveBody = ["get", "head"].includes( - method?.toLowerCase() ?? "", - ); - - const fetchOptions: RequestInit = { - headers: { - authorization: req.headers.get("authorization") ?? "", - }, - body: shouldNotHaveBody ? null : req.body, - redirect: "manual", - method, - // @ts-ignore - duplex: "half", - }; - - let fetchResult; - - try { - fetchResult = await fetch(targetUrl, fetchOptions); - } finally { - console.log( - "[Any Proxy]", - targetUrl, - { - method: method, - }, - { - status: fetchResult?.status, - statusText: fetchResult?.statusText, - }, - ); - } - - return fetchResult; -} - -export const PUT = handle; -export const GET = handle; -export const OPTIONS = handle; - -export const runtime = "edge"; diff --git a/app/api/xai.ts b/app/api/xai.ts deleted file mode 100644 index 4aad5e5fb3e..00000000000 --- a/app/api/xai.ts +++ /dev/null @@ -1,128 +0,0 @@ -import { getServerSideConfig } from "@/app/config/server"; -import { - XAI_BASE_URL, - ApiPath, - ModelProvider, - ServiceProvider, -} from "@/app/constant"; -import { prettyObject } from "@/app/utils/format"; -import { NextRequest, NextResponse } from "next/server"; -import { auth } from "@/app/api/auth"; -import { isModelNotavailableInServer } from "@/app/utils/model"; - -const serverConfig = getServerSideConfig(); - -export async function handle( - req: NextRequest, - { params }: { params: { path: string[] } }, -) { - console.log("[XAI Route] params ", params); - - if (req.method === "OPTIONS") { - return NextResponse.json({ body: "OK" }, { status: 200 }); - } - - const authResult = auth(req, ModelProvider.XAI); - if (authResult.error) { - return NextResponse.json(authResult, { - status: 401, - }); - } - - try { - const response = await request(req); - return response; - } catch (e) { - console.error("[XAI] ", e); - return NextResponse.json(prettyObject(e)); - } -} - -async function request(req: NextRequest) { - const controller = new AbortController(); - - // alibaba use base url or just remove the path - let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.XAI, ""); - - let baseUrl = serverConfig.xaiUrl || XAI_BASE_URL; - - if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, -1); - } - - console.log("[Proxy] ", path); - console.log("[Base Url]", baseUrl); - - const timeoutId = setTimeout( - () => { - controller.abort(); - }, - 10 * 60 * 1000, - ); - - const fetchUrl = `${baseUrl}${path}`; - const fetchOptions: RequestInit = { - headers: { - "Content-Type": "application/json", - Authorization: req.headers.get("Authorization") ?? "", - }, - method: req.method, - body: req.body, - redirect: "manual", - // @ts-ignore - duplex: "half", - signal: controller.signal, - }; - - // #1815 try to refuse some request to some models - if (serverConfig.customModels && req.body) { - try { - const clonedBody = await req.text(); - fetchOptions.body = clonedBody; - - const jsonBody = JSON.parse(clonedBody) as { model?: string }; - - // not undefined and is false - if ( - isModelNotavailableInServer( - serverConfig.customModels, - jsonBody?.model as string, - ServiceProvider.XAI as string, - ) - ) { - return NextResponse.json( - { - error: true, - message: `you are not allowed to use ${jsonBody?.model} model`, - }, - { - status: 403, - }, - ); - } - } catch (e) { - console.error(`[XAI] filter`, e); - } - } - try { - const res = await fetch(fetchUrl, fetchOptions); - - // to prevent browser prompt for credentials - const newHeaders = new Headers(res.headers); - newHeaders.delete("www-authenticate"); - // to disable nginx buffering - newHeaders.set("X-Accel-Buffering", "no"); - - return new Response(res.body, { - status: res.status, - statusText: res.statusText, - headers: newHeaders, - }); - } finally { - clearTimeout(timeoutId); - } -} diff --git a/app/client/api.ts b/app/client/api.ts deleted file mode 100644 index f60b0e2ad71..00000000000 --- a/app/client/api.ts +++ /dev/null @@ -1,399 +0,0 @@ -import { getClientConfig } from "../config/client"; -import { - ACCESS_CODE_PREFIX, - ModelProvider, - ServiceProvider, -} from "../constant"; -import { - ChatMessageTool, - ChatMessage, - ModelType, - useAccessStore, - useChatStore, -} from "../store"; -import { ChatGPTApi, DalleRequestPayload } from "./platforms/openai"; -import { GeminiProApi } from "./platforms/google"; -import { ClaudeApi } from "./platforms/anthropic"; -import { ErnieApi } from "./platforms/baidu"; -import { DoubaoApi } from "./platforms/bytedance"; -import { QwenApi } from "./platforms/alibaba"; -import { HunyuanApi } from "./platforms/tencent"; -import { MoonshotApi } from "./platforms/moonshot"; -import { SparkApi } from "./platforms/iflytek"; -import { DeepSeekApi } from "./platforms/deepseek"; -import { XAIApi } from "./platforms/xai"; -import { ChatGLMApi } from "./platforms/glm"; -import { SiliconflowApi } from "./platforms/siliconflow"; -import { Ai302Api } from "./platforms/ai302"; - -export const ROLES = ["system", "user", "assistant"] as const; -export type MessageRole = (typeof ROLES)[number]; - -export const Models = ["gpt-3.5-turbo", "gpt-4"] as const; -export const TTSModels = ["tts-1", "tts-1-hd"] as const; -export type ChatModel = ModelType; - -export interface MultimodalContent { - type: "text" | "image_url"; - text?: string; - image_url?: { - url: string; - }; -} - -export interface MultimodalContentForAlibaba { - text?: string; - image?: string; -} - -export interface RequestMessage { - role: MessageRole; - content: string | MultimodalContent[]; -} - -export interface LLMConfig { - model: string; - providerName?: string; - temperature?: number; - top_p?: number; - stream?: boolean; - presence_penalty?: number; - frequency_penalty?: number; - size?: DalleRequestPayload["size"]; - quality?: DalleRequestPayload["quality"]; - style?: DalleRequestPayload["style"]; -} - -export interface SpeechOptions { - model: string; - input: string; - voice: string; - response_format?: string; - speed?: number; - onController?: (controller: AbortController) => void; -} - -export interface ChatOptions { - messages: RequestMessage[]; - config: LLMConfig; - - onUpdate?: (message: string, chunk: string) => void; - onFinish: (message: string, responseRes: Response) => void; - onError?: (err: Error) => void; - onController?: (controller: AbortController) => void; - onBeforeTool?: (tool: ChatMessageTool) => void; - onAfterTool?: (tool: ChatMessageTool) => void; -} - -export interface LLMUsage { - used: number; - total: number; -} - -export interface LLMModel { - name: string; - displayName?: string; - available: boolean; - provider: LLMModelProvider; - sorted: number; -} - -export interface LLMModelProvider { - id: string; - providerName: string; - providerType: string; - sorted: number; -} - -export abstract class LLMApi { - abstract chat(options: ChatOptions): Promise; - abstract speech(options: SpeechOptions): Promise; - abstract usage(): Promise; - abstract models(): Promise; -} - -type ProviderName = "openai" | "azure" | "claude" | "palm"; - -interface Model { - name: string; - provider: ProviderName; - ctxlen: number; -} - -interface ChatProvider { - name: ProviderName; - apiConfig: { - baseUrl: string; - apiKey: string; - summaryModel: Model; - }; - models: Model[]; - - chat: () => void; - usage: () => void; -} - -export class ClientApi { - public llm: LLMApi; - - constructor(provider: ModelProvider = ModelProvider.GPT) { - switch (provider) { - case ModelProvider.GeminiPro: - this.llm = new GeminiProApi(); - break; - case ModelProvider.Claude: - this.llm = new ClaudeApi(); - break; - case ModelProvider.Ernie: - this.llm = new ErnieApi(); - break; - case ModelProvider.Doubao: - this.llm = new DoubaoApi(); - break; - case ModelProvider.Qwen: - this.llm = new QwenApi(); - break; - case ModelProvider.Hunyuan: - this.llm = new HunyuanApi(); - break; - case ModelProvider.Moonshot: - this.llm = new MoonshotApi(); - break; - case ModelProvider.Iflytek: - this.llm = new SparkApi(); - break; - case ModelProvider.DeepSeek: - this.llm = new DeepSeekApi(); - break; - case ModelProvider.XAI: - this.llm = new XAIApi(); - break; - case ModelProvider.ChatGLM: - this.llm = new ChatGLMApi(); - break; - case ModelProvider.SiliconFlow: - this.llm = new SiliconflowApi(); - break; - case ModelProvider["302.AI"]: - this.llm = new Ai302Api(); - break; - default: - this.llm = new ChatGPTApi(); - } - } - - config() {} - - prompts() {} - - masks() {} - - async share(messages: ChatMessage[], avatarUrl: string | null = null) { - const msgs = messages - .map((m) => ({ - from: m.role === "user" ? "human" : "gpt", - value: m.content, - })) - .concat([ - { - from: "human", - value: - "Share from [NextChat]: https://github.com/Yidadaa/ChatGPT-Next-Web", - }, - ]); - // 敬告二开开发者们,为了开源大模型的发展,请不要修改上述消息,此消息用于后续数据清洗使用 - // Please do not modify this message - - console.log("[Share]", messages, msgs); - const clientConfig = getClientConfig(); - const proxyUrl = "/sharegpt"; - const rawUrl = "https://sharegpt.com/api/conversations"; - const shareUrl = clientConfig?.isApp ? rawUrl : proxyUrl; - const res = await fetch(shareUrl, { - body: JSON.stringify({ - avatarUrl, - items: msgs, - }), - headers: { - "Content-Type": "application/json", - }, - method: "POST", - }); - - const resJson = await res.json(); - console.log("[Share]", resJson); - if (resJson.id) { - return `https://shareg.pt/${resJson.id}`; - } - } -} - -export function getBearerToken( - apiKey: string, - noBearer: boolean = false, -): string { - return validString(apiKey) - ? `${noBearer ? "" : "Bearer "}${apiKey.trim()}` - : ""; -} - -export function validString(x: string): boolean { - return x?.length > 0; -} - -export function getHeaders(ignoreHeaders: boolean = false) { - const accessStore = useAccessStore.getState(); - const chatStore = useChatStore.getState(); - let headers: Record = {}; - if (!ignoreHeaders) { - headers = { - "Content-Type": "application/json", - Accept: "application/json", - }; - } - - const clientConfig = getClientConfig(); - - function getConfig() { - const modelConfig = chatStore.currentSession().mask.modelConfig; - const isGoogle = modelConfig.providerName === ServiceProvider.Google; - const isAzure = modelConfig.providerName === ServiceProvider.Azure; - const isAnthropic = modelConfig.providerName === ServiceProvider.Anthropic; - const isBaidu = modelConfig.providerName == ServiceProvider.Baidu; - const isByteDance = modelConfig.providerName === ServiceProvider.ByteDance; - const isAlibaba = modelConfig.providerName === ServiceProvider.Alibaba; - const isMoonshot = modelConfig.providerName === ServiceProvider.Moonshot; - const isIflytek = modelConfig.providerName === ServiceProvider.Iflytek; - const isDeepSeek = modelConfig.providerName === ServiceProvider.DeepSeek; - const isXAI = modelConfig.providerName === ServiceProvider.XAI; - const isChatGLM = modelConfig.providerName === ServiceProvider.ChatGLM; - const isSiliconFlow = - modelConfig.providerName === ServiceProvider.SiliconFlow; - const isAI302 = modelConfig.providerName === ServiceProvider["302.AI"]; - const isEnabledAccessControl = accessStore.enabledAccessControl(); - const apiKey = isGoogle - ? accessStore.googleApiKey - : isAzure - ? accessStore.azureApiKey - : isAnthropic - ? accessStore.anthropicApiKey - : isByteDance - ? accessStore.bytedanceApiKey - : isAlibaba - ? accessStore.alibabaApiKey - : isMoonshot - ? accessStore.moonshotApiKey - : isXAI - ? accessStore.xaiApiKey - : isDeepSeek - ? accessStore.deepseekApiKey - : isChatGLM - ? accessStore.chatglmApiKey - : isSiliconFlow - ? accessStore.siliconflowApiKey - : isIflytek - ? accessStore.iflytekApiKey && accessStore.iflytekApiSecret - ? accessStore.iflytekApiKey + ":" + accessStore.iflytekApiSecret - : "" - : isAI302 - ? accessStore.ai302ApiKey - : accessStore.openaiApiKey; - return { - isGoogle, - isAzure, - isAnthropic, - isBaidu, - isByteDance, - isAlibaba, - isMoonshot, - isIflytek, - isDeepSeek, - isXAI, - isChatGLM, - isSiliconFlow, - isAI302, - apiKey, - isEnabledAccessControl, - }; - } - - function getAuthHeader(): string { - return isAzure - ? "api-key" - : isAnthropic - ? "x-api-key" - : isGoogle - ? "x-goog-api-key" - : "Authorization"; - } - - const { - isGoogle, - isAzure, - isAnthropic, - isBaidu, - isByteDance, - isAlibaba, - isMoonshot, - isIflytek, - isDeepSeek, - isXAI, - isChatGLM, - isSiliconFlow, - isAI302, - apiKey, - isEnabledAccessControl, - } = getConfig(); - // when using baidu api in app, not set auth header - if (isBaidu && clientConfig?.isApp) return headers; - - const authHeader = getAuthHeader(); - - const bearerToken = getBearerToken( - apiKey, - isAzure || isAnthropic || isGoogle, - ); - - if (bearerToken) { - headers[authHeader] = bearerToken; - } else if (isEnabledAccessControl && validString(accessStore.accessCode)) { - headers["Authorization"] = getBearerToken( - ACCESS_CODE_PREFIX + accessStore.accessCode, - ); - } - - return headers; -} - -export function getClientApi(provider: ServiceProvider): ClientApi { - switch (provider) { - case ServiceProvider.Google: - return new ClientApi(ModelProvider.GeminiPro); - case ServiceProvider.Anthropic: - return new ClientApi(ModelProvider.Claude); - case ServiceProvider.Baidu: - return new ClientApi(ModelProvider.Ernie); - case ServiceProvider.ByteDance: - return new ClientApi(ModelProvider.Doubao); - case ServiceProvider.Alibaba: - return new ClientApi(ModelProvider.Qwen); - case ServiceProvider.Tencent: - return new ClientApi(ModelProvider.Hunyuan); - case ServiceProvider.Moonshot: - return new ClientApi(ModelProvider.Moonshot); - case ServiceProvider.Iflytek: - return new ClientApi(ModelProvider.Iflytek); - case ServiceProvider.DeepSeek: - return new ClientApi(ModelProvider.DeepSeek); - case ServiceProvider.XAI: - return new ClientApi(ModelProvider.XAI); - case ServiceProvider.ChatGLM: - return new ClientApi(ModelProvider.ChatGLM); - case ServiceProvider.SiliconFlow: - return new ClientApi(ModelProvider.SiliconFlow); - case ServiceProvider["302.AI"]: - return new ClientApi(ModelProvider["302.AI"]); - default: - return new ClientApi(ModelProvider.GPT); - } -} diff --git a/app/client/controller.ts b/app/client/controller.ts deleted file mode 100644 index a2e00173dd0..00000000000 --- a/app/client/controller.ts +++ /dev/null @@ -1,37 +0,0 @@ -// To store message streaming controller -export const ChatControllerPool = { - controllers: {} as Record, - - addController( - sessionId: string, - messageId: string, - controller: AbortController, - ) { - const key = this.key(sessionId, messageId); - this.controllers[key] = controller; - return key; - }, - - stop(sessionId: string, messageId: string) { - const key = this.key(sessionId, messageId); - const controller = this.controllers[key]; - controller?.abort(); - }, - - stopAll() { - Object.values(this.controllers).forEach((v) => v.abort()); - }, - - hasPending() { - return Object.values(this.controllers).length > 0; - }, - - remove(sessionId: string, messageId: string) { - const key = this.key(sessionId, messageId); - delete this.controllers[key]; - }, - - key(sessionId: string, messageIndex: string) { - return `${sessionId},${messageIndex}`; - }, -}; diff --git a/app/client/platforms/ai302.ts b/app/client/platforms/ai302.ts deleted file mode 100644 index 7bd18f96d4c..00000000000 --- a/app/client/platforms/ai302.ts +++ /dev/null @@ -1,287 +0,0 @@ -"use client"; - -import { - ApiPath, - AI302_BASE_URL, - DEFAULT_MODELS, - AI302, -} from "@/app/constant"; -import { - useAccessStore, - useAppConfig, - useChatStore, - ChatMessageTool, - usePluginStore, -} from "@/app/store"; -import { preProcessImageContent, streamWithThink } from "@/app/utils/chat"; -import { - ChatOptions, - getHeaders, - LLMApi, - LLMModel, - SpeechOptions, -} from "../api"; -import { getClientConfig } from "@/app/config/client"; -import { - getMessageTextContent, - getMessageTextContentWithoutThinking, - isVisionModel, - getTimeoutMSByModel, -} from "@/app/utils"; -import { RequestPayload } from "./openai"; - -import { fetch } from "@/app/utils/stream"; -export interface Ai302ListModelResponse { - object: string; - data: Array<{ - id: string; - object: string; - root: string; - }>; -} - -export class Ai302Api implements LLMApi { - private disableListModels = false; - - path(path: string): string { - const accessStore = useAccessStore.getState(); - - let baseUrl = ""; - - if (accessStore.useCustomConfig) { - baseUrl = accessStore.ai302Url; - } - - if (baseUrl.length === 0) { - const isApp = !!getClientConfig()?.isApp; - const apiPath = ApiPath["302.AI"]; - baseUrl = isApp ? AI302_BASE_URL : apiPath; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, baseUrl.length - 1); - } - if ( - !baseUrl.startsWith("http") && - !baseUrl.startsWith(ApiPath["302.AI"]) - ) { - baseUrl = "https://" + baseUrl; - } - - console.log("[Proxy Endpoint] ", baseUrl, path); - - return [baseUrl, path].join("/"); - } - - extractMessage(res: any) { - return res.choices?.at(0)?.message?.content ?? ""; - } - - speech(options: SpeechOptions): Promise { - throw new Error("Method not implemented."); - } - - async chat(options: ChatOptions) { - const visionModel = isVisionModel(options.config.model); - const messages: ChatOptions["messages"] = []; - for (const v of options.messages) { - if (v.role === "assistant") { - const content = getMessageTextContentWithoutThinking(v); - messages.push({ role: v.role, content }); - } else { - const content = visionModel - ? await preProcessImageContent(v.content) - : getMessageTextContent(v); - messages.push({ role: v.role, content }); - } - } - - const modelConfig = { - ...useAppConfig.getState().modelConfig, - ...useChatStore.getState().currentSession().mask.modelConfig, - ...{ - model: options.config.model, - providerName: options.config.providerName, - }, - }; - - const requestPayload: RequestPayload = { - messages, - stream: options.config.stream, - model: modelConfig.model, - temperature: modelConfig.temperature, - presence_penalty: modelConfig.presence_penalty, - frequency_penalty: modelConfig.frequency_penalty, - top_p: modelConfig.top_p, - // max_tokens: Math.max(modelConfig.max_tokens, 1024), - // Please do not ask me why not send max_tokens, no reason, this param is just shit, I dont want to explain anymore. - }; - - console.log("[Request] openai payload: ", requestPayload); - - const shouldStream = !!options.config.stream; - const controller = new AbortController(); - options.onController?.(controller); - - try { - const chatPath = this.path(AI302.ChatPath); - const chatPayload = { - method: "POST", - body: JSON.stringify(requestPayload), - signal: controller.signal, - headers: getHeaders(), - }; - - // console.log(chatPayload); - - // Use extended timeout for thinking models as they typically require more processing time - const requestTimeoutId = setTimeout( - () => controller.abort(), - getTimeoutMSByModel(options.config.model), - ); - - if (shouldStream) { - const [tools, funcs] = usePluginStore - .getState() - .getAsTools( - useChatStore.getState().currentSession().mask?.plugin || [], - ); - return streamWithThink( - chatPath, - requestPayload, - getHeaders(), - tools as any, - funcs, - controller, - // parseSSE - (text: string, runTools: ChatMessageTool[]) => { - // console.log("parseSSE", text, runTools); - const json = JSON.parse(text); - const choices = json.choices as Array<{ - delta: { - content: string | null; - tool_calls: ChatMessageTool[]; - reasoning_content: string | null; - }; - }>; - const tool_calls = choices[0]?.delta?.tool_calls; - if (tool_calls?.length > 0) { - const index = tool_calls[0]?.index; - const id = tool_calls[0]?.id; - const args = tool_calls[0]?.function?.arguments; - if (id) { - runTools.push({ - id, - type: tool_calls[0]?.type, - function: { - name: tool_calls[0]?.function?.name as string, - arguments: args, - }, - }); - } else { - // @ts-ignore - runTools[index]["function"]["arguments"] += args; - } - } - const reasoning = choices[0]?.delta?.reasoning_content; - const content = choices[0]?.delta?.content; - - // Skip if both content and reasoning_content are empty or null - if ( - (!reasoning || reasoning.length === 0) && - (!content || content.length === 0) - ) { - return { - isThinking: false, - content: "", - }; - } - - if (reasoning && reasoning.length > 0) { - return { - isThinking: true, - content: reasoning, - }; - } else if (content && content.length > 0) { - return { - isThinking: false, - content: content, - }; - } - - return { - isThinking: false, - content: "", - }; - }, - // processToolMessage, include tool_calls message and tool call results - ( - requestPayload: RequestPayload, - toolCallMessage: any, - toolCallResult: any[], - ) => { - // @ts-ignore - requestPayload?.messages?.splice( - // @ts-ignore - requestPayload?.messages?.length, - 0, - toolCallMessage, - ...toolCallResult, - ); - }, - options, - ); - } else { - const res = await fetch(chatPath, chatPayload); - clearTimeout(requestTimeoutId); - - const resJson = await res.json(); - const message = this.extractMessage(resJson); - options.onFinish(message, res); - } - } catch (e) { - console.log("[Request] failed to make a chat request", e); - options.onError?.(e as Error); - } - } - async usage() { - return { - used: 0, - total: 0, - }; - } - - async models(): Promise { - if (this.disableListModels) { - return DEFAULT_MODELS.slice(); - } - - const res = await fetch(this.path(AI302.ListModelPath), { - method: "GET", - headers: { - ...getHeaders(), - }, - }); - - const resJson = (await res.json()) as Ai302ListModelResponse; - const chatModels = resJson.data; - console.log("[Models]", chatModels); - - if (!chatModels) { - return []; - } - - let seq = 1000; //同 Constant.ts 中的排序保持一致 - return chatModels.map((m) => ({ - name: m.id, - available: true, - sorted: seq++, - provider: { - id: "ai302", - providerName: "302.AI", - providerType: "ai302", - sorted: 15, - }, - })); - } -} diff --git a/app/client/platforms/alibaba.ts b/app/client/platforms/alibaba.ts deleted file mode 100644 index 4875e5c02d9..00000000000 --- a/app/client/platforms/alibaba.ts +++ /dev/null @@ -1,277 +0,0 @@ -"use client"; -import { ApiPath, Alibaba, ALIBABA_BASE_URL } from "@/app/constant"; -import { - useAccessStore, - useAppConfig, - useChatStore, - ChatMessageTool, - usePluginStore, -} from "@/app/store"; -import { - preProcessImageContentForAlibabaDashScope, - streamWithThink, -} from "@/app/utils/chat"; -import { - ChatOptions, - getHeaders, - LLMApi, - LLMModel, - SpeechOptions, - MultimodalContent, - MultimodalContentForAlibaba, -} from "../api"; -import { getClientConfig } from "@/app/config/client"; -import { - getMessageTextContent, - getMessageTextContentWithoutThinking, - getTimeoutMSByModel, - isVisionModel, -} from "@/app/utils"; -import { fetch } from "@/app/utils/stream"; - -export interface OpenAIListModelResponse { - object: string; - data: Array<{ - id: string; - object: string; - root: string; - }>; -} - -interface RequestInput { - messages: { - role: "system" | "user" | "assistant"; - content: string | MultimodalContent[]; - }[]; -} -interface RequestParam { - result_format: string; - incremental_output?: boolean; - temperature: number; - repetition_penalty?: number; - top_p: number; - max_tokens?: number; -} -interface RequestPayload { - model: string; - input: RequestInput; - parameters: RequestParam; -} - -export class QwenApi implements LLMApi { - path(path: string): string { - const accessStore = useAccessStore.getState(); - - let baseUrl = ""; - - if (accessStore.useCustomConfig) { - baseUrl = accessStore.alibabaUrl; - } - - if (baseUrl.length === 0) { - const isApp = !!getClientConfig()?.isApp; - baseUrl = isApp ? ALIBABA_BASE_URL : ApiPath.Alibaba; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, baseUrl.length - 1); - } - if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.Alibaba)) { - baseUrl = "https://" + baseUrl; - } - - console.log("[Proxy Endpoint] ", baseUrl, path); - - return [baseUrl, path].join("/"); - } - - extractMessage(res: any) { - return res?.output?.choices?.at(0)?.message?.content ?? ""; - } - - speech(options: SpeechOptions): Promise { - throw new Error("Method not implemented."); - } - - async chat(options: ChatOptions) { - const modelConfig = { - ...useAppConfig.getState().modelConfig, - ...useChatStore.getState().currentSession().mask.modelConfig, - ...{ - model: options.config.model, - }, - }; - - const visionModel = isVisionModel(options.config.model); - - const messages: ChatOptions["messages"] = []; - for (const v of options.messages) { - const content = ( - visionModel - ? await preProcessImageContentForAlibabaDashScope(v.content) - : v.role === "assistant" - ? getMessageTextContentWithoutThinking(v) - : getMessageTextContent(v) - ) as any; - - messages.push({ role: v.role, content }); - } - - const shouldStream = !!options.config.stream; - const requestPayload: RequestPayload = { - model: modelConfig.model, - input: { - messages, - }, - parameters: { - result_format: "message", - incremental_output: shouldStream, - temperature: modelConfig.temperature, - // max_tokens: modelConfig.max_tokens, - top_p: modelConfig.top_p === 1 ? 0.99 : modelConfig.top_p, // qwen top_p is should be < 1 - }, - }; - - const controller = new AbortController(); - options.onController?.(controller); - - try { - const headers = { - ...getHeaders(), - "X-DashScope-SSE": shouldStream ? "enable" : "disable", - }; - - const chatPath = this.path(Alibaba.ChatPath(modelConfig.model)); - const chatPayload = { - method: "POST", - body: JSON.stringify(requestPayload), - signal: controller.signal, - headers: headers, - }; - - // make a fetch request - const requestTimeoutId = setTimeout( - () => controller.abort(), - getTimeoutMSByModel(options.config.model), - ); - - if (shouldStream) { - const [tools, funcs] = usePluginStore - .getState() - .getAsTools( - useChatStore.getState().currentSession().mask?.plugin || [], - ); - return streamWithThink( - chatPath, - requestPayload, - headers, - tools as any, - funcs, - controller, - // parseSSE - (text: string, runTools: ChatMessageTool[]) => { - // console.log("parseSSE", text, runTools); - const json = JSON.parse(text); - const choices = json.output.choices as Array<{ - message: { - content: string | null | MultimodalContentForAlibaba[]; - tool_calls: ChatMessageTool[]; - reasoning_content: string | null; - }; - }>; - - if (!choices?.length) return { isThinking: false, content: "" }; - - const tool_calls = choices[0]?.message?.tool_calls; - if (tool_calls?.length > 0) { - const index = tool_calls[0]?.index; - const id = tool_calls[0]?.id; - const args = tool_calls[0]?.function?.arguments; - if (id) { - runTools.push({ - id, - type: tool_calls[0]?.type, - function: { - name: tool_calls[0]?.function?.name as string, - arguments: args, - }, - }); - } else { - // @ts-ignore - runTools[index]["function"]["arguments"] += args; - } - } - - const reasoning = choices[0]?.message?.reasoning_content; - const content = choices[0]?.message?.content; - - // Skip if both content and reasoning_content are empty or null - if ( - (!reasoning || reasoning.length === 0) && - (!content || content.length === 0) - ) { - return { - isThinking: false, - content: "", - }; - } - - if (reasoning && reasoning.length > 0) { - return { - isThinking: true, - content: reasoning, - }; - } else if (content && content.length > 0) { - return { - isThinking: false, - content: Array.isArray(content) - ? content.map((item) => item.text).join(",") - : content, - }; - } - - return { - isThinking: false, - content: "", - }; - }, - // processToolMessage, include tool_calls message and tool call results - ( - requestPayload: RequestPayload, - toolCallMessage: any, - toolCallResult: any[], - ) => { - requestPayload?.input?.messages?.splice( - requestPayload?.input?.messages?.length, - 0, - toolCallMessage, - ...toolCallResult, - ); - }, - options, - ); - } else { - const res = await fetch(chatPath, chatPayload); - clearTimeout(requestTimeoutId); - - const resJson = await res.json(); - const message = this.extractMessage(resJson); - options.onFinish(message, res); - } - } catch (e) { - console.log("[Request] failed to make a chat request", e); - options.onError?.(e as Error); - } - } - async usage() { - return { - used: 0, - total: 0, - }; - } - - async models(): Promise { - return []; - } -} -export { Alibaba }; diff --git a/app/client/platforms/anthropic.ts b/app/client/platforms/anthropic.ts deleted file mode 100644 index 2377470904f..00000000000 --- a/app/client/platforms/anthropic.ts +++ /dev/null @@ -1,424 +0,0 @@ -import { Anthropic, ApiPath } from "@/app/constant"; -import { ChatOptions, getHeaders, LLMApi, SpeechOptions } from "../api"; -import { - useAccessStore, - useAppConfig, - useChatStore, - usePluginStore, - ChatMessageTool, -} from "@/app/store"; -import { getClientConfig } from "@/app/config/client"; -import { ANTHROPIC_BASE_URL } from "@/app/constant"; -import { getMessageTextContent, isVisionModel } from "@/app/utils"; -import { preProcessImageContent, stream } from "@/app/utils/chat"; -import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare"; -import { RequestPayload } from "./openai"; -import { fetch } from "@/app/utils/stream"; - -export type MultiBlockContent = { - type: "image" | "text"; - source?: { - type: string; - media_type: string; - data: string; - }; - text?: string; -}; - -export type AnthropicMessage = { - role: (typeof ClaudeMapper)[keyof typeof ClaudeMapper]; - content: string | MultiBlockContent[]; -}; - -export interface AnthropicChatRequest { - model: string; // The model that will complete your prompt. - messages: AnthropicMessage[]; // The prompt that you want Claude to complete. - max_tokens: number; // The maximum number of tokens to generate before stopping. - stop_sequences?: string[]; // Sequences that will cause the model to stop generating completion text. - temperature?: number; // Amount of randomness injected into the response. - top_p?: number; // Use nucleus sampling. - top_k?: number; // Only sample from the top K options for each subsequent token. - metadata?: object; // An object describing metadata about the request. - stream?: boolean; // Whether to incrementally stream the response using server-sent events. -} - -export interface ChatRequest { - model: string; // The model that will complete your prompt. - prompt: string; // The prompt that you want Claude to complete. - max_tokens_to_sample: number; // The maximum number of tokens to generate before stopping. - stop_sequences?: string[]; // Sequences that will cause the model to stop generating completion text. - temperature?: number; // Amount of randomness injected into the response. - top_p?: number; // Use nucleus sampling. - top_k?: number; // Only sample from the top K options for each subsequent token. - metadata?: object; // An object describing metadata about the request. - stream?: boolean; // Whether to incrementally stream the response using server-sent events. -} - -export interface ChatResponse { - completion: string; - stop_reason: "stop_sequence" | "max_tokens"; - model: string; -} - -export type ChatStreamResponse = ChatResponse & { - stop?: string; - log_id: string; -}; - -const ClaudeMapper = { - assistant: "assistant", - user: "user", - system: "user", -} as const; - -const keys = ["claude-2, claude-instant-1"]; - -export class ClaudeApi implements LLMApi { - speech(options: SpeechOptions): Promise { - throw new Error("Method not implemented."); - } - - extractMessage(res: any) { - console.log("[Response] claude response: ", res); - - return res?.content?.[0]?.text; - } - async chat(options: ChatOptions): Promise { - const visionModel = isVisionModel(options.config.model); - - const accessStore = useAccessStore.getState(); - - const shouldStream = !!options.config.stream; - - const modelConfig = { - ...useAppConfig.getState().modelConfig, - ...useChatStore.getState().currentSession().mask.modelConfig, - ...{ - model: options.config.model, - }, - }; - - // try get base64image from local cache image_url - const messages: ChatOptions["messages"] = []; - for (const v of options.messages) { - const content = await preProcessImageContent(v.content); - messages.push({ role: v.role, content }); - } - - const keys = ["system", "user"]; - - // roles must alternate between "user" and "assistant" in claude, so add a fake assistant message between two user messages - for (let i = 0; i < messages.length - 1; i++) { - const message = messages[i]; - const nextMessage = messages[i + 1]; - - if (keys.includes(message.role) && keys.includes(nextMessage.role)) { - messages[i] = [ - message, - { - role: "assistant", - content: ";", - }, - ] as any; - } - } - - const prompt = messages - .flat() - .filter((v) => { - if (!v.content) return false; - if (typeof v.content === "string" && !v.content.trim()) return false; - return true; - }) - .map((v) => { - const { role, content } = v; - const insideRole = ClaudeMapper[role] ?? "user"; - - if (!visionModel || typeof content === "string") { - return { - role: insideRole, - content: getMessageTextContent(v), - }; - } - return { - role: insideRole, - content: content - .filter((v) => v.image_url || v.text) - .map(({ type, text, image_url }) => { - if (type === "text") { - return { - type, - text: text!, - }; - } - const { url = "" } = image_url || {}; - const colonIndex = url.indexOf(":"); - const semicolonIndex = url.indexOf(";"); - const comma = url.indexOf(","); - - const mimeType = url.slice(colonIndex + 1, semicolonIndex); - const encodeType = url.slice(semicolonIndex + 1, comma); - const data = url.slice(comma + 1); - - return { - type: "image" as const, - source: { - type: encodeType, - media_type: mimeType, - data, - }, - }; - }), - }; - }); - - if (prompt[0]?.role === "assistant") { - prompt.unshift({ - role: "user", - content: ";", - }); - } - - const requestBody: AnthropicChatRequest = { - messages: prompt, - stream: shouldStream, - - model: modelConfig.model, - max_tokens: modelConfig.max_tokens, - temperature: modelConfig.temperature, - top_p: modelConfig.top_p, - // top_k: modelConfig.top_k, - top_k: 5, - }; - - const path = this.path(Anthropic.ChatPath); - - const controller = new AbortController(); - options.onController?.(controller); - - if (shouldStream) { - let index = -1; - const [tools, funcs] = usePluginStore - .getState() - .getAsTools( - useChatStore.getState().currentSession().mask?.plugin || [], - ); - return stream( - path, - requestBody, - { - ...getHeaders(), - "anthropic-version": accessStore.anthropicApiVersion, - }, - // @ts-ignore - tools.map((tool) => ({ - name: tool?.function?.name, - description: tool?.function?.description, - input_schema: tool?.function?.parameters, - })), - funcs, - controller, - // parseSSE - (text: string, runTools: ChatMessageTool[]) => { - // console.log("parseSSE", text, runTools); - let chunkJson: - | undefined - | { - type: "content_block_delta" | "content_block_stop" | "message_delta" | "message_stop"; - content_block?: { - type: "tool_use"; - id: string; - name: string; - }; - delta?: { - type: "text_delta" | "input_json_delta"; - text?: string; - partial_json?: string; - stop_reason?: string; - }; - index: number; - }; - chunkJson = JSON.parse(text); - - // Handle refusal stop reason in message_delta - if (chunkJson?.delta?.stop_reason === "refusal") { - // Return a message to display to the user - const refusalMessage = "\n\n[Assistant refused to respond. Please modify your request and try again.]"; - options.onError?.(new Error("Content policy violation: " + refusalMessage)); - return refusalMessage; - } - - if (chunkJson?.content_block?.type == "tool_use") { - index += 1; - const id = chunkJson?.content_block.id; - const name = chunkJson?.content_block.name; - runTools.push({ - id, - type: "function", - function: { - name, - arguments: "", - }, - }); - } - if ( - chunkJson?.delta?.type == "input_json_delta" && - chunkJson?.delta?.partial_json - ) { - // @ts-ignore - runTools[index]["function"]["arguments"] += - chunkJson?.delta?.partial_json; - } - return chunkJson?.delta?.text; - }, - // processToolMessage, include tool_calls message and tool call results - ( - requestPayload: RequestPayload, - toolCallMessage: any, - toolCallResult: any[], - ) => { - // reset index value - index = -1; - // @ts-ignore - requestPayload?.messages?.splice( - // @ts-ignore - requestPayload?.messages?.length, - 0, - { - role: "assistant", - content: toolCallMessage.tool_calls.map( - (tool: ChatMessageTool) => ({ - type: "tool_use", - id: tool.id, - name: tool?.function?.name, - input: tool?.function?.arguments - ? JSON.parse(tool?.function?.arguments) - : {}, - }), - ), - }, - // @ts-ignore - ...toolCallResult.map((result) => ({ - role: "user", - content: [ - { - type: "tool_result", - tool_use_id: result.tool_call_id, - content: result.content, - }, - ], - })), - ); - }, - options, - ); - } else { - const payload = { - method: "POST", - body: JSON.stringify(requestBody), - signal: controller.signal, - headers: { - ...getHeaders(), // get common headers - "anthropic-version": accessStore.anthropicApiVersion, - // do not send `anthropicApiKey` in browser!!! - // Authorization: getAuthKey(accessStore.anthropicApiKey), - }, - }; - - try { - controller.signal.onabort = () => - options.onFinish("", new Response(null, { status: 400 })); - - const res = await fetch(path, payload); - const resJson = await res.json(); - - const message = this.extractMessage(resJson); - options.onFinish(message, res); - } catch (e) { - console.error("failed to chat", e); - options.onError?.(e as Error); - } - } - } - async usage() { - return { - used: 0, - total: 0, - }; - } - async models() { - // const provider = { - // id: "anthropic", - // providerName: "Anthropic", - // providerType: "anthropic", - // }; - - return [ - // { - // name: "claude-instant-1.2", - // available: true, - // provider, - // }, - // { - // name: "claude-2.0", - // available: true, - // provider, - // }, - // { - // name: "claude-2.1", - // available: true, - // provider, - // }, - // { - // name: "claude-3-opus-20240229", - // available: true, - // provider, - // }, - // { - // name: "claude-3-sonnet-20240229", - // available: true, - // provider, - // }, - // { - // name: "claude-3-haiku-20240307", - // available: true, - // provider, - // }, - ]; - } - path(path: string): string { - const accessStore = useAccessStore.getState(); - - let baseUrl: string = ""; - - if (accessStore.useCustomConfig) { - baseUrl = accessStore.anthropicUrl; - } - - // if endpoint is empty, use default endpoint - if (baseUrl.trim().length === 0) { - const isApp = !!getClientConfig()?.isApp; - - baseUrl = isApp ? ANTHROPIC_BASE_URL : ApiPath.Anthropic; - } - - if (!baseUrl.startsWith("http") && !baseUrl.startsWith("/api")) { - baseUrl = "https://" + baseUrl; - } - - baseUrl = trimEnd(baseUrl, "/"); - - // try rebuild url, when using cloudflare ai gateway in client - return cloudflareAIGatewayUrl(`${baseUrl}/${path}`); - } -} - -function trimEnd(s: string, end = " ") { - if (end.length === 0) return s; - - while (s.endsWith(end)) { - s = s.slice(0, -end.length); - } - - return s; -} diff --git a/app/client/platforms/baidu.ts b/app/client/platforms/baidu.ts deleted file mode 100644 index dc990db4103..00000000000 --- a/app/client/platforms/baidu.ts +++ /dev/null @@ -1,284 +0,0 @@ -"use client"; -import { ApiPath, Baidu, BAIDU_BASE_URL } from "@/app/constant"; -import { useAccessStore, useAppConfig, useChatStore } from "@/app/store"; -import { getAccessToken } from "@/app/utils/baidu"; - -import { - ChatOptions, - getHeaders, - LLMApi, - LLMModel, - MultimodalContent, - SpeechOptions, -} from "../api"; -import Locale from "../../locales"; -import { - EventStreamContentType, - fetchEventSource, -} from "@fortaine/fetch-event-source"; -import { prettyObject } from "@/app/utils/format"; -import { getClientConfig } from "@/app/config/client"; -import { getMessageTextContent, getTimeoutMSByModel } from "@/app/utils"; -import { fetch } from "@/app/utils/stream"; - -export interface OpenAIListModelResponse { - object: string; - data: Array<{ - id: string; - object: string; - root: string; - }>; -} - -interface RequestPayload { - messages: { - role: "system" | "user" | "assistant"; - content: string | MultimodalContent[]; - }[]; - stream?: boolean; - model: string; - temperature: number; - presence_penalty: number; - frequency_penalty: number; - top_p: number; - max_tokens?: number; -} - -export class ErnieApi implements LLMApi { - path(path: string): string { - const accessStore = useAccessStore.getState(); - - let baseUrl = ""; - - if (accessStore.useCustomConfig) { - baseUrl = accessStore.baiduUrl; - } - - if (baseUrl.length === 0) { - const isApp = !!getClientConfig()?.isApp; - // do not use proxy for baidubce api - baseUrl = isApp ? BAIDU_BASE_URL : ApiPath.Baidu; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, baseUrl.length - 1); - } - if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.Baidu)) { - baseUrl = "https://" + baseUrl; - } - - console.log("[Proxy Endpoint] ", baseUrl, path); - - return [baseUrl, path].join("/"); - } - - speech(options: SpeechOptions): Promise { - throw new Error("Method not implemented."); - } - - async chat(options: ChatOptions) { - const messages = options.messages.map((v) => ({ - // "error_code": 336006, "error_msg": "the role of message with even index in the messages must be user or function", - role: v.role === "system" ? "user" : v.role, - content: getMessageTextContent(v), - })); - - // "error_code": 336006, "error_msg": "the length of messages must be an odd number", - if (messages.length % 2 === 0) { - if (messages.at(0)?.role === "user") { - messages.splice(1, 0, { - role: "assistant", - content: " ", - }); - } else { - messages.unshift({ - role: "user", - content: " ", - }); - } - } - - const modelConfig = { - ...useAppConfig.getState().modelConfig, - ...useChatStore.getState().currentSession().mask.modelConfig, - ...{ - model: options.config.model, - }, - }; - - const shouldStream = !!options.config.stream; - const requestPayload: RequestPayload = { - messages, - stream: shouldStream, - model: modelConfig.model, - temperature: modelConfig.temperature, - presence_penalty: modelConfig.presence_penalty, - frequency_penalty: modelConfig.frequency_penalty, - top_p: modelConfig.top_p, - }; - - console.log("[Request] Baidu payload: ", requestPayload); - - const controller = new AbortController(); - options.onController?.(controller); - - try { - let chatPath = this.path(Baidu.ChatPath(modelConfig.model)); - - // getAccessToken can not run in browser, because cors error - if (!!getClientConfig()?.isApp) { - const accessStore = useAccessStore.getState(); - if (accessStore.useCustomConfig) { - if (accessStore.isValidBaidu()) { - const { access_token } = await getAccessToken( - accessStore.baiduApiKey, - accessStore.baiduSecretKey, - ); - chatPath = `${chatPath}${ - chatPath.includes("?") ? "&" : "?" - }access_token=${access_token}`; - } - } - } - const chatPayload = { - method: "POST", - body: JSON.stringify(requestPayload), - signal: controller.signal, - headers: getHeaders(), - }; - - // make a fetch request - const requestTimeoutId = setTimeout( - () => controller.abort(), - getTimeoutMSByModel(options.config.model), - ); - - if (shouldStream) { - let responseText = ""; - let remainText = ""; - let finished = false; - let responseRes: Response; - - // animate response to make it looks smooth - function animateResponseText() { - if (finished || controller.signal.aborted) { - responseText += remainText; - console.log("[Response Animation] finished"); - if (responseText?.length === 0) { - options.onError?.(new Error("empty response from server")); - } - return; - } - - if (remainText.length > 0) { - const fetchCount = Math.max(1, Math.round(remainText.length / 60)); - const fetchText = remainText.slice(0, fetchCount); - responseText += fetchText; - remainText = remainText.slice(fetchCount); - options.onUpdate?.(responseText, fetchText); - } - - requestAnimationFrame(animateResponseText); - } - - // start animaion - animateResponseText(); - - const finish = () => { - if (!finished) { - finished = true; - options.onFinish(responseText + remainText, responseRes); - } - }; - - controller.signal.onabort = finish; - - fetchEventSource(chatPath, { - fetch: fetch as any, - ...chatPayload, - async onopen(res) { - clearTimeout(requestTimeoutId); - const contentType = res.headers.get("content-type"); - console.log("[Baidu] request response content type: ", contentType); - responseRes = res; - if (contentType?.startsWith("text/plain")) { - responseText = await res.clone().text(); - return finish(); - } - - if ( - !res.ok || - !res.headers - .get("content-type") - ?.startsWith(EventStreamContentType) || - res.status !== 200 - ) { - const responseTexts = [responseText]; - let extraInfo = await res.clone().text(); - try { - const resJson = await res.clone().json(); - extraInfo = prettyObject(resJson); - } catch {} - - if (res.status === 401) { - responseTexts.push(Locale.Error.Unauthorized); - } - - if (extraInfo) { - responseTexts.push(extraInfo); - } - - responseText = responseTexts.join("\n\n"); - - return finish(); - } - }, - onmessage(msg) { - if (msg.data === "[DONE]" || finished) { - return finish(); - } - const text = msg.data; - try { - const json = JSON.parse(text); - const delta = json?.result; - if (delta) { - remainText += delta; - } - } catch (e) { - console.error("[Request] parse error", text, msg); - } - }, - onclose() { - finish(); - }, - onerror(e) { - options.onError?.(e); - throw e; - }, - openWhenHidden: true, - }); - } else { - const res = await fetch(chatPath, chatPayload); - clearTimeout(requestTimeoutId); - - const resJson = await res.json(); - const message = resJson?.result; - options.onFinish(message, res); - } - } catch (e) { - console.log("[Request] failed to make a chat request", e); - options.onError?.(e as Error); - } - } - async usage() { - return { - used: 0, - total: 0, - }; - } - - async models(): Promise { - return []; - } -} -export { Baidu }; diff --git a/app/client/platforms/bytedance.ts b/app/client/platforms/bytedance.ts deleted file mode 100644 index f9524cba28d..00000000000 --- a/app/client/platforms/bytedance.ts +++ /dev/null @@ -1,250 +0,0 @@ -"use client"; -import { ApiPath, ByteDance, BYTEDANCE_BASE_URL } from "@/app/constant"; -import { - useAccessStore, - useAppConfig, - useChatStore, - ChatMessageTool, - usePluginStore, -} from "@/app/store"; - -import { - ChatOptions, - getHeaders, - LLMApi, - LLMModel, - MultimodalContent, - SpeechOptions, -} from "../api"; - -import { streamWithThink } from "@/app/utils/chat"; -import { getClientConfig } from "@/app/config/client"; -import { preProcessImageContent } from "@/app/utils/chat"; -import { - getMessageTextContentWithoutThinking, - getTimeoutMSByModel, -} from "@/app/utils"; -import { fetch } from "@/app/utils/stream"; - -export interface OpenAIListModelResponse { - object: string; - data: Array<{ - id: string; - object: string; - root: string; - }>; -} - -interface RequestPayloadForByteDance { - messages: { - role: "system" | "user" | "assistant"; - content: string | MultimodalContent[]; - }[]; - stream?: boolean; - model: string; - temperature: number; - presence_penalty: number; - frequency_penalty: number; - top_p: number; - max_tokens?: number; -} - -export class DoubaoApi implements LLMApi { - path(path: string): string { - const accessStore = useAccessStore.getState(); - - let baseUrl = ""; - - if (accessStore.useCustomConfig) { - baseUrl = accessStore.bytedanceUrl; - } - - if (baseUrl.length === 0) { - const isApp = !!getClientConfig()?.isApp; - baseUrl = isApp ? BYTEDANCE_BASE_URL : ApiPath.ByteDance; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, baseUrl.length - 1); - } - if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.ByteDance)) { - baseUrl = "https://" + baseUrl; - } - - console.log("[Proxy Endpoint] ", baseUrl, path); - - return [baseUrl, path].join("/"); - } - - extractMessage(res: any) { - return res.choices?.at(0)?.message?.content ?? ""; - } - - speech(options: SpeechOptions): Promise { - throw new Error("Method not implemented."); - } - - async chat(options: ChatOptions) { - const messages: ChatOptions["messages"] = []; - for (const v of options.messages) { - const content = - v.role === "assistant" - ? getMessageTextContentWithoutThinking(v) - : await preProcessImageContent(v.content); - messages.push({ role: v.role, content }); - } - - const modelConfig = { - ...useAppConfig.getState().modelConfig, - ...useChatStore.getState().currentSession().mask.modelConfig, - ...{ - model: options.config.model, - }, - }; - - const shouldStream = !!options.config.stream; - const requestPayload: RequestPayloadForByteDance = { - messages, - stream: shouldStream, - model: modelConfig.model, - temperature: modelConfig.temperature, - presence_penalty: modelConfig.presence_penalty, - frequency_penalty: modelConfig.frequency_penalty, - top_p: modelConfig.top_p, - }; - - const controller = new AbortController(); - options.onController?.(controller); - - try { - const chatPath = this.path(ByteDance.ChatPath); - const chatPayload = { - method: "POST", - body: JSON.stringify(requestPayload), - signal: controller.signal, - headers: getHeaders(), - }; - - // make a fetch request - const requestTimeoutId = setTimeout( - () => controller.abort(), - getTimeoutMSByModel(options.config.model), - ); - - if (shouldStream) { - const [tools, funcs] = usePluginStore - .getState() - .getAsTools( - useChatStore.getState().currentSession().mask?.plugin || [], - ); - return streamWithThink( - chatPath, - requestPayload, - getHeaders(), - tools as any, - funcs, - controller, - // parseSSE - (text: string, runTools: ChatMessageTool[]) => { - // console.log("parseSSE", text, runTools); - const json = JSON.parse(text); - const choices = json.choices as Array<{ - delta: { - content: string | null; - tool_calls: ChatMessageTool[]; - reasoning_content: string | null; - }; - }>; - - if (!choices?.length) return { isThinking: false, content: "" }; - - const tool_calls = choices[0]?.delta?.tool_calls; - if (tool_calls?.length > 0) { - const index = tool_calls[0]?.index; - const id = tool_calls[0]?.id; - const args = tool_calls[0]?.function?.arguments; - if (id) { - runTools.push({ - id, - type: tool_calls[0]?.type, - function: { - name: tool_calls[0]?.function?.name as string, - arguments: args, - }, - }); - } else { - // @ts-ignore - runTools[index]["function"]["arguments"] += args; - } - } - const reasoning = choices[0]?.delta?.reasoning_content; - const content = choices[0]?.delta?.content; - - // Skip if both content and reasoning_content are empty or null - if ( - (!reasoning || reasoning.length === 0) && - (!content || content.length === 0) - ) { - return { - isThinking: false, - content: "", - }; - } - - if (reasoning && reasoning.length > 0) { - return { - isThinking: true, - content: reasoning, - }; - } else if (content && content.length > 0) { - return { - isThinking: false, - content: content, - }; - } - - return { - isThinking: false, - content: "", - }; - }, - // processToolMessage, include tool_calls message and tool call results - ( - requestPayload: RequestPayloadForByteDance, - toolCallMessage: any, - toolCallResult: any[], - ) => { - requestPayload?.messages?.splice( - requestPayload?.messages?.length, - 0, - toolCallMessage, - ...toolCallResult, - ); - }, - options, - ); - } else { - const res = await fetch(chatPath, chatPayload); - clearTimeout(requestTimeoutId); - - const resJson = await res.json(); - const message = this.extractMessage(resJson); - options.onFinish(message, res); - } - } catch (e) { - console.log("[Request] failed to make a chat request", e); - options.onError?.(e as Error); - } - } - async usage() { - return { - used: 0, - total: 0, - }; - } - - async models(): Promise { - return []; - } -} -export { ByteDance }; diff --git a/app/client/platforms/deepseek.ts b/app/client/platforms/deepseek.ts deleted file mode 100644 index 1b38b40cc64..00000000000 --- a/app/client/platforms/deepseek.ts +++ /dev/null @@ -1,253 +0,0 @@ -"use client"; -// azure and openai, using same models. so using same LLMApi. -import { ApiPath, DEEPSEEK_BASE_URL, DeepSeek } from "@/app/constant"; -import { - useAccessStore, - useAppConfig, - useChatStore, - ChatMessageTool, - usePluginStore, -} from "@/app/store"; -import { streamWithThink } from "@/app/utils/chat"; -import { - ChatOptions, - getHeaders, - LLMApi, - LLMModel, - SpeechOptions, -} from "../api"; -import { getClientConfig } from "@/app/config/client"; -import { - getMessageTextContent, - getMessageTextContentWithoutThinking, - getTimeoutMSByModel, -} from "@/app/utils"; -import { RequestPayload } from "./openai"; -import { fetch } from "@/app/utils/stream"; - -export class DeepSeekApi implements LLMApi { - private disableListModels = true; - - path(path: string): string { - const accessStore = useAccessStore.getState(); - - let baseUrl = ""; - - if (accessStore.useCustomConfig) { - baseUrl = accessStore.deepseekUrl; - } - - if (baseUrl.length === 0) { - const isApp = !!getClientConfig()?.isApp; - const apiPath = ApiPath.DeepSeek; - baseUrl = isApp ? DEEPSEEK_BASE_URL : apiPath; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, baseUrl.length - 1); - } - if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.DeepSeek)) { - baseUrl = "https://" + baseUrl; - } - - console.log("[Proxy Endpoint] ", baseUrl, path); - - return [baseUrl, path].join("/"); - } - - extractMessage(res: any) { - return res.choices?.at(0)?.message?.content ?? ""; - } - - speech(options: SpeechOptions): Promise { - throw new Error("Method not implemented."); - } - - async chat(options: ChatOptions) { - const messages: ChatOptions["messages"] = []; - for (const v of options.messages) { - if (v.role === "assistant") { - const content = getMessageTextContentWithoutThinking(v); - messages.push({ role: v.role, content }); - } else { - const content = getMessageTextContent(v); - messages.push({ role: v.role, content }); - } - } - - // 检测并修复消息顺序,确保除system外的第一个消息是user - const filteredMessages: ChatOptions["messages"] = []; - let hasFoundFirstUser = false; - - for (const msg of messages) { - if (msg.role === "system") { - // Keep all system messages - filteredMessages.push(msg); - } else if (msg.role === "user") { - // User message directly added - filteredMessages.push(msg); - hasFoundFirstUser = true; - } else if (hasFoundFirstUser) { - // After finding the first user message, all subsequent non-system messages are retained. - filteredMessages.push(msg); - } - // If hasFoundFirstUser is false and it is not a system message, it will be skipped. - } - - const modelConfig = { - ...useAppConfig.getState().modelConfig, - ...useChatStore.getState().currentSession().mask.modelConfig, - ...{ - model: options.config.model, - providerName: options.config.providerName, - }, - }; - - const requestPayload: RequestPayload = { - messages: filteredMessages, - stream: options.config.stream, - model: modelConfig.model, - temperature: modelConfig.temperature, - presence_penalty: modelConfig.presence_penalty, - frequency_penalty: modelConfig.frequency_penalty, - top_p: modelConfig.top_p, - // max_tokens: Math.max(modelConfig.max_tokens, 1024), - // Please do not ask me why not send max_tokens, no reason, this param is just shit, I dont want to explain anymore. - }; - - console.log("[Request] openai payload: ", requestPayload); - - const shouldStream = !!options.config.stream; - const controller = new AbortController(); - options.onController?.(controller); - - try { - const chatPath = this.path(DeepSeek.ChatPath); - const chatPayload = { - method: "POST", - body: JSON.stringify(requestPayload), - signal: controller.signal, - headers: getHeaders(), - }; - - // make a fetch request - const requestTimeoutId = setTimeout( - () => controller.abort(), - getTimeoutMSByModel(options.config.model), - ); - - if (shouldStream) { - const [tools, funcs] = usePluginStore - .getState() - .getAsTools( - useChatStore.getState().currentSession().mask?.plugin || [], - ); - return streamWithThink( - chatPath, - requestPayload, - getHeaders(), - tools as any, - funcs, - controller, - // parseSSE - (text: string, runTools: ChatMessageTool[]) => { - // console.log("parseSSE", text, runTools); - const json = JSON.parse(text); - const choices = json.choices as Array<{ - delta: { - content: string | null; - tool_calls: ChatMessageTool[]; - reasoning_content: string | null; - }; - }>; - const tool_calls = choices[0]?.delta?.tool_calls; - if (tool_calls?.length > 0) { - const index = tool_calls[0]?.index; - const id = tool_calls[0]?.id; - const args = tool_calls[0]?.function?.arguments; - if (id) { - runTools.push({ - id, - type: tool_calls[0]?.type, - function: { - name: tool_calls[0]?.function?.name as string, - arguments: args, - }, - }); - } else { - // @ts-ignore - runTools[index]["function"]["arguments"] += args; - } - } - const reasoning = choices[0]?.delta?.reasoning_content; - const content = choices[0]?.delta?.content; - - // Skip if both content and reasoning_content are empty or null - if ( - (!reasoning || reasoning.length === 0) && - (!content || content.length === 0) - ) { - return { - isThinking: false, - content: "", - }; - } - - if (reasoning && reasoning.length > 0) { - return { - isThinking: true, - content: reasoning, - }; - } else if (content && content.length > 0) { - return { - isThinking: false, - content: content, - }; - } - - return { - isThinking: false, - content: "", - }; - }, - // processToolMessage, include tool_calls message and tool call results - ( - requestPayload: RequestPayload, - toolCallMessage: any, - toolCallResult: any[], - ) => { - // @ts-ignore - requestPayload?.messages?.splice( - // @ts-ignore - requestPayload?.messages?.length, - 0, - toolCallMessage, - ...toolCallResult, - ); - }, - options, - ); - } else { - const res = await fetch(chatPath, chatPayload); - clearTimeout(requestTimeoutId); - - const resJson = await res.json(); - const message = this.extractMessage(resJson); - options.onFinish(message, res); - } - } catch (e) { - console.log("[Request] failed to make a chat request", e); - options.onError?.(e as Error); - } - } - async usage() { - return { - used: 0, - total: 0, - }; - } - - async models(): Promise { - return []; - } -} diff --git a/app/client/platforms/glm.ts b/app/client/platforms/glm.ts deleted file mode 100644 index 98b10277de9..00000000000 --- a/app/client/platforms/glm.ts +++ /dev/null @@ -1,292 +0,0 @@ -"use client"; -import { ApiPath, CHATGLM_BASE_URL, ChatGLM } from "@/app/constant"; -import { - useAccessStore, - useAppConfig, - useChatStore, - ChatMessageTool, - usePluginStore, -} from "@/app/store"; -import { stream } from "@/app/utils/chat"; -import { - ChatOptions, - getHeaders, - LLMApi, - LLMModel, - SpeechOptions, -} from "../api"; -import { getClientConfig } from "@/app/config/client"; -import { - getMessageTextContent, - isVisionModel, - getTimeoutMSByModel, -} from "@/app/utils"; -import { RequestPayload } from "./openai"; -import { fetch } from "@/app/utils/stream"; -import { preProcessImageContent } from "@/app/utils/chat"; - -interface BasePayload { - model: string; -} - -interface ChatPayload extends BasePayload { - messages: ChatOptions["messages"]; - stream?: boolean; - temperature?: number; - presence_penalty?: number; - frequency_penalty?: number; - top_p?: number; -} - -interface ImageGenerationPayload extends BasePayload { - prompt: string; - size?: string; - user_id?: string; -} - -interface VideoGenerationPayload extends BasePayload { - prompt: string; - duration?: number; - resolution?: string; - user_id?: string; -} - -type ModelType = "chat" | "image" | "video"; - -export class ChatGLMApi implements LLMApi { - private disableListModels = true; - - private getModelType(model: string): ModelType { - if (model.startsWith("cogview-")) return "image"; - if (model.startsWith("cogvideo-")) return "video"; - return "chat"; - } - - private getModelPath(type: ModelType): string { - switch (type) { - case "image": - return ChatGLM.ImagePath; - case "video": - return ChatGLM.VideoPath; - default: - return ChatGLM.ChatPath; - } - } - - private createPayload( - messages: ChatOptions["messages"], - modelConfig: any, - options: ChatOptions, - ): BasePayload { - const modelType = this.getModelType(modelConfig.model); - const lastMessage = messages[messages.length - 1]; - const prompt = - typeof lastMessage.content === "string" - ? lastMessage.content - : lastMessage.content.map((c) => c.text).join("\n"); - - switch (modelType) { - case "image": - return { - model: modelConfig.model, - prompt, - size: options.config.size, - } as ImageGenerationPayload; - default: - return { - messages, - stream: options.config.stream, - model: modelConfig.model, - temperature: modelConfig.temperature, - presence_penalty: modelConfig.presence_penalty, - frequency_penalty: modelConfig.frequency_penalty, - top_p: modelConfig.top_p, - } as ChatPayload; - } - } - - private parseResponse(modelType: ModelType, json: any): string { - switch (modelType) { - case "image": { - const imageUrl = json.data?.[0]?.url; - return imageUrl ? `![Generated Image](${imageUrl})` : ""; - } - case "video": { - const videoUrl = json.data?.[0]?.url; - return videoUrl ? `` : ""; - } - default: - return this.extractMessage(json); - } - } - - path(path: string): string { - const accessStore = useAccessStore.getState(); - let baseUrl = ""; - - if (accessStore.useCustomConfig) { - baseUrl = accessStore.chatglmUrl; - } - - if (baseUrl.length === 0) { - const isApp = !!getClientConfig()?.isApp; - const apiPath = ApiPath.ChatGLM; - baseUrl = isApp ? CHATGLM_BASE_URL : apiPath; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, baseUrl.length - 1); - } - if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.ChatGLM)) { - baseUrl = "https://" + baseUrl; - } - - console.log("[Proxy Endpoint] ", baseUrl, path); - return [baseUrl, path].join("/"); - } - - extractMessage(res: any) { - return res.choices?.at(0)?.message?.content ?? ""; - } - - speech(options: SpeechOptions): Promise { - throw new Error("Method not implemented."); - } - - async chat(options: ChatOptions) { - const visionModel = isVisionModel(options.config.model); - const messages: ChatOptions["messages"] = []; - for (const v of options.messages) { - const content = visionModel - ? await preProcessImageContent(v.content) - : getMessageTextContent(v); - messages.push({ role: v.role, content }); - } - - const modelConfig = { - ...useAppConfig.getState().modelConfig, - ...useChatStore.getState().currentSession().mask.modelConfig, - ...{ - model: options.config.model, - providerName: options.config.providerName, - }, - }; - const modelType = this.getModelType(modelConfig.model); - const requestPayload = this.createPayload(messages, modelConfig, options); - const path = this.path(this.getModelPath(modelType)); - - console.log(`[Request] glm ${modelType} payload: `, requestPayload); - - const controller = new AbortController(); - options.onController?.(controller); - - try { - const chatPayload = { - method: "POST", - body: JSON.stringify(requestPayload), - signal: controller.signal, - headers: getHeaders(), - }; - - const requestTimeoutId = setTimeout( - () => controller.abort(), - getTimeoutMSByModel(options.config.model), - ); - - if (modelType === "image" || modelType === "video") { - const res = await fetch(path, chatPayload); - clearTimeout(requestTimeoutId); - - const resJson = await res.json(); - console.log(`[Response] glm ${modelType}:`, resJson); - const message = this.parseResponse(modelType, resJson); - options.onFinish(message, res); - return; - } - - const shouldStream = !!options.config.stream; - if (shouldStream) { - const [tools, funcs] = usePluginStore - .getState() - .getAsTools( - useChatStore.getState().currentSession().mask?.plugin || [], - ); - return stream( - path, - requestPayload, - getHeaders(), - tools as any, - funcs, - controller, - // parseSSE - (text: string, runTools: ChatMessageTool[]) => { - const json = JSON.parse(text); - const choices = json.choices as Array<{ - delta: { - content: string; - tool_calls: ChatMessageTool[]; - }; - }>; - const tool_calls = choices[0]?.delta?.tool_calls; - if (tool_calls?.length > 0) { - const index = tool_calls[0]?.index; - const id = tool_calls[0]?.id; - const args = tool_calls[0]?.function?.arguments; - if (id) { - runTools.push({ - id, - type: tool_calls[0]?.type, - function: { - name: tool_calls[0]?.function?.name as string, - arguments: args, - }, - }); - } else { - // @ts-ignore - runTools[index]["function"]["arguments"] += args; - } - } - return choices[0]?.delta?.content; - }, - // processToolMessage - ( - requestPayload: RequestPayload, - toolCallMessage: any, - toolCallResult: any[], - ) => { - // @ts-ignore - requestPayload?.messages?.splice( - // @ts-ignore - requestPayload?.messages?.length, - 0, - toolCallMessage, - ...toolCallResult, - ); - }, - options, - ); - } else { - const res = await fetch(path, chatPayload); - clearTimeout(requestTimeoutId); - - const resJson = await res.json(); - const message = this.extractMessage(resJson); - options.onFinish(message, res); - } - } catch (e) { - console.log("[Request] failed to make a chat request", e); - options.onError?.(e as Error); - } - } - - async usage() { - return { - used: 0, - total: 0, - }; - } - - async models(): Promise { - return []; - } -} diff --git a/app/client/platforms/google.ts b/app/client/platforms/google.ts deleted file mode 100644 index 654f0e3e4c7..00000000000 --- a/app/client/platforms/google.ts +++ /dev/null @@ -1,317 +0,0 @@ -import { ApiPath, Google } from "@/app/constant"; -import { - ChatOptions, - getHeaders, - LLMApi, - LLMModel, - LLMUsage, - SpeechOptions, -} from "../api"; -import { - useAccessStore, - useAppConfig, - useChatStore, - usePluginStore, - ChatMessageTool, -} from "@/app/store"; -import { stream } from "@/app/utils/chat"; -import { getClientConfig } from "@/app/config/client"; -import { GEMINI_BASE_URL } from "@/app/constant"; - -import { - getMessageTextContent, - getMessageImages, - isVisionModel, - getTimeoutMSByModel, -} from "@/app/utils"; -import { preProcessImageContent } from "@/app/utils/chat"; -import { nanoid } from "nanoid"; -import { RequestPayload } from "./openai"; -import { fetch } from "@/app/utils/stream"; - -export class GeminiProApi implements LLMApi { - path(path: string, shouldStream = false): string { - const accessStore = useAccessStore.getState(); - - let baseUrl = ""; - if (accessStore.useCustomConfig) { - baseUrl = accessStore.googleUrl; - } - - const isApp = !!getClientConfig()?.isApp; - if (baseUrl.length === 0) { - baseUrl = isApp ? GEMINI_BASE_URL : ApiPath.Google; - } - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, baseUrl.length - 1); - } - if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.Google)) { - baseUrl = "https://" + baseUrl; - } - - console.log("[Proxy Endpoint] ", baseUrl, path); - - let chatPath = [baseUrl, path].join("/"); - if (shouldStream) { - chatPath += chatPath.includes("?") ? "&alt=sse" : "?alt=sse"; - } - - return chatPath; - } - extractMessage(res: any) { - console.log("[Response] gemini-pro response: ", res); - - const getTextFromParts = (parts: any[]) => { - if (!Array.isArray(parts)) return ""; - - return parts - .map((part) => part?.text || "") - .filter((text) => text.trim() !== "") - .join("\n\n"); - }; - - let content = ""; - if (Array.isArray(res)) { - res.map((item) => { - content += getTextFromParts(item?.candidates?.at(0)?.content?.parts); - }); - } - - return ( - getTextFromParts(res?.candidates?.at(0)?.content?.parts) || - content || //getTextFromParts(res?.at(0)?.candidates?.at(0)?.content?.parts) || - res?.error?.message || - "" - ); - } - speech(options: SpeechOptions): Promise { - throw new Error("Method not implemented."); - } - - async chat(options: ChatOptions): Promise { - const apiClient = this; - let multimodal = false; - - // try get base64image from local cache image_url - const _messages: ChatOptions["messages"] = []; - for (const v of options.messages) { - const content = await preProcessImageContent(v.content); - _messages.push({ role: v.role, content }); - } - const messages = _messages.map((v) => { - let parts: any[] = [{ text: getMessageTextContent(v) }]; - if (isVisionModel(options.config.model)) { - const images = getMessageImages(v); - if (images.length > 0) { - multimodal = true; - parts = parts.concat( - images.map((image) => { - const imageType = image.split(";")[0].split(":")[1]; - const imageData = image.split(",")[1]; - return { - inline_data: { - mime_type: imageType, - data: imageData, - }, - }; - }), - ); - } - } - return { - role: v.role.replace("assistant", "model").replace("system", "user"), - parts: parts, - }; - }); - - // google requires that role in neighboring messages must not be the same - for (let i = 0; i < messages.length - 1; ) { - // Check if current and next item both have the role "model" - if (messages[i].role === messages[i + 1].role) { - // Concatenate the 'parts' of the current and next item - messages[i].parts = messages[i].parts.concat(messages[i + 1].parts); - // Remove the next item - messages.splice(i + 1, 1); - } else { - // Move to the next item - i++; - } - } - // if (visionModel && messages.length > 1) { - // options.onError?.(new Error("Multiturn chat is not enabled for models/gemini-pro-vision")); - // } - - const accessStore = useAccessStore.getState(); - - const modelConfig = { - ...useAppConfig.getState().modelConfig, - ...useChatStore.getState().currentSession().mask.modelConfig, - ...{ - model: options.config.model, - }, - }; - const requestPayload = { - contents: messages, - generationConfig: { - // stopSequences: [ - // "Title" - // ], - temperature: modelConfig.temperature, - maxOutputTokens: modelConfig.max_tokens, - topP: modelConfig.top_p, - // "topK": modelConfig.top_k, - }, - safetySettings: [ - { - category: "HARM_CATEGORY_HARASSMENT", - threshold: accessStore.googleSafetySettings, - }, - { - category: "HARM_CATEGORY_HATE_SPEECH", - threshold: accessStore.googleSafetySettings, - }, - { - category: "HARM_CATEGORY_SEXUALLY_EXPLICIT", - threshold: accessStore.googleSafetySettings, - }, - { - category: "HARM_CATEGORY_DANGEROUS_CONTENT", - threshold: accessStore.googleSafetySettings, - }, - ], - }; - - let shouldStream = !!options.config.stream; - const controller = new AbortController(); - options.onController?.(controller); - try { - // https://github.com/google-gemini/cookbook/blob/main/quickstarts/rest/Streaming_REST.ipynb - const chatPath = this.path( - Google.ChatPath(modelConfig.model), - shouldStream, - ); - - const chatPayload = { - method: "POST", - body: JSON.stringify(requestPayload), - signal: controller.signal, - headers: getHeaders(), - }; - - const isThinking = options.config.model.includes("-thinking"); - // make a fetch request - const requestTimeoutId = setTimeout( - () => controller.abort(), - getTimeoutMSByModel(options.config.model), - ); - - if (shouldStream) { - const [tools, funcs] = usePluginStore - .getState() - .getAsTools( - useChatStore.getState().currentSession().mask?.plugin || [], - ); - return stream( - chatPath, - requestPayload, - getHeaders(), - // @ts-ignore - tools.length > 0 - ? // @ts-ignore - [{ functionDeclarations: tools.map((tool) => tool.function) }] - : [], - funcs, - controller, - // parseSSE - (text: string, runTools: ChatMessageTool[]) => { - // console.log("parseSSE", text, runTools); - const chunkJson = JSON.parse(text); - - const functionCall = chunkJson?.candidates - ?.at(0) - ?.content.parts.at(0)?.functionCall; - if (functionCall) { - const { name, args } = functionCall; - runTools.push({ - id: nanoid(), - type: "function", - function: { - name, - arguments: JSON.stringify(args), // utils.chat call function, using JSON.parse - }, - }); - } - return chunkJson?.candidates - ?.at(0) - ?.content.parts?.map((part: { text: string }) => part.text) - .join("\n\n"); - }, - // processToolMessage, include tool_calls message and tool call results - ( - requestPayload: RequestPayload, - toolCallMessage: any, - toolCallResult: any[], - ) => { - // @ts-ignore - requestPayload?.contents?.splice( - // @ts-ignore - requestPayload?.contents?.length, - 0, - { - role: "model", - parts: toolCallMessage.tool_calls.map( - (tool: ChatMessageTool) => ({ - functionCall: { - name: tool?.function?.name, - args: JSON.parse(tool?.function?.arguments as string), - }, - }), - ), - }, - // @ts-ignore - ...toolCallResult.map((result) => ({ - role: "function", - parts: [ - { - functionResponse: { - name: result.name, - response: { - name: result.name, - content: result.content, // TODO just text content... - }, - }, - }, - ], - })), - ); - }, - options, - ); - } else { - const res = await fetch(chatPath, chatPayload); - clearTimeout(requestTimeoutId); - const resJson = await res.json(); - if (resJson?.promptFeedback?.blockReason) { - // being blocked - options.onError?.( - new Error( - "Message is being blocked for reason: " + - resJson.promptFeedback.blockReason, - ), - ); - } - const message = apiClient.extractMessage(resJson); - options.onFinish(message, res); - } - } catch (e) { - console.log("[Request] failed to make a chat request", e); - options.onError?.(e as Error); - } - } - usage(): Promise { - throw new Error("Method not implemented."); - } - async models(): Promise { - return []; - } -} diff --git a/app/client/platforms/iflytek.ts b/app/client/platforms/iflytek.ts deleted file mode 100644 index cfc37b3b256..00000000000 --- a/app/client/platforms/iflytek.ts +++ /dev/null @@ -1,253 +0,0 @@ -"use client"; -import { - ApiPath, - IFLYTEK_BASE_URL, - Iflytek, - REQUEST_TIMEOUT_MS, -} from "@/app/constant"; -import { useAccessStore, useAppConfig, useChatStore } from "@/app/store"; - -import { - ChatOptions, - getHeaders, - LLMApi, - LLMModel, - SpeechOptions, -} from "../api"; -import Locale from "../../locales"; -import { - EventStreamContentType, - fetchEventSource, -} from "@fortaine/fetch-event-source"; -import { prettyObject } from "@/app/utils/format"; -import { getClientConfig } from "@/app/config/client"; -import { getMessageTextContent } from "@/app/utils"; -import { fetch } from "@/app/utils/stream"; - -import { RequestPayload } from "./openai"; - -export class SparkApi implements LLMApi { - private disableListModels = true; - - path(path: string): string { - const accessStore = useAccessStore.getState(); - - let baseUrl = ""; - - if (accessStore.useCustomConfig) { - baseUrl = accessStore.iflytekUrl; - } - - if (baseUrl.length === 0) { - const isApp = !!getClientConfig()?.isApp; - const apiPath = ApiPath.Iflytek; - baseUrl = isApp ? IFLYTEK_BASE_URL : apiPath; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, baseUrl.length - 1); - } - if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.Iflytek)) { - baseUrl = "https://" + baseUrl; - } - - console.log("[Proxy Endpoint] ", baseUrl, path); - - return [baseUrl, path].join("/"); - } - - extractMessage(res: any) { - return res.choices?.at(0)?.message?.content ?? ""; - } - - speech(options: SpeechOptions): Promise { - throw new Error("Method not implemented."); - } - - async chat(options: ChatOptions) { - const messages: ChatOptions["messages"] = []; - for (const v of options.messages) { - const content = getMessageTextContent(v); - messages.push({ role: v.role, content }); - } - - const modelConfig = { - ...useAppConfig.getState().modelConfig, - ...useChatStore.getState().currentSession().mask.modelConfig, - ...{ - model: options.config.model, - providerName: options.config.providerName, - }, - }; - - const requestPayload: RequestPayload = { - messages, - stream: options.config.stream, - model: modelConfig.model, - temperature: modelConfig.temperature, - presence_penalty: modelConfig.presence_penalty, - frequency_penalty: modelConfig.frequency_penalty, - top_p: modelConfig.top_p, - // max_tokens: Math.max(modelConfig.max_tokens, 1024), - // Please do not ask me why not send max_tokens, no reason, this param is just shit, I dont want to explain anymore. - }; - - console.log("[Request] Spark payload: ", requestPayload); - - const shouldStream = !!options.config.stream; - const controller = new AbortController(); - options.onController?.(controller); - - try { - const chatPath = this.path(Iflytek.ChatPath); - const chatPayload = { - method: "POST", - body: JSON.stringify(requestPayload), - signal: controller.signal, - headers: getHeaders(), - }; - - // Make a fetch request - const requestTimeoutId = setTimeout( - () => controller.abort(), - REQUEST_TIMEOUT_MS, - ); - - if (shouldStream) { - let responseText = ""; - let remainText = ""; - let finished = false; - let responseRes: Response; - - // Animate response text to make it look smooth - function animateResponseText() { - if (finished || controller.signal.aborted) { - responseText += remainText; - console.log("[Response Animation] finished"); - return; - } - - if (remainText.length > 0) { - const fetchCount = Math.max(1, Math.round(remainText.length / 60)); - const fetchText = remainText.slice(0, fetchCount); - responseText += fetchText; - remainText = remainText.slice(fetchCount); - options.onUpdate?.(responseText, fetchText); - } - - requestAnimationFrame(animateResponseText); - } - - // Start animation - animateResponseText(); - - const finish = () => { - if (!finished) { - finished = true; - options.onFinish(responseText + remainText, responseRes); - } - }; - - controller.signal.onabort = finish; - - fetchEventSource(chatPath, { - fetch: fetch as any, - ...chatPayload, - async onopen(res) { - clearTimeout(requestTimeoutId); - const contentType = res.headers.get("content-type"); - console.log("[Spark] request response content type: ", contentType); - responseRes = res; - if (contentType?.startsWith("text/plain")) { - responseText = await res.clone().text(); - return finish(); - } - - // Handle different error scenarios - if ( - !res.ok || - !res.headers - .get("content-type") - ?.startsWith(EventStreamContentType) || - res.status !== 200 - ) { - let extraInfo = await res.clone().text(); - try { - const resJson = await res.clone().json(); - extraInfo = prettyObject(resJson); - } catch {} - - if (res.status === 401) { - extraInfo = Locale.Error.Unauthorized; - } - - options.onError?.( - new Error( - `Request failed with status ${res.status}: ${extraInfo}`, - ), - ); - return finish(); - } - }, - onmessage(msg) { - if (msg.data === "[DONE]" || finished) { - return finish(); - } - const text = msg.data; - try { - const json = JSON.parse(text); - const choices = json.choices as Array<{ - delta: { content: string }; - }>; - const delta = choices[0]?.delta?.content; - - if (delta) { - remainText += delta; - } - } catch (e) { - console.error("[Request] parse error", text); - options.onError?.(new Error(`Failed to parse response: ${text}`)); - } - }, - onclose() { - finish(); - }, - onerror(e) { - options.onError?.(e); - throw e; - }, - openWhenHidden: true, - }); - } else { - const res = await fetch(chatPath, chatPayload); - clearTimeout(requestTimeoutId); - - if (!res.ok) { - const errorText = await res.text(); - options.onError?.( - new Error(`Request failed with status ${res.status}: ${errorText}`), - ); - return; - } - - const resJson = await res.json(); - const message = this.extractMessage(resJson); - options.onFinish(message, res); - } - } catch (e) { - console.log("[Request] failed to make a chat request", e); - options.onError?.(e as Error); - } - } - - async usage() { - return { - used: 0, - total: 0, - }; - } - - async models(): Promise { - return []; - } -} diff --git a/app/client/platforms/moonshot.ts b/app/client/platforms/moonshot.ts deleted file mode 100644 index b6812c0d766..00000000000 --- a/app/client/platforms/moonshot.ts +++ /dev/null @@ -1,200 +0,0 @@ -"use client"; -// azure and openai, using same models. so using same LLMApi. -import { - ApiPath, - MOONSHOT_BASE_URL, - Moonshot, - REQUEST_TIMEOUT_MS, -} from "@/app/constant"; -import { - useAccessStore, - useAppConfig, - useChatStore, - ChatMessageTool, - usePluginStore, -} from "@/app/store"; -import { stream } from "@/app/utils/chat"; -import { - ChatOptions, - getHeaders, - LLMApi, - LLMModel, - SpeechOptions, -} from "../api"; -import { getClientConfig } from "@/app/config/client"; -import { getMessageTextContent } from "@/app/utils"; -import { RequestPayload } from "./openai"; -import { fetch } from "@/app/utils/stream"; - -export class MoonshotApi implements LLMApi { - private disableListModels = true; - - path(path: string): string { - const accessStore = useAccessStore.getState(); - - let baseUrl = ""; - - if (accessStore.useCustomConfig) { - baseUrl = accessStore.moonshotUrl; - } - - if (baseUrl.length === 0) { - const isApp = !!getClientConfig()?.isApp; - const apiPath = ApiPath.Moonshot; - baseUrl = isApp ? MOONSHOT_BASE_URL : apiPath; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, baseUrl.length - 1); - } - if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.Moonshot)) { - baseUrl = "https://" + baseUrl; - } - - console.log("[Proxy Endpoint] ", baseUrl, path); - - return [baseUrl, path].join("/"); - } - - extractMessage(res: any) { - return res.choices?.at(0)?.message?.content ?? ""; - } - - speech(options: SpeechOptions): Promise { - throw new Error("Method not implemented."); - } - - async chat(options: ChatOptions) { - const messages: ChatOptions["messages"] = []; - for (const v of options.messages) { - const content = getMessageTextContent(v); - messages.push({ role: v.role, content }); - } - - const modelConfig = { - ...useAppConfig.getState().modelConfig, - ...useChatStore.getState().currentSession().mask.modelConfig, - ...{ - model: options.config.model, - providerName: options.config.providerName, - }, - }; - - const requestPayload: RequestPayload = { - messages, - stream: options.config.stream, - model: modelConfig.model, - temperature: modelConfig.temperature, - presence_penalty: modelConfig.presence_penalty, - frequency_penalty: modelConfig.frequency_penalty, - top_p: modelConfig.top_p, - // max_tokens: Math.max(modelConfig.max_tokens, 1024), - // Please do not ask me why not send max_tokens, no reason, this param is just shit, I dont want to explain anymore. - }; - - console.log("[Request] openai payload: ", requestPayload); - - const shouldStream = !!options.config.stream; - const controller = new AbortController(); - options.onController?.(controller); - - try { - const chatPath = this.path(Moonshot.ChatPath); - const chatPayload = { - method: "POST", - body: JSON.stringify(requestPayload), - signal: controller.signal, - headers: getHeaders(), - }; - - // make a fetch request - const requestTimeoutId = setTimeout( - () => controller.abort(), - REQUEST_TIMEOUT_MS, - ); - - if (shouldStream) { - const [tools, funcs] = usePluginStore - .getState() - .getAsTools( - useChatStore.getState().currentSession().mask?.plugin || [], - ); - return stream( - chatPath, - requestPayload, - getHeaders(), - tools as any, - funcs, - controller, - // parseSSE - (text: string, runTools: ChatMessageTool[]) => { - // console.log("parseSSE", text, runTools); - const json = JSON.parse(text); - const choices = json.choices as Array<{ - delta: { - content: string; - tool_calls: ChatMessageTool[]; - }; - }>; - const tool_calls = choices[0]?.delta?.tool_calls; - if (tool_calls?.length > 0) { - const index = tool_calls[0]?.index; - const id = tool_calls[0]?.id; - const args = tool_calls[0]?.function?.arguments; - if (id) { - runTools.push({ - id, - type: tool_calls[0]?.type, - function: { - name: tool_calls[0]?.function?.name as string, - arguments: args, - }, - }); - } else { - // @ts-ignore - runTools[index]["function"]["arguments"] += args; - } - } - return choices[0]?.delta?.content; - }, - // processToolMessage, include tool_calls message and tool call results - ( - requestPayload: RequestPayload, - toolCallMessage: any, - toolCallResult: any[], - ) => { - // @ts-ignore - requestPayload?.messages?.splice( - // @ts-ignore - requestPayload?.messages?.length, - 0, - toolCallMessage, - ...toolCallResult, - ); - }, - options, - ); - } else { - const res = await fetch(chatPath, chatPayload); - clearTimeout(requestTimeoutId); - - const resJson = await res.json(); - const message = this.extractMessage(resJson); - options.onFinish(message, res); - } - } catch (e) { - console.log("[Request] failed to make a chat request", e); - options.onError?.(e as Error); - } - } - async usage() { - return { - used: 0, - total: 0, - }; - } - - async models(): Promise { - return []; - } -} diff --git a/app/client/platforms/openai.ts b/app/client/platforms/openai.ts deleted file mode 100644 index cfbff99e978..00000000000 --- a/app/client/platforms/openai.ts +++ /dev/null @@ -1,534 +0,0 @@ -"use client"; -// azure and openai, using same models. so using same LLMApi. -import { - ApiPath, - OPENAI_BASE_URL, - DEFAULT_MODELS, - OpenaiPath, - Azure, - REQUEST_TIMEOUT_MS, - ServiceProvider, -} from "@/app/constant"; -import { - ChatMessageTool, - useAccessStore, - useAppConfig, - useChatStore, - usePluginStore, -} from "@/app/store"; -import { collectModelsWithDefaultModel } from "@/app/utils/model"; -import { - preProcessImageContent, - uploadImage, - base64Image2Blob, - streamWithThink, -} from "@/app/utils/chat"; -import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare"; -import { ModelSize, DalleQuality, DalleStyle } from "@/app/typing"; - -import { - ChatOptions, - getHeaders, - LLMApi, - LLMModel, - LLMUsage, - MultimodalContent, - SpeechOptions, -} from "../api"; -import Locale from "../../locales"; -import { getClientConfig } from "@/app/config/client"; -import { - getMessageTextContent, - isVisionModel, - isDalle3 as _isDalle3, - getTimeoutMSByModel, -} from "@/app/utils"; -import { fetch } from "@/app/utils/stream"; - -export interface OpenAIListModelResponse { - object: string; - data: Array<{ - id: string; - object: string; - root: string; - }>; -} - -export interface RequestPayload { - messages: { - role: "developer" | "system" | "user" | "assistant"; - content: string | MultimodalContent[]; - }[]; - stream?: boolean; - model: string; - temperature: number; - presence_penalty: number; - frequency_penalty: number; - top_p: number; - max_tokens?: number; - max_completion_tokens?: number; -} - -export interface DalleRequestPayload { - model: string; - prompt: string; - response_format: "url" | "b64_json"; - n: number; - size: ModelSize; - quality: DalleQuality; - style: DalleStyle; -} - -export class ChatGPTApi implements LLMApi { - private disableListModels = true; - - path(path: string): string { - const accessStore = useAccessStore.getState(); - - let baseUrl = ""; - - const isAzure = path.includes("deployments"); - if (accessStore.useCustomConfig) { - if (isAzure && !accessStore.isValidAzure()) { - throw Error( - "incomplete azure config, please check it in your settings page", - ); - } - - baseUrl = isAzure ? accessStore.azureUrl : accessStore.openaiUrl; - } - - if (baseUrl.length === 0) { - const isApp = !!getClientConfig()?.isApp; - const apiPath = isAzure ? ApiPath.Azure : ApiPath.OpenAI; - baseUrl = isApp ? OPENAI_BASE_URL : apiPath; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, baseUrl.length - 1); - } - if ( - !baseUrl.startsWith("http") && - !isAzure && - !baseUrl.startsWith(ApiPath.OpenAI) - ) { - baseUrl = "https://" + baseUrl; - } - - console.log("[Proxy Endpoint] ", baseUrl, path); - - // try rebuild url, when using cloudflare ai gateway in client - return cloudflareAIGatewayUrl([baseUrl, path].join("/")); - } - - async extractMessage(res: any) { - if (res.error) { - return "```\n" + JSON.stringify(res, null, 4) + "\n```"; - } - // dalle3 model return url, using url create image message - if (res.data) { - let url = res.data?.at(0)?.url ?? ""; - const b64_json = res.data?.at(0)?.b64_json ?? ""; - if (!url && b64_json) { - // uploadImage - url = await uploadImage(base64Image2Blob(b64_json, "image/png")); - } - return [ - { - type: "image_url", - image_url: { - url, - }, - }, - ]; - } - return res.choices?.at(0)?.message?.content ?? res; - } - - async speech(options: SpeechOptions): Promise { - const requestPayload = { - model: options.model, - input: options.input, - voice: options.voice, - response_format: options.response_format, - speed: options.speed, - }; - - console.log("[Request] openai speech payload: ", requestPayload); - - const controller = new AbortController(); - options.onController?.(controller); - - try { - const speechPath = this.path(OpenaiPath.SpeechPath); - const speechPayload = { - method: "POST", - body: JSON.stringify(requestPayload), - signal: controller.signal, - headers: getHeaders(), - }; - - // make a fetch request - const requestTimeoutId = setTimeout( - () => controller.abort(), - REQUEST_TIMEOUT_MS, - ); - - const res = await fetch(speechPath, speechPayload); - clearTimeout(requestTimeoutId); - return await res.arrayBuffer(); - } catch (e) { - console.log("[Request] failed to make a speech request", e); - throw e; - } - } - - async chat(options: ChatOptions) { - const modelConfig = { - ...useAppConfig.getState().modelConfig, - ...useChatStore.getState().currentSession().mask.modelConfig, - ...{ - model: options.config.model, - providerName: options.config.providerName, - }, - }; - - let requestPayload: RequestPayload | DalleRequestPayload; - - const isDalle3 = _isDalle3(options.config.model); - const isO1OrO3 = - options.config.model.startsWith("o1") || - options.config.model.startsWith("o3") || - options.config.model.startsWith("o4-mini"); - const isGpt5 = options.config.model.startsWith("gpt-5"); - if (isDalle3) { - const prompt = getMessageTextContent( - options.messages.slice(-1)?.pop() as any, - ); - requestPayload = { - model: options.config.model, - prompt, - // URLs are only valid for 60 minutes after the image has been generated. - response_format: "b64_json", // using b64_json, and save image in CacheStorage - n: 1, - size: options.config?.size ?? "1024x1024", - quality: options.config?.quality ?? "standard", - style: options.config?.style ?? "vivid", - }; - } else { - const visionModel = isVisionModel(options.config.model); - const messages: ChatOptions["messages"] = []; - for (const v of options.messages) { - const content = visionModel - ? await preProcessImageContent(v.content) - : getMessageTextContent(v); - if (!(isO1OrO3 && v.role === "system")) - messages.push({ role: v.role, content }); - } - - // O1 not support image, tools (plugin in ChatGPTNextWeb) and system, stream, logprobs, temperature, top_p, n, presence_penalty, frequency_penalty yet. - requestPayload = { - messages, - stream: options.config.stream, - model: modelConfig.model, - temperature: (!isO1OrO3 && !isGpt5) ? modelConfig.temperature : 1, - presence_penalty: !isO1OrO3 ? modelConfig.presence_penalty : 0, - frequency_penalty: !isO1OrO3 ? modelConfig.frequency_penalty : 0, - top_p: !isO1OrO3 ? modelConfig.top_p : 1, - // max_tokens: Math.max(modelConfig.max_tokens, 1024), - // Please do not ask me why not send max_tokens, no reason, this param is just shit, I dont want to explain anymore. - }; - - if (isGpt5) { - // Remove max_tokens if present - delete requestPayload.max_tokens; - // Add max_completion_tokens (or max_completion_tokens if that's what you meant) - requestPayload["max_completion_tokens"] = modelConfig.max_tokens; - - } else if (isO1OrO3) { - // by default the o1/o3 models will not attempt to produce output that includes markdown formatting - // manually add "Formatting re-enabled" developer message to encourage markdown inclusion in model responses - // (https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/reasoning?tabs=python-secure#markdown-output) - requestPayload["messages"].unshift({ - role: "developer", - content: "Formatting re-enabled", - }); - - // o1/o3 uses max_completion_tokens to control the number of tokens (https://platform.openai.com/docs/guides/reasoning#controlling-costs) - requestPayload["max_completion_tokens"] = modelConfig.max_tokens; - } - - - // add max_tokens to vision model - if (visionModel && !isO1OrO3 && ! isGpt5) { - requestPayload["max_tokens"] = Math.max(modelConfig.max_tokens, 4000); - } - } - - console.log("[Request] openai payload: ", requestPayload); - - const shouldStream = !isDalle3 && !!options.config.stream; - const controller = new AbortController(); - options.onController?.(controller); - - try { - let chatPath = ""; - if (modelConfig.providerName === ServiceProvider.Azure) { - // find model, and get displayName as deployName - const { models: configModels, customModels: configCustomModels } = - useAppConfig.getState(); - const { - defaultModel, - customModels: accessCustomModels, - useCustomConfig, - } = useAccessStore.getState(); - const models = collectModelsWithDefaultModel( - configModels, - [configCustomModels, accessCustomModels].join(","), - defaultModel, - ); - const model = models.find( - (model) => - model.name === modelConfig.model && - model?.provider?.providerName === ServiceProvider.Azure, - ); - chatPath = this.path( - (isDalle3 ? Azure.ImagePath : Azure.ChatPath)( - (model?.displayName ?? model?.name) as string, - useCustomConfig ? useAccessStore.getState().azureApiVersion : "", - ), - ); - } else { - chatPath = this.path( - isDalle3 ? OpenaiPath.ImagePath : OpenaiPath.ChatPath, - ); - } - if (shouldStream) { - let index = -1; - const [tools, funcs] = usePluginStore - .getState() - .getAsTools( - useChatStore.getState().currentSession().mask?.plugin || [], - ); - // console.log("getAsTools", tools, funcs); - streamWithThink( - chatPath, - requestPayload, - getHeaders(), - tools as any, - funcs, - controller, - // parseSSE - (text: string, runTools: ChatMessageTool[]) => { - // console.log("parseSSE", text, runTools); - const json = JSON.parse(text); - const choices = json.choices as Array<{ - delta: { - content: string; - tool_calls: ChatMessageTool[]; - reasoning_content: string | null; - }; - }>; - - if (!choices?.length) return { isThinking: false, content: "" }; - - const tool_calls = choices[0]?.delta?.tool_calls; - if (tool_calls?.length > 0) { - const id = tool_calls[0]?.id; - const args = tool_calls[0]?.function?.arguments; - if (id) { - index += 1; - runTools.push({ - id, - type: tool_calls[0]?.type, - function: { - name: tool_calls[0]?.function?.name as string, - arguments: args, - }, - }); - } else { - // @ts-ignore - runTools[index]["function"]["arguments"] += args; - } - } - - const reasoning = choices[0]?.delta?.reasoning_content; - const content = choices[0]?.delta?.content; - - // Skip if both content and reasoning_content are empty or null - if ( - (!reasoning || reasoning.length === 0) && - (!content || content.length === 0) - ) { - return { - isThinking: false, - content: "", - }; - } - - if (reasoning && reasoning.length > 0) { - return { - isThinking: true, - content: reasoning, - }; - } else if (content && content.length > 0) { - return { - isThinking: false, - content: content, - }; - } - - return { - isThinking: false, - content: "", - }; - }, - // processToolMessage, include tool_calls message and tool call results - ( - requestPayload: RequestPayload, - toolCallMessage: any, - toolCallResult: any[], - ) => { - // reset index value - index = -1; - // @ts-ignore - requestPayload?.messages?.splice( - // @ts-ignore - requestPayload?.messages?.length, - 0, - toolCallMessage, - ...toolCallResult, - ); - }, - options, - ); - } else { - const chatPayload = { - method: "POST", - body: JSON.stringify(requestPayload), - signal: controller.signal, - headers: getHeaders(), - }; - - // make a fetch request - const requestTimeoutId = setTimeout( - () => controller.abort(), - getTimeoutMSByModel(options.config.model), - ); - - const res = await fetch(chatPath, chatPayload); - clearTimeout(requestTimeoutId); - - const resJson = await res.json(); - const message = await this.extractMessage(resJson); - options.onFinish(message, res); - } - } catch (e) { - console.log("[Request] failed to make a chat request", e); - options.onError?.(e as Error); - } - } - async usage() { - const formatDate = (d: Date) => - `${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, "0")}-${d - .getDate() - .toString() - .padStart(2, "0")}`; - const ONE_DAY = 1 * 24 * 60 * 60 * 1000; - const now = new Date(); - const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1); - const startDate = formatDate(startOfMonth); - const endDate = formatDate(new Date(Date.now() + ONE_DAY)); - - const [used, subs] = await Promise.all([ - fetch( - this.path( - `${OpenaiPath.UsagePath}?start_date=${startDate}&end_date=${endDate}`, - ), - { - method: "GET", - headers: getHeaders(), - }, - ), - fetch(this.path(OpenaiPath.SubsPath), { - method: "GET", - headers: getHeaders(), - }), - ]); - - if (used.status === 401) { - throw new Error(Locale.Error.Unauthorized); - } - - if (!used.ok || !subs.ok) { - throw new Error("Failed to query usage from openai"); - } - - const response = (await used.json()) as { - total_usage?: number; - error?: { - type: string; - message: string; - }; - }; - - const total = (await subs.json()) as { - hard_limit_usd?: number; - }; - - if (response.error && response.error.type) { - throw Error(response.error.message); - } - - if (response.total_usage) { - response.total_usage = Math.round(response.total_usage) / 100; - } - - if (total.hard_limit_usd) { - total.hard_limit_usd = Math.round(total.hard_limit_usd * 100) / 100; - } - - return { - used: response.total_usage, - total: total.hard_limit_usd, - } as LLMUsage; - } - - async models(): Promise { - if (this.disableListModels) { - return DEFAULT_MODELS.slice(); - } - - const res = await fetch(this.path(OpenaiPath.ListModelPath), { - method: "GET", - headers: { - ...getHeaders(), - }, - }); - - const resJson = (await res.json()) as OpenAIListModelResponse; - const chatModels = resJson.data?.filter( - (m) => m.id.startsWith("gpt-") || m.id.startsWith("chatgpt-"), - ); - console.log("[Models]", chatModels); - - if (!chatModels) { - return []; - } - - //由于目前 OpenAI 的 disableListModels 默认为 true,所以当前实际不会运行到这场 - let seq = 1000; //同 Constant.ts 中的排序保持一致 - return chatModels.map((m) => ({ - name: m.id, - available: true, - sorted: seq++, - provider: { - id: "openai", - providerName: "OpenAI", - providerType: "openai", - sorted: 1, - }, - })); - } -} -export { OpenaiPath }; diff --git a/app/client/platforms/siliconflow.ts b/app/client/platforms/siliconflow.ts deleted file mode 100644 index 34f0844c328..00000000000 --- a/app/client/platforms/siliconflow.ts +++ /dev/null @@ -1,287 +0,0 @@ -"use client"; -// azure and openai, using same models. so using same LLMApi. -import { - ApiPath, - SILICONFLOW_BASE_URL, - SiliconFlow, - DEFAULT_MODELS, -} from "@/app/constant"; -import { - useAccessStore, - useAppConfig, - useChatStore, - ChatMessageTool, - usePluginStore, -} from "@/app/store"; -import { preProcessImageContent, streamWithThink } from "@/app/utils/chat"; -import { - ChatOptions, - getHeaders, - LLMApi, - LLMModel, - SpeechOptions, -} from "../api"; -import { getClientConfig } from "@/app/config/client"; -import { - getMessageTextContent, - getMessageTextContentWithoutThinking, - isVisionModel, - getTimeoutMSByModel, -} from "@/app/utils"; -import { RequestPayload } from "./openai"; - -import { fetch } from "@/app/utils/stream"; -export interface SiliconFlowListModelResponse { - object: string; - data: Array<{ - id: string; - object: string; - root: string; - }>; -} - -export class SiliconflowApi implements LLMApi { - private disableListModels = false; - - path(path: string): string { - const accessStore = useAccessStore.getState(); - - let baseUrl = ""; - - if (accessStore.useCustomConfig) { - baseUrl = accessStore.siliconflowUrl; - } - - if (baseUrl.length === 0) { - const isApp = !!getClientConfig()?.isApp; - const apiPath = ApiPath.SiliconFlow; - baseUrl = isApp ? SILICONFLOW_BASE_URL : apiPath; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, baseUrl.length - 1); - } - if ( - !baseUrl.startsWith("http") && - !baseUrl.startsWith(ApiPath.SiliconFlow) - ) { - baseUrl = "https://" + baseUrl; - } - - console.log("[Proxy Endpoint] ", baseUrl, path); - - return [baseUrl, path].join("/"); - } - - extractMessage(res: any) { - return res.choices?.at(0)?.message?.content ?? ""; - } - - speech(options: SpeechOptions): Promise { - throw new Error("Method not implemented."); - } - - async chat(options: ChatOptions) { - const visionModel = isVisionModel(options.config.model); - const messages: ChatOptions["messages"] = []; - for (const v of options.messages) { - if (v.role === "assistant") { - const content = getMessageTextContentWithoutThinking(v); - messages.push({ role: v.role, content }); - } else { - const content = visionModel - ? await preProcessImageContent(v.content) - : getMessageTextContent(v); - messages.push({ role: v.role, content }); - } - } - - const modelConfig = { - ...useAppConfig.getState().modelConfig, - ...useChatStore.getState().currentSession().mask.modelConfig, - ...{ - model: options.config.model, - providerName: options.config.providerName, - }, - }; - - const requestPayload: RequestPayload = { - messages, - stream: options.config.stream, - model: modelConfig.model, - temperature: modelConfig.temperature, - presence_penalty: modelConfig.presence_penalty, - frequency_penalty: modelConfig.frequency_penalty, - top_p: modelConfig.top_p, - // max_tokens: Math.max(modelConfig.max_tokens, 1024), - // Please do not ask me why not send max_tokens, no reason, this param is just shit, I dont want to explain anymore. - }; - - console.log("[Request] openai payload: ", requestPayload); - - const shouldStream = !!options.config.stream; - const controller = new AbortController(); - options.onController?.(controller); - - try { - const chatPath = this.path(SiliconFlow.ChatPath); - const chatPayload = { - method: "POST", - body: JSON.stringify(requestPayload), - signal: controller.signal, - headers: getHeaders(), - }; - - // console.log(chatPayload); - - // Use extended timeout for thinking models as they typically require more processing time - const requestTimeoutId = setTimeout( - () => controller.abort(), - getTimeoutMSByModel(options.config.model), - ); - - if (shouldStream) { - const [tools, funcs] = usePluginStore - .getState() - .getAsTools( - useChatStore.getState().currentSession().mask?.plugin || [], - ); - return streamWithThink( - chatPath, - requestPayload, - getHeaders(), - tools as any, - funcs, - controller, - // parseSSE - (text: string, runTools: ChatMessageTool[]) => { - // console.log("parseSSE", text, runTools); - const json = JSON.parse(text); - const choices = json.choices as Array<{ - delta: { - content: string | null; - tool_calls: ChatMessageTool[]; - reasoning_content: string | null; - }; - }>; - const tool_calls = choices[0]?.delta?.tool_calls; - if (tool_calls?.length > 0) { - const index = tool_calls[0]?.index; - const id = tool_calls[0]?.id; - const args = tool_calls[0]?.function?.arguments; - if (id) { - runTools.push({ - id, - type: tool_calls[0]?.type, - function: { - name: tool_calls[0]?.function?.name as string, - arguments: args, - }, - }); - } else { - // @ts-ignore - runTools[index]["function"]["arguments"] += args; - } - } - const reasoning = choices[0]?.delta?.reasoning_content; - const content = choices[0]?.delta?.content; - - // Skip if both content and reasoning_content are empty or null - if ( - (!reasoning || reasoning.length === 0) && - (!content || content.length === 0) - ) { - return { - isThinking: false, - content: "", - }; - } - - if (reasoning && reasoning.length > 0) { - return { - isThinking: true, - content: reasoning, - }; - } else if (content && content.length > 0) { - return { - isThinking: false, - content: content, - }; - } - - return { - isThinking: false, - content: "", - }; - }, - // processToolMessage, include tool_calls message and tool call results - ( - requestPayload: RequestPayload, - toolCallMessage: any, - toolCallResult: any[], - ) => { - // @ts-ignore - requestPayload?.messages?.splice( - // @ts-ignore - requestPayload?.messages?.length, - 0, - toolCallMessage, - ...toolCallResult, - ); - }, - options, - ); - } else { - const res = await fetch(chatPath, chatPayload); - clearTimeout(requestTimeoutId); - - const resJson = await res.json(); - const message = this.extractMessage(resJson); - options.onFinish(message, res); - } - } catch (e) { - console.log("[Request] failed to make a chat request", e); - options.onError?.(e as Error); - } - } - async usage() { - return { - used: 0, - total: 0, - }; - } - - async models(): Promise { - if (this.disableListModels) { - return DEFAULT_MODELS.slice(); - } - - const res = await fetch(this.path(SiliconFlow.ListModelPath), { - method: "GET", - headers: { - ...getHeaders(), - }, - }); - - const resJson = (await res.json()) as SiliconFlowListModelResponse; - const chatModels = resJson.data; - console.log("[Models]", chatModels); - - if (!chatModels) { - return []; - } - - let seq = 1000; //同 Constant.ts 中的排序保持一致 - return chatModels.map((m) => ({ - name: m.id, - available: true, - sorted: seq++, - provider: { - id: "siliconflow", - providerName: "SiliconFlow", - providerType: "siliconflow", - sorted: 14, - }, - })); - } -} diff --git a/app/client/platforms/tencent.ts b/app/client/platforms/tencent.ts deleted file mode 100644 index 8adeb1b3ea7..00000000000 --- a/app/client/platforms/tencent.ts +++ /dev/null @@ -1,278 +0,0 @@ -"use client"; -import { ApiPath, TENCENT_BASE_URL } from "@/app/constant"; -import { useAccessStore, useAppConfig, useChatStore } from "@/app/store"; - -import { - ChatOptions, - getHeaders, - LLMApi, - LLMModel, - MultimodalContent, - SpeechOptions, -} from "../api"; -import Locale from "../../locales"; -import { - EventStreamContentType, - fetchEventSource, -} from "@fortaine/fetch-event-source"; -import { prettyObject } from "@/app/utils/format"; -import { getClientConfig } from "@/app/config/client"; -import { - getMessageTextContent, - isVisionModel, - getTimeoutMSByModel, -} from "@/app/utils"; -import mapKeys from "lodash-es/mapKeys"; -import mapValues from "lodash-es/mapValues"; -import isArray from "lodash-es/isArray"; -import isObject from "lodash-es/isObject"; -import { fetch } from "@/app/utils/stream"; - -export interface OpenAIListModelResponse { - object: string; - data: Array<{ - id: string; - object: string; - root: string; - }>; -} - -interface RequestPayload { - Messages: { - Role: "system" | "user" | "assistant"; - Content: string | MultimodalContent[]; - }[]; - Stream?: boolean; - Model: string; - Temperature: number; - TopP: number; -} - -function capitalizeKeys(obj: any): any { - if (isArray(obj)) { - return obj.map(capitalizeKeys); - } else if (isObject(obj)) { - return mapValues( - mapKeys(obj, (value: any, key: string) => - key.replace(/(^|_)(\w)/g, (m, $1, $2) => $2.toUpperCase()), - ), - capitalizeKeys, - ); - } else { - return obj; - } -} - -export class HunyuanApi implements LLMApi { - path(): string { - const accessStore = useAccessStore.getState(); - - let baseUrl = ""; - - if (accessStore.useCustomConfig) { - baseUrl = accessStore.tencentUrl; - } - - if (baseUrl.length === 0) { - const isApp = !!getClientConfig()?.isApp; - baseUrl = isApp ? TENCENT_BASE_URL : ApiPath.Tencent; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, baseUrl.length - 1); - } - if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.Tencent)) { - baseUrl = "https://" + baseUrl; - } - - console.log("[Proxy Endpoint] ", baseUrl); - return baseUrl; - } - - extractMessage(res: any) { - return res.Choices?.at(0)?.Message?.Content ?? ""; - } - - speech(options: SpeechOptions): Promise { - throw new Error("Method not implemented."); - } - - async chat(options: ChatOptions) { - const visionModel = isVisionModel(options.config.model); - const messages = options.messages.map((v, index) => ({ - // "Messages 中 system 角色必须位于列表的最开始" - role: index !== 0 && v.role === "system" ? "user" : v.role, - content: visionModel ? v.content : getMessageTextContent(v), - })); - - const modelConfig = { - ...useAppConfig.getState().modelConfig, - ...useChatStore.getState().currentSession().mask.modelConfig, - ...{ - model: options.config.model, - }, - }; - - const requestPayload: RequestPayload = capitalizeKeys({ - model: modelConfig.model, - messages, - temperature: modelConfig.temperature, - top_p: modelConfig.top_p, - stream: options.config.stream, - }); - - console.log("[Request] Tencent payload: ", requestPayload); - - const shouldStream = !!options.config.stream; - const controller = new AbortController(); - options.onController?.(controller); - - try { - const chatPath = this.path(); - const chatPayload = { - method: "POST", - body: JSON.stringify(requestPayload), - signal: controller.signal, - headers: getHeaders(), - }; - - // make a fetch request - const requestTimeoutId = setTimeout( - () => controller.abort(), - getTimeoutMSByModel(options.config.model), - ); - - if (shouldStream) { - let responseText = ""; - let remainText = ""; - let finished = false; - let responseRes: Response; - - // animate response to make it looks smooth - function animateResponseText() { - if (finished || controller.signal.aborted) { - responseText += remainText; - console.log("[Response Animation] finished"); - if (responseText?.length === 0) { - options.onError?.(new Error("empty response from server")); - } - return; - } - - if (remainText.length > 0) { - const fetchCount = Math.max(1, Math.round(remainText.length / 60)); - const fetchText = remainText.slice(0, fetchCount); - responseText += fetchText; - remainText = remainText.slice(fetchCount); - options.onUpdate?.(responseText, fetchText); - } - - requestAnimationFrame(animateResponseText); - } - - // start animaion - animateResponseText(); - - const finish = () => { - if (!finished) { - finished = true; - options.onFinish(responseText + remainText, responseRes); - } - }; - - controller.signal.onabort = finish; - - fetchEventSource(chatPath, { - fetch: fetch as any, - ...chatPayload, - async onopen(res) { - clearTimeout(requestTimeoutId); - const contentType = res.headers.get("content-type"); - console.log( - "[Tencent] request response content type: ", - contentType, - ); - responseRes = res; - if (contentType?.startsWith("text/plain")) { - responseText = await res.clone().text(); - return finish(); - } - - if ( - !res.ok || - !res.headers - .get("content-type") - ?.startsWith(EventStreamContentType) || - res.status !== 200 - ) { - const responseTexts = [responseText]; - let extraInfo = await res.clone().text(); - try { - const resJson = await res.clone().json(); - extraInfo = prettyObject(resJson); - } catch {} - - if (res.status === 401) { - responseTexts.push(Locale.Error.Unauthorized); - } - - if (extraInfo) { - responseTexts.push(extraInfo); - } - - responseText = responseTexts.join("\n\n"); - - return finish(); - } - }, - onmessage(msg) { - if (msg.data === "[DONE]" || finished) { - return finish(); - } - const text = msg.data; - try { - const json = JSON.parse(text); - const choices = json.Choices as Array<{ - Delta: { Content: string }; - }>; - const delta = choices[0]?.Delta?.Content; - if (delta) { - remainText += delta; - } - } catch (e) { - console.error("[Request] parse error", text, msg); - } - }, - onclose() { - finish(); - }, - onerror(e) { - options.onError?.(e); - throw e; - }, - openWhenHidden: true, - }); - } else { - const res = await fetch(chatPath, chatPayload); - clearTimeout(requestTimeoutId); - - const resJson = await res.json(); - const message = this.extractMessage(resJson); - options.onFinish(message, res); - } - } catch (e) { - console.log("[Request] failed to make a chat request", e); - options.onError?.(e as Error); - } - } - async usage() { - return { - used: 0, - total: 0, - }; - } - - async models(): Promise { - return []; - } -} diff --git a/app/client/platforms/xai.ts b/app/client/platforms/xai.ts deleted file mode 100644 index 830ad4778ac..00000000000 --- a/app/client/platforms/xai.ts +++ /dev/null @@ -1,194 +0,0 @@ -"use client"; -// azure and openai, using same models. so using same LLMApi. -import { ApiPath, XAI_BASE_URL, XAI } from "@/app/constant"; -import { - useAccessStore, - useAppConfig, - useChatStore, - ChatMessageTool, - usePluginStore, -} from "@/app/store"; -import { stream } from "@/app/utils/chat"; -import { - ChatOptions, - getHeaders, - LLMApi, - LLMModel, - SpeechOptions, -} from "../api"; -import { getClientConfig } from "@/app/config/client"; -import { getTimeoutMSByModel } from "@/app/utils"; -import { preProcessImageContent } from "@/app/utils/chat"; -import { RequestPayload } from "./openai"; -import { fetch } from "@/app/utils/stream"; - -export class XAIApi implements LLMApi { - private disableListModels = true; - - path(path: string): string { - const accessStore = useAccessStore.getState(); - - let baseUrl = ""; - - if (accessStore.useCustomConfig) { - baseUrl = accessStore.xaiUrl; - } - - if (baseUrl.length === 0) { - const isApp = !!getClientConfig()?.isApp; - const apiPath = ApiPath.XAI; - baseUrl = isApp ? XAI_BASE_URL : apiPath; - } - - if (baseUrl.endsWith("/")) { - baseUrl = baseUrl.slice(0, baseUrl.length - 1); - } - if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.XAI)) { - baseUrl = "https://" + baseUrl; - } - - console.log("[Proxy Endpoint] ", baseUrl, path); - - return [baseUrl, path].join("/"); - } - - extractMessage(res: any) { - return res.choices?.at(0)?.message?.content ?? ""; - } - - speech(options: SpeechOptions): Promise { - throw new Error("Method not implemented."); - } - - async chat(options: ChatOptions) { - const messages: ChatOptions["messages"] = []; - for (const v of options.messages) { - const content = await preProcessImageContent(v.content); - messages.push({ role: v.role, content }); - } - - const modelConfig = { - ...useAppConfig.getState().modelConfig, - ...useChatStore.getState().currentSession().mask.modelConfig, - ...{ - model: options.config.model, - providerName: options.config.providerName, - }, - }; - - const requestPayload: RequestPayload = { - messages, - stream: options.config.stream, - model: modelConfig.model, - temperature: modelConfig.temperature, - presence_penalty: modelConfig.presence_penalty, - frequency_penalty: modelConfig.frequency_penalty, - top_p: modelConfig.top_p, - }; - - console.log("[Request] xai payload: ", requestPayload); - - const shouldStream = !!options.config.stream; - const controller = new AbortController(); - options.onController?.(controller); - - try { - const chatPath = this.path(XAI.ChatPath); - const chatPayload = { - method: "POST", - body: JSON.stringify(requestPayload), - signal: controller.signal, - headers: getHeaders(), - }; - - // make a fetch request - const requestTimeoutId = setTimeout( - () => controller.abort(), - getTimeoutMSByModel(options.config.model), - ); - - if (shouldStream) { - const [tools, funcs] = usePluginStore - .getState() - .getAsTools( - useChatStore.getState().currentSession().mask?.plugin || [], - ); - return stream( - chatPath, - requestPayload, - getHeaders(), - tools as any, - funcs, - controller, - // parseSSE - (text: string, runTools: ChatMessageTool[]) => { - // console.log("parseSSE", text, runTools); - const json = JSON.parse(text); - const choices = json.choices as Array<{ - delta: { - content: string; - tool_calls: ChatMessageTool[]; - }; - }>; - const tool_calls = choices[0]?.delta?.tool_calls; - if (tool_calls?.length > 0) { - const index = tool_calls[0]?.index; - const id = tool_calls[0]?.id; - const args = tool_calls[0]?.function?.arguments; - if (id) { - runTools.push({ - id, - type: tool_calls[0]?.type, - function: { - name: tool_calls[0]?.function?.name as string, - arguments: args, - }, - }); - } else { - // @ts-ignore - runTools[index]["function"]["arguments"] += args; - } - } - return choices[0]?.delta?.content; - }, - // processToolMessage, include tool_calls message and tool call results - ( - requestPayload: RequestPayload, - toolCallMessage: any, - toolCallResult: any[], - ) => { - // @ts-ignore - requestPayload?.messages?.splice( - // @ts-ignore - requestPayload?.messages?.length, - 0, - toolCallMessage, - ...toolCallResult, - ); - }, - options, - ); - } else { - const res = await fetch(chatPath, chatPayload); - clearTimeout(requestTimeoutId); - - const resJson = await res.json(); - const message = this.extractMessage(resJson); - options.onFinish(message, res); - } - } catch (e) { - console.log("[Request] failed to make a chat request", e); - options.onError?.(e as Error); - } - } - async usage() { - return { - used: 0, - total: 0, - }; - } - - async models(): Promise { - return []; - } -} diff --git a/app/command.ts b/app/command.ts deleted file mode 100644 index aec73ef53d6..00000000000 --- a/app/command.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { useEffect } from "react"; -import { useSearchParams } from "react-router-dom"; -import Locale from "./locales"; - -type Command = (param: string) => void; -interface Commands { - fill?: Command; - submit?: Command; - mask?: Command; - code?: Command; - settings?: Command; -} - -export function useCommand(commands: Commands = {}) { - const [searchParams, setSearchParams] = useSearchParams(); - - useEffect(() => { - let shouldUpdate = false; - searchParams.forEach((param, name) => { - const commandName = name as keyof Commands; - if (typeof commands[commandName] === "function") { - commands[commandName]!(param); - searchParams.delete(name); - shouldUpdate = true; - } - }); - - if (shouldUpdate) { - setSearchParams(searchParams); - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [searchParams, commands]); -} - -interface ChatCommands { - new?: Command; - newm?: Command; - next?: Command; - prev?: Command; - clear?: Command; - fork?: Command; - del?: Command; -} - -// Compatible with Chinese colon character ":" -export const ChatCommandPrefix = /^[::]/; - -export function useChatCommand(commands: ChatCommands = {}) { - function extract(userInput: string) { - const match = userInput.match(ChatCommandPrefix); - if (match) { - return userInput.slice(1) as keyof ChatCommands; - } - return userInput as keyof ChatCommands; - } - - function search(userInput: string) { - const input = extract(userInput); - const desc = Locale.Chat.Commands; - return Object.keys(commands) - .filter((c) => c.startsWith(input)) - .map((c) => ({ - title: desc[c as keyof ChatCommands], - content: ":" + c, - })); - } - - function match(userInput: string) { - const command = extract(userInput); - const matched = typeof commands[command] === "function"; - - return { - matched, - invoke: () => matched && commands[command]!(userInput), - }; - } - - return { match, search }; -} diff --git a/app/components/artifacts.module.scss b/app/components/artifacts.module.scss deleted file mode 100644 index 6bd0fd9cf53..00000000000 --- a/app/components/artifacts.module.scss +++ /dev/null @@ -1,31 +0,0 @@ -.artifacts { - display: flex; - width: 100%; - height: 100%; - flex-direction: column; - &-header { - display: flex; - align-items: center; - height: 36px; - padding: 20px; - background: var(--second); - } - &-title { - flex: 1; - text-align: center; - font-weight: bold; - font-size: 24px; - } - &-content { - flex-grow: 1; - padding: 0 20px 20px 20px; - background-color: var(--second); - } -} - -.artifacts-iframe { - width: 100%; - border: var(--border-in-light); - border-radius: 6px; - background-color: var(--gray); -} diff --git a/app/components/artifacts.tsx b/app/components/artifacts.tsx deleted file mode 100644 index 7546260d92b..00000000000 --- a/app/components/artifacts.tsx +++ /dev/null @@ -1,266 +0,0 @@ -import { - useEffect, - useState, - useRef, - useMemo, - forwardRef, - useImperativeHandle, -} from "react"; -import { useParams } from "react-router"; -import { IconButton } from "./button"; -import { nanoid } from "nanoid"; -import ExportIcon from "../icons/share.svg"; -import CopyIcon from "../icons/copy.svg"; -import DownloadIcon from "../icons/download.svg"; -import GithubIcon from "../icons/github.svg"; -import LoadingButtonIcon from "../icons/loading.svg"; -import ReloadButtonIcon from "../icons/reload.svg"; -import Locale from "../locales"; -import { Modal, showToast } from "./ui-lib"; -import { copyToClipboard, downloadAs } from "../utils"; -import { Path, ApiPath, REPO_URL } from "@/app/constant"; -import { Loading } from "./home"; -import styles from "./artifacts.module.scss"; - -type HTMLPreviewProps = { - code: string; - autoHeight?: boolean; - height?: number | string; - onLoad?: (title?: string) => void; -}; - -export type HTMLPreviewHandler = { - reload: () => void; -}; - -export const HTMLPreview = forwardRef( - function HTMLPreview(props, ref) { - const iframeRef = useRef(null); - const [frameId, setFrameId] = useState(nanoid()); - const [iframeHeight, setIframeHeight] = useState(600); - const [title, setTitle] = useState(""); - /* - * https://stackoverflow.com/questions/19739001/what-is-the-difference-between-srcdoc-and-src-datatext-html-in-an - * 1. using srcdoc - * 2. using src with dataurl: - * easy to share - * length limit (Data URIs cannot be larger than 32,768 characters.) - */ - - useEffect(() => { - const handleMessage = (e: any) => { - const { id, height, title } = e.data; - setTitle(title); - if (id == frameId) { - setIframeHeight(height); - } - }; - window.addEventListener("message", handleMessage); - return () => { - window.removeEventListener("message", handleMessage); - }; - }, [frameId]); - - useImperativeHandle(ref, () => ({ - reload: () => { - setFrameId(nanoid()); - }, - })); - - const height = useMemo(() => { - if (!props.autoHeight) return props.height || 600; - if (typeof props.height === "string") { - return props.height; - } - const parentHeight = props.height || 600; - return iframeHeight + 40 > parentHeight - ? parentHeight - : iframeHeight + 40; - }, [props.autoHeight, props.height, iframeHeight]); - - const srcDoc = useMemo(() => { - const script = ``; - if (props.code.includes("")) { - props.code.replace("", "" + script); - } - return script + props.code; - }, [props.code, frameId]); - - const handleOnLoad = () => { - if (props?.onLoad) { - props.onLoad(title); - } - }; - - return ( -