106 lines
2.5 KiB
Go
106 lines
2.5 KiB
Go
package reasoningmode
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
type Mode string
|
|
|
|
type ExecutionMode string
|
|
|
|
const (
|
|
ModeQuick Mode = "quick"
|
|
ModeReasoning Mode = "reasoning"
|
|
|
|
ExecutionQuick ExecutionMode = "quick"
|
|
ExecutionReasoning ExecutionMode = "reasoning"
|
|
ExecutionReasoningSingle ExecutionMode = "reasoning_single"
|
|
)
|
|
|
|
var ErrInvalidState = errors.New("invalid reasoning mode state")
|
|
|
|
type State struct {
|
|
Mode Mode
|
|
MaxRounds int
|
|
RoundsUsed int
|
|
HintEnabled bool
|
|
LastActivityAt time.Time
|
|
ExpiresAt time.Time
|
|
OnboardingShown bool
|
|
OnboardingShownAt time.Time
|
|
}
|
|
|
|
type Completion struct {
|
|
Next State
|
|
RoundNumber int
|
|
ShowHint bool
|
|
AutoClosed bool
|
|
ShowOnboarding bool
|
|
}
|
|
|
|
func DefaultState() State {
|
|
return State{Mode: ModeQuick, MaxRounds: 10, HintEnabled: true}
|
|
}
|
|
|
|
func EffectiveState(state State, now time.Time) (State, bool) {
|
|
if state.Mode != ModeReasoning || state.ExpiresAt.IsZero() || state.ExpiresAt.After(now) {
|
|
return state, false
|
|
}
|
|
state.Mode = ModeQuick
|
|
state.RoundsUsed = 0
|
|
state.ExpiresAt = time.Time{}
|
|
return state, true
|
|
}
|
|
|
|
func ApplySuccessfulTurn(state State, execution ExecutionMode, now time.Time, idleTimeout time.Duration) (Completion, error) {
|
|
if err := validateState(state); err != nil {
|
|
return Completion{}, err
|
|
}
|
|
if idleTimeout <= 0 {
|
|
return Completion{}, ErrInvalidState
|
|
}
|
|
if execution != ExecutionQuick && execution != ExecutionReasoning && execution != ExecutionReasoningSingle {
|
|
return Completion{}, ErrInvalidState
|
|
}
|
|
|
|
result := Completion{Next: state}
|
|
if !state.OnboardingShown {
|
|
result.ShowOnboarding = true
|
|
result.Next.OnboardingShown = true
|
|
result.Next.OnboardingShownAt = now
|
|
}
|
|
|
|
if execution != ExecutionReasoning || state.Mode != ModeReasoning {
|
|
return result, nil
|
|
}
|
|
|
|
result.RoundNumber = state.RoundsUsed + 1
|
|
result.Next.LastActivityAt = now
|
|
if result.RoundNumber >= state.MaxRounds {
|
|
result.AutoClosed = true
|
|
result.Next.Mode = ModeQuick
|
|
result.Next.RoundsUsed = 0
|
|
result.Next.ExpiresAt = time.Time{}
|
|
return result, nil
|
|
}
|
|
|
|
result.Next.RoundsUsed = result.RoundNumber
|
|
result.Next.ExpiresAt = now.Add(idleTimeout)
|
|
result.ShowHint = state.HintEnabled && state.MaxRounds-result.RoundNumber == 2
|
|
return result, nil
|
|
}
|
|
|
|
func validateState(state State) error {
|
|
if state.Mode != ModeQuick && state.Mode != ModeReasoning {
|
|
return ErrInvalidState
|
|
}
|
|
if state.MaxRounds < 1 || state.MaxRounds > 50 {
|
|
return ErrInvalidState
|
|
}
|
|
if state.RoundsUsed < 0 || state.RoundsUsed > state.MaxRounds {
|
|
return ErrInvalidState
|
|
}
|
|
return nil
|
|
}
|