phonic 0.30.33 → 0.30.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dist/cjs/Client.js +2 -2
  2. package/dist/cjs/api/resources/agents/client/Client.d.ts +47 -12
  3. package/dist/cjs/api/resources/agents/client/Client.js +119 -18
  4. package/dist/cjs/api/resources/agents/client/requests/AgentsAddCustomPhoneNumberRequest.d.ts +23 -1
  5. package/dist/cjs/api/resources/agents/client/requests/AgentsCreateRequest.d.ts +11 -3
  6. package/dist/cjs/api/resources/agents/client/requests/AgentsDeleteCustomPhoneNumberRequest.d.ts +16 -0
  7. package/dist/cjs/api/resources/agents/client/requests/AgentsDeleteCustomPhoneNumberRequest.js +5 -0
  8. package/dist/cjs/api/resources/agents/client/requests/AgentsUpdatePhoneNumberRequest.d.ts +38 -0
  9. package/dist/cjs/api/resources/agents/client/requests/AgentsUpdatePhoneNumberRequest.js +5 -0
  10. package/dist/cjs/api/resources/agents/client/requests/UpdateAgentRequest.d.ts +13 -5
  11. package/dist/cjs/api/resources/agents/client/requests/UpsertAgentRequest.d.ts +2 -2
  12. package/dist/cjs/api/resources/agents/client/requests/index.d.ts +2 -1
  13. package/dist/cjs/api/resources/agents/types/AgentsDeleteCustomPhoneNumberResponse.d.ts +6 -0
  14. package/dist/cjs/api/resources/agents/types/AgentsDeleteCustomPhoneNumberResponse.js +5 -0
  15. package/dist/cjs/api/resources/agents/types/AgentsUpdatePhoneNumberResponse.d.ts +6 -0
  16. package/dist/cjs/api/resources/agents/types/AgentsUpdatePhoneNumberResponse.js +5 -0
  17. package/dist/cjs/api/resources/agents/types/index.d.ts +2 -1
  18. package/dist/cjs/api/resources/agents/types/index.js +2 -1
  19. package/dist/cjs/api/resources/conversations/client/Client.d.ts +1 -1
  20. package/dist/cjs/api/resources/conversations/client/Client.js +1 -1
  21. package/dist/cjs/api/resources/conversations/client/requests/OutboundCallRequest.d.ts +1 -1
  22. package/dist/cjs/api/types/Agent.d.ts +8 -0
  23. package/dist/cjs/api/types/CreateAgentRequest.d.ts +11 -3
  24. package/dist/cjs/version.d.ts +1 -1
  25. package/dist/cjs/version.js +1 -1
  26. package/dist/esm/Client.mjs +2 -2
  27. package/dist/esm/api/resources/agents/client/Client.d.mts +47 -12
  28. package/dist/esm/api/resources/agents/client/Client.mjs +119 -18
  29. package/dist/esm/api/resources/agents/client/requests/AgentsAddCustomPhoneNumberRequest.d.mts +23 -1
  30. package/dist/esm/api/resources/agents/client/requests/AgentsCreateRequest.d.mts +11 -3
  31. package/dist/esm/api/resources/agents/client/requests/AgentsDeleteCustomPhoneNumberRequest.d.mts +16 -0
  32. package/dist/esm/api/resources/agents/client/requests/AgentsDeleteCustomPhoneNumberRequest.mjs +4 -0
  33. package/dist/esm/api/resources/agents/client/requests/AgentsUpdatePhoneNumberRequest.d.mts +38 -0
  34. package/dist/esm/api/resources/agents/client/requests/AgentsUpdatePhoneNumberRequest.mjs +4 -0
  35. package/dist/esm/api/resources/agents/client/requests/UpdateAgentRequest.d.mts +13 -5
  36. package/dist/esm/api/resources/agents/client/requests/UpsertAgentRequest.d.mts +2 -2
  37. package/dist/esm/api/resources/agents/client/requests/index.d.mts +2 -1
  38. package/dist/esm/api/resources/agents/types/AgentsDeleteCustomPhoneNumberResponse.d.mts +6 -0
  39. package/dist/esm/api/resources/agents/types/AgentsDeleteCustomPhoneNumberResponse.mjs +4 -0
  40. package/dist/esm/api/resources/agents/types/AgentsUpdatePhoneNumberResponse.d.mts +6 -0
  41. package/dist/esm/api/resources/agents/types/AgentsUpdatePhoneNumberResponse.mjs +4 -0
  42. package/dist/esm/api/resources/agents/types/index.d.mts +2 -1
  43. package/dist/esm/api/resources/agents/types/index.mjs +2 -1
  44. package/dist/esm/api/resources/conversations/client/Client.d.mts +1 -1
  45. package/dist/esm/api/resources/conversations/client/Client.mjs +1 -1
  46. package/dist/esm/api/resources/conversations/client/requests/OutboundCallRequest.d.mts +1 -1
  47. package/dist/esm/api/types/Agent.d.mts +8 -0
  48. package/dist/esm/api/types/CreateAgentRequest.d.mts +11 -3
  49. package/dist/esm/version.d.mts +1 -1
  50. package/dist/esm/version.mjs +1 -1
  51. package/dist/index.d.mts +686 -0
  52. package/dist/index.d.ts +686 -0
  53. package/dist/index.js +643 -0
  54. package/dist/index.mjs +606 -0
  55. package/package.json +1 -1
