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) [ • 24.6 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 { normalizeClientOptionsWithAuth } from "../../../../BaseClient.mjs";
import { mergeHeaders } from "../../../../core/headers.mjs";
import * as core from "../../../../core/index.mjs";
import * as environments from "../../../../environments.mjs";
import { handleNonStatusCodeError } from "../../../../errors/handleNonStatusCodeError.mjs";
import * as errors from "../../../../errors/index.mjs";
import * as Phonic from "../../../index.mjs";
export class ToolsClient {
constructor(options = {}) {
this._options = normalizeClientOptionsWithAuth(options);
}
/**
* Returns all custom tools for the organization.
*
* @param {Phonic.ToolsListRequest} request
* @param {ToolsClient.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, _e, _f, _g, _h, _j, _k;
const { project } = request;
const _queryParams = {
project,
};
const _authRequest = yield this._options.authProvider.getAuthRequest();
const _headers = mergeHeaders(_authRequest.headers, (_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, 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: ((_g = (_e = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) !== null && _e !== void 0 ? _e : (_f = this._options) === null || _f === void 0 ? void 0 : _f.timeoutInSeconds) !== null && _g !== void 0 ? _g : 60) * 1000,
maxRetries: (_h = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries) !== null && _h !== void 0 ? _h : (_j = this._options) === null || _j === void 0 ? void 0 : _j.maxRetries,
abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
fetchFn: (_k = this._options) === null || _k === void 0 ? void 0 : _k.fetch,
logging: this._options.logging,
});
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,
});
}
}
return handleNonStatusCodeError(_response.error, _response.rawResponse, "GET", "/tools");
});
}
/**
* Creates a new tool in a project.
*
* @param {Phonic.CreateToolRequest} request
* @param {ToolsClient.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,
* context: "Press the A button 5 times then gently shake the printer."
* })
*
* @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,
* wait_for_response: false
* })
*
* @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",
* dynamic_dtmf: false,
* 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, _e, _f, _g, _h, _j, _k;
const { project } = request, _body = __rest(request, ["project"]);
const _queryParams = {
project,
};
const _authRequest = yield this._options.authProvider.getAuthRequest();
const _headers = mergeHeaders(_authRequest.headers, (_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, 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: ((_g = (_e = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) !== null && _e !== void 0 ? _e : (_f = this._options) === null || _f === void 0 ? void 0 : _f.timeoutInSeconds) !== null && _g !== void 0 ? _g : 60) * 1000,
maxRetries: (_h = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries) !== null && _h !== void 0 ? _h : (_j = this._options) === null || _j === void 0 ? void 0 : _j.maxRetries,
abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
fetchFn: (_k = this._options) === null || _k === void 0 ? void 0 : _k.fetch,
logging: this._options.logging,
});
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,
});
}
}
return handleNonStatusCodeError(_response.error, _response.rawResponse, "POST", "/tools");
});
}
/**
* 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 {ToolsClient.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, _e, _f, _g, _h, _j, _k;
const { project } = request;
const _queryParams = {
project,
};
const _authRequest = yield this._options.authProvider.getAuthRequest();
const _headers = mergeHeaders(_authRequest.headers, (_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, 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/${core.url.encodePathParam(nameOrId)}`),
method: "GET",
headers: _headers,
queryParameters: Object.assign(Object.assign({}, _queryParams), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams),
timeoutMs: ((_g = (_e = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) !== null && _e !== void 0 ? _e : (_f = this._options) === null || _f === void 0 ? void 0 : _f.timeoutInSeconds) !== null && _g !== void 0 ? _g : 60) * 1000,
maxRetries: (_h = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries) !== null && _h !== void 0 ? _h : (_j = this._options) === null || _j === void 0 ? void 0 : _j.maxRetries,
abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
fetchFn: (_k = this._options) === null || _k === void 0 ? void 0 : _k.fetch,
logging: this._options.logging,
});
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,
});
}
}
return handleNonStatusCodeError(_response.error, _response.rawResponse, "GET", "/tools/{nameOrId}");
});
}
/**
* 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 {ToolsClient.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, _e, _f, _g, _h, _j, _k;
const { project } = request;
const _queryParams = {
project,
};
const _authRequest = yield this._options.authProvider.getAuthRequest();
const _headers = mergeHeaders(_authRequest.headers, (_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, 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/${core.url.encodePathParam(nameOrId)}`),
method: "DELETE",
headers: _headers,
queryParameters: Object.assign(Object.assign({}, _queryParams), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams),
timeoutMs: ((_g = (_e = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) !== null && _e !== void 0 ? _e : (_f = this._options) === null || _f === void 0 ? void 0 : _f.timeoutInSeconds) !== null && _g !== void 0 ? _g : 60) * 1000,
maxRetries: (_h = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries) !== null && _h !== void 0 ? _h : (_j = this._options) === null || _j === void 0 ? void 0 : _j.maxRetries,
abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
fetchFn: (_k = this._options) === null || _k === void 0 ? void 0 : _k.fetch,
logging: this._options.logging,
});
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,
});
}
}
return handleNonStatusCodeError(_response.error, _response.rawResponse, "DELETE", "/tools/{nameOrId}");
});
}
/**
* 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 {ToolsClient.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, _e, _f, _g, _h, _j, _k;
const { project } = request, _body = __rest(request, ["project"]);
const _queryParams = {
project,
};
const _authRequest = yield this._options.authProvider.getAuthRequest();
const _headers = mergeHeaders(_authRequest.headers, (_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, 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/${core.url.encodePathParam(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: ((_g = (_e = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) !== null && _e !== void 0 ? _e : (_f = this._options) === null || _f === void 0 ? void 0 : _f.timeoutInSeconds) !== null && _g !== void 0 ? _g : 60) * 1000,
maxRetries: (_h = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries) !== null && _h !== void 0 ? _h : (_j = this._options) === null || _j === void 0 ? void 0 : _j.maxRetries,
abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
fetchFn: (_k = this._options) === null || _k === void 0 ? void 0 : _k.fetch,
logging: this._options.logging,
});
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,
});
}
}
return handleNonStatusCodeError(_response.error, _response.rawResponse, "PATCH", "/tools/{nameOrId}");
});
}
}