2
2
const mBot_idOverride = process . env . OPENAI_MAHT_GPT_OVERRIDE
3
3
const mDefaultBotTypeArray = [ 'personal-avatar' , 'avatar' ]
4
4
const mDefaultBotType = mDefaultBotTypeArray [ 0 ]
5
- const mDefaultGreeting = 'Hello! If you need to know how to get started, just ask!'
5
+ const mDefaultGreeting = 'avatar' // greeting routine
6
6
const mDefaultGreetings = [ 'Welcome to MyLife! I am here to help you!' ]
7
7
const mDefaultTeam = 'memory'
8
8
const mRequiredBotTypes = [ 'personal-avatar' ]
@@ -28,20 +28,24 @@ const mTeams = [
28
28
class Bot {
29
29
#collectionsAgent
30
30
#conversation
31
+ #documentName
31
32
#factory
32
33
#feedback
33
- #initialGreeting
34
+ #firstAccess= false
35
+ #greetingRoutine
34
36
#greetings
35
37
#instructionNodes = new Set ( )
36
38
#llm
37
39
#type
38
40
constructor ( botData , llm , factory ) {
39
41
this . #factory = factory
40
42
this . #llm = llm
41
- const { feedback= [ ] , greeting= mDefaultGreeting , greetings= mDefaultGreetings , type= mDefaultBotType , ..._botData } = botData
43
+ const { feedback= [ ] , greeting= mDefaultGreeting , greetings= mDefaultGreetings , name, unaccessed, type= mDefaultBotType , ..._botData } = botData
44
+ this . #documentName = name
42
45
this . #feedback = feedback
46
+ this . #firstAccess = unaccessed
43
47
this . #greetings = greetings
44
- this . #initialGreeting = greeting
48
+ this . #greetingRoutine = type . split ( '-' ) . pop ( )
45
49
this . #type = type
46
50
Object . assign ( this , this . globals . sanitize ( _botData ) )
47
51
switch ( type ) {
@@ -75,7 +79,7 @@ class Bot {
75
79
Conversation . prompt = message
76
80
Conversation . originalPrompt = originalMessage
77
81
await mCallLLM ( Conversation , allowSave , this . #llm, this . #factory, avatar ) // mutates Conversation
78
- /* frontend mutations */
82
+ this . accessed = true
79
83
return Conversation
80
84
}
81
85
/**
@@ -153,17 +157,27 @@ class Bot {
153
157
/**
154
158
* Retrieves a greeting message from the active bot.
155
159
* @param {Boolean } dynamic - Whether to use dynamic greetings (`true`) or static (`false`)
156
- * @param {String } greetingPrompt - The prompt for the dynamic greeting
157
- * @returns {String } - The greeting message string
160
+ * @param {string } greetingPrompt - The prompt for the dynamic greeting
161
+ * @returns {object } - The Response object { responses, routine, success, }
158
162
*/
159
163
async greeting ( dynamic = false , greetingPrompt = 'Greet me and tell me briefly what we did last' ) {
160
164
if ( ! this . llm_id )
161
165
throw new Error ( 'Bot initialized incorrectly: missing `llm_id` from database' )
162
- const greetings = dynamic
163
- ? await mBotGreetings ( this . thread_id , this . llm_id , greetingPrompt , this . #llm, this . #factory)
164
- : this . greetings
165
- const greeting = greetings [ Math . floor ( Math . random ( ) * greetings . length ) ]
166
- return greeting
166
+ let greeting ,
167
+ responses = [ ] ,
168
+ routine
169
+ if ( ! this . #firstAccess) {
170
+ const greetings = dynamic
171
+ ? await mBotGreetings ( this . thread_id , this . llm_id , greetingPrompt , this . #llm, this . #factory)
172
+ : [ this . greetings [ Math . floor ( Math . random ( ) * this . greetings . length ) ] ]
173
+ responses . push ( ...greetings )
174
+ } else
175
+ routine = this . #greetingRoutine
176
+ return {
177
+ responses,
178
+ routine,
179
+ success : true ,
180
+ }
167
181
}
168
182
/**
169
183
* Migrates Conversation from an old thread to a newly-created destination thread, observable in `this.Conversation`.
@@ -215,12 +229,23 @@ class Bot {
215
229
await this . #factory. updateBot ( bot )
216
230
}
217
231
/* getters/setters */
232
+ get accessed ( ) {
233
+ return ! this . unaccessed
234
+ }
235
+ set accessed ( accessed = true ) {
236
+ if ( accessed ) {
237
+ this . update ( {
238
+ unaccessed : false ,
239
+ } )
240
+ this . #firstAccess = false
241
+ }
242
+ }
218
243
/**
219
244
* Gets the frontend bot object.
220
245
* @getter
221
246
*/
222
247
get bot ( ) {
223
- const { bot_name : name , description, flags, id, interests, purpose, type, version } = this
248
+ const { description, flags, id, interests, name , purpose, type, version } = this
224
249
const bot = {
225
250
description,
226
251
flags,
@@ -263,24 +288,11 @@ class Bot {
263
288
get isMyLife ( ) {
264
289
return this . #factory. isMyLife
265
290
}
266
- get micro ( ) {
267
- const {
268
- bot_name,
269
- id,
270
- name,
271
- provider,
272
- type,
273
- version,
274
- } = this
275
- const microBot = {
276
- bot_name,
277
- id,
278
- name,
279
- provider,
280
- type,
281
- version,
282
- }
283
- return microBot
291
+ get name ( ) {
292
+ return this . bot_name
293
+ }
294
+ set name ( name ) {
295
+ this . bot_name = name
284
296
}
285
297
get type ( ) {
286
298
return this . #type
@@ -298,7 +310,7 @@ class Bot {
298
310
class BotAgent {
299
311
#activeBot
300
312
#activeTeam = mDefaultTeam
301
- #avatarId
313
+ #avatar
302
314
#bots
303
315
#factory
304
316
#fileConversation
@@ -311,19 +323,19 @@ class BotAgent {
311
323
/**
312
324
* Initializes the BotAgent instance.
313
325
* @async
314
- * @param {Guid } avatarId - The Avatar id
326
+ * @param {Guid } Avatar - The Avatar instance
315
327
* @param {string } vectorstoreId - The Vectorstore id
316
328
* @returns {Promise<BotAgent> } - The BotAgent instance
317
329
*/
318
- async init ( avatarId , vectorstoreId ) {
330
+ async init ( Avatar ) {
319
331
/* validate request */
320
- if ( ! avatarId ?. length )
321
- throw new Error ( 'AvatarId required' )
322
- this . #avatarId = avatarId
332
+ if ( ! Avatar )
333
+ throw new Error ( 'Avatar required' )
334
+ this . #avatar = Avatar
323
335
this . #bots = [ ]
324
- this . #vectorstoreId = vectorstoreId
336
+ this . #vectorstoreId = Avatar . vectorstoreId
325
337
/* execute request */
326
- await mInit ( this , this . #bots, this . #factory, this . #llm)
338
+ await mInit ( this , this . #bots, this . #avatar , this . # factory, this . #llm)
327
339
return this
328
340
}
329
341
/* public functions */
@@ -346,7 +358,7 @@ class BotAgent {
346
358
* @returns {Bot } - The created Bot instance
347
359
*/
348
360
async botCreate ( botData ) {
349
- const Bot = await mBotCreate ( this . # avatarId, this . #vectorstoreId, botData , this . #llm, this . #factory)
361
+ const Bot = await mBotCreate ( this . avatarId , this . #vectorstoreId, botData , this . #llm, this . #factory)
350
362
this . #bots. push ( Bot )
351
363
this . setActiveBot ( Bot . id )
352
364
return Bot
@@ -509,10 +521,11 @@ class BotAgent {
509
521
version = versionCurrent
510
522
versionUpdate = this . #factory. botInstructionsVersion ( type )
511
523
}
512
- greeting = await Bot . greeting ( dynamic , `Greet member while thanking them for selecting you` )
524
+ const { responses , routine , success : greetingSuccess , } = await Bot . greeting ( dynamic , `Greet member while thanking them for selecting you` )
513
525
return {
514
526
bot_id,
515
- greeting,
527
+ responses,
528
+ routine,
516
529
success,
517
530
version,
518
531
versionUpdate,
@@ -634,7 +647,7 @@ class BotAgent {
634
647
* @returns {String } - The Avatar id
635
648
*/
636
649
get avatarId ( ) {
637
- return this . #avatarId
650
+ return this . #avatar ?. id
638
651
}
639
652
/**
640
653
* Gets the Biographer bot for the BotAgent.
@@ -726,15 +739,17 @@ async function mBotCreate(avatarId, vectorstore_id, botData, llm, factory){
726
739
?? 'gpt-4o'
727
740
const { tools, tool_resources, } = mGetAIFunctions ( type , factory . globals , vectorstore_id )
728
741
const id = factory . newGuid
742
+ const typeShort = type . split ( '-' ) . pop ( )
729
743
let {
730
- bot_name = `My ${ type } ` ,
731
- description = `I am a ${ type } for ${ factory . memberName } ` ,
732
- name = `bot_${ type } _${ avatarId } ` ,
744
+ bot_name= `My ${ typeShort } ` ,
745
+ description= `I am a ${ typeShort } for ${ factory . memberName } ` ,
746
+ name= `bot_${ type } _${ avatarId } ` ,
733
747
} = botData
734
748
const validBotData = {
735
- being : 'bot' ,
749
+ being : 'bot' , // intentionally hard-coded
736
750
bot_name,
737
751
description,
752
+ unaccessed : true ,
738
753
greeting,
739
754
greetings,
740
755
id,
@@ -751,6 +766,7 @@ async function mBotCreate(avatarId, vectorstore_id, botData, llm, factory){
751
766
tools,
752
767
tool_resources,
753
768
type,
769
+ unaccessed : true , // removed after first chat
754
770
vectorstore_id,
755
771
version,
756
772
}
@@ -763,7 +779,7 @@ async function mBotCreate(avatarId, vectorstore_id, botData, llm, factory){
763
779
validBotData . thread_id = thread_id
764
780
botData = await factory . createBot ( validBotData ) // repurposed incoming botData
765
781
const _Bot = new Bot ( botData , llm , factory )
766
- console . log ( `bot created::${ type } ` , _Bot . thread_id , _Bot . id , _Bot . bot_id , _Bot . bot_name )
782
+ console . log ( `bot created::${ type } ` , _Bot . thread_id , _Bot . id , _Bot . llm_id , _Bot . bot_name )
767
783
return _Bot
768
784
}
769
785
/**
@@ -1183,30 +1199,50 @@ function mGetGPTResources(globals, toolName, vectorstoreId){
1183
1199
* @module
1184
1200
* @param {BotAgent } BotAgent - The BotAgent to initialize
1185
1201
* @param {Bot[] } bots - The array of bots (empty on init)
1202
+ * @param {Avatar } Avatar - The Avatar instance
1186
1203
* @param {AgentFactory } factory - The factory instance
1187
1204
* @param {LLMServices } llm - The LLMServices instance
1188
1205
* @returns {void }
1189
1206
*/
1190
- async function mInit ( BotAgent , bots , factory , llm ) {
1191
- const { avatarId , vectorstoreId, } = BotAgent
1192
- bots . push ( ...await mInitBots ( avatarId , vectorstoreId , factory , llm ) )
1207
+ async function mInit ( BotAgent , bots , Avatar , factory , llm ) {
1208
+ const { vectorstoreId, } = BotAgent
1209
+ bots . push ( ...await mInitBots ( vectorstoreId , Avatar , factory , llm ) )
1193
1210
BotAgent . setActiveBot ( null , false )
1194
1211
}
1195
1212
/**
1196
1213
* Initializes active bots based upon criteria.
1197
- * @param {Guid } avatarId - The Avatar id
1198
1214
* @param {String } vectorstore_id - The Vectorstore id
1215
+ * @param {Avatar } Avatar - The Avatar instance
1199
1216
* @param {AgentFactory } factory - The MyLife factory instance
1200
1217
* @param {LLMServices } llm - The LLMServices instance
1201
1218
* @returns {Bot[] } - The array of activated and available bots
1202
1219
*/
1203
- async function mInitBots ( avatarId , vectorstore_id , factory , llm ) {
1204
- const bots = ( await factory . bots ( avatarId ) )
1205
- . map ( botData => {
1220
+ async function mInitBots ( vectorstore_id , Avatar , factory , llm ) {
1221
+ let bots = await factory . bots ( Avatar ?. id )
1222
+ if ( bots ?. length ) {
1223
+ bots = bots . map ( botData => {
1206
1224
botData . vectorstore_id = vectorstore_id
1207
- botData . object_id = avatarId
1225
+ botData . object_id = Avatar . id
1208
1226
return new Bot ( botData , llm , factory )
1209
1227
} )
1228
+ } else {
1229
+ if ( factory . isMyLife )
1230
+ throw new Error ( 'MyLife bots not yet implemented' )
1231
+ const botTypes = mGetBotTypes ( factory . isMyLife )
1232
+ bots = await Promise . all (
1233
+ botTypes . map ( async type => {
1234
+ const botData = {
1235
+ object_id : Avatar . id ,
1236
+ type,
1237
+ }
1238
+ if ( type . includes ( 'avatar' ) )
1239
+ botData . bot_name = Avatar . nickname
1240
+ const Bot = await mBotCreate ( Avatar . id , vectorstore_id , botData , llm , factory )
1241
+ return Bot
1242
+ } )
1243
+ )
1244
+ Avatar . setupComplete = true
1245
+ }
1210
1246
return bots
1211
1247
}
1212
1248
/**
0 commit comments