Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix(agent-vault): suggest and replace the whole key around the caret

Scott Wilson5f3b298eab6d
diff --git a/frontend/src/components/agent-vault/service-sheet/VariableReferenceInput.tsx b/frontend/src/components/agent-vault/service-sheet/VariableReferenceInput.tsx
index 93df51bd96..61ddcc3333 100644
--- a/frontend/src/components/agent-vault/service-sheet/VariableReferenceInput.tsx
+++ b/frontend/src/components/agent-vault/service-sheet/VariableReferenceInput.tsx
@@ -11,7 +11,7 @@ import {
} from "@app/components/v3";
import { cn } from "@app/components/v3/utils";
import {
- findOpenVariableReference,
+ findVariableReferenceAtCaret,
normalizeVariableKey,
splitVariableReferences,
toVariableReference,
@@ -173,7 +173,7 @@ export const useVariableAutocomplete = ({
const caret = input?.selectionStart ?? null;
const next =
input && caret !== null && caret === input.selectionEnd
- ? findOpenVariableReference(input.value.slice(0, caret))
+ ? findVariableReferenceAtCaret(input.value, caret)
: null;
if (!next) {
@@ -183,7 +183,7 @@ export const useVariableAutocomplete = ({
}
if (next.start === dismissedAt.current) return;
if (openReference?.start !== next.start || openReference.query !== next.query) {
- setOpenReference(next);
+ setOpenReference({ start: next.start, query: next.query });
setActiveIndex(0);
}
};
@@ -193,16 +193,19 @@ export const useVariableAutocomplete = ({
close();
};
- // Read when a suggestion is chosen, before the list closes. Retyping the key inside a reference that is
- // already closed replaces it, rather than leaving the old key's tail behind the new one.
+ // Read when a suggestion is chosen, before the list closes. The pick replaces the whole reference the caret is
+ // in, closed or not, so no part of the old key is left behind the new one.
const surroundings = () => {
const input = inputRef.current;
if (!input || !openReference) return null;
- const caret = input.selectionStart ?? input.value.length;
- const tail = /^[A-Za-z0-9_]*\}\}/.exec(input.value.slice(caret));
+ const reference = findVariableReferenceAtCaret(
+ input.value,
+ input.selectionStart ?? input.value.length
+ );
+ if (!reference) return null;
return {
- before: input.value.slice(0, openReference.start),
- after: input.value.slice(caret + (tail?.[0].length ?? 0))
+ before: input.value.slice(0, reference.start),
+ after: input.value.slice(reference.end)
};
};
diff --git a/frontend/src/helpers/agentVaultVariables.test.ts b/frontend/src/helpers/agentVaultVariables.test.ts
new file mode 100644
index 0000000000..95a81532e0
--- /dev/null
+++ b/frontend/src/helpers/agentVaultVariables.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, it } from "vitest";
+
+import { findVariableReferenceAtCaret } from "./agentVaultVariables";
+
+describe("findVariableReferenceAtCaret", () => {
+ it("reads the whole key of an unfinished reference when the caret moves back into it", () => {
+ const text = "{{API_TOKEN";
+ expect(findVariableReferenceAtCaret(text, "{{API".length)).toEqual({
+ start: 0,
+ query: "API_TOKEN",
+ end: text.length
+ });
+ });
+
+ it("ends a closed reference after its braces, so a pick replaces all of it", () => {
+ const text = "Bearer {{API_TOKEN}} rest";
+ expect(findVariableReferenceAtCaret(text, "Bearer {{API".length)).toEqual({
+ start: "Bearer ".length,
+ query: "API_TOKEN",
+ end: "Bearer {{API_TOKEN}}".length
+ });
+ });
+
+ it("stops the key at the first character a key can't hold", () => {
+ expect(findVariableReferenceAtCaret("{{AP-rest", "{{AP".length)).toEqual({
+ start: 0,
+ query: "AP",
+ end: "{{AP".length
+ });
+ });
+
+ it("finds nothing when the caret is outside a reference", () => {
+ expect(findVariableReferenceAtCaret("{{API}} x", "{{API}} x".length)).toBeNull();
+ expect(findVariableReferenceAtCaret("plain", 3)).toBeNull();
+ });
+});
diff --git a/frontend/src/helpers/agentVaultVariables.ts b/frontend/src/helpers/agentVaultVariables.ts
index c03600e51b..95ab3690ae 100644
--- a/frontend/src/helpers/agentVaultVariables.ts
+++ b/frontend/src/helpers/agentVaultVariables.ts
@@ -16,6 +16,9 @@ const REFERENCE_ONLY_RE = /^\{\{[A-Z][A-Z0-9_]*\}\}$/;
// An opened reference the caret is still inside, so the suggestions can follow what is typed.
const OPEN_REFERENCE_RE = /\{\{([A-Za-z0-9_]*)$/;
+// The rest of that reference's key after the caret, and its closing braces if it has them.
+const REFERENCE_TAIL_RE = /^([A-Za-z0-9_]*)(\}\})?/;
+
export const toVariableReference = (key: string) => `{{${key}}}`;
export const findVariableReferences = (text: string) =>
@@ -54,10 +57,19 @@ export const splitVariableReferences = (text: string): TVariableSegment[] => {
return segments;
};
-/** Where an unfinished reference starts and what has been typed of its key, or null. */
-export const findOpenVariableReference = (textBeforeCaret: string) => {
- const match = OPEN_REFERENCE_RE.exec(textBeforeCaret);
- return match ? { start: match.index, query: match[1] } : null;
+/**
+ * The reference the caret is in: its whole key, the part after the caret included, and where it starts and ends,
+ * closing braces and all. Null when the caret isn't in one.
+ */
+export const findVariableReferenceAtCaret = (text: string, caret: number) => {
+ const open = OPEN_REFERENCE_RE.exec(text.slice(0, caret));
+ if (!open) return null;
+ const tail = REFERENCE_TAIL_RE.exec(text.slice(caret));
+ return {
+ start: open.index,
+ query: open[1] + (tail?.[1] ?? ""),
+ end: caret + (tail?.[0].length ?? 0)
+ };
};
/**