Skip to content

Commit

Permalink
work on role cache
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Feb 14, 2025
1 parent 538d421 commit d0d606f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 43 deletions.
15 changes: 3 additions & 12 deletions packages/agent/src/swarm/communityManager/actions/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,14 @@ import {
logger,
} from "@elizaos/core";
import { type Message } from "discord.js";
import { ServerRoleState } from "../../shared/role/types";

interface TimeoutSettings {
enabled: boolean;
duration: number; // timeout duration in minutes
lastUpdated: number;
}

interface ServerRoleCache {
roles: {
[userId: string]: {
role: string;
platformId: string;
};
};
lastUpdated: number;
}

export const timeoutAction: Action = {
name: "TIMEOUT_USER",
similes: ["MUTE_USER", "SILENCE_USER", "RESTRICT_USER"],
Expand Down Expand Up @@ -66,7 +57,7 @@ export const timeoutAction: Action = {
}

// Get user roles
const roles = await runtime.cacheManager.get<ServerRoleCache>(
const roles = await runtime.cacheManager.get<ServerRoleState>(
`server_${serverId}_user_roles`
);

Expand Down Expand Up @@ -148,7 +139,7 @@ export const timeoutAction: Action = {
}

// Get roles to check if we can timeout this user
const roles = await runtime.cacheManager.get<ServerRoleCache>(
const roles = await runtime.cacheManager.get<ServerRoleState>(
`server_${serverId}_user_roles`
);

Expand Down
22 changes: 3 additions & 19 deletions packages/agent/src/swarm/shared/role/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
logger,
} from "@elizaos/core";
import { type Message } from "discord.js";
import { ServerRoleState } from "./types";

export enum RoleName {
ADMIN = "ADMIN",
Expand All @@ -31,22 +32,6 @@ export enum RoleName {
IGNORE = "IGNORE",
}

interface UserRole {
userId: string;
platformId: string;
serverId: string;
role: RoleName;
assignedBy: string;
assignedAt: number;
}

interface ServerRoleCache {
roles: {
[userId: string]: UserRole;
};
lastUpdated: number;
}

const canModifyRole = (
currentRole: RoleName,
targetRole: RoleName,
Expand Down Expand Up @@ -92,7 +77,7 @@ const updateOrgRoleAction: Action = {

try {
// Get roles cache
const roleCache = await runtime.cacheManager.get<ServerRoleCache>(
const roleCache = await runtime.cacheManager.get<ServerRoleState>(
`server_${serverId}_user_roles`
);

Expand Down Expand Up @@ -159,7 +144,7 @@ const updateOrgRoleAction: Action = {

try {
// Get roles cache
let roleCache = await runtime.cacheManager.get<ServerRoleCache>(
let roleCache = await runtime.cacheManager.get<ServerRoleState>(
`server_${serverId}_user_roles`
);

Expand Down Expand Up @@ -222,7 +207,6 @@ const updateOrgRoleAction: Action = {
// Update role
roleCache.roles[mentionedUser.id] = {
userId: mentionedUser.id,
platformId: mentionedUser.id,
serverId,
role: newRole,
assignedBy: requesterId,
Expand Down
17 changes: 8 additions & 9 deletions packages/agent/src/swarm/shared/role/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ export enum RoleName {
IGNORE = "IGNORE"
}

export interface UserRole {
userId: string;
serverId: string;
role: RoleName;
assignedBy: string;
assignedAt: number;
}

export interface ServerRoleState {
roles: {
[userId: string]: UserRole;
Expand Down Expand Up @@ -35,15 +43,6 @@ export function canModifyRole(modifierRole: RoleName, targetRole: RoleName, newR
return false; // Other roles can't modify roles
}

export interface UserRole {
userId: string;
platformId: string; // Discord ID, Telegram ID, etc
serverId: string;
role: RoleName;
assignedBy: string;
assignedAt: number;
}

// Role access helpers
export async function getUserServerRole(
runtime: IAgentRuntime,
Expand Down
5 changes: 2 additions & 3 deletions packages/plugin-bootstrap/src/providers/anxiety.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ interface AnxietyScore {
lastDecayTimestamp: number;
}

interface ServerRoleCache {
interface ServerRoleState {
roles: {
[userId: string]: {
role: string;
platformId: string;
};
};
lastUpdated: number;
Expand Down Expand Up @@ -66,7 +65,7 @@ const socialAwarenessProvider: Provider = {
}

// Check roles for message sender
const roles = await runtime.cacheManager.get<ServerRoleCache>(
const roles = await runtime.cacheManager.get<ServerRoleState>(
`server_${serverId}_user_roles`
);

Expand Down

0 comments on commit d0d606f

Please sign in to comment.