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 { getModel, type ProviderView } from "@cindy/model-providers/registry";
import type { AgentKind } from "@cindy/model-providers/types";
import { matchCodexBucketForModel } from "@cindy/maker-shared/codex-usage-buckets";
import type { MobileCodexRateLimitsResult } from "@cindy/maker-shared/device-link-contract";
import type { MobileModelPricingMap } from "@/device-link/mobileMakerTransport";

/** Desktop priceTierOf: standard USD output price; discounts do not change the tier. */
export function mobileCostMarks(
  provider: ProviderView | undefined,
  model: string,
  agent: AgentKind,
  prices?: MobileModelPricingMap | null,
): string | null {
  if (!provider || provider.access?.kind === "subscription") return null;
  const output =
    provider.id === "xd"
      ? prices?.[model]?.outputUsdPerMtok
      : getModel(provider, model, agent)?.cost?.output;
  if (typeof output !== "number" || !Number.isFinite(output) || output <= 0)
    return null;
  return "$".repeat(output <= 3 ? 1 : output <= 15 ? 2 : 3);
}
export type QuotaSource = "codex" | "claude" | "xai";
export function mobileQuotaSource(p: ProviderView): QuotaSource | null {
  if (
    !p.connected ||
    p.suspended ||
    p.openAiAccount?.reconnectRequired ||
    p.auth?.method !== "oauth" ||
    (p.access && p.access.kind !== "subscription")
  )
    return null;
  if (p.id === "openai" || p.auth.native === "codex") return "codex";
  if (p.auth.native === "claude") return "claude";
  if (p.auth.native === "xai") return "xai";
  if (p.source !== "builtin") return null;
  return p.id === "anthropic" ? "claude" : p.id === "xai" ? "xai" : null;
}
const WEEKLY_WINDOW_MINUTES = 7 * 24 * 60;
export interface MobileWeeklyQuota {
  remaining: number;
  resetsAt: number | null;
  /** Known window length for capping the countdown; null when resetsAt may not be weekly (xAI). */
  windowMinutes: number | null;
}
const object = (v: unknown): Record<string, unknown> =>
  v && typeof v === "object" && !Array.isArray(v)
    ? (v as Record<string, unknown>)
    : {};
export function mobileWeeklyQuota(
  source: QuotaSource,
  raw: unknown,
  now: number,
  modelId?: string,
): MobileWeeklyQuota | null {
  const data = object(raw);
  let used: unknown;
  let resets: unknown;
  let windowMinutes: number | null = WEEKLY_WINDOW_MINUTES;
  if (source === "codex") {
    const snapshot = data as unknown as MobileCodexRateLimitsResult;
    if (!snapshot.rateLimits) return null;
    const bucket = matchCodexBucketForModel(
      snapshot.rateLimitsByLimitId ?? {
      [snapshot.rateLimits.limitId ?? "codex"]: snapshot.rateLimits,
      },
      modelId,
      now,
    );
    const window = [bucket?.primary, bucket?.secondary].find(
      (w) => w?.windowMinutes === 10080,
    );
    used = window?.usedPercent;
    resets = window?.resetsAt;
  } else if (source === "claude") {
    const window = object(data.sevenDay);
    used = window.utilization;
    resets = window.resetsAt;
  } else {
    if (
      typeof data.updatedAt !== "number" ||
      !Number.isFinite(data.updatedAt) ||
      data.updatedAt <= 0 ||
      now - data.updatedAt > 30 * 60 * 1000
    )
      return null;
    used = data.creditUsagePercent;
    resets = data.resetsAt;
    // resetsAt may fall back to a non-weekly period end.
    windowMinutes = null;
  }
  if (typeof used !== "number" || !Number.isFinite(used)) return null;
  const resetsAt =
    typeof resets === "number" && Number.isFinite(resets) && resets > 0
      ? resets
      : null;
  if (resetsAt !== null && resetsAt * 1000 <= now) return null;
  return {
    remaining: Math.round(Math.max(0, Math.min(100, 100 - used))),
    resetsAt,
    windowMinutes,
  };
}
/**
 * Match Desktop compactQuotaCountdown: one unit, rounded up; expired stays unknown.
 * A known windowMinutes caps the countdown: right after a reset, server rounding or
 * clock skew can put resetsAt slightly past now + window, and rounding up would read
 * "8d" for a 7-day window.
 */
export type QuotaTimeUnit = 'day' | 'hour' | 'minute' | 'second';
export function quotaCountdown(reset: number, now: number, unitLabel: (unit: QuotaTimeUnit) => string = unit => ({day:'d',hour:'h',minute:'m',second:'s'})[unit], windowMinutes?: number | null): string | null {
  if (!Number.isFinite(reset) || reset <= 0) return null;
  let remaining = reset * 1000 - now;
  if (remaining <= 0) return null;
  if (typeof windowMinutes === "number" && Number.isFinite(windowMinutes) && windowMinutes > 0) {
    remaining = Math.min(remaining, windowMinutes * 60000);
  }
  if (remaining >= 86400000) return `${Math.ceil(remaining / 86400000)}${unitLabel("day")}`;
  if (remaining >= 3600000) return `${Math.ceil(remaining / 3600000)}${unitLabel("hour")}`;
  if (remaining >= 60000) return `${Math.ceil(remaining / 60000)}${unitLabel("minute")}`;
  return `${Math.max(1, Math.ceil(remaining / 1000))}${unitLabel("second")}`;
}

/** The host's connection name and public identity, never the controller's account. */
export function mobileProviderAccountTitle(provider: ProviderView): string {
  const name = provider.name || provider.id;
  const identity = provider.openAiAccount?.identity?.trim() || provider.subscriptionAccount?.identity?.trim();
  if (!identity || name === identity) return name;
  const base = name.replace(/ \(\d+\)$/, '');
  if (base.endsWith(` · ${identity}`)) return name;
  const separator = base.indexOf(' · ');
  if (separator >= 0 && base.length === 50 && `${base.slice(0, separator)} · ${identity}`.slice(0, 50) === base) return name;
  return `${name} · ${identity}`;
}