@stream-io/node-sdk 0.4.13 → 0.4.14

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.
@@ -1,6 +1,6 @@
1
1
  import { ApiConfig, RequestMetadata } from './types';
2
2
  export declare class BaseApi {
3
- private readonly apiConfig;
3
+ protected readonly apiConfig: ApiConfig;
4
4
  constructor(apiConfig: ApiConfig);
5
5
  protected sendRequest: <T>(method: string, url: string, pathParams?: Record<string, string>, queryParams?: Record<string, any>, body?: any) => Promise<{
6
6
  body: T;
@@ -1,5 +1,18 @@
1
1
  import { VideoApi } from './gen/video/VideoApi';
2
2
  import { StreamCall } from './StreamCall';
3
+ import type { StreamClient } from './StreamClient';
4
+ import type { ApiConfig } from './types';
5
+ import type { RealtimeClient } from '@stream-io/openai-realtime-api';
3
6
  export declare class StreamVideoClient extends VideoApi {
7
+ private readonly streamClient;
8
+ constructor({ streamClient, ...apiConfig }: ApiConfig & {
9
+ streamClient: StreamClient;
10
+ });
4
11
  call: (type: string, id: string) => StreamCall;
12
+ connectOpenAi: (options: {
13
+ call: StreamCall;
14
+ agentUserId: string;
15
+ openAiApiKey: string;
16
+ validityInSeconds: number;
17
+ }) => Promise<RealtimeClient>;
5
18
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stream-io/node-sdk",
3
- "version": "0.4.13",
3
+ "version": "0.4.14",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": {
@@ -49,6 +49,7 @@
49
49
  "@openapitools/openapi-generator-cli": "^2.7.0",
50
50
  "@rollup/plugin-replace": "^5.0.2",
51
51
  "@rollup/plugin-typescript": "^11.1.4",
52
+ "@stream-io/openai-realtime-api": "prerelease",
52
53
  "@types/uuid": "^9.0.4",
53
54
  "@typescript-eslint/eslint-plugin": "^6.4.0",
54
55
  "dotenv": "^16.3.1",
@@ -74,6 +75,14 @@
74
75
  "jsonwebtoken": "^9.0.2",
75
76
  "uuid": "^9.0.1"
76
77
  },
78
+ "peerDependencies": {
79
+ "@stream-io/openai-realtime-api": "prerelease"
80
+ },
81
+ "peerDependenciesMeta": {
82
+ "@stream-io/openai-realtime-api": {
83
+ "optional": true
84
+ }
85
+ },
77
86
  "engines": {
78
87
  "node": ">=18.0.0"
79
88
  },
package/src/BaseApi.ts CHANGED
@@ -4,7 +4,7 @@ import { APIError } from './gen/models';
4
4
  import { getRateLimitFromResponseHeader } from './utils/rate-limit';
5
5
 
6
6
  export class BaseApi {
7
- constructor(private readonly apiConfig: ApiConfig) {}
7
+ constructor(protected readonly apiConfig: ApiConfig) {}
8
8
 
9
9
  protected sendRequest = async <T>(
10
10
  method: string,
@@ -38,6 +38,7 @@ export class StreamClient extends CommonApi {
38
38
  super({ apiKey, token, timeout, baseUrl: chatBaseUrl });
39
39
 
40
40
  this.video = new StreamVideoClient({
41
+ streamClient: this,
41
42
  apiKey,
42
43
  token,
43
44
  timeout,
@@ -1,8 +1,63 @@
1
1
  import { VideoApi } from './gen/video/VideoApi';
2
2
  import { StreamCall } from './StreamCall';
3
+ import type { StreamClient } from './StreamClient';
4
+ import type { ApiConfig } from './types';
5
+ import type {
6
+ RealtimeClient,
7
+ createRealtimeClient,
8
+ } from '@stream-io/openai-realtime-api';
3
9
 
4
10
  export class StreamVideoClient extends VideoApi {
11
+ private readonly streamClient: StreamClient;
12
+
13
+ constructor({
14
+ streamClient,
15
+ ...apiConfig
16
+ }: ApiConfig & { streamClient: StreamClient }) {
17
+ super(apiConfig);
18
+ this.streamClient = streamClient;
19
+ }
20
+
5
21
  call = (type: string, id: string) => {
6
22
  return new StreamCall(this, type, id);
7
23
  };
24
+
25
+ connectOpenAi = async (options: {
26
+ call: StreamCall;
27
+ agentUserId: string;
28
+ openAiApiKey: string;
29
+ validityInSeconds: number;
30
+ }): Promise<RealtimeClient> => {
31
+ let doCreateRealtimeClient: typeof createRealtimeClient;
32
+
33
+ try {
34
+ doCreateRealtimeClient = (await import('@stream-io/openai-realtime-api'))
35
+ .createRealtimeClient;
36
+ } catch {
37
+ throw new Error(
38
+ 'Cannot create Realtime API client. Is @stream-io/openai-realtime-api installed?',
39
+ );
40
+ }
41
+
42
+ if (!options.agentUserId) {
43
+ throw new Error('"agentUserId" must by specified in options');
44
+ }
45
+
46
+ const token = this.streamClient.generateCallToken({
47
+ user_id: options.agentUserId,
48
+ call_cids: [options.call.cid],
49
+ validity_in_seconds: options.validityInSeconds,
50
+ });
51
+
52
+ const realtimeClient = doCreateRealtimeClient({
53
+ baseUrl: this.apiConfig.baseUrl,
54
+ call: options.call,
55
+ streamApiKey: this.apiConfig.apiKey,
56
+ streamUserToken: token,
57
+ openAiApiKey: options.openAiApiKey,
58
+ });
59
+
60
+ await realtimeClient.connect();
61
+ return realtimeClient;
62
+ };
8
63
  }