Marketplace client

This commit is contained in:
SwiftyOS
2024-08-01 11:51:11 +02:00
parent 0c093db346
commit 55344efb1a
3 changed files with 119 additions and 0 deletions

View 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;
}
}

View File

@@ -0,0 +1,4 @@
import MarketplaceAPI from "./client";
export default MarketplaceAPI;
export * from "./types";

View 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>;
};