Skip to content

Commit 8f7fb1e

Browse files
committed
Refactor GuardrailPoliciesAction and CreateGuardrailModal to remove base prompt auto-detection logic. Update BasePromptStep to enhance intent verification messaging and simplify state management. Adjust GuardrailPolicies DTO to reflect changes in base prompt handling.
1 parent 2c430ee commit 8f7fb1e

File tree

4 files changed

+21
-126
lines changed

4 files changed

+21
-126
lines changed

apps/dashboard/src/main/java/com/akto/action/GuardrailPoliciesAction.java

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.akto.action;
22

3-
import com.akto.dao.ApiCollectionsDao;
43
import com.akto.dao.GuardrailPoliciesDao;
54
import com.akto.dao.context.Context;
6-
import com.akto.dto.ApiCollection;
75
import com.akto.dto.GuardrailPolicies;
86
import com.akto.dto.User;
97
import com.akto.log.LoggerMaker;
@@ -12,7 +10,6 @@
1210
import com.mongodb.client.model.Filters;
1311
import com.mongodb.client.model.UpdateOptions;
1412
import com.mongodb.client.model.Updates;
15-
import org.apache.commons.lang3.StringUtils;
1613

1714
import lombok.Getter;
1815
import lombok.Setter;
@@ -50,11 +47,6 @@ public String fetchGuardrailPolicies() {
5047
this.guardrailPolicies = GuardrailPoliciesDao.instance.findAllSortedByCreatedTimestamp(0, 20);
5148
this.total = GuardrailPoliciesDao.instance.getTotalCount();
5249

53-
// Populate basePrompt for policies with autoDetect enabled
54-
for (GuardrailPolicies policy : guardrailPolicies) {
55-
populateBasePromptIfNeeded(policy);
56-
}
57-
5850
loggerMaker.info("Fetched " + guardrailPolicies.size() + " guardrail policies out of " + total + " total");
5951

6052
return SUCCESS.toUpperCase();
@@ -64,52 +56,6 @@ public String fetchGuardrailPolicies() {
6456
}
6557
}
6658

67-
/**
68-
* Populates basePrompt in basePromptRule if:
69-
* 1. basePromptRule exists and is enabled
70-
* 2. autoDetect is true
71-
* 3. basePrompt is not already set (or is empty)
72-
* 4. There are selected agent servers
73-
*
74-
* Fetches detectedBasePrompt from the first selected agent collection
75-
*/
76-
private void populateBasePromptIfNeeded(GuardrailPolicies policy) {
77-
try {
78-
GuardrailPolicies.BasePromptRule basePromptRule = policy.getBasePromptRule();
79-
if (basePromptRule == null || !basePromptRule.isEnabled() || !basePromptRule.isAutoDetect()) {
80-
return;
81-
}
82-
83-
// If basePrompt is already set, use it
84-
if (StringUtils.isNotBlank(basePromptRule.getBasePrompt())) {
85-
return;
86-
}
87-
88-
// Get selected agent servers (prefer V2 format, fallback to old format)
89-
List<GuardrailPolicies.SelectedServer> agentServers = policy.getEffectiveSelectedAgentServers();
90-
if (agentServers == null || agentServers.isEmpty()) {
91-
return;
92-
}
93-
94-
// Try to fetch detected base prompt from the first selected agent collection
95-
try {
96-
int firstAgentCollectionId = Integer.parseInt(agentServers.get(0).getId());
97-
ApiCollection agentCollection = ApiCollectionsDao.instance.getMeta(firstAgentCollectionId);
98-
99-
if (agentCollection != null && StringUtils.isNotBlank(agentCollection.getDetectedBasePrompt())) {
100-
basePromptRule.setBasePrompt(agentCollection.getDetectedBasePrompt());
101-
loggerMaker.debug("Populated basePrompt from collection " + firstAgentCollectionId +
102-
" for policy: " + policy.getName());
103-
}
104-
} catch (NumberFormatException e) {
105-
loggerMaker.debug("Invalid agent collection ID format: " + agentServers.get(0).getId());
106-
} catch (Exception e) {
107-
loggerMaker.debug("Error fetching detected base prompt for policy " + policy.getName() + ": " + e.getMessage());
108-
}
109-
} catch (Exception e) {
110-
loggerMaker.debug("Error populating base prompt: " + e.getMessage());
111-
}
112-
}
11359

