Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 8 additions & 100 deletions apps/desktop/src/ui/Composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { primaryModifier } from "../lib/platform";

import { memo, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
import { useShallow } from "zustand/react/shallow";
import { projectName, type SlashCommand } from "../acp/types";
import type { SlashCommand } from "../acp/types";
import { fieldBeat } from "../lib/backdrop/events";
import { insertTranscript } from "../lib/dictation";
import { FIELD_FADE, type FieldFade, fieldFade } from "../lib/fade";
Expand Down Expand Up @@ -48,10 +48,8 @@ import {
rankThreads,
sentMessages,
slashToken,
type ThreadChoice,
trackPastes,
} from "../state/compose";
import { agentStateOf, type DelegateNode, delegateTree } from "../state/delegates";
import { atToken, insertKeyword, insertRef, type MentionScope, rankFiles } from "../state/fileRefs";
import { controlDisabledReason } from "../state/harness";
import { useMachineReconnecting } from "../state/machines";
Expand Down Expand Up @@ -98,16 +96,12 @@ import {
import { UsageMeter } from "./ComposerUsage";
import {
ALL_MATCHES,
agentChoices,
type DrawerEntry,
type DrawerPick,
type DrawerRowItem,
FILES_FRESH_MS,
keywordEntries,
mentionStateOf,
NO_PLANS,
NOT_EXPANDED,
projectKeyOf,
} from "./composerMentions";
import { DictationButton, MicrophoneNotice, useDictation } from "./Dictation";
import { draftMirror, listContinuation } from "./DraftMirror";
Expand All @@ -122,6 +116,7 @@ import { cx, noTextHelpers, Pill, Spinner, Tooltip } from "./primitives";
import { QueuedAnnouncer } from "./QueuedMessages";
import { ScheduledMessages } from "./ScheduledMessages";
import { SendLaterDialog } from "./SendLaterDialog";
import { useMentionSources } from "./useMentionSources";

export { PermissionMenu } from "./ComposerFooter";

Expand Down Expand Up @@ -263,99 +258,12 @@ export const Composer = memo(function Composer({
// What is typed after the trigger, and the group the trigger narrowed to (`#` narrows to threads).
const mentionQuery = hash?.query ?? at?.query ?? "";
const scope: MentionScope | null = hash ? "threads" : (at?.scope ?? null);
const sessions = useApp((s) => s.sessions);
const links = useApp((s) => s.links);
const planCache = useApp((s) => s.planCache);
// The harness and login a new thread would start on: what a delegate it names would inherit.
const newHarness = useApp((s) => newThreadChoice(s)?.harness);
const newAccount = useApp((s) => s.newThreadAccount);
const signedOut = useApp((s) => s.signedOutLogins);
const openId = thread?.id;
// The open thread's project: its own, or the folder a new thread will start in.
const home = useMemo(() => {
const open = openId ? sessions.find((s) => s.id === openId) : undefined;
return open ? projectKeyOf(open) : cwdForFiles;
}, [sessions, openId, cwdForFiles]);
// The threads that work or ask right now, as their rows say it: read as lists so a streamed chunk
// somewhere else does not redraw the composer.
const working = useApp(
useShallow((s) => Object.values(s.threads).flatMap((t) => (isRunning(t) ? [t.id] : []))),
);
const asking = useApp(
useShallow((s) => Object.values(s.threads).flatMap((t) => (t.permissions.length ? [t.id] : []))),
);
const threadChoices = useMemo(() => {
const workingIds = new Set(working);
const askingIds = new Set(asking);
return sessions.flatMap((session): ThreadChoice[] => {
const title = session.title?.trim();
// A thread cannot point at itself, and one with no name has nothing to match or to write.
if (!title || session.id === openId) return [];
const project = projectKeyOf(session);
return [
{
id: session.id,
title,
harness: session.harness,
project: project ? projectName(project) : undefined,
home: project === home || session.cwd === home,
state: mentionStateOf(session, workingIds.has(session.id), askingIds.has(session.id)),
updatedAt: session.updatedAt,
},
];
});
}, [sessions, openId, home, working, asking]);
// The threads this one started and still runs, at any depth: the tree the details panel draws.
const delegates = useMemo(() => {
if (!openId) return [];
const workingIds = new Set(working);
const askingIds = new Set(asking);
const found: ThreadChoice[] = [];
const walk = (nodes: readonly DelegateNode[]) => {
for (const node of nodes) {
const session = sessions.find((s) => s.id === node.id);
const title = session?.title?.trim();
// What the thread itself reports wins over the link's own record, as the details tree reads
// it (state/delegates.ts `agentStateOf`); the app's live turn, when it has one, first of all.
const activity = askingIds.has(node.id)
? "needs_input"
: workingIds.has(node.id)
? "running"
: session?.status;
const state = agentStateOf(node.link, activity);
if (session && title && (state === "working" || state === "needs")) {
found.push({
id: node.id,
title,
harness: session.harness,
state,
delegate: true,
home: true,
updatedAt: session.updatedAt,
});
}
walk(node.children);
}
};
walk(delegateTree(openId, links.links));
return found;
}, [openId, sessions, links, working, asking]);
// `@`'s agents: who a delegate could run on, on which model, at which effort.
// The thread a delegate would be started from: a child on this harness inherits its login.
const parent = useMemo(
() => ({
harness: openId ? sessions.find((x) => x.id === openId)?.harness : newHarness,
account: openId ? (sessions.find((x) => x.id === openId)?.account ?? null) : newAccount,
}),
[openId, sessions, newHarness, newAccount],
);
// A machine's harness rows carry no plan: `planCache` is this computer's `_rustcode/plans`,
// and a login that runs there is not the one whose windows it read.
const plans = host ? NO_PLANS : planCache;
const agents = useMemo(
() => agentChoices(harnesses ?? [], plans, signedOut, parent),
[harnesses, plans, signedOut, parent],
);
const { home, threadChoices, delegates, agents } = useMentionSources({
openId: thread?.id,
host,
harnesses,
cwdForFiles,
});
const mentioning = !!hash || !!at;
// `@`'s machines: where the agent should run the work, so it need not look for one.
const machineMentions = useMachineMentions(host);
Expand Down
121 changes: 121 additions & 0 deletions apps/desktop/src/ui/useMentionSources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { useMemo } from "react";
import { useShallow } from "zustand/react/shallow";
import { projectName } from "../acp/types";
import type { ThreadChoice } from "../state/compose";
import { agentStateOf, type DelegateNode, delegateTree } from "../state/delegates";
import { newThreadChoice, useApp } from "../state/store";
import { isRunning } from "../state/thread";
import { agentChoices, mentionStateOf, NO_PLANS, projectKeyOf } from "./composerMentions";

/** What the composer's `@` and `#` drawer can point at, besides the project's files and machines. */
export interface MentionSources {
/** The open thread's project, or the folder a new thread will start in. */
home: string | undefined;
/** Every other named thread, marked when it shares the open thread's project. */
threadChoices: ThreadChoice[];
/** The threads the open one started and still runs, at any depth: the details panel's tree. */
delegates: ThreadChoice[];
/** Who a delegate could run on, on which model, at which effort. */
agents: ReturnType<typeof agentChoices>;
}

/**
* The threads, delegates and agents the composer's mention drawer lists. Running and asking threads
* are read as id lists, so a streamed chunk in another thread does not redraw the composer. A
* machine's harness rows carry no plan: `planCache` is this computer's `_rustcode/plans`. A
* delegate started from the open thread inherits its harness and login, and a new thread's
* delegate the harness and login the new thread will start on.
*/
export function useMentionSources({
openId,
host,
harnesses,
cwdForFiles,
}: {
openId: string | undefined;
host: string | undefined;
harnesses: Parameters<typeof agentChoices>[0] | undefined;
cwdForFiles: string | undefined;
}): MentionSources {
const sessions = useApp((s) => s.sessions);
const links = useApp((s) => s.links);
const planCache = useApp((s) => s.planCache);
const newHarness = useApp((s) => newThreadChoice(s)?.harness);
const newAccount = useApp((s) => s.newThreadAccount);
const signedOut = useApp((s) => s.signedOutLogins);
const home = useMemo(() => {
const open = openId ? sessions.find((s) => s.id === openId) : undefined;
return open ? projectKeyOf(open) : cwdForFiles;
}, [sessions, openId, cwdForFiles]);
const working = useApp(
useShallow((s) => Object.values(s.threads).flatMap((t) => (isRunning(t) ? [t.id] : []))),
);
const asking = useApp(
useShallow((s) => Object.values(s.threads).flatMap((t) => (t.permissions.length ? [t.id] : []))),
);
const threadChoices = useMemo(() => {
const workingIds = new Set(working);
const askingIds = new Set(asking);
return sessions.flatMap((session): ThreadChoice[] => {
const title = session.title?.trim();
if (!title || session.id === openId) return [];
const project = projectKeyOf(session);
return [
{
id: session.id,
title,
harness: session.harness,
project: project ? projectName(project) : undefined,
home: project === home || session.cwd === home,
state: mentionStateOf(session, workingIds.has(session.id), askingIds.has(session.id)),
updatedAt: session.updatedAt,
},
];
});
}, [sessions, openId, home, working, asking]);
const delegates = useMemo(() => {
if (!openId) return [];
const workingIds = new Set(working);
const askingIds = new Set(asking);
const found: ThreadChoice[] = [];
const walk = (nodes: readonly DelegateNode[]) => {
for (const node of nodes) {
const session = sessions.find((s) => s.id === node.id);
const title = session?.title?.trim();
const activity = askingIds.has(node.id)
? "needs_input"
: workingIds.has(node.id)
? "running"
: session?.status;
const state = agentStateOf(node.link, activity);
if (session && title && (state === "working" || state === "needs")) {
found.push({
id: node.id,
title,
harness: session.harness,
state,
delegate: true,
home: true,
updatedAt: session.updatedAt,
});
}
walk(node.children);
}
};
walk(delegateTree(openId, links.links));
return found;
}, [openId, sessions, links, working, asking]);
const parent = useMemo(
() => ({
harness: openId ? sessions.find((x) => x.id === openId)?.harness : newHarness,
account: openId ? (sessions.find((x) => x.id === openId)?.account ?? null) : newAccount,
}),
[openId, sessions, newHarness, newAccount],
);
const plans = host ? NO_PLANS : planCache;
const agents = useMemo(
() => agentChoices(harnesses ?? [], plans, signedOut, parent),
[harnesses, plans, signedOut, parent],
);
return { home, threadChoices, delegates, agents };
}
2 changes: 1 addition & 1 deletion scripts/comment-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@
97 apps/desktop/src/ui/ChatView.tsx
11 apps/desktop/src/ui/CommandPalette.tsx
5 apps/desktop/src/ui/Compare.tsx
222 apps/desktop/src/ui/Composer.tsx
210 apps/desktop/src/ui/Composer.tsx
30 apps/desktop/src/ui/ComposerDrawer.tsx
30 apps/desktop/src/ui/ComposerFooter.tsx
23 apps/desktop/src/ui/ComposerUsage.tsx
Expand Down
Loading