supabase
Handles the full Supabase workflow from schema changes to deployment, with built-in security guardrails that catch common traps like RLS...
perplexity-policy-guardrails
Implement content moderation, model selection policy, citation quality
Full skill instructions
Policy enforcement for Perplexity Sonar API. Since Perplexity performs live web searches, guardrails must address: query content moderation (what users can search for), citation reliability (filtering low-quality sources), cost control (model selection + token limits), and responsible AI usage.
User Query
│
▼
Query Moderation (block harmful queries)
│
▼
PII Sanitization (strip personal data)
│
▼
Quota Check (daily limit by user tier)
│
▼
Model Selection (enforce tier-appropriate model)
│
▼
Perplexity API Call
│
▼
Citation Quality Scoring (filter low-trust sources)
│
▼
Response to User
const BLOCKED_PATTERNS = [
/\b(write|generate|create)\s+(malware|virus|exploit|ransomware)\b/i,
/\b(personal|private)\s+(address|phone|ssn)\s+of\s+\w+/i,
/\b(bypass|circumvent|hack)\s+(security|firewall|authentication)\b/i,
/\b(how to|tutorial)\s+(stalk|dox|harass)\b/i,
];
const MAX_QUERY_LENGTH = 2000;
class PolicyError extends Error {
constructor(public code: string, message: string) {
super(message);
this.name = "PolicyError";
}
}
function moderateQuery(query: string): string {
if (query.length > MAX_QUERY_LENGTH) {
throw new PolicyError("QUERY_TOO_LONG", `Query exceeds ${MAX_QUERY_LENGTH} characters`);
}
for (const pattern of BLOCKED_PATTERNS) {
if (pattern.test(query)) {
throw new PolicyError("CONTENT_BLOCKED", "Query blocked by content policy");
}
}
return query;
}
interface ModelPolicy {
model: string;
maxTokens: number;
costPerRequest: number;
}
const MODEL_POLICIES: Record<string, ModelPolicy> = {
free: { model: "sonar", maxTokens: 256, costPerRequest: 0.005 },
basic: { model: "sonar", maxTokens: 1024, costPerRequest: 0.005 },
pro: { model: "sonar-pro", maxTokens: 2048, costPerRequest: 0.02 },
enterprise: { model: "sonar-pro", maxTokens: 4096, costPerRequest: 0.02 },
};
function enforceModelPolicy(
userTier: string,
requestedModel?: string
): ModelPolicy {
const policy = MODEL_POLICIES[userTier] || MODEL_POLICIES.free;
// Prevent free users from using expensive models
if (requestedModel === "sonar-pro" && !["pro", "enterprise"].includes(userTier)) {
console.warn(`User tier ${userTier} not allowed sonar-pro, using sonar`);
return MODEL_POLICIES.free;
}
return requestedModel ? { ...policy, model: requestedModel } : policy;
}
class UsageQuota {
private usage: Map<string, { count: number; resetAt: number }> = new Map();
private readonly limits: Record<string, number> = {
free: 50,
basic: 200,
pro: 1000,
enterprise: 5000,
};
check(userId: string, tier: string = "free"): void {
const key = `${userId}:${new Date().toISOString().slice(0, 10)}`;
const entry = this.usage.get(key) || { count: 0, resetAt: this.endOfDay() };
// Reset if past end of day
if (Date.now() > entry.resetAt) {
entry.count = 0;
entry.resetAt = this.endOfDay();
}
const limit = this.limits[tier] || this.limits.free;
if (entry.count >= limit) {
throw new PolicyError(
"QUOTA_EXCEEDED",
`Daily quota exceeded (${entry.count}/${limit}). Resets at midnight UTC.`
);
}
entry.count++;
this.usage.set(key, entry);
}
getUsage(userId: string): { used: number; limit: number; remaining: number } {
const key = `${userId}:${new Date().toISOString().slice(0, 10)}`;
const entry = this.usage.get(key);
const used = entry?.count || 0;
return { used, limit: 50, remaining: Math.max(0, 50 - used) };
}
private endOfDay(): number {
const d = new Date();
d.setUTCHours(23, 59, 59, 999);
return d.getTime();
}
}
const TRUSTED_TLDS = new Set(["gov", "edu", "org"]);
const HIGH_QUALITY_DOMAINS = new Set([
"nature.com", "science.org", "arxiv.org", "wikipedia.org",
"nih.gov", "cdc.gov", "who.int",
]);
const LOW_QUALITY_DOMAINS = new Set([
"reddit.com", "quora.com", "medium.com", "yahoo.com",
]);
interface CitationQuality {
url: string;
trust: "high" | "medium" | "low";
domain: string;
}
function scoreCitations(citations: string[]): {
scored: CitationQuality[];
highTrustPercent: number;
} {
const scored = citations.map((url) => {
const domain = new URL(url).hostname;
const tld = domain.split(".").pop() || "";
let trust: "high" | "medium" | "low" = "medium";
if (TRUSTED_TLDS.has(tld) || HIGH_QUALITY_DOMAINS.has(domain)) {
trust = "high";
} else if (LOW_QUALITY_DOMAINS.has(domain)) {
trust = "low";
}
return { url, trust, domain };
});
const highTrust = scored.filter((s) => s.trust === "high").length;
return {
scored,
highTrustPercent: citations.length > 0 ? highTrust / citations.length : 0,
};
}
const quota = new UsageQuota();
async function policiedSearch(
query: string,
userId: string,
userTier: string = "free",
requestedModel?: string
) {
// 1. Content moderation
const moderated = moderateQuery(query);
// 2. PII sanitization
const { clean } = sanitizeQuery(moderated);
// 3. Quota check
quota.check(userId, userTier);
// 4. Model policy
const policy = enforceModelPolicy(userTier, requestedModel);
// 5. API call
const response = await perplexity.chat.completions.create({
model: policy.model,
messages: [{ role: "user", content: clean }],
max_tokens: policy.maxTokens,
});
// 6. Citation quality
const citations = (response as any).citations || [];
const quality = scoreCitations(citations);
return {
answer: response.choices[0].message.content,
citations: quality.scored,
citationQuality: quality.highTrustPercent,
model: response.model,
tokens: response.usage?.total_tokens,
};
}
| Issue | Cause | Solution |
|---|---|---|
| Query blocked | Content moderation triggered | Review patterns, adjust if false positive |
| Quota exceeded | User hit daily limit | Upgrade tier or wait for reset |
| Model downgraded | User tier restricts access | Inform user of tier limitations |
| Low citation quality | All sources from forums | Add search_domain_filter for trusted sources |
For architecture patterns, see perplexity-architecture-variants.
Handles the full Supabase workflow from schema changes to deployment, with built-in security guardrails that catch common traps like RLS...
Comprehensive guides and best practices for Neon Serverless Postgres, covering setup, connection methods, authentication, and platform APIs.
Guide for setting up and using Firebase Authentication. Use this skill when the user's app requires user sign-in, user management, or secure data access using auth rules.
A skill to evaluate how secure Firestore security rules are. Use this when Firestore security rules are updated to ensure that the generated rules are extremely secure and robust.
Official skill for integrating Firebase AI Logic (Gemini API) into web applications. Covers setup, multimodal inference, structured output, and security.
Complete Better Auth server and client setup with database adapters, session management, plugins, and security configuration.
Deploy and manage projects on Vercel using token-based authentication. Use when working with Vercel CLI using access tokens rather than interactive login — e.g. "deploy to vercel", "set up vercel", "add environment variables to vercel".
Complete Cloudflare platform integration with decision trees for compute, storage, AI, networking, security, and infrastructure-as-code.
Run Azure compliance and security audits with azqr plus Key Vault expiration checks. Covers best-practice assessment, resource review, policy/compliance validation, and security posture checks. WHEN: compliance scan, security audit, BEFORE running azqr (compliance cli tool), Azure best practices, Key Vault expiration check, expired certificates, expiring secrets, orphaned resources, compliance assessment.
Send, read, and manage Gmail messages, drafts, labels, and account settings.
Comprehensive website auditing across 230+ rules in 21 categories including SEO, performance, security, and accessibility.
Shared authentication, CLI syntax, and output formatting patterns for gws Google Workspace commands.
Discover your celebrity doppelgänger with StarByFace!
GeoSpy: Pricing, Features, FAQs, and Alternatives for AI Teams
Detect AI-generated voices to protect against audio fraud.
Ensure Your Content's Originality with AI Plagiarism Checker
Upscale images by 400% without quality loss
Protect your social media from copyright disputes
Accurately Detect AI-Generated Content with TheChecker.AI
Chrome extension that detects and flags AI-generated content