mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-11-06 00:54:23 +00:00
Enhance command parsing in ParseLlamaCommand and improve error handling in ParseCommandRequest
This commit is contained in:
@@ -2,6 +2,7 @@ package llamacpp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -30,79 +31,102 @@ func ParseLlamaCommand(command string) (*LlamaServerOptions, error) {
|
|||||||
|
|
||||||
// 3. Parse arguments into map
|
// 3. Parse arguments into map
|
||||||
options := make(map[string]any)
|
options := make(map[string]any)
|
||||||
|
|
||||||
|
// Known multi-valued flags (snake_case form)
|
||||||
|
multiValued := map[string]struct{}{
|
||||||
|
"override_tensor": {},
|
||||||
|
"override_kv": {},
|
||||||
|
"lora": {},
|
||||||
|
"lora_scaled": {},
|
||||||
|
"control_vector": {},
|
||||||
|
"control_vector_scaled": {},
|
||||||
|
"dry_sequence_breaker": {},
|
||||||
|
"logit_bias": {},
|
||||||
|
}
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
for i < len(args) {
|
for i < len(args) {
|
||||||
arg := args[i]
|
arg := args[i]
|
||||||
|
|
||||||
// Skip non-flag arguments
|
if !strings.HasPrefix(arg, "-") { // skip positional / stray values
|
||||||
if !strings.HasPrefix(arg, "-") {
|
|
||||||
i++
|
i++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle --flag=value format
|
// Reject malformed flags with more than two leading dashes (e.g. ---model) to surface user mistakes
|
||||||
|
if strings.HasPrefix(arg, "---") {
|
||||||
|
return nil, fmt.Errorf("malformed flag: %s", arg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unified parsing for --flag=value vs --flag value
|
||||||
|
var rawFlag, rawValue string
|
||||||
|
hasEquals := false
|
||||||
if strings.Contains(arg, "=") {
|
if strings.Contains(arg, "=") {
|
||||||
parts := strings.SplitN(arg, "=", 2)
|
parts := strings.SplitN(arg, "=", 2)
|
||||||
flag := strings.TrimPrefix(parts[0], "-")
|
rawFlag = parts[0]
|
||||||
flag = strings.TrimPrefix(flag, "-")
|
rawValue = parts[1] // may be empty string
|
||||||
|
hasEquals = true
|
||||||
// Convert flag from kebab-case to snake_case for consistency with JSON field names
|
|
||||||
flagName := strings.ReplaceAll(flag, "-", "_")
|
|
||||||
|
|
||||||
// Convert value to appropriate type
|
|
||||||
value := parseValue(parts[1])
|
|
||||||
|
|
||||||
// Handle array flags by checking if flag already exists
|
|
||||||
if existingValue, exists := options[flagName]; exists {
|
|
||||||
// Convert to array if not already
|
|
||||||
switch existing := existingValue.(type) {
|
|
||||||
case []string:
|
|
||||||
options[flagName] = append(existing, parts[1])
|
|
||||||
case string:
|
|
||||||
options[flagName] = []string{existing, parts[1]}
|
|
||||||
default:
|
|
||||||
options[flagName] = []string{fmt.Sprintf("%v", existing), parts[1]}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
options[flagName] = value
|
rawFlag = arg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
flagCore := strings.TrimPrefix(strings.TrimPrefix(rawFlag, "-"), "-")
|
||||||
|
flagName := strings.ReplaceAll(flagCore, "-", "_")
|
||||||
|
|
||||||
|
// Detect value if not in equals form
|
||||||
|
valueProvided := hasEquals
|
||||||
|
if !hasEquals {
|
||||||
|
if i+1 < len(args) && !isFlag(args[i+1]) { // next token is value
|
||||||
|
rawValue = args[i+1]
|
||||||
|
valueProvided = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine if multi-valued flag
|
||||||
|
_, isMulti := multiValued[flagName]
|
||||||
|
|
||||||
|
// Normalization helper: ensure slice for multi-valued flags
|
||||||
|
appendValue := func(valStr string) {
|
||||||
|
if existing, ok := options[flagName]; ok {
|
||||||
|
// Existing value; ensure slice semantics for multi-valued flags or repeated occurrences
|
||||||
|
if slice, ok := existing.([]string); ok {
|
||||||
|
options[flagName] = append(slice, valStr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Convert scalar to slice
|
||||||
|
options[flagName] = []string{fmt.Sprintf("%v", existing), valStr}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// First value
|
||||||
|
if isMulti {
|
||||||
|
options[flagName] = []string{valStr}
|
||||||
|
} else {
|
||||||
|
// We'll parse type below for single-valued flags
|
||||||
|
options[flagName] = valStr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if valueProvided {
|
||||||
|
// Use raw token for multi-valued flags; else allow typed parsing
|
||||||
|
appendValue(rawValue)
|
||||||
|
if !isMulti { // convert to typed value if scalar
|
||||||
|
if strVal, ok := options[flagName].(string); ok { // still scalar
|
||||||
|
options[flagName] = parseValue(strVal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Advance index: if we consumed a following token as value (non equals form), skip it
|
||||||
|
if !hasEquals && i+1 < len(args) && rawValue == args[i+1] {
|
||||||
|
i += 2
|
||||||
|
} else {
|
||||||
i++
|
i++
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle --flag value format
|
// Boolean flag (no value)
|
||||||
flag := strings.TrimPrefix(arg, "-")
|
|
||||||
flag = strings.TrimPrefix(flag, "-")
|
|
||||||
|
|
||||||
// Convert flag from kebab-case to snake_case for consistency with JSON field names
|
|
||||||
flagName := strings.ReplaceAll(flag, "-", "_")
|
|
||||||
|
|
||||||
// Check if next arg is a value (not a flag)
|
|
||||||
// Special case: allow negative numbers as values
|
|
||||||
if i+1 < len(args) && !isFlag(args[i+1]) {
|
|
||||||
value := parseValue(args[i+1])
|
|
||||||
|
|
||||||
// Handle array flags by checking if flag already exists
|
|
||||||
if existingValue, exists := options[flagName]; exists {
|
|
||||||
// Convert to array if not already
|
|
||||||
switch existing := existingValue.(type) {
|
|
||||||
case []string:
|
|
||||||
options[flagName] = append(existing, args[i+1])
|
|
||||||
case string:
|
|
||||||
options[flagName] = []string{existing, args[i+1]}
|
|
||||||
default:
|
|
||||||
options[flagName] = []string{fmt.Sprintf("%v", existing), args[i+1]}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
options[flagName] = value
|
|
||||||
}
|
|
||||||
i += 2 // Skip flag and value
|
|
||||||
} else {
|
|
||||||
// Boolean flag
|
|
||||||
options[flagName] = true
|
options[flagName] = true
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 4. Convert to LlamaServerOptions using existing UnmarshalJSON
|
// 4. Convert to LlamaServerOptions using existing UnmarshalJSON
|
||||||
jsonData, err := json.Marshal(options)
|
jsonData, err := json.Marshal(options)
|
||||||
@@ -121,26 +145,28 @@ func ParseLlamaCommand(command string) (*LlamaServerOptions, error) {
|
|||||||
|
|
||||||
// parseValue attempts to parse a string value into the most appropriate type
|
// parseValue attempts to parse a string value into the most appropriate type
|
||||||
func parseValue(value string) any {
|
func parseValue(value string) any {
|
||||||
// Try to parse as boolean
|
// Surrounding matching quotes (single or double)
|
||||||
if strings.ToLower(value) == "true" {
|
if l := len(value); l >= 2 {
|
||||||
|
if (value[0] == '"' && value[l-1] == '"') || (value[0] == '\'' && value[l-1] == '\'') {
|
||||||
|
value = value[1 : l-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lower := strings.ToLower(value)
|
||||||
|
if lower == "true" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if strings.ToLower(value) == "false" {
|
if lower == "false" {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to parse as integer (handle negative numbers)
|
|
||||||
if intVal, err := strconv.Atoi(value); err == nil {
|
if intVal, err := strconv.Atoi(value); err == nil {
|
||||||
return intVal
|
return intVal
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to parse as float
|
|
||||||
if floatVal, err := strconv.ParseFloat(value, 64); err == nil {
|
if floatVal, err := strconv.ParseFloat(value, 64); err == nil {
|
||||||
return floatVal
|
return floatVal
|
||||||
}
|
}
|
||||||
|
return value
|
||||||
// Default to string (remove quotes if present)
|
|
||||||
return strings.Trim(value, `""`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// normalizeMultilineCommand handles multiline commands with backslashes
|
// normalizeMultilineCommand handles multiline commands with backslashes
|
||||||
@@ -234,7 +260,7 @@ func splitCommandTokens(command string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if inQuotes {
|
if inQuotes {
|
||||||
return nil, fmt.Errorf("unterminated quoted string")
|
return nil, errors.New("unterminated quoted string")
|
||||||
}
|
}
|
||||||
|
|
||||||
if current.Len() > 0 {
|
if current.Len() > 0 {
|
||||||
|
|||||||
@@ -370,3 +370,44 @@ func TestParseLlamaCommandUnslothExample(t *testing.T) {
|
|||||||
t.Errorf("expected api_key 'sk-1234567890abcdef', got '%s'", result.APIKey)
|
t.Errorf("expected api_key 'sk-1234567890abcdef', got '%s'", result.APIKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Focused additional edge case tests (kept minimal per guidance)
|
||||||
|
func TestParseLlamaCommandSingleQuotedValue(t *testing.T) {
|
||||||
|
cmd := "llama-server --model 'my model.gguf' --alias 'Test Alias'"
|
||||||
|
result, err := ParseLlamaCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if result.Model != "my model.gguf" {
|
||||||
|
t.Errorf("expected model 'my model.gguf', got '%s'", result.Model)
|
||||||
|
}
|
||||||
|
if result.Alias != "Test Alias" {
|
||||||
|
t.Errorf("expected alias 'Test Alias', got '%s'", result.Alias)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseLlamaCommandMixedArrayForms(t *testing.T) {
|
||||||
|
// Same multi-value flag using --flag value and --flag=value forms
|
||||||
|
cmd := "llama-server --lora adapter1.bin --lora=adapter2.bin --lora adapter3.bin"
|
||||||
|
result, err := ParseLlamaCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if len(result.Lora) != 3 {
|
||||||
|
t.Fatalf("expected 3 lora values, got %d (%v)", len(result.Lora), result.Lora)
|
||||||
|
}
|
||||||
|
expected := []string{"adapter1.bin", "adapter2.bin", "adapter3.bin"}
|
||||||
|
for i, v := range expected {
|
||||||
|
if result.Lora[i] != v {
|
||||||
|
t.Errorf("expected lora[%d]=%s got %s", i, v, result.Lora[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseLlamaCommandMalformedFlag(t *testing.T) {
|
||||||
|
cmd := "llama-server ---model test.gguf"
|
||||||
|
_, err := ParseLlamaCommand(cmd)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("expected error for malformed flag but got none")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -646,38 +646,41 @@ type ParseCommandRequest struct {
|
|||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param request body ParseCommandRequest true "Command to parse"
|
// @Param request body ParseCommandRequest true "Command to parse"
|
||||||
// @Success 200 {object} instance.CreateInstanceOptions "Parsed options"
|
// @Success 200 {object} instance.CreateInstanceOptions "Parsed options"
|
||||||
// @Failure 400 {string} string "Invalid request or command"
|
// @Failure 400 {object} map[string]string "Invalid request or command"
|
||||||
// @Failure 500 {string} string "Internal Server Error"
|
// @Failure 500 {object} map[string]string "Internal Server Error"
|
||||||
// @Router /backends/llama-cpp/parse-command [post]
|
// @Router /backends/llama-cpp/parse-command [post]
|
||||||
func (h *Handler) ParseLlamaCommand() http.HandlerFunc {
|
func (h *Handler) ParseLlamaCommand() http.HandlerFunc {
|
||||||
|
type errorResponse struct {
|
||||||
|
Error string `json:"error"`
|
||||||
|
Details string `json:"details,omitempty"`
|
||||||
|
}
|
||||||
|
writeError := func(w http.ResponseWriter, status int, code, details string) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(status)
|
||||||
|
_ = json.NewEncoder(w).Encode(errorResponse{Error: code, Details: details})
|
||||||
|
}
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var req ParseCommandRequest
|
var req ParseCommandRequest
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
writeError(w, http.StatusBadRequest, "invalid_request", "Invalid JSON body")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if strings.TrimSpace(req.Command) == "" {
|
||||||
if req.Command == "" {
|
writeError(w, http.StatusBadRequest, "invalid_command", "Command cannot be empty")
|
||||||
http.Error(w, "Command cannot be empty", http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the command using llamacpp parser
|
|
||||||
llamaOptions, err := llamacpp.ParseLlamaCommand(req.Command)
|
llamaOptions, err := llamacpp.ParseLlamaCommand(req.Command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to parse command: "+err.Error(), http.StatusBadRequest)
|
writeError(w, http.StatusBadRequest, "parse_error", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the full CreateInstanceOptions
|
|
||||||
options := &instance.CreateInstanceOptions{
|
options := &instance.CreateInstanceOptions{
|
||||||
BackendType: backends.BackendTypeLlamaCpp,
|
BackendType: backends.BackendTypeLlamaCpp,
|
||||||
LlamaServerOptions: llamaOptions,
|
LlamaServerOptions: llamaOptions,
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
if err := json.NewEncoder(w).Encode(options); err != nil {
|
if err := json.NewEncoder(w).Encode(options); err != nil {
|
||||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
writeError(w, http.StatusInternalServerError, "encode_error", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user