Skip to content

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

Go
15.8 KB
Raw Blame
"use client";

import { useMemo } from "react";
import type { CommandBarContext } from "@multica/core/command-bar";
import { useFeatureEnabled } from "@multica/core/config";
import { DOCS_V1_FLAG } from "@multica/core/feature-flags";
import { useWorkspaceId } from "@multica/core/hooks";
import { WORKSPACE_PAGES, type WorkspacePageKey } from "@multica/core/paths";
import { useIsGuest } from "@multica/core/permissions";
import { partitionAggregatedSearchResults } from "@multica/core/search/cancelled-rank";
import type { SearchDocResult, SearchIssueResult, SearchProjectResult } from "@multica/core/types";
import { useComposioAvailable } from "../common/use-composio-available";
import { useT } from "../i18n";
import { useAgentCommandGroup, useAutopilotCommandGroup, useChatCommandGroup, useInboxCommandGroup, useProjectCommandGroup } from "./commands/context-commands";
import { useGlobalCommands } from "./commands/global-commands";
import { useIssueCommandGroup } from "./commands/issue-commands";
import { withCursorLast } from "./issue-target";
import { parseScope, scoreRow, scoreText, type SearchScope } from "./matching";
import type { IssueTarget, PaletteCommand, PaletteCommandGroup } from "./types";
import type { AgentEntry } from "./use-actor-directory";
import { useObjectSearch, type ObjectHits, type PaletteDoc, type Scored } from "./use-object-search";
import { useRecentItems, type RecentItem } from "./use-recent-items";
import { useServerSearch, type SearchResults } from "./use-server-search";
import { useDocSearch } from "./use-doc-search";

// The palette's Pages group is generated from WORKSPACE_PAGES, the same
// registry the sidebar nav and the desktop tab bar read. It used to be a
// hand-written list, which silently went stale every time a page was added:
// Chat, Autopilot, Squads and Analytics shipped in the sidebar but were
// unreachable from the palette (MUL-6272). Deriving the list means a new
// workspace page is in the palette the moment it is in the registry.
//
// Page keys double as WorkspacePaths method names, so `p[key]()` resolves the
// destination against the current workspace slug at render time. Every
// WorkspacePageKey must therefore stay a parameterless path builder.

// Extra query aliases per page, on top of the localized label. Declared as a
// total Record so adding a workspace page is a compile error until its
// keywords are filled in.
const PAGE_KEYWORDS: Record<WorkspacePageKey, string[]> = {
  inbox: ["inbox", "notifications", "收件箱", "通知"],
  chat: ["chat", "messages", "conversation", "聊天", "消息", "对话"],
  myIssues: ["my", "issues", "assigned", "mine", "我的", "任务"],
  issues: ["issues", "tasks", "bugs", "任务"],
  views: ["views", "saved", "filters", "视图", "筛选"],
  projects: ["projects", "kanban", "项目"],
  autopilots: ["autopilot", "autopilots", "automation", "schedule", "cron", "webhook", "自动化", "定时"],
  agents: ["agents", "bots", "ai", "智能体"],
  squads: ["squads", "teams", "小队", "团队"],
  usage: ["usage", "analytics", "stats", "metrics", "统计", "分析", "用量"],
  runtimes: ["runtimes", "environments", "machines", "运行时"],
  docs: ["docs", "knowledge", "知识库"],
  skills: ["skills", "library", "技能"],
  apps: ["apps", "integrations", "connected", "accounts", "composio", "应用", "集成"],
  settings: ["settings", "config", "preferences", "设置", "配置"],
};

const NAV_PAGE_KEYS = Object.keys(WORKSPACE_PAGES) as WorkspacePageKey[];

export interface NavPage {
  key: WorkspacePageKey;
  label: string;
  keywords: string[];
}

export type BestMatch =
  | { type: "agent"; entry: AgentEntry; score: number }
  | { type: "squad"; entry: ObjectHits["squads"][number]["item"]; score: number }
  | { type: "member"; member: ObjectHits["members"][number]["item"]; score: number }
  | { type: "project"; project: SearchProjectResult; score: number };

