UNPKG

humanlayer

Version:

typescript client for humanlayer.dev

150 lines (146 loc) 4.01 kB
class HumanLayerException extends Error { } class HumanLayerCloudConnection { constructor(api_key, api_base_url) { this.apiKey = api_key; this.apiBaseURL = api_base_url; if (!this.apiKey) { throw new Error("HUMANLAYER_API_KEY is required for cloud approvals"); } this.apiBaseURL = this.apiBaseURL || "https://api.humanlayer.dev/humanlayer/v1"; } async request({ method, path, body }) { const resp = await fetch(`${this.apiBaseURL}${path}`, { method, headers: { Authorization: `Bearer ${this.apiKey}`, "content-type": "application/json" }, body: JSON.stringify(body) }); if (resp.status >= 400) { const err = new HumanLayerException(`${method} ${path} ${resp.status}: ${await resp.text()}`); throw err; } return resp; } async getProjectInfo() { const resp = await this.request({ method: "GET", path: "/project" }); return resp.json(); } } class CloudFunctionCallStore { constructor(connection) { this.connection = connection; } async add(item) { const resp = await this.connection.request({ method: "POST", path: "/function_calls", body: item }); const data = await resp.json(); return data; } async get(call_id) { const resp = await this.connection.request({ method: "GET", path: `/function_calls/${call_id}` }); const data = await resp.json(); return data; } async respond(call_id, status) { const resp = await this.connection.request({ method: "POST", path: `/agent/function_calls/${call_id}/respond`, body: status }); const data = await resp.json(); return data; } } class CloudHumanContactStore { constructor(connection) { this.connection = connection; } async add(item) { const resp = await this.connection.request({ method: "POST", path: "/contact_requests", body: item }); const data = await resp.json(); return data; } async get(call_id) { const resp = await this.connection.request({ method: "GET", path: `/contact_requests/${call_id}` }); const data = await resp.json(); return data; } async respond(call_id, status) { const resp = await this.connection.request({ method: "POST", path: `/agent/human_contacts/${call_id}/respond`, body: status }); const data = await resp.json(); return data; } } class CloudHumanLayerBackend { constructor(connection) { this.connection = connection; this._function_calls = new CloudFunctionCallStore(connection); this._human_contacts = new CloudHumanContactStore(connection); } functions() { return this._function_calls; } contacts() { return this._human_contacts; } } async function main() { const command = process.argv[2]; if (command === "info") { const apiKey = process.env.HUMANLAYER_API_KEY; const apiBase = process.env.HUMANLAYER_API_BASE; if (!apiKey) { console.error("Error: HUMANLAYER_API_KEY environment variable is required"); process.exit(1); } try { const connection = new HumanLayerCloudConnection(apiKey, apiBase); const projectInfo = await connection.getProjectInfo(); console.log("Project Information:"); console.log(`Name: ${projectInfo.name}`); console.log(`Slug: ${projectInfo.slug}`); if (projectInfo.organization) { console.log(`Organization: ${projectInfo.organization}`); } } catch (error) { if (error instanceof Error) { console.error("Error:", error.message); } else { console.error("An unknown error occurred"); } process.exit(1); } } else { console.log("Available commands:"); console.log(" info Show information about the current project"); } } main().catch(console.error); export { CloudHumanLayerBackend as C, HumanLayerCloudConnection as H, HumanLayerException as a };