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 { hasTag } from "~/lib/taggedUnion";
import type { ResourceTelemetryHistoryInput, ResourceTelemetrySnapshot } from "@akeru/contracts";
import * as Cause from "effect/Cause";
import { useCallback } from "react";

import { usePrimaryEnvironment } from "../state/environments";
import { useEnvironmentQuery } from "../state/query";
import { serverEnvironment } from "../state/server";
import { useAtomCommand } from "../state/use-atom-command";

export interface ResourceTelemetryState {
  readonly data: ResourceTelemetrySnapshot | null;
  readonly error: string | null;
  readonly isPending: boolean;
  readonly refresh: () => void;
  readonly retry: () => Promise<ResourceTelemetrySnapshot>;
}

export function useResourceTelemetry(): ResourceTelemetryState {
  const primaryEnvironment = usePrimaryEnvironment();
  const environmentId = primaryEnvironment?.environmentId ?? null;

  const query = useEnvironmentQuery(
    environmentId === null
      ? null
      : serverEnvironment.resourceTelemetry({ environmentId, input: {} }),
  );

  const retryCommand = useAtomCommand(serverEnvironment.retryResourceTelemetry, {
    reportFailure: false,
  });

  const retry = useCallback(async () => {
    if (environmentId === null) {
      throw new Error("No environment is selected.");
    }

    const result = await retryCommand({ environmentId, input: {} });

    if (hasTag(result, "Failure")) {
      throw Cause.squash(result.cause);
    }

    return result.value.snapshot;
  }, [environmentId, retryCommand]);

  return { ...query, retry };
}

export function useResourceTelemetryHistory(input: ResourceTelemetryHistoryInput) {
  const primaryEnvironment = usePrimaryEnvironment();
  const environmentId = primaryEnvironment?.environmentId ?? null;

  return useEnvironmentQuery(
    environmentId === null
      ? null
      : serverEnvironment.resourceTelemetryHistory({ environmentId, input }),
  );
}