@loonylabs/tts-middleware 0.1.1

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.
Files changed (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +201 -0
  3. package/dist/index.d.ts +22 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +39 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/middleware/services/tts/index.d.ts +30 -0
  8. package/dist/middleware/services/tts/index.d.ts.map +1 -0
  9. package/dist/middleware/services/tts/index.js +69 -0
  10. package/dist/middleware/services/tts/index.js.map +1 -0
  11. package/dist/middleware/services/tts/providers/azure-provider.d.ts +131 -0
  12. package/dist/middleware/services/tts/providers/azure-provider.d.ts.map +1 -0
  13. package/dist/middleware/services/tts/providers/azure-provider.js +375 -0
  14. package/dist/middleware/services/tts/providers/azure-provider.js.map +1 -0
  15. package/dist/middleware/services/tts/providers/base-tts-provider.d.ts +204 -0
  16. package/dist/middleware/services/tts/providers/base-tts-provider.d.ts.map +1 -0
  17. package/dist/middleware/services/tts/providers/base-tts-provider.js +267 -0
  18. package/dist/middleware/services/tts/providers/base-tts-provider.js.map +1 -0
  19. package/dist/middleware/services/tts/providers/edenai-provider.d.ts +112 -0
  20. package/dist/middleware/services/tts/providers/edenai-provider.d.ts.map +1 -0
  21. package/dist/middleware/services/tts/providers/edenai-provider.js +289 -0
  22. package/dist/middleware/services/tts/providers/edenai-provider.js.map +1 -0
  23. package/dist/middleware/services/tts/providers/index.d.ts +9 -0
  24. package/dist/middleware/services/tts/providers/index.d.ts.map +1 -0
  25. package/dist/middleware/services/tts/providers/index.js +29 -0
  26. package/dist/middleware/services/tts/providers/index.js.map +1 -0
  27. package/dist/middleware/services/tts/tts.service.d.ts +175 -0
  28. package/dist/middleware/services/tts/tts.service.d.ts.map +1 -0
  29. package/dist/middleware/services/tts/tts.service.js +287 -0
  30. package/dist/middleware/services/tts/tts.service.js.map +1 -0
  31. package/dist/middleware/services/tts/types/common.types.d.ts +303 -0
  32. package/dist/middleware/services/tts/types/common.types.d.ts.map +1 -0
  33. package/dist/middleware/services/tts/types/common.types.js +42 -0
  34. package/dist/middleware/services/tts/types/common.types.js.map +1 -0
  35. package/dist/middleware/services/tts/types/index.d.ts +22 -0
  36. package/dist/middleware/services/tts/types/index.d.ts.map +1 -0
  37. package/dist/middleware/services/tts/types/index.js +46 -0
  38. package/dist/middleware/services/tts/types/index.js.map +1 -0
  39. package/dist/middleware/services/tts/types/provider-options.types.d.ts +414 -0
  40. package/dist/middleware/services/tts/types/provider-options.types.d.ts.map +1 -0
  41. package/dist/middleware/services/tts/types/provider-options.types.js +71 -0
  42. package/dist/middleware/services/tts/types/provider-options.types.js.map +1 -0
  43. package/dist/middleware/services/tts/utils/character-counter.utils.d.ts +160 -0
  44. package/dist/middleware/services/tts/utils/character-counter.utils.d.ts.map +1 -0
  45. package/dist/middleware/services/tts/utils/character-counter.utils.js +205 -0
  46. package/dist/middleware/services/tts/utils/character-counter.utils.js.map +1 -0
  47. package/dist/middleware/services/tts/utils/index.d.ts +9 -0
  48. package/dist/middleware/services/tts/utils/index.d.ts.map +1 -0
  49. package/dist/middleware/services/tts/utils/index.js +25 -0
  50. package/dist/middleware/services/tts/utils/index.js.map +1 -0
  51. package/dist/middleware/services/tts/utils/logger.utils.d.ts +116 -0
  52. package/dist/middleware/services/tts/utils/logger.utils.d.ts.map +1 -0
  53. package/dist/middleware/services/tts/utils/logger.utils.js +186 -0
  54. package/dist/middleware/services/tts/utils/logger.utils.js.map +1 -0
  55. package/dist/middleware/shared/config/tts.config.d.ts +147 -0
  56. package/dist/middleware/shared/config/tts.config.d.ts.map +1 -0
  57. package/dist/middleware/shared/config/tts.config.js +162 -0
  58. package/dist/middleware/shared/config/tts.config.js.map +1 -0
  59. package/package.json +94 -0
@@ -0,0 +1,205 @@
1
+ "use strict";
2
+ /**
3
+ * Character Counting Utilities
4
+ *
5
+ * @description Accurate character counting for billing purposes.
6
+ * This is billing-critical code - all functions must be 100% accurate
7
+ * and match provider billing methodologies.
8
+ *
9
+ * @critical This code directly impacts customer billing. Any bugs here
10
+ * could result in incorrect charges. Test coverage MUST be 100%.
11
+ */
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.countCharacters = countCharacters;
14
+ exports.countCharactersWithoutSSML = countCharactersWithoutSSML;
15
+ exports.validateCharacterCount = validateCharacterCount;
16
+ exports.countBillableCharacters = countBillableCharacters;
17
+ exports.estimateAudioDuration = estimateAudioDuration;
18
+ exports.formatCharacterCount = formatCharacterCount;
19
+ /**
20
+ * Count all characters in text for billing purposes
21
+ *
22
+ * @param text - The input text to count
23
+ * @returns The total number of characters including spaces, punctuation, and newlines
24
+ *
25
+ * @description
26
+ * This function counts ALL characters in the input text:
27
+ * - Letters (a-z, A-Z)
28
+ * - Numbers (0-9)
29
+ * - Spaces and tabs
30
+ * - Newlines (\n, \r\n)
31
+ * - Punctuation and special characters
32
+ * - Unicode characters (counted as 1 character each)
33
+ * - Emoji (counted as 1 character each, regardless of byte length)
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * countCharacters("Hello World!") // returns 12
38
+ * countCharacters("Guten Morgen") // returns 12
39
+ * countCharacters("Hello 👋") // returns 8
40
+ * countCharacters("") // returns 0
41
+ * ```
42
+ *
43
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
44
+ *
45
+ * @critical Billing-critical function. Must be 100% accurate.
46
+ */
47
+ function countCharacters(text) {
48
+ // JavaScript string length counts UTF-16 code units
49
+ // For most characters this equals 1, which matches provider billing
50
+ return text.length;
51
+ }
52
+ /**
53
+ * Count characters in text after removing SSML markup
54
+ *
55
+ * @param text - The input text (may contain SSML tags)
56
+ * @returns The number of characters excluding SSML tags
57
+ *
58
+ * @description
59
+ * This function removes all SSML (Speech Synthesis Markup Language) tags
60
+ * before counting characters. This is used when the middleware generates
61
+ * SSML internally but billing is based on the plain text content.
62
+ *
63
+ * SSML tags removed include:
64
+ * - `<speak>`, `</speak>` - Root element
65
+ * - `<voice>`, `</voice>` - Voice selection
66
+ * - `<prosody>`, `</prosody>` - Prosody (rate, pitch, volume)
67
+ * - `<emphasis>`, `</emphasis>` - Emphasis
68
+ * - `<break>` - Pauses
69
+ * - `<say-as>`, `</say-as>` - Interpret-as hints
70
+ * - `<phoneme>`, `</phoneme>` - Phonetic pronunciation
71
+ * - `<sub>`, `</sub>` - Substitution
72
+ * - `<lang>`, `</lang>` - Language switching
73
+ * - And all other SSML tags
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * countCharactersWithoutSSML("<speak>Hello</speak>")
78
+ * // returns 5 (only "Hello" is counted)
79
+ *
80
+ * countCharactersWithoutSSML('<voice name="en-US-Jenny">Text</voice>')
81
+ * // returns 4 (only "Text" is counted)
82
+ *
83
+ * countCharactersWithoutSSML('<prosody rate="slow">Hello World</prosody>')
84
+ * // returns 11 (only "Hello World" is counted)
85
+ * ```
86
+ *
87
+ * @critical Billing-critical function. Must match Azure's character counting.
88
+ */
89
+ function countCharactersWithoutSSML(text) {
90
+ // Remove all SSML tags (opening and closing tags, including self-closing)
91
+ // Pattern: <tag> or </tag> or <tag attr="value"> or <tag/>
92
+ const withoutSSML = text.replace(/<[^>]+>/g, '');
93
+ // Count the remaining characters
94
+ return withoutSSML.length;
95
+ }
96
+ /**
97
+ * Validate that character count matches expected range
98
+ *
99
+ * @param text - The input text
100
+ * @param minChars - Minimum expected characters (default: 0)
101
+ * @param maxChars - Maximum expected characters (default: Infinity)
102
+ * @returns True if count is within range
103
+ * @throws {RangeError} If character count is outside expected range
104
+ *
105
+ * @description
106
+ * This is a utility function to validate text length before synthesis.
107
+ * Providers often have limits on input text length (e.g., Azure: 3000 chars per request).
108
+ *
109
+ * @example
110
+ * ```typescript
111
+ * validateCharacterCount("Hello", 1, 100) // returns true
112
+ * validateCharacterCount("", 1, 100) // throws RangeError
113
+ * ```
114
+ */
115
+ function validateCharacterCount(text, minChars = 0, maxChars = Infinity) {
116
+ const count = countCharacters(text);
117
+ if (count < minChars) {
118
+ throw new RangeError(`Text must have at least ${minChars} characters (got ${count})`);
119
+ }
120
+ if (count > maxChars) {
121
+ throw new RangeError(`Text must have at most ${maxChars} characters (got ${count})`);
122
+ }
123
+ return true;
124
+ }
125
+ /**
126
+ * Count billable characters (alias for countCharacters)
127
+ *
128
+ * @param text - The input text
129
+ * @returns The number of billable characters
130
+ *
131
+ * @description
132
+ * Semantic alias for countCharacters to make billing calculations clearer.
133
+ * This is the function consumers should use when calculating costs.
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * const text = "Hello World!";
138
+ * const chars = countBillableCharacters(text); // 12
139
+ * const costUSD = (chars / 1_000_000) * 16; // $16 per 1M chars
140
+ * ```
141
+ */
142
+ function countBillableCharacters(text) {
143
+ return countCharacters(text);
144
+ }
145
+ /**
146
+ * Estimate audio duration based on character count
147
+ *
148
+ * @param text - The input text
149
+ * @param speedMultiplier - Speech speed (0.5 to 2.0, default: 1.0)
150
+ * @param charsPerSecond - Average characters spoken per second (default: 15)
151
+ * @returns Estimated duration in milliseconds
152
+ *
153
+ * @description
154
+ * Provides a rough estimate of audio duration based on character count.
155
+ * This is NOT exact and varies by:
156
+ * - Language (some languages are more verbose)
157
+ * - Voice (different voices have different pacing)
158
+ * - Content (technical terms take longer)
159
+ * - Speed setting
160
+ *
161
+ * Default assumption: ~15 characters per second at normal speed
162
+ * (roughly 150-180 words per minute, assuming 5-6 chars per word)
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * estimateAudioDuration("Hello World!", 1.0) // ~800ms
167
+ * estimateAudioDuration("Hello World!", 0.5) // ~1600ms (slower)
168
+ * ```
169
+ */
170
+ function estimateAudioDuration(text, speedMultiplier = 1.0, charsPerSecond = 15) {
171
+ const charCount = countCharacters(text);
172
+ // Calculate seconds at normal speed
173
+ const secondsAtNormalSpeed = charCount / charsPerSecond;
174
+ // Adjust for speed multiplier (higher speed = shorter duration)
175
+ const adjustedSeconds = secondsAtNormalSpeed / speedMultiplier;
176
+ // Convert to milliseconds
177
+ return Math.round(adjustedSeconds * 1000);
178
+ }
179
+ /**
180
+ * Format character count for display
181
+ *
182
+ * @param count - The character count
183
+ * @returns Formatted string (e.g., "1.2K chars", "1.5M chars")
184
+ *
185
+ * @description
186
+ * Formats large character counts in human-readable format.
187
+ * Useful for displaying usage statistics.
188
+ *
189
+ * @example
190
+ * ```typescript
191
+ * formatCharacterCount(500) // "500 chars"
192
+ * formatCharacterCount(1234) // "1.2K chars"
193
+ * formatCharacterCount(1500000) // "1.5M chars"
194
+ * ```
195
+ */
196
+ function formatCharacterCount(count) {
197
+ if (count < 1000) {
198
+ return `${count} chars`;
199
+ }
200
+ if (count < 1000000) {
201
+ return `${(count / 1000).toFixed(1)}K chars`;
202
+ }
203
+ return `${(count / 1000000).toFixed(1)}M chars`;
204
+ }
205
+ //# sourceMappingURL=character-counter.utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"character-counter.utils.js","sourceRoot":"","sources":["../../../../../src/middleware/services/tts/utils/character-counter.utils.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;AA8BH,0CAIC;AAuCD,gEAOC;AAqBD,wDAoBC;AAmBD,0DAEC;AA2BD,sDAeC;AAmBD,oDAUC;AAnND;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,SAAgB,eAAe,CAAC,IAAY;IAC1C,oDAAoD;IACpD,oEAAoE;IACpE,OAAO,IAAI,CAAC,MAAM,CAAC;AACrB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,SAAgB,0BAA0B,CAAC,IAAY;IACrD,0EAA0E;IAC1E,2DAA2D;IAC3D,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;IAEjD,iCAAiC;IACjC,OAAO,WAAW,CAAC,MAAM,CAAC;AAC5B,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH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,CAAC;IAED,OAAO,GAAG,CAAC,KAAK,GAAG,OAAS,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;AACpD,CAAC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * TTS Utility Functions
3
+ *
4
+ * Export all utility functions for TTS operations
5
+ */
6
+ export { countCharacters, countCharactersWithoutSSML, validateCharacterCount, countBillableCharacters, estimateAudioDuration, formatCharacterCount, } from './character-counter.utils';
7
+ export { setLogger, getLogger, resetLogger, setLogLevel, getLogLevel, silentLogger, log, } from './logger.utils';
8
+ export type { TTSLogger, LogLevel } from './logger.utils';
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/middleware/services/tts/utils/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,eAAe,EACf,0BAA0B,EAC1B,sBAAsB,EACtB,uBAAuB,EACvB,qBAAqB,EACrB,oBAAoB,GACrB,MAAM,2BAA2B,CAAC;AAGnC,OAAO,EACL,SAAS,EACT,SAAS,EACT,WAAW,EACX,WAAW,EACX,WAAW,EACX,YAAY,EACZ,GAAG,GACJ,MAAM,gBAAgB,CAAC;AAExB,YAAY,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ /**
3
+ * TTS Utility Functions
4
+ *
5
+ * Export all utility functions for TTS operations
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.log = exports.silentLogger = exports.getLogLevel = exports.setLogLevel = exports.resetLogger = exports.getLogger = exports.setLogger = exports.formatCharacterCount = exports.estimateAudioDuration = exports.countBillableCharacters = exports.validateCharacterCount = exports.countCharactersWithoutSSML = exports.countCharacters = void 0;
9
+ var character_counter_utils_1 = require("./character-counter.utils");
10
+ Object.defineProperty(exports, "countCharacters", { enumerable: true, get: function () { return character_counter_utils_1.countCharacters; } });
11
+ Object.defineProperty(exports, "countCharactersWithoutSSML", { enumerable: true, get: function () { return character_counter_utils_1.countCharactersWithoutSSML; } });
12
+ Object.defineProperty(exports, "validateCharacterCount", { enumerable: true, get: function () { return character_counter_utils_1.validateCharacterCount; } });
13
+ Object.defineProperty(exports, "countBillableCharacters", { enumerable: true, get: function () { return character_counter_utils_1.countBillableCharacters; } });
14
+ Object.defineProperty(exports, "estimateAudioDuration", { enumerable: true, get: function () { return character_counter_utils_1.estimateAudioDuration; } });
15
+ Object.defineProperty(exports, "formatCharacterCount", { enumerable: true, get: function () { return character_counter_utils_1.formatCharacterCount; } });
16
+ // Logger utilities
17
+ var logger_utils_1 = require("./logger.utils");
18
+ Object.defineProperty(exports, "setLogger", { enumerable: true, get: function () { return logger_utils_1.setLogger; } });
19
+ Object.defineProperty(exports, "getLogger", { enumerable: true, get: function () { return logger_utils_1.getLogger; } });
20
+ Object.defineProperty(exports, "resetLogger", { enumerable: true, get: function () { return logger_utils_1.resetLogger; } });
21
+ Object.defineProperty(exports, "setLogLevel", { enumerable: true, get: function () { return logger_utils_1.setLogLevel; } });
22
+ Object.defineProperty(exports, "getLogLevel", { enumerable: true, get: function () { return logger_utils_1.getLogLevel; } });
23
+ Object.defineProperty(exports, "silentLogger", { enumerable: true, get: function () { return logger_utils_1.silentLogger; } });
24
+ Object.defineProperty(exports, "log", { enumerable: true, get: function () { return logger_utils_1.log; } });
25
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/middleware/services/tts/utils/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAEH,qEAOmC;AANjC,0HAAA,eAAe,OAAA;AACf,qIAAA,0BAA0B,OAAA;AAC1B,iIAAA,sBAAsB,OAAA;AACtB,kIAAA,uBAAuB,OAAA;AACvB,gIAAA,qBAAqB,OAAA;AACrB,+HAAA,oBAAoB,OAAA;AAGtB,mBAAmB;AACnB,+CAQwB;AAPtB,yGAAA,SAAS,OAAA;AACT,yGAAA,SAAS,OAAA;AACT,2GAAA,WAAW,OAAA;AACX,2GAAA,WAAW,OAAA;AACX,2GAAA,WAAW,OAAA;AACX,4GAAA,YAAY,OAAA;AACZ,mGAAA,GAAG,OAAA"}
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Pluggable Logger Interface for TTS Middleware
3
+ *
4
+ * @description Allows customization of logging behavior.
5
+ * By default, uses console logging, but can be replaced
6
+ * with any logger implementation (Winston, Pino, etc.)
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * // Use custom logger
11
+ * import { setLogger } from '@loonylabs/tts-middleware';
12
+ *
13
+ * setLogger({
14
+ * info: (msg, meta) => winston.info(msg, meta),
15
+ * warn: (msg, meta) => winston.warn(msg, meta),
16
+ * error: (msg, meta) => winston.error(msg, meta),
17
+ * debug: (msg, meta) => winston.debug(msg, meta),
18
+ * });
19
+ * ```
20
+ */
21
+ /**
22
+ * Log level type
23
+ */
24
+ export type LogLevel = 'info' | 'warn' | 'error' | 'debug';
25
+ /**
26
+ * Logger interface that can be implemented by any logging library
27
+ */
28
+ export interface TTSLogger {
29
+ /**
30
+ * Log an info message
31
+ */
32
+ info(message: string, meta?: Record<string, unknown>): void;
33
+ /**
34
+ * Log a warning message
35
+ */
36
+ warn(message: string, meta?: Record<string, unknown>): void;
37
+ /**
38
+ * Log an error message
39
+ */
40
+ error(message: string, meta?: Record<string, unknown>): void;
41
+ /**
42
+ * Log a debug message
43
+ */
44
+ debug(message: string, meta?: Record<string, unknown>): void;
45
+ }
46
+ /**
47
+ * Silent logger that discards all messages
48
+ * Useful for testing or when logging should be disabled
49
+ */
50
+ export declare const silentLogger: TTSLogger;
51
+ /**
52
+ * Set the logger implementation
53
+ *
54
+ * @param logger - The logger implementation to use
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * import { setLogger, silentLogger } from '@loonylabs/tts-middleware';
59
+ *
60
+ * // Disable logging
61
+ * setLogger(silentLogger);
62
+ *
63
+ * // Use custom logger
64
+ * setLogger({
65
+ * info: (msg, meta) => myLogger.info(msg, meta),
66
+ * warn: (msg, meta) => myLogger.warn(msg, meta),
67
+ * error: (msg, meta) => myLogger.error(msg, meta),
68
+ * debug: (msg, meta) => myLogger.debug(msg, meta),
69
+ * });
70
+ * ```
71
+ */
72
+ export declare function setLogger(logger: TTSLogger): void;
73
+ /**
74
+ * Get the current logger implementation
75
+ *
76
+ * @returns The current logger
77
+ */
78
+ export declare function getLogger(): TTSLogger;
79
+ /**
80
+ * Reset logger to default console logger
81
+ */
82
+ export declare function resetLogger(): void;
83
+ /**
84
+ * Set the minimum log level
85
+ *
86
+ * @param level - The minimum log level to display
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * import { setLogLevel } from '@loonylabs/tts-middleware';
91
+ *
92
+ * // Only show warnings and errors
93
+ * setLogLevel('warn');
94
+ *
95
+ * // Show all messages including debug
96
+ * setLogLevel('debug');
97
+ * ```
98
+ */
99
+ export declare function setLogLevel(level: LogLevel): void;
100
+ /**
101
+ * Get the current log level
102
+ *
103
+ * @returns The current minimum log level
104
+ */
105
+ export declare function getLogLevel(): LogLevel;
106
+ /**
107
+ * Internal logging function used by TTS components
108
+ *
109
+ * @internal
110
+ * @param provider - Provider name for log prefix
111
+ * @param level - Log level
112
+ * @param message - Log message
113
+ * @param meta - Optional metadata
114
+ */
115
+ export declare function log(provider: string, level: LogLevel, message: string, meta?: Record<string, unknown>): void;
116
+ //# sourceMappingURL=logger.utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.utils.d.ts","sourceRoot":"","sources":["../../../../../src/middleware/services/tts/utils/logger.utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,CAAC;AAE3D;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB;;OAEG;IACH,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IAE5D;;OAEG;IACH,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IAE5D;;OAEG;IACH,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IAE7D;;OAEG;IACH,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;CAC9D;AAoCD;;;GAGG;AACH,eAAO,MAAM,YAAY,EAAE,SAK1B,CAAC;AAsBF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAEjD;AAED;;;;GAIG;AACH,wBAAgB,SAAS,IAAI,SAAS,CAErC;AAED;;GAEG;AACH,wBAAgB,WAAW,IAAI,IAAI,CAElC;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI,CAEjD;AAED;;;;GAIG;AACH,wBAAgB,WAAW,IAAI,QAAQ,CAEtC;AAYD;;;;;;;;GAQG;AACH,wBAAgB,GAAG,CACjB,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,QAAQ,EACf,OAAO,EAAE,MAAM,EACf,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,IAAI,CAUN"}
@@ -0,0 +1,186 @@
1
+ "use strict";
2
+ /**
3
+ * Pluggable Logger Interface for TTS Middleware
4
+ *
5
+ * @description Allows customization of logging behavior.
6
+ * By default, uses console logging, but can be replaced
7
+ * with any logger implementation (Winston, Pino, etc.)
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * // Use custom logger
12
+ * import { setLogger } from '@loonylabs/tts-middleware';
13
+ *
14
+ * setLogger({
15
+ * info: (msg, meta) => winston.info(msg, meta),
16
+ * warn: (msg, meta) => winston.warn(msg, meta),
17
+ * error: (msg, meta) => winston.error(msg, meta),
18
+ * debug: (msg, meta) => winston.debug(msg, meta),
19
+ * });
20
+ * ```
21
+ */
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.silentLogger = void 0;
24
+ exports.setLogger = setLogger;
25
+ exports.getLogger = getLogger;
26
+ exports.resetLogger = resetLogger;
27
+ exports.setLogLevel = setLogLevel;
28
+ exports.getLogLevel = getLogLevel;
29
+ exports.log = log;
30
+ /**
31
+ * Default console logger implementation
32
+ */
33
+ const consoleLogger = {
34
+ info: (message, meta) => {
35
+ if (meta) {
36
+ console.info(message, meta);
37
+ }
38
+ else {
39
+ console.info(message);
40
+ }
41
+ },
42
+ warn: (message, meta) => {
43
+ if (meta) {
44
+ console.warn(message, meta);
45
+ }
46
+ else {
47
+ console.warn(message);
48
+ }
49
+ },
50
+ error: (message, meta) => {
51
+ if (meta) {
52
+ console.error(message, meta);
53
+ }
54
+ else {
55
+ console.error(message);
56
+ }
57
+ },
58
+ debug: (message, meta) => {
59
+ if (meta) {
60
+ console.debug(message, meta);
61
+ }
62
+ else {
63
+ console.debug(message);
64
+ }
65
+ },
66
+ };
67
+ /**
68
+ * Silent logger that discards all messages
69
+ * Useful for testing or when logging should be disabled
70
+ */
71
+ exports.silentLogger = {
72
+ info: () => { },
73
+ warn: () => { },
74
+ error: () => { },
75
+ debug: () => { },
76
+ };
77
+ /**
78
+ * Current logger instance
79
+ */
80
+ let currentLogger = consoleLogger;
81
+ /**
82
+ * Current log level (messages below this level are discarded)
83
+ */
84
+ let currentLogLevel = 'info';
85
+ /**
86
+ * Log level priority (higher = more important)
87
+ */
88
+ const LOG_LEVEL_PRIORITY = {
89
+ debug: 0,
90
+ info: 1,
91
+ warn: 2,
92
+ error: 3,
93
+ };
94
+ /**
95
+ * Set the logger implementation
96
+ *
97
+ * @param logger - The logger implementation to use
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * import { setLogger, silentLogger } from '@loonylabs/tts-middleware';
102
+ *
103
+ * // Disable logging
104
+ * setLogger(silentLogger);
105
+ *
106
+ * // Use custom logger
107
+ * setLogger({
108
+ * info: (msg, meta) => myLogger.info(msg, meta),
109
+ * warn: (msg, meta) => myLogger.warn(msg, meta),
110
+ * error: (msg, meta) => myLogger.error(msg, meta),
111
+ * debug: (msg, meta) => myLogger.debug(msg, meta),
112
+ * });
113
+ * ```
114
+ */
115
+ function setLogger(logger) {
116
+ currentLogger = logger;
117
+ }
118
+ /**
119
+ * Get the current logger implementation
120
+ *
121
+ * @returns The current logger
122
+ */
123
+ function getLogger() {
124
+ return currentLogger;
125
+ }
126
+ /**
127
+ * Reset logger to default console logger
128
+ */
129
+ function resetLogger() {
130
+ currentLogger = consoleLogger;
131
+ }
132
+ /**
133
+ * Set the minimum log level
134
+ *
135
+ * @param level - The minimum log level to display
136
+ *
137
+ * @example
138
+ * ```typescript
139
+ * import { setLogLevel } from '@loonylabs/tts-middleware';
140
+ *
141
+ * // Only show warnings and errors
142
+ * setLogLevel('warn');
143
+ *
144
+ * // Show all messages including debug
145
+ * setLogLevel('debug');
146
+ * ```
147
+ */
148
+ function setLogLevel(level) {
149
+ currentLogLevel = level;
150
+ }
151
+ /**
152
+ * Get the current log level
153
+ *
154
+ * @returns The current minimum log level
155
+ */
156
+ function getLogLevel() {
157
+ return currentLogLevel;
158
+ }
159
+ /**
160
+ * Check if a log level should be displayed
161
+ *
162
+ * @param level - The level to check
163
+ * @returns True if the level should be displayed
164
+ */
165
+ function shouldLog(level) {
166
+ return LOG_LEVEL_PRIORITY[level] >= LOG_LEVEL_PRIORITY[currentLogLevel];
167
+ }
168
+ /**
169
+ * Internal logging function used by TTS components
170
+ *
171
+ * @internal
172
+ * @param provider - Provider name for log prefix
173
+ * @param level - Log level
174
+ * @param message - Log message
175
+ * @param meta - Optional metadata
176
+ */
177
+ function log(provider, level, message, meta) {
178
+ if (!shouldLog(level)) {
179
+ return;
180
+ }
181
+ const timestamp = new Date().toISOString();
182
+ const prefix = `[${timestamp}] [${provider.toUpperCase()}] [${level.toUpperCase()}]`;
183
+ const formattedMessage = `${prefix} ${message}`;
184
+ currentLogger[level](formattedMessage, meta);
185
+ }
186
+ //# sourceMappingURL=logger.utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.utils.js","sourceRoot":"","sources":["../../../../../src/middleware/services/tts/utils/logger.utils.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;GAmBG;;;AAsHH,8BAEC;AAOD,8BAEC;AAKD,kCAEC;AAkBD,kCAEC;AAOD,kCAEC;AAqBD,kBAeC;AAzKD;;GAEG;AACH,MAAM,aAAa,GAAc;IAC/B,IAAI,EAAE,CAAC,OAAe,EAAE,IAA8B,EAAE,EAAE;QACxD,IAAI,IAAI,EAAE,CAAC;YACT,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAC9B,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACxB,CAAC;IACH,CAAC;IACD,IAAI,EAAE,CAAC,OAAe,EAAE,IAA8B,EAAE,EAAE;QACxD,IAAI,IAAI,EAAE,CAAC;YACT,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAC9B,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACxB,CAAC;IACH,CAAC;IACD,KAAK,EAAE,CAAC,OAAe,EAAE,IAA8B,EAAE,EAAE;QACzD,IAAI,IAAI,EAAE,CAAC;YACT,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAC/B,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,KA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