Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
4.7 KB
Raw Blame
"use client";

import { useQuery } from "@tanstack/react-query";
import { FolderOpen, GitBranch } from "@multica/ui/icons";
import { projectResourcesOptions } from "@multica/core/projects";
import type { Agent, LocalDirectoryResourceRef, ProjectResource } from "@multica/core/types";
import { useWorkspaceId } from "@multica/core/hooks";
import { isAgentRuntimeBound } from "@multica/core/agents";
import { runtimeListOptions } from "@multica/core/runtimes";
import { useLocalDaemonStatus } from "../../platform";
import { useT } from "../../i18n";
import { localDirectoryLabel } from "./local-directory-label";

/**
 * Banner shown at the top of the issue's Activity section when the
 * project is pinned to a `local_directory` resource on the assigned agent's
 * daemon (or this daemon when no agent is assigned).
 * Tells the user what starting an agent here will actually do to that
 * directory, which differs by the resource's execution mode:
 *
 * - `in_place`: the agent edits {label} ({path}) itself, so the user should
 *   expect their working copy to change under them.
 * - `worktree`: the agent never touches that working copy — it runs in an
 *   isolated worktree of the repo and hands back a branch. Saying "in-place"
 *   here would be a plain factual error, and it would send the user looking
 *   for results in a directory that will not have changed (MUL-5707).
 *
 * Rendered only on desktop: web has no daemon to compare against, so the
 * "this machine" check would always fail. Web users will see local_directory
 * resources read-only in the sidebar but no Activity-section hint.
 *
 * SSR-safe: the underlying hook reads `window.daemonAPI` defensively, so
 * server renders return null. An unresolved agent runtime does not fall back
 * to the viewer's directory.
 */
export function LocalDirectoryHint({
  projectId,
  assignedAgent,
}: {
  projectId: string | null | undefined;
  // undefined means no agent assignee; null means the assigned agent is unresolved.
  assignedAgent?: Pick<Agent, "runtime_id" | "runtime_bound"> | null;
}) {
  const { t } = useT("projects");
  const wsId = useWorkspaceId();
  const daemon = useLocalDaemonStatus();
  const runtimeId = assignedAgent && isAgentRuntimeBound(assignedAgent) ? assignedAgent.runtime_id : null;
  const { data: runtimes = [] } = useQuery({
    ...runtimeListOptions(wsId),
    enabled: Boolean(projectId && daemon.daemonId && runtimeId),
  });
  const targetDaemonId = assignedAgent !== undefined
    ? runtimes.find((runtime) => runtime.id === runtimeId)?.daemon_id
    : daemon.daemonId;
  const { data: resources = [] } = useQuery({
    ...projectResourcesOptions(wsId, projectId ?? ""),
    enabled: Boolean(projectId),
  });

  if (!projectId) return null;
  if (!daemon.daemonId) return null;
  if (!targetDaemonId) return null;

  const matches: Array<ProjectResource & { resource_ref: LocalDirectoryResourceRef }> =
    resources
      .filter(
        (r): r is ProjectResource & { resource_ref: LocalDirectoryResourceRef } =>
          r.resource_type === "local_directory",
      )
      .filter((r) => r.resource_ref.daemon_id === targetDaemonId);

  if (matches.length === 0) return null;

  return (
    <div className="mt-3 space-y-1 rounded-md border border-dashed bg-muted/40 px-3 py-2 text-caption text-muted-foreground">
      {matches.map((resource) => {
        const ref = resource.resource_ref;
        const label = localDirectoryLabel(resource);
        // Anything other than an explicit "worktree" is in_place: the mode is
        // absent on resources created before it existed, and an unknown value
        // from a newer server must not claim isolation we cannot verify.
        const isWorktree = ref.execution_mode === "worktree";
        return (
          <div key={resource.id} className="space-y-0.5">
            <div className="flex items-center gap-2">
              {isWorktree ? (
                <GitBranch className="size-3 shrink-0" />
              ) : (
                <FolderOpen className="size-3 shrink-0" />
              )}
              <span className="truncate">
                {isWorktree
                  ? t(($) => $.resources.chat_hint_worktree_prefix)
                  : t(($) => $.resources.chat_hint_prefix)}
                <span className="font-medium text-foreground"> {label} </span>
                <span className="font-mono opacity-70">({ref.local_path})</span>
              </span>
            </div>
            {/* Where the work ends up. Only worktree mode needs saying: in
                place, the answer is the directory already named above. */}
            {isWorktree && (
              <div className="pl-5 opacity-80">
                {t(($) => $.resources.chat_hint_worktree_note)}
              </div>
            )}
          </div>
        );
      })}
    </div>
  );
}