Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

3.4 KB
Raw Blame
import type {
  AccessEntry,
  PermissionProject,
  PrincipalKind,
  ProjectBinding,
  ResourceAccessLevel,
} from './types';

export type ProjectAccessMode = 'inherit' | 'owner_only' | 'restricted';

export type ResourcePolicyOwnerContext = {
  isInstanceOwner?: boolean;
  organizationGroupIds?: string[] | null;
};

export function normalizePrincipal(kind: PrincipalKind, value: string): string {
  const normalized = value.trim();
  if (kind === 'organization_group') return normalized;
  return (kind === 'email_domain' ? normalized.replace(/^@/, '') : normalized).toLowerCase();
}

export function hasDuplicateAccessEntries(entries: AccessEntry[]): boolean {
  const keys = entries.map((entry) => `${entry.kind}:${normalizePrincipal(entry.kind, entry.value)}`);
  return new Set(keys).size !== keys.length;
}

const ACCESS_ROLE_RANK: Record<AccessEntry['role'], number> = {
  viewer: 1,
  editor: 2,
  member: 3,
};

export function requiresAccessNarrowing(
  current: AccessEntry | null,
  next: AccessEntry,
): boolean {
  if (!current) return false;
  if (
    current.kind !== next.kind
    || normalizePrincipal(current.kind, current.value) !== normalizePrincipal(next.kind, next.value)
  ) {
    return true;
  }
  return ACCESS_ROLE_RANK[next.role] < ACCESS_ROLE_RANK[current.role];
}

export function hasDuplicateProjectBindings(bindings: ProjectBinding[]): boolean {
  const keys = bindings.map((binding) => (
    `${binding.principal_kind}:${normalizePrincipal(binding.principal_kind, binding.principal_value)}`
  ));
  return new Set(keys).size !== keys.length;
}

export function projectMode(project: PermissionProject): ProjectAccessMode {
  if (project.access.mode === 'restricted' && project.access.bindings.length === 0) {
    return 'owner_only';
  }
  return project.access.mode;
}

const bindingKey = (binding: ProjectBinding): string => (
  `${binding.principal_kind}:${normalizePrincipal(binding.principal_kind, binding.principal_value)}`
);

export function requiresProjectNarrowing(
  currentMode: ProjectAccessMode,
  currentBindings: ProjectBinding[],
  nextMode: ProjectAccessMode,
  nextBindings: ProjectBinding[],
): boolean {
  if (currentMode === 'inherit') return nextMode !== 'inherit';
  if (currentMode === 'owner_only') return false;
  if (nextMode === 'owner_only') return true;
  if (nextMode === 'inherit') return false;
  const nextByPrincipal = new Map(nextBindings.map((binding) => [bindingKey(binding), binding]));
  return currentBindings.some((binding) => {
    const next = nextByPrincipal.get(bindingKey(binding));
    return !next || (binding.access_role === 'editor' && next.access_role === 'viewer');
  });
}

export function requiresResourcePolicyNarrowing(
  currentLevel: ResourceAccessLevel,
  currentGroupIds: string[],
  nextLevel: ResourceAccessLevel,
  nextGroupIds: string[],
  ownerContext: ResourcePolicyOwnerContext = {},
): boolean {
  if (currentLevel === 'public') return nextLevel !== 'public';
  if (currentLevel === 'private') {
    if (nextLevel !== 'scope') return false;
    if (ownerContext.isInstanceOwner === true) return false;
    const ownerGroups = ownerContext.organizationGroupIds;
    return !ownerGroups?.some((groupId) => nextGroupIds.includes(groupId));
  }
  if (nextLevel === 'private') return true;
  if (nextLevel === 'public') return false;
  const nextGroups = new Set(nextGroupIds);
  return currentGroupIds.some((groupId) => !nextGroups.has(groupId));
}