@@ -323,42 +323,53 @@ function initializeDatabase(dataDir: string) {
323
323
}
324
324
}
325
325
326
+ // also adds plugins from character file into the runtime
326
327
export async function initializeClients (
327
328
character : Character ,
328
329
runtime : IAgentRuntime
329
330
) {
330
- const clients = [ ] ;
331
- const clientTypes =
331
+ // each client can only register once
332
+ // and if we want two we can explicitly support it
333
+ const clients : Record < string , any > = { } ;
334
+ const clientTypes :string [ ] =
332
335
character . clients ?. map ( ( str ) => str . toLowerCase ( ) ) || [ ] ;
336
+ elizaLogger . log ( 'initializeClients' , clientTypes , 'for' , character . name )
333
337
334
338
if ( clientTypes . includes ( "auto" ) ) {
335
339
const autoClient = await AutoClientInterface . start ( runtime ) ;
336
- if ( autoClient ) clients . push ( autoClient ) ;
340
+ if ( autoClient ) clients . auto = autoClient ;
337
341
}
338
342
339
343
if ( clientTypes . includes ( "discord" ) ) {
340
- clients . push ( await DiscordClientInterface . start ( runtime ) ) ;
344
+ const discordClient = await DiscordClientInterface . start ( runtime ) ;
345
+ if ( discordClient ) clients . discord = discordClient ;
341
346
}
342
347
343
348
if ( clientTypes . includes ( "telegram" ) ) {
344
349
const telegramClient = await TelegramClientInterface . start ( runtime ) ;
345
- if ( telegramClient ) clients . push ( telegramClient ) ;
350
+ if ( telegramClient ) clients . telegram = telegramClient ;
346
351
}
347
352
348
353
if ( clientTypes . includes ( "twitter" ) ) {
349
354
TwitterClientInterface . enableSearch = ! isFalsish ( getSecret ( character , "TWITTER_SEARCH_ENABLE" ) ) ;
350
- const twitterClients = await TwitterClientInterface . start ( runtime ) ;
351
- clients . push ( twitterClients ) ;
355
+ const twitterClient = await TwitterClientInterface . start ( runtime ) ;
356
+ if ( twitterClient ) clients . twitter = twitterClient ;
352
357
}
353
358
354
359
if ( clientTypes . includes ( "farcaster" ) ) {
355
- const farcasterClients = new FarcasterAgentClient ( runtime ) ;
356
- farcasterClients . start ( ) ;
357
- clients . push ( farcasterClients ) ;
360
+ // why is this one different :(
361
+ const farcasterClient = new FarcasterAgentClient ( runtime ) ;
362
+ if ( farcasterClient ) {
363
+ farcasterClient . start ( ) ;
364
+ clients . farcaster = farcasterClient ;
365
+ }
358
366
}
359
367
368
+ elizaLogger . log ( 'client keys' , Object . keys ( clients ) ) ;
369
+
360
370
if ( character . plugins ?. length > 0 ) {
361
371
for ( const plugin of character . plugins ) {
372
+ // if plugin has clients, add those..
362
373
if ( plugin . clients ) {
363
374
for ( const client of plugin . clients ) {
364
375
clients . push ( await client . start ( runtime ) ) ;
@@ -387,7 +398,7 @@ function isFalsish(input: any): boolean {
387
398
}
388
399
389
400
function getSecret ( character : Character , secret : string ) {
390
- return character . settings . secrets ?. [ secret ] || process . env [ secret ] ;
401
+ return character . settings ? .secrets ?. [ secret ] || process . env [ secret ] ;
391
402
}
392
403
393
404
let nodePlugin : any | undefined ;
@@ -397,7 +408,7 @@ export async function createAgent(
397
408
db : IDatabaseAdapter ,
398
409
cache : ICacheManager ,
399
410
token : string
400
- ) {
411
+ ) : AgentRuntime {
401
412
elizaLogger . success (
402
413
elizaLogger . successesTitle ,
403
414
"Creating runtime for character" ,
@@ -430,6 +441,7 @@ export async function createAgent(
430
441
modelProvider : character . modelProvider ,
431
442
evaluators : [ ] ,
432
443
character,
444
+ // character.plugins are handled when clients are added
433
445
plugins : [
434
446
bootstrapPlugin ,
435
447
getSecret ( character , "CONFLUX_CORE_PRIVATE_KEY" )
@@ -500,7 +512,7 @@ function initializeDbCache(character: Character, db: IDatabaseCacheAdapter) {
500
512
return cache ;
501
513
}
502
514
503
- async function startAgent ( character : Character , directClient ) {
515
+ async function startAgent ( character : Character , directClient ) : AgentRuntime {
504
516
let db : IDatabaseAdapter & IDatabaseCacheAdapter ;
505
517
try {
506
518
character . id ??= stringToUuid ( character . name ) ;
@@ -519,15 +531,21 @@ async function startAgent(character: Character, directClient) {
519
531
await db . init ( ) ;
520
532
521
533
const cache = initializeDbCache ( character , db ) ;
522
- const runtime = await createAgent ( character , db , cache , token ) ;
534
+ const runtime : AgentRuntime = await createAgent ( character , db , cache , token ) ;
523
535
536
+ // start services/plugins/process knowledge
524
537
await runtime . initialize ( ) ;
525
538
526
- const clients = await initializeClients ( character , runtime ) ;
539
+ // start assigned clients
540
+ runtime . clients = await initializeClients ( character , runtime ) ;
527
541
542
+ // add to container
528
543
directClient . registerAgent ( runtime ) ;
529
544
530
- return clients ;
545
+ // report to console
546
+ elizaLogger . debug ( `Started ${ character . name } as ${ runtime . agentId } ` )
547
+
548
+ return runtime ;
531
549
} catch ( error ) {
532
550
elizaLogger . error (
533
551
`Error starting agent for character ${ character . name } :` ,
@@ -571,8 +589,8 @@ const startAgents = async () => {
571
589
} ) ;
572
590
}
573
591
574
- elizaLogger . log ( "Chat started. Type 'exit' to quit." ) ;
575
592
if ( ! args [ "non-interactive" ] ) {
593
+ elizaLogger . log ( "Chat started. Type 'exit' to quit." ) ;
576
594
chat ( ) ;
577
595
}
578
596
} ;
0 commit comments