UNPKG

phonic

Version:

[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2FPhonic-Co%2Fphonic-node) [![npm shield](htt

546 lines (545 loc) 30.7 kB
/** * This file was auto-generated by Fern from our API Definition. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import * as environments from "../../../../environments.mjs"; import * as core from "../../../../core/index.mjs"; import * as Phonic from "../../../index.mjs"; import { mergeHeaders, mergeOnlyDefinedHeaders } from "../../../../core/headers.mjs"; import * as errors from "../../../../errors/index.mjs"; export class Projects { constructor(_options = {}) { this._options = _options; } /** * Returns all projects in a workspace. * * @param {Projects.RequestOptions} requestOptions - Request-specific configuration. * * @throws {@link Phonic.UnauthorizedError} * @throws {@link Phonic.InternalServerError} * * @example * await client.projects.list() */ list(requestOptions) { return core.HttpResponsePromise.fromPromise(this.__list(requestOptions)); } __list(requestOptions) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; let _headers = mergeHeaders((_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, mergeOnlyDefinedHeaders({ Authorization: yield this._getAuthorizationHeader() }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers); const _response = yield ((_b = this._options.fetcher) !== null && _b !== void 0 ? _b : core.fetcher)({ url: core.url.join((_c = (yield core.Supplier.get(this._options.baseUrl))) !== null && _c !== void 0 ? _c : ((_d = (yield core.Supplier.get(this._options.environment))) !== null && _d !== void 0 ? _d : environments.PhonicEnvironment.Default) .base, "projects"), method: "GET", headers: _headers, queryParameters: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams, timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000, maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries, abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal, }); if (_response.ok) { return { data: _response.body, rawResponse: _response.rawResponse }; } if (_response.error.reason === "status-code") { switch (_response.error.statusCode) { case 401: throw new Phonic.UnauthorizedError(_response.error.body, _response.rawResponse); case 500: throw new Phonic.InternalServerError(_response.error.body, _response.rawResponse); default: throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.body, rawResponse: _response.rawResponse, }); } } switch (_response.error.reason) { case "non-json": throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.rawBody, rawResponse: _response.rawResponse, }); case "timeout": throw new errors.PhonicTimeoutError("Timeout exceeded when calling GET /projects."); case "unknown": throw new errors.PhonicError({ message: _response.error.errorMessage, rawResponse: _response.rawResponse, }); } }); } /** * Creates a new project in a workspace. * * @param {Phonic.CreateProjectRequest} request * @param {Projects.RequestOptions} requestOptions - Request-specific configuration. * * @throws {@link Phonic.BadRequestError} * @throws {@link Phonic.UnauthorizedError} * @throws {@link Phonic.ConflictError} * @throws {@link Phonic.InternalServerError} * * @example * await client.projects.create({ * name: "customer-support" * }) */ create(request, requestOptions) { return core.HttpResponsePromise.fromPromise(this.__create(request, requestOptions)); } __create(request, requestOptions) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; let _headers = mergeHeaders((_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, mergeOnlyDefinedHeaders({ Authorization: yield this._getAuthorizationHeader() }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers); const _response = yield ((_b = this._options.fetcher) !== null && _b !== void 0 ? _b : core.fetcher)({ url: core.url.join((_c = (yield core.Supplier.get(this._options.baseUrl))) !== null && _c !== void 0 ? _c : ((_d = (yield core.Supplier.get(this._options.environment))) !== null && _d !== void 0 ? _d : environments.PhonicEnvironment.Default) .base, "projects"), method: "POST", headers: _headers, contentType: "application/json", queryParameters: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams, requestType: "json", body: request, timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000, maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries, abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal, }); if (_response.ok) { return { data: _response.body, rawResponse: _response.rawResponse }; } if (_response.error.reason === "status-code") { switch (_response.error.statusCode) { case 400: throw new Phonic.BadRequestError(_response.error.body, _response.rawResponse); case 401: throw new Phonic.UnauthorizedError(_response.error.body, _response.rawResponse); case 409: throw new Phonic.ConflictError(_response.error.body, _response.rawResponse); case 500: throw new Phonic.InternalServerError(_response.error.body, _response.rawResponse); default: throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.body, rawResponse: _response.rawResponse, }); } } switch (_response.error.reason) { case "non-json": throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.rawBody, rawResponse: _response.rawResponse, }); case "timeout": throw new errors.PhonicTimeoutError("Timeout exceeded when calling POST /projects."); case "unknown": throw new errors.PhonicError({ message: _response.error.errorMessage, rawResponse: _response.rawResponse, }); } }); } /** * Returns a project by name or ID. * * @param {string} nameOrId - The name or the ID of the project to get. * @param {Projects.RequestOptions} requestOptions - Request-specific configuration. * * @throws {@link Phonic.UnauthorizedError} * @throws {@link Phonic.ForbiddenError} * @throws {@link Phonic.NotFoundError} * @throws {@link Phonic.InternalServerError} * * @example * await client.projects.get("nameOrId") */ get(nameOrId, requestOptions) { return core.HttpResponsePromise.fromPromise(this.__get(nameOrId, requestOptions)); } __get(nameOrId, requestOptions) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; let _headers = mergeHeaders((_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, mergeOnlyDefinedHeaders({ Authorization: yield this._getAuthorizationHeader() }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers); const _response = yield ((_b = this._options.fetcher) !== null && _b !== void 0 ? _b : core.fetcher)({ url: core.url.join((_c = (yield core.Supplier.get(this._options.baseUrl))) !== null && _c !== void 0 ? _c : ((_d = (yield core.Supplier.get(this._options.environment))) !== null && _d !== void 0 ? _d : environments.PhonicEnvironment.Default) .base, `projects/${encodeURIComponent(nameOrId)}`), method: "GET", headers: _headers, queryParameters: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams, timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000, maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries, abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal, }); if (_response.ok) { return { data: _response.body, rawResponse: _response.rawResponse }; } if (_response.error.reason === "status-code") { switch (_response.error.statusCode) { case 401: throw new Phonic.UnauthorizedError(_response.error.body, _response.rawResponse); case 403: throw new Phonic.ForbiddenError(_response.error.body, _response.rawResponse); case 404: throw new Phonic.NotFoundError(_response.error.body, _response.rawResponse); case 500: throw new Phonic.InternalServerError(_response.error.body, _response.rawResponse); default: throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.body, rawResponse: _response.rawResponse, }); } } switch (_response.error.reason) { case "non-json": throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.rawBody, rawResponse: _response.rawResponse, }); case "timeout": throw new errors.PhonicTimeoutError("Timeout exceeded when calling GET /projects/{nameOrId}."); case "unknown": throw new errors.PhonicError({ message: _response.error.errorMessage, rawResponse: _response.rawResponse, }); } }); } /** * Deletes a project by name or ID. * * @param {string} nameOrId - The name or the ID of the project to delete. * @param {Projects.RequestOptions} requestOptions - Request-specific configuration. * * @throws {@link Phonic.UnauthorizedError} * @throws {@link Phonic.ForbiddenError} * @throws {@link Phonic.NotFoundError} * @throws {@link Phonic.InternalServerError} * * @example * await client.projects.delete("nameOrId") */ delete(nameOrId, requestOptions) { return core.HttpResponsePromise.fromPromise(this.__delete(nameOrId, requestOptions)); } __delete(nameOrId, requestOptions) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; let _headers = mergeHeaders((_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, mergeOnlyDefinedHeaders({ Authorization: yield this._getAuthorizationHeader() }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers); const _response = yield ((_b = this._options.fetcher) !== null && _b !== void 0 ? _b : core.fetcher)({ url: core.url.join((_c = (yield core.Supplier.get(this._options.baseUrl))) !== null && _c !== void 0 ? _c : ((_d = (yield core.Supplier.get(this._options.environment))) !== null && _d !== void 0 ? _d : environments.PhonicEnvironment.Default) .base, `projects/${encodeURIComponent(nameOrId)}`), method: "DELETE", headers: _headers, queryParameters: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams, timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000, maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries, abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal, }); if (_response.ok) { return { data: _response.body, rawResponse: _response.rawResponse }; } if (_response.error.reason === "status-code") { switch (_response.error.statusCode) { case 401: throw new Phonic.UnauthorizedError(_response.error.body, _response.rawResponse); case 403: throw new Phonic.ForbiddenError(_response.error.body, _response.rawResponse); case 404: throw new Phonic.NotFoundError(_response.error.body, _response.rawResponse); case 500: throw new Phonic.InternalServerError(_response.error.body, _response.rawResponse); default: throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.body, rawResponse: _response.rawResponse, }); } } switch (_response.error.reason) { case "non-json": throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.rawBody, rawResponse: _response.rawResponse, }); case "timeout": throw new errors.PhonicTimeoutError("Timeout exceeded when calling DELETE /projects/{nameOrId}."); case "unknown": throw new errors.PhonicError({ message: _response.error.errorMessage, rawResponse: _response.rawResponse, }); } }); } /** * Updates a project by name or ID. * * @param {string} nameOrId - The name or the ID of the project to update. * @param {Phonic.UpdateProjectRequest} request * @param {Projects.RequestOptions} requestOptions - Request-specific configuration. * * @throws {@link Phonic.BadRequestError} * @throws {@link Phonic.UnauthorizedError} * @throws {@link Phonic.ForbiddenError} * @throws {@link Phonic.NotFoundError} * @throws {@link Phonic.ConflictError} * @throws {@link Phonic.InternalServerError} * * @example * await client.projects.update("nameOrId", { * name: "updated-customer-support", * default_agent: "another-agent" * }) */ update(nameOrId, request, requestOptions) { return core.HttpResponsePromise.fromPromise(this.__update(nameOrId, request, requestOptions)); } __update(nameOrId, request, requestOptions) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; let _headers = mergeHeaders((_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, mergeOnlyDefinedHeaders({ Authorization: yield this._getAuthorizationHeader() }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers); const _response = yield ((_b = this._options.fetcher) !== null && _b !== void 0 ? _b : core.fetcher)({ url: core.url.join((_c = (yield core.Supplier.get(this._options.baseUrl))) !== null && _c !== void 0 ? _c : ((_d = (yield core.Supplier.get(this._options.environment))) !== null && _d !== void 0 ? _d : environments.PhonicEnvironment.Default) .base, `projects/${encodeURIComponent(nameOrId)}`), method: "PATCH", headers: _headers, contentType: "application/json", queryParameters: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams, requestType: "json", body: request, timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000, maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries, abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal, }); if (_response.ok) { return { data: _response.body, rawResponse: _response.rawResponse }; } if (_response.error.reason === "status-code") { switch (_response.error.statusCode) { case 400: throw new Phonic.BadRequestError(_response.error.body, _response.rawResponse); case 401: throw new Phonic.UnauthorizedError(_response.error.body, _response.rawResponse); case 403: throw new Phonic.ForbiddenError(_response.error.body, _response.rawResponse); case 404: throw new Phonic.NotFoundError(_response.error.body, _response.rawResponse); case 409: throw new Phonic.ConflictError(_response.error.body, _response.rawResponse); case 500: throw new Phonic.InternalServerError(_response.error.body, _response.rawResponse); default: throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.body, rawResponse: _response.rawResponse, }); } } switch (_response.error.reason) { case "non-json": throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.rawBody, rawResponse: _response.rawResponse, }); case "timeout": throw new errors.PhonicTimeoutError("Timeout exceeded when calling PATCH /projects/{nameOrId}."); case "unknown": throw new errors.PhonicError({ message: _response.error.errorMessage, rawResponse: _response.rawResponse, }); } }); } /** * Returns all conversation evaluation prompts for a project. * * @param {string} id - The ID of the project. * @param {Projects.RequestOptions} requestOptions - Request-specific configuration. * * @throws {@link Phonic.UnauthorizedError} * @throws {@link Phonic.ForbiddenError} * @throws {@link Phonic.NotFoundError} * @throws {@link Phonic.InternalServerError} * * @example * await client.projects.listEvalPrompts("id") */ listEvalPrompts(id, requestOptions) { return core.HttpResponsePromise.fromPromise(this.__listEvalPrompts(id, requestOptions)); } __listEvalPrompts(id, requestOptions) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; let _headers = mergeHeaders((_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, mergeOnlyDefinedHeaders({ Authorization: yield this._getAuthorizationHeader() }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers); const _response = yield ((_b = this._options.fetcher) !== null && _b !== void 0 ? _b : core.fetcher)({ url: core.url.join((_c = (yield core.Supplier.get(this._options.baseUrl))) !== null && _c !== void 0 ? _c : ((_d = (yield core.Supplier.get(this._options.environment))) !== null && _d !== void 0 ? _d : environments.PhonicEnvironment.Default) .base, `projects/${encodeURIComponent(id)}/conversation_eval_prompts`), method: "GET", headers: _headers, queryParameters: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams, timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000, maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries, abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal, }); if (_response.ok) { return { data: _response.body, rawResponse: _response.rawResponse, }; } if (_response.error.reason === "status-code") { switch (_response.error.statusCode) { case 401: throw new Phonic.UnauthorizedError(_response.error.body, _response.rawResponse); case 403: throw new Phonic.ForbiddenError(_response.error.body, _response.rawResponse); case 404: throw new Phonic.NotFoundError(_response.error.body, _response.rawResponse); case 500: throw new Phonic.InternalServerError(_response.error.body, _response.rawResponse); default: throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.body, rawResponse: _response.rawResponse, }); } } switch (_response.error.reason) { case "non-json": throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.rawBody, rawResponse: _response.rawResponse, }); case "timeout": throw new errors.PhonicTimeoutError("Timeout exceeded when calling GET /projects/{id}/conversation_eval_prompts."); case "unknown": throw new errors.PhonicError({ message: _response.error.errorMessage, rawResponse: _response.rawResponse, }); } }); } /** * Creates a new conversation evaluation prompt for a project. * * @param {string} id - The ID of the project. * @param {Phonic.CreateConversationEvalPromptRequest} request * @param {Projects.RequestOptions} requestOptions - Request-specific configuration. * * @throws {@link Phonic.BadRequestError} * @throws {@link Phonic.UnauthorizedError} * @throws {@link Phonic.ForbiddenError} * @throws {@link Phonic.NotFoundError} * @throws {@link Phonic.ConflictError} * @throws {@link Phonic.InternalServerError} * * @example * await client.projects.createEvalPrompt("id", { * name: "test_prompt", * prompt: "The assistant used the word chocolate in the conversation" * }) */ createEvalPrompt(id, request, requestOptions) { return core.HttpResponsePromise.fromPromise(this.__createEvalPrompt(id, request, requestOptions)); } __createEvalPrompt(id, request, requestOptions) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; let _headers = mergeHeaders((_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, mergeOnlyDefinedHeaders({ Authorization: yield this._getAuthorizationHeader() }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers); const _response = yield ((_b = this._options.fetcher) !== null && _b !== void 0 ? _b : core.fetcher)({ url: core.url.join((_c = (yield core.Supplier.get(this._options.baseUrl))) !== null && _c !== void 0 ? _c : ((_d = (yield core.Supplier.get(this._options.environment))) !== null && _d !== void 0 ? _d : environments.PhonicEnvironment.Default) .base, `projects/${encodeURIComponent(id)}/conversation_eval_prompts`), method: "POST", headers: _headers, contentType: "application/json", queryParameters: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams, requestType: "json", body: request, timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000, maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries, abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal, }); if (_response.ok) { return { data: _response.body, rawResponse: _response.rawResponse, }; } if (_response.error.reason === "status-code") { switch (_response.error.statusCode) { case 400: throw new Phonic.BadRequestError(_response.error.body, _response.rawResponse); case 401: throw new Phonic.UnauthorizedError(_response.error.body, _response.rawResponse); case 403: throw new Phonic.ForbiddenError(_response.error.body, _response.rawResponse); case 404: throw new Phonic.NotFoundError(_response.error.body, _response.rawResponse); case 409: throw new Phonic.ConflictError(_response.error.body, _response.rawResponse); case 500: throw new Phonic.InternalServerError(_response.error.body, _response.rawResponse); default: throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.body, rawResponse: _response.rawResponse, }); } } switch (_response.error.reason) { case "non-json": throw new errors.PhonicError({ statusCode: _response.error.statusCode, body: _response.error.rawBody, rawResponse: _response.rawResponse, }); case "timeout": throw new errors.PhonicTimeoutError("Timeout exceeded when calling POST /projects/{id}/conversation_eval_prompts."); case "unknown": throw new errors.PhonicError({ message: _response.error.errorMessage, rawResponse: _response.rawResponse, }); } }); } _getAuthorizationHeader() { return __awaiter(this, void 0, void 0, function* () { var _a; const bearer = (_a = (yield core.Supplier.get(this._options.apiKey))) !== null && _a !== void 0 ? _a : process === null || process === void 0 ? void 0 : process.env["PHONIC_API_KEY"]; if (bearer != null) { return `Bearer ${bearer}`; } return undefined; }); } }