phonic 0.32.11 → 0.32.13

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 (138) hide show
  1. package/dist/cjs/BaseClient.js +2 -2
  2. package/dist/cjs/Client.d.ts +3 -0
  3. package/dist/cjs/Client.js +17 -12
  4. package/dist/cjs/api/resources/agents/client/Client.d.ts +3 -0
  5. package/dist/cjs/api/resources/agents/client/Client.js +3 -0
  6. package/dist/cjs/api/resources/agents/client/requests/AgentsAddCustomPhoneNumberRequest.d.ts +10 -7
  7. package/dist/cjs/api/resources/agents/client/requests/AgentsCreateRequest.d.ts +3 -1
  8. package/dist/cjs/api/resources/agents/client/requests/AgentsUpdatePhoneNumberRequest.d.ts +1 -1
  9. package/dist/cjs/api/resources/agents/client/requests/UpdateAgentRequest.d.ts +4 -2
  10. package/dist/cjs/api/resources/externalStoragePolicies/client/Client.d.ts +117 -0
  11. package/dist/cjs/api/resources/externalStoragePolicies/client/Client.js +427 -0
  12. package/dist/cjs/api/resources/externalStoragePolicies/client/index.d.ts +1 -0
  13. package/dist/cjs/api/resources/externalStoragePolicies/client/index.js +17 -0
  14. package/dist/cjs/api/resources/externalStoragePolicies/client/requests/CreateExternalStoragePolicyRequest.d.ts +43 -0
  15. package/dist/cjs/api/resources/externalStoragePolicies/client/requests/CreateExternalStoragePolicyRequest.js +13 -0
  16. package/dist/cjs/api/resources/externalStoragePolicies/client/requests/ExternalStoragePoliciesDeleteRequest.d.ts +10 -0
  17. package/dist/cjs/api/resources/externalStoragePolicies/client/requests/ExternalStoragePoliciesDeleteRequest.js +3 -0
  18. package/dist/cjs/api/resources/externalStoragePolicies/client/requests/ExternalStoragePoliciesGetRequest.d.ts +10 -0
  19. package/dist/cjs/api/resources/externalStoragePolicies/client/requests/ExternalStoragePoliciesGetRequest.js +3 -0
  20. package/dist/cjs/api/resources/externalStoragePolicies/client/requests/ExternalStoragePoliciesListRequest.d.ts +10 -0
  21. package/dist/cjs/api/resources/externalStoragePolicies/client/requests/ExternalStoragePoliciesListRequest.js +3 -0
  22. package/dist/cjs/api/resources/externalStoragePolicies/client/requests/UpdateExternalStoragePolicyRequest.d.ts +37 -0
  23. package/dist/cjs/api/resources/externalStoragePolicies/client/requests/UpdateExternalStoragePolicyRequest.js +13 -0
  24. package/dist/cjs/api/resources/externalStoragePolicies/client/requests/index.d.ts +5 -0
  25. package/dist/cjs/api/resources/externalStoragePolicies/client/requests/index.js +7 -0
  26. package/dist/cjs/api/resources/externalStoragePolicies/exports.d.ts +2 -0
  27. package/dist/cjs/api/resources/externalStoragePolicies/exports.js +21 -0
  28. package/dist/cjs/api/resources/externalStoragePolicies/index.d.ts +2 -0
  29. package/dist/cjs/api/resources/externalStoragePolicies/index.js +18 -0
  30. package/dist/cjs/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesCreateResponse.d.ts +6 -0
  31. package/dist/cjs/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesCreateResponse.js +3 -0
  32. package/dist/cjs/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesDeleteResponse.d.ts +4 -0
  33. package/dist/cjs/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesDeleteResponse.js +3 -0
  34. package/dist/cjs/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesGetResponse.d.ts +4 -0
  35. package/dist/cjs/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesGetResponse.js +3 -0
  36. package/dist/cjs/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesListResponse.d.ts +4 -0
  37. package/dist/cjs/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesListResponse.js +3 -0
  38. package/dist/cjs/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesUpdateResponse.d.ts +4 -0
  39. package/dist/cjs/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesUpdateResponse.js +3 -0
  40. package/dist/cjs/api/resources/externalStoragePolicies/types/index.d.ts +5 -0
  41. package/dist/cjs/api/resources/externalStoragePolicies/types/index.js +21 -0
  42. package/dist/cjs/api/resources/index.d.ts +3 -0
  43. package/dist/cjs/api/resources/index.js +4 -1
  44. package/dist/cjs/api/resources/tools/client/Client.d.ts +65 -0
  45. package/dist/cjs/api/resources/tools/client/Client.js +65 -0
  46. package/dist/cjs/api/resources/tools/client/requests/CreateToolRequest.d.ts +100 -6
  47. package/dist/cjs/api/resources/tools/client/requests/CreateToolRequest.js +7 -0
  48. package/dist/cjs/api/resources/tools/client/requests/UpdateToolRequest.d.ts +27 -6
  49. package/dist/cjs/api/resources/tools/client/requests/UpdateToolRequest.js +8 -1
  50. package/dist/cjs/api/types/Agent.d.ts +3 -1
  51. package/dist/cjs/api/types/ConfigOptions.d.ts +4 -2
  52. package/dist/cjs/api/types/CreateAgentRequest.d.ts +4 -2
  53. package/dist/cjs/api/types/ExternalStoragePolicy.d.ts +30 -0
  54. package/dist/cjs/api/types/ExternalStoragePolicy.js +13 -0
  55. package/dist/cjs/api/types/InlineWebSocketTool.d.ts +2 -0
  56. package/dist/cjs/api/types/OutboundCallConfig.d.ts +2 -2
  57. package/dist/cjs/api/types/Tool.d.ts +26 -2
  58. package/dist/cjs/api/types/Tool.js +7 -0
  59. package/dist/cjs/api/types/ToolParameter.d.ts +5 -2
  60. package/dist/cjs/api/types/ToolParameter.js +2 -1
  61. package/dist/cjs/api/types/ToolParametersJsonSchema.d.ts +15 -0
  62. package/dist/cjs/api/types/ToolParametersJsonSchema.js +3 -0
  63. package/dist/cjs/api/types/index.d.ts +2 -0
  64. package/dist/cjs/api/types/index.js +2 -0
  65. package/dist/cjs/version.d.ts +1 -1
  66. package/dist/cjs/version.js +1 -1
  67. package/dist/esm/BaseClient.mjs +2 -2
  68. package/dist/esm/Client.d.mts +3 -0
  69. package/dist/esm/Client.mjs +5 -0
  70. package/dist/esm/api/resources/agents/client/Client.d.mts +3 -0
  71. package/dist/esm/api/resources/agents/client/Client.mjs +3 -0
  72. package/dist/esm/api/resources/agents/client/requests/AgentsAddCustomPhoneNumberRequest.d.mts +10 -7
  73. package/dist/esm/api/resources/agents/client/requests/AgentsCreateRequest.d.mts +3 -1
  74. package/dist/esm/api/resources/agents/client/requests/AgentsUpdatePhoneNumberRequest.d.mts +1 -1
  75. package/dist/esm/api/resources/agents/client/requests/UpdateAgentRequest.d.mts +4 -2
  76. package/dist/esm/api/resources/externalStoragePolicies/client/Client.d.mts +117 -0
  77. package/dist/esm/api/resources/externalStoragePolicies/client/Client.mjs +390 -0
  78. package/dist/esm/api/resources/externalStoragePolicies/client/index.d.mts +1 -0
  79. package/dist/esm/api/resources/externalStoragePolicies/client/index.mjs +1 -0
  80. package/dist/esm/api/resources/externalStoragePolicies/client/requests/CreateExternalStoragePolicyRequest.d.mts +43 -0
  81. package/dist/esm/api/resources/externalStoragePolicies/client/requests/CreateExternalStoragePolicyRequest.mjs +10 -0
  82. package/dist/esm/api/resources/externalStoragePolicies/client/requests/ExternalStoragePoliciesDeleteRequest.d.mts +10 -0
  83. package/dist/esm/api/resources/externalStoragePolicies/client/requests/ExternalStoragePoliciesDeleteRequest.mjs +2 -0
  84. package/dist/esm/api/resources/externalStoragePolicies/client/requests/ExternalStoragePoliciesGetRequest.d.mts +10 -0
  85. package/dist/esm/api/resources/externalStoragePolicies/client/requests/ExternalStoragePoliciesGetRequest.mjs +2 -0
  86. package/dist/esm/api/resources/externalStoragePolicies/client/requests/ExternalStoragePoliciesListRequest.d.mts +10 -0
  87. package/dist/esm/api/resources/externalStoragePolicies/client/requests/ExternalStoragePoliciesListRequest.mjs +2 -0
  88. package/dist/esm/api/resources/externalStoragePolicies/client/requests/UpdateExternalStoragePolicyRequest.d.mts +37 -0
  89. package/dist/esm/api/resources/externalStoragePolicies/client/requests/UpdateExternalStoragePolicyRequest.mjs +10 -0
  90. package/dist/esm/api/resources/externalStoragePolicies/client/requests/index.d.mts +5 -0
  91. package/dist/esm/api/resources/externalStoragePolicies/client/requests/index.mjs +2 -0
  92. package/dist/esm/api/resources/externalStoragePolicies/exports.d.mts +2 -0
  93. package/dist/esm/api/resources/externalStoragePolicies/exports.mjs +3 -0
  94. package/dist/esm/api/resources/externalStoragePolicies/index.d.mts +2 -0
  95. package/dist/esm/api/resources/externalStoragePolicies/index.mjs +2 -0
  96. package/dist/esm/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesCreateResponse.d.mts +6 -0
  97. package/dist/esm/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesCreateResponse.mjs +2 -0
  98. package/dist/esm/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesDeleteResponse.d.mts +4 -0
  99. package/dist/esm/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesDeleteResponse.mjs +2 -0
  100. package/dist/esm/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesGetResponse.d.mts +4 -0
  101. package/dist/esm/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesGetResponse.mjs +2 -0
  102. package/dist/esm/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesListResponse.d.mts +4 -0
  103. package/dist/esm/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesListResponse.mjs +2 -0
  104. package/dist/esm/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesUpdateResponse.d.mts +4 -0
  105. package/dist/esm/api/resources/externalStoragePolicies/types/ExternalStoragePoliciesUpdateResponse.mjs +2 -0
  106. package/dist/esm/api/resources/externalStoragePolicies/types/index.d.mts +5 -0
  107. package/dist/esm/api/resources/externalStoragePolicies/types/index.mjs +5 -0
  108. package/dist/esm/api/resources/index.d.mts +3 -0
  109. package/dist/esm/api/resources/index.mjs +3 -0
  110. package/dist/esm/api/resources/tools/client/Client.d.mts +65 -0
  111. package/dist/esm/api/resources/tools/client/Client.mjs +65 -0
  112. package/dist/esm/api/resources/tools/client/requests/CreateToolRequest.d.mts +100 -6
  113. package/dist/esm/api/resources/tools/client/requests/CreateToolRequest.mjs +7 -0
  114. package/dist/esm/api/resources/tools/client/requests/UpdateToolRequest.d.mts +27 -6
  115. package/dist/esm/api/resources/tools/client/requests/UpdateToolRequest.mjs +8 -1
  116. package/dist/esm/api/types/Agent.d.mts +3 -1
  117. package/dist/esm/api/types/ConfigOptions.d.mts +4 -2
  118. package/dist/esm/api/types/CreateAgentRequest.d.mts +4 -2
  119. package/dist/esm/api/types/ExternalStoragePolicy.d.mts +30 -0
  120. package/dist/esm/api/types/ExternalStoragePolicy.mjs +10 -0
  121. package/dist/esm/api/types/InlineWebSocketTool.d.mts +2 -0
  122. package/dist/esm/api/types/OutboundCallConfig.d.mts +2 -2
  123. package/dist/esm/api/types/Tool.d.mts +26 -2
  124. package/dist/esm/api/types/Tool.mjs +7 -0
  125. package/dist/esm/api/types/ToolParameter.d.mts +5 -2
  126. package/dist/esm/api/types/ToolParameter.mjs +2 -1
  127. package/dist/esm/api/types/ToolParametersJsonSchema.d.mts +15 -0
  128. package/dist/esm/api/types/ToolParametersJsonSchema.mjs +2 -0
  129. package/dist/esm/api/types/index.d.mts +2 -0
  130. package/dist/esm/api/types/index.mjs +2 -0
  131. package/dist/esm/version.d.mts +1 -1
  132. package/dist/esm/version.mjs +1 -1
  133. package/dist/index.d.mts +240 -0
  134. package/dist/index.d.ts +240 -0
  135. package/dist/index.js +347 -0
  136. package/dist/index.mjs +310 -0
  137. package/package.json +12 -1
  138. package/reference.md +363 -0
