11package intent
22
33import (
4+ "bytes"
45 "context"
56 "encoding/json"
67 "fmt"
@@ -12,6 +13,9 @@ import (
1213// CommandContext is the function used to create exec.Cmd. Override in tests.
1314var CommandContext = exec .CommandContext
1415
16+ // MinConfidence is the minimum confidence score required to accept a classification.
17+ const MinConfidence = 0.5
18+
1519// Classify interprets a natural language query as a forge command.
1620func Classify (ctx context.Context , query string ) (* Result , error ) {
1721 if _ , err := exec .LookPath ("claude" ); err != nil {
@@ -21,19 +25,22 @@ func Classify(ctx context.Context, query string) (*Result, error) {
2125 ctx , cancel := context .WithTimeout (ctx , 30 * time .Second )
2226 defer cancel ()
2327
24- dc := GatherContext ()
28+ dc := GatherContext ("." )
2529 prompt := BuildPrompt (query , dc )
2630
2731 cmd := CommandContext (ctx , "claude" , "-p" , prompt , "--output-format" , "json" , "--max-tokens" , "256" )
28- out , err := cmd .CombinedOutput ()
32+ var stdout , stderr bytes.Buffer
33+ cmd .Stdout = & stdout
34+ cmd .Stderr = & stderr
35+ err := cmd .Run ()
2936 if err != nil {
3037 if ctx .Err () == context .DeadlineExceeded {
3138 return nil , fmt .Errorf ("%w: timed out after 30s" , ErrClassificationFailed )
3239 }
33- return nil , fmt .Errorf ("%w: %s" , ErrClassificationFailed , string ( out ))
40+ return nil , fmt .Errorf ("%w: %s" , ErrClassificationFailed , stderr . String ( ))
3441 }
3542
36- return parseResponse (string ( out ))
43+ return parseResponse (stdout . String ( ))
3744}
3845
3946// parseResponse extracts a Result from the claude CLI JSON output.
@@ -55,6 +62,10 @@ func parseResponse(raw string) (*Result, error) {
5562 return nil , fmt .Errorf ("%w: empty argv" , ErrClassificationFailed )
5663 }
5764
65+ if r .Confidence < MinConfidence {
66+ return nil , fmt .Errorf ("%w: confidence %.2f below threshold %.2f: %s" , ErrClassificationFailed , r .Confidence , MinConfidence , r .Reasoning )
67+ }
68+
5869 return & r , nil
5970}
6071
0 commit comments