@charivo/realtime-provider-openai 0.2.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 +21 -0
- package/README.md +49 -0
- package/dist/index.d.mts +16 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +96 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +93 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Zeikar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# @charivo/realtime-provider-openai
|
|
2
|
+
|
|
3
|
+
Server-side OpenAI realtime provider for Charivo.
|
|
4
|
+
|
|
5
|
+
This package owns the OpenAI realtime session bootstrap call. In browser apps,
|
|
6
|
+
pair it with a server route and consume that route through
|
|
7
|
+
`@charivo/realtime-client-remote`.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pnpm add @charivo/realtime-provider-openai
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { createOpenAIRealtimeProvider } from "@charivo/realtime-provider-openai";
|
|
19
|
+
|
|
20
|
+
const provider = createOpenAIRealtimeProvider({
|
|
21
|
+
apiKey: process.env.OPENAI_API_KEY!,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const session = await provider.createSession({
|
|
25
|
+
transport: "webrtc",
|
|
26
|
+
sdpOffer: "offer-sdp",
|
|
27
|
+
session: {
|
|
28
|
+
provider: "openai",
|
|
29
|
+
model: "gpt-realtime-mini",
|
|
30
|
+
voice: "marin",
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Returned bootstrap:
|
|
36
|
+
|
|
37
|
+
```json
|
|
38
|
+
{
|
|
39
|
+
"adapter": "openai-webrtc",
|
|
40
|
+
"transport": "webrtc",
|
|
41
|
+
"answerSdp": "..."
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Config
|
|
46
|
+
|
|
47
|
+
- `apiKey`
|
|
48
|
+
- `baseUrl?` defaults to `https://api.openai.com/v1`
|
|
49
|
+
- `dangerouslyAllowBrowser?` testing only
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { RealtimeProvider, RealtimeSessionRequest, RealtimeSessionBootstrap } from '@charivo/core';
|
|
2
|
+
|
|
3
|
+
interface OpenAIRealtimeProviderConfig {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
dangerouslyAllowBrowser?: boolean;
|
|
7
|
+
}
|
|
8
|
+
declare class OpenAIRealtimeProvider implements RealtimeProvider {
|
|
9
|
+
private config;
|
|
10
|
+
private endpoint;
|
|
11
|
+
constructor(config: OpenAIRealtimeProviderConfig);
|
|
12
|
+
createSession(request: RealtimeSessionRequest): Promise<RealtimeSessionBootstrap>;
|
|
13
|
+
}
|
|
14
|
+
declare function createOpenAIRealtimeProvider(config: OpenAIRealtimeProviderConfig): OpenAIRealtimeProvider;
|
|
15
|
+
|
|
16
|
+
export { OpenAIRealtimeProvider, type OpenAIRealtimeProviderConfig, createOpenAIRealtimeProvider };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { RealtimeProvider, RealtimeSessionRequest, RealtimeSessionBootstrap } from '@charivo/core';
|
|
2
|
+
|
|
3
|
+
interface OpenAIRealtimeProviderConfig {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
dangerouslyAllowBrowser?: boolean;
|
|
7
|
+
}
|
|
8
|
+
declare class OpenAIRealtimeProvider implements RealtimeProvider {
|
|
9
|
+
private config;
|
|
10
|
+
private endpoint;
|
|
11
|
+
constructor(config: OpenAIRealtimeProviderConfig);
|
|
12
|
+
createSession(request: RealtimeSessionRequest): Promise<RealtimeSessionBootstrap>;
|
|
13
|
+
}
|
|
14
|
+
declare function createOpenAIRealtimeProvider(config: OpenAIRealtimeProviderConfig): OpenAIRealtimeProvider;
|
|
15
|
+
|
|
16
|
+
export { OpenAIRealtimeProvider, type OpenAIRealtimeProviderConfig, createOpenAIRealtimeProvider };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@charivo/core');
|
|
4
|
+
var shared = require('@charivo/shared');
|
|
5
|
+
|
|
6
|
+
// src/index.ts
|
|
7
|
+
var DEFAULT_REALTIME_URL = "https://api.openai.com/v1/realtime/calls";
|
|
8
|
+
var OpenAIRealtimeProvider = class {
|
|
9
|
+
constructor(config) {
|
|
10
|
+
this.config = config;
|
|
11
|
+
if (typeof window !== "undefined" && !config.dangerouslyAllowBrowser) {
|
|
12
|
+
throw new Error(
|
|
13
|
+
"OpenAI realtime provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing"
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
this.endpoint = config.baseUrl ? `${config.baseUrl.replace(/\/$/, "")}/realtime/calls` : DEFAULT_REALTIME_URL;
|
|
17
|
+
}
|
|
18
|
+
endpoint;
|
|
19
|
+
async createSession(request) {
|
|
20
|
+
if (request.session.provider !== void 0 && request.session.provider !== "openai") {
|
|
21
|
+
throw new Error(
|
|
22
|
+
`OpenAI realtime provider only supports provider "openai", received ${request.session.provider}`
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
if (request.transport !== "webrtc") {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`OpenAI realtime provider only supports webrtc transport, received ${request.transport}`
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
if (!request.sdpOffer) {
|
|
31
|
+
throw new Error("SDP offer is required for WebRTC realtime sessions");
|
|
32
|
+
}
|
|
33
|
+
const response = await shared.fetchWithTimeout(
|
|
34
|
+
this.endpoint,
|
|
35
|
+
{
|
|
36
|
+
method: "POST",
|
|
37
|
+
headers: {
|
|
38
|
+
Authorization: `Bearer ${this.config.apiKey}`
|
|
39
|
+
},
|
|
40
|
+
body: toRealtimeFormData(request)
|
|
41
|
+
},
|
|
42
|
+
`OpenAI realtime request timed out after ${shared.DEFAULT_REQUEST_TIMEOUT_MS}ms`
|
|
43
|
+
);
|
|
44
|
+
if (!response.ok) {
|
|
45
|
+
const errorText = await response.text();
|
|
46
|
+
throw new Error(`OpenAI Realtime Error: ${errorText}`);
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
adapter: core.OPENAI_REALTIME_ADAPTER,
|
|
50
|
+
transport: "webrtc",
|
|
51
|
+
answerSdp: await response.text()
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
function createOpenAIRealtimeProvider(config) {
|
|
56
|
+
return new OpenAIRealtimeProvider(config);
|
|
57
|
+
}
|
|
58
|
+
function toRealtimeFormData(request) {
|
|
59
|
+
const formData = new FormData();
|
|
60
|
+
formData.set("sdp", request.sdpOffer || "");
|
|
61
|
+
formData.set(
|
|
62
|
+
"session",
|
|
63
|
+
JSON.stringify(toOpenAIRealtimeSession(request.session))
|
|
64
|
+
);
|
|
65
|
+
return formData;
|
|
66
|
+
}
|
|
67
|
+
function toOpenAIRealtimeSession(session) {
|
|
68
|
+
const payload = {
|
|
69
|
+
type: "realtime",
|
|
70
|
+
model: session.model || "gpt-realtime-mini",
|
|
71
|
+
audio: {
|
|
72
|
+
output: {
|
|
73
|
+
voice: session.voice || "marin"
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
tool_choice: session.toolChoice ?? "auto"
|
|
77
|
+
};
|
|
78
|
+
if (session.instructions) {
|
|
79
|
+
payload.instructions = session.instructions;
|
|
80
|
+
}
|
|
81
|
+
if (session.temperature !== void 0) {
|
|
82
|
+
payload.temperature = session.temperature;
|
|
83
|
+
}
|
|
84
|
+
if (session.maxTokens !== void 0) {
|
|
85
|
+
payload.max_response_output_tokens = session.maxTokens;
|
|
86
|
+
}
|
|
87
|
+
if (session.tools?.length) {
|
|
88
|
+
payload.tools = session.tools;
|
|
89
|
+
}
|
|
90
|
+
return payload;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
exports.OpenAIRealtimeProvider = OpenAIRealtimeProvider;
|
|
94
|
+
exports.createOpenAIRealtimeProvider = createOpenAIRealtimeProvider;
|
|
95
|
+
//# sourceMappingURL=index.js.map
|
|
96
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"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GACrC;AAEA,EAAA,IAAI,QAAQ,YAAA,EAAc;AACxB,IAAA,OAAA,CAAQ,eAAe,OAAA,CAAQ,YAAA;AAAA,EACjC;AAEA,EAAA,IAAI,OAAA,CAAQ,gBAAgB,MAAA,EAAW;AACrC,IAAA,OAAA,CAAQ,cAAc,OAAA,CAAQ,WAAA;AAAA,EAChC;AAEA,EAAA,IAAI,OAAA,CAAQ,cAAc,MAAA,EAAW;AACnC,IAAA,OAAA,CAAQ,6BAA6B,OAAA,CAAQ,SAAA;AAAA,EAC/C;AAEA,EAAA,IAAI,OAAA,CAAQ,OAAO,MAAA,EAAQ;AACzB,IAAA,OAAA,CAAQ,QAAQ,OAAA,CAAQ,KAAA;AAAA,EAC1B;AAEA,EAAA,OAAO,OAAA;AACT","file":"index.js","sourcesContent":["import type {\n RealtimeProvider,\n RealtimeSessionBootstrap,\n RealtimeSessionConfig,\n RealtimeSessionRequest,\n} from \"@charivo/core\";\nimport { OPENAI_REALTIME_ADAPTER } from \"@charivo/core\";\nimport { DEFAULT_REQUEST_TIMEOUT_MS, fetchWithTimeout } from \"@charivo/shared\";\n\nconst DEFAULT_REALTIME_URL = \"https://api.openai.com/v1/realtime/calls\";\n\nexport interface OpenAIRealtimeProviderConfig {\n apiKey: string;\n baseUrl?: string;\n dangerouslyAllowBrowser?: boolean;\n}\n\nexport class OpenAIRealtimeProvider implements RealtimeProvider {\n private endpoint: string;\n\n constructor(private config: OpenAIRealtimeProviderConfig) {\n if (typeof window !== \"undefined\" && !config.dangerouslyAllowBrowser) {\n throw new Error(\n \"OpenAI realtime provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing\",\n );\n }\n\n this.endpoint = config.baseUrl\n ? `${config.baseUrl.replace(/\\/$/, \"\")}/realtime/calls`\n : DEFAULT_REALTIME_URL;\n }\n\n async createSession(\n request: RealtimeSessionRequest,\n ): Promise<RealtimeSessionBootstrap> {\n if (\n request.session.provider !== undefined &&\n request.session.provider !== \"openai\"\n ) {\n throw new Error(\n `OpenAI realtime provider only supports provider \"openai\", received ${request.session.provider}`,\n );\n }\n\n if (request.transport !== \"webrtc\") {\n throw new Error(\n `OpenAI realtime provider only supports webrtc transport, received ${request.transport}`,\n );\n }\n\n if (!request.sdpOffer) {\n throw new Error(\"SDP offer is required for WebRTC realtime sessions\");\n }\n\n const response = await fetchWithTimeout(\n this.endpoint,\n {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${this.config.apiKey}`,\n },\n body: toRealtimeFormData(request),\n },\n `OpenAI realtime request timed out after ${DEFAULT_REQUEST_TIMEOUT_MS}ms`,\n );\n\n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(`OpenAI Realtime Error: ${errorText}`);\n }\n\n return {\n adapter: OPENAI_REALTIME_ADAPTER,\n transport: \"webrtc\",\n answerSdp: await response.text(),\n };\n }\n}\n\nexport function createOpenAIRealtimeProvider(\n config: OpenAIRealtimeProviderConfig,\n): OpenAIRealtimeProvider {\n return new OpenAIRealtimeProvider(config);\n}\n\nfunction toRealtimeFormData(request: RealtimeSessionRequest): FormData {\n const formData = new FormData();\n formData.set(\"sdp\", request.sdpOffer || \"\");\n formData.set(\n \"session\",\n JSON.stringify(toOpenAIRealtimeSession(request.session)),\n );\n return formData;\n}\n\nfunction toOpenAIRealtimeSession(\n session: RealtimeSessionConfig,\n): Record<string, unknown> {\n const payload: Record<string, unknown> = {\n type: \"realtime\",\n model: session.model || \"gpt-realtime-mini\",\n audio: {\n output: {\n voice: session.voice || \"marin\",\n },\n },\n tool_choice: session.toolChoice ?? \"auto\",\n };\n\n if (session.instructions) {\n payload.instructions = session.instructions;\n }\n\n if (session.temperature !== undefined) {\n payload.temperature = session.temperature;\n }\n\n if (session.maxTokens !== undefined) {\n payload.max_response_output_tokens = session.maxTokens;\n }\n\n if (session.tools?.length) {\n payload.tools = session.tools;\n }\n\n return payload;\n}\n"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { OPENAI_REALTIME_ADAPTER } from '@charivo/core';
|
|
2
|
+
import { fetchWithTimeout, DEFAULT_REQUEST_TIMEOUT_MS } from '@charivo/shared';
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
var DEFAULT_REALTIME_URL = "https://api.openai.com/v1/realtime/calls";
|
|
6
|
+
var OpenAIRealtimeProvider = class {
|
|
7
|
+
constructor(config) {
|
|
8
|
+
this.config = config;
|
|
9
|
+
if (typeof window !== "undefined" && !config.dangerouslyAllowBrowser) {
|
|
10
|
+
throw new Error(
|
|
11
|
+
"OpenAI realtime provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing"
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
this.endpoint = config.baseUrl ? `${config.baseUrl.replace(/\/$/, "")}/realtime/calls` : DEFAULT_REALTIME_URL;
|
|
15
|
+
}
|
|
16
|
+
endpoint;
|
|
17
|
+
async createSession(request) {
|
|
18
|
+
if (request.session.provider !== void 0 && request.session.provider !== "openai") {
|
|
19
|
+
throw new Error(
|
|
20
|
+
`OpenAI realtime provider only supports provider "openai", received ${request.session.provider}`
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
if (request.transport !== "webrtc") {
|
|
24
|
+
throw new Error(
|
|
25
|
+
`OpenAI realtime provider only supports webrtc transport, received ${request.transport}`
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
if (!request.sdpOffer) {
|
|
29
|
+
throw new Error("SDP offer is required for WebRTC realtime sessions");
|
|
30
|
+
}
|
|
31
|
+
const response = await fetchWithTimeout(
|
|
32
|
+
this.endpoint,
|
|
33
|
+
{
|
|
34
|
+
method: "POST",
|
|
35
|
+
headers: {
|
|
36
|
+
Authorization: `Bearer ${this.config.apiKey}`
|
|
37
|
+
},
|
|
38
|
+
body: toRealtimeFormData(request)
|
|
39
|
+
},
|
|
40
|
+
`OpenAI realtime request timed out after ${DEFAULT_REQUEST_TIMEOUT_MS}ms`
|
|
41
|
+
);
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
const errorText = await response.text();
|
|
44
|
+
throw new Error(`OpenAI Realtime Error: ${errorText}`);
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
adapter: OPENAI_REALTIME_ADAPTER,
|
|
48
|
+
transport: "webrtc",
|
|
49
|
+
answerSdp: await response.text()
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
function createOpenAIRealtimeProvider(config) {
|
|
54
|
+
return new OpenAIRealtimeProvider(config);
|
|
55
|
+
}
|
|
56
|
+
function toRealtimeFormData(request) {
|
|
57
|
+
const formData = new FormData();
|
|
58
|
+
formData.set("sdp", request.sdpOffer || "");
|
|
59
|
+
formData.set(
|
|
60
|
+
"session",
|
|
61
|
+
JSON.stringify(toOpenAIRealtimeSession(request.session))
|
|
62
|
+
);
|
|
63
|
+
return formData;
|
|
64
|
+
}
|
|
65
|
+
function toOpenAIRealtimeSession(session) {
|
|
66
|
+
const payload = {
|
|
67
|
+
type: "realtime",
|
|
68
|
+
model: session.model || "gpt-realtime-mini",
|
|
69
|
+
audio: {
|
|
70
|
+
output: {
|
|
71
|
+
voice: session.voice || "marin"
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
tool_choice: session.toolChoice ?? "auto"
|
|
75
|
+
};
|
|
76
|
+
if (session.instructions) {
|
|
77
|
+
payload.instructions = session.instructions;
|
|
78
|
+
}
|
|
79
|
+
if (session.temperature !== void 0) {
|
|
80
|
+
payload.temperature = session.temperature;
|
|
81
|
+
}
|
|
82
|
+
if (session.maxTokens !== void 0) {
|
|
83
|
+
payload.max_response_output_tokens = session.maxTokens;
|
|
84
|
+
}
|
|
85
|
+
if (session.tools?.length) {
|
|
86
|
+
payload.tools = session.tools;
|
|
87
|
+
}
|
|
88
|
+
return payload;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { OpenAIRealtimeProvider, createOpenAIRealtimeProvider };
|
|
92
|
+
//# sourceMappingURL=index.mjs.map
|
|
93
|
+
//# sour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EAAA,IAAI,QAAQ,YAAA,EAAc;AACxB,IAAA,OAAA,CAAQ,eAAe,OAAA,CAAQ,YAAA;AAAA,EACjC;AAEA,EAAA,IAAI,OAAA,CAAQ,gBAAgB,MAAA,EAAW;AACrC,IAAA,OAAA,CAAQ,cAAc,OAAA,CAAQ,WAAA;AAAA,EAChC;AAEA,EAAA,IAAI,OAAA,CAAQ,cAAc,MAAA,EAAW;AACnC,IAAA,OAAA,CAAQ,6BAA6B,OAAA,CAAQ,SAAA;AAAA,EAC/C;AAEA,EAAA,IAAI,OAAA,CAAQ,OAAO,MAAA,EAAQ;AACzB,IAAA,OAAA,CAAQ,QAAQ,OAAA,CAAQ,KAAA;AAAA,EAC1B;AAEA,EAAA,OAAO,OAAA;AACT","file":"index.mjs","sourcesContent":["import type {\n RealtimeProvider,\n RealtimeSessionBootstrap,\n RealtimeSessionConfig,\n RealtimeSessionRequest,\n} from \"@charivo/core\";\nimport { OPENAI_REALTIME_ADAPTER } from \"@charivo/core\";\nimport { DEFAULT_REQUEST_TIMEOUT_MS, fetchWithTimeout } from \"@charivo/shared\";\n\nconst DEFAULT_REALTIME_URL = \"https://api.openai.com/v1/realtime/calls\";\n\nexport interface OpenAIRealtimeProviderConfig {\n apiKey: string;\n baseUrl?: string;\n dangerouslyAllowBrowser?: boolean;\n}\n\nexport class OpenAIRealtimeProvider implements RealtimeProvider {\n private endpoint: string;\n\n constructor(private config: OpenAIRealtimeProviderConfig) {\n if (typeof window !== \"undefined\" && !config.dangerouslyAllowBrowser) {\n throw new Error(\n \"OpenAI realtime provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing\",\n );\n }\n\n this.endpoint = config.baseUrl\n ? `${config.baseUrl.replace(/\\/$/, \"\")}/realtime/calls`\n : DEFAULT_REALTIME_URL;\n }\n\n async createSession(\n request: RealtimeSessionRequest,\n ): Promise<RealtimeSessionBootstrap> {\n if (\n request.session.provider !== undefined &&\n request.session.provider !== \"openai\"\n ) {\n throw new Error(\n `OpenAI realtime provider only supports provider \"openai\", received ${request.session.provider}`,\n );\n }\n\n if (request.transport !== \"webrtc\") {\n throw new Error(\n `OpenAI realtime provider only supports webrtc transport, received ${request.transport}`,\n );\n }\n\n if (!request.sdpOffer) {\n throw new Error(\"SDP offer is required for WebRTC realtime sessions\");\n }\n\n const response = await fetchWithTimeout(\n this.endpoint,\n {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${this.config.apiKey}`,\n },\n body: toRealtimeFormData(request),\n },\n `OpenAI realtime request timed out after ${DEFAULT_REQUEST_TIMEOUT_MS}ms`,\n );\n\n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(`OpenAI Realtime Error: ${errorText}`);\n }\n\n return {\n adapter: OPENAI_REALTIME_ADAPTER,\n transport: \"webrtc\",\n answerSdp: await response.text(),\n };\n }\n}\n\nexport function createOpenAIRealtimeProvider(\n config: OpenAIRealtimeProviderConfig,\n): OpenAIRealtimeProvider {\n return new OpenAIRealtimeProvider(config);\n}\n\nfunction toRealtimeFormData(request: RealtimeSessionRequest): FormData {\n const formData = new FormData();\n formData.set(\"sdp\", request.sdpOffer || \"\");\n formData.set(\n \"session\",\n JSON.stringify(toOpenAIRealtimeSession(request.session)),\n );\n return formData;\n}\n\nfunction toOpenAIRealtimeSession(\n session: RealtimeSessionConfig,\n): Record<string, unknown> {\n const payload: Record<string, unknown> = {\n type: \"realtime\",\n model: session.model || \"gpt-realtime-mini\",\n audio: {\n output: {\n voice: session.voice || \"marin\",\n },\n },\n tool_choice: session.toolChoice ?? \"auto\",\n };\n\n if (session.instructions) {\n payload.instructions = session.instructions;\n }\n\n if (session.temperature !== undefined) {\n payload.temperature = session.temperature;\n }\n\n if (session.maxTokens !== undefined) {\n payload.max_response_output_tokens = session.maxTokens;\n }\n\n if (session.tools?.length) {\n payload.tools = session.tools;\n }\n\n return payload;\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@charivo/realtime-provider-openai",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "OpenAI realtime provider for Charivo (server-side)",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@charivo/core": "0.3.0",
|
|
17
|
+
"@charivo/shared": "0.1.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"tsup": "^8.5.0",
|
|
21
|
+
"typescript": "^5.9.2"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/zeikar/charivo.git",
|
|
33
|
+
"directory": "packages/realtime-provider-openai"
|
|
34
|
+
},
|
|
35
|
+
"author": {
|
|
36
|
+
"name": "Zeikar",
|
|
37
|
+
"url": "https://github.com/zeikar"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/zeikar/charivo#readme",
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/zeikar/charivo/issues"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsup",
|
|
45
|
+
"dev": "tsup --watch",
|
|
46
|
+
"clean": "rm -rf dist",
|
|
47
|
+
"typecheck": "tsc --noEmit"
|
|
48
|
+
}
|
|
49
|
+
}
|