|
| 1 | +import { useCallback, useState, useEffect, useRef } from "react" |
| 2 | +import { VSCodeTextField } from "@vscode/webview-ui-toolkit/react" |
| 3 | + |
| 4 | +import { |
| 5 | + type ProviderSettings, |
| 6 | + type OrganizationAllowList, |
| 7 | + type ExtensionMessage, |
| 8 | + poeDefaultModelId, |
| 9 | + type ProviderName, |
| 10 | +} from "@roo-code/types" |
| 11 | + |
| 12 | +import { RouterName } from "@roo/api" |
| 13 | + |
| 14 | +import { useAppTranslation } from "@src/i18n/TranslationContext" |
| 15 | +import { VSCodeButtonLink } from "@src/components/common/VSCodeButtonLink" |
| 16 | +import { useExtensionState } from "@src/context/ExtensionStateContext" |
| 17 | +import { vscode } from "@src/utils/vscode" |
| 18 | +import { Button } from "@src/components/ui" |
| 19 | + |
| 20 | +import { inputEventTransform } from "../transforms" |
| 21 | +import { ModelPicker } from "../ModelPicker" |
| 22 | +import { handleModelChangeSideEffects } from "../utils/providerModelConfig" |
| 23 | + |
| 24 | +type PoeProps = { |
| 25 | + apiConfiguration: ProviderSettings |
| 26 | + setApiConfigurationField: (field: keyof ProviderSettings, value: ProviderSettings[keyof ProviderSettings]) => void |
| 27 | + organizationAllowList: OrganizationAllowList |
| 28 | + modelValidationError?: string |
| 29 | + simplifySettings?: boolean |
| 30 | +} |
| 31 | + |
| 32 | +export const Poe = ({ |
| 33 | + apiConfiguration, |
| 34 | + setApiConfigurationField, |
| 35 | + organizationAllowList, |
| 36 | + modelValidationError, |
| 37 | + simplifySettings, |
| 38 | +}: PoeProps) => { |
| 39 | + const { t } = useAppTranslation() |
| 40 | + const { routerModels } = useExtensionState() |
| 41 | + const [refreshStatus, setRefreshStatus] = useState<"idle" | "loading" | "success" | "error">("idle") |
| 42 | + const [refreshError, setRefreshError] = useState<string | undefined>() |
| 43 | + const poeErrorJustReceived = useRef(false) |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + const handleMessage = (event: MessageEvent<ExtensionMessage>) => { |
| 47 | + const message = event.data |
| 48 | + if (message.type === "singleRouterModelFetchResponse" && !message.success) { |
| 49 | + const providerName = message.values?.provider as RouterName |
| 50 | + if (providerName === "poe") { |
| 51 | + poeErrorJustReceived.current = true |
| 52 | + setRefreshStatus("error") |
| 53 | + setRefreshError(message.error) |
| 54 | + } |
| 55 | + } else if (message.type === "routerModels") { |
| 56 | + if (refreshStatus === "loading") { |
| 57 | + if (!poeErrorJustReceived.current) { |
| 58 | + setRefreshStatus("success") |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + window.addEventListener("message", handleMessage) |
| 65 | + return () => { |
| 66 | + window.removeEventListener("message", handleMessage) |
| 67 | + } |
| 68 | + }, [refreshStatus]) |
| 69 | + |
| 70 | + const handleInputChange = useCallback( |
| 71 | + <K extends keyof ProviderSettings, E>( |
| 72 | + field: K, |
| 73 | + transform: (event: E) => ProviderSettings[K] = inputEventTransform, |
| 74 | + ) => |
| 75 | + (event: E | Event) => { |
| 76 | + setApiConfigurationField(field, transform(event as E)) |
| 77 | + }, |
| 78 | + [setApiConfigurationField], |
| 79 | + ) |
| 80 | + |
| 81 | + const handleRefreshModels = useCallback(() => { |
| 82 | + poeErrorJustReceived.current = false |
| 83 | + setRefreshStatus("loading") |
| 84 | + setRefreshError(undefined) |
| 85 | + |
| 86 | + const key = apiConfiguration.poeApiKey |
| 87 | + |
| 88 | + if (!key) { |
| 89 | + setRefreshStatus("error") |
| 90 | + setRefreshError(t("settings:providers.refreshModels.missingConfig")) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + vscode.postMessage({ |
| 95 | + type: "requestRouterModels", |
| 96 | + values: { poeApiKey: key, poeBaseUrl: apiConfiguration.poeBaseUrl }, |
| 97 | + }) |
| 98 | + }, [apiConfiguration, t]) |
| 99 | + |
| 100 | + return ( |
| 101 | + <> |
| 102 | + <VSCodeTextField |
| 103 | + value={apiConfiguration?.poeApiKey || ""} |
| 104 | + type="password" |
| 105 | + onInput={handleInputChange("poeApiKey")} |
| 106 | + placeholder={t("settings:placeholders.apiKey")} |
| 107 | + className="w-full"> |
| 108 | + <label className="block font-medium mb-1">{t("settings:providers.poeApiKey")}</label> |
| 109 | + </VSCodeTextField> |
| 110 | + <div className="text-sm text-vscode-descriptionForeground -mt-2"> |
| 111 | + {t("settings:providers.apiKeyStorageNotice")} |
| 112 | + </div> |
| 113 | + {!apiConfiguration?.poeApiKey && ( |
| 114 | + <VSCodeButtonLink href="https://poe.com/api_key" appearance="secondary"> |
| 115 | + {t("settings:providers.getPoeApiKey")} |
| 116 | + </VSCodeButtonLink> |
| 117 | + )} |
| 118 | + <Button |
| 119 | + variant="outline" |
| 120 | + onClick={handleRefreshModels} |
| 121 | + disabled={refreshStatus === "loading" || !apiConfiguration.poeApiKey}> |
| 122 | + <div className="flex items-center gap-2"> |
| 123 | + {refreshStatus === "loading" ? ( |
| 124 | + <span className="codicon codicon-loading codicon-modifier-spin" /> |
| 125 | + ) : ( |
| 126 | + <span className="codicon codicon-refresh" /> |
| 127 | + )} |
| 128 | + {t("settings:providers.refreshModels.label")} |
| 129 | + </div> |
| 130 | + </Button> |
| 131 | + {refreshStatus === "loading" && ( |
| 132 | + <div className="text-sm text-vscode-descriptionForeground"> |
| 133 | + {t("settings:providers.refreshModels.loading")} |
| 134 | + </div> |
| 135 | + )} |
| 136 | + {refreshStatus === "success" && ( |
| 137 | + <div className="text-sm text-vscode-foreground">{t("settings:providers.refreshModels.success")}</div> |
| 138 | + )} |
| 139 | + {refreshStatus === "error" && ( |
| 140 | + <div className="text-sm text-vscode-errorForeground"> |
| 141 | + {refreshError || t("settings:providers.refreshModels.error")} |
| 142 | + </div> |
| 143 | + )} |
| 144 | + <ModelPicker |
| 145 | + apiConfiguration={apiConfiguration} |
| 146 | + setApiConfigurationField={setApiConfigurationField} |
| 147 | + defaultModelId={poeDefaultModelId} |
| 148 | + models={routerModels?.poe ?? {}} |
| 149 | + modelIdKey="apiModelId" |
| 150 | + serviceName="Poe" |
| 151 | + serviceUrl="https://poe.com" |
| 152 | + organizationAllowList={organizationAllowList} |
| 153 | + errorMessage={modelValidationError} |
| 154 | + simplifySettings={simplifySettings} |
| 155 | + onModelChange={(modelId) => |
| 156 | + handleModelChangeSideEffects("poe" as ProviderName, modelId, setApiConfigurationField) |
| 157 | + } |
| 158 | + /> |
| 159 | + </> |
| 160 | + ) |
| 161 | +} |
0 commit comments