mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-04-08 03:00:28 -04:00
Marketplace client
This commit is contained in:
74
rnd/autogpt_builder/src/lib/marketplace-api/client.ts
Normal file
74
rnd/autogpt_builder/src/lib/marketplace-api/client.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import {
|
||||
AddAgentRequest,
|
||||
Agent,
|
||||
AgentList,
|
||||
AgentDetail
|
||||
|
||||
} from "./types"
|
||||
|
||||
export default class MarketplaceAPI {
|
||||
private baseUrl: string;
|
||||
private messageHandlers: { [key: string]: (data: any) => void } = {};
|
||||
|
||||
constructor(
|
||||
baseUrl: string = process.env.AGPT_SERVER_URL || "http://localhost:8000/api"
|
||||
) {
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
|
||||
async listAgents(): Promise<AgentList> {
|
||||
return this._get("/agents")
|
||||
}
|
||||
|
||||
async getAgent(id: string): Promise<AgentDetail> {
|
||||
return this._get(`/agents/${id}`);
|
||||
}
|
||||
|
||||
async addAgent(agent: AddAgentRequest): Promise<Agent> {
|
||||
return this._post("/agents", agent);
|
||||
}
|
||||
|
||||
async downloadAgent(id: string): Promise<Blob> {
|
||||
return this._get(`/agents/${id}/download`);
|
||||
}
|
||||
|
||||
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 _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;
|
||||
}
|
||||
}
|
||||
4
rnd/autogpt_builder/src/lib/marketplace-api/index.ts
Normal file
4
rnd/autogpt_builder/src/lib/marketplace-api/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import MarketplaceAPI from "./client";
|
||||
|
||||
export default MarketplaceAPI;
|
||||
export * from "./types";
|
||||
41
rnd/autogpt_builder/src/lib/marketplace-api/types.ts
Normal file
41
rnd/autogpt_builder/src/lib/marketplace-api/types.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
// Define type aliases for request and response data structures using your preferred style
|
||||
|
||||
export type AddAgentRequest = {
|
||||
graph: Record<string, any>;
|
||||
author: string;
|
||||
keywords: string[];
|
||||
categories: string[];
|
||||
};
|
||||
|
||||
export type Agent = {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
author: string;
|
||||
keywords: string[];
|
||||
categories: string[];
|
||||
version: number;
|
||||
createdAt: string; // ISO8601 datetime string
|
||||
updatedAt: string; // ISO8601 datetime string
|
||||
};
|
||||
|
||||
export type AgentList = {
|
||||
agents: Agent[];
|
||||
total_count: number;
|
||||
page: number;
|
||||
page_size: number;
|
||||
total_pages: number;
|
||||
};
|
||||
|
||||
export type AgentDetail = {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
author: string;
|
||||
keywords: string[];
|
||||
categories: string[];
|
||||
version: number;
|
||||
createdAt: string; // ISO8601 datetime string
|
||||
updatedAt: string; // ISO8601 datetime string
|
||||
graph: Record<string, any>;
|
||||
};
|
||||
Reference in New Issue
Block a user