88 lines
2.3 KiB
Go
88 lines
2.3 KiB
Go
package usecase
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"go.maximumdirect.net/promptkit/internal/domain"
|
|
)
|
|
|
|
type resolvedProfileSelection struct {
|
|
id string
|
|
profile *domain.ExecutionProfile
|
|
}
|
|
|
|
func (r *Runner) profileLookupID(profileID string) (string, error) {
|
|
if strings.TrimSpace(profileID) == "" {
|
|
return "", fmt.Errorf("%w: profile id is required", ErrInvalidRequest)
|
|
}
|
|
if r == nil || r.profiles == nil {
|
|
return "", fmt.Errorf("%w: profile repository is not configured", ErrProfileLoad)
|
|
}
|
|
return profileID, nil
|
|
}
|
|
|
|
func (r *Runner) resolveProfileSelection(
|
|
ctx context.Context,
|
|
profileID string,
|
|
) (*resolvedProfileSelection, error) {
|
|
lookupID, err := r.profileLookupID(profileID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
selectedProfile, err := r.profiles.GetProfile(ctx, lookupID)
|
|
if err != nil {
|
|
return nil, profileLookupError(err)
|
|
}
|
|
if selectedProfile == nil {
|
|
return nil, fmt.Errorf("%w: profile repository returned nil profile", ErrProfileLoad)
|
|
}
|
|
|
|
profileValue := *selectedProfile
|
|
if profileValue.ResolvedTarget == nil {
|
|
return nil, fmt.Errorf("%w: profile %q has no resolved target", ErrProfileLoad, lookupID)
|
|
}
|
|
|
|
return &resolvedProfileSelection{
|
|
id: lookupID,
|
|
profile: &profileValue,
|
|
}, nil
|
|
}
|
|
|
|
func (r *Runner) resolveProfileIdentity(ctx context.Context, profileID string) (string, error) {
|
|
lookupID, err := r.profileLookupID(profileID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
identity, err := r.profiles.GetProfileIdentity(ctx, lookupID)
|
|
if err != nil {
|
|
return "", profileLookupError(err)
|
|
}
|
|
return identity, nil
|
|
}
|
|
|
|
func normalizeResolvedExecutionTarget(target domain.ExecutionTarget) (domain.ExecutionTarget, error) {
|
|
target, err := normalizeResolvedExecutionSettings(target)
|
|
if err != nil {
|
|
return domain.ExecutionTarget{}, err
|
|
}
|
|
if strings.TrimSpace(target.Model) == "" {
|
|
return domain.ExecutionTarget{}, errors.New("execution model is required")
|
|
}
|
|
return target, nil
|
|
}
|
|
|
|
func normalizeResolvedExecutionSettings(target domain.ExecutionTarget) (domain.ExecutionTarget, error) {
|
|
endpoint, err := domain.NormalizeOpenAICompatibleBaseEndpoint(target.Endpoint)
|
|
if err != nil {
|
|
return domain.ExecutionTarget{}, fmt.Errorf("execution endpoint: %w", err)
|
|
}
|
|
target.Endpoint = endpoint
|
|
if err := domain.ValidateExecutionTargetSettings(target); err != nil {
|
|
return domain.ExecutionTarget{}, err
|
|
}
|
|
return target, nil
|
|
}
|