package/dist/index.js ADDED
@@ -0,0 +1,347 @@
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.18.3";
39
+
40
+ // src/conversations/index.ts
41
+ var Conversations = class {
42
+ constructor(phonic) {
43
+ this.phonic = phonic;
44
+ }
45
+ async get(id) {
46
+ const response = await this.phonic.get(
47
+ `/conversations/${id}`
48
+ );
49
+ return response;
50
+ }
51
+ async getByExternalId({
52
+ project,
53
+ externalId
54
+ }) {
55
+ const queryString = new URLSearchParams({
56
+ ...project !== void 0 && { project },
57
+ external_id: externalId
58
+ }).toString();
59
+ const response = await this.phonic.get(
60
+ `/conversations?${queryString}`
61
+ );
62
+ return response;
63
+ }
64
+ async list({
65
+ project,
66
+ durationMin,
67
+ durationMax,
68
+ startedAtMin,
69
+ startedAtMax
70
+ }) {
71
+ const queryString = new URLSearchParams({
72
+ ...project !== void 0 && { project },
73
+ ...durationMin !== void 0 && { duration_min: String(durationMin) },
74
+ ...durationMax !== void 0 && { duration_max: String(durationMax) },
75
+ ...startedAtMin !== void 0 && { started_at_min: startedAtMin },
76
+ ...startedAtMax !== void 0 && { started_at_max: startedAtMax }
77
+ }).toString();
78
+ const response = await this.phonic.get(
79
+ `/conversations?${queryString}`
80
+ );
81
+ return response;
82
+ }
83
+ };
84
+
85
+ // src/sts/index.ts
86
+ var import_ws = __toESM(require("ws"));
87
+
88
+ // src/sts/twilio/index.ts
89
+ var Twilio = class {
90
+ constructor(phonic) {
91
+ this.phonic = phonic;
92
+ }
93
+ async outboundCall(params, config) {
94
+ const response = await this.phonic.post(
95
+ "/sts/twilio/outbound_call",
96
+ {
97
+ from_phone_number: params.from_phone_number,
98
+ to_phone_number: params.to_phone_number,
99
+ config
100
+ },
101
+ {
102
+ "X-Twilio-Account-Sid": params.account_sid,
103
+ "X-Twilio-Api-Key-Sid": params.api_key_sid,
104
+ "X-Twilio-Api-Key-Secret": params.api_key_secret
105
+ }
106
+ );
107
+ return response;
108
+ }
109
+ };
110
+
111
+ // src/sts/websocket.ts
112
+ var PhonicSTSWebSocket = class {
113
+ constructor(ws, config) {
114
+ this.ws = ws;
115
+ this.config = config;
116
+ this.buffer.push(
117
+ JSON.stringify({
118
+ type: "config",
119
+ ...this.config
120
+ })
121
+ );
122
+ this.ws.onopen = () => {
123
+ for (const message of this.buffer) {
124
+ this.ws.send(message);
125
+ }
126
+ this.isOpen = true;
127
+ };
128
+ this.ws.onmessage = (event) => {
129
+ if (this.onMessageCallback === null) {
130
+ return;
131
+ }
132
+ if (typeof event.data !== "string") {
133
+ throw new Error("Received non-string message");
134
+ }
135
+ const dataObj = JSON.parse(
136
+ event.data
137
+ );
138
+ this.onMessageCallback(dataObj);
139
+ };
140
+ this.ws.onclose = (event) => {
141
+ if (this.onCloseCallback === null) {
142
+ return;
143
+ }
144
+ this.onCloseCallback(event);
145
+ };
146
+ this.ws.onerror = (event) => {
147
+ if (this.onErrorCallback === null) {
148
+ return;
149
+ }
150
+ this.onErrorCallback(event);
151
+ };
152
+ this.onMessage = this.onMessage.bind(this);
153
+ this.onClose = this.onClose.bind(this);
154
+ this.onError = this.onError.bind(this);
155
+ this.audioChunk = this.audioChunk.bind(this);
156
+ this.close = this.close.bind(this);
157
+ }
158
+ onMessageCallback = null;
159
+ onCloseCallback = null;
160
+ onErrorCallback = null;
161
+ buffer = [];
162
+ isOpen = false;
163
+ onMessage(callback) {
164
+ this.onMessageCallback = callback;
165
+ }
166
+ onClose(callback) {
167
+ this.onCloseCallback = callback;
168
+ }
169
+ onError(callback) {
170
+ this.onErrorCallback = callback;
171
+ }
172
+ audioChunk({ audio }) {
173
+ const audiochunkMessage = JSON.stringify({
174
+ type: "audio_chunk",
175
+ audio
176
+ });
177
+ if (this.isOpen) {
178
+ this.ws.send(audiochunkMessage);
179
+ } else {
180
+ this.buffer.push(audiochunkMessage);
181
+ }
182
+ }
183
+ updateSystemPrompt({ systemPrompt }) {
184
+ const updateSystemPromptMessage = JSON.stringify({
185
+ type: "update_system_prompt",
186
+ system_prompt: systemPrompt
187
+ });
188
+ if (this.isOpen) {
189
+ this.ws.send(updateSystemPromptMessage);
190
+ } else {
191
+ this.buffer.push(updateSystemPromptMessage);
192
+ }
193
+ }
194
+ setExternalId({ externalId }) {
195
+ const setExternalIdMessage = JSON.stringify({
196
+ type: "set_external_id",
197
+ external_id: externalId
198
+ });
199
+ if (this.isOpen) {
200
+ this.ws.send(setExternalIdMessage);
201
+ } else {
202
+ this.buffer.push(setExternalIdMessage);
203
+ }
204
+ }
205
+ close(code) {
206
+ this.ws.close(code ?? 1e3);
207
+ }
208
+ };
209
+
210
+ // src/sts/index.ts
211
+ var SpeechToSpeech = class {
212
+ constructor(phonic) {
213
+ this.phonic = phonic;
214
+ this.twilio = new Twilio(phonic);
215
+ }
216
+ twilio;
217
+ websocket(config) {
218
+ const wsBaseUrl = this.phonic.baseUrl.replace(/^http/, "ws");
219
+ const queryString = new URLSearchParams({
220
+ ...this.phonic.__downstreamWebSocketUrl !== null && {
221
+ downstream_websocket_url: this.phonic.__downstreamWebSocketUrl
222
+ }
223
+ }).toString();
224
+ const phonicApiWsUrl = `${wsBaseUrl}/v1/sts/ws?${queryString}`;
225
+ const ws = new import_ws.default(phonicApiWsUrl, {
226
+ headers: this.phonic.headers
227
+ });
228
+ return new PhonicSTSWebSocket(ws, config);
229
+ }
230
+ async outboundCall(toPhoneNumber, config) {
231
+ const response = await this.phonic.post(
232
+ "/sts/outbound_call",
233
+ {
234
+ to_phone_number: toPhoneNumber,
235
+ config
236
+ }
237
+ );
238
+ return response;
239
+ }
240
+ };
241
+
242
+ // src/voices/index.ts
243
+ var Voices = class {
244
+ constructor(phonic) {
245
+ this.phonic = phonic;
246
+ }
247
+ async list({ model }) {
248
+ const response = await this.phonic.get(
249
+ `/voices?model=${encodeURIComponent(model)}`
250
+ );
251
+ return response;
252
+ }
253
+ async get(id) {
254
+ const response = await this.phonic.get(
255
+ `/voices/${id}`
256
+ );
257
+ return response;
258
+ }
259
+ };
260
+
261
+ // src/phonic.ts
262
+ var defaultBaseUrl = "https://api.phonic.co";
263
+ var defaultUserAgent = `phonic-node:${version}`;
264
+ var Phonic = class {
265
+ constructor(apiKey, config) {
266
+ this.apiKey = apiKey;
267
+ if (typeof process === "undefined") {
268
+ throw new Error(
269
+ "Phonic SDK is intended to be used in Node.js environment."
270
+ );
271
+ }
272
+ if (!this.apiKey) {
273
+ throw new Error(
274
+ 'API key is missing. Pass it to the constructor: `new Phonic("ph_...")`'
275
+ );
276
+ }
277
+ this.baseUrl = (config?.baseUrl ?? defaultBaseUrl).replace(/\/$/, "");
278
+ this.__downstreamWebSocketUrl = config?.__downstreamWebSocketUrl || null;
279
+ this.headers = {
280
+ Authorization: `Bearer ${this.apiKey}`,
281
+ "User-Agent": process.env.PHONIC_USER_AGENT || defaultUserAgent,
282
+ "Content-Type": "application/json",
283
+ ...config?.headers
284
+ };
285
+ }
286
+ baseUrl;
287
+ __downstreamWebSocketUrl;
288
+ headers;
289
+ conversations = new Conversations(this);
290
+ voices = new Voices(this);
291
+ sts = new SpeechToSpeech(this);
292
+ async fetchRequest(path, options) {
293
+ try {
294
+ const { headers, ...restOptions } = options;
295
+ const response = await fetch(`${this.baseUrl}/v1${path}`, {
296
+ headers: {
297
+ ...this.headers,
298
+ ...headers
299
+ },
300
+ ...restOptions
301
+ });
302
+ if (response.ok) {
303
+ const data = await response.json();
304
+ return { data, error: null };
305
+ }
306
+ try {
307
+ const data = await response.json();
308
+ const errorMessage = data.error.message || response.statusText;
309
+ return {
310
+ data: null,
311
+ error: {
312
+ message: errorMessage
313
+ }
314
+ };
315
+ } catch (error) {
316
+ return {
317
+ data: null,
318
+ error: {
319
+ message: response.statusText
320
+ }
321
+ };
322
+ }
323
+ } catch (error) {
324
+ console.error(error);
325
+ return {
326
+ data: null,
327
+ error: {
328
+ message: "Fetch request failed"
329
+ }
330
+ };
331
+ }
332
+ }
333
+ async get(path) {
334
+ return this.fetchRequest(path, { method: "GET" });
335
+ }
336
+ async post(path, body, headers) {
337
+ return this.fetchRequest(path, {
338
+ method: "POST",
339
+ body: JSON.stringify(body),
340
+ headers
341
+ });
342
+ }
343
+ };
344
+ // Annotate the CommonJS export names for ESM import in node:
345
+ 0 && (module.exports = {
346
+ Phonic
347
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,310 @@
1
+ // package.json
2
+ var version = "0.18.3";
3
+
4
+ // src/conversations/index.ts
5
+ var Conversations = class {
6
+ constructor(phonic) {
7
+ this.phonic = phonic;
8
+ }
9
+ async get(id) {
10
+ const response = await this.phonic.get(
11
+ `/conversations/${id}`
12
+ );
13
+ return response;
14
+ }
15
+ async getByExternalId({
16
+ project,
17
+ externalId
18
+ }) {
19
+ const queryString = new URLSearchParams({
20
+ ...project !== void 0 && { project },
21
+ external_id: externalId
22
+ }).toString();
23
+ const response = await this.phonic.get(
24
+ `/conversations?${queryString}`
25
+ );
26
+ return response;
27
+ }
28
+ async list({
29
+ project,
30
+ durationMin,
31
+ durationMax,
32
+ startedAtMin,
33
+ startedAtMax
34
+ }) {
35
+ const queryString = new URLSearchParams({
36
+ ...project !== void 0 && { project },
37
+ ...durationMin !== void 0 && { duration_min: String(durationMin) },
38
+ ...durationMax !== void 0 && { duration_max: String(durationMax) },
39
+ ...startedAtMin !== void 0 && { started_at_min: startedAtMin },
40
+ ...startedAtMax !== void 0 && { started_at_max: startedAtMax }
41
+ }).toString();
42
+ const response = await this.phonic.get(
43
+ `/conversations?${queryString}`
44
+ );
45
+ return response;
46
+ }
47
+ };
48
+
49
+ // src/sts/index.ts
50
+ import WebSocket from "ws";
51
+
52
+ // src/sts/twilio/index.ts
53
+ var Twilio = class {
54
+ constructor(phonic) {
55
+ this.phonic = phonic;
56
+ }
57
+ async outboundCall(params, config) {
58
+ const response = await this.phonic.post(
59
+ "/sts/twilio/outbound_call",
60
+ {
61
+ from_phone_number: params.from_phone_number,
62
+ to_phone_number: params.to_phone_number,
63
+ config
64
+ },
65
+ {
66
+ "X-Twilio-Account-Sid": params.account_sid,
67
+ "X-Twilio-Api-Key-Sid": params.api_key_sid,
68
+ "X-Twilio-Api-Key-Secret": params.api_key_secret
69
+ }
70
+ );
71
+ return response;
72
+ }
73
+ };
74
+
75
+ // src/sts/websocket.ts
76
+ var PhonicSTSWebSocket = class {
77
+ constructor(ws, config) {
78
+ this.ws = ws;
79
+ this.config = config;
80
+ this.buffer.push(
81
+ JSON.stringify({
82
+ type: "config",
83
+ ...this.config
84
+ })
85
+ );
86
+ this.ws.onopen = () => {
87
+ for (const message of this.buffer) {
88
+ this.ws.send(message);
89
+ }
90
+ this.isOpen = true;
91
+ };
92
+ this.ws.onmessage = (event) => {
93
+ if (this.onMessageCallback === null) {
94
+ return;
95
+ }
96
+ if (typeof event.data !== "string") {
97
+ throw new Error("Received non-string message");
98
+ }
99
+ const dataObj = JSON.parse(
100
+ event.data
101
+ );
102
+ this.onMessageCallback(dataObj);
103
+ };
104
+ this.ws.onclose = (event) => {
105
+ if (this.onCloseCallback === null) {
106
+ return;
107
+ }
108
+ this.onCloseCallback(event);
109
+ };
110
+ this.ws.onerror = (event) => {
111
+ if (this.onErrorCallback === null) {
112
+ return;
113
+ }
114
+ this.onErrorCallback(event);
115
+ };
116
+ this.onMessage = this.onMessage.bind(this);
117
+ this.onClose = this.onClose.bind(this);
118
+ this.onError = this.onError.bind(this);
119
+ this.audioChunk = this.audioChunk.bind(this);
120
+ this.close = this.close.bind(this);
121
+ }
122
+ onMessageCallback = null;
123
+ onCloseCallback = null;
124
+ onErrorCallback = null;
125
+ buffer = [];
126
+ isOpen = false;
127
+ onMessage(callback) {
128
+ this.onMessageCallback = callback;
129
+ }
130
+ onClose(callback) {
131
+ this.onCloseCallback = callback;
132
+ }
133
+ onError(callback) {
134
+ this.onErrorCallback = callback;
135
+ }
136
+ audioChunk({ audio }) {
137
+ const audiochunkMessage = JSON.stringify({
138
+ type: "audio_chunk",
139
+ audio
140
+ });
141
+ if (this.isOpen) {
142
+ this.ws.send(audiochunkMessage);
143
+ } else {
144
+ this.buffer.push(audiochunkMessage);
145
+ }
146
+ }
147
+ updateSystemPrompt({ systemPrompt }) {
148
+ const updateSystemPromptMessage = JSON.stringify({
149
+ type: "update_system_prompt",
150
+ system_prompt: systemPrompt
151
+ });
152
+ if (this.isOpen) {
153
+ this.ws.send(updateSystemPromptMessage);
154
+ } else {
155
+ this.buffer.push(updateSystemPromptMessage);
156
+ }
157
+ }
158
+ setExternalId({ externalId }) {
159
+ const setExternalIdMessage = JSON.stringify({
160
+ type: "set_external_id",
161
+ external_id: externalId
162
+ });
163
+ if (this.isOpen) {
164
+ this.ws.send(setExternalIdMessage);
165
+ } else {
166
+ this.buffer.push(setExternalIdMessage);
167
+ }
168
+ }
169
+ close(code) {
170
+ this.ws.close(code ?? 1e3);
171
+ }
172
+ };
173
+
174
+ // src/sts/index.ts
175
+ var SpeechToSpeech = class {
176
+ constructor(phonic) {
177
+ this.phonic = phonic;
178
+ this.twilio = new Twilio(phonic);
179
+ }
180
+ twilio;
181
+ websocket(config) {
182
+ const wsBaseUrl = this.phonic.baseUrl.replace(/^http/, "ws");
183
+ const queryString = new URLSearchParams({
184
+ ...this.phonic.__downstreamWebSocketUrl !== null && {
185
+ downstream_websocket_url: this.phonic.__downstreamWebSocketUrl
186
+ }
187
+ }).toString();
188
+ const phonicApiWsUrl = `${wsBaseUrl}/v1/sts/ws?${queryString}`;
189
+ const ws = new WebSocket(phonicApiWsUrl, {
190
+ headers: this.phonic.headers
191
+ });
192
+ return new PhonicSTSWebSocket(ws, config);
193
+ }
194
+ async outboundCall(toPhoneNumber, config) {
195
+ const response = await this.phonic.post(
196
+ "/sts/outbound_call",
197
+ {
198
+ to_phone_number: toPhoneNumber,
199
+ config
200
+ }
201
+ );
202
+ return response;
203
+ }
204
+ };
205
+
206
+ // src/voices/index.ts
207
+ var Voices = class {
208
+ constructor(phonic) {
209
+ this.phonic = phonic;
210
+ }
211
+ async list({ model }) {
212
+ const response = await this.phonic.get(
213
+ `/voices?model=${encodeURIComponent(model)}`
214
+ );
215
+ return response;
216
+ }
217
+ async get(id) {
218
+ const response = await this.phonic.get(
219
+ `/voices/${id}`
220
+ );
221
+ return response;
222
+ }
223
+ };
224
+
225
+ // src/phonic.ts
226
+ var defaultBaseUrl = "https://api.phonic.co";
227
+ var defaultUserAgent = `phonic-node:${version}`;
228
+ var Phonic = class {
229
+ constructor(apiKey, config) {
230
+ this.apiKey = apiKey;
231
+ if (typeof process === "undefined") {
232
+ throw new Error(
233
+ "Phonic SDK is intended to be used in Node.js environment."
234
+ );
235
+ }
236
+ if (!this.apiKey) {
237
+ throw new Error(
238
+ 'API key is missing. Pass it to the constructor: `new Phonic("ph_...")`'
239
+ );
240
+ }
241
+ this.baseUrl = (config?.baseUrl ?? defaultBaseUrl).replace(/\/$/, "");
242
+ this.__downstreamWebSocketUrl = config?.__downstreamWebSocketUrl || null;
243
+ this.headers = {
244
+ Authorization: `Bearer ${this.apiKey}`,
245
+ "User-Agent": process.env.PHONIC_USER_AGENT || defaultUserAgent,
246
+ "Content-Type": "application/json",
247
+ ...config?.headers
248
+ };
249
+ }
250
+ baseUrl;
251
+ __downstreamWebSocketUrl;
252
+ headers;
253
+ conversations = new Conversations(this);
254
+ voices = new Voices(this);
255
+ sts = new SpeechToSpeech(this);
256
+ async fetchRequest(path, options) {
257
+ try {
258
+ const { headers, ...restOptions } = options;
259
+ const response = await fetch(`${this.baseUrl}/v1${path}`, {
260
+ headers: {
261
+ ...this.headers,
262
+ ...headers
263
+ },
264
+ ...restOptions
265
+ });
266
+ if (response.ok) {
267
+ const data = await response.json();
268
+ return { data, error: null };
269
+ }
270
+ try {
271
+ const data = await response.json();
272
+ const errorMessage = data.error.message || response.statusText;
273
+ return {
274
+ data: null,
275
+ error: {
276
+ message: errorMessage
277
+ }
278
+ };
279
+ } catch (error) {
280
+ return {
281
+ data: null,
282
+ error: {
283
+ message: response.statusText
284
+ }
285
+ };
286
+ }
287
+ } catch (error) {
288
+ console.error(error);
289
+ return {
290
+ data: null,
291
+ error: {
292
+ message: "Fetch request failed"
293
+ }
294
+ };
295
+ }
296
+ }
297
+ async get(path) {
298
+ return this.fetchRequest(path, { method: "GET" });
299
+ }
300
+ async post(path, body, headers) {
301
+ return this.fetchRequest(path, {
302
+ method: "POST",
303
+ body: JSON.stringify(body),
304
+ headers
305
+ });
306
+ }
307
+ };
308
+ export {
309
+ Phonic
310
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phonic",
3
- "version": "0.32.11",
3
+ "version": "0.32.13",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",
@@ -45,6 +45,17 @@
45
45
  },
46
46
  "default": "./dist/cjs/api/resources/tools/exports.js"
47
47
  },
48
+ "./externalStoragePolicies": {
49
+ "import": {
50
+ "types": "./dist/esm/api/resources/externalStoragePolicies/exports.d.mts",
51
+ "default": "./dist/esm/api/resources/externalStoragePolicies/exports.mjs"
52
+ },
53
+ "require": {
54
+ "types": "./dist/cjs/api/resources/externalStoragePolicies/exports.d.ts",
55
+ "default": "./dist/cjs/api/resources/externalStoragePolicies/exports.js"
56
+ },
57
+ "default": "./dist/cjs/api/resources/externalStoragePolicies/exports.js"
58
+ },
48
59
  "./extractionSchemas": {
49
60
  "import": {
50
61
  "types": "./dist/esm/api/resources/extractionSchemas/exports.d.mts",