Skip to content

Commit

Permalink
Fix default bot (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
crspeller authored May 28, 2024
1 parent f2c01c2 commit 99cac14
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Req

func (p *Plugin) aiBotRequired(c *gin.Context) {
botUsername := c.DefaultQuery("botUsername", p.getConfiguration().DefaultBotName)
bot := p.GetBotByUsername(botUsername)
bot := p.GetBotByUsernameOrFirst(botUsername)
if bot == nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to get bot: %s", botUsername))
return
Expand Down
16 changes: 16 additions & 0 deletions server/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,22 @@ func (p *Plugin) GetBotByUsername(botUsername string) *Bot {
return nil
}

// GetBotByUsernameOrFirst retrieves the bot associated with the given bot username or the first bot if not found
func (p *Plugin) GetBotByUsernameOrFirst(botUsername string) *Bot {
bot := p.GetBotByUsername(botUsername)
if bot != nil {
return bot
}

p.botsLock.RLock()
defer p.botsLock.RUnlock()
if len(p.bots) > 0 {
return p.bots[0]
}

return nil
}

// GetBotByID retrieves the bot associated with the given bot ID
func (p *Plugin) GetBotByID(botID string) *Bot {
p.botsLock.RLock()
Expand Down
9 changes: 8 additions & 1 deletion webapp/src/components/system_console/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,14 @@ const Config = (props: Props) => {
>
<Bots
bots={props.value.bots ?? []}
onChange={(bots: LLMBotConfig[]) => props.onChange(props.id, {...value, bots})}
onChange={(bots: LLMBotConfig[]) => {
if (value.bots.findIndex((bot) => bot.name === value.defaultBotName) === -1) {
props.onChange(props.id, {...value, bots, defaultBotName: bots[0].name});
} else {
props.onChange(props.id, {...value, bots});
}
props.setSaveNeeded();
}}
botChangedAvatar={botChangedAvatar}
/>
<PanelFooterText>
Expand Down

0 comments on commit 99cac14

Please sign in to comment.