Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

1.9 KB
Raw Blame
import {
  AuthAccessWriteScope,
  type AuthClientSession,
  type AuthEnvironmentScope,
  type AuthSessionId,
  type OrchestrationCommand,
} from "@akeru/contracts";

export interface AuthenticatedCommandActor {
  readonly personId: AuthSessionId;
  readonly displayName: string;
  readonly canManageGroups: boolean;
}

export function canManageGroupPeople(
  command: OrchestrationCommand,
  scopes: Iterable<AuthEnvironmentScope>,
): boolean {
  if (command.type !== "group.person.assign" && command.type !== "group.person.unassign") {
    return true;
  }

  return [...scopes].includes(AuthAccessWriteScope);
}

export function applyKnownGroupPerson(
  command: OrchestrationCommand,
  clientSessions: ReadonlyArray<AuthClientSession>,
): OrchestrationCommand | null {
  if (command.type === "group.person.unassign") return command;

  if (command.type !== "group.person.assign") return command;

  const clientSession = clientSessions.find(
    (candidate) => candidate.sessionId === command.person.personId,
  );

  if (!clientSession) return null;

  return {
    ...command,
    person: {
      kind: "person",
      personId: clientSession.sessionId,
      displayName: clientSession.client.label ?? "Paired person",
    },
  };
}

export function applyAuthenticatedCommandActor(
  command: OrchestrationCommand,
  actor: AuthenticatedCommandActor,
): OrchestrationCommand {
  switch (command.type) {
    case "group.create":
      return {
        ...command,
        creator: {
          kind: "person",
          personId: actor.personId,
          displayName: actor.displayName,
        },
      };
    case "group.leave":
      return { ...command, personId: actor.personId };
    case "thread.turn.start":
      return {
        ...command,
        senderPersonId: actor.personId,
        senderDisplayName: actor.displayName,
        senderCanManageGroups: actor.canManageGroups,
      };
    default:
      return command;
  }
}