Skip to content

Commit b4b67e0

Browse files
authored
refactor: function argument parsing using named regex (mudler#4708)
Signed-off-by: Maximilian Kenfenheuer <maximilian.kenfenheuer@ksol.it>
1 parent 91e1ff5 commit b4b67e0

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

pkg/functions/parse.go

+27-28
Original file line numberDiff line numberDiff line change
@@ -331,37 +331,36 @@ func ParseFunctionCall(llmresult string, functionConfig FunctionsConfig) []FuncC
331331
}
332332

333333
func ParseFunctionCallArgs(functionArguments string, functionConfig FunctionsConfig) string {
334-
if len(functionConfig.ArgumentRegex) > 0 {
335-
// We use named regexes here to extract the function argument key value pairs and convert this to valid json.
336-
// TODO: there might be responses where an object as a value is expected/required. This is currently not handled.
337-
args := make(map[string]string)
338-
339-
agrsRegexKeyName := "key"
340-
agrsRegexValueName := "value"
334+
if len(functionConfig.ArgumentRegex) == 0 {
335+
return functionArguments
336+
}
341337

342-
if functionConfig.ArgumentRegexKey != "" {
343-
agrsRegexKeyName = functionConfig.ArgumentRegexKey
344-
}
345-
if functionConfig.ArgumentRegexValue != "" {
346-
agrsRegexValueName = functionConfig.ArgumentRegexValue
347-
}
338+
// We use named regexes here to extract the function argument key value pairs and convert this to valid json.
339+
// TODO: there might be responses where an object as a value is expected/required. This is currently not handled.
340+
args := make(map[string]string)
348341

349-
for _, r := range functionConfig.ArgumentRegex {
350-
var respRegex = regexp.MustCompile(r)
351-
var nameRange []string = respRegex.SubexpNames()
352-
var keyIndex = slices.Index(nameRange, agrsRegexKeyName)
353-
var valueIndex = slices.Index(nameRange, agrsRegexValueName)
354-
matches := respRegex.FindAllStringSubmatch(functionArguments, -1)
355-
for _, match := range matches {
356-
args[match[keyIndex]] = match[valueIndex]
357-
}
358-
}
342+
agrsRegexKeyName := "key"
343+
agrsRegexValueName := "value"
359344

360-
jsonBytes, _ := json.Marshal(args)
361-
jsonString := string(jsonBytes)
345+
if functionConfig.ArgumentRegexKey != "" {
346+
agrsRegexKeyName = functionConfig.ArgumentRegexKey
347+
}
348+
if functionConfig.ArgumentRegexValue != "" {
349+
agrsRegexValueName = functionConfig.ArgumentRegexValue
350+
}
362351

363-
return jsonString
364-
} else {
365-
return functionArguments
352+
for _, r := range functionConfig.ArgumentRegex {
353+
var respRegex = regexp.MustCompile(r)
354+
var nameRange []string = respRegex.SubexpNames()
355+
var keyIndex = slices.Index(nameRange, agrsRegexKeyName)
356+
var valueIndex = slices.Index(nameRange, agrsRegexValueName)
357+
matches := respRegex.FindAllStringSubmatch(functionArguments, -1)
358+
for _, match := range matches {
359+
args[match[keyIndex]] = match[valueIndex]
360+
}
366361
}
362+
363+
jsonBytes, _ := json.Marshal(args)
364+
365+
return string(jsonBytes)
367366
}

0 commit comments

Comments
 (0)