-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemini.js
More file actions
328 lines (281 loc) · 9.71 KB
/
gemini.js
File metadata and controls
328 lines (281 loc) · 9.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Constants and state management
const STORY_CONFIG = {
RESPONSE_LIMIT: 10,
UI_ELEMENTS: {
story: document.getElementById('story'),
option1: document.getElementById('option1'),
option2: document.getElementById('option2'),
continue: document.getElementById('continue'),
},
CLASSES: {
hidden: 'noDisplay',
visible: 'yesDisplay',
},
};
class StoryState {
constructor() {
this.remainingResponses = STORY_CONFIG.RESPONSE_LIMIT;
this.fullStory = '';
this.databaseFullStory = '';
this.characterName = '';
}
decrementResponses() {
this.remainingResponses--;
return this.remainingResponses;
}
getStoryProgress() {
console.log(this.remainingResponses);
if (this.remainingResponses === 3) {
return 'story will need to finish in 3 more decisions, begin to wrap up story lines';
} else if (this.remainingResponses === 2) {
return 'story will need to finish in 2 more decisions, begin to tell the story of how the characters ended up';
} else if (this.remainingResponses === 1) {
return 'the story should conclude now, give the user a final decision to make to determine their future';
} else if (this.remainingResponses < 1) {
return 'the story must end now, wrap it up wonderfully in 4 sentences exactly, and stop giving options';
}
return '';
}
appendToStory(text) {
this.fullStory += (this.fullStory ? '\n\n' : '') + text;
this.databaseFullStory += '\n\n' + text;
}
appendChoice(choice) {
this.fullStory += '\n\nChose: ' + choice;
this.databaseFullStory += '\n\n - ' + choice;
}
setCharacterName(name) {
this.characterName = name;
}
}
// AI Service class
class AIService {
constructor() {
this.genAI = null;
}
async initialize() {
const response = await fetch('get_api_key.php');
const apiKey = await response.text();
const { GoogleGenerativeAI } = await import(
'https://esm.run/@google/generative-ai'
);
this.genAI = new GoogleGenerativeAI(apiKey.trim());
}
async generateStorySegment(prompt) {
const model = this.genAI.getGenerativeModel({ model: 'gemini-pro' });
// Request JSON structure specifically
const enhancedPrompt = `${prompt}
Please respond with a JSON object in the following format:
{
"story": "story text here",
"options": [
"first option text here",
"second option text here"
]
}`;
const result = await model.generateContent(enhancedPrompt);
const response = await result.response;
const text = response.text();
try {
// Parse the response as JSON
return JSON.parse(text);
} catch (error) {
console.error('Failed to parse AI response as JSON:', error);
throw new Error('Invalid AI response format');
}
}
}
// UI Manager class
class UIManager {
constructor(elements) {
this.elements = elements;
}
updateStory(storySegment) {
const { story, options } = storySegment;
this.elements.story.textContent = story;
this.updateOptions(options);
this.resetButtonStyles();
}
updateOptions(options) {
const [option1, option2] = options;
this.elements.option1.textContent = option1;
this.elements.option2.textContent = option2;
this.toggleOptionVisibility(this.elements.option1, option1?.length > 10);
this.toggleOptionVisibility(this.elements.option2, option2?.length > 10);
this.toggleContinueButton(option1?.length <= 10 && option2?.length <= 10);
}
toggleOptionVisibility(element, show) {
element.classList.toggle(STORY_CONFIG.CLASSES.hidden, !show);
element.classList.toggle(STORY_CONFIG.CLASSES.visible, show);
}
toggleContinueButton(show) {
this.elements.continue.classList.toggle(STORY_CONFIG.CLASSES.hidden, !show);
this.elements.continue.classList.toggle(STORY_CONFIG.CLASSES.visible, show);
}
setEndingState() {
console.log('The End');
const continueBtn = this.elements.continue;
continueBtn.textContent = 'The End';
continueBtn.style.border = 'none';
continueBtn.style.fontFamily = "'Great Vibes', cursive";
continueBtn.style.fontSize = '2em';
continueBtn.style.padding = '0';
continueBtn.style.color = 'white';
continueBtn.style.display = 'block';
}
resetButtonStyles() {
const elements = [
this.elements.option1,
this.elements.option2,
this.elements.continue,
];
elements.forEach((element) => {
element.style.color = 'white';
element.style.border = 'white solid 2px';
});
}
}
// Story Manager class
class StoryManager {
constructor() {
this.state = new StoryState();
this.aiService = new AIService();
this.uiManager = new UIManager(STORY_CONFIG.UI_ELEMENTS);
this.isFirstPrompt = true;
}
async initialize() {
await this.aiService.initialize();
this.setupEventListeners();
this.generateNextSegment();
}
setupEventListeners() {
STORY_CONFIG.UI_ELEMENTS.option1.addEventListener('click', () =>
this.handleOptionClick(1)
);
STORY_CONFIG.UI_ELEMENTS.option2.addEventListener('click', () =>
this.handleOptionClick(2)
);
STORY_CONFIG.UI_ELEMENTS.continue.addEventListener('click', () =>
this.generateNextSegment()
);
}
async handleOptionClick(optionNumber) {
const optionText =
STORY_CONFIG.UI_ELEMENTS[`option${optionNumber}`].textContent;
this.state.appendChoice(optionText);
await this.generateNextSegment();
}
async generateNextSegment() {
try {
const prompt = this.constructPrompt();
const storySegment = await this.aiService.generateStorySegment(prompt);
// If this is the first prompt, try to extract the character's name
if (this.isFirstPrompt) {
// Simple regex to find a name (this is a basic example - you might want to improve it)
const nameMatch = storySegment.story.match(
/([A-Z][a-z]+ )?[A-Z][a-z]+/
);
if (nameMatch) {
this.state.setCharacterName(nameMatch[0]);
}
}
this.state.appendToStory(storySegment.story);
this.uiManager.updateStory(storySegment);
this.state.decrementResponses();
this.isFirstPrompt = false;
if (
this.state.remainingResponses < 1 &&
(!storySegment.options[0] || storySegment.options[0].length < 10) &&
(!storySegment.options[1] || storySegment.options[1].length < 10)
) {
if (
this.state.remainingResponses < 1 &&
storySegment.options.every((option) => option.length < 10)
) {
this.uiManager.setEndingState();
await this.saveToDatabase();
}
}
} catch (error) {
console.error('Error generating story segment:', error);
setTimeout(() => this.generateNextSegment(), 1000);
}
}
constructPrompt() {
return this.isFirstPrompt
? this.constructInitialPrompt()
: this.constructContinuationPrompt();
}
constructInitialPrompt() {
const character = sessionStorage.getItem('character');
const weapon = sessionStorage.getItem('weapon');
const sidekick = sessionStorage.getItem('sidekick');
const location = sessionStorage.getItem('location');
return `You are a master storyteller creating a choose-your-own-adventure game. Create the opening paragrapy (up to 2 sentenses) of our story with these required elements:
Most Important: Keep it concise and provide an excellent hook to keep the reader interested!
Key elements that MUST be established and used consistently throughout the story:
- Location: ${location}
- Main Character: Create a ${character}-type character. Give them a memorable name that fits their personality.
- Weapon: ${weapon} (must be used at least once, preferably for humor)
- Sidekick: ${sidekick} (their trusty companion)
Story Guidelines:
- Introduce the main character by their new name
- Establish the setting and initial situation
- Keep the tone humorous and engaging
- Provide clear stakes or a goal
RESPONSE FORMAT: You must return a JSON object exactly like this:
{
"story": "Your concise opening story segment here (no numbering)",
"options": [
"First choice option here (clear and direct)",
"Second choice option here (clear and direct)"
]
}`;
}
constructContinuationPrompt() {
const progress = this.state.getStoryProgress();
const remainingSteps = this.state.remainingResponses;
return `Continue our choose-your-own-adventure story. Create the next segment with ${remainingSteps} steps remaining.
Important context:
${this.state.fullStory}
${progress}
Requirements:
- Use the established character name
- Keep each response to 1-2 sentences
- Maintain the humorous tone
- Keep the story moving forward
- Be concise but engaging
RESPONSE FORMAT: You must return a JSON object exactly like this:
{
"story": "Your concise story segment here (no numbering)",
"options": [
"First choice option here (clear and direct)",
"Second choice option here (clear and direct)"
]
}`;
}
async saveToDatabase() {
const data = {
characters: sessionStorage.getItem('character'),
sidekick: sessionStorage.getItem('databaseSidekick'),
weapon: sessionStorage.getItem('weapon'),
setting: sessionStorage.getItem('databaseLocation'),
fullStory: this.state.databaseFullStory,
};
try {
const response = await fetch('insert_story.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error('Failed to save story to database');
}
} catch (error) {
console.error('Database save error:', error);
}
}
}
// Initialize the application
const storyGame = new StoryManager();
storyGame.initialize().catch(console.error);