Skip to content

Commit

Permalink
feat: support api proxy (#266)
Browse files Browse the repository at this point in the history
1. proxy to rewrite all front-end request to local server
2. remove .env.example, commit .env as default file
  • Loading branch information
plutoless authored Sep 4, 2024
1 parent c2d4543 commit 5a70923
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 63 deletions.
1 change: 1 addition & 0 deletions playground/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AGENT_SERVER_URL=http://localhost:8080
2 changes: 0 additions & 2 deletions playground/.env.example

This file was deleted.

3 changes: 2 additions & 1 deletion playground/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ web_modules/
.yarn-integrity

# dotenv environment variable files
.env
# .env
!.env
.env.development.local
.env.test.local
.env.production.local
Expand Down
2 changes: 1 addition & 1 deletion playground/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FROM base AS builder

WORKDIR /app

COPY .env.example .env
# COPY .env.example .env
COPY . .

RUN npm i && \
Expand Down
32 changes: 32 additions & 0 deletions playground/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
/** @type {import('next').NextConfig} */

const { AGENT_SERVER_URL } = process.env;

// Check if environment variables are available
if (!AGENT_SERVER_URL) {
throw "Environment variables AGENT_SERVER_URL are not available";
}

const nextConfig = {
// basePath: '/ai-agent',
// output: 'export',
output: 'standalone',
reactStrictMode: false,
async rewrites() {
return [
// customize agents start at /app/api/agents/start.tsx
{
source: '/api/agents/start',
destination: '/api/agents/start',
},
// Proxy all other agents API requests
{
source: '/api/agents/:path*',
destination: `${AGENT_SERVER_URL}/:path*`,
},
// Proxy all other documents requests
{
source: '/api/vector/:path*',
destination: `${AGENT_SERVER_URL}/vector/:path*`,
},
// Proxy all other documents requests
{
source: '/api/token/:path*',
destination: `${AGENT_SERVER_URL}/token/:path*`,
},
];
},
webpack(config) {
// Grab the existing rule that handles SVG imports
const fileLoaderRule = config.module.rules.find((rule) =>
Expand Down
48 changes: 0 additions & 48 deletions playground/src/app/api/agents/stop/route.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions playground/src/common/constant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { IOptions, ColorItem, LanguageOptionItem, VoiceOptionItem, GraphOptionItem } from "@/types"

export const REQUEST_URL = process.env.NEXT_PUBLIC_REQUEST_URL ?? ""
export const GITHUB_URL = "https://github.com/TEN-framework/ASTRA.ai"
export const OPTIONS_KEY = "__options__"
export const DEFAULT_OPTIONS: IOptions = {
Expand Down
15 changes: 8 additions & 7 deletions playground/src/common/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { AnyObject } from "antd/es/_util/type"
import { REQUEST_URL } from "./constant"
import { genUUID } from "./utils"
import { Language } from "@/types"

Expand All @@ -17,7 +15,8 @@ interface GenAgoraDataConfig {
}

export const apiGenAgoraData = async (config: GenAgoraDataConfig) => {
const url = `${REQUEST_URL}/token/generate`
// the request will be rewrite at next.config.mjs to send to $AGENT_SERVER_URL
const url = `/api/token/generate`
const { userId, channel } = config
const data = {
request_id: genUUID(),
Expand Down Expand Up @@ -59,7 +58,7 @@ export const apiStartService = async (config: StartRequestConfig): Promise<any>
}

export const apiStopService = async (channel: string) => {
// look at app/api/agents/stop/route.tsx for the server-side implementation
// the request will be rewrite at next.config.mjs to send to $AGENT_SERVER_URL
const url = `/api/agents/stop`
const data = {
request_id: genUUID(),
Expand All @@ -77,7 +76,7 @@ export const apiStopService = async (channel: string) => {
}

export const apiGetDocumentList = async () => {
const url = `${REQUEST_URL}/vector/document/preset/list`
const url = `/api/vector/document/preset/list`
let resp: any = await fetch(url, {
method: "GET",
headers: {
Expand All @@ -92,7 +91,8 @@ export const apiGetDocumentList = async () => {
}

export const apiUpdateDocument = async (options: { channel: string, collection: string, fileName: string }) => {
const url = `${REQUEST_URL}/vector/document/update`
// the request will be rewrite at next.config.mjs to send to $AGENT_SERVER_URL
const url = `/api/vector/document/update`
const { channel, collection, fileName } = options
const data = {
request_id: genUUID(),
Expand All @@ -114,7 +114,8 @@ export const apiUpdateDocument = async (options: { channel: string, collection:

// ping/pong
export const apiPing = async (channel: string) => {
const url = `${REQUEST_URL}/ping`
// the request will be rewrite at next.config.mjs to send to $AGENT_SERVER_URL
const url = `/api/agents/ping`
const data = {
request_id: genUUID(),
channel_name: channel
Expand Down
4 changes: 2 additions & 2 deletions playground/src/components/pdfSelect/upload/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Select, Button, message, Upload, UploadProps } from "antd"
import { useState } from "react"
import { PlusOutlined, LoadingOutlined } from '@ant-design/icons';
import { REQUEST_URL, useAppSelector, genUUID } from "@/common"
import { useAppSelector, genUUID } from "@/common"
import { IPdfData } from "@/types"

import styles from "./index.module.scss"
Expand All @@ -22,7 +22,7 @@ const PdfUpload = (props: PdfSelectProps) => {
accept: "application/pdf",
maxCount: 1,
showUploadList: false,
action: `${REQUEST_URL}/vector/document/upload`,
action: `/api/vector/document/upload`,
data: {
channel_name: channel,
uid: String(userId),
Expand Down

0 comments on commit 5a70923

Please sign in to comment.