Skip to content

Commit 5d22156

Browse files
author
Daniel Duong
committed
fix(autorag/create): address coderabbit
1 parent 91c96d1 commit 5d22156

4 files changed

Lines changed: 51 additions & 10 deletions

File tree

packages/autorag/bff/internal/api/middleware.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,24 @@ func isValidDNS1123Label(label string) bool {
5050

5151
// getSecretDataCaseInsensitive performs a case-insensitive lookup in secret data.
5252
// Returns the value and true if found, empty string and false if not found.
53-
func getSecretDataCaseInsensitive(data map[string][]byte, key string) (string, bool) {
54-
lowerKey := strings.ToLower(key)
53+
func getSecretDataCaseInsensitive(data map[string][]byte, key string) (string, bool, error) {
54+
// Prefer exact key when present.
55+
if v, ok := data[key]; ok {
56+
return string(v), true, nil
57+
}
58+
59+
var matched string
60+
found := false
5561
for k, v := range data {
56-
if strings.ToLower(k) == lowerKey {
57-
return string(v), true
62+
if strings.EqualFold(k, key) {
63+
if found {
64+
return "", false, fmt.Errorf("ambiguous secret data: multiple keys match %q case-insensitively", key)
65+
}
66+
matched = string(v)
67+
found = true
5868
}
5969
}
60-
return "", false
70+
return matched, found, nil
6171
}
6272

6373
// isValidDNS1123Subdomain validates a string against DNS-1123 subdomain rules
@@ -372,8 +382,16 @@ func (app *App) AttachLlamaStackClientFromSecret(next func(http.ResponseWriter,
372382
}
373383

374384
// Extract LlamaStack credentials from secret data using case-insensitive key lookups.
375-
baseURL, foundBaseURL := getSecretDataCaseInsensitive(foundSecret.Data, "llama_stack_client_base_url")
376-
apiKey, foundAPIKey := getSecretDataCaseInsensitive(foundSecret.Data, "llama_stack_client_api_key")
385+
baseURL, foundBaseURL, err := getSecretDataCaseInsensitive(foundSecret.Data, "llama_stack_client_base_url")
386+
if err != nil {
387+
app.badRequestResponse(w, r, fmt.Errorf("invalid secret %q: %w", secretName, err))
388+
return
389+
}
390+
apiKey, foundAPIKey, err := getSecretDataCaseInsensitive(foundSecret.Data, "llama_stack_client_api_key")
391+
if err != nil {
392+
app.badRequestResponse(w, r, fmt.Errorf("invalid secret %q: %w", secretName, err))
393+
return
394+
}
377395

378396
if !foundBaseURL || baseURL == "" {
379397
app.badRequestResponse(w, r, fmt.Errorf("secret %q is missing or has empty value for required key: llama_stack_client_base_url", secretName))

packages/autorag/frontend/src/app/components/configure/AutoragExperimentSettings.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ const AutoragExperimentSettings: React.FC<AutoragExperimentSettingsProps> = ({
169169
<ModalFooter>
170170
<Button
171171
variant="primary"
172-
onClick={() => onClose()}
172+
onClick={onClose}
173173
isDisabled={!isDirty || hasFieldErrors}
174174
data-testid="experiment-settings-save"
175175
>

packages/autorag/frontend/src/app/hooks/mutations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useMutation, UseMutationResult } from '@tanstack/react-query';
22
import { handleRestFailures, isModArchResponse, restCREATE } from 'mod-arch-core';
3-
import z from 'zod';
3+
import * as z from 'zod';
44
import type { PipelineRun } from '~/app/types';
55
import { BFF_API_VERSION, URL_PREFIX } from '~/app/utilities/const';
66

packages/autorag/frontend/src/app/hooks/queries.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useQuery, UseQueryResult } from '@tanstack/react-query';
2+
import * as z from 'zod';
23
import { getLlamaStackModels } from '~/app/api/k8s';
34
import { LlamaStackModelsResponse, LlamaStackModelType } from '~/app/types';
45

@@ -9,7 +10,29 @@ export function useLlamaStackModelsQuery(
910
): UseQueryResult<LlamaStackModelsResponse, Error> {
1011
return useQuery({
1112
queryKey: ['models', namespace, secretName, modelType],
12-
queryFn: () => getLlamaStackModels('')(namespace, secretName)({}),
13+
enabled: !!namespace && !!secretName,
14+
queryFn: async () => {
15+
try {
16+
const response = await getLlamaStackModels('')(namespace, secretName)({});
17+
z.object({
18+
models: z.array(
19+
z.object({
20+
id: z.string(),
21+
type: z.union([z.literal('llm'), z.literal('embedding')]),
22+
provider: z.string(),
23+
// eslint-disable-next-line camelcase
24+
resource_path: z.string(),
25+
}),
26+
),
27+
}).parse(response);
28+
return response;
29+
} catch (error) {
30+
if (error instanceof z.ZodError) {
31+
throw new Error('Invalid llama stack models response');
32+
}
33+
throw error;
34+
}
35+
},
1336
select: modelType
1437
? (data) => ({ models: data.models.filter((m) => m.type === modelType) })
1538
: undefined,

0 commit comments

Comments
 (0)