phonic 0.0.0 → 0.1.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/LICENSE +7 -0
- package/README.md +97 -1
- package/dist/index.d.mts +91 -0
- package/dist/index.d.ts +91 -0
- package/dist/index.js +198 -0
- package/dist/index.mjs +161 -0
- package/package.json +51 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2024 Phonic, Inc.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,99 @@
|
|
|
1
1
|
# Phonic Node.js SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Node.js library for the Phonic API.
|
|
4
|
+
|
|
5
|
+
- [Installation](#installation)
|
|
6
|
+
- [Setup](#setup)
|
|
7
|
+
- [Usage](#usage)
|
|
8
|
+
- [Get voices](#get-voices)
|
|
9
|
+
- [Get voice by id](#get-voice-by-id)
|
|
10
|
+
- [Text-to-speech via WebSocket](#text-to-speech-via-websocket)
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm i phonic
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Setup
|
|
19
|
+
|
|
20
|
+
Grab an API key from [Phonic settings](https://phonic.co/settings) and pass it to the Phonic constructor.
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
import { Phonic } from "phonic";
|
|
24
|
+
|
|
25
|
+
const phonic = new Phonic("ph_...");
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
### Get voices
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
const { data, error } = await phonic.voices.list();
|
|
34
|
+
|
|
35
|
+
if (error === null) {
|
|
36
|
+
console.log(data.voices);
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
### Get voice by id
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
const { data, error } = await phonic.voices.get("australian-man");
|
|
45
|
+
|
|
46
|
+
if (error === null) {
|
|
47
|
+
console.log(data.voice);
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Text-to-speech via WebSocket
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
const { data, error } = await phonic.tts.websocket();
|
|
55
|
+
|
|
56
|
+
if (error === null) {
|
|
57
|
+
const { phonicWebSocket } = data;
|
|
58
|
+
const stream = phonicWebSocket.send({
|
|
59
|
+
type: "generate",
|
|
60
|
+
script: "How can I help you today?", // 600 characters max
|
|
61
|
+
output_format: "mulaw_8000", // or "pcm_44100"
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
for await (const data of stream) {
|
|
65
|
+
if (data instanceof Buffer) {
|
|
66
|
+
// Do something with the audio chunk,
|
|
67
|
+
// e.g. send `data.toString("base64")` to Twilio.
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
phonicWebSocket.close();
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
To perform other work while receiving chunks, use:
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
phonicWebSocket.onMessage((data) => {
|
|
79
|
+
if (data instanceof Buffer) {
|
|
80
|
+
// Do something with the audio chunk,
|
|
81
|
+
// e.g. send `data.toString("base64")` to Twilio.
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
phonicWebSocket.send({
|
|
86
|
+
type: "generate",
|
|
87
|
+
script: "How can I help you today?",
|
|
88
|
+
output_format: "mulaw_8000",
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Perform other work here
|
|
92
|
+
|
|
93
|
+
await phonicWebSocket.streamEnded; // This Promise will be resolved once the last chunk is received
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import WebSocket from 'ws';
|
|
2
|
+
|
|
3
|
+
type PhonicConfigBaseUrl = `http://${string}` | `https://${string}`;
|
|
4
|
+
type PhonicConfig = {
|
|
5
|
+
baseUrl?: PhonicConfigBaseUrl;
|
|
6
|
+
};
|
|
7
|
+
type FetchOptions = {
|
|
8
|
+
method: "GET";
|
|
9
|
+
};
|
|
10
|
+
type ErrorResponse = {
|
|
11
|
+
message: string;
|
|
12
|
+
code?: string;
|
|
13
|
+
};
|
|
14
|
+
type DataOrError<T> = Promise<{
|
|
15
|
+
data: T;
|
|
16
|
+
error: null;
|
|
17
|
+
} | {
|
|
18
|
+
data: null;
|
|
19
|
+
error: ErrorResponse;
|
|
20
|
+
}>;
|
|
21
|
+
|
|
22
|
+
type PhonicWebSocketMessage = {
|
|
23
|
+
type: "generate";
|
|
24
|
+
script: string;
|
|
25
|
+
output_format: "pcm_44100" | "mulaw_8000";
|
|
26
|
+
};
|
|
27
|
+
type PhonicWebSocketResponseMessage = {
|
|
28
|
+
type: "stream-ended";
|
|
29
|
+
error?: {
|
|
30
|
+
message: string;
|
|
31
|
+
code?: string;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
type OnMessageCallback = (data: PhonicWebSocketResponseMessage | Buffer) => void;
|
|
35
|
+
|
|
36
|
+
declare class PhonicWebSocket {
|
|
37
|
+
private readonly ws;
|
|
38
|
+
private onMessageCallback;
|
|
39
|
+
private streamEndedResolve;
|
|
40
|
+
readonly streamEnded: Promise<void>;
|
|
41
|
+
private streamController;
|
|
42
|
+
constructor(ws: WebSocket);
|
|
43
|
+
onMessage(callback: OnMessageCallback): void;
|
|
44
|
+
send(message: PhonicWebSocketMessage): ReadableStream<any>;
|
|
45
|
+
close(): void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
declare class TextToSpeech {
|
|
49
|
+
private readonly phonic;
|
|
50
|
+
constructor(phonic: Phonic);
|
|
51
|
+
websocket(): DataOrError<{
|
|
52
|
+
phonicWebSocket: PhonicWebSocket;
|
|
53
|
+
}>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
type Voice = {
|
|
57
|
+
id: string;
|
|
58
|
+
name: string;
|
|
59
|
+
};
|
|
60
|
+
type VoicesSuccessResponse = {
|
|
61
|
+
voices: Array<Voice>;
|
|
62
|
+
};
|
|
63
|
+
type VoiceSuccessResponse = {
|
|
64
|
+
voice: Voice;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
declare class Voices {
|
|
68
|
+
private readonly phonic;
|
|
69
|
+
constructor(phonic: Phonic);
|
|
70
|
+
list(): DataOrError<VoicesSuccessResponse>;
|
|
71
|
+
get(id: string): DataOrError<VoiceSuccessResponse>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
declare class Phonic {
|
|
75
|
+
readonly apiKey: string;
|
|
76
|
+
readonly baseUrl: string;
|
|
77
|
+
private readonly headers;
|
|
78
|
+
readonly voices: Voices;
|
|
79
|
+
readonly tts: TextToSpeech;
|
|
80
|
+
constructor(apiKey: string, config?: PhonicConfig);
|
|
81
|
+
fetchRequest<T>(path: string, options: FetchOptions): DataOrError<T>;
|
|
82
|
+
get<T>(path: string): Promise<{
|
|
83
|
+
data: null;
|
|
84
|
+
error: ErrorResponse;
|
|
85
|
+
} | {
|
|
86
|
+
data: T;
|
|
87
|
+
error: null;
|
|
88
|
+
}>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { Phonic };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import WebSocket from 'ws';
|
|
2
|
+
|
|
3
|
+
type PhonicConfigBaseUrl = `http://${string}` | `https://${string}`;
|
|
4
|
+
type PhonicConfig = {
|
|
5
|
+
baseUrl?: PhonicConfigBaseUrl;
|
|
6
|
+
};
|
|
7
|
+
type FetchOptions = {
|
|
8
|
+
method: "GET";
|
|
9
|
+
};
|
|
10
|
+
type ErrorResponse = {
|
|
11
|
+
message: string;
|
|
12
|
+
code?: string;
|
|
13
|
+
};
|
|
14
|
+
type DataOrError<T> = Promise<{
|
|
15
|
+
data: T;
|
|
16
|
+
error: null;
|
|
17
|
+
} | {
|
|
18
|
+
data: null;
|
|
19
|
+
error: ErrorResponse;
|
|
20
|
+
}>;
|
|
21
|
+
|
|
22
|
+
type PhonicWebSocketMessage = {
|
|
23
|
+
type: "generate";
|
|
24
|
+
script: string;
|
|
25
|
+
output_format: "pcm_44100" | "mulaw_8000";
|
|
26
|
+
};
|
|
27
|
+
type PhonicWebSocketResponseMessage = {
|
|
28
|
+
type: "stream-ended";
|
|
29
|
+
error?: {
|
|
30
|
+
message: string;
|
|
31
|
+
code?: string;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
type OnMessageCallback = (data: PhonicWebSocketResponseMessage | Buffer) => void;
|
|
35
|
+
|
|
36
|
+
declare class PhonicWebSocket {
|
|
37
|
+
private readonly ws;
|
|
38
|
+
private onMessageCallback;
|
|
39
|
+
private streamEndedResolve;
|
|
40
|
+
readonly streamEnded: Promise<void>;
|
|
41
|
+
private streamController;
|
|
42
|
+
constructor(ws: WebSocket);
|
|
43
|
+
onMessage(callback: OnMessageCallback): void;
|
|
44
|
+
send(message: PhonicWebSocketMessage): ReadableStream<any>;
|
|
45
|
+
close(): void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
declare class TextToSpeech {
|
|
49
|
+
private readonly phonic;
|
|
50
|
+
constructor(phonic: Phonic);
|
|
51
|
+
websocket(): DataOrError<{
|
|
52
|
+
phonicWebSocket: PhonicWebSocket;
|
|
53
|
+
}>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
type Voice = {
|
|
57
|
+
id: string;
|
|
58
|
+
name: string;
|
|
59
|
+
};
|
|
60
|
+
type VoicesSuccessResponse = {
|
|
61
|
+
voices: Array<Voice>;
|
|
62
|
+
};
|
|
63
|
+
type VoiceSuccessResponse = {
|
|
64
|
+
voice: Voice;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
declare class Voices {
|
|
68
|
+
private readonly phonic;
|
|
69
|
+
constructor(phonic: Phonic);
|
|
70
|
+
list(): DataOrError<VoicesSuccessResponse>;
|
|
71
|
+
get(id: string): DataOrError<VoiceSuccessResponse>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
declare class Phonic {
|
|
75
|
+
readonly apiKey: string;
|
|
76
|
+
readonly baseUrl: string;
|
|
77
|
+
private readonly headers;
|
|
78
|
+
readonly voices: Voices;
|
|
79
|
+
readonly tts: TextToSpeech;
|
|
80
|
+
constructor(apiKey: string, config?: PhonicConfig);
|
|
81
|
+
fetchRequest<T>(path: string, options: FetchOptions): DataOrError<T>;
|
|
82
|
+
get<T>(path: string): Promise<{
|
|
83
|
+
data: null;
|
|
84
|
+
error: ErrorResponse;
|
|
85
|
+
} | {
|
|
86
|
+
data: T;
|
|
87
|
+
error: null;
|
|
88
|
+
}>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { Phonic };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
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 src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
Phonic: () => Phonic
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
|
|
37
|
+
// package.json
|
|
38
|
+
var version = "0.1.0";
|
|
39
|
+
|
|
40
|
+
// src/tts/index.ts
|
|
41
|
+
var import_ws = __toESM(require("ws"));
|
|
42
|
+
|
|
43
|
+
// src/tts/websocket.ts
|
|
44
|
+
var PhonicWebSocket = class {
|
|
45
|
+
constructor(ws) {
|
|
46
|
+
this.ws = ws;
|
|
47
|
+
this.ws.onmessage = (event) => {
|
|
48
|
+
if (typeof event.data === "string") {
|
|
49
|
+
const dataObj = JSON.parse(
|
|
50
|
+
event.data
|
|
51
|
+
);
|
|
52
|
+
this.onMessageCallback?.(dataObj);
|
|
53
|
+
if (dataObj.type === "stream-ended") {
|
|
54
|
+
this.streamEndedResolve();
|
|
55
|
+
this.streamController?.close();
|
|
56
|
+
} else {
|
|
57
|
+
this.streamController?.enqueue(dataObj);
|
|
58
|
+
}
|
|
59
|
+
} else if (event.data instanceof Buffer) {
|
|
60
|
+
this.onMessageCallback?.(event.data);
|
|
61
|
+
this.streamController?.enqueue(event.data);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
onMessageCallback = null;
|
|
66
|
+
streamEndedResolve = () => {
|
|
67
|
+
};
|
|
68
|
+
streamEnded = new Promise((resolve) => {
|
|
69
|
+
this.streamEndedResolve = resolve;
|
|
70
|
+
});
|
|
71
|
+
streamController = null;
|
|
72
|
+
onMessage(callback) {
|
|
73
|
+
this.onMessageCallback = callback;
|
|
74
|
+
}
|
|
75
|
+
send(message) {
|
|
76
|
+
const self = this;
|
|
77
|
+
self.streamController?.close();
|
|
78
|
+
this.ws.send(JSON.stringify(message));
|
|
79
|
+
return new ReadableStream({
|
|
80
|
+
start(controller) {
|
|
81
|
+
self.streamController = controller;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
close() {
|
|
86
|
+
this.ws.close();
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// src/tts/index.ts
|
|
91
|
+
var TextToSpeech = class {
|
|
92
|
+
constructor(phonic) {
|
|
93
|
+
this.phonic = phonic;
|
|
94
|
+
}
|
|
95
|
+
async websocket() {
|
|
96
|
+
return new Promise((resolve) => {
|
|
97
|
+
const wsBaseUrl = this.phonic.baseUrl.replace(/^http/, "ws");
|
|
98
|
+
const ws = new import_ws.default(`${wsBaseUrl}/v1/generate/audio/ws`, {
|
|
99
|
+
headers: {
|
|
100
|
+
Authorization: `Bearer ${this.phonic.apiKey}`
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
ws.onopen = () => {
|
|
104
|
+
const phonicWebSocket = new PhonicWebSocket(ws);
|
|
105
|
+
resolve({ data: { phonicWebSocket }, error: null });
|
|
106
|
+
};
|
|
107
|
+
ws.onerror = (error) => {
|
|
108
|
+
resolve({
|
|
109
|
+
data: null,
|
|
110
|
+
error: {
|
|
111
|
+
message: error.message
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// src/voices/index.ts
|
|
120
|
+
var Voices = class {
|
|
121
|
+
constructor(phonic) {
|
|
122
|
+
this.phonic = phonic;
|
|
123
|
+
}
|
|
124
|
+
async list() {
|
|
125
|
+
const response = await this.phonic.get("/voices");
|
|
126
|
+
return response;
|
|
127
|
+
}
|
|
128
|
+
async get(id) {
|
|
129
|
+
const response = await this.phonic.get(
|
|
130
|
+
`/voices/${id}`
|
|
131
|
+
);
|
|
132
|
+
return response;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// src/phonic.ts
|
|
137
|
+
var defaultBaseUrl = "https://api.phonic.co";
|
|
138
|
+
var defaultUserAgent = `phonic-node:${version}`;
|
|
139
|
+
var Phonic = class {
|
|
140
|
+
constructor(apiKey, config) {
|
|
141
|
+
this.apiKey = apiKey;
|
|
142
|
+
if (typeof process === "undefined") {
|
|
143
|
+
throw new Error(
|
|
144
|
+
"Phonic SDK is intended to be used in Node.js environment."
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
if (!this.apiKey) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
'API key is missing. Pass it to the constructor: `new Phonic("ph_...")`'
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
this.baseUrl = config?.baseUrl ?? defaultBaseUrl;
|
|
153
|
+
this.headers = new Headers({
|
|
154
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
155
|
+
"User-Agent": process.env.PHONIC_USER_AGENT || defaultUserAgent,
|
|
156
|
+
"Content-Type": "application/json"
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
baseUrl;
|
|
160
|
+
headers;
|
|
161
|
+
voices = new Voices(this);
|
|
162
|
+
tts = new TextToSpeech(this);
|
|
163
|
+
async fetchRequest(path, options) {
|
|
164
|
+
try {
|
|
165
|
+
const response = await fetch(`${this.baseUrl}/v1${path}`, {
|
|
166
|
+
headers: this.headers,
|
|
167
|
+
...options
|
|
168
|
+
});
|
|
169
|
+
if (!response.ok) {
|
|
170
|
+
const statusText = await response.text();
|
|
171
|
+
console.error(response);
|
|
172
|
+
return {
|
|
173
|
+
data: null,
|
|
174
|
+
error: {
|
|
175
|
+
message: statusText
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
const data = await response.json();
|
|
180
|
+
return { data, error: null };
|
|
181
|
+
} catch (error) {
|
|
182
|
+
console.error(error);
|
|
183
|
+
return {
|
|
184
|
+
data: null,
|
|
185
|
+
error: {
|
|
186
|
+
message: "Fetch request failed"
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
async get(path) {
|
|
192
|
+
return this.fetchRequest(path, { method: "GET" });
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
196
|
+
0 && (module.exports = {
|
|
197
|
+
Phonic
|
|
198
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// package.json
|
|
2
|
+
var version = "0.1.0";
|
|
3
|
+
|
|
4
|
+
// src/tts/index.ts
|
|
5
|
+
import WebSocket from "ws";
|
|
6
|
+
|
|
7
|
+
// src/tts/websocket.ts
|
|
8
|
+
var PhonicWebSocket = class {
|
|
9
|
+
constructor(ws) {
|
|
10
|
+
this.ws = ws;
|
|
11
|
+
this.ws.onmessage = (event) => {
|
|
12
|
+
if (typeof event.data === "string") {
|
|
13
|
+
const dataObj = JSON.parse(
|
|
14
|
+
event.data
|
|
15
|
+
);
|
|
16
|
+
this.onMessageCallback?.(dataObj);
|
|
17
|
+
if (dataObj.type === "stream-ended") {
|
|
18
|
+
this.streamEndedResolve();
|
|
19
|
+
this.streamController?.close();
|
|
20
|
+
} else {
|
|
21
|
+
this.streamController?.enqueue(dataObj);
|
|
22
|
+
}
|
|
23
|
+
} else if (event.data instanceof Buffer) {
|
|
24
|
+
this.onMessageCallback?.(event.data);
|
|
25
|
+
this.streamController?.enqueue(event.data);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
onMessageCallback = null;
|
|
30
|
+
streamEndedResolve = () => {
|
|
31
|
+
};
|
|
32
|
+
streamEnded = new Promise((resolve) => {
|
|
33
|
+
this.streamEndedResolve = resolve;
|
|
34
|
+
});
|
|
35
|
+
streamController = null;
|
|
36
|
+
onMessage(callback) {
|
|
37
|
+
this.onMessageCallback = callback;
|
|
38
|
+
}
|
|
39
|
+
send(message) {
|
|
40
|
+
const self = this;
|
|
41
|
+
self.streamController?.close();
|
|
42
|
+
this.ws.send(JSON.stringify(message));
|
|
43
|
+
return new ReadableStream({
|
|
44
|
+
start(controller) {
|
|
45
|
+
self.streamController = controller;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
close() {
|
|
50
|
+
this.ws.close();
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// src/tts/index.ts
|
|
55
|
+
var TextToSpeech = class {
|
|
56
|
+
constructor(phonic) {
|
|
57
|
+
this.phonic = phonic;
|
|
58
|
+
}
|
|
59
|
+
async websocket() {
|
|
60
|
+
return new Promise((resolve) => {
|
|
61
|
+
const wsBaseUrl = this.phonic.baseUrl.replace(/^http/, "ws");
|
|
62
|
+
const ws = new WebSocket(`${wsBaseUrl}/v1/generate/audio/ws`, {
|
|
63
|
+
headers: {
|
|
64
|
+
Authorization: `Bearer ${this.phonic.apiKey}`
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
ws.onopen = () => {
|
|
68
|
+
const phonicWebSocket = new PhonicWebSocket(ws);
|
|
69
|
+
resolve({ data: { phonicWebSocket }, error: null });
|
|
70
|
+
};
|
|
71
|
+
ws.onerror = (error) => {
|
|
72
|
+
resolve({
|
|
73
|
+
data: null,
|
|
74
|
+
error: {
|
|
75
|
+
message: error.message
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// src/voices/index.ts
|
|
84
|
+
var Voices = class {
|
|
85
|
+
constructor(phonic) {
|
|
86
|
+
this.phonic = phonic;
|
|
87
|
+
}
|
|
88
|
+
async list() {
|
|
89
|
+
const response = await this.phonic.get("/voices");
|
|
90
|
+
return response;
|
|
91
|
+
}
|
|
92
|
+
async get(id) {
|
|
93
|
+
const response = await this.phonic.get(
|
|
94
|
+
`/voices/${id}`
|
|
95
|
+
);
|
|
96
|
+
return response;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// src/phonic.ts
|
|
101
|
+
var defaultBaseUrl = "https://api.phonic.co";
|
|
102
|
+
var defaultUserAgent = `phonic-node:${version}`;
|
|
103
|
+
var Phonic = class {
|
|
104
|
+
constructor(apiKey, config) {
|
|
105
|
+
this.apiKey = apiKey;
|
|
106
|
+
if (typeof process === "undefined") {
|
|
107
|
+
throw new Error(
|
|
108
|
+
"Phonic SDK is intended to be used in Node.js environment."
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
if (!this.apiKey) {
|
|
112
|
+
throw new Error(
|
|
113
|
+
'API key is missing. Pass it to the constructor: `new Phonic("ph_...")`'
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
this.baseUrl = config?.baseUrl ?? defaultBaseUrl;
|
|
117
|
+
this.headers = new Headers({
|
|
118
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
119
|
+
"User-Agent": process.env.PHONIC_USER_AGENT || defaultUserAgent,
|
|
120
|
+
"Content-Type": "application/json"
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
baseUrl;
|
|
124
|
+
headers;
|
|
125
|
+
voices = new Voices(this);
|
|
126
|
+
tts = new TextToSpeech(this);
|
|
127
|
+
async fetchRequest(path, options) {
|
|
128
|
+
try {
|
|
129
|
+
const response = await fetch(`${this.baseUrl}/v1${path}`, {
|
|
130
|
+
headers: this.headers,
|
|
131
|
+
...options
|
|
132
|
+
});
|
|
133
|
+
if (!response.ok) {
|
|
134
|
+
const statusText = await response.text();
|
|
135
|
+
console.error(response);
|
|
136
|
+
return {
|
|
137
|
+
data: null,
|
|
138
|
+
error: {
|
|
139
|
+
message: statusText
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const data = await response.json();
|
|
144
|
+
return { data, error: null };
|
|
145
|
+
} catch (error) {
|
|
146
|
+
console.error(error);
|
|
147
|
+
return {
|
|
148
|
+
data: null,
|
|
149
|
+
error: {
|
|
150
|
+
message: "Fetch request failed"
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async get(path) {
|
|
156
|
+
return this.fetchRequest(path, { method: "GET" });
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
export {
|
|
160
|
+
Phonic
|
|
161
|
+
};
|
package/package.json
CHANGED
|
@@ -1,16 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "phonic",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Phonic Node.js SDK",
|
|
5
|
-
"main": "index.js",
|
|
6
5
|
"scripts": {
|
|
7
|
-
"
|
|
6
|
+
"build": "tsup",
|
|
7
|
+
"check": "biome check --write",
|
|
8
|
+
"ci": "bun tsc && biome ci && bun test",
|
|
9
|
+
"release": "bun run build && changeset publish"
|
|
10
|
+
},
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"module": "./dist/index.mjs",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./dist/index.d.mts",
|
|
18
|
+
"default": "./dist/index.mjs"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
8
25
|
},
|
|
9
26
|
"repository": {
|
|
10
27
|
"type": "git",
|
|
11
28
|
"url": "git+https://github.com/Phonic-Co/phonic-node.git"
|
|
12
29
|
},
|
|
13
|
-
"
|
|
14
|
-
"
|
|
30
|
+
"homepage": "https://github.com/Phonic-Co/phonic-node#readme",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/Phonic-Co/phonic-node/issues"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"ws": "8.18.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@biomejs/biome": "1.9.4",
|
|
39
|
+
"@changesets/cli": "2.27.9",
|
|
40
|
+
"@types/bun": "1.1.13",
|
|
41
|
+
"tsup": "8.3.5",
|
|
42
|
+
"typescript": "5.6.3",
|
|
43
|
+
"zod": "3.23.8"
|
|
44
|
+
},
|
|
45
|
+
"files": ["dist/**"],
|
|
46
|
+
"author": {
|
|
47
|
+
"name": "Phonic",
|
|
48
|
+
"url": "https://phonic.co"
|
|
49
|
+
},
|
|
50
|
+
"keywords": [
|
|
51
|
+
"phonic",
|
|
52
|
+
"text-to-speech",
|
|
53
|
+
"tts",
|
|
54
|
+
"javascript",
|
|
55
|
+
"typescript",
|
|
56
|
+
"ai",
|
|
57
|
+
"voice",
|
|
58
|
+
"audio",
|
|
59
|
+
"sdk"
|
|
60
|
+
],
|
|
15
61
|
"license": "MIT"
|
|
16
62
|
}
|