Demonstrate automatic usage in composed pipeline
This commit is contained in:
@@ -345,6 +345,12 @@ and the command's outcome under core presentation rules.
|
||||
|
||||
## Stage 7: Demonstrate and verify normal consumer installation
|
||||
|
||||
**Complete.** The offline composed pipeline reports two executions, 28 tokens,
|
||||
and USD 0.0025 from the injected client. Consumer tests exercise both assembly
|
||||
paths and registration orders, explicit observer composition, empty suites,
|
||||
help/completion, failure, quiet, JSON, disabled reporting, and smoke aliases.
|
||||
The ckprompt and composed-consumer workspace suites pass with Go 1.27.4.
|
||||
|
||||
1. Extend the maintained composed consumer rather than add another consumer.
|
||||
Add an application-owned `pipeline run` command that loads its engine normally
|
||||
and performs two prompt executions with `Invocation.Context`. Keep accounting,
|
||||
|
||||
@@ -60,7 +60,8 @@ func (client *exampleLLMClient) Generate(context.Context, promptkit.GenerateRequ
|
||||
if client.calls != nil {
|
||||
(*client.calls)++
|
||||
}
|
||||
return &promptkit.GenerateResponse{Content: "Example response."}, nil
|
||||
cost := 0.00125
|
||||
return &promptkit.GenerateResponse{Content: "Example response.", Usage: promptkit.TokenUsage{PromptTokens: 10, CompletionTokens: 4, TotalTokens: 14, ReasoningTokens: 1, CachedTokens: 2, CacheWriteTokens: 0}, UsagePresence: &promptkit.TokenUsagePresence{PromptTokens: true, CompletionTokens: true, TotalTokens: true, ReasoningTokens: true, CachedTokens: true, CacheWriteTokens: true}, CostUSD: &cost}, nil
|
||||
}
|
||||
|
||||
type restoreInput struct {
|
||||
@@ -338,8 +339,25 @@ func newAppWithEngineLoader(
|
||||
return commandkit.Success()
|
||||
},
|
||||
})
|
||||
pipeline := commandkit.NewCommand(commandkit.CommandSpec[struct{}]{Name: "run", Summary: "Run a two-prompt planning pipeline.", Run: func(inv commandkit.Invocation, _ struct{}) commandkit.Outcome {
|
||||
engine, err := engineLoader(inv.Context, ckconfig.Selection{}, &inspectionInput{}, &runCache{llmClient: client})
|
||||
if err != nil {
|
||||
return commandkit.Failure("Could not load PromptKit engine.").WithCause(err)
|
||||
}
|
||||
results := make([]string, 0, 2)
|
||||
for _, topic := range []string{"planning", "review"} {
|
||||
result, err := engine.Run(inv.Context, promptkit.RunRequest{PromptID: "briefing", Inputs: map[string]promptkit.ArtifactRef{"topic": promptkit.Inline(topic)}, Vars: map[string]string{"mode": "", "topic": topic}})
|
||||
if err != nil {
|
||||
return commandkit.Failure("Pipeline prompt failed: " + err.Error()).WithCause(err)
|
||||
}
|
||||
results = append(results, result.RawOutput)
|
||||
}
|
||||
_ = inv.Reporter().Report(commandkit.Report{Value: results})
|
||||
return commandkit.Success()
|
||||
}})
|
||||
declarations := []commandkit.Declaration{
|
||||
configCommand,
|
||||
commandkit.Group("pipeline", "Run an application pipeline.", pipeline),
|
||||
commandkit.Completion("composed-example"),
|
||||
commandkit.Group("session", "Manage saved sessions.", restore),
|
||||
commandkit.Group("request", "Inspect application-owned request inputs.", requestInspect),
|
||||
|
||||
@@ -865,3 +865,143 @@ func TestComposedCampaignPromptID(t *testing.T) {
|
||||
}
|
||||
t.Fatalf("catalog lacks selected campaign prompt: %s", output.String())
|
||||
}
|
||||
|
||||
func TestPipelineAutomaticallyReportsNativeUsage(t *testing.T) {
|
||||
app, err := newApp()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
args []string
|
||||
summary bool
|
||||
quiet bool
|
||||
}{
|
||||
{"enabled", []string{"pipeline", "run", "--show-tokens"}, true, false},
|
||||
{"disabled", []string{"pipeline", "run", "--show-tokens=false"}, false, false},
|
||||
{"omitted", []string{"pipeline", "run"}, false, false},
|
||||
{"json", []string{"pipeline", "run", "--format=json", "--show-tokens"}, true, false},
|
||||
{"quiet", []string{"pipeline", "run", "--quiet", "--show-tokens"}, false, true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var out, diag bytes.Buffer
|
||||
result := app.Execute(context.Background(), tc.args, commandkit.Streams{Out: &out, Err: &diag})
|
||||
if result.Status != 0 || strings.Contains(diag.String(), "Observed PromptKit execution usage") != tc.summary {
|
||||
t.Fatalf("%#v out=%q err=%q", result, out.String(), diag.String())
|
||||
}
|
||||
if tc.summary && (!strings.Contains(diag.String(), "Prompts: 2") || !strings.Contains(diag.String(), "Total tokens: 28") || !strings.Contains(diag.String(), "Cost: USD 0.0025 [provider]")) {
|
||||
t.Fatalf("summary=%q", diag.String())
|
||||
}
|
||||
if tc.name == "json" && !strings.Contains(out.String(), `"Example response."`) {
|
||||
t.Fatalf("json=%q", out.String())
|
||||
}
|
||||
if tc.quiet && out.Len() != 0 {
|
||||
t.Fatalf("quiet output=%q", out.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPipelineAcquisitionFailureStillReports(t *testing.T) {
|
||||
cause := errors.New("load failed")
|
||||
app, err := newAppWithEngineLoader(nil, func(context.Context, ckconfig.Selection, *inspectionInput, *runCache) (*promptkit.Engine, error) {
|
||||
return nil, cause
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var diag bytes.Buffer
|
||||
out := app.Execute(context.Background(), []string{"pipeline", "run", "--show-tokens"}, commandkit.Streams{Err: &diag})
|
||||
if !errors.Is(out.Cause, cause) || !strings.Contains(diag.String(), "Prompts: 0") || !strings.Contains(diag.String(), "ERROR:") {
|
||||
t.Fatalf("%#v %q", out, diag.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestObserverCompositionAndInstallationPaths(t *testing.T) {
|
||||
client := &exampleLLMClient{}
|
||||
loads := 0
|
||||
loader := func(ctx context.Context, selection ckconfig.Selection, input *inspectionInput, cache *runCache) (*promptkit.Engine, error) {
|
||||
loads++
|
||||
return loadPromptEngine(ctx, selection, input, cache)
|
||||
}
|
||||
suite := ckprompt.FromEngine(loader)
|
||||
for _, factory := range []string{"command", "declarations"} {
|
||||
for _, first := range []bool{true, false} {
|
||||
var explicit int
|
||||
command := commandkit.NewCommand(commandkit.CommandSpec[struct{}]{Name: "run", Run: func(inv commandkit.Invocation, _ struct{}) commandkit.Outcome {
|
||||
engine, err := loader(inv.Context, ckconfig.Selection{}, &inspectionInput{}, &runCache{llmClient: client})
|
||||
if err != nil {
|
||||
return commandkit.Failure("load").WithCause(err)
|
||||
}
|
||||
for range 2 {
|
||||
_, err = engine.Run(inv.Context, promptkit.RunRequest{PromptID: "briefing", Inputs: map[string]promptkit.ArtifactRef{"topic": promptkit.Inline("topic")}, Vars: map[string]string{"topic": "topic", "mode": ""}, Observer: func(promptkit.ExecutionEvent) { explicit++ }})
|
||||
if err != nil {
|
||||
return commandkit.Failure("run").WithCause(err)
|
||||
}
|
||||
}
|
||||
return commandkit.Success()
|
||||
}})
|
||||
var declarations []commandkit.Declaration
|
||||
if factory == "command" {
|
||||
declarations = []commandkit.Declaration{ckprompt.Command(suite)}
|
||||
} else {
|
||||
declarations = ckprompt.Declarations(suite)
|
||||
}
|
||||
var app *commandkit.App
|
||||
var err error
|
||||
if first {
|
||||
app, err = commandkit.New(commandkit.Identity{Name: "test"}, declarations...)
|
||||
if err == nil {
|
||||
err = app.Register(command)
|
||||
}
|
||||
} else {
|
||||
app, err = commandkit.New(commandkit.Identity{Name: "test"}, command)
|
||||
if err == nil {
|
||||
err = app.Register(declarations...)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := app.Register(commandkit.Completion("test")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before := loads
|
||||
for _, args := range [][]string{{"run", "--help"}, {"promptkit", "--help"}, {"__complete", "run", "--show"}} {
|
||||
app.Execute(context.Background(), args, commandkit.Streams{})
|
||||
if loads != before {
|
||||
t.Fatal("help acquired engine")
|
||||
}
|
||||
}
|
||||
var diag bytes.Buffer
|
||||
out := app.Execute(context.Background(), []string{"run", "--show-tokens"}, commandkit.Streams{Err: &diag})
|
||||
if out.Status != 0 || explicit == 0 || !strings.Contains(diag.String(), "Prompts: 2") || !strings.Contains(diag.String(), "Total tokens: 28") {
|
||||
t.Fatalf("%s %v: %#v explicit=%d %q", factory, first, out, explicit, diag.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
empty := ckprompt.Command(ckprompt.Suite[struct{}, struct{}]{})
|
||||
app, err := commandkit.New(commandkit.Identity{Name: "test"}, empty, commandkit.NewCommand(commandkit.CommandSpec[struct{}]{Name: "run", Run: func(commandkit.Invocation, struct{}) commandkit.Outcome { return commandkit.Success() }}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out := app.Execute(context.Background(), []string{"run", "--show-tokens"}, commandkit.Streams{}); out.Kind != commandkit.OutcomeUsage {
|
||||
t.Fatalf("empty suite installed flag: %#v", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSmokeUsageIsSeparateAndAliasAware(t *testing.T) {
|
||||
app, err := newAppWithEngineLoader(nil, loadPromptEngine, func(_ context.Context, request ckprompt.SmokeRequest, _ *inspectionInput, _ *runCache) (commandkit.Acquired[*promptkit.SmokeTestReport], error) {
|
||||
return commandkit.Acquired[*promptkit.SmokeTestReport]{Value: &promptkit.SmokeTestReport{Kind: request.Request.Kind, Supported: true}}, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, args := range [][]string{{"promptkit", "test", "authentication", "--show-tokens"}, {"smoke", "authentication", "--show-tokens"}} {
|
||||
var out, diag bytes.Buffer
|
||||
result := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diag})
|
||||
if result.Status != 0 || !strings.Contains(diag.String(), "Prompts: 0") || !strings.Contains(diag.String(), "Smoke-test usage is reported separately") || !strings.Contains(out.String(), "authentication") {
|
||||
t.Fatalf("%v: %#v %q %q", args, result, out.String(), diag.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user