phonic 0.10.0 → 0.12.0
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/README.md +34 -2
- package/dist/index.d.mts +47 -2
- package/dist/index.d.ts +47 -2
- package/dist/index.js +33 -5
- package/dist/index.mjs +33 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,6 +7,8 @@ Node.js library for the Phonic API.
|
|
|
7
7
|
- [Usage](#usage)
|
|
8
8
|
- [Get voices](#get-voices)
|
|
9
9
|
- [Get voice by id](#get-voice-by-id)
|
|
10
|
+
- [Get conversation by id](#get-conversation-by-id)
|
|
11
|
+
- [Get conversation by external id](#get-conversation-by-external-id)
|
|
10
12
|
- [Speech-to-speech via WebSocket](#speech-to-speech-via-websocket)
|
|
11
13
|
|
|
12
14
|
## Installation
|
|
@@ -38,7 +40,7 @@ if (error === null) {
|
|
|
38
40
|
```
|
|
39
41
|
|
|
40
42
|
|
|
41
|
-
### Get voice by
|
|
43
|
+
### Get voice by id
|
|
42
44
|
|
|
43
45
|
```ts
|
|
44
46
|
const { data, error } = await phonic.voices.get("meredith");
|
|
@@ -48,6 +50,26 @@ if (error === null) {
|
|
|
48
50
|
}
|
|
49
51
|
```
|
|
50
52
|
|
|
53
|
+
### Get conversation by id
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
const { data, error } = await phonic.conversations.get("conv_b1804883-5be4-42fe-b1cf-aa84450d5c84");
|
|
57
|
+
|
|
58
|
+
if (error === null) {
|
|
59
|
+
console.log(data.conversation);
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Get conversation by external id
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
const { data, error } = await phonic.conversations.getByExternalId("CAdb9c032c809fec7feb932ea4c96d71e1");
|
|
67
|
+
|
|
68
|
+
if (error === null) {
|
|
69
|
+
console.log(data.conversation);
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
51
73
|
### Speech-to-speech via WebSocket
|
|
52
74
|
|
|
53
75
|
To start a conversation, open a WebSocket connection:
|
|
@@ -104,10 +126,20 @@ phonicWebSocket.onMessage((message) => {
|
|
|
104
126
|
}
|
|
105
127
|
});
|
|
106
128
|
```
|
|
129
|
+
|
|
107
130
|
Update the system prompt mid-conversation:
|
|
131
|
+
|
|
108
132
|
```ts
|
|
109
133
|
phonicWebSocket.updateSystemPrompt({
|
|
110
|
-
systemPrompt
|
|
134
|
+
systemPrompt: "..."
|
|
135
|
+
})
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Set an external id for the conversation (can be the Twilio Call SID, for example):
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
phonicWebSocket.setExternalId({
|
|
142
|
+
externalId: "..."
|
|
111
143
|
})
|
|
112
144
|
```
|
|
113
145
|
|
package/dist/index.d.mts
CHANGED
|
@@ -20,6 +20,47 @@ type DataOrError<T> = Promise<{
|
|
|
20
20
|
error: ErrorResponse;
|
|
21
21
|
}>;
|
|
22
22
|
|
|
23
|
+
type ISODateTime = `${string}Z`;
|
|
24
|
+
type ConversationItem = {
|
|
25
|
+
role: "user";
|
|
26
|
+
item_idx: number;
|
|
27
|
+
text: string;
|
|
28
|
+
duration_ms: number;
|
|
29
|
+
started_at: string;
|
|
30
|
+
} | {
|
|
31
|
+
role: "assistant";
|
|
32
|
+
item_idx: number;
|
|
33
|
+
text: string;
|
|
34
|
+
voice_id: string;
|
|
35
|
+
system_prompt: string;
|
|
36
|
+
output_audio_speed: number;
|
|
37
|
+
duration_ms: number;
|
|
38
|
+
started_at: string;
|
|
39
|
+
};
|
|
40
|
+
type Conversation = {
|
|
41
|
+
id: string;
|
|
42
|
+
external_id: string | null;
|
|
43
|
+
model: string;
|
|
44
|
+
welcome_message: string | null;
|
|
45
|
+
input_format: "pcm_44100" | "mulaw_8000";
|
|
46
|
+
output_format: "pcm_44100" | "mulaw_8000";
|
|
47
|
+
text: string;
|
|
48
|
+
duration_ms: number;
|
|
49
|
+
started_at: ISODateTime;
|
|
50
|
+
ended_at: ISODateTime;
|
|
51
|
+
items: Array<ConversationItem>;
|
|
52
|
+
};
|
|
53
|
+
type ConversationSuccessResponse = {
|
|
54
|
+
conversation: Conversation;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
declare class Conversations {
|
|
58
|
+
private readonly phonic;
|
|
59
|
+
constructor(phonic: Phonic);
|
|
60
|
+
get(id: string): DataOrError<ConversationSuccessResponse>;
|
|
61
|
+
getByExternalId(externalId: string): DataOrError<ConversationSuccessResponse>;
|
|
62
|
+
}
|
|
63
|
+
|
|
23
64
|
type PhonicSTSConfig = {
|
|
24
65
|
input_format: "pcm_44100" | "mulaw_8000";
|
|
25
66
|
system_prompt?: string;
|
|
@@ -66,12 +107,15 @@ declare class PhonicSTSWebSocket {
|
|
|
66
107
|
onMessage(callback: OnMessageCallback): void;
|
|
67
108
|
onClose(callback: OnCloseCallback): void;
|
|
68
109
|
onError(callback: OnErrorCallback): void;
|
|
69
|
-
audioChunk(
|
|
110
|
+
audioChunk({ audio }: {
|
|
70
111
|
audio: string;
|
|
71
112
|
}): void;
|
|
72
|
-
updateSystemPrompt(
|
|
113
|
+
updateSystemPrompt({ systemPrompt }: {
|
|
73
114
|
systemPrompt: string;
|
|
74
115
|
}): void;
|
|
116
|
+
setExternalId({ externalId }: {
|
|
117
|
+
externalId: string;
|
|
118
|
+
}): void;
|
|
75
119
|
close(code?: number): void;
|
|
76
120
|
}
|
|
77
121
|
|
|
@@ -109,6 +153,7 @@ declare class Phonic {
|
|
|
109
153
|
readonly baseUrl: string;
|
|
110
154
|
readonly __downstreamWebSocketUrl: string | null;
|
|
111
155
|
readonly headers: Record<string, string>;
|
|
156
|
+
readonly conversations: Conversations;
|
|
112
157
|
readonly voices: Voices;
|
|
113
158
|
readonly sts: SpeechToSpeech;
|
|
114
159
|
constructor(apiKey: string, config?: PhonicConfig);
|
package/dist/index.d.ts
CHANGED
|
@@ -20,6 +20,47 @@ type DataOrError<T> = Promise<{
|
|
|
20
20
|
error: ErrorResponse;
|
|
21
21
|
}>;
|
|
22
22
|
|
|
23
|
+
type ISODateTime = `${string}Z`;
|
|
24
|
+
type ConversationItem = {
|
|
25
|
+
role: "user";
|
|
26
|
+
item_idx: number;
|
|
27
|
+
text: string;
|
|
28
|
+
duration_ms: number;
|
|
29
|
+
started_at: string;
|
|
30
|
+
} | {
|
|
31
|
+
role: "assistant";
|
|
32
|
+
item_idx: number;
|
|
33
|
+
text: string;
|
|
34
|
+
voice_id: string;
|
|
35
|
+
system_prompt: string;
|
|
36
|
+
output_audio_speed: number;
|
|
37
|
+
duration_ms: number;
|
|
38
|
+
started_at: string;
|
|
39
|
+
};
|
|
40
|
+
type Conversation = {
|
|
41
|
+
id: string;
|
|
42
|
+
external_id: string | null;
|
|
43
|
+
model: string;
|
|
44
|
+
welcome_message: string | null;
|
|
45
|
+
input_format: "pcm_44100" | "mulaw_8000";
|
|
46
|
+
output_format: "pcm_44100" | "mulaw_8000";
|
|
47
|
+
text: string;
|
|
48
|
+
duration_ms: number;
|
|
49
|
+
started_at: ISODateTime;
|
|
50
|
+
ended_at: ISODateTime;
|
|
51
|
+
items: Array<ConversationItem>;
|
|
52
|
+
};
|
|
53
|
+
type ConversationSuccessResponse = {
|
|
54
|
+
conversation: Conversation;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
declare class Conversations {
|
|
58
|
+
private readonly phonic;
|
|
59
|
+
constructor(phonic: Phonic);
|
|
60
|
+
get(id: string): DataOrError<ConversationSuccessResponse>;
|
|
61
|
+
getByExternalId(externalId: string): DataOrError<ConversationSuccessResponse>;
|
|
62
|
+
}
|
|
63
|
+
|
|
23
64
|
type PhonicSTSConfig = {
|
|
24
65
|
input_format: "pcm_44100" | "mulaw_8000";
|
|
25
66
|
system_prompt?: string;
|
|
@@ -66,12 +107,15 @@ declare class PhonicSTSWebSocket {
|
|
|
66
107
|
onMessage(callback: OnMessageCallback): void;
|
|
67
108
|
onClose(callback: OnCloseCallback): void;
|
|
68
109
|
onError(callback: OnErrorCallback): void;
|
|
69
|
-
audioChunk(
|
|
110
|
+
audioChunk({ audio }: {
|
|
70
111
|
audio: string;
|
|
71
112
|
}): void;
|
|
72
|
-
updateSystemPrompt(
|
|
113
|
+
updateSystemPrompt({ systemPrompt }: {
|
|
73
114
|
systemPrompt: string;
|
|
74
115
|
}): void;
|
|
116
|
+
setExternalId({ externalId }: {
|
|
117
|
+
externalId: string;
|
|
118
|
+
}): void;
|
|
75
119
|
close(code?: number): void;
|
|
76
120
|
}
|
|
77
121
|
|
|
@@ -109,6 +153,7 @@ declare class Phonic {
|
|
|
109
153
|
readonly baseUrl: string;
|
|
110
154
|
readonly __downstreamWebSocketUrl: string | null;
|
|
111
155
|
readonly headers: Record<string, string>;
|
|
156
|
+
readonly conversations: Conversations;
|
|
112
157
|
readonly voices: Voices;
|
|
113
158
|
readonly sts: SpeechToSpeech;
|
|
114
159
|
constructor(apiKey: string, config?: PhonicConfig);
|
package/dist/index.js
CHANGED
|
@@ -35,7 +35,26 @@ __export(index_exports, {
|
|
|
35
35
|
module.exports = __toCommonJS(index_exports);
|
|
36
36
|
|
|
37
37
|
// package.json
|
|
38
|
-
var version = "0.
|
|
38
|
+
var version = "0.12.0";
|
|
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(externalId) {
|
|
52
|
+
const response = await this.phonic.get(
|
|
53
|
+
`/conversations?external_id=${externalId}`
|
|
54
|
+
);
|
|
55
|
+
return response;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
39
58
|
|
|
40
59
|
// src/sts/index.ts
|
|
41
60
|
var import_ws = __toESM(require("ws"));
|
|
@@ -86,19 +105,27 @@ var PhonicSTSWebSocket = class {
|
|
|
86
105
|
onError(callback) {
|
|
87
106
|
this.onErrorCallback = callback;
|
|
88
107
|
}
|
|
89
|
-
audioChunk(
|
|
108
|
+
audioChunk({ audio }) {
|
|
90
109
|
this.ws.send(
|
|
91
110
|
JSON.stringify({
|
|
92
111
|
type: "audio_chunk",
|
|
93
|
-
|
|
112
|
+
audio
|
|
94
113
|
})
|
|
95
114
|
);
|
|
96
115
|
}
|
|
97
|
-
updateSystemPrompt(
|
|
116
|
+
updateSystemPrompt({ systemPrompt }) {
|
|
98
117
|
this.ws.send(
|
|
99
118
|
JSON.stringify({
|
|
100
119
|
type: "update_system_prompt",
|
|
101
|
-
system_prompt:
|
|
120
|
+
system_prompt: systemPrompt
|
|
121
|
+
})
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
setExternalId({ externalId }) {
|
|
125
|
+
this.ws.send(
|
|
126
|
+
JSON.stringify({
|
|
127
|
+
type: "set_external_id",
|
|
128
|
+
external_id: externalId
|
|
102
129
|
})
|
|
103
130
|
);
|
|
104
131
|
}
|
|
@@ -248,6 +275,7 @@ var Phonic = class {
|
|
|
248
275
|
baseUrl;
|
|
249
276
|
__downstreamWebSocketUrl;
|
|
250
277
|
headers;
|
|
278
|
+
conversations = new Conversations(this);
|
|
251
279
|
voices = new Voices(this);
|
|
252
280
|
sts = new SpeechToSpeech(this);
|
|
253
281
|
async fetchRequest(path, options) {
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
// package.json
|
|
2
|
-
var version = "0.
|
|
2
|
+
var version = "0.12.0";
|
|
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(externalId) {
|
|
16
|
+
const response = await this.phonic.get(
|
|
17
|
+
`/conversations?external_id=${externalId}`
|
|
18
|
+
);
|
|
19
|
+
return response;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
3
22
|
|
|
4
23
|
// src/sts/index.ts
|
|
5
24
|
import WebSocket from "ws";
|
|
@@ -50,19 +69,27 @@ var PhonicSTSWebSocket = class {
|
|
|
50
69
|
onError(callback) {
|
|
51
70
|
this.onErrorCallback = callback;
|
|
52
71
|
}
|
|
53
|
-
audioChunk(
|
|
72
|
+
audioChunk({ audio }) {
|
|
54
73
|
this.ws.send(
|
|
55
74
|
JSON.stringify({
|
|
56
75
|
type: "audio_chunk",
|
|
57
|
-
|
|
76
|
+
audio
|
|
58
77
|
})
|
|
59
78
|
);
|
|
60
79
|
}
|
|
61
|
-
updateSystemPrompt(
|
|
80
|
+
updateSystemPrompt({ systemPrompt }) {
|
|
62
81
|
this.ws.send(
|
|
63
82
|
JSON.stringify({
|
|
64
83
|
type: "update_system_prompt",
|
|
65
|
-
system_prompt:
|
|
84
|
+
system_prompt: systemPrompt
|
|
85
|
+
})
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
setExternalId({ externalId }) {
|
|
89
|
+
this.ws.send(
|
|
90
|
+
JSON.stringify({
|
|
91
|
+
type: "set_external_id",
|
|
92
|
+
external_id: externalId
|
|
66
93
|
})
|
|
67
94
|
);
|
|
68
95
|
}
|
|
@@ -212,6 +239,7 @@ var Phonic = class {
|
|
|
212
239
|
baseUrl;
|
|
213
240
|
__downstreamWebSocketUrl;
|
|
214
241
|
headers;
|
|
242
|
+
conversations = new Conversations(this);
|
|
215
243
|
voices = new Voices(this);
|
|
216
244
|
sts = new SpeechToSpeech(this);
|
|
217
245
|
async fetchRequest(path, options) {
|