@mulingai-npm/redis 3.40.3 → 3.40.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.
@@ -6,18 +6,33 @@
6
6
  * to improve translation quality and transcription correction.
7
7
  *
8
8
  * Redis key: smarttranslate:context-enhancer:{roomId}
9
- * Type: String (raw text, up to 15,000 chars)
10
- * TTL: 36 hours (covers "prep day before" use case)
9
+ * Type: String (JSON: { text, savedAt })
10
+ * TTL: environment-dependent (15min localhost, 30min development, 36h production)
11
11
  */
12
12
  import { RedisClient } from '../redis-client';
13
+ export interface ContextEnhancerResult {
14
+ text: string;
15
+ savedAt: number;
16
+ expiresAt: number;
17
+ ttlSeconds: number;
18
+ }
13
19
  export declare class ContextEnhancerManager {
14
20
  private redisClient;
15
- constructor(redisClient: RedisClient);
21
+ private ttlSeconds;
22
+ constructor(redisClient: RedisClient, environment?: string);
16
23
  private key;
17
- /** Save context text for a room. Truncates to MAX_CHARS. */
24
+ /** Get TTL configured for this environment (in seconds) */
25
+ getTtlSeconds(): number;
26
+ /** Check cooldown. Returns remaining seconds (0 = no cooldown). */
27
+ getCooldownRemaining(roomId: string | number): Promise<number>;
28
+ /** Save context text for a room. Truncates to MAX_CHARS. Call getCooldownRemaining() first. */
18
29
  saveContext(roomId: string | number, text: string): Promise<void>;
19
- /** Get context text for a room. Returns null if expired or not set. */
20
- getContext(roomId: string | number): Promise<string | null>;
30
+ /** Get raw context data (internal). */
31
+ private getContextData;
32
+ /** Get context with metadata (text, savedAt, expiresAt, ttlSeconds). */
33
+ getContext(roomId: string | number): Promise<ContextEnhancerResult | null>;
34
+ /** Get just the text (for pipeline injection, no metadata overhead). */
35
+ getContextText(roomId: string | number): Promise<string | null>;
21
36
  /** Delete context text for a room. */
22
37
  deleteContext(roomId: string | number): Promise<void>;
23
38
  /** Check if context text exists for a room (not expired). */
@@ -7,30 +7,85 @@
7
7
  * to improve translation quality and transcription correction.
8
8
  *
9
9
  * Redis key: smarttranslate:context-enhancer:{roomId}
10
- * Type: String (raw text, up to 15,000 chars)
11
- * TTL: 36 hours (covers "prep day before" use case)
10
+ * Type: String (JSON: { text, savedAt })
11
+ * TTL: environment-dependent (15min localhost, 30min development, 36h production)
12
12
  */
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.ContextEnhancerManager = void 0;
15
- const CONTEXT_ENHANCER_TTL = 36 * 60 * 60; // 36 hours
16
15
  const MAX_CHARS = 15000;
16
+ const COOLDOWN_SECONDS = 60; // 1 minute cooldown between saves
17
+ // TTL by environment (seconds)
18
+ const TTL_BY_ENV = {
19
+ localhost: 15 * 60,
20
+ development: 30 * 60,
21
+ production: 36 * 60 * 60 // 36 hours
22
+ };
17
23
  class ContextEnhancerManager {
18
- constructor(redisClient) {
24
+ constructor(redisClient, environment) {
19
25
  this.redisClient = redisClient;
26
+ this.ttlSeconds = TTL_BY_ENV[environment || 'production'] || TTL_BY_ENV.production;
20
27
  }
21
28
  key(roomId) {
22
29
  return `smarttranslate:context-enhancer:${roomId}`;
23
30
  }
24
- /** Save context text for a room. Truncates to MAX_CHARS. */
31
+ /** Get TTL configured for this environment (in seconds) */
32
+ getTtlSeconds() {
33
+ return this.ttlSeconds;
34
+ }
35
+ /** Check cooldown. Returns remaining seconds (0 = no cooldown). */
36
+ async getCooldownRemaining(roomId) {
37
+ const existing = await this.getContextData(roomId);
38
+ if (!existing)
39
+ return 0;
40
+ const elapsedSeconds = (Date.now() - existing.savedAt) / 1000;
41
+ if (elapsedSeconds < COOLDOWN_SECONDS) {
42
+ return Math.ceil(COOLDOWN_SECONDS - elapsedSeconds);
43
+ }
44
+ return 0;
45
+ }
46
+ /** Save context text for a room. Truncates to MAX_CHARS. Call getCooldownRemaining() first. */
25
47
  async saveContext(roomId, text) {
26
48
  const truncated = text.slice(0, MAX_CHARS);
49
+ const data = {
50
+ text: truncated,
51
+ savedAt: Date.now()
52
+ };
27
53
  const key = this.key(roomId);
28
- await this.redisClient.set(key, truncated);
29
- await this.redisClient.expire(key, CONTEXT_ENHANCER_TTL);
54
+ await this.redisClient.set(key, JSON.stringify(data));
55
+ await this.redisClient.expire(key, this.ttlSeconds);
30
56
  }
31
- /** Get context text for a room. Returns null if expired or not set. */
57
+ /** Get raw context data (internal). */
58
+ async getContextData(roomId) {
59
+ const raw = await this.redisClient.get(this.key(roomId));
60
+ if (!raw)
61
+ return null;
62
+ try {
63
+ return JSON.parse(raw);
64
+ }
65
+ catch {
66
+ // Legacy plain text format (before JSON migration)
67
+ return { text: raw, savedAt: Date.now() };
68
+ }
69
+ }
70
+ /** Get context with metadata (text, savedAt, expiresAt, ttlSeconds). */
32
71
  async getContext(roomId) {
33
- return this.redisClient.get(this.key(roomId));
72
+ const data = await this.getContextData(roomId);
73
+ if (!data)
74
+ return null;
75
+ const remainingTtl = await this.redisClient.ttl(this.key(roomId));
76
+ if (remainingTtl < 0)
77
+ return null; // expired or no TTL
78
+ return {
79
+ text: data.text,
80
+ savedAt: data.savedAt,
81
+ expiresAt: Date.now() + (remainingTtl * 1000),
82
+ ttlSeconds: remainingTtl
83
+ };
84
+ }
85
+ /** Get just the text (for pipeline injection, no metadata overhead). */
86
+ async getContextText(roomId) {
87
+ const data = await this.getContextData(roomId);
88
+ return (data === null || data === void 0 ? void 0 : data.text) || null;
34
89
  }
35
90
  /** Delete context text for a room. */
36
91
  async deleteContext(roomId) {
@@ -14,6 +14,8 @@ export declare class RedisClient {
14
14
  del(key: string): Promise<number>;
15
15
  exists(key: string): Promise<number>;
16
16
  expire(key: string, seconds: number): Promise<number>;
17
+ /** Get remaining TTL in seconds (-1 = no expiry, -2 = key doesn't exist) */
18
+ ttl(key: string): Promise<number>;
17
19
  sadd(key: string, ...values: string[]): Promise<number>;
18
20
  srem(key: string, ...values: string[]): Promise<number>;
19
21
  smembers(key: string): Promise<string[]>;
@@ -37,6 +37,10 @@ class RedisClient {
37
37
  async expire(key, seconds) {
38
38
  return this.client.expire(key, seconds);
39
39
  }
40
+ /** Get remaining TTL in seconds (-1 = no expiry, -2 = key doesn't exist) */
41
+ async ttl(key) {
42
+ return this.client.ttl(key);
43
+ }
40
44
  async sadd(key, ...values) {
41
45
  return this.client.sadd(key, values);
42
46
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mulingai-npm/redis",
3
- "version": "3.40.3",
3
+ "version": "3.40.5",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": {