UNPKG

@humanlayer/sdk

Version:

typescript client for humanlayer.dev

128 lines (127 loc) 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CloudHumanLayerBackend = exports.CloudHumanContactStore = exports.CloudFunctionCallStore = exports.HumanLayerCloudConnection = void 0; const protocol_1 = require("./protocol"); 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'; // todo ping api to validate token } 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 protocol_1.HumanLayerException(`${method} ${path} ${resp.status}: ${await resp.text()}`); throw err; } return resp; } } exports.HumanLayerCloudConnection = HumanLayerCloudConnection; 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; } async escalateEmail(call_id, escalation) { const resp = await this.connection.request({ method: 'POST', path: `/agent/function_calls/${call_id}/escalate_email`, body: escalation, }); const data = await resp.json(); return data; } } exports.CloudFunctionCallStore = CloudFunctionCallStore; 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; } async escalateEmail(call_id, escalation) { const resp = await this.connection.request({ method: 'POST', path: `/agent/human_contacts/${call_id}/escalate_email`, body: escalation, }); const data = await resp.json(); return data; } } exports.CloudHumanContactStore = CloudHumanContactStore; 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; } } exports.CloudHumanLayerBackend = CloudHumanLayerBackend;