phonic
Version:
[](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2FPhonic-Co%2Fphonic-node) [ • 25.9 kB
JavaScript
/**
* 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());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
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 Tools {
constructor(_options = {}) {
this._options = _options;
}
/**
* Returns all custom tools for the organization.
*
* @param {Phonic.ToolsListRequest} request
* @param {Tools.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link Phonic.NotFoundError}
*
* @example
* await client.tools.list({
* project: "main"
* })
*/
list(request = {}, requestOptions) {
return core.HttpResponsePromise.fromPromise(this.__list(request, requestOptions));
}
__list() {
return __awaiter(this, arguments, void 0, function* (request = {}, requestOptions) {
var _a, _b, _c, _d;
const { project } = request;
const _queryParams = {};
if (project != null) {
_queryParams["project"] = project;
}
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, "tools"),
method: "GET",
headers: _headers,
queryParameters: Object.assign(Object.assign({}, _queryParams), 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 404:
throw new Phonic.NotFoundError(_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 /tools.");
case "unknown":
throw new errors.PhonicError({
message: _response.error.errorMessage,
rawResponse: _response.rawResponse,
});
}
});
}
/**
* Creates a new tool in a project.
*
* @param {Phonic.CreateToolRequest} request
* @param {Tools.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link Phonic.BadRequestError}
* @throws {@link Phonic.ForbiddenError}
* @throws {@link Phonic.ConflictError}
*
* @example
* await client.tools.create({
* project: "main",
* name: "context_printer",
* description: "Gets the specific context for fixing our printer",
* type: "custom_context",
* execution_mode: "sync",
* parameters: [{
* type: "string",
* name: "name",
* description: "description",
* is_required: true
* }],
* require_speech_before_tool_call: false,
* forbid_speech_after_tool_call: false,
* allow_tool_chaining: true
* })
*
* @example
* await client.tools.create({
* project: "main",
* name: "book_appointment",
* description: "Books an appointment in the calendar system",
* type: "custom_webhook",
* execution_mode: "sync",
* parameters: [{
* type: "string",
* name: "date",
* description: "The date for the appointment in YYYY-MM-DD format",
* is_required: true,
* location: "request_body"
* }, {
* type: "string",
* name: "time",
* description: "The time for the appointment in HH:MM format",
* is_required: true,
* location: "request_body"
* }],
* endpoint_method: "POST",
* endpoint_url: "https://api.example.com/book-appointment",
* endpoint_headers: {
* "Authorization": "Bearer token123",
* "Content-Type": "application/json"
* },
* endpoint_timeout_ms: 5000,
* require_speech_before_tool_call: false,
* wait_for_speech_before_tool_call: false,
* forbid_speech_after_tool_call: false,
* allow_tool_chaining: true
* })
*
* @example
* await client.tools.create({
* project: "main",
* name: "check_inventory",
* description: "Checks product inventory levels",
* type: "custom_websocket",
* execution_mode: "async",
* parameters: [{
* type: "string",
* name: "product_id",
* description: "The product ID to check",
* is_required: true
* }],
* tool_call_output_timeout_ms: 5000,
* require_speech_before_tool_call: false,
* wait_for_speech_before_tool_call: false,
* forbid_speech_after_tool_call: false,
* allow_tool_chaining: true
* })
*
* @example
* await client.tools.create({
* project: "main",
* name: "transfer_to_support",
* description: "Transfers the caller to the support team",
* type: "built_in_transfer_to_phone_number",
* execution_mode: "sync",
* phone_number: "+15551234567",
* dtmf: "1234",
* use_agent_phone_number: true,
* detect_voicemail: false,
* require_speech_before_tool_call: false
* })
*
* @example
* await client.tools.create({
* project: "main",
* name: "transfer_to_specialist",
* description: "Transfers the caller to a specialist agent",
* type: "built_in_transfer_to_agent",
* execution_mode: "sync",
* agents_to_transfer_to: ["sales-agent", "support-agent", "technical-agent"],
* require_speech_before_tool_call: false
* })
*/
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;
const { project } = request, _body = __rest(request, ["project"]);
const _queryParams = {};
if (project != null) {
_queryParams["project"] = project;
}
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, "tools"),
method: "POST",
headers: _headers,
contentType: "application/json",
queryParameters: Object.assign(Object.assign({}, _queryParams), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams),
requestType: "json",
body: _body,
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 403:
throw new Phonic.ForbiddenError(_response.error.body, _response.rawResponse);
case 409:
throw new Phonic.ConflictError(_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 /tools.");
case "unknown":
throw new errors.PhonicError({
message: _response.error.errorMessage,
rawResponse: _response.rawResponse,
});
}
});
}
/**
* Returns a tool by name or ID.
*
* @param {string} nameOrId - The name or the ID of the tool to get.
* @param {Phonic.ToolsGetRequest} request
* @param {Tools.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link Phonic.ForbiddenError}
* @throws {@link Phonic.NotFoundError}
*
* @example
* await client.tools.get("nameOrId", {
* project: "main"
* })
*/
get(nameOrId, request = {}, requestOptions) {
return core.HttpResponsePromise.fromPromise(this.__get(nameOrId, request, requestOptions));
}
__get(nameOrId_1) {
return __awaiter(this, arguments, void 0, function* (nameOrId, request = {}, requestOptions) {
var _a, _b, _c, _d;
const { project } = request;
const _queryParams = {};
if (project != null) {
_queryParams["project"] = project;
}
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, `tools/${encodeURIComponent(nameOrId)}`),
method: "GET",
headers: _headers,
queryParameters: Object.assign(Object.assign({}, _queryParams), 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 403:
throw new Phonic.ForbiddenError(_response.error.body, _response.rawResponse);
case 404:
throw new Phonic.NotFoundError(_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 /tools/{nameOrId}.");
case "unknown":
throw new errors.PhonicError({
message: _response.error.errorMessage,
rawResponse: _response.rawResponse,
});
}
});
}
/**
* Deletes a tool by name or ID.
*
* @param {string} nameOrId - The name or the ID of the tool to delete.
* @param {Phonic.ToolsDeleteRequest} request
* @param {Tools.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link Phonic.NotFoundError}
*
* @example
* await client.tools.delete("nameOrId", {
* project: "main"
* })
*/
delete(nameOrId, request = {}, requestOptions) {
return core.HttpResponsePromise.fromPromise(this.__delete(nameOrId, request, requestOptions));
}
__delete(nameOrId_1) {
return __awaiter(this, arguments, void 0, function* (nameOrId, request = {}, requestOptions) {
var _a, _b, _c, _d;
const { project } = request;
const _queryParams = {};
if (project != null) {
_queryParams["project"] = project;
}
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, `tools/${encodeURIComponent(nameOrId)}`),
method: "DELETE",
headers: _headers,
queryParameters: Object.assign(Object.assign({}, _queryParams), 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 404:
throw new Phonic.NotFoundError(_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 /tools/{nameOrId}.");
case "unknown":
throw new errors.PhonicError({
message: _response.error.errorMessage,
rawResponse: _response.rawResponse,
});
}
});
}
/**
* Updates a tool by name or ID.
*
* @param {string} nameOrId - The name or the ID of the tool to update.
* @param {Phonic.UpdateToolRequest} request
* @param {Tools.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link Phonic.BadRequestError}
* @throws {@link Phonic.NotFoundError}
* @throws {@link Phonic.ConflictError}
*
* @example
* await client.tools.update("nameOrId", {
* project: "main",
* description: "Updated description for booking appointments with enhanced features",
* endpoint_headers: {
* "Authorization": "Bearer updated_token456"
* },
* endpoint_timeout_ms: 7000
* })
*/
update(nameOrId, request = {}, requestOptions) {
return core.HttpResponsePromise.fromPromise(this.__update(nameOrId, request, requestOptions));
}
__update(nameOrId_1) {
return __awaiter(this, arguments, void 0, function* (nameOrId, request = {}, requestOptions) {
var _a, _b, _c, _d;
const { project } = request, _body = __rest(request, ["project"]);
const _queryParams = {};
if (project != null) {
_queryParams["project"] = project;
}
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, `tools/${encodeURIComponent(nameOrId)}`),
method: "PATCH",
headers: _headers,
contentType: "application/json",
queryParameters: Object.assign(Object.assign({}, _queryParams), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams),
requestType: "json",
body: _body,
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 404:
throw new Phonic.NotFoundError(_response.error.body, _response.rawResponse);
case 409:
throw new Phonic.ConflictError(_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 /tools/{nameOrId}.");
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;
});
}
}