65 lines
1.9 KiB
Go
65 lines
1.9 KiB
Go
package usecase
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"go.maximumdirect.net/promptkit/internal/domain"
|
|
)
|
|
|
|
type resolvedPromptDefinition struct {
|
|
definition *domain.PromptDefinition
|
|
hash string
|
|
}
|
|
|
|
func (r *Runner) resolvePromptDefinition(ctx context.Context, promptID string) (*resolvedPromptDefinition, error) {
|
|
if strings.TrimSpace(promptID) == "" {
|
|
return nil, fmt.Errorf("%w: prompt id is required", ErrInvalidRequest)
|
|
}
|
|
if r == nil || r.promptDefs == nil {
|
|
return nil, fmt.Errorf("%w: prompt repository is not configured", ErrPromptLoad)
|
|
}
|
|
|
|
definition, hash, err := r.promptDefs.GetPromptSnapshot(ctx, promptID)
|
|
if err != nil {
|
|
return nil, promptLookupError(err)
|
|
}
|
|
if definition == nil {
|
|
return nil, fmt.Errorf("%w: prompt repository returned nil definition", ErrPromptLoad)
|
|
}
|
|
|
|
return &resolvedPromptDefinition{definition: definition, hash: hash}, nil
|
|
}
|
|
|
|
func (r *Runner) resolvePromptInspection(ctx context.Context, promptID string) (*domain.PromptInspection, error) {
|
|
if strings.TrimSpace(promptID) == "" {
|
|
return nil, fmt.Errorf("%w: prompt id is required", ErrInvalidRequest)
|
|
}
|
|
if r == nil || r.promptDefs == nil {
|
|
return nil, fmt.Errorf("%w: prompt repository is not configured", ErrPromptLoad)
|
|
}
|
|
inspection, err := r.promptDefs.GetPromptInspection(ctx, promptID)
|
|
if err != nil {
|
|
return nil, promptLookupError(err)
|
|
}
|
|
if inspection == nil {
|
|
return nil, fmt.Errorf("%w: prompt repository returned nil inspection", ErrPromptLoad)
|
|
}
|
|
return inspection, nil
|
|
}
|
|
|
|
// InspectPrompt resolves one explicit prompt without execution work.
|
|
func (r *Runner) InspectPrompt(ctx context.Context, promptID string) (*domain.PromptInspection, error) {
|
|
if strings.TrimSpace(promptID) == "" {
|
|
return nil, fmt.Errorf("%w: prompt id is required", ErrInvalidRequest)
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, &OperationError{Category: ErrPromptLoad, Cause: ctx.Err()}
|
|
default:
|
|
}
|
|
|
|
return r.resolvePromptInspection(ctx, promptID)
|
|
}
|