@zodic/shared 0.0.174 → 0.0.175
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.
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { DurableObjectState } from '@cloudflare/workers-types';
|
|
2
2
|
|
|
3
|
-
export class ConceptNameDO {
|
|
3
|
+
export class ConceptNameDO extends DurableObject {
|
|
4
4
|
state: DurableObjectState;
|
|
5
5
|
env: any;
|
|
6
6
|
names: { 'en-us': string[]; 'pt-br': string[] };
|
|
7
7
|
|
|
8
8
|
constructor(state: DurableObjectState, env: any) {
|
|
9
|
+
super(state, env); // ✅ Call super() for DurableObject
|
|
9
10
|
this.state = state;
|
|
10
11
|
this.env = env;
|
|
11
12
|
this.names = { 'en-us': [], 'pt-br': [] };
|
|
@@ -23,18 +24,22 @@ export class ConceptNameDO {
|
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
if (method === 'POST' && url.pathname === '/add-name') {
|
|
26
|
-
|
|
27
|
-
language
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
27
|
+
try {
|
|
28
|
+
const { language, name } = (await request.json()) as {
|
|
29
|
+
language: 'en-us' | 'pt-br';
|
|
30
|
+
name: string;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
if (!['en-us', 'pt-br'].includes(language)) {
|
|
34
|
+
return new Response('Invalid language', { status: 400 });
|
|
35
|
+
}
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
await this.addName(language, name);
|
|
38
|
+
return new Response('OK', { status: 200 });
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.error('❌ Error processing request:', error);
|
|
41
|
+
return new Response('Bad Request', { status: 400 });
|
|
42
|
+
}
|
|
38
43
|
}
|
|
39
44
|
|
|
40
45
|
return new Response('Not Found', { status: 404 });
|
|
@@ -45,14 +50,19 @@ export class ConceptNameDO {
|
|
|
45
50
|
'en-us': string[];
|
|
46
51
|
'pt-br': string[];
|
|
47
52
|
}>('names');
|
|
53
|
+
|
|
48
54
|
if (stored) {
|
|
49
55
|
this.names = stored;
|
|
50
56
|
}
|
|
51
57
|
}
|
|
52
58
|
|
|
53
59
|
async addName(language: 'en-us' | 'pt-br', name: string): Promise<void> {
|
|
60
|
+
await this.loadNames(); // Ensure we have the latest names
|
|
61
|
+
|
|
54
62
|
if (!this.names[language].includes(name)) {
|
|
55
63
|
this.names[language].push(name);
|
|
64
|
+
|
|
65
|
+
// ✅ Ensure concurrent writes do not overwrite existing stored data
|
|
56
66
|
await this.state.storage.put('names', this.names);
|
|
57
67
|
}
|
|
58
68
|
}
|
|
@@ -2,7 +2,7 @@ import { and, eq } from 'drizzle-orm';
|
|
|
2
2
|
import { inject, injectable } from 'inversify';
|
|
3
3
|
import 'reflect-metadata';
|
|
4
4
|
import { v4 as uuidv4 } from 'uuid';
|
|
5
|
-
import { schema } from '../..';
|
|
5
|
+
import { ConceptNameDO, schema } from '../..';
|
|
6
6
|
import { Concept, ControlNetConfig, Languages } from '../../types';
|
|
7
7
|
import {
|
|
8
8
|
KVConcept,
|
|
@@ -35,9 +35,9 @@ export class ConceptService {
|
|
|
35
35
|
const kvKeyPT = buildConceptKVKey('pt-br', conceptSlug, combinationString);
|
|
36
36
|
const failureKey = `failures:basic-info:${conceptSlug}:${combinationString}`;
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
// ✅ Use Durable Object stub
|
|
39
|
+
const id = this.context.env.CONCEPT_NAMES_DO.idFromName(conceptSlug);
|
|
40
|
+
const stub = this.context.env.CONCEPT_NAMES_DO.get(id);
|
|
41
41
|
|
|
42
42
|
let attempts = 0;
|
|
43
43
|
const maxAttempts = 3;
|
|
@@ -51,7 +51,7 @@ export class ConceptService {
|
|
|
51
51
|
// ✅ Fetch the latest name list from Durable Object
|
|
52
52
|
let allNamesEN: string[] = [];
|
|
53
53
|
let allNamesPT: string[] = [];
|
|
54
|
-
const response = await fetch(
|
|
54
|
+
const response = await stub.fetch('https://internal/names');
|
|
55
55
|
if (response.ok) {
|
|
56
56
|
const data = (await response.json()) as {
|
|
57
57
|
'en-us': string[];
|
|
@@ -60,8 +60,8 @@ export class ConceptService {
|
|
|
60
60
|
allNamesEN = data['en-us'] || [];
|
|
61
61
|
allNamesPT = data['pt-br'] || [];
|
|
62
62
|
} else {
|
|
63
|
-
console.log('!-- Error
|
|
64
|
-
console.log('!-- Response:
|
|
63
|
+
console.log('!-- Error fetching names from Durable Object');
|
|
64
|
+
console.log('!-- Response:', await response.text());
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
// ✅ Fetch existing KV data to backfill Durable Object if necessary
|
|
@@ -71,37 +71,19 @@ export class ConceptService {
|
|
|
71
71
|
// 🔥 If names already exist in KV but not in DO, add them
|
|
72
72
|
if (existingEN.name && !allNamesEN.includes(existingEN.name)) {
|
|
73
73
|
console.log(`⚡ Backfilling existing name to DO: ${existingEN.name}`);
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
name: existingEN.name,
|
|
80
|
-
}),
|
|
81
|
-
headers: { 'Content-Type': 'application/json' },
|
|
82
|
-
});
|
|
83
|
-
} catch (e) {
|
|
84
|
-
console.log(
|
|
85
|
-
`!-- Error on fetch ConceptNames ${(e as Error).message}`
|
|
86
|
-
);
|
|
87
|
-
}
|
|
74
|
+
await stub.fetch('https://internal/add-name', {
|
|
75
|
+
method: 'POST',
|
|
76
|
+
body: JSON.stringify({ language: 'en-us', name: existingEN.name }),
|
|
77
|
+
headers: { 'Content-Type': 'application/json' },
|
|
78
|
+
});
|
|
88
79
|
}
|
|
89
80
|
if (existingPT.name && !allNamesPT.includes(existingPT.name)) {
|
|
90
81
|
console.log(`⚡ Backfilling existing name to DO: ${existingPT.name}`);
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
name: existingPT.name,
|
|
97
|
-
}),
|
|
98
|
-
headers: { 'Content-Type': 'application/json' },
|
|
99
|
-
});
|
|
100
|
-
} catch (e) {
|
|
101
|
-
console.log(
|
|
102
|
-
`!-- Error on fetch ConceptNames ${(e as Error).message}`
|
|
103
|
-
);
|
|
104
|
-
}
|
|
82
|
+
await stub.fetch('https://internal/add-name', {
|
|
83
|
+
method: 'POST',
|
|
84
|
+
body: JSON.stringify({ language: 'pt-br', name: existingPT.name }),
|
|
85
|
+
headers: { 'Content-Type': 'application/json' },
|
|
86
|
+
});
|
|
105
87
|
}
|
|
106
88
|
|
|
107
89
|
// ✅ Skip generation if basic info already exists and override is false
|
|
@@ -145,13 +127,13 @@ export class ConceptService {
|
|
|
145
127
|
}
|
|
146
128
|
|
|
147
129
|
// ✅ **Immediately update Durable Object with the new name**
|
|
148
|
-
await fetch(
|
|
130
|
+
await stub.fetch('https://internal/add-name', {
|
|
149
131
|
method: 'POST',
|
|
150
132
|
body: JSON.stringify({ language: 'en-us', name: nameEN }),
|
|
151
133
|
headers: { 'Content-Type': 'application/json' },
|
|
152
134
|
});
|
|
153
135
|
|
|
154
|
-
await fetch(
|
|
136
|
+
await stub.fetch('https://internal/add-name', {
|
|
155
137
|
method: 'POST',
|
|
156
138
|
body: JSON.stringify({ language: 'pt-br', name: namePT }),
|
|
157
139
|
headers: { 'Content-Type': 'application/json' },
|
package/package.json
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
import { DrizzleD1Database } from 'drizzle-orm/d1';
|
|
10
10
|
import { Context, Env } from 'hono';
|
|
11
11
|
import type { Container } from 'inversify';
|
|
12
|
+
import { ConceptNameDO } from '../../app';
|
|
12
13
|
|
|
13
14
|
export type CentralBindings = {
|
|
14
15
|
TEST_IMAGES_BUCKET: R2Bucket;
|
|
@@ -25,7 +26,7 @@ export type CentralBindings = {
|
|
|
25
26
|
KV_CONCEPT_FAILURES: KVNamespace;
|
|
26
27
|
KV_CONCEPT_NAMES: KVNamespace;
|
|
27
28
|
CONCEPT_GENERATION_QUEUE: Queue;
|
|
28
|
-
CONCEPT_NAMES_DO: DurableObjectNamespace
|
|
29
|
+
CONCEPT_NAMES_DO: DurableObjectNamespace<ConceptNameDO>;
|
|
29
30
|
|
|
30
31
|
PROMPT_IMAGE_DESCRIBER: string;
|
|
31
32
|
PROMPT_GENERATE_ARCHETYPE_BASIC_INFO: string;
|