File size: 11,579 Bytes
426a708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f89d3
426a708
59f89d3
 
426a708
 
 
59f89d3
 
426a708
 
 
 
 
 
 
 
59f89d3
426a708
 
 
59f89d3
426a708
 
 
 
 
 
 
 
59f89d3
426a708
 
 
 
 
 
 
 
59f89d3
426a708
 
59f89d3
426a708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f89d3
426a708
 
 
 
 
 
 
 
 
 
 
 
 
59f89d3
426a708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f89d3
426a708
 
 
 
 
 
 
 
 
 
 
59f89d3
426a708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f89d3
426a708
59f89d3
426a708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f89d3
426a708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f89d3
 
 
 
 
 
426a708
 
 
 
 
 
 
 
 
 
 
 
 
59f89d3
 
 
 
 
 
 
 
 
 
426a708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { SISTER_LABEL, GIRL_LABEL, BAR_LABEL, DJ_LABEL, SHYGUY_LABEL } from "./constants.js";
import { ConversationLLM } from "./conversation_llm.js";

export function nameToLabel(name) {
  switch (name) {
    case "Jessica's sister":
      return SISTER_LABEL;
    case "bartender":
      return BAR_LABEL;
    case "DJ":
      return DJ_LABEL;
    case "Jessica":
      return GIRL_LABEL;
    case "Shyguy":
      return SHYGUY_LABEL;
    default:
      return "";
  }
}

class Bar {
  constructor(shyguy) {
    this.name = "bartender";
    this.situation_prompt = "This is a conversation with the bartender.";
    this.personality = "This is the bartender. He always offers triple vodka in his first answer. Then he is helpful, but he says that he has a crush on Jessica's sister.";
    // this.past_conversation = "";
    this.imgpath = "assets/assets/barman.jpeg";
    this.output_format_prompt = "";
    this.met_shyguy = false;
    this.shyguy = shyguy;
    this.functionDescriptions = [
      {
        key: "analyzeConsumption",
        description:
          "Analyze the conversation to determine how many beers were consumed. Also if heavy alcohol was consumed",
        parameters: {
          num_beers: {
            type: "number",
            description: "Total number of beers ordered by Shyguy for himself in the conversation. ",
          },
          heavy_alcohol: {
            type: "boolean",
            description: "If Shyguy ordered or agreed to have heavy alcohol (like vodka), True",
          },
        },
      },
    ];
    this.functionPrompt = "";
  }

  getSystemPrompt() {
    return `${this.personality}. ${this.met_shyguy ? "He has met Shyguy already." : "He has not met Shyguy yet."}. The song playing is ${this.shyguy.song_playing}.`;
  }
}

class DJ {
  constructor() {
    this.name = "DJ";
    this.situation_prompt = "This is a conversation with the DJ.";
    this.personality =
      "This is the DJ. He is funny. He offers drugs to the shyguy. He can only play songs from the options [Let it be, Call me maybe, Shape of you]. If asked to play a song, he will play it and not propose any other song. He answers in two sentences maximum. He is funny.";
    // this.past_conversation = "";
    this.imgpath = "assets/assets/dj.jpeg";
    this.met_shyguy = false;
    this.output_format_prompt = "";
    this.functionDescriptions = [
      {
        key: "chooseSongToPlay",
        description: "Analyze the conversation to choose which song to play",
        parameters: {
          song: {
            type: "string",
            description: "The song to play",
          },
        },
      },
    ];
    this.functionPrompt =
      "Based on the conversation, choose a song from options [Let it be, Call me maybe, Shape of you]";
  }

  getSystemPrompt() {
    return `${this.personality}. ${this.met_shyguy ? "He has met Shyguy already." : "He has not met Shyguy yet."}`;
  }
}

