api key integration

This commit is contained in:
2026-09-24 11:01:16 +05:30
parent 697b77f8c1
commit 9698de32d5
9 changed files with 503 additions and 10 deletions

View File

@@ -103,6 +103,10 @@ type assistantService struct {
registry *tools.Registry
chat utils.Chat
agents map[string]Agent
// How fast one person may ask. Only questions are limited — approving a
// change the person has already read costs nothing and must not be the call
// that gets refused.
limit *askLimiter
}
// NewAssistantService takes its agents already loaded and validated.
@@ -111,7 +115,7 @@ type assistantService struct {
// should refuse to start, not quietly run a different assistant than the one
// its configuration describes.
func NewAssistantService(registry *tools.Registry, chat utils.Chat, agents map[string]Agent) AssistantService {
return &assistantService{registry: registry, chat: chat, agents: agents}
return &assistantService{registry: registry, chat: chat, agents: agents, limit: newAskLimiter(nil)}
}
func (s *assistantService) Available() bool { return s.chat != nil }
@@ -158,6 +162,12 @@ func (s *assistantService) Ask(ctx context.Context, agentName, question string,
if s.chat == nil {
return AssistantAnswer{}, utils.ErrChatNotConfigured
}
// Checked after the cheap refusals and before the paid one. An empty or
// oversized question should be told what is wrong with it rather than
// spending a token from an allowance it never needed.
if err := s.limit.allow(caller.Userid); err != nil {
return AssistantAnswer{}, err
}
agent, known := s.agents[agentName]
if !known {