@zodic/shared 0.0.192 → 0.0.194

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.
@@ -72,6 +72,8 @@ export class ConceptService {
72
72
 
73
73
  let attempts = 0;
74
74
  const maxAttempts = 3;
75
+ let newlyGeneratedNames: string[] = []; // ✅ Track generated names in memory
76
+
75
77
  while (attempts < maxAttempts) {
76
78
  let phase = 'generation';
77
79
  try {
@@ -94,7 +96,9 @@ export class ConceptService {
94
96
  }
95
97
 
96
98
  console.log(`✏️ Generating new name...`);
97
- const recentNamesEN = allNamesEN.slice(-40);
99
+ // Merge newly generated names with existing recent names
100
+ const recentNamesEN = [...allNamesEN.slice(-40), ...newlyGeneratedNames];
101
+
98
102
  const messages = this.context
99
103
  .buildLLMMessages()
100
104
  .generateConceptBasicInfo({
@@ -117,11 +121,64 @@ export class ConceptService {
117
121
 
118
122
  console.log(`🎭 Generated names: EN - "${nameEN}", PT - "${namePT}"`);
119
123
 
124
+ // ✅ **Add the generated names to our local tracking list**
125
+ newlyGeneratedNames.push(nameEN, namePT);
126
+
120
127
  // ✅ Check uniqueness before storing
121
128
  if (allNamesEN.includes(nameEN) || allNamesPT.includes(namePT)) {
122
129
  console.warn(`⚠️ Duplicate Name Detected: "${nameEN}" or "${namePT}"`);
123
- if (attempts >= maxAttempts)
124
- throw new Error(`🚨 Could not generate a unique name`);
130
+
131
+ // **On third attempt, store the name anyway & register in kvFailures**
132
+ if (attempts >= maxAttempts) {
133
+ console.log(`🚨 Max attempts reached. Storing name despite duplicate.`);
134
+
135
+ // ✅ **Register duplicate storage in KV failures**
136
+ await kvFailuresStore.put(
137
+ `failures:duplicates:${conceptSlug}:${combinationString}`,
138
+ JSON.stringify({
139
+ nameEN,
140
+ namePT,
141
+ attempts,
142
+ conceptSlug,
143
+ combinationString,
144
+ timestamp: new Date().toISOString(),
145
+ })
146
+ );
147
+
148
+ // ✅ **Store names in Durable Object**
149
+ await stub.fetch(`https://internal/add-name`, {
150
+ method: 'POST',
151
+ body: JSON.stringify({ language: 'en-us', name: nameEN }),
152
+ headers: { 'Content-Type': 'application/json' },
153
+ });
154
+
155
+ await stub.fetch(`https://internal/add-name`, {
156
+ method: 'POST',
157
+ body: JSON.stringify({ language: 'pt-br', name: namePT }),
158
+ headers: { 'Content-Type': 'application/json' },
159
+ });
160
+
161
+ // ✅ Store the generated basic info in KV
162
+ Object.assign(existingEN, {
163
+ name: nameEN,
164
+ description: descriptionEN,
165
+ poem: poemEN,
166
+ status: 'forced', // 🔥 Mark as "forced" due to duplication
167
+ });
168
+ await kvStore.put(kvKeyEN, JSON.stringify(existingEN));
169
+
170
+ Object.assign(existingPT, {
171
+ name: namePT,
172
+ description: descriptionPT,
173
+ poem: poemPT,
174
+ status: 'forced', // 🔥 Mark as "forced" due to duplication
175
+ });
176
+ await kvStore.put(kvKeyPT, JSON.stringify(existingPT));
177
+
178
+ console.log(`✅ Stored duplicate name after max retries: ${nameEN}, ${namePT}`);
179
+ return;
180
+ }
181
+
125
182
  console.log('🔁 Retrying due to duplicate name...');
126
183
  continue;
127
184
  }
@@ -158,35 +215,9 @@ export class ConceptService {
158
215
  await kvStore.put(kvKeyPT, JSON.stringify(existingPT));
159
216
 
160
217
  console.log(`✅ Stored basic info for ${conceptSlug}, combination: ${combinationString}.`);
161
-
162
- // ✅ **Throttling for next request**
163
- const delayMs = Math.floor(Math.random() * (500 - 200 + 1)) + 200; // 🔥 Random delay between 200-500ms
164
- console.log(`⏳ Waiting ${delayMs}ms before processing next missing entry...`);
165
- await new Promise((resolve) => setTimeout(resolve, delayMs));
166
-
167
218
  return;
168
219
  } catch (error) {
169
220
  console.error(`❌ Attempt ${attempts} failed at phase: ${phase}`, (error as Error).message);
170
-
171
- // ✅ Store failure details in KV for debugging
172
- await kvFailuresStore.put(
173
- failureKey,
174
- JSON.stringify({
175
- error: (error as Error).message,
176
- attempt: attempts,
177
- phase,
178
- conceptSlug,
179
- combinationString,
180
- timestamp: new Date().toISOString(),
181
- })
182
- );
183
-
184
- if (attempts >= maxAttempts) {
185
- console.error(`🚨 All ${maxAttempts} attempts failed.`);
186
- throw new Error(`Failed to generate basic info after ${maxAttempts} attempts.`);
187
- }
188
- console.log('🔁 Retrying...');
189
- await new Promise((resolve) => setTimeout(resolve, 300));
190
221
  }
191
222
  }
192
223
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zodic/shared",
3
- "version": "0.0.192",
3
+ "version": "0.0.194",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "publishConfig": {