class Sister {
  constructor() {
    // Initialize Sister properties
    this.name = "Jessica's sister";
    this.mood = 1;
    this.situation_prompt = "This is a conversation with the sister of Jessica.";
    this.personality =
      "This is the sister of Jessica. She is a deeply religious Christian. Her first answer is rude. Her second answer is about christianity. First answer is helpful. If asked about the favourite song of the girl, she says happily that it is 'Call me maybe'.";
    // this.past_conversation = "";
    this.imgpath = "assets/assets/sister.jpeg";
    this.met_shyguy = false;
    this.functionDescriptions = [
      {
        key: "analyzeMood",
        description: "Mood of the sister and gameover",
        parameters: {
          mood: {
            type: "number",
            description: "The mood of the sister on the scale of 1 to 10",
          },
          game_over: {
            type: "boolean",
            description: "Whether the game is over",
          },
        },
      },
    ];
    this.functionPrompt =
      "Analyze the conversation to determine the mood of the sister on the scale 1 to 10, based of how nice the conversation was. Also determine if the game is over. The game is over if shyguy was mean to the sister.";
  }

  getSystemPrompt() {
    return `${this.personality}. Her mood is ${this.mood} on the level 1 to 10. If the mood is low, she will be rude. If the mood is high, she will be helpful. ${this.met_shyguy ? "She has met Shyguy already." : "She has not met Shyguy yet."}`;
  }
}

class Girl {
  constructor(shyguy) {
    this.name = "Jessica";
    this.situation_prompt = "This is a conversation with the Jessica. She is the girl that shyguy likes.";
    this.personality = "This is Jessica. She is shy but nice. She likes the song 'Call me maybe'.";
    this.imgpath = "assets/assets/jessica.jpeg";
    this.output_format_prompt = "";
    this.shyguy = shyguy;
    this.met_shyguy = false;
    this.functionDescriptions = [
      {
        key: "analyzeLiking",
        description: "Analyze if Jessica is happy with the conversation or not",
        parameters: {
          likes_shyguy: {
            type: "boolean",
            description: "If Jessica is happy with the conversation, True",
          },
        },
      },
    ];
    this.functionPrompt = "Analyze if Jessica is happy with the conversation or not";
  }

  getSystemPrompt() {
    if (this.shyguy.song_playing === "Call me maybe") {
      return `${this.personality}. She is very happy with the song playing. The first thing she says is that she really likes the music. Therefore she is nice and she likes shyguy. However, if he talks about algorithms, she does not like it and she becomes mean. Also if he talks with a lot of hesitation, she does not like it and she becomes mean. ${this.met_shyguy ? "She has met Shyguy already." : "She has not met Shyguy yet."}`;
    } else {
      return `${this.personality}. She does not like the song that the DJ plays. The first thing she says is that the song is terrible. Then she is mean all the time. ${this.met_shyguy ? "She has met Shyguy already." : "She has not met Shyguy yet."}`;
    }
  }
}

class Wingman {
  constructor() {
    this.name = "wingman";
    this.situation_prompt = "This conversation happens with your wingman at the party.";
    this.personality = "This is your wingman. He is experienced with dating and gives practical advice. He's supportive but direct, and always encourages confidence without being aggressive.";
    this.imgpath = "assets/assets/wingman.jpeg";
    this.output_format_prompt = "";
    this.functionDescriptions = [
      {
        key: "analyzeAdvice",
        description: "Analyze the quality and impact of the conversation with the wingman",
        parameters: {
          confidence_boost: {
            type: "number",
            description: "How much confidence boost received on scale 1-5",
          },
          good_advice: {
            type: "boolean",
            description: "If practical, actionable advice was given, True",
          },
        },
      },
    ];
    this.functionPrompt = "Analyze how helpful the wingman's advice was and how much it boosted confidence";
  }

  getSystemPrompt() {
    return `${this.personality}.`;
  }
}



export class StoryEngine {
  constructor(shyguy) {
    // Initialize story engine properties with provided shyguy instance
    this.shyguy = shyguy;
    this.sister = new Sister();
    this.bar = new Bar(shyguy);
    this.dj = new DJ();
    this.girl = new Girl(shyguy);
  }

