@zodic/shared 0.0.317 → 0.0.318
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.
|
@@ -53,39 +53,52 @@ export class LeonardoService {
|
|
|
53
53
|
* Processes a message from ARCHETYPE_POPULATION_QUEUE to ensure the archetype has three images.
|
|
54
54
|
*/
|
|
55
55
|
async processArchetypePopulation(message: QueueMessage): Promise<void> {
|
|
56
|
-
const { archetypeDataId
|
|
57
|
-
message;
|
|
56
|
+
const { archetypeDataId } = message;
|
|
58
57
|
await this.log('info', 'Processing archetype population', {
|
|
59
58
|
archetypeDataId,
|
|
60
|
-
combination,
|
|
61
|
-
gender,
|
|
62
|
-
language,
|
|
63
|
-
archetypeIndex,
|
|
64
59
|
});
|
|
65
|
-
|
|
60
|
+
|
|
66
61
|
const db = this.context.drizzle();
|
|
67
62
|
const archetype = await db
|
|
68
|
-
.select(
|
|
63
|
+
.select({
|
|
64
|
+
id: schema.archetypesData.id,
|
|
65
|
+
combination: schema.archetypesData.combination,
|
|
66
|
+
gender: schema.archetypesData.gender,
|
|
67
|
+
language: schema.archetypesData.language,
|
|
68
|
+
archetypeIndex: schema.archetypesData.archetypeIndex,
|
|
69
|
+
leonardoPrompt: schema.archetypesData.leonardoPrompt,
|
|
70
|
+
images: schema.archetypesData.images,
|
|
71
|
+
})
|
|
69
72
|
.from(schema.archetypesData)
|
|
70
73
|
.where(eq(schema.archetypesData.id, archetypeDataId))
|
|
71
74
|
.limit(1)
|
|
72
75
|
.execute();
|
|
73
|
-
|
|
76
|
+
|
|
74
77
|
if (!archetype[0]) {
|
|
75
78
|
await this.log('error', 'Archetype not found', { archetypeDataId });
|
|
76
79
|
throw new Error(`Archetype not found: ${archetypeDataId}`);
|
|
77
80
|
}
|
|
78
|
-
|
|
79
|
-
const images =
|
|
80
|
-
|
|
81
|
+
|
|
82
|
+
const { combination, gender, language, archetypeIndex, leonardoPrompt, images } = archetype[0];
|
|
83
|
+
|
|
84
|
+
await this.log('info', 'Fetched archetype details', {
|
|
85
|
+
archetypeDataId,
|
|
86
|
+
combination,
|
|
87
|
+
gender,
|
|
88
|
+
language,
|
|
89
|
+
archetypeIndex,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const parsedImages = JSON.parse(images || '[]');
|
|
93
|
+
if (parsedImages.length >= 3) {
|
|
81
94
|
await this.log('info', 'Archetype already has sufficient images', {
|
|
82
95
|
archetypeDataId,
|
|
83
|
-
imageCount:
|
|
96
|
+
imageCount: parsedImages.length,
|
|
84
97
|
});
|
|
85
98
|
return;
|
|
86
99
|
}
|
|
87
|
-
|
|
88
|
-
if (!
|
|
100
|
+
|
|
101
|
+
if (!leonardoPrompt) {
|
|
89
102
|
await this.log('error', 'Missing Leonardo prompt for archetype', {
|
|
90
103
|
archetypeDataId,
|
|
91
104
|
});
|
|
@@ -93,21 +106,21 @@ export class LeonardoService {
|
|
|
93
106
|
`Missing Leonardo prompt for archetype: ${archetypeDataId}`
|
|
94
107
|
);
|
|
95
108
|
}
|
|
96
|
-
|
|
109
|
+
|
|
97
110
|
await this.log('debug', 'Generating images for archetype', {
|
|
98
111
|
archetypeDataId,
|
|
99
|
-
prompt:
|
|
112
|
+
prompt: leonardoPrompt,
|
|
100
113
|
});
|
|
101
|
-
|
|
114
|
+
|
|
102
115
|
const generationResponse = await this.context
|
|
103
116
|
.api()
|
|
104
117
|
.callLeonardo.generateImage({
|
|
105
|
-
prompt:
|
|
118
|
+
prompt: leonardoPrompt,
|
|
106
119
|
width: 512,
|
|
107
120
|
height: 640,
|
|
108
|
-
quantity: 3 -
|
|
121
|
+
quantity: 3 - parsedImages.length,
|
|
109
122
|
});
|
|
110
|
-
|
|
123
|
+
|
|
111
124
|
const generationId = generationResponse.sdGenerationJob.generationId;
|
|
112
125
|
if (!generationId) {
|
|
113
126
|
await this.log(
|
|
@@ -117,7 +130,7 @@ export class LeonardoService {
|
|
|
117
130
|
);
|
|
118
131
|
throw new Error('Leonardo generation failed to return a valid ID');
|
|
119
132
|
}
|
|
120
|
-
|
|
133
|
+
|
|
121
134
|
await db.insert(schema.generations).values({
|
|
122
135
|
id: generationId,
|
|
123
136
|
archetypeIndex: parseInt(archetypeIndex),
|
|
@@ -127,11 +140,11 @@ export class LeonardoService {
|
|
|
127
140
|
gender,
|
|
128
141
|
createdAt: new Date(),
|
|
129
142
|
});
|
|
130
|
-
|
|
143
|
+
|
|
131
144
|
await this.log('info', 'Queued image generation', {
|
|
132
145
|
generationId,
|
|
133
146
|
archetypeDataId,
|
|
134
|
-
imagesToGenerate: 3 -
|
|
147
|
+
imagesToGenerate: 3 - parsedImages.length,
|
|
135
148
|
});
|
|
136
149
|
}
|
|
137
150
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { and, eq } from 'drizzle-orm';
|
|
1
|
+
import { and, eq, inArray } from 'drizzle-orm';
|
|
2
2
|
import { inject, injectable } from 'inversify';
|
|
3
3
|
import { schema } from '../..';
|
|
4
4
|
import { Gender } from '../../types';
|
|
@@ -55,7 +55,7 @@ export class ArchetypeWorkflow {
|
|
|
55
55
|
gender,
|
|
56
56
|
language,
|
|
57
57
|
});
|
|
58
|
-
|
|
58
|
+
|
|
59
59
|
// Step 1: Check if archetypes exist, if not, generate names
|
|
60
60
|
let archetypes = await this.archetypeService.fetchArchetypesFromDB(
|
|
61
61
|
combinationString,
|
|
@@ -79,7 +79,7 @@ export class ArchetypeWorkflow {
|
|
|
79
79
|
archetypesCount: archetypes.length,
|
|
80
80
|
archetypeIds: archetypes.map((a) => a.id),
|
|
81
81
|
});
|
|
82
|
-
|
|
82
|
+
|
|
83
83
|
// Step 2: Check if any archetypes are already being processed (images are generating)
|
|
84
84
|
const db = this.context.drizzle();
|
|
85
85
|
await this.log('debug', 'Checking for in-progress image generations', {
|
|
@@ -93,11 +93,14 @@ export class ArchetypeWorkflow {
|
|
|
93
93
|
and(
|
|
94
94
|
eq(schema.generations.status, 'pending'),
|
|
95
95
|
eq(schema.generations.gender, gender),
|
|
96
|
-
|
|
96
|
+
inArray(
|
|
97
|
+
schema.generations.archetypeDataId,
|
|
98
|
+
archetypes.map((a) => a.id)
|
|
99
|
+
)
|
|
97
100
|
)
|
|
98
101
|
)
|
|
99
102
|
.execute();
|
|
100
|
-
|
|
103
|
+
|
|
101
104
|
if (inProgressGenerations.length > 0) {
|
|
102
105
|
await this.log(
|
|
103
106
|
'warn',
|
|
@@ -113,7 +116,7 @@ export class ArchetypeWorkflow {
|
|
|
113
116
|
combinationString,
|
|
114
117
|
gender,
|
|
115
118
|
});
|
|
116
|
-
|
|
119
|
+
|
|
117
120
|
// Step 3: Generate missing textual content (description, virtues, content, leonardoPrompt)
|
|
118
121
|
const archetypesNeedingText = archetypes.filter(
|
|
119
122
|
(arc) =>
|
|
@@ -122,7 +125,7 @@ export class ArchetypeWorkflow {
|
|
|
122
125
|
arc.content === '[]' ||
|
|
123
126
|
!arc.leonardoPrompt
|
|
124
127
|
);
|
|
125
|
-
|
|
128
|
+
|
|
126
129
|
if (archetypesNeedingText.length > 0) {
|
|
127
130
|
await this.log('info', 'Generating missing textual content', {
|
|
128
131
|
combinationString,
|
|
@@ -131,7 +134,7 @@ export class ArchetypeWorkflow {
|
|
|
131
134
|
archetypesNeedingTextCount: archetypesNeedingText.length,
|
|
132
135
|
archetypeIds: archetypesNeedingText.map((a) => a.id),
|
|
133
136
|
});
|
|
134
|
-
|
|
137
|
+
|
|
135
138
|
const descVirtues =
|
|
136
139
|
await this.archetypeService.generateDescriptionsAndVirtues(
|
|
137
140
|
combinationString,
|
|
@@ -157,7 +160,7 @@ export class ArchetypeWorkflow {
|
|
|
157
160
|
language,
|
|
158
161
|
});
|
|
159
162
|
}
|
|
160
|
-
|
|
163
|
+
|
|
161
164
|
// Refetch archetypes after generating textual content
|
|
162
165
|
const updatedArchetypes = await this.archetypeService.fetchArchetypesFromDB(
|
|
163
166
|
combinationString,
|
|
@@ -168,12 +171,12 @@ export class ArchetypeWorkflow {
|
|
|
168
171
|
updatedArchetypesCount: updatedArchetypes.length,
|
|
169
172
|
archetypeIds: updatedArchetypes.map((a) => a.id),
|
|
170
173
|
});
|
|
171
|
-
|
|
174
|
+
|
|
172
175
|
// Step 4: Queue image generation if needed
|
|
173
176
|
const archetypesNeedingImages = updatedArchetypes.filter(
|
|
174
177
|
(arc) => JSON.parse(arc.images).length < 3
|
|
175
178
|
);
|
|
176
|
-
|
|
179
|
+
|
|
177
180
|
if (archetypesNeedingImages.length > 0) {
|
|
178
181
|
await this.log(
|
|
179
182
|
'info',
|
|
@@ -183,14 +186,10 @@ export class ArchetypeWorkflow {
|
|
|
183
186
|
archetypeIds: archetypesNeedingImages.map((a) => a.id),
|
|
184
187
|
}
|
|
185
188
|
);
|
|
186
|
-
|
|
189
|
+
|
|
187
190
|
const batch = archetypesNeedingImages.map((arc) => ({
|
|
188
191
|
body: {
|
|
189
192
|
archetypeDataId: arc.id,
|
|
190
|
-
combination: combinationString,
|
|
191
|
-
gender,
|
|
192
|
-
language,
|
|
193
|
-
archetypeIndex: arc.archetypeIndex,
|
|
194
193
|
},
|
|
195
194
|
}));
|
|
196
195
|
await this.log('debug', 'Sending batch to ARCHETYPE_POPULATION_QUEUE', {
|
|
@@ -201,10 +200,10 @@ export class ArchetypeWorkflow {
|
|
|
201
200
|
await this.log('info', 'Successfully queued image generation', {
|
|
202
201
|
batchCount: batch.length,
|
|
203
202
|
});
|
|
204
|
-
|
|
203
|
+
|
|
205
204
|
return { message: 'Images are being processed, please try again later.' };
|
|
206
205
|
}
|
|
207
|
-
|
|
206
|
+
|
|
208
207
|
await this.log('info', 'Archetypes fully processed and ready', {
|
|
209
208
|
updatedArchetypes,
|
|
210
209
|
});
|