81 lines
2.7 KiB
Go
81 lines
2.7 KiB
Go
package usecase
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/promptkit/internal/domain"
|
|
"gitea.maximumdirect.net/eric/promptkit/internal/prompt"
|
|
)
|
|
|
|
// RequirementInspectionRequest selects a captured prompt and supplies the
|
|
// context used for dependency classification.
|
|
type RequirementInspectionRequest struct {
|
|
PromptID string
|
|
PromptVersion string
|
|
ProfileID string
|
|
Vars map[string]string
|
|
SessionID string
|
|
}
|
|
|
|
type requirementSnapshotRepository interface {
|
|
GetRequirementSnapshot(id, version string) (prompt.RequirementSnapshot, bool)
|
|
}
|
|
|
|
// InspectRequirements discovers prompt inputs and caller variables without
|
|
// loading artifacts or performing execution preparation.
|
|
func (r *Runner) InspectRequirements(
|
|
ctx context.Context,
|
|
req RequirementInspectionRequest,
|
|
) (*domain.RequirementInspection, error) {
|
|
if strings.TrimSpace(req.PromptID) == "" {
|
|
return nil, fmt.Errorf("%w: prompt id is required", ErrInvalidRequest)
|
|
}
|
|
directSessionID, err := domain.NormalizeSessionID(req.SessionID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: session_id: %v", ErrInvalidRequest, err)
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, fmt.Errorf("%w: %w", ErrPromptLoad, err)
|
|
}
|
|
|
|
selection, err := r.resolvePromptInspection(ctx, req.PromptID, req.PromptVersion)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, fmt.Errorf("%w: %w", ErrProfileLoad, err)
|
|
}
|
|
selectedProfileID := req.ProfileID
|
|
if strings.TrimSpace(selectedProfileID) == "" {
|
|
selectedProfileID = selection.DefaultProfileID
|
|
}
|
|
if strings.TrimSpace(selectedProfileID) == "" {
|
|
return nil, fmt.Errorf("%w: %w: profile id is required either in request or prompt default_profile", ErrInvalidRequest, ErrProfileRequired)
|
|
}
|
|
profileIdentity, err := r.resolveProfileIdentity(ctx, selectedProfileID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, fmt.Errorf("%w: %w", ErrPromptLoad, err)
|
|
}
|
|
|
|
repository, ok := r.promptDefs.(requirementSnapshotRepository)
|
|
if !ok {
|
|
return nil, fmt.Errorf("%w: prompt repository does not provide captured requirement snapshots", ErrPromptLoad)
|
|
}
|
|
snapshot, ok := repository.GetRequirementSnapshot(selection.PromptID, selection.PromptVersion)
|
|
if !ok {
|
|
return nil, fmt.Errorf("%w: captured requirement snapshot is unavailable", ErrPromptLoad)
|
|
}
|
|
definition := &domain.PromptDefinition{ID: selection.PromptID, Version: selection.PromptVersion}
|
|
inspection, err := prompt.AnalyzeRequirements(ctx, definition, snapshot, req.Vars, directSessionID != "")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: %w", ErrPromptLoad, err)
|
|
}
|
|
inspection.ProfileID = profileIdentity
|
|
return &inspection, nil
|
|
}
|