  async onEncounter(entity) {
    switch (entity) {
      case SISTER_LABEL:
        return this.generalInteraction(this.sister);
      case BAR_LABEL:
        return this.generalInteraction(this.bar);
      case GIRL_LABEL:
        return this.generalInteraction(this.girl);
      case DJ_LABEL:
        return this.generalInteraction(this.dj);
      default:
        console.log("Unknown entity encountered");
    }
  }

  async generalInteraction(targetEntity) {
    await this.shyguy.learnLesson(targetEntity.name);
    console.log("Lesson for " + targetEntity.name + this.shyguy.lessons_learned);
    const conversation_llm = new ConversationLLM(
      "Shyguy",
      targetEntity.name,
      this.shyguy.getSystemPrompt(),
      targetEntity.getSystemPrompt(),
      targetEntity.situation_prompt,
      targetEntity.output_format_prompt,
      targetEntity.functionDescriptions,
      targetEntity.functionPrompt
    );
    const conversation_output = await conversation_llm.generateConversation(6);
    const conversation = conversation_output.conversation;

    // Append the conversation to shyguy's history
    this.shyguy.conversation_history += `\nConversation with ${targetEntity.name}:\n${conversation}\n`;
    console.log(conversation);
    const conversationString = Object.values(conversation)
      .map(msg => `${msg.role}: ${msg.content}`)
      .join('\n');
    this.shyguy.learnFromConversation(conversationString);
    console.log("Lessons learned from conversation with " + targetEntity.name + this.shyguy.lessons_learned);

    let gameOver = this.decideGameOver(conversation_output.analysis.parameters.game_over);
    let gameSuccesful = this.decideGameSuccesful(conversation_output.analysis.parameters.likes_shyguy);
    if (targetEntity.name === "Jessica" && !gameSuccesful) {
      gameOver = true;
    }
    console.log("gameOver: " + gameOver);
    console.log("gameSuccesful: " + gameSuccesful);
    console.log(conversation_output);

    this.updateStates(conversation_output.analysis, targetEntity.name);
    console.log("shyguy num_beers: " + this.shyguy.num_beers);

    targetEntity.met_shyguy = true;

    if (targetEntity.name === "DJ") {
      this.shyguy.met_dj = true;
    } else if (targetEntity.name === "bartender") {
      this.shyguy.met_bar = true;
    } else if (targetEntity.name === "Jessica's sister") {
      this.shyguy.met_sister = true;
    }

    return {
      conversation: conversation,
      char1imgpath: this.shyguy.imgpath,
      char2imgpath: targetEntity.imgpath,
      gameOver: gameOver,
      gameSuccesful: gameSuccesful,
    };
  }

  decideGameOver(gameOverParameter) {
    let gameOver = false;
    if (gameOverParameter === "none") {
      gameOver = false;
    } else if (gameOverParameter === true) {
      gameOver = true;
    } else {
      gameOver = false;
    }
    return gameOver;
  }

  decideGameSuccesful(likesShyguy) {
    let gameSuccesful = false;
    if (likesShyguy) {
      gameSuccesful = true;
    } else {
      gameSuccesful = false;
    }
    return gameSuccesful;
  }

  updateStates(conversation_analysis, targetName) {
    if (targetName === "Jessica's sister") {
      if (conversation_analysis.parameters.mood !== "none") {
        this.sister.mood = conversation_analysis.parameters.mood;
      }
    } else if (targetName === "bartender") {
      if (conversation_analysis.parameters.num_beers !== "none") {
        this.shyguy.num_beers += Number(conversation_analysis.parameters.num_beers);
        this.shyguy.courage += 2 * Number(conversation_analysis.parameters.num_beers);
        this.shyguy.num_beers += 3 * Number(conversation_analysis.parameters.heavy_alcohol === true);
        console.log("Shyguy num_beers inside updateStates: " + this.shyguy.num_beers);
        this.shyguy.courage += 3 * Number(conversation_analysis.parameters.heavy_alcohol);
      }
    } else if (targetName === "DJ") {
      if (conversation_analysis.parameters.song !== "none") {
        this.shyguy.song_playing = conversation_analysis.parameters.song;
      }
    }
  }
}