|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + // Packages |
| 10 | + otel "github.com/mutablelogic/go-client/pkg/otel" |
| 11 | + httpclient "github.com/mutablelogic/go-llm/pkg/httpclient" |
| 12 | + schema "github.com/mutablelogic/go-llm/pkg/schema" |
| 13 | +) |
| 14 | + |
| 15 | +/////////////////////////////////////////////////////////////////////////////// |
| 16 | +// TYPES |
| 17 | + |
| 18 | +type GenerateCommands struct { |
| 19 | + Ask AskCommand `cmd:"" name:"ask" help:"Send a stateless text request to a model." group:"GENERATE"` |
| 20 | + Chat ChatCommand `cmd:"" name:"chat" help:"Send a message within a session (creates one if needed)." group:"GENERATE"` |
| 21 | + Embedding EmbeddingCommand `cmd:"" name:"embedding" help:"Generate embedding vectors from text." group:"GENERATE"` |
| 22 | +} |
| 23 | + |
| 24 | +type AskCommand struct { |
| 25 | + schema.AskRequest |
| 26 | + File []string `help:"Path or glob pattern for files to attach (may be repeated)" optional:""` |
| 27 | + URL string `help:"URL to attach as a reference" optional:""` |
| 28 | +} |
| 29 | + |
| 30 | +/////////////////////////////////////////////////////////////////////////////// |
| 31 | +// COMMANDS |
| 32 | + |
| 33 | +func (cmd *AskCommand) Run(ctx *Globals) (err error) { |
| 34 | + // Load defaults for model and provider when not explicitly set |
| 35 | + if cmd.AskRequest.Model == "" { |
| 36 | + cmd.AskRequest.Model = ctx.defaults.GetString("model") |
| 37 | + } |
| 38 | + if cmd.AskRequest.Provider == "" { |
| 39 | + cmd.AskRequest.Provider = ctx.defaults.GetString("provider") |
| 40 | + } |
| 41 | + if cmd.AskRequest.Model == "" { |
| 42 | + return fmt.Errorf("model is required (set with --model or store a default)") |
| 43 | + } |
| 44 | + |
| 45 | + // Store model and provider as defaults |
| 46 | + if err := ctx.defaults.Set("model", cmd.AskRequest.Model); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + if cmd.AskRequest.Provider != "" { |
| 50 | + if err := ctx.defaults.Set("provider", cmd.AskRequest.Provider); err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // When format is set, hint the model to reply in JSON |
| 56 | + if len(cmd.AskRequest.Format) > 0 { |
| 57 | + cmd.AskRequest.SystemPrompt = "Respond with valid JSON only. Do not include any other text or formatting.\n" + cmd.AskRequest.SystemPrompt |
| 58 | + } |
| 59 | + |
| 60 | + client, err := ctx.Client() |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + // OTEL |
| 66 | + parent, endSpan := otel.StartSpan(ctx.tracer, ctx.ctx, "AskCommand") |
| 67 | + defer func() { endSpan(err) }() |
| 68 | + |
| 69 | + // Build options |
| 70 | + var opts []httpclient.AskOpt |
| 71 | + for _, pattern := range cmd.File { |
| 72 | + matches, err := filepath.Glob(pattern) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("invalid glob pattern %q: %w", pattern, err) |
| 75 | + } |
| 76 | + if len(matches) == 0 { |
| 77 | + return fmt.Errorf("no files match %q", pattern) |
| 78 | + } |
| 79 | + for _, path := range matches { |
| 80 | + f, err := os.Open(path) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + defer f.Close() |
| 85 | + opts = append(opts, httpclient.WithFile(f.Name(), f)) |
| 86 | + } |
| 87 | + } |
| 88 | + if cmd.URL != "" { |
| 89 | + opts = append(opts, httpclient.WithURL(cmd.URL)) |
| 90 | + } |
| 91 | + |
| 92 | + // Send request |
| 93 | + response, err := client.Ask(parent, cmd.AskRequest, opts...) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + // Print |
| 99 | + if ctx.Debug { |
| 100 | + fmt.Println(response) |
| 101 | + } else { |
| 102 | + // Collect text and thinking from content blocks |
| 103 | + var text, thinking string |
| 104 | + for _, block := range response.Content { |
| 105 | + if block.Text != nil { |
| 106 | + text += *block.Text |
| 107 | + } |
| 108 | + if block.Thinking != nil { |
| 109 | + thinking += *block.Thinking |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // If format was set, try to pretty-print as indented JSON |
| 114 | + if len(cmd.AskRequest.Format) > 0 { |
| 115 | + var raw json.RawMessage |
| 116 | + if err := json.Unmarshal([]byte(text), &raw); err == nil { |
| 117 | + if indented, err := json.MarshalIndent(raw, "", " "); err == nil { |
| 118 | + fmt.Println(string(indented)) |
| 119 | + return nil |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Print thinking block if present |
| 125 | + if thinking != "" { |
| 126 | + label := "thinking" |
| 127 | + if isTerminal(os.Stdout) { |
| 128 | + label = "\033[2m" + label + "\033[0m" // dim |
| 129 | + thinking = "\033[2m" + thinking + "\033[0m" |
| 130 | + } |
| 131 | + fmt.Println(label + ": " + thinking) |
| 132 | + fmt.Println() |
| 133 | + } |
| 134 | + |
| 135 | + // Prepend role |
| 136 | + role := response.Role |
| 137 | + if role != "" { |
| 138 | + if isTerminal(os.Stdout) { |
| 139 | + role = "\033[1m" + role + "\033[0m" |
| 140 | + } |
| 141 | + text = role + ": " + text |
| 142 | + } |
| 143 | + fmt.Println(text) |
| 144 | + } |
| 145 | + return nil |
| 146 | +} |
0 commit comments