package/dist/index.js ADDED
@@ -0,0 +1,643 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ Phonic: () => Phonic
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+
37
+ // package.json
38
+ var version = "0.29.1";
39
+
40
+ // src/agents/index.ts
41
+ var Agents = class {
42
+ constructor(phonic) {
43
+ this.phonic = phonic;
44
+ }
45
+ getQueryString(params) {
46
+ const project = params?.project;
47
+ const queryString = new URLSearchParams({
48
+ ...project !== void 0 && { project }
49
+ }).toString();
50
+ return queryString;
51
+ }
52
+ getTemplateVariablesForBody(templateVariables) {
53
+ if (templateVariables === void 0) {
54
+ return void 0;
55
+ }
56
+ return Object.fromEntries(
57
+ Object.entries(templateVariables).map(([key, value]) => [
58
+ key,
59
+ {
60
+ default_value: value.defaultValue
61
+ }
62
+ ])
63
+ );
64
+ }
65
+ getConfigurationEndpointForBody(configurationEndpoint) {
66
+ if (configurationEndpoint === void 0 || configurationEndpoint === null) {
67
+ return configurationEndpoint;
68
+ }
69
+ return {
70
+ url: configurationEndpoint.url,
71
+ headers: configurationEndpoint.headers,
72
+ timeout_ms: configurationEndpoint.timeoutMs
73
+ };
74
+ }
75
+ async list(params) {
76
+ const response = await this.phonic.get(
77
+ `/agents?${this.getQueryString(params)}`
78
+ );
79
+ return response;
80
+ }
81
+ async get(nameOrId, params) {
82
+ const response = await this.phonic.get(
83
+ `/agents/${nameOrId}?${this.getQueryString(params)}`
84
+ );
85
+ return response;
86
+ }
87
+ async create(params) {
88
+ const response = await this.phonic.post(
89
+ `/agents?${this.getQueryString(params)}`,
90
+ {
91
+ name: params.name,
92
+ phone_number: params.phoneNumber,
93
+ timezone: params.timezone,
94
+ audio_format: params.phoneNumber === "assign-automatically" ? "mulaw_8000" : params.audioFormat,
95
+ audio_speed: params.audioSpeed,
96
+ voice_id: params.voiceId,
97
+ welcome_message: params.welcomeMessage,
98
+ system_prompt: params.systemPrompt,
99
+ template_variables: this.getTemplateVariablesForBody(
100
+ params.templateVariables
101
+ ),
102
+ tools: params.tools,
103
+ no_input_poke_sec: params.noInputPokeSec,
104
+ no_input_poke_text: params.noInputPokeText,
105
+ no_input_end_conversation_sec: params.noInputEndConversationSec,
106
+ boosted_keywords: params.boostedKeywords,
107
+ configuration_endpoint: this.getConfigurationEndpointForBody(
108
+ params.configurationEndpoint
109
+ )
110
+ }
111
+ );
112
+ return response;
113
+ }
114
+ async update(nameOrId, params) {
115
+ const response = await this.phonic.patch(
116
+ `/agents/${nameOrId}?${this.getQueryString(params)}`,
117
+ {
118
+ name: params.name,
119
+ phone_number: params.phoneNumber,
120
+ timezone: params.timezone,
121
+ audio_format: params.phoneNumber === "assign-automatically" ? "mulaw_8000" : params.audioFormat,
122
+ voice_id: params.voiceId,
123
+ audio_speed: params.audioSpeed,
124
+ welcome_message: params.welcomeMessage,
125
+ system_prompt: params.systemPrompt,
126
+ template_variables: this.getTemplateVariablesForBody(
127
+ params.templateVariables
128
+ ),
129
+ tools: params.tools,
130
+ no_input_poke_sec: params.noInputPokeSec,
131
+ no_input_poke_text: params.noInputPokeText,
132
+ no_input_end_conversation_sec: params.noInputEndConversationSec,
133
+ boosted_keywords: params.boostedKeywords,
134
+ configuration_endpoint: this.getConfigurationEndpointForBody(
135
+ params.configurationEndpoint
136
+ )
137
+ }
138
+ );
139
+ return response;
140
+ }
141
+ async upsert(params) {
142
+ const response = await this.phonic.put(
143
+ `/agents/upsert?${this.getQueryString(params)}`,
144
+ {
145
+ name: params.name,
146
+ phone_number: params.phoneNumber,
147
+ timezone: params.timezone,
148
+ audio_format: params.phoneNumber === "assign-automatically" ? "mulaw_8000" : params.audioFormat,
149
+ voice_id: params.voiceId,
150
+ welcome_message: params.welcomeMessage,
151
+ system_prompt: params.systemPrompt,
152
+ audio_speed: params.audioSpeed,
153
+ template_variables: this.getTemplateVariablesForBody(
154
+ params.templateVariables
155
+ ),
156
+ tools: params.tools,
157
+ no_input_poke_sec: params.noInputPokeSec,
158
+ no_input_poke_text: params.noInputPokeText,
159
+ no_input_end_conversation_sec: params.noInputEndConversationSec,
160
+ boosted_keywords: params.boostedKeywords,
161
+ configuration_endpoint: this.getConfigurationEndpointForBody(
162
+ params.configurationEndpoint
163
+ )
164
+ }
165
+ );
166
+ return response;
167
+ }
168
+ async delete(nameOrId, params) {
169
+ const response = await this.phonic.delete(
170
+ `/agents/${nameOrId}?${this.getQueryString(params)}`
171
+ );
172
+ return response;
173
+ }
174
+ };
175
+
176
+ // src/conversations/twilio/index.ts
177
+ var Twilio = class {
178
+ constructor(phonic) {
179
+ this.phonic = phonic;
180
+ }
181
+ async outboundCall(params, config) {
182
+ const response = await this.phonic.post(
183
+ "/conversations/twilio/outbound_call",
184
+ {
185
+ from_phone_number: params.from_phone_number,
186
+ to_phone_number: params.to_phone_number,
187
+ config
188
+ },
189
+ {
190
+ "X-Twilio-Account-Sid": params.account_sid,
191
+ "X-Twilio-Api-Key-Sid": params.api_key_sid,
192
+ "X-Twilio-Api-Key-Secret": params.api_key_secret
193
+ }
194
+ );
195
+ return response;
196
+ }
197
+ };
198
+
199
+ // src/conversations/index.ts
200
+ var Conversations = class {
201
+ constructor(phonic) {
202
+ this.phonic = phonic;
203
+ this.twilio = new Twilio(phonic);
204
+ }
205
+ twilio;
206
+ async list({
207
+ project,
208
+ durationMin,
209
+ durationMax,
210
+ startedAtMin,
211
+ startedAtMax
212
+ }) {
213
+ const queryString = new URLSearchParams({
214
+ ...project !== void 0 && { project },
215
+ ...durationMin !== void 0 && { duration_min: String(durationMin) },
216
+ ...durationMax !== void 0 && { duration_max: String(durationMax) },
217
+ ...startedAtMin !== void 0 && { started_at_min: startedAtMin },
218
+ ...startedAtMax !== void 0 && { started_at_max: startedAtMax }
219
+ }).toString();
220
+ const response = await this.phonic.get(
221
+ `/conversations?${queryString}`
222
+ );
223
+ return response;
224
+ }
225
+ async get(id) {
226
+ const response = await this.phonic.get(
227
+ `/conversations/${id}`
228
+ );
229
+ return response;
230
+ }
231
+ async getByExternalId({
232
+ project,
233
+ externalId
234
+ }) {
235
+ const queryString = new URLSearchParams({
236
+ ...project !== void 0 && { project },
237
+ external_id: externalId
238
+ }).toString();
239
+ const response = await this.phonic.get(
240
+ `/conversations?${queryString}`
241
+ );
242
+ return response;
243
+ }
244
+ async outboundCall(toPhoneNumber, config) {
245
+ const response = await this.phonic.post(
246
+ "/conversations/outbound_call",
247
+ {
248
+ to_phone_number: toPhoneNumber,
249
+ config
250
+ }
251
+ );
252
+ return response;
253
+ }
254
+ };
255
+
256
+ // src/projects/index.ts
257
+ var Projects = class {
258
+ constructor(phonic) {
259
+ this.phonic = phonic;
260
+ }
261
+ async list() {
262
+ const response = await this.phonic.get("/projects");
263
+ return response;
264
+ }
265
+ async get(nameOrId) {
266
+ const response = await this.phonic.get(
267
+ `/projects/${nameOrId}`
268
+ );
269
+ return response;
270
+ }
271
+ async create(params) {
272
+ const response = await this.phonic.post(
273
+ "/projects",
274
+ {
275
+ name: params.name
276
+ }
277
+ );
278
+ return response;
279
+ }
280
+ async update(nameOrId, params) {
281
+ const response = await this.phonic.patch(
282
+ `/projects/${nameOrId}`,
283
+ {
284
+ name: params.name,
285
+ default_agent: params.defaultAgent
286
+ }
287
+ );
288
+ return response;
289
+ }
290
+ async delete(nameOrId) {
291
+ const response = await this.phonic.delete(
292
+ `/projects/${nameOrId}`
293
+ );
294
+ return response;
295
+ }
296
+ };
297
+
298
+ // src/sts/index.ts
299
+ var import_ws = __toESM(require("ws"));
300
+
301
+ // src/sts/websocket.ts
302
+ var PhonicSTSWebSocket = class {
303
+ constructor(ws, config) {
304
+ this.ws = ws;
305
+ this.config = config;
306
+ this.buffer.push(
307
+ JSON.stringify({
308
+ type: "config",
309
+ ...this.config
310
+ })
311
+ );
312
+ this.ws.onopen = () => {
313
+ for (const message of this.buffer) {
314
+ this.ws.send(message);
315
+ }
316
+ this.isOpen = true;
317
+ };
318
+ this.ws.onmessage = (event) => {
319
+ if (this.onMessageCallback === null) {
320
+ return;
321
+ }
322
+ if (typeof event.data !== "string") {
323
+ throw new Error("Received non-string message");
324
+ }
325
+ const dataObj = JSON.parse(
326
+ event.data
327
+ );
328
+ this.onMessageCallback(dataObj);
329
+ };
330
+ this.ws.onclose = (event) => {
331
+ if (this.onCloseCallback === null) {
332
+ return;
333
+ }
334
+ this.onCloseCallback(event);
335
+ };
336
+ this.ws.onerror = (event) => {
337
+ if (this.onErrorCallback === null) {
338
+ return;
339
+ }
340
+ this.onErrorCallback(event);
341
+ };
342
+ this.onMessage = this.onMessage.bind(this);
343
+ this.onClose = this.onClose.bind(this);
344
+ this.onError = this.onError.bind(this);
345
+ this.audioChunk = this.audioChunk.bind(this);
346
+ this.sendToolCallOutput = this.sendToolCallOutput.bind(this);
347
+ this.updateSystemPrompt = this.updateSystemPrompt.bind(this);
348
+ this.setExternalId = this.setExternalId.bind(this);
349
+ this.close = this.close.bind(this);
350
+ }
351
+ onMessageCallback = null;
352
+ onCloseCallback = null;
353
+ onErrorCallback = null;
354
+ buffer = [];
355
+ isOpen = false;
356
+ processUserMessage(message) {
357
+ const messageStr = JSON.stringify(message);
358
+ if (this.isOpen) {
359
+ this.ws.send(messageStr);
360
+ } else {
361
+ this.buffer.push(messageStr);
362
+ }
363
+ }
364
+ onMessage(callback) {
365
+ this.onMessageCallback = callback;
366
+ }
367
+ onClose(callback) {
368
+ this.onCloseCallback = callback;
369
+ }
370
+ onError(callback) {
371
+ this.onErrorCallback = callback;
372
+ }
373
+ audioChunk({ audio }) {
374
+ this.processUserMessage({
375
+ type: "audio_chunk",
376
+ audio
377
+ });
378
+ }
379
+ sendToolCallOutput({
380
+ toolCallId,
381
+ output
382
+ }) {
383
+ this.processUserMessage({
384
+ type: "tool_call_output",
385
+ tool_call_id: toolCallId,
386
+ output
387
+ });
388
+ }
389
+ updateSystemPrompt({ systemPrompt }) {
390
+ this.processUserMessage({
391
+ type: "update_system_prompt",
392
+ system_prompt: systemPrompt
393
+ });
394
+ }
395
+ setExternalId({ externalId }) {
396
+ this.processUserMessage({
397
+ type: "set_external_id",
398
+ external_id: externalId
399
+ });
400
+ }
401
+ close(code) {
402
+ this.ws.close(code ?? 1e3);
403
+ }
404
+ };
405
+
406
+ // src/sts/index.ts
407
+ var SpeechToSpeech = class {
408
+ constructor(phonic) {
409
+ this.phonic = phonic;
410
+ }
411
+ websocket(config) {
412
+ const wsBaseUrl = this.phonic.baseUrl.replace(/^http/, "ws");
413
+ const queryString = new URLSearchParams({
414
+ ...this.phonic.__downstreamWebSocketUrl !== null && {
415
+ downstream_websocket_url: this.phonic.__downstreamWebSocketUrl
416
+ }
417
+ }).toString();
418
+ const phonicApiWsUrl = `${wsBaseUrl}/v1/sts/ws?${queryString}`;
419
+ const ws = new import_ws.default(phonicApiWsUrl, {
420
+ headers: this.phonic.headers
421
+ });
422
+ return new PhonicSTSWebSocket(ws, config);
423
+ }
424
+ };
425
+
426
+ // src/tools/index.ts
427
+ var Tools = class {
428
+ constructor(phonic) {
429
+ this.phonic = phonic;
430
+ }
431
+ getQueryString(params) {
432
+ const project = params?.project;
433
+ const queryString = new URLSearchParams({
434
+ ...project !== void 0 && { project }
435
+ }).toString();
436
+ return queryString;
437
+ }
438
+ getParametersForBody(parameters) {
439
+ if (parameters === void 0) {
440
+ return void 0;
441
+ }
442
+ return parameters.map((parameter) => {
443
+ return {
444
+ type: parameter.type,
445
+ name: parameter.name,
446
+ description: parameter.description,
447
+ is_required: parameter.isRequired,
448
+ ...parameter.type === "array" && {
449
+ item_type: parameter.itemType
450
+ }
451
+ };
452
+ });
453
+ }
454
+ async list(params) {
455
+ const response = await this.phonic.get(
456
+ `/tools?${this.getQueryString(params)}`
457
+ );
458
+ return response;
459
+ }
460
+ async get(nameOrId, params) {
461
+ const response = await this.phonic.get(
462
+ `/tools/${nameOrId}?${this.getQueryString(params)}`
463
+ );
464
+ return response;
465
+ }
466
+ async create(params) {
467
+ const body = {
468
+ name: params.name,
469
+ description: params.description,
470
+ type: params.type,
471
+ execution_mode: params.executionMode,
472
+ parameters: this.getParametersForBody(params.parameters)
473
+ };
474
+ if (params.type === "custom_webhook") {
475
+ body.endpoint_method = params.endpointMethod;
476
+ body.endpoint_url = params.endpointUrl;
477
+ body.endpoint_headers = params.endpointHeaders;
478
+ body.endpoint_timeout_ms = params.endpointTimeoutMs;
479
+ }
480
+ if (params.type === "custom_websocket") {
481
+ body.tool_call_output_timeout_ms = params.toolCallOutputTimeoutMs;
482
+ }
483
+ const response = await this.phonic.post(
484
+ `/tools?${this.getQueryString(params)}`,
485
+ body
486
+ );
487
+ return response;
488
+ }
489
+ async update(nameOrId, params) {
490
+ const response = await this.phonic.patch(
491
+ `/tools/${nameOrId}?${this.getQueryString(params)}`,
492
+ {
493
+ name: params.name,
494
+ description: params.description,
495
+ type: params.type,
496
+ execution_mode: params.executionMode,
497
+ endpoint_method: params.endpointMethod,
498
+ endpoint_url: params.endpointUrl,
499
+ endpoint_headers: params.endpointHeaders,
500
+ endpoint_timeout_ms: params.endpointTimeoutMs,
501
+ parameters: this.getParametersForBody(params.parameters),
502
+ tool_call_output_timeout_ms: params.toolCallOutputTimeoutMs
503
+ }
504
+ );
505
+ return response;
506
+ }
507
+ async delete(nameOrId, params) {
508
+ const response = await this.phonic.delete(
509
+ `/tools/${nameOrId}?${this.getQueryString(params)}`
510
+ );
511
+ return response;
512
+ }
513
+ };
514
+
515
+ // src/voices/index.ts
516
+ var Voices = class {
517
+ constructor(phonic) {
518
+ this.phonic = phonic;
519
+ }
520
+ async list({ model }) {
521
+ const response = await this.phonic.get(
522
+ `/voices?model=${encodeURIComponent(model)}`
523
+ );
524
+ return response;
525
+ }
526
+ async get(id) {
527
+ const response = await this.phonic.get(
528
+ `/voices/${id}`
529
+ );
530
+ return response;
531
+ }
532
+ };
533
+
534
+ // src/phonic.ts
535
+ var defaultBaseUrl = "https://api.phonic.co";
536
+ var defaultUserAgent = `phonic-node:${version}`;
537
+ var Phonic = class {
538
+ constructor(apiKey, config) {
539
+ this.apiKey = apiKey;
540
+ if (typeof process === "undefined") {
541
+ throw new Error(
542
+ "Phonic SDK is intended to be used in Node.js environment."
543
+ );
544
+ }
545
+ if (!this.apiKey) {
546
+ throw new Error(
547
+ 'API key is missing. Pass it to the constructor: `new Phonic("ph_...")`'
548
+ );
549
+ }
550
+ this.baseUrl = (config?.baseUrl ?? defaultBaseUrl).replace(/\/$/, "");
551
+ this.__downstreamWebSocketUrl = config?.__downstreamWebSocketUrl || null;
552
+ this.headers = {
553
+ Authorization: `Bearer ${this.apiKey}`,
554
+ "User-Agent": process.env.PHONIC_USER_AGENT || defaultUserAgent,
555
+ "Content-Type": "application/json",
556
+ ...config?.headers
557
+ };
558
+ }
559
+ baseUrl;
560
+ __downstreamWebSocketUrl;
561
+ headers;
562
+ agents = new Agents(this);
563
+ conversations = new Conversations(this);
564
+ projects = new Projects(this);
565
+ tools = new Tools(this);
566
+ voices = new Voices(this);
567
+ sts = new SpeechToSpeech(this);
568
+ async fetchRequest(path, options) {
569
+ try {
570
+ const { headers, ...restOptions } = options;
571
+ const response = await fetch(`${this.baseUrl}/v1${path}`, {
572
+ headers: {
573
+ ...this.headers,
574
+ ...headers
575
+ },
576
+ ...restOptions
577
+ });
578
+ if (response.ok) {
579
+ const data = await response.json();
580
+ return { data, error: null };
581
+ }
582
+ try {
583
+ const data = await response.json();
584
+ return {
585
+ data: null,
586
+ error: {
587
+ message: data.error.message || response.statusText,
588
+ param_errors: data.param_errors
589
+ }
590
+ };
591
+ } catch (error) {
592
+ return {
593
+ data: null,
594
+ error: {
595
+ message: response.statusText
596
+ }
597
+ };
598
+ }
599
+ } catch (error) {
600
+ console.error(error);
601
+ return {
602
+ data: null,
603
+ error: {
604
+ message: "Fetch request failed"
605
+ }
606
+ };
607
+ }
608
+ }
609
+ async get(path) {
610
+ return this.fetchRequest(path, { method: "GET" });
611
+ }
612
+ async post(path, body, headers) {
613
+ return this.fetchRequest(path, {
614
+ method: "POST",
615
+ body: JSON.stringify(body),
616
+ headers
617
+ });
618
+ }
619
+ async patch(path, body, headers) {
620
+ return this.fetchRequest(path, {
621
+ method: "PATCH",
622
+ body: JSON.stringify(body),
623
+ headers
624
+ });
625
+ }
626
+ async put(path, body, headers) {
627
+ return this.fetchRequest(path, {
628
+ method: "PUT",
629
+ body: JSON.stringify(body),
630
+ headers
631
+ });
632
+ }
633
+ async delete(path, headers) {
634
+ return this.fetchRequest(path, {
635
+ method: "DELETE",
636
+ headers
637
+ });
638
+ }
639
+ };
640
+ // Annotate the CommonJS export names for ESM import in node:
641
+ 0 && (module.exports = {
642
+ Phonic
643
+ });