Skip to content

Commit 61270ca

Browse files
committed
perf: optimize header comparison and improve state synchronization
- Replace JSON.stringify with fast-deep-equal for better performance - Add missing dependency to useDebounce hook in ApiOptions - Improve header state synchronization in OpenAICompatible component - Use existing fast-deep-equal library for consistency across codebase
1 parent 01b6630 commit 61270ca

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

webview-ui/src/components/settings/ApiOptions.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { convertHeadersToObject } from "./utils/headers"
33
import { useDebounce } from "react-use"
44
import { VSCodeLink } from "@vscode/webview-ui-toolkit/react"
55
import { ExternalLinkIcon } from "@radix-ui/react-icons"
6+
import isEqual from "fast-deep-equal"
67

78
import {
89
type ProviderName,
@@ -116,7 +117,7 @@ const ApiOptions = ({
116117
useEffect(() => {
117118
const propHeaders = apiConfiguration?.openAiHeaders || {}
118119

119-
if (JSON.stringify(customHeaders) !== JSON.stringify(Object.entries(propHeaders))) {
120+
if (!isEqual(customHeaders, Object.entries(propHeaders))) {
120121
setCustomHeaders(Object.entries(propHeaders))
121122
}
122123
}, [apiConfiguration?.openAiHeaders, customHeaders])
@@ -131,7 +132,7 @@ const ApiOptions = ({
131132
const newHeadersObject = convertHeadersToObject(customHeaders)
132133

133134
// Only update if the processed object is different from the current config.
134-
if (JSON.stringify(currentConfigHeaders) !== JSON.stringify(newHeadersObject)) {
135+
if (!isEqual(currentConfigHeaders, newHeadersObject)) {
135136
setApiConfigurationField("openAiHeaders", newHeadersObject)
136137
}
137138
},
@@ -213,6 +214,7 @@ const ApiOptions = ({
213214
apiConfiguration?.lmStudioBaseUrl,
214215
apiConfiguration?.litellmBaseUrl,
215216
apiConfiguration?.litellmApiKey,
217+
apiConfiguration?.openAiHeaders,
216218
customHeaders,
217219
],
218220
)

webview-ui/src/components/settings/providers/OpenAICompatible.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { ExtensionMessage } from "@roo/ExtensionMessage"
1616

1717
import { useAppTranslation } from "@src/i18n/TranslationContext"
1818
import { Button, StandardTooltip } from "@src/components/ui"
19+
import deepEqual from "fast-deep-equal"
1920

2021
import { convertHeadersToObject } from "../utils/headers"
2122
import { inputEventTransform, noTransform } from "../transforms"
@@ -48,6 +49,12 @@ export const OpenAICompatible = ({
4849
return Object.entries(headers)
4950
})
5051

52+
// Sync local state with parent's headers when they change externally
53+
useEffect(() => {
54+
const headers = apiConfiguration?.openAiHeaders || {}
55+
setCustomHeaders(Object.entries(headers))
56+
}, [apiConfiguration?.openAiHeaders])
57+
5158
const handleAddCustomHeader = useCallback(() => {
5259
// Only update the local state to show the new row in the UI.
5360
setCustomHeaders((prev) => [...prev, ["", ""]])
@@ -91,7 +98,7 @@ export const OpenAICompatible = ({
9198
const newHeadersObject = convertHeadersToObject(customHeaders)
9299

93100
// Only update if the processed object is different from the current config
94-
if (JSON.stringify(currentConfigHeaders) !== JSON.stringify(newHeadersObject)) {
101+
if (!deepEqual(currentConfigHeaders, newHeadersObject)) {
95102
setApiConfigurationField("openAiHeaders", newHeadersObject)
96103
}
97104
}, 300)

0 commit comments

Comments
 (0)