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.d.ts
DELETED
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
import WebSocket from 'ws';
|
|
2
|
-
|
|
3
|
-
type PhonicConfig = {
|
|
4
|
-
baseUrl?: string;
|
|
5
|
-
headers?: Record<string, string>;
|
|
6
|
-
__downstreamWebSocketUrl?: string;
|
|
7
|
-
};
|
|
8
|
-
type FetchOptions = {
|
|
9
|
-
method: "GET";
|
|
10
|
-
headers?: Record<string, string>;
|
|
11
|
-
} | {
|
|
12
|
-
method: "POST";
|
|
13
|
-
headers?: Record<string, string>;
|
|
14
|
-
body: string;
|
|
15
|
-
};
|
|
16
|
-
type DataOrError<T> = Promise<{
|
|
17
|
-
data: T;
|
|
18
|
-
error: null;
|
|
19
|
-
} | {
|
|
20
|
-
data: null;
|
|
21
|
-
error: {
|
|
22
|
-
message: string;
|
|
23
|
-
code?: string;
|
|
24
|
-
};
|
|
25
|
-
}>;
|
|
26
|
-
type ISODate = `${string}-${string}-${string}`;
|
|
27
|
-
type ISODateTime$1 = `${string}Z`;
|
|
28
|
-
|
|
29
|
-
type ISODateTime = `${string}Z`;
|
|
30
|
-
type ConversationItem = {
|
|
31
|
-
role: "user";
|
|
32
|
-
item_idx: number;
|
|
33
|
-
text: string;
|
|
34
|
-
duration_ms: number;
|
|
35
|
-
started_at: string;
|
|
36
|
-
} | {
|
|
37
|
-
role: "assistant";
|
|
38
|
-
item_idx: number;
|
|
39
|
-
text: string;
|
|
40
|
-
voice_id: string;
|
|
41
|
-
system_prompt: string;
|
|
42
|
-
output_audio_speed: number;
|
|
43
|
-
duration_ms: number;
|
|
44
|
-
started_at: string;
|
|
45
|
-
};
|
|
46
|
-
type Conversation = {
|
|
47
|
-
id: string;
|
|
48
|
-
external_id: string | null;
|
|
49
|
-
model: string;
|
|
50
|
-
welcome_message: string | null;
|
|
51
|
-
input_format: "pcm_44100" | "mulaw_8000";
|
|
52
|
-
output_format: "pcm_44100" | "mulaw_8000";
|
|
53
|
-
text: string;
|
|
54
|
-
duration_ms: number;
|
|
55
|
-
started_at: ISODateTime;
|
|
56
|
-
ended_at: ISODateTime;
|
|
57
|
-
items: Array<ConversationItem>;
|
|
58
|
-
};
|
|
59
|
-
type ConversationSuccessResponse = {
|
|
60
|
-
conversation: Conversation;
|
|
61
|
-
};
|
|
62
|
-
type ConversationsSuccessResponse = {
|
|
63
|
-
conversations: Array<Conversation>;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
declare class Conversations {
|
|
67
|
-
private readonly phonic;
|
|
68
|
-
constructor(phonic: Phonic);
|
|
69
|
-
get(id: string): DataOrError<ConversationSuccessResponse>;
|
|
70
|
-
getByExternalId({ project, externalId, }: {
|
|
71
|
-
project?: string;
|
|
72
|
-
externalId: string;
|
|
73
|
-
}): DataOrError<ConversationSuccessResponse>;
|
|
74
|
-
list({ project, durationMin, durationMax, startedAtMin, startedAtMax, }: {
|
|
75
|
-
project?: string;
|
|
76
|
-
durationMin?: number;
|
|
77
|
-
durationMax?: number;
|
|
78
|
-
startedAtMin?: ISODate | ISODateTime$1;
|
|
79
|
-
startedAtMax?: ISODate | ISODateTime$1;
|
|
80
|
-
}): DataOrError<ConversationsSuccessResponse>;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
type PhonicSTSConfig = {
|
|
84
|
-
project: string;
|
|
85
|
-
input_format: "pcm_44100" | "mulaw_8000";
|
|
86
|
-
system_prompt?: string;
|
|
87
|
-
welcome_message?: string;
|
|
88
|
-
voice_id?: string;
|
|
89
|
-
output_format?: "pcm_44100" | "mulaw_8000";
|
|
90
|
-
enable_silent_audio_fallback?: boolean;
|
|
91
|
-
experimental_params?: Record<string, unknown>;
|
|
92
|
-
tools?: string[];
|
|
93
|
-
vad_prebuffer_duration_ms?: number;
|
|
94
|
-
vad_min_speech_duration_ms?: number;
|
|
95
|
-
vad_min_silence_duration_ms?: number;
|
|
96
|
-
vad_threshold?: number;
|
|
97
|
-
};
|
|
98
|
-
type PhonicSTSWebSocketResponseMessage = {
|
|
99
|
-
type: "ready_to_start_conversation";
|
|
100
|
-
} | {
|
|
101
|
-
type: "input_text";
|
|
102
|
-
text: string;
|
|
103
|
-
} | {
|
|
104
|
-
type: "audio_chunk";
|
|
105
|
-
text: string;
|
|
106
|
-
audio: string;
|
|
107
|
-
} | {
|
|
108
|
-
type: "is_user_speaking";
|
|
109
|
-
isUserSpeaking: boolean;
|
|
110
|
-
} | {
|
|
111
|
-
type: "interrupted_response";
|
|
112
|
-
interruptedResponse: string;
|
|
113
|
-
} | {
|
|
114
|
-
type: "assistant_ended_conversation";
|
|
115
|
-
} | {
|
|
116
|
-
type: "error";
|
|
117
|
-
error: {
|
|
118
|
-
message: string;
|
|
119
|
-
code?: string;
|
|
120
|
-
};
|
|
121
|
-
param_errors?: {
|
|
122
|
-
input_format?: string;
|
|
123
|
-
system_prompt?: string;
|
|
124
|
-
welcome_message?: string;
|
|
125
|
-
voice_id?: string;
|
|
126
|
-
output_format?: string;
|
|
127
|
-
};
|
|
128
|
-
};
|
|
129
|
-
type OnMessageCallback = (message: PhonicSTSWebSocketResponseMessage) => void;
|
|
130
|
-
type OnCloseCallback = (event: WebSocket.CloseEvent) => void;
|
|
131
|
-
type OnErrorCallback = (event: WebSocket.ErrorEvent) => void;
|
|
132
|
-
type PhonicSTSOutboundCallConfig = Omit<PhonicSTSConfig, "input_format" | "output_format">;
|
|
133
|
-
type OutboundCallSuccessResponse = {
|
|
134
|
-
success: true;
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
type PhonicSTSTwilioOutboundCallParams = {
|
|
138
|
-
account_sid: string;
|
|
139
|
-
api_key_sid: string;
|
|
140
|
-
api_key_secret: string;
|
|
141
|
-
from_phone_number: string;
|
|
142
|
-
to_phone_number: string;
|
|
143
|
-
};
|
|
144
|
-
type PhonicSTSTwilioOutboundCallConfig = PhonicSTSOutboundCallConfig;
|
|
145
|
-
type TwilioOutboundCallSuccessResponse = {
|
|
146
|
-
success: true;
|
|
147
|
-
callSid: string;
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
declare class Twilio {
|
|
151
|
-
private readonly phonic;
|
|
152
|
-
constructor(phonic: Phonic);
|
|
153
|
-
outboundCall(params: PhonicSTSTwilioOutboundCallParams, config: PhonicSTSTwilioOutboundCallConfig): DataOrError<TwilioOutboundCallSuccessResponse>;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
declare class PhonicSTSWebSocket {
|
|
157
|
-
private readonly ws;
|
|
158
|
-
private readonly config;
|
|
159
|
-
private onMessageCallback;
|
|
160
|
-
private onCloseCallback;
|
|
161
|
-
private onErrorCallback;
|
|
162
|
-
private buffer;
|
|
163
|
-
private isOpen;
|
|
164
|
-
constructor(ws: WebSocket, config: PhonicSTSConfig);
|
|
165
|
-
onMessage(callback: OnMessageCallback): void;
|
|
166
|
-
onClose(callback: OnCloseCallback): void;
|
|
167
|
-
onError(callback: OnErrorCallback): void;
|
|
168
|
-
audioChunk({ audio }: {
|
|
169
|
-
audio: string;
|
|
170
|
-
}): void;
|
|
171
|
-
updateSystemPrompt({ systemPrompt }: {
|
|
172
|
-
systemPrompt: string;
|
|
173
|
-
}): void;
|
|
174
|
-
setExternalId({ externalId }: {
|
|
175
|
-
externalId: string;
|
|
176
|
-
}): void;
|
|
177
|
-
close(code?: number): void;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
declare class SpeechToSpeech {
|
|
181
|
-
private readonly phonic;
|
|
182
|
-
readonly twilio: Twilio;
|
|
183
|
-
constructor(phonic: Phonic);
|
|
184
|
-
websocket(config: PhonicSTSConfig): PhonicSTSWebSocket;
|
|
185
|
-
outboundCall(toPhoneNumber: string, config: PhonicSTSOutboundCallConfig): DataOrError<OutboundCallSuccessResponse>;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
type Voice = {
|
|
189
|
-
id: string;
|
|
190
|
-
name: string;
|
|
191
|
-
};
|
|
192
|
-
type VoicesSuccessResponse = {
|
|
193
|
-
voices: Array<Voice>;
|
|
194
|
-
};
|
|
195
|
-
type VoiceSuccessResponse = {
|
|
196
|
-
voice: Voice;
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
declare class Voices {
|
|
200
|
-
private readonly phonic;
|
|
201
|
-
constructor(phonic: Phonic);
|
|
202
|
-
list({ model }: {
|
|
203
|
-
model: string;
|
|
204
|
-
}): DataOrError<VoicesSuccessResponse>;
|
|
205
|
-
get(id: string): DataOrError<VoiceSuccessResponse>;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
declare class Phonic {
|
|
209
|
-
readonly apiKey: string;
|
|
210
|
-
readonly baseUrl: string;
|
|
211
|
-
readonly __downstreamWebSocketUrl: string | null;
|
|
212
|
-
readonly headers: Record<string, string>;
|
|
213
|
-
readonly conversations: Conversations;
|
|
214
|
-
readonly voices: Voices;
|
|
215
|
-
readonly sts: SpeechToSpeech;
|
|
216
|
-
constructor(apiKey: string, config?: PhonicConfig);
|
|
217
|
-
fetchRequest<T>(path: string, options: FetchOptions): DataOrError<T>;
|
|
218
|
-
get<T>(path: string): Promise<{
|
|
219
|
-
data: null;
|
|
220
|
-
error: {
|
|
221
|
-
message: string;
|
|
222
|
-
code?: string;
|
|
223
|
-
};
|
|
224
|
-
} | {
|
|
225
|
-
data: T;
|
|
226
|
-
error: null;
|
|
227
|
-
}>;
|
|
228
|
-
post<T>(path: string, body: Record<string, unknown>, headers?: Record<string, string>): Promise<{
|
|
229
|
-
data: null;
|
|
230
|
-
error: {
|
|
231
|
-
message: string;
|
|
232
|
-
code?: string;
|
|
233
|
-
};
|
|
234
|
-
} | {
|
|
235
|
-
data: T;
|
|
236
|
-
error: null;
|
|
237
|
-
}>;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
export { Phonic, type PhonicSTSConfig, PhonicSTSWebSocket };
|
package/dist/index.js
DELETED
|
@@ -1,347 +0,0 @@
|
|
|
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
|
-
});
|