From dec04954e1bd0b86816387b323f9c8a55b671b67 Mon Sep 17 00:00:00 2001 From: Ed Zynda Date: Thu, 11 Jun 2026 14:50:18 +0300 Subject: [PATCH] pkg/kit: consolidate model-path helpers and argument tokenizer - ExtractModelFromPath mis-parsed model IDs containing '/' (e.g. 'openrouter/meta/llama' -> 'meta'); it now delegates to RemoveProviderFromModel and is deprecated alongside ExtractProviderFromPath (-> GetCurrentProvider) - parseFields delegated to prompts.ParseCommandArgs so extension argument parsing and builtin prompt-template parsing share one quote/escape grammar; ParseCommandArgs now also splits on tabs (superset of both previous tokenizers) --- internal/prompts/template.go | 8 ++--- pkg/kit/template_bridge.go | 60 ++++++++---------------------------- 2 files changed, 17 insertions(+), 51 deletions(-) diff --git a/internal/prompts/template.go b/internal/prompts/template.go index d5aac374..87810a2f 100644 --- a/internal/prompts/template.go +++ b/internal/prompts/template.go @@ -70,7 +70,8 @@ func ParseTemplate(path string) (*PromptTemplate, error) { } // ParseCommandArgs splits a command line into arguments respecting quotes. -// It handles single quotes, double quotes, and backslash escaping. +// It handles single quotes, double quotes, backslash escaping, and splits on +// spaces and tabs. func ParseCommandArgs(input string) []string { var args []string var current strings.Builder @@ -78,7 +79,7 @@ func ParseCommandArgs(input string) []string { inDoubleQuote := false escaped := false - for i, r := range input { + for _, r := range input { if escaped { current.WriteRune(r) escaped = false @@ -101,7 +102,7 @@ func ParseCommandArgs(input string) []string { continue } - if r == ' ' && !inSingleQuote && !inDoubleQuote { + if (r == ' ' || r == '\t') && !inSingleQuote && !inDoubleQuote { if current.Len() > 0 { args = append(args, current.String()) current.Reset() @@ -110,7 +111,6 @@ func ParseCommandArgs(input string) []string { } current.WriteRune(r) - _ = i // silence unused warning when we need position later } if current.Len() > 0 { diff --git a/pkg/kit/template_bridge.go b/pkg/kit/template_bridge.go index 98307b11..9373dcf3 100644 --- a/pkg/kit/template_bridge.go +++ b/pkg/kit/template_bridge.go @@ -7,6 +7,7 @@ import ( "github.com/mark3labs/kit/internal/extensions" "github.com/mark3labs/kit/internal/models" + "github.com/mark3labs/kit/internal/prompts" ) // --------------------------------------------------------------------------- @@ -183,44 +184,12 @@ func SimpleParseArguments(input string, count int) []string { return result } -// parseFields splits input respecting quoted strings. +// parseFields splits input into arguments respecting quoted strings and +// backslash escaping. It delegates to the canonical tokenizer in +// internal/prompts so extension argument parsing and builtin prompt-template +// parsing agree on grammar. func parseFields(input string) []string { - var fields []string - var current strings.Builder - inQuote := false - quoteChar := rune(0) - - for _, r := range input { - switch r { - case '"', '\'': - if !inQuote { - inQuote = true - quoteChar = r - } else if r == quoteChar { - inQuote = false - quoteChar = 0 - } else { - current.WriteRune(r) - } - case ' ', '\t': - if inQuote { - current.WriteRune(r) - } else { - if current.Len() > 0 { - fields = append(fields, current.String()) - current.Reset() - } - } - default: - current.WriteRune(r) - } - } - - if current.Len() > 0 { - fields = append(fields, current.String()) - } - - return fields + return prompts.ParseCommandArgs(input) } // EvaluateModelConditional checks if condition matches current model. @@ -417,21 +386,18 @@ func MatchModelGlob(model, pattern string) bool { } // ExtractProviderFromPath extracts provider from a path-like model string. +// +// Deprecated: Use GetCurrentProvider instead. func ExtractProviderFromPath(model string) string { - parts := strings.Split(model, "/") - if len(parts) >= 2 { - return parts[0] - } - return "" + return GetCurrentProvider(model) } // ExtractModelFromPath extracts model ID from a path-like model string. +// +// Deprecated: Use RemoveProviderFromModel instead, which correctly handles +// model IDs containing "/" (e.g. "openrouter/meta/llama"). func ExtractModelFromPath(model string) string { - parts := strings.Split(model, "/") - if len(parts) >= 2 { - return parts[1] - } - return model + return RemoveProviderFromModel(model) } // IsBareModelID checks if a string is a bare model ID (no provider).