Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

3.4 KB
Raw Blame
import type { GoalStatus } from '@lobechat/const/goal';
import type { GoalConfig, GoalSpend, TaskStatus } from '@lobechat/types';
import { formatDuration } from '@lobechat/utils';

/** Goal lifecycle state β†’ i18n status key (goal list vocabulary). */
const goalStatusKeyMap = {
  achieved: 'goalList.status.achieved',
  canceled: 'goalList.status.canceled',
  draft: 'goalList.status.draft',
  failed: 'goalList.status.error',
  paused: 'goalList.status.paused',
  planning: 'goalList.status.planning',
  review: 'goalList.status.review',
  running: 'goalList.status.running',
  verifying: 'goalList.status.verifying',
} as const satisfies Record<GoalStatus, string>;

export type GoalStatusKey = (typeof goalStatusKeyMap)[GoalStatus];

export const goalStatusKey = (status: GoalStatus): GoalStatusKey => goalStatusKeyMap[status];

/** `4m` / `1h 23m` / `1d 2h 5m` β€” locale-neutral compact span for header metrics and drill-downs. */
export const formatSpan = (ms: number): string =>
  formatDuration(Math.max(1, Math.round(ms / 60_000)) * 60_000, { minUnit: 'm' });

/** Goal lifecycle state β†’ the execution-status vocabulary the shared glyphs use. */
export const goalStatusToTaskStatus = (goalStatus: GoalStatus): TaskStatus => {
  switch (goalStatus) {
    case 'achieved': {
      return 'completed';
    }
    case 'canceled': {
      return 'canceled';
    }
    case 'failed': {
      return 'failed';
    }
    case 'paused':
    case 'review': {
      return 'paused';
    }
    case 'draft':
    case 'planning': {
      return 'backlog';
    }
    default: {
      return 'running';
    }
  }
};

/** `$6.4` β€” a budget read as money, at the precision a budget is set in. */
export const formatUsd = (amount: number): string => `$${Math.round(amount * 100) / 100}`;

/**
 * Spend and budget as ONE metric β€” they are a fraction, and two neighbouring
 * cards made the reader do the division.
 *
 * The fraction is always stated in the unit the budget is actually capped in.
 * A goal capped in rounds would otherwise read "$6.4 / 5 rounds", which is two
 * units wearing the costume of a ratio; there the rounds are the meaningful
 * numerator and the dollars move into the drill-down.
 */
export type GoalBudgetSummary =
  | { cap: number; kind: 'cost'; spent: number }
  | { cap: number; kind: 'rounds'; runs: number }
  | { kind: 'uncapped'; spent: number };

export const summarizeGoalBudget = (
  goal: { maxRounds: number | null; maxTotalCost: number | null },
  spend?: Pick<GoalSpend, 'runs' | 'totalCost'>,
): GoalBudgetSummary => {
  // An unfetched spend is 0 rather than unknown: the graph read that carries
  // the budget carries the spend with it, so the pair is never half-loaded.
  const { runs = 0, totalCost = 0 } = spend ?? {};

  if (goal.maxTotalCost !== null) return { cap: goal.maxTotalCost, kind: 'cost', spent: totalCost };
  if (goal.maxRounds !== null) return { cap: goal.maxRounds, kind: 'rounds', runs };
  return { kind: 'uncapped', spent: totalCost };
};

/** The planner's persistent conversation belongs to the goal agent, not the task executor. */
export const goalManagerConversation = (goal: {
  agentId: string | null;
  config?: GoalConfig | null;
}): { agentId: string; topicId: string } | undefined => {
  const topicId = goal.config?.manager ? goal.config.managerState?.topicId : undefined;
  return goal.agentId && topicId ? { agentId: goal.agentId, topicId } : undefined;
};