@zodic/shared 0.0.185 → 0.0.187
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.
- package/app/services/ConceptService.ts +37 -32
- package/package.json +1 -1
- package/types/scopes/cloudflare.ts +10 -0
|
@@ -39,9 +39,39 @@ export class ConceptService {
|
|
|
39
39
|
const id = this.context.env.CONCEPT_NAMES_DO.idFromName(conceptSlug);
|
|
40
40
|
const stub = this.context.env.CONCEPT_NAMES_DO.get(id);
|
|
41
41
|
|
|
42
|
+
console.log(`📡 Fetching existing KV data for ${conceptSlug}...`);
|
|
43
|
+
const existingEN = await this.getKVConcept(kvKeyEN);
|
|
44
|
+
const existingPT = await this.getKVConcept(kvKeyPT);
|
|
45
|
+
|
|
46
|
+
// ✅ IMMEDIATE SKIP if already exists and override is false
|
|
47
|
+
if (!override && existingEN.name && existingPT.name) {
|
|
48
|
+
console.log(`⚡ Basic info already exists for ${conceptSlug}, skipping.`);
|
|
49
|
+
|
|
50
|
+
// ✅ **Backfill Durable Object** to register the names
|
|
51
|
+
console.log(`📡 Backfilling Durable Object for ${conceptSlug}...`);
|
|
52
|
+
try {
|
|
53
|
+
await Promise.all([
|
|
54
|
+
stub.fetch(`https://internal/add-name`, {
|
|
55
|
+
method: 'POST',
|
|
56
|
+
body: JSON.stringify({ language: 'en-us', name: existingEN.name }),
|
|
57
|
+
headers: { 'Content-Type': 'application/json' },
|
|
58
|
+
}),
|
|
59
|
+
stub.fetch(`https://internal/add-name`, {
|
|
60
|
+
method: 'POST',
|
|
61
|
+
body: JSON.stringify({ language: 'pt-br', name: existingPT.name }),
|
|
62
|
+
headers: { 'Content-Type': 'application/json' },
|
|
63
|
+
}),
|
|
64
|
+
]);
|
|
65
|
+
console.log(`✅ Successfully backfilled Durable Object for ${conceptSlug}`);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.error(`❌ Error backfilling Durable Object:`, error);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return; // ✅ Exit early
|
|
71
|
+
}
|
|
72
|
+
|
|
42
73
|
let attempts = 0;
|
|
43
74
|
const maxAttempts = 3;
|
|
44
|
-
|
|
45
75
|
while (attempts < maxAttempts) {
|
|
46
76
|
let phase = 'generation';
|
|
47
77
|
try {
|
|
@@ -63,37 +93,6 @@ export class ConceptService {
|
|
|
63
93
|
console.log(`❌ Error fetching names from DO for ${conceptSlug}: ${await response.text()}`);
|
|
64
94
|
}
|
|
65
95
|
|
|
66
|
-
// ✅ Fetch existing KV data to backfill Durable Object if necessary
|
|
67
|
-
console.log(`📡 Fetching existing KV data for ${conceptSlug}...`);
|
|
68
|
-
const existingEN = await this.getKVConcept(kvKeyEN);
|
|
69
|
-
const existingPT = await this.getKVConcept(kvKeyPT);
|
|
70
|
-
|
|
71
|
-
console.log(`📝 Existing EN name: ${existingEN.name || 'None'}, PT name: ${existingPT.name || 'None'}`);
|
|
72
|
-
|
|
73
|
-
// 🔥 If names exist in KV but not in DO, add them
|
|
74
|
-
if (existingEN.name && !allNamesEN.includes(existingEN.name)) {
|
|
75
|
-
console.log(`⚡ Backfilling existing EN name to DO: ${existingEN.name}`);
|
|
76
|
-
await stub.fetch(`https://internal/add-name`, {
|
|
77
|
-
method: 'POST',
|
|
78
|
-
body: JSON.stringify({ language: 'en-us', name: existingEN.name }),
|
|
79
|
-
headers: { 'Content-Type': 'application/json' },
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
if (existingPT.name && !allNamesPT.includes(existingPT.name)) {
|
|
83
|
-
console.log(`⚡ Backfilling existing PT name to DO: ${existingPT.name}`);
|
|
84
|
-
await stub.fetch(`https://internal/add-name`, {
|
|
85
|
-
method: 'POST',
|
|
86
|
-
body: JSON.stringify({ language: 'pt-br', name: existingPT.name }),
|
|
87
|
-
headers: { 'Content-Type': 'application/json' },
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// ✅ Skip generation if basic info already exists and override is false
|
|
92
|
-
if (!override && existingEN.name && existingPT.name) {
|
|
93
|
-
console.log(`⚡ Basic info already exists for ${conceptSlug}, skipping.`);
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
96
|
console.log(`✏️ Generating new name...`);
|
|
98
97
|
const recentNamesEN = allNamesEN.slice(-60);
|
|
99
98
|
const messages = this.context
|
|
@@ -159,6 +158,12 @@ export class ConceptService {
|
|
|
159
158
|
await kvStore.put(kvKeyPT, JSON.stringify(existingPT));
|
|
160
159
|
|
|
161
160
|
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
|
+
|
|
162
167
|
return;
|
|
163
168
|
} catch (error) {
|
|
164
169
|
console.error(`❌ Attempt ${attempts} failed at phase: ${phase}`, (error as Error).message);
|
package/package.json
CHANGED
|
@@ -27,6 +27,11 @@ export type CentralBindings = {
|
|
|
27
27
|
CONCEPT_GENERATION_QUEUE: Queue;
|
|
28
28
|
CONCEPT_NAMES_DO: DurableObjectNamespace;
|
|
29
29
|
CROWN_QUEUE: Queue;
|
|
30
|
+
SCEPTER_QUEUE: Queue;
|
|
31
|
+
RING_QUEUE: Queue;
|
|
32
|
+
AMULET_QUEUE: Queue;
|
|
33
|
+
LANTERN_QUEUE: Queue;
|
|
34
|
+
ORB_QUEUE: Queue;
|
|
30
35
|
|
|
31
36
|
PROMPT_IMAGE_DESCRIBER: string;
|
|
32
37
|
PROMPT_GENERATE_ARCHETYPE_BASIC_INFO: string;
|
|
@@ -118,6 +123,11 @@ export type BackendBindings = Env &
|
|
|
118
123
|
| 'CONCEPT_NAMES_DO'
|
|
119
124
|
| 'API_BASE_URL'
|
|
120
125
|
| 'CROWN_QUEUE'
|
|
126
|
+
| 'AMULET_QUEUE'
|
|
127
|
+
| 'SCEPTER_QUEUE'
|
|
128
|
+
| 'RING_QUEUE'
|
|
129
|
+
| 'LANTERN_QUEUE'
|
|
130
|
+
| 'ORB_QUEUE'
|
|
121
131
|
>;
|
|
122
132
|
|
|
123
133
|
export type BackendCtx = Context<
|