From 6c3ffe4aa39016dbd155da5194c32fa0b48e314b Mon Sep 17 00:00:00 2001 From: Sean Pryor Date: Wed, 18 Dec 2024 11:55:32 -0500 Subject: [PATCH] Add in proper handling of spaces and quotes in ISVC parsing This adds a rudimentary parser that should handle most cases for inferenceservice argument parsing. Note, I'm not a JS expert, so it's possible there's some degerenate edge case with this, but in testing it worked fine --- frontend/src/api/k8s/inferenceServices.ts | 33 +++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/frontend/src/api/k8s/inferenceServices.ts b/frontend/src/api/k8s/inferenceServices.ts index fded27ccbc..3fbff23997 100644 --- a/frontend/src/api/k8s/inferenceServices.ts +++ b/frontend/src/api/k8s/inferenceServices.ts @@ -45,6 +45,13 @@ export const assembleInferenceService = ( const dataConnectionKey = secretKey || dataConnection; const nonEmptyArgs = servingRuntimeArgs?.filter(Boolean) || []; + // If we have nonEmptyArgs, ensure that we properly handle separating args + let splitArgs: string[] = []; + if (nonEmptyArgs.length > 0) { + for (let i = 0; i < nonEmptyArgs.length; i++) { + splitArgs = splitArgs.concat(parseCommandLine(nonEmptyArgs[i])); + } + } const nonEmptyEnvVars = servingRuntimeEnvVars?.filter((ev) => ev.name) || []; const updateInferenceService: InferenceServiceKind = inferenceService @@ -87,7 +94,7 @@ export const assembleInferenceService = ( path, }, }), - args: nonEmptyArgs, + args: splitArgs, env: nonEmptyEnvVars, }, }, @@ -135,7 +142,7 @@ export const assembleInferenceService = ( path, }, }), - args: nonEmptyArgs, + args: splitArgs, env: nonEmptyEnvVars, }, }, @@ -325,3 +332,25 @@ export const deleteInferenceService = ( opts, ), ); + + +function parseCommandLine(input: string): string[] { + const args: string[] = []; + const regex = /(?:[^\s"']+|"[^"]*"|'[^']*')+/g; + let match: RegExpExecArray | null; + + while ((match = regex.exec(input)) !== null) { + let arg: string = match[0]; + + // Remove surrounding quotes if any + if (arg.startsWith('"') && arg.endsWith('"')) { + arg = arg.slice(1, -1).replace(/\\"/g, '"'); // Unescape double quotes + } else if (arg.startsWith("'") && arg.endsWith("'")) { + arg = arg.slice(1, -1); // Remove single quotes + } + + args.push(arg); + } + + return args; +} \ No newline at end of file