v0
This commit is contained in:
39
plugin-agent-permissions/src/manifest.ts
Normal file
39
plugin-agent-permissions/src/manifest.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { PaperclipPluginManifestV1 } from "@paperclipai/plugin-sdk";
|
||||
|
||||
const manifest: PaperclipPluginManifestV1 = {
|
||||
id: "paperclipai.plugin-agent-permissions",
|
||||
apiVersion: 1,
|
||||
version: "0.1.0",
|
||||
displayName: "Agent Permissions",
|
||||
description: "Per-agent permission toggling for fine-grained access control",
|
||||
author: "FrenoCorp",
|
||||
categories: ["ui", "automation"],
|
||||
capabilities: [
|
||||
"agents.read",
|
||||
"ui.detailTab.register",
|
||||
"ui.sidebar.register"
|
||||
],
|
||||
entrypoints: {
|
||||
worker: "./dist/worker.js",
|
||||
ui: "./dist/ui"
|
||||
},
|
||||
ui: {
|
||||
slots: [
|
||||
{
|
||||
type: "detailTab",
|
||||
id: "permissions",
|
||||
displayName: "Permissions",
|
||||
exportName: "AgentPermissionsTab",
|
||||
entityTypes: ["agent"]
|
||||
},
|
||||
{
|
||||
type: "sidebar",
|
||||
id: "permissions-nav",
|
||||
displayName: "Permissions",
|
||||
exportName: "PermissionsNav"
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
export default manifest;
|
||||
88
plugin-agent-permissions/src/ui/AgentPermissionsTab.tsx
Normal file
88
plugin-agent-permissions/src/ui/AgentPermissionsTab.tsx
Normal file
@@ -0,0 +1,88 @@
|
||||
import { useState } from "react";
|
||||
import { usePluginData, usePluginAction } from "@paperclipai/plugin-sdk/ui";
|
||||
import type { PluginDetailTabProps } from "@paperclipai/plugin-sdk/ui";
|
||||
|
||||
const PERMISSION_KEYS = [
|
||||
"agents:create",
|
||||
"users:invite",
|
||||
"users:manage_permissions",
|
||||
"tasks:assign",
|
||||
"tasks:assign_scope",
|
||||
"joins:approve"
|
||||
] as const;
|
||||
|
||||
const PERMISSION_LABELS: Record<string, string> = {
|
||||
"agents:create": "Create Agents",
|
||||
"users:invite": "Invite Users",
|
||||
"users:manage_permissions": "Manage Permissions",
|
||||
"tasks:assign": "Assign Tasks",
|
||||
"tasks:assign_scope": "Task Assignment Scope",
|
||||
"joins:approve": "Approve Join Requests"
|
||||
};
|
||||
|
||||
export function AgentPermissionsTab({ context }: PluginDetailTabProps) {
|
||||
const { entityId: agentId } = context;
|
||||
|
||||
const { data, loading, error, refresh } = usePluginData<{
|
||||
agentId: string;
|
||||
permissions: Record<string, boolean>;
|
||||
}>("agent-permissions", { agentId });
|
||||
|
||||
const togglePermission = usePluginAction("toggle-agent-permission");
|
||||
const [updating, setUpdating] = useState<string | null>(null);
|
||||
|
||||
if (loading) return <div style={{ padding: "1rem" }}>Loading permissions...</div>;
|
||||
if (error) return <div style={{ padding: "1rem", color: "#c00" }}>Error: {error.message}</div>;
|
||||
if (!data) return <div style={{ padding: "1rem" }}>No permissions data available</div>;
|
||||
|
||||
async function handleToggle(permissionKey: string, currentEnabled: boolean) {
|
||||
setUpdating(permissionKey);
|
||||
try {
|
||||
await togglePermission({ agentId, permissionKey, enabled: !currentEnabled });
|
||||
await refresh();
|
||||
} catch (err) {
|
||||
console.error("Failed to toggle permission:", err);
|
||||
} finally {
|
||||
setUpdating(null);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={{ padding: "1rem", maxWidth: "600px" }}>
|
||||
<h2 style={{ marginBottom: "1.5rem", fontSize: "1.25rem", fontWeight: 600 }}>Agent Permissions</h2>
|
||||
<p style={{ color: "#666", marginBottom: "1.5rem" }}>
|
||||
Control what actions this agent can perform.
|
||||
</p>
|
||||
|
||||
<div style={{ display: "flex", flexDirection: "column", gap: "0.75rem" }}>
|
||||
{PERMISSION_KEYS.map((key) => {
|
||||
const enabled = data.permissions[key] ?? false;
|
||||
const isUpdating = updating === key;
|
||||
|
||||
return (
|
||||
<label key={key} style={{
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
gap: "0.75rem",
|
||||
cursor: isUpdating ? "wait" : "pointer",
|
||||
padding: "0.5rem",
|
||||
borderRadius: "4px",
|
||||
backgroundColor: isUpdating ? "#f5f5f5" : "transparent",
|
||||
transition: "background-color 0.2s"
|
||||
}}>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={enabled}
|
||||
onChange={() => handleToggle(key, enabled)}
|
||||
disabled={updating !== null}
|
||||
style={{ width: "18px", height: "18px", cursor: isUpdating ? "wait" : "pointer" }}
|
||||
/>
|
||||
<span style={{ fontWeight: 500 }}>{PERMISSION_LABELS[key] || key}</span>
|
||||
{isUpdating && <span style={{ color: "#888", fontSize: "0.875rem" }}>updating...</span>}
|
||||
</label>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
54
plugin-agent-permissions/src/ui/PermissionsNav.tsx
Normal file
54
plugin-agent-permissions/src/ui/PermissionsNav.tsx
Normal file
@@ -0,0 +1,54 @@
|
||||
import { usePluginData } from "@paperclipai/plugin-sdk/ui";
|
||||
import type { PluginSidebarProps } from "@paperclipai/plugin-sdk/ui";
|
||||
|
||||
interface AgentPermissionsSummary {
|
||||
agentId: string;
|
||||
agentName: string;
|
||||
permissions: Record<string, boolean>;
|
||||
}
|
||||
|
||||
export function PermissionsNav({ context }: PluginSidebarProps) {
|
||||
const { data: agentsData, loading, error } = usePluginData<AgentPermissionsSummary[]>("all-agents-permissions");
|
||||
|
||||
if (loading) return <div style={{ padding: "1rem" }}>Loading...</div>;
|
||||
if (error) return <div style={{ padding: "1rem", color: "#c00" }}>Error: {error.message}</div>;
|
||||
if (!agentsData || agentsData.length === 0) {
|
||||
return (
|
||||
<div style={{ padding: "1rem" }}>
|
||||
<h3 style={{ fontSize: "1rem", fontWeight: 600, marginBottom: "0.5rem" }}>Permissions</h3>
|
||||
<p style={{ color: "#666", fontSize: "0.875rem" }}>No agents found</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const agentsWithPermissions = agentsData.filter(a =>
|
||||
Object.values(a.permissions).some(v => v)
|
||||
);
|
||||
|
||||
return (
|
||||
<div style={{ padding: "1rem" }}>
|
||||
<h3 style={{ fontSize: "1rem", fontWeight: 600, marginBottom: "0.75rem" }}>Permissions</h3>
|
||||
<p style={{ color: "#666", fontSize: "0.75rem", marginBottom: "1rem" }}>
|
||||
{agentsWithPermissions.length} agent(s) with custom permissions
|
||||
</p>
|
||||
<div style={{ display: "flex", flexDirection: "column", gap: "0.5rem" }}>
|
||||
{agentsData.slice(0, 5).map(agent => {
|
||||
const permCount = Object.values(agent.permissions).filter(Boolean).length;
|
||||
return (
|
||||
<div key={agent.agentId} style={{
|
||||
fontSize: "0.875rem",
|
||||
padding: "0.5rem",
|
||||
backgroundColor: "#f9f9f9",
|
||||
borderRadius: "4px"
|
||||
}}>
|
||||
<div style={{ fontWeight: 500 }}>{agent.agentName}</div>
|
||||
<div style={{ color: "#888", fontSize: "0.75rem" }}>
|
||||
{permCount} permission(s) granted
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
2
plugin-agent-permissions/src/ui/index.tsx
Normal file
2
plugin-agent-permissions/src/ui/index.tsx
Normal file
@@ -0,0 +1,2 @@
|
||||
export { AgentPermissionsTab } from './AgentPermissionsTab';
|
||||
export { PermissionsNav } from './PermissionsNav';
|
||||
99
plugin-agent-permissions/src/worker.ts
Normal file
99
plugin-agent-permissions/src/worker.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { definePlugin, runWorker } from "@paperclipai/plugin-sdk";
|
||||
|
||||
const PERMISSION_KEYS = [
|
||||
"agents:create",
|
||||
"users:invite",
|
||||
"users:manage_permissions",
|
||||
"tasks:assign",
|
||||
"tasks:assign_scope",
|
||||
"joins:approve"
|
||||
] as const;
|
||||
|
||||
type PermissionKey = typeof PERMISSION_KEYS[number];
|
||||
|
||||
interface AgentPermissions {
|
||||
agentId: string;
|
||||
permissions: Record<PermissionKey, boolean>;
|
||||
}
|
||||
|
||||
interface AllAgentsPermissions {
|
||||
agentId: string;
|
||||
agentName: string;
|
||||
permissions: Record<PermissionKey, boolean>;
|
||||
}
|
||||
|
||||
type AgentPermissionsMap = Record<string, Record<PermissionKey, boolean>>;
|
||||
|
||||
const plugin = definePlugin({
|
||||
async setup(ctx) {
|
||||
ctx.data.register("agent-permissions", async (params) => {
|
||||
const { agentId } = params as { agentId: string };
|
||||
|
||||
const allPerms = (await ctx.state.get(
|
||||
{ scopeKind: "instance", stateKey: "agent_permissions" }
|
||||
) as AgentPermissionsMap) ?? {};
|
||||
|
||||
const agentPerms = allPerms[agentId] ?? {};
|
||||
|
||||
return {
|
||||
agentId,
|
||||
permissions: PERMISSION_KEYS.reduce((acc, key) => ({
|
||||
...acc,
|
||||
[key]: agentPerms[key] ?? false
|
||||
}), {} as Record<PermissionKey, boolean>)
|
||||
};
|
||||
});
|
||||
|
||||
ctx.actions.register("toggle-agent-permission", async (params) => {
|
||||
const { agentId, permissionKey, enabled } = params as {
|
||||
agentId: string;
|
||||
permissionKey: PermissionKey;
|
||||
enabled: boolean;
|
||||
};
|
||||
|
||||
const allPerms = (await ctx.state.get(
|
||||
{ scopeKind: "instance", stateKey: "agent_permissions" }
|
||||
) as AgentPermissionsMap) ?? {};
|
||||
|
||||
const agentPerms = allPerms[agentId] ?? {};
|
||||
const updated = { ...agentPerms, [permissionKey]: enabled };
|
||||
|
||||
await ctx.state.set(
|
||||
{ scopeKind: "instance", stateKey: "agent_permissions" },
|
||||
{ ...allPerms, [agentId]: updated }
|
||||
);
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
ctx.data.register("all-agents-permissions", async (params) => {
|
||||
const companyId = typeof params.companyId === "string" ? params.companyId : "";
|
||||
|
||||
const agents = companyId
|
||||
? await ctx.agents.list({ companyId, limit: 100, offset: 0 })
|
||||
: [];
|
||||
|
||||
const allPerms = (await ctx.state.get(
|
||||
{ scopeKind: "instance", stateKey: "agent_permissions" }
|
||||
) as AgentPermissionsMap) ?? {};
|
||||
|
||||
const result: AllAgentsPermissions[] = agents.map(agent => ({
|
||||
agentId: agent.id,
|
||||
agentName: agent.name,
|
||||
permissions: PERMISSION_KEYS.reduce((acc, key) => ({
|
||||
...acc,
|
||||
[key]: allPerms[agent.id]?.[key] ?? false
|
||||
}), {} as Record<PermissionKey, boolean>)
|
||||
}));
|
||||
|
||||
return result;
|
||||
});
|
||||
},
|
||||
|
||||
async onHealth() {
|
||||
return { status: "ok", message: "Agent permissions plugin running" };
|
||||
}
|
||||
});
|
||||
|
||||
export default plugin;
|
||||
runWorker(plugin, import.meta.url);
|
||||
Reference in New Issue
Block a user