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.

9.5 KB
Raw Blame
import type {
  ChatTopicMetadata,
  LobeAgentAgencyConfig,
  TaskExecutionConfig,
  WorkingDirConfig,
} from '@lobechat/types';
import { readTaskExecutionConfig } from '@lobechat/types';

/**
 * What a task's own execution selection contributes to ONE run.
 *
 * Kept separate from {@link TaskExecutionConfig} because the two answer
 * different questions: the stored config says what the user picked, this says
 * what the run should be told. Only the latter has to know that the device
 * rides as `deviceId` and the directory as `initialTopicMetadata`.
 */
export interface TaskRunExecution {
  /** `ExecAgentParams.deviceId` β€” pins the run's execution plan to this machine. */
  deviceId?: string;
  /** Stamped onto the topic this run creates; see `ExecAgentAppContext`. */
  initialTopicMetadata?: {
    repos?: string[];
    workingDirectory?: string;
    workingDirectoryConfig?: WorkingDirConfig;
  };
}

/**
 * The device a task's run will ACTUALLY use, once the assignee agent's own
 * target policy has had its say.
 *
 * A workspace author can fix the agent's execution target
 * (`executionTargetSelectionPolicy: 'fixed'`), and `turnSetup` then drops
 * whatever device the run asks for and routes to the agent's own target
 * instead (`effectiveRequestedDeviceId`, `topicBoundDeviceId`). This mirrors
 * that derivation, because the task side has to tell a pin the run will use
 * from one it will not β€” see `resolveTaskRunExecution`.
 *
 * `undefined` means the run does not land on a device at all (the sandbox).
 */
export const resolveRunDeviceId = (
  execution: TaskExecutionConfig | undefined,
  agencyConfig: LobeAgentAgencyConfig | null | undefined,
  workspaceId?: string,
  /** A device the caller named for this run only (`lh task run --device`). */
  requestedDeviceId?: string,
): string | undefined => {
  const isFixedSelection =
    !!workspaceId && agencyConfig?.executionTargetSelectionPolicy === 'fixed';

  // A member-selected target follows the run's request first, then the task pin.
  if (!isFixedSelection) return requestedDeviceId ?? execution?.boundDeviceId;

  // Only a `device` fixed target names a machine; a fixed sandbox names none.
  return agencyConfig?.executionTarget === 'device' ? agencyConfig.boundDeviceId : undefined;
};

/**
 * Map a task's stored execution selection onto run parameters.
 *
 * Returns `undefined` when the task pins nothing, so callers spread it away and
 * the run keeps the assignee agent's own target and cwd β€” the behaviour every
 * task had before a task could carry a selection. Never returns an empty
 * `initialTopicMetadata`: an empty object would still count as "client-supplied
 * metadata" at the topic-creation site and change how the topic row is built.
 */
export const resolveTaskRunExecution = (
  execution: TaskExecutionConfig | undefined,
  /**
   * The device this run will use β€” `resolveRunDeviceId`. Required, not optional:
   * it decides whether the directory may travel, and a caller that forgot it
   * would silently drop the directory of every pinned task.
   */
  runDeviceId: string | undefined,
  /**
   * A device the caller named for this run only. When the run honors it
   * (`runDeviceId` names it) it outranks the task's pin as the run's
   * `deviceId`; the pin's directory then stays behind unless the two name the
   * same machine (pass the same request to `resolveRunDeviceId`).
   */
  requestedDeviceId?: string,
): TaskRunExecution | undefined => {
  // A request the run will not honor (a fixed agent target replaced it) must not
  // stand in for the pin: it would be persisted on a continued topic as the
  // run's machine while the run goes elsewhere.
  const honoredDeviceId =
    requestedDeviceId && requestedDeviceId === runDeviceId ? requestedDeviceId : undefined;

  if (!execution) return honoredDeviceId ? { deviceId: honoredDeviceId } : undefined;

  const { boundDeviceId, repos, workingDirectory, workingDirectoryConfig } = execution;

  // Directory precedence: an explicit config, then an explicit path, then the
  // primary repo. The last step mirrors the chat gateway, which also treats a
  // repo selection as the run's directory (as a github repo) so a task that
  // only picked repos still starts somewhere β€” the cloud repo surface has no
  // absolute path to offer.
  const deviceDirectory =
    workingDirectoryConfig ?? (workingDirectory ? { path: workingDirectory } : undefined);

  // A directory is picked FOR a machine β€” `TaskWorkingDirectoryChip` offers the
  // run target's OWN directories β€” so it may only travel with that machine. When
  // the run does not land on the pinned device (a fixed agent target replaced
  // it, or the run goes to the sandbox), the path has to stay behind with the
  // pin. Nothing downstream can catch it: the topic carries the EFFECTIVE
  // device, so `resolveDeviceWorkingDirectoryConfig` sees matching run/topic
  // device ids and accepts another machine's absolute path as this one's.
  const isDeviceDirectoryUsable = !boundDeviceId || boundDeviceId === runDeviceId;

  const directoryConfig: WorkingDirConfig | undefined =
    (isDeviceDirectoryUsable ? deviceDirectory : undefined) ??
    (repos && repos.length > 0 ? { path: repos[0], repoType: 'github' } : undefined);

  const initialTopicMetadata = {
    ...(repos && repos.length > 0 ? { repos } : {}),
    ...(directoryConfig
      ? { workingDirectory: directoryConfig.path, workingDirectoryConfig: directoryConfig }
      : {}),
  };

  // An unhonored request falls back to exactly what the run sent before the
  // caller could name a device: the task's own pin.
  const deviceId = honoredDeviceId ?? boundDeviceId;

  const runExecution: TaskRunExecution = {
    ...(deviceId ? { deviceId } : {}),
    ...(Object.keys(initialTopicMetadata).length > 0 ? { initialTopicMetadata } : {}),
  };

  return Object.keys(runExecution).length > 0 ? runExecution : undefined;
};

