feat: AI suggestion (#34)
## Feat * feat(ai): add AI suggestion engine, context gathering, and ghost text overlay (`v0.3.0`) ## Security & Perf * fix(ai): restrict background `--help` execution to a hardcoded command allowlist to prevent RCE * fix(root): defer context cancellation in goroutine to prevent resource leaks * perf(ai): implement LRU eviction and maximum size limit for `ProviderCache` * perf(ai): truncate command context output (`CommandContextProvider.Gather`) to 1000 characters to save tokens ## Bug Fixes * fix(ai): add mutex synchronization and thread-safe snapshotting to `AIEngine.RegisterProvider` and `GatherDynamicContext` * fix(ai): return a copy of fresh `Suggestion` in `AIEngine.Suggest` to prevent cache mutation * fix(ai): use length-prefixed encoding in `EnvSnapshot.Hash` to prevent delimiter collisions * fix(ai): skip variable assignment lines containing `=` when extracting Makefile targets * fix(runner): check `scanner.Err()` and return `nil` on scan errors in justfile generator * test(ci): rename `git commit` to `git checkout` in overlay test to resolve CI `typos` false positive
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/versenilvis/iris/ai"
|
||||
"github.com/versenilvis/iris/config"
|
||||
"github.com/versenilvis/iris/root"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
fmt.Printf("Error loading config: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
config.Init(cfg)
|
||||
|
||||
fmt.Printf("AI Enabled: %v\n", cfg.AI.Enabled)
|
||||
fmt.Printf("Active Provider: %q\n", cfg.AI.Provider)
|
||||
|
||||
pCfg, ok := cfg.AI.GetActiveProvider()
|
||||
if !ok {
|
||||
fmt.Println("Error: active provider not found in config")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Endpoint: %s\n", pCfg.Endpoint)
|
||||
fmt.Printf("Model: %s\n", pCfg.Model)
|
||||
fmt.Printf("API Key Present: %v (len %d)\n", pCfg.GetAPIKey() != "", len(pCfg.GetAPIKey()))
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cwd, _ := os.Getwd()
|
||||
env := ai.EnvSnapshot{
|
||||
Cwd: cwd,
|
||||
LastCmd: "git status",
|
||||
LastExitCode: 0,
|
||||
}
|
||||
|
||||
engine := root.GetAIEngine()
|
||||
dynCtx := engine.GatherDynamicContext(ctx, "git commit -m \"", cwd)
|
||||
fmt.Printf("\n--- Dynamic Context Gathered by UniversalProvider ---\n%s\n-----------------------------------------------------\n", dynCtx)
|
||||
|
||||
fmt.Println("\nSending live test completion request to Groq for: \"git commit -m \" ...")
|
||||
start := time.Now()
|
||||
sugg, err := engine.Suggest(ctx, "git commit -m \"", env, dynCtx)
|
||||
duration := time.Since(start)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("AI Suggest Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if sugg == nil {
|
||||
fmt.Println("AI returned no suggestion (nil)")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
fmt.Printf("SUCCESS! Response received in %v:\n", duration)
|
||||
fmt.Printf(" Cmd: %s\n", sugg.Cmd)
|
||||
fmt.Printf(" Confidence: %d\n", sugg.Confidence)
|
||||
fmt.Printf(" Source: %s\n", sugg.Source)
|
||||
}
|
||||
Reference in New Issue
Block a user