98 lines
4.8 KiB
Go
98 lines
4.8 KiB
Go
package cli
|
|
|
|
import (
|
|
"text/template"
|
|
"time"
|
|
|
|
"go.maximumdirect.net/ckconfig"
|
|
"go.maximumdirect.net/ckprompt"
|
|
"go.maximumdirect.net/commandkit"
|
|
"go.maximumdirect.net/promptkit"
|
|
)
|
|
|
|
type runInput struct {
|
|
Selection ckconfig.Selection
|
|
PromptID string
|
|
Resources ckprompt.ResourceInput
|
|
Inputs ckprompt.PromptInputs
|
|
Execution ckprompt.ExecutionInput
|
|
Namespaces promptkit.Namespaces
|
|
OperationTimeout time.Duration
|
|
ReportPath commandkit.Supplied[string]
|
|
ReportFormat commandkit.Supplied[commandkit.Format]
|
|
}
|
|
|
|
var artifactTextTemplate = template.Must(commandkit.ParseTextTemplate("artifact", `{{printf "%s" .Artifact.Body}}`, nil))
|
|
|
|
func runDeclarations() []commandkit.Declaration {
|
|
resources := ckprompt.NewResourceFlags(func(input *runInput) *ckprompt.ResourceInput { return &input.Resources })
|
|
inputs := ckprompt.NewPromptInputFlags(func(input *runInput) *ckprompt.PromptInputs { return &input.Inputs })
|
|
execution := ckprompt.NewExecutionFlags(func(input *runInput) *ckprompt.ExecutionInput { return &input.Execution })
|
|
flags := configurationSelectors(func(input *runInput) *ckconfig.Selection { return &input.Selection })
|
|
flags = append(flags, resources.Profile, resources.SessionID)
|
|
flags = append(flags, inputs.All()...)
|
|
flags = append(flags, execution.All()...)
|
|
flags = append(flags, reportFlags()...)
|
|
flags = append(flags, commandkit.ValueFlag(commandkit.FlagMeta{Name: "run-timeout", Usage: "Whole-operation timeout as a Go duration."}, (*time.Duration)(nil), time.ParseDuration,
|
|
func(input *runInput, value commandkit.Supplied[time.Duration]) { input.OperationTimeout = value.Value }))
|
|
for _, namespace := range []struct {
|
|
name string
|
|
field func(*runInput) *string
|
|
}{
|
|
{"prompt", func(input *runInput) *string { return &input.Namespaces.Prompt }},
|
|
{"profile", func(input *runInput) *string { return &input.Namespaces.Profile }},
|
|
{"backend", func(input *runInput) *string { return &input.Namespaces.Backend }},
|
|
} {
|
|
flags = append(flags, commandkit.ValueFlag(commandkit.FlagMeta{Name: "llm-" + namespace.name + "-namespace", Usage: "Select the " + namespace.name + " namespace."}, (*string)(nil), parseString,
|
|
func(input *runInput, value commandkit.Supplied[string]) { *namespace.field(input) = value.Value }))
|
|
}
|
|
command := commandkit.NewCommand(commandkit.CommandSpec[runInput]{
|
|
Name: "run", Summary: "Run a stored prompt.", Flags: flags,
|
|
Arguments: []commandkit.Argument[runInput]{commandkit.ValueArgument(commandkit.ArgumentMeta{Name: "PROMPT_ID", Usage: "Stored prompt identifier."}, true, parseString,
|
|
func(input *runInput, value commandkit.Supplied[string]) { input.PromptID = value.Value })},
|
|
Run: runPrompt,
|
|
CheckInput: func(input runInput) error { return checkReportInput(input.ReportPath) },
|
|
})
|
|
declarations := []commandkit.Declaration{command}
|
|
for _, name := range []string{"profile", "session-id", "var", "input-text", "input-file", "endpoint", "model", "temperature", "max-tokens", "top-p", "timeout-seconds", "service-tier", "reasoning-effort", "api-key-env", "transient-retries", "extra-params", "prompt-namespace", "profile-namespace", "backend-namespace"} {
|
|
declarations = append(declarations, commandkit.FlagAliasFor("run", "--"+name, "--llm-"+name))
|
|
}
|
|
for _, name := range []string{"input", "llm-input"} {
|
|
declarations = append(declarations, commandkit.FlagAliasFor("run", "--"+name, "--llm-input-text"))
|
|
}
|
|
return declarations
|
|
}
|
|
|
|
func requestFromRunInput(input runInput) promptkit.RunRequest {
|
|
return promptkit.RunRequest{
|
|
PromptID: input.PromptID, ProfileID: input.Resources.Profile.Value, SessionID: input.Resources.SessionID.Value,
|
|
Inputs: input.Inputs.Inputs, Vars: input.Inputs.Vars, Execution: input.Execution.Execution,
|
|
TransientRetries: input.Execution.TransientRetries, OperationTimeout: input.OperationTimeout,
|
|
}
|
|
}
|
|
|
|
func runPrompt(invocation commandkit.Invocation, input runInput) commandkit.Outcome {
|
|
state := &acquisitionState{}
|
|
engine, err := loadEngine(invocation.Context, input.Selection, applicationInput{}, state)
|
|
acquired, err := attachApplicationEvidence(commandkit.Acquired[*promptkit.Engine]{Value: engine}, err, state, 0, false)
|
|
queueDiagnostics(invocation.Reporter(), acquired.Diagnostics)
|
|
if err != nil {
|
|
return requestFailure(err, acquired.Explanation)
|
|
}
|
|
inspection, err := engine.InspectConfiguration()
|
|
if err != nil {
|
|
return requestFailure(err, acquired.Explanation)
|
|
}
|
|
diagnostics, explanation := ckconfig.AdaptDiagnostics("PromptKit configuration:\n", inspection.Sources(), inspection.Diagnostics())
|
|
queueDiagnostics(invocation.Reporter(), diagnostics)
|
|
engine, err = engine.WithNamespaces(input.Namespaces)
|
|
if err != nil {
|
|
return requestFailure(err, explanation)
|
|
}
|
|
result, err := engine.Run(invocation.Context, requestFromRunInput(input))
|
|
if err != nil {
|
|
return requestFailure(err, explanation)
|
|
}
|
|
return deliverRunResult(invocation, input, result)
|
|
}
|