77 lines
3.1 KiB
Go
77 lines
3.1 KiB
Go
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 {
|
|
ProbeOpenRouterKey(context.Context, 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) {
|
|
outcome, err := p.support(backend)
|
|
if err != nil || !outcome.supported || (outcome.applicable != nil && !*outcome.applicable) {
|
|
return outcome, err
|
|
}
|
|
result, requestErr := p.client.ProbeOpenRouterKey(ctx, 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
|
|
}
|
|
|
|
func (p authenticationProbe) support(backend domain.Backend) (authenticationProbeOutcome, error) {
|
|
if backend.SmokeTests == nil || backend.SmokeTests.Authentication == nil {
|
|
return authenticationProbeOutcome{reason: domain.SmokeTestReasonProbeNotConfigured}, nil
|
|
}
|
|
authentication := backend.SmokeTests.Authentication
|
|
if authentication.Kind == nil {
|
|
return authenticationProbeOutcome{}, fmt.Errorf("%w: authentication probe kind is missing", ErrInvalidRequest)
|
|
}
|
|
switch *authentication.Kind {
|
|
case "none":
|
|
return authenticationProbeOutcome{supported: true, applicable: boolPointer(false), reason: domain.SmokeTestReasonAuthenticationNotApplicable}, nil
|
|
case "openrouter_key":
|
|
if p.client == nil {
|
|
return authenticationProbeOutcome{reason: domain.SmokeTestReasonHTTPDiagnosticsUnavailable}, nil
|
|
}
|
|
return authenticationProbeOutcome{supported: true, applicable: boolPointer(true)}, nil
|
|
default:
|
|
return authenticationProbeOutcome{}, fmt.Errorf("%w: unsupported authentication probe kind %q", ErrInvalidRequest, *authentication.Kind)
|
|
}
|
|
}
|