/**
 * What the trailing "Hand to agent" row opens quick create with: the input as
 * typed, plus the page's project and open issue (as parent).
 */
export interface HandToAgentSeed {
  prompt: string;
  project_id?: string;
  parent_issue_id?: string;
  parent_issue_identifier?: string;
}

export interface RootModel {
  scope: SearchScope;
  text: string;
  /** Lower-cased `text`. */
  q: string;
  contextGroups: (PaletteCommandGroup & { score: number })[];
  commands: Scored<PaletteCommand>[];
  pages: Scored<NavPage>[];
  objects: ObjectHits;
  server: { results: SearchResults; isLoading: boolean; stale: boolean };
  partitioned: ReturnType<typeof partitionAggregatedSearchResults>;
  liveIssueScore: number;
  liveProjectScore: number;
  bestMatch: BestMatch | null;
  recent: RecentItem[];
  newCommands: PaletteCommand[];
  handToAgent: HandToAgentSeed | null;
  /** The issue or selection that best-match actions such as "Assign … to" act on. */
  primaryIssueTarget: IssueTarget | null;
  hasResults: boolean;
}

// Server rows answer a query the client cannot re-score exactly (they may
// have matched on a description or comment). A title hit scores like any
// other row; a snippet-only hit ranks below every client-side match.
const SERVER_ONLY_SCORE = 10;

export function issueScore(issue: SearchIssueResult, q: string): number {
  if (issue.identifier.toLowerCase() === q) return 100;
  return Math.max(scoreText(issue.title, q), SERVER_ONLY_SCORE);
}

/**
 * Title hits from the cached doc tree answer on every keystroke; the server
 * adds what only it can find, content matches with an excerpt. A doc found
 * both ways keeps its instant row, picking up the server's excerpt if any.
 * While the server is still answering an earlier query (`stale`), its rows
 * stay painted but disabled, the way stale issue rows are, and lend no
 * excerpts to the fresh title hits.
 */
export function mergeDocHits(
  local: Scored<PaletteDoc>[],
  server: SearchDocResult[],
  folders: ReadonlyMap<string, string>,
  q: string,
  stale = false,
): Scored<PaletteDoc>[] {
  const fromServer = new Map(server.map((doc) => [doc.id, doc]));
  const merged = local.map((hit) => {
    const snippet = stale ? null : fromServer.get(hit.item.doc.id)?.matchedSnippet;
    return snippet ? { ...hit, item: { ...hit.item, snippet } } : hit;
  });
  const listed = new Set(local.map((hit) => hit.item.doc.id));
  for (const doc of server) {
    if (listed.has(doc.id)) continue;
    merged.push({
      item: {
        doc,
        folder: doc.folderId ? (folders.get(doc.folderId) ?? null) : null,
        ...(doc.matchedSnippet ? { snippet: doc.matchedSnippet } : {}),
        ...(stale ? { stale: true } : {}),
      },
      score: Math.max(scoreText(doc.title, q), SERVER_ONLY_SCORE),
    });
  }
  return merged;
}

export function projectScore(project: SearchProjectResult, q: string): number {
  return Math.max(scoreText(project.title, q), SERVER_ONLY_SCORE);
}

function maxScore(entries: readonly { score: number }[]) {
  return entries.reduce((max, entry) => Math.max(max, entry.score), 0);
}

