@openai/agents-extensions 0.1.2 → 0.1.5
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/CloudflareRealtimeTransport.d.ts +21 -0
- package/dist/CloudflareRealtimeTransport.js +60 -0
- package/dist/CloudflareRealtimeTransport.js.map +1 -0
- package/dist/CloudflareRealtimeTransport.mjs +56 -0
- package/dist/CloudflareRealtimeTransport.mjs.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -1
- package/dist/metadata.js +2 -2
- package/dist/metadata.mjs +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { RealtimeTransportLayer, OpenAIRealtimeWebSocket, OpenAIRealtimeWebSocketOptions } from '@openai/agents/realtime';
|
|
2
|
+
/**
|
|
3
|
+
* An adapter transport for Cloudflare Workers (workerd) environments.
|
|
4
|
+
*
|
|
5
|
+
* Cloudflare Workers cannot open outbound client WebSockets using the global `WebSocket`
|
|
6
|
+
* constructor. Instead, a `fetch()` request with `Upgrade: websocket` must be performed and the
|
|
7
|
+
* returned `response.webSocket` must be `accept()`ed. This transport encapsulates that pattern and
|
|
8
|
+
* plugs into the Realtime SDK via the factory-based `createWebSocket` option.
|
|
9
|
+
*
|
|
10
|
+
* It behaves like `OpenAIRealtimeWebSocket`, but establishes the connection using `fetch()` and
|
|
11
|
+
* sets `skipOpenEventListeners: true` since workerd sockets do not emit a traditional `open`
|
|
12
|
+
* event after acceptance.
|
|
13
|
+
*
|
|
14
|
+
* Reference: Response API — `response.webSocket` (Cloudflare Workers).
|
|
15
|
+
* https://developers.cloudflare.com/workers/runtime-apis/response/.
|
|
16
|
+
*/
|
|
17
|
+
export declare class CloudflareRealtimeTransportLayer extends OpenAIRealtimeWebSocket implements RealtimeTransportLayer {
|
|
18
|
+
#private;
|
|
19
|
+
protected _audioLengthMs: number;
|
|
20
|
+
constructor(options: OpenAIRealtimeWebSocketOptions);
|
|
21
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CloudflareRealtimeTransportLayer = void 0;
|
|
4
|
+
const realtime_1 = require("@openai/agents/realtime");
|
|
5
|
+
/**
|
|
6
|
+
* An adapter transport for Cloudflare Workers (workerd) environments.
|
|
7
|
+
*
|
|
8
|
+
* Cloudflare Workers cannot open outbound client WebSockets using the global `WebSocket`
|
|
9
|
+
* constructor. Instead, a `fetch()` request with `Upgrade: websocket` must be performed and the
|
|
10
|
+
* returned `response.webSocket` must be `accept()`ed. This transport encapsulates that pattern and
|
|
11
|
+
* plugs into the Realtime SDK via the factory-based `createWebSocket` option.
|
|
12
|
+
*
|
|
13
|
+
* It behaves like `OpenAIRealtimeWebSocket`, but establishes the connection using `fetch()` and
|
|
14
|
+
* sets `skipOpenEventListeners: true` since workerd sockets do not emit a traditional `open`
|
|
15
|
+
* event after acceptance.
|
|
16
|
+
*
|
|
17
|
+
* Reference: Response API — `response.webSocket` (Cloudflare Workers).
|
|
18
|
+
* https://developers.cloudflare.com/workers/runtime-apis/response/.
|
|
19
|
+
*/
|
|
20
|
+
class CloudflareRealtimeTransportLayer extends realtime_1.OpenAIRealtimeWebSocket {
|
|
21
|
+
_audioLengthMs = 0;
|
|
22
|
+
constructor(options) {
|
|
23
|
+
super({
|
|
24
|
+
...options,
|
|
25
|
+
createWebSocket: async ({ url, apiKey }) => {
|
|
26
|
+
return await this.#buildCloudflareWebSocket({ url, apiKey });
|
|
27
|
+
},
|
|
28
|
+
skipOpenEventListeners: true,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Builds a WebSocket using Cloudflare's `fetch()` + `Upgrade: websocket` flow and accepts it.
|
|
33
|
+
* Transforms `ws(s)` to `http(s)` for the upgrade request and forwards standard headers.
|
|
34
|
+
*/
|
|
35
|
+
async #buildCloudflareWebSocket({ url, apiKey, }) {
|
|
36
|
+
const transformedUrl = url.replace(/^ws/i, 'http');
|
|
37
|
+
if (!transformedUrl) {
|
|
38
|
+
throw new Error('Realtime URL is not defined');
|
|
39
|
+
}
|
|
40
|
+
const response = await fetch(transformedUrl, {
|
|
41
|
+
method: 'GET',
|
|
42
|
+
headers: {
|
|
43
|
+
Authorization: `Bearer ${apiKey}`,
|
|
44
|
+
'Sec-WebSocket-Protocol': 'realtime',
|
|
45
|
+
Connection: 'Upgrade',
|
|
46
|
+
Upgrade: 'websocket',
|
|
47
|
+
...this.getCommonRequestHeaders(),
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
const upgradedSocket = response.webSocket;
|
|
51
|
+
if (!upgradedSocket) {
|
|
52
|
+
const body = await response.text().catch(() => '');
|
|
53
|
+
throw new Error(`Failed to upgrade websocket: ${response.status} ${body}`);
|
|
54
|
+
}
|
|
55
|
+
upgradedSocket.accept();
|
|
56
|
+
return upgradedSocket;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.CloudflareRealtimeTransportLayer = CloudflareRealtimeTransportLayer;
|
|
60
|
+
//# sourceMappingURL=CloudflareRealtimeTransport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CloudflareRealtimeTransport.js","sourceRoot":"","sources":["../src/CloudflareRealtimeTransport.ts"],"names":[],"mappings":";;;AAAA,sDAIiC;AAEjC;;;;;;;;;;;;;;GAcG;AACH,MAAa,gCACX,SAAQ,kCAAuB;IAGrB,cAAc,GAAW,CAAC,CAAC;IAErC,YAAY,OAAuC;QACjD,KAAK,CAAC;YACJ,GAAG,OAAO;YACV,eAAe,EAAE,KAAK,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE;gBACzC,OAAO,MAAM,IAAI,CAAC,yBAAyB,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,CAAC;YAC/D,CAAC;YACD,sBAAsB,EAAE,IAAI;SAC7B,CAAC,CAAC;IACL,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,yBAAyB,CAAC,EAC9B,GAAG,EACH,MAAM,GAIP;QACC,MAAM,cAAc,GAAG,GAAG,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACnD,IAAI,CAAC,cAAc,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;QACjD,CAAC;QAED,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,cAAc,EAAE;YAC3C,MAAM,EAAE,KAAK;YACb,OAAO,EAAE;gBACP,aAAa,EAAE,UAAU,MAAM,EAAE;gBACjC,wBAAwB,EAAE,UAAU;gBACpC,UAAU,EAAE,SAAS;gBACrB,OAAO,EAAE,WAAW;gBACpB,GAAG,IAAI,CAAC,uBAAuB,EAAE;aAClC;SACF,CAAC,CAAC;QAEH,MAAM,cAAc,GAAI,QAAgB,CAAC,SAAS,CAAC;QACnD,IAAI,CAAC,cAAc,EAAE,CAAC;YACpB,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;YACnD,MAAM,IAAI,KAAK,CACb,gCAAgC,QAAQ,CAAC,MAAM,IAAI,IAAI,EAAE,CAC1D,CAAC;QACJ,CAAC;QAED,cAAc,CAAC,MAAM,EAAE,CAAC;QACxB,OAAO,cAAsC,CAAC;IAChD,CAAC;CACF;AAtDD,4EAsDC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { OpenAIRealtimeWebSocket, } from '@openai/agents/realtime';
|
|
2
|
+
/**
|
|
3
|
+
* An adapter transport for Cloudflare Workers (workerd) environments.
|
|
4
|
+
*
|
|
5
|
+
* Cloudflare Workers cannot open outbound client WebSockets using the global `WebSocket`
|
|
6
|
+
* constructor. Instead, a `fetch()` request with `Upgrade: websocket` must be performed and the
|
|
7
|
+
* returned `response.webSocket` must be `accept()`ed. This transport encapsulates that pattern and
|
|
8
|
+
* plugs into the Realtime SDK via the factory-based `createWebSocket` option.
|
|
9
|
+
*
|
|
10
|
+
* It behaves like `OpenAIRealtimeWebSocket`, but establishes the connection using `fetch()` and
|
|
11
|
+
* sets `skipOpenEventListeners: true` since workerd sockets do not emit a traditional `open`
|
|
12
|
+
* event after acceptance.
|
|
13
|
+
*
|
|
14
|
+
* Reference: Response API — `response.webSocket` (Cloudflare Workers).
|
|
15
|
+
* https://developers.cloudflare.com/workers/runtime-apis/response/.
|
|
16
|
+
*/
|
|
17
|
+
export class CloudflareRealtimeTransportLayer extends OpenAIRealtimeWebSocket {
|
|
18
|
+
_audioLengthMs = 0;
|
|
19
|
+
constructor(options) {
|
|
20
|
+
super({
|
|
21
|
+
...options,
|
|
22
|
+
createWebSocket: async ({ url, apiKey }) => {
|
|
23
|
+
return await this.#buildCloudflareWebSocket({ url, apiKey });
|
|
24
|
+
},
|
|
25
|
+
skipOpenEventListeners: true,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Builds a WebSocket using Cloudflare's `fetch()` + `Upgrade: websocket` flow and accepts it.
|
|
30
|
+
* Transforms `ws(s)` to `http(s)` for the upgrade request and forwards standard headers.
|
|
31
|
+
*/
|
|
32
|
+
async #buildCloudflareWebSocket({ url, apiKey, }) {
|
|
33
|
+
const transformedUrl = url.replace(/^ws/i, 'http');
|
|
34
|
+
if (!transformedUrl) {
|
|
35
|
+
throw new Error('Realtime URL is not defined');
|
|
36
|
+
}
|
|
37
|
+
const response = await fetch(transformedUrl, {
|
|
38
|
+
method: 'GET',
|
|
39
|
+
headers: {
|
|
40
|
+
Authorization: `Bearer ${apiKey}`,
|
|
41
|
+
'Sec-WebSocket-Protocol': 'realtime',
|
|
42
|
+
Connection: 'Upgrade',
|
|
43
|
+
Upgrade: 'websocket',
|
|
44
|
+
...this.getCommonRequestHeaders(),
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
const upgradedSocket = response.webSocket;
|
|
48
|
+
if (!upgradedSocket) {
|
|
49
|
+
const body = await response.text().catch(() => '');
|
|
50
|
+
throw new Error(`Failed to upgrade websocket: ${response.status} ${body}`);
|
|
51
|
+
}
|
|
52
|
+
upgradedSocket.accept();
|
|
53
|
+
return upgradedSocket;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=CloudflareRealtimeTransport.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CloudflareRealtimeTransport.mjs","sourceRoot":"","sources":["../src/CloudflareRealtimeTransport.ts"],"names":[],"mappings":"OAAO,EAEL,uBAAuB,GAExB,MAAM,yBAAyB;AAEhC;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,gCACX,SAAQ,uBAAuB;IAGrB,cAAc,GAAW,CAAC,CAAC;IAErC,YAAY,OAAuC;QACjD,KAAK,CAAC;YACJ,GAAG,OAAO;YACV,eAAe,EAAE,KAAK,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE;gBACzC,OAAO,MAAM,IAAI,CAAC,yBAAyB,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,CAAC;YAC/D,CAAC;YACD,sBAAsB,EAAE,IAAI;SAC7B,CAAC,CAAC;IACL,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,yBAAyB,CAAC,EAC9B,GAAG,EACH,MAAM,GAIP;QACC,MAAM,cAAc,GAAG,GAAG,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACnD,IAAI,CAAC,cAAc,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;QACjD,CAAC;QAED,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,cAAc,EAAE;YAC3C,MAAM,EAAE,KAAK;YACb,OAAO,EAAE;gBACP,aAAa,EAAE,UAAU,MAAM,EAAE;gBACjC,wBAAwB,EAAE,UAAU;gBACpC,UAAU,EAAE,SAAS;gBACrB,OAAO,EAAE,WAAW;gBACpB,GAAG,IAAI,CAAC,uBAAuB,EAAE;aAClC;SACF,CAAC,CAAC;QAEH,MAAM,cAAc,GAAI,QAAgB,CAAC,SAAS,CAAC;QACnD,IAAI,CAAC,cAAc,EAAE,CAAC;YACpB,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;YACnD,MAAM,IAAI,KAAK,CACb,gCAAgC,QAAQ,CAAC,MAAM,IAAI,IAAI,EAAE,CAC1D,CAAC;QACJ,CAAC;QAED,cAAc,CAAC,MAAM,EAAE,CAAC;QACxB,OAAO,cAAsC,CAAC;IAChD,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./TwilioRealtimeTransport.js"), exports);
|
|
18
17
|
__exportStar(require("./aiSdk.js"), exports);
|
|
18
|
+
__exportStar(require("./CloudflareRealtimeTransport.js"), exports);
|
|
19
|
+
__exportStar(require("./TwilioRealtimeTransport.js"), exports);
|
|
19
20
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+DAA0C
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAAwB;AACxB,mEAA8C;AAC9C,+DAA0C"}
|
package/dist/index.mjs
CHANGED
package/dist/metadata.js
CHANGED
|
@@ -4,9 +4,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.METADATA = void 0;
|
|
5
5
|
exports.METADATA = {
|
|
6
6
|
"name": "@openai/agents-extensions",
|
|
7
|
-
"version": "0.1.
|
|
7
|
+
"version": "0.1.5",
|
|
8
8
|
"versions": {
|
|
9
|
-
"@openai/agents-extensions": "0.1.
|
|
9
|
+
"@openai/agents-extensions": "0.1.5"
|
|
10
10
|
}
|
|
11
11
|
};
|
|
12
12
|
exports.default = exports.METADATA;
|
package/dist/metadata.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// This file is automatically generated
|
|
2
2
|
export const METADATA = {
|
|
3
3
|
"name": "@openai/agents-extensions",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.5",
|
|
5
5
|
"versions": {
|
|
6
|
-
"@openai/agents-extensions": "0.1.
|
|
6
|
+
"@openai/agents-extensions": "0.1.5"
|
|
7
7
|
}
|
|
8
8
|
};
|
|
9
9
|
export default METADATA;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@openai/agents-extensions",
|
|
3
3
|
"repository": "https://github.com/openai/openai-agents-js",
|
|
4
4
|
"homepage": "https://openai.github.io/openai-agents-js/",
|
|
5
|
-
"version": "0.1.
|
|
5
|
+
"version": "0.1.5",
|
|
6
6
|
"description": "Extensions for the OpenAI Agents SDK",
|
|
7
7
|
"author": "OpenAI <support@openai.com>",
|
|
8
8
|
"main": "dist/index.js",
|