Files
promptkit/internal/usecase/smoke_authentication.go

74 lines
2.9 KiB
Go
Raw Normal View History

package usecase
import (
"context"
"fmt"
"net/http"
"go.maximumdirect.net/promptkit/internal/domain"
"go.maximumdirect.net/promptkit/internal/generation"
"go.maximumdirect.net/promptkit/internal/llm"
)
type smokeAuthenticationClient interface {
ProbeAuthentication(context.Context, string, string, int, domain.ExecutionTarget) (llm.AuthenticationEvidence, error)
}
type authenticationProbe struct{ client smokeAuthenticationClient }
type authenticationProbeOutcome struct {
supported bool
applicable *bool
reason domain.SmokeTestReason
evidence llm.HTTPExecutionEvidence
credentialsAccepted *bool
failure domain.ExecutionFailureKind
}
func (p authenticationProbe) run(ctx context.Context, backend domain.Backend, target domain.SmokeTestTarget) (authenticationProbeOutcome, error) {
2026-10-13 17:48:21 +00:00
outcome, err := p.support(backend)
if err != nil || !outcome.supported || (outcome.applicable != nil && !*outcome.applicable) {
return outcome, err
}
adapter := llm.AuthenticationAdapter(backend.Provider, backend.SmokeTests)
result, requestErr := p.client.ProbeAuthentication(ctx, adapter, target.Endpoint, target.EffectiveSettings.TimeoutSeconds, target.EffectiveSettings)
outcome.evidence = result.HTTP
outcome.credentialsAccepted = result.Accepted
if result.Overflow {
outcome.reason = domain.SmokeTestReasonAuthenticationResponseLimitExceeded
outcome.failure = domain.ExecutionFailureOther
} else if result.Unrecognized {
outcome.reason = domain.SmokeTestReasonAuthenticationResponseUnrecognized
outcome.failure = domain.ExecutionFailureMalformedResponse
} else if requestErr != nil {
if !result.HTTP.DispatchInvoked {
return outcome, fmt.Errorf("execute authentication request: %w", requestErr)
}
outcome.failure = httpProbeFailure(ctx, requestErr)
} else if result.HTTP.ResponseReceived {
if result.HTTP.HTTPStatus != nil && *result.HTTP.HTTPStatus != http.StatusOK && *result.HTTP.HTTPStatus != http.StatusUnauthorized {
outcome.failure = generation.ClassifyFailure(llm.ErrUnexpectedStatus)
}
} else {
outcome.failure = httpProbeFailure(ctx, nil)
}
return outcome, nil
2026-10-13 17:48:21 +00:00
}
func (p authenticationProbe) support(backend domain.Backend) (authenticationProbeOutcome, error) {
adapter := llm.AuthenticationAdapter(backend.Provider, backend.SmokeTests)
if adapter == "" {
return authenticationProbeOutcome{reason: domain.SmokeTestReasonProbeNotConfigured}, nil
}
if adapter == "none" {
return authenticationProbeOutcome{supported: true, applicable: boolPointer(false), reason: domain.SmokeTestReasonAuthenticationNotApplicable}, nil
}
if !llm.SupportedAuthenticationAdapter(adapter) {
return authenticationProbeOutcome{reason: domain.SmokeTestReasonProbeNotConfigured}, nil
}
if p.client == nil {
return authenticationProbeOutcome{reason: domain.SmokeTestReasonHTTPDiagnosticsUnavailable}, nil
}
return authenticationProbeOutcome{supported: true, applicable: boolPointer(true)}, nil
}