@sayna-ai/node-sdk 0.0.21 → 0.0.22

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.
@@ -0,0 +1,30 @@
1
+ import type { ProviderAuth } from "./types";
2
+ /**
3
+ * Resolves Google credentials from a string to a parsed object.
4
+ *
5
+ * If the value is already an object it is returned as-is.
6
+ * If it is a string, the function first tries `JSON.parse`.
7
+ * When parsing fails, the string is treated as a file path
8
+ * and the file is read and parsed.
9
+ *
10
+ * @throws {SaynaValidationError} When the string is neither valid JSON nor a readable JSON file.
11
+ */
12
+ export declare function resolveGoogleCredentials(credentials: string | Record<string, unknown>): Promise<Record<string, unknown>>;
13
+ /**
14
+ * Resolves provider auth credentials.
15
+ *
16
+ * For {@link GoogleAuth} with string credentials the value is resolved
17
+ * via {@link resolveGoogleCredentials}. All other auth types pass through unchanged.
18
+ */
19
+ export declare function resolveProviderAuth(auth: ProviderAuth): Promise<ProviderAuth>;
20
+ /**
21
+ * Resolves provider auth inside an STT or TTS config object.
22
+ *
23
+ * Returns the original config when no resolution is needed,
24
+ * or a shallow copy with the resolved auth otherwise.
25
+ * Accepts `undefined` for convenience and returns it unchanged.
26
+ */
27
+ export declare function resolveConfigAuth<T extends {
28
+ auth?: ProviderAuth;
29
+ }>(config: T | undefined): Promise<T | undefined>;
30
+ //# sourceMappingURL=credentials.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"credentials.d.ts","sourceRoot":"","sources":["../src/credentials.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAc,YAAY,EAAE,MAAM,SAAS,CAAC;AAOxD;;;;;;;;;GASG;AACH,wBAAsB,wBAAwB,CAC5C,WAAW,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC5C,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAmDlC;AAED;;;;;GAKG;AACH,wBAAsB,mBAAmB,CACvC,IAAI,EAAE,YAAY,GACjB,OAAO,CAAC,YAAY,CAAC,CAOvB;AAED;;;;;;GAMG;AACH,wBAAsB,iBAAiB,CACrC,CAAC,SAAS;IAAE,IAAI,CAAC,EAAE,YAAY,CAAA;CAAE,EACjC,MAAM,EAAE,CAAC,GAAG,SAAS,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAS/C"}
package/dist/index.cjs CHANGED
@@ -30,6 +30,9 @@ var __export = (target, all) => {
30
30
  var exports_src = {};
31
31
  __export(exports_src, {
32
32
  saynaConnect: () => saynaConnect,
33
+ resolveProviderAuth: () => resolveProviderAuth,
34
+ resolveGoogleCredentials: () => resolveGoogleCredentials,
35
+ resolveConfigAuth: () => resolveConfigAuth,
33
36
  WebhookReceiver: () => WebhookReceiver,
34
37
  SaynaValidationError: () => SaynaValidationError,
35
38
  SaynaServerError: () => SaynaServerError,
@@ -96,6 +99,62 @@ class SaynaServerError extends SaynaError {
96
99
  }
97
100
  }
98
101
 
102
+ // src/credentials.ts
103
+ var import_promises = require("node:fs/promises");
104
+ function isGoogleAuth(auth) {
105
+ return "credentials" in auth;
106
+ }
107
+ async function resolveGoogleCredentials(credentials) {
108
+ if (typeof credentials !== "string") {
109
+ return credentials;
110
+ }
111
+ try {
112
+ const parsed2 = JSON.parse(credentials);
113
+ if (typeof parsed2 === "object" && parsed2 !== null && !Array.isArray(parsed2)) {
114
+ return parsed2;
115
+ }
116
+ throw new SaynaValidationError(`GoogleAuth credentials JSON string must parse to an object, got ${Array.isArray(parsed2) ? "array" : typeof parsed2}`);
117
+ } catch (error) {
118
+ if (error instanceof SaynaValidationError) {
119
+ throw error;
120
+ }
121
+ }
122
+ let content;
123
+ try {
124
+ content = await import_promises.readFile(credentials, "utf-8");
125
+ } catch (_error) {
126
+ throw new SaynaValidationError(`GoogleAuth credentials string is not valid JSON and could not be read as a file: ${credentials}`);
127
+ }
128
+ let parsed;
129
+ try {
130
+ parsed = JSON.parse(content);
131
+ } catch {
132
+ throw new SaynaValidationError(`GoogleAuth credentials file does not contain valid JSON: ${credentials}`);
133
+ }
134
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
135
+ throw new SaynaValidationError(`GoogleAuth credentials file must contain a JSON object: ${credentials}`);
136
+ }
137
+ return parsed;
138
+ }
139
+ async function resolveProviderAuth(auth) {
140
+ if (!isGoogleAuth(auth) || typeof auth.credentials !== "string") {
141
+ return auth;
142
+ }
143
+ return {
144
+ credentials: await resolveGoogleCredentials(auth.credentials)
145
+ };
146
+ }
147
+ async function resolveConfigAuth(config) {
148
+ if (!config?.auth) {
149
+ return config;
150
+ }
151
+ const resolved = await resolveProviderAuth(config.auth);
152
+ if (resolved === config.auth) {
153
+ return config;
154
+ }
155
+ return { ...config, auth: resolved };
156
+ }
157
+
99
158
  // src/sayna-client.ts
100
159
  var isBun = typeof process !== "undefined" && typeof process.versions?.bun === "string";
101
160
  var isNode = typeof process !== "undefined" && typeof process.versions?.node === "string" && !isBun;
@@ -159,6 +218,8 @@ class SaynaClient {
159
218
  if (this.isConnected) {
160
219
  return;
161
220
  }
221
+ const sttConfig = await resolveConfigAuth(this.sttConfig);
222
+ const ttsConfig = await resolveConfigAuth(this.ttsConfig);
162
223
  const wsUrl = this.getWebSocketUrl();
163
224
  return new Promise((resolve, reject) => {
164
225
  this.readyPromiseResolve = resolve;
@@ -171,8 +232,8 @@ class SaynaClient {
171
232
  const configMessage = {
172
233
  type: "config",
173
234
  stream_id: this.inputStreamId,
174
- stt_config: this.sttConfig,
175
- tts_config: this.ttsConfig,
235
+ stt_config: sttConfig,
236
+ tts_config: ttsConfig,
176
237
  livekit: this.livekitConfig,
177
238
  audio: !this.withoutAudio
178
239
  };
@@ -726,11 +787,12 @@ class SaynaClient {
726
787
  if (!text || text.trim().length === 0) {
727
788
  throw new SaynaValidationError("Text cannot be empty");
728
789
  }
790
+ const resolved = await resolveConfigAuth(ttsConfig);
729
791
  return this.fetchFromSayna("speak", {
730
792
  method: "POST",
731
793
  body: JSON.stringify({
732
794
  text,
733
- tts_config: ttsConfig
795
+ tts_config: resolved
734
796
  })
735
797
  }, "arrayBuffer");
736
798
  }
@@ -1064,4 +1126,4 @@ async function saynaConnect(url, sttConfig, ttsConfig, livekitConfig, withoutAud
1064
1126
  return client;
1065
1127
  }
1066
1128
 
1067
- //# debugId=5E4C5FDAA012925764756E2164756E21
1129
+ //# debugId=CF87EB8AB646758764756E2164756E21