Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
refactor(agent-vault): split VariableReferenceInput by what it holds
Scott Wilson0f3558f586e5
diff --git a/frontend/src/components/agent-vault/service-sheet/CredentialFields.tsx b/frontend/src/components/agent-vault/service-sheet/CredentialFields.tsxindex 7214603adf..6cd6657591 100644--- a/frontend/src/components/agent-vault/service-sheet/CredentialFields.tsx+++ b/frontend/src/components/agent-vault/service-sheet/CredentialFields.tsx@@ -26,15 +26,12 @@ import { cn } from "@app/components/v3/utils";import { isVariableReferenceOnly } from "@app/helpers/agentVaultVariables";import { AgentVaultCredentialType } from "@app/hooks/api/agentVault";+import { ReferenceHighlights } from "./ReferenceHighlights";import { SendsPreview } from "./SendsPreview";import { CREDENTIAL_LABELS, TServiceForm, UNCHANGED_SECRET } from "./serviceSchema";import { useServiceVariables } from "./ServiceVariablesContext";-import {- ReferenceHighlights,- useVariableAutocomplete,- VariableKeyChips,- VariableSuggestions-} from "./VariableReferenceInput";+import { VariableKeyChips } from "./VariableChips";+import { useVariableAutocomplete, VariableSuggestions } from "./VariableSuggestions";type SecretName =| "secret"diff --git a/frontend/src/components/agent-vault/service-sheet/ReferenceHighlights.tsx b/frontend/src/components/agent-vault/service-sheet/ReferenceHighlights.tsxnew file mode 100644index 0000000000..f8ef323f3a--- /dev/null+++ b/frontend/src/components/agent-vault/service-sheet/ReferenceHighlights.tsx@@ -0,0 +1,50 @@+import { RefObject } from "react";++import { cn } from "@app/components/v3/utils";+import { splitVariableReferences } from "@app/helpers/agentVaultVariables";++import { useKnownVariableKeys } from "./ServiceVariablesContext";+import { chipTone } from "./VariableChips";++/**+ * The value drawn behind a transparent input, with each reference marked. Marks add colour but no+ * padding, so every character sits exactly where the input's own would.+ */+export const ReferenceHighlights = ({+ value,+ overlayRef+}: {+ value: string;+ overlayRef: RefObject<HTMLDivElement>;+}) => {+ const knownKeys = useKnownVariableKeys();++ return (+ <div aria-hidden className="pointer-events-none absolute inset-0 flex items-center px-2.5">+ <div+ ref={overlayRef}+ className="w-full overflow-hidden text-sm whitespace-pre text-foreground"+ >+ {splitVariableReferences(value).map((segment, index) =>+ segment.type === "text" ? (+ // eslint-disable-next-line react/no-array-index-key+ <span key={index}>{segment.text}</span>+ ) : (+ <mark+ // eslint-disable-next-line react/no-array-index-key+ key={index}+ className={cn(+ "rounded-[3px]",+ chipTone(segment.isValid && (!knownKeys || knownKeys.has(segment.key)))+ )}+ >+ {segment.text}+ </mark>+ )+ )}+ {/* The caret's width: the input scrolls past it at the end, and plain text has nothing there. */}+ <span className="inline-block w-0.5" />+ </div>+ </div>+ );+};diff --git a/frontend/src/components/agent-vault/service-sheet/SendsPreview.tsx b/frontend/src/components/agent-vault/service-sheet/SendsPreview.tsxindex a8ba77e24e..3eb8a66a64 100644--- a/frontend/src/components/agent-vault/service-sheet/SendsPreview.tsx+++ b/frontend/src/components/agent-vault/service-sheet/SendsPreview.tsx@@ -5,7 +5,8 @@ import { splitVariableReferences } from "@app/helpers/agentVaultVariables";import { AgentVaultCredentialType } from "@app/hooks/api/agentVault";import { TServiceForm, UNCHANGED_SECRET } from "./serviceSchema";-import { useKnownVariableKeys, VariableChip } from "./VariableReferenceInput";+import { useKnownVariableKeys } from "./ServiceVariablesContext";+import { VariableChip } from "./VariableChips";const MASK = "•".repeat(8);diff --git a/frontend/src/components/agent-vault/service-sheet/ServiceVariablesContext.ts b/frontend/src/components/agent-vault/service-sheet/ServiceVariablesContext.tsindex bc23ef98d7..24795ee060 100644--- a/frontend/src/components/agent-vault/service-sheet/ServiceVariablesContext.ts+++ b/frontend/src/components/agent-vault/service-sheet/ServiceVariablesContext.ts@@ -1,4 +1,4 @@-import { createContext, useContext } from "react";+import { createContext, useContext, useMemo } from "react";import { TAgentVaultVariable } from "@app/hooks/api/agentVault/types";@@ -28,3 +28,12 @@ export const ServiceVariablesContext = createContext<TServiceVariables>({});export const useServiceVariables = () => useContext(ServiceVariablesContext);++/** Undefined while the list loads, so nothing is marked unknown before it can be known. */+export const useKnownVariableKeys = () => {+ const { variables } = useServiceVariables();+ return useMemo(+ () => (variables ? new Set(variables.map((variable) => variable.key)) : undefined),+ [variables]+ );+};diff --git a/frontend/src/components/agent-vault/service-sheet/VariableChips.tsx b/frontend/src/components/agent-vault/service-sheet/VariableChips.tsxnew file mode 100644index 0000000000..7493db4583--- /dev/null+++ b/frontend/src/components/agent-vault/service-sheet/VariableChips.tsx@@ -0,0 +1,59 @@+import { HoverCard, HoverCardContent, HoverCardTrigger } from "@app/components/v3";+import { cn } from "@app/components/v3/utils";+import { toVariableReference } from "@app/helpers/agentVaultVariables";++export const chipTone = (isKnown: boolean) =>+ isKnown+ ? "bg-foreground/10 text-label"+ : "bg-danger/15 text-danger ring-1 ring-danger/40 ring-inset";++export const VariableChip = ({+ reference,+ isKnown,+ className+}: {+ reference: string;+ isKnown: boolean;+ className?: string;+}) => <span className={cn("rounded-[3px] px-1", chipTone(isKnown), className)}>{reference}</span>;++// Only the first key shows, so a value using several keeps the hint under its field to one line.+export const VariableKeyChips = ({ keys }: { keys: string[] }) => {+ const [firstKey, ...otherKeys] = keys;+ if (!firstKey) return null;++ return (+ <>+ <VariableChip reference={toVariableReference(firstKey)} isKnown className="font-mono" />+ {otherKeys.length > 0 && (+ <HoverCard openDelay={150} closeDelay={100}>+ <HoverCardTrigger asChild>+ <button+ type="button"+ aria-label={`${otherKeys.length} more: ${otherKeys.join(", ")}`}+ className={cn(+ "rounded-[3px] px-1 font-mono outline-hidden focus-visible:ring-2 focus-visible:ring-ring/50",+ chipTone(true)+ )}+ >+ +{otherKeys.length}+ </button>+ </HoverCardTrigger>+ <HoverCardContent+ align="start"+ className="flex w-auto max-w-sm flex-col items-start gap-1"+ >+ {otherKeys.map((key) => (+ <VariableChip+ key={key}+ reference={toVariableReference(key)}+ isKnown+ className="font-mono text-xs break-all"+ />+ ))}+ </HoverCardContent>+ </HoverCard>+ )}+ </>+ );+};diff --git a/frontend/src/components/agent-vault/service-sheet/VariableReferenceInput.tsx b/frontend/src/components/agent-vault/service-sheet/VariableSuggestions.tsxsimilarity index 72%rename from frontend/src/components/agent-vault/service-sheet/VariableReferenceInput.tsxrename to frontend/src/components/agent-vault/service-sheet/VariableSuggestions.tsxindex 61ddcc3333..9a14813cc2 100644--- a/frontend/src/components/agent-vault/service-sheet/VariableReferenceInput.tsx+++ b/frontend/src/components/agent-vault/service-sheet/VariableSuggestions.tsx@@ -1,19 +1,11 @@import { KeyboardEvent, ReactNode, RefObject, useId, useMemo, useRef, useState } from "react";import { LockIcon, PlusIcon } from "lucide-react";-import {- HoverCard,- HoverCardContent,- HoverCardTrigger,- Popover,- PopoverAnchor,- PopoverContent-} from "@app/components/v3";+import { Popover, PopoverAnchor, PopoverContent } from "@app/components/v3";import { cn } from "@app/components/v3/utils";import {findVariableReferenceAtCaret,normalizeVariableKey,- splitVariableReferences,toVariableReference,VARIABLE_KEY_RE} from "@app/helpers/agentVaultVariables";@@ -21,114 +13,6 @@ import { TAgentVaultVariable } from "@app/hooks/api/agentVault/types";import { useServiceVariables } from "./ServiceVariablesContext";-/** Undefined while the list loads, so nothing is marked unknown before it can be known. */-export const useKnownVariableKeys = () => {- const { variables } = useServiceVariables();- return useMemo(- () => (variables ? new Set(variables.map((variable) => variable.key)) : undefined),- [variables]- );-};--const chipTone = (isKnown: boolean) =>- isKnown- ? "bg-foreground/10 text-label"- : "bg-danger/15 text-danger ring-1 ring-danger/40 ring-inset";--export const VariableChip = ({- reference,- isKnown,- className-}: {- reference: string;- isKnown: boolean;- className?: string;-}) => <span className={cn("rounded-[3px] px-1", chipTone(isKnown), className)}>{reference}</span>;--// Only the first key shows, so a value using several keeps the hint under its field to one line.-export const VariableKeyChips = ({ keys }: { keys: string[] }) => {- const [firstKey, ...otherKeys] = keys;- if (!firstKey) return null;-- return (- <>- <VariableChip reference={toVariableReference(firstKey)} isKnown className="font-mono" />- {otherKeys.length > 0 && (- <HoverCard openDelay={150} closeDelay={100}>- <HoverCardTrigger asChild>- <button- type="button"- aria-label={`${otherKeys.length} more: ${otherKeys.join(", ")}`}- className={cn(- "rounded-[3px] px-1 font-mono outline-hidden focus-visible:ring-2 focus-visible:ring-ring/50",- chipTone(true)- )}- >- +{otherKeys.length}- </button>- </HoverCardTrigger>- <HoverCardContent- align="start"- className="flex w-auto max-w-sm flex-col items-start gap-1"- >- {otherKeys.map((key) => (- <VariableChip- key={key}- reference={toVariableReference(key)}- isKnown- className="font-mono text-xs break-all"- />- ))}- </HoverCardContent>- </HoverCard>- )}- </>- );-};--/**- * The value drawn behind a transparent input, with each reference marked. Marks add colour but no- * padding, so every character sits exactly where the input's own would.- */-export const ReferenceHighlights = ({- value,- overlayRef-}: {- value: string;- overlayRef: RefObject<HTMLDivElement>;-}) => {- const knownKeys = useKnownVariableKeys();-- return (- <div aria-hidden className="pointer-events-none absolute inset-0 flex items-center px-2.5">- <div- ref={overlayRef}- className="w-full overflow-hidden text-sm whitespace-pre text-foreground"- >- {splitVariableReferences(value).map((segment, index) =>- segment.type === "text" ? (- // eslint-disable-next-line react/no-array-index-key- <span key={index}>{segment.text}</span>- ) : (- <mark- // eslint-disable-next-line react/no-array-index-key- key={index}- className={cn(- "rounded-[3px]",- chipTone(segment.isValid && (!knownKeys || knownKeys.has(segment.key)))- )}- >- {segment.text}- </mark>- )- )}- {/* The caret's width: the input scrolls past it at the end, and plain text has nothing there. */}- <span className="inline-block w-0.5" />- </div>- </div>- );-};-type TOpenReference = { start: number; query: string };type TSuggestion =