Files
kandev/apps/web/components/task-create-dialog-form-body.tsx
T
Kandev Agent 9e8079de69 feat(multi-repo): per-repo git ops, unified branch endpoint, chip-style task create
Bundles a string of multi-repo fixes and refactors that came up while
testing the feature end-to-end:

- workspace_path stored on multi-repo TaskEnvironment was one level too
  high, so agentctl booted with the wrong WorkDir on cold start; fixed
  in resolveTaskEnvWorkspacePath + GetWorkspaceInfoForSession.
- Stage/Unstage/Discard/Commit/Revert/Reset/Amend now thread `repo`
  through agentctl client → WS handler → frontend hook → SessionGit
  fan-out, so the right git repo is targeted (was running at the task
  root, which isn't a git repo for multi-repo tasks).
- agentctl GitLog is multi-repo aware: when no `repo` is specified and
  per-repo trackers exist, fans out one log per repo and merges sorted
  newest-first with RepositoryName stamped on each commit.
- Per-repo grouping for files (Changes panel) and commits, with
  collapsible group headers; commits section defaults to collapsed.
  Algorithm extracted into one generic groupByRepositoryName helper
  (was copy-pasted three times).
- Task creation dialog rewritten around a unified `fs.repositories[]`
  list — no more "primary vs extras" split. Chip-style row with one
  pill per repo + branch, "+" to add, x to remove, GitHub URL toggle
  inline. Discovered (on-machine) repos appear in any chip's dropdown.
  Removes ~6 fields and 3 helpers from the dialog state.
- Branch listing endpoints collapsed to one:
  GET /workspaces/:id/branches?repository_id=X | path=Y
  Frontend collapses to a single useBranches hook with a tagged-union
  source. Drops two HTTP routes, two service methods, two API client
  funcs, and one whole hook file.

Tests added across the chain: workspace_path resolver, sort-commits-
by-date, manager poll-mode propagation, RepoSubpaths, chip grouping
+ collapse + per-repo isLatest, group-by-repo helper, dialog payload
builder + chip wiring + discovered-repo flow.
2026-04-26 21:23:48 +01:00

324 lines
10 KiB
TypeScript

"use client";
import { memo, useCallback, useState } from "react";
import Link from "next/link";
import type { AgentProfileOption } from "@/lib/state/slices";
import type { WorkflowSnapshotData } from "@/lib/state/slices/kanban/types";
import { WorkflowSelectorRow } from "@/components/workflow-selector-row";
import { AgentLogo } from "@/components/agent-logo";
import type { DialogFormState } from "@/components/task-create-dialog-types";
import type { useKeyboardShortcutHandler } from "@/hooks/use-keyboard-shortcut";
import { TaskFormInputs } from "@/components/task-create-dialog-selectors";
import type { JiraTicket } from "@/lib/types/jira";
type SelectorOption = {
value: string;
label: string;
renderLabel: () => React.ReactNode;
};
type CreateEditSelectorsProps = {
isTaskStarted: boolean;
agentProfiles: AgentProfileOption[];
agentProfilesLoading: boolean;
agentProfileOptions: SelectorOption[];
agentProfileId: string;
onAgentProfileChange: (value: string) => void;
isCreatingSession: boolean;
executorProfileOptions: Array<{
value: string;
label: string;
renderLabel?: () => React.ReactNode;
}>;
executorProfileId: string;
onExecutorProfileChange: (value: string) => void;
executorsLoading: boolean;
AgentSelectorComponent: React.ComponentType<{
options: SelectorOption[];
value: string;
onValueChange: (value: string) => void;
disabled: boolean;
placeholder: string;
triggerClassName?: string;
}>;
ExecutorProfileSelectorComponent: React.ComponentType<{
options: Array<{ value: string; label: string; renderLabel?: () => React.ReactNode }>;
value: string;
onValueChange: (value: string) => void;
disabled: boolean;
placeholder: string;
triggerClassName?: string;
}>;
workflowAgentLocked: boolean;
};
export const CreateEditSelectors = memo(function CreateEditSelectors({
isTaskStarted,
agentProfiles,
agentProfilesLoading,
agentProfileOptions,
agentProfileId,
onAgentProfileChange,
isCreatingSession,
executorProfileOptions,
executorProfileId,
onExecutorProfileChange,
executorsLoading,
workflowAgentLocked,
AgentSelectorComponent,
ExecutorProfileSelectorComponent,
}: CreateEditSelectorsProps) {
if (isTaskStarted) return null;
const agentLockedByWorkflow = workflowAgentLocked;
const agentPlaceholder = (() => {
if (agentProfilesLoading) return "Loading agents...";
if (agentProfiles.length === 0) return "No agents available";
return "Select agent";
})();
// Branch + repo selection moved into the chip row above the description;
// this row carries only agent and executor profile selectors now.
return (
<div className="grid gap-4 grid-cols-1 sm:grid-cols-2">
<div>
{agentProfiles.length === 0 && !agentProfilesLoading ? (
<div className="flex h-7 items-center justify-center gap-2 rounded-sm border border-input px-3 text-xs text-muted-foreground">
<span>No agents found.</span>
<Link href="/settings/agents" className="text-primary hover:underline">
Add agent
</Link>
</div>
) : (
<>
<AgentSelectorComponent
options={agentProfileOptions}
value={agentProfileId}
onValueChange={onAgentProfileChange}
placeholder={agentPlaceholder}
disabled={agentProfilesLoading || isCreatingSession || agentLockedByWorkflow}
/>
{agentLockedByWorkflow && (
<p className="text-[11px] text-muted-foreground mt-1">Agent set by workflow</p>
)}
</>
)}
</div>
<div>
<ExecutorProfileSelectorComponent
options={executorProfileOptions}
value={executorProfileId}
onValueChange={onExecutorProfileChange}
placeholder={executorsLoading ? "Loading profiles..." : "Select profile"}
disabled={executorsLoading}
/>
</div>
</div>
);
});
type SessionSelectorsProps = {
agentProfileOptions: SelectorOption[];
agentProfileId: string;
onAgentProfileChange: (value: string) => void;
agentProfilesLoading: boolean;
isCreatingSession: boolean;
executorProfileOptions: Array<{
value: string;
label: string;
renderLabel?: () => React.ReactNode;
}>;
executorProfileId: string;
onExecutorProfileChange: (value: string) => void;
executorsLoading: boolean;
AgentSelectorComponent: React.ComponentType<{
options: SelectorOption[];
value: string;
onValueChange: (value: string) => void;
disabled: boolean;
placeholder: string;
triggerClassName?: string;
}>;
ExecutorProfileSelectorComponent: React.ComponentType<{
options: Array<{ value: string; label: string; renderLabel?: () => React.ReactNode }>;
value: string;
onValueChange: (value: string) => void;
disabled: boolean;
placeholder: string;
triggerClassName?: string;
}>;
};
export const SessionSelectors = memo(function SessionSelectors({
agentProfileOptions,
agentProfileId,
onAgentProfileChange,
agentProfilesLoading,
isCreatingSession,
executorProfileOptions,
executorProfileId,
onExecutorProfileChange,
executorsLoading,
AgentSelectorComponent,
ExecutorProfileSelectorComponent,
}: SessionSelectorsProps) {
return (
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
<AgentSelectorComponent
options={agentProfileOptions}
value={agentProfileId}
onValueChange={onAgentProfileChange}
placeholder={agentProfilesLoading ? "Loading agent profiles..." : "Select agent profile"}
disabled={agentProfilesLoading || isCreatingSession}
/>
<ExecutorProfileSelectorComponent
options={executorProfileOptions}
value={executorProfileId}
onValueChange={onExecutorProfileChange}
placeholder={executorsLoading ? "Loading profiles..." : "Select profile"}
disabled={executorsLoading || isCreatingSession}
/>
</div>
);
});
type WorkflowSectionProps = {
isCreateMode: boolean;
isTaskStarted: boolean;
workflows: Array<{ id: string; name: string; agent_profile_id?: string; [key: string]: unknown }>;
snapshots: Record<string, WorkflowSnapshotData>;
effectiveWorkflowId: string | null;
onWorkflowChange: (value: string) => void;
agentProfiles: AgentProfileOption[];
};
export const WorkflowSection = memo(function WorkflowSection({
isCreateMode,
isTaskStarted,
workflows,
snapshots,
effectiveWorkflowId,
onWorkflowChange,
agentProfiles,
}: WorkflowSectionProps) {
const [lastUsedWorkflowId, setLastUsedWorkflowId] = useState<string | null>(null);
const handleWorkflowChange = useCallback(
(workflowId: string) => {
setLastUsedWorkflowId(workflowId);
onWorkflowChange(workflowId);
},
[onWorkflowChange],
);
if (!isCreateMode || isTaskStarted) return null;
// Single workflow — show agent override info if any overrides exist
if (workflows.length <= 1) {
const singleWorkflow = workflows[0];
if (!singleWorkflow) return null;
const snapshot = snapshots[singleWorkflow.id];
const workflowProfile = singleWorkflow.agent_profile_id
? agentProfiles.find((p) => p.id === singleWorkflow.agent_profile_id)
: null;
const stepsWithOverrides = (snapshot?.steps ?? [])
.filter((s) => s.agent_profile_id)
.map((s) => ({
name: s.title,
profile: agentProfiles.find((p) => p.id === s.agent_profile_id),
}))
.filter((s) => s.profile);
if (!workflowProfile && stepsWithOverrides.length === 0) return null;
return (
<div
className="flex flex-wrap items-center gap-2 text-xs text-muted-foreground"
data-testid="workflow-override-info"
>
{workflowProfile && (
<span className="flex items-center gap-1">
<AgentLogo agentName={workflowProfile.agent_name} size={14} className="shrink-0" />
<span>{workflowProfile.label}</span>
</span>
)}
{stepsWithOverrides.map((s) => (
<span key={s.name} className="flex items-center gap-1">
<span className="text-muted-foreground/50">{s.name}:</span>
<AgentLogo agentName={s.profile!.agent_name} size={14} className="shrink-0" />
<span>{s.profile!.label}</span>
</span>
))}
</div>
);
}
return (
<WorkflowSelectorRow
workflows={workflows}
snapshots={snapshots}
selectedWorkflowId={effectiveWorkflowId ?? null}
onWorkflowChange={handleWorkflowChange}
lastUsedWorkflowId={lastUsedWorkflowId}
agentProfiles={agentProfiles}
/>
);
});
export type DialogPromptSectionProps = {
isSessionMode: boolean;
isTaskStarted: boolean;
isPassthroughProfile: boolean;
initialDescription: string;
hasDescription: boolean;
fs: DialogFormState;
handleKeyDown: ReturnType<typeof useKeyboardShortcutHandler>;
enhance?: { onEnhance: () => void; isLoading: boolean; isConfigured: boolean };
workspaceId?: string | null;
onJiraImport?: (ticket: JiraTicket) => void;
};
export function DialogPromptSection({
isSessionMode,
isTaskStarted,
isPassthroughProfile,
initialDescription,
hasDescription,
fs,
handleKeyDown,
enhance,
workspaceId,
onJiraImport,
}: DialogPromptSectionProps) {
const showJiraImport = !isSessionMode && !isTaskStarted && !!onJiraImport;
return (
<>
<TaskFormInputs
key={fs.openCycle}
isSessionMode={isSessionMode}
autoFocus={!isTaskStarted}
initialDescription={initialDescription}
onDescriptionChange={fs.setHasDescription}
onKeyDown={handleKeyDown}
descriptionValueRef={fs.descriptionInputRef}
disabled={isTaskStarted || isPassthroughProfile}
placeholder={isPassthroughProfile ? "Passthrough mode — prompt not supported" : undefined}
onEnhancePrompt={enhance?.onEnhance}
isEnhancingPrompt={enhance?.isLoading}
isUtilityConfigured={enhance?.isConfigured}
jiraImport={
showJiraImport && onJiraImport
? {
workspaceId: workspaceId ?? null,
disabled: isPassthroughProfile,
onImport: onJiraImport,
}
: undefined
}
/>
{isPassthroughProfile && hasDescription && (
<p className="text-xs text-amber-500">Prompt ignored — passthrough mode active</p>
)}
</>
);
}