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.

5.9 KB
Raw Blame
import type { GoalGraphSnapshot } from '@lobechat/types';

import { defineReplica, type ReplicaEntityAdapter } from '@/libs/replica';
import type { GoalListItem, goalService } from '@/services/goal';
import type { MetricSeriesWithPoints } from '@/services/metric';

import type { GoalListFilter } from './initialState';

type GoalListResponse = Awaited<ReturnType<typeof goalService.list>>;

/**
 * The list read's value: the page of goals plus the server's own total. The
 * total is the whole set, not the page β€” the header counts a scope whose goal
 * history is longer than one `limit` correctly, and the project dashboard
 * reports real progress rather than the slice it happens to hold.
 */
export interface GoalListValue {
  goals: GoalListItem[];
  total: number;
}

/**
 * Which list a goals page is showing. `all` is the scope's own entry β€” the
 * page-level window the project dashboard and the route meta read β€” while each
 * narrow tab keeps its own entry: the read only loads the newest page, so a
 * client-side filter would let a busy scope's older matching goal fall past the
 * page and the tab would report itself empty while the goal exists.
 */
export interface GoalListParams {
  agentId?: string;
  filter: GoalListFilter;
  /**
   * Cache scope (`userId:workspaceId`) of the cross-agent list, which names
   * neither an agent nor a project β€” so a workspace you left cannot answer here.
   */
  globalScope?: string;
  projectId?: string;
}

/** The home rail's cross-agent roll-up, scoped like the other home feeds. */
export interface HomeGoalListParams {
  /** Cache scope (`userId:workspaceId`), so a workspace you left cannot answer here. */
  scope: string;
}

const GLOBAL_SCOPE_PREFIX = 'global:';
const PROJECT_SCOPE_PREFIX = 'project:';

/** Scope id of the cross-agent goals page for one cache scope. */
export const globalGoalListScopeId = (globalScope: string): string =>
  `${GLOBAL_SCOPE_PREFIX}${globalScope}`;

/**
 * Scope id of a goals page: an agent's page, a project's, or the cross-agent
 * page. Also the entry key of that scope's `all` list β€” the key
 * `goalListByAgentId` has always used.
 */
export const goalListScopeId = ({
  agentId,
  globalScope,
  projectId,
}: Pick<GoalListParams, 'agentId' | 'globalScope' | 'projectId'>): string =>
  projectId
    ? `${PROJECT_SCOPE_PREFIX}${projectId}`
    : (agentId ?? globalGoalListScopeId(globalScope!));

/** Rebuild the list params of one scope id (the inverse of `goalListScopeId`). */
export const goalListScopeParams = (scopeId: string, filter: GoalListFilter): GoalListParams => {
  if (scopeId.startsWith(PROJECT_SCOPE_PREFIX))
    return { filter, projectId: scopeId.slice(PROJECT_SCOPE_PREFIX.length) };
  if (scopeId.startsWith(GLOBAL_SCOPE_PREFIX))
    return { filter, globalScope: scopeId.slice(GLOBAL_SCOPE_PREFIX.length) };
  return { agentId: scopeId, filter };
};

/**
 * Entry key of one tab's list. `all` keys the scope itself; a narrow tab keys a
 * sibling entry so switching tabs never resets the page-level window.
 */
export const goalListKey = (params: GoalListParams): string => {
  const scopeId = goalListScopeId(params);
  return params.filter === 'all' ? scopeId : `${scopeId}:goals-page:${params.filter}`;
};

/** Every tab of one scope, for a refresh that has to cover them all. */
export const GOAL_LIST_FILTERS: GoalListFilter[] = [
  'all',
  'review',
  'running',
  'achieved',
  'archived',
];

/** One scope's goal list, one entry per tab (`goalListByAgentId`). */
export const goalListResource = defineReplica<GoalListParams, GoalListValue, GoalListResponse>({
  key: goalListKey,
  name: 'goalList',
  storage: 'indexedDB',
  version: 1,
});

/** The home rail's cross-agent goal roll-up, one entry per cache scope. */
export const homeGoalListResource = defineReplica<
  HomeGoalListParams,
  GoalListValue,
  GoalListResponse
>({
  key: ({ scope }) => scope,
  name: 'homeGoalList',
  storage: 'indexedDB',
  version: 1,
});

/**
 * The Goal Graph snapshot behind the process-control surface, keyed by the
 * `goals` row id β€” the id every `goal.*` procedure takes (not the carrier
 * task's identifier).
 */
export const goalGraphResource = defineReplica<string, GoalGraphSnapshot>({
  key: (goalId) => goalId,
  name: 'goalGraph',
  storage: 'indexedDB',
  version: 1,
});

/**
 * North-star series of a goal (subjectType 'goal'), points included, keyed by
 * goal id like the graph snapshot β€” the strip and the graph describe the same
 * row and refresh together.
 */
export const goalMetricSeriesResource = defineReplica<string, MetricSeriesWithPoints[]>({
  key: (goalId) => goalId,
  name: 'goalMetricSeries',
  storage: 'indexedDB',
  version: 1,
});

/**
 * Goals created from one conversation, keyed by topic id. Bounded: every topic
 * the user ever opened would otherwise keep its own persisted row.
 */
export const topicGoalListResource = defineReplica<string, GoalListValue, GoalListResponse>({
  key: (topicId) => topicId,
  name: 'topicGoalList',
  persist: { maxEntries: 50 },
  storage: 'indexedDB',
  version: 1,
});

/**
 * Goals are addressed by `goal.id` across the goal store, and a list row
 * carries the row plus its graph roll-up. Deleting one drops the row and the
 * total it was counted in, in every list that holds it β€” loaded or only
 * persisted, since the replica patches both.
 */
export const goalListEntity: ReplicaEntityAdapter<GoalListValue, GoalListItem> = {
  has: (data, id) => data.goals.some((item) => item.goal.id === id),
  map: (data, id, fn) => {
    const index = data.goals.findIndex((item) => item.goal.id === id);
    if (index === -1) return data;
    const current = data.goals[index];
    const next = fn(current);
    if (next === current) return data;
    if (next === undefined)
      return {
        goals: data.goals.filter((_, i) => i !== index),
        total: Math.max(0, data.total - 1),
      };
    const goals = [...data.goals];
    goals[index] = next;
    return { ...data, goals };
  },
};