Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

5.5 KB
Raw Blame
import { useEffect, useRef, useState } from "react";
import { AppState } from "react-native";
import {
  MODEL_FAVORITES_GET,
  MODEL_FAVORITES_APPLY,
  parseModelFavorites,
  type RemoteModelFavorite,
  type ModelFavoriteMutation,
} from "@cindy/device-link";
import {
  useDeviceLink,
  subscribeRemoteFavoritesChanged,
} from "@/device-link/DeviceLinkContext";
import { startFocusedTopicSubscription } from "@/device-link/focusedTopicSubscription";
import { useUnresponsiveDevices } from "@/device-link/unresponsiveDevicesStore";
import { subscribeMobileAuthOwner } from "@/auth/authOwnerGeneration";
import type { MobileModelFavorite } from "./unifiedMobileModels";

// 上次从那台电脑读到的收藏(账号 + 电脑):每次打开选择器先显示它,再在后台读最新的。
// 只用于显示与起草改动;写入仍带 expected 交给那台电脑核对,旧快照改不坏真相。
const lastFavorites = new Map<string, RemoteModelFavorite[]>();
subscribeMobileAuthOwner(() => lastFavorites.clear());
export function __resetRemoteMobileFavoritesCacheForTest(): void {
  lastFavorites.clear();
}

export function mobileFavorite(item: RemoteModelFavorite): MobileModelFavorite {
  return {
    ...item,
    agent: item.agent === "cc" ? "claude-code" : item.agent,
    effort: item.effort ?? "",
    fast: item.fast ?? false,
  };
}
/** Convert exactly one UI action to an operation, never send a stale full table. */
export function mobileFavoriteMutation(
  before: RemoteModelFavorite[],
  after: MobileModelFavorite[],
): ModelFavoriteMutation | null {
  const old = before.map(mobileFavorite);
  const changed = after.filter((item) => {
    const prior = old.find((value) => value.uid === item.uid);
    return (
      !prior ||
      item.providerId !== prior.providerId ||
      item.modelId !== prior.modelId ||
      item.agent !== prior.agent ||
      item.effort !== prior.effort ||
      item.fast !== prior.fast
    );
  });
  const removed = before.filter(
    (item) => !after.some((value) => value.uid === item.uid),
  );
  if (!changed.length && !removed.length) return null;
  if (changed.length + removed.length !== 1)
    throw new Error("Only one favorite may change at a time");
  if (removed.length) return { kind: "remove", expected: removed[0]! };
  const next = changed[0]!;
  const item = {
    providerId: next.providerId,
    modelId: next.modelId,
    agent: next.agent === "claude-code" ? ("cc" as const) : next.agent,
    ...(next.effort ? { effort: next.effort } : {}),
    ...(next.fast ? { fast: true as const } : {}),
  };
  const expected = before.find((value) => value.uid === next.uid);
  return expected ? { kind: "update", expected, item } : { kind: "add", item };
}
export function useRemoteMobileFavorites(scope: string, visible: boolean) {
  const { invoke, connectionEpoch, status, subscribe, unsubscribe, recoveringDeviceIds } =
    useDeviceLink();
  const [accountId, deviceId] = JSON.parse(scope) as [string, string];
  const cacheKey = JSON.stringify([accountId, deviceId]);
  const unavailable = useUnresponsiveDevices().has(deviceId) || recoveringDeviceIds.has(deviceId);
  const binding = JSON.stringify([scope, connectionEpoch, status, visible]);
  const current = useRef(binding);
  current.current = binding;
  const seq = useRef(0);
  const [state, setState] = useState<{
    binding: string;
    items: RemoteModelFavorite[];
  } | null>(null);
  const [error, setError] = useState<unknown>(null);
  const valid = () => current.current === binding;
  const refresh = async () => {
    if (!visible || status !== "online" || unavailable) return;
    const request = ++seq.current;
    try {
      const items = parseModelFavorites(
        await invoke(deviceId, MODEL_FAVORITES_GET, []),
      );
      if (valid() && seq.current === request) {
        lastFavorites.set(cacheKey, items);
        setState({ binding, items });
        setError(null);
      }
    } catch (error) {
      if (valid() && seq.current === request) {
        // A transient failed refresh must not erase this binding's last snapshot.
        setError(error);
      }
    }
  };
  useEffect(() => {
    if (!visible || status !== "online" || unavailable) return;
    void refresh();
    const off = subscribeRemoteFavoritesChanged((source) => {
      if (source === deviceId) void refresh();
    });
    const stop = startFocusedTopicSubscription({
      deviceId,
      owner: "model-favorites",
      topic: "sessions",
      subscribe,
      unsubscribe,
    });
    const app = AppState.addEventListener("change", (state) => {
      if (state === "active") void refresh();
    });
    return () => {
      off();
      stop();
      app.remove();
      ++seq.current;
    };
  }, [binding, invoke, subscribe, unsubscribe, unavailable]);
  const cached = lastFavorites.get(cacheKey);
  const loaded = state?.binding === binding || cached !== undefined;
  const items = state?.binding === binding ? state.items : (cached ?? []);
  return {
    ready: loaded && status === "online" && !unavailable,
    items: items.map(mobileFavorite),
    error,
    save: async (next: MobileModelFavorite[]) => {
      const mutation = mobileFavoriteMutation(items, next);
      if (!mutation) return;
      if (!valid() || !loaded || status !== "online")
        throw new Error("Favorites not ready");
      ++seq.current;
      try {
        await invoke(deviceId, MODEL_FAVORITES_APPLY, [mutation]);
        if (!valid()) throw new Error("Device changed");
        await refresh();
      } catch (error) {
        if (valid()) {
          setError(error);
          void refresh();
        }
        throw error;
      }
    },
  };
}