Skip to content

Commit

Permalink
Serving frontend from the forge agent server (#5252)
Browse files Browse the repository at this point in the history
  • Loading branch information
Swiftyos committed Sep 18, 2023
1 parent 7875cb6 commit 8b3a915
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion autogpts/forge/forge/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
print(logo)
database_name = os.getenv("DATABASE_STRING")
workspace = LocalWorkspace(os.getenv("AGENT_WORKSPACE"))
port = os.getenv("PORT")
port = os.getenv("PORT", 8000)

database = forge.sdk.db.AgentDB(database_name, debug_enabled=True)
agent = forge.agent.ForgeAgent(database=database, workspace=workspace)
Expand Down
10 changes: 9 additions & 1 deletion autogpts/forge/forge/sdk/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from uuid import uuid4

from fastapi import APIRouter, FastAPI, UploadFile
from fastapi.responses import RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from hypercorn.asyncio import serve
Expand Down Expand Up @@ -51,8 +53,14 @@ def start(self, port: int = 8000, router: APIRouter = base_router):
allow_headers=["*"],
)

app.include_router(router)
app.include_router(router, prefix="/api/v1")
app.mount("/app", StaticFiles(directory="../../frontend/build/web"), name="app")
app.add_middleware(AgentMiddleware, agent=self)

@app.get("/", include_in_schema=False)
async def root():
return RedirectResponse(url='/app/index.html', status_code=307)

config.loglevel = "ERROR"
config.bind = [f"0.0.0.0:{port}"]

Expand Down
4 changes: 4 additions & 0 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,13 @@ def start(agent_name):

script_dir = os.path.dirname(os.path.realpath(__file__))
agent_dir = os.path.join(script_dir, f"autogpts/{agent_name}")
frontend_dir = os.path.join(script_dir, "frontend")
frontend_build = os.path.join(frontend_dir, "build.sh")
run_command = os.path.join(agent_dir, "run")
if os.path.exists(agent_dir) and os.path.isfile(run_command):
subprocess.Popen([frontend_build], cwd=frontend_dir)
os.chdir(agent_dir)

subprocess.Popen(["./run"], cwd=agent_dir)
click.echo(f"Agent '{agent_name}' started")
elif not os.path.exists(agent_dir):
Expand Down
3 changes: 3 additions & 0 deletions frontend/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

flutter build web --base-href /app/
2 changes: 1 addition & 1 deletion frontend/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void main() async {
MultiProvider(
providers: [
Provider(
create: (context) => RestApiUtility("http://127.0.0.1:8000"),
create: (context) => RestApiUtility("http://127.0.0.1:8000/api/v1"),
),
ProxyProvider<RestApiUtility, ChatService>(
update: (context, restApiUtility, chatService) =>
Expand Down
2 changes: 1 addition & 1 deletion frontend/lib/views/task/api_base_url_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ApiBaseUrlField extends StatelessWidget {
children: [
ElevatedButton(
onPressed: () {
controller.text = 'http://127.0.0.1:8000';
controller.text = 'http://127.0.0.1:8000/api/v1';
apiSettingsViewModel.updateBaseURL(controller.text);
},
style: ElevatedButton.styleFrom(
Expand Down

0 comments on commit 8b3a915

Please sign in to comment.