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.

2.9 KB
Raw Blame
import { createBotEnvironmentAtoms } from "@akeru/client-runtime/state/bots";
import type { EnvironmentId, OrchestrationBot, OrchestrationGroup } from "@akeru/contracts";
import * as Option from "effect/Option";
import { AsyncResult, Atom } from "effect/unstable/reactivity";

import { environmentCatalog } from "../connection/catalog";
import { connectionAtomRuntime } from "../connection/runtime";
import { environmentSnapshotAtom } from "./shell";

const EMPTY_BOTS: ReadonlyArray<OrchestrationBot> = Object.freeze([]);

const EMPTY_GROUPS: ReadonlyArray<OrchestrationGroup> = Object.freeze([]);

export const botEnvironment = createBotEnvironmentAtoms(connectionAtomRuntime);

export const environmentBotsAtom = Atom.family((environmentId: EnvironmentId) =>
  Atom.make(
    (get): ReadonlyArray<OrchestrationBot> =>
      get(environmentSnapshotAtom(environmentId))?.bots ?? EMPTY_BOTS,
  ).pipe(Atom.withLabel(`web-bots:${environmentId}`)),
);

export const environmentGroupsAtom = Atom.family((environmentId: EnvironmentId) =>
  Atom.make(
    (get): ReadonlyArray<OrchestrationGroup> =>
      get(environmentSnapshotAtom(environmentId))?.groups ?? EMPTY_GROUPS,
  ).pipe(Atom.withLabel(`web-groups:${environmentId}`)),
);

export interface EnvironmentRoster {
  readonly environmentId: EnvironmentId;
  readonly bots: ReadonlyArray<OrchestrationBot>;
  readonly groups: ReadonlyArray<OrchestrationGroup>;
}

/** The bots and groups of every environment whose snapshot has loaded. */
export const allEnvironmentRostersAtom = Atom.make((get): ReadonlyArray<EnvironmentRoster> => {
  const catalog = AsyncResult.value(get(environmentCatalog.catalogAtom));

  if (Option.isNone(catalog)) return [];

  return Array.from(catalog.value.entries.keys(), (environmentId) => ({
    environmentId,
    bots: get(environmentBotsAtom(environmentId)),
    groups: get(environmentGroupsAtom(environmentId)),
  }));
}).pipe(Atom.withLabel("web-all-environment-rosters"));

export const environmentPeopleAtom = Atom.family((environmentId: EnvironmentId) =>
  Atom.make((get) => {
    const snapshot = get(environmentSnapshotAtom(environmentId));

    return {
      current: snapshot?.currentPersonId
        ? {
            id: snapshot.currentPersonId,
            displayName: snapshot.currentPersonDisplayName ?? "Paired person",
          }
        : null,
      host: snapshot?.environmentHostPersonId
        ? {
            id: snapshot.environmentHostPersonId,
            displayName: snapshot.environmentHostDisplayName ?? "Host",
          }
        : null,
    };
  }).pipe(Atom.withLabel(`web-people:${environmentId}`)),
);

/** Snapshot presence gates the server-backed roster swap and bootstrap. */
export const environmentRosterLoadedAtom = Atom.family((environmentId: EnvironmentId) =>
  Atom.make((get): boolean => get(environmentSnapshotAtom(environmentId)) !== null).pipe(
    Atom.withLabel(`web-roster-loaded:${environmentId}`),
  ),
);