/** The execution axes a topic carries, in the shape the readers expect. */
const topicExecutionOf = (metadata?: ChatTopicMetadata | null): string =>
  JSON.stringify({
    boundDeviceId: metadata?.boundDeviceId ?? null,
    repos: metadata?.repos ?? null,
    workingDirectory: metadata?.workingDirectory ?? null,
    workingDirectoryConfig: metadata?.workingDirectoryConfig ?? null,
  });

/** Whether two topic metadata snapshots agree on every execution axis. */
export const isSameTopicExecution = (
  a: ChatTopicMetadata | null | undefined,
  b: ChatTopicMetadata | null | undefined,
): boolean => topicExecutionOf(a) === topicExecutionOf(b);

/**
 * The execution metadata a CONTINUED topic has to carry before this run.
 *
 * A topic's own values outrank everything a later run brings: `turnSetup` stamps
 * `initialTopicMetadata` only when it CREATES the topic, `heteroDispatch`'s cwd
 * resolver reads `topic.metadata.workingDirectory` above the run's initial
 * metadata (and above the agent's per-device pick and the device default), and
 * `topic.metadata.boundDeviceId` is what project grouping and the client's
 * worktree probes read. So a task retargeted since its topic was written would
 * continue on the machine it now pins with the PREVIOUS machine's directory β€”
 * a path that may not exist there β€” while the UI kept filing the topic under the
 * old machine.
 *
 * The task's own selection is the one statement that covers EVERY run of it, so
 * mirror it onto the topic before dispatching. Axes the task does not pin are
 * CLEARED, because "inherit the agent" is a decision too: a pin the user removed
 * must stop deciding where the run goes.
 *
 * Only a task that has EVER made a selection gets that treatment. A task with no
 * `execution` block at all β€” every task written before this existed, and every
 * one nobody pinned β€” never said anything about where it runs, so its topic's
 * own device and directory stand: that is where the continued topic's CLI session
 * and cwd live, and `turnSetup` keeps routing the continuation there. Clearing
 * them would let a since-changed agent target move the run to another machine
 * and lose the session. A selection the user cleared is still persisted (as
 * `null` axes, see `toTaskExecutionConfigPatch`), so the two stay tellable apart.
 *
 * Returns `undefined` when there is nothing to write β€” the topic already agrees
 * (the common case: a continuation should not pay for a write it does not need)
 * or the task never selected anything.
 */
export const resolveTopicExecutionPatch = (
  topicMetadata: ChatTopicMetadata | null | undefined,
  /** The task's raw `config` β€” the block's PRESENCE matters, not only its values. */
  taskConfig: null | Record<string, unknown> | undefined,
  /** The device this run will use β€” see {@link resolveTaskRunExecution}. */
  runDeviceId: string | undefined,
  /** A device the caller named for this run only; it outranks the task's pin. */
  requestedDeviceId?: string,
): ChatTopicMetadata | undefined => {
  const stored = taskConfig?.execution;
  if (!stored || typeof stored !== 'object' || Array.isArray(stored)) return undefined;

  const execution = readTaskExecutionConfig(taskConfig);
  const run = resolveTaskRunExecution(execution, runDeviceId, requestedDeviceId);
  const initial = run?.initialTopicMetadata;

  const next: ChatTopicMetadata = {
    // `undefined` clears the axis: `TopicModel.updateMetadata` shallow-merges, so
    // the key is dropped rather than kept at its previous value.
    boundDeviceId: run?.deviceId,
    repos: initial?.repos,
    workingDirectory: initial?.workingDirectory,
    workingDirectoryConfig: initial?.workingDirectoryConfig,
  };

  return isSameTopicExecution(topicMetadata, next) ? undefined : next;
};