Skip to content

Commit

Permalink
Merge branch 'checkpoint-2-vault-webui-and-api-integration' of https:…
Browse files Browse the repository at this point in the history
…//github.com/Agenta-AI/agenta into checkpoint-2-vault-webui-and-api-integration
  • Loading branch information
bekossy committed Dec 17, 2024
2 parents 6108cba + ddfcd02 commit bff72ab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
9 changes: 3 additions & 6 deletions agenta-web/src/components/pages/settings/Secrets/Secrets.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {useVaultSecret} from "@/hooks/useVaultSecret"
import {getLlmProviderKey, type LlmProvider} from "@/lib/helpers/llmProviders"
import {isDemo} from "@/lib/helpers/utils"
import {type LlmProvider} from "@/lib/helpers/llmProviders"
import {Button, Input, Space, Typography, message} from "antd"
import {useEffect, useState} from "react"

Expand All @@ -12,9 +11,7 @@ export default function Secrets() {
const [messageAPI, contextHolder] = message.useMessage()

useEffect(() => {
if (isDemo()) {
setLlmProviderKeys(secrets)
}
setLlmProviderKeys(secrets)
}, [secrets])

return (
Expand Down Expand Up @@ -51,7 +48,7 @@ export default function Secrets() {
<Button
data-cy="openai-api-save"
type="primary"
disabled={key === getLlmProviderKey(title) || !key}
disabled={!key}
onClick={async () => {
await handleModifyVaultSecret({
name,
Expand Down
31 changes: 30 additions & 1 deletion agenta-web/src/hooks/useVaultSecret.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {useEffect, useState} from "react"
import {useEffect, useRef, useState} from "react"
import {
getAllProviderLlmKeys,
llmAvailableProviders,
llmAvailableProvidersToken,
LlmProvider,
removeSingleLlmProviderKey,
saveLlmProviderKey,
Expand All @@ -11,6 +12,7 @@ import {dynamicLib, dynamicService} from "@/lib/helpers/dynamic"

export const useVaultSecret = () => {
const [secrets, setSecrets] = useState<LlmProvider[]>(llmAvailableProviders)
const shouldRunMigration = useRef(true)

const getVaultSecrets = async () => {
try {
Expand Down Expand Up @@ -44,6 +46,33 @@ export const useVaultSecret = () => {
getVaultSecrets()
}, [])

const migrateProviderKeys = async () => {
try {
const localStorageProviders = localStorage.getItem(llmAvailableProvidersToken)

if (localStorageProviders) {
const providers = JSON.parse(localStorageProviders)

for (const provider of providers) {
if (provider.key) {
await handleModifyVaultSecret(provider as LlmProvider)
}
}

localStorage.removeItem(llmAvailableProvidersToken)
}
} catch (error) {
console.error(error)
}
}

useEffect(() => {
if (shouldRunMigration.current) {
shouldRunMigration.current = false
migrateProviderKeys()
}
}, [])

const handleModifyVaultSecret = async (provider: LlmProvider) => {
try {
if (isDemo()) {
Expand Down
5 changes: 1 addition & 4 deletions agenta-web/src/lib/helpers/llmProviders.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cloneDeep from "lodash/cloneDeep"

const llmAvailableProvidersToken = "llmAvailableProvidersToken"
export const llmAvailableProvidersToken = "llmAvailableProvidersToken"

export type LlmProvider = {
title: string
Expand Down Expand Up @@ -55,9 +55,6 @@ export const saveLlmProviderKey = (providerName: string, keyValue: string) => {
localStorage.setItem(llmAvailableProvidersToken, JSON.stringify(keys))
}

export const getLlmProviderKey = (providerName: string) =>
getAllProviderLlmKeys().find((item: LlmProvider) => item.title === providerName)?.key

export const getAllProviderLlmKeys = () => {
const providers = cloneDeep(llmAvailableProviders)
try {
Expand Down

0 comments on commit bff72ab

Please sign in to comment.