phonic 0.32.13 → 0.32.15
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.
- package/dist/cjs/BaseClient.js +2 -2
- package/dist/cjs/api/resources/agents/client/requests/UpdateAgentRequest.d.ts +1 -1
- package/dist/cjs/api/resources/tools/client/requests/CreateToolRequest.d.ts +2 -0
- package/dist/cjs/api/resources/tools/client/requests/UpdateToolRequest.d.ts +2 -0
- package/dist/cjs/api/resources/tts/client/requests/StreamTtsRequest.d.ts +6 -0
- package/dist/cjs/api/types/Agent.d.ts +1 -1
- package/dist/cjs/api/types/BuiltInToolConfig.d.ts +1 -1
- package/dist/cjs/api/types/BuiltInToolConfigs.d.ts +5 -2
- package/dist/cjs/api/types/BuiltInToolDefinition.d.ts +8 -2
- package/dist/cjs/api/types/BuiltInToolDefinition.js +1 -0
- package/dist/cjs/api/types/ChooseNotToRespondToolConfig.d.ts +7 -0
- package/dist/cjs/api/types/ChooseNotToRespondToolConfig.js +3 -0
- package/dist/cjs/api/types/CreateAgentRequest.d.ts +1 -1
- package/dist/cjs/api/types/OutboundCallConfig.d.ts +1 -1
- package/dist/cjs/api/types/Tool.d.ts +2 -0
- package/dist/cjs/api/types/index.d.ts +1 -0
- package/dist/cjs/api/types/index.js +1 -0
- package/dist/cjs/custom/ReconnectableConversationsSocket.d.ts +16 -0
- package/dist/cjs/custom/ReconnectableConversationsSocket.js +127 -11
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/BaseClient.mjs +2 -2
- package/dist/esm/api/resources/agents/client/requests/UpdateAgentRequest.d.mts +1 -1
- package/dist/esm/api/resources/tools/client/requests/CreateToolRequest.d.mts +2 -0
- package/dist/esm/api/resources/tools/client/requests/UpdateToolRequest.d.mts +2 -0
- package/dist/esm/api/resources/tts/client/requests/StreamTtsRequest.d.mts +6 -0
- package/dist/esm/api/types/Agent.d.mts +1 -1
- package/dist/esm/api/types/BuiltInToolConfig.d.mts +1 -1
- package/dist/esm/api/types/BuiltInToolConfigs.d.mts +5 -2
- package/dist/esm/api/types/BuiltInToolDefinition.d.mts +8 -2
- package/dist/esm/api/types/BuiltInToolDefinition.mjs +1 -0
- package/dist/esm/api/types/ChooseNotToRespondToolConfig.d.mts +7 -0
- package/dist/esm/api/types/ChooseNotToRespondToolConfig.mjs +2 -0
- package/dist/esm/api/types/CreateAgentRequest.d.mts +1 -1
- package/dist/esm/api/types/OutboundCallConfig.d.mts +1 -1
- package/dist/esm/api/types/Tool.d.mts +2 -0
- package/dist/esm/api/types/index.d.mts +1 -0
- package/dist/esm/api/types/index.mjs +1 -0
- package/dist/esm/custom/ReconnectableConversationsSocket.d.mts +16 -0
- package/dist/esm/custom/ReconnectableConversationsSocket.mjs +127 -11
- package/dist/esm/version.d.mts +1 -1
- package/dist/esm/version.mjs +1 -1
- package/package.json +1 -1
- package/dist/index.d.mts +0 -240
- package/dist/index.d.ts +0 -240
- package/dist/index.js +0 -347
- package/dist/index.mjs +0 -310
package/dist/index.mjs
DELETED
|
@@ -1,310 +0,0 @@
|
|
|
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
|
-
};
|