Skip to content

Commit

Permalink
Ensure spells in spellbook remain on load.
Browse files Browse the repository at this point in the history
  • Loading branch information
parzival418 committed Aug 22, 2024
1 parent 88e1f1f commit 31faa47
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/server/agent-service/src/lib/spellbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class Spellbook<
* We use this to scale spell runners and to keep track of them.
*/
private eventMap: Map<string, Map<string, SpellCaster<A>>> = new Map()
private spells: Map<string, SpellInterface> = new Map()
spells: Map<string, SpellInterface> = new Map()

private commandHub: CommandHub<A>

Expand Down
30 changes: 26 additions & 4 deletions packages/server/agents/src/lib/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,35 @@ export class Agent
},
})
if (!spellsData.data.length) {
this.error(`No spells found for agent ${this.id} to load into spellbook.`)
this.warn(
`No spells found in database for agent ${this.id} to load into spellbook.`
)
this.warn('Current spells in spellbook: ', this.spellbook.spells)
return
}

const spells = spellsData.data
console.log('SPELLS', spells)
await this.spellbook.loadSpells(spells)
const loadedSpells = this.spellbook.spells
const databaseSpells = spellsData.data

// Combine loaded spells and database spells, deduplicating based on spell name
const combinedSpells = new Map()

// Add loaded spells to the map
loadedSpells.forEach(spell => {
combinedSpells.set(spell.name, spell)
})

// Add or update with database spells
databaseSpells.forEach((spell: SpellInterface) => {
combinedSpells.set(spell.name, spell)
})

// Convert the map back to an array
const spells = Array.from(combinedSpells.values())

this.logger.debug(
`Combined ${loadedSpells.size} loaded spells with ${databaseSpells.length} database spells, resulting in ${spells.length} unique spells.`
)

return this.spellbook.loadSpells(spells)
}
Expand Down

0 comments on commit 31faa47

Please sign in to comment.