export function useRootModel(
  rawQuery: string,
  registeredContexts: readonly CommandBarContext[],
  options: { active: boolean },
): RootModel {
  const contexts = useMemo(() => withCursorLast(registeredContexts), [registeredContexts]);
  const { t: tNav } = useT("layout");
  const { scope, text } = parseScope(rawQuery);
  const q = text.toLowerCase();
  const active = options.active;

  // Context lookups by kind; the store keeps newest registration first.
  const find = <K extends CommandBarContext["kind"]>(kind: K) =>
    contexts.find((c): c is Extract<CommandBarContext, { kind: K }> => c.kind === kind) ?? null;
  const issueContext = find("issue");
  const issuesContext = find("issues");
  const projectContext = find("project");
  const singleTarget = useMemo<IssueTarget | null>(
    () => (issueContext ? { kind: "single", issue: issueContext.issue } : null),
    [issueContext],
  );
  const batchTarget = useMemo<IssueTarget | null>(
    () =>
      issuesContext
        ? { kind: "batch", issues: issuesContext.issues, clearSelection: issuesContext.clearSelection }
        : null,
    [issuesContext],
  );

  const singleGroup = useIssueCommandGroup(singleTarget, issueContext?.onDeletedFallbackPath);
  const batchGroup = useIssueCommandGroup(batchTarget);
  const projectGroup = useProjectCommandGroup(projectContext);
  const agentGroup = useAgentCommandGroup(find("agent"));
  const autopilotGroup = useAutopilotCommandGroup(find("autopilot"));
  const inboxGroup = useInboxCommandGroup(find("inbox"));
  const chatGroup = useChatCommandGroup(find("chat"));
  const globalCommands = useGlobalCommands(contexts);

  const searching = active && scope !== "commands" && scope !== "people" ? text : "";
  const server = useServerSearch(searching, { projects: scope === "all" });
  const docsEnabled = useFeatureEnabled(DOCS_V1_FLAG, false);
  // A guest (MUL-9420) has no docs; the server refuses doc search.
  const guest = useIsGuest(useWorkspaceId());
  const docSearch = useDocSearch(searching, scope === "all" && docsEnabled && !guest);
  const objects = useObjectSearch(scope === "issues" || scope === "commands" ? "" : q, active);
  const recent = useRecentItems(active && !rawQuery.trim());

  const appsAvailable = useComposioAvailable();
  const navPages = useMemo<NavPage[]>(
    () =>
      NAV_PAGE_KEYS.filter((key) => (key !== "docs" || docsEnabled) && (key !== "apps" || appsAvailable)).map((key) => ({
        key,
        label: tNav(($) => $.nav[WORKSPACE_PAGES[key].navKey]),
        keywords: PAGE_KEYWORDS[key],
      })),
    [docsEnabled, tNav, appsAvailable],
  );

  return useMemo<RootModel>(() => {
    const groupByKind: Record<string, PaletteCommandGroup | null> = {
      issue: singleGroup,
      issues: batchGroup,
      project: projectGroup,
      agent: agentGroup,
      autopilot: autopilotGroup,
      inbox: inboxGroup,
      chat: chatGroup,
    };
    const orderedGroups = contexts
      .map((c) => groupByKind[c.kind])
      .filter((group, index, all): group is PaletteCommandGroup => !!group && all.indexOf(group) === index);

    const commandsAllowed = scope === "all" || scope === "commands";
    let contextGroups: RootModel["contextGroups"] = [];
    let commands: Scored<PaletteCommand>[] = [];
    let pages: Scored<NavPage>[] = [];
    if (!q) {
      if (scope === "all") {
        contextGroups = orderedGroups
          .map((group) => ({ ...group, commands: group.commands.filter((c) => c.primary), score: 0 }))
          .filter((group) => group.commands.length > 0);
      } else if (scope === "commands") {
        contextGroups = orderedGroups.map((group) => ({ ...group, score: 0 }));
        commands = globalCommands.map((command) => ({ item: command, score: 0 }));
      }
    } else if (commandsAllowed) {
      const rank = (list: PaletteCommand[]) =>
        list
          .map((command) => ({ item: command, score: scoreRow(command.label, command.keywords ?? [], q) }))
          .filter((entry) => entry.score > 0)
          .sort((a, b) => b.score - a.score);
      contextGroups = orderedGroups
        .map((group) => {
          const ranked = rank(group.commands);
          return { ...group, commands: ranked.map((r) => r.item), score: maxScore(ranked) };
        })
        .filter((group) => group.commands.length > 0);
      commands = rank(globalCommands);
      pages = navPages
        .map((page) => ({ item: page, score: scoreRow(page.label, page.keywords, q) }))
        .filter((entry) => entry.score > 0)
        .sort((a, b) => b.score - a.score);
    }

    const { results, stale } = server;
    // The partition describes these rows, so it keys off the query they
    // answer — not what the user has typed since.
    const partitioned = partitionAggregatedSearchResults({
      issues: results.issues,
      projects: scope === "all" ? results.projects : [],
      query: results.query,
    });
    const resultsQ = results.query.toLowerCase();
    const liveIssueScore = partitioned.liveIssues.reduce((max, issue) => Math.max(max, issueScore(issue, resultsQ)), 0);
    const liveProjectScore = partitioned.liveProjects.reduce(
      (max, project) => Math.max(max, projectScore(project, resultsQ)),
      0,
    );

    const people = scope === "all" || scope === "people";
    const visibleObjects: ObjectHits = {
      agents: people ? objects.agents : [],
      squads: people ? objects.squads : [],
      members: people ? objects.members : [],
      views: scope === "all" ? objects.views : [],
      autopilots: scope === "all" ? objects.autopilots : [],
      chats: scope === "all" ? objects.chats : [],
      skills: scope === "all" ? objects.skills : [],
      docs:
        scope === "all"
          ? mergeDocHits(
              objects.docs,
              docSearch.docs,
              objects.docFolders,
              q,
              docSearch.query !== searching.trim(),
            )
          : [],
      workspaces: scope === "all" ? objects.workspaces : [],
    };

    // Best match: a top hit worth acting on directly ("Assign MUL-1 to
    // Lambda", "Chat with Lambda") is lifted above everything, actions under
    // it. Only prefix-or-better hits qualify; a substring hit is a guess.
    let bestMatch: BestMatch | null = null;
    if (q && scope === "all") {
      const candidates: BestMatch[] = [];
      const agent = visibleObjects.agents[0];
      if (agent) candidates.push({ type: "agent", entry: agent.item, score: agent.score });
      const squad = visibleObjects.squads[0];
      if (squad) candidates.push({ type: "squad", entry: squad.item, score: squad.score });
      const member = visibleObjects.members[0];
      if (member) candidates.push({ type: "member", member: member.item, score: member.score });
      if (!stale) {
        const project = partitioned.liveProjects.find((p) => scoreText(p.title, resultsQ) >= 80);
        if (project) candidates.push({ type: "project", project, score: scoreText(project.title, resultsQ) });
      }
      const top = candidates.sort((a, b) => b.score - a.score)[0];
      if (top && top.score >= 80 && top.score >= liveIssueScore) bestMatch = top;
    }

    const objectCount =
      visibleObjects.agents.length +
      visibleObjects.squads.length +
      visibleObjects.members.length +
      visibleObjects.views.length +
      visibleObjects.autopilots.length +
      visibleObjects.chats.length +
      visibleObjects.skills.length +
      visibleObjects.docs.length +
      visibleObjects.workspaces.length;
    const hasResults =
      objectCount > 0 ||
      results.issues.length > 0 ||
      results.projects.length > 0 ||
      pages.length > 0 ||
      commands.length > 0 ||
      contextGroups.length > 0;

    let handToAgent: HandToAgentSeed | null = null;
    if (q && scope === "all") {
      const issue = issueContext?.issue ?? null;
      const projectId = projectContext?.project.id ?? issue?.project_id ?? null;
      handToAgent = {
        prompt: text,
        ...(projectId ? { project_id: projectId } : {}),
        ...(issue ? { parent_issue_id: issue.id, parent_issue_identifier: issue.identifier } : {}),
      };
    }

    const primaryIssueTarget =
      contexts
        .map((c) => (c.kind === "issue" ? singleTarget : c.kind === "issues" ? batchTarget : null))
        .find((target): target is IssueTarget => !!target) ?? null;

    return {
      scope,
      text,
      q,
      contextGroups,
      commands,
      pages,
      objects: visibleObjects,
      server,
      partitioned,
      liveIssueScore,
      liveProjectScore,
      bestMatch,
      recent,
      newCommands: globalCommands.filter((command) => command.primary),
      handToAgent,
      primaryIssueTarget,
      hasResults,
    };
  }, [
    agentGroup,
    autopilotGroup,
    batchGroup,
    batchTarget,
    chatGroup,
    contexts,
    docSearch,
    globalCommands,
    inboxGroup,
    issueContext,
    navPages,
    objects,
    projectContext,
    projectGroup,
    q,
    recent,
    scope,
    searching,
    server,
    singleGroup,
    singleTarget,
    text,
  ]);
}