@zodic/shared 0.0.139 → 0.0.140
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 +59 -40
- package/package.json +1 -1
|
@@ -16,40 +16,60 @@ export class ConceptService {
|
|
|
16
16
|
/**
|
|
17
17
|
* Generate basic info for a concept: name, description, and poem.
|
|
18
18
|
*/
|
|
19
|
-
async generateBasicInfo(
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
async generateBasicInfo(
|
|
20
|
+
conceptSlug: Concept,
|
|
21
|
+
combinationString: string
|
|
22
|
+
): Promise<void> {
|
|
23
|
+
console.log(
|
|
24
|
+
`🚀 Generating basic info for concept: ${conceptSlug}, combination: ${combinationString}`
|
|
25
|
+
);
|
|
26
|
+
|
|
22
27
|
// ✅ Build the messages to request content
|
|
23
28
|
const messages = this.context.buildLLMMessages().generateConceptBasicInfo({
|
|
24
29
|
combination: combinationString,
|
|
25
30
|
conceptSlug,
|
|
26
31
|
});
|
|
27
|
-
|
|
32
|
+
|
|
28
33
|
// ✅ Call ChatGPT API
|
|
29
34
|
const response = await this.context.api().callChatGPT.single(messages, {});
|
|
30
35
|
if (!response) {
|
|
31
|
-
throw new Error(
|
|
36
|
+
throw new Error(
|
|
37
|
+
`❌ Failed to generate basic info for concept: ${conceptSlug}`
|
|
38
|
+
);
|
|
32
39
|
}
|
|
33
|
-
|
|
40
|
+
|
|
34
41
|
// ✅ Parse response for both languages
|
|
35
|
-
const { nameEN, descriptionEN, poemEN, namePT, descriptionPT, poemPT } =
|
|
36
|
-
|
|
42
|
+
const { nameEN, descriptionEN, poemEN, namePT, descriptionPT, poemPT } =
|
|
43
|
+
this.parseBasicInfoResponse(response);
|
|
44
|
+
|
|
37
45
|
// ✅ Store in KV for both languages
|
|
38
46
|
const kvStore = this.context.kvConceptsStore();
|
|
39
|
-
|
|
47
|
+
|
|
40
48
|
// 🌍 English version
|
|
41
|
-
const kvKeyEN = buildConceptKVKey(
|
|
49
|
+
const kvKeyEN = buildConceptKVKey('en-us', conceptSlug, combinationString);
|
|
42
50
|
const conceptEN = await this.getKVConcept(kvKeyEN);
|
|
43
|
-
Object.assign(conceptEN, {
|
|
51
|
+
Object.assign(conceptEN, {
|
|
52
|
+
name: nameEN,
|
|
53
|
+
description: descriptionEN,
|
|
54
|
+
poem: poemEN,
|
|
55
|
+
status: 'idle',
|
|
56
|
+
});
|
|
44
57
|
await kvStore.put(kvKeyEN, JSON.stringify(conceptEN));
|
|
45
|
-
|
|
58
|
+
|
|
46
59
|
// 🇧🇷 Portuguese version
|
|
47
|
-
const kvKeyPT = buildConceptKVKey(
|
|
60
|
+
const kvKeyPT = buildConceptKVKey('pt-br', conceptSlug, combinationString);
|
|
48
61
|
const conceptPT = await this.getKVConcept(kvKeyPT);
|
|
49
|
-
Object.assign(conceptPT, {
|
|
62
|
+
Object.assign(conceptPT, {
|
|
63
|
+
name: namePT,
|
|
64
|
+
description: descriptionPT,
|
|
65
|
+
poem: poemPT,
|
|
66
|
+
status: 'idle',
|
|
67
|
+
});
|
|
50
68
|
await kvStore.put(kvKeyPT, JSON.stringify(conceptPT));
|
|
51
|
-
|
|
52
|
-
console.log(
|
|
69
|
+
|
|
70
|
+
console.log(
|
|
71
|
+
`✅ Basic info stored for ${conceptSlug}, combination: ${combinationString}, in both languages.`
|
|
72
|
+
);
|
|
53
73
|
}
|
|
54
74
|
|
|
55
75
|
private parseBasicInfoResponse(response: string): {
|
|
@@ -60,30 +80,29 @@ export class ConceptService {
|
|
|
60
80
|
descriptionPT: string;
|
|
61
81
|
poemPT: string;
|
|
62
82
|
} {
|
|
63
|
-
console.log(
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
throw new Error("Invalid basic info response format");
|
|
83
|
+
console.log('📌 Parsing basic info response from ChatGPT:', response);
|
|
84
|
+
|
|
85
|
+
const enMatch = response.match(
|
|
86
|
+
/EN:\s*•\s*Name:\s*(.+?)\s*•\s*Description:\s*([\s\S]+?)\s*•\s*Poetic Passage:\s*([\s\S]+?)\s*(?=PT:|$)/
|
|
87
|
+
);
|
|
88
|
+
const ptMatch = response.match(
|
|
89
|
+
/PT:\s*•\s*Nome:\s*(.+?)\s*•\s*Descrição:\s*([\s\S]+?)\s*•\s*Passagem Poética:\s*([\s\S]+)/
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
if (!enMatch || !ptMatch) {
|
|
93
|
+
console.error('❌ Invalid basic info response format:', response);
|
|
94
|
+
throw new Error('Invalid basic info response format');
|
|
76
95
|
}
|
|
77
|
-
|
|
78
|
-
const nameEN =
|
|
79
|
-
const descriptionEN =
|
|
80
|
-
const poemEN =
|
|
81
|
-
|
|
82
|
-
const namePT =
|
|
83
|
-
const descriptionPT =
|
|
84
|
-
const poemPT =
|
|
85
|
-
|
|
86
|
-
console.log(
|
|
96
|
+
|
|
97
|
+
const nameEN = enMatch[1].trim();
|
|
98
|
+
const descriptionEN = enMatch[2].trim();
|
|
99
|
+
const poemEN = enMatch[3].trim();
|
|
100
|
+
|
|
101
|
+
const namePT = ptMatch[1].trim();
|
|
102
|
+
const descriptionPT = ptMatch[2].trim();
|
|
103
|
+
const poemPT = ptMatch[3].trim();
|
|
104
|
+
|
|
105
|
+
console.log('✅ Successfully parsed basic info:', {
|
|
87
106
|
nameEN,
|
|
88
107
|
descriptionEN,
|
|
89
108
|
poemEN,
|
|
@@ -91,7 +110,7 @@ export class ConceptService {
|
|
|
91
110
|
descriptionPT,
|
|
92
111
|
poemPT,
|
|
93
112
|
});
|
|
94
|
-
|
|
113
|
+
|
|
95
114
|
return { nameEN, descriptionEN, poemEN, namePT, descriptionPT, poemPT };
|
|
96
115
|
}
|
|
97
116
|
|