Repository navigation
✨ ask: hub backend for optional Ask AI help #19200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
626f1bc
b34672b
04994d3
c0ad209
c0401a4
6b9fac6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Hub operators can enable an optional Ask AI help backend that answers docs questions server side through the Kapa.ai Chat API: per-person consent, credential-shaped questions refused, only allowlisted cited sources, per-person and hub-wide limits, a circuit breaker and content-free audit lines. Off by default and inert until a key file, project id and integration id are configured; no panel yet ([#19180](https://github.com/hivecommons/hive/issues/19180)). |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package askai | ||
|
|
||
| import ( | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| const ( | ||
| // DefaultBreakerFailures consecutive upstream failures open the breaker. | ||
| DefaultBreakerFailures = 3 | ||
| // DefaultBreakerCooldown is how long an open breaker short-circuits to | ||
| // the docs link before one trial call is let through. | ||
| DefaultBreakerCooldown = 5 * time.Minute | ||
| ) | ||
|
|
||
| // breakerEvent reports a state change for the audit line. | ||
| type breakerEvent int | ||
|
|
||
| const ( | ||
| breakerNoChange breakerEvent = iota | ||
| breakerOpened | ||
| breakerClosed | ||
| ) | ||
|
|
||
| // breaker is a consecutive-failure circuit breaker with a half-open trial. | ||
| type breaker struct { | ||
| mu sync.Mutex | ||
| threshold int | ||
| cooldown time.Duration | ||
| failures int | ||
| openUntil time.Time | ||
| open bool | ||
| trial bool | ||
| } | ||
|
|
||
| func newBreaker(threshold int, cooldown time.Duration) *breaker { | ||
| if threshold <= 0 { | ||
| threshold = DefaultBreakerFailures | ||
| } | ||
| if cooldown <= 0 { | ||
| cooldown = DefaultBreakerCooldown | ||
| } | ||
| return &breaker{threshold: threshold, cooldown: cooldown} | ||
| } | ||
|
|
||
| // allow reports whether a call may go out, and how long to wait if not. | ||
| // After the cool-down exactly one trial call is admitted at a time. | ||
| func (b *breaker) allow(now time.Time) (bool, time.Duration) { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| if !b.open { | ||
| return true, 0 | ||
| } | ||
| if now.Before(b.openUntil) { | ||
| return false, b.openUntil.Sub(now) | ||
| } | ||
| if b.trial { | ||
| return false, time.Second | ||
| } | ||
| b.trial = true | ||
| return true, 0 | ||
| } | ||
|
|
||
| // cancelTrial returns an admitted trial slot that never reached the vendor | ||
| // (for example, the request was rate limited after allow). | ||
| func (b *breaker) cancelTrial() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| b.trial = false | ||
| } | ||
|
|
||
| func (b *breaker) success() breakerEvent { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| wasOpen := b.open | ||
| b.failures, b.open, b.trial = 0, false, false | ||
| if wasOpen { | ||
| return breakerClosed | ||
| } | ||
| return breakerNoChange | ||
| } | ||
|
|
||
| func (b *breaker) failure(now time.Time) breakerEvent { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| b.failures++ | ||
| if b.open { | ||
| // A failed trial re-opens for another cool-down. | ||
| b.openUntil, b.trial = now.Add(b.cooldown), false | ||
| return breakerNoChange | ||
| } | ||
| if b.failures >= b.threshold { | ||
| b.open, b.openUntil, b.trial = true, now.Add(b.cooldown), false | ||
| return breakerOpened | ||
| } | ||
| return breakerNoChange | ||
| } | ||
|
|
||
| func (b *breaker) isOpen() bool { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| return b.open | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cancelTrial clears trial unconditionally, so a request that never took the trial slot can free another request's half-open trial. Return a 'took trial' bool from allow and cancel only then.