"use client";
import { useCallback, useState, type ReactNode } from "react";
import { IconEdit, IconInfoCircle, IconRefresh } from "@tabler/icons-react";
import { Button } from "@kandev/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@kandev/ui/dialog";
import { Label } from "@kandev/ui/label";
import { Popover, PopoverContent, PopoverTrigger } from "@kandev/ui/popover";
import { Switch } from "@kandev/ui/switch";
import { Textarea } from "@kandev/ui/textarea";
import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip";
import { useToast } from "@/components/toast-provider";
import { useTaskCIAutomationOptions } from "@/hooks/domains/github/use-task-ci-options";
import { useResponsiveBreakpoint } from "@/hooks/use-responsive-breakpoint";
import { autoFixRoundForState, findCIAutomationStateForPR } from "@/lib/github/ci-automation";
import type { TaskCIAutomationPatch, TaskCIPRAutomationState, TaskPR } from "@/lib/types/github";
const PR_FEEDBACK_PLACEHOLDER = "{{pr.feedback}}";
function CIAutomationInfoButton() {
return (
Watches this task's linked pull request during the 1 minute PR refresh loop. Auto-fix queues
a task prompt for new failed checks and unresolved review comments when the prompt includes
the PR feedback placeholder, then snapshots what was handled so the next round only sends
newly observed issues. Auto-merge runs only after CI, review, and mergeability are ready.
);
}
function insertPRFeedbackPlaceholder(prompt: string) {
if (prompt.includes(PR_FEEDBACK_PLACEHOLDER)) return prompt;
const trimmedEnd = prompt.trimEnd();
if (!trimmedEnd) return PR_FEEDBACK_PLACEHOLDER;
return `${trimmedEnd}\n\n${PR_FEEDBACK_PLACEHOLDER}`;
}
function CIAutomationPromptDialog({
open,
prompt,
saving,
onPromptChange,
onClose,
onSave,
onReset,
}: {
open: boolean;
prompt: string;
saving: boolean;
onPromptChange: (value: string) => void;
onClose: () => void;
onSave: () => void;
onReset: () => void;
}) {
const trimmed = prompt.trim();
return (
);
}
function CIAutomationRow({
id,
label,
checked,
disabled,
onCheckedChange,
help,
}: {
id: string;
label: string;
checked: boolean;
disabled: boolean;
onCheckedChange: (checked: boolean) => void;
help?: ReactNode;
}) {
return (
);
}
function CIAutomationErrorRow({
error,
loading,
onRetry,
}: {
error: string;
loading: boolean;
onRetry: () => void;
}) {
return (
{error}
);
}
function CIAutoFixRoundHelpButton({
state,
maxRounds,
}: {
state: TaskCIPRAutomationState | undefined;
maxRounds: number | null | undefined;
}) {
const round = autoFixRoundForState(state, maxRounds);
const { isFinePointer } = useResponsiveBreakpoint();
const [open, setOpen] = useState(false);
const trigger = (
);
const explanation = (
Auto-fix has used {round.current} of {round.max} rounds for this PR. A round is counted when
Kandev sends or queues a CI auto-fix message. Kandev waits for all PR checks to finish before
starting a new CI auto-fix turn, so the agent gets the final failed checks and current
comments together. Updating an already queued auto-fix message does not use another round.
When this is at {round.max}/{round.max} and there is no pending auto-fix message left to
update, Kandev pauses auto-fix for this PR so it cannot loop forever. Disable and re-enable
auto-fix after manual review to start over.
);
if (!isFinePointer) {
return (
{trigger}
{explanation}
);
}
return (
{trigger}
{explanation}
);
}
export function PRCIAutomationControls({ pr }: { pr: TaskPR }) {
const { options, loading, saving, error, refresh, update, resetPrompt } =
useTaskCIAutomationOptions(pr.task_id);
const { toast } = useToast();
const [promptOpen, setPromptOpen] = useState(false);
const [promptDraft, setPromptDraft] = useState("");
const automationState = findCIAutomationStateForPR(options?.pr_states, pr);
const openPromptEditor = useCallback(() => {
setPromptDraft(options?.auto_fix_prompt_override ?? options?.effective_auto_fix_prompt ?? "");
setPromptOpen(true);
}, [options]);
const reportError = useCallback(
(description: string) => {
toast({ description, variant: "error" });
},
[toast],
);
const patchOption = useCallback(
(patch: TaskCIAutomationPatch) => {
Promise.resolve(update(patch)).catch(() => reportError("Failed to update CI automation."));
},
[reportError, update],
);
const savePrompt = useCallback(() => {
const value = promptDraft.trim();
if (!value) return;
Promise.resolve(update({ auto_fix_prompt_override: value }))
.then(() => setPromptOpen(false))
.catch(() => reportError("Failed to save auto-fix prompt."));
}, [promptDraft, reportError, update]);
const useDefaultPrompt = useCallback(() => {
Promise.resolve(resetPrompt())
.then(() => setPromptOpen(false))
.catch(() => reportError("Failed to reset auto-fix prompt."));
}, [reportError, resetPrompt]);
const retryLoad = useCallback(() => {
Promise.resolve(refresh()).catch(() => reportError("Failed to load CI automation."));
}, [refresh, reportError]);
const disabled = loading || saving || !options;
return (
patchOption({ auto_fix_enabled: checked })}
help={
options?.auto_fix_enabled ? (
) : null
}
/>
patchOption({ auto_merge_enabled: checked })}
/>
{error && }
setPromptOpen(false)}
onSave={savePrompt}
onReset={useDefaultPrompt}
/>
);
}