fix(market, builder): hook up frontend and backend

This commit is contained in:
Nicholas Tindle
2024-08-01 23:17:41 -05:00
parent 6e5948eb7c
commit f5a3162f67
2 changed files with 121 additions and 86 deletions

View File

@@ -1,94 +1,120 @@
import {
AddAgentRequest,
AgentResponse,
ListAgentsParams,
AgentListResponse,
AgentDetailResponse
AddAgentRequest,
AgentResponse,
ListAgentsParams,
AgentListResponse,
AgentDetailResponse,
} from "./types";
export default class MarketplaceAPI {
private baseUrl: string;
private baseUrl: string;
constructor(
baseUrl: string = process.env.AGPT_MARKETPLACE_URL || "http://localhost:8000/api"
) {
this.baseUrl = baseUrl;
constructor(
baseUrl: string = process.env.AGPT_MARKETPLACE_URL ||
"http://localhost:8000/api"
) {
this.baseUrl = baseUrl;
}
async listAgents(params: ListAgentsParams = {}): Promise<AgentListResponse> {
const queryParams = new URLSearchParams(
Object.entries(params).filter(([_, v]) => v != null) as [string, string][]
);
return this._get(`/agents/agents?${queryParams.toString()}`);
}
async getTopDownloadedAgents(
page: number = 1,
pageSize: number = 10
): Promise<AgentListResponse> {
return this._get(
`/agents/top-downloads/agents?page=${page}&page_size=${pageSize}`
);
}
async getAgentDetails(
id: string,
version?: number
): Promise<AgentDetailResponse> {
const queryParams = new URLSearchParams();
if (version) queryParams.append("version", version.toString());
return this._get(`/agents/agents/${id}?${queryParams.toString()}`);
}
async downloadAgent(
id: string,
version?: number
): Promise<AgentDetailResponse> {
const queryParams = new URLSearchParams();
if (version) queryParams.append("version", version.toString());
return this._get(`/agents/agents/${id}/download?${queryParams.toString()}`);
}
async downloadAgentFile(id: string, version?: number): Promise<Blob> {
const queryParams = new URLSearchParams();
if (version) queryParams.append("version", version.toString());
return this._getBlob(
`/agents/${id}/download-file?${queryParams.toString()}`
);
}
async createAgentEntry(request: AddAgentRequest): Promise<AgentResponse> {
return this._post("/admin/agent", request);
}
private async _get(path: string) {
return this._request("GET", path);
}
private async _post(path: string, payload: { [key: string]: any }) {
return this._request("POST", path, payload);
}
private async _getBlob(path: string): Promise<Blob> {
const response = await fetch(this.baseUrl + path);
if (!response.ok) {
const errorData = await response.json();
console.warn(
`GET ${path} returned non-OK response:`,
errorData.detail,
response
);
throw new Error(`HTTP error ${response.status}! ${errorData.detail}`);
}
return response.blob();
}
private async _request(
method: "GET" | "POST" | "PUT" | "PATCH",
path: string,
payload?: { [key: string]: any }
) {
if (method != "GET") {
console.debug(`${method} ${path} payload:`, payload);
}
async listAgents(params: ListAgentsParams = {}): Promise<AgentListResponse> {
const queryParams = new URLSearchParams(
Object.entries(params).filter(([_, v]) => v != null) as [string, string][]
);
return this._get(`/agents?${queryParams.toString()}`);
const response = await fetch(
this.baseUrl + path,
method != "GET"
? {
method,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
}
: undefined
);
const response_data = await response.json();
if (!response.ok) {
console.warn(
`${method} ${path} returned non-OK response:`,
response_data.detail,
response
);
throw new Error(`HTTP error ${response.status}! ${response_data.detail}`);
}
async getAgentDetails(id: string, version?: number): Promise<AgentDetailResponse> {
const queryParams = new URLSearchParams();
if (version) queryParams.append('version', version.toString());
return this._get(`/agents/${id}?${queryParams.toString()}`);
}
async downloadAgent(id: string, version?: number): Promise<AgentDetailResponse> {
const queryParams = new URLSearchParams();
if (version) queryParams.append('version', version.toString());
return this._get(`/agents/${id}/download?${queryParams.toString()}`);
}
async downloadAgentFile(id: string, version?: number): Promise<Blob> {
const queryParams = new URLSearchParams();
if (version) queryParams.append('version', version.toString());
return this._getBlob(`/agents/${id}/download-file?${queryParams.toString()}`);
}
async createAgentEntry(request: AddAgentRequest): Promise<AgentResponse> {
return this._post("/admin/agent", request);
}
private async _get(path: string) {
return this._request("GET", path);
}
private async _post(path: string, payload: { [key: string]: any }) {
return this._request("POST", path, payload);
}
private async _getBlob(path: string): Promise<Blob> {
const response = await fetch(this.baseUrl + path);
if (!response.ok) {
const errorData = await response.json();
console.warn(`GET ${path} returned non-OK response:`, errorData.detail, response);
throw new Error(`HTTP error ${response.status}! ${errorData.detail}`);
}
return response.blob();
}
private async _request(
method: "GET" | "POST" | "PUT" | "PATCH",
path: string,
payload?: { [key: string]: any },
) {
if (method != "GET") {
console.debug(`${method} ${path} payload:`, payload);
}
const response = await fetch(
this.baseUrl + path,
method != "GET" ? {
method,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
} : undefined
);
const response_data = await response.json();
if (!response.ok) {
console.warn(
`${method} ${path} returned non-OK response:`, response_data.detail, response
);
throw new Error(`HTTP error ${response.status}! ${response_data.detail}`);
}
return response_data;
}
}
return response_data;
}
}

View File

@@ -3,6 +3,7 @@ import os
import dotenv
import fastapi
import fastapi.middleware.cors
import fastapi.middleware.gzip
import prisma
import sentry_sdk
@@ -51,6 +52,14 @@ app = fastapi.FastAPI(
)
app.add_middleware(fastapi.middleware.gzip.GZipMiddleware, minimum_size=1000)
# ! This is a security risk, do not use in production
app.add_middleware(
middleware_class=fastapi.middleware.cors.CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(
market.routes.agents.router, prefix="/market/agents", tags=["agents"]
)