11460
public String createGuardrailPolicy() {
11561
try {

apps/dashboard/web/polaris_web/web/src/apps/dashboard/pages/guardrails/components/CreateGuardrailModal.jsx

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ const CreateGuardrailModal = ({ isOpen, onClose, onSave, editingPolicy = null, i
7878
const [llmPrompt, setLlmPrompt] = useState("");
7979
const [llmConfidenceScore, setLlmConfidenceScore] = useState(0.5);
8080

81-
// Step 7: Base Prompt Rule
81+
// Step 7: Base Prompt Based Validation (AI Agents)
8282
const [enableBasePromptRule, setEnableBasePromptRule] = useState(false);
83-
const [basePrompt, setBasePrompt] = useState("");
84-
const [basePromptAutoDetect, setBasePromptAutoDetect] = useState(true);
8583
const [basePromptConfidenceScore, setBasePromptConfidenceScore] = useState(0.5);
8684

8785
// Step 8: External model based evaluation
@@ -122,8 +120,6 @@ const CreateGuardrailModal = ({ isOpen, onClose, onSave, editingPolicy = null, i
122120
llmConfidenceScore,
123121
// Step 7
124122
enableBasePromptRule,
125-
basePromptAutoDetect,
126-
basePrompt,
127123
basePromptConfidenceScore,
128124
// Step 8
129125
url,
@@ -289,8 +285,6 @@ const CreateGuardrailModal = ({ isOpen, onClose, onSave, editingPolicy = null, i
289285
setLlmPrompt("");
290286
setLlmConfidenceScore(0.5);
291287
setEnableBasePromptRule(false);
292-
setBasePrompt("");
293-
setBasePromptAutoDetect(true);
294288
setBasePromptConfidenceScore(0.5);
295289
setUrl("");
296290
setConfidenceScore(25);
@@ -360,16 +354,12 @@ const CreateGuardrailModal = ({ isOpen, onClose, onSave, editingPolicy = null, i
360354
setLlmConfidenceScore(0.5);
361355
}
362356

363-
// Base Prompt Rule
357+
// Base Prompt Based Validation (AI Agents)
364358
if (policy.basePromptRule) {
365359
setEnableBasePromptRule(policy.basePromptRule.enabled || false);
366-
setBasePrompt(policy.basePromptRule.basePrompt || "");
367-
setBasePromptAutoDetect(policy.basePromptRule.autoDetect !== undefined ? policy.basePromptRule.autoDetect : true);
368360
setBasePromptConfidenceScore(policy.basePromptRule.confidenceScore !== undefined ? policy.basePromptRule.confidenceScore : 0.5);
369361
} else {
370362
setEnableBasePromptRule(false);
371-
setBasePrompt("");
372-
setBasePromptAutoDetect(true);
373363
setBasePromptConfidenceScore(0.5);
374364
}
375365

@@ -474,8 +464,6 @@ const CreateGuardrailModal = ({ isOpen, onClose, onSave, editingPolicy = null, i
474464
...(enableBasePromptRule ? {
475465
basePromptRule: {
476466
enabled: true,
477-
basePrompt: basePromptAutoDetect ? "" : basePrompt.trim(), // Send empty if auto-detect
478-
autoDetect: basePromptAutoDetect,
479467
confidenceScore: basePromptConfidenceScore
480468
}
481469
} : {}),
@@ -677,10 +665,6 @@ const CreateGuardrailModal = ({ isOpen, onClose, onSave, editingPolicy = null, i
677665
<BasePromptStep
678666
enableBasePromptRule={enableBasePromptRule}
679667
setEnableBasePromptRule={setEnableBasePromptRule}
680-
basePrompt={basePrompt}
681-
setBasePrompt={setBasePrompt}
682-
basePromptAutoDetect={basePromptAutoDetect}
683-
setBasePromptAutoDetect={setBasePromptAutoDetect}
684668
basePromptConfidenceScore={basePromptConfidenceScore}
685669
setBasePromptConfidenceScore={setBasePromptConfidenceScore}
686670
/>

apps/dashboard/web/polaris_web/web/src/apps/dashboard/pages/guardrails/components/steps/BasePromptStep.jsx

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,53 @@
1-
import { VerticalStack, Text, TextField, RangeSlider, FormLayout, Checkbox, Box } from "@shopify/polaris";
1+
import { VerticalStack, Text, RangeSlider, FormLayout, Checkbox, Box } from "@shopify/polaris";
22

33
export const BasePromptConfig = {
44
number: 7,
5-
title: "Base Prompt Rule",
5+
title: "Intent verification using base prompt (AI Agents)",
66

77
validate: () => {
88
return { isValid: true, errorMessage: null };
99
},
1010

11-
getSummary: ({ enableBasePromptRule, basePromptAutoDetect, basePrompt, basePromptConfidenceScore }) => {
11+
getSummary: ({ enableBasePromptRule, basePromptConfidenceScore }) => {
1212
if (!enableBasePromptRule) return null;
13-
const autoDetectText = basePromptAutoDetect ? ' (Auto-detect)' : '';
14-
const promptText = basePrompt ? ` - ${basePrompt.substring(0, 30)}${basePrompt.length > 30 ? '...' : ''}` : '';
15-
if (!(autoDetectText && promptText)) return null;
16-
return `${autoDetectText}${promptText}, Confidence: ${basePromptConfidenceScore.toFixed(2)}`;
13+
return `Auto-detect from traffic, Confidence: ${basePromptConfidenceScore.toFixed(2)}`;
1714
}
1815
};
1916

2017
const BasePromptStep = ({
2118
enableBasePromptRule,
2219
setEnableBasePromptRule,
23-
basePrompt,
24-
setBasePrompt,
25-
basePromptAutoDetect,
26-
setBasePromptAutoDetect,
2720
basePromptConfidenceScore,
2821
setBasePromptConfidenceScore
2922
}) => {
3023
return (
3124
<VerticalStack gap="4">
32-
<Text variant="headingMd">Base Prompt Rule</Text>
3325
<Text variant="bodyMd" tone="subdued">
34-
Configure a base prompt rule to check the intent of user input in agent prompts with placeholders like {`{var}`} or {`{}`}.
26+
Verify if agent requests match the intent of the base prompt. The base prompt is automatically detected from traffic, and user inputs filling placeholders like {`{var}`} or {`{}`} are checked against this intent.
3527
</Text>
3628

3729
<FormLayout>
3830
<Checkbox
39-
label="Enable base prompt rule"
31+
label="Enable agent intent verification"
4032
checked={enableBasePromptRule}
4133
onChange={setEnableBasePromptRule}
42-
helpText="When enabled, the guardrail will analyze user inputs that fill placeholders in the base prompt."
4334
/>
4435

4536
{enableBasePromptRule && (
46-
<>
47-
<Checkbox
48-
label="Auto-detect base prompt from traffic"
49-
checked={basePromptAutoDetect}
50-
onChange={setBasePromptAutoDetect}
51-
helpText="Automatically detect the base prompt pattern from agent traffic. If disabled, you must provide the base prompt manually."
52-
/>
53-
54-
{ /* TODO: Add auto-detected base prompt display with fallback placeholder text when auto-detect is enabled */ }
55-
{!basePromptAutoDetect && (
56-
<TextField
57-
label="Base Prompt Template"
58-
value={basePrompt}
59-
onChange={setBasePrompt}
60-
multiline={5}
61-
placeholder="You are a helpful assistant. Answer the following question: {}"
62-
helpText="Provide the base prompt template with placeholders using {} or {var_name} syntax."
37+
<Box>
38+
<Box paddingBlockStart="2">
39+
<RangeSlider
40+
label="Confidence Threshold"
41+
value={basePromptConfidenceScore}
42+
min={0}
43+
max={1}
44+
step={0.1}
45+
output
46+
onChange={setBasePromptConfidenceScore}
47+
helpText="Set the confidence threshold (0-1). Higher values require more confidence to block content."
6348
/>
64-
)}
65-
66-
<Box>
67-
<Text variant="bodyMd" fontWeight="medium">Confidence Score: {basePromptConfidenceScore.toFixed(2)}</Text>
68-
<Box paddingBlockStart="2">
69-
<RangeSlider
70-
label="Confidence Threshold"
71-
value={basePromptConfidenceScore}
72-
min={0}
73-
max={1}
74-
step={0.1}
75-
output
76-
onChange={setBasePromptConfidenceScore}
77-
helpText="Set the confidence threshold (0-1). Higher values require more confidence to block content."
78-
/>
79-
</Box>
8049
</Box>
81-
</>
50+
</Box>
8251
)}
8352
</FormLayout>
8453
</VerticalStack>

libs/dao/src/main/java/com/akto/dto/GuardrailPolicies.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,10 @@ public LLMRule(boolean enabled, String userPrompt, double confidenceScore) {
227227
@NoArgsConstructor
228228
public static class BasePromptRule {
229229
private boolean enabled;
230-
private String basePrompt; // Base prompt with placeholders like {var} or {}
231-
private boolean autoDetect; // Whether to auto-detect base_prompt from traffic
232230
private double confidenceScore;
233231

234-
public BasePromptRule(boolean enabled, String basePrompt, boolean autoDetect, double confidenceScore) {
232+
public BasePromptRule(boolean enabled, double confidenceScore) {
235233
this.enabled = enabled;
236-
this.basePrompt = basePrompt;
237-
this.autoDetect = autoDetect;
238234
this.confidenceScore = confidenceScore;
239235
}
240236
}

0 commit comments

Comments
 (0)